blob: 5ba6623b20bb189afd93aad33088e038858beb53 [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();
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Anders Carlsson67e33202009-06-13 00:08:58 +0000114 return false;
115}
116
Douglas Gregore53060f2009-06-25 22:08:12 +0000117bool Decl::isFunctionOrFunctionTemplate() const {
John McCall9488ea12009-11-17 05:59:44 +0000118 if (const UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(this))
Anders Carlsson58badb72009-06-26 05:26:50 +0000119 return UD->getTargetDecl()->isFunctionOrFunctionTemplate();
Mike Stump1eb44332009-09-09 15:08:12 +0000120
Douglas Gregore53060f2009-06-25 22:08:12 +0000121 return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this);
122}
123
Douglas Gregor79c22782010-01-16 20:21:20 +0000124bool Decl::isDefinedOutsideFunctionOrMethod() const {
125 for (const DeclContext *DC = getDeclContext();
126 DC && !DC->isTranslationUnit();
127 DC = DC->getParent())
128 if (DC->isFunctionOrMethod())
129 return false;
130
131 return true;
132}
133
134
Eli Friedman56d29372008-06-07 16:52:53 +0000135//===----------------------------------------------------------------------===//
Chris Lattner49f28ca2009-03-05 08:00:35 +0000136// PrettyStackTraceDecl Implementation
137//===----------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Chris Lattner49f28ca2009-03-05 08:00:35 +0000139void PrettyStackTraceDecl::print(llvm::raw_ostream &OS) const {
140 SourceLocation TheLoc = Loc;
141 if (TheLoc.isInvalid() && TheDecl)
142 TheLoc = TheDecl->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000143
Chris Lattner49f28ca2009-03-05 08:00:35 +0000144 if (TheLoc.isValid()) {
145 TheLoc.print(OS, SM);
146 OS << ": ";
147 }
148
149 OS << Message;
150
Daniel Dunbarc5236562009-11-21 09:05:59 +0000151 if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
Chris Lattner49f28ca2009-03-05 08:00:35 +0000152 OS << " '" << DN->getQualifiedNameAsString() << '\'';
153 OS << '\n';
154}
Mike Stump1eb44332009-09-09 15:08:12 +0000155
Chris Lattner49f28ca2009-03-05 08:00:35 +0000156//===----------------------------------------------------------------------===//
Eli Friedman56d29372008-06-07 16:52:53 +0000157// Decl Implementation
158//===----------------------------------------------------------------------===//
159
Chris Lattner769dbdf2009-03-27 20:18:19 +0000160// Out-of-line virtual method providing a home for Decl.
Douglas Gregorff331c12010-07-25 18:17:45 +0000161Decl::~Decl() { }
Chris Lattner769dbdf2009-03-27 20:18:19 +0000162
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000163void Decl::setDeclContext(DeclContext *DC) {
164 if (isOutOfSemaDC())
165 delete getMultipleDC();
Mike Stump1eb44332009-09-09 15:08:12 +0000166
Chris Lattneree219fd2009-03-29 06:06:59 +0000167 DeclCtx = DC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000168}
169
170void Decl::setLexicalDeclContext(DeclContext *DC) {
171 if (DC == getLexicalDeclContext())
172 return;
173
174 if (isInSemaDC()) {
Ted Kremenek94a39002009-12-01 00:07:10 +0000175 MultipleDC *MDC = new (getASTContext()) MultipleDC();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000176 MDC->SemanticDC = getDeclContext();
177 MDC->LexicalDC = DC;
Chris Lattneree219fd2009-03-29 06:06:59 +0000178 DeclCtx = MDC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000179 } else {
180 getMultipleDC()->LexicalDC = DC;
181 }
182}
183
John McCall9aeed322009-10-01 00:25:31 +0000184bool Decl::isInAnonymousNamespace() const {
185 const DeclContext *DC = getDeclContext();
186 do {
187 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
188 if (ND->isAnonymousNamespace())
189 return true;
190 } while ((DC = DC->getParent()));
191
192 return false;
193}
194
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000195TranslationUnitDecl *Decl::getTranslationUnitDecl() {
Argyrios Kyrtzidis9b346692009-06-30 02:34:53 +0000196 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
197 return TUD;
198
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000199 DeclContext *DC = getDeclContext();
200 assert(DC && "This decl is not contained in a translation unit!");
Mike Stump1eb44332009-09-09 15:08:12 +0000201
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000202 while (!DC->isTranslationUnit()) {
203 DC = DC->getParent();
204 assert(DC && "This decl is not contained in a translation unit!");
205 }
Mike Stump1eb44332009-09-09 15:08:12 +0000206
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000207 return cast<TranslationUnitDecl>(DC);
208}
209
210ASTContext &Decl::getASTContext() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000211 return getTranslationUnitDecl()->getASTContext();
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000212}
213
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +0000214ASTMutationListener *Decl::getASTMutationListener() const {
215 return getASTContext().getASTMutationListener();
216}
217
Douglas Gregorc070cc62010-06-17 23:14:26 +0000218bool Decl::isUsed(bool CheckUsedAttr) const {
Tanya Lattner12ead492010-02-17 02:17:21 +0000219 if (Used)
220 return true;
221
222 // Check for used attribute.
Douglas Gregorc070cc62010-06-17 23:14:26 +0000223 if (CheckUsedAttr && hasAttr<UsedAttr>())
Tanya Lattner12ead492010-02-17 02:17:21 +0000224 return true;
225
226 // Check redeclarations for used attribute.
227 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Douglas Gregorc070cc62010-06-17 23:14:26 +0000228 if ((CheckUsedAttr && I->hasAttr<UsedAttr>()) || I->Used)
Tanya Lattner12ead492010-02-17 02:17:21 +0000229 return true;
230 }
231
232 return false;
233}
234
235
Chris Lattner769dbdf2009-03-27 20:18:19 +0000236unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
237 switch (DeclKind) {
John McCall9488ea12009-11-17 05:59:44 +0000238 case Function:
239 case CXXMethod:
240 case CXXConstructor:
241 case CXXDestructor:
242 case CXXConversion:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000243 case EnumConstant:
244 case Var:
245 case ImplicitParam:
246 case ParmVar:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000247 case NonTypeTemplateParm:
248 case ObjCMethod:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000249 case ObjCProperty:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000250 return IDNS_Ordinary;
John McCalld04efc92010-04-23 02:41:41 +0000251
John McCall0d6b1642010-04-23 18:46:30 +0000252 case ObjCCompatibleAlias:
253 case ObjCInterface:
254 return IDNS_Ordinary | IDNS_Type;
255
256 case Typedef:
257 case UnresolvedUsingTypename:
258 case TemplateTypeParm:
259 return IDNS_Ordinary | IDNS_Type;
260
John McCall9488ea12009-11-17 05:59:44 +0000261 case UsingShadow:
262 return 0; // we'll actually overwrite this later
263
John McCall7ba107a2009-11-18 02:36:19 +0000264 case UnresolvedUsingValue:
John McCall7ba107a2009-11-18 02:36:19 +0000265 return IDNS_Ordinary | IDNS_Using;
John McCall9488ea12009-11-17 05:59:44 +0000266
267 case Using:
268 return IDNS_Using;
269
Chris Lattner769dbdf2009-03-27 20:18:19 +0000270 case ObjCProtocol:
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000271 return IDNS_ObjCProtocol;
Mike Stump1eb44332009-09-09 15:08:12 +0000272
Chris Lattner769dbdf2009-03-27 20:18:19 +0000273 case Field:
274 case ObjCAtDefsField:
275 case ObjCIvar:
276 return IDNS_Member;
Mike Stump1eb44332009-09-09 15:08:12 +0000277
Chris Lattner769dbdf2009-03-27 20:18:19 +0000278 case Record:
279 case CXXRecord:
280 case Enum:
John McCall0d6b1642010-04-23 18:46:30 +0000281 return IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000282
Chris Lattner769dbdf2009-03-27 20:18:19 +0000283 case Namespace:
John McCall0d6b1642010-04-23 18:46:30 +0000284 case NamespaceAlias:
285 return IDNS_Namespace;
286
Chris Lattner769dbdf2009-03-27 20:18:19 +0000287 case FunctionTemplate:
John McCall0d6b1642010-04-23 18:46:30 +0000288 return IDNS_Ordinary;
289
Chris Lattner769dbdf2009-03-27 20:18:19 +0000290 case ClassTemplate:
291 case TemplateTemplateParm:
John McCall0d6b1642010-04-23 18:46:30 +0000292 return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000293
Chris Lattner769dbdf2009-03-27 20:18:19 +0000294 // Never have names.
John McCall02cace72009-08-28 07:59:38 +0000295 case Friend:
John McCalldd4a3b02009-09-16 22:47:08 +0000296 case FriendTemplate:
Abramo Bagnara6206d532010-06-05 05:09:32 +0000297 case AccessSpec:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000298 case LinkageSpec:
299 case FileScopeAsm:
300 case StaticAssert:
301 case ObjCClass:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000302 case ObjCPropertyImpl:
303 case ObjCForwardProtocol:
304 case Block:
305 case TranslationUnit:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000306
Chris Lattner769dbdf2009-03-27 20:18:19 +0000307 case UsingDirective:
308 case ClassTemplateSpecialization:
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000309 case ClassTemplatePartialSpecialization:
Douglas Gregorbd4187b2010-04-22 23:19:50 +0000310 case ObjCImplementation:
311 case ObjCCategory:
312 case ObjCCategoryImpl:
313 // Never looked up by name.
Chris Lattner769dbdf2009-03-27 20:18:19 +0000314 return 0;
315 }
John McCall9488ea12009-11-17 05:59:44 +0000316
317 return 0;
Eli Friedman56d29372008-06-07 16:52:53 +0000318}
319
Sean Huntcf807c42010-08-18 23:23:40 +0000320void Decl::setAttrs(const AttrVec &attrs) {
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000321 assert(!HasAttrs && "Decl already contains attrs.");
322
Sean Huntcf807c42010-08-18 23:23:40 +0000323 AttrVec &AttrBlank = getASTContext().getDeclAttrs(this);
324 assert(AttrBlank.empty() && "HasAttrs was wrong?");
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000325
326 AttrBlank = attrs;
327 HasAttrs = true;
328}
329
Sean Huntcf807c42010-08-18 23:23:40 +0000330void Decl::dropAttrs() {
Eli Friedman56d29372008-06-07 16:52:53 +0000331 if (!HasAttrs) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000332
Eli Friedman56d29372008-06-07 16:52:53 +0000333 HasAttrs = false;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000334 getASTContext().eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000335}
336
Sean Huntcf807c42010-08-18 23:23:40 +0000337const AttrVec &Decl::getAttrs() const {
338 assert(HasAttrs && "No attrs to get!");
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000339 return getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000340}
341
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000342void Decl::swapAttrs(Decl *RHS) {
Eli Friedman56d29372008-06-07 16:52:53 +0000343 bool HasLHSAttr = this->HasAttrs;
344 bool HasRHSAttr = RHS->HasAttrs;
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Eli Friedman56d29372008-06-07 16:52:53 +0000346 // Usually, neither decl has attrs, nothing to do.
347 if (!HasLHSAttr && !HasRHSAttr) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000348
Eli Friedman56d29372008-06-07 16:52:53 +0000349 // If 'this' has no attrs, swap the other way.
350 if (!HasLHSAttr)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000351 return RHS->swapAttrs(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000352
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000353 ASTContext &Context = getASTContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Eli Friedman56d29372008-06-07 16:52:53 +0000355 // Handle the case when both decls have attrs.
356 if (HasRHSAttr) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000357 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman56d29372008-06-07 16:52:53 +0000358 return;
359 }
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Eli Friedman56d29372008-06-07 16:52:53 +0000361 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor68584ed2009-06-18 16:11:24 +0000362 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
363 Context.eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000364 this->HasAttrs = false;
365 RHS->HasAttrs = true;
366}
367
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000368Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000369 Decl::Kind DK = D->getDeclKind();
370 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000371#define DECL(NAME, BASE)
372#define DECL_CONTEXT(NAME) \
373 case Decl::NAME: \
374 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
375#define DECL_CONTEXT_BASE(NAME)
376#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000377 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000378#define DECL(NAME, BASE)
379#define DECL_CONTEXT_BASE(NAME) \
380 if (DK >= first##NAME && DK <= last##NAME) \
381 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
382#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000383 assert(false && "a decl that inherits DeclContext isn't handled");
384 return 0;
385 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000386}
387
388DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000389 Decl::Kind DK = D->getKind();
390 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000391#define DECL(NAME, BASE)
392#define DECL_CONTEXT(NAME) \
393 case Decl::NAME: \
394 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
395#define DECL_CONTEXT_BASE(NAME)
396#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000397 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000398#define DECL(NAME, BASE)
399#define DECL_CONTEXT_BASE(NAME) \
400 if (DK >= first##NAME && DK <= last##NAME) \
401 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
402#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000403 assert(false && "a decl that inherits DeclContext isn't handled");
404 return 0;
405 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000406}
407
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000408SourceLocation Decl::getBodyRBrace() const {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000409 // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
410 // FunctionDecl stores EndRangeLoc for this purpose.
411 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
412 const FunctionDecl *Definition;
413 if (FD->hasBody(Definition))
414 return Definition->getSourceRange().getEnd();
415 return SourceLocation();
416 }
417
Argyrios Kyrtzidis6717ef42010-07-07 11:31:27 +0000418 if (Stmt *Body = getBody())
419 return Body->getSourceRange().getEnd();
420
421 return SourceLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +0000422}
423
Anders Carlsson1329c272009-03-25 23:38:06 +0000424#ifndef NDEBUG
425void Decl::CheckAccessDeclContext() const {
John McCall46460a62010-01-20 21:53:11 +0000426 // Suppress this check if any of the following hold:
427 // 1. this is the translation unit (and thus has no parent)
428 // 2. this is a template parameter (and thus doesn't belong to its context)
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000429 // 3. this is a non-type template parameter
430 // 4. the context is not a record
431 // 5. it's invalid
432 // 6. it's a C++0x static_assert.
Anders Carlsson35eda442009-08-29 20:47:47 +0000433 if (isa<TranslationUnitDecl>(this) ||
Argyrios Kyrtzidis04aed0e2010-07-02 11:55:44 +0000434 isa<TemplateTypeParmDecl>(this) ||
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000435 isa<NonTypeTemplateParmDecl>(this) ||
Douglas Gregorfdd8ab12010-02-22 17:53:38 +0000436 !isa<CXXRecordDecl>(getDeclContext()) ||
Argyrios Kyrtzidis65b63ec2010-09-08 21:32:35 +0000437 isInvalidDecl() ||
438 isa<StaticAssertDecl>(this) ||
439 // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
440 // as DeclContext (?).
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000441 isa<ParmVarDecl>(this) ||
442 // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
443 // AS_none as access specifier.
444 isa<CXXRecordDecl>(this))
Anders Carlsson35eda442009-08-29 20:47:47 +0000445 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000446
447 assert(Access != AS_none &&
Anders Carlsson1329c272009-03-25 23:38:06 +0000448 "Access specifier is AS_none inside a record decl");
449}
450
451#endif
452
Eli Friedman56d29372008-06-07 16:52:53 +0000453//===----------------------------------------------------------------------===//
454// DeclContext Implementation
455//===----------------------------------------------------------------------===//
456
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000457bool DeclContext::classof(const Decl *D) {
458 switch (D->getKind()) {
Sean Hunt9a555912010-05-30 07:21:58 +0000459#define DECL(NAME, BASE)
460#define DECL_CONTEXT(NAME) case Decl::NAME:
461#define DECL_CONTEXT_BASE(NAME)
462#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000463 return true;
464 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000465#define DECL(NAME, BASE)
466#define DECL_CONTEXT_BASE(NAME) \
467 if (D->getKind() >= Decl::first##NAME && \
468 D->getKind() <= Decl::last##NAME) \
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000469 return true;
Sean Hunt9a555912010-05-30 07:21:58 +0000470#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000471 return false;
472 }
473}
474
Douglas Gregora2da7802010-07-25 18:38:02 +0000475DeclContext::~DeclContext() { }
Douglas Gregor44b43212008-12-11 16:49:14 +0000476
Douglas Gregore942bbe2009-09-10 16:57:35 +0000477/// \brief Find the parent context of this context that will be
478/// used for unqualified name lookup.
479///
480/// Generally, the parent lookup context is the semantic context. However, for
481/// a friend function the parent lookup context is the lexical context, which
482/// is the class in which the friend is declared.
483DeclContext *DeclContext::getLookupParent() {
484 // FIXME: Find a better way to identify friends
485 if (isa<FunctionDecl>(this))
Sebastian Redl7a126a42010-08-31 00:36:30 +0000486 if (getParent()->getRedeclContext()->isFileContext() &&
487 getLexicalParent()->getRedeclContext()->isRecord())
Douglas Gregore942bbe2009-09-10 16:57:35 +0000488 return getLexicalParent();
489
490 return getParent();
491}
492
Sebastian Redl410c4f22010-08-31 20:53:31 +0000493bool DeclContext::isInlineNamespace() const {
494 return isNamespace() &&
495 cast<NamespaceDecl>(this)->isInline();
496}
497
Douglas Gregorbc221632009-05-28 16:34:51 +0000498bool DeclContext::isDependentContext() const {
499 if (isFileContext())
500 return false;
501
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000502 if (isa<ClassTemplatePartialSpecializationDecl>(this))
503 return true;
504
Douglas Gregorbc221632009-05-28 16:34:51 +0000505 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
506 if (Record->getDescribedClassTemplate())
507 return true;
508
John McCall0c01d182010-03-24 05:22:00 +0000509 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregorbc221632009-05-28 16:34:51 +0000510 if (Function->getDescribedFunctionTemplate())
511 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000512
John McCall0c01d182010-03-24 05:22:00 +0000513 // Friend function declarations are dependent if their *lexical*
514 // context is dependent.
515 if (cast<Decl>(this)->getFriendObjectKind())
516 return getLexicalParent()->isDependentContext();
517 }
518
Douglas Gregorbc221632009-05-28 16:34:51 +0000519 return getParent() && getParent()->isDependentContext();
520}
521
Douglas Gregor074149e2009-01-05 19:45:36 +0000522bool DeclContext::isTransparentContext() const {
523 if (DeclKind == Decl::Enum)
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000524 return !cast<EnumDecl>(this)->isScoped();
Douglas Gregor074149e2009-01-05 19:45:36 +0000525 else if (DeclKind == Decl::LinkageSpec)
526 return true;
Sean Hunt9a555912010-05-30 07:21:58 +0000527 else if (DeclKind >= Decl::firstRecord && DeclKind <= Decl::lastRecord)
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000528 return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
Douglas Gregor074149e2009-01-05 19:45:36 +0000529
530 return false;
531}
532
John McCallac65c622010-10-26 04:59:26 +0000533bool DeclContext::isExternCContext() const {
534 const DeclContext *DC = this;
535 while (DC->DeclKind != Decl::TranslationUnit) {
536 if (DC->DeclKind == Decl::LinkageSpec)
537 return cast<LinkageSpecDecl>(DC)->getLanguage()
538 == LinkageSpecDecl::lang_c;
539 DC = DC->getParent();
540 }
541 return false;
542}
543
Sebastian Redl7a126a42010-08-31 00:36:30 +0000544bool DeclContext::Encloses(const DeclContext *DC) const {
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000545 if (getPrimaryContext() != this)
546 return getPrimaryContext()->Encloses(DC);
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000548 for (; DC; DC = DC->getParent())
549 if (DC->getPrimaryContext() == this)
550 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000551 return false;
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000552}
553
Steve Naroff0701bbb2009-01-08 17:28:14 +0000554DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000555 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000556 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000557 case Decl::LinkageSpec:
Mike Stump1eb44332009-09-09 15:08:12 +0000558 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000559 // There is only one DeclContext for these entities.
560 return this;
561
562 case Decl::Namespace:
563 // The original namespace is our primary context.
564 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
565
Douglas Gregor44b43212008-12-11 16:49:14 +0000566 case Decl::ObjCMethod:
567 return this;
568
569 case Decl::ObjCInterface:
Steve Naroff0701bbb2009-01-08 17:28:14 +0000570 case Decl::ObjCProtocol:
571 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000572 // FIXME: Can Objective-C interfaces be forward-declared?
573 return this;
574
Steve Naroff0701bbb2009-01-08 17:28:14 +0000575 case Decl::ObjCImplementation:
576 case Decl::ObjCCategoryImpl:
577 return this;
578
Douglas Gregor44b43212008-12-11 16:49:14 +0000579 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000580 if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000581 // If this is a tag type that has a definition or is currently
582 // being defined, that definition is our primary context.
John McCall3cb0ebd2010-03-10 03:28:59 +0000583 TagDecl *Tag = cast<TagDecl>(this);
584 assert(isa<TagType>(Tag->TypeForDecl) ||
585 isa<InjectedClassNameType>(Tag->TypeForDecl));
586
587 if (TagDecl *Def = Tag->getDefinition())
588 return Def;
589
590 if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) {
591 const TagType *TagTy = cast<TagType>(Tag->TypeForDecl);
592 if (TagTy->isBeingDefined())
593 // FIXME: is it necessarily being defined in the decl
594 // that owns the type?
595 return TagTy->getDecl();
596 }
597
598 return Tag;
Douglas Gregorcc636682009-02-17 23:15:12 +0000599 }
600
Sean Hunt9a555912010-05-30 07:21:58 +0000601 assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
Douglas Gregor44b43212008-12-11 16:49:14 +0000602 "Unknown DeclContext kind");
603 return this;
604 }
605}
606
607DeclContext *DeclContext::getNextContext() {
608 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000609 case Decl::Namespace:
610 // Return the next namespace
611 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
612
613 default:
Douglas Gregor44b43212008-12-11 16:49:14 +0000614 return 0;
615 }
616}
617
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000618std::pair<Decl *, Decl *>
619DeclContext::BuildDeclChain(const llvm::SmallVectorImpl<Decl*> &Decls) {
620 // Build up a chain of declarations via the Decl::NextDeclInContext field.
621 Decl *FirstNewDecl = 0;
622 Decl *PrevDecl = 0;
623 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
624 Decl *D = Decls[I];
625 if (PrevDecl)
626 PrevDecl->NextDeclInContext = D;
627 else
628 FirstNewDecl = D;
629
630 PrevDecl = D;
631 }
632
633 return std::make_pair(FirstNewDecl, PrevDecl);
634}
635
Douglas Gregor2cf26342009-04-09 22:27:44 +0000636/// \brief Load the declarations within this lexical storage from an
637/// external source.
Mike Stump1eb44332009-09-09 15:08:12 +0000638void
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000639DeclContext::LoadLexicalDeclsFromExternalStorage() const {
640 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000641 assert(hasExternalLexicalStorage() && Source && "No external storage?");
642
Argyrios Kyrtzidis0dbbc042010-07-30 10:03:23 +0000643 // Notify that we have a DeclContext that is initializing.
644 ExternalASTSource::Deserializing ADeclContext(Source);
645
John McCall76bd1f32010-06-01 09:23:16 +0000646 llvm::SmallVector<Decl*, 64> Decls;
647 if (Source->FindExternalLexicalDecls(this, Decls))
Douglas Gregor2cf26342009-04-09 22:27:44 +0000648 return;
649
650 // There is no longer any lexical storage in this context
651 ExternalLexicalStorage = false;
652
653 if (Decls.empty())
654 return;
655
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000656 // We may have already loaded just the fields of this record, in which case
657 // don't add the decls, just replace the FirstDecl/LastDecl chain.
658 if (const RecordDecl *RD = dyn_cast<RecordDecl>(this))
659 if (RD->LoadedFieldsFromExternalStorage) {
660 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
661 return;
662 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000663
664 // Splice the newly-read declarations into the beginning of the list
665 // of declarations.
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000666 Decl *ExternalFirst, *ExternalLast;
667 llvm::tie(ExternalFirst, ExternalLast) = BuildDeclChain(Decls);
668 ExternalLast->NextDeclInContext = FirstDecl;
669 FirstDecl = ExternalFirst;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000670 if (!LastDecl)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000671 LastDecl = ExternalLast;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000672}
673
John McCall76bd1f32010-06-01 09:23:16 +0000674DeclContext::lookup_result
675ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
676 DeclarationName Name) {
677 ASTContext &Context = DC->getParentASTContext();
678 StoredDeclsMap *Map;
679 if (!(Map = DC->LookupPtr))
680 Map = DC->CreateStoredDeclsMap(Context);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000681
John McCall76bd1f32010-06-01 09:23:16 +0000682 StoredDeclsList &List = (*Map)[Name];
683 assert(List.isNull());
684 (void) List;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000685
John McCall76bd1f32010-06-01 09:23:16 +0000686 return DeclContext::lookup_result();
687}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000688
John McCall76bd1f32010-06-01 09:23:16 +0000689DeclContext::lookup_result
690ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +0000691 DeclarationName Name,
692 llvm::SmallVectorImpl<NamedDecl*> &Decls) {
693 ASTContext &Context = DC->getParentASTContext();;
694
695 StoredDeclsMap *Map;
696 if (!(Map = DC->LookupPtr))
697 Map = DC->CreateStoredDeclsMap(Context);
698
699 StoredDeclsList &List = (*Map)[Name];
700 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
701 if (List.isNull())
702 List.setOnlyValue(Decls[I]);
703 else
704 List.AddSubsequentDecl(Decls[I]);
705 }
706
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000707 return List.getLookupResult();
John McCall76bd1f32010-06-01 09:23:16 +0000708}
709
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000710void ExternalASTSource::MaterializeVisibleDeclsForName(const DeclContext *DC,
711 DeclarationName Name,
712 llvm::SmallVectorImpl<NamedDecl*> &Decls) {
713 assert(DC->LookupPtr);
714 StoredDeclsMap &Map = *DC->LookupPtr;
715
716 // If there's an entry in the table the visible decls for this name have
717 // already been deserialized.
718 if (Map.find(Name) == Map.end()) {
719 StoredDeclsList &List = Map[Name];
720 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
721 if (List.isNull())
722 List.setOnlyValue(Decls[I]);
723 else
724 List.AddSubsequentDecl(Decls[I]);
725 }
726 }
727}
728
Sebastian Redl681d7232010-07-27 00:17:23 +0000729DeclContext::decl_iterator DeclContext::noload_decls_begin() const {
730 return decl_iterator(FirstDecl);
731}
732
733DeclContext::decl_iterator DeclContext::noload_decls_end() const {
734 return decl_iterator();
735}
736
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000737DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000738 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000739 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000740
741 // FIXME: Check whether we need to load some declarations from
742 // external storage.
Mike Stump1eb44332009-09-09 15:08:12 +0000743 return decl_iterator(FirstDecl);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000744}
745
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000746DeclContext::decl_iterator DeclContext::decls_end() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000747 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000748 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000749
Mike Stump1eb44332009-09-09 15:08:12 +0000750 return decl_iterator();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000751}
752
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000753bool DeclContext::decls_empty() const {
Douglas Gregor8038d512009-04-10 17:25:41 +0000754 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000755 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor8038d512009-04-10 17:25:41 +0000756
757 return !FirstDecl;
758}
759
John McCall9f54ad42009-12-10 09:41:52 +0000760void DeclContext::removeDecl(Decl *D) {
761 assert(D->getLexicalDeclContext() == this &&
762 "decl being removed from non-lexical context");
763 assert((D->NextDeclInContext || D == LastDecl) &&
764 "decl is not in decls list");
765
766 // Remove D from the decl chain. This is O(n) but hopefully rare.
767 if (D == FirstDecl) {
768 if (D == LastDecl)
769 FirstDecl = LastDecl = 0;
770 else
771 FirstDecl = D->NextDeclInContext;
772 } else {
773 for (Decl *I = FirstDecl; true; I = I->NextDeclInContext) {
774 assert(I && "decl not found in linked list");
775 if (I->NextDeclInContext == D) {
776 I->NextDeclInContext = D->NextDeclInContext;
777 if (D == LastDecl) LastDecl = I;
778 break;
779 }
780 }
781 }
782
783 // Mark that D is no longer in the decl chain.
784 D->NextDeclInContext = 0;
785
786 // Remove D from the lookup table if necessary.
787 if (isa<NamedDecl>(D)) {
788 NamedDecl *ND = cast<NamedDecl>(D);
789
John McCall0c01d182010-03-24 05:22:00 +0000790 StoredDeclsMap *Map = getPrimaryContext()->LookupPtr;
791 if (!Map) return;
John McCall9f54ad42009-12-10 09:41:52 +0000792
John McCall9f54ad42009-12-10 09:41:52 +0000793 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
794 assert(Pos != Map->end() && "no lookup entry for decl");
795 Pos->second.remove(ND);
796 }
797}
798
John McCall3f9a8a62009-08-11 06:59:38 +0000799void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner7f0be132009-02-20 00:56:18 +0000800 assert(D->getLexicalDeclContext() == this &&
801 "Decl inserted into wrong lexical context");
Mike Stump1eb44332009-09-09 15:08:12 +0000802 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000803 "Decl already inserted into a DeclContext");
804
805 if (FirstDecl) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000806 LastDecl->NextDeclInContext = D;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000807 LastDecl = D;
808 } else {
809 FirstDecl = LastDecl = D;
810 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000811
812 // Notify a C++ record declaration that we've added a member, so it can
813 // update it's class-specific state.
814 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
815 Record->addedMember(D);
John McCall3f9a8a62009-08-11 06:59:38 +0000816}
817
818void DeclContext::addDecl(Decl *D) {
819 addHiddenDecl(D);
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000820
821 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000822 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor44b43212008-12-11 16:49:14 +0000823}
824
Douglas Gregor074149e2009-01-05 19:45:36 +0000825/// buildLookup - Build the lookup data structure with all of the
826/// declarations in DCtx (and any other contexts linked to it or
827/// transparent contexts nested within it).
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000828void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000829 for (; DCtx; DCtx = DCtx->getNextContext()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000830 for (decl_iterator D = DCtx->decls_begin(),
831 DEnd = DCtx->decls_end();
Douglas Gregor4f3b8f82009-01-06 07:17:58 +0000832 D != DEnd; ++D) {
John McCall3f9a8a62009-08-11 06:59:38 +0000833 // Insert this declaration into the lookup structure, but only
834 // if it's semantically in its decl context. During non-lazy
835 // lookup building, this is implicitly enforced by addDecl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000836 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
John McCall3f9a8a62009-08-11 06:59:38 +0000837 if (D->getDeclContext() == DCtx)
838 makeDeclVisibleInContextImpl(ND);
Douglas Gregor074149e2009-01-05 19:45:36 +0000839
Ted Kremenekc32b1d82009-11-17 22:58:30 +0000840 // Insert any forward-declared Objective-C interfaces into the lookup
841 // data structure.
842 if (ObjCClassDecl *Class = dyn_cast<ObjCClassDecl>(*D))
843 for (ObjCClassDecl::iterator I = Class->begin(), IEnd = Class->end();
844 I != IEnd; ++I)
Ted Kremenek321c22f2009-11-18 00:28:11 +0000845 makeDeclVisibleInContextImpl(I->getInterface());
Ted Kremenekc32b1d82009-11-17 22:58:30 +0000846
Sebastian Redl410c4f22010-08-31 20:53:31 +0000847 // If this declaration is itself a transparent declaration context or
848 // inline namespace, add its members (recursively).
Douglas Gregor074149e2009-01-05 19:45:36 +0000849 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
Sebastian Redl410c4f22010-08-31 20:53:31 +0000850 if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000851 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregor074149e2009-01-05 19:45:36 +0000852 }
853 }
854}
855
Mike Stump1eb44332009-09-09 15:08:12 +0000856DeclContext::lookup_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000857DeclContext::lookup(DeclarationName Name) {
Steve Naroff0701bbb2009-01-08 17:28:14 +0000858 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000859 if (PrimaryContext != this)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000860 return PrimaryContext->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000861
John McCall76bd1f32010-06-01 09:23:16 +0000862 if (hasExternalVisibleStorage()) {
863 // Check to see if we've already cached the lookup results.
864 if (LookupPtr) {
865 StoredDeclsMap::iterator I = LookupPtr->find(Name);
866 if (I != LookupPtr->end())
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000867 return I->second.getLookupResult();
John McCall76bd1f32010-06-01 09:23:16 +0000868 }
869
870 ExternalASTSource *Source = getParentASTContext().getExternalSource();
871 return Source->FindExternalVisibleDeclsByName(this, Name);
872 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000873
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000874 /// If there is no lookup data structure, build one now by walking
Douglas Gregor44b43212008-12-11 16:49:14 +0000875 /// all of the linked DeclContexts (in declaration order!) and
876 /// inserting their values.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000877 if (!LookupPtr) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000878 buildLookup(this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000879
Douglas Gregorc36c5402009-04-09 17:29:08 +0000880 if (!LookupPtr)
Douglas Gregora5fdd9c2010-05-11 06:18:17 +0000881 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Douglas Gregorc36c5402009-04-09 17:29:08 +0000882 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000883
John McCall0c01d182010-03-24 05:22:00 +0000884 StoredDeclsMap::iterator Pos = LookupPtr->find(Name);
885 if (Pos == LookupPtr->end())
Douglas Gregora5fdd9c2010-05-11 06:18:17 +0000886 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000887 return Pos->second.getLookupResult();
Douglas Gregor44b43212008-12-11 16:49:14 +0000888}
889
Mike Stump1eb44332009-09-09 15:08:12 +0000890DeclContext::lookup_const_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000891DeclContext::lookup(DeclarationName Name) const {
892 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000893}
894
Sebastian Redl7a126a42010-08-31 00:36:30 +0000895DeclContext *DeclContext::getRedeclContext() {
Chris Lattner0cf2b192009-03-27 19:19:59 +0000896 DeclContext *Ctx = this;
Sebastian Redl410c4f22010-08-31 20:53:31 +0000897 // Skip through transparent contexts.
898 while (Ctx->isTransparentContext())
Douglas Gregorce356072009-01-06 23:51:29 +0000899 Ctx = Ctx->getParent();
900 return Ctx;
901}
902
Douglas Gregor88b70942009-02-25 22:02:03 +0000903DeclContext *DeclContext::getEnclosingNamespaceContext() {
904 DeclContext *Ctx = this;
905 // Skip through non-namespace, non-translation-unit contexts.
Sebastian Redl51a8a372010-08-31 00:36:23 +0000906 while (!Ctx->isFileContext())
Douglas Gregor88b70942009-02-25 22:02:03 +0000907 Ctx = Ctx->getParent();
908 return Ctx->getPrimaryContext();
909}
910
Sebastian Redl7a126a42010-08-31 00:36:30 +0000911bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
912 // For non-file contexts, this is equivalent to Equals.
913 if (!isFileContext())
914 return O->Equals(this);
915
916 do {
917 if (O->Equals(this))
918 return true;
919
920 const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O);
921 if (!NS || !NS->isInline())
922 break;
923 O = NS->getParent();
924 } while (O);
925
926 return false;
927}
928
John McCallab88d972009-08-31 22:39:49 +0000929void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000930 // FIXME: This feels like a hack. Should DeclarationName support
931 // template-ids, or is there a better way to keep specializations
932 // from being visible?
933 if (isa<ClassTemplateSpecializationDecl>(D))
934 return;
Eli Friedman6bc20132009-12-08 05:40:03 +0000935 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
936 if (FD->isFunctionTemplateSpecialization())
937 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000938
Steve Naroff0701bbb2009-01-08 17:28:14 +0000939 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000940 if (PrimaryContext != this) {
John McCallab88d972009-08-31 22:39:49 +0000941 PrimaryContext->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor44b43212008-12-11 16:49:14 +0000942 return;
943 }
944
945 // If we already have a lookup data structure, perform the insertion
Argyrios Kyrtzidis5586b012010-07-04 21:44:25 +0000946 // into it. If we haven't deserialized externally stored decls, deserialize
947 // them so we can add the decl. Otherwise, be lazy and don't build that
948 // structure until someone asks for it.
949 if (LookupPtr || !Recoverable || hasExternalVisibleStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000950 makeDeclVisibleInContextImpl(D);
Douglas Gregor074149e2009-01-05 19:45:36 +0000951
Sebastian Redl410c4f22010-08-31 20:53:31 +0000952 // If we are a transparent context or inline namespace, insert into our
953 // parent context, too. This operation is recursive.
954 if (isTransparentContext() || isInlineNamespace())
John McCallab88d972009-08-31 22:39:49 +0000955 getParent()->makeDeclVisibleInContext(D, Recoverable);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +0000956
957 Decl *DCAsDecl = cast<Decl>(this);
958 // Notify that a decl was made visible unless it's a Tag being defined.
959 if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined()))
960 if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
961 L->AddedVisibleDecl(this, D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000962}
963
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000964void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000965 // Skip unnamed declarations.
966 if (!D->getDeclName())
967 return;
968
Douglas Gregorcc636682009-02-17 23:15:12 +0000969 // FIXME: This feels like a hack. Should DeclarationName support
970 // template-ids, or is there a better way to keep specializations
971 // from being visible?
972 if (isa<ClassTemplateSpecializationDecl>(D))
973 return;
974
Argyrios Kyrtzidis5586b012010-07-04 21:44:25 +0000975 ASTContext *C = 0;
976 if (!LookupPtr) {
977 C = &getParentASTContext();
978 CreateStoredDeclsMap(*C);
979 }
980
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000981 // If there is an external AST source, load any declarations it knows about
982 // with this declaration's name.
983 // If the lookup table contains an entry about this name it means that we
984 // have already checked the external source.
985 if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
986 if (hasExternalVisibleStorage() &&
987 LookupPtr->find(D->getDeclName()) == LookupPtr->end())
988 Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
989
Douglas Gregor44b43212008-12-11 16:49:14 +0000990 // Insert this declaration into the map.
John McCall0c01d182010-03-24 05:22:00 +0000991 StoredDeclsList &DeclNameEntries = (*LookupPtr)[D->getDeclName()];
Chris Lattner67762a32009-02-20 01:44:05 +0000992 if (DeclNameEntries.isNull()) {
993 DeclNameEntries.setOnlyValue(D);
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000994 return;
Douglas Gregor44b43212008-12-11 16:49:14 +0000995 }
Chris Lattner91942502009-02-20 00:55:03 +0000996
Chris Lattnerbdc3d002009-02-20 01:10:07 +0000997 // If it is possible that this is a redeclaration, check to see if there is
998 // already a decl for which declarationReplaces returns true. If there is
999 // one, just replace it and return.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001000 if (DeclNameEntries.HandleRedeclaration(D))
Chris Lattner67762a32009-02-20 01:44:05 +00001001 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001002
Chris Lattnerbd6c8002009-02-19 07:00:44 +00001003 // Put this declaration into the appropriate slot.
Chris Lattner67762a32009-02-20 01:44:05 +00001004 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor44b43212008-12-11 16:49:14 +00001005}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001006
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00001007void DeclContext::MaterializeVisibleDeclsFromExternalStorage() {
1008 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1009 assert(hasExternalVisibleStorage() && Source && "No external storage?");
1010
1011 if (!LookupPtr)
1012 CreateStoredDeclsMap(getParentASTContext());
1013 Source->MaterializeVisibleDecls(this);
1014}
1015
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001016/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
1017/// this context.
Mike Stump1eb44332009-09-09 15:08:12 +00001018DeclContext::udir_iterator_range
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001019DeclContext::getUsingDirectives() const {
1020 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001021 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
1022 reinterpret_cast<udir_iterator>(Result.second));
1023}
Douglas Gregor2cf26342009-04-09 22:27:44 +00001024
Ted Kremenek3478eb62010-02-11 07:12:28 +00001025//===----------------------------------------------------------------------===//
1026// Creation and Destruction of StoredDeclsMaps. //
1027//===----------------------------------------------------------------------===//
1028
John McCall0c01d182010-03-24 05:22:00 +00001029StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
1030 assert(!LookupPtr && "context already has a decls map");
1031 assert(getPrimaryContext() == this &&
1032 "creating decls map on non-primary context");
1033
1034 StoredDeclsMap *M;
1035 bool Dependent = isDependentContext();
1036 if (Dependent)
1037 M = new DependentStoredDeclsMap();
1038 else
1039 M = new StoredDeclsMap();
1040 M->Previous = C.LastSDM;
1041 C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
1042 LookupPtr = M;
Ted Kremenek3478eb62010-02-11 07:12:28 +00001043 return M;
1044}
1045
1046void ASTContext::ReleaseDeclContextMaps() {
John McCall0c01d182010-03-24 05:22:00 +00001047 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1048 // pointer because the subclass doesn't add anything that needs to
1049 // be deleted.
John McCall0c01d182010-03-24 05:22:00 +00001050 StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1051}
1052
1053void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1054 while (Map) {
1055 // Advance the iteration before we invalidate memory.
1056 llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1057
1058 if (Dependent)
1059 delete static_cast<DependentStoredDeclsMap*>(Map);
1060 else
1061 delete Map;
1062
1063 Map = Next.getPointer();
1064 Dependent = Next.getInt();
1065 }
1066}
1067
John McCall0c01d182010-03-24 05:22:00 +00001068DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1069 DeclContext *Parent,
1070 const PartialDiagnostic &PDiag) {
1071 assert(Parent->isDependentContext()
1072 && "cannot iterate dependent diagnostics of non-dependent context");
1073 Parent = Parent->getPrimaryContext();
1074 if (!Parent->LookupPtr)
1075 Parent->CreateStoredDeclsMap(C);
1076
1077 DependentStoredDeclsMap *Map
1078 = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr);
1079
Douglas Gregorb8365182010-03-29 23:56:53 +00001080 // Allocate the copy of the PartialDiagnostic via the ASTContext's
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00001081 // BumpPtrAllocator, rather than the ASTContext itself.
Douglas Gregorb8365182010-03-29 23:56:53 +00001082 PartialDiagnostic::Storage *DiagStorage = 0;
1083 if (PDiag.hasStorage())
1084 DiagStorage = new (C) PartialDiagnostic::Storage;
1085
1086 DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
John McCall0c01d182010-03-24 05:22:00 +00001087
1088 // TODO: Maybe we shouldn't reverse the order during insertion.
1089 DD->NextDiagnostic = Map->FirstDiagnostic;
1090 Map->FirstDiagnostic = DD;
1091
1092 return DD;
Ted Kremenek3478eb62010-02-11 07:12:28 +00001093}