blob: 0642f423c6f34516ebaa431f0febb07d6b5b092e [file] [log] [blame]
Eli Friedman7dbab8a2008-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 Gregor8bd3c2e2009-02-02 23:39:07 +000015#include "clang/AST/Decl.h"
Douglas Gregorb1fe2c92009-04-07 17:20:56 +000016#include "clang/AST/DeclContextInternals.h"
Argyrios Kyrtzidis2951e142008-06-09 21:05:31 +000017#include "clang/AST/DeclCXX.h"
John McCallbbbbe4e2010-03-11 07:50:04 +000018#include "clang/AST/DeclFriend.h"
Douglas Gregorded2d7b2009-02-04 19:02:06 +000019#include "clang/AST/DeclObjC.h"
20#include "clang/AST/DeclTemplate.h"
John McCallc62bb642010-03-24 05:22:00 +000021#include "clang/AST/DependentDiagnostic.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000022#include "clang/AST/ExternalASTSource.h"
Eli Friedman7dbab8a2008-06-07 16:52:53 +000023#include "clang/AST/ASTContext.h"
Douglas Gregor91f84212008-12-11 16:49:14 +000024#include "clang/AST/Type.h"
Sebastian Redla7b98a72009-04-26 20:35:05 +000025#include "clang/AST/Stmt.h"
26#include "clang/AST/StmtCXX.h"
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +000027#include "clang/AST/ASTMutationListener.h"
Eli Friedman7dbab8a2008-06-07 16:52:53 +000028#include "llvm/ADT/DenseMap.h"
Chris Lattnereae6cb62009-03-05 08:00:35 +000029#include "llvm/Support/raw_ostream.h"
Douglas Gregor8b9ccca2008-12-23 21:05:05 +000030#include <algorithm>
Chris Lattnerc25d8a72009-03-02 22:20:04 +000031#include <cstdio>
Douglas Gregor55297ac2008-12-23 00:26:44 +000032#include <vector>
Eli Friedman7dbab8a2008-06-07 16:52:53 +000033using namespace clang;
34
35//===----------------------------------------------------------------------===//
36// Statistics
37//===----------------------------------------------------------------------===//
38
Alexis Hunted053252010-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 Friedman7dbab8a2008-06-07 16:52:53 +000042
43static bool StatSwitch = false;
44
Eli Friedman7dbab8a2008-06-07 16:52:53 +000045const char *Decl::getDeclKindName() const {
46 switch (DeclKind) {
Alexis Hunted053252010-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 Friedman7dbab8a2008-06-07 16:52:53 +000051 }
52}
53
Douglas Gregor90d47172010-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 Naroff5faaef72009-01-20 19:53:53 +000064const char *DeclContext::getDeclKindName() const {
65 switch (DeclKind) {
Alexis Hunted053252010-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 Naroff5faaef72009-01-20 19:53:53 +000070 }
71}
72
Eli Friedman7dbab8a2008-06-07 16:52:53 +000073bool Decl::CollectingStats(bool Enable) {
Kovarththanan Rajaratnam130f7f92009-11-29 14:54:35 +000074 if (Enable) StatSwitch = true;
Eli Friedman7dbab8a2008-06-07 16:52:53 +000075 return StatSwitch;
76}
77
78void Decl::PrintStats() {
79 fprintf(stderr, "*** Decl Stats:\n");
Mike Stump11289f42009-09-09 15:08:12 +000080
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +000081 int totalDecls = 0;
Alexis Hunted053252010-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 Gregor8bd3c2e2009-02-02 23:39:07 +000085 fprintf(stderr, " %d decls total.\n", totalDecls);
Mike Stump11289f42009-09-09 15:08:12 +000086
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +000087 int totalBytes = 0;
Alexis Hunted053252010-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 Gregor8bd3c2e2009-02-02 23:39:07 +000094 }
Alexis Hunted053252010-05-30 07:21:58 +000095#define ABSTRACT_DECL(DECL)
96#include "clang/AST/DeclNodes.inc"
Mike Stump11289f42009-09-09 15:08:12 +000097
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +000098 fprintf(stderr, "Total bytes = %d\n", totalBytes);
Eli Friedman7dbab8a2008-06-07 16:52:53 +000099}
100
Alexis Hunted053252010-05-30 07:21:58 +0000101void Decl::add(Kind k) {
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000102 switch (k) {
Alexis Hunted053252010-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 Friedman7dbab8a2008-06-07 16:52:53 +0000107 }
108}
109
Anders Carlssonaa73b912009-06-13 00:08:58 +0000110bool Decl::isTemplateParameterPack() const {
111 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
112 return TTP->isParameterPack();
Douglas Gregorda3cc0d2010-12-23 23:51:58 +0000113 if (const NonTypeTemplateParmDecl *NTTP
Douglas Gregorf5500772011-01-05 15:48:55 +0000114 = dyn_cast<NonTypeTemplateParmDecl>(this))
Douglas Gregorda3cc0d2010-12-23 23:51:58 +0000115 return NTTP->isParameterPack();
Douglas Gregorf5500772011-01-05 15:48:55 +0000116 if (const TemplateTemplateParmDecl *TTP
117 = dyn_cast<TemplateTemplateParmDecl>(this))
118 return TTP->isParameterPack();
Anders Carlssonaa73b912009-06-13 00:08:58 +0000119 return false;
120}
121
Douglas Gregor3c6bd2a2011-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 Gregorad3f2fc2009-06-25 22:08:12 +0000129bool Decl::isFunctionOrFunctionTemplate() const {
John McCall3f746822009-11-17 05:59:44 +0000130 if (const UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(this))
Anders Carlssonf057cb22009-06-26 05:26:50 +0000131 return UD->getTargetDecl()->isFunctionOrFunctionTemplate();
Mike Stump11289f42009-09-09 15:08:12 +0000132
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000133 return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this);
134}
135
Douglas Gregorf5974fa2010-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
Douglas Gregor133eddd2011-02-17 08:47:29 +0000146
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000147//===----------------------------------------------------------------------===//
Chris Lattnereae6cb62009-03-05 08:00:35 +0000148// PrettyStackTraceDecl Implementation
149//===----------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000150
Chris Lattnereae6cb62009-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 Stump11289f42009-09-09 15:08:12 +0000155
Chris Lattnereae6cb62009-03-05 08:00:35 +0000156 if (TheLoc.isValid()) {
157 TheLoc.print(OS, SM);
158 OS << ": ";
159 }
160
161 OS << Message;
162
Daniel Dunbar4f1054e2009-11-21 09:05:59 +0000163 if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
Chris Lattnereae6cb62009-03-05 08:00:35 +0000164 OS << " '" << DN->getQualifiedNameAsString() << '\'';
165 OS << '\n';
166}
Mike Stump11289f42009-09-09 15:08:12 +0000167
Chris Lattnereae6cb62009-03-05 08:00:35 +0000168//===----------------------------------------------------------------------===//
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000169// Decl Implementation
170//===----------------------------------------------------------------------===//
171
Douglas Gregorb11aad82011-02-19 18:51:44 +0000172// Out-of-line virtual method providing a home for Decl.
173Decl::~Decl() { }
Douglas Gregora43942a2011-02-17 07:02:32 +0000174
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000175void Decl::setDeclContext(DeclContext *DC) {
176 if (isOutOfSemaDC())
177 delete getMultipleDC();
Mike Stump11289f42009-09-09 15:08:12 +0000178
Chris Lattnerb81eb052009-03-29 06:06:59 +0000179 DeclCtx = DC;
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000180}
181
182void Decl::setLexicalDeclContext(DeclContext *DC) {
183 if (DC == getLexicalDeclContext())
184 return;
185
186 if (isInSemaDC()) {
Ted Kremenekf8c12a32009-12-01 00:07:10 +0000187 MultipleDC *MDC = new (getASTContext()) MultipleDC();
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000188 MDC->SemanticDC = getDeclContext();
189 MDC->LexicalDC = DC;
Chris Lattnerb81eb052009-03-29 06:06:59 +0000190 DeclCtx = MDC;
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000191 } else {
192 getMultipleDC()->LexicalDC = DC;
193 }
194}
195
John McCall4fa53422009-10-01 00:25:31 +0000196bool Decl::isInAnonymousNamespace() const {
197 const DeclContext *DC = getDeclContext();
198 do {
199 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
200 if (ND->isAnonymousNamespace())
201 return true;
202 } while ((DC = DC->getParent()));
203
204 return false;
205}
206
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000207TranslationUnitDecl *Decl::getTranslationUnitDecl() {
Argyrios Kyrtzidis4e1a72b2009-06-30 02:34:53 +0000208 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
209 return TUD;
210
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000211 DeclContext *DC = getDeclContext();
212 assert(DC && "This decl is not contained in a translation unit!");
Mike Stump11289f42009-09-09 15:08:12 +0000213
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000214 while (!DC->isTranslationUnit()) {
215 DC = DC->getParent();
216 assert(DC && "This decl is not contained in a translation unit!");
217 }
Mike Stump11289f42009-09-09 15:08:12 +0000218
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000219 return cast<TranslationUnitDecl>(DC);
220}
221
222ASTContext &Decl::getASTContext() const {
Mike Stump11289f42009-09-09 15:08:12 +0000223 return getTranslationUnitDecl()->getASTContext();
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000224}
225
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +0000226ASTMutationListener *Decl::getASTMutationListener() const {
227 return getASTContext().getASTMutationListener();
228}
229
Douglas Gregorebada0772010-06-17 23:14:26 +0000230bool Decl::isUsed(bool CheckUsedAttr) const {
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000231 if (Used)
232 return true;
233
234 // Check for used attribute.
Douglas Gregorebada0772010-06-17 23:14:26 +0000235 if (CheckUsedAttr && hasAttr<UsedAttr>())
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000236 return true;
237
238 // Check redeclarations for used attribute.
239 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Douglas Gregorebada0772010-06-17 23:14:26 +0000240 if ((CheckUsedAttr && I->hasAttr<UsedAttr>()) || I->Used)
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000241 return true;
242 }
243
244 return false;
245}
246
247
Chris Lattner8e097192009-03-27 20:18:19 +0000248unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
249 switch (DeclKind) {
John McCall3f746822009-11-17 05:59:44 +0000250 case Function:
251 case CXXMethod:
252 case CXXConstructor:
253 case CXXDestructor:
254 case CXXConversion:
Chris Lattner8e097192009-03-27 20:18:19 +0000255 case EnumConstant:
256 case Var:
257 case ImplicitParam:
258 case ParmVar:
Chris Lattner8e097192009-03-27 20:18:19 +0000259 case NonTypeTemplateParm:
260 case ObjCMethod:
Daniel Dunbar45b2d8a2010-04-23 13:07:39 +0000261 case ObjCProperty:
Daniel Dunbar45b2d8a2010-04-23 13:07:39 +0000262 return IDNS_Ordinary;
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000263 case Label:
264 return IDNS_Label;
Francois Pichet783dd6e2010-11-21 06:08:52 +0000265 case IndirectField:
266 return IDNS_Ordinary | IDNS_Member;
267
John McCalle87beb22010-04-23 18:46:30 +0000268 case ObjCCompatibleAlias:
269 case ObjCInterface:
270 return IDNS_Ordinary | IDNS_Type;
271
272 case Typedef:
273 case UnresolvedUsingTypename:
274 case TemplateTypeParm:
275 return IDNS_Ordinary | IDNS_Type;
276
John McCall3f746822009-11-17 05:59:44 +0000277 case UsingShadow:
278 return 0; // we'll actually overwrite this later
279
John McCalle61f2ba2009-11-18 02:36:19 +0000280 case UnresolvedUsingValue:
John McCalle61f2ba2009-11-18 02:36:19 +0000281 return IDNS_Ordinary | IDNS_Using;
John McCall3f746822009-11-17 05:59:44 +0000282
283 case Using:
284 return IDNS_Using;
285
Chris Lattner8e097192009-03-27 20:18:19 +0000286 case ObjCProtocol:
Douglas Gregor79947a22009-04-24 00:11:27 +0000287 return IDNS_ObjCProtocol;
Mike Stump11289f42009-09-09 15:08:12 +0000288
Chris Lattner8e097192009-03-27 20:18:19 +0000289 case Field:
290 case ObjCAtDefsField:
291 case ObjCIvar:
292 return IDNS_Member;
Mike Stump11289f42009-09-09 15:08:12 +0000293
Chris Lattner8e097192009-03-27 20:18:19 +0000294 case Record:
295 case CXXRecord:
296 case Enum:
John McCalle87beb22010-04-23 18:46:30 +0000297 return IDNS_Tag | IDNS_Type;
Mike Stump11289f42009-09-09 15:08:12 +0000298
Chris Lattner8e097192009-03-27 20:18:19 +0000299 case Namespace:
John McCalle87beb22010-04-23 18:46:30 +0000300 case NamespaceAlias:
301 return IDNS_Namespace;
302
Chris Lattner8e097192009-03-27 20:18:19 +0000303 case FunctionTemplate:
John McCalle87beb22010-04-23 18:46:30 +0000304 return IDNS_Ordinary;
305
Chris Lattner8e097192009-03-27 20:18:19 +0000306 case ClassTemplate:
307 case TemplateTemplateParm:
John McCalle87beb22010-04-23 18:46:30 +0000308 return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
Mike Stump11289f42009-09-09 15:08:12 +0000309
Chris Lattner8e097192009-03-27 20:18:19 +0000310 // Never have names.
John McCallaa74a0c2009-08-28 07:59:38 +0000311 case Friend:
John McCall11083da2009-09-16 22:47:08 +0000312 case FriendTemplate:
Abramo Bagnarad7340582010-06-05 05:09:32 +0000313 case AccessSpec:
Chris Lattner8e097192009-03-27 20:18:19 +0000314 case LinkageSpec:
315 case FileScopeAsm:
316 case StaticAssert:
317 case ObjCClass:
Chris Lattner8e097192009-03-27 20:18:19 +0000318 case ObjCPropertyImpl:
319 case ObjCForwardProtocol:
320 case Block:
321 case TranslationUnit:
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000322
Chris Lattner8e097192009-03-27 20:18:19 +0000323 case UsingDirective:
324 case ClassTemplateSpecialization:
Douglas Gregor2373c592009-05-31 09:31:02 +0000325 case ClassTemplatePartialSpecialization:
Douglas Gregore93525e2010-04-22 23:19:50 +0000326 case ObjCImplementation:
327 case ObjCCategory:
328 case ObjCCategoryImpl:
329 // Never looked up by name.
Chris Lattner8e097192009-03-27 20:18:19 +0000330 return 0;
331 }
John McCall3f746822009-11-17 05:59:44 +0000332
333 return 0;
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000334}
335
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000336void Decl::setAttrs(const AttrVec &attrs) {
Argyrios Kyrtzidis91167172010-06-11 23:09:25 +0000337 assert(!HasAttrs && "Decl already contains attrs.");
338
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000339 AttrVec &AttrBlank = getASTContext().getDeclAttrs(this);
340 assert(AttrBlank.empty() && "HasAttrs was wrong?");
Argyrios Kyrtzidis91167172010-06-11 23:09:25 +0000341
342 AttrBlank = attrs;
343 HasAttrs = true;
344}
345
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000346void Decl::dropAttrs() {
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000347 if (!HasAttrs) return;
Mike Stump11289f42009-09-09 15:08:12 +0000348
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000349 HasAttrs = false;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000350 getASTContext().eraseDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000351}
352
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000353const AttrVec &Decl::getAttrs() const {
354 assert(HasAttrs && "No attrs to get!");
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000355 return getASTContext().getDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000356}
357
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000358void Decl::swapAttrs(Decl *RHS) {
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000359 bool HasLHSAttr = this->HasAttrs;
360 bool HasRHSAttr = RHS->HasAttrs;
Mike Stump11289f42009-09-09 15:08:12 +0000361
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000362 // Usually, neither decl has attrs, nothing to do.
363 if (!HasLHSAttr && !HasRHSAttr) return;
Mike Stump11289f42009-09-09 15:08:12 +0000364
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000365 // If 'this' has no attrs, swap the other way.
366 if (!HasLHSAttr)
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000367 return RHS->swapAttrs(this);
Mike Stump11289f42009-09-09 15:08:12 +0000368
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000369 ASTContext &Context = getASTContext();
Mike Stump11289f42009-09-09 15:08:12 +0000370
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000371 // Handle the case when both decls have attrs.
372 if (HasRHSAttr) {
Douglas Gregor78bd61f2009-06-18 16:11:24 +0000373 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000374 return;
375 }
Mike Stump11289f42009-09-09 15:08:12 +0000376
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000377 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor78bd61f2009-06-18 16:11:24 +0000378 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
379 Context.eraseDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000380 this->HasAttrs = false;
381 RHS->HasAttrs = true;
382}
383
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000384Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000385 Decl::Kind DK = D->getDeclKind();
386 switch(DK) {
Alexis Hunted053252010-05-30 07:21:58 +0000387#define DECL(NAME, BASE)
388#define DECL_CONTEXT(NAME) \
389 case Decl::NAME: \
390 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
391#define DECL_CONTEXT_BASE(NAME)
392#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000393 default:
Alexis Hunted053252010-05-30 07:21:58 +0000394#define DECL(NAME, BASE)
395#define DECL_CONTEXT_BASE(NAME) \
396 if (DK >= first##NAME && DK <= last##NAME) \
397 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
398#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000399 assert(false && "a decl that inherits DeclContext isn't handled");
400 return 0;
401 }
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000402}
403
404DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000405 Decl::Kind DK = D->getKind();
406 switch(DK) {
Alexis Hunted053252010-05-30 07:21:58 +0000407#define DECL(NAME, BASE)
408#define DECL_CONTEXT(NAME) \
409 case Decl::NAME: \
410 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
411#define DECL_CONTEXT_BASE(NAME)
412#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000413 default:
Alexis Hunted053252010-05-30 07:21:58 +0000414#define DECL(NAME, BASE)
415#define DECL_CONTEXT_BASE(NAME) \
416 if (DK >= first##NAME && DK <= last##NAME) \
417 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
418#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000419 assert(false && "a decl that inherits DeclContext isn't handled");
420 return 0;
421 }
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000422}
423
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000424SourceLocation Decl::getBodyRBrace() const {
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +0000425 // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
426 // FunctionDecl stores EndRangeLoc for this purpose.
427 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
428 const FunctionDecl *Definition;
429 if (FD->hasBody(Definition))
430 return Definition->getSourceRange().getEnd();
431 return SourceLocation();
432 }
433
Argyrios Kyrtzidis6fbc8fa2010-07-07 11:31:27 +0000434 if (Stmt *Body = getBody())
435 return Body->getSourceRange().getEnd();
436
437 return SourceLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +0000438}
439
Anders Carlssona28908d2009-03-25 23:38:06 +0000440void Decl::CheckAccessDeclContext() const {
Douglas Gregor4b00d3b2010-12-02 00:22:25 +0000441#ifndef NDEBUG
John McCall401982f2010-01-20 21:53:11 +0000442 // Suppress this check if any of the following hold:
443 // 1. this is the translation unit (and thus has no parent)
444 // 2. this is a template parameter (and thus doesn't belong to its context)
Argyrios Kyrtzidise1778632010-09-08 21:58:42 +0000445 // 3. this is a non-type template parameter
446 // 4. the context is not a record
447 // 5. it's invalid
448 // 6. it's a C++0x static_assert.
Anders Carlssonadf36b22009-08-29 20:47:47 +0000449 if (isa<TranslationUnitDecl>(this) ||
Argyrios Kyrtzidisa45855f2010-07-02 11:55:44 +0000450 isa<TemplateTypeParmDecl>(this) ||
Argyrios Kyrtzidise1778632010-09-08 21:58:42 +0000451 isa<NonTypeTemplateParmDecl>(this) ||
Douglas Gregor2b76dd92010-02-22 17:53:38 +0000452 !isa<CXXRecordDecl>(getDeclContext()) ||
Argyrios Kyrtzidis260b4a82010-09-08 21:32:35 +0000453 isInvalidDecl() ||
454 isa<StaticAssertDecl>(this) ||
455 // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
456 // as DeclContext (?).
Argyrios Kyrtzidise1778632010-09-08 21:58:42 +0000457 isa<ParmVarDecl>(this) ||
458 // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
459 // AS_none as access specifier.
460 isa<CXXRecordDecl>(this))
Anders Carlssonadf36b22009-08-29 20:47:47 +0000461 return;
Mike Stump11289f42009-09-09 15:08:12 +0000462
463 assert(Access != AS_none &&
Anders Carlssona28908d2009-03-25 23:38:06 +0000464 "Access specifier is AS_none inside a record decl");
Douglas Gregor4b00d3b2010-12-02 00:22:25 +0000465#endif
Anders Carlssona28908d2009-03-25 23:38:06 +0000466}
467
John McCallb67608f2011-02-22 22:25:23 +0000468DeclContext *Decl::getNonClosureContext() {
469 DeclContext *DC = getDeclContext();
470
471 // This is basically "while (DC->isClosure()) DC = DC->getParent();"
472 // except that it's significantly more efficient to cast to a known
473 // decl type and call getDeclContext() than to call getParent().
474 do {
475 if (isa<BlockDecl>(DC)) {
476 DC = cast<BlockDecl>(DC)->getDeclContext();
477 continue;
478 }
479 } while (false);
480
481 assert(!DC->isClosure());
482 return DC;
483}
Anders Carlssona28908d2009-03-25 23:38:06 +0000484
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000485//===----------------------------------------------------------------------===//
486// DeclContext Implementation
487//===----------------------------------------------------------------------===//
488
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000489bool DeclContext::classof(const Decl *D) {
490 switch (D->getKind()) {
Alexis Hunted053252010-05-30 07:21:58 +0000491#define DECL(NAME, BASE)
492#define DECL_CONTEXT(NAME) case Decl::NAME:
493#define DECL_CONTEXT_BASE(NAME)
494#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000495 return true;
496 default:
Alexis Hunted053252010-05-30 07:21:58 +0000497#define DECL(NAME, BASE)
498#define DECL_CONTEXT_BASE(NAME) \
499 if (D->getKind() >= Decl::first##NAME && \
500 D->getKind() <= Decl::last##NAME) \
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000501 return true;
Alexis Hunted053252010-05-30 07:21:58 +0000502#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000503 return false;
504 }
505}
506
Douglas Gregor9c832f72010-07-25 18:38:02 +0000507DeclContext::~DeclContext() { }
Douglas Gregor91f84212008-12-11 16:49:14 +0000508
Douglas Gregor7f737c02009-09-10 16:57:35 +0000509/// \brief Find the parent context of this context that will be
510/// used for unqualified name lookup.
511///
512/// Generally, the parent lookup context is the semantic context. However, for
513/// a friend function the parent lookup context is the lexical context, which
514/// is the class in which the friend is declared.
515DeclContext *DeclContext::getLookupParent() {
516 // FIXME: Find a better way to identify friends
517 if (isa<FunctionDecl>(this))
Sebastian Redl50c68252010-08-31 00:36:30 +0000518 if (getParent()->getRedeclContext()->isFileContext() &&
519 getLexicalParent()->getRedeclContext()->isRecord())
Douglas Gregor7f737c02009-09-10 16:57:35 +0000520 return getLexicalParent();
521
522 return getParent();
523}
524
Sebastian Redlbd595762010-08-31 20:53:31 +0000525bool DeclContext::isInlineNamespace() const {
526 return isNamespace() &&
527 cast<NamespaceDecl>(this)->isInline();
528}
529
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000530bool DeclContext::isDependentContext() const {
531 if (isFileContext())
532 return false;
533
Douglas Gregor2373c592009-05-31 09:31:02 +0000534 if (isa<ClassTemplatePartialSpecializationDecl>(this))
535 return true;
536
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000537 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
538 if (Record->getDescribedClassTemplate())
539 return true;
540
John McCallc62bb642010-03-24 05:22:00 +0000541 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000542 if (Function->getDescribedFunctionTemplate())
543 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000544
John McCallc62bb642010-03-24 05:22:00 +0000545 // Friend function declarations are dependent if their *lexical*
546 // context is dependent.
547 if (cast<Decl>(this)->getFriendObjectKind())
548 return getLexicalParent()->isDependentContext();
549 }
550
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000551 return getParent() && getParent()->isDependentContext();
552}
553
Douglas Gregor07665a62009-01-05 19:45:36 +0000554bool DeclContext::isTransparentContext() const {
555 if (DeclKind == Decl::Enum)
Douglas Gregor0bf31402010-10-08 23:50:27 +0000556 return !cast<EnumDecl>(this)->isScoped();
Douglas Gregor07665a62009-01-05 19:45:36 +0000557 else if (DeclKind == Decl::LinkageSpec)
558 return true;
Douglas Gregor07665a62009-01-05 19:45:36 +0000559
560 return false;
561}
562
John McCall5fe84122010-10-26 04:59:26 +0000563bool DeclContext::isExternCContext() const {
564 const DeclContext *DC = this;
565 while (DC->DeclKind != Decl::TranslationUnit) {
566 if (DC->DeclKind == Decl::LinkageSpec)
567 return cast<LinkageSpecDecl>(DC)->getLanguage()
568 == LinkageSpecDecl::lang_c;
569 DC = DC->getParent();
570 }
571 return false;
572}
573
Sebastian Redl50c68252010-08-31 00:36:30 +0000574bool DeclContext::Encloses(const DeclContext *DC) const {
Douglas Gregore985a3b2009-08-27 06:03:53 +0000575 if (getPrimaryContext() != this)
576 return getPrimaryContext()->Encloses(DC);
Mike Stump11289f42009-09-09 15:08:12 +0000577
Douglas Gregore985a3b2009-08-27 06:03:53 +0000578 for (; DC; DC = DC->getParent())
579 if (DC->getPrimaryContext() == this)
580 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000581 return false;
Douglas Gregore985a3b2009-08-27 06:03:53 +0000582}
583
Steve Naroff35c62ae2009-01-08 17:28:14 +0000584DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor91f84212008-12-11 16:49:14 +0000585 switch (DeclKind) {
Douglas Gregor91f84212008-12-11 16:49:14 +0000586 case Decl::TranslationUnit:
Douglas Gregor07665a62009-01-05 19:45:36 +0000587 case Decl::LinkageSpec:
Mike Stump11289f42009-09-09 15:08:12 +0000588 case Decl::Block:
Douglas Gregor91f84212008-12-11 16:49:14 +0000589 // There is only one DeclContext for these entities.
590 return this;
591
592 case Decl::Namespace:
593 // The original namespace is our primary context.
594 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
595
Douglas Gregor91f84212008-12-11 16:49:14 +0000596 case Decl::ObjCMethod:
597 return this;
598
599 case Decl::ObjCInterface:
Steve Naroff35c62ae2009-01-08 17:28:14 +0000600 case Decl::ObjCProtocol:
601 case Decl::ObjCCategory:
Douglas Gregor91f84212008-12-11 16:49:14 +0000602 // FIXME: Can Objective-C interfaces be forward-declared?
603 return this;
604
Steve Naroff35c62ae2009-01-08 17:28:14 +0000605 case Decl::ObjCImplementation:
606 case Decl::ObjCCategoryImpl:
607 return this;
608
Douglas Gregor91f84212008-12-11 16:49:14 +0000609 default:
Alexis Hunted053252010-05-30 07:21:58 +0000610 if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
Douglas Gregor67a65642009-02-17 23:15:12 +0000611 // If this is a tag type that has a definition or is currently
612 // being defined, that definition is our primary context.
John McCalle78aac42010-03-10 03:28:59 +0000613 TagDecl *Tag = cast<TagDecl>(this);
614 assert(isa<TagType>(Tag->TypeForDecl) ||
615 isa<InjectedClassNameType>(Tag->TypeForDecl));
616
617 if (TagDecl *Def = Tag->getDefinition())
618 return Def;
619
620 if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) {
621 const TagType *TagTy = cast<TagType>(Tag->TypeForDecl);
622 if (TagTy->isBeingDefined())
623 // FIXME: is it necessarily being defined in the decl
624 // that owns the type?
625 return TagTy->getDecl();
626 }
627
628 return Tag;
Douglas Gregor67a65642009-02-17 23:15:12 +0000629 }
630
Alexis Hunted053252010-05-30 07:21:58 +0000631 assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
Douglas Gregor91f84212008-12-11 16:49:14 +0000632 "Unknown DeclContext kind");
633 return this;
634 }
635}
636
637DeclContext *DeclContext::getNextContext() {
638 switch (DeclKind) {
Douglas Gregor91f84212008-12-11 16:49:14 +0000639 case Decl::Namespace:
640 // Return the next namespace
641 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
642
643 default:
Douglas Gregor91f84212008-12-11 16:49:14 +0000644 return 0;
645 }
646}
647
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000648std::pair<Decl *, Decl *>
649DeclContext::BuildDeclChain(const llvm::SmallVectorImpl<Decl*> &Decls) {
650 // Build up a chain of declarations via the Decl::NextDeclInContext field.
651 Decl *FirstNewDecl = 0;
652 Decl *PrevDecl = 0;
653 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
654 Decl *D = Decls[I];
655 if (PrevDecl)
656 PrevDecl->NextDeclInContext = D;
657 else
658 FirstNewDecl = D;
659
660 PrevDecl = D;
661 }
662
663 return std::make_pair(FirstNewDecl, PrevDecl);
664}
665
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000666/// \brief Load the declarations within this lexical storage from an
667/// external source.
Mike Stump11289f42009-09-09 15:08:12 +0000668void
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000669DeclContext::LoadLexicalDeclsFromExternalStorage() const {
670 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000671 assert(hasExternalLexicalStorage() && Source && "No external storage?");
672
Argyrios Kyrtzidis98d045e2010-07-30 10:03:23 +0000673 // Notify that we have a DeclContext that is initializing.
674 ExternalASTSource::Deserializing ADeclContext(Source);
675
John McCall75b960e2010-06-01 09:23:16 +0000676 llvm::SmallVector<Decl*, 64> Decls;
677 if (Source->FindExternalLexicalDecls(this, Decls))
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000678 return;
679
680 // There is no longer any lexical storage in this context
681 ExternalLexicalStorage = false;
682
683 if (Decls.empty())
684 return;
685
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000686 // We may have already loaded just the fields of this record, in which case
687 // don't add the decls, just replace the FirstDecl/LastDecl chain.
688 if (const RecordDecl *RD = dyn_cast<RecordDecl>(this))
689 if (RD->LoadedFieldsFromExternalStorage) {
690 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
691 return;
692 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000693
694 // Splice the newly-read declarations into the beginning of the list
695 // of declarations.
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000696 Decl *ExternalFirst, *ExternalLast;
697 llvm::tie(ExternalFirst, ExternalLast) = BuildDeclChain(Decls);
698 ExternalLast->NextDeclInContext = FirstDecl;
699 FirstDecl = ExternalFirst;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000700 if (!LastDecl)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000701 LastDecl = ExternalLast;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000702}
703
John McCall75b960e2010-06-01 09:23:16 +0000704DeclContext::lookup_result
705ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
706 DeclarationName Name) {
707 ASTContext &Context = DC->getParentASTContext();
708 StoredDeclsMap *Map;
709 if (!(Map = DC->LookupPtr))
710 Map = DC->CreateStoredDeclsMap(Context);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000711
John McCall75b960e2010-06-01 09:23:16 +0000712 StoredDeclsList &List = (*Map)[Name];
713 assert(List.isNull());
714 (void) List;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000715
John McCall75b960e2010-06-01 09:23:16 +0000716 return DeclContext::lookup_result();
717}
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000718
John McCall75b960e2010-06-01 09:23:16 +0000719DeclContext::lookup_result
720ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +0000721 DeclarationName Name,
722 llvm::SmallVectorImpl<NamedDecl*> &Decls) {
723 ASTContext &Context = DC->getParentASTContext();;
724
725 StoredDeclsMap *Map;
726 if (!(Map = DC->LookupPtr))
727 Map = DC->CreateStoredDeclsMap(Context);
728
729 StoredDeclsList &List = (*Map)[Name];
730 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
731 if (List.isNull())
732 List.setOnlyValue(Decls[I]);
733 else
734 List.AddSubsequentDecl(Decls[I]);
735 }
736
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000737 return List.getLookupResult();
John McCall75b960e2010-06-01 09:23:16 +0000738}
739
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +0000740void ExternalASTSource::MaterializeVisibleDeclsForName(const DeclContext *DC,
741 DeclarationName Name,
742 llvm::SmallVectorImpl<NamedDecl*> &Decls) {
743 assert(DC->LookupPtr);
744 StoredDeclsMap &Map = *DC->LookupPtr;
745
746 // If there's an entry in the table the visible decls for this name have
747 // already been deserialized.
748 if (Map.find(Name) == Map.end()) {
749 StoredDeclsList &List = Map[Name];
750 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
751 if (List.isNull())
752 List.setOnlyValue(Decls[I]);
753 else
754 List.AddSubsequentDecl(Decls[I]);
755 }
756 }
757}
758
Sebastian Redl66c5eef2010-07-27 00:17:23 +0000759DeclContext::decl_iterator DeclContext::noload_decls_begin() const {
760 return decl_iterator(FirstDecl);
761}
762
763DeclContext::decl_iterator DeclContext::noload_decls_end() const {
764 return decl_iterator();
765}
766
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000767DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000768 if (hasExternalLexicalStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000769 LoadLexicalDeclsFromExternalStorage();
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000770
771 // FIXME: Check whether we need to load some declarations from
772 // external storage.
Mike Stump11289f42009-09-09 15:08:12 +0000773 return decl_iterator(FirstDecl);
Douglas Gregorbcced4e2009-04-09 21:40:53 +0000774}
775
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000776DeclContext::decl_iterator DeclContext::decls_end() const {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000777 if (hasExternalLexicalStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000778 LoadLexicalDeclsFromExternalStorage();
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000779
Mike Stump11289f42009-09-09 15:08:12 +0000780 return decl_iterator();
Douglas Gregorbcced4e2009-04-09 21:40:53 +0000781}
782
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000783bool DeclContext::decls_empty() const {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +0000784 if (hasExternalLexicalStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000785 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +0000786
787 return !FirstDecl;
788}
789
John McCall84d87672009-12-10 09:41:52 +0000790void DeclContext::removeDecl(Decl *D) {
791 assert(D->getLexicalDeclContext() == this &&
792 "decl being removed from non-lexical context");
793 assert((D->NextDeclInContext || D == LastDecl) &&
794 "decl is not in decls list");
795
796 // Remove D from the decl chain. This is O(n) but hopefully rare.
797 if (D == FirstDecl) {
798 if (D == LastDecl)
799 FirstDecl = LastDecl = 0;
800 else
801 FirstDecl = D->NextDeclInContext;
802 } else {
803 for (Decl *I = FirstDecl; true; I = I->NextDeclInContext) {
804 assert(I && "decl not found in linked list");
805 if (I->NextDeclInContext == D) {
806 I->NextDeclInContext = D->NextDeclInContext;
807 if (D == LastDecl) LastDecl = I;
808 break;
809 }
810 }
811 }
812
813 // Mark that D is no longer in the decl chain.
814 D->NextDeclInContext = 0;
815
816 // Remove D from the lookup table if necessary.
817 if (isa<NamedDecl>(D)) {
818 NamedDecl *ND = cast<NamedDecl>(D);
819
John McCallc62bb642010-03-24 05:22:00 +0000820 StoredDeclsMap *Map = getPrimaryContext()->LookupPtr;
821 if (!Map) return;
John McCall84d87672009-12-10 09:41:52 +0000822
John McCall84d87672009-12-10 09:41:52 +0000823 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
824 assert(Pos != Map->end() && "no lookup entry for decl");
825 Pos->second.remove(ND);
826 }
827}
828
John McCalld1e9d832009-08-11 06:59:38 +0000829void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner33f219d2009-02-20 00:56:18 +0000830 assert(D->getLexicalDeclContext() == this &&
831 "Decl inserted into wrong lexical context");
Mike Stump11289f42009-09-09 15:08:12 +0000832 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor020713e2009-01-09 19:42:16 +0000833 "Decl already inserted into a DeclContext");
834
835 if (FirstDecl) {
Chris Lattnerfcd33a62009-03-28 06:04:26 +0000836 LastDecl->NextDeclInContext = D;
Douglas Gregor020713e2009-01-09 19:42:16 +0000837 LastDecl = D;
838 } else {
839 FirstDecl = LastDecl = D;
840 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000841
842 // Notify a C++ record declaration that we've added a member, so it can
843 // update it's class-specific state.
844 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
845 Record->addedMember(D);
John McCalld1e9d832009-08-11 06:59:38 +0000846}
847
848void DeclContext::addDecl(Decl *D) {
849 addHiddenDecl(D);
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000850
851 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000852 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor91f84212008-12-11 16:49:14 +0000853}
854
Douglas Gregor07665a62009-01-05 19:45:36 +0000855/// buildLookup - Build the lookup data structure with all of the
856/// declarations in DCtx (and any other contexts linked to it or
857/// transparent contexts nested within it).
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000858void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregor07665a62009-01-05 19:45:36 +0000859 for (; DCtx; DCtx = DCtx->getNextContext()) {
Mike Stump11289f42009-09-09 15:08:12 +0000860 for (decl_iterator D = DCtx->decls_begin(),
861 DEnd = DCtx->decls_end();
Douglas Gregord05cb412009-01-06 07:17:58 +0000862 D != DEnd; ++D) {
John McCalld1e9d832009-08-11 06:59:38 +0000863 // Insert this declaration into the lookup structure, but only
864 // if it's semantically in its decl context. During non-lazy
865 // lookup building, this is implicitly enforced by addDecl.
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000866 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
John McCalld1e9d832009-08-11 06:59:38 +0000867 if (D->getDeclContext() == DCtx)
868 makeDeclVisibleInContextImpl(ND);
Douglas Gregor07665a62009-01-05 19:45:36 +0000869
Ted Kremenek707ece62009-11-17 22:58:30 +0000870 // Insert any forward-declared Objective-C interfaces into the lookup
871 // data structure.
872 if (ObjCClassDecl *Class = dyn_cast<ObjCClassDecl>(*D))
873 for (ObjCClassDecl::iterator I = Class->begin(), IEnd = Class->end();
874 I != IEnd; ++I)
Ted Kremenek9b124e12009-11-18 00:28:11 +0000875 makeDeclVisibleInContextImpl(I->getInterface());
Ted Kremenek707ece62009-11-17 22:58:30 +0000876
Sebastian Redlbd595762010-08-31 20:53:31 +0000877 // If this declaration is itself a transparent declaration context or
878 // inline namespace, add its members (recursively).
Douglas Gregor07665a62009-01-05 19:45:36 +0000879 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
Sebastian Redlbd595762010-08-31 20:53:31 +0000880 if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000881 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregor07665a62009-01-05 19:45:36 +0000882 }
883 }
884}
885
Mike Stump11289f42009-09-09 15:08:12 +0000886DeclContext::lookup_result
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000887DeclContext::lookup(DeclarationName Name) {
Steve Naroff35c62ae2009-01-08 17:28:14 +0000888 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor91f84212008-12-11 16:49:14 +0000889 if (PrimaryContext != this)
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000890 return PrimaryContext->lookup(Name);
Douglas Gregor91f84212008-12-11 16:49:14 +0000891
John McCall75b960e2010-06-01 09:23:16 +0000892 if (hasExternalVisibleStorage()) {
893 // Check to see if we've already cached the lookup results.
894 if (LookupPtr) {
895 StoredDeclsMap::iterator I = LookupPtr->find(Name);
896 if (I != LookupPtr->end())
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000897 return I->second.getLookupResult();
John McCall75b960e2010-06-01 09:23:16 +0000898 }
899
900 ExternalASTSource *Source = getParentASTContext().getExternalSource();
901 return Source->FindExternalVisibleDeclsByName(this, Name);
902 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000903
Douglas Gregor55297ac2008-12-23 00:26:44 +0000904 /// If there is no lookup data structure, build one now by walking
Douglas Gregor91f84212008-12-11 16:49:14 +0000905 /// all of the linked DeclContexts (in declaration order!) and
906 /// inserting their values.
Douglas Gregor9615ec22009-04-09 17:29:08 +0000907 if (!LookupPtr) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000908 buildLookup(this);
Douglas Gregor91f84212008-12-11 16:49:14 +0000909
Douglas Gregor9615ec22009-04-09 17:29:08 +0000910 if (!LookupPtr)
Douglas Gregor10dc8aa2010-05-11 06:18:17 +0000911 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Douglas Gregor9615ec22009-04-09 17:29:08 +0000912 }
Douglas Gregor91f84212008-12-11 16:49:14 +0000913
John McCallc62bb642010-03-24 05:22:00 +0000914 StoredDeclsMap::iterator Pos = LookupPtr->find(Name);
915 if (Pos == LookupPtr->end())
Douglas Gregor10dc8aa2010-05-11 06:18:17 +0000916 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000917 return Pos->second.getLookupResult();
Douglas Gregor91f84212008-12-11 16:49:14 +0000918}
919
Mike Stump11289f42009-09-09 15:08:12 +0000920DeclContext::lookup_const_result
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000921DeclContext::lookup(DeclarationName Name) const {
922 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor91f84212008-12-11 16:49:14 +0000923}
924
Sebastian Redl50c68252010-08-31 00:36:30 +0000925DeclContext *DeclContext::getRedeclContext() {
Chris Lattner17a1bfa2009-03-27 19:19:59 +0000926 DeclContext *Ctx = this;
Sebastian Redlbd595762010-08-31 20:53:31 +0000927 // Skip through transparent contexts.
928 while (Ctx->isTransparentContext())
Douglas Gregor6ad0ef52009-01-06 23:51:29 +0000929 Ctx = Ctx->getParent();
930 return Ctx;
931}
932
Douglas Gregorf47b9112009-02-25 22:02:03 +0000933DeclContext *DeclContext::getEnclosingNamespaceContext() {
934 DeclContext *Ctx = this;
935 // Skip through non-namespace, non-translation-unit contexts.
Sebastian Redl4f08c962010-08-31 00:36:23 +0000936 while (!Ctx->isFileContext())
Douglas Gregorf47b9112009-02-25 22:02:03 +0000937 Ctx = Ctx->getParent();
938 return Ctx->getPrimaryContext();
939}
940
Sebastian Redl50c68252010-08-31 00:36:30 +0000941bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
942 // For non-file contexts, this is equivalent to Equals.
943 if (!isFileContext())
944 return O->Equals(this);
945
946 do {
947 if (O->Equals(this))
948 return true;
949
950 const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O);
951 if (!NS || !NS->isInline())
952 break;
953 O = NS->getParent();
954 } while (O);
955
956 return false;
957}
958
John McCall759e32b2009-08-31 22:39:49 +0000959void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable) {
Douglas Gregor67a65642009-02-17 23:15:12 +0000960 // FIXME: This feels like a hack. Should DeclarationName support
961 // template-ids, or is there a better way to keep specializations
962 // from being visible?
Douglas Gregorfd7c2252011-03-04 17:52:15 +0000963 if (isa<ClassTemplateSpecializationDecl>(D) || D->isTemplateParameter())
Douglas Gregor67a65642009-02-17 23:15:12 +0000964 return;
Eli Friedman73168192009-12-08 05:40:03 +0000965 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
966 if (FD->isFunctionTemplateSpecialization())
967 return;
Douglas Gregor67a65642009-02-17 23:15:12 +0000968
Steve Naroff35c62ae2009-01-08 17:28:14 +0000969 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor91f84212008-12-11 16:49:14 +0000970 if (PrimaryContext != this) {
John McCall759e32b2009-08-31 22:39:49 +0000971 PrimaryContext->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor91f84212008-12-11 16:49:14 +0000972 return;
973 }
974
975 // If we already have a lookup data structure, perform the insertion
Argyrios Kyrtzidise51e5542010-07-04 21:44:25 +0000976 // into it. If we haven't deserialized externally stored decls, deserialize
977 // them so we can add the decl. Otherwise, be lazy and don't build that
978 // structure until someone asks for it.
979 if (LookupPtr || !Recoverable || hasExternalVisibleStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000980 makeDeclVisibleInContextImpl(D);
Douglas Gregor07665a62009-01-05 19:45:36 +0000981
Sebastian Redlbd595762010-08-31 20:53:31 +0000982 // If we are a transparent context or inline namespace, insert into our
983 // parent context, too. This operation is recursive.
984 if (isTransparentContext() || isInlineNamespace())
John McCall759e32b2009-08-31 22:39:49 +0000985 getParent()->makeDeclVisibleInContext(D, Recoverable);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +0000986
987 Decl *DCAsDecl = cast<Decl>(this);
988 // Notify that a decl was made visible unless it's a Tag being defined.
989 if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined()))
990 if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
991 L->AddedVisibleDecl(this, D);
Douglas Gregor91f84212008-12-11 16:49:14 +0000992}
993
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000994void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) {
Douglas Gregor07665a62009-01-05 19:45:36 +0000995 // Skip unnamed declarations.
996 if (!D->getDeclName())
997 return;
998
Douglas Gregor67a65642009-02-17 23:15:12 +0000999 // FIXME: This feels like a hack. Should DeclarationName support
1000 // template-ids, or is there a better way to keep specializations
1001 // from being visible?
Douglas Gregorfd7c2252011-03-04 17:52:15 +00001002 if (isa<ClassTemplateSpecializationDecl>(D) || D->isTemplateParameter())
Douglas Gregor67a65642009-02-17 23:15:12 +00001003 return;
1004
Argyrios Kyrtzidise51e5542010-07-04 21:44:25 +00001005 ASTContext *C = 0;
1006 if (!LookupPtr) {
1007 C = &getParentASTContext();
1008 CreateStoredDeclsMap(*C);
1009 }
1010
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001011 // If there is an external AST source, load any declarations it knows about
1012 // with this declaration's name.
1013 // If the lookup table contains an entry about this name it means that we
1014 // have already checked the external source.
1015 if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
1016 if (hasExternalVisibleStorage() &&
1017 LookupPtr->find(D->getDeclName()) == LookupPtr->end())
1018 Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
1019
Douglas Gregor91f84212008-12-11 16:49:14 +00001020 // Insert this declaration into the map.
John McCallc62bb642010-03-24 05:22:00 +00001021 StoredDeclsList &DeclNameEntries = (*LookupPtr)[D->getDeclName()];
Chris Lattnercaae7162009-02-20 01:44:05 +00001022 if (DeclNameEntries.isNull()) {
1023 DeclNameEntries.setOnlyValue(D);
Chris Lattnerdfd6b3d2009-02-19 07:00:44 +00001024 return;
Douglas Gregor91f84212008-12-11 16:49:14 +00001025 }
Chris Lattner24e24d52009-02-20 00:55:03 +00001026
Chris Lattner29578f32009-02-20 01:10:07 +00001027 // If it is possible that this is a redeclaration, check to see if there is
1028 // already a decl for which declarationReplaces returns true. If there is
1029 // one, just replace it and return.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001030 if (DeclNameEntries.HandleRedeclaration(D))
Chris Lattnercaae7162009-02-20 01:44:05 +00001031 return;
Mike Stump11289f42009-09-09 15:08:12 +00001032
Chris Lattnerdfd6b3d2009-02-19 07:00:44 +00001033 // Put this declaration into the appropriate slot.
Chris Lattnercaae7162009-02-20 01:44:05 +00001034 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor91f84212008-12-11 16:49:14 +00001035}
Douglas Gregor889ceb72009-02-03 19:21:40 +00001036
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00001037void DeclContext::MaterializeVisibleDeclsFromExternalStorage() {
1038 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1039 assert(hasExternalVisibleStorage() && Source && "No external storage?");
1040
1041 if (!LookupPtr)
1042 CreateStoredDeclsMap(getParentASTContext());
1043 Source->MaterializeVisibleDecls(this);
1044}
1045
Douglas Gregor889ceb72009-02-03 19:21:40 +00001046/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
1047/// this context.
Mike Stump11289f42009-09-09 15:08:12 +00001048DeclContext::udir_iterator_range
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001049DeclContext::getUsingDirectives() const {
1050 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor889ceb72009-02-03 19:21:40 +00001051 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
1052 reinterpret_cast<udir_iterator>(Result.second));
1053}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001054
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001055//===----------------------------------------------------------------------===//
1056// Creation and Destruction of StoredDeclsMaps. //
1057//===----------------------------------------------------------------------===//
1058
John McCallc62bb642010-03-24 05:22:00 +00001059StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
1060 assert(!LookupPtr && "context already has a decls map");
1061 assert(getPrimaryContext() == this &&
1062 "creating decls map on non-primary context");
1063
1064 StoredDeclsMap *M;
1065 bool Dependent = isDependentContext();
1066 if (Dependent)
1067 M = new DependentStoredDeclsMap();
1068 else
1069 M = new StoredDeclsMap();
1070 M->Previous = C.LastSDM;
1071 C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
1072 LookupPtr = M;
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001073 return M;
1074}
1075
1076void ASTContext::ReleaseDeclContextMaps() {
John McCallc62bb642010-03-24 05:22:00 +00001077 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1078 // pointer because the subclass doesn't add anything that needs to
1079 // be deleted.
John McCallc62bb642010-03-24 05:22:00 +00001080 StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1081}
1082
1083void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1084 while (Map) {
1085 // Advance the iteration before we invalidate memory.
1086 llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1087
1088 if (Dependent)
1089 delete static_cast<DependentStoredDeclsMap*>(Map);
1090 else
1091 delete Map;
1092
1093 Map = Next.getPointer();
1094 Dependent = Next.getInt();
1095 }
1096}
1097
John McCallc62bb642010-03-24 05:22:00 +00001098DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1099 DeclContext *Parent,
1100 const PartialDiagnostic &PDiag) {
1101 assert(Parent->isDependentContext()
1102 && "cannot iterate dependent diagnostics of non-dependent context");
1103 Parent = Parent->getPrimaryContext();
1104 if (!Parent->LookupPtr)
1105 Parent->CreateStoredDeclsMap(C);
1106
1107 DependentStoredDeclsMap *Map
1108 = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr);
1109
Douglas Gregora55530e2010-03-29 23:56:53 +00001110 // Allocate the copy of the PartialDiagnostic via the ASTContext's
Douglas Gregor89336232010-03-29 23:34:08 +00001111 // BumpPtrAllocator, rather than the ASTContext itself.
Douglas Gregora55530e2010-03-29 23:56:53 +00001112 PartialDiagnostic::Storage *DiagStorage = 0;
1113 if (PDiag.hasStorage())
1114 DiagStorage = new (C) PartialDiagnostic::Storage;
1115
1116 DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
John McCallc62bb642010-03-24 05:22:00 +00001117
1118 // TODO: Maybe we shouldn't reverse the order during insertion.
1119 DD->NextDiagnostic = Map->FirstDiagnostic;
1120 Map->FirstDiagnostic = DD;
1121
1122 return DD;
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001123}