blob: 5e5ec93e17ebd808ca3c59d54530e7e48db8541f [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"
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000028#include "clang/Basic/TargetInfo.h"
Eli Friedman7dbab8a2008-06-07 16:52:53 +000029#include "llvm/ADT/DenseMap.h"
Chris Lattnereae6cb62009-03-05 08:00:35 +000030#include "llvm/Support/raw_ostream.h"
Douglas Gregor8b9ccca2008-12-23 21:05:05 +000031#include <algorithm>
Eli Friedman7dbab8a2008-06-07 16:52:53 +000032using namespace clang;
33
34//===----------------------------------------------------------------------===//
35// Statistics
36//===----------------------------------------------------------------------===//
37
Alexis Hunted053252010-05-30 07:21:58 +000038#define DECL(DERIVED, BASE) static int n##DERIVED##s = 0;
39#define ABSTRACT_DECL(DECL)
40#include "clang/AST/DeclNodes.inc"
Eli Friedman7dbab8a2008-06-07 16:52:53 +000041
42static bool StatSwitch = false;
43
Eli Friedman7dbab8a2008-06-07 16:52:53 +000044const char *Decl::getDeclKindName() const {
45 switch (DeclKind) {
Alexis Hunted053252010-05-30 07:21:58 +000046 default: assert(0 && "Declaration not in DeclNodes.inc!");
47#define DECL(DERIVED, BASE) case DERIVED: return #DERIVED;
48#define ABSTRACT_DECL(DECL)
49#include "clang/AST/DeclNodes.inc"
Eli Friedman7dbab8a2008-06-07 16:52:53 +000050 }
51}
52
Douglas Gregor90d47172010-03-05 00:26:45 +000053void Decl::setInvalidDecl(bool Invalid) {
54 InvalidDecl = Invalid;
55 if (Invalid) {
56 // Defensive maneuver for ill-formed code: we're likely not to make it to
57 // a point where we set the access specifier, so default it to "public"
58 // to avoid triggering asserts elsewhere in the front end.
59 setAccess(AS_public);
60 }
61}
62
Steve Naroff5faaef72009-01-20 19:53:53 +000063const char *DeclContext::getDeclKindName() const {
64 switch (DeclKind) {
Alexis Hunted053252010-05-30 07:21:58 +000065 default: assert(0 && "Declaration context not in DeclNodes.inc!");
66#define DECL(DERIVED, BASE) case Decl::DERIVED: return #DERIVED;
67#define ABSTRACT_DECL(DECL)
68#include "clang/AST/DeclNodes.inc"
Steve Naroff5faaef72009-01-20 19:53:53 +000069 }
70}
71
Eli Friedman7dbab8a2008-06-07 16:52:53 +000072bool Decl::CollectingStats(bool Enable) {
Kovarththanan Rajaratnam130f7f92009-11-29 14:54:35 +000073 if (Enable) StatSwitch = true;
Eli Friedman7dbab8a2008-06-07 16:52:53 +000074 return StatSwitch;
75}
76
77void Decl::PrintStats() {
Chandler Carruthbfb154a2011-07-04 06:13:27 +000078 llvm::errs() << "\n*** Decl Stats:\n";
Mike Stump11289f42009-09-09 15:08:12 +000079
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +000080 int totalDecls = 0;
Alexis Hunted053252010-05-30 07:21:58 +000081#define DECL(DERIVED, BASE) totalDecls += n##DERIVED##s;
82#define ABSTRACT_DECL(DECL)
83#include "clang/AST/DeclNodes.inc"
Chandler Carruthbfb154a2011-07-04 06:13:27 +000084 llvm::errs() << " " << totalDecls << " decls total.\n";
Mike Stump11289f42009-09-09 15:08:12 +000085
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +000086 int totalBytes = 0;
Alexis Hunted053252010-05-30 07:21:58 +000087#define DECL(DERIVED, BASE) \
88 if (n##DERIVED##s > 0) { \
89 totalBytes += (int)(n##DERIVED##s * sizeof(DERIVED##Decl)); \
Chandler Carruthbfb154a2011-07-04 06:13:27 +000090 llvm::errs() << " " << n##DERIVED##s << " " #DERIVED " decls, " \
91 << sizeof(DERIVED##Decl) << " each (" \
92 << n##DERIVED##s * sizeof(DERIVED##Decl) \
93 << " bytes)\n"; \
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
Chandler Carruthbfb154a2011-07-04 06:13:27 +000098 llvm::errs() << "Total bytes = " << totalBytes << "\n";
Eli Friedman7dbab8a2008-06-07 16:52:53 +000099}
100
Alexis Hunted053252010-05-30 07:21:58 +0000101void Decl::add(Kind k) {
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000102 switch (k) {
Alexis Hunted053252010-05-30 07:21:58 +0000103 default: assert(0 && "Declaration not in DeclNodes.inc!");
104#define DECL(DERIVED, BASE) case DERIVED: ++n##DERIVED##s; break;
105#define ABSTRACT_DECL(DECL)
106#include "clang/AST/DeclNodes.inc"
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000107 }
108}
109
Anders Carlssonaa73b912009-06-13 00:08:58 +0000110bool Decl::isTemplateParameterPack() const {
111 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
112 return TTP->isParameterPack();
Douglas Gregorda3cc0d2010-12-23 23:51:58 +0000113 if (const NonTypeTemplateParmDecl *NTTP
Douglas Gregorf5500772011-01-05 15:48:55 +0000114 = dyn_cast<NonTypeTemplateParmDecl>(this))
Douglas Gregorda3cc0d2010-12-23 23:51:58 +0000115 return NTTP->isParameterPack();
Douglas Gregorf5500772011-01-05 15:48:55 +0000116 if (const TemplateTemplateParmDecl *TTP
117 = dyn_cast<TemplateTemplateParmDecl>(this))
118 return TTP->isParameterPack();
Anders Carlssonaa73b912009-06-13 00:08:58 +0000119 return false;
120}
121
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +0000122bool Decl::isParameterPack() const {
123 if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(this))
124 return Parm->isParameterPack();
125
126 return isTemplateParameterPack();
127}
128
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000129bool Decl::isFunctionOrFunctionTemplate() const {
John McCall3f746822009-11-17 05:59:44 +0000130 if (const UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(this))
Anders Carlssonf057cb22009-06-26 05:26:50 +0000131 return UD->getTargetDecl()->isFunctionOrFunctionTemplate();
Mike Stump11289f42009-09-09 15:08:12 +0000132
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000133 return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this);
134}
135
Douglas Gregorf5974fa2010-01-16 20:21:20 +0000136bool Decl::isDefinedOutsideFunctionOrMethod() const {
137 for (const DeclContext *DC = getDeclContext();
138 DC && !DC->isTranslationUnit();
139 DC = DC->getParent())
140 if (DC->isFunctionOrMethod())
141 return false;
142
143 return true;
144}
145
Douglas Gregor133eddd2011-02-17 08:47:29 +0000146
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000147//===----------------------------------------------------------------------===//
Chris Lattnereae6cb62009-03-05 08:00:35 +0000148// PrettyStackTraceDecl Implementation
149//===----------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000150
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000151void PrettyStackTraceDecl::print(raw_ostream &OS) const {
Chris Lattnereae6cb62009-03-05 08:00:35 +0000152 SourceLocation TheLoc = Loc;
153 if (TheLoc.isInvalid() && TheDecl)
154 TheLoc = TheDecl->getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000155
Chris Lattnereae6cb62009-03-05 08:00:35 +0000156 if (TheLoc.isValid()) {
157 TheLoc.print(OS, SM);
158 OS << ": ";
159 }
160
161 OS << Message;
162
Daniel Dunbar4f1054e2009-11-21 09:05:59 +0000163 if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
Chris Lattnereae6cb62009-03-05 08:00:35 +0000164 OS << " '" << DN->getQualifiedNameAsString() << '\'';
165 OS << '\n';
166}
Mike Stump11289f42009-09-09 15:08:12 +0000167
Chris Lattnereae6cb62009-03-05 08:00:35 +0000168//===----------------------------------------------------------------------===//
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000169// Decl Implementation
170//===----------------------------------------------------------------------===//
171
Douglas Gregorb11aad82011-02-19 18:51:44 +0000172// Out-of-line virtual method providing a home for Decl.
173Decl::~Decl() { }
Douglas Gregora43942a2011-02-17 07:02:32 +0000174
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000175void Decl::setDeclContext(DeclContext *DC) {
Chris Lattnerb81eb052009-03-29 06:06:59 +0000176 DeclCtx = DC;
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000177}
178
179void Decl::setLexicalDeclContext(DeclContext *DC) {
180 if (DC == getLexicalDeclContext())
181 return;
182
183 if (isInSemaDC()) {
Ted Kremenekf8c12a32009-12-01 00:07:10 +0000184 MultipleDC *MDC = new (getASTContext()) MultipleDC();
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000185 MDC->SemanticDC = getDeclContext();
186 MDC->LexicalDC = DC;
Chris Lattnerb81eb052009-03-29 06:06:59 +0000187 DeclCtx = MDC;
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000188 } else {
189 getMultipleDC()->LexicalDC = DC;
190 }
191}
192
John McCall4fa53422009-10-01 00:25:31 +0000193bool Decl::isInAnonymousNamespace() const {
194 const DeclContext *DC = getDeclContext();
195 do {
196 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
197 if (ND->isAnonymousNamespace())
198 return true;
199 } while ((DC = DC->getParent()));
200
201 return false;
202}
203
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000204TranslationUnitDecl *Decl::getTranslationUnitDecl() {
Argyrios Kyrtzidis4e1a72b2009-06-30 02:34:53 +0000205 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
206 return TUD;
207
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000208 DeclContext *DC = getDeclContext();
209 assert(DC && "This decl is not contained in a translation unit!");
Mike Stump11289f42009-09-09 15:08:12 +0000210
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000211 while (!DC->isTranslationUnit()) {
212 DC = DC->getParent();
213 assert(DC && "This decl is not contained in a translation unit!");
214 }
Mike Stump11289f42009-09-09 15:08:12 +0000215
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000216 return cast<TranslationUnitDecl>(DC);
217}
218
219ASTContext &Decl::getASTContext() const {
Mike Stump11289f42009-09-09 15:08:12 +0000220 return getTranslationUnitDecl()->getASTContext();
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000221}
222
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +0000223ASTMutationListener *Decl::getASTMutationListener() const {
224 return getASTContext().getASTMutationListener();
225}
226
Douglas Gregorebada0772010-06-17 23:14:26 +0000227bool Decl::isUsed(bool CheckUsedAttr) const {
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000228 if (Used)
229 return true;
230
231 // Check for used attribute.
Douglas Gregorebada0772010-06-17 23:14:26 +0000232 if (CheckUsedAttr && hasAttr<UsedAttr>())
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000233 return true;
234
235 // Check redeclarations for used attribute.
236 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Douglas Gregorebada0772010-06-17 23:14:26 +0000237 if ((CheckUsedAttr && I->hasAttr<UsedAttr>()) || I->Used)
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000238 return true;
239 }
240
241 return false;
242}
243
Argyrios Kyrtzidis16180232011-04-19 19:51:10 +0000244bool Decl::isReferenced() const {
245 if (Referenced)
246 return true;
247
248 // Check redeclarations.
249 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
250 if (I->Referenced)
251 return true;
252
253 return false;
254}
255
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000256/// \brief Determine the availability of the given declaration based on
257/// the target platform.
258///
259/// When it returns an availability result other than \c AR_Available,
260/// if the \p Message parameter is non-NULL, it will be set to a
261/// string describing why the entity is unavailable.
262///
263/// FIXME: Make these strings localizable, since they end up in
264/// diagnostics.
265static AvailabilityResult CheckAvailability(ASTContext &Context,
266 const AvailabilityAttr *A,
267 std::string *Message) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000268 StringRef TargetPlatform = Context.Target.getPlatformName();
269 StringRef PrettyPlatformName
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000270 = AvailabilityAttr::getPrettyPlatformName(TargetPlatform);
271 if (PrettyPlatformName.empty())
272 PrettyPlatformName = TargetPlatform;
273
274 VersionTuple TargetMinVersion = Context.Target.getPlatformMinVersion();
275 if (TargetMinVersion.empty())
276 return AR_Available;
277
278 // Match the platform name.
279 if (A->getPlatform()->getName() != TargetPlatform)
280 return AR_Available;
281
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000282 // Make sure that this declaration has not been marked 'unavailable'.
283 if (A->getUnavailable()) {
284 if (Message) {
285 Message->clear();
286 llvm::raw_string_ostream Out(*Message);
287 Out << "not available on " << PrettyPlatformName;
288 }
289
290 return AR_Unavailable;
291 }
292
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000293 // Make sure that this declaration has already been introduced.
294 if (!A->getIntroduced().empty() &&
295 TargetMinVersion < A->getIntroduced()) {
296 if (Message) {
297 Message->clear();
298 llvm::raw_string_ostream Out(*Message);
299 Out << "introduced in " << PrettyPlatformName << ' '
300 << A->getIntroduced();
301 }
302
303 return AR_NotYetIntroduced;
304 }
305
306 // Make sure that this declaration hasn't been obsoleted.
307 if (!A->getObsoleted().empty() && TargetMinVersion >= A->getObsoleted()) {
308 if (Message) {
309 Message->clear();
310 llvm::raw_string_ostream Out(*Message);
311 Out << "obsoleted in " << PrettyPlatformName << ' '
312 << A->getObsoleted();
313 }
314
315 return AR_Unavailable;
316 }
317
318 // Make sure that this declaration hasn't been deprecated.
319 if (!A->getDeprecated().empty() && TargetMinVersion >= A->getDeprecated()) {
320 if (Message) {
321 Message->clear();
322 llvm::raw_string_ostream Out(*Message);
323 Out << "first deprecated in " << PrettyPlatformName << ' '
324 << A->getDeprecated();
325 }
326
327 return AR_Deprecated;
328 }
329
330 return AR_Available;
331}
332
333AvailabilityResult Decl::getAvailability(std::string *Message) const {
334 AvailabilityResult Result = AR_Available;
335 std::string ResultMessage;
336
337 for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
338 if (DeprecatedAttr *Deprecated = dyn_cast<DeprecatedAttr>(*A)) {
339 if (Result >= AR_Deprecated)
340 continue;
341
342 if (Message)
343 ResultMessage = Deprecated->getMessage();
344
345 Result = AR_Deprecated;
346 continue;
347 }
348
349 if (UnavailableAttr *Unavailable = dyn_cast<UnavailableAttr>(*A)) {
350 if (Message)
351 *Message = Unavailable->getMessage();
352 return AR_Unavailable;
353 }
354
355 if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
356 AvailabilityResult AR = CheckAvailability(getASTContext(), Availability,
357 Message);
358
359 if (AR == AR_Unavailable)
360 return AR_Unavailable;
361
362 if (AR > Result) {
363 Result = AR;
364 if (Message)
365 ResultMessage.swap(*Message);
366 }
367 continue;
368 }
369 }
370
371 if (Message)
372 Message->swap(ResultMessage);
373 return Result;
374}
375
376bool Decl::canBeWeakImported(bool &IsDefinition) const {
377 IsDefinition = false;
378 if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
379 if (!Var->hasExternalStorage() || Var->getInit()) {
380 IsDefinition = true;
381 return false;
382 }
383 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
384 if (FD->hasBody()) {
385 IsDefinition = true;
386 return false;
387 }
388 } else if (isa<ObjCPropertyDecl>(this) || isa<ObjCMethodDecl>(this))
389 return false;
390 else if (!(getASTContext().getLangOptions().ObjCNonFragileABI &&
391 isa<ObjCInterfaceDecl>(this)))
392 return false;
393
394 return true;
395}
396
397bool Decl::isWeakImported() const {
398 bool IsDefinition;
399 if (!canBeWeakImported(IsDefinition))
400 return false;
401
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000402 for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
403 if (isa<WeakImportAttr>(*A))
404 return true;
405
406 if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
407 if (CheckAvailability(getASTContext(), Availability, 0)
408 == AR_NotYetIntroduced)
409 return true;
410 }
411 }
412
413 return false;
414}
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000415
Chris Lattner8e097192009-03-27 20:18:19 +0000416unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
417 switch (DeclKind) {
John McCall3f746822009-11-17 05:59:44 +0000418 case Function:
419 case CXXMethod:
420 case CXXConstructor:
421 case CXXDestructor:
422 case CXXConversion:
Chris Lattner8e097192009-03-27 20:18:19 +0000423 case EnumConstant:
424 case Var:
425 case ImplicitParam:
426 case ParmVar:
Chris Lattner8e097192009-03-27 20:18:19 +0000427 case NonTypeTemplateParm:
428 case ObjCMethod:
Daniel Dunbar45b2d8a2010-04-23 13:07:39 +0000429 case ObjCProperty:
Daniel Dunbar45b2d8a2010-04-23 13:07:39 +0000430 return IDNS_Ordinary;
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000431 case Label:
432 return IDNS_Label;
Francois Pichet783dd6e2010-11-21 06:08:52 +0000433 case IndirectField:
434 return IDNS_Ordinary | IDNS_Member;
435
John McCalle87beb22010-04-23 18:46:30 +0000436 case ObjCCompatibleAlias:
437 case ObjCInterface:
438 return IDNS_Ordinary | IDNS_Type;
439
440 case Typedef:
Richard Smithdda56e42011-04-15 14:24:37 +0000441 case TypeAlias:
Richard Smith3f1b5d02011-05-05 21:57:07 +0000442 case TypeAliasTemplate:
John McCalle87beb22010-04-23 18:46:30 +0000443 case UnresolvedUsingTypename:
444 case TemplateTypeParm:
445 return IDNS_Ordinary | IDNS_Type;
446
John McCall3f746822009-11-17 05:59:44 +0000447 case UsingShadow:
448 return 0; // we'll actually overwrite this later
449
John McCalle61f2ba2009-11-18 02:36:19 +0000450 case UnresolvedUsingValue:
John McCalle61f2ba2009-11-18 02:36:19 +0000451 return IDNS_Ordinary | IDNS_Using;
John McCall3f746822009-11-17 05:59:44 +0000452
453 case Using:
454 return IDNS_Using;
455
Chris Lattner8e097192009-03-27 20:18:19 +0000456 case ObjCProtocol:
Douglas Gregor79947a22009-04-24 00:11:27 +0000457 return IDNS_ObjCProtocol;
Mike Stump11289f42009-09-09 15:08:12 +0000458
Chris Lattner8e097192009-03-27 20:18:19 +0000459 case Field:
460 case ObjCAtDefsField:
461 case ObjCIvar:
462 return IDNS_Member;
Mike Stump11289f42009-09-09 15:08:12 +0000463
Chris Lattner8e097192009-03-27 20:18:19 +0000464 case Record:
465 case CXXRecord:
466 case Enum:
John McCalle87beb22010-04-23 18:46:30 +0000467 return IDNS_Tag | IDNS_Type;
Mike Stump11289f42009-09-09 15:08:12 +0000468
Chris Lattner8e097192009-03-27 20:18:19 +0000469 case Namespace:
John McCalle87beb22010-04-23 18:46:30 +0000470 case NamespaceAlias:
471 return IDNS_Namespace;
472
Chris Lattner8e097192009-03-27 20:18:19 +0000473 case FunctionTemplate:
John McCalle87beb22010-04-23 18:46:30 +0000474 return IDNS_Ordinary;
475
Chris Lattner8e097192009-03-27 20:18:19 +0000476 case ClassTemplate:
477 case TemplateTemplateParm:
John McCalle87beb22010-04-23 18:46:30 +0000478 return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
Mike Stump11289f42009-09-09 15:08:12 +0000479
Chris Lattner8e097192009-03-27 20:18:19 +0000480 // Never have names.
John McCallaa74a0c2009-08-28 07:59:38 +0000481 case Friend:
John McCall11083da2009-09-16 22:47:08 +0000482 case FriendTemplate:
Abramo Bagnarad7340582010-06-05 05:09:32 +0000483 case AccessSpec:
Chris Lattner8e097192009-03-27 20:18:19 +0000484 case LinkageSpec:
485 case FileScopeAsm:
486 case StaticAssert:
487 case ObjCClass:
Chris Lattner8e097192009-03-27 20:18:19 +0000488 case ObjCPropertyImpl:
489 case ObjCForwardProtocol:
490 case Block:
491 case TranslationUnit:
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000492
Chris Lattner8e097192009-03-27 20:18:19 +0000493 case UsingDirective:
494 case ClassTemplateSpecialization:
Douglas Gregor2373c592009-05-31 09:31:02 +0000495 case ClassTemplatePartialSpecialization:
Francois Pichet00c7e6c2011-08-14 03:52:19 +0000496 case ClassScopeFunctionSpecialization:
Douglas Gregore93525e2010-04-22 23:19:50 +0000497 case ObjCImplementation:
498 case ObjCCategory:
499 case ObjCCategoryImpl:
500 // Never looked up by name.
Chris Lattner8e097192009-03-27 20:18:19 +0000501 return 0;
502 }
John McCall3f746822009-11-17 05:59:44 +0000503
504 return 0;
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000505}
506
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000507void Decl::setAttrs(const AttrVec &attrs) {
Argyrios Kyrtzidis91167172010-06-11 23:09:25 +0000508 assert(!HasAttrs && "Decl already contains attrs.");
509
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000510 AttrVec &AttrBlank = getASTContext().getDeclAttrs(this);
511 assert(AttrBlank.empty() && "HasAttrs was wrong?");
Argyrios Kyrtzidis91167172010-06-11 23:09:25 +0000512
513 AttrBlank = attrs;
514 HasAttrs = true;
515}
516
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000517void Decl::dropAttrs() {
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000518 if (!HasAttrs) return;
Mike Stump11289f42009-09-09 15:08:12 +0000519
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000520 HasAttrs = false;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000521 getASTContext().eraseDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000522}
523
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000524const AttrVec &Decl::getAttrs() const {
525 assert(HasAttrs && "No attrs to get!");
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000526 return getASTContext().getDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000527}
528
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000529void Decl::swapAttrs(Decl *RHS) {
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000530 bool HasLHSAttr = this->HasAttrs;
531 bool HasRHSAttr = RHS->HasAttrs;
Mike Stump11289f42009-09-09 15:08:12 +0000532
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000533 // Usually, neither decl has attrs, nothing to do.
534 if (!HasLHSAttr && !HasRHSAttr) return;
Mike Stump11289f42009-09-09 15:08:12 +0000535
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000536 // If 'this' has no attrs, swap the other way.
537 if (!HasLHSAttr)
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000538 return RHS->swapAttrs(this);
Mike Stump11289f42009-09-09 15:08:12 +0000539
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000540 ASTContext &Context = getASTContext();
Mike Stump11289f42009-09-09 15:08:12 +0000541
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000542 // Handle the case when both decls have attrs.
543 if (HasRHSAttr) {
Douglas Gregor78bd61f2009-06-18 16:11:24 +0000544 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000545 return;
546 }
Mike Stump11289f42009-09-09 15:08:12 +0000547
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000548 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor78bd61f2009-06-18 16:11:24 +0000549 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
550 Context.eraseDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000551 this->HasAttrs = false;
552 RHS->HasAttrs = true;
553}
554
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000555Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000556 Decl::Kind DK = D->getDeclKind();
557 switch(DK) {
Alexis Hunted053252010-05-30 07:21:58 +0000558#define DECL(NAME, BASE)
559#define DECL_CONTEXT(NAME) \
560 case Decl::NAME: \
561 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
562#define DECL_CONTEXT_BASE(NAME)
563#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000564 default:
Alexis Hunted053252010-05-30 07:21:58 +0000565#define DECL(NAME, BASE)
566#define DECL_CONTEXT_BASE(NAME) \
567 if (DK >= first##NAME && DK <= last##NAME) \
568 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
569#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000570 assert(false && "a decl that inherits DeclContext isn't handled");
571 return 0;
572 }
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000573}
574
575DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000576 Decl::Kind DK = D->getKind();
577 switch(DK) {
Alexis Hunted053252010-05-30 07:21:58 +0000578#define DECL(NAME, BASE)
579#define DECL_CONTEXT(NAME) \
580 case Decl::NAME: \
581 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
582#define DECL_CONTEXT_BASE(NAME)
583#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000584 default:
Alexis Hunted053252010-05-30 07:21:58 +0000585#define DECL(NAME, BASE)
586#define DECL_CONTEXT_BASE(NAME) \
587 if (DK >= first##NAME && DK <= last##NAME) \
588 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
589#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000590 assert(false && "a decl that inherits DeclContext isn't handled");
591 return 0;
592 }
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000593}
594
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000595SourceLocation Decl::getBodyRBrace() const {
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +0000596 // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
597 // FunctionDecl stores EndRangeLoc for this purpose.
598 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
599 const FunctionDecl *Definition;
600 if (FD->hasBody(Definition))
601 return Definition->getSourceRange().getEnd();
602 return SourceLocation();
603 }
604
Argyrios Kyrtzidis6fbc8fa2010-07-07 11:31:27 +0000605 if (Stmt *Body = getBody())
606 return Body->getSourceRange().getEnd();
607
608 return SourceLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +0000609}
610
Anders Carlssona28908d2009-03-25 23:38:06 +0000611void Decl::CheckAccessDeclContext() const {
Douglas Gregor4b00d3b2010-12-02 00:22:25 +0000612#ifndef NDEBUG
John McCall401982f2010-01-20 21:53:11 +0000613 // Suppress this check if any of the following hold:
614 // 1. this is the translation unit (and thus has no parent)
615 // 2. this is a template parameter (and thus doesn't belong to its context)
Argyrios Kyrtzidise1778632010-09-08 21:58:42 +0000616 // 3. this is a non-type template parameter
617 // 4. the context is not a record
618 // 5. it's invalid
619 // 6. it's a C++0x static_assert.
Anders Carlssonadf36b22009-08-29 20:47:47 +0000620 if (isa<TranslationUnitDecl>(this) ||
Argyrios Kyrtzidisa45855f2010-07-02 11:55:44 +0000621 isa<TemplateTypeParmDecl>(this) ||
Argyrios Kyrtzidise1778632010-09-08 21:58:42 +0000622 isa<NonTypeTemplateParmDecl>(this) ||
Douglas Gregor2b76dd92010-02-22 17:53:38 +0000623 !isa<CXXRecordDecl>(getDeclContext()) ||
Argyrios Kyrtzidis260b4a82010-09-08 21:32:35 +0000624 isInvalidDecl() ||
625 isa<StaticAssertDecl>(this) ||
626 // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
627 // as DeclContext (?).
Argyrios Kyrtzidise1778632010-09-08 21:58:42 +0000628 isa<ParmVarDecl>(this) ||
629 // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
630 // AS_none as access specifier.
631 isa<CXXRecordDecl>(this))
Anders Carlssonadf36b22009-08-29 20:47:47 +0000632 return;
Mike Stump11289f42009-09-09 15:08:12 +0000633
634 assert(Access != AS_none &&
Anders Carlssona28908d2009-03-25 23:38:06 +0000635 "Access specifier is AS_none inside a record decl");
Douglas Gregor4b00d3b2010-12-02 00:22:25 +0000636#endif
Anders Carlssona28908d2009-03-25 23:38:06 +0000637}
638
John McCallb67608f2011-02-22 22:25:23 +0000639DeclContext *Decl::getNonClosureContext() {
640 DeclContext *DC = getDeclContext();
641
642 // This is basically "while (DC->isClosure()) DC = DC->getParent();"
643 // except that it's significantly more efficient to cast to a known
644 // decl type and call getDeclContext() than to call getParent().
John McCall63b45fe2011-06-23 21:18:31 +0000645 while (isa<BlockDecl>(DC))
646 DC = cast<BlockDecl>(DC)->getDeclContext();
John McCallb67608f2011-02-22 22:25:23 +0000647
648 assert(!DC->isClosure());
649 return DC;
650}
Anders Carlssona28908d2009-03-25 23:38:06 +0000651
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000652//===----------------------------------------------------------------------===//
653// DeclContext Implementation
654//===----------------------------------------------------------------------===//
655
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000656bool DeclContext::classof(const Decl *D) {
657 switch (D->getKind()) {
Alexis Hunted053252010-05-30 07:21:58 +0000658#define DECL(NAME, BASE)
659#define DECL_CONTEXT(NAME) case Decl::NAME:
660#define DECL_CONTEXT_BASE(NAME)
661#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000662 return true;
663 default:
Alexis Hunted053252010-05-30 07:21:58 +0000664#define DECL(NAME, BASE)
665#define DECL_CONTEXT_BASE(NAME) \
666 if (D->getKind() >= Decl::first##NAME && \
667 D->getKind() <= Decl::last##NAME) \
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000668 return true;
Alexis Hunted053252010-05-30 07:21:58 +0000669#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000670 return false;
671 }
672}
673
Douglas Gregor9c832f72010-07-25 18:38:02 +0000674DeclContext::~DeclContext() { }
Douglas Gregor91f84212008-12-11 16:49:14 +0000675
Douglas Gregor7f737c02009-09-10 16:57:35 +0000676/// \brief Find the parent context of this context that will be
677/// used for unqualified name lookup.
678///
679/// Generally, the parent lookup context is the semantic context. However, for
680/// a friend function the parent lookup context is the lexical context, which
681/// is the class in which the friend is declared.
682DeclContext *DeclContext::getLookupParent() {
683 // FIXME: Find a better way to identify friends
684 if (isa<FunctionDecl>(this))
Sebastian Redl50c68252010-08-31 00:36:30 +0000685 if (getParent()->getRedeclContext()->isFileContext() &&
686 getLexicalParent()->getRedeclContext()->isRecord())
Douglas Gregor7f737c02009-09-10 16:57:35 +0000687 return getLexicalParent();
688
689 return getParent();
690}
691
Sebastian Redlbd595762010-08-31 20:53:31 +0000692bool DeclContext::isInlineNamespace() const {
693 return isNamespace() &&
694 cast<NamespaceDecl>(this)->isInline();
695}
696
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000697bool DeclContext::isDependentContext() const {
698 if (isFileContext())
699 return false;
700
Douglas Gregor2373c592009-05-31 09:31:02 +0000701 if (isa<ClassTemplatePartialSpecializationDecl>(this))
702 return true;
703
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000704 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
705 if (Record->getDescribedClassTemplate())
706 return true;
707
John McCallc62bb642010-03-24 05:22:00 +0000708 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000709 if (Function->getDescribedFunctionTemplate())
710 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000711
John McCallc62bb642010-03-24 05:22:00 +0000712 // Friend function declarations are dependent if their *lexical*
713 // context is dependent.
714 if (cast<Decl>(this)->getFriendObjectKind())
715 return getLexicalParent()->isDependentContext();
716 }
717
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000718 return getParent() && getParent()->isDependentContext();
719}
720
Douglas Gregor07665a62009-01-05 19:45:36 +0000721bool DeclContext::isTransparentContext() const {
722 if (DeclKind == Decl::Enum)
Douglas Gregor0bf31402010-10-08 23:50:27 +0000723 return !cast<EnumDecl>(this)->isScoped();
Douglas Gregor07665a62009-01-05 19:45:36 +0000724 else if (DeclKind == Decl::LinkageSpec)
725 return true;
Douglas Gregor07665a62009-01-05 19:45:36 +0000726
727 return false;
728}
729
John McCall5fe84122010-10-26 04:59:26 +0000730bool DeclContext::isExternCContext() const {
731 const DeclContext *DC = this;
732 while (DC->DeclKind != Decl::TranslationUnit) {
733 if (DC->DeclKind == Decl::LinkageSpec)
734 return cast<LinkageSpecDecl>(DC)->getLanguage()
735 == LinkageSpecDecl::lang_c;
736 DC = DC->getParent();
737 }
738 return false;
739}
740
Sebastian Redl50c68252010-08-31 00:36:30 +0000741bool DeclContext::Encloses(const DeclContext *DC) const {
Douglas Gregore985a3b2009-08-27 06:03:53 +0000742 if (getPrimaryContext() != this)
743 return getPrimaryContext()->Encloses(DC);
Mike Stump11289f42009-09-09 15:08:12 +0000744
Douglas Gregore985a3b2009-08-27 06:03:53 +0000745 for (; DC; DC = DC->getParent())
746 if (DC->getPrimaryContext() == this)
747 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000748 return false;
Douglas Gregore985a3b2009-08-27 06:03:53 +0000749}
750
Steve Naroff35c62ae2009-01-08 17:28:14 +0000751DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor91f84212008-12-11 16:49:14 +0000752 switch (DeclKind) {
Douglas Gregor91f84212008-12-11 16:49:14 +0000753 case Decl::TranslationUnit:
Douglas Gregor07665a62009-01-05 19:45:36 +0000754 case Decl::LinkageSpec:
Mike Stump11289f42009-09-09 15:08:12 +0000755 case Decl::Block:
Douglas Gregor91f84212008-12-11 16:49:14 +0000756 // There is only one DeclContext for these entities.
757 return this;
758
759 case Decl::Namespace:
760 // The original namespace is our primary context.
761 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
762
Douglas Gregor91f84212008-12-11 16:49:14 +0000763 case Decl::ObjCMethod:
764 return this;
765
766 case Decl::ObjCInterface:
Steve Naroff35c62ae2009-01-08 17:28:14 +0000767 case Decl::ObjCProtocol:
768 case Decl::ObjCCategory:
Douglas Gregor91f84212008-12-11 16:49:14 +0000769 // FIXME: Can Objective-C interfaces be forward-declared?
770 return this;
771
Steve Naroff35c62ae2009-01-08 17:28:14 +0000772 case Decl::ObjCImplementation:
773 case Decl::ObjCCategoryImpl:
774 return this;
775
Douglas Gregor91f84212008-12-11 16:49:14 +0000776 default:
Alexis Hunted053252010-05-30 07:21:58 +0000777 if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
Douglas Gregor67a65642009-02-17 23:15:12 +0000778 // If this is a tag type that has a definition or is currently
779 // being defined, that definition is our primary context.
John McCalle78aac42010-03-10 03:28:59 +0000780 TagDecl *Tag = cast<TagDecl>(this);
781 assert(isa<TagType>(Tag->TypeForDecl) ||
782 isa<InjectedClassNameType>(Tag->TypeForDecl));
783
784 if (TagDecl *Def = Tag->getDefinition())
785 return Def;
786
787 if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) {
788 const TagType *TagTy = cast<TagType>(Tag->TypeForDecl);
789 if (TagTy->isBeingDefined())
790 // FIXME: is it necessarily being defined in the decl
791 // that owns the type?
792 return TagTy->getDecl();
793 }
794
795 return Tag;
Douglas Gregor67a65642009-02-17 23:15:12 +0000796 }
797
Alexis Hunted053252010-05-30 07:21:58 +0000798 assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
Douglas Gregor91f84212008-12-11 16:49:14 +0000799 "Unknown DeclContext kind");
800 return this;
801 }
802}
803
804DeclContext *DeclContext::getNextContext() {
805 switch (DeclKind) {
Douglas Gregor91f84212008-12-11 16:49:14 +0000806 case Decl::Namespace:
807 // Return the next namespace
808 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
809
810 default:
Douglas Gregor91f84212008-12-11 16:49:14 +0000811 return 0;
812 }
813}
814
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000815std::pair<Decl *, Decl *>
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000816DeclContext::BuildDeclChain(const SmallVectorImpl<Decl*> &Decls) {
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000817 // Build up a chain of declarations via the Decl::NextDeclInContext field.
818 Decl *FirstNewDecl = 0;
819 Decl *PrevDecl = 0;
820 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
821 Decl *D = Decls[I];
822 if (PrevDecl)
823 PrevDecl->NextDeclInContext = D;
824 else
825 FirstNewDecl = D;
826
827 PrevDecl = D;
828 }
829
830 return std::make_pair(FirstNewDecl, PrevDecl);
831}
832
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000833/// \brief Load the declarations within this lexical storage from an
834/// external source.
Mike Stump11289f42009-09-09 15:08:12 +0000835void
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000836DeclContext::LoadLexicalDeclsFromExternalStorage() const {
837 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000838 assert(hasExternalLexicalStorage() && Source && "No external storage?");
839
Argyrios Kyrtzidis98d045e2010-07-30 10:03:23 +0000840 // Notify that we have a DeclContext that is initializing.
841 ExternalASTSource::Deserializing ADeclContext(Source);
842
Douglas Gregor3d0adb32011-07-15 21:46:17 +0000843 // Load the external declarations, if any.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000844 SmallVector<Decl*, 64> Decls;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000845 ExternalLexicalStorage = false;
Douglas Gregor3d0adb32011-07-15 21:46:17 +0000846 switch (Source->FindExternalLexicalDecls(this, Decls)) {
847 case ELR_Success:
848 break;
849
850 case ELR_Failure:
851 case ELR_AlreadyLoaded:
852 return;
853 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000854
855 if (Decls.empty())
856 return;
857
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000858 // We may have already loaded just the fields of this record, in which case
859 // don't add the decls, just replace the FirstDecl/LastDecl chain.
860 if (const RecordDecl *RD = dyn_cast<RecordDecl>(this))
861 if (RD->LoadedFieldsFromExternalStorage) {
862 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
863 return;
864 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000865
866 // Splice the newly-read declarations into the beginning of the list
867 // of declarations.
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000868 Decl *ExternalFirst, *ExternalLast;
869 llvm::tie(ExternalFirst, ExternalLast) = BuildDeclChain(Decls);
870 ExternalLast->NextDeclInContext = FirstDecl;
871 FirstDecl = ExternalFirst;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000872 if (!LastDecl)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000873 LastDecl = ExternalLast;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000874}
875
John McCall75b960e2010-06-01 09:23:16 +0000876DeclContext::lookup_result
877ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
878 DeclarationName Name) {
879 ASTContext &Context = DC->getParentASTContext();
880 StoredDeclsMap *Map;
881 if (!(Map = DC->LookupPtr))
882 Map = DC->CreateStoredDeclsMap(Context);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000883
John McCall75b960e2010-06-01 09:23:16 +0000884 StoredDeclsList &List = (*Map)[Name];
885 assert(List.isNull());
886 (void) List;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000887
John McCall75b960e2010-06-01 09:23:16 +0000888 return DeclContext::lookup_result();
889}
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000890
John McCall75b960e2010-06-01 09:23:16 +0000891DeclContext::lookup_result
892ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +0000893 DeclarationName Name,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000894 SmallVectorImpl<NamedDecl*> &Decls) {
John McCall75b960e2010-06-01 09:23:16 +0000895 ASTContext &Context = DC->getParentASTContext();;
896
897 StoredDeclsMap *Map;
898 if (!(Map = DC->LookupPtr))
899 Map = DC->CreateStoredDeclsMap(Context);
900
901 StoredDeclsList &List = (*Map)[Name];
902 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
903 if (List.isNull())
904 List.setOnlyValue(Decls[I]);
905 else
906 List.AddSubsequentDecl(Decls[I]);
907 }
908
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000909 return List.getLookupResult();
John McCall75b960e2010-06-01 09:23:16 +0000910}
911
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +0000912void ExternalASTSource::MaterializeVisibleDeclsForName(const DeclContext *DC,
913 DeclarationName Name,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000914 SmallVectorImpl<NamedDecl*> &Decls) {
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +0000915 assert(DC->LookupPtr);
916 StoredDeclsMap &Map = *DC->LookupPtr;
917
918 // If there's an entry in the table the visible decls for this name have
919 // already been deserialized.
920 if (Map.find(Name) == Map.end()) {
921 StoredDeclsList &List = Map[Name];
922 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
923 if (List.isNull())
924 List.setOnlyValue(Decls[I]);
925 else
926 List.AddSubsequentDecl(Decls[I]);
927 }
928 }
929}
930
Sebastian Redl66c5eef2010-07-27 00:17:23 +0000931DeclContext::decl_iterator DeclContext::noload_decls_begin() const {
932 return decl_iterator(FirstDecl);
933}
934
935DeclContext::decl_iterator DeclContext::noload_decls_end() const {
936 return decl_iterator();
937}
938
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000939DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000940 if (hasExternalLexicalStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000941 LoadLexicalDeclsFromExternalStorage();
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000942
943 // FIXME: Check whether we need to load some declarations from
944 // external storage.
Mike Stump11289f42009-09-09 15:08:12 +0000945 return decl_iterator(FirstDecl);
Douglas Gregorbcced4e2009-04-09 21:40:53 +0000946}
947
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000948DeclContext::decl_iterator DeclContext::decls_end() const {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000949 if (hasExternalLexicalStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000950 LoadLexicalDeclsFromExternalStorage();
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000951
Mike Stump11289f42009-09-09 15:08:12 +0000952 return decl_iterator();
Douglas Gregorbcced4e2009-04-09 21:40:53 +0000953}
954
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000955bool DeclContext::decls_empty() const {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +0000956 if (hasExternalLexicalStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000957 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +0000958
959 return !FirstDecl;
960}
961
John McCall84d87672009-12-10 09:41:52 +0000962void DeclContext::removeDecl(Decl *D) {
963 assert(D->getLexicalDeclContext() == this &&
964 "decl being removed from non-lexical context");
965 assert((D->NextDeclInContext || D == LastDecl) &&
966 "decl is not in decls list");
967
968 // Remove D from the decl chain. This is O(n) but hopefully rare.
969 if (D == FirstDecl) {
970 if (D == LastDecl)
971 FirstDecl = LastDecl = 0;
972 else
973 FirstDecl = D->NextDeclInContext;
974 } else {
975 for (Decl *I = FirstDecl; true; I = I->NextDeclInContext) {
976 assert(I && "decl not found in linked list");
977 if (I->NextDeclInContext == D) {
978 I->NextDeclInContext = D->NextDeclInContext;
979 if (D == LastDecl) LastDecl = I;
980 break;
981 }
982 }
983 }
984
985 // Mark that D is no longer in the decl chain.
986 D->NextDeclInContext = 0;
987
988 // Remove D from the lookup table if necessary.
989 if (isa<NamedDecl>(D)) {
990 NamedDecl *ND = cast<NamedDecl>(D);
991
John McCallc62bb642010-03-24 05:22:00 +0000992 StoredDeclsMap *Map = getPrimaryContext()->LookupPtr;
993 if (!Map) return;
John McCall84d87672009-12-10 09:41:52 +0000994
John McCall84d87672009-12-10 09:41:52 +0000995 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
996 assert(Pos != Map->end() && "no lookup entry for decl");
997 Pos->second.remove(ND);
998 }
999}
1000
John McCalld1e9d832009-08-11 06:59:38 +00001001void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner33f219d2009-02-20 00:56:18 +00001002 assert(D->getLexicalDeclContext() == this &&
1003 "Decl inserted into wrong lexical context");
Mike Stump11289f42009-09-09 15:08:12 +00001004 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor020713e2009-01-09 19:42:16 +00001005 "Decl already inserted into a DeclContext");
1006
1007 if (FirstDecl) {
Chris Lattnerfcd33a62009-03-28 06:04:26 +00001008 LastDecl->NextDeclInContext = D;
Douglas Gregor020713e2009-01-09 19:42:16 +00001009 LastDecl = D;
1010 } else {
1011 FirstDecl = LastDecl = D;
1012 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +00001013
1014 // Notify a C++ record declaration that we've added a member, so it can
1015 // update it's class-specific state.
1016 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
1017 Record->addedMember(D);
John McCalld1e9d832009-08-11 06:59:38 +00001018}
1019
1020void DeclContext::addDecl(Decl *D) {
1021 addHiddenDecl(D);
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001022
1023 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001024 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor91f84212008-12-11 16:49:14 +00001025}
1026
Douglas Gregor07665a62009-01-05 19:45:36 +00001027/// buildLookup - Build the lookup data structure with all of the
1028/// declarations in DCtx (and any other contexts linked to it or
1029/// transparent contexts nested within it).
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001030void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregor07665a62009-01-05 19:45:36 +00001031 for (; DCtx; DCtx = DCtx->getNextContext()) {
Mike Stump11289f42009-09-09 15:08:12 +00001032 for (decl_iterator D = DCtx->decls_begin(),
1033 DEnd = DCtx->decls_end();
Douglas Gregord05cb412009-01-06 07:17:58 +00001034 D != DEnd; ++D) {
John McCalld1e9d832009-08-11 06:59:38 +00001035 // Insert this declaration into the lookup structure, but only
1036 // if it's semantically in its decl context. During non-lazy
1037 // lookup building, this is implicitly enforced by addDecl.
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001038 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
John McCalld1e9d832009-08-11 06:59:38 +00001039 if (D->getDeclContext() == DCtx)
1040 makeDeclVisibleInContextImpl(ND);
Douglas Gregor07665a62009-01-05 19:45:36 +00001041
Ted Kremenek707ece62009-11-17 22:58:30 +00001042 // Insert any forward-declared Objective-C interfaces into the lookup
1043 // data structure.
1044 if (ObjCClassDecl *Class = dyn_cast<ObjCClassDecl>(*D))
1045 for (ObjCClassDecl::iterator I = Class->begin(), IEnd = Class->end();
1046 I != IEnd; ++I)
Ted Kremenek9b124e12009-11-18 00:28:11 +00001047 makeDeclVisibleInContextImpl(I->getInterface());
Ted Kremenek707ece62009-11-17 22:58:30 +00001048
Sebastian Redlbd595762010-08-31 20:53:31 +00001049 // If this declaration is itself a transparent declaration context or
1050 // inline namespace, add its members (recursively).
Douglas Gregor07665a62009-01-05 19:45:36 +00001051 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
Sebastian Redlbd595762010-08-31 20:53:31 +00001052 if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001053 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregor07665a62009-01-05 19:45:36 +00001054 }
1055 }
1056}
1057
Mike Stump11289f42009-09-09 15:08:12 +00001058DeclContext::lookup_result
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001059DeclContext::lookup(DeclarationName Name) {
Steve Naroff35c62ae2009-01-08 17:28:14 +00001060 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor91f84212008-12-11 16:49:14 +00001061 if (PrimaryContext != this)
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001062 return PrimaryContext->lookup(Name);
Douglas Gregor91f84212008-12-11 16:49:14 +00001063
John McCall75b960e2010-06-01 09:23:16 +00001064 if (hasExternalVisibleStorage()) {
1065 // Check to see if we've already cached the lookup results.
1066 if (LookupPtr) {
1067 StoredDeclsMap::iterator I = LookupPtr->find(Name);
1068 if (I != LookupPtr->end())
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001069 return I->second.getLookupResult();
John McCall75b960e2010-06-01 09:23:16 +00001070 }
1071
1072 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1073 return Source->FindExternalVisibleDeclsByName(this, Name);
1074 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001075
Douglas Gregor55297ac2008-12-23 00:26:44 +00001076 /// If there is no lookup data structure, build one now by walking
Douglas Gregor91f84212008-12-11 16:49:14 +00001077 /// all of the linked DeclContexts (in declaration order!) and
1078 /// inserting their values.
Douglas Gregor9615ec22009-04-09 17:29:08 +00001079 if (!LookupPtr) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001080 buildLookup(this);
Douglas Gregor91f84212008-12-11 16:49:14 +00001081
Douglas Gregor9615ec22009-04-09 17:29:08 +00001082 if (!LookupPtr)
Douglas Gregor10dc8aa2010-05-11 06:18:17 +00001083 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Douglas Gregor9615ec22009-04-09 17:29:08 +00001084 }
Douglas Gregor91f84212008-12-11 16:49:14 +00001085
John McCallc62bb642010-03-24 05:22:00 +00001086 StoredDeclsMap::iterator Pos = LookupPtr->find(Name);
1087 if (Pos == LookupPtr->end())
Douglas Gregor10dc8aa2010-05-11 06:18:17 +00001088 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001089 return Pos->second.getLookupResult();
Douglas Gregor91f84212008-12-11 16:49:14 +00001090}
1091
Mike Stump11289f42009-09-09 15:08:12 +00001092DeclContext::lookup_const_result
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001093DeclContext::lookup(DeclarationName Name) const {
1094 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor91f84212008-12-11 16:49:14 +00001095}
1096
Sebastian Redl50c68252010-08-31 00:36:30 +00001097DeclContext *DeclContext::getRedeclContext() {
Chris Lattner17a1bfa2009-03-27 19:19:59 +00001098 DeclContext *Ctx = this;
Sebastian Redlbd595762010-08-31 20:53:31 +00001099 // Skip through transparent contexts.
1100 while (Ctx->isTransparentContext())
Douglas Gregor6ad0ef52009-01-06 23:51:29 +00001101 Ctx = Ctx->getParent();
1102 return Ctx;
1103}
1104
Douglas Gregorf47b9112009-02-25 22:02:03 +00001105DeclContext *DeclContext::getEnclosingNamespaceContext() {
1106 DeclContext *Ctx = this;
1107 // Skip through non-namespace, non-translation-unit contexts.
Sebastian Redl4f08c962010-08-31 00:36:23 +00001108 while (!Ctx->isFileContext())
Douglas Gregorf47b9112009-02-25 22:02:03 +00001109 Ctx = Ctx->getParent();
1110 return Ctx->getPrimaryContext();
1111}
1112
Sebastian Redl50c68252010-08-31 00:36:30 +00001113bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
1114 // For non-file contexts, this is equivalent to Equals.
1115 if (!isFileContext())
1116 return O->Equals(this);
1117
1118 do {
1119 if (O->Equals(this))
1120 return true;
1121
1122 const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O);
1123 if (!NS || !NS->isInline())
1124 break;
1125 O = NS->getParent();
1126 } while (O);
1127
1128 return false;
1129}
1130
John McCall759e32b2009-08-31 22:39:49 +00001131void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable) {
Douglas Gregor67a65642009-02-17 23:15:12 +00001132 // FIXME: This feels like a hack. Should DeclarationName support
1133 // template-ids, or is there a better way to keep specializations
1134 // from being visible?
Douglas Gregorfd7c2252011-03-04 17:52:15 +00001135 if (isa<ClassTemplateSpecializationDecl>(D) || D->isTemplateParameter())
Douglas Gregor67a65642009-02-17 23:15:12 +00001136 return;
Eli Friedman73168192009-12-08 05:40:03 +00001137 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1138 if (FD->isFunctionTemplateSpecialization())
1139 return;
Douglas Gregor67a65642009-02-17 23:15:12 +00001140
Steve Naroff35c62ae2009-01-08 17:28:14 +00001141 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor91f84212008-12-11 16:49:14 +00001142 if (PrimaryContext != this) {
John McCall759e32b2009-08-31 22:39:49 +00001143 PrimaryContext->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor91f84212008-12-11 16:49:14 +00001144 return;
1145 }
1146
1147 // If we already have a lookup data structure, perform the insertion
Argyrios Kyrtzidise51e5542010-07-04 21:44:25 +00001148 // into it. If we haven't deserialized externally stored decls, deserialize
1149 // them so we can add the decl. Otherwise, be lazy and don't build that
1150 // structure until someone asks for it.
1151 if (LookupPtr || !Recoverable || hasExternalVisibleStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001152 makeDeclVisibleInContextImpl(D);
Douglas Gregor07665a62009-01-05 19:45:36 +00001153
Sebastian Redlbd595762010-08-31 20:53:31 +00001154 // If we are a transparent context or inline namespace, insert into our
1155 // parent context, too. This operation is recursive.
1156 if (isTransparentContext() || isInlineNamespace())
John McCall759e32b2009-08-31 22:39:49 +00001157 getParent()->makeDeclVisibleInContext(D, Recoverable);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00001158
1159 Decl *DCAsDecl = cast<Decl>(this);
1160 // Notify that a decl was made visible unless it's a Tag being defined.
1161 if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined()))
1162 if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
1163 L->AddedVisibleDecl(this, D);
Douglas Gregor91f84212008-12-11 16:49:14 +00001164}
1165
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001166void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) {
Douglas Gregor07665a62009-01-05 19:45:36 +00001167 // Skip unnamed declarations.
1168 if (!D->getDeclName())
1169 return;
1170
Douglas Gregor0de016d2011-05-06 23:32:38 +00001171 // Skip entities that can't be found by name lookup into a particular
1172 // context.
1173 if ((D->getIdentifierNamespace() == 0 && !isa<UsingDirectiveDecl>(D)) ||
1174 D->isTemplateParameter())
Douglas Gregor67a65642009-02-17 23:15:12 +00001175 return;
1176
Argyrios Kyrtzidise51e5542010-07-04 21:44:25 +00001177 ASTContext *C = 0;
1178 if (!LookupPtr) {
1179 C = &getParentASTContext();
1180 CreateStoredDeclsMap(*C);
1181 }
1182
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001183 // If there is an external AST source, load any declarations it knows about
1184 // with this declaration's name.
1185 // If the lookup table contains an entry about this name it means that we
1186 // have already checked the external source.
1187 if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
1188 if (hasExternalVisibleStorage() &&
1189 LookupPtr->find(D->getDeclName()) == LookupPtr->end())
1190 Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
1191
Douglas Gregor91f84212008-12-11 16:49:14 +00001192 // Insert this declaration into the map.
John McCallc62bb642010-03-24 05:22:00 +00001193 StoredDeclsList &DeclNameEntries = (*LookupPtr)[D->getDeclName()];
Chris Lattnercaae7162009-02-20 01:44:05 +00001194 if (DeclNameEntries.isNull()) {
1195 DeclNameEntries.setOnlyValue(D);
Chris Lattnerdfd6b3d2009-02-19 07:00:44 +00001196 return;
Douglas Gregor91f84212008-12-11 16:49:14 +00001197 }
Chris Lattner24e24d52009-02-20 00:55:03 +00001198
Chris Lattner29578f32009-02-20 01:10:07 +00001199 // If it is possible that this is a redeclaration, check to see if there is
1200 // already a decl for which declarationReplaces returns true. If there is
1201 // one, just replace it and return.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001202 if (DeclNameEntries.HandleRedeclaration(D))
Chris Lattnercaae7162009-02-20 01:44:05 +00001203 return;
Mike Stump11289f42009-09-09 15:08:12 +00001204
Chris Lattnerdfd6b3d2009-02-19 07:00:44 +00001205 // Put this declaration into the appropriate slot.
Chris Lattnercaae7162009-02-20 01:44:05 +00001206 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor91f84212008-12-11 16:49:14 +00001207}
Douglas Gregor889ceb72009-02-03 19:21:40 +00001208
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00001209void DeclContext::MaterializeVisibleDeclsFromExternalStorage() {
1210 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1211 assert(hasExternalVisibleStorage() && Source && "No external storage?");
1212
1213 if (!LookupPtr)
1214 CreateStoredDeclsMap(getParentASTContext());
1215 Source->MaterializeVisibleDecls(this);
1216}
1217
Douglas Gregor889ceb72009-02-03 19:21:40 +00001218/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
1219/// this context.
Mike Stump11289f42009-09-09 15:08:12 +00001220DeclContext::udir_iterator_range
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001221DeclContext::getUsingDirectives() const {
1222 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor889ceb72009-02-03 19:21:40 +00001223 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
1224 reinterpret_cast<udir_iterator>(Result.second));
1225}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001226
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001227//===----------------------------------------------------------------------===//
1228// Creation and Destruction of StoredDeclsMaps. //
1229//===----------------------------------------------------------------------===//
1230
John McCallc62bb642010-03-24 05:22:00 +00001231StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
1232 assert(!LookupPtr && "context already has a decls map");
1233 assert(getPrimaryContext() == this &&
1234 "creating decls map on non-primary context");
1235
1236 StoredDeclsMap *M;
1237 bool Dependent = isDependentContext();
1238 if (Dependent)
1239 M = new DependentStoredDeclsMap();
1240 else
1241 M = new StoredDeclsMap();
1242 M->Previous = C.LastSDM;
1243 C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
1244 LookupPtr = M;
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001245 return M;
1246}
1247
1248void ASTContext::ReleaseDeclContextMaps() {
John McCallc62bb642010-03-24 05:22:00 +00001249 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1250 // pointer because the subclass doesn't add anything that needs to
1251 // be deleted.
John McCallc62bb642010-03-24 05:22:00 +00001252 StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1253}
1254
1255void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1256 while (Map) {
1257 // Advance the iteration before we invalidate memory.
1258 llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1259
1260 if (Dependent)
1261 delete static_cast<DependentStoredDeclsMap*>(Map);
1262 else
1263 delete Map;
1264
1265 Map = Next.getPointer();
1266 Dependent = Next.getInt();
1267 }
1268}
1269
John McCallc62bb642010-03-24 05:22:00 +00001270DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1271 DeclContext *Parent,
1272 const PartialDiagnostic &PDiag) {
1273 assert(Parent->isDependentContext()
1274 && "cannot iterate dependent diagnostics of non-dependent context");
1275 Parent = Parent->getPrimaryContext();
1276 if (!Parent->LookupPtr)
1277 Parent->CreateStoredDeclsMap(C);
1278
1279 DependentStoredDeclsMap *Map
1280 = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr);
1281
Douglas Gregora55530e2010-03-29 23:56:53 +00001282 // Allocate the copy of the PartialDiagnostic via the ASTContext's
Douglas Gregor89336232010-03-29 23:34:08 +00001283 // BumpPtrAllocator, rather than the ASTContext itself.
Douglas Gregora55530e2010-03-29 23:56:53 +00001284 PartialDiagnostic::Storage *DiagStorage = 0;
1285 if (PDiag.hasStorage())
1286 DiagStorage = new (C) PartialDiagnostic::Storage;
1287
1288 DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
John McCallc62bb642010-03-24 05:22:00 +00001289
1290 // TODO: Maybe we shouldn't reverse the order during insertion.
1291 DD->NextDiagnostic = Map->FirstDiagnostic;
1292 Map->FirstDiagnostic = DD;
1293
1294 return DD;
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001295}