blob: 0b958fe82b093a38ddc819d2b973c43906b4f3f2 [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
Douglas Gregorc070cc62010-06-17 23:14:26 +0000213bool Decl::isUsed(bool CheckUsedAttr) const {
Tanya Lattner12ead492010-02-17 02:17:21 +0000214 if (Used)
215 return true;
216
217 // Check for used attribute.
Douglas Gregorc070cc62010-06-17 23:14:26 +0000218 if (CheckUsedAttr && hasAttr<UsedAttr>())
Tanya Lattner12ead492010-02-17 02:17:21 +0000219 return true;
220
221 // Check redeclarations for used attribute.
222 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Douglas Gregorc070cc62010-06-17 23:14:26 +0000223 if ((CheckUsedAttr && I->hasAttr<UsedAttr>()) || I->Used)
Tanya Lattner12ead492010-02-17 02:17:21 +0000224 return true;
225 }
226
227 return false;
228}
229
230
Chris Lattner769dbdf2009-03-27 20:18:19 +0000231unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
232 switch (DeclKind) {
John McCall9488ea12009-11-17 05:59:44 +0000233 case Function:
234 case CXXMethod:
235 case CXXConstructor:
236 case CXXDestructor:
237 case CXXConversion:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000238 case EnumConstant:
239 case Var:
240 case ImplicitParam:
241 case ParmVar:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000242 case NonTypeTemplateParm:
243 case ObjCMethod:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000244 case ObjCProperty:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000245 return IDNS_Ordinary;
John McCalld04efc92010-04-23 02:41:41 +0000246
John McCall0d6b1642010-04-23 18:46:30 +0000247 case ObjCCompatibleAlias:
248 case ObjCInterface:
249 return IDNS_Ordinary | IDNS_Type;
250
251 case Typedef:
252 case UnresolvedUsingTypename:
253 case TemplateTypeParm:
254 return IDNS_Ordinary | IDNS_Type;
255
John McCall9488ea12009-11-17 05:59:44 +0000256 case UsingShadow:
257 return 0; // we'll actually overwrite this later
258
John McCall7ba107a2009-11-18 02:36:19 +0000259 case UnresolvedUsingValue:
John McCall7ba107a2009-11-18 02:36:19 +0000260 return IDNS_Ordinary | IDNS_Using;
John McCall9488ea12009-11-17 05:59:44 +0000261
262 case Using:
263 return IDNS_Using;
264
Chris Lattner769dbdf2009-03-27 20:18:19 +0000265 case ObjCProtocol:
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000266 return IDNS_ObjCProtocol;
Mike Stump1eb44332009-09-09 15:08:12 +0000267
Chris Lattner769dbdf2009-03-27 20:18:19 +0000268 case Field:
269 case ObjCAtDefsField:
270 case ObjCIvar:
271 return IDNS_Member;
Mike Stump1eb44332009-09-09 15:08:12 +0000272
Chris Lattner769dbdf2009-03-27 20:18:19 +0000273 case Record:
274 case CXXRecord:
275 case Enum:
John McCall0d6b1642010-04-23 18:46:30 +0000276 return IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000277
Chris Lattner769dbdf2009-03-27 20:18:19 +0000278 case Namespace:
John McCall0d6b1642010-04-23 18:46:30 +0000279 case NamespaceAlias:
280 return IDNS_Namespace;
281
Chris Lattner769dbdf2009-03-27 20:18:19 +0000282 case FunctionTemplate:
John McCall0d6b1642010-04-23 18:46:30 +0000283 return IDNS_Ordinary;
284
Chris Lattner769dbdf2009-03-27 20:18:19 +0000285 case ClassTemplate:
286 case TemplateTemplateParm:
John McCall0d6b1642010-04-23 18:46:30 +0000287 return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000288
Chris Lattner769dbdf2009-03-27 20:18:19 +0000289 // Never have names.
John McCall02cace72009-08-28 07:59:38 +0000290 case Friend:
John McCalldd4a3b02009-09-16 22:47:08 +0000291 case FriendTemplate:
Abramo Bagnara6206d532010-06-05 05:09:32 +0000292 case AccessSpec:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000293 case LinkageSpec:
294 case FileScopeAsm:
295 case StaticAssert:
296 case ObjCClass:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000297 case ObjCPropertyImpl:
298 case ObjCForwardProtocol:
299 case Block:
300 case TranslationUnit:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000301
Chris Lattner769dbdf2009-03-27 20:18:19 +0000302 case UsingDirective:
303 case ClassTemplateSpecialization:
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000304 case ClassTemplatePartialSpecialization:
Douglas Gregorbd4187b2010-04-22 23:19:50 +0000305 case ObjCImplementation:
306 case ObjCCategory:
307 case ObjCCategoryImpl:
308 // Never looked up by name.
Chris Lattner769dbdf2009-03-27 20:18:19 +0000309 return 0;
310 }
John McCall9488ea12009-11-17 05:59:44 +0000311
312 return 0;
Eli Friedman56d29372008-06-07 16:52:53 +0000313}
314
Sean Huntcf807c42010-08-18 23:23:40 +0000315void Decl::setAttrs(const AttrVec &attrs) {
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000316 assert(!HasAttrs && "Decl already contains attrs.");
317
Sean Huntcf807c42010-08-18 23:23:40 +0000318 AttrVec &AttrBlank = getASTContext().getDeclAttrs(this);
319 assert(AttrBlank.empty() && "HasAttrs was wrong?");
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000320
321 AttrBlank = attrs;
322 HasAttrs = true;
323}
324
Sean Huntcf807c42010-08-18 23:23:40 +0000325void Decl::dropAttrs() {
Eli Friedman56d29372008-06-07 16:52:53 +0000326 if (!HasAttrs) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Eli Friedman56d29372008-06-07 16:52:53 +0000328 HasAttrs = false;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000329 getASTContext().eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000330}
331
Sean Huntcf807c42010-08-18 23:23:40 +0000332const AttrVec &Decl::getAttrs() const {
333 assert(HasAttrs && "No attrs to get!");
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000334 return getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000335}
336
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000337void Decl::swapAttrs(Decl *RHS) {
Eli Friedman56d29372008-06-07 16:52:53 +0000338 bool HasLHSAttr = this->HasAttrs;
339 bool HasRHSAttr = RHS->HasAttrs;
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Eli Friedman56d29372008-06-07 16:52:53 +0000341 // Usually, neither decl has attrs, nothing to do.
342 if (!HasLHSAttr && !HasRHSAttr) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000343
Eli Friedman56d29372008-06-07 16:52:53 +0000344 // If 'this' has no attrs, swap the other way.
345 if (!HasLHSAttr)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000346 return RHS->swapAttrs(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000348 ASTContext &Context = getASTContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Eli Friedman56d29372008-06-07 16:52:53 +0000350 // Handle the case when both decls have attrs.
351 if (HasRHSAttr) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000352 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman56d29372008-06-07 16:52:53 +0000353 return;
354 }
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Eli Friedman56d29372008-06-07 16:52:53 +0000356 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor68584ed2009-06-18 16:11:24 +0000357 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
358 Context.eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000359 this->HasAttrs = false;
360 RHS->HasAttrs = true;
361}
362
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000363Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000364 Decl::Kind DK = D->getDeclKind();
365 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000366#define DECL(NAME, BASE)
367#define DECL_CONTEXT(NAME) \
368 case Decl::NAME: \
369 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
370#define DECL_CONTEXT_BASE(NAME)
371#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000372 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000373#define DECL(NAME, BASE)
374#define DECL_CONTEXT_BASE(NAME) \
375 if (DK >= first##NAME && DK <= last##NAME) \
376 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
377#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000378 assert(false && "a decl that inherits DeclContext isn't handled");
379 return 0;
380 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000381}
382
383DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000384 Decl::Kind DK = D->getKind();
385 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000386#define DECL(NAME, BASE)
387#define DECL_CONTEXT(NAME) \
388 case Decl::NAME: \
389 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
390#define DECL_CONTEXT_BASE(NAME)
391#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000392 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000393#define DECL(NAME, BASE)
394#define DECL_CONTEXT_BASE(NAME) \
395 if (DK >= first##NAME && DK <= last##NAME) \
396 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
397#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000398 assert(false && "a decl that inherits DeclContext isn't handled");
399 return 0;
400 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000401}
402
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000403SourceLocation Decl::getBodyRBrace() const {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000404 // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
405 // FunctionDecl stores EndRangeLoc for this purpose.
406 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
407 const FunctionDecl *Definition;
408 if (FD->hasBody(Definition))
409 return Definition->getSourceRange().getEnd();
410 return SourceLocation();
411 }
412
Argyrios Kyrtzidis6717ef42010-07-07 11:31:27 +0000413 if (Stmt *Body = getBody())
414 return Body->getSourceRange().getEnd();
415
416 return SourceLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +0000417}
418
Anders Carlsson1329c272009-03-25 23:38:06 +0000419#ifndef NDEBUG
420void Decl::CheckAccessDeclContext() const {
Argyrios Kyrtzidis04aed0e2010-07-02 11:55:44 +0000421 // FIXME: Disable this until rdar://8146294 "access specifier for inner class
422 // templates is not set or checked" is fixed.
423 return;
John McCall46460a62010-01-20 21:53:11 +0000424 // Suppress this check if any of the following hold:
425 // 1. this is the translation unit (and thus has no parent)
426 // 2. this is a template parameter (and thus doesn't belong to its context)
Argyrios Kyrtzidis04aed0e2010-07-02 11:55:44 +0000427 // 3. the context is not a record
428 // 4. it's invalid
Anders Carlsson35eda442009-08-29 20:47:47 +0000429 if (isa<TranslationUnitDecl>(this) ||
Argyrios Kyrtzidis04aed0e2010-07-02 11:55:44 +0000430 isa<TemplateTypeParmDecl>(this) ||
Douglas Gregorfdd8ab12010-02-22 17:53:38 +0000431 !isa<CXXRecordDecl>(getDeclContext()) ||
432 isInvalidDecl())
Anders Carlsson35eda442009-08-29 20:47:47 +0000433 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000434
435 assert(Access != AS_none &&
Anders Carlsson1329c272009-03-25 23:38:06 +0000436 "Access specifier is AS_none inside a record decl");
437}
438
439#endif
440
Eli Friedman56d29372008-06-07 16:52:53 +0000441//===----------------------------------------------------------------------===//
442// DeclContext Implementation
443//===----------------------------------------------------------------------===//
444
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000445bool DeclContext::classof(const Decl *D) {
446 switch (D->getKind()) {
Sean Hunt9a555912010-05-30 07:21:58 +0000447#define DECL(NAME, BASE)
448#define DECL_CONTEXT(NAME) case Decl::NAME:
449#define DECL_CONTEXT_BASE(NAME)
450#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000451 return true;
452 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000453#define DECL(NAME, BASE)
454#define DECL_CONTEXT_BASE(NAME) \
455 if (D->getKind() >= Decl::first##NAME && \
456 D->getKind() <= Decl::last##NAME) \
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000457 return true;
Sean Hunt9a555912010-05-30 07:21:58 +0000458#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000459 return false;
460 }
461}
462
Douglas Gregora2da7802010-07-25 18:38:02 +0000463DeclContext::~DeclContext() { }
Douglas Gregor44b43212008-12-11 16:49:14 +0000464
Douglas Gregore942bbe2009-09-10 16:57:35 +0000465/// \brief Find the parent context of this context that will be
466/// used for unqualified name lookup.
467///
468/// Generally, the parent lookup context is the semantic context. However, for
469/// a friend function the parent lookup context is the lexical context, which
470/// is the class in which the friend is declared.
471DeclContext *DeclContext::getLookupParent() {
472 // FIXME: Find a better way to identify friends
473 if (isa<FunctionDecl>(this))
Sebastian Redl7a126a42010-08-31 00:36:30 +0000474 if (getParent()->getRedeclContext()->isFileContext() &&
475 getLexicalParent()->getRedeclContext()->isRecord())
Douglas Gregore942bbe2009-09-10 16:57:35 +0000476 return getLexicalParent();
477
478 return getParent();
479}
480
Sebastian Redl410c4f22010-08-31 20:53:31 +0000481bool DeclContext::isInlineNamespace() const {
482 return isNamespace() &&
483 cast<NamespaceDecl>(this)->isInline();
484}
485
Douglas Gregorbc221632009-05-28 16:34:51 +0000486bool DeclContext::isDependentContext() const {
487 if (isFileContext())
488 return false;
489
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000490 if (isa<ClassTemplatePartialSpecializationDecl>(this))
491 return true;
492
Douglas Gregorbc221632009-05-28 16:34:51 +0000493 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
494 if (Record->getDescribedClassTemplate())
495 return true;
496
John McCall0c01d182010-03-24 05:22:00 +0000497 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregorbc221632009-05-28 16:34:51 +0000498 if (Function->getDescribedFunctionTemplate())
499 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000500
John McCall0c01d182010-03-24 05:22:00 +0000501 // Friend function declarations are dependent if their *lexical*
502 // context is dependent.
503 if (cast<Decl>(this)->getFriendObjectKind())
504 return getLexicalParent()->isDependentContext();
505 }
506
Douglas Gregorbc221632009-05-28 16:34:51 +0000507 return getParent() && getParent()->isDependentContext();
508}
509
Douglas Gregor074149e2009-01-05 19:45:36 +0000510bool DeclContext::isTransparentContext() const {
511 if (DeclKind == Decl::Enum)
512 return true; // FIXME: Check for C++0x scoped enums
513 else if (DeclKind == Decl::LinkageSpec)
514 return true;
Sean Hunt9a555912010-05-30 07:21:58 +0000515 else if (DeclKind >= Decl::firstRecord && DeclKind <= Decl::lastRecord)
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000516 return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
Douglas Gregor074149e2009-01-05 19:45:36 +0000517
518 return false;
519}
520
Sebastian Redl7a126a42010-08-31 00:36:30 +0000521bool DeclContext::Encloses(const DeclContext *DC) const {
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000522 if (getPrimaryContext() != this)
523 return getPrimaryContext()->Encloses(DC);
Mike Stump1eb44332009-09-09 15:08:12 +0000524
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000525 for (; DC; DC = DC->getParent())
526 if (DC->getPrimaryContext() == this)
527 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000528 return false;
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000529}
530
Steve Naroff0701bbb2009-01-08 17:28:14 +0000531DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000532 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000533 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000534 case Decl::LinkageSpec:
Mike Stump1eb44332009-09-09 15:08:12 +0000535 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000536 // There is only one DeclContext for these entities.
537 return this;
538
539 case Decl::Namespace:
540 // The original namespace is our primary context.
541 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
542
Douglas Gregor44b43212008-12-11 16:49:14 +0000543 case Decl::ObjCMethod:
544 return this;
545
546 case Decl::ObjCInterface:
Steve Naroff0701bbb2009-01-08 17:28:14 +0000547 case Decl::ObjCProtocol:
548 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000549 // FIXME: Can Objective-C interfaces be forward-declared?
550 return this;
551
Steve Naroff0701bbb2009-01-08 17:28:14 +0000552 case Decl::ObjCImplementation:
553 case Decl::ObjCCategoryImpl:
554 return this;
555
Douglas Gregor44b43212008-12-11 16:49:14 +0000556 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000557 if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000558 // If this is a tag type that has a definition or is currently
559 // being defined, that definition is our primary context.
John McCall3cb0ebd2010-03-10 03:28:59 +0000560 TagDecl *Tag = cast<TagDecl>(this);
561 assert(isa<TagType>(Tag->TypeForDecl) ||
562 isa<InjectedClassNameType>(Tag->TypeForDecl));
563
564 if (TagDecl *Def = Tag->getDefinition())
565 return Def;
566
567 if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) {
568 const TagType *TagTy = cast<TagType>(Tag->TypeForDecl);
569 if (TagTy->isBeingDefined())
570 // FIXME: is it necessarily being defined in the decl
571 // that owns the type?
572 return TagTy->getDecl();
573 }
574
575 return Tag;
Douglas Gregorcc636682009-02-17 23:15:12 +0000576 }
577
Sean Hunt9a555912010-05-30 07:21:58 +0000578 assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
Douglas Gregor44b43212008-12-11 16:49:14 +0000579 "Unknown DeclContext kind");
580 return this;
581 }
582}
583
584DeclContext *DeclContext::getNextContext() {
585 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000586 case Decl::Namespace:
587 // Return the next namespace
588 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
589
590 default:
Douglas Gregor44b43212008-12-11 16:49:14 +0000591 return 0;
592 }
593}
594
Douglas Gregor2cf26342009-04-09 22:27:44 +0000595/// \brief Load the declarations within this lexical storage from an
596/// external source.
Mike Stump1eb44332009-09-09 15:08:12 +0000597void
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000598DeclContext::LoadLexicalDeclsFromExternalStorage() const {
599 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000600 assert(hasExternalLexicalStorage() && Source && "No external storage?");
601
Argyrios Kyrtzidis0dbbc042010-07-30 10:03:23 +0000602 // Notify that we have a DeclContext that is initializing.
603 ExternalASTSource::Deserializing ADeclContext(Source);
604
John McCall76bd1f32010-06-01 09:23:16 +0000605 llvm::SmallVector<Decl*, 64> Decls;
606 if (Source->FindExternalLexicalDecls(this, Decls))
Douglas Gregor2cf26342009-04-09 22:27:44 +0000607 return;
608
609 // There is no longer any lexical storage in this context
610 ExternalLexicalStorage = false;
611
612 if (Decls.empty())
613 return;
614
615 // Resolve all of the declaration IDs into declarations, building up
616 // a chain of declarations via the Decl::NextDeclInContext field.
617 Decl *FirstNewDecl = 0;
618 Decl *PrevDecl = 0;
619 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
John McCall76bd1f32010-06-01 09:23:16 +0000620 Decl *D = Decls[I];
Douglas Gregor2cf26342009-04-09 22:27:44 +0000621 if (PrevDecl)
622 PrevDecl->NextDeclInContext = D;
623 else
624 FirstNewDecl = D;
625
626 PrevDecl = D;
627 }
628
629 // Splice the newly-read declarations into the beginning of the list
630 // of declarations.
631 PrevDecl->NextDeclInContext = FirstDecl;
632 FirstDecl = FirstNewDecl;
633 if (!LastDecl)
634 LastDecl = PrevDecl;
635}
636
John McCall76bd1f32010-06-01 09:23:16 +0000637DeclContext::lookup_result
638ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
639 DeclarationName Name) {
640 ASTContext &Context = DC->getParentASTContext();
641 StoredDeclsMap *Map;
642 if (!(Map = DC->LookupPtr))
643 Map = DC->CreateStoredDeclsMap(Context);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000644
John McCall76bd1f32010-06-01 09:23:16 +0000645 StoredDeclsList &List = (*Map)[Name];
646 assert(List.isNull());
647 (void) List;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000648
John McCall76bd1f32010-06-01 09:23:16 +0000649 return DeclContext::lookup_result();
650}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000651
John McCall76bd1f32010-06-01 09:23:16 +0000652DeclContext::lookup_result
653ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +0000654 DeclarationName Name,
655 llvm::SmallVectorImpl<NamedDecl*> &Decls) {
656 ASTContext &Context = DC->getParentASTContext();;
657
658 StoredDeclsMap *Map;
659 if (!(Map = DC->LookupPtr))
660 Map = DC->CreateStoredDeclsMap(Context);
661
662 StoredDeclsList &List = (*Map)[Name];
663 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
664 if (List.isNull())
665 List.setOnlyValue(Decls[I]);
666 else
667 List.AddSubsequentDecl(Decls[I]);
668 }
669
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000670 return List.getLookupResult();
John McCall76bd1f32010-06-01 09:23:16 +0000671}
672
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000673void ExternalASTSource::MaterializeVisibleDeclsForName(const DeclContext *DC,
674 DeclarationName Name,
675 llvm::SmallVectorImpl<NamedDecl*> &Decls) {
676 assert(DC->LookupPtr);
677 StoredDeclsMap &Map = *DC->LookupPtr;
678
679 // If there's an entry in the table the visible decls for this name have
680 // already been deserialized.
681 if (Map.find(Name) == Map.end()) {
682 StoredDeclsList &List = Map[Name];
683 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
684 if (List.isNull())
685 List.setOnlyValue(Decls[I]);
686 else
687 List.AddSubsequentDecl(Decls[I]);
688 }
689 }
690}
691
Sebastian Redl681d7232010-07-27 00:17:23 +0000692DeclContext::decl_iterator DeclContext::noload_decls_begin() const {
693 return decl_iterator(FirstDecl);
694}
695
696DeclContext::decl_iterator DeclContext::noload_decls_end() const {
697 return decl_iterator();
698}
699
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000700DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000701 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000702 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000703
704 // FIXME: Check whether we need to load some declarations from
705 // external storage.
Mike Stump1eb44332009-09-09 15:08:12 +0000706 return decl_iterator(FirstDecl);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000707}
708
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000709DeclContext::decl_iterator DeclContext::decls_end() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000710 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000711 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000712
Mike Stump1eb44332009-09-09 15:08:12 +0000713 return decl_iterator();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000714}
715
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000716bool DeclContext::decls_empty() const {
Douglas Gregor8038d512009-04-10 17:25:41 +0000717 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000718 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor8038d512009-04-10 17:25:41 +0000719
720 return !FirstDecl;
721}
722
John McCall9f54ad42009-12-10 09:41:52 +0000723void DeclContext::removeDecl(Decl *D) {
724 assert(D->getLexicalDeclContext() == this &&
725 "decl being removed from non-lexical context");
726 assert((D->NextDeclInContext || D == LastDecl) &&
727 "decl is not in decls list");
728
729 // Remove D from the decl chain. This is O(n) but hopefully rare.
730 if (D == FirstDecl) {
731 if (D == LastDecl)
732 FirstDecl = LastDecl = 0;
733 else
734 FirstDecl = D->NextDeclInContext;
735 } else {
736 for (Decl *I = FirstDecl; true; I = I->NextDeclInContext) {
737 assert(I && "decl not found in linked list");
738 if (I->NextDeclInContext == D) {
739 I->NextDeclInContext = D->NextDeclInContext;
740 if (D == LastDecl) LastDecl = I;
741 break;
742 }
743 }
744 }
745
746 // Mark that D is no longer in the decl chain.
747 D->NextDeclInContext = 0;
748
749 // Remove D from the lookup table if necessary.
750 if (isa<NamedDecl>(D)) {
751 NamedDecl *ND = cast<NamedDecl>(D);
752
John McCall0c01d182010-03-24 05:22:00 +0000753 StoredDeclsMap *Map = getPrimaryContext()->LookupPtr;
754 if (!Map) return;
John McCall9f54ad42009-12-10 09:41:52 +0000755
John McCall9f54ad42009-12-10 09:41:52 +0000756 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
757 assert(Pos != Map->end() && "no lookup entry for decl");
758 Pos->second.remove(ND);
759 }
760}
761
John McCall3f9a8a62009-08-11 06:59:38 +0000762void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner7f0be132009-02-20 00:56:18 +0000763 assert(D->getLexicalDeclContext() == this &&
764 "Decl inserted into wrong lexical context");
Mike Stump1eb44332009-09-09 15:08:12 +0000765 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000766 "Decl already inserted into a DeclContext");
767
768 if (FirstDecl) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000769 LastDecl->NextDeclInContext = D;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000770 LastDecl = D;
771 } else {
772 FirstDecl = LastDecl = D;
773 }
John McCall3f9a8a62009-08-11 06:59:38 +0000774}
775
776void DeclContext::addDecl(Decl *D) {
777 addHiddenDecl(D);
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000778
779 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000780 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor44b43212008-12-11 16:49:14 +0000781}
782
Douglas Gregor074149e2009-01-05 19:45:36 +0000783/// buildLookup - Build the lookup data structure with all of the
784/// declarations in DCtx (and any other contexts linked to it or
785/// transparent contexts nested within it).
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000786void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000787 for (; DCtx; DCtx = DCtx->getNextContext()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000788 for (decl_iterator D = DCtx->decls_begin(),
789 DEnd = DCtx->decls_end();
Douglas Gregor4f3b8f82009-01-06 07:17:58 +0000790 D != DEnd; ++D) {
John McCall3f9a8a62009-08-11 06:59:38 +0000791 // Insert this declaration into the lookup structure, but only
792 // if it's semantically in its decl context. During non-lazy
793 // lookup building, this is implicitly enforced by addDecl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000794 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
John McCall3f9a8a62009-08-11 06:59:38 +0000795 if (D->getDeclContext() == DCtx)
796 makeDeclVisibleInContextImpl(ND);
Douglas Gregor074149e2009-01-05 19:45:36 +0000797
Ted Kremenekc32b1d82009-11-17 22:58:30 +0000798 // Insert any forward-declared Objective-C interfaces into the lookup
799 // data structure.
800 if (ObjCClassDecl *Class = dyn_cast<ObjCClassDecl>(*D))
801 for (ObjCClassDecl::iterator I = Class->begin(), IEnd = Class->end();
802 I != IEnd; ++I)
Ted Kremenek321c22f2009-11-18 00:28:11 +0000803 makeDeclVisibleInContextImpl(I->getInterface());
Ted Kremenekc32b1d82009-11-17 22:58:30 +0000804
Sebastian Redl410c4f22010-08-31 20:53:31 +0000805 // If this declaration is itself a transparent declaration context or
806 // inline namespace, add its members (recursively).
Douglas Gregor074149e2009-01-05 19:45:36 +0000807 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
Sebastian Redl410c4f22010-08-31 20:53:31 +0000808 if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000809 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregor074149e2009-01-05 19:45:36 +0000810 }
811 }
812}
813
Mike Stump1eb44332009-09-09 15:08:12 +0000814DeclContext::lookup_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000815DeclContext::lookup(DeclarationName Name) {
Steve Naroff0701bbb2009-01-08 17:28:14 +0000816 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000817 if (PrimaryContext != this)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000818 return PrimaryContext->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000819
John McCall76bd1f32010-06-01 09:23:16 +0000820 if (hasExternalVisibleStorage()) {
821 // Check to see if we've already cached the lookup results.
822 if (LookupPtr) {
823 StoredDeclsMap::iterator I = LookupPtr->find(Name);
824 if (I != LookupPtr->end())
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000825 return I->second.getLookupResult();
John McCall76bd1f32010-06-01 09:23:16 +0000826 }
827
828 ExternalASTSource *Source = getParentASTContext().getExternalSource();
829 return Source->FindExternalVisibleDeclsByName(this, Name);
830 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000831
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000832 /// If there is no lookup data structure, build one now by walking
Douglas Gregor44b43212008-12-11 16:49:14 +0000833 /// all of the linked DeclContexts (in declaration order!) and
834 /// inserting their values.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000835 if (!LookupPtr) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000836 buildLookup(this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000837
Douglas Gregorc36c5402009-04-09 17:29:08 +0000838 if (!LookupPtr)
Douglas Gregora5fdd9c2010-05-11 06:18:17 +0000839 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Douglas Gregorc36c5402009-04-09 17:29:08 +0000840 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000841
John McCall0c01d182010-03-24 05:22:00 +0000842 StoredDeclsMap::iterator Pos = LookupPtr->find(Name);
843 if (Pos == LookupPtr->end())
Douglas Gregora5fdd9c2010-05-11 06:18:17 +0000844 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000845 return Pos->second.getLookupResult();
Douglas Gregor44b43212008-12-11 16:49:14 +0000846}
847
Mike Stump1eb44332009-09-09 15:08:12 +0000848DeclContext::lookup_const_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000849DeclContext::lookup(DeclarationName Name) const {
850 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000851}
852
Sebastian Redl7a126a42010-08-31 00:36:30 +0000853DeclContext *DeclContext::getRedeclContext() {
Chris Lattner0cf2b192009-03-27 19:19:59 +0000854 DeclContext *Ctx = this;
Sebastian Redl410c4f22010-08-31 20:53:31 +0000855 // Skip through transparent contexts.
856 while (Ctx->isTransparentContext())
Douglas Gregorce356072009-01-06 23:51:29 +0000857 Ctx = Ctx->getParent();
858 return Ctx;
859}
860
Douglas Gregor88b70942009-02-25 22:02:03 +0000861DeclContext *DeclContext::getEnclosingNamespaceContext() {
862 DeclContext *Ctx = this;
863 // Skip through non-namespace, non-translation-unit contexts.
Sebastian Redl51a8a372010-08-31 00:36:23 +0000864 while (!Ctx->isFileContext())
Douglas Gregor88b70942009-02-25 22:02:03 +0000865 Ctx = Ctx->getParent();
866 return Ctx->getPrimaryContext();
867}
868
Sebastian Redl7a126a42010-08-31 00:36:30 +0000869bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
870 // For non-file contexts, this is equivalent to Equals.
871 if (!isFileContext())
872 return O->Equals(this);
873
874 do {
875 if (O->Equals(this))
876 return true;
877
878 const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O);
879 if (!NS || !NS->isInline())
880 break;
881 O = NS->getParent();
882 } while (O);
883
884 return false;
885}
886
John McCallab88d972009-08-31 22:39:49 +0000887void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000888 // FIXME: This feels like a hack. Should DeclarationName support
889 // template-ids, or is there a better way to keep specializations
890 // from being visible?
891 if (isa<ClassTemplateSpecializationDecl>(D))
892 return;
Eli Friedman6bc20132009-12-08 05:40:03 +0000893 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
894 if (FD->isFunctionTemplateSpecialization())
895 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000896
Steve Naroff0701bbb2009-01-08 17:28:14 +0000897 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000898 if (PrimaryContext != this) {
John McCallab88d972009-08-31 22:39:49 +0000899 PrimaryContext->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor44b43212008-12-11 16:49:14 +0000900 return;
901 }
902
903 // If we already have a lookup data structure, perform the insertion
Argyrios Kyrtzidis5586b012010-07-04 21:44:25 +0000904 // into it. If we haven't deserialized externally stored decls, deserialize
905 // them so we can add the decl. Otherwise, be lazy and don't build that
906 // structure until someone asks for it.
907 if (LookupPtr || !Recoverable || hasExternalVisibleStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000908 makeDeclVisibleInContextImpl(D);
Douglas Gregor074149e2009-01-05 19:45:36 +0000909
Sebastian Redl410c4f22010-08-31 20:53:31 +0000910 // If we are a transparent context or inline namespace, insert into our
911 // parent context, too. This operation is recursive.
912 if (isTransparentContext() || isInlineNamespace())
John McCallab88d972009-08-31 22:39:49 +0000913 getParent()->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor44b43212008-12-11 16:49:14 +0000914}
915
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000916void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000917 // Skip unnamed declarations.
918 if (!D->getDeclName())
919 return;
920
Douglas Gregorcc636682009-02-17 23:15:12 +0000921 // FIXME: This feels like a hack. Should DeclarationName support
922 // template-ids, or is there a better way to keep specializations
923 // from being visible?
924 if (isa<ClassTemplateSpecializationDecl>(D))
925 return;
926
Argyrios Kyrtzidis5586b012010-07-04 21:44:25 +0000927 ASTContext *C = 0;
928 if (!LookupPtr) {
929 C = &getParentASTContext();
930 CreateStoredDeclsMap(*C);
931 }
932
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000933 // If there is an external AST source, load any declarations it knows about
934 // with this declaration's name.
935 // If the lookup table contains an entry about this name it means that we
936 // have already checked the external source.
937 if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
938 if (hasExternalVisibleStorage() &&
939 LookupPtr->find(D->getDeclName()) == LookupPtr->end())
940 Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
941
Douglas Gregor44b43212008-12-11 16:49:14 +0000942 // Insert this declaration into the map.
John McCall0c01d182010-03-24 05:22:00 +0000943 StoredDeclsList &DeclNameEntries = (*LookupPtr)[D->getDeclName()];
Chris Lattner67762a32009-02-20 01:44:05 +0000944 if (DeclNameEntries.isNull()) {
945 DeclNameEntries.setOnlyValue(D);
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000946 return;
Douglas Gregor44b43212008-12-11 16:49:14 +0000947 }
Chris Lattner91942502009-02-20 00:55:03 +0000948
Chris Lattnerbdc3d002009-02-20 01:10:07 +0000949 // If it is possible that this is a redeclaration, check to see if there is
950 // already a decl for which declarationReplaces returns true. If there is
951 // one, just replace it and return.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000952 if (DeclNameEntries.HandleRedeclaration(D))
Chris Lattner67762a32009-02-20 01:44:05 +0000953 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000954
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000955 // Put this declaration into the appropriate slot.
Chris Lattner67762a32009-02-20 01:44:05 +0000956 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000957}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000958
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000959void DeclContext::MaterializeVisibleDeclsFromExternalStorage() {
960 ExternalASTSource *Source = getParentASTContext().getExternalSource();
961 assert(hasExternalVisibleStorage() && Source && "No external storage?");
962
963 if (!LookupPtr)
964 CreateStoredDeclsMap(getParentASTContext());
965 Source->MaterializeVisibleDecls(this);
966}
967
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000968/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
969/// this context.
Mike Stump1eb44332009-09-09 15:08:12 +0000970DeclContext::udir_iterator_range
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000971DeclContext::getUsingDirectives() const {
972 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000973 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
974 reinterpret_cast<udir_iterator>(Result.second));
975}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000976
Ted Kremenek3478eb62010-02-11 07:12:28 +0000977//===----------------------------------------------------------------------===//
978// Creation and Destruction of StoredDeclsMaps. //
979//===----------------------------------------------------------------------===//
980
John McCall0c01d182010-03-24 05:22:00 +0000981StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
982 assert(!LookupPtr && "context already has a decls map");
983 assert(getPrimaryContext() == this &&
984 "creating decls map on non-primary context");
985
986 StoredDeclsMap *M;
987 bool Dependent = isDependentContext();
988 if (Dependent)
989 M = new DependentStoredDeclsMap();
990 else
991 M = new StoredDeclsMap();
992 M->Previous = C.LastSDM;
993 C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
994 LookupPtr = M;
Ted Kremenek3478eb62010-02-11 07:12:28 +0000995 return M;
996}
997
998void ASTContext::ReleaseDeclContextMaps() {
John McCall0c01d182010-03-24 05:22:00 +0000999 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1000 // pointer because the subclass doesn't add anything that needs to
1001 // be deleted.
John McCall0c01d182010-03-24 05:22:00 +00001002 StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1003}
1004
1005void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1006 while (Map) {
1007 // Advance the iteration before we invalidate memory.
1008 llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1009
1010 if (Dependent)
1011 delete static_cast<DependentStoredDeclsMap*>(Map);
1012 else
1013 delete Map;
1014
1015 Map = Next.getPointer();
1016 Dependent = Next.getInt();
1017 }
1018}
1019
John McCall0c01d182010-03-24 05:22:00 +00001020DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1021 DeclContext *Parent,
1022 const PartialDiagnostic &PDiag) {
1023 assert(Parent->isDependentContext()
1024 && "cannot iterate dependent diagnostics of non-dependent context");
1025 Parent = Parent->getPrimaryContext();
1026 if (!Parent->LookupPtr)
1027 Parent->CreateStoredDeclsMap(C);
1028
1029 DependentStoredDeclsMap *Map
1030 = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr);
1031
Douglas Gregorb8365182010-03-29 23:56:53 +00001032 // Allocate the copy of the PartialDiagnostic via the ASTContext's
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00001033 // BumpPtrAllocator, rather than the ASTContext itself.
Douglas Gregorb8365182010-03-29 23:56:53 +00001034 PartialDiagnostic::Storage *DiagStorage = 0;
1035 if (PDiag.hasStorage())
1036 DiagStorage = new (C) PartialDiagnostic::Storage;
1037
1038 DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
John McCall0c01d182010-03-24 05:22:00 +00001039
1040 // TODO: Maybe we shouldn't reverse the order during insertion.
1041 DD->NextDiagnostic = Map->FirstDiagnostic;
1042 Map->FirstDiagnostic = DD;
1043
1044 return DD;
Ted Kremenek3478eb62010-02-11 07:12:28 +00001045}