blob: 6d517c5440894d40606b509c1f7d584ad287af49 [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>
Chris Lattnerc25d8a72009-03-02 22:20:04 +000032#include <cstdio>
Eli Friedman7dbab8a2008-06-07 16:52:53 +000033using namespace clang;
34
35//===----------------------------------------------------------------------===//
36// Statistics
37//===----------------------------------------------------------------------===//
38
Alexis Hunted053252010-05-30 07:21:58 +000039#define DECL(DERIVED, BASE) static int n##DERIVED##s = 0;
40#define ABSTRACT_DECL(DECL)
41#include "clang/AST/DeclNodes.inc"
Eli Friedman7dbab8a2008-06-07 16:52:53 +000042
43static bool StatSwitch = false;
44
Eli Friedman7dbab8a2008-06-07 16:52:53 +000045const char *Decl::getDeclKindName() const {
46 switch (DeclKind) {
Alexis Hunted053252010-05-30 07:21:58 +000047 default: assert(0 && "Declaration not in DeclNodes.inc!");
48#define DECL(DERIVED, BASE) case DERIVED: return #DERIVED;
49#define ABSTRACT_DECL(DECL)
50#include "clang/AST/DeclNodes.inc"
Eli Friedman7dbab8a2008-06-07 16:52:53 +000051 }
52}
53
Douglas Gregor90d47172010-03-05 00:26:45 +000054void Decl::setInvalidDecl(bool Invalid) {
55 InvalidDecl = Invalid;
56 if (Invalid) {
57 // Defensive maneuver for ill-formed code: we're likely not to make it to
58 // a point where we set the access specifier, so default it to "public"
59 // to avoid triggering asserts elsewhere in the front end.
60 setAccess(AS_public);
61 }
62}
63
Steve Naroff5faaef72009-01-20 19:53:53 +000064const char *DeclContext::getDeclKindName() const {
65 switch (DeclKind) {
Alexis Hunted053252010-05-30 07:21:58 +000066 default: assert(0 && "Declaration context not in DeclNodes.inc!");
67#define DECL(DERIVED, BASE) case Decl::DERIVED: return #DERIVED;
68#define ABSTRACT_DECL(DECL)
69#include "clang/AST/DeclNodes.inc"
Steve Naroff5faaef72009-01-20 19:53:53 +000070 }
71}
72
Eli Friedman7dbab8a2008-06-07 16:52:53 +000073bool Decl::CollectingStats(bool Enable) {
Kovarththanan Rajaratnam130f7f92009-11-29 14:54:35 +000074 if (Enable) StatSwitch = true;
Eli Friedman7dbab8a2008-06-07 16:52:53 +000075 return StatSwitch;
76}
77
78void Decl::PrintStats() {
79 fprintf(stderr, "*** Decl Stats:\n");
Mike Stump11289f42009-09-09 15:08:12 +000080
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +000081 int totalDecls = 0;
Alexis Hunted053252010-05-30 07:21:58 +000082#define DECL(DERIVED, BASE) totalDecls += n##DERIVED##s;
83#define ABSTRACT_DECL(DECL)
84#include "clang/AST/DeclNodes.inc"
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +000085 fprintf(stderr, " %d decls total.\n", totalDecls);
Mike Stump11289f42009-09-09 15:08:12 +000086
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +000087 int totalBytes = 0;
Alexis Hunted053252010-05-30 07:21:58 +000088#define DECL(DERIVED, BASE) \
89 if (n##DERIVED##s > 0) { \
90 totalBytes += (int)(n##DERIVED##s * sizeof(DERIVED##Decl)); \
91 fprintf(stderr, " %d " #DERIVED " decls, %d each (%d bytes)\n", \
92 n##DERIVED##s, (int)sizeof(DERIVED##Decl), \
93 (int)(n##DERIVED##s * sizeof(DERIVED##Decl))); \
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +000094 }
Alexis Hunted053252010-05-30 07:21:58 +000095#define ABSTRACT_DECL(DECL)
96#include "clang/AST/DeclNodes.inc"
Mike Stump11289f42009-09-09 15:08:12 +000097
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +000098 fprintf(stderr, "Total bytes = %d\n", totalBytes);
Eli Friedman7dbab8a2008-06-07 16:52:53 +000099}
100
Alexis Hunted053252010-05-30 07:21:58 +0000101void Decl::add(Kind k) {
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000102 switch (k) {
Alexis Hunted053252010-05-30 07:21:58 +0000103 default: assert(0 && "Declaration not in DeclNodes.inc!");
104#define DECL(DERIVED, BASE) case DERIVED: ++n##DERIVED##s; break;
105#define ABSTRACT_DECL(DECL)
106#include "clang/AST/DeclNodes.inc"
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000107 }
108}
109
Anders Carlssonaa73b912009-06-13 00:08:58 +0000110bool Decl::isTemplateParameterPack() const {
111 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
112 return TTP->isParameterPack();
Douglas Gregorda3cc0d2010-12-23 23:51:58 +0000113 if (const NonTypeTemplateParmDecl *NTTP
Douglas Gregorf5500772011-01-05 15:48:55 +0000114 = dyn_cast<NonTypeTemplateParmDecl>(this))
Douglas Gregorda3cc0d2010-12-23 23:51:58 +0000115 return NTTP->isParameterPack();
Douglas Gregorf5500772011-01-05 15:48:55 +0000116 if (const TemplateTemplateParmDecl *TTP
117 = dyn_cast<TemplateTemplateParmDecl>(this))
118 return TTP->isParameterPack();
Anders Carlssonaa73b912009-06-13 00:08:58 +0000119 return false;
120}
121
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +0000122bool Decl::isParameterPack() const {
123 if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(this))
124 return Parm->isParameterPack();
125
126 return isTemplateParameterPack();
127}
128
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000129bool Decl::isFunctionOrFunctionTemplate() const {
John McCall3f746822009-11-17 05:59:44 +0000130 if (const UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(this))
Anders Carlssonf057cb22009-06-26 05:26:50 +0000131 return UD->getTargetDecl()->isFunctionOrFunctionTemplate();
Mike Stump11289f42009-09-09 15:08:12 +0000132
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000133 return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this);
134}
135
Douglas Gregorf5974fa2010-01-16 20:21:20 +0000136bool Decl::isDefinedOutsideFunctionOrMethod() const {
137 for (const DeclContext *DC = getDeclContext();
138 DC && !DC->isTranslationUnit();
139 DC = DC->getParent())
140 if (DC->isFunctionOrMethod())
141 return false;
142
143 return true;
144}
145
Douglas Gregor133eddd2011-02-17 08:47:29 +0000146
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000147//===----------------------------------------------------------------------===//
Chris Lattnereae6cb62009-03-05 08:00:35 +0000148// PrettyStackTraceDecl Implementation
149//===----------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000150
Chris Lattnereae6cb62009-03-05 08:00:35 +0000151void PrettyStackTraceDecl::print(llvm::raw_ostream &OS) const {
152 SourceLocation TheLoc = Loc;
153 if (TheLoc.isInvalid() && TheDecl)
154 TheLoc = TheDecl->getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000155
Chris Lattnereae6cb62009-03-05 08:00:35 +0000156 if (TheLoc.isValid()) {
157 TheLoc.print(OS, SM);
158 OS << ": ";
159 }
160
161 OS << Message;
162
Daniel Dunbar4f1054e2009-11-21 09:05:59 +0000163 if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
Chris Lattnereae6cb62009-03-05 08:00:35 +0000164 OS << " '" << DN->getQualifiedNameAsString() << '\'';
165 OS << '\n';
166}
Mike Stump11289f42009-09-09 15:08:12 +0000167
Chris Lattnereae6cb62009-03-05 08:00:35 +0000168//===----------------------------------------------------------------------===//
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000169// Decl Implementation
170//===----------------------------------------------------------------------===//
171
Douglas Gregorb11aad82011-02-19 18:51:44 +0000172// Out-of-line virtual method providing a home for Decl.
173Decl::~Decl() { }
Douglas Gregora43942a2011-02-17 07:02:32 +0000174
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000175void Decl::setDeclContext(DeclContext *DC) {
Chris Lattnerb81eb052009-03-29 06:06:59 +0000176 DeclCtx = DC;
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000177}
178
179void Decl::setLexicalDeclContext(DeclContext *DC) {
180 if (DC == getLexicalDeclContext())
181 return;
182
183 if (isInSemaDC()) {
Ted Kremenekf8c12a32009-12-01 00:07:10 +0000184 MultipleDC *MDC = new (getASTContext()) MultipleDC();
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000185 MDC->SemanticDC = getDeclContext();
186 MDC->LexicalDC = DC;
Chris Lattnerb81eb052009-03-29 06:06:59 +0000187 DeclCtx = MDC;
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000188 } else {
189 getMultipleDC()->LexicalDC = DC;
190 }
191}
192
John McCall4fa53422009-10-01 00:25:31 +0000193bool Decl::isInAnonymousNamespace() const {
194 const DeclContext *DC = getDeclContext();
195 do {
196 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
197 if (ND->isAnonymousNamespace())
198 return true;
199 } while ((DC = DC->getParent()));
200
201 return false;
202}
203
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000204TranslationUnitDecl *Decl::getTranslationUnitDecl() {
Argyrios Kyrtzidis4e1a72b2009-06-30 02:34:53 +0000205 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
206 return TUD;
207
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000208 DeclContext *DC = getDeclContext();
209 assert(DC && "This decl is not contained in a translation unit!");
Mike Stump11289f42009-09-09 15:08:12 +0000210
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000211 while (!DC->isTranslationUnit()) {
212 DC = DC->getParent();
213 assert(DC && "This decl is not contained in a translation unit!");
214 }
Mike Stump11289f42009-09-09 15:08:12 +0000215
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000216 return cast<TranslationUnitDecl>(DC);
217}
218
219ASTContext &Decl::getASTContext() const {
Mike Stump11289f42009-09-09 15:08:12 +0000220 return getTranslationUnitDecl()->getASTContext();
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000221}
222
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +0000223ASTMutationListener *Decl::getASTMutationListener() const {
224 return getASTContext().getASTMutationListener();
225}
226
Douglas Gregorebada0772010-06-17 23:14:26 +0000227bool Decl::isUsed(bool CheckUsedAttr) const {
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000228 if (Used)
229 return true;
230
231 // Check for used attribute.
Douglas Gregorebada0772010-06-17 23:14:26 +0000232 if (CheckUsedAttr && hasAttr<UsedAttr>())
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000233 return true;
234
235 // Check redeclarations for used attribute.
236 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Douglas Gregorebada0772010-06-17 23:14:26 +0000237 if ((CheckUsedAttr && I->hasAttr<UsedAttr>()) || I->Used)
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000238 return true;
239 }
240
241 return false;
242}
243
Argyrios Kyrtzidis16180232011-04-19 19:51:10 +0000244bool Decl::isReferenced() const {
245 if (Referenced)
246 return true;
247
248 // Check redeclarations.
249 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
250 if (I->Referenced)
251 return true;
252
253 return false;
254}
255
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000256/// \brief Determine the availability of the given declaration based on
257/// the target platform.
258///
259/// When it returns an availability result other than \c AR_Available,
260/// if the \p Message parameter is non-NULL, it will be set to a
261/// string describing why the entity is unavailable.
262///
263/// FIXME: Make these strings localizable, since they end up in
264/// diagnostics.
265static AvailabilityResult CheckAvailability(ASTContext &Context,
266 const AvailabilityAttr *A,
267 std::string *Message) {
268 llvm::StringRef TargetPlatform = Context.Target.getPlatformName();
269 llvm::StringRef PrettyPlatformName
270 = AvailabilityAttr::getPrettyPlatformName(TargetPlatform);
271 if (PrettyPlatformName.empty())
272 PrettyPlatformName = TargetPlatform;
273
274 VersionTuple TargetMinVersion = Context.Target.getPlatformMinVersion();
275 if (TargetMinVersion.empty())
276 return AR_Available;
277
278 // Match the platform name.
279 if (A->getPlatform()->getName() != TargetPlatform)
280 return AR_Available;
281
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000282 // Make sure that this declaration has not been marked 'unavailable'.
283 if (A->getUnavailable()) {
284 if (Message) {
285 Message->clear();
286 llvm::raw_string_ostream Out(*Message);
287 Out << "not available on " << PrettyPlatformName;
288 }
289
290 return AR_Unavailable;
291 }
292
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000293 // Make sure that this declaration has already been introduced.
294 if (!A->getIntroduced().empty() &&
295 TargetMinVersion < A->getIntroduced()) {
296 if (Message) {
297 Message->clear();
298 llvm::raw_string_ostream Out(*Message);
299 Out << "introduced in " << PrettyPlatformName << ' '
300 << A->getIntroduced();
301 }
302
303 return AR_NotYetIntroduced;
304 }
305
306 // Make sure that this declaration hasn't been obsoleted.
307 if (!A->getObsoleted().empty() && TargetMinVersion >= A->getObsoleted()) {
308 if (Message) {
309 Message->clear();
310 llvm::raw_string_ostream Out(*Message);
311 Out << "obsoleted in " << PrettyPlatformName << ' '
312 << A->getObsoleted();
313 }
314
315 return AR_Unavailable;
316 }
317
318 // Make sure that this declaration hasn't been deprecated.
319 if (!A->getDeprecated().empty() && TargetMinVersion >= A->getDeprecated()) {
320 if (Message) {
321 Message->clear();
322 llvm::raw_string_ostream Out(*Message);
323 Out << "first deprecated in " << PrettyPlatformName << ' '
324 << A->getDeprecated();
325 }
326
327 return AR_Deprecated;
328 }
329
330 return AR_Available;
331}
332
333AvailabilityResult Decl::getAvailability(std::string *Message) const {
334 AvailabilityResult Result = AR_Available;
335 std::string ResultMessage;
336
337 for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
338 if (DeprecatedAttr *Deprecated = dyn_cast<DeprecatedAttr>(*A)) {
339 if (Result >= AR_Deprecated)
340 continue;
341
342 if (Message)
343 ResultMessage = Deprecated->getMessage();
344
345 Result = AR_Deprecated;
346 continue;
347 }
348
349 if (UnavailableAttr *Unavailable = dyn_cast<UnavailableAttr>(*A)) {
350 if (Message)
351 *Message = Unavailable->getMessage();
352 return AR_Unavailable;
353 }
354
355 if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
356 AvailabilityResult AR = CheckAvailability(getASTContext(), Availability,
357 Message);
358
359 if (AR == AR_Unavailable)
360 return AR_Unavailable;
361
362 if (AR > Result) {
363 Result = AR;
364 if (Message)
365 ResultMessage.swap(*Message);
366 }
367 continue;
368 }
369 }
370
371 if (Message)
372 Message->swap(ResultMessage);
373 return Result;
374}
375
376bool Decl::canBeWeakImported(bool &IsDefinition) const {
377 IsDefinition = false;
378 if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
379 if (!Var->hasExternalStorage() || Var->getInit()) {
380 IsDefinition = true;
381 return false;
382 }
383 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
384 if (FD->hasBody()) {
385 IsDefinition = true;
386 return false;
387 }
388 } else if (isa<ObjCPropertyDecl>(this) || isa<ObjCMethodDecl>(this))
389 return false;
390 else if (!(getASTContext().getLangOptions().ObjCNonFragileABI &&
391 isa<ObjCInterfaceDecl>(this)))
392 return false;
393
394 return true;
395}
396
397bool Decl::isWeakImported() const {
398 bool IsDefinition;
399 if (!canBeWeakImported(IsDefinition))
400 return false;
401
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000402 for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
403 if (isa<WeakImportAttr>(*A))
404 return true;
405
406 if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
407 if (CheckAvailability(getASTContext(), Availability, 0)
408 == AR_NotYetIntroduced)
409 return true;
410 }
411 }
412
413 return false;
414}
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000415
Chris Lattner8e097192009-03-27 20:18:19 +0000416unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
417 switch (DeclKind) {
John McCall3f746822009-11-17 05:59:44 +0000418 case Function:
419 case CXXMethod:
420 case CXXConstructor:
421 case CXXDestructor:
422 case CXXConversion:
Chris Lattner8e097192009-03-27 20:18:19 +0000423 case EnumConstant:
424 case Var:
425 case ImplicitParam:
426 case ParmVar:
Chris Lattner8e097192009-03-27 20:18:19 +0000427 case NonTypeTemplateParm:
428 case ObjCMethod:
Daniel Dunbar45b2d8a2010-04-23 13:07:39 +0000429 case ObjCProperty:
Daniel Dunbar45b2d8a2010-04-23 13:07:39 +0000430 return IDNS_Ordinary;
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000431 case Label:
432 return IDNS_Label;
Francois Pichet783dd6e2010-11-21 06:08:52 +0000433 case IndirectField:
434 return IDNS_Ordinary | IDNS_Member;
435
John McCalle87beb22010-04-23 18:46:30 +0000436 case ObjCCompatibleAlias:
437 case ObjCInterface:
438 return IDNS_Ordinary | IDNS_Type;
439
440 case Typedef:
Richard Smithdda56e42011-04-15 14:24:37 +0000441 case TypeAlias:
John McCalle87beb22010-04-23 18:46:30 +0000442 case UnresolvedUsingTypename:
443 case TemplateTypeParm:
444 return IDNS_Ordinary | IDNS_Type;
445
John McCall3f746822009-11-17 05:59:44 +0000446 case UsingShadow:
447 return 0; // we'll actually overwrite this later
448
John McCalle61f2ba2009-11-18 02:36:19 +0000449 case UnresolvedUsingValue:
John McCalle61f2ba2009-11-18 02:36:19 +0000450 return IDNS_Ordinary | IDNS_Using;
John McCall3f746822009-11-17 05:59:44 +0000451
452 case Using:
453 return IDNS_Using;
454
Chris Lattner8e097192009-03-27 20:18:19 +0000455 case ObjCProtocol:
Douglas Gregor79947a22009-04-24 00:11:27 +0000456 return IDNS_ObjCProtocol;
Mike Stump11289f42009-09-09 15:08:12 +0000457
Chris Lattner8e097192009-03-27 20:18:19 +0000458 case Field:
459 case ObjCAtDefsField:
460 case ObjCIvar:
461 return IDNS_Member;
Mike Stump11289f42009-09-09 15:08:12 +0000462
Chris Lattner8e097192009-03-27 20:18:19 +0000463 case Record:
464 case CXXRecord:
465 case Enum:
John McCalle87beb22010-04-23 18:46:30 +0000466 return IDNS_Tag | IDNS_Type;
Mike Stump11289f42009-09-09 15:08:12 +0000467
Chris Lattner8e097192009-03-27 20:18:19 +0000468 case Namespace:
John McCalle87beb22010-04-23 18:46:30 +0000469 case NamespaceAlias:
470 return IDNS_Namespace;
471
Chris Lattner8e097192009-03-27 20:18:19 +0000472 case FunctionTemplate:
John McCalle87beb22010-04-23 18:46:30 +0000473 return IDNS_Ordinary;
474
Chris Lattner8e097192009-03-27 20:18:19 +0000475 case ClassTemplate:
476 case TemplateTemplateParm:
John McCalle87beb22010-04-23 18:46:30 +0000477 return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
Mike Stump11289f42009-09-09 15:08:12 +0000478
Chris Lattner8e097192009-03-27 20:18:19 +0000479 // Never have names.
John McCallaa74a0c2009-08-28 07:59:38 +0000480 case Friend:
John McCall11083da2009-09-16 22:47:08 +0000481 case FriendTemplate:
Abramo Bagnarad7340582010-06-05 05:09:32 +0000482 case AccessSpec:
Chris Lattner8e097192009-03-27 20:18:19 +0000483 case LinkageSpec:
484 case FileScopeAsm:
485 case StaticAssert:
486 case ObjCClass:
Chris Lattner8e097192009-03-27 20:18:19 +0000487 case ObjCPropertyImpl:
488 case ObjCForwardProtocol:
489 case Block:
490 case TranslationUnit:
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000491
Chris Lattner8e097192009-03-27 20:18:19 +0000492 case UsingDirective:
493 case ClassTemplateSpecialization:
Douglas Gregor2373c592009-05-31 09:31:02 +0000494 case ClassTemplatePartialSpecialization:
Douglas Gregore93525e2010-04-22 23:19:50 +0000495 case ObjCImplementation:
496 case ObjCCategory:
497 case ObjCCategoryImpl:
498 // Never looked up by name.
Chris Lattner8e097192009-03-27 20:18:19 +0000499 return 0;
500 }
John McCall3f746822009-11-17 05:59:44 +0000501
502 return 0;
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000503}
504
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000505void Decl::setAttrs(const AttrVec &attrs) {
Argyrios Kyrtzidis91167172010-06-11 23:09:25 +0000506 assert(!HasAttrs && "Decl already contains attrs.");
507
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000508 AttrVec &AttrBlank = getASTContext().getDeclAttrs(this);
509 assert(AttrBlank.empty() && "HasAttrs was wrong?");
Argyrios Kyrtzidis91167172010-06-11 23:09:25 +0000510
511 AttrBlank = attrs;
512 HasAttrs = true;
513}
514
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000515void Decl::dropAttrs() {
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000516 if (!HasAttrs) return;
Mike Stump11289f42009-09-09 15:08:12 +0000517
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000518 HasAttrs = false;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000519 getASTContext().eraseDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000520}
521
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000522const AttrVec &Decl::getAttrs() const {
523 assert(HasAttrs && "No attrs to get!");
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000524 return getASTContext().getDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000525}
526
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000527void Decl::swapAttrs(Decl *RHS) {
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000528 bool HasLHSAttr = this->HasAttrs;
529 bool HasRHSAttr = RHS->HasAttrs;
Mike Stump11289f42009-09-09 15:08:12 +0000530
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000531 // Usually, neither decl has attrs, nothing to do.
532 if (!HasLHSAttr && !HasRHSAttr) return;
Mike Stump11289f42009-09-09 15:08:12 +0000533
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000534 // If 'this' has no attrs, swap the other way.
535 if (!HasLHSAttr)
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000536 return RHS->swapAttrs(this);
Mike Stump11289f42009-09-09 15:08:12 +0000537
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000538 ASTContext &Context = getASTContext();
Mike Stump11289f42009-09-09 15:08:12 +0000539
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000540 // Handle the case when both decls have attrs.
541 if (HasRHSAttr) {
Douglas Gregor78bd61f2009-06-18 16:11:24 +0000542 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000543 return;
544 }
Mike Stump11289f42009-09-09 15:08:12 +0000545
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000546 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor78bd61f2009-06-18 16:11:24 +0000547 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
548 Context.eraseDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000549 this->HasAttrs = false;
550 RHS->HasAttrs = true;
551}
552
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000553Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000554 Decl::Kind DK = D->getDeclKind();
555 switch(DK) {
Alexis Hunted053252010-05-30 07:21:58 +0000556#define DECL(NAME, BASE)
557#define DECL_CONTEXT(NAME) \
558 case Decl::NAME: \
559 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
560#define DECL_CONTEXT_BASE(NAME)
561#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000562 default:
Alexis Hunted053252010-05-30 07:21:58 +0000563#define DECL(NAME, BASE)
564#define DECL_CONTEXT_BASE(NAME) \
565 if (DK >= first##NAME && DK <= last##NAME) \
566 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
567#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000568 assert(false && "a decl that inherits DeclContext isn't handled");
569 return 0;
570 }
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000571}
572
573DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000574 Decl::Kind DK = D->getKind();
575 switch(DK) {
Alexis Hunted053252010-05-30 07:21:58 +0000576#define DECL(NAME, BASE)
577#define DECL_CONTEXT(NAME) \
578 case Decl::NAME: \
579 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
580#define DECL_CONTEXT_BASE(NAME)
581#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000582 default:
Alexis Hunted053252010-05-30 07:21:58 +0000583#define DECL(NAME, BASE)
584#define DECL_CONTEXT_BASE(NAME) \
585 if (DK >= first##NAME && DK <= last##NAME) \
586 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
587#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000588 assert(false && "a decl that inherits DeclContext isn't handled");
589 return 0;
590 }
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000591}
592
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000593SourceLocation Decl::getBodyRBrace() const {
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +0000594 // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
595 // FunctionDecl stores EndRangeLoc for this purpose.
596 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
597 const FunctionDecl *Definition;
598 if (FD->hasBody(Definition))
599 return Definition->getSourceRange().getEnd();
600 return SourceLocation();
601 }
602
Argyrios Kyrtzidis6fbc8fa2010-07-07 11:31:27 +0000603 if (Stmt *Body = getBody())
604 return Body->getSourceRange().getEnd();
605
606 return SourceLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +0000607}
608
Anders Carlssona28908d2009-03-25 23:38:06 +0000609void Decl::CheckAccessDeclContext() const {
Douglas Gregor4b00d3b2010-12-02 00:22:25 +0000610#ifndef NDEBUG
John McCall401982f2010-01-20 21:53:11 +0000611 // Suppress this check if any of the following hold:
612 // 1. this is the translation unit (and thus has no parent)
613 // 2. this is a template parameter (and thus doesn't belong to its context)
Argyrios Kyrtzidise1778632010-09-08 21:58:42 +0000614 // 3. this is a non-type template parameter
615 // 4. the context is not a record
616 // 5. it's invalid
617 // 6. it's a C++0x static_assert.
Anders Carlssonadf36b22009-08-29 20:47:47 +0000618 if (isa<TranslationUnitDecl>(this) ||
Argyrios Kyrtzidisa45855f2010-07-02 11:55:44 +0000619 isa<TemplateTypeParmDecl>(this) ||
Argyrios Kyrtzidise1778632010-09-08 21:58:42 +0000620 isa<NonTypeTemplateParmDecl>(this) ||
Douglas Gregor2b76dd92010-02-22 17:53:38 +0000621 !isa<CXXRecordDecl>(getDeclContext()) ||
Argyrios Kyrtzidis260b4a82010-09-08 21:32:35 +0000622 isInvalidDecl() ||
623 isa<StaticAssertDecl>(this) ||
624 // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
625 // as DeclContext (?).
Argyrios Kyrtzidise1778632010-09-08 21:58:42 +0000626 isa<ParmVarDecl>(this) ||
627 // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
628 // AS_none as access specifier.
629 isa<CXXRecordDecl>(this))
Anders Carlssonadf36b22009-08-29 20:47:47 +0000630 return;
Mike Stump11289f42009-09-09 15:08:12 +0000631
632 assert(Access != AS_none &&
Anders Carlssona28908d2009-03-25 23:38:06 +0000633 "Access specifier is AS_none inside a record decl");
Douglas Gregor4b00d3b2010-12-02 00:22:25 +0000634#endif
Anders Carlssona28908d2009-03-25 23:38:06 +0000635}
636
John McCallb67608f2011-02-22 22:25:23 +0000637DeclContext *Decl::getNonClosureContext() {
638 DeclContext *DC = getDeclContext();
639
640 // This is basically "while (DC->isClosure()) DC = DC->getParent();"
641 // except that it's significantly more efficient to cast to a known
642 // decl type and call getDeclContext() than to call getParent().
643 do {
644 if (isa<BlockDecl>(DC)) {
645 DC = cast<BlockDecl>(DC)->getDeclContext();
646 continue;
647 }
648 } while (false);
649
650 assert(!DC->isClosure());
651 return DC;
652}
Anders Carlssona28908d2009-03-25 23:38:06 +0000653
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000654//===----------------------------------------------------------------------===//
655// DeclContext Implementation
656//===----------------------------------------------------------------------===//
657
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000658bool DeclContext::classof(const Decl *D) {
659 switch (D->getKind()) {
Alexis Hunted053252010-05-30 07:21:58 +0000660#define DECL(NAME, BASE)
661#define DECL_CONTEXT(NAME) case Decl::NAME:
662#define DECL_CONTEXT_BASE(NAME)
663#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000664 return true;
665 default:
Alexis Hunted053252010-05-30 07:21:58 +0000666#define DECL(NAME, BASE)
667#define DECL_CONTEXT_BASE(NAME) \
668 if (D->getKind() >= Decl::first##NAME && \
669 D->getKind() <= Decl::last##NAME) \
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000670 return true;
Alexis Hunted053252010-05-30 07:21:58 +0000671#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000672 return false;
673 }
674}
675
Douglas Gregor9c832f72010-07-25 18:38:02 +0000676DeclContext::~DeclContext() { }
Douglas Gregor91f84212008-12-11 16:49:14 +0000677
Douglas Gregor7f737c02009-09-10 16:57:35 +0000678/// \brief Find the parent context of this context that will be
679/// used for unqualified name lookup.
680///
681/// Generally, the parent lookup context is the semantic context. However, for
682/// a friend function the parent lookup context is the lexical context, which
683/// is the class in which the friend is declared.
684DeclContext *DeclContext::getLookupParent() {
685 // FIXME: Find a better way to identify friends
686 if (isa<FunctionDecl>(this))
Sebastian Redl50c68252010-08-31 00:36:30 +0000687 if (getParent()->getRedeclContext()->isFileContext() &&
688 getLexicalParent()->getRedeclContext()->isRecord())
Douglas Gregor7f737c02009-09-10 16:57:35 +0000689 return getLexicalParent();
690
691 return getParent();
692}
693
Sebastian Redlbd595762010-08-31 20:53:31 +0000694bool DeclContext::isInlineNamespace() const {
695 return isNamespace() &&
696 cast<NamespaceDecl>(this)->isInline();
697}
698
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000699bool DeclContext::isDependentContext() const {
700 if (isFileContext())
701 return false;
702
Douglas Gregor2373c592009-05-31 09:31:02 +0000703 if (isa<ClassTemplatePartialSpecializationDecl>(this))
704 return true;
705
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000706 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
707 if (Record->getDescribedClassTemplate())
708 return true;
709
John McCallc62bb642010-03-24 05:22:00 +0000710 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000711 if (Function->getDescribedFunctionTemplate())
712 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000713
John McCallc62bb642010-03-24 05:22:00 +0000714 // Friend function declarations are dependent if their *lexical*
715 // context is dependent.
716 if (cast<Decl>(this)->getFriendObjectKind())
717 return getLexicalParent()->isDependentContext();
718 }
719
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000720 return getParent() && getParent()->isDependentContext();
721}
722
Douglas Gregor07665a62009-01-05 19:45:36 +0000723bool DeclContext::isTransparentContext() const {
724 if (DeclKind == Decl::Enum)
Douglas Gregor0bf31402010-10-08 23:50:27 +0000725 return !cast<EnumDecl>(this)->isScoped();
Douglas Gregor07665a62009-01-05 19:45:36 +0000726 else if (DeclKind == Decl::LinkageSpec)
727 return true;
Douglas Gregor07665a62009-01-05 19:45:36 +0000728
729 return false;
730}
731
John McCall5fe84122010-10-26 04:59:26 +0000732bool DeclContext::isExternCContext() const {
733 const DeclContext *DC = this;
734 while (DC->DeclKind != Decl::TranslationUnit) {
735 if (DC->DeclKind == Decl::LinkageSpec)
736 return cast<LinkageSpecDecl>(DC)->getLanguage()
737 == LinkageSpecDecl::lang_c;
738 DC = DC->getParent();
739 }
740 return false;
741}
742
Sebastian Redl50c68252010-08-31 00:36:30 +0000743bool DeclContext::Encloses(const DeclContext *DC) const {
Douglas Gregore985a3b2009-08-27 06:03:53 +0000744 if (getPrimaryContext() != this)
745 return getPrimaryContext()->Encloses(DC);
Mike Stump11289f42009-09-09 15:08:12 +0000746
Douglas Gregore985a3b2009-08-27 06:03:53 +0000747 for (; DC; DC = DC->getParent())
748 if (DC->getPrimaryContext() == this)
749 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000750 return false;
Douglas Gregore985a3b2009-08-27 06:03:53 +0000751}
752
Steve Naroff35c62ae2009-01-08 17:28:14 +0000753DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor91f84212008-12-11 16:49:14 +0000754 switch (DeclKind) {
Douglas Gregor91f84212008-12-11 16:49:14 +0000755 case Decl::TranslationUnit:
Douglas Gregor07665a62009-01-05 19:45:36 +0000756 case Decl::LinkageSpec:
Mike Stump11289f42009-09-09 15:08:12 +0000757 case Decl::Block:
Douglas Gregor91f84212008-12-11 16:49:14 +0000758 // There is only one DeclContext for these entities.
759 return this;
760
761 case Decl::Namespace:
762 // The original namespace is our primary context.
763 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
764
Douglas Gregor91f84212008-12-11 16:49:14 +0000765 case Decl::ObjCMethod:
766 return this;
767
768 case Decl::ObjCInterface:
Steve Naroff35c62ae2009-01-08 17:28:14 +0000769 case Decl::ObjCProtocol:
770 case Decl::ObjCCategory:
Douglas Gregor91f84212008-12-11 16:49:14 +0000771 // FIXME: Can Objective-C interfaces be forward-declared?
772 return this;
773
Steve Naroff35c62ae2009-01-08 17:28:14 +0000774 case Decl::ObjCImplementation:
775 case Decl::ObjCCategoryImpl:
776 return this;
777
Douglas Gregor91f84212008-12-11 16:49:14 +0000778 default:
Alexis Hunted053252010-05-30 07:21:58 +0000779 if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
Douglas Gregor67a65642009-02-17 23:15:12 +0000780 // If this is a tag type that has a definition or is currently
781 // being defined, that definition is our primary context.
John McCalle78aac42010-03-10 03:28:59 +0000782 TagDecl *Tag = cast<TagDecl>(this);
783 assert(isa<TagType>(Tag->TypeForDecl) ||
784 isa<InjectedClassNameType>(Tag->TypeForDecl));
785
786 if (TagDecl *Def = Tag->getDefinition())
787 return Def;
788
789 if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) {
790 const TagType *TagTy = cast<TagType>(Tag->TypeForDecl);
791 if (TagTy->isBeingDefined())
792 // FIXME: is it necessarily being defined in the decl
793 // that owns the type?
794 return TagTy->getDecl();
795 }
796
797 return Tag;
Douglas Gregor67a65642009-02-17 23:15:12 +0000798 }
799
Alexis Hunted053252010-05-30 07:21:58 +0000800 assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
Douglas Gregor91f84212008-12-11 16:49:14 +0000801 "Unknown DeclContext kind");
802 return this;
803 }
804}
805
806DeclContext *DeclContext::getNextContext() {
807 switch (DeclKind) {
Douglas Gregor91f84212008-12-11 16:49:14 +0000808 case Decl::Namespace:
809 // Return the next namespace
810 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
811
812 default:
Douglas Gregor91f84212008-12-11 16:49:14 +0000813 return 0;
814 }
815}
816
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000817std::pair<Decl *, Decl *>
818DeclContext::BuildDeclChain(const llvm::SmallVectorImpl<Decl*> &Decls) {
819 // Build up a chain of declarations via the Decl::NextDeclInContext field.
820 Decl *FirstNewDecl = 0;
821 Decl *PrevDecl = 0;
822 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
823 Decl *D = Decls[I];
824 if (PrevDecl)
825 PrevDecl->NextDeclInContext = D;
826 else
827 FirstNewDecl = D;
828
829 PrevDecl = D;
830 }
831
832 return std::make_pair(FirstNewDecl, PrevDecl);
833}
834
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000835/// \brief Load the declarations within this lexical storage from an
836/// external source.
Mike Stump11289f42009-09-09 15:08:12 +0000837void
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000838DeclContext::LoadLexicalDeclsFromExternalStorage() const {
839 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000840 assert(hasExternalLexicalStorage() && Source && "No external storage?");
841
Argyrios Kyrtzidis98d045e2010-07-30 10:03:23 +0000842 // Notify that we have a DeclContext that is initializing.
843 ExternalASTSource::Deserializing ADeclContext(Source);
844
John McCall75b960e2010-06-01 09:23:16 +0000845 llvm::SmallVector<Decl*, 64> Decls;
846 if (Source->FindExternalLexicalDecls(this, Decls))
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000847 return;
848
849 // There is no longer any lexical storage in this context
850 ExternalLexicalStorage = false;
851
852 if (Decls.empty())
853 return;
854
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000855 // We may have already loaded just the fields of this record, in which case
856 // don't add the decls, just replace the FirstDecl/LastDecl chain.
857 if (const RecordDecl *RD = dyn_cast<RecordDecl>(this))
858 if (RD->LoadedFieldsFromExternalStorage) {
859 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
860 return;
861 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000862
863 // Splice the newly-read declarations into the beginning of the list
864 // of declarations.
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000865 Decl *ExternalFirst, *ExternalLast;
866 llvm::tie(ExternalFirst, ExternalLast) = BuildDeclChain(Decls);
867 ExternalLast->NextDeclInContext = FirstDecl;
868 FirstDecl = ExternalFirst;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000869 if (!LastDecl)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000870 LastDecl = ExternalLast;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000871}
872
John McCall75b960e2010-06-01 09:23:16 +0000873DeclContext::lookup_result
874ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
875 DeclarationName Name) {
876 ASTContext &Context = DC->getParentASTContext();
877 StoredDeclsMap *Map;
878 if (!(Map = DC->LookupPtr))
879 Map = DC->CreateStoredDeclsMap(Context);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000880
John McCall75b960e2010-06-01 09:23:16 +0000881 StoredDeclsList &List = (*Map)[Name];
882 assert(List.isNull());
883 (void) List;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000884
John McCall75b960e2010-06-01 09:23:16 +0000885 return DeclContext::lookup_result();
886}
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000887
John McCall75b960e2010-06-01 09:23:16 +0000888DeclContext::lookup_result
889ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +0000890 DeclarationName Name,
891 llvm::SmallVectorImpl<NamedDecl*> &Decls) {
892 ASTContext &Context = DC->getParentASTContext();;
893
894 StoredDeclsMap *Map;
895 if (!(Map = DC->LookupPtr))
896 Map = DC->CreateStoredDeclsMap(Context);
897
898 StoredDeclsList &List = (*Map)[Name];
899 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
900 if (List.isNull())
901 List.setOnlyValue(Decls[I]);
902 else
903 List.AddSubsequentDecl(Decls[I]);
904 }
905
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000906 return List.getLookupResult();
John McCall75b960e2010-06-01 09:23:16 +0000907}
908
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +0000909void ExternalASTSource::MaterializeVisibleDeclsForName(const DeclContext *DC,
910 DeclarationName Name,
911 llvm::SmallVectorImpl<NamedDecl*> &Decls) {
912 assert(DC->LookupPtr);
913 StoredDeclsMap &Map = *DC->LookupPtr;
914
915 // If there's an entry in the table the visible decls for this name have
916 // already been deserialized.
917 if (Map.find(Name) == Map.end()) {
918 StoredDeclsList &List = Map[Name];
919 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
920 if (List.isNull())
921 List.setOnlyValue(Decls[I]);
922 else
923 List.AddSubsequentDecl(Decls[I]);
924 }
925 }
926}
927
Sebastian Redl66c5eef2010-07-27 00:17:23 +0000928DeclContext::decl_iterator DeclContext::noload_decls_begin() const {
929 return decl_iterator(FirstDecl);
930}
931
932DeclContext::decl_iterator DeclContext::noload_decls_end() const {
933 return decl_iterator();
934}
935
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000936DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000937 if (hasExternalLexicalStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000938 LoadLexicalDeclsFromExternalStorage();
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000939
940 // FIXME: Check whether we need to load some declarations from
941 // external storage.
Mike Stump11289f42009-09-09 15:08:12 +0000942 return decl_iterator(FirstDecl);
Douglas Gregorbcced4e2009-04-09 21:40:53 +0000943}
944
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000945DeclContext::decl_iterator DeclContext::decls_end() const {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000946 if (hasExternalLexicalStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000947 LoadLexicalDeclsFromExternalStorage();
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000948
Mike Stump11289f42009-09-09 15:08:12 +0000949 return decl_iterator();
Douglas Gregorbcced4e2009-04-09 21:40:53 +0000950}
951
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000952bool DeclContext::decls_empty() const {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +0000953 if (hasExternalLexicalStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000954 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +0000955
956 return !FirstDecl;
957}
958
John McCall84d87672009-12-10 09:41:52 +0000959void DeclContext::removeDecl(Decl *D) {
960 assert(D->getLexicalDeclContext() == this &&
961 "decl being removed from non-lexical context");
962 assert((D->NextDeclInContext || D == LastDecl) &&
963 "decl is not in decls list");
964
965 // Remove D from the decl chain. This is O(n) but hopefully rare.
966 if (D == FirstDecl) {
967 if (D == LastDecl)
968 FirstDecl = LastDecl = 0;
969 else
970 FirstDecl = D->NextDeclInContext;
971 } else {
972 for (Decl *I = FirstDecl; true; I = I->NextDeclInContext) {
973 assert(I && "decl not found in linked list");
974 if (I->NextDeclInContext == D) {
975 I->NextDeclInContext = D->NextDeclInContext;
976 if (D == LastDecl) LastDecl = I;
977 break;
978 }
979 }
980 }
981
982 // Mark that D is no longer in the decl chain.
983 D->NextDeclInContext = 0;
984
985 // Remove D from the lookup table if necessary.
986 if (isa<NamedDecl>(D)) {
987 NamedDecl *ND = cast<NamedDecl>(D);
988
John McCallc62bb642010-03-24 05:22:00 +0000989 StoredDeclsMap *Map = getPrimaryContext()->LookupPtr;
990 if (!Map) return;
John McCall84d87672009-12-10 09:41:52 +0000991
John McCall84d87672009-12-10 09:41:52 +0000992 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
993 assert(Pos != Map->end() && "no lookup entry for decl");
994 Pos->second.remove(ND);
995 }
996}
997
John McCalld1e9d832009-08-11 06:59:38 +0000998void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner33f219d2009-02-20 00:56:18 +0000999 assert(D->getLexicalDeclContext() == this &&
1000 "Decl inserted into wrong lexical context");
Mike Stump11289f42009-09-09 15:08:12 +00001001 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor020713e2009-01-09 19:42:16 +00001002 "Decl already inserted into a DeclContext");
1003
1004 if (FirstDecl) {
Chris Lattnerfcd33a62009-03-28 06:04:26 +00001005 LastDecl->NextDeclInContext = D;
Douglas Gregor020713e2009-01-09 19:42:16 +00001006 LastDecl = D;
1007 } else {
1008 FirstDecl = LastDecl = D;
1009 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +00001010
1011 // Notify a C++ record declaration that we've added a member, so it can
1012 // update it's class-specific state.
1013 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
1014 Record->addedMember(D);
John McCalld1e9d832009-08-11 06:59:38 +00001015}
1016
1017void DeclContext::addDecl(Decl *D) {
1018 addHiddenDecl(D);
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001019
1020 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001021 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor91f84212008-12-11 16:49:14 +00001022}
1023
Douglas Gregor07665a62009-01-05 19:45:36 +00001024/// buildLookup - Build the lookup data structure with all of the
1025/// declarations in DCtx (and any other contexts linked to it or
1026/// transparent contexts nested within it).
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001027void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregor07665a62009-01-05 19:45:36 +00001028 for (; DCtx; DCtx = DCtx->getNextContext()) {
Mike Stump11289f42009-09-09 15:08:12 +00001029 for (decl_iterator D = DCtx->decls_begin(),
1030 DEnd = DCtx->decls_end();
Douglas Gregord05cb412009-01-06 07:17:58 +00001031 D != DEnd; ++D) {
John McCalld1e9d832009-08-11 06:59:38 +00001032 // Insert this declaration into the lookup structure, but only
1033 // if it's semantically in its decl context. During non-lazy
1034 // lookup building, this is implicitly enforced by addDecl.
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001035 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
John McCalld1e9d832009-08-11 06:59:38 +00001036 if (D->getDeclContext() == DCtx)
1037 makeDeclVisibleInContextImpl(ND);
Douglas Gregor07665a62009-01-05 19:45:36 +00001038
Ted Kremenek707ece62009-11-17 22:58:30 +00001039 // Insert any forward-declared Objective-C interfaces into the lookup
1040 // data structure.
1041 if (ObjCClassDecl *Class = dyn_cast<ObjCClassDecl>(*D))
1042 for (ObjCClassDecl::iterator I = Class->begin(), IEnd = Class->end();
1043 I != IEnd; ++I)
Ted Kremenek9b124e12009-11-18 00:28:11 +00001044 makeDeclVisibleInContextImpl(I->getInterface());
Ted Kremenek707ece62009-11-17 22:58:30 +00001045
Sebastian Redlbd595762010-08-31 20:53:31 +00001046 // If this declaration is itself a transparent declaration context or
1047 // inline namespace, add its members (recursively).
Douglas Gregor07665a62009-01-05 19:45:36 +00001048 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
Sebastian Redlbd595762010-08-31 20:53:31 +00001049 if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001050 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregor07665a62009-01-05 19:45:36 +00001051 }
1052 }
1053}
1054
Mike Stump11289f42009-09-09 15:08:12 +00001055DeclContext::lookup_result
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001056DeclContext::lookup(DeclarationName Name) {
Steve Naroff35c62ae2009-01-08 17:28:14 +00001057 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor91f84212008-12-11 16:49:14 +00001058 if (PrimaryContext != this)
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001059 return PrimaryContext->lookup(Name);
Douglas Gregor91f84212008-12-11 16:49:14 +00001060
John McCall75b960e2010-06-01 09:23:16 +00001061 if (hasExternalVisibleStorage()) {
1062 // Check to see if we've already cached the lookup results.
1063 if (LookupPtr) {
1064 StoredDeclsMap::iterator I = LookupPtr->find(Name);
1065 if (I != LookupPtr->end())
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001066 return I->second.getLookupResult();
John McCall75b960e2010-06-01 09:23:16 +00001067 }
1068
1069 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1070 return Source->FindExternalVisibleDeclsByName(this, Name);
1071 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001072
Douglas Gregor55297ac2008-12-23 00:26:44 +00001073 /// If there is no lookup data structure, build one now by walking
Douglas Gregor91f84212008-12-11 16:49:14 +00001074 /// all of the linked DeclContexts (in declaration order!) and
1075 /// inserting their values.
Douglas Gregor9615ec22009-04-09 17:29:08 +00001076 if (!LookupPtr) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001077 buildLookup(this);
Douglas Gregor91f84212008-12-11 16:49:14 +00001078
Douglas Gregor9615ec22009-04-09 17:29:08 +00001079 if (!LookupPtr)
Douglas Gregor10dc8aa2010-05-11 06:18:17 +00001080 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Douglas Gregor9615ec22009-04-09 17:29:08 +00001081 }
Douglas Gregor91f84212008-12-11 16:49:14 +00001082
John McCallc62bb642010-03-24 05:22:00 +00001083 StoredDeclsMap::iterator Pos = LookupPtr->find(Name);
1084 if (Pos == LookupPtr->end())
Douglas Gregor10dc8aa2010-05-11 06:18:17 +00001085 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001086 return Pos->second.getLookupResult();
Douglas Gregor91f84212008-12-11 16:49:14 +00001087}
1088
Mike Stump11289f42009-09-09 15:08:12 +00001089DeclContext::lookup_const_result
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001090DeclContext::lookup(DeclarationName Name) const {
1091 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor91f84212008-12-11 16:49:14 +00001092}
1093
Sebastian Redl50c68252010-08-31 00:36:30 +00001094DeclContext *DeclContext::getRedeclContext() {
Chris Lattner17a1bfa2009-03-27 19:19:59 +00001095 DeclContext *Ctx = this;
Sebastian Redlbd595762010-08-31 20:53:31 +00001096 // Skip through transparent contexts.
1097 while (Ctx->isTransparentContext())
Douglas Gregor6ad0ef52009-01-06 23:51:29 +00001098 Ctx = Ctx->getParent();
1099 return Ctx;
1100}
1101
Douglas Gregorf47b9112009-02-25 22:02:03 +00001102DeclContext *DeclContext::getEnclosingNamespaceContext() {
1103 DeclContext *Ctx = this;
1104 // Skip through non-namespace, non-translation-unit contexts.
Sebastian Redl4f08c962010-08-31 00:36:23 +00001105 while (!Ctx->isFileContext())
Douglas Gregorf47b9112009-02-25 22:02:03 +00001106 Ctx = Ctx->getParent();
1107 return Ctx->getPrimaryContext();
1108}
1109
Sebastian Redl50c68252010-08-31 00:36:30 +00001110bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
1111 // For non-file contexts, this is equivalent to Equals.
1112 if (!isFileContext())
1113 return O->Equals(this);
1114
1115 do {
1116 if (O->Equals(this))
1117 return true;
1118
1119 const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O);
1120 if (!NS || !NS->isInline())
1121 break;
1122 O = NS->getParent();
1123 } while (O);
1124
1125 return false;
1126}
1127
John McCall759e32b2009-08-31 22:39:49 +00001128void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable) {
Douglas Gregor67a65642009-02-17 23:15:12 +00001129 // FIXME: This feels like a hack. Should DeclarationName support
1130 // template-ids, or is there a better way to keep specializations
1131 // from being visible?
Douglas Gregorfd7c2252011-03-04 17:52:15 +00001132 if (isa<ClassTemplateSpecializationDecl>(D) || D->isTemplateParameter())
Douglas Gregor67a65642009-02-17 23:15:12 +00001133 return;
Eli Friedman73168192009-12-08 05:40:03 +00001134 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1135 if (FD->isFunctionTemplateSpecialization())
1136 return;
Douglas Gregor67a65642009-02-17 23:15:12 +00001137
Steve Naroff35c62ae2009-01-08 17:28:14 +00001138 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor91f84212008-12-11 16:49:14 +00001139 if (PrimaryContext != this) {
John McCall759e32b2009-08-31 22:39:49 +00001140 PrimaryContext->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor91f84212008-12-11 16:49:14 +00001141 return;
1142 }
1143
1144 // If we already have a lookup data structure, perform the insertion
Argyrios Kyrtzidise51e5542010-07-04 21:44:25 +00001145 // into it. If we haven't deserialized externally stored decls, deserialize
1146 // them so we can add the decl. Otherwise, be lazy and don't build that
1147 // structure until someone asks for it.
1148 if (LookupPtr || !Recoverable || hasExternalVisibleStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001149 makeDeclVisibleInContextImpl(D);
Douglas Gregor07665a62009-01-05 19:45:36 +00001150
Sebastian Redlbd595762010-08-31 20:53:31 +00001151 // If we are a transparent context or inline namespace, insert into our
1152 // parent context, too. This operation is recursive.
1153 if (isTransparentContext() || isInlineNamespace())
John McCall759e32b2009-08-31 22:39:49 +00001154 getParent()->makeDeclVisibleInContext(D, Recoverable);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00001155
1156 Decl *DCAsDecl = cast<Decl>(this);
1157 // Notify that a decl was made visible unless it's a Tag being defined.
1158 if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined()))
1159 if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
1160 L->AddedVisibleDecl(this, D);
Douglas Gregor91f84212008-12-11 16:49:14 +00001161}
1162
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001163void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) {
Douglas Gregor07665a62009-01-05 19:45:36 +00001164 // Skip unnamed declarations.
1165 if (!D->getDeclName())
1166 return;
1167
Douglas Gregor67a65642009-02-17 23:15:12 +00001168 // FIXME: This feels like a hack. Should DeclarationName support
1169 // template-ids, or is there a better way to keep specializations
1170 // from being visible?
Douglas Gregorfd7c2252011-03-04 17:52:15 +00001171 if (isa<ClassTemplateSpecializationDecl>(D) || D->isTemplateParameter())
Douglas Gregor67a65642009-02-17 23:15:12 +00001172 return;
1173
Argyrios Kyrtzidise51e5542010-07-04 21:44:25 +00001174 ASTContext *C = 0;
1175 if (!LookupPtr) {
1176 C = &getParentASTContext();
1177 CreateStoredDeclsMap(*C);
1178 }
1179
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001180 // If there is an external AST source, load any declarations it knows about
1181 // with this declaration's name.
1182 // If the lookup table contains an entry about this name it means that we
1183 // have already checked the external source.
1184 if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
1185 if (hasExternalVisibleStorage() &&
1186 LookupPtr->find(D->getDeclName()) == LookupPtr->end())
1187 Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
1188
Douglas Gregor91f84212008-12-11 16:49:14 +00001189 // Insert this declaration into the map.
John McCallc62bb642010-03-24 05:22:00 +00001190 StoredDeclsList &DeclNameEntries = (*LookupPtr)[D->getDeclName()];
Chris Lattnercaae7162009-02-20 01:44:05 +00001191 if (DeclNameEntries.isNull()) {
1192 DeclNameEntries.setOnlyValue(D);
Chris Lattnerdfd6b3d2009-02-19 07:00:44 +00001193 return;
Douglas Gregor91f84212008-12-11 16:49:14 +00001194 }
Chris Lattner24e24d52009-02-20 00:55:03 +00001195
Chris Lattner29578f32009-02-20 01:10:07 +00001196 // If it is possible that this is a redeclaration, check to see if there is
1197 // already a decl for which declarationReplaces returns true. If there is
1198 // one, just replace it and return.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001199 if (DeclNameEntries.HandleRedeclaration(D))
Chris Lattnercaae7162009-02-20 01:44:05 +00001200 return;
Mike Stump11289f42009-09-09 15:08:12 +00001201
Chris Lattnerdfd6b3d2009-02-19 07:00:44 +00001202 // Put this declaration into the appropriate slot.
Chris Lattnercaae7162009-02-20 01:44:05 +00001203 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor91f84212008-12-11 16:49:14 +00001204}
Douglas Gregor889ceb72009-02-03 19:21:40 +00001205
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00001206void DeclContext::MaterializeVisibleDeclsFromExternalStorage() {
1207 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1208 assert(hasExternalVisibleStorage() && Source && "No external storage?");
1209
1210 if (!LookupPtr)
1211 CreateStoredDeclsMap(getParentASTContext());
1212 Source->MaterializeVisibleDecls(this);
1213}
1214
Douglas Gregor889ceb72009-02-03 19:21:40 +00001215/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
1216/// this context.
Mike Stump11289f42009-09-09 15:08:12 +00001217DeclContext::udir_iterator_range
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001218DeclContext::getUsingDirectives() const {
1219 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor889ceb72009-02-03 19:21:40 +00001220 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
1221 reinterpret_cast<udir_iterator>(Result.second));
1222}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001223
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001224//===----------------------------------------------------------------------===//
1225// Creation and Destruction of StoredDeclsMaps. //
1226//===----------------------------------------------------------------------===//
1227
John McCallc62bb642010-03-24 05:22:00 +00001228StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
1229 assert(!LookupPtr && "context already has a decls map");
1230 assert(getPrimaryContext() == this &&
1231 "creating decls map on non-primary context");
1232
1233 StoredDeclsMap *M;
1234 bool Dependent = isDependentContext();
1235 if (Dependent)
1236 M = new DependentStoredDeclsMap();
1237 else
1238 M = new StoredDeclsMap();
1239 M->Previous = C.LastSDM;
1240 C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
1241 LookupPtr = M;
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001242 return M;
1243}
1244
1245void ASTContext::ReleaseDeclContextMaps() {
John McCallc62bb642010-03-24 05:22:00 +00001246 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1247 // pointer because the subclass doesn't add anything that needs to
1248 // be deleted.
John McCallc62bb642010-03-24 05:22:00 +00001249 StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1250}
1251
1252void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1253 while (Map) {
1254 // Advance the iteration before we invalidate memory.
1255 llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1256
1257 if (Dependent)
1258 delete static_cast<DependentStoredDeclsMap*>(Map);
1259 else
1260 delete Map;
1261
1262 Map = Next.getPointer();
1263 Dependent = Next.getInt();
1264 }
1265}
1266
John McCallc62bb642010-03-24 05:22:00 +00001267DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1268 DeclContext *Parent,
1269 const PartialDiagnostic &PDiag) {
1270 assert(Parent->isDependentContext()
1271 && "cannot iterate dependent diagnostics of non-dependent context");
1272 Parent = Parent->getPrimaryContext();
1273 if (!Parent->LookupPtr)
1274 Parent->CreateStoredDeclsMap(C);
1275
1276 DependentStoredDeclsMap *Map
1277 = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr);
1278
Douglas Gregora55530e2010-03-29 23:56:53 +00001279 // Allocate the copy of the PartialDiagnostic via the ASTContext's
Douglas Gregor89336232010-03-29 23:34:08 +00001280 // BumpPtrAllocator, rather than the ASTContext itself.
Douglas Gregora55530e2010-03-29 23:56:53 +00001281 PartialDiagnostic::Storage *DiagStorage = 0;
1282 if (PDiag.hasStorage())
1283 DiagStorage = new (C) PartialDiagnostic::Storage;
1284
1285 DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
John McCallc62bb642010-03-24 05:22:00 +00001286
1287 // TODO: Maybe we shouldn't reverse the order during insertion.
1288 DD->NextDiagnostic = Map->FirstDiagnostic;
1289 Map->FirstDiagnostic = DD;
1290
1291 return DD;
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001292}