blob: 110de64d8cdb047ad5daba2bb45294b033394c6c [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
146
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
Chris Lattner8e097192009-03-27 20:18:19 +0000172// Out-of-line virtual method providing a home for Decl.
Douglas Gregorb412e172010-07-25 18:17:45 +0000173Decl::~Decl() { }
Chris Lattner8e097192009-03-27 20:18:19 +0000174
Douglas Gregora43942a2011-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 Gregor6e6ad602009-01-20 01:17:11 +0000184void Decl::setDeclContext(DeclContext *DC) {
185 if (isOutOfSemaDC())
186 delete getMultipleDC();
Mike Stump11289f42009-09-09 15:08:12 +0000187
Chris Lattnerb81eb052009-03-29 06:06:59 +0000188 DeclCtx = DC;
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000189}
190
191void Decl::setLexicalDeclContext(DeclContext *DC) {
192 if (DC == getLexicalDeclContext())
193 return;
194
195 if (isInSemaDC()) {
Ted Kremenekf8c12a32009-12-01 00:07:10 +0000196 MultipleDC *MDC = new (getASTContext()) MultipleDC();
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000197 MDC->SemanticDC = getDeclContext();
198 MDC->LexicalDC = DC;
Chris Lattnerb81eb052009-03-29 06:06:59 +0000199 DeclCtx = MDC;
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000200 } else {
201 getMultipleDC()->LexicalDC = DC;
202 }
203}
204
John McCall4fa53422009-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 Kyrtzidis743e7db2009-06-29 17:38:40 +0000216TranslationUnitDecl *Decl::getTranslationUnitDecl() {
Argyrios Kyrtzidis4e1a72b2009-06-30 02:34:53 +0000217 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
218 return TUD;
219
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000220 DeclContext *DC = getDeclContext();
221 assert(DC && "This decl is not contained in a translation unit!");
Mike Stump11289f42009-09-09 15:08:12 +0000222
Argyrios Kyrtzidis743e7db2009-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 Stump11289f42009-09-09 15:08:12 +0000227
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000228 return cast<TranslationUnitDecl>(DC);
229}
230
231ASTContext &Decl::getASTContext() const {
Mike Stump11289f42009-09-09 15:08:12 +0000232 return getTranslationUnitDecl()->getASTContext();
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000233}
234
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +0000235ASTMutationListener *Decl::getASTMutationListener() const {
236 return getASTContext().getASTMutationListener();
237}
238
Douglas Gregorebada0772010-06-17 23:14:26 +0000239bool Decl::isUsed(bool CheckUsedAttr) const {
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000240 if (Used)
241 return true;
242
243 // Check for used attribute.
Douglas Gregorebada0772010-06-17 23:14:26 +0000244 if (CheckUsedAttr && hasAttr<UsedAttr>())
Tanya Lattner8aefcbe2010-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 Gregorebada0772010-06-17 23:14:26 +0000249 if ((CheckUsedAttr && I->hasAttr<UsedAttr>()) || I->Used)
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000250 return true;
251 }
252
253 return false;
254}
255
256
Chris Lattner8e097192009-03-27 20:18:19 +0000257unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
258 switch (DeclKind) {
John McCall3f746822009-11-17 05:59:44 +0000259 case Function:
260 case CXXMethod:
261 case CXXConstructor:
262 case CXXDestructor:
263 case CXXConversion:
Chris Lattner8e097192009-03-27 20:18:19 +0000264 case EnumConstant:
265 case Var:
266 case ImplicitParam:
267 case ParmVar:
Chris Lattner8e097192009-03-27 20:18:19 +0000268 case NonTypeTemplateParm:
269 case ObjCMethod:
Daniel Dunbar45b2d8a2010-04-23 13:07:39 +0000270 case ObjCProperty:
Daniel Dunbar45b2d8a2010-04-23 13:07:39 +0000271 return IDNS_Ordinary;
John McCalla2456712010-04-23 02:41:41 +0000272
Francois Pichet783dd6e2010-11-21 06:08:52 +0000273 case IndirectField:
274 return IDNS_Ordinary | IDNS_Member;
275
John McCalle87beb22010-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 McCall3f746822009-11-17 05:59:44 +0000285 case UsingShadow:
286 return 0; // we'll actually overwrite this later
287
John McCalle61f2ba2009-11-18 02:36:19 +0000288 case UnresolvedUsingValue:
John McCalle61f2ba2009-11-18 02:36:19 +0000289 return IDNS_Ordinary | IDNS_Using;
John McCall3f746822009-11-17 05:59:44 +0000290
291 case Using:
292 return IDNS_Using;
293
Chris Lattner8e097192009-03-27 20:18:19 +0000294 case ObjCProtocol:
Douglas Gregor79947a22009-04-24 00:11:27 +0000295 return IDNS_ObjCProtocol;
Mike Stump11289f42009-09-09 15:08:12 +0000296
Chris Lattner8e097192009-03-27 20:18:19 +0000297 case Field:
298 case ObjCAtDefsField:
299 case ObjCIvar:
300 return IDNS_Member;
Mike Stump11289f42009-09-09 15:08:12 +0000301
Chris Lattner8e097192009-03-27 20:18:19 +0000302 case Record:
303 case CXXRecord:
304 case Enum:
John McCalle87beb22010-04-23 18:46:30 +0000305 return IDNS_Tag | IDNS_Type;
Mike Stump11289f42009-09-09 15:08:12 +0000306
Chris Lattner8e097192009-03-27 20:18:19 +0000307 case Namespace:
John McCalle87beb22010-04-23 18:46:30 +0000308 case NamespaceAlias:
309 return IDNS_Namespace;
310
Chris Lattner8e097192009-03-27 20:18:19 +0000311 case FunctionTemplate:
John McCalle87beb22010-04-23 18:46:30 +0000312 return IDNS_Ordinary;
313
Chris Lattner8e097192009-03-27 20:18:19 +0000314 case ClassTemplate:
315 case TemplateTemplateParm:
John McCalle87beb22010-04-23 18:46:30 +0000316 return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
Mike Stump11289f42009-09-09 15:08:12 +0000317
Chris Lattner8e097192009-03-27 20:18:19 +0000318 // Never have names.
John McCallaa74a0c2009-08-28 07:59:38 +0000319 case Friend:
John McCall11083da2009-09-16 22:47:08 +0000320 case FriendTemplate:
Abramo Bagnarad7340582010-06-05 05:09:32 +0000321 case AccessSpec:
Chris Lattner8e097192009-03-27 20:18:19 +0000322 case LinkageSpec:
323 case FileScopeAsm:
324 case StaticAssert:
325 case ObjCClass:
Chris Lattner8e097192009-03-27 20:18:19 +0000326 case ObjCPropertyImpl:
327 case ObjCForwardProtocol:
328 case Block:
329 case TranslationUnit:
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000330
Chris Lattner8e097192009-03-27 20:18:19 +0000331 case UsingDirective:
332 case ClassTemplateSpecialization:
Douglas Gregor2373c592009-05-31 09:31:02 +0000333 case ClassTemplatePartialSpecialization:
Douglas Gregore93525e2010-04-22 23:19:50 +0000334 case ObjCImplementation:
335 case ObjCCategory:
336 case ObjCCategoryImpl:
337 // Never looked up by name.
Chris Lattner8e097192009-03-27 20:18:19 +0000338 return 0;
339 }
John McCall3f746822009-11-17 05:59:44 +0000340
341 return 0;
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000342}
343
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000344void Decl::setAttrs(const AttrVec &attrs) {
Argyrios Kyrtzidis91167172010-06-11 23:09:25 +0000345 assert(!HasAttrs && "Decl already contains attrs.");
346
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000347 AttrVec &AttrBlank = getASTContext().getDeclAttrs(this);
348 assert(AttrBlank.empty() && "HasAttrs was wrong?");
Argyrios Kyrtzidis91167172010-06-11 23:09:25 +0000349
350 AttrBlank = attrs;
351 HasAttrs = true;
352}
353
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000354void Decl::dropAttrs() {
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000355 if (!HasAttrs) return;
Mike Stump11289f42009-09-09 15:08:12 +0000356
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000357 HasAttrs = false;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000358 getASTContext().eraseDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000359}
360
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000361const AttrVec &Decl::getAttrs() const {
362 assert(HasAttrs && "No attrs to get!");
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000363 return getASTContext().getDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000364}
365
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000366void Decl::swapAttrs(Decl *RHS) {
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000367 bool HasLHSAttr = this->HasAttrs;
368 bool HasRHSAttr = RHS->HasAttrs;
Mike Stump11289f42009-09-09 15:08:12 +0000369
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000370 // Usually, neither decl has attrs, nothing to do.
371 if (!HasLHSAttr && !HasRHSAttr) return;
Mike Stump11289f42009-09-09 15:08:12 +0000372
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000373 // If 'this' has no attrs, swap the other way.
374 if (!HasLHSAttr)
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000375 return RHS->swapAttrs(this);
Mike Stump11289f42009-09-09 15:08:12 +0000376
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000377 ASTContext &Context = getASTContext();
Mike Stump11289f42009-09-09 15:08:12 +0000378
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000379 // Handle the case when both decls have attrs.
380 if (HasRHSAttr) {
Douglas Gregor78bd61f2009-06-18 16:11:24 +0000381 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000382 return;
383 }
Mike Stump11289f42009-09-09 15:08:12 +0000384
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000385 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor78bd61f2009-06-18 16:11:24 +0000386 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
387 Context.eraseDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000388 this->HasAttrs = false;
389 RHS->HasAttrs = true;
390}
391
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000392Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000393 Decl::Kind DK = D->getDeclKind();
394 switch(DK) {
Alexis Hunted053252010-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 Kyrtzidisafe24c82009-02-16 14:29:28 +0000401 default:
Alexis Hunted053252010-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 Kyrtzidisafe24c82009-02-16 14:29:28 +0000407 assert(false && "a decl that inherits DeclContext isn't handled");
408 return 0;
409 }
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000410}
411
412DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000413 Decl::Kind DK = D->getKind();
414 switch(DK) {
Alexis Hunted053252010-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 Kyrtzidisafe24c82009-02-16 14:29:28 +0000421 default:
Alexis Hunted053252010-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 Kyrtzidisafe24c82009-02-16 14:29:28 +0000427 assert(false && "a decl that inherits DeclContext isn't handled");
428 return 0;
429 }
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000430}
431
Douglas Gregorf7b2c932011-02-17 07:13:24 +0000432Stmt *Decl::getBody() const {
433 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
434 return FD->getBody();
435 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(this))
436 return MD->getBody();
437 if (const BlockDecl *BD = dyn_cast<BlockDecl>(this))
438 return BD->getBody();
439
440 return 0;
441}
442
443bool Decl::hasBody() const {
444 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
445 return FD->hasBody();
446
447 return getBody() != 0;
448}
449
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000450SourceLocation Decl::getBodyRBrace() const {
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +0000451 // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
452 // FunctionDecl stores EndRangeLoc for this purpose.
453 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
454 const FunctionDecl *Definition;
455 if (FD->hasBody(Definition))
456 return Definition->getSourceRange().getEnd();
457 return SourceLocation();
458 }
459
Argyrios Kyrtzidis6fbc8fa2010-07-07 11:31:27 +0000460 if (Stmt *Body = getBody())
461 return Body->getSourceRange().getEnd();
462
463 return SourceLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +0000464}
465
Anders Carlssona28908d2009-03-25 23:38:06 +0000466void Decl::CheckAccessDeclContext() const {
Douglas Gregor4b00d3b2010-12-02 00:22:25 +0000467#ifndef NDEBUG
John McCall401982f2010-01-20 21:53:11 +0000468 // Suppress this check if any of the following hold:
469 // 1. this is the translation unit (and thus has no parent)
470 // 2. this is a template parameter (and thus doesn't belong to its context)
Argyrios Kyrtzidise1778632010-09-08 21:58:42 +0000471 // 3. this is a non-type template parameter
472 // 4. the context is not a record
473 // 5. it's invalid
474 // 6. it's a C++0x static_assert.
Anders Carlssonadf36b22009-08-29 20:47:47 +0000475 if (isa<TranslationUnitDecl>(this) ||
Argyrios Kyrtzidisa45855f2010-07-02 11:55:44 +0000476 isa<TemplateTypeParmDecl>(this) ||
Argyrios Kyrtzidise1778632010-09-08 21:58:42 +0000477 isa<NonTypeTemplateParmDecl>(this) ||
Douglas Gregor2b76dd92010-02-22 17:53:38 +0000478 !isa<CXXRecordDecl>(getDeclContext()) ||
Argyrios Kyrtzidis260b4a82010-09-08 21:32:35 +0000479 isInvalidDecl() ||
480 isa<StaticAssertDecl>(this) ||
481 // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
482 // as DeclContext (?).
Argyrios Kyrtzidise1778632010-09-08 21:58:42 +0000483 isa<ParmVarDecl>(this) ||
484 // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
485 // AS_none as access specifier.
486 isa<CXXRecordDecl>(this))
Anders Carlssonadf36b22009-08-29 20:47:47 +0000487 return;
Mike Stump11289f42009-09-09 15:08:12 +0000488
489 assert(Access != AS_none &&
Anders Carlssona28908d2009-03-25 23:38:06 +0000490 "Access specifier is AS_none inside a record decl");
Douglas Gregor4b00d3b2010-12-02 00:22:25 +0000491#endif
Anders Carlssona28908d2009-03-25 23:38:06 +0000492}
493
Anders Carlssona28908d2009-03-25 23:38:06 +0000494
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000495//===----------------------------------------------------------------------===//
496// DeclContext Implementation
497//===----------------------------------------------------------------------===//
498
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000499bool DeclContext::classof(const Decl *D) {
500 switch (D->getKind()) {
Alexis Hunted053252010-05-30 07:21:58 +0000501#define DECL(NAME, BASE)
502#define DECL_CONTEXT(NAME) case Decl::NAME:
503#define DECL_CONTEXT_BASE(NAME)
504#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000505 return true;
506 default:
Alexis Hunted053252010-05-30 07:21:58 +0000507#define DECL(NAME, BASE)
508#define DECL_CONTEXT_BASE(NAME) \
509 if (D->getKind() >= Decl::first##NAME && \
510 D->getKind() <= Decl::last##NAME) \
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000511 return true;
Alexis Hunted053252010-05-30 07:21:58 +0000512#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000513 return false;
514 }
515}
516
Douglas Gregor9c832f72010-07-25 18:38:02 +0000517DeclContext::~DeclContext() { }
Douglas Gregor91f84212008-12-11 16:49:14 +0000518
Douglas Gregor7f737c02009-09-10 16:57:35 +0000519/// \brief Find the parent context of this context that will be
520/// used for unqualified name lookup.
521///
522/// Generally, the parent lookup context is the semantic context. However, for
523/// a friend function the parent lookup context is the lexical context, which
524/// is the class in which the friend is declared.
525DeclContext *DeclContext::getLookupParent() {
526 // FIXME: Find a better way to identify friends
527 if (isa<FunctionDecl>(this))
Sebastian Redl50c68252010-08-31 00:36:30 +0000528 if (getParent()->getRedeclContext()->isFileContext() &&
529 getLexicalParent()->getRedeclContext()->isRecord())
Douglas Gregor7f737c02009-09-10 16:57:35 +0000530 return getLexicalParent();
531
532 return getParent();
533}
534
Sebastian Redlbd595762010-08-31 20:53:31 +0000535bool DeclContext::isInlineNamespace() const {
536 return isNamespace() &&
537 cast<NamespaceDecl>(this)->isInline();
538}
539
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000540bool DeclContext::isDependentContext() const {
541 if (isFileContext())
542 return false;
543
Douglas Gregor2373c592009-05-31 09:31:02 +0000544 if (isa<ClassTemplatePartialSpecializationDecl>(this))
545 return true;
546
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000547 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
548 if (Record->getDescribedClassTemplate())
549 return true;
550
John McCallc62bb642010-03-24 05:22:00 +0000551 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000552 if (Function->getDescribedFunctionTemplate())
553 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000554
John McCallc62bb642010-03-24 05:22:00 +0000555 // Friend function declarations are dependent if their *lexical*
556 // context is dependent.
557 if (cast<Decl>(this)->getFriendObjectKind())
558 return getLexicalParent()->isDependentContext();
559 }
560
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000561 return getParent() && getParent()->isDependentContext();
562}
563
Douglas Gregor07665a62009-01-05 19:45:36 +0000564bool DeclContext::isTransparentContext() const {
565 if (DeclKind == Decl::Enum)
Douglas Gregor0bf31402010-10-08 23:50:27 +0000566 return !cast<EnumDecl>(this)->isScoped();
Douglas Gregor07665a62009-01-05 19:45:36 +0000567 else if (DeclKind == Decl::LinkageSpec)
568 return true;
Douglas Gregor07665a62009-01-05 19:45:36 +0000569
570 return false;
571}
572
John McCall5fe84122010-10-26 04:59:26 +0000573bool DeclContext::isExternCContext() const {
574 const DeclContext *DC = this;
575 while (DC->DeclKind != Decl::TranslationUnit) {
576 if (DC->DeclKind == Decl::LinkageSpec)
577 return cast<LinkageSpecDecl>(DC)->getLanguage()
578 == LinkageSpecDecl::lang_c;
579 DC = DC->getParent();
580 }
581 return false;
582}
583
Sebastian Redl50c68252010-08-31 00:36:30 +0000584bool DeclContext::Encloses(const DeclContext *DC) const {
Douglas Gregore985a3b2009-08-27 06:03:53 +0000585 if (getPrimaryContext() != this)
586 return getPrimaryContext()->Encloses(DC);
Mike Stump11289f42009-09-09 15:08:12 +0000587
Douglas Gregore985a3b2009-08-27 06:03:53 +0000588 for (; DC; DC = DC->getParent())
589 if (DC->getPrimaryContext() == this)
590 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000591 return false;
Douglas Gregore985a3b2009-08-27 06:03:53 +0000592}
593
Steve Naroff35c62ae2009-01-08 17:28:14 +0000594DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor91f84212008-12-11 16:49:14 +0000595 switch (DeclKind) {
Douglas Gregor91f84212008-12-11 16:49:14 +0000596 case Decl::TranslationUnit:
Douglas Gregor07665a62009-01-05 19:45:36 +0000597 case Decl::LinkageSpec:
Mike Stump11289f42009-09-09 15:08:12 +0000598 case Decl::Block:
Douglas Gregor91f84212008-12-11 16:49:14 +0000599 // There is only one DeclContext for these entities.
600 return this;
601
602 case Decl::Namespace:
603 // The original namespace is our primary context.
604 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
605
Douglas Gregor91f84212008-12-11 16:49:14 +0000606 case Decl::ObjCMethod:
607 return this;
608
609 case Decl::ObjCInterface:
Steve Naroff35c62ae2009-01-08 17:28:14 +0000610 case Decl::ObjCProtocol:
611 case Decl::ObjCCategory:
Douglas Gregor91f84212008-12-11 16:49:14 +0000612 // FIXME: Can Objective-C interfaces be forward-declared?
613 return this;
614
Steve Naroff35c62ae2009-01-08 17:28:14 +0000615 case Decl::ObjCImplementation:
616 case Decl::ObjCCategoryImpl:
617 return this;
618
Douglas Gregor91f84212008-12-11 16:49:14 +0000619 default:
Alexis Hunted053252010-05-30 07:21:58 +0000620 if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
Douglas Gregor67a65642009-02-17 23:15:12 +0000621 // If this is a tag type that has a definition or is currently
622 // being defined, that definition is our primary context.
John McCalle78aac42010-03-10 03:28:59 +0000623 TagDecl *Tag = cast<TagDecl>(this);
624 assert(isa<TagType>(Tag->TypeForDecl) ||
625 isa<InjectedClassNameType>(Tag->TypeForDecl));
626
627 if (TagDecl *Def = Tag->getDefinition())
628 return Def;
629
630 if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) {
631 const TagType *TagTy = cast<TagType>(Tag->TypeForDecl);
632 if (TagTy->isBeingDefined())
633 // FIXME: is it necessarily being defined in the decl
634 // that owns the type?
635 return TagTy->getDecl();
636 }
637
638 return Tag;
Douglas Gregor67a65642009-02-17 23:15:12 +0000639 }
640
Alexis Hunted053252010-05-30 07:21:58 +0000641 assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
Douglas Gregor91f84212008-12-11 16:49:14 +0000642 "Unknown DeclContext kind");
643 return this;
644 }
645}
646
647DeclContext *DeclContext::getNextContext() {
648 switch (DeclKind) {
Douglas Gregor91f84212008-12-11 16:49:14 +0000649 case Decl::Namespace:
650 // Return the next namespace
651 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
652
653 default:
Douglas Gregor91f84212008-12-11 16:49:14 +0000654 return 0;
655 }
656}
657
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000658std::pair<Decl *, Decl *>
659DeclContext::BuildDeclChain(const llvm::SmallVectorImpl<Decl*> &Decls) {
660 // Build up a chain of declarations via the Decl::NextDeclInContext field.
661 Decl *FirstNewDecl = 0;
662 Decl *PrevDecl = 0;
663 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
664 Decl *D = Decls[I];
665 if (PrevDecl)
666 PrevDecl->NextDeclInContext = D;
667 else
668 FirstNewDecl = D;
669
670 PrevDecl = D;
671 }
672
673 return std::make_pair(FirstNewDecl, PrevDecl);
674}
675
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000676/// \brief Load the declarations within this lexical storage from an
677/// external source.
Mike Stump11289f42009-09-09 15:08:12 +0000678void
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000679DeclContext::LoadLexicalDeclsFromExternalStorage() const {
680 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000681 assert(hasExternalLexicalStorage() && Source && "No external storage?");
682
Argyrios Kyrtzidis98d045e2010-07-30 10:03:23 +0000683 // Notify that we have a DeclContext that is initializing.
684 ExternalASTSource::Deserializing ADeclContext(Source);
685
John McCall75b960e2010-06-01 09:23:16 +0000686 llvm::SmallVector<Decl*, 64> Decls;
687 if (Source->FindExternalLexicalDecls(this, Decls))
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000688 return;
689
690 // There is no longer any lexical storage in this context
691 ExternalLexicalStorage = false;
692
693 if (Decls.empty())
694 return;
695
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000696 // We may have already loaded just the fields of this record, in which case
697 // don't add the decls, just replace the FirstDecl/LastDecl chain.
698 if (const RecordDecl *RD = dyn_cast<RecordDecl>(this))
699 if (RD->LoadedFieldsFromExternalStorage) {
700 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
701 return;
702 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000703
704 // Splice the newly-read declarations into the beginning of the list
705 // of declarations.
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000706 Decl *ExternalFirst, *ExternalLast;
707 llvm::tie(ExternalFirst, ExternalLast) = BuildDeclChain(Decls);
708 ExternalLast->NextDeclInContext = FirstDecl;
709 FirstDecl = ExternalFirst;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000710 if (!LastDecl)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000711 LastDecl = ExternalLast;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000712}
713
John McCall75b960e2010-06-01 09:23:16 +0000714DeclContext::lookup_result
715ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
716 DeclarationName Name) {
717 ASTContext &Context = DC->getParentASTContext();
718 StoredDeclsMap *Map;
719 if (!(Map = DC->LookupPtr))
720 Map = DC->CreateStoredDeclsMap(Context);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000721
John McCall75b960e2010-06-01 09:23:16 +0000722 StoredDeclsList &List = (*Map)[Name];
723 assert(List.isNull());
724 (void) List;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000725
John McCall75b960e2010-06-01 09:23:16 +0000726 return DeclContext::lookup_result();
727}
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000728
John McCall75b960e2010-06-01 09:23:16 +0000729DeclContext::lookup_result
730ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +0000731 DeclarationName Name,
732 llvm::SmallVectorImpl<NamedDecl*> &Decls) {
733 ASTContext &Context = DC->getParentASTContext();;
734
735 StoredDeclsMap *Map;
736 if (!(Map = DC->LookupPtr))
737 Map = DC->CreateStoredDeclsMap(Context);
738
739 StoredDeclsList &List = (*Map)[Name];
740 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
741 if (List.isNull())
742 List.setOnlyValue(Decls[I]);
743 else
744 List.AddSubsequentDecl(Decls[I]);
745 }
746
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000747 return List.getLookupResult();
John McCall75b960e2010-06-01 09:23:16 +0000748}
749
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +0000750void ExternalASTSource::MaterializeVisibleDeclsForName(const DeclContext *DC,
751 DeclarationName Name,
752 llvm::SmallVectorImpl<NamedDecl*> &Decls) {
753 assert(DC->LookupPtr);
754 StoredDeclsMap &Map = *DC->LookupPtr;
755
756 // If there's an entry in the table the visible decls for this name have
757 // already been deserialized.
758 if (Map.find(Name) == Map.end()) {
759 StoredDeclsList &List = Map[Name];
760 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
761 if (List.isNull())
762 List.setOnlyValue(Decls[I]);
763 else
764 List.AddSubsequentDecl(Decls[I]);
765 }
766 }
767}
768
Sebastian Redl66c5eef2010-07-27 00:17:23 +0000769DeclContext::decl_iterator DeclContext::noload_decls_begin() const {
770 return decl_iterator(FirstDecl);
771}
772
773DeclContext::decl_iterator DeclContext::noload_decls_end() const {
774 return decl_iterator();
775}
776
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000777DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000778 if (hasExternalLexicalStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000779 LoadLexicalDeclsFromExternalStorage();
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000780
781 // FIXME: Check whether we need to load some declarations from
782 // external storage.
Mike Stump11289f42009-09-09 15:08:12 +0000783 return decl_iterator(FirstDecl);
Douglas Gregorbcced4e2009-04-09 21:40:53 +0000784}
785
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000786DeclContext::decl_iterator DeclContext::decls_end() const {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000787 if (hasExternalLexicalStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000788 LoadLexicalDeclsFromExternalStorage();
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000789
Mike Stump11289f42009-09-09 15:08:12 +0000790 return decl_iterator();
Douglas Gregorbcced4e2009-04-09 21:40:53 +0000791}
792
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000793bool DeclContext::decls_empty() const {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +0000794 if (hasExternalLexicalStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000795 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +0000796
797 return !FirstDecl;
798}
799
John McCall84d87672009-12-10 09:41:52 +0000800void DeclContext::removeDecl(Decl *D) {
801 assert(D->getLexicalDeclContext() == this &&
802 "decl being removed from non-lexical context");
803 assert((D->NextDeclInContext || D == LastDecl) &&
804 "decl is not in decls list");
805
806 // Remove D from the decl chain. This is O(n) but hopefully rare.
807 if (D == FirstDecl) {
808 if (D == LastDecl)
809 FirstDecl = LastDecl = 0;
810 else
811 FirstDecl = D->NextDeclInContext;
812 } else {
813 for (Decl *I = FirstDecl; true; I = I->NextDeclInContext) {
814 assert(I && "decl not found in linked list");
815 if (I->NextDeclInContext == D) {
816 I->NextDeclInContext = D->NextDeclInContext;
817 if (D == LastDecl) LastDecl = I;
818 break;
819 }
820 }
821 }
822
823 // Mark that D is no longer in the decl chain.
824 D->NextDeclInContext = 0;
825
826 // Remove D from the lookup table if necessary.
827 if (isa<NamedDecl>(D)) {
828 NamedDecl *ND = cast<NamedDecl>(D);
829
John McCallc62bb642010-03-24 05:22:00 +0000830 StoredDeclsMap *Map = getPrimaryContext()->LookupPtr;
831 if (!Map) return;
John McCall84d87672009-12-10 09:41:52 +0000832
John McCall84d87672009-12-10 09:41:52 +0000833 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
834 assert(Pos != Map->end() && "no lookup entry for decl");
835 Pos->second.remove(ND);
836 }
837}
838
John McCalld1e9d832009-08-11 06:59:38 +0000839void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner33f219d2009-02-20 00:56:18 +0000840 assert(D->getLexicalDeclContext() == this &&
841 "Decl inserted into wrong lexical context");
Mike Stump11289f42009-09-09 15:08:12 +0000842 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor020713e2009-01-09 19:42:16 +0000843 "Decl already inserted into a DeclContext");
844
845 if (FirstDecl) {
Chris Lattnerfcd33a62009-03-28 06:04:26 +0000846 LastDecl->NextDeclInContext = D;
Douglas Gregor020713e2009-01-09 19:42:16 +0000847 LastDecl = D;
848 } else {
849 FirstDecl = LastDecl = D;
850 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000851
852 // Notify a C++ record declaration that we've added a member, so it can
853 // update it's class-specific state.
854 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
855 Record->addedMember(D);
John McCalld1e9d832009-08-11 06:59:38 +0000856}
857
858void DeclContext::addDecl(Decl *D) {
859 addHiddenDecl(D);
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000860
861 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000862 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor91f84212008-12-11 16:49:14 +0000863}
864
Douglas Gregor07665a62009-01-05 19:45:36 +0000865/// buildLookup - Build the lookup data structure with all of the
866/// declarations in DCtx (and any other contexts linked to it or
867/// transparent contexts nested within it).
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000868void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregor07665a62009-01-05 19:45:36 +0000869 for (; DCtx; DCtx = DCtx->getNextContext()) {
Mike Stump11289f42009-09-09 15:08:12 +0000870 for (decl_iterator D = DCtx->decls_begin(),
871 DEnd = DCtx->decls_end();
Douglas Gregord05cb412009-01-06 07:17:58 +0000872 D != DEnd; ++D) {
John McCalld1e9d832009-08-11 06:59:38 +0000873 // Insert this declaration into the lookup structure, but only
874 // if it's semantically in its decl context. During non-lazy
875 // lookup building, this is implicitly enforced by addDecl.
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000876 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
John McCalld1e9d832009-08-11 06:59:38 +0000877 if (D->getDeclContext() == DCtx)
878 makeDeclVisibleInContextImpl(ND);
Douglas Gregor07665a62009-01-05 19:45:36 +0000879
Ted Kremenek707ece62009-11-17 22:58:30 +0000880 // Insert any forward-declared Objective-C interfaces into the lookup
881 // data structure.
882 if (ObjCClassDecl *Class = dyn_cast<ObjCClassDecl>(*D))
883 for (ObjCClassDecl::iterator I = Class->begin(), IEnd = Class->end();
884 I != IEnd; ++I)
Ted Kremenek9b124e12009-11-18 00:28:11 +0000885 makeDeclVisibleInContextImpl(I->getInterface());
Ted Kremenek707ece62009-11-17 22:58:30 +0000886
Sebastian Redlbd595762010-08-31 20:53:31 +0000887 // If this declaration is itself a transparent declaration context or
888 // inline namespace, add its members (recursively).
Douglas Gregor07665a62009-01-05 19:45:36 +0000889 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
Sebastian Redlbd595762010-08-31 20:53:31 +0000890 if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000891 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregor07665a62009-01-05 19:45:36 +0000892 }
893 }
894}
895
Mike Stump11289f42009-09-09 15:08:12 +0000896DeclContext::lookup_result
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000897DeclContext::lookup(DeclarationName Name) {
Steve Naroff35c62ae2009-01-08 17:28:14 +0000898 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor91f84212008-12-11 16:49:14 +0000899 if (PrimaryContext != this)
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000900 return PrimaryContext->lookup(Name);
Douglas Gregor91f84212008-12-11 16:49:14 +0000901
John McCall75b960e2010-06-01 09:23:16 +0000902 if (hasExternalVisibleStorage()) {
903 // Check to see if we've already cached the lookup results.
904 if (LookupPtr) {
905 StoredDeclsMap::iterator I = LookupPtr->find(Name);
906 if (I != LookupPtr->end())
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000907 return I->second.getLookupResult();
John McCall75b960e2010-06-01 09:23:16 +0000908 }
909
910 ExternalASTSource *Source = getParentASTContext().getExternalSource();
911 return Source->FindExternalVisibleDeclsByName(this, Name);
912 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000913
Douglas Gregor55297ac2008-12-23 00:26:44 +0000914 /// If there is no lookup data structure, build one now by walking
Douglas Gregor91f84212008-12-11 16:49:14 +0000915 /// all of the linked DeclContexts (in declaration order!) and
916 /// inserting their values.
Douglas Gregor9615ec22009-04-09 17:29:08 +0000917 if (!LookupPtr) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000918 buildLookup(this);
Douglas Gregor91f84212008-12-11 16:49:14 +0000919
Douglas Gregor9615ec22009-04-09 17:29:08 +0000920 if (!LookupPtr)
Douglas Gregor10dc8aa2010-05-11 06:18:17 +0000921 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Douglas Gregor9615ec22009-04-09 17:29:08 +0000922 }
Douglas Gregor91f84212008-12-11 16:49:14 +0000923
John McCallc62bb642010-03-24 05:22:00 +0000924 StoredDeclsMap::iterator Pos = LookupPtr->find(Name);
925 if (Pos == LookupPtr->end())
Douglas Gregor10dc8aa2010-05-11 06:18:17 +0000926 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000927 return Pos->second.getLookupResult();
Douglas Gregor91f84212008-12-11 16:49:14 +0000928}
929
Mike Stump11289f42009-09-09 15:08:12 +0000930DeclContext::lookup_const_result
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000931DeclContext::lookup(DeclarationName Name) const {
932 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor91f84212008-12-11 16:49:14 +0000933}
934
Sebastian Redl50c68252010-08-31 00:36:30 +0000935DeclContext *DeclContext::getRedeclContext() {
Chris Lattner17a1bfa2009-03-27 19:19:59 +0000936 DeclContext *Ctx = this;
Sebastian Redlbd595762010-08-31 20:53:31 +0000937 // Skip through transparent contexts.
938 while (Ctx->isTransparentContext())
Douglas Gregor6ad0ef52009-01-06 23:51:29 +0000939 Ctx = Ctx->getParent();
940 return Ctx;
941}
942
Douglas Gregorf47b9112009-02-25 22:02:03 +0000943DeclContext *DeclContext::getEnclosingNamespaceContext() {
944 DeclContext *Ctx = this;
945 // Skip through non-namespace, non-translation-unit contexts.
Sebastian Redl4f08c962010-08-31 00:36:23 +0000946 while (!Ctx->isFileContext())
Douglas Gregorf47b9112009-02-25 22:02:03 +0000947 Ctx = Ctx->getParent();
948 return Ctx->getPrimaryContext();
949}
950
Sebastian Redl50c68252010-08-31 00:36:30 +0000951bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
952 // For non-file contexts, this is equivalent to Equals.
953 if (!isFileContext())
954 return O->Equals(this);
955
956 do {
957 if (O->Equals(this))
958 return true;
959
960 const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O);
961 if (!NS || !NS->isInline())
962 break;
963 O = NS->getParent();
964 } while (O);
965
966 return false;
967}
968
John McCall759e32b2009-08-31 22:39:49 +0000969void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable) {
Douglas Gregor67a65642009-02-17 23:15:12 +0000970 // FIXME: This feels like a hack. Should DeclarationName support
971 // template-ids, or is there a better way to keep specializations
972 // from being visible?
973 if (isa<ClassTemplateSpecializationDecl>(D))
974 return;
Eli Friedman73168192009-12-08 05:40:03 +0000975 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
976 if (FD->isFunctionTemplateSpecialization())
977 return;
Douglas Gregor67a65642009-02-17 23:15:12 +0000978
Steve Naroff35c62ae2009-01-08 17:28:14 +0000979 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor91f84212008-12-11 16:49:14 +0000980 if (PrimaryContext != this) {
John McCall759e32b2009-08-31 22:39:49 +0000981 PrimaryContext->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor91f84212008-12-11 16:49:14 +0000982 return;
983 }
984
985 // If we already have a lookup data structure, perform the insertion
Argyrios Kyrtzidise51e5542010-07-04 21:44:25 +0000986 // into it. If we haven't deserialized externally stored decls, deserialize
987 // them so we can add the decl. Otherwise, be lazy and don't build that
988 // structure until someone asks for it.
989 if (LookupPtr || !Recoverable || hasExternalVisibleStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000990 makeDeclVisibleInContextImpl(D);
Douglas Gregor07665a62009-01-05 19:45:36 +0000991
Sebastian Redlbd595762010-08-31 20:53:31 +0000992 // If we are a transparent context or inline namespace, insert into our
993 // parent context, too. This operation is recursive.
994 if (isTransparentContext() || isInlineNamespace())
John McCall759e32b2009-08-31 22:39:49 +0000995 getParent()->makeDeclVisibleInContext(D, Recoverable);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +0000996
997 Decl *DCAsDecl = cast<Decl>(this);
998 // Notify that a decl was made visible unless it's a Tag being defined.
999 if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined()))
1000 if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
1001 L->AddedVisibleDecl(this, D);
Douglas Gregor91f84212008-12-11 16:49:14 +00001002}
1003
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001004void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) {
Douglas Gregor07665a62009-01-05 19:45:36 +00001005 // Skip unnamed declarations.
1006 if (!D->getDeclName())
1007 return;
1008
Douglas Gregor67a65642009-02-17 23:15:12 +00001009 // FIXME: This feels like a hack. Should DeclarationName support
1010 // template-ids, or is there a better way to keep specializations
1011 // from being visible?
1012 if (isa<ClassTemplateSpecializationDecl>(D))
1013 return;
1014
Argyrios Kyrtzidise51e5542010-07-04 21:44:25 +00001015 ASTContext *C = 0;
1016 if (!LookupPtr) {
1017 C = &getParentASTContext();
1018 CreateStoredDeclsMap(*C);
1019 }
1020
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001021 // If there is an external AST source, load any declarations it knows about
1022 // with this declaration's name.
1023 // If the lookup table contains an entry about this name it means that we
1024 // have already checked the external source.
1025 if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
1026 if (hasExternalVisibleStorage() &&
1027 LookupPtr->find(D->getDeclName()) == LookupPtr->end())
1028 Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
1029
Douglas Gregor91f84212008-12-11 16:49:14 +00001030 // Insert this declaration into the map.
John McCallc62bb642010-03-24 05:22:00 +00001031 StoredDeclsList &DeclNameEntries = (*LookupPtr)[D->getDeclName()];
Chris Lattnercaae7162009-02-20 01:44:05 +00001032 if (DeclNameEntries.isNull()) {
1033 DeclNameEntries.setOnlyValue(D);
Chris Lattnerdfd6b3d2009-02-19 07:00:44 +00001034 return;
Douglas Gregor91f84212008-12-11 16:49:14 +00001035 }
Chris Lattner24e24d52009-02-20 00:55:03 +00001036
Chris Lattner29578f32009-02-20 01:10:07 +00001037 // If it is possible that this is a redeclaration, check to see if there is
1038 // already a decl for which declarationReplaces returns true. If there is
1039 // one, just replace it and return.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001040 if (DeclNameEntries.HandleRedeclaration(D))
Chris Lattnercaae7162009-02-20 01:44:05 +00001041 return;
Mike Stump11289f42009-09-09 15:08:12 +00001042
Chris Lattnerdfd6b3d2009-02-19 07:00:44 +00001043 // Put this declaration into the appropriate slot.
Chris Lattnercaae7162009-02-20 01:44:05 +00001044 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor91f84212008-12-11 16:49:14 +00001045}
Douglas Gregor889ceb72009-02-03 19:21:40 +00001046
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00001047void DeclContext::MaterializeVisibleDeclsFromExternalStorage() {
1048 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1049 assert(hasExternalVisibleStorage() && Source && "No external storage?");
1050
1051 if (!LookupPtr)
1052 CreateStoredDeclsMap(getParentASTContext());
1053 Source->MaterializeVisibleDecls(this);
1054}
1055
Douglas Gregor889ceb72009-02-03 19:21:40 +00001056/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
1057/// this context.
Mike Stump11289f42009-09-09 15:08:12 +00001058DeclContext::udir_iterator_range
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001059DeclContext::getUsingDirectives() const {
1060 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor889ceb72009-02-03 19:21:40 +00001061 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
1062 reinterpret_cast<udir_iterator>(Result.second));
1063}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001064
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001065//===----------------------------------------------------------------------===//
1066// Creation and Destruction of StoredDeclsMaps. //
1067//===----------------------------------------------------------------------===//
1068
John McCallc62bb642010-03-24 05:22:00 +00001069StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
1070 assert(!LookupPtr && "context already has a decls map");
1071 assert(getPrimaryContext() == this &&
1072 "creating decls map on non-primary context");
1073
1074 StoredDeclsMap *M;
1075 bool Dependent = isDependentContext();
1076 if (Dependent)
1077 M = new DependentStoredDeclsMap();
1078 else
1079 M = new StoredDeclsMap();
1080 M->Previous = C.LastSDM;
1081 C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
1082 LookupPtr = M;
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001083 return M;
1084}
1085
1086void ASTContext::ReleaseDeclContextMaps() {
John McCallc62bb642010-03-24 05:22:00 +00001087 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1088 // pointer because the subclass doesn't add anything that needs to
1089 // be deleted.
John McCallc62bb642010-03-24 05:22:00 +00001090 StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1091}
1092
1093void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1094 while (Map) {
1095 // Advance the iteration before we invalidate memory.
1096 llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1097
1098 if (Dependent)
1099 delete static_cast<DependentStoredDeclsMap*>(Map);
1100 else
1101 delete Map;
1102
1103 Map = Next.getPointer();
1104 Dependent = Next.getInt();
1105 }
1106}
1107
John McCallc62bb642010-03-24 05:22:00 +00001108DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1109 DeclContext *Parent,
1110 const PartialDiagnostic &PDiag) {
1111 assert(Parent->isDependentContext()
1112 && "cannot iterate dependent diagnostics of non-dependent context");
1113 Parent = Parent->getPrimaryContext();
1114 if (!Parent->LookupPtr)
1115 Parent->CreateStoredDeclsMap(C);
1116
1117 DependentStoredDeclsMap *Map
1118 = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr);
1119
Douglas Gregora55530e2010-03-29 23:56:53 +00001120 // Allocate the copy of the PartialDiagnostic via the ASTContext's
Douglas Gregor89336232010-03-29 23:34:08 +00001121 // BumpPtrAllocator, rather than the ASTContext itself.
Douglas Gregora55530e2010-03-29 23:56:53 +00001122 PartialDiagnostic::Storage *DiagStorage = 0;
1123 if (PDiag.hasStorage())
1124 DiagStorage = new (C) PartialDiagnostic::Storage;
1125
1126 DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
John McCallc62bb642010-03-24 05:22:00 +00001127
1128 // TODO: Maybe we shouldn't reverse the order during insertion.
1129 DD->NextDiagnostic = Map->FirstDiagnostic;
1130 Map->FirstDiagnostic = DD;
1131
1132 return DD;
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001133}