blob: 883e3fd69801ea8217872036983b35932e75a439 [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"
Chandler Carruth55fc8732012-12-04 09:13:33 +000015#include "clang/AST/ASTContext.h"
16#include "clang/AST/ASTMutationListener.h"
17#include "clang/AST/Attr.h"
Douglas Gregor64650af2009-02-02 23:39:07 +000018#include "clang/AST/Decl.h"
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +000019#include "clang/AST/DeclCXX.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000020#include "clang/AST/DeclContextInternals.h"
John McCall92b7f702010-03-11 07:50:04 +000021#include "clang/AST/DeclFriend.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000022#include "clang/AST/DeclObjC.h"
23#include "clang/AST/DeclTemplate.h"
John McCall0c01d182010-03-24 05:22:00 +000024#include "clang/AST/DependentDiagnostic.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000025#include "clang/AST/ExternalASTSource.h"
Sebastian Redld3a413d2009-04-26 20:35:05 +000026#include "clang/AST/Stmt.h"
27#include "clang/AST/StmtCXX.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000028#include "clang/AST/Type.h"
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000029#include "clang/Basic/TargetInfo.h"
Eli Friedman56d29372008-06-07 16:52:53 +000030#include "llvm/ADT/DenseMap.h"
Chris Lattner49f28ca2009-03-05 08:00:35 +000031#include "llvm/Support/raw_ostream.h"
Douglas Gregor6ed40e32008-12-23 21:05:05 +000032#include <algorithm>
Eli Friedman56d29372008-06-07 16:52:53 +000033using namespace clang;
34
35//===----------------------------------------------------------------------===//
36// Statistics
37//===----------------------------------------------------------------------===//
38
Sean Hunt9a555912010-05-30 07:21:58 +000039#define DECL(DERIVED, BASE) static int n##DERIVED##s = 0;
40#define ABSTRACT_DECL(DECL)
41#include "clang/AST/DeclNodes.inc"
Eli Friedman56d29372008-06-07 16:52:53 +000042
Douglas Gregor1e68ecc2012-01-05 21:55:30 +000043void *Decl::AllocateDeserializedDecl(const ASTContext &Context,
44 unsigned ID,
45 unsigned Size) {
Douglas Gregor5d1f4962012-01-05 23:49:36 +000046 // Allocate an extra 8 bytes worth of storage, which ensures that the
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +000047 // resulting pointer will still be 8-byte aligned.
Douglas Gregor5d1f4962012-01-05 23:49:36 +000048 void *Start = Context.Allocate(Size + 8);
49 void *Result = (char*)Start + 8;
Douglas Gregorb6b60c12012-01-05 22:27:05 +000050
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +000051 unsigned *PrefixPtr = (unsigned *)Result - 2;
52
53 // Zero out the first 4 bytes; this is used to store the owning module ID.
54 PrefixPtr[0] = 0;
55
56 // Store the global declaration ID in the second 4 bytes.
57 PrefixPtr[1] = ID;
Douglas Gregorb6b60c12012-01-05 22:27:05 +000058
59 return Result;
Douglas Gregor1e68ecc2012-01-05 21:55:30 +000060}
61
Eli Friedman56d29372008-06-07 16:52:53 +000062const char *Decl::getDeclKindName() const {
63 switch (DeclKind) {
David Blaikieb219cfc2011-09-23 05:06:16 +000064 default: llvm_unreachable("Declaration not in DeclNodes.inc!");
Sean Hunt9a555912010-05-30 07:21:58 +000065#define DECL(DERIVED, BASE) case DERIVED: return #DERIVED;
66#define ABSTRACT_DECL(DECL)
67#include "clang/AST/DeclNodes.inc"
Eli Friedman56d29372008-06-07 16:52:53 +000068 }
69}
70
Douglas Gregor42738572010-03-05 00:26:45 +000071void Decl::setInvalidDecl(bool Invalid) {
72 InvalidDecl = Invalid;
Argyrios Kyrtzidisba50b3e2012-03-09 21:09:04 +000073 if (Invalid && !isa<ParmVarDecl>(this)) {
Douglas Gregor42738572010-03-05 00:26:45 +000074 // Defensive maneuver for ill-formed code: we're likely not to make it to
75 // a point where we set the access specifier, so default it to "public"
76 // to avoid triggering asserts elsewhere in the front end.
77 setAccess(AS_public);
78 }
79}
80
Steve Naroff0a473932009-01-20 19:53:53 +000081const char *DeclContext::getDeclKindName() const {
82 switch (DeclKind) {
David Blaikieb219cfc2011-09-23 05:06:16 +000083 default: llvm_unreachable("Declaration context not in DeclNodes.inc!");
Sean Hunt9a555912010-05-30 07:21:58 +000084#define DECL(DERIVED, BASE) case Decl::DERIVED: return #DERIVED;
85#define ABSTRACT_DECL(DECL)
86#include "clang/AST/DeclNodes.inc"
Steve Naroff0a473932009-01-20 19:53:53 +000087 }
88}
89
Daniel Dunbar02892a62012-03-05 21:42:49 +000090bool Decl::StatisticsEnabled = false;
91void Decl::EnableStatistics() {
92 StatisticsEnabled = true;
Eli Friedman56d29372008-06-07 16:52:53 +000093}
94
95void Decl::PrintStats() {
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000096 llvm::errs() << "\n*** Decl Stats:\n";
Mike Stump1eb44332009-09-09 15:08:12 +000097
Douglas Gregor64650af2009-02-02 23:39:07 +000098 int totalDecls = 0;
Sean Hunt9a555912010-05-30 07:21:58 +000099#define DECL(DERIVED, BASE) totalDecls += n##DERIVED##s;
100#define ABSTRACT_DECL(DECL)
101#include "clang/AST/DeclNodes.inc"
Chandler Carruthb43c8ec2011-07-04 06:13:27 +0000102 llvm::errs() << " " << totalDecls << " decls total.\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000103
Douglas Gregor64650af2009-02-02 23:39:07 +0000104 int totalBytes = 0;
Sean Hunt9a555912010-05-30 07:21:58 +0000105#define DECL(DERIVED, BASE) \
106 if (n##DERIVED##s > 0) { \
107 totalBytes += (int)(n##DERIVED##s * sizeof(DERIVED##Decl)); \
Chandler Carruthb43c8ec2011-07-04 06:13:27 +0000108 llvm::errs() << " " << n##DERIVED##s << " " #DERIVED " decls, " \
109 << sizeof(DERIVED##Decl) << " each (" \
110 << n##DERIVED##s * sizeof(DERIVED##Decl) \
111 << " bytes)\n"; \
Douglas Gregor64650af2009-02-02 23:39:07 +0000112 }
Sean Hunt9a555912010-05-30 07:21:58 +0000113#define ABSTRACT_DECL(DECL)
114#include "clang/AST/DeclNodes.inc"
Mike Stump1eb44332009-09-09 15:08:12 +0000115
Chandler Carruthb43c8ec2011-07-04 06:13:27 +0000116 llvm::errs() << "Total bytes = " << totalBytes << "\n";
Eli Friedman56d29372008-06-07 16:52:53 +0000117}
118
Sean Hunt9a555912010-05-30 07:21:58 +0000119void Decl::add(Kind k) {
Eli Friedman56d29372008-06-07 16:52:53 +0000120 switch (k) {
Sean Hunt9a555912010-05-30 07:21:58 +0000121#define DECL(DERIVED, BASE) case DERIVED: ++n##DERIVED##s; break;
122#define ABSTRACT_DECL(DECL)
123#include "clang/AST/DeclNodes.inc"
Eli Friedman56d29372008-06-07 16:52:53 +0000124 }
125}
126
Anders Carlsson67e33202009-06-13 00:08:58 +0000127bool Decl::isTemplateParameterPack() const {
128 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
129 return TTP->isParameterPack();
Douglas Gregor10738d32010-12-23 23:51:58 +0000130 if (const NonTypeTemplateParmDecl *NTTP
Douglas Gregor61c4d282011-01-05 15:48:55 +0000131 = dyn_cast<NonTypeTemplateParmDecl>(this))
Douglas Gregor10738d32010-12-23 23:51:58 +0000132 return NTTP->isParameterPack();
Douglas Gregor61c4d282011-01-05 15:48:55 +0000133 if (const TemplateTemplateParmDecl *TTP
134 = dyn_cast<TemplateTemplateParmDecl>(this))
135 return TTP->isParameterPack();
Anders Carlsson67e33202009-06-13 00:08:58 +0000136 return false;
137}
138
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000139bool Decl::isParameterPack() const {
140 if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(this))
141 return Parm->isParameterPack();
142
143 return isTemplateParameterPack();
144}
145
Douglas Gregore53060f2009-06-25 22:08:12 +0000146bool Decl::isFunctionOrFunctionTemplate() const {
John McCall9488ea12009-11-17 05:59:44 +0000147 if (const UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(this))
Anders Carlsson58badb72009-06-26 05:26:50 +0000148 return UD->getTargetDecl()->isFunctionOrFunctionTemplate();
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Douglas Gregore53060f2009-06-25 22:08:12 +0000150 return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this);
151}
152
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000153bool Decl::isTemplateDecl() const {
154 return isa<TemplateDecl>(this);
155}
156
Argyrios Kyrtzidisc8680f42011-09-28 02:45:33 +0000157const DeclContext *Decl::getParentFunctionOrMethod() const {
158 for (const DeclContext *DC = getDeclContext();
159 DC && !DC->isTranslationUnit() && !DC->isNamespace();
Douglas Gregor79c22782010-01-16 20:21:20 +0000160 DC = DC->getParent())
161 if (DC->isFunctionOrMethod())
Argyrios Kyrtzidisc8680f42011-09-28 02:45:33 +0000162 return DC;
Douglas Gregor79c22782010-01-16 20:21:20 +0000163
Argyrios Kyrtzidisc8680f42011-09-28 02:45:33 +0000164 return 0;
Douglas Gregor79c22782010-01-16 20:21:20 +0000165}
166
Douglas Gregor4c3e0ee2011-02-17 08:47:29 +0000167
Eli Friedman56d29372008-06-07 16:52:53 +0000168//===----------------------------------------------------------------------===//
Chris Lattner49f28ca2009-03-05 08:00:35 +0000169// PrettyStackTraceDecl Implementation
170//===----------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000171
Chris Lattner5f9e2722011-07-23 10:55:15 +0000172void PrettyStackTraceDecl::print(raw_ostream &OS) const {
Chris Lattner49f28ca2009-03-05 08:00:35 +0000173 SourceLocation TheLoc = Loc;
174 if (TheLoc.isInvalid() && TheDecl)
175 TheLoc = TheDecl->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000176
Chris Lattner49f28ca2009-03-05 08:00:35 +0000177 if (TheLoc.isValid()) {
178 TheLoc.print(OS, SM);
179 OS << ": ";
180 }
181
182 OS << Message;
183
Daniel Dunbarc5236562009-11-21 09:05:59 +0000184 if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
Chris Lattner49f28ca2009-03-05 08:00:35 +0000185 OS << " '" << DN->getQualifiedNameAsString() << '\'';
186 OS << '\n';
187}
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Chris Lattner49f28ca2009-03-05 08:00:35 +0000189//===----------------------------------------------------------------------===//
Eli Friedman56d29372008-06-07 16:52:53 +0000190// Decl Implementation
191//===----------------------------------------------------------------------===//
192
Douglas Gregorda2142f2011-02-19 18:51:44 +0000193// Out-of-line virtual method providing a home for Decl.
194Decl::~Decl() { }
Douglas Gregorf4a03cc2011-02-17 07:02:32 +0000195
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000196void Decl::setDeclContext(DeclContext *DC) {
Chris Lattneree219fd2009-03-29 06:06:59 +0000197 DeclCtx = DC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000198}
199
200void Decl::setLexicalDeclContext(DeclContext *DC) {
201 if (DC == getLexicalDeclContext())
202 return;
203
204 if (isInSemaDC()) {
Argyrios Kyrtzidis4bbb8502012-02-09 02:44:08 +0000205 setDeclContextsImpl(getDeclContext(), DC, getASTContext());
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000206 } else {
207 getMultipleDC()->LexicalDC = DC;
208 }
209}
210
Argyrios Kyrtzidis4bbb8502012-02-09 02:44:08 +0000211void Decl::setDeclContextsImpl(DeclContext *SemaDC, DeclContext *LexicalDC,
212 ASTContext &Ctx) {
213 if (SemaDC == LexicalDC) {
214 DeclCtx = SemaDC;
215 } else {
216 Decl::MultipleDC *MDC = new (Ctx) Decl::MultipleDC();
217 MDC->SemanticDC = SemaDC;
218 MDC->LexicalDC = LexicalDC;
219 DeclCtx = MDC;
220 }
221}
222
John McCall9aeed322009-10-01 00:25:31 +0000223bool Decl::isInAnonymousNamespace() const {
224 const DeclContext *DC = getDeclContext();
225 do {
226 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
227 if (ND->isAnonymousNamespace())
228 return true;
229 } while ((DC = DC->getParent()));
230
231 return false;
232}
233
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000234TranslationUnitDecl *Decl::getTranslationUnitDecl() {
Argyrios Kyrtzidis9b346692009-06-30 02:34:53 +0000235 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
236 return TUD;
237
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000238 DeclContext *DC = getDeclContext();
239 assert(DC && "This decl is not contained in a translation unit!");
Mike Stump1eb44332009-09-09 15:08:12 +0000240
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000241 while (!DC->isTranslationUnit()) {
242 DC = DC->getParent();
243 assert(DC && "This decl is not contained in a translation unit!");
244 }
Mike Stump1eb44332009-09-09 15:08:12 +0000245
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000246 return cast<TranslationUnitDecl>(DC);
247}
248
249ASTContext &Decl::getASTContext() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000250 return getTranslationUnitDecl()->getASTContext();
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000251}
252
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +0000253ASTMutationListener *Decl::getASTMutationListener() const {
254 return getASTContext().getASTMutationListener();
255}
256
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +0000257unsigned Decl::getMaxAlignment() const {
258 if (!hasAttrs())
259 return 0;
260
261 unsigned Align = 0;
262 const AttrVec &V = getAttrs();
263 ASTContext &Ctx = getASTContext();
264 specific_attr_iterator<AlignedAttr> I(V.begin()), E(V.end());
265 for (; I != E; ++I)
266 Align = std::max(Align, I->getAlignment(Ctx));
267 return Align;
268}
269
Douglas Gregorc070cc62010-06-17 23:14:26 +0000270bool Decl::isUsed(bool CheckUsedAttr) const {
Tanya Lattner12ead492010-02-17 02:17:21 +0000271 if (Used)
272 return true;
273
274 // Check for used attribute.
Douglas Gregorc070cc62010-06-17 23:14:26 +0000275 if (CheckUsedAttr && hasAttr<UsedAttr>())
Tanya Lattner12ead492010-02-17 02:17:21 +0000276 return true;
Rafael Espindola919b7e62012-11-23 16:26:30 +0000277
Tanya Lattner12ead492010-02-17 02:17:21 +0000278 return false;
279}
280
Argyrios Kyrtzidis6b6b42a2011-04-19 19:51:10 +0000281bool Decl::isReferenced() const {
282 if (Referenced)
283 return true;
284
285 // Check redeclarations.
286 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
287 if (I->Referenced)
288 return true;
289
290 return false;
291}
292
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000293/// \brief Determine the availability of the given declaration based on
294/// the target platform.
295///
296/// When it returns an availability result other than \c AR_Available,
297/// if the \p Message parameter is non-NULL, it will be set to a
298/// string describing why the entity is unavailable.
299///
300/// FIXME: Make these strings localizable, since they end up in
301/// diagnostics.
302static AvailabilityResult CheckAvailability(ASTContext &Context,
303 const AvailabilityAttr *A,
304 std::string *Message) {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000305 StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000306 StringRef PrettyPlatformName
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000307 = AvailabilityAttr::getPrettyPlatformName(TargetPlatform);
308 if (PrettyPlatformName.empty())
309 PrettyPlatformName = TargetPlatform;
310
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000311 VersionTuple TargetMinVersion = Context.getTargetInfo().getPlatformMinVersion();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000312 if (TargetMinVersion.empty())
313 return AR_Available;
314
315 // Match the platform name.
316 if (A->getPlatform()->getName() != TargetPlatform)
317 return AR_Available;
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000318
319 std::string HintMessage;
320 if (!A->getMessage().empty()) {
321 HintMessage = " - ";
322 HintMessage += A->getMessage();
323 }
324
Douglas Gregorb53e4172011-03-26 03:35:55 +0000325 // Make sure that this declaration has not been marked 'unavailable'.
326 if (A->getUnavailable()) {
327 if (Message) {
328 Message->clear();
329 llvm::raw_string_ostream Out(*Message);
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000330 Out << "not available on " << PrettyPlatformName
331 << HintMessage;
Douglas Gregorb53e4172011-03-26 03:35:55 +0000332 }
333
334 return AR_Unavailable;
335 }
336
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000337 // Make sure that this declaration has already been introduced.
338 if (!A->getIntroduced().empty() &&
339 TargetMinVersion < A->getIntroduced()) {
340 if (Message) {
341 Message->clear();
342 llvm::raw_string_ostream Out(*Message);
343 Out << "introduced in " << PrettyPlatformName << ' '
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000344 << A->getIntroduced() << HintMessage;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000345 }
346
347 return AR_NotYetIntroduced;
348 }
349
350 // Make sure that this declaration hasn't been obsoleted.
351 if (!A->getObsoleted().empty() && TargetMinVersion >= A->getObsoleted()) {
352 if (Message) {
353 Message->clear();
354 llvm::raw_string_ostream Out(*Message);
355 Out << "obsoleted in " << PrettyPlatformName << ' '
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000356 << A->getObsoleted() << HintMessage;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000357 }
358
359 return AR_Unavailable;
360 }
361
362 // Make sure that this declaration hasn't been deprecated.
363 if (!A->getDeprecated().empty() && TargetMinVersion >= A->getDeprecated()) {
364 if (Message) {
365 Message->clear();
366 llvm::raw_string_ostream Out(*Message);
367 Out << "first deprecated in " << PrettyPlatformName << ' '
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000368 << A->getDeprecated() << HintMessage;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000369 }
370
371 return AR_Deprecated;
372 }
373
374 return AR_Available;
375}
376
377AvailabilityResult Decl::getAvailability(std::string *Message) const {
378 AvailabilityResult Result = AR_Available;
379 std::string ResultMessage;
380
381 for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
382 if (DeprecatedAttr *Deprecated = dyn_cast<DeprecatedAttr>(*A)) {
383 if (Result >= AR_Deprecated)
384 continue;
385
386 if (Message)
387 ResultMessage = Deprecated->getMessage();
388
389 Result = AR_Deprecated;
390 continue;
391 }
392
393 if (UnavailableAttr *Unavailable = dyn_cast<UnavailableAttr>(*A)) {
394 if (Message)
395 *Message = Unavailable->getMessage();
396 return AR_Unavailable;
397 }
398
399 if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
400 AvailabilityResult AR = CheckAvailability(getASTContext(), Availability,
401 Message);
402
403 if (AR == AR_Unavailable)
404 return AR_Unavailable;
405
406 if (AR > Result) {
407 Result = AR;
408 if (Message)
409 ResultMessage.swap(*Message);
410 }
411 continue;
412 }
413 }
414
415 if (Message)
416 Message->swap(ResultMessage);
417 return Result;
418}
419
420bool Decl::canBeWeakImported(bool &IsDefinition) const {
421 IsDefinition = false;
John McCall260611a2012-06-20 06:18:46 +0000422
423 // Variables, if they aren't definitions.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000424 if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
425 if (!Var->hasExternalStorage() || Var->getInit()) {
426 IsDefinition = true;
427 return false;
428 }
John McCall260611a2012-06-20 06:18:46 +0000429 return true;
430
431 // Functions, if they aren't definitions.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000432 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
433 if (FD->hasBody()) {
434 IsDefinition = true;
435 return false;
436 }
John McCall260611a2012-06-20 06:18:46 +0000437 return true;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000438
John McCall260611a2012-06-20 06:18:46 +0000439 // Objective-C classes, if this is the non-fragile runtime.
440 } else if (isa<ObjCInterfaceDecl>(this) &&
John McCall0b92fcb2012-06-20 21:58:02 +0000441 getASTContext().getLangOpts().ObjCRuntime.hasWeakClassImport()) {
John McCall260611a2012-06-20 06:18:46 +0000442 return true;
443
444 // Nothing else.
445 } else {
446 return false;
447 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000448}
449
450bool Decl::isWeakImported() const {
451 bool IsDefinition;
452 if (!canBeWeakImported(IsDefinition))
453 return false;
454
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000455 for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
456 if (isa<WeakImportAttr>(*A))
457 return true;
458
459 if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
460 if (CheckAvailability(getASTContext(), Availability, 0)
461 == AR_NotYetIntroduced)
462 return true;
463 }
464 }
465
466 return false;
467}
Tanya Lattner12ead492010-02-17 02:17:21 +0000468
Chris Lattner769dbdf2009-03-27 20:18:19 +0000469unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
470 switch (DeclKind) {
John McCall9488ea12009-11-17 05:59:44 +0000471 case Function:
472 case CXXMethod:
473 case CXXConstructor:
474 case CXXDestructor:
475 case CXXConversion:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000476 case EnumConstant:
477 case Var:
478 case ImplicitParam:
479 case ParmVar:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000480 case NonTypeTemplateParm:
481 case ObjCMethod:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000482 case ObjCProperty:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000483 return IDNS_Ordinary;
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000484 case Label:
485 return IDNS_Label;
Francois Pichet87c2e122010-11-21 06:08:52 +0000486 case IndirectField:
487 return IDNS_Ordinary | IDNS_Member;
488
John McCall0d6b1642010-04-23 18:46:30 +0000489 case ObjCCompatibleAlias:
490 case ObjCInterface:
491 return IDNS_Ordinary | IDNS_Type;
492
493 case Typedef:
Richard Smith162e1c12011-04-15 14:24:37 +0000494 case TypeAlias:
Richard Smith3e4c6c42011-05-05 21:57:07 +0000495 case TypeAliasTemplate:
John McCall0d6b1642010-04-23 18:46:30 +0000496 case UnresolvedUsingTypename:
497 case TemplateTypeParm:
498 return IDNS_Ordinary | IDNS_Type;
499
John McCall9488ea12009-11-17 05:59:44 +0000500 case UsingShadow:
501 return 0; // we'll actually overwrite this later
502
John McCall7ba107a2009-11-18 02:36:19 +0000503 case UnresolvedUsingValue:
John McCall7ba107a2009-11-18 02:36:19 +0000504 return IDNS_Ordinary | IDNS_Using;
John McCall9488ea12009-11-17 05:59:44 +0000505
506 case Using:
507 return IDNS_Using;
508
Chris Lattner769dbdf2009-03-27 20:18:19 +0000509 case ObjCProtocol:
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000510 return IDNS_ObjCProtocol;
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Chris Lattner769dbdf2009-03-27 20:18:19 +0000512 case Field:
513 case ObjCAtDefsField:
514 case ObjCIvar:
515 return IDNS_Member;
Mike Stump1eb44332009-09-09 15:08:12 +0000516
Chris Lattner769dbdf2009-03-27 20:18:19 +0000517 case Record:
518 case CXXRecord:
519 case Enum:
John McCall0d6b1642010-04-23 18:46:30 +0000520 return IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000521
Chris Lattner769dbdf2009-03-27 20:18:19 +0000522 case Namespace:
John McCall0d6b1642010-04-23 18:46:30 +0000523 case NamespaceAlias:
524 return IDNS_Namespace;
525
Chris Lattner769dbdf2009-03-27 20:18:19 +0000526 case FunctionTemplate:
John McCall0d6b1642010-04-23 18:46:30 +0000527 return IDNS_Ordinary;
528
Chris Lattner769dbdf2009-03-27 20:18:19 +0000529 case ClassTemplate:
530 case TemplateTemplateParm:
John McCall0d6b1642010-04-23 18:46:30 +0000531 return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Chris Lattner769dbdf2009-03-27 20:18:19 +0000533 // Never have names.
John McCall02cace72009-08-28 07:59:38 +0000534 case Friend:
John McCalldd4a3b02009-09-16 22:47:08 +0000535 case FriendTemplate:
Abramo Bagnara6206d532010-06-05 05:09:32 +0000536 case AccessSpec:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000537 case LinkageSpec:
538 case FileScopeAsm:
539 case StaticAssert:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000540 case ObjCPropertyImpl:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000541 case Block:
542 case TranslationUnit:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000543
Chris Lattner769dbdf2009-03-27 20:18:19 +0000544 case UsingDirective:
545 case ClassTemplateSpecialization:
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000546 case ClassTemplatePartialSpecialization:
Francois Pichetaf0f4d02011-08-14 03:52:19 +0000547 case ClassScopeFunctionSpecialization:
Douglas Gregorbd4187b2010-04-22 23:19:50 +0000548 case ObjCImplementation:
549 case ObjCCategory:
550 case ObjCCategoryImpl:
Douglas Gregor15de72c2011-12-02 23:23:56 +0000551 case Import:
Douglas Gregorbd4187b2010-04-22 23:19:50 +0000552 // Never looked up by name.
Chris Lattner769dbdf2009-03-27 20:18:19 +0000553 return 0;
554 }
John McCall9488ea12009-11-17 05:59:44 +0000555
David Blaikie30263482012-01-20 21:50:17 +0000556 llvm_unreachable("Invalid DeclKind!");
Eli Friedman56d29372008-06-07 16:52:53 +0000557}
558
Argyrios Kyrtzidis4bbb8502012-02-09 02:44:08 +0000559void Decl::setAttrsImpl(const AttrVec &attrs, ASTContext &Ctx) {
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000560 assert(!HasAttrs && "Decl already contains attrs.");
561
Argyrios Kyrtzidis4bbb8502012-02-09 02:44:08 +0000562 AttrVec &AttrBlank = Ctx.getDeclAttrs(this);
Sean Huntcf807c42010-08-18 23:23:40 +0000563 assert(AttrBlank.empty() && "HasAttrs was wrong?");
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000564
565 AttrBlank = attrs;
566 HasAttrs = true;
567}
568
Sean Huntcf807c42010-08-18 23:23:40 +0000569void Decl::dropAttrs() {
Eli Friedman56d29372008-06-07 16:52:53 +0000570 if (!HasAttrs) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000571
Eli Friedman56d29372008-06-07 16:52:53 +0000572 HasAttrs = false;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000573 getASTContext().eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000574}
575
Sean Huntcf807c42010-08-18 23:23:40 +0000576const AttrVec &Decl::getAttrs() const {
577 assert(HasAttrs && "No attrs to get!");
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000578 return getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000579}
580
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000581void Decl::swapAttrs(Decl *RHS) {
Eli Friedman56d29372008-06-07 16:52:53 +0000582 bool HasLHSAttr = this->HasAttrs;
583 bool HasRHSAttr = RHS->HasAttrs;
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Eli Friedman56d29372008-06-07 16:52:53 +0000585 // Usually, neither decl has attrs, nothing to do.
586 if (!HasLHSAttr && !HasRHSAttr) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000587
Eli Friedman56d29372008-06-07 16:52:53 +0000588 // If 'this' has no attrs, swap the other way.
589 if (!HasLHSAttr)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000590 return RHS->swapAttrs(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000591
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000592 ASTContext &Context = getASTContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000593
Eli Friedman56d29372008-06-07 16:52:53 +0000594 // Handle the case when both decls have attrs.
595 if (HasRHSAttr) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000596 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman56d29372008-06-07 16:52:53 +0000597 return;
598 }
Mike Stump1eb44332009-09-09 15:08:12 +0000599
Eli Friedman56d29372008-06-07 16:52:53 +0000600 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor68584ed2009-06-18 16:11:24 +0000601 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
602 Context.eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000603 this->HasAttrs = false;
604 RHS->HasAttrs = true;
605}
606
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000607Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000608 Decl::Kind DK = D->getDeclKind();
609 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000610#define DECL(NAME, BASE)
611#define DECL_CONTEXT(NAME) \
612 case Decl::NAME: \
613 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
614#define DECL_CONTEXT_BASE(NAME)
615#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000616 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000617#define DECL(NAME, BASE)
618#define DECL_CONTEXT_BASE(NAME) \
619 if (DK >= first##NAME && DK <= last##NAME) \
620 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
621#include "clang/AST/DeclNodes.inc"
David Blaikieb219cfc2011-09-23 05:06:16 +0000622 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000623 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000624}
625
626DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000627 Decl::Kind DK = D->getKind();
628 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000629#define DECL(NAME, BASE)
630#define DECL_CONTEXT(NAME) \
631 case Decl::NAME: \
632 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
633#define DECL_CONTEXT_BASE(NAME)
634#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000635 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000636#define DECL(NAME, BASE)
637#define DECL_CONTEXT_BASE(NAME) \
638 if (DK >= first##NAME && DK <= last##NAME) \
639 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
640#include "clang/AST/DeclNodes.inc"
David Blaikieb219cfc2011-09-23 05:06:16 +0000641 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000642 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000643}
644
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000645SourceLocation Decl::getBodyRBrace() const {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000646 // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
647 // FunctionDecl stores EndRangeLoc for this purpose.
648 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
649 const FunctionDecl *Definition;
650 if (FD->hasBody(Definition))
651 return Definition->getSourceRange().getEnd();
652 return SourceLocation();
653 }
654
Argyrios Kyrtzidis6717ef42010-07-07 11:31:27 +0000655 if (Stmt *Body = getBody())
656 return Body->getSourceRange().getEnd();
657
658 return SourceLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +0000659}
660
Anders Carlsson1329c272009-03-25 23:38:06 +0000661void Decl::CheckAccessDeclContext() const {
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000662#ifndef NDEBUG
John McCall46460a62010-01-20 21:53:11 +0000663 // Suppress this check if any of the following hold:
664 // 1. this is the translation unit (and thus has no parent)
665 // 2. this is a template parameter (and thus doesn't belong to its context)
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000666 // 3. this is a non-type template parameter
667 // 4. the context is not a record
668 // 5. it's invalid
669 // 6. it's a C++0x static_assert.
Anders Carlsson35eda442009-08-29 20:47:47 +0000670 if (isa<TranslationUnitDecl>(this) ||
Argyrios Kyrtzidis04aed0e2010-07-02 11:55:44 +0000671 isa<TemplateTypeParmDecl>(this) ||
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000672 isa<NonTypeTemplateParmDecl>(this) ||
Douglas Gregorfdd8ab12010-02-22 17:53:38 +0000673 !isa<CXXRecordDecl>(getDeclContext()) ||
Argyrios Kyrtzidis65b63ec2010-09-08 21:32:35 +0000674 isInvalidDecl() ||
675 isa<StaticAssertDecl>(this) ||
676 // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
677 // as DeclContext (?).
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000678 isa<ParmVarDecl>(this) ||
679 // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
680 // AS_none as access specifier.
Francois Pichetbc845322011-08-17 01:06:54 +0000681 isa<CXXRecordDecl>(this) ||
682 isa<ClassScopeFunctionSpecializationDecl>(this))
Anders Carlsson35eda442009-08-29 20:47:47 +0000683 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000684
685 assert(Access != AS_none &&
Anders Carlsson1329c272009-03-25 23:38:06 +0000686 "Access specifier is AS_none inside a record decl");
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000687#endif
Anders Carlsson1329c272009-03-25 23:38:06 +0000688}
689
John McCallaab9e312011-02-22 22:25:23 +0000690DeclContext *Decl::getNonClosureContext() {
John McCall4b9c2d22011-11-06 09:01:30 +0000691 return getDeclContext()->getNonClosureAncestor();
692}
693
694DeclContext *DeclContext::getNonClosureAncestor() {
695 DeclContext *DC = this;
John McCallaab9e312011-02-22 22:25:23 +0000696
697 // This is basically "while (DC->isClosure()) DC = DC->getParent();"
698 // except that it's significantly more efficient to cast to a known
699 // decl type and call getDeclContext() than to call getParent().
John McCall7b3f8532011-06-23 21:18:31 +0000700 while (isa<BlockDecl>(DC))
701 DC = cast<BlockDecl>(DC)->getDeclContext();
John McCallaab9e312011-02-22 22:25:23 +0000702
703 assert(!DC->isClosure());
704 return DC;
705}
Anders Carlsson1329c272009-03-25 23:38:06 +0000706
Eli Friedman56d29372008-06-07 16:52:53 +0000707//===----------------------------------------------------------------------===//
708// DeclContext Implementation
709//===----------------------------------------------------------------------===//
710
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000711bool DeclContext::classof(const Decl *D) {
712 switch (D->getKind()) {
Sean Hunt9a555912010-05-30 07:21:58 +0000713#define DECL(NAME, BASE)
714#define DECL_CONTEXT(NAME) case Decl::NAME:
715#define DECL_CONTEXT_BASE(NAME)
716#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000717 return true;
718 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000719#define DECL(NAME, BASE)
720#define DECL_CONTEXT_BASE(NAME) \
721 if (D->getKind() >= Decl::first##NAME && \
722 D->getKind() <= Decl::last##NAME) \
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000723 return true;
Sean Hunt9a555912010-05-30 07:21:58 +0000724#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000725 return false;
726 }
727}
728
Douglas Gregora2da7802010-07-25 18:38:02 +0000729DeclContext::~DeclContext() { }
Douglas Gregor44b43212008-12-11 16:49:14 +0000730
Douglas Gregore942bbe2009-09-10 16:57:35 +0000731/// \brief Find the parent context of this context that will be
732/// used for unqualified name lookup.
733///
734/// Generally, the parent lookup context is the semantic context. However, for
735/// a friend function the parent lookup context is the lexical context, which
736/// is the class in which the friend is declared.
737DeclContext *DeclContext::getLookupParent() {
738 // FIXME: Find a better way to identify friends
739 if (isa<FunctionDecl>(this))
Sebastian Redl7a126a42010-08-31 00:36:30 +0000740 if (getParent()->getRedeclContext()->isFileContext() &&
741 getLexicalParent()->getRedeclContext()->isRecord())
Douglas Gregore942bbe2009-09-10 16:57:35 +0000742 return getLexicalParent();
743
744 return getParent();
745}
746
Sebastian Redl410c4f22010-08-31 20:53:31 +0000747bool DeclContext::isInlineNamespace() const {
748 return isNamespace() &&
749 cast<NamespaceDecl>(this)->isInline();
750}
751
Douglas Gregorbc221632009-05-28 16:34:51 +0000752bool DeclContext::isDependentContext() const {
753 if (isFileContext())
754 return false;
755
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000756 if (isa<ClassTemplatePartialSpecializationDecl>(this))
757 return true;
758
Douglas Gregorf4b7de12012-02-21 19:11:17 +0000759 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this)) {
Douglas Gregorbc221632009-05-28 16:34:51 +0000760 if (Record->getDescribedClassTemplate())
761 return true;
Douglas Gregorf4b7de12012-02-21 19:11:17 +0000762
763 if (Record->isDependentLambda())
764 return true;
765 }
766
John McCall0c01d182010-03-24 05:22:00 +0000767 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregorbc221632009-05-28 16:34:51 +0000768 if (Function->getDescribedFunctionTemplate())
769 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000770
John McCall0c01d182010-03-24 05:22:00 +0000771 // Friend function declarations are dependent if their *lexical*
772 // context is dependent.
773 if (cast<Decl>(this)->getFriendObjectKind())
774 return getLexicalParent()->isDependentContext();
775 }
776
Douglas Gregorbc221632009-05-28 16:34:51 +0000777 return getParent() && getParent()->isDependentContext();
778}
779
Douglas Gregor074149e2009-01-05 19:45:36 +0000780bool DeclContext::isTransparentContext() const {
781 if (DeclKind == Decl::Enum)
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000782 return !cast<EnumDecl>(this)->isScoped();
Douglas Gregor074149e2009-01-05 19:45:36 +0000783 else if (DeclKind == Decl::LinkageSpec)
784 return true;
Douglas Gregor074149e2009-01-05 19:45:36 +0000785
786 return false;
787}
788
John McCallac65c622010-10-26 04:59:26 +0000789bool DeclContext::isExternCContext() const {
790 const DeclContext *DC = this;
791 while (DC->DeclKind != Decl::TranslationUnit) {
792 if (DC->DeclKind == Decl::LinkageSpec)
793 return cast<LinkageSpecDecl>(DC)->getLanguage()
794 == LinkageSpecDecl::lang_c;
795 DC = DC->getParent();
796 }
797 return false;
798}
799
Sebastian Redl7a126a42010-08-31 00:36:30 +0000800bool DeclContext::Encloses(const DeclContext *DC) const {
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000801 if (getPrimaryContext() != this)
802 return getPrimaryContext()->Encloses(DC);
Mike Stump1eb44332009-09-09 15:08:12 +0000803
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000804 for (; DC; DC = DC->getParent())
805 if (DC->getPrimaryContext() == this)
806 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000807 return false;
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000808}
809
Steve Naroff0701bbb2009-01-08 17:28:14 +0000810DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000811 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000812 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000813 case Decl::LinkageSpec:
Mike Stump1eb44332009-09-09 15:08:12 +0000814 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000815 // There is only one DeclContext for these entities.
816 return this;
817
818 case Decl::Namespace:
819 // The original namespace is our primary context.
820 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
821
Douglas Gregor44b43212008-12-11 16:49:14 +0000822 case Decl::ObjCMethod:
823 return this;
824
825 case Decl::ObjCInterface:
Douglas Gregor53df7a12011-12-15 18:03:09 +0000826 if (ObjCInterfaceDecl *Def = cast<ObjCInterfaceDecl>(this)->getDefinition())
827 return Def;
828
829 return this;
830
Steve Naroff0701bbb2009-01-08 17:28:14 +0000831 case Decl::ObjCProtocol:
Douglas Gregor1d784b22012-01-01 19:51:50 +0000832 if (ObjCProtocolDecl *Def = cast<ObjCProtocolDecl>(this)->getDefinition())
833 return Def;
834
835 return this;
Douglas Gregor53df7a12011-12-15 18:03:09 +0000836
Steve Naroff0701bbb2009-01-08 17:28:14 +0000837 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000838 return this;
839
Steve Naroff0701bbb2009-01-08 17:28:14 +0000840 case Decl::ObjCImplementation:
841 case Decl::ObjCCategoryImpl:
842 return this;
843
Douglas Gregor44b43212008-12-11 16:49:14 +0000844 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000845 if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000846 // If this is a tag type that has a definition or is currently
847 // being defined, that definition is our primary context.
John McCall3cb0ebd2010-03-10 03:28:59 +0000848 TagDecl *Tag = cast<TagDecl>(this);
849 assert(isa<TagType>(Tag->TypeForDecl) ||
850 isa<InjectedClassNameType>(Tag->TypeForDecl));
851
852 if (TagDecl *Def = Tag->getDefinition())
853 return Def;
854
855 if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) {
856 const TagType *TagTy = cast<TagType>(Tag->TypeForDecl);
857 if (TagTy->isBeingDefined())
858 // FIXME: is it necessarily being defined in the decl
859 // that owns the type?
860 return TagTy->getDecl();
861 }
862
863 return Tag;
Douglas Gregorcc636682009-02-17 23:15:12 +0000864 }
865
Sean Hunt9a555912010-05-30 07:21:58 +0000866 assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
Douglas Gregor44b43212008-12-11 16:49:14 +0000867 "Unknown DeclContext kind");
868 return this;
869 }
870}
871
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +0000872void
873DeclContext::collectAllContexts(llvm::SmallVectorImpl<DeclContext *> &Contexts){
874 Contexts.clear();
875
876 if (DeclKind != Decl::Namespace) {
877 Contexts.push_back(this);
878 return;
Douglas Gregor44b43212008-12-11 16:49:14 +0000879 }
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +0000880
881 NamespaceDecl *Self = static_cast<NamespaceDecl *>(this);
Douglas Gregoref96ee02012-01-14 16:38:05 +0000882 for (NamespaceDecl *N = Self->getMostRecentDecl(); N;
883 N = N->getPreviousDecl())
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +0000884 Contexts.push_back(N);
885
886 std::reverse(Contexts.begin(), Contexts.end());
Douglas Gregor44b43212008-12-11 16:49:14 +0000887}
888
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000889std::pair<Decl *, Decl *>
Bill Wendling341785e2012-02-22 09:51:33 +0000890DeclContext::BuildDeclChain(ArrayRef<Decl*> Decls,
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +0000891 bool FieldsAlreadyLoaded) {
Douglas Gregor46cd2182012-01-06 16:59:53 +0000892 // Build up a chain of declarations via the Decl::NextInContextAndBits field.
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000893 Decl *FirstNewDecl = 0;
894 Decl *PrevDecl = 0;
895 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +0000896 if (FieldsAlreadyLoaded && isa<FieldDecl>(Decls[I]))
897 continue;
898
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000899 Decl *D = Decls[I];
900 if (PrevDecl)
Douglas Gregor46cd2182012-01-06 16:59:53 +0000901 PrevDecl->NextInContextAndBits.setPointer(D);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000902 else
903 FirstNewDecl = D;
904
905 PrevDecl = D;
906 }
907
908 return std::make_pair(FirstNewDecl, PrevDecl);
909}
910
Douglas Gregor2cf26342009-04-09 22:27:44 +0000911/// \brief Load the declarations within this lexical storage from an
912/// external source.
Mike Stump1eb44332009-09-09 15:08:12 +0000913void
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000914DeclContext::LoadLexicalDeclsFromExternalStorage() const {
915 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000916 assert(hasExternalLexicalStorage() && Source && "No external storage?");
917
Argyrios Kyrtzidis0dbbc042010-07-30 10:03:23 +0000918 // Notify that we have a DeclContext that is initializing.
919 ExternalASTSource::Deserializing ADeclContext(Source);
Douglas Gregor9fc18c92011-08-26 21:23:06 +0000920
Douglas Gregorba6ffaf2011-07-15 21:46:17 +0000921 // Load the external declarations, if any.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000922 SmallVector<Decl*, 64> Decls;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000923 ExternalLexicalStorage = false;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +0000924 switch (Source->FindExternalLexicalDecls(this, Decls)) {
925 case ELR_Success:
926 break;
927
928 case ELR_Failure:
929 case ELR_AlreadyLoaded:
930 return;
931 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000932
933 if (Decls.empty())
934 return;
935
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +0000936 // We may have already loaded just the fields of this record, in which case
937 // we need to ignore them.
938 bool FieldsAlreadyLoaded = false;
939 if (const RecordDecl *RD = dyn_cast<RecordDecl>(this))
940 FieldsAlreadyLoaded = RD->LoadedFieldsFromExternalStorage;
941
Douglas Gregor2cf26342009-04-09 22:27:44 +0000942 // Splice the newly-read declarations into the beginning of the list
943 // of declarations.
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000944 Decl *ExternalFirst, *ExternalLast;
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +0000945 llvm::tie(ExternalFirst, ExternalLast) = BuildDeclChain(Decls,
946 FieldsAlreadyLoaded);
Douglas Gregor46cd2182012-01-06 16:59:53 +0000947 ExternalLast->NextInContextAndBits.setPointer(FirstDecl);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000948 FirstDecl = ExternalFirst;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000949 if (!LastDecl)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000950 LastDecl = ExternalLast;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000951}
952
John McCall76bd1f32010-06-01 09:23:16 +0000953DeclContext::lookup_result
954ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
955 DeclarationName Name) {
956 ASTContext &Context = DC->getParentASTContext();
957 StoredDeclsMap *Map;
Richard Smithc5d3e802012-03-16 06:12:59 +0000958 if (!(Map = DC->LookupPtr.getPointer()))
John McCall76bd1f32010-06-01 09:23:16 +0000959 Map = DC->CreateStoredDeclsMap(Context);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000960
John McCall76bd1f32010-06-01 09:23:16 +0000961 StoredDeclsList &List = (*Map)[Name];
962 assert(List.isNull());
963 (void) List;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000964
John McCall76bd1f32010-06-01 09:23:16 +0000965 return DeclContext::lookup_result();
966}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000967
John McCall76bd1f32010-06-01 09:23:16 +0000968DeclContext::lookup_result
969ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +0000970 DeclarationName Name,
Argyrios Kyrtzidis45df9c62011-09-09 06:44:14 +0000971 ArrayRef<NamedDecl*> Decls) {
Dmitri Gribenko1ad23d62012-09-10 21:20:09 +0000972 ASTContext &Context = DC->getParentASTContext();
John McCall76bd1f32010-06-01 09:23:16 +0000973
974 StoredDeclsMap *Map;
Richard Smithc5d3e802012-03-16 06:12:59 +0000975 if (!(Map = DC->LookupPtr.getPointer()))
John McCall76bd1f32010-06-01 09:23:16 +0000976 Map = DC->CreateStoredDeclsMap(Context);
977
978 StoredDeclsList &List = (*Map)[Name];
Argyrios Kyrtzidis45df9c62011-09-09 06:44:14 +0000979 for (ArrayRef<NamedDecl*>::iterator
980 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
John McCall76bd1f32010-06-01 09:23:16 +0000981 if (List.isNull())
Argyrios Kyrtzidis45df9c62011-09-09 06:44:14 +0000982 List.setOnlyValue(*I);
John McCall76bd1f32010-06-01 09:23:16 +0000983 else
Argyrios Kyrtzidis45df9c62011-09-09 06:44:14 +0000984 List.AddSubsequentDecl(*I);
John McCall76bd1f32010-06-01 09:23:16 +0000985 }
986
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000987 return List.getLookupResult();
John McCall76bd1f32010-06-01 09:23:16 +0000988}
989
Sebastian Redl681d7232010-07-27 00:17:23 +0000990DeclContext::decl_iterator DeclContext::noload_decls_begin() const {
991 return decl_iterator(FirstDecl);
992}
993
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000994DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000995 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000996 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000997
Mike Stump1eb44332009-09-09 15:08:12 +0000998 return decl_iterator(FirstDecl);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000999}
1000
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001001bool DeclContext::decls_empty() const {
Douglas Gregor8038d512009-04-10 17:25:41 +00001002 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001003 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor8038d512009-04-10 17:25:41 +00001004
1005 return !FirstDecl;
1006}
1007
John McCall9f54ad42009-12-10 09:41:52 +00001008void DeclContext::removeDecl(Decl *D) {
1009 assert(D->getLexicalDeclContext() == this &&
1010 "decl being removed from non-lexical context");
Douglas Gregor46cd2182012-01-06 16:59:53 +00001011 assert((D->NextInContextAndBits.getPointer() || D == LastDecl) &&
John McCall9f54ad42009-12-10 09:41:52 +00001012 "decl is not in decls list");
1013
1014 // Remove D from the decl chain. This is O(n) but hopefully rare.
1015 if (D == FirstDecl) {
1016 if (D == LastDecl)
1017 FirstDecl = LastDecl = 0;
1018 else
Douglas Gregor46cd2182012-01-06 16:59:53 +00001019 FirstDecl = D->NextInContextAndBits.getPointer();
John McCall9f54ad42009-12-10 09:41:52 +00001020 } else {
Douglas Gregor46cd2182012-01-06 16:59:53 +00001021 for (Decl *I = FirstDecl; true; I = I->NextInContextAndBits.getPointer()) {
John McCall9f54ad42009-12-10 09:41:52 +00001022 assert(I && "decl not found in linked list");
Douglas Gregor46cd2182012-01-06 16:59:53 +00001023 if (I->NextInContextAndBits.getPointer() == D) {
1024 I->NextInContextAndBits.setPointer(D->NextInContextAndBits.getPointer());
John McCall9f54ad42009-12-10 09:41:52 +00001025 if (D == LastDecl) LastDecl = I;
1026 break;
1027 }
1028 }
1029 }
1030
1031 // Mark that D is no longer in the decl chain.
Douglas Gregor46cd2182012-01-06 16:59:53 +00001032 D->NextInContextAndBits.setPointer(0);
John McCall9f54ad42009-12-10 09:41:52 +00001033
1034 // Remove D from the lookup table if necessary.
1035 if (isa<NamedDecl>(D)) {
1036 NamedDecl *ND = cast<NamedDecl>(D);
1037
Axel Naumann02368d02011-08-26 14:06:12 +00001038 // Remove only decls that have a name
1039 if (!ND->getDeclName()) return;
1040
Richard Smithc5d3e802012-03-16 06:12:59 +00001041 StoredDeclsMap *Map = getPrimaryContext()->LookupPtr.getPointer();
John McCall0c01d182010-03-24 05:22:00 +00001042 if (!Map) return;
John McCall9f54ad42009-12-10 09:41:52 +00001043
John McCall9f54ad42009-12-10 09:41:52 +00001044 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
1045 assert(Pos != Map->end() && "no lookup entry for decl");
Axel Naumannd9d137e2011-11-08 18:21:06 +00001046 if (Pos->second.getAsVector() || Pos->second.getAsDecl() == ND)
1047 Pos->second.remove(ND);
John McCall9f54ad42009-12-10 09:41:52 +00001048 }
1049}
1050
John McCall3f9a8a62009-08-11 06:59:38 +00001051void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner7f0be132009-02-20 00:56:18 +00001052 assert(D->getLexicalDeclContext() == this &&
1053 "Decl inserted into wrong lexical context");
Mike Stump1eb44332009-09-09 15:08:12 +00001054 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001055 "Decl already inserted into a DeclContext");
1056
1057 if (FirstDecl) {
Douglas Gregor46cd2182012-01-06 16:59:53 +00001058 LastDecl->NextInContextAndBits.setPointer(D);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001059 LastDecl = D;
1060 } else {
1061 FirstDecl = LastDecl = D;
1062 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +00001063
1064 // Notify a C++ record declaration that we've added a member, so it can
1065 // update it's class-specific state.
1066 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
1067 Record->addedMember(D);
Douglas Gregore6649772011-12-03 00:30:27 +00001068
1069 // If this is a newly-created (not de-serialized) import declaration, wire
1070 // it in to the list of local import declarations.
1071 if (!D->isFromASTFile()) {
1072 if (ImportDecl *Import = dyn_cast<ImportDecl>(D))
1073 D->getASTContext().addedLocalImportDecl(Import);
1074 }
John McCall3f9a8a62009-08-11 06:59:38 +00001075}
1076
1077void DeclContext::addDecl(Decl *D) {
1078 addHiddenDecl(D);
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001079
1080 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Richard Smithc5d3e802012-03-16 06:12:59 +00001081 ND->getDeclContext()->getPrimaryContext()->
1082 makeDeclVisibleInContextWithFlags(ND, false, true);
Douglas Gregor44b43212008-12-11 16:49:14 +00001083}
1084
Sean Callanan9faf8102011-10-21 02:57:43 +00001085void DeclContext::addDeclInternal(Decl *D) {
1086 addHiddenDecl(D);
1087
1088 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Richard Smithc5d3e802012-03-16 06:12:59 +00001089 ND->getDeclContext()->getPrimaryContext()->
1090 makeDeclVisibleInContextWithFlags(ND, true, true);
1091}
1092
1093/// shouldBeHidden - Determine whether a declaration which was declared
1094/// within its semantic context should be invisible to qualified name lookup.
1095static bool shouldBeHidden(NamedDecl *D) {
1096 // Skip unnamed declarations.
1097 if (!D->getDeclName())
1098 return true;
1099
1100 // Skip entities that can't be found by name lookup into a particular
1101 // context.
1102 if ((D->getIdentifierNamespace() == 0 && !isa<UsingDirectiveDecl>(D)) ||
1103 D->isTemplateParameter())
1104 return true;
1105
1106 // Skip template specializations.
1107 // FIXME: This feels like a hack. Should DeclarationName support
1108 // template-ids, or is there a better way to keep specializations
1109 // from being visible?
1110 if (isa<ClassTemplateSpecializationDecl>(D))
1111 return true;
1112 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1113 if (FD->isFunctionTemplateSpecialization())
1114 return true;
1115
1116 return false;
1117}
1118
1119/// buildLookup - Build the lookup data structure with all of the
1120/// declarations in this DeclContext (and any other contexts linked
1121/// to it or transparent contexts nested within it) and return it.
1122StoredDeclsMap *DeclContext::buildLookup() {
1123 assert(this == getPrimaryContext() && "buildLookup called on non-primary DC");
1124
1125 if (!LookupPtr.getInt())
1126 return LookupPtr.getPointer();
1127
1128 llvm::SmallVector<DeclContext *, 2> Contexts;
1129 collectAllContexts(Contexts);
1130 for (unsigned I = 0, N = Contexts.size(); I != N; ++I)
1131 buildLookupImpl(Contexts[I]);
1132
1133 // We no longer have any lazy decls.
1134 LookupPtr.setInt(false);
1135 return LookupPtr.getPointer();
1136}
1137
1138/// buildLookupImpl - Build part of the lookup data structure for the
1139/// declarations contained within DCtx, which will either be this
1140/// DeclContext, a DeclContext linked to it, or a transparent context
1141/// nested within it.
1142void DeclContext::buildLookupImpl(DeclContext *DCtx) {
1143 for (decl_iterator I = DCtx->decls_begin(), E = DCtx->decls_end();
1144 I != E; ++I) {
1145 Decl *D = *I;
1146
1147 // Insert this declaration into the lookup structure, but only if
1148 // it's semantically within its decl context. Any other decls which
1149 // should be found in this context are added eagerly.
1150 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1151 if (ND->getDeclContext() == DCtx && !shouldBeHidden(ND))
1152 makeDeclVisibleInContextImpl(ND, false);
1153
1154 // If this declaration is itself a transparent declaration context
1155 // or inline namespace, add the members of this declaration of that
1156 // context (recursively).
1157 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(D))
1158 if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
1159 buildLookupImpl(InnerCtx);
1160 }
Sean Callanan9faf8102011-10-21 02:57:43 +00001161}
1162
Mike Stump1eb44332009-09-09 15:08:12 +00001163DeclContext::lookup_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001164DeclContext::lookup(DeclarationName Name) {
Nick Lewycky65daef12012-03-13 04:12:34 +00001165 assert(DeclKind != Decl::LinkageSpec &&
1166 "Should not perform lookups into linkage specs!");
1167
Steve Naroff0701bbb2009-01-08 17:28:14 +00001168 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +00001169 if (PrimaryContext != this)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001170 return PrimaryContext->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +00001171
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001172 if (hasExternalVisibleStorage()) {
Richard Smithc5d3e802012-03-16 06:12:59 +00001173 // If a PCH has a result for this name, and we have a local declaration, we
1174 // will have imported the PCH result when adding the local declaration.
1175 // FIXME: For modules, we could have had more declarations added by module
1176 // imoprts since we saw the declaration of the local name.
1177 if (StoredDeclsMap *Map = LookupPtr.getPointer()) {
1178 StoredDeclsMap::iterator I = Map->find(Name);
1179 if (I != Map->end())
1180 return I->second.getLookupResult();
1181 }
1182
John McCall76bd1f32010-06-01 09:23:16 +00001183 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1184 return Source->FindExternalVisibleDeclsByName(this, Name);
1185 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001186
Richard Smithc5d3e802012-03-16 06:12:59 +00001187 StoredDeclsMap *Map = LookupPtr.getPointer();
1188 if (LookupPtr.getInt())
1189 Map = buildLookup();
1190
1191 if (!Map)
1192 return lookup_result(lookup_iterator(0), lookup_iterator(0));
1193
1194 StoredDeclsMap::iterator I = Map->find(Name);
1195 if (I == Map->end())
1196 return lookup_result(lookup_iterator(0), lookup_iterator(0));
1197
1198 return I->second.getLookupResult();
Douglas Gregor44b43212008-12-11 16:49:14 +00001199}
1200
Douglas Gregorb75a3452011-10-15 00:10:27 +00001201void DeclContext::localUncachedLookup(DeclarationName Name,
1202 llvm::SmallVectorImpl<NamedDecl *> &Results) {
1203 Results.clear();
1204
1205 // If there's no external storage, just perform a normal lookup and copy
1206 // the results.
Douglas Gregor93ed7cf2012-07-17 21:16:27 +00001207 if (!hasExternalVisibleStorage() && !hasExternalLexicalStorage() && Name) {
Douglas Gregorb75a3452011-10-15 00:10:27 +00001208 lookup_result LookupResults = lookup(Name);
1209 Results.insert(Results.end(), LookupResults.first, LookupResults.second);
1210 return;
1211 }
1212
1213 // If we have a lookup table, check there first. Maybe we'll get lucky.
Douglas Gregor93ed7cf2012-07-17 21:16:27 +00001214 if (Name) {
1215 if (StoredDeclsMap *Map = LookupPtr.getPointer()) {
1216 StoredDeclsMap::iterator Pos = Map->find(Name);
1217 if (Pos != Map->end()) {
1218 Results.insert(Results.end(),
1219 Pos->second.getLookupResult().first,
1220 Pos->second.getLookupResult().second);
1221 return;
1222 }
Douglas Gregorb75a3452011-10-15 00:10:27 +00001223 }
1224 }
Douglas Gregor93ed7cf2012-07-17 21:16:27 +00001225
Douglas Gregorb75a3452011-10-15 00:10:27 +00001226 // Slow case: grovel through the declarations in our chain looking for
1227 // matches.
1228 for (Decl *D = FirstDecl; D; D = D->getNextDeclInContext()) {
1229 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1230 if (ND->getDeclName() == Name)
1231 Results.push_back(ND);
1232 }
1233}
1234
Sebastian Redl7a126a42010-08-31 00:36:30 +00001235DeclContext *DeclContext::getRedeclContext() {
Chris Lattner0cf2b192009-03-27 19:19:59 +00001236 DeclContext *Ctx = this;
Sebastian Redl410c4f22010-08-31 20:53:31 +00001237 // Skip through transparent contexts.
1238 while (Ctx->isTransparentContext())
Douglas Gregorce356072009-01-06 23:51:29 +00001239 Ctx = Ctx->getParent();
1240 return Ctx;
1241}
1242
Douglas Gregor88b70942009-02-25 22:02:03 +00001243DeclContext *DeclContext::getEnclosingNamespaceContext() {
1244 DeclContext *Ctx = this;
1245 // Skip through non-namespace, non-translation-unit contexts.
Sebastian Redl51a8a372010-08-31 00:36:23 +00001246 while (!Ctx->isFileContext())
Douglas Gregor88b70942009-02-25 22:02:03 +00001247 Ctx = Ctx->getParent();
1248 return Ctx->getPrimaryContext();
1249}
1250
Sebastian Redl7a126a42010-08-31 00:36:30 +00001251bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
1252 // For non-file contexts, this is equivalent to Equals.
1253 if (!isFileContext())
1254 return O->Equals(this);
1255
1256 do {
1257 if (O->Equals(this))
1258 return true;
1259
1260 const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O);
1261 if (!NS || !NS->isInline())
1262 break;
1263 O = NS->getParent();
1264 } while (O);
1265
1266 return false;
1267}
1268
Richard Smithc5d3e802012-03-16 06:12:59 +00001269void DeclContext::makeDeclVisibleInContext(NamedDecl *D) {
1270 DeclContext *PrimaryDC = this->getPrimaryContext();
1271 DeclContext *DeclDC = D->getDeclContext()->getPrimaryContext();
1272 // If the decl is being added outside of its semantic decl context, we
1273 // need to ensure that we eagerly build the lookup information for it.
1274 PrimaryDC->makeDeclVisibleInContextWithFlags(D, false, PrimaryDC == DeclDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00001275}
1276
Richard Smithc5d3e802012-03-16 06:12:59 +00001277void DeclContext::makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal,
1278 bool Recoverable) {
1279 assert(this == getPrimaryContext() && "expected a primary DC");
Sean Callanan9faf8102011-10-21 02:57:43 +00001280
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001281 // Skip declarations within functions.
1282 // FIXME: We shouldn't need to build lookup tables for function declarations
1283 // ever, and we can't do so correctly because we can't model the nesting of
1284 // scopes which occurs within functions. We use "qualified" lookup into
1285 // function declarations when handling friend declarations inside nested
1286 // classes, and consequently accept the following invalid code:
1287 //
1288 // void f() { void g(); { int g; struct S { friend void g(); }; } }
1289 if (isFunctionOrMethod() && !isa<FunctionDecl>(D))
1290 return;
1291
Richard Smithc5d3e802012-03-16 06:12:59 +00001292 // Skip declarations which should be invisible to name lookup.
1293 if (shouldBeHidden(D))
1294 return;
1295
1296 // If we already have a lookup data structure, perform the insertion into
1297 // it. If we might have externally-stored decls with this name, look them
1298 // up and perform the insertion. If this decl was declared outside its
1299 // semantic context, buildLookup won't add it, so add it now.
1300 //
1301 // FIXME: As a performance hack, don't add such decls into the translation
1302 // unit unless we're in C++, since qualified lookup into the TU is never
1303 // performed.
1304 if (LookupPtr.getPointer() || hasExternalVisibleStorage() ||
1305 ((!Recoverable || D->getDeclContext() != D->getLexicalDeclContext()) &&
1306 (getParentASTContext().getLangOpts().CPlusPlus ||
1307 !isTranslationUnit()))) {
1308 // If we have lazily omitted any decls, they might have the same name as
1309 // the decl which we are adding, so build a full lookup table before adding
1310 // this decl.
1311 buildLookup();
1312 makeDeclVisibleInContextImpl(D, Internal);
1313 } else {
1314 LookupPtr.setInt(true);
1315 }
1316
1317 // If we are a transparent context or inline namespace, insert into our
1318 // parent context, too. This operation is recursive.
1319 if (isTransparentContext() || isInlineNamespace())
1320 getParent()->getPrimaryContext()->
1321 makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
1322
1323 Decl *DCAsDecl = cast<Decl>(this);
1324 // Notify that a decl was made visible unless we are a Tag being defined.
1325 if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined()))
1326 if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
1327 L->AddedVisibleDecl(this, D);
1328}
1329
1330void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal) {
1331 // Find or create the stored declaration map.
1332 StoredDeclsMap *Map = LookupPtr.getPointer();
1333 if (!Map) {
1334 ASTContext *C = &getParentASTContext();
1335 Map = CreateStoredDeclsMap(*C);
Argyrios Kyrtzidis5586b012010-07-04 21:44:25 +00001336 }
1337
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001338 // If there is an external AST source, load any declarations it knows about
1339 // with this declaration's name.
1340 // If the lookup table contains an entry about this name it means that we
1341 // have already checked the external source.
Sean Callanan9faf8102011-10-21 02:57:43 +00001342 if (!Internal)
1343 if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
1344 if (hasExternalVisibleStorage() &&
Richard Smithc5d3e802012-03-16 06:12:59 +00001345 Map->find(D->getDeclName()) == Map->end())
Sean Callanan9faf8102011-10-21 02:57:43 +00001346 Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001347
Douglas Gregor44b43212008-12-11 16:49:14 +00001348 // Insert this declaration into the map.
Richard Smithc5d3e802012-03-16 06:12:59 +00001349 StoredDeclsList &DeclNameEntries = (*Map)[D->getDeclName()];
Chris Lattner67762a32009-02-20 01:44:05 +00001350 if (DeclNameEntries.isNull()) {
1351 DeclNameEntries.setOnlyValue(D);
Richard Smithc5d3e802012-03-16 06:12:59 +00001352 return;
Douglas Gregor44b43212008-12-11 16:49:14 +00001353 }
Chris Lattner91942502009-02-20 00:55:03 +00001354
Richard Smithc5d3e802012-03-16 06:12:59 +00001355 if (DeclNameEntries.HandleRedeclaration(D)) {
1356 // This declaration has replaced an existing one for which
1357 // declarationReplaces returns true.
1358 return;
1359 }
Mike Stump1eb44332009-09-09 15:08:12 +00001360
Richard Smithc5d3e802012-03-16 06:12:59 +00001361 // Put this declaration into the appropriate slot.
1362 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor44b43212008-12-11 16:49:14 +00001363}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001364
1365/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
1366/// this context.
Mike Stump1eb44332009-09-09 15:08:12 +00001367DeclContext::udir_iterator_range
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001368DeclContext::getUsingDirectives() const {
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001369 // FIXME: Use something more efficient than normal lookup for using
1370 // directives. In C++, using directives are looked up more than anything else.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001371 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001372 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
1373 reinterpret_cast<udir_iterator>(Result.second));
1374}
Douglas Gregor2cf26342009-04-09 22:27:44 +00001375
Ted Kremenek3478eb62010-02-11 07:12:28 +00001376//===----------------------------------------------------------------------===//
1377// Creation and Destruction of StoredDeclsMaps. //
1378//===----------------------------------------------------------------------===//
1379
John McCall0c01d182010-03-24 05:22:00 +00001380StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
Richard Smithc5d3e802012-03-16 06:12:59 +00001381 assert(!LookupPtr.getPointer() && "context already has a decls map");
John McCall0c01d182010-03-24 05:22:00 +00001382 assert(getPrimaryContext() == this &&
1383 "creating decls map on non-primary context");
1384
1385 StoredDeclsMap *M;
1386 bool Dependent = isDependentContext();
1387 if (Dependent)
1388 M = new DependentStoredDeclsMap();
1389 else
1390 M = new StoredDeclsMap();
1391 M->Previous = C.LastSDM;
1392 C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
Richard Smithc5d3e802012-03-16 06:12:59 +00001393 LookupPtr.setPointer(M);
Ted Kremenek3478eb62010-02-11 07:12:28 +00001394 return M;
1395}
1396
1397void ASTContext::ReleaseDeclContextMaps() {
John McCall0c01d182010-03-24 05:22:00 +00001398 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1399 // pointer because the subclass doesn't add anything that needs to
1400 // be deleted.
John McCall0c01d182010-03-24 05:22:00 +00001401 StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1402}
1403
1404void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1405 while (Map) {
1406 // Advance the iteration before we invalidate memory.
1407 llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1408
1409 if (Dependent)
1410 delete static_cast<DependentStoredDeclsMap*>(Map);
1411 else
1412 delete Map;
1413
1414 Map = Next.getPointer();
1415 Dependent = Next.getInt();
1416 }
1417}
1418
John McCall0c01d182010-03-24 05:22:00 +00001419DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1420 DeclContext *Parent,
1421 const PartialDiagnostic &PDiag) {
1422 assert(Parent->isDependentContext()
1423 && "cannot iterate dependent diagnostics of non-dependent context");
1424 Parent = Parent->getPrimaryContext();
Richard Smithc5d3e802012-03-16 06:12:59 +00001425 if (!Parent->LookupPtr.getPointer())
John McCall0c01d182010-03-24 05:22:00 +00001426 Parent->CreateStoredDeclsMap(C);
1427
1428 DependentStoredDeclsMap *Map
Richard Smithc5d3e802012-03-16 06:12:59 +00001429 = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr.getPointer());
John McCall0c01d182010-03-24 05:22:00 +00001430
Douglas Gregorb8365182010-03-29 23:56:53 +00001431 // Allocate the copy of the PartialDiagnostic via the ASTContext's
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00001432 // BumpPtrAllocator, rather than the ASTContext itself.
Douglas Gregorb8365182010-03-29 23:56:53 +00001433 PartialDiagnostic::Storage *DiagStorage = 0;
1434 if (PDiag.hasStorage())
1435 DiagStorage = new (C) PartialDiagnostic::Storage;
1436
1437 DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
John McCall0c01d182010-03-24 05:22:00 +00001438
1439 // TODO: Maybe we shouldn't reverse the order during insertion.
1440 DD->NextDiagnostic = Map->FirstDiagnostic;
1441 Map->FirstDiagnostic = DD;
1442
1443 return DD;
Ted Kremenek3478eb62010-02-11 07:12:28 +00001444}