blob: 27d05baca2a61081716059c6ee710275ed63c5c2 [file] [log] [blame]
Eli Friedman56d29372008-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 Gregor64650af2009-02-02 23:39:07 +000015#include "clang/AST/Decl.h"
Douglas Gregorc2ee10d2009-04-07 17:20:56 +000016#include "clang/AST/DeclContextInternals.h"
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +000017#include "clang/AST/DeclCXX.h"
John McCall92b7f702010-03-11 07:50:04 +000018#include "clang/AST/DeclFriend.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000019#include "clang/AST/DeclObjC.h"
20#include "clang/AST/DeclTemplate.h"
John McCall0c01d182010-03-24 05:22:00 +000021#include "clang/AST/DependentDiagnostic.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000022#include "clang/AST/ExternalASTSource.h"
Eli Friedman56d29372008-06-07 16:52:53 +000023#include "clang/AST/ASTContext.h"
Douglas Gregor44b43212008-12-11 16:49:14 +000024#include "clang/AST/Type.h"
Sebastian Redld3a413d2009-04-26 20:35:05 +000025#include "clang/AST/Stmt.h"
26#include "clang/AST/StmtCXX.h"
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +000027#include "clang/AST/ASTMutationListener.h"
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000028#include "clang/Basic/TargetInfo.h"
Eli Friedman56d29372008-06-07 16:52:53 +000029#include "llvm/ADT/DenseMap.h"
Chris Lattner49f28ca2009-03-05 08:00:35 +000030#include "llvm/Support/raw_ostream.h"
Douglas Gregor6ed40e32008-12-23 21:05:05 +000031#include <algorithm>
Eli Friedman56d29372008-06-07 16:52:53 +000032using namespace clang;
33
34//===----------------------------------------------------------------------===//
35// Statistics
36//===----------------------------------------------------------------------===//
37
Sean Hunt9a555912010-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 Friedman56d29372008-06-07 16:52:53 +000041
42static bool StatSwitch = false;
43
Eli Friedman56d29372008-06-07 16:52:53 +000044const char *Decl::getDeclKindName() const {
45 switch (DeclKind) {
Sean Hunt9a555912010-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 Friedman56d29372008-06-07 16:52:53 +000050 }
51}
52
Douglas Gregor42738572010-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 Naroff0a473932009-01-20 19:53:53 +000063const char *DeclContext::getDeclKindName() const {
64 switch (DeclKind) {
Sean Hunt9a555912010-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 Naroff0a473932009-01-20 19:53:53 +000069 }
70}
71
Eli Friedman56d29372008-06-07 16:52:53 +000072bool Decl::CollectingStats(bool Enable) {
Kovarththanan Rajaratnam2024f4d2009-11-29 14:54:35 +000073 if (Enable) StatSwitch = true;
Eli Friedman56d29372008-06-07 16:52:53 +000074 return StatSwitch;
75}
76
77void Decl::PrintStats() {
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000078 llvm::errs() << "\n*** Decl Stats:\n";
Mike Stump1eb44332009-09-09 15:08:12 +000079
Douglas Gregor64650af2009-02-02 23:39:07 +000080 int totalDecls = 0;
Sean Hunt9a555912010-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 Carruthb43c8ec2011-07-04 06:13:27 +000084 llvm::errs() << " " << totalDecls << " decls total.\n";
Mike Stump1eb44332009-09-09 15:08:12 +000085
Douglas Gregor64650af2009-02-02 23:39:07 +000086 int totalBytes = 0;
Sean Hunt9a555912010-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 Carruthb43c8ec2011-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 Gregor64650af2009-02-02 23:39:07 +000094 }
Sean Hunt9a555912010-05-30 07:21:58 +000095#define ABSTRACT_DECL(DECL)
96#include "clang/AST/DeclNodes.inc"
Mike Stump1eb44332009-09-09 15:08:12 +000097
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000098 llvm::errs() << "Total bytes = " << totalBytes << "\n";
Eli Friedman56d29372008-06-07 16:52:53 +000099}
100
Sean Hunt9a555912010-05-30 07:21:58 +0000101void Decl::add(Kind k) {
Eli Friedman56d29372008-06-07 16:52:53 +0000102 switch (k) {
Sean Hunt9a555912010-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 Friedman56d29372008-06-07 16:52:53 +0000107 }
108}
109
Anders Carlsson67e33202009-06-13 00:08:58 +0000110bool Decl::isTemplateParameterPack() const {
111 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
112 return TTP->isParameterPack();
Douglas Gregor10738d32010-12-23 23:51:58 +0000113 if (const NonTypeTemplateParmDecl *NTTP
Douglas Gregor61c4d282011-01-05 15:48:55 +0000114 = dyn_cast<NonTypeTemplateParmDecl>(this))
Douglas Gregor10738d32010-12-23 23:51:58 +0000115 return NTTP->isParameterPack();
Douglas Gregor61c4d282011-01-05 15:48:55 +0000116 if (const TemplateTemplateParmDecl *TTP
117 = dyn_cast<TemplateTemplateParmDecl>(this))
118 return TTP->isParameterPack();
Anders Carlsson67e33202009-06-13 00:08:58 +0000119 return false;
120}
121
Douglas Gregor1fe85ea2011-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 Gregore53060f2009-06-25 22:08:12 +0000129bool Decl::isFunctionOrFunctionTemplate() const {
John McCall9488ea12009-11-17 05:59:44 +0000130 if (const UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(this))
Anders Carlsson58badb72009-06-26 05:26:50 +0000131 return UD->getTargetDecl()->isFunctionOrFunctionTemplate();
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Douglas Gregore53060f2009-06-25 22:08:12 +0000133 return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this);
134}
135
Douglas Gregor79c22782010-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 Gregor4c3e0ee2011-02-17 08:47:29 +0000146
Eli Friedman56d29372008-06-07 16:52:53 +0000147//===----------------------------------------------------------------------===//
Chris Lattner49f28ca2009-03-05 08:00:35 +0000148// PrettyStackTraceDecl Implementation
149//===----------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000150
Chris Lattner5f9e2722011-07-23 10:55:15 +0000151void PrettyStackTraceDecl::print(raw_ostream &OS) const {
Chris Lattner49f28ca2009-03-05 08:00:35 +0000152 SourceLocation TheLoc = Loc;
153 if (TheLoc.isInvalid() && TheDecl)
154 TheLoc = TheDecl->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000155
Chris Lattner49f28ca2009-03-05 08:00:35 +0000156 if (TheLoc.isValid()) {
157 TheLoc.print(OS, SM);
158 OS << ": ";
159 }
160
161 OS << Message;
162
Daniel Dunbarc5236562009-11-21 09:05:59 +0000163 if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
Chris Lattner49f28ca2009-03-05 08:00:35 +0000164 OS << " '" << DN->getQualifiedNameAsString() << '\'';
165 OS << '\n';
166}
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Chris Lattner49f28ca2009-03-05 08:00:35 +0000168//===----------------------------------------------------------------------===//
Eli Friedman56d29372008-06-07 16:52:53 +0000169// Decl Implementation
170//===----------------------------------------------------------------------===//
171
Douglas Gregorda2142f2011-02-19 18:51:44 +0000172// Out-of-line virtual method providing a home for Decl.
173Decl::~Decl() { }
Douglas Gregorf4a03cc2011-02-17 07:02:32 +0000174
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000175void Decl::setDeclContext(DeclContext *DC) {
Chris Lattneree219fd2009-03-29 06:06:59 +0000176 DeclCtx = DC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000177}
178
179void Decl::setLexicalDeclContext(DeclContext *DC) {
180 if (DC == getLexicalDeclContext())
181 return;
182
183 if (isInSemaDC()) {
Ted Kremenek94a39002009-12-01 00:07:10 +0000184 MultipleDC *MDC = new (getASTContext()) MultipleDC();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000185 MDC->SemanticDC = getDeclContext();
186 MDC->LexicalDC = DC;
Chris Lattneree219fd2009-03-29 06:06:59 +0000187 DeclCtx = MDC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000188 } else {
189 getMultipleDC()->LexicalDC = DC;
190 }
191}
192
John McCall9aeed322009-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 Kyrtzidis3708b3d2009-06-29 17:38:40 +0000204TranslationUnitDecl *Decl::getTranslationUnitDecl() {
Argyrios Kyrtzidis9b346692009-06-30 02:34:53 +0000205 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
206 return TUD;
207
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000208 DeclContext *DC = getDeclContext();
209 assert(DC && "This decl is not contained in a translation unit!");
Mike Stump1eb44332009-09-09 15:08:12 +0000210
Argyrios Kyrtzidis3708b3d2009-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 Stump1eb44332009-09-09 15:08:12 +0000215
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000216 return cast<TranslationUnitDecl>(DC);
217}
218
219ASTContext &Decl::getASTContext() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000220 return getTranslationUnitDecl()->getASTContext();
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000221}
222
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +0000223ASTMutationListener *Decl::getASTMutationListener() const {
224 return getASTContext().getASTMutationListener();
225}
226
Douglas Gregorc070cc62010-06-17 23:14:26 +0000227bool Decl::isUsed(bool CheckUsedAttr) const {
Tanya Lattner12ead492010-02-17 02:17:21 +0000228 if (Used)
229 return true;
230
231 // Check for used attribute.
Douglas Gregorc070cc62010-06-17 23:14:26 +0000232 if (CheckUsedAttr && hasAttr<UsedAttr>())
Tanya Lattner12ead492010-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 Gregorc070cc62010-06-17 23:14:26 +0000237 if ((CheckUsedAttr && I->hasAttr<UsedAttr>()) || I->Used)
Tanya Lattner12ead492010-02-17 02:17:21 +0000238 return true;
239 }
240
241 return false;
242}
243
Argyrios Kyrtzidis6b6b42a2011-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 Gregor0a0d2b12011-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 Lattner5f9e2722011-07-23 10:55:15 +0000268 StringRef TargetPlatform = Context.Target.getPlatformName();
269 StringRef PrettyPlatformName
Douglas Gregor0a0d2b12011-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 Gregorb53e4172011-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 Gregor0a0d2b12011-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 Gregor0a0d2b12011-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 Lattner12ead492010-02-17 02:17:21 +0000415
Chris Lattner769dbdf2009-03-27 20:18:19 +0000416unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
417 switch (DeclKind) {
John McCall9488ea12009-11-17 05:59:44 +0000418 case Function:
419 case CXXMethod:
420 case CXXConstructor:
421 case CXXDestructor:
422 case CXXConversion:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000423 case EnumConstant:
424 case Var:
425 case ImplicitParam:
426 case ParmVar:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000427 case NonTypeTemplateParm:
428 case ObjCMethod:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000429 case ObjCProperty:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000430 return IDNS_Ordinary;
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000431 case Label:
432 return IDNS_Label;
Francois Pichet87c2e122010-11-21 06:08:52 +0000433 case IndirectField:
434 return IDNS_Ordinary | IDNS_Member;
435
John McCall0d6b1642010-04-23 18:46:30 +0000436 case ObjCCompatibleAlias:
437 case ObjCInterface:
438 return IDNS_Ordinary | IDNS_Type;
439
440 case Typedef:
Richard Smith162e1c12011-04-15 14:24:37 +0000441 case TypeAlias:
Richard Smith3e4c6c42011-05-05 21:57:07 +0000442 case TypeAliasTemplate:
John McCall0d6b1642010-04-23 18:46:30 +0000443 case UnresolvedUsingTypename:
444 case TemplateTypeParm:
445 return IDNS_Ordinary | IDNS_Type;
446
John McCall9488ea12009-11-17 05:59:44 +0000447 case UsingShadow:
448 return 0; // we'll actually overwrite this later
449
John McCall7ba107a2009-11-18 02:36:19 +0000450 case UnresolvedUsingValue:
John McCall7ba107a2009-11-18 02:36:19 +0000451 return IDNS_Ordinary | IDNS_Using;
John McCall9488ea12009-11-17 05:59:44 +0000452
453 case Using:
454 return IDNS_Using;
455
Chris Lattner769dbdf2009-03-27 20:18:19 +0000456 case ObjCProtocol:
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000457 return IDNS_ObjCProtocol;
Mike Stump1eb44332009-09-09 15:08:12 +0000458
Chris Lattner769dbdf2009-03-27 20:18:19 +0000459 case Field:
460 case ObjCAtDefsField:
461 case ObjCIvar:
462 return IDNS_Member;
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Chris Lattner769dbdf2009-03-27 20:18:19 +0000464 case Record:
465 case CXXRecord:
466 case Enum:
John McCall0d6b1642010-04-23 18:46:30 +0000467 return IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000468
Chris Lattner769dbdf2009-03-27 20:18:19 +0000469 case Namespace:
John McCall0d6b1642010-04-23 18:46:30 +0000470 case NamespaceAlias:
471 return IDNS_Namespace;
472
Chris Lattner769dbdf2009-03-27 20:18:19 +0000473 case FunctionTemplate:
John McCall0d6b1642010-04-23 18:46:30 +0000474 return IDNS_Ordinary;
475
Chris Lattner769dbdf2009-03-27 20:18:19 +0000476 case ClassTemplate:
477 case TemplateTemplateParm:
John McCall0d6b1642010-04-23 18:46:30 +0000478 return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Chris Lattner769dbdf2009-03-27 20:18:19 +0000480 // Never have names.
John McCall02cace72009-08-28 07:59:38 +0000481 case Friend:
John McCalldd4a3b02009-09-16 22:47:08 +0000482 case FriendTemplate:
Abramo Bagnara6206d532010-06-05 05:09:32 +0000483 case AccessSpec:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000484 case LinkageSpec:
485 case FileScopeAsm:
486 case StaticAssert:
487 case ObjCClass:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000488 case ObjCPropertyImpl:
489 case ObjCForwardProtocol:
490 case Block:
491 case TranslationUnit:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000492
Chris Lattner769dbdf2009-03-27 20:18:19 +0000493 case UsingDirective:
494 case ClassTemplateSpecialization:
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000495 case ClassTemplatePartialSpecialization:
Douglas Gregorbd4187b2010-04-22 23:19:50 +0000496 case ObjCImplementation:
497 case ObjCCategory:
498 case ObjCCategoryImpl:
499 // Never looked up by name.
Chris Lattner769dbdf2009-03-27 20:18:19 +0000500 return 0;
501 }
John McCall9488ea12009-11-17 05:59:44 +0000502
503 return 0;
Eli Friedman56d29372008-06-07 16:52:53 +0000504}
505
Sean Huntcf807c42010-08-18 23:23:40 +0000506void Decl::setAttrs(const AttrVec &attrs) {
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000507 assert(!HasAttrs && "Decl already contains attrs.");
508
Sean Huntcf807c42010-08-18 23:23:40 +0000509 AttrVec &AttrBlank = getASTContext().getDeclAttrs(this);
510 assert(AttrBlank.empty() && "HasAttrs was wrong?");
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000511
512 AttrBlank = attrs;
513 HasAttrs = true;
514}
515
Sean Huntcf807c42010-08-18 23:23:40 +0000516void Decl::dropAttrs() {
Eli Friedman56d29372008-06-07 16:52:53 +0000517 if (!HasAttrs) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000518
Eli Friedman56d29372008-06-07 16:52:53 +0000519 HasAttrs = false;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000520 getASTContext().eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000521}
522
Sean Huntcf807c42010-08-18 23:23:40 +0000523const AttrVec &Decl::getAttrs() const {
524 assert(HasAttrs && "No attrs to get!");
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000525 return getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000526}
527
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000528void Decl::swapAttrs(Decl *RHS) {
Eli Friedman56d29372008-06-07 16:52:53 +0000529 bool HasLHSAttr = this->HasAttrs;
530 bool HasRHSAttr = RHS->HasAttrs;
Mike Stump1eb44332009-09-09 15:08:12 +0000531
Eli Friedman56d29372008-06-07 16:52:53 +0000532 // Usually, neither decl has attrs, nothing to do.
533 if (!HasLHSAttr && !HasRHSAttr) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000534
Eli Friedman56d29372008-06-07 16:52:53 +0000535 // If 'this' has no attrs, swap the other way.
536 if (!HasLHSAttr)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000537 return RHS->swapAttrs(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000538
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000539 ASTContext &Context = getASTContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Eli Friedman56d29372008-06-07 16:52:53 +0000541 // Handle the case when both decls have attrs.
542 if (HasRHSAttr) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000543 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman56d29372008-06-07 16:52:53 +0000544 return;
545 }
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Eli Friedman56d29372008-06-07 16:52:53 +0000547 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor68584ed2009-06-18 16:11:24 +0000548 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
549 Context.eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000550 this->HasAttrs = false;
551 RHS->HasAttrs = true;
552}
553
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000554Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000555 Decl::Kind DK = D->getDeclKind();
556 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000557#define DECL(NAME, BASE)
558#define DECL_CONTEXT(NAME) \
559 case Decl::NAME: \
560 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
561#define DECL_CONTEXT_BASE(NAME)
562#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000563 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000564#define DECL(NAME, BASE)
565#define DECL_CONTEXT_BASE(NAME) \
566 if (DK >= first##NAME && DK <= last##NAME) \
567 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
568#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000569 assert(false && "a decl that inherits DeclContext isn't handled");
570 return 0;
571 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000572}
573
574DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000575 Decl::Kind DK = D->getKind();
576 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000577#define DECL(NAME, BASE)
578#define DECL_CONTEXT(NAME) \
579 case Decl::NAME: \
580 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
581#define DECL_CONTEXT_BASE(NAME)
582#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000583 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000584#define DECL(NAME, BASE)
585#define DECL_CONTEXT_BASE(NAME) \
586 if (DK >= first##NAME && DK <= last##NAME) \
587 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
588#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000589 assert(false && "a decl that inherits DeclContext isn't handled");
590 return 0;
591 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000592}
593
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000594SourceLocation Decl::getBodyRBrace() const {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000595 // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
596 // FunctionDecl stores EndRangeLoc for this purpose.
597 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
598 const FunctionDecl *Definition;
599 if (FD->hasBody(Definition))
600 return Definition->getSourceRange().getEnd();
601 return SourceLocation();
602 }
603
Argyrios Kyrtzidis6717ef42010-07-07 11:31:27 +0000604 if (Stmt *Body = getBody())
605 return Body->getSourceRange().getEnd();
606
607 return SourceLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +0000608}
609
Anders Carlsson1329c272009-03-25 23:38:06 +0000610void Decl::CheckAccessDeclContext() const {
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000611#ifndef NDEBUG
John McCall46460a62010-01-20 21:53:11 +0000612 // Suppress this check if any of the following hold:
613 // 1. this is the translation unit (and thus has no parent)
614 // 2. this is a template parameter (and thus doesn't belong to its context)
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000615 // 3. this is a non-type template parameter
616 // 4. the context is not a record
617 // 5. it's invalid
618 // 6. it's a C++0x static_assert.
Anders Carlsson35eda442009-08-29 20:47:47 +0000619 if (isa<TranslationUnitDecl>(this) ||
Argyrios Kyrtzidis04aed0e2010-07-02 11:55:44 +0000620 isa<TemplateTypeParmDecl>(this) ||
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000621 isa<NonTypeTemplateParmDecl>(this) ||
Douglas Gregorfdd8ab12010-02-22 17:53:38 +0000622 !isa<CXXRecordDecl>(getDeclContext()) ||
Argyrios Kyrtzidis65b63ec2010-09-08 21:32:35 +0000623 isInvalidDecl() ||
624 isa<StaticAssertDecl>(this) ||
625 // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
626 // as DeclContext (?).
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000627 isa<ParmVarDecl>(this) ||
628 // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
629 // AS_none as access specifier.
630 isa<CXXRecordDecl>(this))
Anders Carlsson35eda442009-08-29 20:47:47 +0000631 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000632
633 assert(Access != AS_none &&
Anders Carlsson1329c272009-03-25 23:38:06 +0000634 "Access specifier is AS_none inside a record decl");
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000635#endif
Anders Carlsson1329c272009-03-25 23:38:06 +0000636}
637
John McCallaab9e312011-02-22 22:25:23 +0000638DeclContext *Decl::getNonClosureContext() {
639 DeclContext *DC = getDeclContext();
640
641 // This is basically "while (DC->isClosure()) DC = DC->getParent();"
642 // except that it's significantly more efficient to cast to a known
643 // decl type and call getDeclContext() than to call getParent().
John McCall7b3f8532011-06-23 21:18:31 +0000644 while (isa<BlockDecl>(DC))
645 DC = cast<BlockDecl>(DC)->getDeclContext();
John McCallaab9e312011-02-22 22:25:23 +0000646
647 assert(!DC->isClosure());
648 return DC;
649}
Anders Carlsson1329c272009-03-25 23:38:06 +0000650
Eli Friedman56d29372008-06-07 16:52:53 +0000651//===----------------------------------------------------------------------===//
652// DeclContext Implementation
653//===----------------------------------------------------------------------===//
654
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000655bool DeclContext::classof(const Decl *D) {
656 switch (D->getKind()) {
Sean Hunt9a555912010-05-30 07:21:58 +0000657#define DECL(NAME, BASE)
658#define DECL_CONTEXT(NAME) case Decl::NAME:
659#define DECL_CONTEXT_BASE(NAME)
660#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000661 return true;
662 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000663#define DECL(NAME, BASE)
664#define DECL_CONTEXT_BASE(NAME) \
665 if (D->getKind() >= Decl::first##NAME && \
666 D->getKind() <= Decl::last##NAME) \
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000667 return true;
Sean Hunt9a555912010-05-30 07:21:58 +0000668#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000669 return false;
670 }
671}
672
Douglas Gregora2da7802010-07-25 18:38:02 +0000673DeclContext::~DeclContext() { }
Douglas Gregor44b43212008-12-11 16:49:14 +0000674
Douglas Gregore942bbe2009-09-10 16:57:35 +0000675/// \brief Find the parent context of this context that will be
676/// used for unqualified name lookup.
677///
678/// Generally, the parent lookup context is the semantic context. However, for
679/// a friend function the parent lookup context is the lexical context, which
680/// is the class in which the friend is declared.
681DeclContext *DeclContext::getLookupParent() {
682 // FIXME: Find a better way to identify friends
683 if (isa<FunctionDecl>(this))
Sebastian Redl7a126a42010-08-31 00:36:30 +0000684 if (getParent()->getRedeclContext()->isFileContext() &&
685 getLexicalParent()->getRedeclContext()->isRecord())
Douglas Gregore942bbe2009-09-10 16:57:35 +0000686 return getLexicalParent();
687
688 return getParent();
689}
690
Sebastian Redl410c4f22010-08-31 20:53:31 +0000691bool DeclContext::isInlineNamespace() const {
692 return isNamespace() &&
693 cast<NamespaceDecl>(this)->isInline();
694}
695
Douglas Gregorbc221632009-05-28 16:34:51 +0000696bool DeclContext::isDependentContext() const {
697 if (isFileContext())
698 return false;
699
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000700 if (isa<ClassTemplatePartialSpecializationDecl>(this))
701 return true;
702
Douglas Gregorbc221632009-05-28 16:34:51 +0000703 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
704 if (Record->getDescribedClassTemplate())
705 return true;
706
John McCall0c01d182010-03-24 05:22:00 +0000707 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregorbc221632009-05-28 16:34:51 +0000708 if (Function->getDescribedFunctionTemplate())
709 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000710
John McCall0c01d182010-03-24 05:22:00 +0000711 // Friend function declarations are dependent if their *lexical*
712 // context is dependent.
713 if (cast<Decl>(this)->getFriendObjectKind())
714 return getLexicalParent()->isDependentContext();
715 }
716
Douglas Gregorbc221632009-05-28 16:34:51 +0000717 return getParent() && getParent()->isDependentContext();
718}
719
Douglas Gregor074149e2009-01-05 19:45:36 +0000720bool DeclContext::isTransparentContext() const {
721 if (DeclKind == Decl::Enum)
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000722 return !cast<EnumDecl>(this)->isScoped();
Douglas Gregor074149e2009-01-05 19:45:36 +0000723 else if (DeclKind == Decl::LinkageSpec)
724 return true;
Douglas Gregor074149e2009-01-05 19:45:36 +0000725
726 return false;
727}
728
John McCallac65c622010-10-26 04:59:26 +0000729bool DeclContext::isExternCContext() const {
730 const DeclContext *DC = this;
731 while (DC->DeclKind != Decl::TranslationUnit) {
732 if (DC->DeclKind == Decl::LinkageSpec)
733 return cast<LinkageSpecDecl>(DC)->getLanguage()
734 == LinkageSpecDecl::lang_c;
735 DC = DC->getParent();
736 }
737 return false;
738}
739
Sebastian Redl7a126a42010-08-31 00:36:30 +0000740bool DeclContext::Encloses(const DeclContext *DC) const {
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000741 if (getPrimaryContext() != this)
742 return getPrimaryContext()->Encloses(DC);
Mike Stump1eb44332009-09-09 15:08:12 +0000743
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000744 for (; DC; DC = DC->getParent())
745 if (DC->getPrimaryContext() == this)
746 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000747 return false;
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000748}
749
Steve Naroff0701bbb2009-01-08 17:28:14 +0000750DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000751 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000752 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000753 case Decl::LinkageSpec:
Mike Stump1eb44332009-09-09 15:08:12 +0000754 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000755 // There is only one DeclContext for these entities.
756 return this;
757
758 case Decl::Namespace:
759 // The original namespace is our primary context.
760 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
761
Douglas Gregor44b43212008-12-11 16:49:14 +0000762 case Decl::ObjCMethod:
763 return this;
764
765 case Decl::ObjCInterface:
Steve Naroff0701bbb2009-01-08 17:28:14 +0000766 case Decl::ObjCProtocol:
767 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000768 // FIXME: Can Objective-C interfaces be forward-declared?
769 return this;
770
Steve Naroff0701bbb2009-01-08 17:28:14 +0000771 case Decl::ObjCImplementation:
772 case Decl::ObjCCategoryImpl:
773 return this;
774
Douglas Gregor44b43212008-12-11 16:49:14 +0000775 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000776 if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000777 // If this is a tag type that has a definition or is currently
778 // being defined, that definition is our primary context.
John McCall3cb0ebd2010-03-10 03:28:59 +0000779 TagDecl *Tag = cast<TagDecl>(this);
780 assert(isa<TagType>(Tag->TypeForDecl) ||
781 isa<InjectedClassNameType>(Tag->TypeForDecl));
782
783 if (TagDecl *Def = Tag->getDefinition())
784 return Def;
785
786 if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) {
787 const TagType *TagTy = cast<TagType>(Tag->TypeForDecl);
788 if (TagTy->isBeingDefined())
789 // FIXME: is it necessarily being defined in the decl
790 // that owns the type?
791 return TagTy->getDecl();
792 }
793
794 return Tag;
Douglas Gregorcc636682009-02-17 23:15:12 +0000795 }
796
Sean Hunt9a555912010-05-30 07:21:58 +0000797 assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
Douglas Gregor44b43212008-12-11 16:49:14 +0000798 "Unknown DeclContext kind");
799 return this;
800 }
801}
802
803DeclContext *DeclContext::getNextContext() {
804 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000805 case Decl::Namespace:
806 // Return the next namespace
807 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
808
809 default:
Douglas Gregor44b43212008-12-11 16:49:14 +0000810 return 0;
811 }
812}
813
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000814std::pair<Decl *, Decl *>
Chris Lattner5f9e2722011-07-23 10:55:15 +0000815DeclContext::BuildDeclChain(const SmallVectorImpl<Decl*> &Decls) {
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000816 // Build up a chain of declarations via the Decl::NextDeclInContext field.
817 Decl *FirstNewDecl = 0;
818 Decl *PrevDecl = 0;
819 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
820 Decl *D = Decls[I];
821 if (PrevDecl)
822 PrevDecl->NextDeclInContext = D;
823 else
824 FirstNewDecl = D;
825
826 PrevDecl = D;
827 }
828
829 return std::make_pair(FirstNewDecl, PrevDecl);
830}
831
Douglas Gregor2cf26342009-04-09 22:27:44 +0000832/// \brief Load the declarations within this lexical storage from an
833/// external source.
Mike Stump1eb44332009-09-09 15:08:12 +0000834void
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000835DeclContext::LoadLexicalDeclsFromExternalStorage() const {
836 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000837 assert(hasExternalLexicalStorage() && Source && "No external storage?");
838
Argyrios Kyrtzidis0dbbc042010-07-30 10:03:23 +0000839 // Notify that we have a DeclContext that is initializing.
840 ExternalASTSource::Deserializing ADeclContext(Source);
841
Douglas Gregorba6ffaf2011-07-15 21:46:17 +0000842 // Load the external declarations, if any.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000843 SmallVector<Decl*, 64> Decls;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000844 ExternalLexicalStorage = false;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +0000845 switch (Source->FindExternalLexicalDecls(this, Decls)) {
846 case ELR_Success:
847 break;
848
849 case ELR_Failure:
850 case ELR_AlreadyLoaded:
851 return;
852 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000853
854 if (Decls.empty())
855 return;
856
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000857 // We may have already loaded just the fields of this record, in which case
858 // don't add the decls, just replace the FirstDecl/LastDecl chain.
859 if (const RecordDecl *RD = dyn_cast<RecordDecl>(this))
860 if (RD->LoadedFieldsFromExternalStorage) {
861 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
862 return;
863 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000864
865 // Splice the newly-read declarations into the beginning of the list
866 // of declarations.
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000867 Decl *ExternalFirst, *ExternalLast;
868 llvm::tie(ExternalFirst, ExternalLast) = BuildDeclChain(Decls);
869 ExternalLast->NextDeclInContext = FirstDecl;
870 FirstDecl = ExternalFirst;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000871 if (!LastDecl)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000872 LastDecl = ExternalLast;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000873}
874
John McCall76bd1f32010-06-01 09:23:16 +0000875DeclContext::lookup_result
876ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
877 DeclarationName Name) {
878 ASTContext &Context = DC->getParentASTContext();
879 StoredDeclsMap *Map;
880 if (!(Map = DC->LookupPtr))
881 Map = DC->CreateStoredDeclsMap(Context);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000882
John McCall76bd1f32010-06-01 09:23:16 +0000883 StoredDeclsList &List = (*Map)[Name];
884 assert(List.isNull());
885 (void) List;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000886
John McCall76bd1f32010-06-01 09:23:16 +0000887 return DeclContext::lookup_result();
888}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000889
John McCall76bd1f32010-06-01 09:23:16 +0000890DeclContext::lookup_result
891ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +0000892 DeclarationName Name,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000893 SmallVectorImpl<NamedDecl*> &Decls) {
John McCall76bd1f32010-06-01 09:23:16 +0000894 ASTContext &Context = DC->getParentASTContext();;
895
896 StoredDeclsMap *Map;
897 if (!(Map = DC->LookupPtr))
898 Map = DC->CreateStoredDeclsMap(Context);
899
900 StoredDeclsList &List = (*Map)[Name];
901 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
902 if (List.isNull())
903 List.setOnlyValue(Decls[I]);
904 else
905 List.AddSubsequentDecl(Decls[I]);
906 }
907
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000908 return List.getLookupResult();
John McCall76bd1f32010-06-01 09:23:16 +0000909}
910
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000911void ExternalASTSource::MaterializeVisibleDeclsForName(const DeclContext *DC,
912 DeclarationName Name,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000913 SmallVectorImpl<NamedDecl*> &Decls) {
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000914 assert(DC->LookupPtr);
915 StoredDeclsMap &Map = *DC->LookupPtr;
916
917 // If there's an entry in the table the visible decls for this name have
918 // already been deserialized.
919 if (Map.find(Name) == Map.end()) {
920 StoredDeclsList &List = Map[Name];
921 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
922 if (List.isNull())
923 List.setOnlyValue(Decls[I]);
924 else
925 List.AddSubsequentDecl(Decls[I]);
926 }
927 }
928}
929
Sebastian Redl681d7232010-07-27 00:17:23 +0000930DeclContext::decl_iterator DeclContext::noload_decls_begin() const {
931 return decl_iterator(FirstDecl);
932}
933
934DeclContext::decl_iterator DeclContext::noload_decls_end() const {
935 return decl_iterator();
936}
937
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000938DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000939 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000940 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000941
942 // FIXME: Check whether we need to load some declarations from
943 // external storage.
Mike Stump1eb44332009-09-09 15:08:12 +0000944 return decl_iterator(FirstDecl);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000945}
946
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000947DeclContext::decl_iterator DeclContext::decls_end() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000948 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000949 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000950
Mike Stump1eb44332009-09-09 15:08:12 +0000951 return decl_iterator();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000952}
953
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000954bool DeclContext::decls_empty() const {
Douglas Gregor8038d512009-04-10 17:25:41 +0000955 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000956 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor8038d512009-04-10 17:25:41 +0000957
958 return !FirstDecl;
959}
960
John McCall9f54ad42009-12-10 09:41:52 +0000961void DeclContext::removeDecl(Decl *D) {
962 assert(D->getLexicalDeclContext() == this &&
963 "decl being removed from non-lexical context");
964 assert((D->NextDeclInContext || D == LastDecl) &&
965 "decl is not in decls list");
966
967 // Remove D from the decl chain. This is O(n) but hopefully rare.
968 if (D == FirstDecl) {
969 if (D == LastDecl)
970 FirstDecl = LastDecl = 0;
971 else
972 FirstDecl = D->NextDeclInContext;
973 } else {
974 for (Decl *I = FirstDecl; true; I = I->NextDeclInContext) {
975 assert(I && "decl not found in linked list");
976 if (I->NextDeclInContext == D) {
977 I->NextDeclInContext = D->NextDeclInContext;
978 if (D == LastDecl) LastDecl = I;
979 break;
980 }
981 }
982 }
983
984 // Mark that D is no longer in the decl chain.
985 D->NextDeclInContext = 0;
986
987 // Remove D from the lookup table if necessary.
988 if (isa<NamedDecl>(D)) {
989 NamedDecl *ND = cast<NamedDecl>(D);
990
John McCall0c01d182010-03-24 05:22:00 +0000991 StoredDeclsMap *Map = getPrimaryContext()->LookupPtr;
992 if (!Map) return;
John McCall9f54ad42009-12-10 09:41:52 +0000993
John McCall9f54ad42009-12-10 09:41:52 +0000994 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
995 assert(Pos != Map->end() && "no lookup entry for decl");
996 Pos->second.remove(ND);
997 }
998}
999
John McCall3f9a8a62009-08-11 06:59:38 +00001000void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner7f0be132009-02-20 00:56:18 +00001001 assert(D->getLexicalDeclContext() == this &&
1002 "Decl inserted into wrong lexical context");
Mike Stump1eb44332009-09-09 15:08:12 +00001003 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001004 "Decl already inserted into a DeclContext");
1005
1006 if (FirstDecl) {
Chris Lattner244a67d2009-03-28 06:04:26 +00001007 LastDecl->NextDeclInContext = D;
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001008 LastDecl = D;
1009 } else {
1010 FirstDecl = LastDecl = D;
1011 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +00001012
1013 // Notify a C++ record declaration that we've added a member, so it can
1014 // update it's class-specific state.
1015 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
1016 Record->addedMember(D);
John McCall3f9a8a62009-08-11 06:59:38 +00001017}
1018
1019void DeclContext::addDecl(Decl *D) {
1020 addHiddenDecl(D);
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001021
1022 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001023 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor44b43212008-12-11 16:49:14 +00001024}
1025
Douglas Gregor074149e2009-01-05 19:45:36 +00001026/// buildLookup - Build the lookup data structure with all of the
1027/// declarations in DCtx (and any other contexts linked to it or
1028/// transparent contexts nested within it).
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001029void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregor074149e2009-01-05 19:45:36 +00001030 for (; DCtx; DCtx = DCtx->getNextContext()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001031 for (decl_iterator D = DCtx->decls_begin(),
1032 DEnd = DCtx->decls_end();
Douglas Gregor4f3b8f82009-01-06 07:17:58 +00001033 D != DEnd; ++D) {
John McCall3f9a8a62009-08-11 06:59:38 +00001034 // Insert this declaration into the lookup structure, but only
1035 // if it's semantically in its decl context. During non-lazy
1036 // lookup building, this is implicitly enforced by addDecl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001037 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
John McCall3f9a8a62009-08-11 06:59:38 +00001038 if (D->getDeclContext() == DCtx)
1039 makeDeclVisibleInContextImpl(ND);
Douglas Gregor074149e2009-01-05 19:45:36 +00001040
Ted Kremenekc32b1d82009-11-17 22:58:30 +00001041 // Insert any forward-declared Objective-C interfaces into the lookup
1042 // data structure.
1043 if (ObjCClassDecl *Class = dyn_cast<ObjCClassDecl>(*D))
1044 for (ObjCClassDecl::iterator I = Class->begin(), IEnd = Class->end();
1045 I != IEnd; ++I)
Ted Kremenek321c22f2009-11-18 00:28:11 +00001046 makeDeclVisibleInContextImpl(I->getInterface());
Ted Kremenekc32b1d82009-11-17 22:58:30 +00001047
Sebastian Redl410c4f22010-08-31 20:53:31 +00001048 // If this declaration is itself a transparent declaration context or
1049 // inline namespace, add its members (recursively).
Douglas Gregor074149e2009-01-05 19:45:36 +00001050 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
Sebastian Redl410c4f22010-08-31 20:53:31 +00001051 if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001052 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregor074149e2009-01-05 19:45:36 +00001053 }
1054 }
1055}
1056
Mike Stump1eb44332009-09-09 15:08:12 +00001057DeclContext::lookup_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001058DeclContext::lookup(DeclarationName Name) {
Steve Naroff0701bbb2009-01-08 17:28:14 +00001059 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +00001060 if (PrimaryContext != this)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001061 return PrimaryContext->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +00001062
John McCall76bd1f32010-06-01 09:23:16 +00001063 if (hasExternalVisibleStorage()) {
1064 // Check to see if we've already cached the lookup results.
1065 if (LookupPtr) {
1066 StoredDeclsMap::iterator I = LookupPtr->find(Name);
1067 if (I != LookupPtr->end())
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001068 return I->second.getLookupResult();
John McCall76bd1f32010-06-01 09:23:16 +00001069 }
1070
1071 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1072 return Source->FindExternalVisibleDeclsByName(this, Name);
1073 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001074
Douglas Gregor3fc749d2008-12-23 00:26:44 +00001075 /// If there is no lookup data structure, build one now by walking
Douglas Gregor44b43212008-12-11 16:49:14 +00001076 /// all of the linked DeclContexts (in declaration order!) and
1077 /// inserting their values.
Douglas Gregorc36c5402009-04-09 17:29:08 +00001078 if (!LookupPtr) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001079 buildLookup(this);
Douglas Gregor44b43212008-12-11 16:49:14 +00001080
Douglas Gregorc36c5402009-04-09 17:29:08 +00001081 if (!LookupPtr)
Douglas Gregora5fdd9c2010-05-11 06:18:17 +00001082 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Douglas Gregorc36c5402009-04-09 17:29:08 +00001083 }
Douglas Gregor44b43212008-12-11 16:49:14 +00001084
John McCall0c01d182010-03-24 05:22:00 +00001085 StoredDeclsMap::iterator Pos = LookupPtr->find(Name);
1086 if (Pos == LookupPtr->end())
Douglas Gregora5fdd9c2010-05-11 06:18:17 +00001087 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001088 return Pos->second.getLookupResult();
Douglas Gregor44b43212008-12-11 16:49:14 +00001089}
1090
Mike Stump1eb44332009-09-09 15:08:12 +00001091DeclContext::lookup_const_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001092DeclContext::lookup(DeclarationName Name) const {
1093 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +00001094}
1095
Sebastian Redl7a126a42010-08-31 00:36:30 +00001096DeclContext *DeclContext::getRedeclContext() {
Chris Lattner0cf2b192009-03-27 19:19:59 +00001097 DeclContext *Ctx = this;
Sebastian Redl410c4f22010-08-31 20:53:31 +00001098 // Skip through transparent contexts.
1099 while (Ctx->isTransparentContext())
Douglas Gregorce356072009-01-06 23:51:29 +00001100 Ctx = Ctx->getParent();
1101 return Ctx;
1102}
1103
Douglas Gregor88b70942009-02-25 22:02:03 +00001104DeclContext *DeclContext::getEnclosingNamespaceContext() {
1105 DeclContext *Ctx = this;
1106 // Skip through non-namespace, non-translation-unit contexts.
Sebastian Redl51a8a372010-08-31 00:36:23 +00001107 while (!Ctx->isFileContext())
Douglas Gregor88b70942009-02-25 22:02:03 +00001108 Ctx = Ctx->getParent();
1109 return Ctx->getPrimaryContext();
1110}
1111
Sebastian Redl7a126a42010-08-31 00:36:30 +00001112bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
1113 // For non-file contexts, this is equivalent to Equals.
1114 if (!isFileContext())
1115 return O->Equals(this);
1116
1117 do {
1118 if (O->Equals(this))
1119 return true;
1120
1121 const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O);
1122 if (!NS || !NS->isInline())
1123 break;
1124 O = NS->getParent();
1125 } while (O);
1126
1127 return false;
1128}
1129
John McCallab88d972009-08-31 22:39:49 +00001130void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable) {
Douglas Gregorcc636682009-02-17 23:15:12 +00001131 // FIXME: This feels like a hack. Should DeclarationName support
1132 // template-ids, or is there a better way to keep specializations
1133 // from being visible?
Douglas Gregor9a299e02011-03-04 17:52:15 +00001134 if (isa<ClassTemplateSpecializationDecl>(D) || D->isTemplateParameter())
Douglas Gregorcc636682009-02-17 23:15:12 +00001135 return;
Eli Friedman6bc20132009-12-08 05:40:03 +00001136 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1137 if (FD->isFunctionTemplateSpecialization())
1138 return;
Douglas Gregorcc636682009-02-17 23:15:12 +00001139
Steve Naroff0701bbb2009-01-08 17:28:14 +00001140 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +00001141 if (PrimaryContext != this) {
John McCallab88d972009-08-31 22:39:49 +00001142 PrimaryContext->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor44b43212008-12-11 16:49:14 +00001143 return;
1144 }
1145
1146 // If we already have a lookup data structure, perform the insertion
Argyrios Kyrtzidis5586b012010-07-04 21:44:25 +00001147 // into it. If we haven't deserialized externally stored decls, deserialize
1148 // them so we can add the decl. Otherwise, be lazy and don't build that
1149 // structure until someone asks for it.
1150 if (LookupPtr || !Recoverable || hasExternalVisibleStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001151 makeDeclVisibleInContextImpl(D);
Douglas Gregor074149e2009-01-05 19:45:36 +00001152
Sebastian Redl410c4f22010-08-31 20:53:31 +00001153 // If we are a transparent context or inline namespace, insert into our
1154 // parent context, too. This operation is recursive.
1155 if (isTransparentContext() || isInlineNamespace())
John McCallab88d972009-08-31 22:39:49 +00001156 getParent()->makeDeclVisibleInContext(D, Recoverable);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00001157
1158 Decl *DCAsDecl = cast<Decl>(this);
1159 // Notify that a decl was made visible unless it's a Tag being defined.
1160 if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined()))
1161 if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
1162 L->AddedVisibleDecl(this, D);
Douglas Gregor44b43212008-12-11 16:49:14 +00001163}
1164
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001165void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) {
Douglas Gregor074149e2009-01-05 19:45:36 +00001166 // Skip unnamed declarations.
1167 if (!D->getDeclName())
1168 return;
1169
Douglas Gregor5cb0ef42011-05-06 23:32:38 +00001170 // Skip entities that can't be found by name lookup into a particular
1171 // context.
1172 if ((D->getIdentifierNamespace() == 0 && !isa<UsingDirectiveDecl>(D)) ||
1173 D->isTemplateParameter())
Douglas Gregorcc636682009-02-17 23:15:12 +00001174 return;
1175
Argyrios Kyrtzidis5586b012010-07-04 21:44:25 +00001176 ASTContext *C = 0;
1177 if (!LookupPtr) {
1178 C = &getParentASTContext();
1179 CreateStoredDeclsMap(*C);
1180 }
1181
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001182 // If there is an external AST source, load any declarations it knows about
1183 // with this declaration's name.
1184 // If the lookup table contains an entry about this name it means that we
1185 // have already checked the external source.
1186 if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
1187 if (hasExternalVisibleStorage() &&
1188 LookupPtr->find(D->getDeclName()) == LookupPtr->end())
1189 Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
1190
Douglas Gregor44b43212008-12-11 16:49:14 +00001191 // Insert this declaration into the map.
John McCall0c01d182010-03-24 05:22:00 +00001192 StoredDeclsList &DeclNameEntries = (*LookupPtr)[D->getDeclName()];
Chris Lattner67762a32009-02-20 01:44:05 +00001193 if (DeclNameEntries.isNull()) {
1194 DeclNameEntries.setOnlyValue(D);
Chris Lattnerbd6c8002009-02-19 07:00:44 +00001195 return;
Douglas Gregor44b43212008-12-11 16:49:14 +00001196 }
Chris Lattner91942502009-02-20 00:55:03 +00001197
Chris Lattnerbdc3d002009-02-20 01:10:07 +00001198 // If it is possible that this is a redeclaration, check to see if there is
1199 // already a decl for which declarationReplaces returns true. If there is
1200 // one, just replace it and return.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001201 if (DeclNameEntries.HandleRedeclaration(D))
Chris Lattner67762a32009-02-20 01:44:05 +00001202 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001203
Chris Lattnerbd6c8002009-02-19 07:00:44 +00001204 // Put this declaration into the appropriate slot.
Chris Lattner67762a32009-02-20 01:44:05 +00001205 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor44b43212008-12-11 16:49:14 +00001206}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001207
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00001208void DeclContext::MaterializeVisibleDeclsFromExternalStorage() {
1209 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1210 assert(hasExternalVisibleStorage() && Source && "No external storage?");
1211
1212 if (!LookupPtr)
1213 CreateStoredDeclsMap(getParentASTContext());
1214 Source->MaterializeVisibleDecls(this);
1215}
1216
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001217/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
1218/// this context.
Mike Stump1eb44332009-09-09 15:08:12 +00001219DeclContext::udir_iterator_range
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001220DeclContext::getUsingDirectives() const {
1221 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001222 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
1223 reinterpret_cast<udir_iterator>(Result.second));
1224}
Douglas Gregor2cf26342009-04-09 22:27:44 +00001225
Ted Kremenek3478eb62010-02-11 07:12:28 +00001226//===----------------------------------------------------------------------===//
1227// Creation and Destruction of StoredDeclsMaps. //
1228//===----------------------------------------------------------------------===//
1229
John McCall0c01d182010-03-24 05:22:00 +00001230StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
1231 assert(!LookupPtr && "context already has a decls map");
1232 assert(getPrimaryContext() == this &&
1233 "creating decls map on non-primary context");
1234
1235 StoredDeclsMap *M;
1236 bool Dependent = isDependentContext();
1237 if (Dependent)
1238 M = new DependentStoredDeclsMap();
1239 else
1240 M = new StoredDeclsMap();
1241 M->Previous = C.LastSDM;
1242 C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
1243 LookupPtr = M;
Ted Kremenek3478eb62010-02-11 07:12:28 +00001244 return M;
1245}
1246
1247void ASTContext::ReleaseDeclContextMaps() {
John McCall0c01d182010-03-24 05:22:00 +00001248 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1249 // pointer because the subclass doesn't add anything that needs to
1250 // be deleted.
John McCall0c01d182010-03-24 05:22:00 +00001251 StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1252}
1253
1254void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1255 while (Map) {
1256 // Advance the iteration before we invalidate memory.
1257 llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1258
1259 if (Dependent)
1260 delete static_cast<DependentStoredDeclsMap*>(Map);
1261 else
1262 delete Map;
1263
1264 Map = Next.getPointer();
1265 Dependent = Next.getInt();
1266 }
1267}
1268
John McCall0c01d182010-03-24 05:22:00 +00001269DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1270 DeclContext *Parent,
1271 const PartialDiagnostic &PDiag) {
1272 assert(Parent->isDependentContext()
1273 && "cannot iterate dependent diagnostics of non-dependent context");
1274 Parent = Parent->getPrimaryContext();
1275 if (!Parent->LookupPtr)
1276 Parent->CreateStoredDeclsMap(C);
1277
1278 DependentStoredDeclsMap *Map
1279 = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr);
1280
Douglas Gregorb8365182010-03-29 23:56:53 +00001281 // Allocate the copy of the PartialDiagnostic via the ASTContext's
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00001282 // BumpPtrAllocator, rather than the ASTContext itself.
Douglas Gregorb8365182010-03-29 23:56:53 +00001283 PartialDiagnostic::Storage *DiagStorage = 0;
1284 if (PDiag.hasStorage())
1285 DiagStorage = new (C) PartialDiagnostic::Storage;
1286
1287 DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
John McCall0c01d182010-03-24 05:22:00 +00001288
1289 // TODO: Maybe we shouldn't reverse the order during insertion.
1290 DD->NextDiagnostic = Map->FirstDiagnostic;
1291 Map->FirstDiagnostic = DD;
1292
1293 return DD;
Ted Kremenek3478eb62010-02-11 07:12:28 +00001294}