blob: e1202c23a846b0029def07314fbe01e9ed41f633 [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
Douglas Gregorca2ab452013-01-12 01:29:50 +000062Module *Decl::getOwningModuleSlow() const {
63 assert(isFromASTFile() && "Not from AST file?");
64 return getASTContext().getExternalSource()->getModule(getOwningModuleID());
65}
66
Eli Friedman56d29372008-06-07 16:52:53 +000067const char *Decl::getDeclKindName() const {
68 switch (DeclKind) {
David Blaikieb219cfc2011-09-23 05:06:16 +000069 default: llvm_unreachable("Declaration not in DeclNodes.inc!");
Sean Hunt9a555912010-05-30 07:21:58 +000070#define DECL(DERIVED, BASE) case DERIVED: return #DERIVED;
71#define ABSTRACT_DECL(DECL)
72#include "clang/AST/DeclNodes.inc"
Eli Friedman56d29372008-06-07 16:52:53 +000073 }
74}
75
Douglas Gregor42738572010-03-05 00:26:45 +000076void Decl::setInvalidDecl(bool Invalid) {
77 InvalidDecl = Invalid;
Argyrios Kyrtzidisba50b3e2012-03-09 21:09:04 +000078 if (Invalid && !isa<ParmVarDecl>(this)) {
Douglas Gregor42738572010-03-05 00:26:45 +000079 // Defensive maneuver for ill-formed code: we're likely not to make it to
80 // a point where we set the access specifier, so default it to "public"
81 // to avoid triggering asserts elsewhere in the front end.
82 setAccess(AS_public);
83 }
84}
85
Steve Naroff0a473932009-01-20 19:53:53 +000086const char *DeclContext::getDeclKindName() const {
87 switch (DeclKind) {
David Blaikieb219cfc2011-09-23 05:06:16 +000088 default: llvm_unreachable("Declaration context not in DeclNodes.inc!");
Sean Hunt9a555912010-05-30 07:21:58 +000089#define DECL(DERIVED, BASE) case Decl::DERIVED: return #DERIVED;
90#define ABSTRACT_DECL(DECL)
91#include "clang/AST/DeclNodes.inc"
Steve Naroff0a473932009-01-20 19:53:53 +000092 }
93}
94
Daniel Dunbar02892a62012-03-05 21:42:49 +000095bool Decl::StatisticsEnabled = false;
96void Decl::EnableStatistics() {
97 StatisticsEnabled = true;
Eli Friedman56d29372008-06-07 16:52:53 +000098}
99
100void Decl::PrintStats() {
Chandler Carruthb43c8ec2011-07-04 06:13:27 +0000101 llvm::errs() << "\n*** Decl Stats:\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Douglas Gregor64650af2009-02-02 23:39:07 +0000103 int totalDecls = 0;
Sean Hunt9a555912010-05-30 07:21:58 +0000104#define DECL(DERIVED, BASE) totalDecls += n##DERIVED##s;
105#define ABSTRACT_DECL(DECL)
106#include "clang/AST/DeclNodes.inc"
Chandler Carruthb43c8ec2011-07-04 06:13:27 +0000107 llvm::errs() << " " << totalDecls << " decls total.\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Douglas Gregor64650af2009-02-02 23:39:07 +0000109 int totalBytes = 0;
Sean Hunt9a555912010-05-30 07:21:58 +0000110#define DECL(DERIVED, BASE) \
111 if (n##DERIVED##s > 0) { \
112 totalBytes += (int)(n##DERIVED##s * sizeof(DERIVED##Decl)); \
Chandler Carruthb43c8ec2011-07-04 06:13:27 +0000113 llvm::errs() << " " << n##DERIVED##s << " " #DERIVED " decls, " \
114 << sizeof(DERIVED##Decl) << " each (" \
115 << n##DERIVED##s * sizeof(DERIVED##Decl) \
116 << " bytes)\n"; \
Douglas Gregor64650af2009-02-02 23:39:07 +0000117 }
Sean Hunt9a555912010-05-30 07:21:58 +0000118#define ABSTRACT_DECL(DECL)
119#include "clang/AST/DeclNodes.inc"
Mike Stump1eb44332009-09-09 15:08:12 +0000120
Chandler Carruthb43c8ec2011-07-04 06:13:27 +0000121 llvm::errs() << "Total bytes = " << totalBytes << "\n";
Eli Friedman56d29372008-06-07 16:52:53 +0000122}
123
Sean Hunt9a555912010-05-30 07:21:58 +0000124void Decl::add(Kind k) {
Eli Friedman56d29372008-06-07 16:52:53 +0000125 switch (k) {
Sean Hunt9a555912010-05-30 07:21:58 +0000126#define DECL(DERIVED, BASE) case DERIVED: ++n##DERIVED##s; break;
127#define ABSTRACT_DECL(DECL)
128#include "clang/AST/DeclNodes.inc"
Eli Friedman56d29372008-06-07 16:52:53 +0000129 }
130}
131
Anders Carlsson67e33202009-06-13 00:08:58 +0000132bool Decl::isTemplateParameterPack() const {
133 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
134 return TTP->isParameterPack();
Douglas Gregor10738d32010-12-23 23:51:58 +0000135 if (const NonTypeTemplateParmDecl *NTTP
Douglas Gregor61c4d282011-01-05 15:48:55 +0000136 = dyn_cast<NonTypeTemplateParmDecl>(this))
Douglas Gregor10738d32010-12-23 23:51:58 +0000137 return NTTP->isParameterPack();
Douglas Gregor61c4d282011-01-05 15:48:55 +0000138 if (const TemplateTemplateParmDecl *TTP
139 = dyn_cast<TemplateTemplateParmDecl>(this))
140 return TTP->isParameterPack();
Anders Carlsson67e33202009-06-13 00:08:58 +0000141 return false;
142}
143
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000144bool Decl::isParameterPack() const {
145 if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(this))
146 return Parm->isParameterPack();
147
148 return isTemplateParameterPack();
149}
150
Douglas Gregore53060f2009-06-25 22:08:12 +0000151bool Decl::isFunctionOrFunctionTemplate() const {
John McCall9488ea12009-11-17 05:59:44 +0000152 if (const UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(this))
Anders Carlsson58badb72009-06-26 05:26:50 +0000153 return UD->getTargetDecl()->isFunctionOrFunctionTemplate();
Mike Stump1eb44332009-09-09 15:08:12 +0000154
Douglas Gregore53060f2009-06-25 22:08:12 +0000155 return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this);
156}
157
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000158bool Decl::isTemplateDecl() const {
159 return isa<TemplateDecl>(this);
160}
161
Argyrios Kyrtzidisc8680f42011-09-28 02:45:33 +0000162const DeclContext *Decl::getParentFunctionOrMethod() const {
163 for (const DeclContext *DC = getDeclContext();
164 DC && !DC->isTranslationUnit() && !DC->isNamespace();
Douglas Gregor79c22782010-01-16 20:21:20 +0000165 DC = DC->getParent())
166 if (DC->isFunctionOrMethod())
Argyrios Kyrtzidisc8680f42011-09-28 02:45:33 +0000167 return DC;
Douglas Gregor79c22782010-01-16 20:21:20 +0000168
Argyrios Kyrtzidisc8680f42011-09-28 02:45:33 +0000169 return 0;
Douglas Gregor79c22782010-01-16 20:21:20 +0000170}
171
Douglas Gregor4c3e0ee2011-02-17 08:47:29 +0000172
Eli Friedman56d29372008-06-07 16:52:53 +0000173//===----------------------------------------------------------------------===//
Chris Lattner49f28ca2009-03-05 08:00:35 +0000174// PrettyStackTraceDecl Implementation
175//===----------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000176
Chris Lattner5f9e2722011-07-23 10:55:15 +0000177void PrettyStackTraceDecl::print(raw_ostream &OS) const {
Chris Lattner49f28ca2009-03-05 08:00:35 +0000178 SourceLocation TheLoc = Loc;
179 if (TheLoc.isInvalid() && TheDecl)
180 TheLoc = TheDecl->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000181
Chris Lattner49f28ca2009-03-05 08:00:35 +0000182 if (TheLoc.isValid()) {
183 TheLoc.print(OS, SM);
184 OS << ": ";
185 }
186
187 OS << Message;
188
Daniel Dunbarc5236562009-11-21 09:05:59 +0000189 if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
Chris Lattner49f28ca2009-03-05 08:00:35 +0000190 OS << " '" << DN->getQualifiedNameAsString() << '\'';
191 OS << '\n';
192}
Mike Stump1eb44332009-09-09 15:08:12 +0000193
Chris Lattner49f28ca2009-03-05 08:00:35 +0000194//===----------------------------------------------------------------------===//
Eli Friedman56d29372008-06-07 16:52:53 +0000195// Decl Implementation
196//===----------------------------------------------------------------------===//
197
Douglas Gregorda2142f2011-02-19 18:51:44 +0000198// Out-of-line virtual method providing a home for Decl.
199Decl::~Decl() { }
Douglas Gregorf4a03cc2011-02-17 07:02:32 +0000200
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000201void Decl::setDeclContext(DeclContext *DC) {
Chris Lattneree219fd2009-03-29 06:06:59 +0000202 DeclCtx = DC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000203}
204
205void Decl::setLexicalDeclContext(DeclContext *DC) {
206 if (DC == getLexicalDeclContext())
207 return;
208
209 if (isInSemaDC()) {
Argyrios Kyrtzidis4bbb8502012-02-09 02:44:08 +0000210 setDeclContextsImpl(getDeclContext(), DC, getASTContext());
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000211 } else {
212 getMultipleDC()->LexicalDC = DC;
213 }
214}
215
Argyrios Kyrtzidis4bbb8502012-02-09 02:44:08 +0000216void Decl::setDeclContextsImpl(DeclContext *SemaDC, DeclContext *LexicalDC,
217 ASTContext &Ctx) {
218 if (SemaDC == LexicalDC) {
219 DeclCtx = SemaDC;
220 } else {
221 Decl::MultipleDC *MDC = new (Ctx) Decl::MultipleDC();
222 MDC->SemanticDC = SemaDC;
223 MDC->LexicalDC = LexicalDC;
224 DeclCtx = MDC;
225 }
226}
227
John McCall9aeed322009-10-01 00:25:31 +0000228bool Decl::isInAnonymousNamespace() const {
229 const DeclContext *DC = getDeclContext();
230 do {
231 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
232 if (ND->isAnonymousNamespace())
233 return true;
234 } while ((DC = DC->getParent()));
235
236 return false;
237}
238
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000239TranslationUnitDecl *Decl::getTranslationUnitDecl() {
Argyrios Kyrtzidis9b346692009-06-30 02:34:53 +0000240 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
241 return TUD;
242
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000243 DeclContext *DC = getDeclContext();
244 assert(DC && "This decl is not contained in a translation unit!");
Mike Stump1eb44332009-09-09 15:08:12 +0000245
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000246 while (!DC->isTranslationUnit()) {
247 DC = DC->getParent();
248 assert(DC && "This decl is not contained in a translation unit!");
249 }
Mike Stump1eb44332009-09-09 15:08:12 +0000250
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000251 return cast<TranslationUnitDecl>(DC);
252}
253
254ASTContext &Decl::getASTContext() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000255 return getTranslationUnitDecl()->getASTContext();
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000256}
257
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +0000258ASTMutationListener *Decl::getASTMutationListener() const {
259 return getASTContext().getASTMutationListener();
260}
261
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +0000262unsigned Decl::getMaxAlignment() const {
263 if (!hasAttrs())
264 return 0;
265
266 unsigned Align = 0;
267 const AttrVec &V = getAttrs();
268 ASTContext &Ctx = getASTContext();
269 specific_attr_iterator<AlignedAttr> I(V.begin()), E(V.end());
270 for (; I != E; ++I)
271 Align = std::max(Align, I->getAlignment(Ctx));
272 return Align;
273}
274
Douglas Gregorc070cc62010-06-17 23:14:26 +0000275bool Decl::isUsed(bool CheckUsedAttr) const {
Tanya Lattner12ead492010-02-17 02:17:21 +0000276 if (Used)
277 return true;
278
279 // Check for used attribute.
Douglas Gregorc070cc62010-06-17 23:14:26 +0000280 if (CheckUsedAttr && hasAttr<UsedAttr>())
Tanya Lattner12ead492010-02-17 02:17:21 +0000281 return true;
Rafael Espindola919b7e62012-11-23 16:26:30 +0000282
Tanya Lattner12ead492010-02-17 02:17:21 +0000283 return false;
284}
285
Argyrios Kyrtzidis6b6b42a2011-04-19 19:51:10 +0000286bool Decl::isReferenced() const {
287 if (Referenced)
288 return true;
289
290 // Check redeclarations.
291 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
292 if (I->Referenced)
293 return true;
294
295 return false;
296}
297
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000298/// \brief Determine the availability of the given declaration based on
299/// the target platform.
300///
301/// When it returns an availability result other than \c AR_Available,
302/// if the \p Message parameter is non-NULL, it will be set to a
303/// string describing why the entity is unavailable.
304///
305/// FIXME: Make these strings localizable, since they end up in
306/// diagnostics.
307static AvailabilityResult CheckAvailability(ASTContext &Context,
308 const AvailabilityAttr *A,
309 std::string *Message) {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000310 StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000311 StringRef PrettyPlatformName
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000312 = AvailabilityAttr::getPrettyPlatformName(TargetPlatform);
313 if (PrettyPlatformName.empty())
314 PrettyPlatformName = TargetPlatform;
315
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000316 VersionTuple TargetMinVersion = Context.getTargetInfo().getPlatformMinVersion();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000317 if (TargetMinVersion.empty())
318 return AR_Available;
319
320 // Match the platform name.
321 if (A->getPlatform()->getName() != TargetPlatform)
322 return AR_Available;
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000323
324 std::string HintMessage;
325 if (!A->getMessage().empty()) {
326 HintMessage = " - ";
327 HintMessage += A->getMessage();
328 }
329
Douglas Gregorb53e4172011-03-26 03:35:55 +0000330 // Make sure that this declaration has not been marked 'unavailable'.
331 if (A->getUnavailable()) {
332 if (Message) {
333 Message->clear();
334 llvm::raw_string_ostream Out(*Message);
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000335 Out << "not available on " << PrettyPlatformName
336 << HintMessage;
Douglas Gregorb53e4172011-03-26 03:35:55 +0000337 }
338
339 return AR_Unavailable;
340 }
341
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000342 // Make sure that this declaration has already been introduced.
343 if (!A->getIntroduced().empty() &&
344 TargetMinVersion < A->getIntroduced()) {
345 if (Message) {
346 Message->clear();
347 llvm::raw_string_ostream Out(*Message);
348 Out << "introduced in " << PrettyPlatformName << ' '
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000349 << A->getIntroduced() << HintMessage;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000350 }
351
352 return AR_NotYetIntroduced;
353 }
354
355 // Make sure that this declaration hasn't been obsoleted.
356 if (!A->getObsoleted().empty() && TargetMinVersion >= A->getObsoleted()) {
357 if (Message) {
358 Message->clear();
359 llvm::raw_string_ostream Out(*Message);
360 Out << "obsoleted in " << PrettyPlatformName << ' '
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000361 << A->getObsoleted() << HintMessage;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000362 }
363
364 return AR_Unavailable;
365 }
366
367 // Make sure that this declaration hasn't been deprecated.
368 if (!A->getDeprecated().empty() && TargetMinVersion >= A->getDeprecated()) {
369 if (Message) {
370 Message->clear();
371 llvm::raw_string_ostream Out(*Message);
372 Out << "first deprecated in " << PrettyPlatformName << ' '
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000373 << A->getDeprecated() << HintMessage;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000374 }
375
376 return AR_Deprecated;
377 }
378
379 return AR_Available;
380}
381
382AvailabilityResult Decl::getAvailability(std::string *Message) const {
383 AvailabilityResult Result = AR_Available;
384 std::string ResultMessage;
385
386 for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
387 if (DeprecatedAttr *Deprecated = dyn_cast<DeprecatedAttr>(*A)) {
388 if (Result >= AR_Deprecated)
389 continue;
390
391 if (Message)
392 ResultMessage = Deprecated->getMessage();
393
394 Result = AR_Deprecated;
395 continue;
396 }
397
398 if (UnavailableAttr *Unavailable = dyn_cast<UnavailableAttr>(*A)) {
399 if (Message)
400 *Message = Unavailable->getMessage();
401 return AR_Unavailable;
402 }
403
404 if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
405 AvailabilityResult AR = CheckAvailability(getASTContext(), Availability,
406 Message);
407
408 if (AR == AR_Unavailable)
409 return AR_Unavailable;
410
411 if (AR > Result) {
412 Result = AR;
413 if (Message)
414 ResultMessage.swap(*Message);
415 }
416 continue;
417 }
418 }
419
420 if (Message)
421 Message->swap(ResultMessage);
422 return Result;
423}
424
425bool Decl::canBeWeakImported(bool &IsDefinition) const {
426 IsDefinition = false;
John McCall260611a2012-06-20 06:18:46 +0000427
428 // Variables, if they aren't definitions.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000429 if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
430 if (!Var->hasExternalStorage() || Var->getInit()) {
431 IsDefinition = true;
432 return false;
433 }
John McCall260611a2012-06-20 06:18:46 +0000434 return true;
435
436 // Functions, if they aren't definitions.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000437 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
438 if (FD->hasBody()) {
439 IsDefinition = true;
440 return false;
441 }
John McCall260611a2012-06-20 06:18:46 +0000442 return true;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000443
John McCall260611a2012-06-20 06:18:46 +0000444 // Objective-C classes, if this is the non-fragile runtime.
445 } else if (isa<ObjCInterfaceDecl>(this) &&
John McCall0b92fcb2012-06-20 21:58:02 +0000446 getASTContext().getLangOpts().ObjCRuntime.hasWeakClassImport()) {
John McCall260611a2012-06-20 06:18:46 +0000447 return true;
448
449 // Nothing else.
450 } else {
451 return false;
452 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000453}
454
455bool Decl::isWeakImported() const {
456 bool IsDefinition;
457 if (!canBeWeakImported(IsDefinition))
458 return false;
459
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000460 for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
461 if (isa<WeakImportAttr>(*A))
462 return true;
463
464 if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
465 if (CheckAvailability(getASTContext(), Availability, 0)
466 == AR_NotYetIntroduced)
467 return true;
468 }
469 }
470
471 return false;
472}
Tanya Lattner12ead492010-02-17 02:17:21 +0000473
Chris Lattner769dbdf2009-03-27 20:18:19 +0000474unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
475 switch (DeclKind) {
John McCall9488ea12009-11-17 05:59:44 +0000476 case Function:
477 case CXXMethod:
478 case CXXConstructor:
479 case CXXDestructor:
480 case CXXConversion:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000481 case EnumConstant:
482 case Var:
483 case ImplicitParam:
484 case ParmVar:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000485 case NonTypeTemplateParm:
486 case ObjCMethod:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000487 case ObjCProperty:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000488 return IDNS_Ordinary;
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000489 case Label:
490 return IDNS_Label;
Francois Pichet87c2e122010-11-21 06:08:52 +0000491 case IndirectField:
492 return IDNS_Ordinary | IDNS_Member;
493
John McCall0d6b1642010-04-23 18:46:30 +0000494 case ObjCCompatibleAlias:
495 case ObjCInterface:
496 return IDNS_Ordinary | IDNS_Type;
497
498 case Typedef:
Richard Smith162e1c12011-04-15 14:24:37 +0000499 case TypeAlias:
Richard Smith3e4c6c42011-05-05 21:57:07 +0000500 case TypeAliasTemplate:
John McCall0d6b1642010-04-23 18:46:30 +0000501 case UnresolvedUsingTypename:
502 case TemplateTypeParm:
503 return IDNS_Ordinary | IDNS_Type;
504
John McCall9488ea12009-11-17 05:59:44 +0000505 case UsingShadow:
506 return 0; // we'll actually overwrite this later
507
John McCall7ba107a2009-11-18 02:36:19 +0000508 case UnresolvedUsingValue:
John McCall7ba107a2009-11-18 02:36:19 +0000509 return IDNS_Ordinary | IDNS_Using;
John McCall9488ea12009-11-17 05:59:44 +0000510
511 case Using:
512 return IDNS_Using;
513
Chris Lattner769dbdf2009-03-27 20:18:19 +0000514 case ObjCProtocol:
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000515 return IDNS_ObjCProtocol;
Mike Stump1eb44332009-09-09 15:08:12 +0000516
Chris Lattner769dbdf2009-03-27 20:18:19 +0000517 case Field:
518 case ObjCAtDefsField:
519 case ObjCIvar:
520 return IDNS_Member;
Mike Stump1eb44332009-09-09 15:08:12 +0000521
Chris Lattner769dbdf2009-03-27 20:18:19 +0000522 case Record:
523 case CXXRecord:
524 case Enum:
John McCall0d6b1642010-04-23 18:46:30 +0000525 return IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Chris Lattner769dbdf2009-03-27 20:18:19 +0000527 case Namespace:
John McCall0d6b1642010-04-23 18:46:30 +0000528 case NamespaceAlias:
529 return IDNS_Namespace;
530
Chris Lattner769dbdf2009-03-27 20:18:19 +0000531 case FunctionTemplate:
John McCall0d6b1642010-04-23 18:46:30 +0000532 return IDNS_Ordinary;
533
Chris Lattner769dbdf2009-03-27 20:18:19 +0000534 case ClassTemplate:
535 case TemplateTemplateParm:
John McCall0d6b1642010-04-23 18:46:30 +0000536 return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000537
Chris Lattner769dbdf2009-03-27 20:18:19 +0000538 // Never have names.
John McCall02cace72009-08-28 07:59:38 +0000539 case Friend:
John McCalldd4a3b02009-09-16 22:47:08 +0000540 case FriendTemplate:
Abramo Bagnara6206d532010-06-05 05:09:32 +0000541 case AccessSpec:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000542 case LinkageSpec:
543 case FileScopeAsm:
544 case StaticAssert:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000545 case ObjCPropertyImpl:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000546 case Block:
547 case TranslationUnit:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000548
Chris Lattner769dbdf2009-03-27 20:18:19 +0000549 case UsingDirective:
550 case ClassTemplateSpecialization:
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000551 case ClassTemplatePartialSpecialization:
Francois Pichetaf0f4d02011-08-14 03:52:19 +0000552 case ClassScopeFunctionSpecialization:
Douglas Gregorbd4187b2010-04-22 23:19:50 +0000553 case ObjCImplementation:
554 case ObjCCategory:
555 case ObjCCategoryImpl:
Douglas Gregor15de72c2011-12-02 23:23:56 +0000556 case Import:
Douglas Gregorbd4187b2010-04-22 23:19:50 +0000557 // Never looked up by name.
Chris Lattner769dbdf2009-03-27 20:18:19 +0000558 return 0;
559 }
John McCall9488ea12009-11-17 05:59:44 +0000560
David Blaikie30263482012-01-20 21:50:17 +0000561 llvm_unreachable("Invalid DeclKind!");
Eli Friedman56d29372008-06-07 16:52:53 +0000562}
563
Argyrios Kyrtzidis4bbb8502012-02-09 02:44:08 +0000564void Decl::setAttrsImpl(const AttrVec &attrs, ASTContext &Ctx) {
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000565 assert(!HasAttrs && "Decl already contains attrs.");
566
Argyrios Kyrtzidis4bbb8502012-02-09 02:44:08 +0000567 AttrVec &AttrBlank = Ctx.getDeclAttrs(this);
Sean Huntcf807c42010-08-18 23:23:40 +0000568 assert(AttrBlank.empty() && "HasAttrs was wrong?");
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000569
570 AttrBlank = attrs;
571 HasAttrs = true;
572}
573
Sean Huntcf807c42010-08-18 23:23:40 +0000574void Decl::dropAttrs() {
Eli Friedman56d29372008-06-07 16:52:53 +0000575 if (!HasAttrs) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000576
Eli Friedman56d29372008-06-07 16:52:53 +0000577 HasAttrs = false;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000578 getASTContext().eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000579}
580
Sean Huntcf807c42010-08-18 23:23:40 +0000581const AttrVec &Decl::getAttrs() const {
582 assert(HasAttrs && "No attrs to get!");
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000583 return getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000584}
585
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000586void Decl::swapAttrs(Decl *RHS) {
Eli Friedman56d29372008-06-07 16:52:53 +0000587 bool HasLHSAttr = this->HasAttrs;
588 bool HasRHSAttr = RHS->HasAttrs;
Mike Stump1eb44332009-09-09 15:08:12 +0000589
Eli Friedman56d29372008-06-07 16:52:53 +0000590 // Usually, neither decl has attrs, nothing to do.
591 if (!HasLHSAttr && !HasRHSAttr) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000592
Eli Friedman56d29372008-06-07 16:52:53 +0000593 // If 'this' has no attrs, swap the other way.
594 if (!HasLHSAttr)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000595 return RHS->swapAttrs(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000596
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000597 ASTContext &Context = getASTContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000598
Eli Friedman56d29372008-06-07 16:52:53 +0000599 // Handle the case when both decls have attrs.
600 if (HasRHSAttr) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000601 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman56d29372008-06-07 16:52:53 +0000602 return;
603 }
Mike Stump1eb44332009-09-09 15:08:12 +0000604
Eli Friedman56d29372008-06-07 16:52:53 +0000605 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor68584ed2009-06-18 16:11:24 +0000606 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
607 Context.eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000608 this->HasAttrs = false;
609 RHS->HasAttrs = true;
610}
611
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000612Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000613 Decl::Kind DK = D->getDeclKind();
614 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000615#define DECL(NAME, BASE)
616#define DECL_CONTEXT(NAME) \
617 case Decl::NAME: \
618 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
619#define DECL_CONTEXT_BASE(NAME)
620#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000621 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000622#define DECL(NAME, BASE)
623#define DECL_CONTEXT_BASE(NAME) \
624 if (DK >= first##NAME && DK <= last##NAME) \
625 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
626#include "clang/AST/DeclNodes.inc"
David Blaikieb219cfc2011-09-23 05:06:16 +0000627 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000628 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000629}
630
631DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000632 Decl::Kind DK = D->getKind();
633 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000634#define DECL(NAME, BASE)
635#define DECL_CONTEXT(NAME) \
636 case Decl::NAME: \
637 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
638#define DECL_CONTEXT_BASE(NAME)
639#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000640 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000641#define DECL(NAME, BASE)
642#define DECL_CONTEXT_BASE(NAME) \
643 if (DK >= first##NAME && DK <= last##NAME) \
644 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
645#include "clang/AST/DeclNodes.inc"
David Blaikieb219cfc2011-09-23 05:06:16 +0000646 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000647 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000648}
649
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000650SourceLocation Decl::getBodyRBrace() const {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000651 // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
652 // FunctionDecl stores EndRangeLoc for this purpose.
653 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
654 const FunctionDecl *Definition;
655 if (FD->hasBody(Definition))
656 return Definition->getSourceRange().getEnd();
657 return SourceLocation();
658 }
659
Argyrios Kyrtzidis6717ef42010-07-07 11:31:27 +0000660 if (Stmt *Body = getBody())
661 return Body->getSourceRange().getEnd();
662
663 return SourceLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +0000664}
665
Anders Carlsson1329c272009-03-25 23:38:06 +0000666void Decl::CheckAccessDeclContext() const {
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000667#ifndef NDEBUG
John McCall46460a62010-01-20 21:53:11 +0000668 // Suppress this check if any of the following hold:
669 // 1. this is the translation unit (and thus has no parent)
670 // 2. this is a template parameter (and thus doesn't belong to its context)
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000671 // 3. this is a non-type template parameter
672 // 4. the context is not a record
673 // 5. it's invalid
674 // 6. it's a C++0x static_assert.
Anders Carlsson35eda442009-08-29 20:47:47 +0000675 if (isa<TranslationUnitDecl>(this) ||
Argyrios Kyrtzidis04aed0e2010-07-02 11:55:44 +0000676 isa<TemplateTypeParmDecl>(this) ||
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000677 isa<NonTypeTemplateParmDecl>(this) ||
Douglas Gregorfdd8ab12010-02-22 17:53:38 +0000678 !isa<CXXRecordDecl>(getDeclContext()) ||
Argyrios Kyrtzidis65b63ec2010-09-08 21:32:35 +0000679 isInvalidDecl() ||
680 isa<StaticAssertDecl>(this) ||
681 // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
682 // as DeclContext (?).
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000683 isa<ParmVarDecl>(this) ||
684 // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
685 // AS_none as access specifier.
Francois Pichetbc845322011-08-17 01:06:54 +0000686 isa<CXXRecordDecl>(this) ||
687 isa<ClassScopeFunctionSpecializationDecl>(this))
Anders Carlsson35eda442009-08-29 20:47:47 +0000688 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000689
690 assert(Access != AS_none &&
Anders Carlsson1329c272009-03-25 23:38:06 +0000691 "Access specifier is AS_none inside a record decl");
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000692#endif
Anders Carlsson1329c272009-03-25 23:38:06 +0000693}
694
John McCallaab9e312011-02-22 22:25:23 +0000695DeclContext *Decl::getNonClosureContext() {
John McCall4b9c2d22011-11-06 09:01:30 +0000696 return getDeclContext()->getNonClosureAncestor();
697}
698
699DeclContext *DeclContext::getNonClosureAncestor() {
700 DeclContext *DC = this;
John McCallaab9e312011-02-22 22:25:23 +0000701
702 // This is basically "while (DC->isClosure()) DC = DC->getParent();"
703 // except that it's significantly more efficient to cast to a known
704 // decl type and call getDeclContext() than to call getParent().
John McCall7b3f8532011-06-23 21:18:31 +0000705 while (isa<BlockDecl>(DC))
706 DC = cast<BlockDecl>(DC)->getDeclContext();
John McCallaab9e312011-02-22 22:25:23 +0000707
708 assert(!DC->isClosure());
709 return DC;
710}
Anders Carlsson1329c272009-03-25 23:38:06 +0000711
Eli Friedman56d29372008-06-07 16:52:53 +0000712//===----------------------------------------------------------------------===//
713// DeclContext Implementation
714//===----------------------------------------------------------------------===//
715
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000716bool DeclContext::classof(const Decl *D) {
717 switch (D->getKind()) {
Sean Hunt9a555912010-05-30 07:21:58 +0000718#define DECL(NAME, BASE)
719#define DECL_CONTEXT(NAME) case Decl::NAME:
720#define DECL_CONTEXT_BASE(NAME)
721#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000722 return true;
723 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000724#define DECL(NAME, BASE)
725#define DECL_CONTEXT_BASE(NAME) \
726 if (D->getKind() >= Decl::first##NAME && \
727 D->getKind() <= Decl::last##NAME) \
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000728 return true;
Sean Hunt9a555912010-05-30 07:21:58 +0000729#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000730 return false;
731 }
732}
733
Douglas Gregora2da7802010-07-25 18:38:02 +0000734DeclContext::~DeclContext() { }
Douglas Gregor44b43212008-12-11 16:49:14 +0000735
Douglas Gregore942bbe2009-09-10 16:57:35 +0000736/// \brief Find the parent context of this context that will be
737/// used for unqualified name lookup.
738///
739/// Generally, the parent lookup context is the semantic context. However, for
740/// a friend function the parent lookup context is the lexical context, which
741/// is the class in which the friend is declared.
742DeclContext *DeclContext::getLookupParent() {
743 // FIXME: Find a better way to identify friends
744 if (isa<FunctionDecl>(this))
Sebastian Redl7a126a42010-08-31 00:36:30 +0000745 if (getParent()->getRedeclContext()->isFileContext() &&
746 getLexicalParent()->getRedeclContext()->isRecord())
Douglas Gregore942bbe2009-09-10 16:57:35 +0000747 return getLexicalParent();
748
749 return getParent();
750}
751
Sebastian Redl410c4f22010-08-31 20:53:31 +0000752bool DeclContext::isInlineNamespace() const {
753 return isNamespace() &&
754 cast<NamespaceDecl>(this)->isInline();
755}
756
Douglas Gregorbc221632009-05-28 16:34:51 +0000757bool DeclContext::isDependentContext() const {
758 if (isFileContext())
759 return false;
760
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000761 if (isa<ClassTemplatePartialSpecializationDecl>(this))
762 return true;
763
Douglas Gregorf4b7de12012-02-21 19:11:17 +0000764 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this)) {
Douglas Gregorbc221632009-05-28 16:34:51 +0000765 if (Record->getDescribedClassTemplate())
766 return true;
Douglas Gregorf4b7de12012-02-21 19:11:17 +0000767
768 if (Record->isDependentLambda())
769 return true;
770 }
771
John McCall0c01d182010-03-24 05:22:00 +0000772 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregorbc221632009-05-28 16:34:51 +0000773 if (Function->getDescribedFunctionTemplate())
774 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000775
John McCall0c01d182010-03-24 05:22:00 +0000776 // Friend function declarations are dependent if their *lexical*
777 // context is dependent.
778 if (cast<Decl>(this)->getFriendObjectKind())
779 return getLexicalParent()->isDependentContext();
780 }
781
Douglas Gregorbc221632009-05-28 16:34:51 +0000782 return getParent() && getParent()->isDependentContext();
783}
784
Douglas Gregor074149e2009-01-05 19:45:36 +0000785bool DeclContext::isTransparentContext() const {
786 if (DeclKind == Decl::Enum)
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000787 return !cast<EnumDecl>(this)->isScoped();
Douglas Gregor074149e2009-01-05 19:45:36 +0000788 else if (DeclKind == Decl::LinkageSpec)
789 return true;
Douglas Gregor074149e2009-01-05 19:45:36 +0000790
791 return false;
792}
793
John McCallac65c622010-10-26 04:59:26 +0000794bool DeclContext::isExternCContext() const {
795 const DeclContext *DC = this;
796 while (DC->DeclKind != Decl::TranslationUnit) {
797 if (DC->DeclKind == Decl::LinkageSpec)
798 return cast<LinkageSpecDecl>(DC)->getLanguage()
799 == LinkageSpecDecl::lang_c;
800 DC = DC->getParent();
801 }
802 return false;
803}
804
Sebastian Redl7a126a42010-08-31 00:36:30 +0000805bool DeclContext::Encloses(const DeclContext *DC) const {
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000806 if (getPrimaryContext() != this)
807 return getPrimaryContext()->Encloses(DC);
Mike Stump1eb44332009-09-09 15:08:12 +0000808
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000809 for (; DC; DC = DC->getParent())
810 if (DC->getPrimaryContext() == this)
811 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000812 return false;
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000813}
814
Steve Naroff0701bbb2009-01-08 17:28:14 +0000815DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000816 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000817 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000818 case Decl::LinkageSpec:
Mike Stump1eb44332009-09-09 15:08:12 +0000819 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000820 // There is only one DeclContext for these entities.
821 return this;
822
823 case Decl::Namespace:
824 // The original namespace is our primary context.
825 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
826
Douglas Gregor44b43212008-12-11 16:49:14 +0000827 case Decl::ObjCMethod:
828 return this;
829
830 case Decl::ObjCInterface:
Douglas Gregor53df7a12011-12-15 18:03:09 +0000831 if (ObjCInterfaceDecl *Def = cast<ObjCInterfaceDecl>(this)->getDefinition())
832 return Def;
833
834 return this;
835
Steve Naroff0701bbb2009-01-08 17:28:14 +0000836 case Decl::ObjCProtocol:
Douglas Gregor1d784b22012-01-01 19:51:50 +0000837 if (ObjCProtocolDecl *Def = cast<ObjCProtocolDecl>(this)->getDefinition())
838 return Def;
839
840 return this;
Douglas Gregor53df7a12011-12-15 18:03:09 +0000841
Steve Naroff0701bbb2009-01-08 17:28:14 +0000842 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000843 return this;
844
Steve Naroff0701bbb2009-01-08 17:28:14 +0000845 case Decl::ObjCImplementation:
846 case Decl::ObjCCategoryImpl:
847 return this;
848
Douglas Gregor44b43212008-12-11 16:49:14 +0000849 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000850 if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000851 // If this is a tag type that has a definition or is currently
852 // being defined, that definition is our primary context.
John McCall3cb0ebd2010-03-10 03:28:59 +0000853 TagDecl *Tag = cast<TagDecl>(this);
854 assert(isa<TagType>(Tag->TypeForDecl) ||
855 isa<InjectedClassNameType>(Tag->TypeForDecl));
856
857 if (TagDecl *Def = Tag->getDefinition())
858 return Def;
859
860 if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) {
861 const TagType *TagTy = cast<TagType>(Tag->TypeForDecl);
862 if (TagTy->isBeingDefined())
863 // FIXME: is it necessarily being defined in the decl
864 // that owns the type?
865 return TagTy->getDecl();
866 }
867
868 return Tag;
Douglas Gregorcc636682009-02-17 23:15:12 +0000869 }
870
Sean Hunt9a555912010-05-30 07:21:58 +0000871 assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
Douglas Gregor44b43212008-12-11 16:49:14 +0000872 "Unknown DeclContext kind");
873 return this;
874 }
875}
876
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +0000877void
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000878DeclContext::collectAllContexts(SmallVectorImpl<DeclContext *> &Contexts){
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +0000879 Contexts.clear();
880
881 if (DeclKind != Decl::Namespace) {
882 Contexts.push_back(this);
883 return;
Douglas Gregor44b43212008-12-11 16:49:14 +0000884 }
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +0000885
886 NamespaceDecl *Self = static_cast<NamespaceDecl *>(this);
Douglas Gregoref96ee02012-01-14 16:38:05 +0000887 for (NamespaceDecl *N = Self->getMostRecentDecl(); N;
888 N = N->getPreviousDecl())
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +0000889 Contexts.push_back(N);
890
891 std::reverse(Contexts.begin(), Contexts.end());
Douglas Gregor44b43212008-12-11 16:49:14 +0000892}
893
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000894std::pair<Decl *, Decl *>
Bill Wendling341785e2012-02-22 09:51:33 +0000895DeclContext::BuildDeclChain(ArrayRef<Decl*> Decls,
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +0000896 bool FieldsAlreadyLoaded) {
Douglas Gregor46cd2182012-01-06 16:59:53 +0000897 // Build up a chain of declarations via the Decl::NextInContextAndBits field.
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000898 Decl *FirstNewDecl = 0;
899 Decl *PrevDecl = 0;
900 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +0000901 if (FieldsAlreadyLoaded && isa<FieldDecl>(Decls[I]))
902 continue;
903
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000904 Decl *D = Decls[I];
905 if (PrevDecl)
Douglas Gregor46cd2182012-01-06 16:59:53 +0000906 PrevDecl->NextInContextAndBits.setPointer(D);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000907 else
908 FirstNewDecl = D;
909
910 PrevDecl = D;
911 }
912
913 return std::make_pair(FirstNewDecl, PrevDecl);
914}
915
Richard Smithbbcd0f32013-02-07 03:37:08 +0000916/// \brief We have just acquired external visible storage, and we already have
917/// built a lookup map. For every name in the map, pull in the new names from
918/// the external storage.
919void DeclContext::reconcileExternalVisibleStorage() {
920 assert(NeedToReconcileExternalVisibleStorage);
921 if (!LookupPtr.getPointer())
922 return;
923
924 NeedToReconcileExternalVisibleStorage = false;
925
926 StoredDeclsMap &Map = *LookupPtr.getPointer();
927 ExternalASTSource *Source = getParentASTContext().getExternalSource();
928 for (StoredDeclsMap::iterator I = Map.begin(); I != Map.end(); ++I) {
929 I->second.removeExternalDecls();
930 Source->FindExternalVisibleDeclsByName(this, I->first);
931 }
932}
933
Douglas Gregor2cf26342009-04-09 22:27:44 +0000934/// \brief Load the declarations within this lexical storage from an
935/// external source.
Mike Stump1eb44332009-09-09 15:08:12 +0000936void
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000937DeclContext::LoadLexicalDeclsFromExternalStorage() const {
938 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000939 assert(hasExternalLexicalStorage() && Source && "No external storage?");
940
Argyrios Kyrtzidis0dbbc042010-07-30 10:03:23 +0000941 // Notify that we have a DeclContext that is initializing.
942 ExternalASTSource::Deserializing ADeclContext(Source);
Douglas Gregor9fc18c92011-08-26 21:23:06 +0000943
Douglas Gregorba6ffaf2011-07-15 21:46:17 +0000944 // Load the external declarations, if any.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000945 SmallVector<Decl*, 64> Decls;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000946 ExternalLexicalStorage = false;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +0000947 switch (Source->FindExternalLexicalDecls(this, Decls)) {
948 case ELR_Success:
949 break;
950
951 case ELR_Failure:
952 case ELR_AlreadyLoaded:
953 return;
954 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000955
956 if (Decls.empty())
957 return;
958
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +0000959 // We may have already loaded just the fields of this record, in which case
960 // we need to ignore them.
961 bool FieldsAlreadyLoaded = false;
962 if (const RecordDecl *RD = dyn_cast<RecordDecl>(this))
963 FieldsAlreadyLoaded = RD->LoadedFieldsFromExternalStorage;
964
Douglas Gregor2cf26342009-04-09 22:27:44 +0000965 // Splice the newly-read declarations into the beginning of the list
966 // of declarations.
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000967 Decl *ExternalFirst, *ExternalLast;
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +0000968 llvm::tie(ExternalFirst, ExternalLast) = BuildDeclChain(Decls,
969 FieldsAlreadyLoaded);
Douglas Gregor46cd2182012-01-06 16:59:53 +0000970 ExternalLast->NextInContextAndBits.setPointer(FirstDecl);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000971 FirstDecl = ExternalFirst;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000972 if (!LastDecl)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000973 LastDecl = ExternalLast;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000974}
975
John McCall76bd1f32010-06-01 09:23:16 +0000976DeclContext::lookup_result
977ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
978 DeclarationName Name) {
979 ASTContext &Context = DC->getParentASTContext();
980 StoredDeclsMap *Map;
Richard Smithc5d3e802012-03-16 06:12:59 +0000981 if (!(Map = DC->LookupPtr.getPointer()))
John McCall76bd1f32010-06-01 09:23:16 +0000982 Map = DC->CreateStoredDeclsMap(Context);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000983
Richard Smithbbcd0f32013-02-07 03:37:08 +0000984 // Add an entry to the map for this name, if it's not already present.
985 (*Map)[Name];
Douglas Gregor2cf26342009-04-09 22:27:44 +0000986
John McCall76bd1f32010-06-01 09:23:16 +0000987 return DeclContext::lookup_result();
988}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000989
John McCall76bd1f32010-06-01 09:23:16 +0000990DeclContext::lookup_result
991ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +0000992 DeclarationName Name,
Argyrios Kyrtzidis45df9c62011-09-09 06:44:14 +0000993 ArrayRef<NamedDecl*> Decls) {
Dmitri Gribenko1ad23d62012-09-10 21:20:09 +0000994 ASTContext &Context = DC->getParentASTContext();
John McCall76bd1f32010-06-01 09:23:16 +0000995 StoredDeclsMap *Map;
Richard Smithc5d3e802012-03-16 06:12:59 +0000996 if (!(Map = DC->LookupPtr.getPointer()))
John McCall76bd1f32010-06-01 09:23:16 +0000997 Map = DC->CreateStoredDeclsMap(Context);
998
999 StoredDeclsList &List = (*Map)[Name];
Argyrios Kyrtzidis45df9c62011-09-09 06:44:14 +00001000 for (ArrayRef<NamedDecl*>::iterator
1001 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
John McCall76bd1f32010-06-01 09:23:16 +00001002 if (List.isNull())
Argyrios Kyrtzidis45df9c62011-09-09 06:44:14 +00001003 List.setOnlyValue(*I);
John McCall76bd1f32010-06-01 09:23:16 +00001004 else
Richard Smithbbcd0f32013-02-07 03:37:08 +00001005 // FIXME: Need declarationReplaces handling for redeclarations in modules.
Argyrios Kyrtzidis45df9c62011-09-09 06:44:14 +00001006 List.AddSubsequentDecl(*I);
John McCall76bd1f32010-06-01 09:23:16 +00001007 }
1008
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001009 return List.getLookupResult();
John McCall76bd1f32010-06-01 09:23:16 +00001010}
1011
Sebastian Redl681d7232010-07-27 00:17:23 +00001012DeclContext::decl_iterator DeclContext::noload_decls_begin() const {
1013 return decl_iterator(FirstDecl);
1014}
1015
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001016DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +00001017 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001018 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001019
Mike Stump1eb44332009-09-09 15:08:12 +00001020 return decl_iterator(FirstDecl);
Douglas Gregor6ab35242009-04-09 21:40:53 +00001021}
1022
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001023bool DeclContext::decls_empty() const {
Douglas Gregor8038d512009-04-10 17:25:41 +00001024 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001025 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor8038d512009-04-10 17:25:41 +00001026
1027 return !FirstDecl;
1028}
1029
John McCall9f54ad42009-12-10 09:41:52 +00001030void DeclContext::removeDecl(Decl *D) {
1031 assert(D->getLexicalDeclContext() == this &&
1032 "decl being removed from non-lexical context");
Douglas Gregor46cd2182012-01-06 16:59:53 +00001033 assert((D->NextInContextAndBits.getPointer() || D == LastDecl) &&
John McCall9f54ad42009-12-10 09:41:52 +00001034 "decl is not in decls list");
1035
1036 // Remove D from the decl chain. This is O(n) but hopefully rare.
1037 if (D == FirstDecl) {
1038 if (D == LastDecl)
1039 FirstDecl = LastDecl = 0;
1040 else
Douglas Gregor46cd2182012-01-06 16:59:53 +00001041 FirstDecl = D->NextInContextAndBits.getPointer();
John McCall9f54ad42009-12-10 09:41:52 +00001042 } else {
Douglas Gregor46cd2182012-01-06 16:59:53 +00001043 for (Decl *I = FirstDecl; true; I = I->NextInContextAndBits.getPointer()) {
John McCall9f54ad42009-12-10 09:41:52 +00001044 assert(I && "decl not found in linked list");
Douglas Gregor46cd2182012-01-06 16:59:53 +00001045 if (I->NextInContextAndBits.getPointer() == D) {
1046 I->NextInContextAndBits.setPointer(D->NextInContextAndBits.getPointer());
John McCall9f54ad42009-12-10 09:41:52 +00001047 if (D == LastDecl) LastDecl = I;
1048 break;
1049 }
1050 }
1051 }
1052
1053 // Mark that D is no longer in the decl chain.
Douglas Gregor46cd2182012-01-06 16:59:53 +00001054 D->NextInContextAndBits.setPointer(0);
John McCall9f54ad42009-12-10 09:41:52 +00001055
1056 // Remove D from the lookup table if necessary.
1057 if (isa<NamedDecl>(D)) {
1058 NamedDecl *ND = cast<NamedDecl>(D);
1059
Axel Naumann02368d02011-08-26 14:06:12 +00001060 // Remove only decls that have a name
1061 if (!ND->getDeclName()) return;
1062
Richard Smithc5d3e802012-03-16 06:12:59 +00001063 StoredDeclsMap *Map = getPrimaryContext()->LookupPtr.getPointer();
John McCall0c01d182010-03-24 05:22:00 +00001064 if (!Map) return;
John McCall9f54ad42009-12-10 09:41:52 +00001065
John McCall9f54ad42009-12-10 09:41:52 +00001066 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
1067 assert(Pos != Map->end() && "no lookup entry for decl");
Axel Naumannd9d137e2011-11-08 18:21:06 +00001068 if (Pos->second.getAsVector() || Pos->second.getAsDecl() == ND)
1069 Pos->second.remove(ND);
John McCall9f54ad42009-12-10 09:41:52 +00001070 }
1071}
1072
John McCall3f9a8a62009-08-11 06:59:38 +00001073void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner7f0be132009-02-20 00:56:18 +00001074 assert(D->getLexicalDeclContext() == this &&
1075 "Decl inserted into wrong lexical context");
Mike Stump1eb44332009-09-09 15:08:12 +00001076 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001077 "Decl already inserted into a DeclContext");
1078
1079 if (FirstDecl) {
Douglas Gregor46cd2182012-01-06 16:59:53 +00001080 LastDecl->NextInContextAndBits.setPointer(D);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001081 LastDecl = D;
1082 } else {
1083 FirstDecl = LastDecl = D;
1084 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +00001085
1086 // Notify a C++ record declaration that we've added a member, so it can
1087 // update it's class-specific state.
1088 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
1089 Record->addedMember(D);
Douglas Gregore6649772011-12-03 00:30:27 +00001090
1091 // If this is a newly-created (not de-serialized) import declaration, wire
1092 // it in to the list of local import declarations.
1093 if (!D->isFromASTFile()) {
1094 if (ImportDecl *Import = dyn_cast<ImportDecl>(D))
1095 D->getASTContext().addedLocalImportDecl(Import);
1096 }
John McCall3f9a8a62009-08-11 06:59:38 +00001097}
1098
1099void DeclContext::addDecl(Decl *D) {
1100 addHiddenDecl(D);
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001101
1102 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Richard Smithc5d3e802012-03-16 06:12:59 +00001103 ND->getDeclContext()->getPrimaryContext()->
1104 makeDeclVisibleInContextWithFlags(ND, false, true);
Douglas Gregor44b43212008-12-11 16:49:14 +00001105}
1106
Sean Callanan9faf8102011-10-21 02:57:43 +00001107void DeclContext::addDeclInternal(Decl *D) {
1108 addHiddenDecl(D);
1109
1110 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Richard Smithc5d3e802012-03-16 06:12:59 +00001111 ND->getDeclContext()->getPrimaryContext()->
1112 makeDeclVisibleInContextWithFlags(ND, true, true);
1113}
1114
1115/// shouldBeHidden - Determine whether a declaration which was declared
1116/// within its semantic context should be invisible to qualified name lookup.
1117static bool shouldBeHidden(NamedDecl *D) {
1118 // Skip unnamed declarations.
1119 if (!D->getDeclName())
1120 return true;
1121
1122 // Skip entities that can't be found by name lookup into a particular
1123 // context.
1124 if ((D->getIdentifierNamespace() == 0 && !isa<UsingDirectiveDecl>(D)) ||
1125 D->isTemplateParameter())
1126 return true;
1127
1128 // Skip template specializations.
1129 // FIXME: This feels like a hack. Should DeclarationName support
1130 // template-ids, or is there a better way to keep specializations
1131 // from being visible?
1132 if (isa<ClassTemplateSpecializationDecl>(D))
1133 return true;
1134 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1135 if (FD->isFunctionTemplateSpecialization())
1136 return true;
1137
1138 return false;
1139}
1140
1141/// buildLookup - Build the lookup data structure with all of the
1142/// declarations in this DeclContext (and any other contexts linked
1143/// to it or transparent contexts nested within it) and return it.
1144StoredDeclsMap *DeclContext::buildLookup() {
1145 assert(this == getPrimaryContext() && "buildLookup called on non-primary DC");
1146
1147 if (!LookupPtr.getInt())
1148 return LookupPtr.getPointer();
1149
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001150 SmallVector<DeclContext *, 2> Contexts;
Richard Smithc5d3e802012-03-16 06:12:59 +00001151 collectAllContexts(Contexts);
1152 for (unsigned I = 0, N = Contexts.size(); I != N; ++I)
1153 buildLookupImpl(Contexts[I]);
1154
1155 // We no longer have any lazy decls.
1156 LookupPtr.setInt(false);
1157 return LookupPtr.getPointer();
1158}
1159
1160/// buildLookupImpl - Build part of the lookup data structure for the
1161/// declarations contained within DCtx, which will either be this
1162/// DeclContext, a DeclContext linked to it, or a transparent context
1163/// nested within it.
1164void DeclContext::buildLookupImpl(DeclContext *DCtx) {
Richard Smithbbcd0f32013-02-07 03:37:08 +00001165 // FIXME: If buildLookup is supposed to return a complete map, we should not
1166 // bail out in buildLookup if hasExternalVisibleStorage. If it is not required
1167 // to include names from PCH and modules, we should use the noload_ iterators
1168 // here.
Richard Smithc5d3e802012-03-16 06:12:59 +00001169 for (decl_iterator I = DCtx->decls_begin(), E = DCtx->decls_end();
1170 I != E; ++I) {
1171 Decl *D = *I;
1172
1173 // Insert this declaration into the lookup structure, but only if
1174 // it's semantically within its decl context. Any other decls which
1175 // should be found in this context are added eagerly.
1176 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1177 if (ND->getDeclContext() == DCtx && !shouldBeHidden(ND))
1178 makeDeclVisibleInContextImpl(ND, false);
1179
1180 // If this declaration is itself a transparent declaration context
1181 // or inline namespace, add the members of this declaration of that
1182 // context (recursively).
1183 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(D))
1184 if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
1185 buildLookupImpl(InnerCtx);
1186 }
Sean Callanan9faf8102011-10-21 02:57:43 +00001187}
1188
Mike Stump1eb44332009-09-09 15:08:12 +00001189DeclContext::lookup_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001190DeclContext::lookup(DeclarationName Name) {
Nick Lewycky65daef12012-03-13 04:12:34 +00001191 assert(DeclKind != Decl::LinkageSpec &&
1192 "Should not perform lookups into linkage specs!");
1193
Steve Naroff0701bbb2009-01-08 17:28:14 +00001194 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +00001195 if (PrimaryContext != this)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001196 return PrimaryContext->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +00001197
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001198 if (hasExternalVisibleStorage()) {
Richard Smithbbcd0f32013-02-07 03:37:08 +00001199 if (NeedToReconcileExternalVisibleStorage)
1200 reconcileExternalVisibleStorage();
1201
1202 StoredDeclsMap *Map = LookupPtr.getPointer();
1203 if (LookupPtr.getInt())
1204 Map = buildLookup();
1205
Richard Smith2bb07c12013-02-08 00:37:45 +00001206 if (!Map)
1207 Map = CreateStoredDeclsMap(getParentASTContext());
1208
Richard Smithbbcd0f32013-02-07 03:37:08 +00001209 // If a PCH/module has a result for this name, and we have a local
1210 // declaration, we will have imported the PCH/module result when adding the
1211 // local declaration or when reconciling the module.
Richard Smith2bb07c12013-02-08 00:37:45 +00001212 std::pair<StoredDeclsMap::iterator, bool> R =
1213 Map->insert(std::make_pair(Name, StoredDeclsList()));
1214 if (!R.second)
1215 return R.first->second.getLookupResult();
Richard Smithc5d3e802012-03-16 06:12:59 +00001216
John McCall76bd1f32010-06-01 09:23:16 +00001217 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Richard Smith3646c682013-02-07 03:30:24 +00001218 if (Source->FindExternalVisibleDeclsByName(this, Name)) {
1219 if (StoredDeclsMap *Map = LookupPtr.getPointer()) {
1220 StoredDeclsMap::iterator I = Map->find(Name);
1221 if (I != Map->end())
1222 return I->second.getLookupResult();
1223 }
1224 }
1225
1226 return lookup_result(lookup_iterator(0), lookup_iterator(0));
John McCall76bd1f32010-06-01 09:23:16 +00001227 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001228
Richard Smithc5d3e802012-03-16 06:12:59 +00001229 StoredDeclsMap *Map = LookupPtr.getPointer();
1230 if (LookupPtr.getInt())
1231 Map = buildLookup();
1232
1233 if (!Map)
1234 return lookup_result(lookup_iterator(0), lookup_iterator(0));
1235
1236 StoredDeclsMap::iterator I = Map->find(Name);
1237 if (I == Map->end())
1238 return lookup_result(lookup_iterator(0), lookup_iterator(0));
1239
1240 return I->second.getLookupResult();
Douglas Gregor44b43212008-12-11 16:49:14 +00001241}
1242
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001243void DeclContext::localUncachedLookup(DeclarationName Name,
1244 SmallVectorImpl<NamedDecl *> &Results) {
Douglas Gregorb75a3452011-10-15 00:10:27 +00001245 Results.clear();
1246
1247 // If there's no external storage, just perform a normal lookup and copy
1248 // the results.
Douglas Gregor93ed7cf2012-07-17 21:16:27 +00001249 if (!hasExternalVisibleStorage() && !hasExternalLexicalStorage() && Name) {
Douglas Gregorb75a3452011-10-15 00:10:27 +00001250 lookup_result LookupResults = lookup(Name);
David Blaikie3bc93e32012-12-19 00:45:41 +00001251 Results.insert(Results.end(), LookupResults.begin(), LookupResults.end());
Douglas Gregorb75a3452011-10-15 00:10:27 +00001252 return;
1253 }
1254
1255 // If we have a lookup table, check there first. Maybe we'll get lucky.
Richard Smithbbcd0f32013-02-07 03:37:08 +00001256 if (Name && !LookupPtr.getInt()) {
Douglas Gregor93ed7cf2012-07-17 21:16:27 +00001257 if (StoredDeclsMap *Map = LookupPtr.getPointer()) {
1258 StoredDeclsMap::iterator Pos = Map->find(Name);
1259 if (Pos != Map->end()) {
1260 Results.insert(Results.end(),
David Blaikie3bc93e32012-12-19 00:45:41 +00001261 Pos->second.getLookupResult().begin(),
1262 Pos->second.getLookupResult().end());
Douglas Gregor93ed7cf2012-07-17 21:16:27 +00001263 return;
1264 }
Douglas Gregorb75a3452011-10-15 00:10:27 +00001265 }
1266 }
Douglas Gregor93ed7cf2012-07-17 21:16:27 +00001267
Douglas Gregorb75a3452011-10-15 00:10:27 +00001268 // Slow case: grovel through the declarations in our chain looking for
1269 // matches.
1270 for (Decl *D = FirstDecl; D; D = D->getNextDeclInContext()) {
1271 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1272 if (ND->getDeclName() == Name)
1273 Results.push_back(ND);
1274 }
1275}
1276
Sebastian Redl7a126a42010-08-31 00:36:30 +00001277DeclContext *DeclContext::getRedeclContext() {
Chris Lattner0cf2b192009-03-27 19:19:59 +00001278 DeclContext *Ctx = this;
Sebastian Redl410c4f22010-08-31 20:53:31 +00001279 // Skip through transparent contexts.
1280 while (Ctx->isTransparentContext())
Douglas Gregorce356072009-01-06 23:51:29 +00001281 Ctx = Ctx->getParent();
1282 return Ctx;
1283}
1284
Douglas Gregor88b70942009-02-25 22:02:03 +00001285DeclContext *DeclContext::getEnclosingNamespaceContext() {
1286 DeclContext *Ctx = this;
1287 // Skip through non-namespace, non-translation-unit contexts.
Sebastian Redl51a8a372010-08-31 00:36:23 +00001288 while (!Ctx->isFileContext())
Douglas Gregor88b70942009-02-25 22:02:03 +00001289 Ctx = Ctx->getParent();
1290 return Ctx->getPrimaryContext();
1291}
1292
Sebastian Redl7a126a42010-08-31 00:36:30 +00001293bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
1294 // For non-file contexts, this is equivalent to Equals.
1295 if (!isFileContext())
1296 return O->Equals(this);
1297
1298 do {
1299 if (O->Equals(this))
1300 return true;
1301
1302 const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O);
1303 if (!NS || !NS->isInline())
1304 break;
1305 O = NS->getParent();
1306 } while (O);
1307
1308 return false;
1309}
1310
Richard Smithc5d3e802012-03-16 06:12:59 +00001311void DeclContext::makeDeclVisibleInContext(NamedDecl *D) {
1312 DeclContext *PrimaryDC = this->getPrimaryContext();
1313 DeclContext *DeclDC = D->getDeclContext()->getPrimaryContext();
1314 // If the decl is being added outside of its semantic decl context, we
1315 // need to ensure that we eagerly build the lookup information for it.
1316 PrimaryDC->makeDeclVisibleInContextWithFlags(D, false, PrimaryDC == DeclDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00001317}
1318
Richard Smithc5d3e802012-03-16 06:12:59 +00001319void DeclContext::makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal,
1320 bool Recoverable) {
1321 assert(this == getPrimaryContext() && "expected a primary DC");
Sean Callanan9faf8102011-10-21 02:57:43 +00001322
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001323 // Skip declarations within functions.
1324 // FIXME: We shouldn't need to build lookup tables for function declarations
1325 // ever, and we can't do so correctly because we can't model the nesting of
1326 // scopes which occurs within functions. We use "qualified" lookup into
1327 // function declarations when handling friend declarations inside nested
1328 // classes, and consequently accept the following invalid code:
1329 //
1330 // void f() { void g(); { int g; struct S { friend void g(); }; } }
1331 if (isFunctionOrMethod() && !isa<FunctionDecl>(D))
1332 return;
1333
Richard Smithc5d3e802012-03-16 06:12:59 +00001334 // Skip declarations which should be invisible to name lookup.
1335 if (shouldBeHidden(D))
1336 return;
1337
1338 // If we already have a lookup data structure, perform the insertion into
1339 // it. If we might have externally-stored decls with this name, look them
1340 // up and perform the insertion. If this decl was declared outside its
1341 // semantic context, buildLookup won't add it, so add it now.
1342 //
1343 // FIXME: As a performance hack, don't add such decls into the translation
1344 // unit unless we're in C++, since qualified lookup into the TU is never
1345 // performed.
1346 if (LookupPtr.getPointer() || hasExternalVisibleStorage() ||
1347 ((!Recoverable || D->getDeclContext() != D->getLexicalDeclContext()) &&
1348 (getParentASTContext().getLangOpts().CPlusPlus ||
1349 !isTranslationUnit()))) {
1350 // If we have lazily omitted any decls, they might have the same name as
1351 // the decl which we are adding, so build a full lookup table before adding
1352 // this decl.
1353 buildLookup();
1354 makeDeclVisibleInContextImpl(D, Internal);
1355 } else {
1356 LookupPtr.setInt(true);
1357 }
1358
1359 // If we are a transparent context or inline namespace, insert into our
1360 // parent context, too. This operation is recursive.
1361 if (isTransparentContext() || isInlineNamespace())
1362 getParent()->getPrimaryContext()->
1363 makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
1364
1365 Decl *DCAsDecl = cast<Decl>(this);
1366 // Notify that a decl was made visible unless we are a Tag being defined.
1367 if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined()))
1368 if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
1369 L->AddedVisibleDecl(this, D);
1370}
1371
1372void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal) {
1373 // Find or create the stored declaration map.
1374 StoredDeclsMap *Map = LookupPtr.getPointer();
1375 if (!Map) {
1376 ASTContext *C = &getParentASTContext();
1377 Map = CreateStoredDeclsMap(*C);
Argyrios Kyrtzidis5586b012010-07-04 21:44:25 +00001378 }
1379
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001380 // If there is an external AST source, load any declarations it knows about
1381 // with this declaration's name.
1382 // If the lookup table contains an entry about this name it means that we
1383 // have already checked the external source.
Sean Callanan9faf8102011-10-21 02:57:43 +00001384 if (!Internal)
1385 if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
1386 if (hasExternalVisibleStorage() &&
Richard Smithc5d3e802012-03-16 06:12:59 +00001387 Map->find(D->getDeclName()) == Map->end())
Sean Callanan9faf8102011-10-21 02:57:43 +00001388 Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001389
Douglas Gregor44b43212008-12-11 16:49:14 +00001390 // Insert this declaration into the map.
Richard Smithc5d3e802012-03-16 06:12:59 +00001391 StoredDeclsList &DeclNameEntries = (*Map)[D->getDeclName()];
Chris Lattner67762a32009-02-20 01:44:05 +00001392 if (DeclNameEntries.isNull()) {
1393 DeclNameEntries.setOnlyValue(D);
Richard Smithc5d3e802012-03-16 06:12:59 +00001394 return;
Douglas Gregor44b43212008-12-11 16:49:14 +00001395 }
Chris Lattner91942502009-02-20 00:55:03 +00001396
Richard Smithc5d3e802012-03-16 06:12:59 +00001397 if (DeclNameEntries.HandleRedeclaration(D)) {
1398 // This declaration has replaced an existing one for which
1399 // declarationReplaces returns true.
1400 return;
1401 }
Mike Stump1eb44332009-09-09 15:08:12 +00001402
Richard Smithc5d3e802012-03-16 06:12:59 +00001403 // Put this declaration into the appropriate slot.
1404 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor44b43212008-12-11 16:49:14 +00001405}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001406
1407/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
1408/// this context.
Mike Stump1eb44332009-09-09 15:08:12 +00001409DeclContext::udir_iterator_range
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001410DeclContext::getUsingDirectives() const {
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001411 // FIXME: Use something more efficient than normal lookup for using
1412 // directives. In C++, using directives are looked up more than anything else.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001413 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
David Blaikie3bc93e32012-12-19 00:45:41 +00001414 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.begin()),
1415 reinterpret_cast<udir_iterator>(Result.end()));
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001416}
Douglas Gregor2cf26342009-04-09 22:27:44 +00001417
Ted Kremenek3478eb62010-02-11 07:12:28 +00001418//===----------------------------------------------------------------------===//
1419// Creation and Destruction of StoredDeclsMaps. //
1420//===----------------------------------------------------------------------===//
1421
John McCall0c01d182010-03-24 05:22:00 +00001422StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
Richard Smithc5d3e802012-03-16 06:12:59 +00001423 assert(!LookupPtr.getPointer() && "context already has a decls map");
John McCall0c01d182010-03-24 05:22:00 +00001424 assert(getPrimaryContext() == this &&
1425 "creating decls map on non-primary context");
1426
1427 StoredDeclsMap *M;
1428 bool Dependent = isDependentContext();
1429 if (Dependent)
1430 M = new DependentStoredDeclsMap();
1431 else
1432 M = new StoredDeclsMap();
1433 M->Previous = C.LastSDM;
1434 C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
Richard Smithc5d3e802012-03-16 06:12:59 +00001435 LookupPtr.setPointer(M);
Ted Kremenek3478eb62010-02-11 07:12:28 +00001436 return M;
1437}
1438
1439void ASTContext::ReleaseDeclContextMaps() {
John McCall0c01d182010-03-24 05:22:00 +00001440 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1441 // pointer because the subclass doesn't add anything that needs to
1442 // be deleted.
John McCall0c01d182010-03-24 05:22:00 +00001443 StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1444}
1445
1446void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1447 while (Map) {
1448 // Advance the iteration before we invalidate memory.
1449 llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1450
1451 if (Dependent)
1452 delete static_cast<DependentStoredDeclsMap*>(Map);
1453 else
1454 delete Map;
1455
1456 Map = Next.getPointer();
1457 Dependent = Next.getInt();
1458 }
1459}
1460
John McCall0c01d182010-03-24 05:22:00 +00001461DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1462 DeclContext *Parent,
1463 const PartialDiagnostic &PDiag) {
1464 assert(Parent->isDependentContext()
1465 && "cannot iterate dependent diagnostics of non-dependent context");
1466 Parent = Parent->getPrimaryContext();
Richard Smithc5d3e802012-03-16 06:12:59 +00001467 if (!Parent->LookupPtr.getPointer())
John McCall0c01d182010-03-24 05:22:00 +00001468 Parent->CreateStoredDeclsMap(C);
1469
1470 DependentStoredDeclsMap *Map
Richard Smithc5d3e802012-03-16 06:12:59 +00001471 = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr.getPointer());
John McCall0c01d182010-03-24 05:22:00 +00001472
Douglas Gregorb8365182010-03-29 23:56:53 +00001473 // Allocate the copy of the PartialDiagnostic via the ASTContext's
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00001474 // BumpPtrAllocator, rather than the ASTContext itself.
Douglas Gregorb8365182010-03-29 23:56:53 +00001475 PartialDiagnostic::Storage *DiagStorage = 0;
1476 if (PDiag.hasStorage())
1477 DiagStorage = new (C) PartialDiagnostic::Storage;
1478
1479 DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
John McCall0c01d182010-03-24 05:22:00 +00001480
1481 // TODO: Maybe we shouldn't reverse the order during insertion.
1482 DD->NextDiagnostic = Map->FirstDiagnostic;
1483 Map->FirstDiagnostic = DD;
1484
1485 return DD;
Ted Kremenek3478eb62010-02-11 07:12:28 +00001486}