blob: d1f8b1c7fd8a192b875786bc4c3f800c541ca4a9 [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) {
David Blaikie83d382b2011-09-23 05:06:16 +000046 default: llvm_unreachable("Declaration not in DeclNodes.inc!");
Alexis Hunted053252010-05-30 07:21:58 +000047#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) {
David Blaikie83d382b2011-09-23 05:06:16 +000065 default: llvm_unreachable("Declaration context not in DeclNodes.inc!");
Alexis Hunted053252010-05-30 07:21:58 +000066#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) {
David Blaikie83d382b2011-09-23 05:06:16 +0000103 default: llvm_unreachable("Declaration not in DeclNodes.inc!");
Alexis Hunted053252010-05-30 07:21:58 +0000104#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
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000136bool Decl::isTemplateDecl() const {
137 return isa<TemplateDecl>(this);
138}
139
Douglas Gregorf5974fa2010-01-16 20:21:20 +0000140bool Decl::isDefinedOutsideFunctionOrMethod() const {
141 for (const DeclContext *DC = getDeclContext();
142 DC && !DC->isTranslationUnit();
143 DC = DC->getParent())
144 if (DC->isFunctionOrMethod())
145 return false;
146
147 return true;
148}
149
Douglas Gregor133eddd2011-02-17 08:47:29 +0000150
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000151//===----------------------------------------------------------------------===//
Chris Lattnereae6cb62009-03-05 08:00:35 +0000152// PrettyStackTraceDecl Implementation
153//===----------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000154
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000155void PrettyStackTraceDecl::print(raw_ostream &OS) const {
Chris Lattnereae6cb62009-03-05 08:00:35 +0000156 SourceLocation TheLoc = Loc;
157 if (TheLoc.isInvalid() && TheDecl)
158 TheLoc = TheDecl->getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000159
Chris Lattnereae6cb62009-03-05 08:00:35 +0000160 if (TheLoc.isValid()) {
161 TheLoc.print(OS, SM);
162 OS << ": ";
163 }
164
165 OS << Message;
166
Daniel Dunbar4f1054e2009-11-21 09:05:59 +0000167 if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
Chris Lattnereae6cb62009-03-05 08:00:35 +0000168 OS << " '" << DN->getQualifiedNameAsString() << '\'';
169 OS << '\n';
170}
Mike Stump11289f42009-09-09 15:08:12 +0000171
Chris Lattnereae6cb62009-03-05 08:00:35 +0000172//===----------------------------------------------------------------------===//
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000173// Decl Implementation
174//===----------------------------------------------------------------------===//
175
Douglas Gregorb11aad82011-02-19 18:51:44 +0000176// Out-of-line virtual method providing a home for Decl.
177Decl::~Decl() { }
Douglas Gregora43942a2011-02-17 07:02:32 +0000178
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000179void Decl::setDeclContext(DeclContext *DC) {
Chris Lattnerb81eb052009-03-29 06:06:59 +0000180 DeclCtx = DC;
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000181}
182
183void Decl::setLexicalDeclContext(DeclContext *DC) {
184 if (DC == getLexicalDeclContext())
185 return;
186
187 if (isInSemaDC()) {
Ted Kremenekf8c12a32009-12-01 00:07:10 +0000188 MultipleDC *MDC = new (getASTContext()) MultipleDC();
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000189 MDC->SemanticDC = getDeclContext();
190 MDC->LexicalDC = DC;
Chris Lattnerb81eb052009-03-29 06:06:59 +0000191 DeclCtx = MDC;
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000192 } else {
193 getMultipleDC()->LexicalDC = DC;
194 }
195}
196
John McCall4fa53422009-10-01 00:25:31 +0000197bool Decl::isInAnonymousNamespace() const {
198 const DeclContext *DC = getDeclContext();
199 do {
200 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
201 if (ND->isAnonymousNamespace())
202 return true;
203 } while ((DC = DC->getParent()));
204
205 return false;
206}
207
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000208TranslationUnitDecl *Decl::getTranslationUnitDecl() {
Argyrios Kyrtzidis4e1a72b2009-06-30 02:34:53 +0000209 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
210 return TUD;
211
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000212 DeclContext *DC = getDeclContext();
213 assert(DC && "This decl is not contained in a translation unit!");
Mike Stump11289f42009-09-09 15:08:12 +0000214
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000215 while (!DC->isTranslationUnit()) {
216 DC = DC->getParent();
217 assert(DC && "This decl is not contained in a translation unit!");
218 }
Mike Stump11289f42009-09-09 15:08:12 +0000219
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000220 return cast<TranslationUnitDecl>(DC);
221}
222
223ASTContext &Decl::getASTContext() const {
Mike Stump11289f42009-09-09 15:08:12 +0000224 return getTranslationUnitDecl()->getASTContext();
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000225}
226
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +0000227ASTMutationListener *Decl::getASTMutationListener() const {
228 return getASTContext().getASTMutationListener();
229}
230
Douglas Gregorebada0772010-06-17 23:14:26 +0000231bool Decl::isUsed(bool CheckUsedAttr) const {
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000232 if (Used)
233 return true;
234
235 // Check for used attribute.
Douglas Gregorebada0772010-06-17 23:14:26 +0000236 if (CheckUsedAttr && hasAttr<UsedAttr>())
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000237 return true;
238
239 // Check redeclarations for used attribute.
240 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Douglas Gregorebada0772010-06-17 23:14:26 +0000241 if ((CheckUsedAttr && I->hasAttr<UsedAttr>()) || I->Used)
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000242 return true;
243 }
244
245 return false;
246}
247
Argyrios Kyrtzidis16180232011-04-19 19:51:10 +0000248bool Decl::isReferenced() const {
249 if (Referenced)
250 return true;
251
252 // Check redeclarations.
253 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
254 if (I->Referenced)
255 return true;
256
257 return false;
258}
259
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000260/// \brief Determine the availability of the given declaration based on
261/// the target platform.
262///
263/// When it returns an availability result other than \c AR_Available,
264/// if the \p Message parameter is non-NULL, it will be set to a
265/// string describing why the entity is unavailable.
266///
267/// FIXME: Make these strings localizable, since they end up in
268/// diagnostics.
269static AvailabilityResult CheckAvailability(ASTContext &Context,
270 const AvailabilityAttr *A,
271 std::string *Message) {
Douglas Gregore8bbc122011-09-02 00:18:52 +0000272 StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000273 StringRef PrettyPlatformName
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000274 = AvailabilityAttr::getPrettyPlatformName(TargetPlatform);
275 if (PrettyPlatformName.empty())
276 PrettyPlatformName = TargetPlatform;
277
Douglas Gregore8bbc122011-09-02 00:18:52 +0000278 VersionTuple TargetMinVersion = Context.getTargetInfo().getPlatformMinVersion();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000279 if (TargetMinVersion.empty())
280 return AR_Available;
281
282 // Match the platform name.
283 if (A->getPlatform()->getName() != TargetPlatform)
284 return AR_Available;
285
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000286 // Make sure that this declaration has not been marked 'unavailable'.
287 if (A->getUnavailable()) {
288 if (Message) {
289 Message->clear();
290 llvm::raw_string_ostream Out(*Message);
291 Out << "not available on " << PrettyPlatformName;
292 }
293
294 return AR_Unavailable;
295 }
296
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000297 // Make sure that this declaration has already been introduced.
298 if (!A->getIntroduced().empty() &&
299 TargetMinVersion < A->getIntroduced()) {
300 if (Message) {
301 Message->clear();
302 llvm::raw_string_ostream Out(*Message);
303 Out << "introduced in " << PrettyPlatformName << ' '
304 << A->getIntroduced();
305 }
306
307 return AR_NotYetIntroduced;
308 }
309
310 // Make sure that this declaration hasn't been obsoleted.
311 if (!A->getObsoleted().empty() && TargetMinVersion >= A->getObsoleted()) {
312 if (Message) {
313 Message->clear();
314 llvm::raw_string_ostream Out(*Message);
315 Out << "obsoleted in " << PrettyPlatformName << ' '
316 << A->getObsoleted();
317 }
318
319 return AR_Unavailable;
320 }
321
322 // Make sure that this declaration hasn't been deprecated.
323 if (!A->getDeprecated().empty() && TargetMinVersion >= A->getDeprecated()) {
324 if (Message) {
325 Message->clear();
326 llvm::raw_string_ostream Out(*Message);
327 Out << "first deprecated in " << PrettyPlatformName << ' '
328 << A->getDeprecated();
329 }
330
331 return AR_Deprecated;
332 }
333
334 return AR_Available;
335}
336
337AvailabilityResult Decl::getAvailability(std::string *Message) const {
338 AvailabilityResult Result = AR_Available;
339 std::string ResultMessage;
340
341 for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
342 if (DeprecatedAttr *Deprecated = dyn_cast<DeprecatedAttr>(*A)) {
343 if (Result >= AR_Deprecated)
344 continue;
345
346 if (Message)
347 ResultMessage = Deprecated->getMessage();
348
349 Result = AR_Deprecated;
350 continue;
351 }
352
353 if (UnavailableAttr *Unavailable = dyn_cast<UnavailableAttr>(*A)) {
354 if (Message)
355 *Message = Unavailable->getMessage();
356 return AR_Unavailable;
357 }
358
359 if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
360 AvailabilityResult AR = CheckAvailability(getASTContext(), Availability,
361 Message);
362
363 if (AR == AR_Unavailable)
364 return AR_Unavailable;
365
366 if (AR > Result) {
367 Result = AR;
368 if (Message)
369 ResultMessage.swap(*Message);
370 }
371 continue;
372 }
373 }
374
375 if (Message)
376 Message->swap(ResultMessage);
377 return Result;
378}
379
380bool Decl::canBeWeakImported(bool &IsDefinition) const {
381 IsDefinition = false;
382 if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
383 if (!Var->hasExternalStorage() || Var->getInit()) {
384 IsDefinition = true;
385 return false;
386 }
387 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
388 if (FD->hasBody()) {
389 IsDefinition = true;
390 return false;
391 }
392 } else if (isa<ObjCPropertyDecl>(this) || isa<ObjCMethodDecl>(this))
393 return false;
394 else if (!(getASTContext().getLangOptions().ObjCNonFragileABI &&
395 isa<ObjCInterfaceDecl>(this)))
396 return false;
397
398 return true;
399}
400
401bool Decl::isWeakImported() const {
402 bool IsDefinition;
403 if (!canBeWeakImported(IsDefinition))
404 return false;
405
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000406 for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
407 if (isa<WeakImportAttr>(*A))
408 return true;
409
410 if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
411 if (CheckAvailability(getASTContext(), Availability, 0)
412 == AR_NotYetIntroduced)
413 return true;
414 }
415 }
416
417 return false;
418}
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000419
Chris Lattner8e097192009-03-27 20:18:19 +0000420unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
421 switch (DeclKind) {
John McCall3f746822009-11-17 05:59:44 +0000422 case Function:
423 case CXXMethod:
424 case CXXConstructor:
425 case CXXDestructor:
426 case CXXConversion:
Chris Lattner8e097192009-03-27 20:18:19 +0000427 case EnumConstant:
428 case Var:
429 case ImplicitParam:
430 case ParmVar:
Chris Lattner8e097192009-03-27 20:18:19 +0000431 case NonTypeTemplateParm:
432 case ObjCMethod:
Daniel Dunbar45b2d8a2010-04-23 13:07:39 +0000433 case ObjCProperty:
Daniel Dunbar45b2d8a2010-04-23 13:07:39 +0000434 return IDNS_Ordinary;
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000435 case Label:
436 return IDNS_Label;
Francois Pichet783dd6e2010-11-21 06:08:52 +0000437 case IndirectField:
438 return IDNS_Ordinary | IDNS_Member;
439
John McCalle87beb22010-04-23 18:46:30 +0000440 case ObjCCompatibleAlias:
441 case ObjCInterface:
442 return IDNS_Ordinary | IDNS_Type;
443
444 case Typedef:
Richard Smithdda56e42011-04-15 14:24:37 +0000445 case TypeAlias:
Richard Smith3f1b5d02011-05-05 21:57:07 +0000446 case TypeAliasTemplate:
John McCalle87beb22010-04-23 18:46:30 +0000447 case UnresolvedUsingTypename:
448 case TemplateTypeParm:
449 return IDNS_Ordinary | IDNS_Type;
450
John McCall3f746822009-11-17 05:59:44 +0000451 case UsingShadow:
452 return 0; // we'll actually overwrite this later
453
John McCalle61f2ba2009-11-18 02:36:19 +0000454 case UnresolvedUsingValue:
John McCalle61f2ba2009-11-18 02:36:19 +0000455 return IDNS_Ordinary | IDNS_Using;
John McCall3f746822009-11-17 05:59:44 +0000456
457 case Using:
458 return IDNS_Using;
459
Chris Lattner8e097192009-03-27 20:18:19 +0000460 case ObjCProtocol:
Douglas Gregor79947a22009-04-24 00:11:27 +0000461 return IDNS_ObjCProtocol;
Mike Stump11289f42009-09-09 15:08:12 +0000462
Chris Lattner8e097192009-03-27 20:18:19 +0000463 case Field:
464 case ObjCAtDefsField:
465 case ObjCIvar:
466 return IDNS_Member;
Mike Stump11289f42009-09-09 15:08:12 +0000467
Chris Lattner8e097192009-03-27 20:18:19 +0000468 case Record:
469 case CXXRecord:
470 case Enum:
John McCalle87beb22010-04-23 18:46:30 +0000471 return IDNS_Tag | IDNS_Type;
Mike Stump11289f42009-09-09 15:08:12 +0000472
Chris Lattner8e097192009-03-27 20:18:19 +0000473 case Namespace:
John McCalle87beb22010-04-23 18:46:30 +0000474 case NamespaceAlias:
475 return IDNS_Namespace;
476
Chris Lattner8e097192009-03-27 20:18:19 +0000477 case FunctionTemplate:
John McCalle87beb22010-04-23 18:46:30 +0000478 return IDNS_Ordinary;
479
Chris Lattner8e097192009-03-27 20:18:19 +0000480 case ClassTemplate:
481 case TemplateTemplateParm:
John McCalle87beb22010-04-23 18:46:30 +0000482 return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
Mike Stump11289f42009-09-09 15:08:12 +0000483
Chris Lattner8e097192009-03-27 20:18:19 +0000484 // Never have names.
John McCallaa74a0c2009-08-28 07:59:38 +0000485 case Friend:
John McCall11083da2009-09-16 22:47:08 +0000486 case FriendTemplate:
Abramo Bagnarad7340582010-06-05 05:09:32 +0000487 case AccessSpec:
Chris Lattner8e097192009-03-27 20:18:19 +0000488 case LinkageSpec:
489 case FileScopeAsm:
490 case StaticAssert:
491 case ObjCClass:
Chris Lattner8e097192009-03-27 20:18:19 +0000492 case ObjCPropertyImpl:
493 case ObjCForwardProtocol:
494 case Block:
495 case TranslationUnit:
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000496
Chris Lattner8e097192009-03-27 20:18:19 +0000497 case UsingDirective:
498 case ClassTemplateSpecialization:
Douglas Gregor2373c592009-05-31 09:31:02 +0000499 case ClassTemplatePartialSpecialization:
Francois Pichet00c7e6c2011-08-14 03:52:19 +0000500 case ClassScopeFunctionSpecialization:
Douglas Gregore93525e2010-04-22 23:19:50 +0000501 case ObjCImplementation:
502 case ObjCCategory:
503 case ObjCCategoryImpl:
504 // Never looked up by name.
Chris Lattner8e097192009-03-27 20:18:19 +0000505 return 0;
506 }
John McCall3f746822009-11-17 05:59:44 +0000507
508 return 0;
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000509}
510
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000511void Decl::setAttrs(const AttrVec &attrs) {
Argyrios Kyrtzidis91167172010-06-11 23:09:25 +0000512 assert(!HasAttrs && "Decl already contains attrs.");
513
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000514 AttrVec &AttrBlank = getASTContext().getDeclAttrs(this);
515 assert(AttrBlank.empty() && "HasAttrs was wrong?");
Argyrios Kyrtzidis91167172010-06-11 23:09:25 +0000516
517 AttrBlank = attrs;
518 HasAttrs = true;
519}
520
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000521void Decl::dropAttrs() {
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000522 if (!HasAttrs) return;
Mike Stump11289f42009-09-09 15:08:12 +0000523
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000524 HasAttrs = false;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000525 getASTContext().eraseDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000526}
527
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000528const AttrVec &Decl::getAttrs() const {
529 assert(HasAttrs && "No attrs to get!");
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000530 return getASTContext().getDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000531}
532
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000533void Decl::swapAttrs(Decl *RHS) {
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000534 bool HasLHSAttr = this->HasAttrs;
535 bool HasRHSAttr = RHS->HasAttrs;
Mike Stump11289f42009-09-09 15:08:12 +0000536
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000537 // Usually, neither decl has attrs, nothing to do.
538 if (!HasLHSAttr && !HasRHSAttr) return;
Mike Stump11289f42009-09-09 15:08:12 +0000539
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000540 // If 'this' has no attrs, swap the other way.
541 if (!HasLHSAttr)
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000542 return RHS->swapAttrs(this);
Mike Stump11289f42009-09-09 15:08:12 +0000543
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000544 ASTContext &Context = getASTContext();
Mike Stump11289f42009-09-09 15:08:12 +0000545
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000546 // Handle the case when both decls have attrs.
547 if (HasRHSAttr) {
Douglas Gregor78bd61f2009-06-18 16:11:24 +0000548 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000549 return;
550 }
Mike Stump11289f42009-09-09 15:08:12 +0000551
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000552 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor78bd61f2009-06-18 16:11:24 +0000553 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
554 Context.eraseDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000555 this->HasAttrs = false;
556 RHS->HasAttrs = true;
557}
558
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000559Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000560 Decl::Kind DK = D->getDeclKind();
561 switch(DK) {
Alexis Hunted053252010-05-30 07:21:58 +0000562#define DECL(NAME, BASE)
563#define DECL_CONTEXT(NAME) \
564 case Decl::NAME: \
565 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
566#define DECL_CONTEXT_BASE(NAME)
567#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000568 default:
Alexis Hunted053252010-05-30 07:21:58 +0000569#define DECL(NAME, BASE)
570#define DECL_CONTEXT_BASE(NAME) \
571 if (DK >= first##NAME && DK <= last##NAME) \
572 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
573#include "clang/AST/DeclNodes.inc"
David Blaikie83d382b2011-09-23 05:06:16 +0000574 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000575 return 0;
576 }
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000577}
578
579DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000580 Decl::Kind DK = D->getKind();
581 switch(DK) {
Alexis Hunted053252010-05-30 07:21:58 +0000582#define DECL(NAME, BASE)
583#define DECL_CONTEXT(NAME) \
584 case Decl::NAME: \
585 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
586#define DECL_CONTEXT_BASE(NAME)
587#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000588 default:
Alexis Hunted053252010-05-30 07:21:58 +0000589#define DECL(NAME, BASE)
590#define DECL_CONTEXT_BASE(NAME) \
591 if (DK >= first##NAME && DK <= last##NAME) \
592 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
593#include "clang/AST/DeclNodes.inc"
David Blaikie83d382b2011-09-23 05:06:16 +0000594 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000595 return 0;
596 }
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000597}
598
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000599SourceLocation Decl::getBodyRBrace() const {
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +0000600 // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
601 // FunctionDecl stores EndRangeLoc for this purpose.
602 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
603 const FunctionDecl *Definition;
604 if (FD->hasBody(Definition))
605 return Definition->getSourceRange().getEnd();
606 return SourceLocation();
607 }
608
Argyrios Kyrtzidis6fbc8fa2010-07-07 11:31:27 +0000609 if (Stmt *Body = getBody())
610 return Body->getSourceRange().getEnd();
611
612 return SourceLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +0000613}
614
Anders Carlssona28908d2009-03-25 23:38:06 +0000615void Decl::CheckAccessDeclContext() const {
Douglas Gregor4b00d3b2010-12-02 00:22:25 +0000616#ifndef NDEBUG
John McCall401982f2010-01-20 21:53:11 +0000617 // Suppress this check if any of the following hold:
618 // 1. this is the translation unit (and thus has no parent)
619 // 2. this is a template parameter (and thus doesn't belong to its context)
Argyrios Kyrtzidise1778632010-09-08 21:58:42 +0000620 // 3. this is a non-type template parameter
621 // 4. the context is not a record
622 // 5. it's invalid
623 // 6. it's a C++0x static_assert.
Anders Carlssonadf36b22009-08-29 20:47:47 +0000624 if (isa<TranslationUnitDecl>(this) ||
Argyrios Kyrtzidisa45855f2010-07-02 11:55:44 +0000625 isa<TemplateTypeParmDecl>(this) ||
Argyrios Kyrtzidise1778632010-09-08 21:58:42 +0000626 isa<NonTypeTemplateParmDecl>(this) ||
Douglas Gregor2b76dd92010-02-22 17:53:38 +0000627 !isa<CXXRecordDecl>(getDeclContext()) ||
Argyrios Kyrtzidis260b4a82010-09-08 21:32:35 +0000628 isInvalidDecl() ||
629 isa<StaticAssertDecl>(this) ||
630 // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
631 // as DeclContext (?).
Argyrios Kyrtzidise1778632010-09-08 21:58:42 +0000632 isa<ParmVarDecl>(this) ||
633 // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
634 // AS_none as access specifier.
Francois Pichet09af8c32011-08-17 01:06:54 +0000635 isa<CXXRecordDecl>(this) ||
636 isa<ClassScopeFunctionSpecializationDecl>(this))
Anders Carlssonadf36b22009-08-29 20:47:47 +0000637 return;
Mike Stump11289f42009-09-09 15:08:12 +0000638
639 assert(Access != AS_none &&
Anders Carlssona28908d2009-03-25 23:38:06 +0000640 "Access specifier is AS_none inside a record decl");
Douglas Gregor4b00d3b2010-12-02 00:22:25 +0000641#endif
Anders Carlssona28908d2009-03-25 23:38:06 +0000642}
643
John McCallb67608f2011-02-22 22:25:23 +0000644DeclContext *Decl::getNonClosureContext() {
645 DeclContext *DC = getDeclContext();
646
647 // This is basically "while (DC->isClosure()) DC = DC->getParent();"
648 // except that it's significantly more efficient to cast to a known
649 // decl type and call getDeclContext() than to call getParent().
John McCall63b45fe2011-06-23 21:18:31 +0000650 while (isa<BlockDecl>(DC))
651 DC = cast<BlockDecl>(DC)->getDeclContext();
John McCallb67608f2011-02-22 22:25:23 +0000652
653 assert(!DC->isClosure());
654 return DC;
655}
Anders Carlssona28908d2009-03-25 23:38:06 +0000656
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000657//===----------------------------------------------------------------------===//
658// DeclContext Implementation
659//===----------------------------------------------------------------------===//
660
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000661bool DeclContext::classof(const Decl *D) {
662 switch (D->getKind()) {
Alexis Hunted053252010-05-30 07:21:58 +0000663#define DECL(NAME, BASE)
664#define DECL_CONTEXT(NAME) case Decl::NAME:
665#define DECL_CONTEXT_BASE(NAME)
666#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000667 return true;
668 default:
Alexis Hunted053252010-05-30 07:21:58 +0000669#define DECL(NAME, BASE)
670#define DECL_CONTEXT_BASE(NAME) \
671 if (D->getKind() >= Decl::first##NAME && \
672 D->getKind() <= Decl::last##NAME) \
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000673 return true;
Alexis Hunted053252010-05-30 07:21:58 +0000674#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000675 return false;
676 }
677}
678
Douglas Gregor9c832f72010-07-25 18:38:02 +0000679DeclContext::~DeclContext() { }
Douglas Gregor91f84212008-12-11 16:49:14 +0000680
Douglas Gregor7f737c02009-09-10 16:57:35 +0000681/// \brief Find the parent context of this context that will be
682/// used for unqualified name lookup.
683///
684/// Generally, the parent lookup context is the semantic context. However, for
685/// a friend function the parent lookup context is the lexical context, which
686/// is the class in which the friend is declared.
687DeclContext *DeclContext::getLookupParent() {
688 // FIXME: Find a better way to identify friends
689 if (isa<FunctionDecl>(this))
Sebastian Redl50c68252010-08-31 00:36:30 +0000690 if (getParent()->getRedeclContext()->isFileContext() &&
691 getLexicalParent()->getRedeclContext()->isRecord())
Douglas Gregor7f737c02009-09-10 16:57:35 +0000692 return getLexicalParent();
693
694 return getParent();
695}
696
Sebastian Redlbd595762010-08-31 20:53:31 +0000697bool DeclContext::isInlineNamespace() const {
698 return isNamespace() &&
699 cast<NamespaceDecl>(this)->isInline();
700}
701
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000702bool DeclContext::isDependentContext() const {
703 if (isFileContext())
704 return false;
705
Douglas Gregor2373c592009-05-31 09:31:02 +0000706 if (isa<ClassTemplatePartialSpecializationDecl>(this))
707 return true;
708
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000709 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
710 if (Record->getDescribedClassTemplate())
711 return true;
712
John McCallc62bb642010-03-24 05:22:00 +0000713 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000714 if (Function->getDescribedFunctionTemplate())
715 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000716
John McCallc62bb642010-03-24 05:22:00 +0000717 // Friend function declarations are dependent if their *lexical*
718 // context is dependent.
719 if (cast<Decl>(this)->getFriendObjectKind())
720 return getLexicalParent()->isDependentContext();
721 }
722
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000723 return getParent() && getParent()->isDependentContext();
724}
725
Douglas Gregor07665a62009-01-05 19:45:36 +0000726bool DeclContext::isTransparentContext() const {
727 if (DeclKind == Decl::Enum)
Douglas Gregor0bf31402010-10-08 23:50:27 +0000728 return !cast<EnumDecl>(this)->isScoped();
Douglas Gregor07665a62009-01-05 19:45:36 +0000729 else if (DeclKind == Decl::LinkageSpec)
730 return true;
Douglas Gregor07665a62009-01-05 19:45:36 +0000731
732 return false;
733}
734
John McCall5fe84122010-10-26 04:59:26 +0000735bool DeclContext::isExternCContext() const {
736 const DeclContext *DC = this;
737 while (DC->DeclKind != Decl::TranslationUnit) {
738 if (DC->DeclKind == Decl::LinkageSpec)
739 return cast<LinkageSpecDecl>(DC)->getLanguage()
740 == LinkageSpecDecl::lang_c;
741 DC = DC->getParent();
742 }
743 return false;
744}
745
Sebastian Redl50c68252010-08-31 00:36:30 +0000746bool DeclContext::Encloses(const DeclContext *DC) const {
Douglas Gregore985a3b2009-08-27 06:03:53 +0000747 if (getPrimaryContext() != this)
748 return getPrimaryContext()->Encloses(DC);
Mike Stump11289f42009-09-09 15:08:12 +0000749
Douglas Gregore985a3b2009-08-27 06:03:53 +0000750 for (; DC; DC = DC->getParent())
751 if (DC->getPrimaryContext() == this)
752 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000753 return false;
Douglas Gregore985a3b2009-08-27 06:03:53 +0000754}
755
Steve Naroff35c62ae2009-01-08 17:28:14 +0000756DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor91f84212008-12-11 16:49:14 +0000757 switch (DeclKind) {
Douglas Gregor91f84212008-12-11 16:49:14 +0000758 case Decl::TranslationUnit:
Douglas Gregor07665a62009-01-05 19:45:36 +0000759 case Decl::LinkageSpec:
Mike Stump11289f42009-09-09 15:08:12 +0000760 case Decl::Block:
Douglas Gregor91f84212008-12-11 16:49:14 +0000761 // There is only one DeclContext for these entities.
762 return this;
763
764 case Decl::Namespace:
765 // The original namespace is our primary context.
766 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
767
Douglas Gregor91f84212008-12-11 16:49:14 +0000768 case Decl::ObjCMethod:
769 return this;
770
771 case Decl::ObjCInterface:
Steve Naroff35c62ae2009-01-08 17:28:14 +0000772 case Decl::ObjCProtocol:
773 case Decl::ObjCCategory:
Douglas Gregor91f84212008-12-11 16:49:14 +0000774 // FIXME: Can Objective-C interfaces be forward-declared?
775 return this;
776
Steve Naroff35c62ae2009-01-08 17:28:14 +0000777 case Decl::ObjCImplementation:
778 case Decl::ObjCCategoryImpl:
779 return this;
780
Douglas Gregor91f84212008-12-11 16:49:14 +0000781 default:
Alexis Hunted053252010-05-30 07:21:58 +0000782 if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
Douglas Gregor67a65642009-02-17 23:15:12 +0000783 // If this is a tag type that has a definition or is currently
784 // being defined, that definition is our primary context.
John McCalle78aac42010-03-10 03:28:59 +0000785 TagDecl *Tag = cast<TagDecl>(this);
786 assert(isa<TagType>(Tag->TypeForDecl) ||
787 isa<InjectedClassNameType>(Tag->TypeForDecl));
788
789 if (TagDecl *Def = Tag->getDefinition())
790 return Def;
791
792 if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) {
793 const TagType *TagTy = cast<TagType>(Tag->TypeForDecl);
794 if (TagTy->isBeingDefined())
795 // FIXME: is it necessarily being defined in the decl
796 // that owns the type?
797 return TagTy->getDecl();
798 }
799
800 return Tag;
Douglas Gregor67a65642009-02-17 23:15:12 +0000801 }
802
Alexis Hunted053252010-05-30 07:21:58 +0000803 assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
Douglas Gregor91f84212008-12-11 16:49:14 +0000804 "Unknown DeclContext kind");
805 return this;
806 }
807}
808
809DeclContext *DeclContext::getNextContext() {
810 switch (DeclKind) {
Douglas Gregor91f84212008-12-11 16:49:14 +0000811 case Decl::Namespace:
812 // Return the next namespace
813 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
814
815 default:
Douglas Gregor91f84212008-12-11 16:49:14 +0000816 return 0;
817 }
818}
819
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000820std::pair<Decl *, Decl *>
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000821DeclContext::BuildDeclChain(const SmallVectorImpl<Decl*> &Decls) {
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000822 // Build up a chain of declarations via the Decl::NextDeclInContext field.
823 Decl *FirstNewDecl = 0;
824 Decl *PrevDecl = 0;
825 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
826 Decl *D = Decls[I];
827 if (PrevDecl)
828 PrevDecl->NextDeclInContext = D;
829 else
830 FirstNewDecl = D;
831
832 PrevDecl = D;
833 }
834
835 return std::make_pair(FirstNewDecl, PrevDecl);
836}
837
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000838/// \brief Load the declarations within this lexical storage from an
839/// external source.
Mike Stump11289f42009-09-09 15:08:12 +0000840void
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000841DeclContext::LoadLexicalDeclsFromExternalStorage() const {
842 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000843 assert(hasExternalLexicalStorage() && Source && "No external storage?");
844
Argyrios Kyrtzidis98d045e2010-07-30 10:03:23 +0000845 // Notify that we have a DeclContext that is initializing.
846 ExternalASTSource::Deserializing ADeclContext(Source);
847
Douglas Gregorf337ae92011-08-26 21:23:06 +0000848 // We may have already loaded just the fields of this record, in which case
849 // we remove all of the fields from the list. The fields will be reloaded
850 // from the external source as part of re-establishing the context.
851 if (const RecordDecl *RD = dyn_cast<RecordDecl>(this)) {
852 if (RD->LoadedFieldsFromExternalStorage) {
853 while (FirstDecl && isa<FieldDecl>(FirstDecl)) {
854 Decl *Next = FirstDecl->NextDeclInContext;
855 FirstDecl->NextDeclInContext = 0;
856 FirstDecl = Next;
857 }
858
859 if (!FirstDecl)
860 LastDecl = 0;
861 }
862 }
863
Douglas Gregor3d0adb32011-07-15 21:46:17 +0000864 // Load the external declarations, if any.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000865 SmallVector<Decl*, 64> Decls;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000866 ExternalLexicalStorage = false;
Douglas Gregor3d0adb32011-07-15 21:46:17 +0000867 switch (Source->FindExternalLexicalDecls(this, Decls)) {
868 case ELR_Success:
869 break;
870
871 case ELR_Failure:
872 case ELR_AlreadyLoaded:
873 return;
874 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000875
876 if (Decls.empty())
877 return;
878
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000879 // Splice the newly-read declarations into the beginning of the list
880 // of declarations.
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000881 Decl *ExternalFirst, *ExternalLast;
882 llvm::tie(ExternalFirst, ExternalLast) = BuildDeclChain(Decls);
883 ExternalLast->NextDeclInContext = FirstDecl;
884 FirstDecl = ExternalFirst;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000885 if (!LastDecl)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000886 LastDecl = ExternalLast;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000887}
888
John McCall75b960e2010-06-01 09:23:16 +0000889DeclContext::lookup_result
890ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
891 DeclarationName Name) {
892 ASTContext &Context = DC->getParentASTContext();
893 StoredDeclsMap *Map;
894 if (!(Map = DC->LookupPtr))
895 Map = DC->CreateStoredDeclsMap(Context);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000896
John McCall75b960e2010-06-01 09:23:16 +0000897 StoredDeclsList &List = (*Map)[Name];
898 assert(List.isNull());
899 (void) List;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000900
John McCall75b960e2010-06-01 09:23:16 +0000901 return DeclContext::lookup_result();
902}
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000903
John McCall75b960e2010-06-01 09:23:16 +0000904DeclContext::lookup_result
905ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +0000906 DeclarationName Name,
Argyrios Kyrtzidis94d3f9d2011-09-09 06:44:14 +0000907 ArrayRef<NamedDecl*> Decls) {
John McCall75b960e2010-06-01 09:23:16 +0000908 ASTContext &Context = DC->getParentASTContext();;
909
910 StoredDeclsMap *Map;
911 if (!(Map = DC->LookupPtr))
912 Map = DC->CreateStoredDeclsMap(Context);
913
914 StoredDeclsList &List = (*Map)[Name];
Argyrios Kyrtzidis94d3f9d2011-09-09 06:44:14 +0000915 for (ArrayRef<NamedDecl*>::iterator
916 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
John McCall75b960e2010-06-01 09:23:16 +0000917 if (List.isNull())
Argyrios Kyrtzidis94d3f9d2011-09-09 06:44:14 +0000918 List.setOnlyValue(*I);
John McCall75b960e2010-06-01 09:23:16 +0000919 else
Argyrios Kyrtzidis94d3f9d2011-09-09 06:44:14 +0000920 List.AddSubsequentDecl(*I);
John McCall75b960e2010-06-01 09:23:16 +0000921 }
922
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000923 return List.getLookupResult();
John McCall75b960e2010-06-01 09:23:16 +0000924}
925
Sebastian Redl66c5eef2010-07-27 00:17:23 +0000926DeclContext::decl_iterator DeclContext::noload_decls_begin() const {
927 return decl_iterator(FirstDecl);
928}
929
930DeclContext::decl_iterator DeclContext::noload_decls_end() const {
931 return decl_iterator();
932}
933
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000934DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000935 if (hasExternalLexicalStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000936 LoadLexicalDeclsFromExternalStorage();
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000937
Mike Stump11289f42009-09-09 15:08:12 +0000938 return decl_iterator(FirstDecl);
Douglas Gregorbcced4e2009-04-09 21:40:53 +0000939}
940
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000941DeclContext::decl_iterator DeclContext::decls_end() const {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000942 if (hasExternalLexicalStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000943 LoadLexicalDeclsFromExternalStorage();
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000944
Mike Stump11289f42009-09-09 15:08:12 +0000945 return decl_iterator();
Douglas Gregorbcced4e2009-04-09 21:40:53 +0000946}
947
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000948bool DeclContext::decls_empty() const {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +0000949 if (hasExternalLexicalStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000950 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +0000951
952 return !FirstDecl;
953}
954
John McCall84d87672009-12-10 09:41:52 +0000955void DeclContext::removeDecl(Decl *D) {
956 assert(D->getLexicalDeclContext() == this &&
957 "decl being removed from non-lexical context");
958 assert((D->NextDeclInContext || D == LastDecl) &&
959 "decl is not in decls list");
960
961 // Remove D from the decl chain. This is O(n) but hopefully rare.
962 if (D == FirstDecl) {
963 if (D == LastDecl)
964 FirstDecl = LastDecl = 0;
965 else
966 FirstDecl = D->NextDeclInContext;
967 } else {
968 for (Decl *I = FirstDecl; true; I = I->NextDeclInContext) {
969 assert(I && "decl not found in linked list");
970 if (I->NextDeclInContext == D) {
971 I->NextDeclInContext = D->NextDeclInContext;
972 if (D == LastDecl) LastDecl = I;
973 break;
974 }
975 }
976 }
977
978 // Mark that D is no longer in the decl chain.
979 D->NextDeclInContext = 0;
980
981 // Remove D from the lookup table if necessary.
982 if (isa<NamedDecl>(D)) {
983 NamedDecl *ND = cast<NamedDecl>(D);
984
Axel Naumanncb2c52f2011-08-26 14:06:12 +0000985 // Remove only decls that have a name
986 if (!ND->getDeclName()) return;
987
John McCallc62bb642010-03-24 05:22:00 +0000988 StoredDeclsMap *Map = getPrimaryContext()->LookupPtr;
989 if (!Map) return;
John McCall84d87672009-12-10 09:41:52 +0000990
John McCall84d87672009-12-10 09:41:52 +0000991 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
992 assert(Pos != Map->end() && "no lookup entry for decl");
993 Pos->second.remove(ND);
994 }
995}
996
John McCalld1e9d832009-08-11 06:59:38 +0000997void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner33f219d2009-02-20 00:56:18 +0000998 assert(D->getLexicalDeclContext() == this &&
999 "Decl inserted into wrong lexical context");
Mike Stump11289f42009-09-09 15:08:12 +00001000 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor020713e2009-01-09 19:42:16 +00001001 "Decl already inserted into a DeclContext");
1002
1003 if (FirstDecl) {
Chris Lattnerfcd33a62009-03-28 06:04:26 +00001004 LastDecl->NextDeclInContext = D;
Douglas Gregor020713e2009-01-09 19:42:16 +00001005 LastDecl = D;
1006 } else {
1007 FirstDecl = LastDecl = D;
1008 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +00001009
1010 // Notify a C++ record declaration that we've added a member, so it can
1011 // update it's class-specific state.
1012 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
1013 Record->addedMember(D);
John McCalld1e9d832009-08-11 06:59:38 +00001014}
1015
1016void DeclContext::addDecl(Decl *D) {
1017 addHiddenDecl(D);
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001018
1019 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001020 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor91f84212008-12-11 16:49:14 +00001021}
1022
Douglas Gregor07665a62009-01-05 19:45:36 +00001023/// buildLookup - Build the lookup data structure with all of the
1024/// declarations in DCtx (and any other contexts linked to it or
1025/// transparent contexts nested within it).
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001026void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregor07665a62009-01-05 19:45:36 +00001027 for (; DCtx; DCtx = DCtx->getNextContext()) {
Mike Stump11289f42009-09-09 15:08:12 +00001028 for (decl_iterator D = DCtx->decls_begin(),
1029 DEnd = DCtx->decls_end();
Douglas Gregord05cb412009-01-06 07:17:58 +00001030 D != DEnd; ++D) {
John McCalld1e9d832009-08-11 06:59:38 +00001031 // Insert this declaration into the lookup structure, but only
1032 // if it's semantically in its decl context. During non-lazy
1033 // lookup building, this is implicitly enforced by addDecl.
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001034 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
John McCalld1e9d832009-08-11 06:59:38 +00001035 if (D->getDeclContext() == DCtx)
1036 makeDeclVisibleInContextImpl(ND);
Douglas Gregor07665a62009-01-05 19:45:36 +00001037
Fariborz Jahanian3a039e32011-08-27 20:50:59 +00001038 // Insert any forward-declared Objective-C interface into the lookup
Ted Kremenek707ece62009-11-17 22:58:30 +00001039 // data structure.
1040 if (ObjCClassDecl *Class = dyn_cast<ObjCClassDecl>(*D))
Fariborz Jahanian3a039e32011-08-27 20:50:59 +00001041 makeDeclVisibleInContextImpl(Class->getForwardInterfaceDecl());
Ted Kremenek707ece62009-11-17 22:58:30 +00001042
Sebastian Redlbd595762010-08-31 20:53:31 +00001043 // If this declaration is itself a transparent declaration context or
1044 // inline namespace, add its members (recursively).
Douglas Gregor07665a62009-01-05 19:45:36 +00001045 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
Sebastian Redlbd595762010-08-31 20:53:31 +00001046 if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001047 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregor07665a62009-01-05 19:45:36 +00001048 }
1049 }
1050}
1051
Mike Stump11289f42009-09-09 15:08:12 +00001052DeclContext::lookup_result
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001053DeclContext::lookup(DeclarationName Name) {
Steve Naroff35c62ae2009-01-08 17:28:14 +00001054 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor91f84212008-12-11 16:49:14 +00001055 if (PrimaryContext != this)
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001056 return PrimaryContext->lookup(Name);
Douglas Gregor91f84212008-12-11 16:49:14 +00001057
John McCall75b960e2010-06-01 09:23:16 +00001058 if (hasExternalVisibleStorage()) {
1059 // Check to see if we've already cached the lookup results.
1060 if (LookupPtr) {
1061 StoredDeclsMap::iterator I = LookupPtr->find(Name);
1062 if (I != LookupPtr->end())
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001063 return I->second.getLookupResult();
John McCall75b960e2010-06-01 09:23:16 +00001064 }
1065
1066 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1067 return Source->FindExternalVisibleDeclsByName(this, Name);
1068 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001069
Douglas Gregor55297ac2008-12-23 00:26:44 +00001070 /// If there is no lookup data structure, build one now by walking
Douglas Gregor91f84212008-12-11 16:49:14 +00001071 /// all of the linked DeclContexts (in declaration order!) and
1072 /// inserting their values.
Douglas Gregor9615ec22009-04-09 17:29:08 +00001073 if (!LookupPtr) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001074 buildLookup(this);
Douglas Gregor91f84212008-12-11 16:49:14 +00001075
Douglas Gregor9615ec22009-04-09 17:29:08 +00001076 if (!LookupPtr)
Douglas Gregor10dc8aa2010-05-11 06:18:17 +00001077 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Douglas Gregor9615ec22009-04-09 17:29:08 +00001078 }
Douglas Gregor91f84212008-12-11 16:49:14 +00001079
John McCallc62bb642010-03-24 05:22:00 +00001080 StoredDeclsMap::iterator Pos = LookupPtr->find(Name);
1081 if (Pos == LookupPtr->end())
Douglas Gregor10dc8aa2010-05-11 06:18:17 +00001082 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001083 return Pos->second.getLookupResult();
Douglas Gregor91f84212008-12-11 16:49:14 +00001084}
1085
Mike Stump11289f42009-09-09 15:08:12 +00001086DeclContext::lookup_const_result
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001087DeclContext::lookup(DeclarationName Name) const {
1088 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor91f84212008-12-11 16:49:14 +00001089}
1090
Sebastian Redl50c68252010-08-31 00:36:30 +00001091DeclContext *DeclContext::getRedeclContext() {
Chris Lattner17a1bfa2009-03-27 19:19:59 +00001092 DeclContext *Ctx = this;
Sebastian Redlbd595762010-08-31 20:53:31 +00001093 // Skip through transparent contexts.
1094 while (Ctx->isTransparentContext())
Douglas Gregor6ad0ef52009-01-06 23:51:29 +00001095 Ctx = Ctx->getParent();
1096 return Ctx;
1097}
1098
Douglas Gregorf47b9112009-02-25 22:02:03 +00001099DeclContext *DeclContext::getEnclosingNamespaceContext() {
1100 DeclContext *Ctx = this;
1101 // Skip through non-namespace, non-translation-unit contexts.
Sebastian Redl4f08c962010-08-31 00:36:23 +00001102 while (!Ctx->isFileContext())
Douglas Gregorf47b9112009-02-25 22:02:03 +00001103 Ctx = Ctx->getParent();
1104 return Ctx->getPrimaryContext();
1105}
1106
Sebastian Redl50c68252010-08-31 00:36:30 +00001107bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
1108 // For non-file contexts, this is equivalent to Equals.
1109 if (!isFileContext())
1110 return O->Equals(this);
1111
1112 do {
1113 if (O->Equals(this))
1114 return true;
1115
1116 const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O);
1117 if (!NS || !NS->isInline())
1118 break;
1119 O = NS->getParent();
1120 } while (O);
1121
1122 return false;
1123}
1124
John McCall759e32b2009-08-31 22:39:49 +00001125void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable) {
Douglas Gregor67a65642009-02-17 23:15:12 +00001126 // FIXME: This feels like a hack. Should DeclarationName support
1127 // template-ids, or is there a better way to keep specializations
1128 // from being visible?
Douglas Gregorfd7c2252011-03-04 17:52:15 +00001129 if (isa<ClassTemplateSpecializationDecl>(D) || D->isTemplateParameter())
Douglas Gregor67a65642009-02-17 23:15:12 +00001130 return;
Eli Friedman73168192009-12-08 05:40:03 +00001131 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1132 if (FD->isFunctionTemplateSpecialization())
1133 return;
Douglas Gregor67a65642009-02-17 23:15:12 +00001134
Steve Naroff35c62ae2009-01-08 17:28:14 +00001135 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor91f84212008-12-11 16:49:14 +00001136 if (PrimaryContext != this) {
John McCall759e32b2009-08-31 22:39:49 +00001137 PrimaryContext->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor91f84212008-12-11 16:49:14 +00001138 return;
1139 }
1140
1141 // If we already have a lookup data structure, perform the insertion
Argyrios Kyrtzidise51e5542010-07-04 21:44:25 +00001142 // into it. If we haven't deserialized externally stored decls, deserialize
1143 // them so we can add the decl. Otherwise, be lazy and don't build that
1144 // structure until someone asks for it.
1145 if (LookupPtr || !Recoverable || hasExternalVisibleStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001146 makeDeclVisibleInContextImpl(D);
Douglas Gregor07665a62009-01-05 19:45:36 +00001147
Sebastian Redlbd595762010-08-31 20:53:31 +00001148 // If we are a transparent context or inline namespace, insert into our
1149 // parent context, too. This operation is recursive.
1150 if (isTransparentContext() || isInlineNamespace())
John McCall759e32b2009-08-31 22:39:49 +00001151 getParent()->makeDeclVisibleInContext(D, Recoverable);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00001152
1153 Decl *DCAsDecl = cast<Decl>(this);
1154 // Notify that a decl was made visible unless it's a Tag being defined.
1155 if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined()))
1156 if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
1157 L->AddedVisibleDecl(this, D);
Douglas Gregor91f84212008-12-11 16:49:14 +00001158}
1159
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001160void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) {
Douglas Gregor07665a62009-01-05 19:45:36 +00001161 // Skip unnamed declarations.
1162 if (!D->getDeclName())
1163 return;
1164
Douglas Gregor0de016d2011-05-06 23:32:38 +00001165 // Skip entities that can't be found by name lookup into a particular
1166 // context.
1167 if ((D->getIdentifierNamespace() == 0 && !isa<UsingDirectiveDecl>(D)) ||
1168 D->isTemplateParameter())
Douglas Gregor67a65642009-02-17 23:15:12 +00001169 return;
1170
Argyrios Kyrtzidise51e5542010-07-04 21:44:25 +00001171 ASTContext *C = 0;
1172 if (!LookupPtr) {
1173 C = &getParentASTContext();
1174 CreateStoredDeclsMap(*C);
1175 }
1176
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001177 // If there is an external AST source, load any declarations it knows about
1178 // with this declaration's name.
1179 // If the lookup table contains an entry about this name it means that we
1180 // have already checked the external source.
1181 if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
1182 if (hasExternalVisibleStorage() &&
1183 LookupPtr->find(D->getDeclName()) == LookupPtr->end())
1184 Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
1185
Douglas Gregor91f84212008-12-11 16:49:14 +00001186 // Insert this declaration into the map.
John McCallc62bb642010-03-24 05:22:00 +00001187 StoredDeclsList &DeclNameEntries = (*LookupPtr)[D->getDeclName()];
Chris Lattnercaae7162009-02-20 01:44:05 +00001188 if (DeclNameEntries.isNull()) {
1189 DeclNameEntries.setOnlyValue(D);
Chris Lattnerdfd6b3d2009-02-19 07:00:44 +00001190 return;
Douglas Gregor91f84212008-12-11 16:49:14 +00001191 }
Chris Lattner24e24d52009-02-20 00:55:03 +00001192
Chris Lattner29578f32009-02-20 01:10:07 +00001193 // If it is possible that this is a redeclaration, check to see if there is
1194 // already a decl for which declarationReplaces returns true. If there is
1195 // one, just replace it and return.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001196 if (DeclNameEntries.HandleRedeclaration(D))
Chris Lattnercaae7162009-02-20 01:44:05 +00001197 return;
Mike Stump11289f42009-09-09 15:08:12 +00001198
Chris Lattnerdfd6b3d2009-02-19 07:00:44 +00001199 // Put this declaration into the appropriate slot.
Chris Lattnercaae7162009-02-20 01:44:05 +00001200 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor91f84212008-12-11 16:49:14 +00001201}
Douglas Gregor889ceb72009-02-03 19:21:40 +00001202
1203/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
1204/// this context.
Mike Stump11289f42009-09-09 15:08:12 +00001205DeclContext::udir_iterator_range
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001206DeclContext::getUsingDirectives() const {
1207 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor889ceb72009-02-03 19:21:40 +00001208 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
1209 reinterpret_cast<udir_iterator>(Result.second));
1210}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001211
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001212//===----------------------------------------------------------------------===//
1213// Creation and Destruction of StoredDeclsMaps. //
1214//===----------------------------------------------------------------------===//
1215
John McCallc62bb642010-03-24 05:22:00 +00001216StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
1217 assert(!LookupPtr && "context already has a decls map");
1218 assert(getPrimaryContext() == this &&
1219 "creating decls map on non-primary context");
1220
1221 StoredDeclsMap *M;
1222 bool Dependent = isDependentContext();
1223 if (Dependent)
1224 M = new DependentStoredDeclsMap();
1225 else
1226 M = new StoredDeclsMap();
1227 M->Previous = C.LastSDM;
1228 C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
1229 LookupPtr = M;
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001230 return M;
1231}
1232
1233void ASTContext::ReleaseDeclContextMaps() {
John McCallc62bb642010-03-24 05:22:00 +00001234 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1235 // pointer because the subclass doesn't add anything that needs to
1236 // be deleted.
John McCallc62bb642010-03-24 05:22:00 +00001237 StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1238}
1239
1240void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1241 while (Map) {
1242 // Advance the iteration before we invalidate memory.
1243 llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1244
1245 if (Dependent)
1246 delete static_cast<DependentStoredDeclsMap*>(Map);
1247 else
1248 delete Map;
1249
1250 Map = Next.getPointer();
1251 Dependent = Next.getInt();
1252 }
1253}
1254
John McCallc62bb642010-03-24 05:22:00 +00001255DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1256 DeclContext *Parent,
1257 const PartialDiagnostic &PDiag) {
1258 assert(Parent->isDependentContext()
1259 && "cannot iterate dependent diagnostics of non-dependent context");
1260 Parent = Parent->getPrimaryContext();
1261 if (!Parent->LookupPtr)
1262 Parent->CreateStoredDeclsMap(C);
1263
1264 DependentStoredDeclsMap *Map
1265 = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr);
1266
Douglas Gregora55530e2010-03-29 23:56:53 +00001267 // Allocate the copy of the PartialDiagnostic via the ASTContext's
Douglas Gregor89336232010-03-29 23:34:08 +00001268 // BumpPtrAllocator, rather than the ASTContext itself.
Douglas Gregora55530e2010-03-29 23:56:53 +00001269 PartialDiagnostic::Storage *DiagStorage = 0;
1270 if (PDiag.hasStorage())
1271 DiagStorage = new (C) PartialDiagnostic::Storage;
1272
1273 DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
John McCallc62bb642010-03-24 05:22:00 +00001274
1275 // TODO: Maybe we shouldn't reverse the order during insertion.
1276 DD->NextDiagnostic = Map->FirstDiagnostic;
1277 Map->FirstDiagnostic = DD;
1278
1279 return DD;
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001280}