blob: 673b718ba0ed866f76acf8300b63d2e065522725 [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>
Chris Lattner3daed522009-03-02 22:20:04 +000032#include <cstdio>
Douglas Gregor3fc749d2008-12-23 00:26:44 +000033#include <vector>
Eli Friedman56d29372008-06-07 16:52:53 +000034using namespace clang;
35
36//===----------------------------------------------------------------------===//
37// Statistics
38//===----------------------------------------------------------------------===//
39
Sean Hunt9a555912010-05-30 07:21:58 +000040#define DECL(DERIVED, BASE) static int n##DERIVED##s = 0;
41#define ABSTRACT_DECL(DECL)
42#include "clang/AST/DeclNodes.inc"
Eli Friedman56d29372008-06-07 16:52:53 +000043
44static bool StatSwitch = false;
45
Eli Friedman56d29372008-06-07 16:52:53 +000046const char *Decl::getDeclKindName() const {
47 switch (DeclKind) {
Sean Hunt9a555912010-05-30 07:21:58 +000048 default: assert(0 && "Declaration not in DeclNodes.inc!");
49#define DECL(DERIVED, BASE) case DERIVED: return #DERIVED;
50#define ABSTRACT_DECL(DECL)
51#include "clang/AST/DeclNodes.inc"
Eli Friedman56d29372008-06-07 16:52:53 +000052 }
53}
54
Douglas Gregor42738572010-03-05 00:26:45 +000055void Decl::setInvalidDecl(bool Invalid) {
56 InvalidDecl = Invalid;
57 if (Invalid) {
58 // Defensive maneuver for ill-formed code: we're likely not to make it to
59 // a point where we set the access specifier, so default it to "public"
60 // to avoid triggering asserts elsewhere in the front end.
61 setAccess(AS_public);
62 }
63}
64
Steve Naroff0a473932009-01-20 19:53:53 +000065const char *DeclContext::getDeclKindName() const {
66 switch (DeclKind) {
Sean Hunt9a555912010-05-30 07:21:58 +000067 default: assert(0 && "Declaration context not in DeclNodes.inc!");
68#define DECL(DERIVED, BASE) case Decl::DERIVED: return #DERIVED;
69#define ABSTRACT_DECL(DECL)
70#include "clang/AST/DeclNodes.inc"
Steve Naroff0a473932009-01-20 19:53:53 +000071 }
72}
73
Eli Friedman56d29372008-06-07 16:52:53 +000074bool Decl::CollectingStats(bool Enable) {
Kovarththanan Rajaratnam2024f4d2009-11-29 14:54:35 +000075 if (Enable) StatSwitch = true;
Eli Friedman56d29372008-06-07 16:52:53 +000076 return StatSwitch;
77}
78
79void Decl::PrintStats() {
80 fprintf(stderr, "*** Decl Stats:\n");
Mike Stump1eb44332009-09-09 15:08:12 +000081
Douglas Gregor64650af2009-02-02 23:39:07 +000082 int totalDecls = 0;
Sean Hunt9a555912010-05-30 07:21:58 +000083#define DECL(DERIVED, BASE) totalDecls += n##DERIVED##s;
84#define ABSTRACT_DECL(DECL)
85#include "clang/AST/DeclNodes.inc"
Douglas Gregor64650af2009-02-02 23:39:07 +000086 fprintf(stderr, " %d decls total.\n", totalDecls);
Mike Stump1eb44332009-09-09 15:08:12 +000087
Douglas Gregor64650af2009-02-02 23:39:07 +000088 int totalBytes = 0;
Sean Hunt9a555912010-05-30 07:21:58 +000089#define DECL(DERIVED, BASE) \
90 if (n##DERIVED##s > 0) { \
91 totalBytes += (int)(n##DERIVED##s * sizeof(DERIVED##Decl)); \
92 fprintf(stderr, " %d " #DERIVED " decls, %d each (%d bytes)\n", \
93 n##DERIVED##s, (int)sizeof(DERIVED##Decl), \
94 (int)(n##DERIVED##s * sizeof(DERIVED##Decl))); \
Douglas Gregor64650af2009-02-02 23:39:07 +000095 }
Sean Hunt9a555912010-05-30 07:21:58 +000096#define ABSTRACT_DECL(DECL)
97#include "clang/AST/DeclNodes.inc"
Mike Stump1eb44332009-09-09 15:08:12 +000098
Douglas Gregor64650af2009-02-02 23:39:07 +000099 fprintf(stderr, "Total bytes = %d\n", totalBytes);
Eli Friedman56d29372008-06-07 16:52:53 +0000100}
101
Sean Hunt9a555912010-05-30 07:21:58 +0000102void Decl::add(Kind k) {
Eli Friedman56d29372008-06-07 16:52:53 +0000103 switch (k) {
Sean Hunt9a555912010-05-30 07:21:58 +0000104 default: assert(0 && "Declaration not in DeclNodes.inc!");
105#define DECL(DERIVED, BASE) case DERIVED: ++n##DERIVED##s; break;
106#define ABSTRACT_DECL(DECL)
107#include "clang/AST/DeclNodes.inc"
Eli Friedman56d29372008-06-07 16:52:53 +0000108 }
109}
110
Anders Carlsson67e33202009-06-13 00:08:58 +0000111bool Decl::isTemplateParameterPack() const {
112 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
113 return TTP->isParameterPack();
Douglas Gregor10738d32010-12-23 23:51:58 +0000114 if (const NonTypeTemplateParmDecl *NTTP
Douglas Gregor61c4d282011-01-05 15:48:55 +0000115 = dyn_cast<NonTypeTemplateParmDecl>(this))
Douglas Gregor10738d32010-12-23 23:51:58 +0000116 return NTTP->isParameterPack();
Douglas Gregor61c4d282011-01-05 15:48:55 +0000117 if (const TemplateTemplateParmDecl *TTP
118 = dyn_cast<TemplateTemplateParmDecl>(this))
119 return TTP->isParameterPack();
Anders Carlsson67e33202009-06-13 00:08:58 +0000120 return false;
121}
122
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000123bool Decl::isParameterPack() const {
124 if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(this))
125 return Parm->isParameterPack();
126
127 return isTemplateParameterPack();
128}
129
Douglas Gregore53060f2009-06-25 22:08:12 +0000130bool Decl::isFunctionOrFunctionTemplate() const {
John McCall9488ea12009-11-17 05:59:44 +0000131 if (const UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(this))
Anders Carlsson58badb72009-06-26 05:26:50 +0000132 return UD->getTargetDecl()->isFunctionOrFunctionTemplate();
Mike Stump1eb44332009-09-09 15:08:12 +0000133
Douglas Gregore53060f2009-06-25 22:08:12 +0000134 return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this);
135}
136
Douglas Gregor79c22782010-01-16 20:21:20 +0000137bool Decl::isDefinedOutsideFunctionOrMethod() const {
138 for (const DeclContext *DC = getDeclContext();
139 DC && !DC->isTranslationUnit();
140 DC = DC->getParent())
141 if (DC->isFunctionOrMethod())
142 return false;
143
144 return true;
145}
146
Douglas Gregor4c3e0ee2011-02-17 08:47:29 +0000147
Eli Friedman56d29372008-06-07 16:52:53 +0000148//===----------------------------------------------------------------------===//
Chris Lattner49f28ca2009-03-05 08:00:35 +0000149// PrettyStackTraceDecl Implementation
150//===----------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000151
Chris Lattner49f28ca2009-03-05 08:00:35 +0000152void PrettyStackTraceDecl::print(llvm::raw_ostream &OS) const {
153 SourceLocation TheLoc = Loc;
154 if (TheLoc.isInvalid() && TheDecl)
155 TheLoc = TheDecl->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Chris Lattner49f28ca2009-03-05 08:00:35 +0000157 if (TheLoc.isValid()) {
158 TheLoc.print(OS, SM);
159 OS << ": ";
160 }
161
162 OS << Message;
163
Daniel Dunbarc5236562009-11-21 09:05:59 +0000164 if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
Chris Lattner49f28ca2009-03-05 08:00:35 +0000165 OS << " '" << DN->getQualifiedNameAsString() << '\'';
166 OS << '\n';
167}
Mike Stump1eb44332009-09-09 15:08:12 +0000168
Chris Lattner49f28ca2009-03-05 08:00:35 +0000169//===----------------------------------------------------------------------===//
Eli Friedman56d29372008-06-07 16:52:53 +0000170// Decl Implementation
171//===----------------------------------------------------------------------===//
172
Douglas Gregorda2142f2011-02-19 18:51:44 +0000173// Out-of-line virtual method providing a home for Decl.
174Decl::~Decl() { }
Douglas Gregorf4a03cc2011-02-17 07:02:32 +0000175
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000176void Decl::setDeclContext(DeclContext *DC) {
Chris Lattneree219fd2009-03-29 06:06:59 +0000177 DeclCtx = DC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000178}
179
180void Decl::setLexicalDeclContext(DeclContext *DC) {
181 if (DC == getLexicalDeclContext())
182 return;
183
184 if (isInSemaDC()) {
Ted Kremenek94a39002009-12-01 00:07:10 +0000185 MultipleDC *MDC = new (getASTContext()) MultipleDC();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000186 MDC->SemanticDC = getDeclContext();
187 MDC->LexicalDC = DC;
Chris Lattneree219fd2009-03-29 06:06:59 +0000188 DeclCtx = MDC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000189 } else {
190 getMultipleDC()->LexicalDC = DC;
191 }
192}
193
John McCall9aeed322009-10-01 00:25:31 +0000194bool Decl::isInAnonymousNamespace() const {
195 const DeclContext *DC = getDeclContext();
196 do {
197 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
198 if (ND->isAnonymousNamespace())
199 return true;
200 } while ((DC = DC->getParent()));
201
202 return false;
203}
204
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000205TranslationUnitDecl *Decl::getTranslationUnitDecl() {
Argyrios Kyrtzidis9b346692009-06-30 02:34:53 +0000206 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
207 return TUD;
208
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000209 DeclContext *DC = getDeclContext();
210 assert(DC && "This decl is not contained in a translation unit!");
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000212 while (!DC->isTranslationUnit()) {
213 DC = DC->getParent();
214 assert(DC && "This decl is not contained in a translation unit!");
215 }
Mike Stump1eb44332009-09-09 15:08:12 +0000216
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000217 return cast<TranslationUnitDecl>(DC);
218}
219
220ASTContext &Decl::getASTContext() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000221 return getTranslationUnitDecl()->getASTContext();
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000222}
223
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +0000224ASTMutationListener *Decl::getASTMutationListener() const {
225 return getASTContext().getASTMutationListener();
226}
227
Douglas Gregorc070cc62010-06-17 23:14:26 +0000228bool Decl::isUsed(bool CheckUsedAttr) const {
Tanya Lattner12ead492010-02-17 02:17:21 +0000229 if (Used)
230 return true;
231
232 // Check for used attribute.
Douglas Gregorc070cc62010-06-17 23:14:26 +0000233 if (CheckUsedAttr && hasAttr<UsedAttr>())
Tanya Lattner12ead492010-02-17 02:17:21 +0000234 return true;
235
236 // Check redeclarations for used attribute.
237 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Douglas Gregorc070cc62010-06-17 23:14:26 +0000238 if ((CheckUsedAttr && I->hasAttr<UsedAttr>()) || I->Used)
Tanya Lattner12ead492010-02-17 02:17:21 +0000239 return true;
240 }
241
242 return false;
243}
244
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000245/// \brief Determine the availability of the given declaration based on
246/// the target platform.
247///
248/// When it returns an availability result other than \c AR_Available,
249/// if the \p Message parameter is non-NULL, it will be set to a
250/// string describing why the entity is unavailable.
251///
252/// FIXME: Make these strings localizable, since they end up in
253/// diagnostics.
254static AvailabilityResult CheckAvailability(ASTContext &Context,
255 const AvailabilityAttr *A,
256 std::string *Message) {
257 llvm::StringRef TargetPlatform = Context.Target.getPlatformName();
258 llvm::StringRef PrettyPlatformName
259 = AvailabilityAttr::getPrettyPlatformName(TargetPlatform);
260 if (PrettyPlatformName.empty())
261 PrettyPlatformName = TargetPlatform;
262
263 VersionTuple TargetMinVersion = Context.Target.getPlatformMinVersion();
264 if (TargetMinVersion.empty())
265 return AR_Available;
266
267 // Match the platform name.
268 if (A->getPlatform()->getName() != TargetPlatform)
269 return AR_Available;
270
Douglas Gregorb53e4172011-03-26 03:35:55 +0000271 // Make sure that this declaration has not been marked 'unavailable'.
272 if (A->getUnavailable()) {
273 if (Message) {
274 Message->clear();
275 llvm::raw_string_ostream Out(*Message);
276 Out << "not available on " << PrettyPlatformName;
277 }
278
279 return AR_Unavailable;
280 }
281
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000282 // Make sure that this declaration has already been introduced.
283 if (!A->getIntroduced().empty() &&
284 TargetMinVersion < A->getIntroduced()) {
285 if (Message) {
286 Message->clear();
287 llvm::raw_string_ostream Out(*Message);
288 Out << "introduced in " << PrettyPlatformName << ' '
289 << A->getIntroduced();
290 }
291
292 return AR_NotYetIntroduced;
293 }
294
295 // Make sure that this declaration hasn't been obsoleted.
296 if (!A->getObsoleted().empty() && TargetMinVersion >= A->getObsoleted()) {
297 if (Message) {
298 Message->clear();
299 llvm::raw_string_ostream Out(*Message);
300 Out << "obsoleted in " << PrettyPlatformName << ' '
301 << A->getObsoleted();
302 }
303
304 return AR_Unavailable;
305 }
306
307 // Make sure that this declaration hasn't been deprecated.
308 if (!A->getDeprecated().empty() && TargetMinVersion >= A->getDeprecated()) {
309 if (Message) {
310 Message->clear();
311 llvm::raw_string_ostream Out(*Message);
312 Out << "first deprecated in " << PrettyPlatformName << ' '
313 << A->getDeprecated();
314 }
315
316 return AR_Deprecated;
317 }
318
319 return AR_Available;
320}
321
322AvailabilityResult Decl::getAvailability(std::string *Message) const {
323 AvailabilityResult Result = AR_Available;
324 std::string ResultMessage;
325
326 for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
327 if (DeprecatedAttr *Deprecated = dyn_cast<DeprecatedAttr>(*A)) {
328 if (Result >= AR_Deprecated)
329 continue;
330
331 if (Message)
332 ResultMessage = Deprecated->getMessage();
333
334 Result = AR_Deprecated;
335 continue;
336 }
337
338 if (UnavailableAttr *Unavailable = dyn_cast<UnavailableAttr>(*A)) {
339 if (Message)
340 *Message = Unavailable->getMessage();
341 return AR_Unavailable;
342 }
343
344 if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
345 AvailabilityResult AR = CheckAvailability(getASTContext(), Availability,
346 Message);
347
348 if (AR == AR_Unavailable)
349 return AR_Unavailable;
350
351 if (AR > Result) {
352 Result = AR;
353 if (Message)
354 ResultMessage.swap(*Message);
355 }
356 continue;
357 }
358 }
359
360 if (Message)
361 Message->swap(ResultMessage);
362 return Result;
363}
364
365bool Decl::canBeWeakImported(bool &IsDefinition) const {
366 IsDefinition = false;
367 if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
368 if (!Var->hasExternalStorage() || Var->getInit()) {
369 IsDefinition = true;
370 return false;
371 }
372 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
373 if (FD->hasBody()) {
374 IsDefinition = true;
375 return false;
376 }
377 } else if (isa<ObjCPropertyDecl>(this) || isa<ObjCMethodDecl>(this))
378 return false;
379 else if (!(getASTContext().getLangOptions().ObjCNonFragileABI &&
380 isa<ObjCInterfaceDecl>(this)))
381 return false;
382
383 return true;
384}
385
386bool Decl::isWeakImported() const {
387 bool IsDefinition;
388 if (!canBeWeakImported(IsDefinition))
389 return false;
390
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000391 for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
392 if (isa<WeakImportAttr>(*A))
393 return true;
394
395 if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
396 if (CheckAvailability(getASTContext(), Availability, 0)
397 == AR_NotYetIntroduced)
398 return true;
399 }
400 }
401
402 return false;
403}
Tanya Lattner12ead492010-02-17 02:17:21 +0000404
Chris Lattner769dbdf2009-03-27 20:18:19 +0000405unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
406 switch (DeclKind) {
John McCall9488ea12009-11-17 05:59:44 +0000407 case Function:
408 case CXXMethod:
409 case CXXConstructor:
410 case CXXDestructor:
411 case CXXConversion:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000412 case EnumConstant:
413 case Var:
414 case ImplicitParam:
415 case ParmVar:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000416 case NonTypeTemplateParm:
417 case ObjCMethod:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000418 case ObjCProperty:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000419 return IDNS_Ordinary;
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000420 case Label:
421 return IDNS_Label;
Francois Pichet87c2e122010-11-21 06:08:52 +0000422 case IndirectField:
423 return IDNS_Ordinary | IDNS_Member;
424
John McCall0d6b1642010-04-23 18:46:30 +0000425 case ObjCCompatibleAlias:
426 case ObjCInterface:
427 return IDNS_Ordinary | IDNS_Type;
428
429 case Typedef:
Richard Smith162e1c12011-04-15 14:24:37 +0000430 case TypeAlias:
John McCall0d6b1642010-04-23 18:46:30 +0000431 case UnresolvedUsingTypename:
432 case TemplateTypeParm:
433 return IDNS_Ordinary | IDNS_Type;
434
John McCall9488ea12009-11-17 05:59:44 +0000435 case UsingShadow:
436 return 0; // we'll actually overwrite this later
437
John McCall7ba107a2009-11-18 02:36:19 +0000438 case UnresolvedUsingValue:
John McCall7ba107a2009-11-18 02:36:19 +0000439 return IDNS_Ordinary | IDNS_Using;
John McCall9488ea12009-11-17 05:59:44 +0000440
441 case Using:
442 return IDNS_Using;
443
Chris Lattner769dbdf2009-03-27 20:18:19 +0000444 case ObjCProtocol:
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000445 return IDNS_ObjCProtocol;
Mike Stump1eb44332009-09-09 15:08:12 +0000446
Chris Lattner769dbdf2009-03-27 20:18:19 +0000447 case Field:
448 case ObjCAtDefsField:
449 case ObjCIvar:
450 return IDNS_Member;
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Chris Lattner769dbdf2009-03-27 20:18:19 +0000452 case Record:
453 case CXXRecord:
454 case Enum:
John McCall0d6b1642010-04-23 18:46:30 +0000455 return IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Chris Lattner769dbdf2009-03-27 20:18:19 +0000457 case Namespace:
John McCall0d6b1642010-04-23 18:46:30 +0000458 case NamespaceAlias:
459 return IDNS_Namespace;
460
Chris Lattner769dbdf2009-03-27 20:18:19 +0000461 case FunctionTemplate:
John McCall0d6b1642010-04-23 18:46:30 +0000462 return IDNS_Ordinary;
463
Chris Lattner769dbdf2009-03-27 20:18:19 +0000464 case ClassTemplate:
465 case TemplateTemplateParm:
John McCall0d6b1642010-04-23 18:46:30 +0000466 return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Chris Lattner769dbdf2009-03-27 20:18:19 +0000468 // Never have names.
John McCall02cace72009-08-28 07:59:38 +0000469 case Friend:
John McCalldd4a3b02009-09-16 22:47:08 +0000470 case FriendTemplate:
Abramo Bagnara6206d532010-06-05 05:09:32 +0000471 case AccessSpec:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000472 case LinkageSpec:
473 case FileScopeAsm:
474 case StaticAssert:
475 case ObjCClass:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000476 case ObjCPropertyImpl:
477 case ObjCForwardProtocol:
478 case Block:
479 case TranslationUnit:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000480
Chris Lattner769dbdf2009-03-27 20:18:19 +0000481 case UsingDirective:
482 case ClassTemplateSpecialization:
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000483 case ClassTemplatePartialSpecialization:
Douglas Gregorbd4187b2010-04-22 23:19:50 +0000484 case ObjCImplementation:
485 case ObjCCategory:
486 case ObjCCategoryImpl:
487 // Never looked up by name.
Chris Lattner769dbdf2009-03-27 20:18:19 +0000488 return 0;
489 }
John McCall9488ea12009-11-17 05:59:44 +0000490
491 return 0;
Eli Friedman56d29372008-06-07 16:52:53 +0000492}
493
Sean Huntcf807c42010-08-18 23:23:40 +0000494void Decl::setAttrs(const AttrVec &attrs) {
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000495 assert(!HasAttrs && "Decl already contains attrs.");
496
Sean Huntcf807c42010-08-18 23:23:40 +0000497 AttrVec &AttrBlank = getASTContext().getDeclAttrs(this);
498 assert(AttrBlank.empty() && "HasAttrs was wrong?");
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000499
500 AttrBlank = attrs;
501 HasAttrs = true;
502}
503
Sean Huntcf807c42010-08-18 23:23:40 +0000504void Decl::dropAttrs() {
Eli Friedman56d29372008-06-07 16:52:53 +0000505 if (!HasAttrs) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000506
Eli Friedman56d29372008-06-07 16:52:53 +0000507 HasAttrs = false;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000508 getASTContext().eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000509}
510
Sean Huntcf807c42010-08-18 23:23:40 +0000511const AttrVec &Decl::getAttrs() const {
512 assert(HasAttrs && "No attrs to get!");
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000513 return getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000514}
515
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000516void Decl::swapAttrs(Decl *RHS) {
Eli Friedman56d29372008-06-07 16:52:53 +0000517 bool HasLHSAttr = this->HasAttrs;
518 bool HasRHSAttr = RHS->HasAttrs;
Mike Stump1eb44332009-09-09 15:08:12 +0000519
Eli Friedman56d29372008-06-07 16:52:53 +0000520 // Usually, neither decl has attrs, nothing to do.
521 if (!HasLHSAttr && !HasRHSAttr) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000522
Eli Friedman56d29372008-06-07 16:52:53 +0000523 // If 'this' has no attrs, swap the other way.
524 if (!HasLHSAttr)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000525 return RHS->swapAttrs(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000527 ASTContext &Context = getASTContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000528
Eli Friedman56d29372008-06-07 16:52:53 +0000529 // Handle the case when both decls have attrs.
530 if (HasRHSAttr) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000531 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman56d29372008-06-07 16:52:53 +0000532 return;
533 }
Mike Stump1eb44332009-09-09 15:08:12 +0000534
Eli Friedman56d29372008-06-07 16:52:53 +0000535 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor68584ed2009-06-18 16:11:24 +0000536 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
537 Context.eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000538 this->HasAttrs = false;
539 RHS->HasAttrs = true;
540}
541
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000542Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000543 Decl::Kind DK = D->getDeclKind();
544 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000545#define DECL(NAME, BASE)
546#define DECL_CONTEXT(NAME) \
547 case Decl::NAME: \
548 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
549#define DECL_CONTEXT_BASE(NAME)
550#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000551 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000552#define DECL(NAME, BASE)
553#define DECL_CONTEXT_BASE(NAME) \
554 if (DK >= first##NAME && DK <= last##NAME) \
555 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
556#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000557 assert(false && "a decl that inherits DeclContext isn't handled");
558 return 0;
559 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000560}
561
562DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000563 Decl::Kind DK = D->getKind();
564 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000565#define DECL(NAME, BASE)
566#define DECL_CONTEXT(NAME) \
567 case Decl::NAME: \
568 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
569#define DECL_CONTEXT_BASE(NAME)
570#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000571 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000572#define DECL(NAME, BASE)
573#define DECL_CONTEXT_BASE(NAME) \
574 if (DK >= first##NAME && DK <= last##NAME) \
575 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
576#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000577 assert(false && "a decl that inherits DeclContext isn't handled");
578 return 0;
579 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000580}
581
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000582SourceLocation Decl::getBodyRBrace() const {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000583 // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
584 // FunctionDecl stores EndRangeLoc for this purpose.
585 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
586 const FunctionDecl *Definition;
587 if (FD->hasBody(Definition))
588 return Definition->getSourceRange().getEnd();
589 return SourceLocation();
590 }
591
Argyrios Kyrtzidis6717ef42010-07-07 11:31:27 +0000592 if (Stmt *Body = getBody())
593 return Body->getSourceRange().getEnd();
594
595 return SourceLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +0000596}
597
Anders Carlsson1329c272009-03-25 23:38:06 +0000598void Decl::CheckAccessDeclContext() const {
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000599#ifndef NDEBUG
John McCall46460a62010-01-20 21:53:11 +0000600 // Suppress this check if any of the following hold:
601 // 1. this is the translation unit (and thus has no parent)
602 // 2. this is a template parameter (and thus doesn't belong to its context)
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000603 // 3. this is a non-type template parameter
604 // 4. the context is not a record
605 // 5. it's invalid
606 // 6. it's a C++0x static_assert.
Anders Carlsson35eda442009-08-29 20:47:47 +0000607 if (isa<TranslationUnitDecl>(this) ||
Argyrios Kyrtzidis04aed0e2010-07-02 11:55:44 +0000608 isa<TemplateTypeParmDecl>(this) ||
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000609 isa<NonTypeTemplateParmDecl>(this) ||
Douglas Gregorfdd8ab12010-02-22 17:53:38 +0000610 !isa<CXXRecordDecl>(getDeclContext()) ||
Argyrios Kyrtzidis65b63ec2010-09-08 21:32:35 +0000611 isInvalidDecl() ||
612 isa<StaticAssertDecl>(this) ||
613 // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
614 // as DeclContext (?).
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000615 isa<ParmVarDecl>(this) ||
616 // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
617 // AS_none as access specifier.
618 isa<CXXRecordDecl>(this))
Anders Carlsson35eda442009-08-29 20:47:47 +0000619 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000620
621 assert(Access != AS_none &&
Anders Carlsson1329c272009-03-25 23:38:06 +0000622 "Access specifier is AS_none inside a record decl");
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000623#endif
Anders Carlsson1329c272009-03-25 23:38:06 +0000624}
625
John McCallaab9e312011-02-22 22:25:23 +0000626DeclContext *Decl::getNonClosureContext() {
627 DeclContext *DC = getDeclContext();
628
629 // This is basically "while (DC->isClosure()) DC = DC->getParent();"
630 // except that it's significantly more efficient to cast to a known
631 // decl type and call getDeclContext() than to call getParent().
632 do {
633 if (isa<BlockDecl>(DC)) {
634 DC = cast<BlockDecl>(DC)->getDeclContext();
635 continue;
636 }
637 } while (false);
638
639 assert(!DC->isClosure());
640 return DC;
641}
Anders Carlsson1329c272009-03-25 23:38:06 +0000642
Eli Friedman56d29372008-06-07 16:52:53 +0000643//===----------------------------------------------------------------------===//
644// DeclContext Implementation
645//===----------------------------------------------------------------------===//
646
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000647bool DeclContext::classof(const Decl *D) {
648 switch (D->getKind()) {
Sean Hunt9a555912010-05-30 07:21:58 +0000649#define DECL(NAME, BASE)
650#define DECL_CONTEXT(NAME) case Decl::NAME:
651#define DECL_CONTEXT_BASE(NAME)
652#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000653 return true;
654 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000655#define DECL(NAME, BASE)
656#define DECL_CONTEXT_BASE(NAME) \
657 if (D->getKind() >= Decl::first##NAME && \
658 D->getKind() <= Decl::last##NAME) \
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000659 return true;
Sean Hunt9a555912010-05-30 07:21:58 +0000660#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000661 return false;
662 }
663}
664
Douglas Gregora2da7802010-07-25 18:38:02 +0000665DeclContext::~DeclContext() { }
Douglas Gregor44b43212008-12-11 16:49:14 +0000666
Douglas Gregore942bbe2009-09-10 16:57:35 +0000667/// \brief Find the parent context of this context that will be
668/// used for unqualified name lookup.
669///
670/// Generally, the parent lookup context is the semantic context. However, for
671/// a friend function the parent lookup context is the lexical context, which
672/// is the class in which the friend is declared.
673DeclContext *DeclContext::getLookupParent() {
674 // FIXME: Find a better way to identify friends
675 if (isa<FunctionDecl>(this))
Sebastian Redl7a126a42010-08-31 00:36:30 +0000676 if (getParent()->getRedeclContext()->isFileContext() &&
677 getLexicalParent()->getRedeclContext()->isRecord())
Douglas Gregore942bbe2009-09-10 16:57:35 +0000678 return getLexicalParent();
679
680 return getParent();
681}
682
Sebastian Redl410c4f22010-08-31 20:53:31 +0000683bool DeclContext::isInlineNamespace() const {
684 return isNamespace() &&
685 cast<NamespaceDecl>(this)->isInline();
686}
687
Douglas Gregorbc221632009-05-28 16:34:51 +0000688bool DeclContext::isDependentContext() const {
689 if (isFileContext())
690 return false;
691
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000692 if (isa<ClassTemplatePartialSpecializationDecl>(this))
693 return true;
694
Douglas Gregorbc221632009-05-28 16:34:51 +0000695 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
696 if (Record->getDescribedClassTemplate())
697 return true;
698
John McCall0c01d182010-03-24 05:22:00 +0000699 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregorbc221632009-05-28 16:34:51 +0000700 if (Function->getDescribedFunctionTemplate())
701 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000702
John McCall0c01d182010-03-24 05:22:00 +0000703 // Friend function declarations are dependent if their *lexical*
704 // context is dependent.
705 if (cast<Decl>(this)->getFriendObjectKind())
706 return getLexicalParent()->isDependentContext();
707 }
708
Douglas Gregorbc221632009-05-28 16:34:51 +0000709 return getParent() && getParent()->isDependentContext();
710}
711
Douglas Gregor074149e2009-01-05 19:45:36 +0000712bool DeclContext::isTransparentContext() const {
713 if (DeclKind == Decl::Enum)
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000714 return !cast<EnumDecl>(this)->isScoped();
Douglas Gregor074149e2009-01-05 19:45:36 +0000715 else if (DeclKind == Decl::LinkageSpec)
716 return true;
Douglas Gregor074149e2009-01-05 19:45:36 +0000717
718 return false;
719}
720
John McCallac65c622010-10-26 04:59:26 +0000721bool DeclContext::isExternCContext() const {
722 const DeclContext *DC = this;
723 while (DC->DeclKind != Decl::TranslationUnit) {
724 if (DC->DeclKind == Decl::LinkageSpec)
725 return cast<LinkageSpecDecl>(DC)->getLanguage()
726 == LinkageSpecDecl::lang_c;
727 DC = DC->getParent();
728 }
729 return false;
730}
731
Sebastian Redl7a126a42010-08-31 00:36:30 +0000732bool DeclContext::Encloses(const DeclContext *DC) const {
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000733 if (getPrimaryContext() != this)
734 return getPrimaryContext()->Encloses(DC);
Mike Stump1eb44332009-09-09 15:08:12 +0000735
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000736 for (; DC; DC = DC->getParent())
737 if (DC->getPrimaryContext() == this)
738 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000739 return false;
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000740}
741
Steve Naroff0701bbb2009-01-08 17:28:14 +0000742DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000743 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000744 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000745 case Decl::LinkageSpec:
Mike Stump1eb44332009-09-09 15:08:12 +0000746 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000747 // There is only one DeclContext for these entities.
748 return this;
749
750 case Decl::Namespace:
751 // The original namespace is our primary context.
752 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
753
Douglas Gregor44b43212008-12-11 16:49:14 +0000754 case Decl::ObjCMethod:
755 return this;
756
757 case Decl::ObjCInterface:
Steve Naroff0701bbb2009-01-08 17:28:14 +0000758 case Decl::ObjCProtocol:
759 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000760 // FIXME: Can Objective-C interfaces be forward-declared?
761 return this;
762
Steve Naroff0701bbb2009-01-08 17:28:14 +0000763 case Decl::ObjCImplementation:
764 case Decl::ObjCCategoryImpl:
765 return this;
766
Douglas Gregor44b43212008-12-11 16:49:14 +0000767 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000768 if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000769 // If this is a tag type that has a definition or is currently
770 // being defined, that definition is our primary context.
John McCall3cb0ebd2010-03-10 03:28:59 +0000771 TagDecl *Tag = cast<TagDecl>(this);
772 assert(isa<TagType>(Tag->TypeForDecl) ||
773 isa<InjectedClassNameType>(Tag->TypeForDecl));
774
775 if (TagDecl *Def = Tag->getDefinition())
776 return Def;
777
778 if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) {
779 const TagType *TagTy = cast<TagType>(Tag->TypeForDecl);
780 if (TagTy->isBeingDefined())
781 // FIXME: is it necessarily being defined in the decl
782 // that owns the type?
783 return TagTy->getDecl();
784 }
785
786 return Tag;
Douglas Gregorcc636682009-02-17 23:15:12 +0000787 }
788
Sean Hunt9a555912010-05-30 07:21:58 +0000789 assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
Douglas Gregor44b43212008-12-11 16:49:14 +0000790 "Unknown DeclContext kind");
791 return this;
792 }
793}
794
795DeclContext *DeclContext::getNextContext() {
796 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000797 case Decl::Namespace:
798 // Return the next namespace
799 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
800
801 default:
Douglas Gregor44b43212008-12-11 16:49:14 +0000802 return 0;
803 }
804}
805
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000806std::pair<Decl *, Decl *>
807DeclContext::BuildDeclChain(const llvm::SmallVectorImpl<Decl*> &Decls) {
808 // Build up a chain of declarations via the Decl::NextDeclInContext field.
809 Decl *FirstNewDecl = 0;
810 Decl *PrevDecl = 0;
811 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
812 Decl *D = Decls[I];
813 if (PrevDecl)
814 PrevDecl->NextDeclInContext = D;
815 else
816 FirstNewDecl = D;
817
818 PrevDecl = D;
819 }
820
821 return std::make_pair(FirstNewDecl, PrevDecl);
822}
823
Douglas Gregor2cf26342009-04-09 22:27:44 +0000824/// \brief Load the declarations within this lexical storage from an
825/// external source.
Mike Stump1eb44332009-09-09 15:08:12 +0000826void
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000827DeclContext::LoadLexicalDeclsFromExternalStorage() const {
828 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000829 assert(hasExternalLexicalStorage() && Source && "No external storage?");
830
Argyrios Kyrtzidis0dbbc042010-07-30 10:03:23 +0000831 // Notify that we have a DeclContext that is initializing.
832 ExternalASTSource::Deserializing ADeclContext(Source);
833
John McCall76bd1f32010-06-01 09:23:16 +0000834 llvm::SmallVector<Decl*, 64> Decls;
835 if (Source->FindExternalLexicalDecls(this, Decls))
Douglas Gregor2cf26342009-04-09 22:27:44 +0000836 return;
837
838 // There is no longer any lexical storage in this context
839 ExternalLexicalStorage = false;
840
841 if (Decls.empty())
842 return;
843
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000844 // We may have already loaded just the fields of this record, in which case
845 // don't add the decls, just replace the FirstDecl/LastDecl chain.
846 if (const RecordDecl *RD = dyn_cast<RecordDecl>(this))
847 if (RD->LoadedFieldsFromExternalStorage) {
848 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
849 return;
850 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000851
852 // Splice the newly-read declarations into the beginning of the list
853 // of declarations.
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000854 Decl *ExternalFirst, *ExternalLast;
855 llvm::tie(ExternalFirst, ExternalLast) = BuildDeclChain(Decls);
856 ExternalLast->NextDeclInContext = FirstDecl;
857 FirstDecl = ExternalFirst;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000858 if (!LastDecl)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000859 LastDecl = ExternalLast;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000860}
861
John McCall76bd1f32010-06-01 09:23:16 +0000862DeclContext::lookup_result
863ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
864 DeclarationName Name) {
865 ASTContext &Context = DC->getParentASTContext();
866 StoredDeclsMap *Map;
867 if (!(Map = DC->LookupPtr))
868 Map = DC->CreateStoredDeclsMap(Context);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000869
John McCall76bd1f32010-06-01 09:23:16 +0000870 StoredDeclsList &List = (*Map)[Name];
871 assert(List.isNull());
872 (void) List;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000873
John McCall76bd1f32010-06-01 09:23:16 +0000874 return DeclContext::lookup_result();
875}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000876
John McCall76bd1f32010-06-01 09:23:16 +0000877DeclContext::lookup_result
878ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +0000879 DeclarationName Name,
880 llvm::SmallVectorImpl<NamedDecl*> &Decls) {
881 ASTContext &Context = DC->getParentASTContext();;
882
883 StoredDeclsMap *Map;
884 if (!(Map = DC->LookupPtr))
885 Map = DC->CreateStoredDeclsMap(Context);
886
887 StoredDeclsList &List = (*Map)[Name];
888 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
889 if (List.isNull())
890 List.setOnlyValue(Decls[I]);
891 else
892 List.AddSubsequentDecl(Decls[I]);
893 }
894
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000895 return List.getLookupResult();
John McCall76bd1f32010-06-01 09:23:16 +0000896}
897
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000898void ExternalASTSource::MaterializeVisibleDeclsForName(const DeclContext *DC,
899 DeclarationName Name,
900 llvm::SmallVectorImpl<NamedDecl*> &Decls) {
901 assert(DC->LookupPtr);
902 StoredDeclsMap &Map = *DC->LookupPtr;
903
904 // If there's an entry in the table the visible decls for this name have
905 // already been deserialized.
906 if (Map.find(Name) == Map.end()) {
907 StoredDeclsList &List = Map[Name];
908 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
909 if (List.isNull())
910 List.setOnlyValue(Decls[I]);
911 else
912 List.AddSubsequentDecl(Decls[I]);
913 }
914 }
915}
916
Sebastian Redl681d7232010-07-27 00:17:23 +0000917DeclContext::decl_iterator DeclContext::noload_decls_begin() const {
918 return decl_iterator(FirstDecl);
919}
920
921DeclContext::decl_iterator DeclContext::noload_decls_end() const {
922 return decl_iterator();
923}
924
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000925DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000926 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000927 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000928
929 // FIXME: Check whether we need to load some declarations from
930 // external storage.
Mike Stump1eb44332009-09-09 15:08:12 +0000931 return decl_iterator(FirstDecl);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000932}
933
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000934DeclContext::decl_iterator DeclContext::decls_end() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000935 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000936 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000937
Mike Stump1eb44332009-09-09 15:08:12 +0000938 return decl_iterator();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000939}
940
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000941bool DeclContext::decls_empty() const {
Douglas Gregor8038d512009-04-10 17:25:41 +0000942 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000943 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor8038d512009-04-10 17:25:41 +0000944
945 return !FirstDecl;
946}
947
John McCall9f54ad42009-12-10 09:41:52 +0000948void DeclContext::removeDecl(Decl *D) {
949 assert(D->getLexicalDeclContext() == this &&
950 "decl being removed from non-lexical context");
951 assert((D->NextDeclInContext || D == LastDecl) &&
952 "decl is not in decls list");
953
954 // Remove D from the decl chain. This is O(n) but hopefully rare.
955 if (D == FirstDecl) {
956 if (D == LastDecl)
957 FirstDecl = LastDecl = 0;
958 else
959 FirstDecl = D->NextDeclInContext;
960 } else {
961 for (Decl *I = FirstDecl; true; I = I->NextDeclInContext) {
962 assert(I && "decl not found in linked list");
963 if (I->NextDeclInContext == D) {
964 I->NextDeclInContext = D->NextDeclInContext;
965 if (D == LastDecl) LastDecl = I;
966 break;
967 }
968 }
969 }
970
971 // Mark that D is no longer in the decl chain.
972 D->NextDeclInContext = 0;
973
974 // Remove D from the lookup table if necessary.
975 if (isa<NamedDecl>(D)) {
976 NamedDecl *ND = cast<NamedDecl>(D);
977
John McCall0c01d182010-03-24 05:22:00 +0000978 StoredDeclsMap *Map = getPrimaryContext()->LookupPtr;
979 if (!Map) return;
John McCall9f54ad42009-12-10 09:41:52 +0000980
John McCall9f54ad42009-12-10 09:41:52 +0000981 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
982 assert(Pos != Map->end() && "no lookup entry for decl");
983 Pos->second.remove(ND);
984 }
985}
986
John McCall3f9a8a62009-08-11 06:59:38 +0000987void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner7f0be132009-02-20 00:56:18 +0000988 assert(D->getLexicalDeclContext() == this &&
989 "Decl inserted into wrong lexical context");
Mike Stump1eb44332009-09-09 15:08:12 +0000990 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000991 "Decl already inserted into a DeclContext");
992
993 if (FirstDecl) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000994 LastDecl->NextDeclInContext = D;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000995 LastDecl = D;
996 } else {
997 FirstDecl = LastDecl = D;
998 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000999
1000 // Notify a C++ record declaration that we've added a member, so it can
1001 // update it's class-specific state.
1002 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
1003 Record->addedMember(D);
John McCall3f9a8a62009-08-11 06:59:38 +00001004}
1005
1006void DeclContext::addDecl(Decl *D) {
1007 addHiddenDecl(D);
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001008
1009 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001010 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor44b43212008-12-11 16:49:14 +00001011}
1012
Douglas Gregor074149e2009-01-05 19:45:36 +00001013/// buildLookup - Build the lookup data structure with all of the
1014/// declarations in DCtx (and any other contexts linked to it or
1015/// transparent contexts nested within it).
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001016void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregor074149e2009-01-05 19:45:36 +00001017 for (; DCtx; DCtx = DCtx->getNextContext()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001018 for (decl_iterator D = DCtx->decls_begin(),
1019 DEnd = DCtx->decls_end();
Douglas Gregor4f3b8f82009-01-06 07:17:58 +00001020 D != DEnd; ++D) {
John McCall3f9a8a62009-08-11 06:59:38 +00001021 // Insert this declaration into the lookup structure, but only
1022 // if it's semantically in its decl context. During non-lazy
1023 // lookup building, this is implicitly enforced by addDecl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001024 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
John McCall3f9a8a62009-08-11 06:59:38 +00001025 if (D->getDeclContext() == DCtx)
1026 makeDeclVisibleInContextImpl(ND);
Douglas Gregor074149e2009-01-05 19:45:36 +00001027
Ted Kremenekc32b1d82009-11-17 22:58:30 +00001028 // Insert any forward-declared Objective-C interfaces into the lookup
1029 // data structure.
1030 if (ObjCClassDecl *Class = dyn_cast<ObjCClassDecl>(*D))
1031 for (ObjCClassDecl::iterator I = Class->begin(), IEnd = Class->end();
1032 I != IEnd; ++I)
Ted Kremenek321c22f2009-11-18 00:28:11 +00001033 makeDeclVisibleInContextImpl(I->getInterface());
Ted Kremenekc32b1d82009-11-17 22:58:30 +00001034
Sebastian Redl410c4f22010-08-31 20:53:31 +00001035 // If this declaration is itself a transparent declaration context or
1036 // inline namespace, add its members (recursively).
Douglas Gregor074149e2009-01-05 19:45:36 +00001037 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
Sebastian Redl410c4f22010-08-31 20:53:31 +00001038 if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001039 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregor074149e2009-01-05 19:45:36 +00001040 }
1041 }
1042}
1043
Mike Stump1eb44332009-09-09 15:08:12 +00001044DeclContext::lookup_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001045DeclContext::lookup(DeclarationName Name) {
Steve Naroff0701bbb2009-01-08 17:28:14 +00001046 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +00001047 if (PrimaryContext != this)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001048 return PrimaryContext->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +00001049
John McCall76bd1f32010-06-01 09:23:16 +00001050 if (hasExternalVisibleStorage()) {
1051 // Check to see if we've already cached the lookup results.
1052 if (LookupPtr) {
1053 StoredDeclsMap::iterator I = LookupPtr->find(Name);
1054 if (I != LookupPtr->end())
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001055 return I->second.getLookupResult();
John McCall76bd1f32010-06-01 09:23:16 +00001056 }
1057
1058 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1059 return Source->FindExternalVisibleDeclsByName(this, Name);
1060 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001061
Douglas Gregor3fc749d2008-12-23 00:26:44 +00001062 /// If there is no lookup data structure, build one now by walking
Douglas Gregor44b43212008-12-11 16:49:14 +00001063 /// all of the linked DeclContexts (in declaration order!) and
1064 /// inserting their values.
Douglas Gregorc36c5402009-04-09 17:29:08 +00001065 if (!LookupPtr) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001066 buildLookup(this);
Douglas Gregor44b43212008-12-11 16:49:14 +00001067
Douglas Gregorc36c5402009-04-09 17:29:08 +00001068 if (!LookupPtr)
Douglas Gregora5fdd9c2010-05-11 06:18:17 +00001069 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Douglas Gregorc36c5402009-04-09 17:29:08 +00001070 }
Douglas Gregor44b43212008-12-11 16:49:14 +00001071
John McCall0c01d182010-03-24 05:22:00 +00001072 StoredDeclsMap::iterator Pos = LookupPtr->find(Name);
1073 if (Pos == LookupPtr->end())
Douglas Gregora5fdd9c2010-05-11 06:18:17 +00001074 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001075 return Pos->second.getLookupResult();
Douglas Gregor44b43212008-12-11 16:49:14 +00001076}
1077
Mike Stump1eb44332009-09-09 15:08:12 +00001078DeclContext::lookup_const_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001079DeclContext::lookup(DeclarationName Name) const {
1080 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +00001081}
1082
Sebastian Redl7a126a42010-08-31 00:36:30 +00001083DeclContext *DeclContext::getRedeclContext() {
Chris Lattner0cf2b192009-03-27 19:19:59 +00001084 DeclContext *Ctx = this;
Sebastian Redl410c4f22010-08-31 20:53:31 +00001085 // Skip through transparent contexts.
1086 while (Ctx->isTransparentContext())
Douglas Gregorce356072009-01-06 23:51:29 +00001087 Ctx = Ctx->getParent();
1088 return Ctx;
1089}
1090
Douglas Gregor88b70942009-02-25 22:02:03 +00001091DeclContext *DeclContext::getEnclosingNamespaceContext() {
1092 DeclContext *Ctx = this;
1093 // Skip through non-namespace, non-translation-unit contexts.
Sebastian Redl51a8a372010-08-31 00:36:23 +00001094 while (!Ctx->isFileContext())
Douglas Gregor88b70942009-02-25 22:02:03 +00001095 Ctx = Ctx->getParent();
1096 return Ctx->getPrimaryContext();
1097}
1098
Sebastian Redl7a126a42010-08-31 00:36:30 +00001099bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
1100 // For non-file contexts, this is equivalent to Equals.
1101 if (!isFileContext())
1102 return O->Equals(this);
1103
1104 do {
1105 if (O->Equals(this))
1106 return true;
1107
1108 const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O);
1109 if (!NS || !NS->isInline())
1110 break;
1111 O = NS->getParent();
1112 } while (O);
1113
1114 return false;
1115}
1116
John McCallab88d972009-08-31 22:39:49 +00001117void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable) {
Douglas Gregorcc636682009-02-17 23:15:12 +00001118 // FIXME: This feels like a hack. Should DeclarationName support
1119 // template-ids, or is there a better way to keep specializations
1120 // from being visible?
Douglas Gregor9a299e02011-03-04 17:52:15 +00001121 if (isa<ClassTemplateSpecializationDecl>(D) || D->isTemplateParameter())
Douglas Gregorcc636682009-02-17 23:15:12 +00001122 return;
Eli Friedman6bc20132009-12-08 05:40:03 +00001123 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1124 if (FD->isFunctionTemplateSpecialization())
1125 return;
Douglas Gregorcc636682009-02-17 23:15:12 +00001126
Steve Naroff0701bbb2009-01-08 17:28:14 +00001127 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +00001128 if (PrimaryContext != this) {
John McCallab88d972009-08-31 22:39:49 +00001129 PrimaryContext->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor44b43212008-12-11 16:49:14 +00001130 return;
1131 }
1132
1133 // If we already have a lookup data structure, perform the insertion
Argyrios Kyrtzidis5586b012010-07-04 21:44:25 +00001134 // into it. If we haven't deserialized externally stored decls, deserialize
1135 // them so we can add the decl. Otherwise, be lazy and don't build that
1136 // structure until someone asks for it.
1137 if (LookupPtr || !Recoverable || hasExternalVisibleStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001138 makeDeclVisibleInContextImpl(D);
Douglas Gregor074149e2009-01-05 19:45:36 +00001139
Sebastian Redl410c4f22010-08-31 20:53:31 +00001140 // If we are a transparent context or inline namespace, insert into our
1141 // parent context, too. This operation is recursive.
1142 if (isTransparentContext() || isInlineNamespace())
John McCallab88d972009-08-31 22:39:49 +00001143 getParent()->makeDeclVisibleInContext(D, Recoverable);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00001144
1145 Decl *DCAsDecl = cast<Decl>(this);
1146 // Notify that a decl was made visible unless it's a Tag being defined.
1147 if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined()))
1148 if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
1149 L->AddedVisibleDecl(this, D);
Douglas Gregor44b43212008-12-11 16:49:14 +00001150}
1151
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001152void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) {
Douglas Gregor074149e2009-01-05 19:45:36 +00001153 // Skip unnamed declarations.
1154 if (!D->getDeclName())
1155 return;
1156
Douglas Gregorcc636682009-02-17 23:15:12 +00001157 // FIXME: This feels like a hack. Should DeclarationName support
1158 // template-ids, or is there a better way to keep specializations
1159 // from being visible?
Douglas Gregor9a299e02011-03-04 17:52:15 +00001160 if (isa<ClassTemplateSpecializationDecl>(D) || D->isTemplateParameter())
Douglas Gregorcc636682009-02-17 23:15:12 +00001161 return;
1162
Argyrios Kyrtzidis5586b012010-07-04 21:44:25 +00001163 ASTContext *C = 0;
1164 if (!LookupPtr) {
1165 C = &getParentASTContext();
1166 CreateStoredDeclsMap(*C);
1167 }
1168
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001169 // If there is an external AST source, load any declarations it knows about
1170 // with this declaration's name.
1171 // If the lookup table contains an entry about this name it means that we
1172 // have already checked the external source.
1173 if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
1174 if (hasExternalVisibleStorage() &&
1175 LookupPtr->find(D->getDeclName()) == LookupPtr->end())
1176 Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
1177
Douglas Gregor44b43212008-12-11 16:49:14 +00001178 // Insert this declaration into the map.
John McCall0c01d182010-03-24 05:22:00 +00001179 StoredDeclsList &DeclNameEntries = (*LookupPtr)[D->getDeclName()];
Chris Lattner67762a32009-02-20 01:44:05 +00001180 if (DeclNameEntries.isNull()) {
1181 DeclNameEntries.setOnlyValue(D);
Chris Lattnerbd6c8002009-02-19 07:00:44 +00001182 return;
Douglas Gregor44b43212008-12-11 16:49:14 +00001183 }
Chris Lattner91942502009-02-20 00:55:03 +00001184
Chris Lattnerbdc3d002009-02-20 01:10:07 +00001185 // If it is possible that this is a redeclaration, check to see if there is
1186 // already a decl for which declarationReplaces returns true. If there is
1187 // one, just replace it and return.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001188 if (DeclNameEntries.HandleRedeclaration(D))
Chris Lattner67762a32009-02-20 01:44:05 +00001189 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001190
Chris Lattnerbd6c8002009-02-19 07:00:44 +00001191 // Put this declaration into the appropriate slot.
Chris Lattner67762a32009-02-20 01:44:05 +00001192 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor44b43212008-12-11 16:49:14 +00001193}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001194
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00001195void DeclContext::MaterializeVisibleDeclsFromExternalStorage() {
1196 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1197 assert(hasExternalVisibleStorage() && Source && "No external storage?");
1198
1199 if (!LookupPtr)
1200 CreateStoredDeclsMap(getParentASTContext());
1201 Source->MaterializeVisibleDecls(this);
1202}
1203
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001204/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
1205/// this context.
Mike Stump1eb44332009-09-09 15:08:12 +00001206DeclContext::udir_iterator_range
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001207DeclContext::getUsingDirectives() const {
1208 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001209 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
1210 reinterpret_cast<udir_iterator>(Result.second));
1211}
Douglas Gregor2cf26342009-04-09 22:27:44 +00001212
Ted Kremenek3478eb62010-02-11 07:12:28 +00001213//===----------------------------------------------------------------------===//
1214// Creation and Destruction of StoredDeclsMaps. //
1215//===----------------------------------------------------------------------===//
1216
John McCall0c01d182010-03-24 05:22:00 +00001217StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
1218 assert(!LookupPtr && "context already has a decls map");
1219 assert(getPrimaryContext() == this &&
1220 "creating decls map on non-primary context");
1221
1222 StoredDeclsMap *M;
1223 bool Dependent = isDependentContext();
1224 if (Dependent)
1225 M = new DependentStoredDeclsMap();
1226 else
1227 M = new StoredDeclsMap();
1228 M->Previous = C.LastSDM;
1229 C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
1230 LookupPtr = M;
Ted Kremenek3478eb62010-02-11 07:12:28 +00001231 return M;
1232}
1233
1234void ASTContext::ReleaseDeclContextMaps() {
John McCall0c01d182010-03-24 05:22:00 +00001235 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1236 // pointer because the subclass doesn't add anything that needs to
1237 // be deleted.
John McCall0c01d182010-03-24 05:22:00 +00001238 StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1239}
1240
1241void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1242 while (Map) {
1243 // Advance the iteration before we invalidate memory.
1244 llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1245
1246 if (Dependent)
1247 delete static_cast<DependentStoredDeclsMap*>(Map);
1248 else
1249 delete Map;
1250
1251 Map = Next.getPointer();
1252 Dependent = Next.getInt();
1253 }
1254}
1255
John McCall0c01d182010-03-24 05:22:00 +00001256DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1257 DeclContext *Parent,
1258 const PartialDiagnostic &PDiag) {
1259 assert(Parent->isDependentContext()
1260 && "cannot iterate dependent diagnostics of non-dependent context");
1261 Parent = Parent->getPrimaryContext();
1262 if (!Parent->LookupPtr)
1263 Parent->CreateStoredDeclsMap(C);
1264
1265 DependentStoredDeclsMap *Map
1266 = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr);
1267
Douglas Gregorb8365182010-03-29 23:56:53 +00001268 // Allocate the copy of the PartialDiagnostic via the ASTContext's
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00001269 // BumpPtrAllocator, rather than the ASTContext itself.
Douglas Gregorb8365182010-03-29 23:56:53 +00001270 PartialDiagnostic::Storage *DiagStorage = 0;
1271 if (PDiag.hasStorage())
1272 DiagStorage = new (C) PartialDiagnostic::Storage;
1273
1274 DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
John McCall0c01d182010-03-24 05:22:00 +00001275
1276 // TODO: Maybe we shouldn't reverse the order during insertion.
1277 DD->NextDiagnostic = Map->FirstDiagnostic;
1278 Map->FirstDiagnostic = DD;
1279
1280 return DD;
Ted Kremenek3478eb62010-02-11 07:12:28 +00001281}