blob: 75ea2df27e0d7e3dc113ee509e11b56b2c87bfe0 [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
114 = llvm::dyn_cast<NonTypeTemplateParmDecl>(this))
115 return NTTP->isParameterPack();
Anders Carlsson67e33202009-06-13 00:08:58 +0000116 return false;
117}
118
Douglas Gregore53060f2009-06-25 22:08:12 +0000119bool Decl::isFunctionOrFunctionTemplate() const {
John McCall9488ea12009-11-17 05:59:44 +0000120 if (const UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(this))
Anders Carlsson58badb72009-06-26 05:26:50 +0000121 return UD->getTargetDecl()->isFunctionOrFunctionTemplate();
Mike Stump1eb44332009-09-09 15:08:12 +0000122
Douglas Gregore53060f2009-06-25 22:08:12 +0000123 return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this);
124}
125
Douglas Gregor79c22782010-01-16 20:21:20 +0000126bool Decl::isDefinedOutsideFunctionOrMethod() const {
127 for (const DeclContext *DC = getDeclContext();
128 DC && !DC->isTranslationUnit();
129 DC = DC->getParent())
130 if (DC->isFunctionOrMethod())
131 return false;
132
133 return true;
134}
135
136
Eli Friedman56d29372008-06-07 16:52:53 +0000137//===----------------------------------------------------------------------===//
Chris Lattner49f28ca2009-03-05 08:00:35 +0000138// PrettyStackTraceDecl Implementation
139//===----------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000140
Chris Lattner49f28ca2009-03-05 08:00:35 +0000141void PrettyStackTraceDecl::print(llvm::raw_ostream &OS) const {
142 SourceLocation TheLoc = Loc;
143 if (TheLoc.isInvalid() && TheDecl)
144 TheLoc = TheDecl->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Chris Lattner49f28ca2009-03-05 08:00:35 +0000146 if (TheLoc.isValid()) {
147 TheLoc.print(OS, SM);
148 OS << ": ";
149 }
150
151 OS << Message;
152
Daniel Dunbarc5236562009-11-21 09:05:59 +0000153 if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
Chris Lattner49f28ca2009-03-05 08:00:35 +0000154 OS << " '" << DN->getQualifiedNameAsString() << '\'';
155 OS << '\n';
156}
Mike Stump1eb44332009-09-09 15:08:12 +0000157
Chris Lattner49f28ca2009-03-05 08:00:35 +0000158//===----------------------------------------------------------------------===//
Eli Friedman56d29372008-06-07 16:52:53 +0000159// Decl Implementation
160//===----------------------------------------------------------------------===//
161
Chris Lattner769dbdf2009-03-27 20:18:19 +0000162// Out-of-line virtual method providing a home for Decl.
Douglas Gregorff331c12010-07-25 18:17:45 +0000163Decl::~Decl() { }
Chris Lattner769dbdf2009-03-27 20:18:19 +0000164
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000165void Decl::setDeclContext(DeclContext *DC) {
166 if (isOutOfSemaDC())
167 delete getMultipleDC();
Mike Stump1eb44332009-09-09 15:08:12 +0000168
Chris Lattneree219fd2009-03-29 06:06:59 +0000169 DeclCtx = DC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000170}
171
172void Decl::setLexicalDeclContext(DeclContext *DC) {
173 if (DC == getLexicalDeclContext())
174 return;
175
176 if (isInSemaDC()) {
Ted Kremenek94a39002009-12-01 00:07:10 +0000177 MultipleDC *MDC = new (getASTContext()) MultipleDC();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000178 MDC->SemanticDC = getDeclContext();
179 MDC->LexicalDC = DC;
Chris Lattneree219fd2009-03-29 06:06:59 +0000180 DeclCtx = MDC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000181 } else {
182 getMultipleDC()->LexicalDC = DC;
183 }
184}
185
John McCall9aeed322009-10-01 00:25:31 +0000186bool Decl::isInAnonymousNamespace() const {
187 const DeclContext *DC = getDeclContext();
188 do {
189 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
190 if (ND->isAnonymousNamespace())
191 return true;
192 } while ((DC = DC->getParent()));
193
194 return false;
195}
196
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000197TranslationUnitDecl *Decl::getTranslationUnitDecl() {
Argyrios Kyrtzidis9b346692009-06-30 02:34:53 +0000198 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
199 return TUD;
200
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000201 DeclContext *DC = getDeclContext();
202 assert(DC && "This decl is not contained in a translation unit!");
Mike Stump1eb44332009-09-09 15:08:12 +0000203
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000204 while (!DC->isTranslationUnit()) {
205 DC = DC->getParent();
206 assert(DC && "This decl is not contained in a translation unit!");
207 }
Mike Stump1eb44332009-09-09 15:08:12 +0000208
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000209 return cast<TranslationUnitDecl>(DC);
210}
211
212ASTContext &Decl::getASTContext() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000213 return getTranslationUnitDecl()->getASTContext();
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000214}
215
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +0000216ASTMutationListener *Decl::getASTMutationListener() const {
217 return getASTContext().getASTMutationListener();
218}
219
Douglas Gregorc070cc62010-06-17 23:14:26 +0000220bool Decl::isUsed(bool CheckUsedAttr) const {
Tanya Lattner12ead492010-02-17 02:17:21 +0000221 if (Used)
222 return true;
223
224 // Check for used attribute.
Douglas Gregorc070cc62010-06-17 23:14:26 +0000225 if (CheckUsedAttr && hasAttr<UsedAttr>())
Tanya Lattner12ead492010-02-17 02:17:21 +0000226 return true;
227
228 // Check redeclarations for used attribute.
229 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Douglas Gregorc070cc62010-06-17 23:14:26 +0000230 if ((CheckUsedAttr && I->hasAttr<UsedAttr>()) || I->Used)
Tanya Lattner12ead492010-02-17 02:17:21 +0000231 return true;
232 }
233
234 return false;
235}
236
237
Chris Lattner769dbdf2009-03-27 20:18:19 +0000238unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
239 switch (DeclKind) {
John McCall9488ea12009-11-17 05:59:44 +0000240 case Function:
241 case CXXMethod:
242 case CXXConstructor:
243 case CXXDestructor:
244 case CXXConversion:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000245 case EnumConstant:
246 case Var:
247 case ImplicitParam:
248 case ParmVar:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000249 case NonTypeTemplateParm:
250 case ObjCMethod:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000251 case ObjCProperty:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000252 return IDNS_Ordinary;
John McCalld04efc92010-04-23 02:41:41 +0000253
Francois Pichet87c2e122010-11-21 06:08:52 +0000254 case IndirectField:
255 return IDNS_Ordinary | IDNS_Member;
256
John McCall0d6b1642010-04-23 18:46:30 +0000257 case ObjCCompatibleAlias:
258 case ObjCInterface:
259 return IDNS_Ordinary | IDNS_Type;
260
261 case Typedef:
262 case UnresolvedUsingTypename:
263 case TemplateTypeParm:
264 return IDNS_Ordinary | IDNS_Type;
265
John McCall9488ea12009-11-17 05:59:44 +0000266 case UsingShadow:
267 return 0; // we'll actually overwrite this later
268
John McCall7ba107a2009-11-18 02:36:19 +0000269 case UnresolvedUsingValue:
John McCall7ba107a2009-11-18 02:36:19 +0000270 return IDNS_Ordinary | IDNS_Using;
John McCall9488ea12009-11-17 05:59:44 +0000271
272 case Using:
273 return IDNS_Using;
274
Chris Lattner769dbdf2009-03-27 20:18:19 +0000275 case ObjCProtocol:
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000276 return IDNS_ObjCProtocol;
Mike Stump1eb44332009-09-09 15:08:12 +0000277
Chris Lattner769dbdf2009-03-27 20:18:19 +0000278 case Field:
279 case ObjCAtDefsField:
280 case ObjCIvar:
281 return IDNS_Member;
Mike Stump1eb44332009-09-09 15:08:12 +0000282
Chris Lattner769dbdf2009-03-27 20:18:19 +0000283 case Record:
284 case CXXRecord:
285 case Enum:
John McCall0d6b1642010-04-23 18:46:30 +0000286 return IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Chris Lattner769dbdf2009-03-27 20:18:19 +0000288 case Namespace:
John McCall0d6b1642010-04-23 18:46:30 +0000289 case NamespaceAlias:
290 return IDNS_Namespace;
291
Chris Lattner769dbdf2009-03-27 20:18:19 +0000292 case FunctionTemplate:
John McCall0d6b1642010-04-23 18:46:30 +0000293 return IDNS_Ordinary;
294
Chris Lattner769dbdf2009-03-27 20:18:19 +0000295 case ClassTemplate:
296 case TemplateTemplateParm:
John McCall0d6b1642010-04-23 18:46:30 +0000297 return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000298
Chris Lattner769dbdf2009-03-27 20:18:19 +0000299 // Never have names.
John McCall02cace72009-08-28 07:59:38 +0000300 case Friend:
John McCalldd4a3b02009-09-16 22:47:08 +0000301 case FriendTemplate:
Abramo Bagnara6206d532010-06-05 05:09:32 +0000302 case AccessSpec:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000303 case LinkageSpec:
304 case FileScopeAsm:
305 case StaticAssert:
306 case ObjCClass:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000307 case ObjCPropertyImpl:
308 case ObjCForwardProtocol:
309 case Block:
310 case TranslationUnit:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000311
Chris Lattner769dbdf2009-03-27 20:18:19 +0000312 case UsingDirective:
313 case ClassTemplateSpecialization:
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000314 case ClassTemplatePartialSpecialization:
Douglas Gregorbd4187b2010-04-22 23:19:50 +0000315 case ObjCImplementation:
316 case ObjCCategory:
317 case ObjCCategoryImpl:
318 // Never looked up by name.
Chris Lattner769dbdf2009-03-27 20:18:19 +0000319 return 0;
320 }
John McCall9488ea12009-11-17 05:59:44 +0000321
322 return 0;
Eli Friedman56d29372008-06-07 16:52:53 +0000323}
324
Sean Huntcf807c42010-08-18 23:23:40 +0000325void Decl::setAttrs(const AttrVec &attrs) {
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000326 assert(!HasAttrs && "Decl already contains attrs.");
327
Sean Huntcf807c42010-08-18 23:23:40 +0000328 AttrVec &AttrBlank = getASTContext().getDeclAttrs(this);
329 assert(AttrBlank.empty() && "HasAttrs was wrong?");
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000330
331 AttrBlank = attrs;
332 HasAttrs = true;
333}
334
Sean Huntcf807c42010-08-18 23:23:40 +0000335void Decl::dropAttrs() {
Eli Friedman56d29372008-06-07 16:52:53 +0000336 if (!HasAttrs) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000337
Eli Friedman56d29372008-06-07 16:52:53 +0000338 HasAttrs = false;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000339 getASTContext().eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000340}
341
Sean Huntcf807c42010-08-18 23:23:40 +0000342const AttrVec &Decl::getAttrs() const {
343 assert(HasAttrs && "No attrs to get!");
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000344 return getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000345}
346
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000347void Decl::swapAttrs(Decl *RHS) {
Eli Friedman56d29372008-06-07 16:52:53 +0000348 bool HasLHSAttr = this->HasAttrs;
349 bool HasRHSAttr = RHS->HasAttrs;
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Eli Friedman56d29372008-06-07 16:52:53 +0000351 // Usually, neither decl has attrs, nothing to do.
352 if (!HasLHSAttr && !HasRHSAttr) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Eli Friedman56d29372008-06-07 16:52:53 +0000354 // If 'this' has no attrs, swap the other way.
355 if (!HasLHSAttr)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000356 return RHS->swapAttrs(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000358 ASTContext &Context = getASTContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Eli Friedman56d29372008-06-07 16:52:53 +0000360 // Handle the case when both decls have attrs.
361 if (HasRHSAttr) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000362 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman56d29372008-06-07 16:52:53 +0000363 return;
364 }
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Eli Friedman56d29372008-06-07 16:52:53 +0000366 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor68584ed2009-06-18 16:11:24 +0000367 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
368 Context.eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000369 this->HasAttrs = false;
370 RHS->HasAttrs = true;
371}
372
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000373Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000374 Decl::Kind DK = D->getDeclKind();
375 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000376#define DECL(NAME, BASE)
377#define DECL_CONTEXT(NAME) \
378 case Decl::NAME: \
379 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
380#define DECL_CONTEXT_BASE(NAME)
381#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000382 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000383#define DECL(NAME, BASE)
384#define DECL_CONTEXT_BASE(NAME) \
385 if (DK >= first##NAME && DK <= last##NAME) \
386 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
387#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000388 assert(false && "a decl that inherits DeclContext isn't handled");
389 return 0;
390 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000391}
392
393DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000394 Decl::Kind DK = D->getKind();
395 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000396#define DECL(NAME, BASE)
397#define DECL_CONTEXT(NAME) \
398 case Decl::NAME: \
399 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
400#define DECL_CONTEXT_BASE(NAME)
401#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000402 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000403#define DECL(NAME, BASE)
404#define DECL_CONTEXT_BASE(NAME) \
405 if (DK >= first##NAME && DK <= last##NAME) \
406 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
407#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000408 assert(false && "a decl that inherits DeclContext isn't handled");
409 return 0;
410 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000411}
412
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000413SourceLocation Decl::getBodyRBrace() const {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000414 // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
415 // FunctionDecl stores EndRangeLoc for this purpose.
416 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
417 const FunctionDecl *Definition;
418 if (FD->hasBody(Definition))
419 return Definition->getSourceRange().getEnd();
420 return SourceLocation();
421 }
422
Argyrios Kyrtzidis6717ef42010-07-07 11:31:27 +0000423 if (Stmt *Body = getBody())
424 return Body->getSourceRange().getEnd();
425
426 return SourceLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +0000427}
428
Anders Carlsson1329c272009-03-25 23:38:06 +0000429void Decl::CheckAccessDeclContext() const {
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000430#ifndef NDEBUG
John McCall46460a62010-01-20 21:53:11 +0000431 // Suppress this check if any of the following hold:
432 // 1. this is the translation unit (and thus has no parent)
433 // 2. this is a template parameter (and thus doesn't belong to its context)
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000434 // 3. this is a non-type template parameter
435 // 4. the context is not a record
436 // 5. it's invalid
437 // 6. it's a C++0x static_assert.
Anders Carlsson35eda442009-08-29 20:47:47 +0000438 if (isa<TranslationUnitDecl>(this) ||
Argyrios Kyrtzidis04aed0e2010-07-02 11:55:44 +0000439 isa<TemplateTypeParmDecl>(this) ||
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000440 isa<NonTypeTemplateParmDecl>(this) ||
Douglas Gregorfdd8ab12010-02-22 17:53:38 +0000441 !isa<CXXRecordDecl>(getDeclContext()) ||
Argyrios Kyrtzidis65b63ec2010-09-08 21:32:35 +0000442 isInvalidDecl() ||
443 isa<StaticAssertDecl>(this) ||
444 // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
445 // as DeclContext (?).
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000446 isa<ParmVarDecl>(this) ||
447 // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
448 // AS_none as access specifier.
449 isa<CXXRecordDecl>(this))
Anders Carlsson35eda442009-08-29 20:47:47 +0000450 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000451
452 assert(Access != AS_none &&
Anders Carlsson1329c272009-03-25 23:38:06 +0000453 "Access specifier is AS_none inside a record decl");
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000454#endif
Anders Carlsson1329c272009-03-25 23:38:06 +0000455}
456
Anders Carlsson1329c272009-03-25 23:38:06 +0000457
Eli Friedman56d29372008-06-07 16:52:53 +0000458//===----------------------------------------------------------------------===//
459// DeclContext Implementation
460//===----------------------------------------------------------------------===//
461
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000462bool DeclContext::classof(const Decl *D) {
463 switch (D->getKind()) {
Sean Hunt9a555912010-05-30 07:21:58 +0000464#define DECL(NAME, BASE)
465#define DECL_CONTEXT(NAME) case Decl::NAME:
466#define DECL_CONTEXT_BASE(NAME)
467#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000468 return true;
469 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000470#define DECL(NAME, BASE)
471#define DECL_CONTEXT_BASE(NAME) \
472 if (D->getKind() >= Decl::first##NAME && \
473 D->getKind() <= Decl::last##NAME) \
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000474 return true;
Sean Hunt9a555912010-05-30 07:21:58 +0000475#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000476 return false;
477 }
478}
479
Douglas Gregora2da7802010-07-25 18:38:02 +0000480DeclContext::~DeclContext() { }
Douglas Gregor44b43212008-12-11 16:49:14 +0000481
Douglas Gregore942bbe2009-09-10 16:57:35 +0000482/// \brief Find the parent context of this context that will be
483/// used for unqualified name lookup.
484///
485/// Generally, the parent lookup context is the semantic context. However, for
486/// a friend function the parent lookup context is the lexical context, which
487/// is the class in which the friend is declared.
488DeclContext *DeclContext::getLookupParent() {
489 // FIXME: Find a better way to identify friends
490 if (isa<FunctionDecl>(this))
Sebastian Redl7a126a42010-08-31 00:36:30 +0000491 if (getParent()->getRedeclContext()->isFileContext() &&
492 getLexicalParent()->getRedeclContext()->isRecord())
Douglas Gregore942bbe2009-09-10 16:57:35 +0000493 return getLexicalParent();
494
495 return getParent();
496}
497
Sebastian Redl410c4f22010-08-31 20:53:31 +0000498bool DeclContext::isInlineNamespace() const {
499 return isNamespace() &&
500 cast<NamespaceDecl>(this)->isInline();
501}
502
Douglas Gregorbc221632009-05-28 16:34:51 +0000503bool DeclContext::isDependentContext() const {
504 if (isFileContext())
505 return false;
506
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000507 if (isa<ClassTemplatePartialSpecializationDecl>(this))
508 return true;
509
Douglas Gregorbc221632009-05-28 16:34:51 +0000510 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
511 if (Record->getDescribedClassTemplate())
512 return true;
513
John McCall0c01d182010-03-24 05:22:00 +0000514 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregorbc221632009-05-28 16:34:51 +0000515 if (Function->getDescribedFunctionTemplate())
516 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000517
John McCall0c01d182010-03-24 05:22:00 +0000518 // Friend function declarations are dependent if their *lexical*
519 // context is dependent.
520 if (cast<Decl>(this)->getFriendObjectKind())
521 return getLexicalParent()->isDependentContext();
522 }
523
Douglas Gregorbc221632009-05-28 16:34:51 +0000524 return getParent() && getParent()->isDependentContext();
525}
526
Douglas Gregor074149e2009-01-05 19:45:36 +0000527bool DeclContext::isTransparentContext() const {
528 if (DeclKind == Decl::Enum)
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000529 return !cast<EnumDecl>(this)->isScoped();
Douglas Gregor074149e2009-01-05 19:45:36 +0000530 else if (DeclKind == Decl::LinkageSpec)
531 return true;
Douglas Gregor074149e2009-01-05 19:45:36 +0000532
533 return false;
534}
535
John McCallac65c622010-10-26 04:59:26 +0000536bool DeclContext::isExternCContext() const {
537 const DeclContext *DC = this;
538 while (DC->DeclKind != Decl::TranslationUnit) {
539 if (DC->DeclKind == Decl::LinkageSpec)
540 return cast<LinkageSpecDecl>(DC)->getLanguage()
541 == LinkageSpecDecl::lang_c;
542 DC = DC->getParent();
543 }
544 return false;
545}
546
Sebastian Redl7a126a42010-08-31 00:36:30 +0000547bool DeclContext::Encloses(const DeclContext *DC) const {
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000548 if (getPrimaryContext() != this)
549 return getPrimaryContext()->Encloses(DC);
Mike Stump1eb44332009-09-09 15:08:12 +0000550
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000551 for (; DC; DC = DC->getParent())
552 if (DC->getPrimaryContext() == this)
553 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000554 return false;
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000555}
556
Steve Naroff0701bbb2009-01-08 17:28:14 +0000557DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000558 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000559 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000560 case Decl::LinkageSpec:
Mike Stump1eb44332009-09-09 15:08:12 +0000561 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000562 // There is only one DeclContext for these entities.
563 return this;
564
565 case Decl::Namespace:
566 // The original namespace is our primary context.
567 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
568
Douglas Gregor44b43212008-12-11 16:49:14 +0000569 case Decl::ObjCMethod:
570 return this;
571
572 case Decl::ObjCInterface:
Steve Naroff0701bbb2009-01-08 17:28:14 +0000573 case Decl::ObjCProtocol:
574 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000575 // FIXME: Can Objective-C interfaces be forward-declared?
576 return this;
577
Steve Naroff0701bbb2009-01-08 17:28:14 +0000578 case Decl::ObjCImplementation:
579 case Decl::ObjCCategoryImpl:
580 return this;
581
Douglas Gregor44b43212008-12-11 16:49:14 +0000582 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000583 if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000584 // If this is a tag type that has a definition or is currently
585 // being defined, that definition is our primary context.
John McCall3cb0ebd2010-03-10 03:28:59 +0000586 TagDecl *Tag = cast<TagDecl>(this);
587 assert(isa<TagType>(Tag->TypeForDecl) ||
588 isa<InjectedClassNameType>(Tag->TypeForDecl));
589
590 if (TagDecl *Def = Tag->getDefinition())
591 return Def;
592
593 if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) {
594 const TagType *TagTy = cast<TagType>(Tag->TypeForDecl);
595 if (TagTy->isBeingDefined())
596 // FIXME: is it necessarily being defined in the decl
597 // that owns the type?
598 return TagTy->getDecl();
599 }
600
601 return Tag;
Douglas Gregorcc636682009-02-17 23:15:12 +0000602 }
603
Sean Hunt9a555912010-05-30 07:21:58 +0000604 assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
Douglas Gregor44b43212008-12-11 16:49:14 +0000605 "Unknown DeclContext kind");
606 return this;
607 }
608}
609
610DeclContext *DeclContext::getNextContext() {
611 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000612 case Decl::Namespace:
613 // Return the next namespace
614 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
615
616 default:
Douglas Gregor44b43212008-12-11 16:49:14 +0000617 return 0;
618 }
619}
620
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000621std::pair<Decl *, Decl *>
622DeclContext::BuildDeclChain(const llvm::SmallVectorImpl<Decl*> &Decls) {
623 // Build up a chain of declarations via the Decl::NextDeclInContext field.
624 Decl *FirstNewDecl = 0;
625 Decl *PrevDecl = 0;
626 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
627 Decl *D = Decls[I];
628 if (PrevDecl)
629 PrevDecl->NextDeclInContext = D;
630 else
631 FirstNewDecl = D;
632
633 PrevDecl = D;
634 }
635
636 return std::make_pair(FirstNewDecl, PrevDecl);
637}
638
Douglas Gregor2cf26342009-04-09 22:27:44 +0000639/// \brief Load the declarations within this lexical storage from an
640/// external source.
Mike Stump1eb44332009-09-09 15:08:12 +0000641void
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000642DeclContext::LoadLexicalDeclsFromExternalStorage() const {
643 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000644 assert(hasExternalLexicalStorage() && Source && "No external storage?");
645
Argyrios Kyrtzidis0dbbc042010-07-30 10:03:23 +0000646 // Notify that we have a DeclContext that is initializing.
647 ExternalASTSource::Deserializing ADeclContext(Source);
648
John McCall76bd1f32010-06-01 09:23:16 +0000649 llvm::SmallVector<Decl*, 64> Decls;
650 if (Source->FindExternalLexicalDecls(this, Decls))
Douglas Gregor2cf26342009-04-09 22:27:44 +0000651 return;
652
653 // There is no longer any lexical storage in this context
654 ExternalLexicalStorage = false;
655
656 if (Decls.empty())
657 return;
658
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000659 // We may have already loaded just the fields of this record, in which case
660 // don't add the decls, just replace the FirstDecl/LastDecl chain.
661 if (const RecordDecl *RD = dyn_cast<RecordDecl>(this))
662 if (RD->LoadedFieldsFromExternalStorage) {
663 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
664 return;
665 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000666
667 // Splice the newly-read declarations into the beginning of the list
668 // of declarations.
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000669 Decl *ExternalFirst, *ExternalLast;
670 llvm::tie(ExternalFirst, ExternalLast) = BuildDeclChain(Decls);
671 ExternalLast->NextDeclInContext = FirstDecl;
672 FirstDecl = ExternalFirst;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000673 if (!LastDecl)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000674 LastDecl = ExternalLast;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000675}
676
John McCall76bd1f32010-06-01 09:23:16 +0000677DeclContext::lookup_result
678ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
679 DeclarationName Name) {
680 ASTContext &Context = DC->getParentASTContext();
681 StoredDeclsMap *Map;
682 if (!(Map = DC->LookupPtr))
683 Map = DC->CreateStoredDeclsMap(Context);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000684
John McCall76bd1f32010-06-01 09:23:16 +0000685 StoredDeclsList &List = (*Map)[Name];
686 assert(List.isNull());
687 (void) List;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000688
John McCall76bd1f32010-06-01 09:23:16 +0000689 return DeclContext::lookup_result();
690}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000691
John McCall76bd1f32010-06-01 09:23:16 +0000692DeclContext::lookup_result
693ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +0000694 DeclarationName Name,
695 llvm::SmallVectorImpl<NamedDecl*> &Decls) {
696 ASTContext &Context = DC->getParentASTContext();;
697
698 StoredDeclsMap *Map;
699 if (!(Map = DC->LookupPtr))
700 Map = DC->CreateStoredDeclsMap(Context);
701
702 StoredDeclsList &List = (*Map)[Name];
703 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
704 if (List.isNull())
705 List.setOnlyValue(Decls[I]);
706 else
707 List.AddSubsequentDecl(Decls[I]);
708 }
709
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000710 return List.getLookupResult();
John McCall76bd1f32010-06-01 09:23:16 +0000711}
712
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000713void ExternalASTSource::MaterializeVisibleDeclsForName(const DeclContext *DC,
714 DeclarationName Name,
715 llvm::SmallVectorImpl<NamedDecl*> &Decls) {
716 assert(DC->LookupPtr);
717 StoredDeclsMap &Map = *DC->LookupPtr;
718
719 // If there's an entry in the table the visible decls for this name have
720 // already been deserialized.
721 if (Map.find(Name) == Map.end()) {
722 StoredDeclsList &List = Map[Name];
723 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
724 if (List.isNull())
725 List.setOnlyValue(Decls[I]);
726 else
727 List.AddSubsequentDecl(Decls[I]);
728 }
729 }
730}
731
Sebastian Redl681d7232010-07-27 00:17:23 +0000732DeclContext::decl_iterator DeclContext::noload_decls_begin() const {
733 return decl_iterator(FirstDecl);
734}
735
736DeclContext::decl_iterator DeclContext::noload_decls_end() const {
737 return decl_iterator();
738}
739
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000740DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000741 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000742 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000743
744 // FIXME: Check whether we need to load some declarations from
745 // external storage.
Mike Stump1eb44332009-09-09 15:08:12 +0000746 return decl_iterator(FirstDecl);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000747}
748
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000749DeclContext::decl_iterator DeclContext::decls_end() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000750 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000751 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000752
Mike Stump1eb44332009-09-09 15:08:12 +0000753 return decl_iterator();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000754}
755
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000756bool DeclContext::decls_empty() const {
Douglas Gregor8038d512009-04-10 17:25:41 +0000757 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000758 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor8038d512009-04-10 17:25:41 +0000759
760 return !FirstDecl;
761}
762
John McCall9f54ad42009-12-10 09:41:52 +0000763void DeclContext::removeDecl(Decl *D) {
764 assert(D->getLexicalDeclContext() == this &&
765 "decl being removed from non-lexical context");
766 assert((D->NextDeclInContext || D == LastDecl) &&
767 "decl is not in decls list");
768
769 // Remove D from the decl chain. This is O(n) but hopefully rare.
770 if (D == FirstDecl) {
771 if (D == LastDecl)
772 FirstDecl = LastDecl = 0;
773 else
774 FirstDecl = D->NextDeclInContext;
775 } else {
776 for (Decl *I = FirstDecl; true; I = I->NextDeclInContext) {
777 assert(I && "decl not found in linked list");
778 if (I->NextDeclInContext == D) {
779 I->NextDeclInContext = D->NextDeclInContext;
780 if (D == LastDecl) LastDecl = I;
781 break;
782 }
783 }
784 }
785
786 // Mark that D is no longer in the decl chain.
787 D->NextDeclInContext = 0;
788
789 // Remove D from the lookup table if necessary.
790 if (isa<NamedDecl>(D)) {
791 NamedDecl *ND = cast<NamedDecl>(D);
792
John McCall0c01d182010-03-24 05:22:00 +0000793 StoredDeclsMap *Map = getPrimaryContext()->LookupPtr;
794 if (!Map) return;
John McCall9f54ad42009-12-10 09:41:52 +0000795
John McCall9f54ad42009-12-10 09:41:52 +0000796 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
797 assert(Pos != Map->end() && "no lookup entry for decl");
798 Pos->second.remove(ND);
799 }
800}
801
John McCall3f9a8a62009-08-11 06:59:38 +0000802void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner7f0be132009-02-20 00:56:18 +0000803 assert(D->getLexicalDeclContext() == this &&
804 "Decl inserted into wrong lexical context");
Mike Stump1eb44332009-09-09 15:08:12 +0000805 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000806 "Decl already inserted into a DeclContext");
807
808 if (FirstDecl) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000809 LastDecl->NextDeclInContext = D;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000810 LastDecl = D;
811 } else {
812 FirstDecl = LastDecl = D;
813 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000814
815 // Notify a C++ record declaration that we've added a member, so it can
816 // update it's class-specific state.
817 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
818 Record->addedMember(D);
John McCall3f9a8a62009-08-11 06:59:38 +0000819}
820
821void DeclContext::addDecl(Decl *D) {
822 addHiddenDecl(D);
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000823
824 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000825 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor44b43212008-12-11 16:49:14 +0000826}
827
Douglas Gregor074149e2009-01-05 19:45:36 +0000828/// buildLookup - Build the lookup data structure with all of the
829/// declarations in DCtx (and any other contexts linked to it or
830/// transparent contexts nested within it).
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000831void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000832 for (; DCtx; DCtx = DCtx->getNextContext()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000833 for (decl_iterator D = DCtx->decls_begin(),
834 DEnd = DCtx->decls_end();
Douglas Gregor4f3b8f82009-01-06 07:17:58 +0000835 D != DEnd; ++D) {
John McCall3f9a8a62009-08-11 06:59:38 +0000836 // Insert this declaration into the lookup structure, but only
837 // if it's semantically in its decl context. During non-lazy
838 // lookup building, this is implicitly enforced by addDecl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000839 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
John McCall3f9a8a62009-08-11 06:59:38 +0000840 if (D->getDeclContext() == DCtx)
841 makeDeclVisibleInContextImpl(ND);
Douglas Gregor074149e2009-01-05 19:45:36 +0000842
Ted Kremenekc32b1d82009-11-17 22:58:30 +0000843 // Insert any forward-declared Objective-C interfaces into the lookup
844 // data structure.
845 if (ObjCClassDecl *Class = dyn_cast<ObjCClassDecl>(*D))
846 for (ObjCClassDecl::iterator I = Class->begin(), IEnd = Class->end();
847 I != IEnd; ++I)
Ted Kremenek321c22f2009-11-18 00:28:11 +0000848 makeDeclVisibleInContextImpl(I->getInterface());
Ted Kremenekc32b1d82009-11-17 22:58:30 +0000849
Sebastian Redl410c4f22010-08-31 20:53:31 +0000850 // If this declaration is itself a transparent declaration context or
851 // inline namespace, add its members (recursively).
Douglas Gregor074149e2009-01-05 19:45:36 +0000852 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
Sebastian Redl410c4f22010-08-31 20:53:31 +0000853 if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000854 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregor074149e2009-01-05 19:45:36 +0000855 }
856 }
857}
858
Mike Stump1eb44332009-09-09 15:08:12 +0000859DeclContext::lookup_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000860DeclContext::lookup(DeclarationName Name) {
Steve Naroff0701bbb2009-01-08 17:28:14 +0000861 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000862 if (PrimaryContext != this)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000863 return PrimaryContext->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000864
John McCall76bd1f32010-06-01 09:23:16 +0000865 if (hasExternalVisibleStorage()) {
866 // Check to see if we've already cached the lookup results.
867 if (LookupPtr) {
868 StoredDeclsMap::iterator I = LookupPtr->find(Name);
869 if (I != LookupPtr->end())
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000870 return I->second.getLookupResult();
John McCall76bd1f32010-06-01 09:23:16 +0000871 }
872
873 ExternalASTSource *Source = getParentASTContext().getExternalSource();
874 return Source->FindExternalVisibleDeclsByName(this, Name);
875 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000876
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000877 /// If there is no lookup data structure, build one now by walking
Douglas Gregor44b43212008-12-11 16:49:14 +0000878 /// all of the linked DeclContexts (in declaration order!) and
879 /// inserting their values.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000880 if (!LookupPtr) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000881 buildLookup(this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000882
Douglas Gregorc36c5402009-04-09 17:29:08 +0000883 if (!LookupPtr)
Douglas Gregora5fdd9c2010-05-11 06:18:17 +0000884 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Douglas Gregorc36c5402009-04-09 17:29:08 +0000885 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000886
John McCall0c01d182010-03-24 05:22:00 +0000887 StoredDeclsMap::iterator Pos = LookupPtr->find(Name);
888 if (Pos == LookupPtr->end())
Douglas Gregora5fdd9c2010-05-11 06:18:17 +0000889 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000890 return Pos->second.getLookupResult();
Douglas Gregor44b43212008-12-11 16:49:14 +0000891}
892
Mike Stump1eb44332009-09-09 15:08:12 +0000893DeclContext::lookup_const_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000894DeclContext::lookup(DeclarationName Name) const {
895 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000896}
897
Sebastian Redl7a126a42010-08-31 00:36:30 +0000898DeclContext *DeclContext::getRedeclContext() {
Chris Lattner0cf2b192009-03-27 19:19:59 +0000899 DeclContext *Ctx = this;
Sebastian Redl410c4f22010-08-31 20:53:31 +0000900 // Skip through transparent contexts.
901 while (Ctx->isTransparentContext())
Douglas Gregorce356072009-01-06 23:51:29 +0000902 Ctx = Ctx->getParent();
903 return Ctx;
904}
905
Douglas Gregor88b70942009-02-25 22:02:03 +0000906DeclContext *DeclContext::getEnclosingNamespaceContext() {
907 DeclContext *Ctx = this;
908 // Skip through non-namespace, non-translation-unit contexts.
Sebastian Redl51a8a372010-08-31 00:36:23 +0000909 while (!Ctx->isFileContext())
Douglas Gregor88b70942009-02-25 22:02:03 +0000910 Ctx = Ctx->getParent();
911 return Ctx->getPrimaryContext();
912}
913
Sebastian Redl7a126a42010-08-31 00:36:30 +0000914bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
915 // For non-file contexts, this is equivalent to Equals.
916 if (!isFileContext())
917 return O->Equals(this);
918
919 do {
920 if (O->Equals(this))
921 return true;
922
923 const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O);
924 if (!NS || !NS->isInline())
925 break;
926 O = NS->getParent();
927 } while (O);
928
929 return false;
930}
931
John McCallab88d972009-08-31 22:39:49 +0000932void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000933 // FIXME: This feels like a hack. Should DeclarationName support
934 // template-ids, or is there a better way to keep specializations
935 // from being visible?
936 if (isa<ClassTemplateSpecializationDecl>(D))
937 return;
Eli Friedman6bc20132009-12-08 05:40:03 +0000938 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
939 if (FD->isFunctionTemplateSpecialization())
940 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000941
Steve Naroff0701bbb2009-01-08 17:28:14 +0000942 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000943 if (PrimaryContext != this) {
John McCallab88d972009-08-31 22:39:49 +0000944 PrimaryContext->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor44b43212008-12-11 16:49:14 +0000945 return;
946 }
947
948 // If we already have a lookup data structure, perform the insertion
Argyrios Kyrtzidis5586b012010-07-04 21:44:25 +0000949 // into it. If we haven't deserialized externally stored decls, deserialize
950 // them so we can add the decl. Otherwise, be lazy and don't build that
951 // structure until someone asks for it.
952 if (LookupPtr || !Recoverable || hasExternalVisibleStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000953 makeDeclVisibleInContextImpl(D);
Douglas Gregor074149e2009-01-05 19:45:36 +0000954
Sebastian Redl410c4f22010-08-31 20:53:31 +0000955 // If we are a transparent context or inline namespace, insert into our
956 // parent context, too. This operation is recursive.
957 if (isTransparentContext() || isInlineNamespace())
John McCallab88d972009-08-31 22:39:49 +0000958 getParent()->makeDeclVisibleInContext(D, Recoverable);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +0000959
960 Decl *DCAsDecl = cast<Decl>(this);
961 // Notify that a decl was made visible unless it's a Tag being defined.
962 if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined()))
963 if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
964 L->AddedVisibleDecl(this, D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000965}
966
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000967void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000968 // Skip unnamed declarations.
969 if (!D->getDeclName())
970 return;
971
Douglas Gregorcc636682009-02-17 23:15:12 +0000972 // FIXME: This feels like a hack. Should DeclarationName support
973 // template-ids, or is there a better way to keep specializations
974 // from being visible?
975 if (isa<ClassTemplateSpecializationDecl>(D))
976 return;
977
Argyrios Kyrtzidis5586b012010-07-04 21:44:25 +0000978 ASTContext *C = 0;
979 if (!LookupPtr) {
980 C = &getParentASTContext();
981 CreateStoredDeclsMap(*C);
982 }
983
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000984 // If there is an external AST source, load any declarations it knows about
985 // with this declaration's name.
986 // If the lookup table contains an entry about this name it means that we
987 // have already checked the external source.
988 if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
989 if (hasExternalVisibleStorage() &&
990 LookupPtr->find(D->getDeclName()) == LookupPtr->end())
991 Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
992
Douglas Gregor44b43212008-12-11 16:49:14 +0000993 // Insert this declaration into the map.
John McCall0c01d182010-03-24 05:22:00 +0000994 StoredDeclsList &DeclNameEntries = (*LookupPtr)[D->getDeclName()];
Chris Lattner67762a32009-02-20 01:44:05 +0000995 if (DeclNameEntries.isNull()) {
996 DeclNameEntries.setOnlyValue(D);
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000997 return;
Douglas Gregor44b43212008-12-11 16:49:14 +0000998 }
Chris Lattner91942502009-02-20 00:55:03 +0000999
Chris Lattnerbdc3d002009-02-20 01:10:07 +00001000 // If it is possible that this is a redeclaration, check to see if there is
1001 // already a decl for which declarationReplaces returns true. If there is
1002 // one, just replace it and return.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001003 if (DeclNameEntries.HandleRedeclaration(D))
Chris Lattner67762a32009-02-20 01:44:05 +00001004 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001005
Chris Lattnerbd6c8002009-02-19 07:00:44 +00001006 // Put this declaration into the appropriate slot.
Chris Lattner67762a32009-02-20 01:44:05 +00001007 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor44b43212008-12-11 16:49:14 +00001008}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001009
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00001010void DeclContext::MaterializeVisibleDeclsFromExternalStorage() {
1011 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1012 assert(hasExternalVisibleStorage() && Source && "No external storage?");
1013
1014 if (!LookupPtr)
1015 CreateStoredDeclsMap(getParentASTContext());
1016 Source->MaterializeVisibleDecls(this);
1017}
1018
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001019/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
1020/// this context.
Mike Stump1eb44332009-09-09 15:08:12 +00001021DeclContext::udir_iterator_range
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001022DeclContext::getUsingDirectives() const {
1023 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001024 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
1025 reinterpret_cast<udir_iterator>(Result.second));
1026}
Douglas Gregor2cf26342009-04-09 22:27:44 +00001027
Ted Kremenek3478eb62010-02-11 07:12:28 +00001028//===----------------------------------------------------------------------===//
1029// Creation and Destruction of StoredDeclsMaps. //
1030//===----------------------------------------------------------------------===//
1031
John McCall0c01d182010-03-24 05:22:00 +00001032StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
1033 assert(!LookupPtr && "context already has a decls map");
1034 assert(getPrimaryContext() == this &&
1035 "creating decls map on non-primary context");
1036
1037 StoredDeclsMap *M;
1038 bool Dependent = isDependentContext();
1039 if (Dependent)
1040 M = new DependentStoredDeclsMap();
1041 else
1042 M = new StoredDeclsMap();
1043 M->Previous = C.LastSDM;
1044 C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
1045 LookupPtr = M;
Ted Kremenek3478eb62010-02-11 07:12:28 +00001046 return M;
1047}
1048
1049void ASTContext::ReleaseDeclContextMaps() {
John McCall0c01d182010-03-24 05:22:00 +00001050 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1051 // pointer because the subclass doesn't add anything that needs to
1052 // be deleted.
John McCall0c01d182010-03-24 05:22:00 +00001053 StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1054}
1055
1056void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1057 while (Map) {
1058 // Advance the iteration before we invalidate memory.
1059 llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1060
1061 if (Dependent)
1062 delete static_cast<DependentStoredDeclsMap*>(Map);
1063 else
1064 delete Map;
1065
1066 Map = Next.getPointer();
1067 Dependent = Next.getInt();
1068 }
1069}
1070
John McCall0c01d182010-03-24 05:22:00 +00001071DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1072 DeclContext *Parent,
1073 const PartialDiagnostic &PDiag) {
1074 assert(Parent->isDependentContext()
1075 && "cannot iterate dependent diagnostics of non-dependent context");
1076 Parent = Parent->getPrimaryContext();
1077 if (!Parent->LookupPtr)
1078 Parent->CreateStoredDeclsMap(C);
1079
1080 DependentStoredDeclsMap *Map
1081 = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr);
1082
Douglas Gregorb8365182010-03-29 23:56:53 +00001083 // Allocate the copy of the PartialDiagnostic via the ASTContext's
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00001084 // BumpPtrAllocator, rather than the ASTContext itself.
Douglas Gregorb8365182010-03-29 23:56:53 +00001085 PartialDiagnostic::Storage *DiagStorage = 0;
1086 if (PDiag.hasStorage())
1087 DiagStorage = new (C) PartialDiagnostic::Storage;
1088
1089 DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
John McCall0c01d182010-03-24 05:22:00 +00001090
1091 // TODO: Maybe we shouldn't reverse the order during insertion.
1092 DD->NextDiagnostic = Map->FirstDiagnostic;
1093 Map->FirstDiagnostic = DD;
1094
1095 return DD;
Ted Kremenek3478eb62010-02-11 07:12:28 +00001096}