blob: 5a117ebbf2f803be84ad2539971ff5fe62801a79 [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
Douglas Gregor00716e82011-02-17 08:12:32 +000045namespace {
46 template<typename Class>
47 inline SourceRange getSourceRangeImpl(const Decl *D,
48 SourceRange (Class::*)() const) {
49 return static_cast<const Class *>(D)->getSourceRange();
50 }
51
52 inline SourceRange getSourceRangeImpl(const Decl *D,
53 SourceRange (Decl::*)() const) {
54 return D->getLocation();
55 }
56}
57
58SourceRange Decl::getSourceRange() const {
59 switch (getKind()) {
60#define ABSTRACT_DECL(Type)
61#define DECL(Type, Base) \
62 case Type: return getSourceRangeImpl(this, &Type##Decl::getSourceRange);
63#include "clang/AST/DeclNodes.inc"
64 }
65
66 return getLocation();
67}
68
Eli Friedman7dbab8a2008-06-07 16:52:53 +000069const char *Decl::getDeclKindName() const {
70 switch (DeclKind) {
Alexis Hunted053252010-05-30 07:21:58 +000071 default: assert(0 && "Declaration not in DeclNodes.inc!");
72#define DECL(DERIVED, BASE) case DERIVED: return #DERIVED;
73#define ABSTRACT_DECL(DECL)
74#include "clang/AST/DeclNodes.inc"
Eli Friedman7dbab8a2008-06-07 16:52:53 +000075 }
76}
77
Douglas Gregor90d47172010-03-05 00:26:45 +000078void Decl::setInvalidDecl(bool Invalid) {
79 InvalidDecl = Invalid;
80 if (Invalid) {
81 // Defensive maneuver for ill-formed code: we're likely not to make it to
82 // a point where we set the access specifier, so default it to "public"
83 // to avoid triggering asserts elsewhere in the front end.
84 setAccess(AS_public);
85 }
86}
87
Steve Naroff5faaef72009-01-20 19:53:53 +000088const char *DeclContext::getDeclKindName() const {
89 switch (DeclKind) {
Alexis Hunted053252010-05-30 07:21:58 +000090 default: assert(0 && "Declaration context not in DeclNodes.inc!");
91#define DECL(DERIVED, BASE) case Decl::DERIVED: return #DERIVED;
92#define ABSTRACT_DECL(DECL)
93#include "clang/AST/DeclNodes.inc"
Steve Naroff5faaef72009-01-20 19:53:53 +000094 }
95}
96
Eli Friedman7dbab8a2008-06-07 16:52:53 +000097bool Decl::CollectingStats(bool Enable) {
Kovarththanan Rajaratnam130f7f92009-11-29 14:54:35 +000098 if (Enable) StatSwitch = true;
Eli Friedman7dbab8a2008-06-07 16:52:53 +000099 return StatSwitch;
100}
101
102void Decl::PrintStats() {
103 fprintf(stderr, "*** Decl Stats:\n");
Mike Stump11289f42009-09-09 15:08:12 +0000104
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +0000105 int totalDecls = 0;
Alexis Hunted053252010-05-30 07:21:58 +0000106#define DECL(DERIVED, BASE) totalDecls += n##DERIVED##s;
107#define ABSTRACT_DECL(DECL)
108#include "clang/AST/DeclNodes.inc"
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +0000109 fprintf(stderr, " %d decls total.\n", totalDecls);
Mike Stump11289f42009-09-09 15:08:12 +0000110
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +0000111 int totalBytes = 0;
Alexis Hunted053252010-05-30 07:21:58 +0000112#define DECL(DERIVED, BASE) \
113 if (n##DERIVED##s > 0) { \
114 totalBytes += (int)(n##DERIVED##s * sizeof(DERIVED##Decl)); \
115 fprintf(stderr, " %d " #DERIVED " decls, %d each (%d bytes)\n", \
116 n##DERIVED##s, (int)sizeof(DERIVED##Decl), \
117 (int)(n##DERIVED##s * sizeof(DERIVED##Decl))); \
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +0000118 }
Alexis Hunted053252010-05-30 07:21:58 +0000119#define ABSTRACT_DECL(DECL)
120#include "clang/AST/DeclNodes.inc"
Mike Stump11289f42009-09-09 15:08:12 +0000121
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +0000122 fprintf(stderr, "Total bytes = %d\n", totalBytes);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000123}
124
Alexis Hunted053252010-05-30 07:21:58 +0000125void Decl::add(Kind k) {
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000126 switch (k) {
Alexis Hunted053252010-05-30 07:21:58 +0000127 default: assert(0 && "Declaration not in DeclNodes.inc!");
128#define DECL(DERIVED, BASE) case DERIVED: ++n##DERIVED##s; break;
129#define ABSTRACT_DECL(DECL)
130#include "clang/AST/DeclNodes.inc"
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000131 }
132}
133
Anders Carlssonaa73b912009-06-13 00:08:58 +0000134bool Decl::isTemplateParameterPack() const {
135 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
136 return TTP->isParameterPack();
Douglas Gregorda3cc0d2010-12-23 23:51:58 +0000137 if (const NonTypeTemplateParmDecl *NTTP
Douglas Gregorf5500772011-01-05 15:48:55 +0000138 = dyn_cast<NonTypeTemplateParmDecl>(this))
Douglas Gregorda3cc0d2010-12-23 23:51:58 +0000139 return NTTP->isParameterPack();
Douglas Gregorf5500772011-01-05 15:48:55 +0000140 if (const TemplateTemplateParmDecl *TTP
141 = dyn_cast<TemplateTemplateParmDecl>(this))
142 return TTP->isParameterPack();
Anders Carlssonaa73b912009-06-13 00:08:58 +0000143 return false;
144}
145
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +0000146bool Decl::isParameterPack() const {
147 if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(this))
148 return Parm->isParameterPack();
149
150 return isTemplateParameterPack();
151}
152
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000153bool Decl::isFunctionOrFunctionTemplate() const {
John McCall3f746822009-11-17 05:59:44 +0000154 if (const UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(this))
Anders Carlssonf057cb22009-06-26 05:26:50 +0000155 return UD->getTargetDecl()->isFunctionOrFunctionTemplate();
Mike Stump11289f42009-09-09 15:08:12 +0000156
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000157 return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this);
158}
159
Douglas Gregorf5974fa2010-01-16 20:21:20 +0000160bool Decl::isDefinedOutsideFunctionOrMethod() const {
161 for (const DeclContext *DC = getDeclContext();
162 DC && !DC->isTranslationUnit();
163 DC = DC->getParent())
164 if (DC->isFunctionOrMethod())
165 return false;
166
167 return true;
168}
169
Douglas Gregor7edc20a2011-02-17 07:58:36 +0000170namespace {
171 template<typename Class, typename Result>
Douglas Gregor5ee06f02011-02-17 08:14:56 +0000172 inline Result *getCanonicalDeclImpl(Decl *D, Result *(Class::*)()) {
173 return static_cast<Class *>(D)->getCanonicalDecl();
Douglas Gregor7edc20a2011-02-17 07:58:36 +0000174 }
175
Douglas Gregor5ee06f02011-02-17 08:14:56 +0000176 inline Decl *getCanonicalDeclImpl(Decl *D, Decl *(Decl::*)()) {
Douglas Gregor7edc20a2011-02-17 07:58:36 +0000177 // No specific implementation.
178 return D;
179 }
180}
181
182Decl *Decl::getCanonicalDecl() {
183 switch (getKind()) {
184#define ABSTRACT_DECL(Type)
185#define DECL(Type, Base) \
186 case Type: \
Douglas Gregor5ee06f02011-02-17 08:14:56 +0000187 return getCanonicalDeclImpl(this, &Type##Decl::getCanonicalDecl);
Douglas Gregor7edc20a2011-02-17 07:58:36 +0000188#include "clang/AST/DeclNodes.inc"
189 }
Douglas Gregor7edc20a2011-02-17 07:58:36 +0000190
Douglas Gregor00716e82011-02-17 08:12:32 +0000191 return this;
Douglas Gregor7edc20a2011-02-17 07:58:36 +0000192}
Douglas Gregorf5974fa2010-01-16 20:21:20 +0000193
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000194//===----------------------------------------------------------------------===//
Chris Lattnereae6cb62009-03-05 08:00:35 +0000195// PrettyStackTraceDecl Implementation
196//===----------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000197
Chris Lattnereae6cb62009-03-05 08:00:35 +0000198void PrettyStackTraceDecl::print(llvm::raw_ostream &OS) const {
199 SourceLocation TheLoc = Loc;
200 if (TheLoc.isInvalid() && TheDecl)
201 TheLoc = TheDecl->getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000202
Chris Lattnereae6cb62009-03-05 08:00:35 +0000203 if (TheLoc.isValid()) {
204 TheLoc.print(OS, SM);
205 OS << ": ";
206 }
207
208 OS << Message;
209
Daniel Dunbar4f1054e2009-11-21 09:05:59 +0000210 if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
Chris Lattnereae6cb62009-03-05 08:00:35 +0000211 OS << " '" << DN->getQualifiedNameAsString() << '\'';
212 OS << '\n';
213}
Mike Stump11289f42009-09-09 15:08:12 +0000214
Chris Lattnereae6cb62009-03-05 08:00:35 +0000215//===----------------------------------------------------------------------===//
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000216// Decl Implementation
217//===----------------------------------------------------------------------===//
218
Chris Lattner8e097192009-03-27 20:18:19 +0000219// Out-of-line virtual method providing a home for Decl.
Douglas Gregorb412e172010-07-25 18:17:45 +0000220Decl::~Decl() { }
Chris Lattner8e097192009-03-27 20:18:19 +0000221
Douglas Gregora43942a2011-02-17 07:02:32 +0000222bool Decl::isOutOfLine() const {
223 if (const VarDecl *VD = dyn_cast<VarDecl>(this))
224 return VD->isOutOfLine();
225 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
226 return FD->isOutOfLine();
227
228 return getLexicalDeclContext() != getDeclContext();
229}
230
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000231void Decl::setDeclContext(DeclContext *DC) {
232 if (isOutOfSemaDC())
233 delete getMultipleDC();
Mike Stump11289f42009-09-09 15:08:12 +0000234
Chris Lattnerb81eb052009-03-29 06:06:59 +0000235 DeclCtx = DC;
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000236}
237
238void Decl::setLexicalDeclContext(DeclContext *DC) {
239 if (DC == getLexicalDeclContext())
240 return;
241
242 if (isInSemaDC()) {
Ted Kremenekf8c12a32009-12-01 00:07:10 +0000243 MultipleDC *MDC = new (getASTContext()) MultipleDC();
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000244 MDC->SemanticDC = getDeclContext();
245 MDC->LexicalDC = DC;
Chris Lattnerb81eb052009-03-29 06:06:59 +0000246 DeclCtx = MDC;
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000247 } else {
248 getMultipleDC()->LexicalDC = DC;
249 }
250}
251
John McCall4fa53422009-10-01 00:25:31 +0000252bool Decl::isInAnonymousNamespace() const {
253 const DeclContext *DC = getDeclContext();
254 do {
255 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
256 if (ND->isAnonymousNamespace())
257 return true;
258 } while ((DC = DC->getParent()));
259
260 return false;
261}
262
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000263TranslationUnitDecl *Decl::getTranslationUnitDecl() {
Argyrios Kyrtzidis4e1a72b2009-06-30 02:34:53 +0000264 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
265 return TUD;
266
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000267 DeclContext *DC = getDeclContext();
268 assert(DC && "This decl is not contained in a translation unit!");
Mike Stump11289f42009-09-09 15:08:12 +0000269
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000270 while (!DC->isTranslationUnit()) {
271 DC = DC->getParent();
272 assert(DC && "This decl is not contained in a translation unit!");
273 }
Mike Stump11289f42009-09-09 15:08:12 +0000274
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000275 return cast<TranslationUnitDecl>(DC);
276}
277
278ASTContext &Decl::getASTContext() const {
Mike Stump11289f42009-09-09 15:08:12 +0000279 return getTranslationUnitDecl()->getASTContext();
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000280}
281
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +0000282ASTMutationListener *Decl::getASTMutationListener() const {
283 return getASTContext().getASTMutationListener();
284}
285
Douglas Gregorebada0772010-06-17 23:14:26 +0000286bool Decl::isUsed(bool CheckUsedAttr) const {
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000287 if (Used)
288 return true;
289
290 // Check for used attribute.
Douglas Gregorebada0772010-06-17 23:14:26 +0000291 if (CheckUsedAttr && hasAttr<UsedAttr>())
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000292 return true;
293
294 // Check redeclarations for used attribute.
295 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Douglas Gregorebada0772010-06-17 23:14:26 +0000296 if ((CheckUsedAttr && I->hasAttr<UsedAttr>()) || I->Used)
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000297 return true;
298 }
299
300 return false;
301}
302
303
Chris Lattner8e097192009-03-27 20:18:19 +0000304unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
305 switch (DeclKind) {
John McCall3f746822009-11-17 05:59:44 +0000306 case Function:
307 case CXXMethod:
308 case CXXConstructor:
309 case CXXDestructor:
310 case CXXConversion:
Chris Lattner8e097192009-03-27 20:18:19 +0000311 case EnumConstant:
312 case Var:
313 case ImplicitParam:
314 case ParmVar:
Chris Lattner8e097192009-03-27 20:18:19 +0000315 case NonTypeTemplateParm:
316 case ObjCMethod:
Daniel Dunbar45b2d8a2010-04-23 13:07:39 +0000317 case ObjCProperty:
Daniel Dunbar45b2d8a2010-04-23 13:07:39 +0000318 return IDNS_Ordinary;
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000319 case Label:
320 return IDNS_Label;
Francois Pichet783dd6e2010-11-21 06:08:52 +0000321 case IndirectField:
322 return IDNS_Ordinary | IDNS_Member;
323
John McCalle87beb22010-04-23 18:46:30 +0000324 case ObjCCompatibleAlias:
325 case ObjCInterface:
326 return IDNS_Ordinary | IDNS_Type;
327
328 case Typedef:
329 case UnresolvedUsingTypename:
330 case TemplateTypeParm:
331 return IDNS_Ordinary | IDNS_Type;
332
John McCall3f746822009-11-17 05:59:44 +0000333 case UsingShadow:
334 return 0; // we'll actually overwrite this later
335
John McCalle61f2ba2009-11-18 02:36:19 +0000336 case UnresolvedUsingValue:
John McCalle61f2ba2009-11-18 02:36:19 +0000337 return IDNS_Ordinary | IDNS_Using;
John McCall3f746822009-11-17 05:59:44 +0000338
339 case Using:
340 return IDNS_Using;
341
Chris Lattner8e097192009-03-27 20:18:19 +0000342 case ObjCProtocol:
Douglas Gregor79947a22009-04-24 00:11:27 +0000343 return IDNS_ObjCProtocol;
Mike Stump11289f42009-09-09 15:08:12 +0000344
Chris Lattner8e097192009-03-27 20:18:19 +0000345 case Field:
346 case ObjCAtDefsField:
347 case ObjCIvar:
348 return IDNS_Member;
Mike Stump11289f42009-09-09 15:08:12 +0000349
Chris Lattner8e097192009-03-27 20:18:19 +0000350 case Record:
351 case CXXRecord:
352 case Enum:
John McCalle87beb22010-04-23 18:46:30 +0000353 return IDNS_Tag | IDNS_Type;
Mike Stump11289f42009-09-09 15:08:12 +0000354
Chris Lattner8e097192009-03-27 20:18:19 +0000355 case Namespace:
John McCalle87beb22010-04-23 18:46:30 +0000356 case NamespaceAlias:
357 return IDNS_Namespace;
358
Chris Lattner8e097192009-03-27 20:18:19 +0000359 case FunctionTemplate:
John McCalle87beb22010-04-23 18:46:30 +0000360 return IDNS_Ordinary;
361
Chris Lattner8e097192009-03-27 20:18:19 +0000362 case ClassTemplate:
363 case TemplateTemplateParm:
John McCalle87beb22010-04-23 18:46:30 +0000364 return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
Mike Stump11289f42009-09-09 15:08:12 +0000365
Chris Lattner8e097192009-03-27 20:18:19 +0000366 // Never have names.
John McCallaa74a0c2009-08-28 07:59:38 +0000367 case Friend:
John McCall11083da2009-09-16 22:47:08 +0000368 case FriendTemplate:
Abramo Bagnarad7340582010-06-05 05:09:32 +0000369 case AccessSpec:
Chris Lattner8e097192009-03-27 20:18:19 +0000370 case LinkageSpec:
371 case FileScopeAsm:
372 case StaticAssert:
373 case ObjCClass:
Chris Lattner8e097192009-03-27 20:18:19 +0000374 case ObjCPropertyImpl:
375 case ObjCForwardProtocol:
376 case Block:
377 case TranslationUnit:
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000378
Chris Lattner8e097192009-03-27 20:18:19 +0000379 case UsingDirective:
380 case ClassTemplateSpecialization:
Douglas Gregor2373c592009-05-31 09:31:02 +0000381 case ClassTemplatePartialSpecialization:
Douglas Gregore93525e2010-04-22 23:19:50 +0000382 case ObjCImplementation:
383 case ObjCCategory:
384 case ObjCCategoryImpl:
385 // Never looked up by name.
Chris Lattner8e097192009-03-27 20:18:19 +0000386 return 0;
387 }
John McCall3f746822009-11-17 05:59:44 +0000388
389 return 0;
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000390}
391
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000392void Decl::setAttrs(const AttrVec &attrs) {
Argyrios Kyrtzidis91167172010-06-11 23:09:25 +0000393 assert(!HasAttrs && "Decl already contains attrs.");
394
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000395 AttrVec &AttrBlank = getASTContext().getDeclAttrs(this);
396 assert(AttrBlank.empty() && "HasAttrs was wrong?");
Argyrios Kyrtzidis91167172010-06-11 23:09:25 +0000397
398 AttrBlank = attrs;
399 HasAttrs = true;
400}
401
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000402void Decl::dropAttrs() {
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000403 if (!HasAttrs) return;
Mike Stump11289f42009-09-09 15:08:12 +0000404
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000405 HasAttrs = false;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000406 getASTContext().eraseDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000407}
408
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000409const AttrVec &Decl::getAttrs() const {
410 assert(HasAttrs && "No attrs to get!");
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000411 return getASTContext().getDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000412}
413
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000414void Decl::swapAttrs(Decl *RHS) {
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000415 bool HasLHSAttr = this->HasAttrs;
416 bool HasRHSAttr = RHS->HasAttrs;
Mike Stump11289f42009-09-09 15:08:12 +0000417
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000418 // Usually, neither decl has attrs, nothing to do.
419 if (!HasLHSAttr && !HasRHSAttr) return;
Mike Stump11289f42009-09-09 15:08:12 +0000420
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000421 // If 'this' has no attrs, swap the other way.
422 if (!HasLHSAttr)
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000423 return RHS->swapAttrs(this);
Mike Stump11289f42009-09-09 15:08:12 +0000424
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000425 ASTContext &Context = getASTContext();
Mike Stump11289f42009-09-09 15:08:12 +0000426
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000427 // Handle the case when both decls have attrs.
428 if (HasRHSAttr) {
Douglas Gregor78bd61f2009-06-18 16:11:24 +0000429 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000430 return;
431 }
Mike Stump11289f42009-09-09 15:08:12 +0000432
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000433 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor78bd61f2009-06-18 16:11:24 +0000434 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
435 Context.eraseDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000436 this->HasAttrs = false;
437 RHS->HasAttrs = true;
438}
439
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000440Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000441 Decl::Kind DK = D->getDeclKind();
442 switch(DK) {
Alexis Hunted053252010-05-30 07:21:58 +0000443#define DECL(NAME, BASE)
444#define DECL_CONTEXT(NAME) \
445 case Decl::NAME: \
446 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
447#define DECL_CONTEXT_BASE(NAME)
448#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000449 default:
Alexis Hunted053252010-05-30 07:21:58 +0000450#define DECL(NAME, BASE)
451#define DECL_CONTEXT_BASE(NAME) \
452 if (DK >= first##NAME && DK <= last##NAME) \
453 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
454#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000455 assert(false && "a decl that inherits DeclContext isn't handled");
456 return 0;
457 }
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000458}
459
460DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000461 Decl::Kind DK = D->getKind();
462 switch(DK) {
Alexis Hunted053252010-05-30 07:21:58 +0000463#define DECL(NAME, BASE)
464#define DECL_CONTEXT(NAME) \
465 case Decl::NAME: \
466 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
467#define DECL_CONTEXT_BASE(NAME)
468#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000469 default:
Alexis Hunted053252010-05-30 07:21:58 +0000470#define DECL(NAME, BASE)
471#define DECL_CONTEXT_BASE(NAME) \
472 if (DK >= first##NAME && DK <= last##NAME) \
473 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
474#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000475 assert(false && "a decl that inherits DeclContext isn't handled");
476 return 0;
477 }
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000478}
479
Douglas Gregorf7b2c932011-02-17 07:13:24 +0000480Stmt *Decl::getBody() const {
481 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
482 return FD->getBody();
483 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(this))
484 return MD->getBody();
485 if (const BlockDecl *BD = dyn_cast<BlockDecl>(this))
486 return BD->getBody();
487
488 return 0;
489}
490
491bool Decl::hasBody() const {
492 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
493 return FD->hasBody();
494
495 return getBody() != 0;
496}
497
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000498SourceLocation Decl::getBodyRBrace() const {
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +0000499 // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
500 // FunctionDecl stores EndRangeLoc for this purpose.
501 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
502 const FunctionDecl *Definition;
503 if (FD->hasBody(Definition))
504 return Definition->getSourceRange().getEnd();
505 return SourceLocation();
506 }
507
Argyrios Kyrtzidis6fbc8fa2010-07-07 11:31:27 +0000508 if (Stmt *Body = getBody())
509 return Body->getSourceRange().getEnd();
510
511 return SourceLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +0000512}
513
Anders Carlssona28908d2009-03-25 23:38:06 +0000514void Decl::CheckAccessDeclContext() const {
Douglas Gregor4b00d3b2010-12-02 00:22:25 +0000515#ifndef NDEBUG
John McCall401982f2010-01-20 21:53:11 +0000516 // Suppress this check if any of the following hold:
517 // 1. this is the translation unit (and thus has no parent)
518 // 2. this is a template parameter (and thus doesn't belong to its context)
Argyrios Kyrtzidise1778632010-09-08 21:58:42 +0000519 // 3. this is a non-type template parameter
520 // 4. the context is not a record
521 // 5. it's invalid
522 // 6. it's a C++0x static_assert.
Anders Carlssonadf36b22009-08-29 20:47:47 +0000523 if (isa<TranslationUnitDecl>(this) ||
Argyrios Kyrtzidisa45855f2010-07-02 11:55:44 +0000524 isa<TemplateTypeParmDecl>(this) ||
Argyrios Kyrtzidise1778632010-09-08 21:58:42 +0000525 isa<NonTypeTemplateParmDecl>(this) ||
Douglas Gregor2b76dd92010-02-22 17:53:38 +0000526 !isa<CXXRecordDecl>(getDeclContext()) ||
Argyrios Kyrtzidis260b4a82010-09-08 21:32:35 +0000527 isInvalidDecl() ||
528 isa<StaticAssertDecl>(this) ||
529 // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
530 // as DeclContext (?).
Argyrios Kyrtzidise1778632010-09-08 21:58:42 +0000531 isa<ParmVarDecl>(this) ||
532 // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
533 // AS_none as access specifier.
534 isa<CXXRecordDecl>(this))
Anders Carlssonadf36b22009-08-29 20:47:47 +0000535 return;
Mike Stump11289f42009-09-09 15:08:12 +0000536
537 assert(Access != AS_none &&
Anders Carlssona28908d2009-03-25 23:38:06 +0000538 "Access specifier is AS_none inside a record decl");
Douglas Gregor4b00d3b2010-12-02 00:22:25 +0000539#endif
Anders Carlssona28908d2009-03-25 23:38:06 +0000540}
541
Anders Carlssona28908d2009-03-25 23:38:06 +0000542
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000543//===----------------------------------------------------------------------===//
544// DeclContext Implementation
545//===----------------------------------------------------------------------===//
546
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000547bool DeclContext::classof(const Decl *D) {
548 switch (D->getKind()) {
Alexis Hunted053252010-05-30 07:21:58 +0000549#define DECL(NAME, BASE)
550#define DECL_CONTEXT(NAME) case Decl::NAME:
551#define DECL_CONTEXT_BASE(NAME)
552#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000553 return true;
554 default:
Alexis Hunted053252010-05-30 07:21:58 +0000555#define DECL(NAME, BASE)
556#define DECL_CONTEXT_BASE(NAME) \
557 if (D->getKind() >= Decl::first##NAME && \
558 D->getKind() <= Decl::last##NAME) \
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000559 return true;
Alexis Hunted053252010-05-30 07:21:58 +0000560#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000561 return false;
562 }
563}
564
Douglas Gregor9c832f72010-07-25 18:38:02 +0000565DeclContext::~DeclContext() { }
Douglas Gregor91f84212008-12-11 16:49:14 +0000566
Douglas Gregor7f737c02009-09-10 16:57:35 +0000567/// \brief Find the parent context of this context that will be
568/// used for unqualified name lookup.
569///
570/// Generally, the parent lookup context is the semantic context. However, for
571/// a friend function the parent lookup context is the lexical context, which
572/// is the class in which the friend is declared.
573DeclContext *DeclContext::getLookupParent() {
574 // FIXME: Find a better way to identify friends
575 if (isa<FunctionDecl>(this))
Sebastian Redl50c68252010-08-31 00:36:30 +0000576 if (getParent()->getRedeclContext()->isFileContext() &&
577 getLexicalParent()->getRedeclContext()->isRecord())
Douglas Gregor7f737c02009-09-10 16:57:35 +0000578 return getLexicalParent();
579
580 return getParent();
581}
582
Sebastian Redlbd595762010-08-31 20:53:31 +0000583bool DeclContext::isInlineNamespace() const {
584 return isNamespace() &&
585 cast<NamespaceDecl>(this)->isInline();
586}
587
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000588bool DeclContext::isDependentContext() const {
589 if (isFileContext())
590 return false;
591
Douglas Gregor2373c592009-05-31 09:31:02 +0000592 if (isa<ClassTemplatePartialSpecializationDecl>(this))
593 return true;
594
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000595 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
596 if (Record->getDescribedClassTemplate())
597 return true;
598
John McCallc62bb642010-03-24 05:22:00 +0000599 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000600 if (Function->getDescribedFunctionTemplate())
601 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000602
John McCallc62bb642010-03-24 05:22:00 +0000603 // Friend function declarations are dependent if their *lexical*
604 // context is dependent.
605 if (cast<Decl>(this)->getFriendObjectKind())
606 return getLexicalParent()->isDependentContext();
607 }
608
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000609 return getParent() && getParent()->isDependentContext();
610}
611
Douglas Gregor07665a62009-01-05 19:45:36 +0000612bool DeclContext::isTransparentContext() const {
613 if (DeclKind == Decl::Enum)
Douglas Gregor0bf31402010-10-08 23:50:27 +0000614 return !cast<EnumDecl>(this)->isScoped();
Douglas Gregor07665a62009-01-05 19:45:36 +0000615 else if (DeclKind == Decl::LinkageSpec)
616 return true;
Douglas Gregor07665a62009-01-05 19:45:36 +0000617
618 return false;
619}
620
John McCall5fe84122010-10-26 04:59:26 +0000621bool DeclContext::isExternCContext() const {
622 const DeclContext *DC = this;
623 while (DC->DeclKind != Decl::TranslationUnit) {
624 if (DC->DeclKind == Decl::LinkageSpec)
625 return cast<LinkageSpecDecl>(DC)->getLanguage()
626 == LinkageSpecDecl::lang_c;
627 DC = DC->getParent();
628 }
629 return false;
630}
631
Sebastian Redl50c68252010-08-31 00:36:30 +0000632bool DeclContext::Encloses(const DeclContext *DC) const {
Douglas Gregore985a3b2009-08-27 06:03:53 +0000633 if (getPrimaryContext() != this)
634 return getPrimaryContext()->Encloses(DC);
Mike Stump11289f42009-09-09 15:08:12 +0000635
Douglas Gregore985a3b2009-08-27 06:03:53 +0000636 for (; DC; DC = DC->getParent())
637 if (DC->getPrimaryContext() == this)
638 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000639 return false;
Douglas Gregore985a3b2009-08-27 06:03:53 +0000640}
641
Steve Naroff35c62ae2009-01-08 17:28:14 +0000642DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor91f84212008-12-11 16:49:14 +0000643 switch (DeclKind) {
Douglas Gregor91f84212008-12-11 16:49:14 +0000644 case Decl::TranslationUnit:
Douglas Gregor07665a62009-01-05 19:45:36 +0000645 case Decl::LinkageSpec:
Mike Stump11289f42009-09-09 15:08:12 +0000646 case Decl::Block:
Douglas Gregor91f84212008-12-11 16:49:14 +0000647 // There is only one DeclContext for these entities.
648 return this;
649
650 case Decl::Namespace:
651 // The original namespace is our primary context.
652 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
653
Douglas Gregor91f84212008-12-11 16:49:14 +0000654 case Decl::ObjCMethod:
655 return this;
656
657 case Decl::ObjCInterface:
Steve Naroff35c62ae2009-01-08 17:28:14 +0000658 case Decl::ObjCProtocol:
659 case Decl::ObjCCategory:
Douglas Gregor91f84212008-12-11 16:49:14 +0000660 // FIXME: Can Objective-C interfaces be forward-declared?
661 return this;
662
Steve Naroff35c62ae2009-01-08 17:28:14 +0000663 case Decl::ObjCImplementation:
664 case Decl::ObjCCategoryImpl:
665 return this;
666
Douglas Gregor91f84212008-12-11 16:49:14 +0000667 default:
Alexis Hunted053252010-05-30 07:21:58 +0000668 if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
Douglas Gregor67a65642009-02-17 23:15:12 +0000669 // If this is a tag type that has a definition or is currently
670 // being defined, that definition is our primary context.
John McCalle78aac42010-03-10 03:28:59 +0000671 TagDecl *Tag = cast<TagDecl>(this);
672 assert(isa<TagType>(Tag->TypeForDecl) ||
673 isa<InjectedClassNameType>(Tag->TypeForDecl));
674
675 if (TagDecl *Def = Tag->getDefinition())
676 return Def;
677
678 if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) {
679 const TagType *TagTy = cast<TagType>(Tag->TypeForDecl);
680 if (TagTy->isBeingDefined())
681 // FIXME: is it necessarily being defined in the decl
682 // that owns the type?
683 return TagTy->getDecl();
684 }
685
686 return Tag;
Douglas Gregor67a65642009-02-17 23:15:12 +0000687 }
688
Alexis Hunted053252010-05-30 07:21:58 +0000689 assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
Douglas Gregor91f84212008-12-11 16:49:14 +0000690 "Unknown DeclContext kind");
691 return this;
692 }
693}
694
695DeclContext *DeclContext::getNextContext() {
696 switch (DeclKind) {
Douglas Gregor91f84212008-12-11 16:49:14 +0000697 case Decl::Namespace:
698 // Return the next namespace
699 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
700
701 default:
Douglas Gregor91f84212008-12-11 16:49:14 +0000702 return 0;
703 }
704}
705
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000706std::pair<Decl *, Decl *>
707DeclContext::BuildDeclChain(const llvm::SmallVectorImpl<Decl*> &Decls) {
708 // Build up a chain of declarations via the Decl::NextDeclInContext field.
709 Decl *FirstNewDecl = 0;
710 Decl *PrevDecl = 0;
711 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
712 Decl *D = Decls[I];
713 if (PrevDecl)
714 PrevDecl->NextDeclInContext = D;
715 else
716 FirstNewDecl = D;
717
718 PrevDecl = D;
719 }
720
721 return std::make_pair(FirstNewDecl, PrevDecl);
722}
723
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000724/// \brief Load the declarations within this lexical storage from an
725/// external source.
Mike Stump11289f42009-09-09 15:08:12 +0000726void
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000727DeclContext::LoadLexicalDeclsFromExternalStorage() const {
728 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000729 assert(hasExternalLexicalStorage() && Source && "No external storage?");
730
Argyrios Kyrtzidis98d045e2010-07-30 10:03:23 +0000731 // Notify that we have a DeclContext that is initializing.
732 ExternalASTSource::Deserializing ADeclContext(Source);
733
John McCall75b960e2010-06-01 09:23:16 +0000734 llvm::SmallVector<Decl*, 64> Decls;
735 if (Source->FindExternalLexicalDecls(this, Decls))
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000736 return;
737
738 // There is no longer any lexical storage in this context
739 ExternalLexicalStorage = false;
740
741 if (Decls.empty())
742 return;
743
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000744 // We may have already loaded just the fields of this record, in which case
745 // don't add the decls, just replace the FirstDecl/LastDecl chain.
746 if (const RecordDecl *RD = dyn_cast<RecordDecl>(this))
747 if (RD->LoadedFieldsFromExternalStorage) {
748 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
749 return;
750 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000751
752 // Splice the newly-read declarations into the beginning of the list
753 // of declarations.
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000754 Decl *ExternalFirst, *ExternalLast;
755 llvm::tie(ExternalFirst, ExternalLast) = BuildDeclChain(Decls);
756 ExternalLast->NextDeclInContext = FirstDecl;
757 FirstDecl = ExternalFirst;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000758 if (!LastDecl)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000759 LastDecl = ExternalLast;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000760}
761
John McCall75b960e2010-06-01 09:23:16 +0000762DeclContext::lookup_result
763ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
764 DeclarationName Name) {
765 ASTContext &Context = DC->getParentASTContext();
766 StoredDeclsMap *Map;
767 if (!(Map = DC->LookupPtr))
768 Map = DC->CreateStoredDeclsMap(Context);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000769
John McCall75b960e2010-06-01 09:23:16 +0000770 StoredDeclsList &List = (*Map)[Name];
771 assert(List.isNull());
772 (void) List;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000773
John McCall75b960e2010-06-01 09:23:16 +0000774 return DeclContext::lookup_result();
775}
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000776
John McCall75b960e2010-06-01 09:23:16 +0000777DeclContext::lookup_result
778ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +0000779 DeclarationName Name,
780 llvm::SmallVectorImpl<NamedDecl*> &Decls) {
781 ASTContext &Context = DC->getParentASTContext();;
782
783 StoredDeclsMap *Map;
784 if (!(Map = DC->LookupPtr))
785 Map = DC->CreateStoredDeclsMap(Context);
786
787 StoredDeclsList &List = (*Map)[Name];
788 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
789 if (List.isNull())
790 List.setOnlyValue(Decls[I]);
791 else
792 List.AddSubsequentDecl(Decls[I]);
793 }
794
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000795 return List.getLookupResult();
John McCall75b960e2010-06-01 09:23:16 +0000796}
797
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +0000798void ExternalASTSource::MaterializeVisibleDeclsForName(const DeclContext *DC,
799 DeclarationName Name,
800 llvm::SmallVectorImpl<NamedDecl*> &Decls) {
801 assert(DC->LookupPtr);
802 StoredDeclsMap &Map = *DC->LookupPtr;
803
804 // If there's an entry in the table the visible decls for this name have
805 // already been deserialized.
806 if (Map.find(Name) == Map.end()) {
807 StoredDeclsList &List = Map[Name];
808 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
809 if (List.isNull())
810 List.setOnlyValue(Decls[I]);
811 else
812 List.AddSubsequentDecl(Decls[I]);
813 }
814 }
815}
816
Sebastian Redl66c5eef2010-07-27 00:17:23 +0000817DeclContext::decl_iterator DeclContext::noload_decls_begin() const {
818 return decl_iterator(FirstDecl);
819}
820
821DeclContext::decl_iterator DeclContext::noload_decls_end() const {
822 return decl_iterator();
823}
824
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000825DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000826 if (hasExternalLexicalStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000827 LoadLexicalDeclsFromExternalStorage();
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000828
829 // FIXME: Check whether we need to load some declarations from
830 // external storage.
Mike Stump11289f42009-09-09 15:08:12 +0000831 return decl_iterator(FirstDecl);
Douglas Gregorbcced4e2009-04-09 21:40:53 +0000832}
833
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000834DeclContext::decl_iterator DeclContext::decls_end() const {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000835 if (hasExternalLexicalStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000836 LoadLexicalDeclsFromExternalStorage();
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000837
Mike Stump11289f42009-09-09 15:08:12 +0000838 return decl_iterator();
Douglas Gregorbcced4e2009-04-09 21:40:53 +0000839}
840
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000841bool DeclContext::decls_empty() const {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +0000842 if (hasExternalLexicalStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000843 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +0000844
845 return !FirstDecl;
846}
847
John McCall84d87672009-12-10 09:41:52 +0000848void DeclContext::removeDecl(Decl *D) {
849 assert(D->getLexicalDeclContext() == this &&
850 "decl being removed from non-lexical context");
851 assert((D->NextDeclInContext || D == LastDecl) &&
852 "decl is not in decls list");
853
854 // Remove D from the decl chain. This is O(n) but hopefully rare.
855 if (D == FirstDecl) {
856 if (D == LastDecl)
857 FirstDecl = LastDecl = 0;
858 else
859 FirstDecl = D->NextDeclInContext;
860 } else {
861 for (Decl *I = FirstDecl; true; I = I->NextDeclInContext) {
862 assert(I && "decl not found in linked list");
863 if (I->NextDeclInContext == D) {
864 I->NextDeclInContext = D->NextDeclInContext;
865 if (D == LastDecl) LastDecl = I;
866 break;
867 }
868 }
869 }
870
871 // Mark that D is no longer in the decl chain.
872 D->NextDeclInContext = 0;
873
874 // Remove D from the lookup table if necessary.
875 if (isa<NamedDecl>(D)) {
876 NamedDecl *ND = cast<NamedDecl>(D);
877
John McCallc62bb642010-03-24 05:22:00 +0000878 StoredDeclsMap *Map = getPrimaryContext()->LookupPtr;
879 if (!Map) return;
John McCall84d87672009-12-10 09:41:52 +0000880
John McCall84d87672009-12-10 09:41:52 +0000881 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
882 assert(Pos != Map->end() && "no lookup entry for decl");
883 Pos->second.remove(ND);
884 }
885}
886
John McCalld1e9d832009-08-11 06:59:38 +0000887void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner33f219d2009-02-20 00:56:18 +0000888 assert(D->getLexicalDeclContext() == this &&
889 "Decl inserted into wrong lexical context");
Mike Stump11289f42009-09-09 15:08:12 +0000890 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor020713e2009-01-09 19:42:16 +0000891 "Decl already inserted into a DeclContext");
892
893 if (FirstDecl) {
Chris Lattnerfcd33a62009-03-28 06:04:26 +0000894 LastDecl->NextDeclInContext = D;
Douglas Gregor020713e2009-01-09 19:42:16 +0000895 LastDecl = D;
896 } else {
897 FirstDecl = LastDecl = D;
898 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000899
900 // Notify a C++ record declaration that we've added a member, so it can
901 // update it's class-specific state.
902 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
903 Record->addedMember(D);
John McCalld1e9d832009-08-11 06:59:38 +0000904}
905
906void DeclContext::addDecl(Decl *D) {
907 addHiddenDecl(D);
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000908
909 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000910 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor91f84212008-12-11 16:49:14 +0000911}
912
Douglas Gregor07665a62009-01-05 19:45:36 +0000913/// buildLookup - Build the lookup data structure with all of the
914/// declarations in DCtx (and any other contexts linked to it or
915/// transparent contexts nested within it).
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000916void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregor07665a62009-01-05 19:45:36 +0000917 for (; DCtx; DCtx = DCtx->getNextContext()) {
Mike Stump11289f42009-09-09 15:08:12 +0000918 for (decl_iterator D = DCtx->decls_begin(),
919 DEnd = DCtx->decls_end();
Douglas Gregord05cb412009-01-06 07:17:58 +0000920 D != DEnd; ++D) {
John McCalld1e9d832009-08-11 06:59:38 +0000921 // Insert this declaration into the lookup structure, but only
922 // if it's semantically in its decl context. During non-lazy
923 // lookup building, this is implicitly enforced by addDecl.
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000924 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
John McCalld1e9d832009-08-11 06:59:38 +0000925 if (D->getDeclContext() == DCtx)
926 makeDeclVisibleInContextImpl(ND);
Douglas Gregor07665a62009-01-05 19:45:36 +0000927
Ted Kremenek707ece62009-11-17 22:58:30 +0000928 // Insert any forward-declared Objective-C interfaces into the lookup
929 // data structure.
930 if (ObjCClassDecl *Class = dyn_cast<ObjCClassDecl>(*D))
931 for (ObjCClassDecl::iterator I = Class->begin(), IEnd = Class->end();
932 I != IEnd; ++I)
Ted Kremenek9b124e12009-11-18 00:28:11 +0000933 makeDeclVisibleInContextImpl(I->getInterface());
Ted Kremenek707ece62009-11-17 22:58:30 +0000934
Sebastian Redlbd595762010-08-31 20:53:31 +0000935 // If this declaration is itself a transparent declaration context or
936 // inline namespace, add its members (recursively).
Douglas Gregor07665a62009-01-05 19:45:36 +0000937 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
Sebastian Redlbd595762010-08-31 20:53:31 +0000938 if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000939 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregor07665a62009-01-05 19:45:36 +0000940 }
941 }
942}
943
Mike Stump11289f42009-09-09 15:08:12 +0000944DeclContext::lookup_result
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000945DeclContext::lookup(DeclarationName Name) {
Steve Naroff35c62ae2009-01-08 17:28:14 +0000946 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor91f84212008-12-11 16:49:14 +0000947 if (PrimaryContext != this)
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000948 return PrimaryContext->lookup(Name);
Douglas Gregor91f84212008-12-11 16:49:14 +0000949
John McCall75b960e2010-06-01 09:23:16 +0000950 if (hasExternalVisibleStorage()) {
951 // Check to see if we've already cached the lookup results.
952 if (LookupPtr) {
953 StoredDeclsMap::iterator I = LookupPtr->find(Name);
954 if (I != LookupPtr->end())
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000955 return I->second.getLookupResult();
John McCall75b960e2010-06-01 09:23:16 +0000956 }
957
958 ExternalASTSource *Source = getParentASTContext().getExternalSource();
959 return Source->FindExternalVisibleDeclsByName(this, Name);
960 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000961
Douglas Gregor55297ac2008-12-23 00:26:44 +0000962 /// If there is no lookup data structure, build one now by walking
Douglas Gregor91f84212008-12-11 16:49:14 +0000963 /// all of the linked DeclContexts (in declaration order!) and
964 /// inserting their values.
Douglas Gregor9615ec22009-04-09 17:29:08 +0000965 if (!LookupPtr) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000966 buildLookup(this);
Douglas Gregor91f84212008-12-11 16:49:14 +0000967
Douglas Gregor9615ec22009-04-09 17:29:08 +0000968 if (!LookupPtr)
Douglas Gregor10dc8aa2010-05-11 06:18:17 +0000969 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Douglas Gregor9615ec22009-04-09 17:29:08 +0000970 }
Douglas Gregor91f84212008-12-11 16:49:14 +0000971
John McCallc62bb642010-03-24 05:22:00 +0000972 StoredDeclsMap::iterator Pos = LookupPtr->find(Name);
973 if (Pos == LookupPtr->end())
Douglas Gregor10dc8aa2010-05-11 06:18:17 +0000974 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000975 return Pos->second.getLookupResult();
Douglas Gregor91f84212008-12-11 16:49:14 +0000976}
977
Mike Stump11289f42009-09-09 15:08:12 +0000978DeclContext::lookup_const_result
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000979DeclContext::lookup(DeclarationName Name) const {
980 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor91f84212008-12-11 16:49:14 +0000981}
982
Sebastian Redl50c68252010-08-31 00:36:30 +0000983DeclContext *DeclContext::getRedeclContext() {
Chris Lattner17a1bfa2009-03-27 19:19:59 +0000984 DeclContext *Ctx = this;
Sebastian Redlbd595762010-08-31 20:53:31 +0000985 // Skip through transparent contexts.
986 while (Ctx->isTransparentContext())
Douglas Gregor6ad0ef52009-01-06 23:51:29 +0000987 Ctx = Ctx->getParent();
988 return Ctx;
989}
990
Douglas Gregorf47b9112009-02-25 22:02:03 +0000991DeclContext *DeclContext::getEnclosingNamespaceContext() {
992 DeclContext *Ctx = this;
993 // Skip through non-namespace, non-translation-unit contexts.
Sebastian Redl4f08c962010-08-31 00:36:23 +0000994 while (!Ctx->isFileContext())
Douglas Gregorf47b9112009-02-25 22:02:03 +0000995 Ctx = Ctx->getParent();
996 return Ctx->getPrimaryContext();
997}
998
Sebastian Redl50c68252010-08-31 00:36:30 +0000999bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
1000 // For non-file contexts, this is equivalent to Equals.
1001 if (!isFileContext())
1002 return O->Equals(this);
1003
1004 do {
1005 if (O->Equals(this))
1006 return true;
1007
1008 const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O);
1009 if (!NS || !NS->isInline())
1010 break;
1011 O = NS->getParent();
1012 } while (O);
1013
1014 return false;
1015}
1016
John McCall759e32b2009-08-31 22:39:49 +00001017void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable) {
Douglas Gregor67a65642009-02-17 23:15:12 +00001018 // FIXME: This feels like a hack. Should DeclarationName support
1019 // template-ids, or is there a better way to keep specializations
1020 // from being visible?
1021 if (isa<ClassTemplateSpecializationDecl>(D))
1022 return;
Eli Friedman73168192009-12-08 05:40:03 +00001023 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1024 if (FD->isFunctionTemplateSpecialization())
1025 return;
Douglas Gregor67a65642009-02-17 23:15:12 +00001026
Steve Naroff35c62ae2009-01-08 17:28:14 +00001027 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor91f84212008-12-11 16:49:14 +00001028 if (PrimaryContext != this) {
John McCall759e32b2009-08-31 22:39:49 +00001029 PrimaryContext->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor91f84212008-12-11 16:49:14 +00001030 return;
1031 }
1032
1033 // If we already have a lookup data structure, perform the insertion
Argyrios Kyrtzidise51e5542010-07-04 21:44:25 +00001034 // into it. If we haven't deserialized externally stored decls, deserialize
1035 // them so we can add the decl. Otherwise, be lazy and don't build that
1036 // structure until someone asks for it.
1037 if (LookupPtr || !Recoverable || hasExternalVisibleStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001038 makeDeclVisibleInContextImpl(D);
Douglas Gregor07665a62009-01-05 19:45:36 +00001039
Sebastian Redlbd595762010-08-31 20:53:31 +00001040 // If we are a transparent context or inline namespace, insert into our
1041 // parent context, too. This operation is recursive.
1042 if (isTransparentContext() || isInlineNamespace())
John McCall759e32b2009-08-31 22:39:49 +00001043 getParent()->makeDeclVisibleInContext(D, Recoverable);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00001044
1045 Decl *DCAsDecl = cast<Decl>(this);
1046 // Notify that a decl was made visible unless it's a Tag being defined.
1047 if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined()))
1048 if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
1049 L->AddedVisibleDecl(this, D);
Douglas Gregor91f84212008-12-11 16:49:14 +00001050}
1051
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001052void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) {
Douglas Gregor07665a62009-01-05 19:45:36 +00001053 // Skip unnamed declarations.
1054 if (!D->getDeclName())
1055 return;
1056
Douglas Gregor67a65642009-02-17 23:15:12 +00001057 // FIXME: This feels like a hack. Should DeclarationName support
1058 // template-ids, or is there a better way to keep specializations
1059 // from being visible?
1060 if (isa<ClassTemplateSpecializationDecl>(D))
1061 return;
1062
Argyrios Kyrtzidise51e5542010-07-04 21:44:25 +00001063 ASTContext *C = 0;
1064 if (!LookupPtr) {
1065 C = &getParentASTContext();
1066 CreateStoredDeclsMap(*C);
1067 }
1068
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001069 // If there is an external AST source, load any declarations it knows about
1070 // with this declaration's name.
1071 // If the lookup table contains an entry about this name it means that we
1072 // have already checked the external source.
1073 if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
1074 if (hasExternalVisibleStorage() &&
1075 LookupPtr->find(D->getDeclName()) == LookupPtr->end())
1076 Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
1077
Douglas Gregor91f84212008-12-11 16:49:14 +00001078 // Insert this declaration into the map.
John McCallc62bb642010-03-24 05:22:00 +00001079 StoredDeclsList &DeclNameEntries = (*LookupPtr)[D->getDeclName()];
Chris Lattnercaae7162009-02-20 01:44:05 +00001080 if (DeclNameEntries.isNull()) {
1081 DeclNameEntries.setOnlyValue(D);
Chris Lattnerdfd6b3d2009-02-19 07:00:44 +00001082 return;
Douglas Gregor91f84212008-12-11 16:49:14 +00001083 }
Chris Lattner24e24d52009-02-20 00:55:03 +00001084
Chris Lattner29578f32009-02-20 01:10:07 +00001085 // If it is possible that this is a redeclaration, check to see if there is
1086 // already a decl for which declarationReplaces returns true. If there is
1087 // one, just replace it and return.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001088 if (DeclNameEntries.HandleRedeclaration(D))
Chris Lattnercaae7162009-02-20 01:44:05 +00001089 return;
Mike Stump11289f42009-09-09 15:08:12 +00001090
Chris Lattnerdfd6b3d2009-02-19 07:00:44 +00001091 // Put this declaration into the appropriate slot.
Chris Lattnercaae7162009-02-20 01:44:05 +00001092 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor91f84212008-12-11 16:49:14 +00001093}
Douglas Gregor889ceb72009-02-03 19:21:40 +00001094
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00001095void DeclContext::MaterializeVisibleDeclsFromExternalStorage() {
1096 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1097 assert(hasExternalVisibleStorage() && Source && "No external storage?");
1098
1099 if (!LookupPtr)
1100 CreateStoredDeclsMap(getParentASTContext());
1101 Source->MaterializeVisibleDecls(this);
1102}
1103
Douglas Gregor889ceb72009-02-03 19:21:40 +00001104/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
1105/// this context.
Mike Stump11289f42009-09-09 15:08:12 +00001106DeclContext::udir_iterator_range
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001107DeclContext::getUsingDirectives() const {
1108 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor889ceb72009-02-03 19:21:40 +00001109 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
1110 reinterpret_cast<udir_iterator>(Result.second));
1111}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001112
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001113//===----------------------------------------------------------------------===//
1114// Creation and Destruction of StoredDeclsMaps. //
1115//===----------------------------------------------------------------------===//
1116
John McCallc62bb642010-03-24 05:22:00 +00001117StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
1118 assert(!LookupPtr && "context already has a decls map");
1119 assert(getPrimaryContext() == this &&
1120 "creating decls map on non-primary context");
1121
1122 StoredDeclsMap *M;
1123 bool Dependent = isDependentContext();
1124 if (Dependent)
1125 M = new DependentStoredDeclsMap();
1126 else
1127 M = new StoredDeclsMap();
1128 M->Previous = C.LastSDM;
1129 C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
1130 LookupPtr = M;
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001131 return M;
1132}
1133
1134void ASTContext::ReleaseDeclContextMaps() {
John McCallc62bb642010-03-24 05:22:00 +00001135 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1136 // pointer because the subclass doesn't add anything that needs to
1137 // be deleted.
John McCallc62bb642010-03-24 05:22:00 +00001138 StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1139}
1140
1141void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1142 while (Map) {
1143 // Advance the iteration before we invalidate memory.
1144 llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1145
1146 if (Dependent)
1147 delete static_cast<DependentStoredDeclsMap*>(Map);
1148 else
1149 delete Map;
1150
1151 Map = Next.getPointer();
1152 Dependent = Next.getInt();
1153 }
1154}
1155
John McCallc62bb642010-03-24 05:22:00 +00001156DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1157 DeclContext *Parent,
1158 const PartialDiagnostic &PDiag) {
1159 assert(Parent->isDependentContext()
1160 && "cannot iterate dependent diagnostics of non-dependent context");
1161 Parent = Parent->getPrimaryContext();
1162 if (!Parent->LookupPtr)
1163 Parent->CreateStoredDeclsMap(C);
1164
1165 DependentStoredDeclsMap *Map
1166 = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr);
1167
Douglas Gregora55530e2010-03-29 23:56:53 +00001168 // Allocate the copy of the PartialDiagnostic via the ASTContext's
Douglas Gregor89336232010-03-29 23:34:08 +00001169 // BumpPtrAllocator, rather than the ASTContext itself.
Douglas Gregora55530e2010-03-29 23:56:53 +00001170 PartialDiagnostic::Storage *DiagStorage = 0;
1171 if (PDiag.hasStorage())
1172 DiagStorage = new (C) PartialDiagnostic::Storage;
1173
1174 DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
John McCallc62bb642010-03-24 05:22:00 +00001175
1176 // TODO: Maybe we shouldn't reverse the order during insertion.
1177 DD->NextDiagnostic = Map->FirstDiagnostic;
1178 Map->FirstDiagnostic = DD;
1179
1180 return DD;
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001181}