blob: a507215aa8439007d88e93457f00665597658619 [file] [log] [blame]
Eli Friedman56d29372008-06-07 16:52:53 +00001//===--- DeclBase.cpp - Declaration AST Node Implementation ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Decl and DeclContext classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclBase.h"
Douglas Gregor64650af2009-02-02 23:39:07 +000015#include "clang/AST/Decl.h"
Douglas Gregorc2ee10d2009-04-07 17:20:56 +000016#include "clang/AST/DeclContextInternals.h"
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +000017#include "clang/AST/DeclCXX.h"
John McCall92b7f702010-03-11 07:50:04 +000018#include "clang/AST/DeclFriend.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000019#include "clang/AST/DeclObjC.h"
20#include "clang/AST/DeclTemplate.h"
John McCall0c01d182010-03-24 05:22:00 +000021#include "clang/AST/DependentDiagnostic.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000022#include "clang/AST/ExternalASTSource.h"
Eli Friedman56d29372008-06-07 16:52:53 +000023#include "clang/AST/ASTContext.h"
Douglas Gregor44b43212008-12-11 16:49:14 +000024#include "clang/AST/Type.h"
Sebastian Redld3a413d2009-04-26 20:35:05 +000025#include "clang/AST/Stmt.h"
26#include "clang/AST/StmtCXX.h"
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +000027#include "clang/AST/ASTMutationListener.h"
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000028#include "clang/Basic/TargetInfo.h"
Eli Friedman56d29372008-06-07 16:52:53 +000029#include "llvm/ADT/DenseMap.h"
Chris Lattner49f28ca2009-03-05 08:00:35 +000030#include "llvm/Support/raw_ostream.h"
Douglas Gregor6ed40e32008-12-23 21:05:05 +000031#include <algorithm>
Eli Friedman56d29372008-06-07 16:52:53 +000032using namespace clang;
33
34//===----------------------------------------------------------------------===//
35// Statistics
36//===----------------------------------------------------------------------===//
37
Sean Hunt9a555912010-05-30 07:21:58 +000038#define DECL(DERIVED, BASE) static int n##DERIVED##s = 0;
39#define ABSTRACT_DECL(DECL)
40#include "clang/AST/DeclNodes.inc"
Eli Friedman56d29372008-06-07 16:52:53 +000041
42static bool StatSwitch = false;
43
Douglas Gregor1e68ecc2012-01-05 21:55:30 +000044void *Decl::AllocateDeserializedDecl(const ASTContext &Context,
45 unsigned ID,
46 unsigned Size) {
Douglas Gregor5d1f4962012-01-05 23:49:36 +000047 // Allocate an extra 8 bytes worth of storage, which ensures that the
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +000048 // resulting pointer will still be 8-byte aligned.
Douglas Gregor5d1f4962012-01-05 23:49:36 +000049 void *Start = Context.Allocate(Size + 8);
50 void *Result = (char*)Start + 8;
Douglas Gregorb6b60c12012-01-05 22:27:05 +000051
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +000052 unsigned *PrefixPtr = (unsigned *)Result - 2;
53
54 // Zero out the first 4 bytes; this is used to store the owning module ID.
55 PrefixPtr[0] = 0;
56
57 // Store the global declaration ID in the second 4 bytes.
58 PrefixPtr[1] = ID;
Douglas Gregorb6b60c12012-01-05 22:27:05 +000059
60 return Result;
Douglas Gregor1e68ecc2012-01-05 21:55:30 +000061}
62
Eli Friedman56d29372008-06-07 16:52:53 +000063const char *Decl::getDeclKindName() const {
64 switch (DeclKind) {
David Blaikieb219cfc2011-09-23 05:06:16 +000065 default: llvm_unreachable("Declaration not in DeclNodes.inc!");
Sean Hunt9a555912010-05-30 07:21:58 +000066#define DECL(DERIVED, BASE) case DERIVED: return #DERIVED;
67#define ABSTRACT_DECL(DECL)
68#include "clang/AST/DeclNodes.inc"
Eli Friedman56d29372008-06-07 16:52:53 +000069 }
70}
71
Douglas Gregor42738572010-03-05 00:26:45 +000072void Decl::setInvalidDecl(bool Invalid) {
73 InvalidDecl = Invalid;
74 if (Invalid) {
75 // Defensive maneuver for ill-formed code: we're likely not to make it to
76 // a point where we set the access specifier, so default it to "public"
77 // to avoid triggering asserts elsewhere in the front end.
78 setAccess(AS_public);
79 }
80}
81
Steve Naroff0a473932009-01-20 19:53:53 +000082const char *DeclContext::getDeclKindName() const {
83 switch (DeclKind) {
David Blaikieb219cfc2011-09-23 05:06:16 +000084 default: llvm_unreachable("Declaration context not in DeclNodes.inc!");
Sean Hunt9a555912010-05-30 07:21:58 +000085#define DECL(DERIVED, BASE) case Decl::DERIVED: return #DERIVED;
86#define ABSTRACT_DECL(DECL)
87#include "clang/AST/DeclNodes.inc"
Steve Naroff0a473932009-01-20 19:53:53 +000088 }
89}
90
Eli Friedman56d29372008-06-07 16:52:53 +000091bool Decl::CollectingStats(bool Enable) {
Kovarththanan Rajaratnam2024f4d2009-11-29 14:54:35 +000092 if (Enable) StatSwitch = true;
Eli Friedman56d29372008-06-07 16:52:53 +000093 return StatSwitch;
94}
95
96void Decl::PrintStats() {
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000097 llvm::errs() << "\n*** Decl Stats:\n";
Mike Stump1eb44332009-09-09 15:08:12 +000098
Douglas Gregor64650af2009-02-02 23:39:07 +000099 int totalDecls = 0;
Sean Hunt9a555912010-05-30 07:21:58 +0000100#define DECL(DERIVED, BASE) totalDecls += n##DERIVED##s;
101#define ABSTRACT_DECL(DECL)
102#include "clang/AST/DeclNodes.inc"
Chandler Carruthb43c8ec2011-07-04 06:13:27 +0000103 llvm::errs() << " " << totalDecls << " decls total.\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Douglas Gregor64650af2009-02-02 23:39:07 +0000105 int totalBytes = 0;
Sean Hunt9a555912010-05-30 07:21:58 +0000106#define DECL(DERIVED, BASE) \
107 if (n##DERIVED##s > 0) { \
108 totalBytes += (int)(n##DERIVED##s * sizeof(DERIVED##Decl)); \
Chandler Carruthb43c8ec2011-07-04 06:13:27 +0000109 llvm::errs() << " " << n##DERIVED##s << " " #DERIVED " decls, " \
110 << sizeof(DERIVED##Decl) << " each (" \
111 << n##DERIVED##s * sizeof(DERIVED##Decl) \
112 << " bytes)\n"; \
Douglas Gregor64650af2009-02-02 23:39:07 +0000113 }
Sean Hunt9a555912010-05-30 07:21:58 +0000114#define ABSTRACT_DECL(DECL)
115#include "clang/AST/DeclNodes.inc"
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Chandler Carruthb43c8ec2011-07-04 06:13:27 +0000117 llvm::errs() << "Total bytes = " << totalBytes << "\n";
Eli Friedman56d29372008-06-07 16:52:53 +0000118}
119
Sean Hunt9a555912010-05-30 07:21:58 +0000120void Decl::add(Kind k) {
Eli Friedman56d29372008-06-07 16:52:53 +0000121 switch (k) {
Sean Hunt9a555912010-05-30 07:21:58 +0000122#define DECL(DERIVED, BASE) case DERIVED: ++n##DERIVED##s; break;
123#define ABSTRACT_DECL(DECL)
124#include "clang/AST/DeclNodes.inc"
Eli Friedman56d29372008-06-07 16:52:53 +0000125 }
126}
127
Anders Carlsson67e33202009-06-13 00:08:58 +0000128bool Decl::isTemplateParameterPack() const {
129 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
130 return TTP->isParameterPack();
Douglas Gregor10738d32010-12-23 23:51:58 +0000131 if (const NonTypeTemplateParmDecl *NTTP
Douglas Gregor61c4d282011-01-05 15:48:55 +0000132 = dyn_cast<NonTypeTemplateParmDecl>(this))
Douglas Gregor10738d32010-12-23 23:51:58 +0000133 return NTTP->isParameterPack();
Douglas Gregor61c4d282011-01-05 15:48:55 +0000134 if (const TemplateTemplateParmDecl *TTP
135 = dyn_cast<TemplateTemplateParmDecl>(this))
136 return TTP->isParameterPack();
Anders Carlsson67e33202009-06-13 00:08:58 +0000137 return false;
138}
139
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000140bool Decl::isParameterPack() const {
141 if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(this))
142 return Parm->isParameterPack();
143
144 return isTemplateParameterPack();
145}
146
Douglas Gregore53060f2009-06-25 22:08:12 +0000147bool Decl::isFunctionOrFunctionTemplate() const {
John McCall9488ea12009-11-17 05:59:44 +0000148 if (const UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(this))
Anders Carlsson58badb72009-06-26 05:26:50 +0000149 return UD->getTargetDecl()->isFunctionOrFunctionTemplate();
Mike Stump1eb44332009-09-09 15:08:12 +0000150
Douglas Gregore53060f2009-06-25 22:08:12 +0000151 return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this);
152}
153
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000154bool Decl::isTemplateDecl() const {
155 return isa<TemplateDecl>(this);
156}
157
Argyrios Kyrtzidisc8680f42011-09-28 02:45:33 +0000158const DeclContext *Decl::getParentFunctionOrMethod() const {
159 for (const DeclContext *DC = getDeclContext();
160 DC && !DC->isTranslationUnit() && !DC->isNamespace();
Douglas Gregor79c22782010-01-16 20:21:20 +0000161 DC = DC->getParent())
162 if (DC->isFunctionOrMethod())
Argyrios Kyrtzidisc8680f42011-09-28 02:45:33 +0000163 return DC;
Douglas Gregor79c22782010-01-16 20:21:20 +0000164
Argyrios Kyrtzidisc8680f42011-09-28 02:45:33 +0000165 return 0;
Douglas Gregor79c22782010-01-16 20:21:20 +0000166}
167
Douglas Gregor4c3e0ee2011-02-17 08:47:29 +0000168
Eli Friedman56d29372008-06-07 16:52:53 +0000169//===----------------------------------------------------------------------===//
Chris Lattner49f28ca2009-03-05 08:00:35 +0000170// PrettyStackTraceDecl Implementation
171//===----------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Chris Lattner5f9e2722011-07-23 10:55:15 +0000173void PrettyStackTraceDecl::print(raw_ostream &OS) const {
Chris Lattner49f28ca2009-03-05 08:00:35 +0000174 SourceLocation TheLoc = Loc;
175 if (TheLoc.isInvalid() && TheDecl)
176 TheLoc = TheDecl->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000177
Chris Lattner49f28ca2009-03-05 08:00:35 +0000178 if (TheLoc.isValid()) {
179 TheLoc.print(OS, SM);
180 OS << ": ";
181 }
182
183 OS << Message;
184
Daniel Dunbarc5236562009-11-21 09:05:59 +0000185 if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
Chris Lattner49f28ca2009-03-05 08:00:35 +0000186 OS << " '" << DN->getQualifiedNameAsString() << '\'';
187 OS << '\n';
188}
Mike Stump1eb44332009-09-09 15:08:12 +0000189
Chris Lattner49f28ca2009-03-05 08:00:35 +0000190//===----------------------------------------------------------------------===//
Eli Friedman56d29372008-06-07 16:52:53 +0000191// Decl Implementation
192//===----------------------------------------------------------------------===//
193
Douglas Gregorda2142f2011-02-19 18:51:44 +0000194// Out-of-line virtual method providing a home for Decl.
195Decl::~Decl() { }
Douglas Gregorf4a03cc2011-02-17 07:02:32 +0000196
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000197void Decl::setDeclContext(DeclContext *DC) {
Chris Lattneree219fd2009-03-29 06:06:59 +0000198 DeclCtx = DC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000199}
200
201void Decl::setLexicalDeclContext(DeclContext *DC) {
202 if (DC == getLexicalDeclContext())
203 return;
204
205 if (isInSemaDC()) {
Argyrios Kyrtzidis4bbb8502012-02-09 02:44:08 +0000206 setDeclContextsImpl(getDeclContext(), DC, getASTContext());
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000207 } else {
208 getMultipleDC()->LexicalDC = DC;
209 }
210}
211
Argyrios Kyrtzidis4bbb8502012-02-09 02:44:08 +0000212void Decl::setDeclContextsImpl(DeclContext *SemaDC, DeclContext *LexicalDC,
213 ASTContext &Ctx) {
214 if (SemaDC == LexicalDC) {
215 DeclCtx = SemaDC;
216 } else {
217 Decl::MultipleDC *MDC = new (Ctx) Decl::MultipleDC();
218 MDC->SemanticDC = SemaDC;
219 MDC->LexicalDC = LexicalDC;
220 DeclCtx = MDC;
221 }
222}
223
John McCall9aeed322009-10-01 00:25:31 +0000224bool Decl::isInAnonymousNamespace() const {
225 const DeclContext *DC = getDeclContext();
226 do {
227 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
228 if (ND->isAnonymousNamespace())
229 return true;
230 } while ((DC = DC->getParent()));
231
232 return false;
233}
234
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000235TranslationUnitDecl *Decl::getTranslationUnitDecl() {
Argyrios Kyrtzidis9b346692009-06-30 02:34:53 +0000236 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
237 return TUD;
238
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000239 DeclContext *DC = getDeclContext();
240 assert(DC && "This decl is not contained in a translation unit!");
Mike Stump1eb44332009-09-09 15:08:12 +0000241
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000242 while (!DC->isTranslationUnit()) {
243 DC = DC->getParent();
244 assert(DC && "This decl is not contained in a translation unit!");
245 }
Mike Stump1eb44332009-09-09 15:08:12 +0000246
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000247 return cast<TranslationUnitDecl>(DC);
248}
249
250ASTContext &Decl::getASTContext() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000251 return getTranslationUnitDecl()->getASTContext();
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000252}
253
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +0000254ASTMutationListener *Decl::getASTMutationListener() const {
255 return getASTContext().getASTMutationListener();
256}
257
Douglas Gregorc070cc62010-06-17 23:14:26 +0000258bool Decl::isUsed(bool CheckUsedAttr) const {
Tanya Lattner12ead492010-02-17 02:17:21 +0000259 if (Used)
260 return true;
261
262 // Check for used attribute.
Douglas Gregorc070cc62010-06-17 23:14:26 +0000263 if (CheckUsedAttr && hasAttr<UsedAttr>())
Tanya Lattner12ead492010-02-17 02:17:21 +0000264 return true;
265
266 // Check redeclarations for used attribute.
267 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Douglas Gregorc070cc62010-06-17 23:14:26 +0000268 if ((CheckUsedAttr && I->hasAttr<UsedAttr>()) || I->Used)
Tanya Lattner12ead492010-02-17 02:17:21 +0000269 return true;
270 }
271
272 return false;
273}
274
Argyrios Kyrtzidis6b6b42a2011-04-19 19:51:10 +0000275bool Decl::isReferenced() const {
276 if (Referenced)
277 return true;
278
279 // Check redeclarations.
280 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
281 if (I->Referenced)
282 return true;
283
284 return false;
285}
286
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000287/// \brief Determine the availability of the given declaration based on
288/// the target platform.
289///
290/// When it returns an availability result other than \c AR_Available,
291/// if the \p Message parameter is non-NULL, it will be set to a
292/// string describing why the entity is unavailable.
293///
294/// FIXME: Make these strings localizable, since they end up in
295/// diagnostics.
296static AvailabilityResult CheckAvailability(ASTContext &Context,
297 const AvailabilityAttr *A,
298 std::string *Message) {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000299 StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000300 StringRef PrettyPlatformName
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000301 = AvailabilityAttr::getPrettyPlatformName(TargetPlatform);
302 if (PrettyPlatformName.empty())
303 PrettyPlatformName = TargetPlatform;
304
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000305 VersionTuple TargetMinVersion = Context.getTargetInfo().getPlatformMinVersion();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000306 if (TargetMinVersion.empty())
307 return AR_Available;
308
309 // Match the platform name.
310 if (A->getPlatform()->getName() != TargetPlatform)
311 return AR_Available;
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000312
313 std::string HintMessage;
314 if (!A->getMessage().empty()) {
315 HintMessage = " - ";
316 HintMessage += A->getMessage();
317 }
318
Douglas Gregorb53e4172011-03-26 03:35:55 +0000319 // Make sure that this declaration has not been marked 'unavailable'.
320 if (A->getUnavailable()) {
321 if (Message) {
322 Message->clear();
323 llvm::raw_string_ostream Out(*Message);
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000324 Out << "not available on " << PrettyPlatformName
325 << HintMessage;
Douglas Gregorb53e4172011-03-26 03:35:55 +0000326 }
327
328 return AR_Unavailable;
329 }
330
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000331 // Make sure that this declaration has already been introduced.
332 if (!A->getIntroduced().empty() &&
333 TargetMinVersion < A->getIntroduced()) {
334 if (Message) {
335 Message->clear();
336 llvm::raw_string_ostream Out(*Message);
337 Out << "introduced in " << PrettyPlatformName << ' '
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000338 << A->getIntroduced() << HintMessage;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000339 }
340
341 return AR_NotYetIntroduced;
342 }
343
344 // Make sure that this declaration hasn't been obsoleted.
345 if (!A->getObsoleted().empty() && TargetMinVersion >= A->getObsoleted()) {
346 if (Message) {
347 Message->clear();
348 llvm::raw_string_ostream Out(*Message);
349 Out << "obsoleted in " << PrettyPlatformName << ' '
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000350 << A->getObsoleted() << HintMessage;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000351 }
352
353 return AR_Unavailable;
354 }
355
356 // Make sure that this declaration hasn't been deprecated.
357 if (!A->getDeprecated().empty() && TargetMinVersion >= A->getDeprecated()) {
358 if (Message) {
359 Message->clear();
360 llvm::raw_string_ostream Out(*Message);
361 Out << "first deprecated in " << PrettyPlatformName << ' '
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000362 << A->getDeprecated() << HintMessage;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000363 }
364
365 return AR_Deprecated;
366 }
367
368 return AR_Available;
369}
370
371AvailabilityResult Decl::getAvailability(std::string *Message) const {
372 AvailabilityResult Result = AR_Available;
373 std::string ResultMessage;
374
375 for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
376 if (DeprecatedAttr *Deprecated = dyn_cast<DeprecatedAttr>(*A)) {
377 if (Result >= AR_Deprecated)
378 continue;
379
380 if (Message)
381 ResultMessage = Deprecated->getMessage();
382
383 Result = AR_Deprecated;
384 continue;
385 }
386
387 if (UnavailableAttr *Unavailable = dyn_cast<UnavailableAttr>(*A)) {
388 if (Message)
389 *Message = Unavailable->getMessage();
390 return AR_Unavailable;
391 }
392
393 if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
394 AvailabilityResult AR = CheckAvailability(getASTContext(), Availability,
395 Message);
396
397 if (AR == AR_Unavailable)
398 return AR_Unavailable;
399
400 if (AR > Result) {
401 Result = AR;
402 if (Message)
403 ResultMessage.swap(*Message);
404 }
405 continue;
406 }
407 }
408
409 if (Message)
410 Message->swap(ResultMessage);
411 return Result;
412}
413
414bool Decl::canBeWeakImported(bool &IsDefinition) const {
415 IsDefinition = false;
416 if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
417 if (!Var->hasExternalStorage() || Var->getInit()) {
418 IsDefinition = true;
419 return false;
420 }
421 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
422 if (FD->hasBody()) {
423 IsDefinition = true;
424 return false;
425 }
426 } else if (isa<ObjCPropertyDecl>(this) || isa<ObjCMethodDecl>(this))
427 return false;
428 else if (!(getASTContext().getLangOptions().ObjCNonFragileABI &&
429 isa<ObjCInterfaceDecl>(this)))
430 return false;
431
432 return true;
433}
434
435bool Decl::isWeakImported() const {
436 bool IsDefinition;
437 if (!canBeWeakImported(IsDefinition))
438 return false;
439
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000440 for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
441 if (isa<WeakImportAttr>(*A))
442 return true;
443
444 if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
445 if (CheckAvailability(getASTContext(), Availability, 0)
446 == AR_NotYetIntroduced)
447 return true;
448 }
449 }
450
451 return false;
452}
Tanya Lattner12ead492010-02-17 02:17:21 +0000453
Chris Lattner769dbdf2009-03-27 20:18:19 +0000454unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
455 switch (DeclKind) {
John McCall9488ea12009-11-17 05:59:44 +0000456 case Function:
457 case CXXMethod:
458 case CXXConstructor:
459 case CXXDestructor:
460 case CXXConversion:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000461 case EnumConstant:
462 case Var:
463 case ImplicitParam:
464 case ParmVar:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000465 case NonTypeTemplateParm:
466 case ObjCMethod:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000467 case ObjCProperty:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000468 return IDNS_Ordinary;
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000469 case Label:
470 return IDNS_Label;
Francois Pichet87c2e122010-11-21 06:08:52 +0000471 case IndirectField:
472 return IDNS_Ordinary | IDNS_Member;
473
John McCall0d6b1642010-04-23 18:46:30 +0000474 case ObjCCompatibleAlias:
475 case ObjCInterface:
476 return IDNS_Ordinary | IDNS_Type;
477
478 case Typedef:
Richard Smith162e1c12011-04-15 14:24:37 +0000479 case TypeAlias:
Richard Smith3e4c6c42011-05-05 21:57:07 +0000480 case TypeAliasTemplate:
John McCall0d6b1642010-04-23 18:46:30 +0000481 case UnresolvedUsingTypename:
482 case TemplateTypeParm:
483 return IDNS_Ordinary | IDNS_Type;
484
John McCall9488ea12009-11-17 05:59:44 +0000485 case UsingShadow:
486 return 0; // we'll actually overwrite this later
487
John McCall7ba107a2009-11-18 02:36:19 +0000488 case UnresolvedUsingValue:
John McCall7ba107a2009-11-18 02:36:19 +0000489 return IDNS_Ordinary | IDNS_Using;
John McCall9488ea12009-11-17 05:59:44 +0000490
491 case Using:
492 return IDNS_Using;
493
Chris Lattner769dbdf2009-03-27 20:18:19 +0000494 case ObjCProtocol:
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000495 return IDNS_ObjCProtocol;
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Chris Lattner769dbdf2009-03-27 20:18:19 +0000497 case Field:
498 case ObjCAtDefsField:
499 case ObjCIvar:
500 return IDNS_Member;
Mike Stump1eb44332009-09-09 15:08:12 +0000501
Chris Lattner769dbdf2009-03-27 20:18:19 +0000502 case Record:
503 case CXXRecord:
504 case Enum:
John McCall0d6b1642010-04-23 18:46:30 +0000505 return IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000506
Chris Lattner769dbdf2009-03-27 20:18:19 +0000507 case Namespace:
John McCall0d6b1642010-04-23 18:46:30 +0000508 case NamespaceAlias:
509 return IDNS_Namespace;
510
Chris Lattner769dbdf2009-03-27 20:18:19 +0000511 case FunctionTemplate:
John McCall0d6b1642010-04-23 18:46:30 +0000512 return IDNS_Ordinary;
513
Chris Lattner769dbdf2009-03-27 20:18:19 +0000514 case ClassTemplate:
515 case TemplateTemplateParm:
John McCall0d6b1642010-04-23 18:46:30 +0000516 return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000517
Chris Lattner769dbdf2009-03-27 20:18:19 +0000518 // Never have names.
John McCall02cace72009-08-28 07:59:38 +0000519 case Friend:
John McCalldd4a3b02009-09-16 22:47:08 +0000520 case FriendTemplate:
Abramo Bagnara6206d532010-06-05 05:09:32 +0000521 case AccessSpec:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000522 case LinkageSpec:
523 case FileScopeAsm:
524 case StaticAssert:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000525 case ObjCPropertyImpl:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000526 case Block:
527 case TranslationUnit:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000528
Chris Lattner769dbdf2009-03-27 20:18:19 +0000529 case UsingDirective:
530 case ClassTemplateSpecialization:
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000531 case ClassTemplatePartialSpecialization:
Francois Pichetaf0f4d02011-08-14 03:52:19 +0000532 case ClassScopeFunctionSpecialization:
Douglas Gregorbd4187b2010-04-22 23:19:50 +0000533 case ObjCImplementation:
534 case ObjCCategory:
535 case ObjCCategoryImpl:
Douglas Gregor15de72c2011-12-02 23:23:56 +0000536 case Import:
Douglas Gregorbd4187b2010-04-22 23:19:50 +0000537 // Never looked up by name.
Chris Lattner769dbdf2009-03-27 20:18:19 +0000538 return 0;
539 }
John McCall9488ea12009-11-17 05:59:44 +0000540
David Blaikie30263482012-01-20 21:50:17 +0000541 llvm_unreachable("Invalid DeclKind!");
Eli Friedman56d29372008-06-07 16:52:53 +0000542}
543
Argyrios Kyrtzidis4bbb8502012-02-09 02:44:08 +0000544void Decl::setAttrsImpl(const AttrVec &attrs, ASTContext &Ctx) {
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000545 assert(!HasAttrs && "Decl already contains attrs.");
546
Argyrios Kyrtzidis4bbb8502012-02-09 02:44:08 +0000547 AttrVec &AttrBlank = Ctx.getDeclAttrs(this);
Sean Huntcf807c42010-08-18 23:23:40 +0000548 assert(AttrBlank.empty() && "HasAttrs was wrong?");
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000549
550 AttrBlank = attrs;
551 HasAttrs = true;
552}
553
Sean Huntcf807c42010-08-18 23:23:40 +0000554void Decl::dropAttrs() {
Eli Friedman56d29372008-06-07 16:52:53 +0000555 if (!HasAttrs) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000556
Eli Friedman56d29372008-06-07 16:52:53 +0000557 HasAttrs = false;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000558 getASTContext().eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000559}
560
Sean Huntcf807c42010-08-18 23:23:40 +0000561const AttrVec &Decl::getAttrs() const {
562 assert(HasAttrs && "No attrs to get!");
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000563 return getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000564}
565
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000566void Decl::swapAttrs(Decl *RHS) {
Eli Friedman56d29372008-06-07 16:52:53 +0000567 bool HasLHSAttr = this->HasAttrs;
568 bool HasRHSAttr = RHS->HasAttrs;
Mike Stump1eb44332009-09-09 15:08:12 +0000569
Eli Friedman56d29372008-06-07 16:52:53 +0000570 // Usually, neither decl has attrs, nothing to do.
571 if (!HasLHSAttr && !HasRHSAttr) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000572
Eli Friedman56d29372008-06-07 16:52:53 +0000573 // If 'this' has no attrs, swap the other way.
574 if (!HasLHSAttr)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000575 return RHS->swapAttrs(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000576
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000577 ASTContext &Context = getASTContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000578
Eli Friedman56d29372008-06-07 16:52:53 +0000579 // Handle the case when both decls have attrs.
580 if (HasRHSAttr) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000581 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman56d29372008-06-07 16:52:53 +0000582 return;
583 }
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Eli Friedman56d29372008-06-07 16:52:53 +0000585 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor68584ed2009-06-18 16:11:24 +0000586 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
587 Context.eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000588 this->HasAttrs = false;
589 RHS->HasAttrs = true;
590}
591
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000592Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000593 Decl::Kind DK = D->getDeclKind();
594 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000595#define DECL(NAME, BASE)
596#define DECL_CONTEXT(NAME) \
597 case Decl::NAME: \
598 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
599#define DECL_CONTEXT_BASE(NAME)
600#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000601 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000602#define DECL(NAME, BASE)
603#define DECL_CONTEXT_BASE(NAME) \
604 if (DK >= first##NAME && DK <= last##NAME) \
605 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
606#include "clang/AST/DeclNodes.inc"
David Blaikieb219cfc2011-09-23 05:06:16 +0000607 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000608 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000609}
610
611DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000612 Decl::Kind DK = D->getKind();
613 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000614#define DECL(NAME, BASE)
615#define DECL_CONTEXT(NAME) \
616 case Decl::NAME: \
617 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
618#define DECL_CONTEXT_BASE(NAME)
619#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000620 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000621#define DECL(NAME, BASE)
622#define DECL_CONTEXT_BASE(NAME) \
623 if (DK >= first##NAME && DK <= last##NAME) \
624 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
625#include "clang/AST/DeclNodes.inc"
David Blaikieb219cfc2011-09-23 05:06:16 +0000626 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000627 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000628}
629
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000630SourceLocation Decl::getBodyRBrace() const {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000631 // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
632 // FunctionDecl stores EndRangeLoc for this purpose.
633 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
634 const FunctionDecl *Definition;
635 if (FD->hasBody(Definition))
636 return Definition->getSourceRange().getEnd();
637 return SourceLocation();
638 }
639
Argyrios Kyrtzidis6717ef42010-07-07 11:31:27 +0000640 if (Stmt *Body = getBody())
641 return Body->getSourceRange().getEnd();
642
643 return SourceLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +0000644}
645
Anders Carlsson1329c272009-03-25 23:38:06 +0000646void Decl::CheckAccessDeclContext() const {
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000647#ifndef NDEBUG
John McCall46460a62010-01-20 21:53:11 +0000648 // Suppress this check if any of the following hold:
649 // 1. this is the translation unit (and thus has no parent)
650 // 2. this is a template parameter (and thus doesn't belong to its context)
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000651 // 3. this is a non-type template parameter
652 // 4. the context is not a record
653 // 5. it's invalid
654 // 6. it's a C++0x static_assert.
Anders Carlsson35eda442009-08-29 20:47:47 +0000655 if (isa<TranslationUnitDecl>(this) ||
Argyrios Kyrtzidis04aed0e2010-07-02 11:55:44 +0000656 isa<TemplateTypeParmDecl>(this) ||
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000657 isa<NonTypeTemplateParmDecl>(this) ||
Douglas Gregorfdd8ab12010-02-22 17:53:38 +0000658 !isa<CXXRecordDecl>(getDeclContext()) ||
Argyrios Kyrtzidis65b63ec2010-09-08 21:32:35 +0000659 isInvalidDecl() ||
660 isa<StaticAssertDecl>(this) ||
661 // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
662 // as DeclContext (?).
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000663 isa<ParmVarDecl>(this) ||
664 // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
665 // AS_none as access specifier.
Francois Pichetbc845322011-08-17 01:06:54 +0000666 isa<CXXRecordDecl>(this) ||
667 isa<ClassScopeFunctionSpecializationDecl>(this))
Anders Carlsson35eda442009-08-29 20:47:47 +0000668 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000669
670 assert(Access != AS_none &&
Anders Carlsson1329c272009-03-25 23:38:06 +0000671 "Access specifier is AS_none inside a record decl");
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000672#endif
Anders Carlsson1329c272009-03-25 23:38:06 +0000673}
674
John McCallaab9e312011-02-22 22:25:23 +0000675DeclContext *Decl::getNonClosureContext() {
John McCall4b9c2d22011-11-06 09:01:30 +0000676 return getDeclContext()->getNonClosureAncestor();
677}
678
679DeclContext *DeclContext::getNonClosureAncestor() {
680 DeclContext *DC = this;
John McCallaab9e312011-02-22 22:25:23 +0000681
682 // This is basically "while (DC->isClosure()) DC = DC->getParent();"
683 // except that it's significantly more efficient to cast to a known
684 // decl type and call getDeclContext() than to call getParent().
John McCall7b3f8532011-06-23 21:18:31 +0000685 while (isa<BlockDecl>(DC))
686 DC = cast<BlockDecl>(DC)->getDeclContext();
John McCallaab9e312011-02-22 22:25:23 +0000687
688 assert(!DC->isClosure());
689 return DC;
690}
Anders Carlsson1329c272009-03-25 23:38:06 +0000691
Eli Friedman56d29372008-06-07 16:52:53 +0000692//===----------------------------------------------------------------------===//
693// DeclContext Implementation
694//===----------------------------------------------------------------------===//
695
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000696bool DeclContext::classof(const Decl *D) {
697 switch (D->getKind()) {
Sean Hunt9a555912010-05-30 07:21:58 +0000698#define DECL(NAME, BASE)
699#define DECL_CONTEXT(NAME) case Decl::NAME:
700#define DECL_CONTEXT_BASE(NAME)
701#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000702 return true;
703 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000704#define DECL(NAME, BASE)
705#define DECL_CONTEXT_BASE(NAME) \
706 if (D->getKind() >= Decl::first##NAME && \
707 D->getKind() <= Decl::last##NAME) \
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000708 return true;
Sean Hunt9a555912010-05-30 07:21:58 +0000709#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000710 return false;
711 }
712}
713
Douglas Gregora2da7802010-07-25 18:38:02 +0000714DeclContext::~DeclContext() { }
Douglas Gregor44b43212008-12-11 16:49:14 +0000715
Douglas Gregore942bbe2009-09-10 16:57:35 +0000716/// \brief Find the parent context of this context that will be
717/// used for unqualified name lookup.
718///
719/// Generally, the parent lookup context is the semantic context. However, for
720/// a friend function the parent lookup context is the lexical context, which
721/// is the class in which the friend is declared.
722DeclContext *DeclContext::getLookupParent() {
723 // FIXME: Find a better way to identify friends
724 if (isa<FunctionDecl>(this))
Sebastian Redl7a126a42010-08-31 00:36:30 +0000725 if (getParent()->getRedeclContext()->isFileContext() &&
726 getLexicalParent()->getRedeclContext()->isRecord())
Douglas Gregore942bbe2009-09-10 16:57:35 +0000727 return getLexicalParent();
728
729 return getParent();
730}
731
Sebastian Redl410c4f22010-08-31 20:53:31 +0000732bool DeclContext::isInlineNamespace() const {
733 return isNamespace() &&
734 cast<NamespaceDecl>(this)->isInline();
735}
736
Douglas Gregorbc221632009-05-28 16:34:51 +0000737bool DeclContext::isDependentContext() const {
738 if (isFileContext())
739 return false;
740
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000741 if (isa<ClassTemplatePartialSpecializationDecl>(this))
742 return true;
743
Douglas Gregorbc221632009-05-28 16:34:51 +0000744 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
745 if (Record->getDescribedClassTemplate())
746 return true;
747
John McCall0c01d182010-03-24 05:22:00 +0000748 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregorbc221632009-05-28 16:34:51 +0000749 if (Function->getDescribedFunctionTemplate())
750 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000751
John McCall0c01d182010-03-24 05:22:00 +0000752 // Friend function declarations are dependent if their *lexical*
753 // context is dependent.
754 if (cast<Decl>(this)->getFriendObjectKind())
755 return getLexicalParent()->isDependentContext();
756 }
757
Douglas Gregorbc221632009-05-28 16:34:51 +0000758 return getParent() && getParent()->isDependentContext();
759}
760
Douglas Gregor074149e2009-01-05 19:45:36 +0000761bool DeclContext::isTransparentContext() const {
762 if (DeclKind == Decl::Enum)
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000763 return !cast<EnumDecl>(this)->isScoped();
Douglas Gregor074149e2009-01-05 19:45:36 +0000764 else if (DeclKind == Decl::LinkageSpec)
765 return true;
Douglas Gregor074149e2009-01-05 19:45:36 +0000766
767 return false;
768}
769
John McCallac65c622010-10-26 04:59:26 +0000770bool DeclContext::isExternCContext() const {
771 const DeclContext *DC = this;
772 while (DC->DeclKind != Decl::TranslationUnit) {
773 if (DC->DeclKind == Decl::LinkageSpec)
774 return cast<LinkageSpecDecl>(DC)->getLanguage()
775 == LinkageSpecDecl::lang_c;
776 DC = DC->getParent();
777 }
778 return false;
779}
780
Sebastian Redl7a126a42010-08-31 00:36:30 +0000781bool DeclContext::Encloses(const DeclContext *DC) const {
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000782 if (getPrimaryContext() != this)
783 return getPrimaryContext()->Encloses(DC);
Mike Stump1eb44332009-09-09 15:08:12 +0000784
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000785 for (; DC; DC = DC->getParent())
786 if (DC->getPrimaryContext() == this)
787 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000788 return false;
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000789}
790
Steve Naroff0701bbb2009-01-08 17:28:14 +0000791DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000792 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000793 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000794 case Decl::LinkageSpec:
Mike Stump1eb44332009-09-09 15:08:12 +0000795 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000796 // There is only one DeclContext for these entities.
797 return this;
798
799 case Decl::Namespace:
800 // The original namespace is our primary context.
801 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
802
Douglas Gregor44b43212008-12-11 16:49:14 +0000803 case Decl::ObjCMethod:
804 return this;
805
806 case Decl::ObjCInterface:
Douglas Gregor53df7a12011-12-15 18:03:09 +0000807 if (ObjCInterfaceDecl *Def = cast<ObjCInterfaceDecl>(this)->getDefinition())
808 return Def;
809
810 return this;
811
Steve Naroff0701bbb2009-01-08 17:28:14 +0000812 case Decl::ObjCProtocol:
Douglas Gregor1d784b22012-01-01 19:51:50 +0000813 if (ObjCProtocolDecl *Def = cast<ObjCProtocolDecl>(this)->getDefinition())
814 return Def;
815
816 return this;
Douglas Gregor53df7a12011-12-15 18:03:09 +0000817
Steve Naroff0701bbb2009-01-08 17:28:14 +0000818 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000819 return this;
820
Steve Naroff0701bbb2009-01-08 17:28:14 +0000821 case Decl::ObjCImplementation:
822 case Decl::ObjCCategoryImpl:
823 return this;
824
Douglas Gregor44b43212008-12-11 16:49:14 +0000825 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000826 if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000827 // If this is a tag type that has a definition or is currently
828 // being defined, that definition is our primary context.
John McCall3cb0ebd2010-03-10 03:28:59 +0000829 TagDecl *Tag = cast<TagDecl>(this);
830 assert(isa<TagType>(Tag->TypeForDecl) ||
831 isa<InjectedClassNameType>(Tag->TypeForDecl));
832
833 if (TagDecl *Def = Tag->getDefinition())
834 return Def;
835
836 if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) {
837 const TagType *TagTy = cast<TagType>(Tag->TypeForDecl);
838 if (TagTy->isBeingDefined())
839 // FIXME: is it necessarily being defined in the decl
840 // that owns the type?
841 return TagTy->getDecl();
842 }
843
844 return Tag;
Douglas Gregorcc636682009-02-17 23:15:12 +0000845 }
846
Sean Hunt9a555912010-05-30 07:21:58 +0000847 assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
Douglas Gregor44b43212008-12-11 16:49:14 +0000848 "Unknown DeclContext kind");
849 return this;
850 }
851}
852
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +0000853void
854DeclContext::collectAllContexts(llvm::SmallVectorImpl<DeclContext *> &Contexts){
855 Contexts.clear();
856
857 if (DeclKind != Decl::Namespace) {
858 Contexts.push_back(this);
859 return;
Douglas Gregor44b43212008-12-11 16:49:14 +0000860 }
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +0000861
862 NamespaceDecl *Self = static_cast<NamespaceDecl *>(this);
Douglas Gregoref96ee02012-01-14 16:38:05 +0000863 for (NamespaceDecl *N = Self->getMostRecentDecl(); N;
864 N = N->getPreviousDecl())
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +0000865 Contexts.push_back(N);
866
867 std::reverse(Contexts.begin(), Contexts.end());
Douglas Gregor44b43212008-12-11 16:49:14 +0000868}
869
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000870std::pair<Decl *, Decl *>
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +0000871DeclContext::BuildDeclChain(const SmallVectorImpl<Decl*> &Decls,
872 bool FieldsAlreadyLoaded) {
Douglas Gregor46cd2182012-01-06 16:59:53 +0000873 // Build up a chain of declarations via the Decl::NextInContextAndBits field.
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000874 Decl *FirstNewDecl = 0;
875 Decl *PrevDecl = 0;
876 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +0000877 if (FieldsAlreadyLoaded && isa<FieldDecl>(Decls[I]))
878 continue;
879
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000880 Decl *D = Decls[I];
881 if (PrevDecl)
Douglas Gregor46cd2182012-01-06 16:59:53 +0000882 PrevDecl->NextInContextAndBits.setPointer(D);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000883 else
884 FirstNewDecl = D;
885
886 PrevDecl = D;
887 }
888
889 return std::make_pair(FirstNewDecl, PrevDecl);
890}
891
Douglas Gregor2cf26342009-04-09 22:27:44 +0000892/// \brief Load the declarations within this lexical storage from an
893/// external source.
Mike Stump1eb44332009-09-09 15:08:12 +0000894void
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000895DeclContext::LoadLexicalDeclsFromExternalStorage() const {
896 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000897 assert(hasExternalLexicalStorage() && Source && "No external storage?");
898
Argyrios Kyrtzidis0dbbc042010-07-30 10:03:23 +0000899 // Notify that we have a DeclContext that is initializing.
900 ExternalASTSource::Deserializing ADeclContext(Source);
Douglas Gregor9fc18c92011-08-26 21:23:06 +0000901
Douglas Gregorba6ffaf2011-07-15 21:46:17 +0000902 // Load the external declarations, if any.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000903 SmallVector<Decl*, 64> Decls;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000904 ExternalLexicalStorage = false;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +0000905 switch (Source->FindExternalLexicalDecls(this, Decls)) {
906 case ELR_Success:
907 break;
908
909 case ELR_Failure:
910 case ELR_AlreadyLoaded:
911 return;
912 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000913
914 if (Decls.empty())
915 return;
916
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +0000917 // We may have already loaded just the fields of this record, in which case
918 // we need to ignore them.
919 bool FieldsAlreadyLoaded = false;
920 if (const RecordDecl *RD = dyn_cast<RecordDecl>(this))
921 FieldsAlreadyLoaded = RD->LoadedFieldsFromExternalStorage;
922
Douglas Gregor2cf26342009-04-09 22:27:44 +0000923 // Splice the newly-read declarations into the beginning of the list
924 // of declarations.
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000925 Decl *ExternalFirst, *ExternalLast;
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +0000926 llvm::tie(ExternalFirst, ExternalLast) = BuildDeclChain(Decls,
927 FieldsAlreadyLoaded);
Douglas Gregor46cd2182012-01-06 16:59:53 +0000928 ExternalLast->NextInContextAndBits.setPointer(FirstDecl);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000929 FirstDecl = ExternalFirst;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000930 if (!LastDecl)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000931 LastDecl = ExternalLast;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000932}
933
John McCall76bd1f32010-06-01 09:23:16 +0000934DeclContext::lookup_result
935ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
936 DeclarationName Name) {
937 ASTContext &Context = DC->getParentASTContext();
938 StoredDeclsMap *Map;
939 if (!(Map = DC->LookupPtr))
940 Map = DC->CreateStoredDeclsMap(Context);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000941
John McCall76bd1f32010-06-01 09:23:16 +0000942 StoredDeclsList &List = (*Map)[Name];
943 assert(List.isNull());
944 (void) List;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000945
John McCall76bd1f32010-06-01 09:23:16 +0000946 return DeclContext::lookup_result();
947}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000948
John McCall76bd1f32010-06-01 09:23:16 +0000949DeclContext::lookup_result
950ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +0000951 DeclarationName Name,
Argyrios Kyrtzidis45df9c62011-09-09 06:44:14 +0000952 ArrayRef<NamedDecl*> Decls) {
John McCall76bd1f32010-06-01 09:23:16 +0000953 ASTContext &Context = DC->getParentASTContext();;
954
955 StoredDeclsMap *Map;
956 if (!(Map = DC->LookupPtr))
957 Map = DC->CreateStoredDeclsMap(Context);
958
959 StoredDeclsList &List = (*Map)[Name];
Argyrios Kyrtzidis45df9c62011-09-09 06:44:14 +0000960 for (ArrayRef<NamedDecl*>::iterator
961 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
John McCall76bd1f32010-06-01 09:23:16 +0000962 if (List.isNull())
Argyrios Kyrtzidis45df9c62011-09-09 06:44:14 +0000963 List.setOnlyValue(*I);
John McCall76bd1f32010-06-01 09:23:16 +0000964 else
Argyrios Kyrtzidis45df9c62011-09-09 06:44:14 +0000965 List.AddSubsequentDecl(*I);
John McCall76bd1f32010-06-01 09:23:16 +0000966 }
967
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000968 return List.getLookupResult();
John McCall76bd1f32010-06-01 09:23:16 +0000969}
970
Sebastian Redl681d7232010-07-27 00:17:23 +0000971DeclContext::decl_iterator DeclContext::noload_decls_begin() const {
972 return decl_iterator(FirstDecl);
973}
974
975DeclContext::decl_iterator DeclContext::noload_decls_end() const {
976 return decl_iterator();
977}
978
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000979DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000980 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000981 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000982
Mike Stump1eb44332009-09-09 15:08:12 +0000983 return decl_iterator(FirstDecl);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000984}
985
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000986DeclContext::decl_iterator DeclContext::decls_end() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000987 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000988 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000989
Mike Stump1eb44332009-09-09 15:08:12 +0000990 return decl_iterator();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000991}
992
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000993bool DeclContext::decls_empty() const {
Douglas Gregor8038d512009-04-10 17:25:41 +0000994 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000995 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor8038d512009-04-10 17:25:41 +0000996
997 return !FirstDecl;
998}
999
John McCall9f54ad42009-12-10 09:41:52 +00001000void DeclContext::removeDecl(Decl *D) {
1001 assert(D->getLexicalDeclContext() == this &&
1002 "decl being removed from non-lexical context");
Douglas Gregor46cd2182012-01-06 16:59:53 +00001003 assert((D->NextInContextAndBits.getPointer() || D == LastDecl) &&
John McCall9f54ad42009-12-10 09:41:52 +00001004 "decl is not in decls list");
1005
1006 // Remove D from the decl chain. This is O(n) but hopefully rare.
1007 if (D == FirstDecl) {
1008 if (D == LastDecl)
1009 FirstDecl = LastDecl = 0;
1010 else
Douglas Gregor46cd2182012-01-06 16:59:53 +00001011 FirstDecl = D->NextInContextAndBits.getPointer();
John McCall9f54ad42009-12-10 09:41:52 +00001012 } else {
Douglas Gregor46cd2182012-01-06 16:59:53 +00001013 for (Decl *I = FirstDecl; true; I = I->NextInContextAndBits.getPointer()) {
John McCall9f54ad42009-12-10 09:41:52 +00001014 assert(I && "decl not found in linked list");
Douglas Gregor46cd2182012-01-06 16:59:53 +00001015 if (I->NextInContextAndBits.getPointer() == D) {
1016 I->NextInContextAndBits.setPointer(D->NextInContextAndBits.getPointer());
John McCall9f54ad42009-12-10 09:41:52 +00001017 if (D == LastDecl) LastDecl = I;
1018 break;
1019 }
1020 }
1021 }
1022
1023 // Mark that D is no longer in the decl chain.
Douglas Gregor46cd2182012-01-06 16:59:53 +00001024 D->NextInContextAndBits.setPointer(0);
John McCall9f54ad42009-12-10 09:41:52 +00001025
1026 // Remove D from the lookup table if necessary.
1027 if (isa<NamedDecl>(D)) {
1028 NamedDecl *ND = cast<NamedDecl>(D);
1029
Axel Naumann02368d02011-08-26 14:06:12 +00001030 // Remove only decls that have a name
1031 if (!ND->getDeclName()) return;
1032
John McCall0c01d182010-03-24 05:22:00 +00001033 StoredDeclsMap *Map = getPrimaryContext()->LookupPtr;
1034 if (!Map) return;
John McCall9f54ad42009-12-10 09:41:52 +00001035
John McCall9f54ad42009-12-10 09:41:52 +00001036 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
1037 assert(Pos != Map->end() && "no lookup entry for decl");
Axel Naumannd9d137e2011-11-08 18:21:06 +00001038 if (Pos->second.getAsVector() || Pos->second.getAsDecl() == ND)
1039 Pos->second.remove(ND);
John McCall9f54ad42009-12-10 09:41:52 +00001040 }
1041}
1042
John McCall3f9a8a62009-08-11 06:59:38 +00001043void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner7f0be132009-02-20 00:56:18 +00001044 assert(D->getLexicalDeclContext() == this &&
1045 "Decl inserted into wrong lexical context");
Mike Stump1eb44332009-09-09 15:08:12 +00001046 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001047 "Decl already inserted into a DeclContext");
1048
1049 if (FirstDecl) {
Douglas Gregor46cd2182012-01-06 16:59:53 +00001050 LastDecl->NextInContextAndBits.setPointer(D);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001051 LastDecl = D;
1052 } else {
1053 FirstDecl = LastDecl = D;
1054 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +00001055
1056 // Notify a C++ record declaration that we've added a member, so it can
1057 // update it's class-specific state.
1058 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
1059 Record->addedMember(D);
Douglas Gregore6649772011-12-03 00:30:27 +00001060
1061 // If this is a newly-created (not de-serialized) import declaration, wire
1062 // it in to the list of local import declarations.
1063 if (!D->isFromASTFile()) {
1064 if (ImportDecl *Import = dyn_cast<ImportDecl>(D))
1065 D->getASTContext().addedLocalImportDecl(Import);
1066 }
John McCall3f9a8a62009-08-11 06:59:38 +00001067}
1068
1069void DeclContext::addDecl(Decl *D) {
1070 addHiddenDecl(D);
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001071
1072 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001073 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor44b43212008-12-11 16:49:14 +00001074}
1075
Sean Callanan9faf8102011-10-21 02:57:43 +00001076void DeclContext::addDeclInternal(Decl *D) {
1077 addHiddenDecl(D);
1078
1079 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1080 ND->getDeclContext()->makeDeclVisibleInContextInternal(ND);
1081}
1082
Douglas Gregor074149e2009-01-05 19:45:36 +00001083/// buildLookup - Build the lookup data structure with all of the
1084/// declarations in DCtx (and any other contexts linked to it or
1085/// transparent contexts nested within it).
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001086void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001087 llvm::SmallVector<DeclContext *, 2> Contexts;
1088 DCtx->collectAllContexts(Contexts);
1089 for (unsigned I = 0, N = Contexts.size(); I != N; ++I) {
1090 for (decl_iterator D = Contexts[I]->decls_begin(),
1091 DEnd = Contexts[I]->decls_end();
Douglas Gregor4f3b8f82009-01-06 07:17:58 +00001092 D != DEnd; ++D) {
John McCall3f9a8a62009-08-11 06:59:38 +00001093 // Insert this declaration into the lookup structure, but only
1094 // if it's semantically in its decl context. During non-lazy
1095 // lookup building, this is implicitly enforced by addDecl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001096 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001097 if (D->getDeclContext() == Contexts[I])
Sean Callanan9faf8102011-10-21 02:57:43 +00001098 makeDeclVisibleInContextImpl(ND, false);
Douglas Gregor074149e2009-01-05 19:45:36 +00001099
Sebastian Redl410c4f22010-08-31 20:53:31 +00001100 // If this declaration is itself a transparent declaration context or
1101 // inline namespace, add its members (recursively).
Douglas Gregor074149e2009-01-05 19:45:36 +00001102 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
Sebastian Redl410c4f22010-08-31 20:53:31 +00001103 if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001104 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregor074149e2009-01-05 19:45:36 +00001105 }
1106 }
1107}
1108
Mike Stump1eb44332009-09-09 15:08:12 +00001109DeclContext::lookup_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001110DeclContext::lookup(DeclarationName Name) {
Steve Naroff0701bbb2009-01-08 17:28:14 +00001111 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +00001112 if (PrimaryContext != this)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001113 return PrimaryContext->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +00001114
John McCall76bd1f32010-06-01 09:23:16 +00001115 if (hasExternalVisibleStorage()) {
1116 // Check to see if we've already cached the lookup results.
1117 if (LookupPtr) {
1118 StoredDeclsMap::iterator I = LookupPtr->find(Name);
1119 if (I != LookupPtr->end())
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001120 return I->second.getLookupResult();
John McCall76bd1f32010-06-01 09:23:16 +00001121 }
1122
1123 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1124 return Source->FindExternalVisibleDeclsByName(this, Name);
1125 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001126
Douglas Gregor3fc749d2008-12-23 00:26:44 +00001127 /// If there is no lookup data structure, build one now by walking
Douglas Gregor44b43212008-12-11 16:49:14 +00001128 /// all of the linked DeclContexts (in declaration order!) and
1129 /// inserting their values.
Douglas Gregorc36c5402009-04-09 17:29:08 +00001130 if (!LookupPtr) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001131 buildLookup(this);
Douglas Gregor44b43212008-12-11 16:49:14 +00001132
Douglas Gregorc36c5402009-04-09 17:29:08 +00001133 if (!LookupPtr)
Douglas Gregora5fdd9c2010-05-11 06:18:17 +00001134 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Douglas Gregorc36c5402009-04-09 17:29:08 +00001135 }
Douglas Gregor44b43212008-12-11 16:49:14 +00001136
John McCall0c01d182010-03-24 05:22:00 +00001137 StoredDeclsMap::iterator Pos = LookupPtr->find(Name);
1138 if (Pos == LookupPtr->end())
Douglas Gregora5fdd9c2010-05-11 06:18:17 +00001139 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001140 return Pos->second.getLookupResult();
Douglas Gregor44b43212008-12-11 16:49:14 +00001141}
1142
Mike Stump1eb44332009-09-09 15:08:12 +00001143DeclContext::lookup_const_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001144DeclContext::lookup(DeclarationName Name) const {
1145 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +00001146}
1147
Douglas Gregorb75a3452011-10-15 00:10:27 +00001148void DeclContext::localUncachedLookup(DeclarationName Name,
1149 llvm::SmallVectorImpl<NamedDecl *> &Results) {
1150 Results.clear();
1151
1152 // If there's no external storage, just perform a normal lookup and copy
1153 // the results.
1154 if (!hasExternalVisibleStorage() && !hasExternalLexicalStorage()) {
1155 lookup_result LookupResults = lookup(Name);
1156 Results.insert(Results.end(), LookupResults.first, LookupResults.second);
1157 return;
1158 }
1159
1160 // If we have a lookup table, check there first. Maybe we'll get lucky.
1161 if (LookupPtr) {
1162 StoredDeclsMap::iterator Pos = LookupPtr->find(Name);
1163 if (Pos != LookupPtr->end()) {
1164 Results.insert(Results.end(),
1165 Pos->second.getLookupResult().first,
1166 Pos->second.getLookupResult().second);
1167 return;
1168 }
1169 }
1170
1171 // Slow case: grovel through the declarations in our chain looking for
1172 // matches.
1173 for (Decl *D = FirstDecl; D; D = D->getNextDeclInContext()) {
1174 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1175 if (ND->getDeclName() == Name)
1176 Results.push_back(ND);
1177 }
1178}
1179
Sebastian Redl7a126a42010-08-31 00:36:30 +00001180DeclContext *DeclContext::getRedeclContext() {
Chris Lattner0cf2b192009-03-27 19:19:59 +00001181 DeclContext *Ctx = this;
Sebastian Redl410c4f22010-08-31 20:53:31 +00001182 // Skip through transparent contexts.
1183 while (Ctx->isTransparentContext())
Douglas Gregorce356072009-01-06 23:51:29 +00001184 Ctx = Ctx->getParent();
1185 return Ctx;
1186}
1187
Douglas Gregor88b70942009-02-25 22:02:03 +00001188DeclContext *DeclContext::getEnclosingNamespaceContext() {
1189 DeclContext *Ctx = this;
1190 // Skip through non-namespace, non-translation-unit contexts.
Sebastian Redl51a8a372010-08-31 00:36:23 +00001191 while (!Ctx->isFileContext())
Douglas Gregor88b70942009-02-25 22:02:03 +00001192 Ctx = Ctx->getParent();
1193 return Ctx->getPrimaryContext();
1194}
1195
Sebastian Redl7a126a42010-08-31 00:36:30 +00001196bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
1197 // For non-file contexts, this is equivalent to Equals.
1198 if (!isFileContext())
1199 return O->Equals(this);
1200
1201 do {
1202 if (O->Equals(this))
1203 return true;
1204
1205 const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O);
1206 if (!NS || !NS->isInline())
1207 break;
1208 O = NS->getParent();
1209 } while (O);
1210
1211 return false;
1212}
1213
Sean Callanan9faf8102011-10-21 02:57:43 +00001214void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable)
1215{
1216 makeDeclVisibleInContextWithFlags(D, false, Recoverable);
1217}
1218
1219void DeclContext::makeDeclVisibleInContextInternal(NamedDecl *D, bool Recoverable)
1220{
1221 makeDeclVisibleInContextWithFlags(D, true, Recoverable);
1222}
1223
1224void DeclContext::makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal, bool Recoverable) {
Douglas Gregorcc636682009-02-17 23:15:12 +00001225 // FIXME: This feels like a hack. Should DeclarationName support
1226 // template-ids, or is there a better way to keep specializations
1227 // from being visible?
Douglas Gregor9a299e02011-03-04 17:52:15 +00001228 if (isa<ClassTemplateSpecializationDecl>(D) || D->isTemplateParameter())
Douglas Gregorcc636682009-02-17 23:15:12 +00001229 return;
Eli Friedman6bc20132009-12-08 05:40:03 +00001230 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1231 if (FD->isFunctionTemplateSpecialization())
1232 return;
Douglas Gregorcc636682009-02-17 23:15:12 +00001233
Steve Naroff0701bbb2009-01-08 17:28:14 +00001234 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +00001235 if (PrimaryContext != this) {
Sean Callanan9faf8102011-10-21 02:57:43 +00001236 PrimaryContext->makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
Douglas Gregor44b43212008-12-11 16:49:14 +00001237 return;
1238 }
1239
1240 // If we already have a lookup data structure, perform the insertion
Argyrios Kyrtzidis5586b012010-07-04 21:44:25 +00001241 // into it. If we haven't deserialized externally stored decls, deserialize
1242 // them so we can add the decl. Otherwise, be lazy and don't build that
1243 // structure until someone asks for it.
1244 if (LookupPtr || !Recoverable || hasExternalVisibleStorage())
Sean Callanan9faf8102011-10-21 02:57:43 +00001245 makeDeclVisibleInContextImpl(D, Internal);
Douglas Gregor074149e2009-01-05 19:45:36 +00001246
Sebastian Redl410c4f22010-08-31 20:53:31 +00001247 // If we are a transparent context or inline namespace, insert into our
1248 // parent context, too. This operation is recursive.
1249 if (isTransparentContext() || isInlineNamespace())
Sean Callanan9faf8102011-10-21 02:57:43 +00001250 getParent()->makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00001251
1252 Decl *DCAsDecl = cast<Decl>(this);
1253 // Notify that a decl was made visible unless it's a Tag being defined.
1254 if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined()))
1255 if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
1256 L->AddedVisibleDecl(this, D);
Douglas Gregor44b43212008-12-11 16:49:14 +00001257}
1258
Sean Callanan9faf8102011-10-21 02:57:43 +00001259void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal) {
Douglas Gregor074149e2009-01-05 19:45:36 +00001260 // Skip unnamed declarations.
1261 if (!D->getDeclName())
1262 return;
1263
Douglas Gregor5cb0ef42011-05-06 23:32:38 +00001264 // Skip entities that can't be found by name lookup into a particular
1265 // context.
1266 if ((D->getIdentifierNamespace() == 0 && !isa<UsingDirectiveDecl>(D)) ||
1267 D->isTemplateParameter())
Douglas Gregorcc636682009-02-17 23:15:12 +00001268 return;
1269
Argyrios Kyrtzidis5586b012010-07-04 21:44:25 +00001270 ASTContext *C = 0;
1271 if (!LookupPtr) {
1272 C = &getParentASTContext();
1273 CreateStoredDeclsMap(*C);
1274 }
1275
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001276 // If there is an external AST source, load any declarations it knows about
1277 // with this declaration's name.
1278 // If the lookup table contains an entry about this name it means that we
1279 // have already checked the external source.
Sean Callanan9faf8102011-10-21 02:57:43 +00001280 if (!Internal)
1281 if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
1282 if (hasExternalVisibleStorage() &&
1283 LookupPtr->find(D->getDeclName()) == LookupPtr->end())
1284 Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001285
Douglas Gregor44b43212008-12-11 16:49:14 +00001286 // Insert this declaration into the map.
John McCall0c01d182010-03-24 05:22:00 +00001287 StoredDeclsList &DeclNameEntries = (*LookupPtr)[D->getDeclName()];
Chris Lattner67762a32009-02-20 01:44:05 +00001288 if (DeclNameEntries.isNull()) {
1289 DeclNameEntries.setOnlyValue(D);
Chris Lattnerbd6c8002009-02-19 07:00:44 +00001290 return;
Douglas Gregor44b43212008-12-11 16:49:14 +00001291 }
Chris Lattner91942502009-02-20 00:55:03 +00001292
Chris Lattnerbdc3d002009-02-20 01:10:07 +00001293 // If it is possible that this is a redeclaration, check to see if there is
1294 // already a decl for which declarationReplaces returns true. If there is
1295 // one, just replace it and return.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001296 if (DeclNameEntries.HandleRedeclaration(D))
Chris Lattner67762a32009-02-20 01:44:05 +00001297 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001298
Chris Lattnerbd6c8002009-02-19 07:00:44 +00001299 // Put this declaration into the appropriate slot.
Chris Lattner67762a32009-02-20 01:44:05 +00001300 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor44b43212008-12-11 16:49:14 +00001301}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001302
1303/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
1304/// this context.
Mike Stump1eb44332009-09-09 15:08:12 +00001305DeclContext::udir_iterator_range
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001306DeclContext::getUsingDirectives() const {
1307 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001308 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
1309 reinterpret_cast<udir_iterator>(Result.second));
1310}
Douglas Gregor2cf26342009-04-09 22:27:44 +00001311
Ted Kremenek3478eb62010-02-11 07:12:28 +00001312//===----------------------------------------------------------------------===//
1313// Creation and Destruction of StoredDeclsMaps. //
1314//===----------------------------------------------------------------------===//
1315
John McCall0c01d182010-03-24 05:22:00 +00001316StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
1317 assert(!LookupPtr && "context already has a decls map");
1318 assert(getPrimaryContext() == this &&
1319 "creating decls map on non-primary context");
1320
1321 StoredDeclsMap *M;
1322 bool Dependent = isDependentContext();
1323 if (Dependent)
1324 M = new DependentStoredDeclsMap();
1325 else
1326 M = new StoredDeclsMap();
1327 M->Previous = C.LastSDM;
1328 C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
1329 LookupPtr = M;
Ted Kremenek3478eb62010-02-11 07:12:28 +00001330 return M;
1331}
1332
1333void ASTContext::ReleaseDeclContextMaps() {
John McCall0c01d182010-03-24 05:22:00 +00001334 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1335 // pointer because the subclass doesn't add anything that needs to
1336 // be deleted.
John McCall0c01d182010-03-24 05:22:00 +00001337 StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1338}
1339
1340void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1341 while (Map) {
1342 // Advance the iteration before we invalidate memory.
1343 llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1344
1345 if (Dependent)
1346 delete static_cast<DependentStoredDeclsMap*>(Map);
1347 else
1348 delete Map;
1349
1350 Map = Next.getPointer();
1351 Dependent = Next.getInt();
1352 }
1353}
1354
John McCall0c01d182010-03-24 05:22:00 +00001355DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1356 DeclContext *Parent,
1357 const PartialDiagnostic &PDiag) {
1358 assert(Parent->isDependentContext()
1359 && "cannot iterate dependent diagnostics of non-dependent context");
1360 Parent = Parent->getPrimaryContext();
1361 if (!Parent->LookupPtr)
1362 Parent->CreateStoredDeclsMap(C);
1363
1364 DependentStoredDeclsMap *Map
1365 = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr);
1366
Douglas Gregorb8365182010-03-29 23:56:53 +00001367 // Allocate the copy of the PartialDiagnostic via the ASTContext's
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00001368 // BumpPtrAllocator, rather than the ASTContext itself.
Douglas Gregorb8365182010-03-29 23:56:53 +00001369 PartialDiagnostic::Storage *DiagStorage = 0;
1370 if (PDiag.hasStorage())
1371 DiagStorage = new (C) PartialDiagnostic::Storage;
1372
1373 DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
John McCall0c01d182010-03-24 05:22:00 +00001374
1375 // TODO: Maybe we shouldn't reverse the order during insertion.
1376 DD->NextDiagnostic = Map->FirstDiagnostic;
1377 Map->FirstDiagnostic = DD;
1378
1379 return DD;
Ted Kremenek3478eb62010-02-11 07:12:28 +00001380}