blob: e16bd22f33655070b8a550973e1d94c2a2885d02 [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"
John McCall92b7f702010-03-11 07:50:04 +000018#include "clang/AST/DeclFriend.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000019#include "clang/AST/DeclObjC.h"
20#include "clang/AST/DeclTemplate.h"
John McCall0c01d182010-03-24 05:22:00 +000021#include "clang/AST/DependentDiagnostic.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000022#include "clang/AST/ExternalASTSource.h"
Eli Friedman56d29372008-06-07 16:52:53 +000023#include "clang/AST/ASTContext.h"
Douglas Gregor44b43212008-12-11 16:49:14 +000024#include "clang/AST/Type.h"
Sebastian Redld3a413d2009-04-26 20:35:05 +000025#include "clang/AST/Stmt.h"
26#include "clang/AST/StmtCXX.h"
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +000027#include "clang/AST/ASTMutationListener.h"
Eli Friedman56d29372008-06-07 16:52:53 +000028#include "llvm/ADT/DenseMap.h"
Chris Lattner49f28ca2009-03-05 08:00:35 +000029#include "llvm/Support/raw_ostream.h"
Douglas Gregor6ed40e32008-12-23 21:05:05 +000030#include <algorithm>
Chris Lattner3daed522009-03-02 22:20:04 +000031#include <cstdio>
Douglas Gregor3fc749d2008-12-23 00:26:44 +000032#include <vector>
Eli Friedman56d29372008-06-07 16:52:53 +000033using namespace clang;
34
35//===----------------------------------------------------------------------===//
36// Statistics
37//===----------------------------------------------------------------------===//
38
Sean Hunt9a555912010-05-30 07:21:58 +000039#define DECL(DERIVED, BASE) static int n##DERIVED##s = 0;
40#define ABSTRACT_DECL(DECL)
41#include "clang/AST/DeclNodes.inc"
Eli Friedman56d29372008-06-07 16:52:53 +000042
43static bool StatSwitch = false;
44
Eli Friedman56d29372008-06-07 16:52:53 +000045const char *Decl::getDeclKindName() const {
46 switch (DeclKind) {
Sean Hunt9a555912010-05-30 07:21:58 +000047 default: assert(0 && "Declaration not in DeclNodes.inc!");
48#define DECL(DERIVED, BASE) case DERIVED: return #DERIVED;
49#define ABSTRACT_DECL(DECL)
50#include "clang/AST/DeclNodes.inc"
Eli Friedman56d29372008-06-07 16:52:53 +000051 }
52}
53
Douglas Gregor42738572010-03-05 00:26:45 +000054void Decl::setInvalidDecl(bool Invalid) {
55 InvalidDecl = Invalid;
56 if (Invalid) {
57 // Defensive maneuver for ill-formed code: we're likely not to make it to
58 // a point where we set the access specifier, so default it to "public"
59 // to avoid triggering asserts elsewhere in the front end.
60 setAccess(AS_public);
61 }
62}
63
Steve Naroff0a473932009-01-20 19:53:53 +000064const char *DeclContext::getDeclKindName() const {
65 switch (DeclKind) {
Sean Hunt9a555912010-05-30 07:21:58 +000066 default: assert(0 && "Declaration context not in DeclNodes.inc!");
67#define DECL(DERIVED, BASE) case Decl::DERIVED: return #DERIVED;
68#define ABSTRACT_DECL(DECL)
69#include "clang/AST/DeclNodes.inc"
Steve Naroff0a473932009-01-20 19:53:53 +000070 }
71}
72
Eli Friedman56d29372008-06-07 16:52:53 +000073bool Decl::CollectingStats(bool Enable) {
Kovarththanan Rajaratnam2024f4d2009-11-29 14:54:35 +000074 if (Enable) StatSwitch = true;
Eli Friedman56d29372008-06-07 16:52:53 +000075 return StatSwitch;
76}
77
78void Decl::PrintStats() {
79 fprintf(stderr, "*** Decl Stats:\n");
Mike Stump1eb44332009-09-09 15:08:12 +000080
Douglas Gregor64650af2009-02-02 23:39:07 +000081 int totalDecls = 0;
Sean Hunt9a555912010-05-30 07:21:58 +000082#define DECL(DERIVED, BASE) totalDecls += n##DERIVED##s;
83#define ABSTRACT_DECL(DECL)
84#include "clang/AST/DeclNodes.inc"
Douglas Gregor64650af2009-02-02 23:39:07 +000085 fprintf(stderr, " %d decls total.\n", totalDecls);
Mike Stump1eb44332009-09-09 15:08:12 +000086
Douglas Gregor64650af2009-02-02 23:39:07 +000087 int totalBytes = 0;
Sean Hunt9a555912010-05-30 07:21:58 +000088#define DECL(DERIVED, BASE) \
89 if (n##DERIVED##s > 0) { \
90 totalBytes += (int)(n##DERIVED##s * sizeof(DERIVED##Decl)); \
91 fprintf(stderr, " %d " #DERIVED " decls, %d each (%d bytes)\n", \
92 n##DERIVED##s, (int)sizeof(DERIVED##Decl), \
93 (int)(n##DERIVED##s * sizeof(DERIVED##Decl))); \
Douglas Gregor64650af2009-02-02 23:39:07 +000094 }
Sean Hunt9a555912010-05-30 07:21:58 +000095#define ABSTRACT_DECL(DECL)
96#include "clang/AST/DeclNodes.inc"
Mike Stump1eb44332009-09-09 15:08:12 +000097
Douglas Gregor64650af2009-02-02 23:39:07 +000098 fprintf(stderr, "Total bytes = %d\n", totalBytes);
Eli Friedman56d29372008-06-07 16:52:53 +000099}
100
Sean Hunt9a555912010-05-30 07:21:58 +0000101void Decl::add(Kind k) {
Eli Friedman56d29372008-06-07 16:52:53 +0000102 switch (k) {
Sean Hunt9a555912010-05-30 07:21:58 +0000103 default: assert(0 && "Declaration not in DeclNodes.inc!");
104#define DECL(DERIVED, BASE) case DERIVED: ++n##DERIVED##s; break;
105#define ABSTRACT_DECL(DECL)
106#include "clang/AST/DeclNodes.inc"
Eli Friedman56d29372008-06-07 16:52:53 +0000107 }
108}
109
Anders Carlsson67e33202009-06-13 00:08:58 +0000110bool Decl::isTemplateParameterPack() const {
111 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
112 return TTP->isParameterPack();
Douglas Gregor10738d32010-12-23 23:51:58 +0000113 if (const NonTypeTemplateParmDecl *NTTP
Douglas Gregor61c4d282011-01-05 15:48:55 +0000114 = dyn_cast<NonTypeTemplateParmDecl>(this))
Douglas Gregor10738d32010-12-23 23:51:58 +0000115 return NTTP->isParameterPack();
Douglas Gregor61c4d282011-01-05 15:48:55 +0000116 if (const TemplateTemplateParmDecl *TTP
117 = dyn_cast<TemplateTemplateParmDecl>(this))
118 return TTP->isParameterPack();
Anders Carlsson67e33202009-06-13 00:08:58 +0000119 return false;
120}
121
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000122bool Decl::isParameterPack() const {
123 if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(this))
124 return Parm->isParameterPack();
125
126 return isTemplateParameterPack();
127}
128
Douglas Gregore53060f2009-06-25 22:08:12 +0000129bool Decl::isFunctionOrFunctionTemplate() const {
John McCall9488ea12009-11-17 05:59:44 +0000130 if (const UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(this))
Anders Carlsson58badb72009-06-26 05:26:50 +0000131 return UD->getTargetDecl()->isFunctionOrFunctionTemplate();
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Douglas Gregore53060f2009-06-25 22:08:12 +0000133 return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this);
134}
135
Douglas Gregor79c22782010-01-16 20:21:20 +0000136bool Decl::isDefinedOutsideFunctionOrMethod() const {
137 for (const DeclContext *DC = getDeclContext();
138 DC && !DC->isTranslationUnit();
139 DC = DC->getParent())
140 if (DC->isFunctionOrMethod())
141 return false;
142
143 return true;
144}
145
146
Eli Friedman56d29372008-06-07 16:52:53 +0000147//===----------------------------------------------------------------------===//
Chris Lattner49f28ca2009-03-05 08:00:35 +0000148// PrettyStackTraceDecl Implementation
149//===----------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000150
Chris Lattner49f28ca2009-03-05 08:00:35 +0000151void PrettyStackTraceDecl::print(llvm::raw_ostream &OS) const {
152 SourceLocation TheLoc = Loc;
153 if (TheLoc.isInvalid() && TheDecl)
154 TheLoc = TheDecl->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000155
Chris Lattner49f28ca2009-03-05 08:00:35 +0000156 if (TheLoc.isValid()) {
157 TheLoc.print(OS, SM);
158 OS << ": ";
159 }
160
161 OS << Message;
162
Daniel Dunbarc5236562009-11-21 09:05:59 +0000163 if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
Chris Lattner49f28ca2009-03-05 08:00:35 +0000164 OS << " '" << DN->getQualifiedNameAsString() << '\'';
165 OS << '\n';
166}
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Chris Lattner49f28ca2009-03-05 08:00:35 +0000168//===----------------------------------------------------------------------===//
Eli Friedman56d29372008-06-07 16:52:53 +0000169// Decl Implementation
170//===----------------------------------------------------------------------===//
171
Chris Lattner769dbdf2009-03-27 20:18:19 +0000172// Out-of-line virtual method providing a home for Decl.
Douglas Gregorff331c12010-07-25 18:17:45 +0000173Decl::~Decl() { }
Chris Lattner769dbdf2009-03-27 20:18:19 +0000174
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000175void Decl::setDeclContext(DeclContext *DC) {
176 if (isOutOfSemaDC())
177 delete getMultipleDC();
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Chris Lattneree219fd2009-03-29 06:06:59 +0000179 DeclCtx = DC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000180}
181
182void Decl::setLexicalDeclContext(DeclContext *DC) {
183 if (DC == getLexicalDeclContext())
184 return;
185
186 if (isInSemaDC()) {
Ted Kremenek94a39002009-12-01 00:07:10 +0000187 MultipleDC *MDC = new (getASTContext()) MultipleDC();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000188 MDC->SemanticDC = getDeclContext();
189 MDC->LexicalDC = DC;
Chris Lattneree219fd2009-03-29 06:06:59 +0000190 DeclCtx = MDC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000191 } else {
192 getMultipleDC()->LexicalDC = DC;
193 }
194}
195
John McCall9aeed322009-10-01 00:25:31 +0000196bool Decl::isInAnonymousNamespace() const {
197 const DeclContext *DC = getDeclContext();
198 do {
199 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
200 if (ND->isAnonymousNamespace())
201 return true;
202 } while ((DC = DC->getParent()));
203
204 return false;
205}
206
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000207TranslationUnitDecl *Decl::getTranslationUnitDecl() {
Argyrios Kyrtzidis9b346692009-06-30 02:34:53 +0000208 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
209 return TUD;
210
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000211 DeclContext *DC = getDeclContext();
212 assert(DC && "This decl is not contained in a translation unit!");
Mike Stump1eb44332009-09-09 15:08:12 +0000213
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000214 while (!DC->isTranslationUnit()) {
215 DC = DC->getParent();
216 assert(DC && "This decl is not contained in a translation unit!");
217 }
Mike Stump1eb44332009-09-09 15:08:12 +0000218
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000219 return cast<TranslationUnitDecl>(DC);
220}
221
222ASTContext &Decl::getASTContext() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000223 return getTranslationUnitDecl()->getASTContext();
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000224}
225
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +0000226ASTMutationListener *Decl::getASTMutationListener() const {
227 return getASTContext().getASTMutationListener();
228}
229
Douglas Gregorc070cc62010-06-17 23:14:26 +0000230bool Decl::isUsed(bool CheckUsedAttr) const {
Tanya Lattner12ead492010-02-17 02:17:21 +0000231 if (Used)
232 return true;
233
234 // Check for used attribute.
Douglas Gregorc070cc62010-06-17 23:14:26 +0000235 if (CheckUsedAttr && hasAttr<UsedAttr>())
Tanya Lattner12ead492010-02-17 02:17:21 +0000236 return true;
237
238 // Check redeclarations for used attribute.
239 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Douglas Gregorc070cc62010-06-17 23:14:26 +0000240 if ((CheckUsedAttr && I->hasAttr<UsedAttr>()) || I->Used)
Tanya Lattner12ead492010-02-17 02:17:21 +0000241 return true;
242 }
243
244 return false;
245}
246
247
Chris Lattner769dbdf2009-03-27 20:18:19 +0000248unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
249 switch (DeclKind) {
John McCall9488ea12009-11-17 05:59:44 +0000250 case Function:
251 case CXXMethod:
252 case CXXConstructor:
253 case CXXDestructor:
254 case CXXConversion:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000255 case EnumConstant:
256 case Var:
257 case ImplicitParam:
258 case ParmVar:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000259 case NonTypeTemplateParm:
260 case ObjCMethod:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000261 case ObjCProperty:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000262 return IDNS_Ordinary;
John McCalld04efc92010-04-23 02:41:41 +0000263
Francois Pichet87c2e122010-11-21 06:08:52 +0000264 case IndirectField:
265 return IDNS_Ordinary | IDNS_Member;
266
John McCall0d6b1642010-04-23 18:46:30 +0000267 case ObjCCompatibleAlias:
268 case ObjCInterface:
269 return IDNS_Ordinary | IDNS_Type;
270
271 case Typedef:
272 case UnresolvedUsingTypename:
273 case TemplateTypeParm:
274 return IDNS_Ordinary | IDNS_Type;
275
John McCall9488ea12009-11-17 05:59:44 +0000276 case UsingShadow:
277 return 0; // we'll actually overwrite this later
278
John McCall7ba107a2009-11-18 02:36:19 +0000279 case UnresolvedUsingValue:
John McCall7ba107a2009-11-18 02:36:19 +0000280 return IDNS_Ordinary | IDNS_Using;
John McCall9488ea12009-11-17 05:59:44 +0000281
282 case Using:
283 return IDNS_Using;
284
Chris Lattner769dbdf2009-03-27 20:18:19 +0000285 case ObjCProtocol:
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000286 return IDNS_ObjCProtocol;
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Chris Lattner769dbdf2009-03-27 20:18:19 +0000288 case Field:
289 case ObjCAtDefsField:
290 case ObjCIvar:
291 return IDNS_Member;
Mike Stump1eb44332009-09-09 15:08:12 +0000292
Chris Lattner769dbdf2009-03-27 20:18:19 +0000293 case Record:
294 case CXXRecord:
295 case Enum:
John McCall0d6b1642010-04-23 18:46:30 +0000296 return IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000297
Chris Lattner769dbdf2009-03-27 20:18:19 +0000298 case Namespace:
John McCall0d6b1642010-04-23 18:46:30 +0000299 case NamespaceAlias:
300 return IDNS_Namespace;
301
Chris Lattner769dbdf2009-03-27 20:18:19 +0000302 case FunctionTemplate:
John McCall0d6b1642010-04-23 18:46:30 +0000303 return IDNS_Ordinary;
304
Chris Lattner769dbdf2009-03-27 20:18:19 +0000305 case ClassTemplate:
306 case TemplateTemplateParm:
John McCall0d6b1642010-04-23 18:46:30 +0000307 return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000308
Chris Lattner769dbdf2009-03-27 20:18:19 +0000309 // Never have names.
John McCall02cace72009-08-28 07:59:38 +0000310 case Friend:
John McCalldd4a3b02009-09-16 22:47:08 +0000311 case FriendTemplate:
Abramo Bagnara6206d532010-06-05 05:09:32 +0000312 case AccessSpec:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000313 case LinkageSpec:
314 case FileScopeAsm:
315 case StaticAssert:
316 case ObjCClass:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000317 case ObjCPropertyImpl:
318 case ObjCForwardProtocol:
319 case Block:
320 case TranslationUnit:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000321
Chris Lattner769dbdf2009-03-27 20:18:19 +0000322 case UsingDirective:
323 case ClassTemplateSpecialization:
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000324 case ClassTemplatePartialSpecialization:
Douglas Gregorbd4187b2010-04-22 23:19:50 +0000325 case ObjCImplementation:
326 case ObjCCategory:
327 case ObjCCategoryImpl:
328 // Never looked up by name.
Chris Lattner769dbdf2009-03-27 20:18:19 +0000329 return 0;
330 }
John McCall9488ea12009-11-17 05:59:44 +0000331
332 return 0;
Eli Friedman56d29372008-06-07 16:52:53 +0000333}
334
Sean Huntcf807c42010-08-18 23:23:40 +0000335void Decl::setAttrs(const AttrVec &attrs) {
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000336 assert(!HasAttrs && "Decl already contains attrs.");
337
Sean Huntcf807c42010-08-18 23:23:40 +0000338 AttrVec &AttrBlank = getASTContext().getDeclAttrs(this);
339 assert(AttrBlank.empty() && "HasAttrs was wrong?");
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000340
341 AttrBlank = attrs;
342 HasAttrs = true;
343}
344
Sean Huntcf807c42010-08-18 23:23:40 +0000345void Decl::dropAttrs() {
Eli Friedman56d29372008-06-07 16:52:53 +0000346 if (!HasAttrs) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Eli Friedman56d29372008-06-07 16:52:53 +0000348 HasAttrs = false;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000349 getASTContext().eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000350}
351
Sean Huntcf807c42010-08-18 23:23:40 +0000352const AttrVec &Decl::getAttrs() const {
353 assert(HasAttrs && "No attrs to get!");
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000354 return getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000355}
356
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000357void Decl::swapAttrs(Decl *RHS) {
Eli Friedman56d29372008-06-07 16:52:53 +0000358 bool HasLHSAttr = this->HasAttrs;
359 bool HasRHSAttr = RHS->HasAttrs;
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Eli Friedman56d29372008-06-07 16:52:53 +0000361 // Usually, neither decl has attrs, nothing to do.
362 if (!HasLHSAttr && !HasRHSAttr) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000363
Eli Friedman56d29372008-06-07 16:52:53 +0000364 // If 'this' has no attrs, swap the other way.
365 if (!HasLHSAttr)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000366 return RHS->swapAttrs(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000368 ASTContext &Context = getASTContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Eli Friedman56d29372008-06-07 16:52:53 +0000370 // Handle the case when both decls have attrs.
371 if (HasRHSAttr) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000372 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman56d29372008-06-07 16:52:53 +0000373 return;
374 }
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Eli Friedman56d29372008-06-07 16:52:53 +0000376 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor68584ed2009-06-18 16:11:24 +0000377 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
378 Context.eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000379 this->HasAttrs = false;
380 RHS->HasAttrs = true;
381}
382
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000383Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000384 Decl::Kind DK = D->getDeclKind();
385 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000386#define DECL(NAME, BASE)
387#define DECL_CONTEXT(NAME) \
388 case Decl::NAME: \
389 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
390#define DECL_CONTEXT_BASE(NAME)
391#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000392 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000393#define DECL(NAME, BASE)
394#define DECL_CONTEXT_BASE(NAME) \
395 if (DK >= first##NAME && DK <= last##NAME) \
396 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
397#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000398 assert(false && "a decl that inherits DeclContext isn't handled");
399 return 0;
400 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000401}
402
403DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000404 Decl::Kind DK = D->getKind();
405 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000406#define DECL(NAME, BASE)
407#define DECL_CONTEXT(NAME) \
408 case Decl::NAME: \
409 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
410#define DECL_CONTEXT_BASE(NAME)
411#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000412 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000413#define DECL(NAME, BASE)
414#define DECL_CONTEXT_BASE(NAME) \
415 if (DK >= first##NAME && DK <= last##NAME) \
416 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
417#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000418 assert(false && "a decl that inherits DeclContext isn't handled");
419 return 0;
420 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000421}
422
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000423SourceLocation Decl::getBodyRBrace() const {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000424 // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
425 // FunctionDecl stores EndRangeLoc for this purpose.
426 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
427 const FunctionDecl *Definition;
428 if (FD->hasBody(Definition))
429 return Definition->getSourceRange().getEnd();
430 return SourceLocation();
431 }
432
Argyrios Kyrtzidis6717ef42010-07-07 11:31:27 +0000433 if (Stmt *Body = getBody())
434 return Body->getSourceRange().getEnd();
435
436 return SourceLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +0000437}
438
Anders Carlsson1329c272009-03-25 23:38:06 +0000439void Decl::CheckAccessDeclContext() const {
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000440#ifndef NDEBUG
John McCall46460a62010-01-20 21:53:11 +0000441 // Suppress this check if any of the following hold:
442 // 1. this is the translation unit (and thus has no parent)
443 // 2. this is a template parameter (and thus doesn't belong to its context)
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000444 // 3. this is a non-type template parameter
445 // 4. the context is not a record
446 // 5. it's invalid
447 // 6. it's a C++0x static_assert.
Anders Carlsson35eda442009-08-29 20:47:47 +0000448 if (isa<TranslationUnitDecl>(this) ||
Argyrios Kyrtzidis04aed0e2010-07-02 11:55:44 +0000449 isa<TemplateTypeParmDecl>(this) ||
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000450 isa<NonTypeTemplateParmDecl>(this) ||
Douglas Gregorfdd8ab12010-02-22 17:53:38 +0000451 !isa<CXXRecordDecl>(getDeclContext()) ||
Argyrios Kyrtzidis65b63ec2010-09-08 21:32:35 +0000452 isInvalidDecl() ||
453 isa<StaticAssertDecl>(this) ||
454 // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
455 // as DeclContext (?).
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000456 isa<ParmVarDecl>(this) ||
457 // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
458 // AS_none as access specifier.
459 isa<CXXRecordDecl>(this))
Anders Carlsson35eda442009-08-29 20:47:47 +0000460 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000461
462 assert(Access != AS_none &&
Anders Carlsson1329c272009-03-25 23:38:06 +0000463 "Access specifier is AS_none inside a record decl");
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000464#endif
Anders Carlsson1329c272009-03-25 23:38:06 +0000465}
466
Anders Carlsson1329c272009-03-25 23:38:06 +0000467
Eli Friedman56d29372008-06-07 16:52:53 +0000468//===----------------------------------------------------------------------===//
469// DeclContext Implementation
470//===----------------------------------------------------------------------===//
471
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000472bool DeclContext::classof(const Decl *D) {
473 switch (D->getKind()) {
Sean Hunt9a555912010-05-30 07:21:58 +0000474#define DECL(NAME, BASE)
475#define DECL_CONTEXT(NAME) case Decl::NAME:
476#define DECL_CONTEXT_BASE(NAME)
477#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000478 return true;
479 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000480#define DECL(NAME, BASE)
481#define DECL_CONTEXT_BASE(NAME) \
482 if (D->getKind() >= Decl::first##NAME && \
483 D->getKind() <= Decl::last##NAME) \
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000484 return true;
Sean Hunt9a555912010-05-30 07:21:58 +0000485#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000486 return false;
487 }
488}
489
Douglas Gregora2da7802010-07-25 18:38:02 +0000490DeclContext::~DeclContext() { }
Douglas Gregor44b43212008-12-11 16:49:14 +0000491
Douglas Gregore942bbe2009-09-10 16:57:35 +0000492/// \brief Find the parent context of this context that will be
493/// used for unqualified name lookup.
494///
495/// Generally, the parent lookup context is the semantic context. However, for
496/// a friend function the parent lookup context is the lexical context, which
497/// is the class in which the friend is declared.
498DeclContext *DeclContext::getLookupParent() {
499 // FIXME: Find a better way to identify friends
500 if (isa<FunctionDecl>(this))
Sebastian Redl7a126a42010-08-31 00:36:30 +0000501 if (getParent()->getRedeclContext()->isFileContext() &&
502 getLexicalParent()->getRedeclContext()->isRecord())
Douglas Gregore942bbe2009-09-10 16:57:35 +0000503 return getLexicalParent();
504
505 return getParent();
506}
507
Sebastian Redl410c4f22010-08-31 20:53:31 +0000508bool DeclContext::isInlineNamespace() const {
509 return isNamespace() &&
510 cast<NamespaceDecl>(this)->isInline();
511}
512
Douglas Gregorbc221632009-05-28 16:34:51 +0000513bool DeclContext::isDependentContext() const {
514 if (isFileContext())
515 return false;
516
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000517 if (isa<ClassTemplatePartialSpecializationDecl>(this))
518 return true;
519
Douglas Gregorbc221632009-05-28 16:34:51 +0000520 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
521 if (Record->getDescribedClassTemplate())
522 return true;
523
John McCall0c01d182010-03-24 05:22:00 +0000524 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregorbc221632009-05-28 16:34:51 +0000525 if (Function->getDescribedFunctionTemplate())
526 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000527
John McCall0c01d182010-03-24 05:22:00 +0000528 // Friend function declarations are dependent if their *lexical*
529 // context is dependent.
530 if (cast<Decl>(this)->getFriendObjectKind())
531 return getLexicalParent()->isDependentContext();
532 }
533
Douglas Gregorbc221632009-05-28 16:34:51 +0000534 return getParent() && getParent()->isDependentContext();
535}
536
Douglas Gregor074149e2009-01-05 19:45:36 +0000537bool DeclContext::isTransparentContext() const {
538 if (DeclKind == Decl::Enum)
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000539 return !cast<EnumDecl>(this)->isScoped();
Douglas Gregor074149e2009-01-05 19:45:36 +0000540 else if (DeclKind == Decl::LinkageSpec)
541 return true;
Douglas Gregor074149e2009-01-05 19:45:36 +0000542
543 return false;
544}
545
John McCallac65c622010-10-26 04:59:26 +0000546bool DeclContext::isExternCContext() const {
547 const DeclContext *DC = this;
548 while (DC->DeclKind != Decl::TranslationUnit) {
549 if (DC->DeclKind == Decl::LinkageSpec)
550 return cast<LinkageSpecDecl>(DC)->getLanguage()
551 == LinkageSpecDecl::lang_c;
552 DC = DC->getParent();
553 }
554 return false;
555}
556
Sebastian Redl7a126a42010-08-31 00:36:30 +0000557bool DeclContext::Encloses(const DeclContext *DC) const {
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000558 if (getPrimaryContext() != this)
559 return getPrimaryContext()->Encloses(DC);
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000561 for (; DC; DC = DC->getParent())
562 if (DC->getPrimaryContext() == this)
563 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000564 return false;
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000565}
566
Steve Naroff0701bbb2009-01-08 17:28:14 +0000567DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000568 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000569 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000570 case Decl::LinkageSpec:
Mike Stump1eb44332009-09-09 15:08:12 +0000571 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000572 // There is only one DeclContext for these entities.
573 return this;
574
575 case Decl::Namespace:
576 // The original namespace is our primary context.
577 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
578
Douglas Gregor44b43212008-12-11 16:49:14 +0000579 case Decl::ObjCMethod:
580 return this;
581
582 case Decl::ObjCInterface:
Steve Naroff0701bbb2009-01-08 17:28:14 +0000583 case Decl::ObjCProtocol:
584 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000585 // FIXME: Can Objective-C interfaces be forward-declared?
586 return this;
587
Steve Naroff0701bbb2009-01-08 17:28:14 +0000588 case Decl::ObjCImplementation:
589 case Decl::ObjCCategoryImpl:
590 return this;
591
Douglas Gregor44b43212008-12-11 16:49:14 +0000592 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000593 if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000594 // If this is a tag type that has a definition or is currently
595 // being defined, that definition is our primary context.
John McCall3cb0ebd2010-03-10 03:28:59 +0000596 TagDecl *Tag = cast<TagDecl>(this);
597 assert(isa<TagType>(Tag->TypeForDecl) ||
598 isa<InjectedClassNameType>(Tag->TypeForDecl));
599
600 if (TagDecl *Def = Tag->getDefinition())
601 return Def;
602
603 if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) {
604 const TagType *TagTy = cast<TagType>(Tag->TypeForDecl);
605 if (TagTy->isBeingDefined())
606 // FIXME: is it necessarily being defined in the decl
607 // that owns the type?
608 return TagTy->getDecl();
609 }
610
611 return Tag;
Douglas Gregorcc636682009-02-17 23:15:12 +0000612 }
613
Sean Hunt9a555912010-05-30 07:21:58 +0000614 assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
Douglas Gregor44b43212008-12-11 16:49:14 +0000615 "Unknown DeclContext kind");
616 return this;
617 }
618}
619
620DeclContext *DeclContext::getNextContext() {
621 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000622 case Decl::Namespace:
623 // Return the next namespace
624 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
625
626 default:
Douglas Gregor44b43212008-12-11 16:49:14 +0000627 return 0;
628 }
629}
630
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000631std::pair<Decl *, Decl *>
632DeclContext::BuildDeclChain(const llvm::SmallVectorImpl<Decl*> &Decls) {
633 // Build up a chain of declarations via the Decl::NextDeclInContext field.
634 Decl *FirstNewDecl = 0;
635 Decl *PrevDecl = 0;
636 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
637 Decl *D = Decls[I];
638 if (PrevDecl)
639 PrevDecl->NextDeclInContext = D;
640 else
641 FirstNewDecl = D;
642
643 PrevDecl = D;
644 }
645
646 return std::make_pair(FirstNewDecl, PrevDecl);
647}
648
Douglas Gregor2cf26342009-04-09 22:27:44 +0000649/// \brief Load the declarations within this lexical storage from an
650/// external source.
Mike Stump1eb44332009-09-09 15:08:12 +0000651void
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000652DeclContext::LoadLexicalDeclsFromExternalStorage() const {
653 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000654 assert(hasExternalLexicalStorage() && Source && "No external storage?");
655
Argyrios Kyrtzidis0dbbc042010-07-30 10:03:23 +0000656 // Notify that we have a DeclContext that is initializing.
657 ExternalASTSource::Deserializing ADeclContext(Source);
658
John McCall76bd1f32010-06-01 09:23:16 +0000659 llvm::SmallVector<Decl*, 64> Decls;
660 if (Source->FindExternalLexicalDecls(this, Decls))
Douglas Gregor2cf26342009-04-09 22:27:44 +0000661 return;
662
663 // There is no longer any lexical storage in this context
664 ExternalLexicalStorage = false;
665
666 if (Decls.empty())
667 return;
668
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000669 // We may have already loaded just the fields of this record, in which case
670 // don't add the decls, just replace the FirstDecl/LastDecl chain.
671 if (const RecordDecl *RD = dyn_cast<RecordDecl>(this))
672 if (RD->LoadedFieldsFromExternalStorage) {
673 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
674 return;
675 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000676
677 // Splice the newly-read declarations into the beginning of the list
678 // of declarations.
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000679 Decl *ExternalFirst, *ExternalLast;
680 llvm::tie(ExternalFirst, ExternalLast) = BuildDeclChain(Decls);
681 ExternalLast->NextDeclInContext = FirstDecl;
682 FirstDecl = ExternalFirst;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000683 if (!LastDecl)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000684 LastDecl = ExternalLast;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000685}
686
John McCall76bd1f32010-06-01 09:23:16 +0000687DeclContext::lookup_result
688ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
689 DeclarationName Name) {
690 ASTContext &Context = DC->getParentASTContext();
691 StoredDeclsMap *Map;
692 if (!(Map = DC->LookupPtr))
693 Map = DC->CreateStoredDeclsMap(Context);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000694
John McCall76bd1f32010-06-01 09:23:16 +0000695 StoredDeclsList &List = (*Map)[Name];
696 assert(List.isNull());
697 (void) List;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000698
John McCall76bd1f32010-06-01 09:23:16 +0000699 return DeclContext::lookup_result();
700}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000701
John McCall76bd1f32010-06-01 09:23:16 +0000702DeclContext::lookup_result
703ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +0000704 DeclarationName Name,
705 llvm::SmallVectorImpl<NamedDecl*> &Decls) {
706 ASTContext &Context = DC->getParentASTContext();;
707
708 StoredDeclsMap *Map;
709 if (!(Map = DC->LookupPtr))
710 Map = DC->CreateStoredDeclsMap(Context);
711
712 StoredDeclsList &List = (*Map)[Name];
713 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
714 if (List.isNull())
715 List.setOnlyValue(Decls[I]);
716 else
717 List.AddSubsequentDecl(Decls[I]);
718 }
719
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000720 return List.getLookupResult();
John McCall76bd1f32010-06-01 09:23:16 +0000721}
722
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000723void ExternalASTSource::MaterializeVisibleDeclsForName(const DeclContext *DC,
724 DeclarationName Name,
725 llvm::SmallVectorImpl<NamedDecl*> &Decls) {
726 assert(DC->LookupPtr);
727 StoredDeclsMap &Map = *DC->LookupPtr;
728
729 // If there's an entry in the table the visible decls for this name have
730 // already been deserialized.
731 if (Map.find(Name) == Map.end()) {
732 StoredDeclsList &List = Map[Name];
733 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
734 if (List.isNull())
735 List.setOnlyValue(Decls[I]);
736 else
737 List.AddSubsequentDecl(Decls[I]);
738 }
739 }
740}
741
Sebastian Redl681d7232010-07-27 00:17:23 +0000742DeclContext::decl_iterator DeclContext::noload_decls_begin() const {
743 return decl_iterator(FirstDecl);
744}
745
746DeclContext::decl_iterator DeclContext::noload_decls_end() const {
747 return decl_iterator();
748}
749
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000750DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000751 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000752 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000753
754 // FIXME: Check whether we need to load some declarations from
755 // external storage.
Mike Stump1eb44332009-09-09 15:08:12 +0000756 return decl_iterator(FirstDecl);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000757}
758
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000759DeclContext::decl_iterator DeclContext::decls_end() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000760 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000761 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000762
Mike Stump1eb44332009-09-09 15:08:12 +0000763 return decl_iterator();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000764}
765
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000766bool DeclContext::decls_empty() const {
Douglas Gregor8038d512009-04-10 17:25:41 +0000767 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000768 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor8038d512009-04-10 17:25:41 +0000769
770 return !FirstDecl;
771}
772
John McCall9f54ad42009-12-10 09:41:52 +0000773void DeclContext::removeDecl(Decl *D) {
774 assert(D->getLexicalDeclContext() == this &&
775 "decl being removed from non-lexical context");
776 assert((D->NextDeclInContext || D == LastDecl) &&
777 "decl is not in decls list");
778
779 // Remove D from the decl chain. This is O(n) but hopefully rare.
780 if (D == FirstDecl) {
781 if (D == LastDecl)
782 FirstDecl = LastDecl = 0;
783 else
784 FirstDecl = D->NextDeclInContext;
785 } else {
786 for (Decl *I = FirstDecl; true; I = I->NextDeclInContext) {
787 assert(I && "decl not found in linked list");
788 if (I->NextDeclInContext == D) {
789 I->NextDeclInContext = D->NextDeclInContext;
790 if (D == LastDecl) LastDecl = I;
791 break;
792 }
793 }
794 }
795
796 // Mark that D is no longer in the decl chain.
797 D->NextDeclInContext = 0;
798
799 // Remove D from the lookup table if necessary.
800 if (isa<NamedDecl>(D)) {
801 NamedDecl *ND = cast<NamedDecl>(D);
802
John McCall0c01d182010-03-24 05:22:00 +0000803 StoredDeclsMap *Map = getPrimaryContext()->LookupPtr;
804 if (!Map) return;
John McCall9f54ad42009-12-10 09:41:52 +0000805
John McCall9f54ad42009-12-10 09:41:52 +0000806 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
807 assert(Pos != Map->end() && "no lookup entry for decl");
808 Pos->second.remove(ND);
809 }
810}
811
John McCall3f9a8a62009-08-11 06:59:38 +0000812void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner7f0be132009-02-20 00:56:18 +0000813 assert(D->getLexicalDeclContext() == this &&
814 "Decl inserted into wrong lexical context");
Mike Stump1eb44332009-09-09 15:08:12 +0000815 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000816 "Decl already inserted into a DeclContext");
817
818 if (FirstDecl) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000819 LastDecl->NextDeclInContext = D;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000820 LastDecl = D;
821 } else {
822 FirstDecl = LastDecl = D;
823 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000824
825 // Notify a C++ record declaration that we've added a member, so it can
826 // update it's class-specific state.
827 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
828 Record->addedMember(D);
John McCall3f9a8a62009-08-11 06:59:38 +0000829}
830
831void DeclContext::addDecl(Decl *D) {
832 addHiddenDecl(D);
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000833
834 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000835 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor44b43212008-12-11 16:49:14 +0000836}
837
Douglas Gregor074149e2009-01-05 19:45:36 +0000838/// buildLookup - Build the lookup data structure with all of the
839/// declarations in DCtx (and any other contexts linked to it or
840/// transparent contexts nested within it).
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000841void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000842 for (; DCtx; DCtx = DCtx->getNextContext()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000843 for (decl_iterator D = DCtx->decls_begin(),
844 DEnd = DCtx->decls_end();
Douglas Gregor4f3b8f82009-01-06 07:17:58 +0000845 D != DEnd; ++D) {
John McCall3f9a8a62009-08-11 06:59:38 +0000846 // Insert this declaration into the lookup structure, but only
847 // if it's semantically in its decl context. During non-lazy
848 // lookup building, this is implicitly enforced by addDecl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000849 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
John McCall3f9a8a62009-08-11 06:59:38 +0000850 if (D->getDeclContext() == DCtx)
851 makeDeclVisibleInContextImpl(ND);
Douglas Gregor074149e2009-01-05 19:45:36 +0000852
Ted Kremenekc32b1d82009-11-17 22:58:30 +0000853 // Insert any forward-declared Objective-C interfaces into the lookup
854 // data structure.
855 if (ObjCClassDecl *Class = dyn_cast<ObjCClassDecl>(*D))
856 for (ObjCClassDecl::iterator I = Class->begin(), IEnd = Class->end();
857 I != IEnd; ++I)
Ted Kremenek321c22f2009-11-18 00:28:11 +0000858 makeDeclVisibleInContextImpl(I->getInterface());
Ted Kremenekc32b1d82009-11-17 22:58:30 +0000859
Sebastian Redl410c4f22010-08-31 20:53:31 +0000860 // If this declaration is itself a transparent declaration context or
861 // inline namespace, add its members (recursively).
Douglas Gregor074149e2009-01-05 19:45:36 +0000862 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
Sebastian Redl410c4f22010-08-31 20:53:31 +0000863 if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000864 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregor074149e2009-01-05 19:45:36 +0000865 }
866 }
867}
868
Mike Stump1eb44332009-09-09 15:08:12 +0000869DeclContext::lookup_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000870DeclContext::lookup(DeclarationName Name) {
Steve Naroff0701bbb2009-01-08 17:28:14 +0000871 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000872 if (PrimaryContext != this)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000873 return PrimaryContext->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000874
John McCall76bd1f32010-06-01 09:23:16 +0000875 if (hasExternalVisibleStorage()) {
876 // Check to see if we've already cached the lookup results.
877 if (LookupPtr) {
878 StoredDeclsMap::iterator I = LookupPtr->find(Name);
879 if (I != LookupPtr->end())
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000880 return I->second.getLookupResult();
John McCall76bd1f32010-06-01 09:23:16 +0000881 }
882
883 ExternalASTSource *Source = getParentASTContext().getExternalSource();
884 return Source->FindExternalVisibleDeclsByName(this, Name);
885 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000886
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000887 /// If there is no lookup data structure, build one now by walking
Douglas Gregor44b43212008-12-11 16:49:14 +0000888 /// all of the linked DeclContexts (in declaration order!) and
889 /// inserting their values.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000890 if (!LookupPtr) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000891 buildLookup(this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000892
Douglas Gregorc36c5402009-04-09 17:29:08 +0000893 if (!LookupPtr)
Douglas Gregora5fdd9c2010-05-11 06:18:17 +0000894 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Douglas Gregorc36c5402009-04-09 17:29:08 +0000895 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000896
John McCall0c01d182010-03-24 05:22:00 +0000897 StoredDeclsMap::iterator Pos = LookupPtr->find(Name);
898 if (Pos == LookupPtr->end())
Douglas Gregora5fdd9c2010-05-11 06:18:17 +0000899 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000900 return Pos->second.getLookupResult();
Douglas Gregor44b43212008-12-11 16:49:14 +0000901}
902
Mike Stump1eb44332009-09-09 15:08:12 +0000903DeclContext::lookup_const_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000904DeclContext::lookup(DeclarationName Name) const {
905 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000906}
907
Sebastian Redl7a126a42010-08-31 00:36:30 +0000908DeclContext *DeclContext::getRedeclContext() {
Chris Lattner0cf2b192009-03-27 19:19:59 +0000909 DeclContext *Ctx = this;
Sebastian Redl410c4f22010-08-31 20:53:31 +0000910 // Skip through transparent contexts.
911 while (Ctx->isTransparentContext())
Douglas Gregorce356072009-01-06 23:51:29 +0000912 Ctx = Ctx->getParent();
913 return Ctx;
914}
915
Douglas Gregor88b70942009-02-25 22:02:03 +0000916DeclContext *DeclContext::getEnclosingNamespaceContext() {
917 DeclContext *Ctx = this;
918 // Skip through non-namespace, non-translation-unit contexts.
Sebastian Redl51a8a372010-08-31 00:36:23 +0000919 while (!Ctx->isFileContext())
Douglas Gregor88b70942009-02-25 22:02:03 +0000920 Ctx = Ctx->getParent();
921 return Ctx->getPrimaryContext();
922}
923
Sebastian Redl7a126a42010-08-31 00:36:30 +0000924bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
925 // For non-file contexts, this is equivalent to Equals.
926 if (!isFileContext())
927 return O->Equals(this);
928
929 do {
930 if (O->Equals(this))
931 return true;
932
933 const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O);
934 if (!NS || !NS->isInline())
935 break;
936 O = NS->getParent();
937 } while (O);
938
939 return false;
940}
941
John McCallab88d972009-08-31 22:39:49 +0000942void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000943 // FIXME: This feels like a hack. Should DeclarationName support
944 // template-ids, or is there a better way to keep specializations
945 // from being visible?
946 if (isa<ClassTemplateSpecializationDecl>(D))
947 return;
Eli Friedman6bc20132009-12-08 05:40:03 +0000948 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
949 if (FD->isFunctionTemplateSpecialization())
950 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000951
Steve Naroff0701bbb2009-01-08 17:28:14 +0000952 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000953 if (PrimaryContext != this) {
John McCallab88d972009-08-31 22:39:49 +0000954 PrimaryContext->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor44b43212008-12-11 16:49:14 +0000955 return;
956 }
957
958 // If we already have a lookup data structure, perform the insertion
Argyrios Kyrtzidis5586b012010-07-04 21:44:25 +0000959 // into it. If we haven't deserialized externally stored decls, deserialize
960 // them so we can add the decl. Otherwise, be lazy and don't build that
961 // structure until someone asks for it.
962 if (LookupPtr || !Recoverable || hasExternalVisibleStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000963 makeDeclVisibleInContextImpl(D);
Douglas Gregor074149e2009-01-05 19:45:36 +0000964
Sebastian Redl410c4f22010-08-31 20:53:31 +0000965 // If we are a transparent context or inline namespace, insert into our
966 // parent context, too. This operation is recursive.
967 if (isTransparentContext() || isInlineNamespace())
John McCallab88d972009-08-31 22:39:49 +0000968 getParent()->makeDeclVisibleInContext(D, Recoverable);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +0000969
970 Decl *DCAsDecl = cast<Decl>(this);
971 // Notify that a decl was made visible unless it's a Tag being defined.
972 if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined()))
973 if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
974 L->AddedVisibleDecl(this, D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000975}
976
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000977void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000978 // Skip unnamed declarations.
979 if (!D->getDeclName())
980 return;
981
Douglas Gregorcc636682009-02-17 23:15:12 +0000982 // FIXME: This feels like a hack. Should DeclarationName support
983 // template-ids, or is there a better way to keep specializations
984 // from being visible?
985 if (isa<ClassTemplateSpecializationDecl>(D))
986 return;
987
Argyrios Kyrtzidis5586b012010-07-04 21:44:25 +0000988 ASTContext *C = 0;
989 if (!LookupPtr) {
990 C = &getParentASTContext();
991 CreateStoredDeclsMap(*C);
992 }
993
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000994 // If there is an external AST source, load any declarations it knows about
995 // with this declaration's name.
996 // If the lookup table contains an entry about this name it means that we
997 // have already checked the external source.
998 if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
999 if (hasExternalVisibleStorage() &&
1000 LookupPtr->find(D->getDeclName()) == LookupPtr->end())
1001 Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
1002
Douglas Gregor44b43212008-12-11 16:49:14 +00001003 // Insert this declaration into the map.
John McCall0c01d182010-03-24 05:22:00 +00001004 StoredDeclsList &DeclNameEntries = (*LookupPtr)[D->getDeclName()];
Chris Lattner67762a32009-02-20 01:44:05 +00001005 if (DeclNameEntries.isNull()) {
1006 DeclNameEntries.setOnlyValue(D);
Chris Lattnerbd6c8002009-02-19 07:00:44 +00001007 return;
Douglas Gregor44b43212008-12-11 16:49:14 +00001008 }
Chris Lattner91942502009-02-20 00:55:03 +00001009
Chris Lattnerbdc3d002009-02-20 01:10:07 +00001010 // If it is possible that this is a redeclaration, check to see if there is
1011 // already a decl for which declarationReplaces returns true. If there is
1012 // one, just replace it and return.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001013 if (DeclNameEntries.HandleRedeclaration(D))
Chris Lattner67762a32009-02-20 01:44:05 +00001014 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001015
Chris Lattnerbd6c8002009-02-19 07:00:44 +00001016 // Put this declaration into the appropriate slot.
Chris Lattner67762a32009-02-20 01:44:05 +00001017 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor44b43212008-12-11 16:49:14 +00001018}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001019
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00001020void DeclContext::MaterializeVisibleDeclsFromExternalStorage() {
1021 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1022 assert(hasExternalVisibleStorage() && Source && "No external storage?");
1023
1024 if (!LookupPtr)
1025 CreateStoredDeclsMap(getParentASTContext());
1026 Source->MaterializeVisibleDecls(this);
1027}
1028
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001029/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
1030/// this context.
Mike Stump1eb44332009-09-09 15:08:12 +00001031DeclContext::udir_iterator_range
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001032DeclContext::getUsingDirectives() const {
1033 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001034 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
1035 reinterpret_cast<udir_iterator>(Result.second));
1036}
Douglas Gregor2cf26342009-04-09 22:27:44 +00001037
Ted Kremenek3478eb62010-02-11 07:12:28 +00001038//===----------------------------------------------------------------------===//
1039// Creation and Destruction of StoredDeclsMaps. //
1040//===----------------------------------------------------------------------===//
1041
John McCall0c01d182010-03-24 05:22:00 +00001042StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
1043 assert(!LookupPtr && "context already has a decls map");
1044 assert(getPrimaryContext() == this &&
1045 "creating decls map on non-primary context");
1046
1047 StoredDeclsMap *M;
1048 bool Dependent = isDependentContext();
1049 if (Dependent)
1050 M = new DependentStoredDeclsMap();
1051 else
1052 M = new StoredDeclsMap();
1053 M->Previous = C.LastSDM;
1054 C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
1055 LookupPtr = M;
Ted Kremenek3478eb62010-02-11 07:12:28 +00001056 return M;
1057}
1058
1059void ASTContext::ReleaseDeclContextMaps() {
John McCall0c01d182010-03-24 05:22:00 +00001060 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1061 // pointer because the subclass doesn't add anything that needs to
1062 // be deleted.
John McCall0c01d182010-03-24 05:22:00 +00001063 StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1064}
1065
1066void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1067 while (Map) {
1068 // Advance the iteration before we invalidate memory.
1069 llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1070
1071 if (Dependent)
1072 delete static_cast<DependentStoredDeclsMap*>(Map);
1073 else
1074 delete Map;
1075
1076 Map = Next.getPointer();
1077 Dependent = Next.getInt();
1078 }
1079}
1080
John McCall0c01d182010-03-24 05:22:00 +00001081DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1082 DeclContext *Parent,
1083 const PartialDiagnostic &PDiag) {
1084 assert(Parent->isDependentContext()
1085 && "cannot iterate dependent diagnostics of non-dependent context");
1086 Parent = Parent->getPrimaryContext();
1087 if (!Parent->LookupPtr)
1088 Parent->CreateStoredDeclsMap(C);
1089
1090 DependentStoredDeclsMap *Map
1091 = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr);
1092
Douglas Gregorb8365182010-03-29 23:56:53 +00001093 // Allocate the copy of the PartialDiagnostic via the ASTContext's
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00001094 // BumpPtrAllocator, rather than the ASTContext itself.
Douglas Gregorb8365182010-03-29 23:56:53 +00001095 PartialDiagnostic::Storage *DiagStorage = 0;
1096 if (PDiag.hasStorage())
1097 DiagStorage = new (C) PartialDiagnostic::Storage;
1098
1099 DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
John McCall0c01d182010-03-24 05:22:00 +00001100
1101 // TODO: Maybe we shouldn't reverse the order during insertion.
1102 DD->NextDiagnostic = Map->FirstDiagnostic;
1103 Map->FirstDiagnostic = DD;
1104
1105 return DD;
Ted Kremenek3478eb62010-02-11 07:12:28 +00001106}