blob: a95ea3f6463676947ce318d07c8ecab785d2aad7 [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;
John McCalld04efc92010-04-23 02:41:41 +0000272
Francois Pichet87c2e122010-11-21 06:08:52 +0000273 case IndirectField:
274 return IDNS_Ordinary | IDNS_Member;
275
John McCall0d6b1642010-04-23 18:46:30 +0000276 case ObjCCompatibleAlias:
277 case ObjCInterface:
278 return IDNS_Ordinary | IDNS_Type;
279
280 case Typedef:
281 case UnresolvedUsingTypename:
282 case TemplateTypeParm:
283 return IDNS_Ordinary | IDNS_Type;
284
John McCall9488ea12009-11-17 05:59:44 +0000285 case UsingShadow:
286 return 0; // we'll actually overwrite this later
287
John McCall7ba107a2009-11-18 02:36:19 +0000288 case UnresolvedUsingValue:
John McCall7ba107a2009-11-18 02:36:19 +0000289 return IDNS_Ordinary | IDNS_Using;
John McCall9488ea12009-11-17 05:59:44 +0000290
291 case Using:
292 return IDNS_Using;
293
Chris Lattner769dbdf2009-03-27 20:18:19 +0000294 case ObjCProtocol:
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000295 return IDNS_ObjCProtocol;
Mike Stump1eb44332009-09-09 15:08:12 +0000296
Chris Lattner769dbdf2009-03-27 20:18:19 +0000297 case Field:
298 case ObjCAtDefsField:
299 case ObjCIvar:
300 return IDNS_Member;
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Chris Lattner769dbdf2009-03-27 20:18:19 +0000302 case Record:
303 case CXXRecord:
304 case Enum:
John McCall0d6b1642010-04-23 18:46:30 +0000305 return IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000306
Chris Lattner769dbdf2009-03-27 20:18:19 +0000307 case Namespace:
John McCall0d6b1642010-04-23 18:46:30 +0000308 case NamespaceAlias:
309 return IDNS_Namespace;
310
Chris Lattner769dbdf2009-03-27 20:18:19 +0000311 case FunctionTemplate:
John McCall0d6b1642010-04-23 18:46:30 +0000312 return IDNS_Ordinary;
313
Chris Lattner769dbdf2009-03-27 20:18:19 +0000314 case ClassTemplate:
315 case TemplateTemplateParm:
John McCall0d6b1642010-04-23 18:46:30 +0000316 return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Chris Lattner769dbdf2009-03-27 20:18:19 +0000318 // Never have names.
John McCall02cace72009-08-28 07:59:38 +0000319 case Friend:
John McCalldd4a3b02009-09-16 22:47:08 +0000320 case FriendTemplate:
Abramo Bagnara6206d532010-06-05 05:09:32 +0000321 case AccessSpec:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000322 case LinkageSpec:
323 case FileScopeAsm:
324 case StaticAssert:
325 case ObjCClass:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000326 case ObjCPropertyImpl:
327 case ObjCForwardProtocol:
328 case Block:
329 case TranslationUnit:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000330
Chris Lattner769dbdf2009-03-27 20:18:19 +0000331 case UsingDirective:
332 case ClassTemplateSpecialization:
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000333 case ClassTemplatePartialSpecialization:
Douglas Gregorbd4187b2010-04-22 23:19:50 +0000334 case ObjCImplementation:
335 case ObjCCategory:
336 case ObjCCategoryImpl:
337 // Never looked up by name.
Chris Lattner769dbdf2009-03-27 20:18:19 +0000338 return 0;
339 }
John McCall9488ea12009-11-17 05:59:44 +0000340
341 return 0;
Eli Friedman56d29372008-06-07 16:52:53 +0000342}
343
Sean Huntcf807c42010-08-18 23:23:40 +0000344void Decl::setAttrs(const AttrVec &attrs) {
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000345 assert(!HasAttrs && "Decl already contains attrs.");
346
Sean Huntcf807c42010-08-18 23:23:40 +0000347 AttrVec &AttrBlank = getASTContext().getDeclAttrs(this);
348 assert(AttrBlank.empty() && "HasAttrs was wrong?");
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000349
350 AttrBlank = attrs;
351 HasAttrs = true;
352}
353
Sean Huntcf807c42010-08-18 23:23:40 +0000354void Decl::dropAttrs() {
Eli Friedman56d29372008-06-07 16:52:53 +0000355 if (!HasAttrs) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Eli Friedman56d29372008-06-07 16:52:53 +0000357 HasAttrs = false;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000358 getASTContext().eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000359}
360
Sean Huntcf807c42010-08-18 23:23:40 +0000361const AttrVec &Decl::getAttrs() const {
362 assert(HasAttrs && "No attrs to get!");
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000363 return getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000364}
365
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000366void Decl::swapAttrs(Decl *RHS) {
Eli Friedman56d29372008-06-07 16:52:53 +0000367 bool HasLHSAttr = this->HasAttrs;
368 bool HasRHSAttr = RHS->HasAttrs;
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Eli Friedman56d29372008-06-07 16:52:53 +0000370 // Usually, neither decl has attrs, nothing to do.
371 if (!HasLHSAttr && !HasRHSAttr) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Eli Friedman56d29372008-06-07 16:52:53 +0000373 // If 'this' has no attrs, swap the other way.
374 if (!HasLHSAttr)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000375 return RHS->swapAttrs(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000376
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000377 ASTContext &Context = getASTContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000378
Eli Friedman56d29372008-06-07 16:52:53 +0000379 // Handle the case when both decls have attrs.
380 if (HasRHSAttr) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000381 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman56d29372008-06-07 16:52:53 +0000382 return;
383 }
Mike Stump1eb44332009-09-09 15:08:12 +0000384
Eli Friedman56d29372008-06-07 16:52:53 +0000385 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor68584ed2009-06-18 16:11:24 +0000386 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
387 Context.eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000388 this->HasAttrs = false;
389 RHS->HasAttrs = true;
390}
391
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000392Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000393 Decl::Kind DK = D->getDeclKind();
394 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000395#define DECL(NAME, BASE)
396#define DECL_CONTEXT(NAME) \
397 case Decl::NAME: \
398 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
399#define DECL_CONTEXT_BASE(NAME)
400#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000401 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000402#define DECL(NAME, BASE)
403#define DECL_CONTEXT_BASE(NAME) \
404 if (DK >= first##NAME && DK <= last##NAME) \
405 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
406#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000407 assert(false && "a decl that inherits DeclContext isn't handled");
408 return 0;
409 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000410}
411
412DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000413 Decl::Kind DK = D->getKind();
414 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000415#define DECL(NAME, BASE)
416#define DECL_CONTEXT(NAME) \
417 case Decl::NAME: \
418 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
419#define DECL_CONTEXT_BASE(NAME)
420#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000421 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000422#define DECL(NAME, BASE)
423#define DECL_CONTEXT_BASE(NAME) \
424 if (DK >= first##NAME && DK <= last##NAME) \
425 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
426#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000427 assert(false && "a decl that inherits DeclContext isn't handled");
428 return 0;
429 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000430}
431
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000432SourceLocation Decl::getBodyRBrace() const {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000433 // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
434 // FunctionDecl stores EndRangeLoc for this purpose.
435 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
436 const FunctionDecl *Definition;
437 if (FD->hasBody(Definition))
438 return Definition->getSourceRange().getEnd();
439 return SourceLocation();
440 }
441
Argyrios Kyrtzidis6717ef42010-07-07 11:31:27 +0000442 if (Stmt *Body = getBody())
443 return Body->getSourceRange().getEnd();
444
445 return SourceLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +0000446}
447
Anders Carlsson1329c272009-03-25 23:38:06 +0000448void Decl::CheckAccessDeclContext() const {
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000449#ifndef NDEBUG
John McCall46460a62010-01-20 21:53:11 +0000450 // Suppress this check if any of the following hold:
451 // 1. this is the translation unit (and thus has no parent)
452 // 2. this is a template parameter (and thus doesn't belong to its context)
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000453 // 3. this is a non-type template parameter
454 // 4. the context is not a record
455 // 5. it's invalid
456 // 6. it's a C++0x static_assert.
Anders Carlsson35eda442009-08-29 20:47:47 +0000457 if (isa<TranslationUnitDecl>(this) ||
Argyrios Kyrtzidis04aed0e2010-07-02 11:55:44 +0000458 isa<TemplateTypeParmDecl>(this) ||
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000459 isa<NonTypeTemplateParmDecl>(this) ||
Douglas Gregorfdd8ab12010-02-22 17:53:38 +0000460 !isa<CXXRecordDecl>(getDeclContext()) ||
Argyrios Kyrtzidis65b63ec2010-09-08 21:32:35 +0000461 isInvalidDecl() ||
462 isa<StaticAssertDecl>(this) ||
463 // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
464 // as DeclContext (?).
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000465 isa<ParmVarDecl>(this) ||
466 // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
467 // AS_none as access specifier.
468 isa<CXXRecordDecl>(this))
Anders Carlsson35eda442009-08-29 20:47:47 +0000469 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000470
471 assert(Access != AS_none &&
Anders Carlsson1329c272009-03-25 23:38:06 +0000472 "Access specifier is AS_none inside a record decl");
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000473#endif
Anders Carlsson1329c272009-03-25 23:38:06 +0000474}
475
Anders Carlsson1329c272009-03-25 23:38:06 +0000476
Eli Friedman56d29372008-06-07 16:52:53 +0000477//===----------------------------------------------------------------------===//
478// DeclContext Implementation
479//===----------------------------------------------------------------------===//
480
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000481bool DeclContext::classof(const Decl *D) {
482 switch (D->getKind()) {
Sean Hunt9a555912010-05-30 07:21:58 +0000483#define DECL(NAME, BASE)
484#define DECL_CONTEXT(NAME) case Decl::NAME:
485#define DECL_CONTEXT_BASE(NAME)
486#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000487 return true;
488 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000489#define DECL(NAME, BASE)
490#define DECL_CONTEXT_BASE(NAME) \
491 if (D->getKind() >= Decl::first##NAME && \
492 D->getKind() <= Decl::last##NAME) \
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000493 return true;
Sean Hunt9a555912010-05-30 07:21:58 +0000494#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000495 return false;
496 }
497}
498
Douglas Gregora2da7802010-07-25 18:38:02 +0000499DeclContext::~DeclContext() { }
Douglas Gregor44b43212008-12-11 16:49:14 +0000500
Douglas Gregore942bbe2009-09-10 16:57:35 +0000501/// \brief Find the parent context of this context that will be
502/// used for unqualified name lookup.
503///
504/// Generally, the parent lookup context is the semantic context. However, for
505/// a friend function the parent lookup context is the lexical context, which
506/// is the class in which the friend is declared.
507DeclContext *DeclContext::getLookupParent() {
508 // FIXME: Find a better way to identify friends
509 if (isa<FunctionDecl>(this))
Sebastian Redl7a126a42010-08-31 00:36:30 +0000510 if (getParent()->getRedeclContext()->isFileContext() &&
511 getLexicalParent()->getRedeclContext()->isRecord())
Douglas Gregore942bbe2009-09-10 16:57:35 +0000512 return getLexicalParent();
513
514 return getParent();
515}
516
Sebastian Redl410c4f22010-08-31 20:53:31 +0000517bool DeclContext::isInlineNamespace() const {
518 return isNamespace() &&
519 cast<NamespaceDecl>(this)->isInline();
520}
521
Douglas Gregorbc221632009-05-28 16:34:51 +0000522bool DeclContext::isDependentContext() const {
523 if (isFileContext())
524 return false;
525
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000526 if (isa<ClassTemplatePartialSpecializationDecl>(this))
527 return true;
528
Douglas Gregorbc221632009-05-28 16:34:51 +0000529 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
530 if (Record->getDescribedClassTemplate())
531 return true;
532
John McCall0c01d182010-03-24 05:22:00 +0000533 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregorbc221632009-05-28 16:34:51 +0000534 if (Function->getDescribedFunctionTemplate())
535 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000536
John McCall0c01d182010-03-24 05:22:00 +0000537 // Friend function declarations are dependent if their *lexical*
538 // context is dependent.
539 if (cast<Decl>(this)->getFriendObjectKind())
540 return getLexicalParent()->isDependentContext();
541 }
542
Douglas Gregorbc221632009-05-28 16:34:51 +0000543 return getParent() && getParent()->isDependentContext();
544}
545
Douglas Gregor074149e2009-01-05 19:45:36 +0000546bool DeclContext::isTransparentContext() const {
547 if (DeclKind == Decl::Enum)
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000548 return !cast<EnumDecl>(this)->isScoped();
Douglas Gregor074149e2009-01-05 19:45:36 +0000549 else if (DeclKind == Decl::LinkageSpec)
550 return true;
Douglas Gregor074149e2009-01-05 19:45:36 +0000551
552 return false;
553}
554
John McCallac65c622010-10-26 04:59:26 +0000555bool DeclContext::isExternCContext() const {
556 const DeclContext *DC = this;
557 while (DC->DeclKind != Decl::TranslationUnit) {
558 if (DC->DeclKind == Decl::LinkageSpec)
559 return cast<LinkageSpecDecl>(DC)->getLanguage()
560 == LinkageSpecDecl::lang_c;
561 DC = DC->getParent();
562 }
563 return false;
564}
565
Sebastian Redl7a126a42010-08-31 00:36:30 +0000566bool DeclContext::Encloses(const DeclContext *DC) const {
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000567 if (getPrimaryContext() != this)
568 return getPrimaryContext()->Encloses(DC);
Mike Stump1eb44332009-09-09 15:08:12 +0000569
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000570 for (; DC; DC = DC->getParent())
571 if (DC->getPrimaryContext() == this)
572 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000573 return false;
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000574}
575
Steve Naroff0701bbb2009-01-08 17:28:14 +0000576DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000577 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000578 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000579 case Decl::LinkageSpec:
Mike Stump1eb44332009-09-09 15:08:12 +0000580 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000581 // There is only one DeclContext for these entities.
582 return this;
583
584 case Decl::Namespace:
585 // The original namespace is our primary context.
586 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
587
Douglas Gregor44b43212008-12-11 16:49:14 +0000588 case Decl::ObjCMethod:
589 return this;
590
591 case Decl::ObjCInterface:
Steve Naroff0701bbb2009-01-08 17:28:14 +0000592 case Decl::ObjCProtocol:
593 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000594 // FIXME: Can Objective-C interfaces be forward-declared?
595 return this;
596
Steve Naroff0701bbb2009-01-08 17:28:14 +0000597 case Decl::ObjCImplementation:
598 case Decl::ObjCCategoryImpl:
599 return this;
600
Douglas Gregor44b43212008-12-11 16:49:14 +0000601 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000602 if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000603 // If this is a tag type that has a definition or is currently
604 // being defined, that definition is our primary context.
John McCall3cb0ebd2010-03-10 03:28:59 +0000605 TagDecl *Tag = cast<TagDecl>(this);
606 assert(isa<TagType>(Tag->TypeForDecl) ||
607 isa<InjectedClassNameType>(Tag->TypeForDecl));
608
609 if (TagDecl *Def = Tag->getDefinition())
610 return Def;
611
612 if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) {
613 const TagType *TagTy = cast<TagType>(Tag->TypeForDecl);
614 if (TagTy->isBeingDefined())
615 // FIXME: is it necessarily being defined in the decl
616 // that owns the type?
617 return TagTy->getDecl();
618 }
619
620 return Tag;
Douglas Gregorcc636682009-02-17 23:15:12 +0000621 }
622
Sean Hunt9a555912010-05-30 07:21:58 +0000623 assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
Douglas Gregor44b43212008-12-11 16:49:14 +0000624 "Unknown DeclContext kind");
625 return this;
626 }
627}
628
629DeclContext *DeclContext::getNextContext() {
630 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000631 case Decl::Namespace:
632 // Return the next namespace
633 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
634
635 default:
Douglas Gregor44b43212008-12-11 16:49:14 +0000636 return 0;
637 }
638}
639
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000640std::pair<Decl *, Decl *>
641DeclContext::BuildDeclChain(const llvm::SmallVectorImpl<Decl*> &Decls) {
642 // Build up a chain of declarations via the Decl::NextDeclInContext field.
643 Decl *FirstNewDecl = 0;
644 Decl *PrevDecl = 0;
645 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
646 Decl *D = Decls[I];
647 if (PrevDecl)
648 PrevDecl->NextDeclInContext = D;
649 else
650 FirstNewDecl = D;
651
652 PrevDecl = D;
653 }
654
655 return std::make_pair(FirstNewDecl, PrevDecl);
656}
657
Douglas Gregor2cf26342009-04-09 22:27:44 +0000658/// \brief Load the declarations within this lexical storage from an
659/// external source.
Mike Stump1eb44332009-09-09 15:08:12 +0000660void
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000661DeclContext::LoadLexicalDeclsFromExternalStorage() const {
662 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000663 assert(hasExternalLexicalStorage() && Source && "No external storage?");
664
Argyrios Kyrtzidis0dbbc042010-07-30 10:03:23 +0000665 // Notify that we have a DeclContext that is initializing.
666 ExternalASTSource::Deserializing ADeclContext(Source);
667
John McCall76bd1f32010-06-01 09:23:16 +0000668 llvm::SmallVector<Decl*, 64> Decls;
669 if (Source->FindExternalLexicalDecls(this, Decls))
Douglas Gregor2cf26342009-04-09 22:27:44 +0000670 return;
671
672 // There is no longer any lexical storage in this context
673 ExternalLexicalStorage = false;
674
675 if (Decls.empty())
676 return;
677
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000678 // We may have already loaded just the fields of this record, in which case
679 // don't add the decls, just replace the FirstDecl/LastDecl chain.
680 if (const RecordDecl *RD = dyn_cast<RecordDecl>(this))
681 if (RD->LoadedFieldsFromExternalStorage) {
682 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
683 return;
684 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000685
686 // Splice the newly-read declarations into the beginning of the list
687 // of declarations.
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000688 Decl *ExternalFirst, *ExternalLast;
689 llvm::tie(ExternalFirst, ExternalLast) = BuildDeclChain(Decls);
690 ExternalLast->NextDeclInContext = FirstDecl;
691 FirstDecl = ExternalFirst;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000692 if (!LastDecl)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000693 LastDecl = ExternalLast;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000694}
695
John McCall76bd1f32010-06-01 09:23:16 +0000696DeclContext::lookup_result
697ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
698 DeclarationName Name) {
699 ASTContext &Context = DC->getParentASTContext();
700 StoredDeclsMap *Map;
701 if (!(Map = DC->LookupPtr))
702 Map = DC->CreateStoredDeclsMap(Context);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000703
John McCall76bd1f32010-06-01 09:23:16 +0000704 StoredDeclsList &List = (*Map)[Name];
705 assert(List.isNull());
706 (void) List;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000707
John McCall76bd1f32010-06-01 09:23:16 +0000708 return DeclContext::lookup_result();
709}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000710
John McCall76bd1f32010-06-01 09:23:16 +0000711DeclContext::lookup_result
712ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +0000713 DeclarationName Name,
714 llvm::SmallVectorImpl<NamedDecl*> &Decls) {
715 ASTContext &Context = DC->getParentASTContext();;
716
717 StoredDeclsMap *Map;
718 if (!(Map = DC->LookupPtr))
719 Map = DC->CreateStoredDeclsMap(Context);
720
721 StoredDeclsList &List = (*Map)[Name];
722 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
723 if (List.isNull())
724 List.setOnlyValue(Decls[I]);
725 else
726 List.AddSubsequentDecl(Decls[I]);
727 }
728
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000729 return List.getLookupResult();
John McCall76bd1f32010-06-01 09:23:16 +0000730}
731
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000732void ExternalASTSource::MaterializeVisibleDeclsForName(const DeclContext *DC,
733 DeclarationName Name,
734 llvm::SmallVectorImpl<NamedDecl*> &Decls) {
735 assert(DC->LookupPtr);
736 StoredDeclsMap &Map = *DC->LookupPtr;
737
738 // If there's an entry in the table the visible decls for this name have
739 // already been deserialized.
740 if (Map.find(Name) == Map.end()) {
741 StoredDeclsList &List = Map[Name];
742 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
743 if (List.isNull())
744 List.setOnlyValue(Decls[I]);
745 else
746 List.AddSubsequentDecl(Decls[I]);
747 }
748 }
749}
750
Sebastian Redl681d7232010-07-27 00:17:23 +0000751DeclContext::decl_iterator DeclContext::noload_decls_begin() const {
752 return decl_iterator(FirstDecl);
753}
754
755DeclContext::decl_iterator DeclContext::noload_decls_end() const {
756 return decl_iterator();
757}
758
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000759DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000760 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000761 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000762
763 // FIXME: Check whether we need to load some declarations from
764 // external storage.
Mike Stump1eb44332009-09-09 15:08:12 +0000765 return decl_iterator(FirstDecl);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000766}
767
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000768DeclContext::decl_iterator DeclContext::decls_end() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000769 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000770 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000771
Mike Stump1eb44332009-09-09 15:08:12 +0000772 return decl_iterator();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000773}
774
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000775bool DeclContext::decls_empty() const {
Douglas Gregor8038d512009-04-10 17:25:41 +0000776 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000777 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor8038d512009-04-10 17:25:41 +0000778
779 return !FirstDecl;
780}
781
John McCall9f54ad42009-12-10 09:41:52 +0000782void DeclContext::removeDecl(Decl *D) {
783 assert(D->getLexicalDeclContext() == this &&
784 "decl being removed from non-lexical context");
785 assert((D->NextDeclInContext || D == LastDecl) &&
786 "decl is not in decls list");
787
788 // Remove D from the decl chain. This is O(n) but hopefully rare.
789 if (D == FirstDecl) {
790 if (D == LastDecl)
791 FirstDecl = LastDecl = 0;
792 else
793 FirstDecl = D->NextDeclInContext;
794 } else {
795 for (Decl *I = FirstDecl; true; I = I->NextDeclInContext) {
796 assert(I && "decl not found in linked list");
797 if (I->NextDeclInContext == D) {
798 I->NextDeclInContext = D->NextDeclInContext;
799 if (D == LastDecl) LastDecl = I;
800 break;
801 }
802 }
803 }
804
805 // Mark that D is no longer in the decl chain.
806 D->NextDeclInContext = 0;
807
808 // Remove D from the lookup table if necessary.
809 if (isa<NamedDecl>(D)) {
810 NamedDecl *ND = cast<NamedDecl>(D);
811
John McCall0c01d182010-03-24 05:22:00 +0000812 StoredDeclsMap *Map = getPrimaryContext()->LookupPtr;
813 if (!Map) return;
John McCall9f54ad42009-12-10 09:41:52 +0000814
John McCall9f54ad42009-12-10 09:41:52 +0000815 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
816 assert(Pos != Map->end() && "no lookup entry for decl");
817 Pos->second.remove(ND);
818 }
819}
820
John McCall3f9a8a62009-08-11 06:59:38 +0000821void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner7f0be132009-02-20 00:56:18 +0000822 assert(D->getLexicalDeclContext() == this &&
823 "Decl inserted into wrong lexical context");
Mike Stump1eb44332009-09-09 15:08:12 +0000824 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000825 "Decl already inserted into a DeclContext");
826
827 if (FirstDecl) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000828 LastDecl->NextDeclInContext = D;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000829 LastDecl = D;
830 } else {
831 FirstDecl = LastDecl = D;
832 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000833
834 // Notify a C++ record declaration that we've added a member, so it can
835 // update it's class-specific state.
836 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
837 Record->addedMember(D);
John McCall3f9a8a62009-08-11 06:59:38 +0000838}
839
840void DeclContext::addDecl(Decl *D) {
841 addHiddenDecl(D);
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000842
843 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000844 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor44b43212008-12-11 16:49:14 +0000845}
846
Douglas Gregor074149e2009-01-05 19:45:36 +0000847/// buildLookup - Build the lookup data structure with all of the
848/// declarations in DCtx (and any other contexts linked to it or
849/// transparent contexts nested within it).
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000850void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000851 for (; DCtx; DCtx = DCtx->getNextContext()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000852 for (decl_iterator D = DCtx->decls_begin(),
853 DEnd = DCtx->decls_end();
Douglas Gregor4f3b8f82009-01-06 07:17:58 +0000854 D != DEnd; ++D) {
John McCall3f9a8a62009-08-11 06:59:38 +0000855 // Insert this declaration into the lookup structure, but only
856 // if it's semantically in its decl context. During non-lazy
857 // lookup building, this is implicitly enforced by addDecl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000858 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
John McCall3f9a8a62009-08-11 06:59:38 +0000859 if (D->getDeclContext() == DCtx)
860 makeDeclVisibleInContextImpl(ND);
Douglas Gregor074149e2009-01-05 19:45:36 +0000861
Ted Kremenekc32b1d82009-11-17 22:58:30 +0000862 // Insert any forward-declared Objective-C interfaces into the lookup
863 // data structure.
864 if (ObjCClassDecl *Class = dyn_cast<ObjCClassDecl>(*D))
865 for (ObjCClassDecl::iterator I = Class->begin(), IEnd = Class->end();
866 I != IEnd; ++I)
Ted Kremenek321c22f2009-11-18 00:28:11 +0000867 makeDeclVisibleInContextImpl(I->getInterface());
Ted Kremenekc32b1d82009-11-17 22:58:30 +0000868
Sebastian Redl410c4f22010-08-31 20:53:31 +0000869 // If this declaration is itself a transparent declaration context or
870 // inline namespace, add its members (recursively).
Douglas Gregor074149e2009-01-05 19:45:36 +0000871 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
Sebastian Redl410c4f22010-08-31 20:53:31 +0000872 if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000873 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregor074149e2009-01-05 19:45:36 +0000874 }
875 }
876}
877
Mike Stump1eb44332009-09-09 15:08:12 +0000878DeclContext::lookup_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000879DeclContext::lookup(DeclarationName Name) {
Steve Naroff0701bbb2009-01-08 17:28:14 +0000880 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000881 if (PrimaryContext != this)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000882 return PrimaryContext->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000883
John McCall76bd1f32010-06-01 09:23:16 +0000884 if (hasExternalVisibleStorage()) {
885 // Check to see if we've already cached the lookup results.
886 if (LookupPtr) {
887 StoredDeclsMap::iterator I = LookupPtr->find(Name);
888 if (I != LookupPtr->end())
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000889 return I->second.getLookupResult();
John McCall76bd1f32010-06-01 09:23:16 +0000890 }
891
892 ExternalASTSource *Source = getParentASTContext().getExternalSource();
893 return Source->FindExternalVisibleDeclsByName(this, Name);
894 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000895
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000896 /// If there is no lookup data structure, build one now by walking
Douglas Gregor44b43212008-12-11 16:49:14 +0000897 /// all of the linked DeclContexts (in declaration order!) and
898 /// inserting their values.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000899 if (!LookupPtr) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000900 buildLookup(this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000901
Douglas Gregorc36c5402009-04-09 17:29:08 +0000902 if (!LookupPtr)
Douglas Gregora5fdd9c2010-05-11 06:18:17 +0000903 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Douglas Gregorc36c5402009-04-09 17:29:08 +0000904 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000905
John McCall0c01d182010-03-24 05:22:00 +0000906 StoredDeclsMap::iterator Pos = LookupPtr->find(Name);
907 if (Pos == LookupPtr->end())
Douglas Gregora5fdd9c2010-05-11 06:18:17 +0000908 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000909 return Pos->second.getLookupResult();
Douglas Gregor44b43212008-12-11 16:49:14 +0000910}
911
Mike Stump1eb44332009-09-09 15:08:12 +0000912DeclContext::lookup_const_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000913DeclContext::lookup(DeclarationName Name) const {
914 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000915}
916
Sebastian Redl7a126a42010-08-31 00:36:30 +0000917DeclContext *DeclContext::getRedeclContext() {
Chris Lattner0cf2b192009-03-27 19:19:59 +0000918 DeclContext *Ctx = this;
Sebastian Redl410c4f22010-08-31 20:53:31 +0000919 // Skip through transparent contexts.
920 while (Ctx->isTransparentContext())
Douglas Gregorce356072009-01-06 23:51:29 +0000921 Ctx = Ctx->getParent();
922 return Ctx;
923}
924
Douglas Gregor88b70942009-02-25 22:02:03 +0000925DeclContext *DeclContext::getEnclosingNamespaceContext() {
926 DeclContext *Ctx = this;
927 // Skip through non-namespace, non-translation-unit contexts.
Sebastian Redl51a8a372010-08-31 00:36:23 +0000928 while (!Ctx->isFileContext())
Douglas Gregor88b70942009-02-25 22:02:03 +0000929 Ctx = Ctx->getParent();
930 return Ctx->getPrimaryContext();
931}
932
Sebastian Redl7a126a42010-08-31 00:36:30 +0000933bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
934 // For non-file contexts, this is equivalent to Equals.
935 if (!isFileContext())
936 return O->Equals(this);
937
938 do {
939 if (O->Equals(this))
940 return true;
941
942 const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O);
943 if (!NS || !NS->isInline())
944 break;
945 O = NS->getParent();
946 } while (O);
947
948 return false;
949}
950
John McCallab88d972009-08-31 22:39:49 +0000951void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000952 // FIXME: This feels like a hack. Should DeclarationName support
953 // template-ids, or is there a better way to keep specializations
954 // from being visible?
955 if (isa<ClassTemplateSpecializationDecl>(D))
956 return;
Eli Friedman6bc20132009-12-08 05:40:03 +0000957 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
958 if (FD->isFunctionTemplateSpecialization())
959 return;
Douglas Gregorcc636682009-02-17 23:15:12 +0000960
Steve Naroff0701bbb2009-01-08 17:28:14 +0000961 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000962 if (PrimaryContext != this) {
John McCallab88d972009-08-31 22:39:49 +0000963 PrimaryContext->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor44b43212008-12-11 16:49:14 +0000964 return;
965 }
966
967 // If we already have a lookup data structure, perform the insertion
Argyrios Kyrtzidis5586b012010-07-04 21:44:25 +0000968 // into it. If we haven't deserialized externally stored decls, deserialize
969 // them so we can add the decl. Otherwise, be lazy and don't build that
970 // structure until someone asks for it.
971 if (LookupPtr || !Recoverable || hasExternalVisibleStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000972 makeDeclVisibleInContextImpl(D);
Douglas Gregor074149e2009-01-05 19:45:36 +0000973
Sebastian Redl410c4f22010-08-31 20:53:31 +0000974 // If we are a transparent context or inline namespace, insert into our
975 // parent context, too. This operation is recursive.
976 if (isTransparentContext() || isInlineNamespace())
John McCallab88d972009-08-31 22:39:49 +0000977 getParent()->makeDeclVisibleInContext(D, Recoverable);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +0000978
979 Decl *DCAsDecl = cast<Decl>(this);
980 // Notify that a decl was made visible unless it's a Tag being defined.
981 if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined()))
982 if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
983 L->AddedVisibleDecl(this, D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000984}
985
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000986void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000987 // Skip unnamed declarations.
988 if (!D->getDeclName())
989 return;
990
Douglas Gregorcc636682009-02-17 23:15:12 +0000991 // FIXME: This feels like a hack. Should DeclarationName support
992 // template-ids, or is there a better way to keep specializations
993 // from being visible?
994 if (isa<ClassTemplateSpecializationDecl>(D))
995 return;
996
Argyrios Kyrtzidis5586b012010-07-04 21:44:25 +0000997 ASTContext *C = 0;
998 if (!LookupPtr) {
999 C = &getParentASTContext();
1000 CreateStoredDeclsMap(*C);
1001 }
1002
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001003 // If there is an external AST source, load any declarations it knows about
1004 // with this declaration's name.
1005 // If the lookup table contains an entry about this name it means that we
1006 // have already checked the external source.
1007 if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
1008 if (hasExternalVisibleStorage() &&
1009 LookupPtr->find(D->getDeclName()) == LookupPtr->end())
1010 Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
1011
Douglas Gregor44b43212008-12-11 16:49:14 +00001012 // Insert this declaration into the map.
John McCall0c01d182010-03-24 05:22:00 +00001013 StoredDeclsList &DeclNameEntries = (*LookupPtr)[D->getDeclName()];
Chris Lattner67762a32009-02-20 01:44:05 +00001014 if (DeclNameEntries.isNull()) {
1015 DeclNameEntries.setOnlyValue(D);
Chris Lattnerbd6c8002009-02-19 07:00:44 +00001016 return;
Douglas Gregor44b43212008-12-11 16:49:14 +00001017 }
Chris Lattner91942502009-02-20 00:55:03 +00001018
Chris Lattnerbdc3d002009-02-20 01:10:07 +00001019 // If it is possible that this is a redeclaration, check to see if there is
1020 // already a decl for which declarationReplaces returns true. If there is
1021 // one, just replace it and return.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001022 if (DeclNameEntries.HandleRedeclaration(D))
Chris Lattner67762a32009-02-20 01:44:05 +00001023 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001024
Chris Lattnerbd6c8002009-02-19 07:00:44 +00001025 // Put this declaration into the appropriate slot.
Chris Lattner67762a32009-02-20 01:44:05 +00001026 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor44b43212008-12-11 16:49:14 +00001027}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001028
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00001029void DeclContext::MaterializeVisibleDeclsFromExternalStorage() {
1030 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1031 assert(hasExternalVisibleStorage() && Source && "No external storage?");
1032
1033 if (!LookupPtr)
1034 CreateStoredDeclsMap(getParentASTContext());
1035 Source->MaterializeVisibleDecls(this);
1036}
1037
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001038/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
1039/// this context.
Mike Stump1eb44332009-09-09 15:08:12 +00001040DeclContext::udir_iterator_range
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001041DeclContext::getUsingDirectives() const {
1042 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001043 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
1044 reinterpret_cast<udir_iterator>(Result.second));
1045}
Douglas Gregor2cf26342009-04-09 22:27:44 +00001046
Ted Kremenek3478eb62010-02-11 07:12:28 +00001047//===----------------------------------------------------------------------===//
1048// Creation and Destruction of StoredDeclsMaps. //
1049//===----------------------------------------------------------------------===//
1050
John McCall0c01d182010-03-24 05:22:00 +00001051StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
1052 assert(!LookupPtr && "context already has a decls map");
1053 assert(getPrimaryContext() == this &&
1054 "creating decls map on non-primary context");
1055
1056 StoredDeclsMap *M;
1057 bool Dependent = isDependentContext();
1058 if (Dependent)
1059 M = new DependentStoredDeclsMap();
1060 else
1061 M = new StoredDeclsMap();
1062 M->Previous = C.LastSDM;
1063 C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
1064 LookupPtr = M;
Ted Kremenek3478eb62010-02-11 07:12:28 +00001065 return M;
1066}
1067
1068void ASTContext::ReleaseDeclContextMaps() {
John McCall0c01d182010-03-24 05:22:00 +00001069 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1070 // pointer because the subclass doesn't add anything that needs to
1071 // be deleted.
John McCall0c01d182010-03-24 05:22:00 +00001072 StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1073}
1074
1075void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1076 while (Map) {
1077 // Advance the iteration before we invalidate memory.
1078 llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1079
1080 if (Dependent)
1081 delete static_cast<DependentStoredDeclsMap*>(Map);
1082 else
1083 delete Map;
1084
1085 Map = Next.getPointer();
1086 Dependent = Next.getInt();
1087 }
1088}
1089
John McCall0c01d182010-03-24 05:22:00 +00001090DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1091 DeclContext *Parent,
1092 const PartialDiagnostic &PDiag) {
1093 assert(Parent->isDependentContext()
1094 && "cannot iterate dependent diagnostics of non-dependent context");
1095 Parent = Parent->getPrimaryContext();
1096 if (!Parent->LookupPtr)
1097 Parent->CreateStoredDeclsMap(C);
1098
1099 DependentStoredDeclsMap *Map
1100 = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr);
1101
Douglas Gregorb8365182010-03-29 23:56:53 +00001102 // Allocate the copy of the PartialDiagnostic via the ASTContext's
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00001103 // BumpPtrAllocator, rather than the ASTContext itself.
Douglas Gregorb8365182010-03-29 23:56:53 +00001104 PartialDiagnostic::Storage *DiagStorage = 0;
1105 if (PDiag.hasStorage())
1106 DiagStorage = new (C) PartialDiagnostic::Storage;
1107
1108 DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
John McCall0c01d182010-03-24 05:22:00 +00001109
1110 // TODO: Maybe we shouldn't reverse the order during insertion.
1111 DD->NextDiagnostic = Map->FirstDiagnostic;
1112 Map->FirstDiagnostic = DD;
1113
1114 return DD;
Ted Kremenek3478eb62010-02-11 07:12:28 +00001115}