blob: dfc5a6ae5d314d4386d9e8dcc61647df9f859e4e [file] [log] [blame]
Eli Friedman56d29372008-06-07 16:52:53 +00001//===--- DeclBase.cpp - Declaration AST Node Implementation ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Decl and DeclContext classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclBase.h"
Douglas Gregor64650af2009-02-02 23:39:07 +000015#include "clang/AST/Decl.h"
Douglas Gregorc2ee10d2009-04-07 17:20:56 +000016#include "clang/AST/DeclContextInternals.h"
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +000017#include "clang/AST/DeclCXX.h"
John McCall92b7f702010-03-11 07:50:04 +000018#include "clang/AST/DeclFriend.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000019#include "clang/AST/DeclObjC.h"
20#include "clang/AST/DeclTemplate.h"
John McCall0c01d182010-03-24 05:22:00 +000021#include "clang/AST/DependentDiagnostic.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000022#include "clang/AST/ExternalASTSource.h"
Eli Friedman56d29372008-06-07 16:52:53 +000023#include "clang/AST/ASTContext.h"
Douglas Gregor44b43212008-12-11 16:49:14 +000024#include "clang/AST/Type.h"
Sebastian Redld3a413d2009-04-26 20:35:05 +000025#include "clang/AST/Stmt.h"
26#include "clang/AST/StmtCXX.h"
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +000027#include "clang/AST/ASTMutationListener.h"
Eli Friedman56d29372008-06-07 16:52:53 +000028#include "llvm/ADT/DenseMap.h"
Chris Lattner49f28ca2009-03-05 08:00:35 +000029#include "llvm/Support/raw_ostream.h"
Douglas Gregor6ed40e32008-12-23 21:05:05 +000030#include <algorithm>
Chris Lattner3daed522009-03-02 22:20:04 +000031#include <cstdio>
Douglas Gregor3fc749d2008-12-23 00:26:44 +000032#include <vector>
Eli Friedman56d29372008-06-07 16:52:53 +000033using namespace clang;
34
35//===----------------------------------------------------------------------===//
36// Statistics
37//===----------------------------------------------------------------------===//
38
Sean Hunt9a555912010-05-30 07:21:58 +000039#define DECL(DERIVED, BASE) static int n##DERIVED##s = 0;
40#define ABSTRACT_DECL(DECL)
41#include "clang/AST/DeclNodes.inc"
Eli Friedman56d29372008-06-07 16:52:53 +000042
43static bool StatSwitch = false;
44
Eli Friedman56d29372008-06-07 16:52:53 +000045const char *Decl::getDeclKindName() const {
46 switch (DeclKind) {
Sean Hunt9a555912010-05-30 07:21:58 +000047 default: assert(0 && "Declaration not in DeclNodes.inc!");
48#define DECL(DERIVED, BASE) case DERIVED: return #DERIVED;
49#define ABSTRACT_DECL(DECL)
50#include "clang/AST/DeclNodes.inc"
Eli Friedman56d29372008-06-07 16:52:53 +000051 }
52}
53
Douglas Gregor42738572010-03-05 00:26:45 +000054void Decl::setInvalidDecl(bool Invalid) {
55 InvalidDecl = Invalid;
56 if (Invalid) {
57 // Defensive maneuver for ill-formed code: we're likely not to make it to
58 // a point where we set the access specifier, so default it to "public"
59 // to avoid triggering asserts elsewhere in the front end.
60 setAccess(AS_public);
61 }
62}
63
Steve Naroff0a473932009-01-20 19:53:53 +000064const char *DeclContext::getDeclKindName() const {
65 switch (DeclKind) {
Sean Hunt9a555912010-05-30 07:21:58 +000066 default: assert(0 && "Declaration context not in DeclNodes.inc!");
67#define DECL(DERIVED, BASE) case Decl::DERIVED: return #DERIVED;
68#define ABSTRACT_DECL(DECL)
69#include "clang/AST/DeclNodes.inc"
Steve Naroff0a473932009-01-20 19:53:53 +000070 }
71}
72
Eli Friedman56d29372008-06-07 16:52:53 +000073bool Decl::CollectingStats(bool Enable) {
Kovarththanan Rajaratnam2024f4d2009-11-29 14:54:35 +000074 if (Enable) StatSwitch = true;
Eli Friedman56d29372008-06-07 16:52:53 +000075 return StatSwitch;
76}
77
78void Decl::PrintStats() {
79 fprintf(stderr, "*** Decl Stats:\n");
Mike Stump1eb44332009-09-09 15:08:12 +000080
Douglas Gregor64650af2009-02-02 23:39:07 +000081 int totalDecls = 0;
Sean Hunt9a555912010-05-30 07:21:58 +000082#define DECL(DERIVED, BASE) totalDecls += n##DERIVED##s;
83#define ABSTRACT_DECL(DECL)
84#include "clang/AST/DeclNodes.inc"
Douglas Gregor64650af2009-02-02 23:39:07 +000085 fprintf(stderr, " %d decls total.\n", totalDecls);
Mike Stump1eb44332009-09-09 15:08:12 +000086
Douglas Gregor64650af2009-02-02 23:39:07 +000087 int totalBytes = 0;
Sean Hunt9a555912010-05-30 07:21:58 +000088#define DECL(DERIVED, BASE) \
89 if (n##DERIVED##s > 0) { \
90 totalBytes += (int)(n##DERIVED##s * sizeof(DERIVED##Decl)); \
91 fprintf(stderr, " %d " #DERIVED " decls, %d each (%d bytes)\n", \
92 n##DERIVED##s, (int)sizeof(DERIVED##Decl), \
93 (int)(n##DERIVED##s * sizeof(DERIVED##Decl))); \
Douglas Gregor64650af2009-02-02 23:39:07 +000094 }
Sean Hunt9a555912010-05-30 07:21:58 +000095#define ABSTRACT_DECL(DECL)
96#include "clang/AST/DeclNodes.inc"
Mike Stump1eb44332009-09-09 15:08:12 +000097
Douglas Gregor64650af2009-02-02 23:39:07 +000098 fprintf(stderr, "Total bytes = %d\n", totalBytes);
Eli Friedman56d29372008-06-07 16:52:53 +000099}
100
Sean Hunt9a555912010-05-30 07:21:58 +0000101void Decl::add(Kind k) {
Eli Friedman56d29372008-06-07 16:52:53 +0000102 switch (k) {
Sean Hunt9a555912010-05-30 07:21:58 +0000103 default: assert(0 && "Declaration not in DeclNodes.inc!");
104#define DECL(DERIVED, BASE) case DERIVED: ++n##DERIVED##s; break;
105#define ABSTRACT_DECL(DECL)
106#include "clang/AST/DeclNodes.inc"
Eli Friedman56d29372008-06-07 16:52:53 +0000107 }
108}
109
Anders Carlsson67e33202009-06-13 00:08:58 +0000110bool Decl::isTemplateParameterPack() const {
111 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
112 return TTP->isParameterPack();
Douglas Gregor10738d32010-12-23 23:51:58 +0000113 if (const NonTypeTemplateParmDecl *NTTP
Douglas Gregor61c4d282011-01-05 15:48:55 +0000114 = dyn_cast<NonTypeTemplateParmDecl>(this))
Douglas Gregor10738d32010-12-23 23:51:58 +0000115 return NTTP->isParameterPack();
Douglas Gregor61c4d282011-01-05 15:48:55 +0000116 if (const TemplateTemplateParmDecl *TTP
117 = dyn_cast<TemplateTemplateParmDecl>(this))
118 return TTP->isParameterPack();
Anders Carlsson67e33202009-06-13 00:08:58 +0000119 return false;
120}
121
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000122bool Decl::isParameterPack() const {
123 if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(this))
124 return Parm->isParameterPack();
125
126 return isTemplateParameterPack();
127}
128
Douglas Gregore53060f2009-06-25 22:08:12 +0000129bool Decl::isFunctionOrFunctionTemplate() const {
John McCall9488ea12009-11-17 05:59:44 +0000130 if (const UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(this))
Anders Carlsson58badb72009-06-26 05:26:50 +0000131 return UD->getTargetDecl()->isFunctionOrFunctionTemplate();
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Douglas Gregore53060f2009-06-25 22:08:12 +0000133 return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this);
134}
135
Douglas Gregor79c22782010-01-16 20:21:20 +0000136bool Decl::isDefinedOutsideFunctionOrMethod() const {
137 for (const DeclContext *DC = getDeclContext();
138 DC && !DC->isTranslationUnit();
139 DC = DC->getParent())
140 if (DC->isFunctionOrMethod())
141 return false;
142
143 return true;
144}
145
146
Eli Friedman56d29372008-06-07 16:52:53 +0000147//===----------------------------------------------------------------------===//
Chris Lattner49f28ca2009-03-05 08:00:35 +0000148// PrettyStackTraceDecl Implementation
149//===----------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000150
Chris Lattner49f28ca2009-03-05 08:00:35 +0000151void PrettyStackTraceDecl::print(llvm::raw_ostream &OS) const {
152 SourceLocation TheLoc = Loc;
153 if (TheLoc.isInvalid() && TheDecl)
154 TheLoc = TheDecl->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000155
Chris Lattner49f28ca2009-03-05 08:00:35 +0000156 if (TheLoc.isValid()) {
157 TheLoc.print(OS, SM);
158 OS << ": ";
159 }
160
161 OS << Message;
162
Daniel Dunbarc5236562009-11-21 09:05:59 +0000163 if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
Chris Lattner49f28ca2009-03-05 08:00:35 +0000164 OS << " '" << DN->getQualifiedNameAsString() << '\'';
165 OS << '\n';
166}
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Chris Lattner49f28ca2009-03-05 08:00:35 +0000168//===----------------------------------------------------------------------===//
Eli Friedman56d29372008-06-07 16:52:53 +0000169// Decl Implementation
170//===----------------------------------------------------------------------===//
171
Chris Lattner769dbdf2009-03-27 20:18:19 +0000172// Out-of-line virtual method providing a home for Decl.
Douglas Gregorff331c12010-07-25 18:17:45 +0000173Decl::~Decl() { }
Chris Lattner769dbdf2009-03-27 20:18:19 +0000174
Douglas Gregorf4a03cc2011-02-17 07:02:32 +0000175bool Decl::isOutOfLine() const {
176 if (const VarDecl *VD = dyn_cast<VarDecl>(this))
177 return VD->isOutOfLine();
178 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
179 return FD->isOutOfLine();
180
181 return getLexicalDeclContext() != getDeclContext();
182}
183
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000184void Decl::setDeclContext(DeclContext *DC) {
185 if (isOutOfSemaDC())
186 delete getMultipleDC();
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Chris Lattneree219fd2009-03-29 06:06:59 +0000188 DeclCtx = DC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000189}
190
191void Decl::setLexicalDeclContext(DeclContext *DC) {
192 if (DC == getLexicalDeclContext())
193 return;
194
195 if (isInSemaDC()) {
Ted Kremenek94a39002009-12-01 00:07:10 +0000196 MultipleDC *MDC = new (getASTContext()) MultipleDC();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000197 MDC->SemanticDC = getDeclContext();
198 MDC->LexicalDC = DC;
Chris Lattneree219fd2009-03-29 06:06:59 +0000199 DeclCtx = MDC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000200 } else {
201 getMultipleDC()->LexicalDC = DC;
202 }
203}
204
John McCall9aeed322009-10-01 00:25:31 +0000205bool Decl::isInAnonymousNamespace() const {
206 const DeclContext *DC = getDeclContext();
207 do {
208 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
209 if (ND->isAnonymousNamespace())
210 return true;
211 } while ((DC = DC->getParent()));
212
213 return false;
214}
215
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000216TranslationUnitDecl *Decl::getTranslationUnitDecl() {
Argyrios Kyrtzidis9b346692009-06-30 02:34:53 +0000217 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
218 return TUD;
219
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000220 DeclContext *DC = getDeclContext();
221 assert(DC && "This decl is not contained in a translation unit!");
Mike Stump1eb44332009-09-09 15:08:12 +0000222
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000223 while (!DC->isTranslationUnit()) {
224 DC = DC->getParent();
225 assert(DC && "This decl is not contained in a translation unit!");
226 }
Mike Stump1eb44332009-09-09 15:08:12 +0000227
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000228 return cast<TranslationUnitDecl>(DC);
229}
230
231ASTContext &Decl::getASTContext() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000232 return getTranslationUnitDecl()->getASTContext();
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000233}
234
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +0000235ASTMutationListener *Decl::getASTMutationListener() const {
236 return getASTContext().getASTMutationListener();
237}
238
Douglas Gregorc070cc62010-06-17 23:14:26 +0000239bool Decl::isUsed(bool CheckUsedAttr) const {
Tanya Lattner12ead492010-02-17 02:17:21 +0000240 if (Used)
241 return true;
242
243 // Check for used attribute.
Douglas Gregorc070cc62010-06-17 23:14:26 +0000244 if (CheckUsedAttr && hasAttr<UsedAttr>())
Tanya Lattner12ead492010-02-17 02:17:21 +0000245 return true;
246
247 // Check redeclarations for used attribute.
248 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Douglas Gregorc070cc62010-06-17 23:14:26 +0000249 if ((CheckUsedAttr && I->hasAttr<UsedAttr>()) || I->Used)
Tanya Lattner12ead492010-02-17 02:17:21 +0000250 return true;
251 }
252
253 return false;
254}
255
256
Chris Lattner769dbdf2009-03-27 20:18:19 +0000257unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
258 switch (DeclKind) {
John McCall9488ea12009-11-17 05:59:44 +0000259 case Function:
260 case CXXMethod:
261 case CXXConstructor:
262 case CXXDestructor:
263 case CXXConversion:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000264 case EnumConstant:
265 case Var:
266 case ImplicitParam:
267 case ParmVar:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000268 case NonTypeTemplateParm:
269 case ObjCMethod:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000270 case ObjCProperty:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000271 return IDNS_Ordinary;
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000272 case Label:
273 return IDNS_Label;
Francois Pichet87c2e122010-11-21 06:08:52 +0000274 case IndirectField:
275 return IDNS_Ordinary | IDNS_Member;
276
John McCall0d6b1642010-04-23 18:46:30 +0000277 case ObjCCompatibleAlias:
278 case ObjCInterface:
279 return IDNS_Ordinary | IDNS_Type;
280
281 case Typedef:
282 case UnresolvedUsingTypename:
283 case TemplateTypeParm:
284 return IDNS_Ordinary | IDNS_Type;
285
John McCall9488ea12009-11-17 05:59:44 +0000286 case UsingShadow:
287 return 0; // we'll actually overwrite this later
288
John McCall7ba107a2009-11-18 02:36:19 +0000289 case UnresolvedUsingValue:
John McCall7ba107a2009-11-18 02:36:19 +0000290 return IDNS_Ordinary | IDNS_Using;
John McCall9488ea12009-11-17 05:59:44 +0000291
292 case Using:
293 return IDNS_Using;
294
Chris Lattner769dbdf2009-03-27 20:18:19 +0000295 case ObjCProtocol:
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000296 return IDNS_ObjCProtocol;
Mike Stump1eb44332009-09-09 15:08:12 +0000297
Chris Lattner769dbdf2009-03-27 20:18:19 +0000298 case Field:
299 case ObjCAtDefsField:
300 case ObjCIvar:
301 return IDNS_Member;
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Chris Lattner769dbdf2009-03-27 20:18:19 +0000303 case Record:
304 case CXXRecord:
305 case Enum:
John McCall0d6b1642010-04-23 18:46:30 +0000306 return IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000307
Chris Lattner769dbdf2009-03-27 20:18:19 +0000308 case Namespace:
John McCall0d6b1642010-04-23 18:46:30 +0000309 case NamespaceAlias:
310 return IDNS_Namespace;
311
Chris Lattner769dbdf2009-03-27 20:18:19 +0000312 case FunctionTemplate:
John McCall0d6b1642010-04-23 18:46:30 +0000313 return IDNS_Ordinary;
314
Chris Lattner769dbdf2009-03-27 20:18:19 +0000315 case ClassTemplate:
316 case TemplateTemplateParm:
John McCall0d6b1642010-04-23 18:46:30 +0000317 return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000318
Chris Lattner769dbdf2009-03-27 20:18:19 +0000319 // Never have names.
John McCall02cace72009-08-28 07:59:38 +0000320 case Friend:
John McCalldd4a3b02009-09-16 22:47:08 +0000321 case FriendTemplate:
Abramo Bagnara6206d532010-06-05 05:09:32 +0000322 case AccessSpec:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000323 case LinkageSpec:
324 case FileScopeAsm:
325 case StaticAssert:
326 case ObjCClass:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000327 case ObjCPropertyImpl:
328 case ObjCForwardProtocol:
329 case Block:
330 case TranslationUnit:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000331
Chris Lattner769dbdf2009-03-27 20:18:19 +0000332 case UsingDirective:
333 case ClassTemplateSpecialization:
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000334 case ClassTemplatePartialSpecialization:
Douglas Gregorbd4187b2010-04-22 23:19:50 +0000335 case ObjCImplementation:
336 case ObjCCategory:
337 case ObjCCategoryImpl:
338 // Never looked up by name.
Chris Lattner769dbdf2009-03-27 20:18:19 +0000339 return 0;
340 }
John McCall9488ea12009-11-17 05:59:44 +0000341
342 return 0;
Eli Friedman56d29372008-06-07 16:52:53 +0000343}
344
Sean Huntcf807c42010-08-18 23:23:40 +0000345void Decl::setAttrs(const AttrVec &attrs) {
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000346 assert(!HasAttrs && "Decl already contains attrs.");
347
Sean Huntcf807c42010-08-18 23:23:40 +0000348 AttrVec &AttrBlank = getASTContext().getDeclAttrs(this);
349 assert(AttrBlank.empty() && "HasAttrs was wrong?");
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000350
351 AttrBlank = attrs;
352 HasAttrs = true;
353}
354
Sean Huntcf807c42010-08-18 23:23:40 +0000355void Decl::dropAttrs() {
Eli Friedman56d29372008-06-07 16:52:53 +0000356 if (!HasAttrs) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Eli Friedman56d29372008-06-07 16:52:53 +0000358 HasAttrs = false;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000359 getASTContext().eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000360}
361
Sean Huntcf807c42010-08-18 23:23:40 +0000362const AttrVec &Decl::getAttrs() const {
363 assert(HasAttrs && "No attrs to get!");
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000364 return getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000365}
366
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000367void Decl::swapAttrs(Decl *RHS) {
Eli Friedman56d29372008-06-07 16:52:53 +0000368 bool HasLHSAttr = this->HasAttrs;
369 bool HasRHSAttr = RHS->HasAttrs;
Mike Stump1eb44332009-09-09 15:08:12 +0000370
Eli Friedman56d29372008-06-07 16:52:53 +0000371 // Usually, neither decl has attrs, nothing to do.
372 if (!HasLHSAttr && !HasRHSAttr) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000373
Eli Friedman56d29372008-06-07 16:52:53 +0000374 // If 'this' has no attrs, swap the other way.
375 if (!HasLHSAttr)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000376 return RHS->swapAttrs(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000377
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000378 ASTContext &Context = getASTContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Eli Friedman56d29372008-06-07 16:52:53 +0000380 // Handle the case when both decls have attrs.
381 if (HasRHSAttr) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000382 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman56d29372008-06-07 16:52:53 +0000383 return;
384 }
Mike Stump1eb44332009-09-09 15:08:12 +0000385
Eli Friedman56d29372008-06-07 16:52:53 +0000386 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor68584ed2009-06-18 16:11:24 +0000387 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
388 Context.eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000389 this->HasAttrs = false;
390 RHS->HasAttrs = true;
391}
392
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000393Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000394 Decl::Kind DK = D->getDeclKind();
395 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000396#define DECL(NAME, BASE)
397#define DECL_CONTEXT(NAME) \
398 case Decl::NAME: \
399 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
400#define DECL_CONTEXT_BASE(NAME)
401#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000402 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000403#define DECL(NAME, BASE)
404#define DECL_CONTEXT_BASE(NAME) \
405 if (DK >= first##NAME && DK <= last##NAME) \
406 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
407#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000408 assert(false && "a decl that inherits DeclContext isn't handled");
409 return 0;
410 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000411}
412
413DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000414 Decl::Kind DK = D->getKind();
415 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000416#define DECL(NAME, BASE)
417#define DECL_CONTEXT(NAME) \
418 case Decl::NAME: \
419 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
420#define DECL_CONTEXT_BASE(NAME)
421#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000422 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000423#define DECL(NAME, BASE)
424#define DECL_CONTEXT_BASE(NAME) \
425 if (DK >= first##NAME && DK <= last##NAME) \
426 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
427#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000428 assert(false && "a decl that inherits DeclContext isn't handled");
429 return 0;
430 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000431}
432
Douglas Gregor1aa3d812011-02-17 07:13:24 +0000433Stmt *Decl::getBody() const {
434 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
435 return FD->getBody();
436 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(this))
437 return MD->getBody();
438 if (const BlockDecl *BD = dyn_cast<BlockDecl>(this))
439 return BD->getBody();
440
441 return 0;
442}
443
444bool Decl::hasBody() const {
445 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
446 return FD->hasBody();
447
448 return getBody() != 0;
449}
450
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000451SourceLocation Decl::getBodyRBrace() const {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000452 // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
453 // FunctionDecl stores EndRangeLoc for this purpose.
454 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
455 const FunctionDecl *Definition;
456 if (FD->hasBody(Definition))
457 return Definition->getSourceRange().getEnd();
458 return SourceLocation();
459 }
460
Argyrios Kyrtzidis6717ef42010-07-07 11:31:27 +0000461 if (Stmt *Body = getBody())
462 return Body->getSourceRange().getEnd();
463
464 return SourceLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +0000465}
466
Anders Carlsson1329c272009-03-25 23:38:06 +0000467void Decl::CheckAccessDeclContext() const {
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000468#ifndef NDEBUG
John McCall46460a62010-01-20 21:53:11 +0000469 // Suppress this check if any of the following hold:
470 // 1. this is the translation unit (and thus has no parent)
471 // 2. this is a template parameter (and thus doesn't belong to its context)
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000472 // 3. this is a non-type template parameter
473 // 4. the context is not a record
474 // 5. it's invalid
475 // 6. it's a C++0x static_assert.
Anders Carlsson35eda442009-08-29 20:47:47 +0000476 if (isa<TranslationUnitDecl>(this) ||
Argyrios Kyrtzidis04aed0e2010-07-02 11:55:44 +0000477 isa<TemplateTypeParmDecl>(this) ||
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000478 isa<NonTypeTemplateParmDecl>(this) ||
Douglas Gregorfdd8ab12010-02-22 17:53:38 +0000479 !isa<CXXRecordDecl>(getDeclContext()) ||
Argyrios Kyrtzidis65b63ec2010-09-08 21:32:35 +0000480 isInvalidDecl() ||
481 isa<StaticAssertDecl>(this) ||
482 // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
483 // as DeclContext (?).
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000484 isa<ParmVarDecl>(this) ||
485 // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
486 // AS_none as access specifier.
487 isa<CXXRecordDecl>(this))
Anders Carlsson35eda442009-08-29 20:47:47 +0000488 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000489
490 assert(Access != AS_none &&
Anders Carlsson1329c272009-03-25 23:38:06 +0000491 "Access specifier is AS_none inside a record decl");
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000492#endif
Anders Carlsson1329c272009-03-25 23:38:06 +0000493}
494
Anders Carlsson1329c272009-03-25 23:38:06 +0000495
Eli Friedman56d29372008-06-07 16:52:53 +0000496//===----------------------------------------------------------------------===//
497// DeclContext Implementation
498//===----------------------------------------------------------------------===//
499
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000500bool DeclContext::classof(const Decl *D) {
501 switch (D->getKind()) {
Sean Hunt9a555912010-05-30 07:21:58 +0000502#define DECL(NAME, BASE)
503#define DECL_CONTEXT(NAME) case Decl::NAME:
504#define DECL_CONTEXT_BASE(NAME)
505#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000506 return true;
507 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000508#define DECL(NAME, BASE)
509#define DECL_CONTEXT_BASE(NAME) \
510 if (D->getKind() >= Decl::first##NAME && \
511 D->getKind() <= Decl::last##NAME) \
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000512 return true;
Sean Hunt9a555912010-05-30 07:21:58 +0000513#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000514 return false;
515 }
516}
517
Douglas Gregora2da7802010-07-25 18:38:02 +0000518DeclContext::~DeclContext() { }
Douglas Gregor44b43212008-12-11 16:49:14 +0000519
Douglas Gregore942bbe2009-09-10 16:57:35 +0000520/// \brief Find the parent context of this context that will be
521/// used for unqualified name lookup.
522///
523/// Generally, the parent lookup context is the semantic context. However, for
524/// a friend function the parent lookup context is the lexical context, which
525/// is the class in which the friend is declared.
526DeclContext *DeclContext::getLookupParent() {
527 // FIXME: Find a better way to identify friends
528 if (isa<FunctionDecl>(this))
Sebastian Redl7a126a42010-08-31 00:36:30 +0000529 if (getParent()->getRedeclContext()->isFileContext() &&
530 getLexicalParent()->getRedeclContext()->isRecord())
Douglas Gregore942bbe2009-09-10 16:57:35 +0000531 return getLexicalParent();
532
533 return getParent();
534}
535
Sebastian Redl410c4f22010-08-31 20:53:31 +0000536bool DeclContext::isInlineNamespace() const {
537 return isNamespace() &&
538 cast<NamespaceDecl>(this)->isInline();
539}
540
Douglas Gregorbc221632009-05-28 16:34:51 +0000541bool DeclContext::isDependentContext() const {
542 if (isFileContext())
543 return false;
544
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000545 if (isa<ClassTemplatePartialSpecializationDecl>(this))
546 return true;
547
Douglas Gregorbc221632009-05-28 16:34:51 +0000548 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
549 if (Record->getDescribedClassTemplate())
550 return true;
551
John McCall0c01d182010-03-24 05:22:00 +0000552 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregorbc221632009-05-28 16:34:51 +0000553 if (Function->getDescribedFunctionTemplate())
554 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000555
John McCall0c01d182010-03-24 05:22:00 +0000556 // Friend function declarations are dependent if their *lexical*
557 // context is dependent.
558 if (cast<Decl>(this)->getFriendObjectKind())
559 return getLexicalParent()->isDependentContext();
560 }
561
Douglas Gregorbc221632009-05-28 16:34:51 +0000562 return getParent() && getParent()->isDependentContext();
563}
564
Douglas Gregor074149e2009-01-05 19:45:36 +0000565bool DeclContext::isTransparentContext() const {
566 if (DeclKind == Decl::Enum)
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000567 return !cast<EnumDecl>(this)->isScoped();
Douglas Gregor074149e2009-01-05 19:45:36 +0000568 else if (DeclKind == Decl::LinkageSpec)
569 return true;
Douglas Gregor074149e2009-01-05 19:45:36 +0000570
571 return false;
572}
573
John McCallac65c622010-10-26 04:59:26 +0000574bool DeclContext::isExternCContext() const {
575 const DeclContext *DC = this;
576 while (DC->DeclKind != Decl::TranslationUnit) {
577 if (DC->DeclKind == Decl::LinkageSpec)
578 return cast<LinkageSpecDecl>(DC)->getLanguage()
579 == LinkageSpecDecl::lang_c;
580 DC = DC->getParent();
581 }
582 return false;
583}
584
Sebastian Redl7a126a42010-08-31 00:36:30 +0000585bool DeclContext::Encloses(const DeclContext *DC) const {
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000586 if (getPrimaryContext() != this)
587 return getPrimaryContext()->Encloses(DC);
Mike Stump1eb44332009-09-09 15:08:12 +0000588
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000589 for (; DC; DC = DC->getParent())
590 if (DC->getPrimaryContext() == this)
591 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000592 return false;
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000593}
594
Steve Naroff0701bbb2009-01-08 17:28:14 +0000595DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000596 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000597 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000598 case Decl::LinkageSpec:
Mike Stump1eb44332009-09-09 15:08:12 +0000599 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000600 // There is only one DeclContext for these entities.
601 return this;
602
603 case Decl::Namespace:
604 // The original namespace is our primary context.
605 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
606
Douglas Gregor44b43212008-12-11 16:49:14 +0000607 case Decl::ObjCMethod:
608 return this;
609
610 case Decl::ObjCInterface:
Steve Naroff0701bbb2009-01-08 17:28:14 +0000611 case Decl::ObjCProtocol:
612 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000613 // FIXME: Can Objective-C interfaces be forward-declared?
614 return this;
615
Steve Naroff0701bbb2009-01-08 17:28:14 +0000616 case Decl::ObjCImplementation:
617 case Decl::ObjCCategoryImpl:
618 return this;
619
Douglas Gregor44b43212008-12-11 16:49:14 +0000620 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000621 if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000622 // If this is a tag type that has a definition or is currently
623 // being defined, that definition is our primary context.
John McCall3cb0ebd2010-03-10 03:28:59 +0000624 TagDecl *Tag = cast<TagDecl>(this);
625 assert(isa<TagType>(Tag->TypeForDecl) ||
626 isa<InjectedClassNameType>(Tag->TypeForDecl));
627
628 if (TagDecl *Def = Tag->getDefinition())
629 return Def;
630
631 if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) {
632 const TagType *TagTy = cast<TagType>(Tag->TypeForDecl);
633 if (TagTy->isBeingDefined())
634 // FIXME: is it necessarily being defined in the decl
635 // that owns the type?
636 return TagTy->getDecl();
637 }
638
639 return Tag;
Douglas Gregorcc636682009-02-17 23:15:12 +0000640 }
641
Sean Hunt9a555912010-05-30 07:21:58 +0000642 assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
Douglas Gregor44b43212008-12-11 16:49:14 +0000643 "Unknown DeclContext kind");
644 return this;
645 }
646}
647
648DeclContext *DeclContext::getNextContext() {
649 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000650 case Decl::Namespace:
651 // Return the next namespace
652 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
653
654 default:
Douglas Gregor44b43212008-12-11 16:49:14 +0000655 return 0;
656 }
657}
658
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000659std::pair<Decl *, Decl *>
660DeclContext::BuildDeclChain(const llvm::SmallVectorImpl<Decl*> &Decls) {
661 // Build up a chain of declarations via the Decl::NextDeclInContext field.
662 Decl *FirstNewDecl = 0;
663 Decl *PrevDecl = 0;
664 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
665 Decl *D = Decls[I];
666 if (PrevDecl)
667 PrevDecl->NextDeclInContext = D;
668 else
669 FirstNewDecl = D;
670
671 PrevDecl = D;
672 }
673
674 return std::make_pair(FirstNewDecl, PrevDecl);
675}
676
Douglas Gregor2cf26342009-04-09 22:27:44 +0000677/// \brief Load the declarations within this lexical storage from an
678/// external source.
Mike Stump1eb44332009-09-09 15:08:12 +0000679void
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000680DeclContext::LoadLexicalDeclsFromExternalStorage() const {
681 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000682 assert(hasExternalLexicalStorage() && Source && "No external storage?");
683
Argyrios Kyrtzidis0dbbc042010-07-30 10:03:23 +0000684 // Notify that we have a DeclContext that is initializing.
685 ExternalASTSource::Deserializing ADeclContext(Source);
686
John McCall76bd1f32010-06-01 09:23:16 +0000687 llvm::SmallVector<Decl*, 64> Decls;
688 if (Source->FindExternalLexicalDecls(this, Decls))
Douglas Gregor2cf26342009-04-09 22:27:44 +0000689 return;
690
691 // There is no longer any lexical storage in this context
692 ExternalLexicalStorage = false;
693
694 if (Decls.empty())
695 return;
696
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000697 // We may have already loaded just the fields of this record, in which case
698 // don't add the decls, just replace the FirstDecl/LastDecl chain.
699 if (const RecordDecl *RD = dyn_cast<RecordDecl>(this))
700 if (RD->LoadedFieldsFromExternalStorage) {
701 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
702 return;
703 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000704
705 // Splice the newly-read declarations into the beginning of the list
706 // of declarations.
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000707 Decl *ExternalFirst, *ExternalLast;
708 llvm::tie(ExternalFirst, ExternalLast) = BuildDeclChain(Decls);
709 ExternalLast->NextDeclInContext = FirstDecl;
710 FirstDecl = ExternalFirst;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000711 if (!LastDecl)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000712 LastDecl = ExternalLast;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000713}
714
John McCall76bd1f32010-06-01 09:23:16 +0000715DeclContext::lookup_result
716ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
717 DeclarationName Name) {
718 ASTContext &Context = DC->getParentASTContext();
719 StoredDeclsMap *Map;
720 if (!(Map = DC->LookupPtr))
721 Map = DC->CreateStoredDeclsMap(Context);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000722
John McCall76bd1f32010-06-01 09:23:16 +0000723 StoredDeclsList &List = (*Map)[Name];
724 assert(List.isNull());
725 (void) List;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000726
John McCall76bd1f32010-06-01 09:23:16 +0000727 return DeclContext::lookup_result();
728}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000729
John McCall76bd1f32010-06-01 09:23:16 +0000730DeclContext::lookup_result
731ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +0000732 DeclarationName Name,
733 llvm::SmallVectorImpl<NamedDecl*> &Decls) {
734 ASTContext &Context = DC->getParentASTContext();;
735
736 StoredDeclsMap *Map;
737 if (!(Map = DC->LookupPtr))
738 Map = DC->CreateStoredDeclsMap(Context);
739
740 StoredDeclsList &List = (*Map)[Name];
741 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
742 if (List.isNull())
743 List.setOnlyValue(Decls[I]);
744 else
745 List.AddSubsequentDecl(Decls[I]);
746 }
747
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000748 return List.getLookupResult();
John McCall76bd1f32010-06-01 09:23:16 +0000749}
750
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000751void ExternalASTSource::MaterializeVisibleDeclsForName(const DeclContext *DC,
752 DeclarationName Name,
753 llvm::SmallVectorImpl<NamedDecl*> &Decls) {
754 assert(DC->LookupPtr);
755 StoredDeclsMap &Map = *DC->LookupPtr;
756
757 // If there's an entry in the table the visible decls for this name have
758 // already been deserialized.
759 if (Map.find(Name) == Map.end()) {
760 StoredDeclsList &List = Map[Name];
761 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
762 if (List.isNull())
763 List.setOnlyValue(Decls[I]);
764 else
765 List.AddSubsequentDecl(Decls[I]);
766 }
767 }
768}
769
Sebastian Redl681d7232010-07-27 00:17:23 +0000770DeclContext::decl_iterator DeclContext::noload_decls_begin() const {
771 return decl_iterator(FirstDecl);
772}
773
774DeclContext::decl_iterator DeclContext::noload_decls_end() const {
775 return decl_iterator();
776}
777
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000778DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000779 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000780 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000781
782 // FIXME: Check whether we need to load some declarations from
783 // external storage.
Mike Stump1eb44332009-09-09 15:08:12 +0000784 return decl_iterator(FirstDecl);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000785}
786
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000787DeclContext::decl_iterator DeclContext::decls_end() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000788 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000789 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000790
Mike Stump1eb44332009-09-09 15:08:12 +0000791 return decl_iterator();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000792}
793
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000794bool DeclContext::decls_empty() const {
Douglas Gregor8038d512009-04-10 17:25:41 +0000795 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000796 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor8038d512009-04-10 17:25:41 +0000797
798 return !FirstDecl;
799}
800
John McCall9f54ad42009-12-10 09:41:52 +0000801void DeclContext::removeDecl(Decl *D) {
802 assert(D->getLexicalDeclContext() == this &&
803 "decl being removed from non-lexical context");
804 assert((D->NextDeclInContext || D == LastDecl) &&
805 "decl is not in decls list");
806
807 // Remove D from the decl chain. This is O(n) but hopefully rare.
808 if (D == FirstDecl) {
809 if (D == LastDecl)
810 FirstDecl = LastDecl = 0;
811 else
812 FirstDecl = D->NextDeclInContext;
813 } else {
814 for (Decl *I = FirstDecl; true; I = I->NextDeclInContext) {
815 assert(I && "decl not found in linked list");
816 if (I->NextDeclInContext == D) {
817 I->NextDeclInContext = D->NextDeclInContext;
818 if (D == LastDecl) LastDecl = I;
819 break;
820 }
821 }
822 }
823
824 // Mark that D is no longer in the decl chain.
825 D->NextDeclInContext = 0;
826
827 // Remove D from the lookup table if necessary.
828 if (isa<NamedDecl>(D)) {
829 NamedDecl *ND = cast<NamedDecl>(D);
830
John McCall0c01d182010-03-24 05:22:00 +0000831 StoredDeclsMap *Map = getPrimaryContext()->LookupPtr;
832 if (!Map) return;
John McCall9f54ad42009-12-10 09:41:52 +0000833
John McCall9f54ad42009-12-10 09:41:52 +0000834 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
835 assert(Pos != Map->end() && "no lookup entry for decl");
836 Pos->second.remove(ND);
837 }
838}
839
John McCall3f9a8a62009-08-11 06:59:38 +0000840void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner7f0be132009-02-20 00:56:18 +0000841 assert(D->getLexicalDeclContext() == this &&
842 "Decl inserted into wrong lexical context");
Mike Stump1eb44332009-09-09 15:08:12 +0000843 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000844 "Decl already inserted into a DeclContext");
845
846 if (FirstDecl) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000847 LastDecl->NextDeclInContext = D;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000848 LastDecl = D;
849 } else {
850 FirstDecl = LastDecl = D;
851 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000852
853 // Notify a C++ record declaration that we've added a member, so it can
854 // update it's class-specific state.
855 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
856 Record->addedMember(D);
John McCall3f9a8a62009-08-11 06:59:38 +0000857}
858
859void DeclContext::addDecl(Decl *D) {
860 addHiddenDecl(D);
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000861
862 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000863 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor44b43212008-12-11 16:49:14 +0000864}
865
Douglas Gregor074149e2009-01-05 19:45:36 +0000866/// buildLookup - Build the lookup data structure with all of the
867/// declarations in DCtx (and any other contexts linked to it or
868/// transparent contexts nested within it).
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000869void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000870 for (; DCtx; DCtx = DCtx->getNextContext()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000871 for (decl_iterator D = DCtx->decls_begin(),
872 DEnd = DCtx->decls_end();
Douglas Gregor4f3b8f82009-01-06 07:17:58 +0000873 D != DEnd; ++D) {
John McCall3f9a8a62009-08-11 06:59:38 +0000874 // Insert this declaration into the lookup structure, but only
875 // if it's semantically in its decl context. During non-lazy
876 // lookup building, this is implicitly enforced by addDecl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000877 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
John McCall3f9a8a62009-08-11 06:59:38 +0000878 if (D->getDeclContext() == DCtx)
879 makeDeclVisibleInContextImpl(ND);
Douglas Gregor074149e2009-01-05 19:45:36 +0000880
Ted Kremenekc32b1d82009-11-17 22:58:30 +0000881 // Insert any forward-declared Objective-C interfaces into the lookup
882 // data structure.
883 if (ObjCClassDecl *Class = dyn_cast<ObjCClassDecl>(*D))
884 for (ObjCClassDecl::iterator I = Class->begin(), IEnd = Class->end();
885 I != IEnd; ++I)
Ted Kremenek321c22f2009-11-18 00:28:11 +0000886 makeDeclVisibleInContextImpl(I->getInterface());
Ted Kremenekc32b1d82009-11-17 22:58:30 +0000887
Sebastian Redl410c4f22010-08-31 20:53:31 +0000888 // If this declaration is itself a transparent declaration context or
889 // inline namespace, add its members (recursively).
Douglas Gregor074149e2009-01-05 19:45:36 +0000890 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
Sebastian Redl410c4f22010-08-31 20:53:31 +0000891 if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000892 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregor074149e2009-01-05 19:45:36 +0000893 }
894 }
895}
896
Mike Stump1eb44332009-09-09 15:08:12 +0000897DeclContext::lookup_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000898DeclContext::lookup(DeclarationName Name) {
Steve Naroff0701bbb2009-01-08 17:28:14 +0000899 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000900 if (PrimaryContext != this)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000901 return PrimaryContext->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000902
John McCall76bd1f32010-06-01 09:23:16 +0000903 if (hasExternalVisibleStorage()) {
904 // Check to see if we've already cached the lookup results.
905 if (LookupPtr) {
906 StoredDeclsMap::iterator I = LookupPtr->find(Name);
907 if (I != LookupPtr->end())
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000908 return I->second.getLookupResult();
John McCall76bd1f32010-06-01 09:23:16 +0000909 }
910
911 ExternalASTSource *Source = getParentASTContext().getExternalSource();
912 return Source->FindExternalVisibleDeclsByName(this, Name);
913 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000914
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000915 /// If there is no lookup data structure, build one now by walking
Douglas Gregor44b43212008-12-11 16:49:14 +0000916 /// all of the linked DeclContexts (in declaration order!) and
917 /// inserting their values.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000918 if (!LookupPtr) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000919 buildLookup(this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000920
Douglas Gregorc36c5402009-04-09 17:29:08 +0000921 if (!LookupPtr)
Douglas Gregora5fdd9c2010-05-11 06:18:17 +0000922 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Douglas Gregorc36c5402009-04-09 17:29:08 +0000923 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000924
John McCall0c01d182010-03-24 05:22:00 +0000925 StoredDeclsMap::iterator Pos = LookupPtr->find(Name);
926 if (Pos == LookupPtr->end())
Douglas Gregora5fdd9c2010-05-11 06:18:17 +0000927 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000928 return Pos->second.getLookupResult();
Douglas Gregor44b43212008-12-11 16:49:14 +0000929}
930
Mike Stump1eb44332009-09-09 15:08:12 +0000931DeclContext::lookup_const_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000932DeclContext::lookup(DeclarationName Name) const {
933 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000934}
935
Sebastian Redl7a126a42010-08-31 00:36:30 +0000936DeclContext *DeclContext::getRedeclContext() {
Chris Lattner0cf2b192009-03-27 19:19:59 +0000937 DeclContext *Ctx = this;
Sebastian Redl410c4f22010-08-31 20:53:31 +0000938 // Skip through transparent contexts.
939 while (Ctx->isTransparentContext())
Douglas Gregorce356072009-01-06 23:51:29 +0000940 Ctx = Ctx->getParent();
941 return Ctx;
942}
943
Douglas Gregor88b70942009-02-25 22:02:03 +0000944DeclContext *DeclContext::getEnclosingNamespaceContext() {
945 DeclContext *Ctx = this;
946 // Skip through non-namespace, non-translation-unit contexts.
Sebastian Redl51a8a372010-08-31 00:36:23 +0000947 while (!Ctx->isFileContext())
Douglas Gregor88b70942009-02-25 22:02:03 +0000948 Ctx = Ctx->getParent();
949 return Ctx->getPrimaryContext();
950}
951
Sebastian Redl7a126a42010-08-31 00:36:30 +0000952bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
953 // For non-file contexts, this is equivalent to Equals.
954 if (!isFileContext())
955 return O->Equals(this);
956
957 do {
958 if (O->Equals(this))
959 return true;
960
961 const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O);
962 if (!NS || !NS->isInline())
963 break;
964 O = NS->getParent();
965 } while (O);
966
967 return false;
968}
969
John McCallab88d972009-08-31 22:39:49 +0000970void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000971 // FIXME: This feels like a hack. Should DeclarationName support
972 // template-ids, or is there a better way to keep specializations
973 // from being visible?
974 if (isa<ClassTemplateSpecializationDecl>(D))
975 return;
Eli Friedman6bc20132009-12-08 05:40:03 +0000976 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
977 if (FD->isFunctionTemplateSpecialization())
978 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000979
Steve Naroff0701bbb2009-01-08 17:28:14 +0000980 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000981 if (PrimaryContext != this) {
John McCallab88d972009-08-31 22:39:49 +0000982 PrimaryContext->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor44b43212008-12-11 16:49:14 +0000983 return;
984 }
985
986 // If we already have a lookup data structure, perform the insertion
Argyrios Kyrtzidis5586b012010-07-04 21:44:25 +0000987 // into it. If we haven't deserialized externally stored decls, deserialize
988 // them so we can add the decl. Otherwise, be lazy and don't build that
989 // structure until someone asks for it.
990 if (LookupPtr || !Recoverable || hasExternalVisibleStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000991 makeDeclVisibleInContextImpl(D);
Douglas Gregor074149e2009-01-05 19:45:36 +0000992
Sebastian Redl410c4f22010-08-31 20:53:31 +0000993 // If we are a transparent context or inline namespace, insert into our
994 // parent context, too. This operation is recursive.
995 if (isTransparentContext() || isInlineNamespace())
John McCallab88d972009-08-31 22:39:49 +0000996 getParent()->makeDeclVisibleInContext(D, Recoverable);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +0000997
998 Decl *DCAsDecl = cast<Decl>(this);
999 // Notify that a decl was made visible unless it's a Tag being defined.
1000 if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined()))
1001 if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
1002 L->AddedVisibleDecl(this, D);
Douglas Gregor44b43212008-12-11 16:49:14 +00001003}
1004
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001005void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) {
Douglas Gregor074149e2009-01-05 19:45:36 +00001006 // Skip unnamed declarations.
1007 if (!D->getDeclName())
1008 return;
1009
Douglas Gregorcc636682009-02-17 23:15:12 +00001010 // FIXME: This feels like a hack. Should DeclarationName support
1011 // template-ids, or is there a better way to keep specializations
1012 // from being visible?
1013 if (isa<ClassTemplateSpecializationDecl>(D))
1014 return;
1015
Argyrios Kyrtzidis5586b012010-07-04 21:44:25 +00001016 ASTContext *C = 0;
1017 if (!LookupPtr) {
1018 C = &getParentASTContext();
1019 CreateStoredDeclsMap(*C);
1020 }
1021
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001022 // If there is an external AST source, load any declarations it knows about
1023 // with this declaration's name.
1024 // If the lookup table contains an entry about this name it means that we
1025 // have already checked the external source.
1026 if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
1027 if (hasExternalVisibleStorage() &&
1028 LookupPtr->find(D->getDeclName()) == LookupPtr->end())
1029 Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
1030
Douglas Gregor44b43212008-12-11 16:49:14 +00001031 // Insert this declaration into the map.
John McCall0c01d182010-03-24 05:22:00 +00001032 StoredDeclsList &DeclNameEntries = (*LookupPtr)[D->getDeclName()];
Chris Lattner67762a32009-02-20 01:44:05 +00001033 if (DeclNameEntries.isNull()) {
1034 DeclNameEntries.setOnlyValue(D);
Chris Lattnerbd6c8002009-02-19 07:00:44 +00001035 return;
Douglas Gregor44b43212008-12-11 16:49:14 +00001036 }
Chris Lattner91942502009-02-20 00:55:03 +00001037
Chris Lattnerbdc3d002009-02-20 01:10:07 +00001038 // If it is possible that this is a redeclaration, check to see if there is
1039 // already a decl for which declarationReplaces returns true. If there is
1040 // one, just replace it and return.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001041 if (DeclNameEntries.HandleRedeclaration(D))
Chris Lattner67762a32009-02-20 01:44:05 +00001042 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001043
Chris Lattnerbd6c8002009-02-19 07:00:44 +00001044 // Put this declaration into the appropriate slot.
Chris Lattner67762a32009-02-20 01:44:05 +00001045 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor44b43212008-12-11 16:49:14 +00001046}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001047
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00001048void DeclContext::MaterializeVisibleDeclsFromExternalStorage() {
1049 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1050 assert(hasExternalVisibleStorage() && Source && "No external storage?");
1051
1052 if (!LookupPtr)
1053 CreateStoredDeclsMap(getParentASTContext());
1054 Source->MaterializeVisibleDecls(this);
1055}
1056
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001057/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
1058/// this context.
Mike Stump1eb44332009-09-09 15:08:12 +00001059DeclContext::udir_iterator_range
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001060DeclContext::getUsingDirectives() const {
1061 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001062 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
1063 reinterpret_cast<udir_iterator>(Result.second));
1064}
Douglas Gregor2cf26342009-04-09 22:27:44 +00001065
Ted Kremenek3478eb62010-02-11 07:12:28 +00001066//===----------------------------------------------------------------------===//
1067// Creation and Destruction of StoredDeclsMaps. //
1068//===----------------------------------------------------------------------===//
1069
John McCall0c01d182010-03-24 05:22:00 +00001070StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
1071 assert(!LookupPtr && "context already has a decls map");
1072 assert(getPrimaryContext() == this &&
1073 "creating decls map on non-primary context");
1074
1075 StoredDeclsMap *M;
1076 bool Dependent = isDependentContext();
1077 if (Dependent)
1078 M = new DependentStoredDeclsMap();
1079 else
1080 M = new StoredDeclsMap();
1081 M->Previous = C.LastSDM;
1082 C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
1083 LookupPtr = M;
Ted Kremenek3478eb62010-02-11 07:12:28 +00001084 return M;
1085}
1086
1087void ASTContext::ReleaseDeclContextMaps() {
John McCall0c01d182010-03-24 05:22:00 +00001088 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1089 // pointer because the subclass doesn't add anything that needs to
1090 // be deleted.
John McCall0c01d182010-03-24 05:22:00 +00001091 StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1092}
1093
1094void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1095 while (Map) {
1096 // Advance the iteration before we invalidate memory.
1097 llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1098
1099 if (Dependent)
1100 delete static_cast<DependentStoredDeclsMap*>(Map);
1101 else
1102 delete Map;
1103
1104 Map = Next.getPointer();
1105 Dependent = Next.getInt();
1106 }
1107}
1108
John McCall0c01d182010-03-24 05:22:00 +00001109DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1110 DeclContext *Parent,
1111 const PartialDiagnostic &PDiag) {
1112 assert(Parent->isDependentContext()
1113 && "cannot iterate dependent diagnostics of non-dependent context");
1114 Parent = Parent->getPrimaryContext();
1115 if (!Parent->LookupPtr)
1116 Parent->CreateStoredDeclsMap(C);
1117
1118 DependentStoredDeclsMap *Map
1119 = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr);
1120
Douglas Gregorb8365182010-03-29 23:56:53 +00001121 // Allocate the copy of the PartialDiagnostic via the ASTContext's
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00001122 // BumpPtrAllocator, rather than the ASTContext itself.
Douglas Gregorb8365182010-03-29 23:56:53 +00001123 PartialDiagnostic::Storage *DiagStorage = 0;
1124 if (PDiag.hasStorage())
1125 DiagStorage = new (C) PartialDiagnostic::Storage;
1126
1127 DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
John McCall0c01d182010-03-24 05:22:00 +00001128
1129 // TODO: Maybe we shouldn't reverse the order during insertion.
1130 DD->NextDiagnostic = Map->FirstDiagnostic;
1131 Map->FirstDiagnostic = DD;
1132
1133 return DD;
Ted Kremenek3478eb62010-02-11 07:12:28 +00001134}