blob: 5606d54478fe0c75d839dea4f0fe7f04e86eefd6 [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"
Eli Friedman56d29372008-06-07 16:52:53 +000027#include "llvm/ADT/DenseMap.h"
Chris Lattner49f28ca2009-03-05 08:00:35 +000028#include "llvm/Support/raw_ostream.h"
Douglas Gregor6ed40e32008-12-23 21:05:05 +000029#include <algorithm>
Chris Lattner3daed522009-03-02 22:20:04 +000030#include <cstdio>
Douglas Gregor3fc749d2008-12-23 00:26:44 +000031#include <vector>
Eli Friedman56d29372008-06-07 16:52:53 +000032using namespace clang;
33
34//===----------------------------------------------------------------------===//
35// Statistics
36//===----------------------------------------------------------------------===//
37
Sean Hunt9a555912010-05-30 07:21:58 +000038#define DECL(DERIVED, BASE) static int n##DERIVED##s = 0;
39#define ABSTRACT_DECL(DECL)
40#include "clang/AST/DeclNodes.inc"
Eli Friedman56d29372008-06-07 16:52:53 +000041
42static bool StatSwitch = false;
43
Eli Friedman56d29372008-06-07 16:52:53 +000044const char *Decl::getDeclKindName() const {
45 switch (DeclKind) {
Sean Hunt9a555912010-05-30 07:21:58 +000046 default: assert(0 && "Declaration not in DeclNodes.inc!");
47#define DECL(DERIVED, BASE) case DERIVED: return #DERIVED;
48#define ABSTRACT_DECL(DECL)
49#include "clang/AST/DeclNodes.inc"
Eli Friedman56d29372008-06-07 16:52:53 +000050 }
51}
52
Douglas Gregor42738572010-03-05 00:26:45 +000053void Decl::setInvalidDecl(bool Invalid) {
54 InvalidDecl = Invalid;
55 if (Invalid) {
56 // Defensive maneuver for ill-formed code: we're likely not to make it to
57 // a point where we set the access specifier, so default it to "public"
58 // to avoid triggering asserts elsewhere in the front end.
59 setAccess(AS_public);
60 }
61}
62
Steve Naroff0a473932009-01-20 19:53:53 +000063const char *DeclContext::getDeclKindName() const {
64 switch (DeclKind) {
Sean Hunt9a555912010-05-30 07:21:58 +000065 default: assert(0 && "Declaration context not in DeclNodes.inc!");
66#define DECL(DERIVED, BASE) case Decl::DERIVED: return #DERIVED;
67#define ABSTRACT_DECL(DECL)
68#include "clang/AST/DeclNodes.inc"
Steve Naroff0a473932009-01-20 19:53:53 +000069 }
70}
71
Eli Friedman56d29372008-06-07 16:52:53 +000072bool Decl::CollectingStats(bool Enable) {
Kovarththanan Rajaratnam2024f4d2009-11-29 14:54:35 +000073 if (Enable) StatSwitch = true;
Eli Friedman56d29372008-06-07 16:52:53 +000074 return StatSwitch;
75}
76
77void Decl::PrintStats() {
78 fprintf(stderr, "*** Decl Stats:\n");
Mike Stump1eb44332009-09-09 15:08:12 +000079
Douglas Gregor64650af2009-02-02 23:39:07 +000080 int totalDecls = 0;
Sean Hunt9a555912010-05-30 07:21:58 +000081#define DECL(DERIVED, BASE) totalDecls += n##DERIVED##s;
82#define ABSTRACT_DECL(DECL)
83#include "clang/AST/DeclNodes.inc"
Douglas Gregor64650af2009-02-02 23:39:07 +000084 fprintf(stderr, " %d decls total.\n", totalDecls);
Mike Stump1eb44332009-09-09 15:08:12 +000085
Douglas Gregor64650af2009-02-02 23:39:07 +000086 int totalBytes = 0;
Sean Hunt9a555912010-05-30 07:21:58 +000087#define DECL(DERIVED, BASE) \
88 if (n##DERIVED##s > 0) { \
89 totalBytes += (int)(n##DERIVED##s * sizeof(DERIVED##Decl)); \
90 fprintf(stderr, " %d " #DERIVED " decls, %d each (%d bytes)\n", \
91 n##DERIVED##s, (int)sizeof(DERIVED##Decl), \
92 (int)(n##DERIVED##s * sizeof(DERIVED##Decl))); \
Douglas Gregor64650af2009-02-02 23:39:07 +000093 }
Sean Hunt9a555912010-05-30 07:21:58 +000094#define ABSTRACT_DECL(DECL)
95#include "clang/AST/DeclNodes.inc"
Mike Stump1eb44332009-09-09 15:08:12 +000096
Douglas Gregor64650af2009-02-02 23:39:07 +000097 fprintf(stderr, "Total bytes = %d\n", totalBytes);
Eli Friedman56d29372008-06-07 16:52:53 +000098}
99
Sean Hunt9a555912010-05-30 07:21:58 +0000100void Decl::add(Kind k) {
Eli Friedman56d29372008-06-07 16:52:53 +0000101 switch (k) {
Sean Hunt9a555912010-05-30 07:21:58 +0000102 default: assert(0 && "Declaration not in DeclNodes.inc!");
103#define DECL(DERIVED, BASE) case DERIVED: ++n##DERIVED##s; break;
104#define ABSTRACT_DECL(DECL)
105#include "clang/AST/DeclNodes.inc"
Eli Friedman56d29372008-06-07 16:52:53 +0000106 }
107}
108
Anders Carlsson67e33202009-06-13 00:08:58 +0000109bool Decl::isTemplateParameterPack() const {
110 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
111 return TTP->isParameterPack();
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Anders Carlsson67e33202009-06-13 00:08:58 +0000113 return false;
114}
115
Douglas Gregore53060f2009-06-25 22:08:12 +0000116bool Decl::isFunctionOrFunctionTemplate() const {
John McCall9488ea12009-11-17 05:59:44 +0000117 if (const UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(this))
Anders Carlsson58badb72009-06-26 05:26:50 +0000118 return UD->getTargetDecl()->isFunctionOrFunctionTemplate();
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Douglas Gregore53060f2009-06-25 22:08:12 +0000120 return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this);
121}
122
Douglas Gregor79c22782010-01-16 20:21:20 +0000123bool Decl::isDefinedOutsideFunctionOrMethod() const {
124 for (const DeclContext *DC = getDeclContext();
125 DC && !DC->isTranslationUnit();
126 DC = DC->getParent())
127 if (DC->isFunctionOrMethod())
128 return false;
129
130 return true;
131}
132
133
Eli Friedman56d29372008-06-07 16:52:53 +0000134//===----------------------------------------------------------------------===//
Chris Lattner49f28ca2009-03-05 08:00:35 +0000135// PrettyStackTraceDecl Implementation
136//===----------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000137
Chris Lattner49f28ca2009-03-05 08:00:35 +0000138void PrettyStackTraceDecl::print(llvm::raw_ostream &OS) const {
139 SourceLocation TheLoc = Loc;
140 if (TheLoc.isInvalid() && TheDecl)
141 TheLoc = TheDecl->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Chris Lattner49f28ca2009-03-05 08:00:35 +0000143 if (TheLoc.isValid()) {
144 TheLoc.print(OS, SM);
145 OS << ": ";
146 }
147
148 OS << Message;
149
Daniel Dunbarc5236562009-11-21 09:05:59 +0000150 if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
Chris Lattner49f28ca2009-03-05 08:00:35 +0000151 OS << " '" << DN->getQualifiedNameAsString() << '\'';
152 OS << '\n';
153}
Mike Stump1eb44332009-09-09 15:08:12 +0000154
Chris Lattner49f28ca2009-03-05 08:00:35 +0000155//===----------------------------------------------------------------------===//
Eli Friedman56d29372008-06-07 16:52:53 +0000156// Decl Implementation
157//===----------------------------------------------------------------------===//
158
Chris Lattner769dbdf2009-03-27 20:18:19 +0000159// Out-of-line virtual method providing a home for Decl.
Douglas Gregorff331c12010-07-25 18:17:45 +0000160Decl::~Decl() { }
Chris Lattner769dbdf2009-03-27 20:18:19 +0000161
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000162void Decl::setDeclContext(DeclContext *DC) {
163 if (isOutOfSemaDC())
164 delete getMultipleDC();
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Chris Lattneree219fd2009-03-29 06:06:59 +0000166 DeclCtx = DC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000167}
168
169void Decl::setLexicalDeclContext(DeclContext *DC) {
170 if (DC == getLexicalDeclContext())
171 return;
172
173 if (isInSemaDC()) {
Ted Kremenek94a39002009-12-01 00:07:10 +0000174 MultipleDC *MDC = new (getASTContext()) MultipleDC();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000175 MDC->SemanticDC = getDeclContext();
176 MDC->LexicalDC = DC;
Chris Lattneree219fd2009-03-29 06:06:59 +0000177 DeclCtx = MDC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000178 } else {
179 getMultipleDC()->LexicalDC = DC;
180 }
181}
182
John McCall9aeed322009-10-01 00:25:31 +0000183bool Decl::isInAnonymousNamespace() const {
184 const DeclContext *DC = getDeclContext();
185 do {
186 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
187 if (ND->isAnonymousNamespace())
188 return true;
189 } while ((DC = DC->getParent()));
190
191 return false;
192}
193
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000194TranslationUnitDecl *Decl::getTranslationUnitDecl() {
Argyrios Kyrtzidis9b346692009-06-30 02:34:53 +0000195 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
196 return TUD;
197
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000198 DeclContext *DC = getDeclContext();
199 assert(DC && "This decl is not contained in a translation unit!");
Mike Stump1eb44332009-09-09 15:08:12 +0000200
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000201 while (!DC->isTranslationUnit()) {
202 DC = DC->getParent();
203 assert(DC && "This decl is not contained in a translation unit!");
204 }
Mike Stump1eb44332009-09-09 15:08:12 +0000205
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000206 return cast<TranslationUnitDecl>(DC);
207}
208
209ASTContext &Decl::getASTContext() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000210 return getTranslationUnitDecl()->getASTContext();
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000211}
212
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +0000213ASTMutationListener *Decl::getASTMutationListener() const {
214 return getASTContext().getASTMutationListener();
215}
216
Douglas Gregorc070cc62010-06-17 23:14:26 +0000217bool Decl::isUsed(bool CheckUsedAttr) const {
Tanya Lattner12ead492010-02-17 02:17:21 +0000218 if (Used)
219 return true;
220
221 // Check for used attribute.
Douglas Gregorc070cc62010-06-17 23:14:26 +0000222 if (CheckUsedAttr && hasAttr<UsedAttr>())
Tanya Lattner12ead492010-02-17 02:17:21 +0000223 return true;
224
225 // Check redeclarations for used attribute.
226 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Douglas Gregorc070cc62010-06-17 23:14:26 +0000227 if ((CheckUsedAttr && I->hasAttr<UsedAttr>()) || I->Used)
Tanya Lattner12ead492010-02-17 02:17:21 +0000228 return true;
229 }
230
231 return false;
232}
233
234
Chris Lattner769dbdf2009-03-27 20:18:19 +0000235unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
236 switch (DeclKind) {
John McCall9488ea12009-11-17 05:59:44 +0000237 case Function:
238 case CXXMethod:
239 case CXXConstructor:
240 case CXXDestructor:
241 case CXXConversion:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000242 case EnumConstant:
243 case Var:
244 case ImplicitParam:
245 case ParmVar:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000246 case NonTypeTemplateParm:
247 case ObjCMethod:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000248 case ObjCProperty:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000249 return IDNS_Ordinary;
John McCalld04efc92010-04-23 02:41:41 +0000250
John McCall0d6b1642010-04-23 18:46:30 +0000251 case ObjCCompatibleAlias:
252 case ObjCInterface:
253 return IDNS_Ordinary | IDNS_Type;
254
255 case Typedef:
256 case UnresolvedUsingTypename:
257 case TemplateTypeParm:
258 return IDNS_Ordinary | IDNS_Type;
259
John McCall9488ea12009-11-17 05:59:44 +0000260 case UsingShadow:
261 return 0; // we'll actually overwrite this later
262
John McCall7ba107a2009-11-18 02:36:19 +0000263 case UnresolvedUsingValue:
John McCall7ba107a2009-11-18 02:36:19 +0000264 return IDNS_Ordinary | IDNS_Using;
John McCall9488ea12009-11-17 05:59:44 +0000265
266 case Using:
267 return IDNS_Using;
268
Chris Lattner769dbdf2009-03-27 20:18:19 +0000269 case ObjCProtocol:
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000270 return IDNS_ObjCProtocol;
Mike Stump1eb44332009-09-09 15:08:12 +0000271
Chris Lattner769dbdf2009-03-27 20:18:19 +0000272 case Field:
273 case ObjCAtDefsField:
274 case ObjCIvar:
275 return IDNS_Member;
Mike Stump1eb44332009-09-09 15:08:12 +0000276
Chris Lattner769dbdf2009-03-27 20:18:19 +0000277 case Record:
278 case CXXRecord:
279 case Enum:
John McCall0d6b1642010-04-23 18:46:30 +0000280 return IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000281
Chris Lattner769dbdf2009-03-27 20:18:19 +0000282 case Namespace:
John McCall0d6b1642010-04-23 18:46:30 +0000283 case NamespaceAlias:
284 return IDNS_Namespace;
285
Chris Lattner769dbdf2009-03-27 20:18:19 +0000286 case FunctionTemplate:
John McCall0d6b1642010-04-23 18:46:30 +0000287 return IDNS_Ordinary;
288
Chris Lattner769dbdf2009-03-27 20:18:19 +0000289 case ClassTemplate:
290 case TemplateTemplateParm:
John McCall0d6b1642010-04-23 18:46:30 +0000291 return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000292
Chris Lattner769dbdf2009-03-27 20:18:19 +0000293 // Never have names.
John McCall02cace72009-08-28 07:59:38 +0000294 case Friend:
John McCalldd4a3b02009-09-16 22:47:08 +0000295 case FriendTemplate:
Abramo Bagnara6206d532010-06-05 05:09:32 +0000296 case AccessSpec:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000297 case LinkageSpec:
298 case FileScopeAsm:
299 case StaticAssert:
300 case ObjCClass:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000301 case ObjCPropertyImpl:
302 case ObjCForwardProtocol:
303 case Block:
304 case TranslationUnit:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000305
Chris Lattner769dbdf2009-03-27 20:18:19 +0000306 case UsingDirective:
307 case ClassTemplateSpecialization:
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000308 case ClassTemplatePartialSpecialization:
Douglas Gregorbd4187b2010-04-22 23:19:50 +0000309 case ObjCImplementation:
310 case ObjCCategory:
311 case ObjCCategoryImpl:
312 // Never looked up by name.
Chris Lattner769dbdf2009-03-27 20:18:19 +0000313 return 0;
314 }
John McCall9488ea12009-11-17 05:59:44 +0000315
316 return 0;
Eli Friedman56d29372008-06-07 16:52:53 +0000317}
318
Sean Huntcf807c42010-08-18 23:23:40 +0000319void Decl::setAttrs(const AttrVec &attrs) {
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000320 assert(!HasAttrs && "Decl already contains attrs.");
321
Sean Huntcf807c42010-08-18 23:23:40 +0000322 AttrVec &AttrBlank = getASTContext().getDeclAttrs(this);
323 assert(AttrBlank.empty() && "HasAttrs was wrong?");
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000324
325 AttrBlank = attrs;
326 HasAttrs = true;
327}
328
Sean Huntcf807c42010-08-18 23:23:40 +0000329void Decl::dropAttrs() {
Eli Friedman56d29372008-06-07 16:52:53 +0000330 if (!HasAttrs) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Eli Friedman56d29372008-06-07 16:52:53 +0000332 HasAttrs = false;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000333 getASTContext().eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000334}
335
Sean Huntcf807c42010-08-18 23:23:40 +0000336const AttrVec &Decl::getAttrs() const {
337 assert(HasAttrs && "No attrs to get!");
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000338 return getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000339}
340
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000341void Decl::swapAttrs(Decl *RHS) {
Eli Friedman56d29372008-06-07 16:52:53 +0000342 bool HasLHSAttr = this->HasAttrs;
343 bool HasRHSAttr = RHS->HasAttrs;
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Eli Friedman56d29372008-06-07 16:52:53 +0000345 // Usually, neither decl has attrs, nothing to do.
346 if (!HasLHSAttr && !HasRHSAttr) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Eli Friedman56d29372008-06-07 16:52:53 +0000348 // If 'this' has no attrs, swap the other way.
349 if (!HasLHSAttr)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000350 return RHS->swapAttrs(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000352 ASTContext &Context = getASTContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Eli Friedman56d29372008-06-07 16:52:53 +0000354 // Handle the case when both decls have attrs.
355 if (HasRHSAttr) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000356 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman56d29372008-06-07 16:52:53 +0000357 return;
358 }
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Eli Friedman56d29372008-06-07 16:52:53 +0000360 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor68584ed2009-06-18 16:11:24 +0000361 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
362 Context.eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000363 this->HasAttrs = false;
364 RHS->HasAttrs = true;
365}
366
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000367Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000368 Decl::Kind DK = D->getDeclKind();
369 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000370#define DECL(NAME, BASE)
371#define DECL_CONTEXT(NAME) \
372 case Decl::NAME: \
373 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
374#define DECL_CONTEXT_BASE(NAME)
375#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000376 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000377#define DECL(NAME, BASE)
378#define DECL_CONTEXT_BASE(NAME) \
379 if (DK >= first##NAME && DK <= last##NAME) \
380 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
381#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000382 assert(false && "a decl that inherits DeclContext isn't handled");
383 return 0;
384 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000385}
386
387DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000388 Decl::Kind DK = D->getKind();
389 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000390#define DECL(NAME, BASE)
391#define DECL_CONTEXT(NAME) \
392 case Decl::NAME: \
393 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
394#define DECL_CONTEXT_BASE(NAME)
395#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000396 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000397#define DECL(NAME, BASE)
398#define DECL_CONTEXT_BASE(NAME) \
399 if (DK >= first##NAME && DK <= last##NAME) \
400 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
401#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000402 assert(false && "a decl that inherits DeclContext isn't handled");
403 return 0;
404 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000405}
406
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000407SourceLocation Decl::getBodyRBrace() const {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000408 // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
409 // FunctionDecl stores EndRangeLoc for this purpose.
410 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
411 const FunctionDecl *Definition;
412 if (FD->hasBody(Definition))
413 return Definition->getSourceRange().getEnd();
414 return SourceLocation();
415 }
416
Argyrios Kyrtzidis6717ef42010-07-07 11:31:27 +0000417 if (Stmt *Body = getBody())
418 return Body->getSourceRange().getEnd();
419
420 return SourceLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +0000421}
422
Anders Carlsson1329c272009-03-25 23:38:06 +0000423#ifndef NDEBUG
424void Decl::CheckAccessDeclContext() const {
John McCall46460a62010-01-20 21:53:11 +0000425 // Suppress this check if any of the following hold:
426 // 1. this is the translation unit (and thus has no parent)
427 // 2. this is a template parameter (and thus doesn't belong to its context)
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000428 // 3. this is a non-type template parameter
429 // 4. the context is not a record
430 // 5. it's invalid
431 // 6. it's a C++0x static_assert.
Anders Carlsson35eda442009-08-29 20:47:47 +0000432 if (isa<TranslationUnitDecl>(this) ||
Argyrios Kyrtzidis04aed0e2010-07-02 11:55:44 +0000433 isa<TemplateTypeParmDecl>(this) ||
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000434 isa<NonTypeTemplateParmDecl>(this) ||
Douglas Gregorfdd8ab12010-02-22 17:53:38 +0000435 !isa<CXXRecordDecl>(getDeclContext()) ||
Argyrios Kyrtzidis65b63ec2010-09-08 21:32:35 +0000436 isInvalidDecl() ||
437 isa<StaticAssertDecl>(this) ||
438 // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
439 // as DeclContext (?).
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000440 isa<ParmVarDecl>(this) ||
441 // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
442 // AS_none as access specifier.
443 isa<CXXRecordDecl>(this))
Anders Carlsson35eda442009-08-29 20:47:47 +0000444 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000445
446 assert(Access != AS_none &&
Anders Carlsson1329c272009-03-25 23:38:06 +0000447 "Access specifier is AS_none inside a record decl");
448}
449
450#endif
451
Eli Friedman56d29372008-06-07 16:52:53 +0000452//===----------------------------------------------------------------------===//
453// DeclContext Implementation
454//===----------------------------------------------------------------------===//
455
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000456bool DeclContext::classof(const Decl *D) {
457 switch (D->getKind()) {
Sean Hunt9a555912010-05-30 07:21:58 +0000458#define DECL(NAME, BASE)
459#define DECL_CONTEXT(NAME) case Decl::NAME:
460#define DECL_CONTEXT_BASE(NAME)
461#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000462 return true;
463 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000464#define DECL(NAME, BASE)
465#define DECL_CONTEXT_BASE(NAME) \
466 if (D->getKind() >= Decl::first##NAME && \
467 D->getKind() <= Decl::last##NAME) \
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000468 return true;
Sean Hunt9a555912010-05-30 07:21:58 +0000469#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000470 return false;
471 }
472}
473
Douglas Gregora2da7802010-07-25 18:38:02 +0000474DeclContext::~DeclContext() { }
Douglas Gregor44b43212008-12-11 16:49:14 +0000475
Douglas Gregore942bbe2009-09-10 16:57:35 +0000476/// \brief Find the parent context of this context that will be
477/// used for unqualified name lookup.
478///
479/// Generally, the parent lookup context is the semantic context. However, for
480/// a friend function the parent lookup context is the lexical context, which
481/// is the class in which the friend is declared.
482DeclContext *DeclContext::getLookupParent() {
483 // FIXME: Find a better way to identify friends
484 if (isa<FunctionDecl>(this))
Sebastian Redl7a126a42010-08-31 00:36:30 +0000485 if (getParent()->getRedeclContext()->isFileContext() &&
486 getLexicalParent()->getRedeclContext()->isRecord())
Douglas Gregore942bbe2009-09-10 16:57:35 +0000487 return getLexicalParent();
488
489 return getParent();
490}
491
Sebastian Redl410c4f22010-08-31 20:53:31 +0000492bool DeclContext::isInlineNamespace() const {
493 return isNamespace() &&
494 cast<NamespaceDecl>(this)->isInline();
495}
496
Douglas Gregorbc221632009-05-28 16:34:51 +0000497bool DeclContext::isDependentContext() const {
498 if (isFileContext())
499 return false;
500
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000501 if (isa<ClassTemplatePartialSpecializationDecl>(this))
502 return true;
503
Douglas Gregorbc221632009-05-28 16:34:51 +0000504 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
505 if (Record->getDescribedClassTemplate())
506 return true;
507
John McCall0c01d182010-03-24 05:22:00 +0000508 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregorbc221632009-05-28 16:34:51 +0000509 if (Function->getDescribedFunctionTemplate())
510 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000511
John McCall0c01d182010-03-24 05:22:00 +0000512 // Friend function declarations are dependent if their *lexical*
513 // context is dependent.
514 if (cast<Decl>(this)->getFriendObjectKind())
515 return getLexicalParent()->isDependentContext();
516 }
517
Douglas Gregorbc221632009-05-28 16:34:51 +0000518 return getParent() && getParent()->isDependentContext();
519}
520
Douglas Gregor074149e2009-01-05 19:45:36 +0000521bool DeclContext::isTransparentContext() const {
522 if (DeclKind == Decl::Enum)
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000523 return !cast<EnumDecl>(this)->isScoped();
Douglas Gregor074149e2009-01-05 19:45:36 +0000524 else if (DeclKind == Decl::LinkageSpec)
525 return true;
Sean Hunt9a555912010-05-30 07:21:58 +0000526 else if (DeclKind >= Decl::firstRecord && DeclKind <= Decl::lastRecord)
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000527 return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
Douglas Gregor074149e2009-01-05 19:45:36 +0000528
529 return false;
530}
531
John McCallac65c622010-10-26 04:59:26 +0000532bool DeclContext::isExternCContext() const {
533 const DeclContext *DC = this;
534 while (DC->DeclKind != Decl::TranslationUnit) {
535 if (DC->DeclKind == Decl::LinkageSpec)
536 return cast<LinkageSpecDecl>(DC)->getLanguage()
537 == LinkageSpecDecl::lang_c;
538 DC = DC->getParent();
539 }
540 return false;
541}
542
Sebastian Redl7a126a42010-08-31 00:36:30 +0000543bool DeclContext::Encloses(const DeclContext *DC) const {
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000544 if (getPrimaryContext() != this)
545 return getPrimaryContext()->Encloses(DC);
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000547 for (; DC; DC = DC->getParent())
548 if (DC->getPrimaryContext() == this)
549 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000550 return false;
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000551}
552
Steve Naroff0701bbb2009-01-08 17:28:14 +0000553DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000554 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000555 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000556 case Decl::LinkageSpec:
Mike Stump1eb44332009-09-09 15:08:12 +0000557 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000558 // There is only one DeclContext for these entities.
559 return this;
560
561 case Decl::Namespace:
562 // The original namespace is our primary context.
563 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
564
Douglas Gregor44b43212008-12-11 16:49:14 +0000565 case Decl::ObjCMethod:
566 return this;
567
568 case Decl::ObjCInterface:
Steve Naroff0701bbb2009-01-08 17:28:14 +0000569 case Decl::ObjCProtocol:
570 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000571 // FIXME: Can Objective-C interfaces be forward-declared?
572 return this;
573
Steve Naroff0701bbb2009-01-08 17:28:14 +0000574 case Decl::ObjCImplementation:
575 case Decl::ObjCCategoryImpl:
576 return this;
577
Douglas Gregor44b43212008-12-11 16:49:14 +0000578 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000579 if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000580 // If this is a tag type that has a definition or is currently
581 // being defined, that definition is our primary context.
John McCall3cb0ebd2010-03-10 03:28:59 +0000582 TagDecl *Tag = cast<TagDecl>(this);
583 assert(isa<TagType>(Tag->TypeForDecl) ||
584 isa<InjectedClassNameType>(Tag->TypeForDecl));
585
586 if (TagDecl *Def = Tag->getDefinition())
587 return Def;
588
589 if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) {
590 const TagType *TagTy = cast<TagType>(Tag->TypeForDecl);
591 if (TagTy->isBeingDefined())
592 // FIXME: is it necessarily being defined in the decl
593 // that owns the type?
594 return TagTy->getDecl();
595 }
596
597 return Tag;
Douglas Gregorcc636682009-02-17 23:15:12 +0000598 }
599
Sean Hunt9a555912010-05-30 07:21:58 +0000600 assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
Douglas Gregor44b43212008-12-11 16:49:14 +0000601 "Unknown DeclContext kind");
602 return this;
603 }
604}
605
606DeclContext *DeclContext::getNextContext() {
607 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000608 case Decl::Namespace:
609 // Return the next namespace
610 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
611
612 default:
Douglas Gregor44b43212008-12-11 16:49:14 +0000613 return 0;
614 }
615}
616
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000617std::pair<Decl *, Decl *>
618DeclContext::BuildDeclChain(const llvm::SmallVectorImpl<Decl*> &Decls) {
619 // Build up a chain of declarations via the Decl::NextDeclInContext field.
620 Decl *FirstNewDecl = 0;
621 Decl *PrevDecl = 0;
622 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
623 Decl *D = Decls[I];
624 if (PrevDecl)
625 PrevDecl->NextDeclInContext = D;
626 else
627 FirstNewDecl = D;
628
629 PrevDecl = D;
630 }
631
632 return std::make_pair(FirstNewDecl, PrevDecl);
633}
634
Douglas Gregor2cf26342009-04-09 22:27:44 +0000635/// \brief Load the declarations within this lexical storage from an
636/// external source.
Mike Stump1eb44332009-09-09 15:08:12 +0000637void
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000638DeclContext::LoadLexicalDeclsFromExternalStorage() const {
639 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000640 assert(hasExternalLexicalStorage() && Source && "No external storage?");
641
Argyrios Kyrtzidis0dbbc042010-07-30 10:03:23 +0000642 // Notify that we have a DeclContext that is initializing.
643 ExternalASTSource::Deserializing ADeclContext(Source);
644
John McCall76bd1f32010-06-01 09:23:16 +0000645 llvm::SmallVector<Decl*, 64> Decls;
646 if (Source->FindExternalLexicalDecls(this, Decls))
Douglas Gregor2cf26342009-04-09 22:27:44 +0000647 return;
648
649 // There is no longer any lexical storage in this context
650 ExternalLexicalStorage = false;
651
652 if (Decls.empty())
653 return;
654
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000655 // We may have already loaded just the fields of this record, in which case
656 // don't add the decls, just replace the FirstDecl/LastDecl chain.
657 if (const RecordDecl *RD = dyn_cast<RecordDecl>(this))
658 if (RD->LoadedFieldsFromExternalStorage) {
659 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
660 return;
661 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000662
663 // Splice the newly-read declarations into the beginning of the list
664 // of declarations.
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000665 Decl *ExternalFirst, *ExternalLast;
666 llvm::tie(ExternalFirst, ExternalLast) = BuildDeclChain(Decls);
667 ExternalLast->NextDeclInContext = FirstDecl;
668 FirstDecl = ExternalFirst;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000669 if (!LastDecl)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000670 LastDecl = ExternalLast;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000671}
672
John McCall76bd1f32010-06-01 09:23:16 +0000673DeclContext::lookup_result
674ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
675 DeclarationName Name) {
676 ASTContext &Context = DC->getParentASTContext();
677 StoredDeclsMap *Map;
678 if (!(Map = DC->LookupPtr))
679 Map = DC->CreateStoredDeclsMap(Context);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000680
John McCall76bd1f32010-06-01 09:23:16 +0000681 StoredDeclsList &List = (*Map)[Name];
682 assert(List.isNull());
683 (void) List;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000684
John McCall76bd1f32010-06-01 09:23:16 +0000685 return DeclContext::lookup_result();
686}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000687
John McCall76bd1f32010-06-01 09:23:16 +0000688DeclContext::lookup_result
689ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +0000690 DeclarationName Name,
691 llvm::SmallVectorImpl<NamedDecl*> &Decls) {
692 ASTContext &Context = DC->getParentASTContext();;
693
694 StoredDeclsMap *Map;
695 if (!(Map = DC->LookupPtr))
696 Map = DC->CreateStoredDeclsMap(Context);
697
698 StoredDeclsList &List = (*Map)[Name];
699 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
700 if (List.isNull())
701 List.setOnlyValue(Decls[I]);
702 else
703 List.AddSubsequentDecl(Decls[I]);
704 }
705
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000706 return List.getLookupResult();
John McCall76bd1f32010-06-01 09:23:16 +0000707}
708
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000709void ExternalASTSource::MaterializeVisibleDeclsForName(const DeclContext *DC,
710 DeclarationName Name,
711 llvm::SmallVectorImpl<NamedDecl*> &Decls) {
712 assert(DC->LookupPtr);
713 StoredDeclsMap &Map = *DC->LookupPtr;
714
715 // If there's an entry in the table the visible decls for this name have
716 // already been deserialized.
717 if (Map.find(Name) == Map.end()) {
718 StoredDeclsList &List = Map[Name];
719 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
720 if (List.isNull())
721 List.setOnlyValue(Decls[I]);
722 else
723 List.AddSubsequentDecl(Decls[I]);
724 }
725 }
726}
727
Sebastian Redl681d7232010-07-27 00:17:23 +0000728DeclContext::decl_iterator DeclContext::noload_decls_begin() const {
729 return decl_iterator(FirstDecl);
730}
731
732DeclContext::decl_iterator DeclContext::noload_decls_end() const {
733 return decl_iterator();
734}
735
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000736DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000737 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000738 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000739
740 // FIXME: Check whether we need to load some declarations from
741 // external storage.
Mike Stump1eb44332009-09-09 15:08:12 +0000742 return decl_iterator(FirstDecl);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000743}
744
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000745DeclContext::decl_iterator DeclContext::decls_end() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000746 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000747 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000748
Mike Stump1eb44332009-09-09 15:08:12 +0000749 return decl_iterator();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000750}
751
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000752bool DeclContext::decls_empty() const {
Douglas Gregor8038d512009-04-10 17:25:41 +0000753 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000754 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor8038d512009-04-10 17:25:41 +0000755
756 return !FirstDecl;
757}
758
John McCall9f54ad42009-12-10 09:41:52 +0000759void DeclContext::removeDecl(Decl *D) {
760 assert(D->getLexicalDeclContext() == this &&
761 "decl being removed from non-lexical context");
762 assert((D->NextDeclInContext || D == LastDecl) &&
763 "decl is not in decls list");
764
765 // Remove D from the decl chain. This is O(n) but hopefully rare.
766 if (D == FirstDecl) {
767 if (D == LastDecl)
768 FirstDecl = LastDecl = 0;
769 else
770 FirstDecl = D->NextDeclInContext;
771 } else {
772 for (Decl *I = FirstDecl; true; I = I->NextDeclInContext) {
773 assert(I && "decl not found in linked list");
774 if (I->NextDeclInContext == D) {
775 I->NextDeclInContext = D->NextDeclInContext;
776 if (D == LastDecl) LastDecl = I;
777 break;
778 }
779 }
780 }
781
782 // Mark that D is no longer in the decl chain.
783 D->NextDeclInContext = 0;
784
785 // Remove D from the lookup table if necessary.
786 if (isa<NamedDecl>(D)) {
787 NamedDecl *ND = cast<NamedDecl>(D);
788
John McCall0c01d182010-03-24 05:22:00 +0000789 StoredDeclsMap *Map = getPrimaryContext()->LookupPtr;
790 if (!Map) return;
John McCall9f54ad42009-12-10 09:41:52 +0000791
John McCall9f54ad42009-12-10 09:41:52 +0000792 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
793 assert(Pos != Map->end() && "no lookup entry for decl");
794 Pos->second.remove(ND);
795 }
796}
797
John McCall3f9a8a62009-08-11 06:59:38 +0000798void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner7f0be132009-02-20 00:56:18 +0000799 assert(D->getLexicalDeclContext() == this &&
800 "Decl inserted into wrong lexical context");
Mike Stump1eb44332009-09-09 15:08:12 +0000801 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000802 "Decl already inserted into a DeclContext");
803
804 if (FirstDecl) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000805 LastDecl->NextDeclInContext = D;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000806 LastDecl = D;
807 } else {
808 FirstDecl = LastDecl = D;
809 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000810
811 // Notify a C++ record declaration that we've added a member, so it can
812 // update it's class-specific state.
813 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
814 Record->addedMember(D);
John McCall3f9a8a62009-08-11 06:59:38 +0000815}
816
817void DeclContext::addDecl(Decl *D) {
818 addHiddenDecl(D);
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000819
820 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000821 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor44b43212008-12-11 16:49:14 +0000822}
823
Douglas Gregor074149e2009-01-05 19:45:36 +0000824/// buildLookup - Build the lookup data structure with all of the
825/// declarations in DCtx (and any other contexts linked to it or
826/// transparent contexts nested within it).
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000827void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000828 for (; DCtx; DCtx = DCtx->getNextContext()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000829 for (decl_iterator D = DCtx->decls_begin(),
830 DEnd = DCtx->decls_end();
Douglas Gregor4f3b8f82009-01-06 07:17:58 +0000831 D != DEnd; ++D) {
John McCall3f9a8a62009-08-11 06:59:38 +0000832 // Insert this declaration into the lookup structure, but only
833 // if it's semantically in its decl context. During non-lazy
834 // lookup building, this is implicitly enforced by addDecl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000835 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
John McCall3f9a8a62009-08-11 06:59:38 +0000836 if (D->getDeclContext() == DCtx)
837 makeDeclVisibleInContextImpl(ND);
Douglas Gregor074149e2009-01-05 19:45:36 +0000838
Ted Kremenekc32b1d82009-11-17 22:58:30 +0000839 // Insert any forward-declared Objective-C interfaces into the lookup
840 // data structure.
841 if (ObjCClassDecl *Class = dyn_cast<ObjCClassDecl>(*D))
842 for (ObjCClassDecl::iterator I = Class->begin(), IEnd = Class->end();
843 I != IEnd; ++I)
Ted Kremenek321c22f2009-11-18 00:28:11 +0000844 makeDeclVisibleInContextImpl(I->getInterface());
Ted Kremenekc32b1d82009-11-17 22:58:30 +0000845
Sebastian Redl410c4f22010-08-31 20:53:31 +0000846 // If this declaration is itself a transparent declaration context or
847 // inline namespace, add its members (recursively).
Douglas Gregor074149e2009-01-05 19:45:36 +0000848 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
Sebastian Redl410c4f22010-08-31 20:53:31 +0000849 if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000850 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregor074149e2009-01-05 19:45:36 +0000851 }
852 }
853}
854
Mike Stump1eb44332009-09-09 15:08:12 +0000855DeclContext::lookup_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000856DeclContext::lookup(DeclarationName Name) {
Steve Naroff0701bbb2009-01-08 17:28:14 +0000857 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000858 if (PrimaryContext != this)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000859 return PrimaryContext->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000860
John McCall76bd1f32010-06-01 09:23:16 +0000861 if (hasExternalVisibleStorage()) {
862 // Check to see if we've already cached the lookup results.
863 if (LookupPtr) {
864 StoredDeclsMap::iterator I = LookupPtr->find(Name);
865 if (I != LookupPtr->end())
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000866 return I->second.getLookupResult();
John McCall76bd1f32010-06-01 09:23:16 +0000867 }
868
869 ExternalASTSource *Source = getParentASTContext().getExternalSource();
870 return Source->FindExternalVisibleDeclsByName(this, Name);
871 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000872
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000873 /// If there is no lookup data structure, build one now by walking
Douglas Gregor44b43212008-12-11 16:49:14 +0000874 /// all of the linked DeclContexts (in declaration order!) and
875 /// inserting their values.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000876 if (!LookupPtr) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000877 buildLookup(this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000878
Douglas Gregorc36c5402009-04-09 17:29:08 +0000879 if (!LookupPtr)
Douglas Gregora5fdd9c2010-05-11 06:18:17 +0000880 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Douglas Gregorc36c5402009-04-09 17:29:08 +0000881 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000882
John McCall0c01d182010-03-24 05:22:00 +0000883 StoredDeclsMap::iterator Pos = LookupPtr->find(Name);
884 if (Pos == LookupPtr->end())
Douglas Gregora5fdd9c2010-05-11 06:18:17 +0000885 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000886 return Pos->second.getLookupResult();
Douglas Gregor44b43212008-12-11 16:49:14 +0000887}
888
Mike Stump1eb44332009-09-09 15:08:12 +0000889DeclContext::lookup_const_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000890DeclContext::lookup(DeclarationName Name) const {
891 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000892}
893
Sebastian Redl7a126a42010-08-31 00:36:30 +0000894DeclContext *DeclContext::getRedeclContext() {
Chris Lattner0cf2b192009-03-27 19:19:59 +0000895 DeclContext *Ctx = this;
Sebastian Redl410c4f22010-08-31 20:53:31 +0000896 // Skip through transparent contexts.
897 while (Ctx->isTransparentContext())
Douglas Gregorce356072009-01-06 23:51:29 +0000898 Ctx = Ctx->getParent();
899 return Ctx;
900}
901
Douglas Gregor88b70942009-02-25 22:02:03 +0000902DeclContext *DeclContext::getEnclosingNamespaceContext() {
903 DeclContext *Ctx = this;
904 // Skip through non-namespace, non-translation-unit contexts.
Sebastian Redl51a8a372010-08-31 00:36:23 +0000905 while (!Ctx->isFileContext())
Douglas Gregor88b70942009-02-25 22:02:03 +0000906 Ctx = Ctx->getParent();
907 return Ctx->getPrimaryContext();
908}
909
Sebastian Redl7a126a42010-08-31 00:36:30 +0000910bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
911 // For non-file contexts, this is equivalent to Equals.
912 if (!isFileContext())
913 return O->Equals(this);
914
915 do {
916 if (O->Equals(this))
917 return true;
918
919 const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O);
920 if (!NS || !NS->isInline())
921 break;
922 O = NS->getParent();
923 } while (O);
924
925 return false;
926}
927
John McCallab88d972009-08-31 22:39:49 +0000928void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000929 // FIXME: This feels like a hack. Should DeclarationName support
930 // template-ids, or is there a better way to keep specializations
931 // from being visible?
932 if (isa<ClassTemplateSpecializationDecl>(D))
933 return;
Eli Friedman6bc20132009-12-08 05:40:03 +0000934 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
935 if (FD->isFunctionTemplateSpecialization())
936 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000937
Steve Naroff0701bbb2009-01-08 17:28:14 +0000938 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000939 if (PrimaryContext != this) {
John McCallab88d972009-08-31 22:39:49 +0000940 PrimaryContext->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor44b43212008-12-11 16:49:14 +0000941 return;
942 }
943
944 // If we already have a lookup data structure, perform the insertion
Argyrios Kyrtzidis5586b012010-07-04 21:44:25 +0000945 // into it. If we haven't deserialized externally stored decls, deserialize
946 // them so we can add the decl. Otherwise, be lazy and don't build that
947 // structure until someone asks for it.
948 if (LookupPtr || !Recoverable || hasExternalVisibleStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000949 makeDeclVisibleInContextImpl(D);
Douglas Gregor074149e2009-01-05 19:45:36 +0000950
Sebastian Redl410c4f22010-08-31 20:53:31 +0000951 // If we are a transparent context or inline namespace, insert into our
952 // parent context, too. This operation is recursive.
953 if (isTransparentContext() || isInlineNamespace())
John McCallab88d972009-08-31 22:39:49 +0000954 getParent()->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor44b43212008-12-11 16:49:14 +0000955}
956
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000957void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000958 // Skip unnamed declarations.
959 if (!D->getDeclName())
960 return;
961
Douglas Gregorcc636682009-02-17 23:15:12 +0000962 // FIXME: This feels like a hack. Should DeclarationName support
963 // template-ids, or is there a better way to keep specializations
964 // from being visible?
965 if (isa<ClassTemplateSpecializationDecl>(D))
966 return;
967
Argyrios Kyrtzidis5586b012010-07-04 21:44:25 +0000968 ASTContext *C = 0;
969 if (!LookupPtr) {
970 C = &getParentASTContext();
971 CreateStoredDeclsMap(*C);
972 }
973
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000974 // If there is an external AST source, load any declarations it knows about
975 // with this declaration's name.
976 // If the lookup table contains an entry about this name it means that we
977 // have already checked the external source.
978 if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
979 if (hasExternalVisibleStorage() &&
980 LookupPtr->find(D->getDeclName()) == LookupPtr->end())
981 Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
982
Douglas Gregor44b43212008-12-11 16:49:14 +0000983 // Insert this declaration into the map.
John McCall0c01d182010-03-24 05:22:00 +0000984 StoredDeclsList &DeclNameEntries = (*LookupPtr)[D->getDeclName()];
Chris Lattner67762a32009-02-20 01:44:05 +0000985 if (DeclNameEntries.isNull()) {
986 DeclNameEntries.setOnlyValue(D);
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000987 return;
Douglas Gregor44b43212008-12-11 16:49:14 +0000988 }
Chris Lattner91942502009-02-20 00:55:03 +0000989
Chris Lattnerbdc3d002009-02-20 01:10:07 +0000990 // If it is possible that this is a redeclaration, check to see if there is
991 // already a decl for which declarationReplaces returns true. If there is
992 // one, just replace it and return.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000993 if (DeclNameEntries.HandleRedeclaration(D))
Chris Lattner67762a32009-02-20 01:44:05 +0000994 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000995
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000996 // Put this declaration into the appropriate slot.
Chris Lattner67762a32009-02-20 01:44:05 +0000997 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000998}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000999
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00001000void DeclContext::MaterializeVisibleDeclsFromExternalStorage() {
1001 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1002 assert(hasExternalVisibleStorage() && Source && "No external storage?");
1003
1004 if (!LookupPtr)
1005 CreateStoredDeclsMap(getParentASTContext());
1006 Source->MaterializeVisibleDecls(this);
1007}
1008
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001009/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
1010/// this context.
Mike Stump1eb44332009-09-09 15:08:12 +00001011DeclContext::udir_iterator_range
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001012DeclContext::getUsingDirectives() const {
1013 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001014 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
1015 reinterpret_cast<udir_iterator>(Result.second));
1016}
Douglas Gregor2cf26342009-04-09 22:27:44 +00001017
Ted Kremenek3478eb62010-02-11 07:12:28 +00001018//===----------------------------------------------------------------------===//
1019// Creation and Destruction of StoredDeclsMaps. //
1020//===----------------------------------------------------------------------===//
1021
John McCall0c01d182010-03-24 05:22:00 +00001022StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
1023 assert(!LookupPtr && "context already has a decls map");
1024 assert(getPrimaryContext() == this &&
1025 "creating decls map on non-primary context");
1026
1027 StoredDeclsMap *M;
1028 bool Dependent = isDependentContext();
1029 if (Dependent)
1030 M = new DependentStoredDeclsMap();
1031 else
1032 M = new StoredDeclsMap();
1033 M->Previous = C.LastSDM;
1034 C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
1035 LookupPtr = M;
Ted Kremenek3478eb62010-02-11 07:12:28 +00001036 return M;
1037}
1038
1039void ASTContext::ReleaseDeclContextMaps() {
John McCall0c01d182010-03-24 05:22:00 +00001040 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1041 // pointer because the subclass doesn't add anything that needs to
1042 // be deleted.
John McCall0c01d182010-03-24 05:22:00 +00001043 StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1044}
1045
1046void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1047 while (Map) {
1048 // Advance the iteration before we invalidate memory.
1049 llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1050
1051 if (Dependent)
1052 delete static_cast<DependentStoredDeclsMap*>(Map);
1053 else
1054 delete Map;
1055
1056 Map = Next.getPointer();
1057 Dependent = Next.getInt();
1058 }
1059}
1060
John McCall0c01d182010-03-24 05:22:00 +00001061DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1062 DeclContext *Parent,
1063 const PartialDiagnostic &PDiag) {
1064 assert(Parent->isDependentContext()
1065 && "cannot iterate dependent diagnostics of non-dependent context");
1066 Parent = Parent->getPrimaryContext();
1067 if (!Parent->LookupPtr)
1068 Parent->CreateStoredDeclsMap(C);
1069
1070 DependentStoredDeclsMap *Map
1071 = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr);
1072
Douglas Gregorb8365182010-03-29 23:56:53 +00001073 // Allocate the copy of the PartialDiagnostic via the ASTContext's
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00001074 // BumpPtrAllocator, rather than the ASTContext itself.
Douglas Gregorb8365182010-03-29 23:56:53 +00001075 PartialDiagnostic::Storage *DiagStorage = 0;
1076 if (PDiag.hasStorage())
1077 DiagStorage = new (C) PartialDiagnostic::Storage;
1078
1079 DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
John McCall0c01d182010-03-24 05:22:00 +00001080
1081 // TODO: Maybe we shouldn't reverse the order during insertion.
1082 DD->NextDiagnostic = Map->FirstDiagnostic;
1083 Map->FirstDiagnostic = DD;
1084
1085 return DD;
Ted Kremenek3478eb62010-02-11 07:12:28 +00001086}