blob: bd6d99cd59eaf3d636bf461935ab94e4998d12eb [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"
Alexey Bataevc6400582013-03-22 06:34:35 +000023#include "clang/AST/DeclOpenMP.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000024#include "clang/AST/DeclTemplate.h"
John McCall0c01d182010-03-24 05:22:00 +000025#include "clang/AST/DependentDiagnostic.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000026#include "clang/AST/ExternalASTSource.h"
Sebastian Redld3a413d2009-04-26 20:35:05 +000027#include "clang/AST/Stmt.h"
28#include "clang/AST/StmtCXX.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000029#include "clang/AST/Type.h"
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000030#include "clang/Basic/TargetInfo.h"
Eli Friedman56d29372008-06-07 16:52:53 +000031#include "llvm/ADT/DenseMap.h"
Chris Lattner49f28ca2009-03-05 08:00:35 +000032#include "llvm/Support/raw_ostream.h"
Douglas Gregor6ed40e32008-12-23 21:05:05 +000033#include <algorithm>
Eli Friedman56d29372008-06-07 16:52:53 +000034using namespace clang;
35
36//===----------------------------------------------------------------------===//
37// Statistics
38//===----------------------------------------------------------------------===//
39
Sean Hunt9a555912010-05-30 07:21:58 +000040#define DECL(DERIVED, BASE) static int n##DERIVED##s = 0;
41#define ABSTRACT_DECL(DECL)
42#include "clang/AST/DeclNodes.inc"
Eli Friedman56d29372008-06-07 16:52:53 +000043
Douglas Gregor6bd99292013-02-09 01:35:03 +000044void Decl::updateOutOfDate(IdentifierInfo &II) const {
45 getASTContext().getExternalSource()->updateOutOfDateIdentifier(II);
46}
47
Douglas Gregor1e68ecc2012-01-05 21:55:30 +000048void *Decl::AllocateDeserializedDecl(const ASTContext &Context,
49 unsigned ID,
50 unsigned Size) {
Douglas Gregor5d1f4962012-01-05 23:49:36 +000051 // Allocate an extra 8 bytes worth of storage, which ensures that the
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +000052 // resulting pointer will still be 8-byte aligned.
Douglas Gregor5d1f4962012-01-05 23:49:36 +000053 void *Start = Context.Allocate(Size + 8);
54 void *Result = (char*)Start + 8;
Douglas Gregorb6b60c12012-01-05 22:27:05 +000055
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +000056 unsigned *PrefixPtr = (unsigned *)Result - 2;
57
58 // Zero out the first 4 bytes; this is used to store the owning module ID.
59 PrefixPtr[0] = 0;
60
61 // Store the global declaration ID in the second 4 bytes.
62 PrefixPtr[1] = ID;
Douglas Gregorb6b60c12012-01-05 22:27:05 +000063
64 return Result;
Douglas Gregor1e68ecc2012-01-05 21:55:30 +000065}
66
Douglas Gregorca2ab452013-01-12 01:29:50 +000067Module *Decl::getOwningModuleSlow() const {
68 assert(isFromASTFile() && "Not from AST file?");
69 return getASTContext().getExternalSource()->getModule(getOwningModuleID());
70}
71
Eli Friedman56d29372008-06-07 16:52:53 +000072const char *Decl::getDeclKindName() const {
73 switch (DeclKind) {
David Blaikieb219cfc2011-09-23 05:06:16 +000074 default: llvm_unreachable("Declaration not in DeclNodes.inc!");
Sean Hunt9a555912010-05-30 07:21:58 +000075#define DECL(DERIVED, BASE) case DERIVED: return #DERIVED;
76#define ABSTRACT_DECL(DECL)
77#include "clang/AST/DeclNodes.inc"
Eli Friedman56d29372008-06-07 16:52:53 +000078 }
79}
80
Douglas Gregor42738572010-03-05 00:26:45 +000081void Decl::setInvalidDecl(bool Invalid) {
82 InvalidDecl = Invalid;
Argyrios Kyrtzidisba50b3e2012-03-09 21:09:04 +000083 if (Invalid && !isa<ParmVarDecl>(this)) {
Douglas Gregor42738572010-03-05 00:26:45 +000084 // Defensive maneuver for ill-formed code: we're likely not to make it to
85 // a point where we set the access specifier, so default it to "public"
86 // to avoid triggering asserts elsewhere in the front end.
87 setAccess(AS_public);
88 }
89}
90
Steve Naroff0a473932009-01-20 19:53:53 +000091const char *DeclContext::getDeclKindName() const {
92 switch (DeclKind) {
David Blaikieb219cfc2011-09-23 05:06:16 +000093 default: llvm_unreachable("Declaration context not in DeclNodes.inc!");
Sean Hunt9a555912010-05-30 07:21:58 +000094#define DECL(DERIVED, BASE) case Decl::DERIVED: return #DERIVED;
95#define ABSTRACT_DECL(DECL)
96#include "clang/AST/DeclNodes.inc"
Steve Naroff0a473932009-01-20 19:53:53 +000097 }
98}
99
Daniel Dunbar02892a62012-03-05 21:42:49 +0000100bool Decl::StatisticsEnabled = false;
101void Decl::EnableStatistics() {
102 StatisticsEnabled = true;
Eli Friedman56d29372008-06-07 16:52:53 +0000103}
104
105void Decl::PrintStats() {
Chandler Carruthb43c8ec2011-07-04 06:13:27 +0000106 llvm::errs() << "\n*** Decl Stats:\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Douglas Gregor64650af2009-02-02 23:39:07 +0000108 int totalDecls = 0;
Sean Hunt9a555912010-05-30 07:21:58 +0000109#define DECL(DERIVED, BASE) totalDecls += n##DERIVED##s;
110#define ABSTRACT_DECL(DECL)
111#include "clang/AST/DeclNodes.inc"
Chandler Carruthb43c8ec2011-07-04 06:13:27 +0000112 llvm::errs() << " " << totalDecls << " decls total.\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Douglas Gregor64650af2009-02-02 23:39:07 +0000114 int totalBytes = 0;
Sean Hunt9a555912010-05-30 07:21:58 +0000115#define DECL(DERIVED, BASE) \
116 if (n##DERIVED##s > 0) { \
117 totalBytes += (int)(n##DERIVED##s * sizeof(DERIVED##Decl)); \
Chandler Carruthb43c8ec2011-07-04 06:13:27 +0000118 llvm::errs() << " " << n##DERIVED##s << " " #DERIVED " decls, " \
119 << sizeof(DERIVED##Decl) << " each (" \
120 << n##DERIVED##s * sizeof(DERIVED##Decl) \
121 << " bytes)\n"; \
Douglas Gregor64650af2009-02-02 23:39:07 +0000122 }
Sean Hunt9a555912010-05-30 07:21:58 +0000123#define ABSTRACT_DECL(DECL)
124#include "clang/AST/DeclNodes.inc"
Mike Stump1eb44332009-09-09 15:08:12 +0000125
Chandler Carruthb43c8ec2011-07-04 06:13:27 +0000126 llvm::errs() << "Total bytes = " << totalBytes << "\n";
Eli Friedman56d29372008-06-07 16:52:53 +0000127}
128
Sean Hunt9a555912010-05-30 07:21:58 +0000129void Decl::add(Kind k) {
Eli Friedman56d29372008-06-07 16:52:53 +0000130 switch (k) {
Sean Hunt9a555912010-05-30 07:21:58 +0000131#define DECL(DERIVED, BASE) case DERIVED: ++n##DERIVED##s; break;
132#define ABSTRACT_DECL(DECL)
133#include "clang/AST/DeclNodes.inc"
Eli Friedman56d29372008-06-07 16:52:53 +0000134 }
135}
136
Anders Carlsson67e33202009-06-13 00:08:58 +0000137bool Decl::isTemplateParameterPack() const {
138 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
139 return TTP->isParameterPack();
Douglas Gregor10738d32010-12-23 23:51:58 +0000140 if (const NonTypeTemplateParmDecl *NTTP
Douglas Gregor61c4d282011-01-05 15:48:55 +0000141 = dyn_cast<NonTypeTemplateParmDecl>(this))
Douglas Gregor10738d32010-12-23 23:51:58 +0000142 return NTTP->isParameterPack();
Douglas Gregor61c4d282011-01-05 15:48:55 +0000143 if (const TemplateTemplateParmDecl *TTP
144 = dyn_cast<TemplateTemplateParmDecl>(this))
145 return TTP->isParameterPack();
Anders Carlsson67e33202009-06-13 00:08:58 +0000146 return false;
147}
148
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000149bool Decl::isParameterPack() const {
150 if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(this))
151 return Parm->isParameterPack();
152
153 return isTemplateParameterPack();
154}
155
Douglas Gregore53060f2009-06-25 22:08:12 +0000156bool Decl::isFunctionOrFunctionTemplate() const {
John McCall9488ea12009-11-17 05:59:44 +0000157 if (const UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(this))
Anders Carlsson58badb72009-06-26 05:26:50 +0000158 return UD->getTargetDecl()->isFunctionOrFunctionTemplate();
Mike Stump1eb44332009-09-09 15:08:12 +0000159
Douglas Gregore53060f2009-06-25 22:08:12 +0000160 return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this);
161}
162
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000163bool Decl::isTemplateDecl() const {
164 return isa<TemplateDecl>(this);
165}
166
Argyrios Kyrtzidisc8680f42011-09-28 02:45:33 +0000167const DeclContext *Decl::getParentFunctionOrMethod() const {
168 for (const DeclContext *DC = getDeclContext();
169 DC && !DC->isTranslationUnit() && !DC->isNamespace();
Douglas Gregor79c22782010-01-16 20:21:20 +0000170 DC = DC->getParent())
171 if (DC->isFunctionOrMethod())
Argyrios Kyrtzidisc8680f42011-09-28 02:45:33 +0000172 return DC;
Douglas Gregor79c22782010-01-16 20:21:20 +0000173
Argyrios Kyrtzidisc8680f42011-09-28 02:45:33 +0000174 return 0;
Douglas Gregor79c22782010-01-16 20:21:20 +0000175}
176
Douglas Gregor4c3e0ee2011-02-17 08:47:29 +0000177
Eli Friedman56d29372008-06-07 16:52:53 +0000178//===----------------------------------------------------------------------===//
Chris Lattner49f28ca2009-03-05 08:00:35 +0000179// PrettyStackTraceDecl Implementation
180//===----------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000181
Chris Lattner5f9e2722011-07-23 10:55:15 +0000182void PrettyStackTraceDecl::print(raw_ostream &OS) const {
Chris Lattner49f28ca2009-03-05 08:00:35 +0000183 SourceLocation TheLoc = Loc;
184 if (TheLoc.isInvalid() && TheDecl)
185 TheLoc = TheDecl->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000186
Chris Lattner49f28ca2009-03-05 08:00:35 +0000187 if (TheLoc.isValid()) {
188 TheLoc.print(OS, SM);
189 OS << ": ";
190 }
191
192 OS << Message;
193
Benjamin Kramerb063ef02013-02-23 13:53:57 +0000194 if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl)) {
195 OS << " '";
196 DN->printQualifiedName(OS);
197 OS << '\'';
198 }
Chris Lattner49f28ca2009-03-05 08:00:35 +0000199 OS << '\n';
200}
Mike Stump1eb44332009-09-09 15:08:12 +0000201
Chris Lattner49f28ca2009-03-05 08:00:35 +0000202//===----------------------------------------------------------------------===//
Eli Friedman56d29372008-06-07 16:52:53 +0000203// Decl Implementation
204//===----------------------------------------------------------------------===//
205
Douglas Gregorda2142f2011-02-19 18:51:44 +0000206// Out-of-line virtual method providing a home for Decl.
207Decl::~Decl() { }
Douglas Gregorf4a03cc2011-02-17 07:02:32 +0000208
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000209void Decl::setDeclContext(DeclContext *DC) {
Chris Lattneree219fd2009-03-29 06:06:59 +0000210 DeclCtx = DC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000211}
212
213void Decl::setLexicalDeclContext(DeclContext *DC) {
214 if (DC == getLexicalDeclContext())
215 return;
216
217 if (isInSemaDC()) {
Argyrios Kyrtzidis4bbb8502012-02-09 02:44:08 +0000218 setDeclContextsImpl(getDeclContext(), DC, getASTContext());
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000219 } else {
220 getMultipleDC()->LexicalDC = DC;
221 }
222}
223
Argyrios Kyrtzidis4bbb8502012-02-09 02:44:08 +0000224void Decl::setDeclContextsImpl(DeclContext *SemaDC, DeclContext *LexicalDC,
225 ASTContext &Ctx) {
226 if (SemaDC == LexicalDC) {
227 DeclCtx = SemaDC;
228 } else {
229 Decl::MultipleDC *MDC = new (Ctx) Decl::MultipleDC();
230 MDC->SemanticDC = SemaDC;
231 MDC->LexicalDC = LexicalDC;
232 DeclCtx = MDC;
233 }
234}
235
John McCall9aeed322009-10-01 00:25:31 +0000236bool Decl::isInAnonymousNamespace() const {
237 const DeclContext *DC = getDeclContext();
238 do {
239 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
240 if (ND->isAnonymousNamespace())
241 return true;
242 } while ((DC = DC->getParent()));
243
244 return false;
245}
246
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000247TranslationUnitDecl *Decl::getTranslationUnitDecl() {
Argyrios Kyrtzidis9b346692009-06-30 02:34:53 +0000248 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
249 return TUD;
250
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000251 DeclContext *DC = getDeclContext();
252 assert(DC && "This decl is not contained in a translation unit!");
Mike Stump1eb44332009-09-09 15:08:12 +0000253
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000254 while (!DC->isTranslationUnit()) {
255 DC = DC->getParent();
256 assert(DC && "This decl is not contained in a translation unit!");
257 }
Mike Stump1eb44332009-09-09 15:08:12 +0000258
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000259 return cast<TranslationUnitDecl>(DC);
260}
261
262ASTContext &Decl::getASTContext() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000263 return getTranslationUnitDecl()->getASTContext();
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000264}
265
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +0000266ASTMutationListener *Decl::getASTMutationListener() const {
267 return getASTContext().getASTMutationListener();
268}
269
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +0000270unsigned Decl::getMaxAlignment() const {
271 if (!hasAttrs())
272 return 0;
273
274 unsigned Align = 0;
275 const AttrVec &V = getAttrs();
276 ASTContext &Ctx = getASTContext();
277 specific_attr_iterator<AlignedAttr> I(V.begin()), E(V.end());
278 for (; I != E; ++I)
279 Align = std::max(Align, I->getAlignment(Ctx));
280 return Align;
281}
282
Douglas Gregorc070cc62010-06-17 23:14:26 +0000283bool Decl::isUsed(bool CheckUsedAttr) const {
Tanya Lattner12ead492010-02-17 02:17:21 +0000284 if (Used)
285 return true;
286
287 // Check for used attribute.
Douglas Gregorc070cc62010-06-17 23:14:26 +0000288 if (CheckUsedAttr && hasAttr<UsedAttr>())
Tanya Lattner12ead492010-02-17 02:17:21 +0000289 return true;
Rafael Espindola919b7e62012-11-23 16:26:30 +0000290
Tanya Lattner12ead492010-02-17 02:17:21 +0000291 return false;
292}
293
Argyrios Kyrtzidis6b6b42a2011-04-19 19:51:10 +0000294bool Decl::isReferenced() const {
295 if (Referenced)
296 return true;
297
298 // Check redeclarations.
299 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
300 if (I->Referenced)
301 return true;
302
303 return false;
304}
305
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000306/// \brief Determine the availability of the given declaration based on
307/// the target platform.
308///
309/// When it returns an availability result other than \c AR_Available,
310/// if the \p Message parameter is non-NULL, it will be set to a
311/// string describing why the entity is unavailable.
312///
313/// FIXME: Make these strings localizable, since they end up in
314/// diagnostics.
315static AvailabilityResult CheckAvailability(ASTContext &Context,
316 const AvailabilityAttr *A,
317 std::string *Message) {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000318 StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000319 StringRef PrettyPlatformName
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000320 = AvailabilityAttr::getPrettyPlatformName(TargetPlatform);
321 if (PrettyPlatformName.empty())
322 PrettyPlatformName = TargetPlatform;
323
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000324 VersionTuple TargetMinVersion = Context.getTargetInfo().getPlatformMinVersion();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000325 if (TargetMinVersion.empty())
326 return AR_Available;
327
328 // Match the platform name.
329 if (A->getPlatform()->getName() != TargetPlatform)
330 return AR_Available;
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000331
332 std::string HintMessage;
333 if (!A->getMessage().empty()) {
334 HintMessage = " - ";
335 HintMessage += A->getMessage();
336 }
337
Douglas Gregorb53e4172011-03-26 03:35:55 +0000338 // Make sure that this declaration has not been marked 'unavailable'.
339 if (A->getUnavailable()) {
340 if (Message) {
341 Message->clear();
342 llvm::raw_string_ostream Out(*Message);
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000343 Out << "not available on " << PrettyPlatformName
344 << HintMessage;
Douglas Gregorb53e4172011-03-26 03:35:55 +0000345 }
346
347 return AR_Unavailable;
348 }
349
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000350 // Make sure that this declaration has already been introduced.
351 if (!A->getIntroduced().empty() &&
352 TargetMinVersion < A->getIntroduced()) {
353 if (Message) {
354 Message->clear();
355 llvm::raw_string_ostream Out(*Message);
356 Out << "introduced in " << PrettyPlatformName << ' '
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000357 << A->getIntroduced() << HintMessage;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000358 }
359
360 return AR_NotYetIntroduced;
361 }
362
363 // Make sure that this declaration hasn't been obsoleted.
364 if (!A->getObsoleted().empty() && TargetMinVersion >= A->getObsoleted()) {
365 if (Message) {
366 Message->clear();
367 llvm::raw_string_ostream Out(*Message);
368 Out << "obsoleted in " << PrettyPlatformName << ' '
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000369 << A->getObsoleted() << HintMessage;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000370 }
371
372 return AR_Unavailable;
373 }
374
375 // Make sure that this declaration hasn't been deprecated.
376 if (!A->getDeprecated().empty() && TargetMinVersion >= A->getDeprecated()) {
377 if (Message) {
378 Message->clear();
379 llvm::raw_string_ostream Out(*Message);
380 Out << "first deprecated in " << PrettyPlatformName << ' '
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000381 << A->getDeprecated() << HintMessage;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000382 }
383
384 return AR_Deprecated;
385 }
386
387 return AR_Available;
388}
389
390AvailabilityResult Decl::getAvailability(std::string *Message) const {
391 AvailabilityResult Result = AR_Available;
392 std::string ResultMessage;
393
394 for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
395 if (DeprecatedAttr *Deprecated = dyn_cast<DeprecatedAttr>(*A)) {
396 if (Result >= AR_Deprecated)
397 continue;
398
399 if (Message)
400 ResultMessage = Deprecated->getMessage();
401
402 Result = AR_Deprecated;
403 continue;
404 }
405
406 if (UnavailableAttr *Unavailable = dyn_cast<UnavailableAttr>(*A)) {
407 if (Message)
408 *Message = Unavailable->getMessage();
409 return AR_Unavailable;
410 }
411
412 if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
413 AvailabilityResult AR = CheckAvailability(getASTContext(), Availability,
414 Message);
415
416 if (AR == AR_Unavailable)
417 return AR_Unavailable;
418
419 if (AR > Result) {
420 Result = AR;
421 if (Message)
422 ResultMessage.swap(*Message);
423 }
424 continue;
425 }
426 }
427
428 if (Message)
429 Message->swap(ResultMessage);
430 return Result;
431}
432
433bool Decl::canBeWeakImported(bool &IsDefinition) const {
434 IsDefinition = false;
John McCall260611a2012-06-20 06:18:46 +0000435
436 // Variables, if they aren't definitions.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000437 if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
Rafael Espindolad2615cc2013-04-03 19:27:57 +0000438 if (Var->isThisDeclarationADefinition()) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000439 IsDefinition = true;
440 return false;
441 }
John McCall260611a2012-06-20 06:18:46 +0000442 return true;
443
444 // Functions, if they aren't definitions.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000445 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
446 if (FD->hasBody()) {
447 IsDefinition = true;
448 return false;
449 }
John McCall260611a2012-06-20 06:18:46 +0000450 return true;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000451
John McCall260611a2012-06-20 06:18:46 +0000452 // Objective-C classes, if this is the non-fragile runtime.
453 } else if (isa<ObjCInterfaceDecl>(this) &&
John McCall0b92fcb2012-06-20 21:58:02 +0000454 getASTContext().getLangOpts().ObjCRuntime.hasWeakClassImport()) {
John McCall260611a2012-06-20 06:18:46 +0000455 return true;
456
457 // Nothing else.
458 } else {
459 return false;
460 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000461}
462
463bool Decl::isWeakImported() const {
464 bool IsDefinition;
465 if (!canBeWeakImported(IsDefinition))
466 return false;
467
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000468 for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
469 if (isa<WeakImportAttr>(*A))
470 return true;
471
472 if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
473 if (CheckAvailability(getASTContext(), Availability, 0)
474 == AR_NotYetIntroduced)
475 return true;
476 }
477 }
478
479 return false;
480}
Tanya Lattner12ead492010-02-17 02:17:21 +0000481
Chris Lattner769dbdf2009-03-27 20:18:19 +0000482unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
483 switch (DeclKind) {
John McCall9488ea12009-11-17 05:59:44 +0000484 case Function:
485 case CXXMethod:
486 case CXXConstructor:
487 case CXXDestructor:
488 case CXXConversion:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000489 case EnumConstant:
490 case Var:
491 case ImplicitParam:
492 case ParmVar:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000493 case NonTypeTemplateParm:
494 case ObjCMethod:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000495 case ObjCProperty:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000496 return IDNS_Ordinary;
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000497 case Label:
498 return IDNS_Label;
Francois Pichet87c2e122010-11-21 06:08:52 +0000499 case IndirectField:
500 return IDNS_Ordinary | IDNS_Member;
501
John McCall0d6b1642010-04-23 18:46:30 +0000502 case ObjCCompatibleAlias:
503 case ObjCInterface:
504 return IDNS_Ordinary | IDNS_Type;
505
506 case Typedef:
Richard Smith162e1c12011-04-15 14:24:37 +0000507 case TypeAlias:
Richard Smith3e4c6c42011-05-05 21:57:07 +0000508 case TypeAliasTemplate:
John McCall0d6b1642010-04-23 18:46:30 +0000509 case UnresolvedUsingTypename:
510 case TemplateTypeParm:
511 return IDNS_Ordinary | IDNS_Type;
512
John McCall9488ea12009-11-17 05:59:44 +0000513 case UsingShadow:
514 return 0; // we'll actually overwrite this later
515
John McCall7ba107a2009-11-18 02:36:19 +0000516 case UnresolvedUsingValue:
John McCall7ba107a2009-11-18 02:36:19 +0000517 return IDNS_Ordinary | IDNS_Using;
John McCall9488ea12009-11-17 05:59:44 +0000518
519 case Using:
520 return IDNS_Using;
521
Chris Lattner769dbdf2009-03-27 20:18:19 +0000522 case ObjCProtocol:
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000523 return IDNS_ObjCProtocol;
Mike Stump1eb44332009-09-09 15:08:12 +0000524
Chris Lattner769dbdf2009-03-27 20:18:19 +0000525 case Field:
526 case ObjCAtDefsField:
527 case ObjCIvar:
528 return IDNS_Member;
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Chris Lattner769dbdf2009-03-27 20:18:19 +0000530 case Record:
531 case CXXRecord:
532 case Enum:
John McCall0d6b1642010-04-23 18:46:30 +0000533 return IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000534
Chris Lattner769dbdf2009-03-27 20:18:19 +0000535 case Namespace:
John McCall0d6b1642010-04-23 18:46:30 +0000536 case NamespaceAlias:
537 return IDNS_Namespace;
538
Chris Lattner769dbdf2009-03-27 20:18:19 +0000539 case FunctionTemplate:
John McCall0d6b1642010-04-23 18:46:30 +0000540 return IDNS_Ordinary;
541
Chris Lattner769dbdf2009-03-27 20:18:19 +0000542 case ClassTemplate:
543 case TemplateTemplateParm:
John McCall0d6b1642010-04-23 18:46:30 +0000544 return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Chris Lattner769dbdf2009-03-27 20:18:19 +0000546 // Never have names.
John McCall02cace72009-08-28 07:59:38 +0000547 case Friend:
John McCalldd4a3b02009-09-16 22:47:08 +0000548 case FriendTemplate:
Abramo Bagnara6206d532010-06-05 05:09:32 +0000549 case AccessSpec:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000550 case LinkageSpec:
551 case FileScopeAsm:
552 case StaticAssert:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000553 case ObjCPropertyImpl:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000554 case Block:
555 case TranslationUnit:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000556
Chris Lattner769dbdf2009-03-27 20:18:19 +0000557 case UsingDirective:
558 case ClassTemplateSpecialization:
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000559 case ClassTemplatePartialSpecialization:
Francois Pichetaf0f4d02011-08-14 03:52:19 +0000560 case ClassScopeFunctionSpecialization:
Douglas Gregorbd4187b2010-04-22 23:19:50 +0000561 case ObjCImplementation:
562 case ObjCCategory:
563 case ObjCCategoryImpl:
Douglas Gregor15de72c2011-12-02 23:23:56 +0000564 case Import:
Alexey Bataevc6400582013-03-22 06:34:35 +0000565 case OMPThreadPrivate:
Michael Han684aa732013-02-22 17:15:32 +0000566 case Empty:
Douglas Gregorbd4187b2010-04-22 23:19:50 +0000567 // Never looked up by name.
Chris Lattner769dbdf2009-03-27 20:18:19 +0000568 return 0;
569 }
John McCall9488ea12009-11-17 05:59:44 +0000570
David Blaikie30263482012-01-20 21:50:17 +0000571 llvm_unreachable("Invalid DeclKind!");
Eli Friedman56d29372008-06-07 16:52:53 +0000572}
573
Argyrios Kyrtzidis4bbb8502012-02-09 02:44:08 +0000574void Decl::setAttrsImpl(const AttrVec &attrs, ASTContext &Ctx) {
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000575 assert(!HasAttrs && "Decl already contains attrs.");
576
Argyrios Kyrtzidis4bbb8502012-02-09 02:44:08 +0000577 AttrVec &AttrBlank = Ctx.getDeclAttrs(this);
Sean Huntcf807c42010-08-18 23:23:40 +0000578 assert(AttrBlank.empty() && "HasAttrs was wrong?");
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000579
580 AttrBlank = attrs;
581 HasAttrs = true;
582}
583
Sean Huntcf807c42010-08-18 23:23:40 +0000584void Decl::dropAttrs() {
Eli Friedman56d29372008-06-07 16:52:53 +0000585 if (!HasAttrs) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000586
Eli Friedman56d29372008-06-07 16:52:53 +0000587 HasAttrs = false;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000588 getASTContext().eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000589}
590
Sean Huntcf807c42010-08-18 23:23:40 +0000591const AttrVec &Decl::getAttrs() const {
592 assert(HasAttrs && "No attrs to get!");
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000593 return getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000594}
595
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000596void Decl::swapAttrs(Decl *RHS) {
Eli Friedman56d29372008-06-07 16:52:53 +0000597 bool HasLHSAttr = this->HasAttrs;
598 bool HasRHSAttr = RHS->HasAttrs;
Mike Stump1eb44332009-09-09 15:08:12 +0000599
Eli Friedman56d29372008-06-07 16:52:53 +0000600 // Usually, neither decl has attrs, nothing to do.
601 if (!HasLHSAttr && !HasRHSAttr) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000602
Eli Friedman56d29372008-06-07 16:52:53 +0000603 // If 'this' has no attrs, swap the other way.
604 if (!HasLHSAttr)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000605 return RHS->swapAttrs(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000606
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000607 ASTContext &Context = getASTContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000608
Eli Friedman56d29372008-06-07 16:52:53 +0000609 // Handle the case when both decls have attrs.
610 if (HasRHSAttr) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000611 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman56d29372008-06-07 16:52:53 +0000612 return;
613 }
Mike Stump1eb44332009-09-09 15:08:12 +0000614
Eli Friedman56d29372008-06-07 16:52:53 +0000615 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor68584ed2009-06-18 16:11:24 +0000616 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
617 Context.eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000618 this->HasAttrs = false;
619 RHS->HasAttrs = true;
620}
621
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000622Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000623 Decl::Kind DK = D->getDeclKind();
624 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000625#define DECL(NAME, BASE)
626#define DECL_CONTEXT(NAME) \
627 case Decl::NAME: \
628 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
629#define DECL_CONTEXT_BASE(NAME)
630#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000631 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000632#define DECL(NAME, BASE)
633#define DECL_CONTEXT_BASE(NAME) \
634 if (DK >= first##NAME && DK <= last##NAME) \
635 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
636#include "clang/AST/DeclNodes.inc"
David Blaikieb219cfc2011-09-23 05:06:16 +0000637 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000638 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000639}
640
641DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000642 Decl::Kind DK = D->getKind();
643 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000644#define DECL(NAME, BASE)
645#define DECL_CONTEXT(NAME) \
646 case Decl::NAME: \
647 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
648#define DECL_CONTEXT_BASE(NAME)
649#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000650 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000651#define DECL(NAME, BASE)
652#define DECL_CONTEXT_BASE(NAME) \
653 if (DK >= first##NAME && DK <= last##NAME) \
654 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
655#include "clang/AST/DeclNodes.inc"
David Blaikieb219cfc2011-09-23 05:06:16 +0000656 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000657 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000658}
659
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000660SourceLocation Decl::getBodyRBrace() const {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000661 // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
662 // FunctionDecl stores EndRangeLoc for this purpose.
663 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
664 const FunctionDecl *Definition;
665 if (FD->hasBody(Definition))
666 return Definition->getSourceRange().getEnd();
667 return SourceLocation();
668 }
669
Argyrios Kyrtzidis6717ef42010-07-07 11:31:27 +0000670 if (Stmt *Body = getBody())
671 return Body->getSourceRange().getEnd();
672
673 return SourceLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +0000674}
675
Anders Carlsson1329c272009-03-25 23:38:06 +0000676void Decl::CheckAccessDeclContext() const {
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000677#ifndef NDEBUG
John McCall46460a62010-01-20 21:53:11 +0000678 // Suppress this check if any of the following hold:
679 // 1. this is the translation unit (and thus has no parent)
680 // 2. this is a template parameter (and thus doesn't belong to its context)
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000681 // 3. this is a non-type template parameter
682 // 4. the context is not a record
683 // 5. it's invalid
684 // 6. it's a C++0x static_assert.
Anders Carlsson35eda442009-08-29 20:47:47 +0000685 if (isa<TranslationUnitDecl>(this) ||
Argyrios Kyrtzidis04aed0e2010-07-02 11:55:44 +0000686 isa<TemplateTypeParmDecl>(this) ||
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000687 isa<NonTypeTemplateParmDecl>(this) ||
Douglas Gregorfdd8ab12010-02-22 17:53:38 +0000688 !isa<CXXRecordDecl>(getDeclContext()) ||
Argyrios Kyrtzidis65b63ec2010-09-08 21:32:35 +0000689 isInvalidDecl() ||
690 isa<StaticAssertDecl>(this) ||
691 // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
692 // as DeclContext (?).
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000693 isa<ParmVarDecl>(this) ||
694 // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
695 // AS_none as access specifier.
Francois Pichetbc845322011-08-17 01:06:54 +0000696 isa<CXXRecordDecl>(this) ||
697 isa<ClassScopeFunctionSpecializationDecl>(this))
Anders Carlsson35eda442009-08-29 20:47:47 +0000698 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000699
700 assert(Access != AS_none &&
Anders Carlsson1329c272009-03-25 23:38:06 +0000701 "Access specifier is AS_none inside a record decl");
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000702#endif
Anders Carlsson1329c272009-03-25 23:38:06 +0000703}
704
John McCallaab9e312011-02-22 22:25:23 +0000705DeclContext *Decl::getNonClosureContext() {
John McCall4b9c2d22011-11-06 09:01:30 +0000706 return getDeclContext()->getNonClosureAncestor();
707}
708
709DeclContext *DeclContext::getNonClosureAncestor() {
710 DeclContext *DC = this;
John McCallaab9e312011-02-22 22:25:23 +0000711
712 // This is basically "while (DC->isClosure()) DC = DC->getParent();"
713 // except that it's significantly more efficient to cast to a known
714 // decl type and call getDeclContext() than to call getParent().
John McCall7b3f8532011-06-23 21:18:31 +0000715 while (isa<BlockDecl>(DC))
716 DC = cast<BlockDecl>(DC)->getDeclContext();
John McCallaab9e312011-02-22 22:25:23 +0000717
718 assert(!DC->isClosure());
719 return DC;
720}
Anders Carlsson1329c272009-03-25 23:38:06 +0000721
Eli Friedman56d29372008-06-07 16:52:53 +0000722//===----------------------------------------------------------------------===//
723// DeclContext Implementation
724//===----------------------------------------------------------------------===//
725
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000726bool DeclContext::classof(const Decl *D) {
727 switch (D->getKind()) {
Sean Hunt9a555912010-05-30 07:21:58 +0000728#define DECL(NAME, BASE)
729#define DECL_CONTEXT(NAME) case Decl::NAME:
730#define DECL_CONTEXT_BASE(NAME)
731#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000732 return true;
733 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000734#define DECL(NAME, BASE)
735#define DECL_CONTEXT_BASE(NAME) \
736 if (D->getKind() >= Decl::first##NAME && \
737 D->getKind() <= Decl::last##NAME) \
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000738 return true;
Sean Hunt9a555912010-05-30 07:21:58 +0000739#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000740 return false;
741 }
742}
743
Douglas Gregora2da7802010-07-25 18:38:02 +0000744DeclContext::~DeclContext() { }
Douglas Gregor44b43212008-12-11 16:49:14 +0000745
Douglas Gregore942bbe2009-09-10 16:57:35 +0000746/// \brief Find the parent context of this context that will be
747/// used for unqualified name lookup.
748///
749/// Generally, the parent lookup context is the semantic context. However, for
750/// a friend function the parent lookup context is the lexical context, which
751/// is the class in which the friend is declared.
752DeclContext *DeclContext::getLookupParent() {
753 // FIXME: Find a better way to identify friends
754 if (isa<FunctionDecl>(this))
Sebastian Redl7a126a42010-08-31 00:36:30 +0000755 if (getParent()->getRedeclContext()->isFileContext() &&
756 getLexicalParent()->getRedeclContext()->isRecord())
Douglas Gregore942bbe2009-09-10 16:57:35 +0000757 return getLexicalParent();
758
759 return getParent();
760}
761
Sebastian Redl410c4f22010-08-31 20:53:31 +0000762bool DeclContext::isInlineNamespace() const {
763 return isNamespace() &&
764 cast<NamespaceDecl>(this)->isInline();
765}
766
Douglas Gregorbc221632009-05-28 16:34:51 +0000767bool DeclContext::isDependentContext() const {
768 if (isFileContext())
769 return false;
770
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000771 if (isa<ClassTemplatePartialSpecializationDecl>(this))
772 return true;
773
Douglas Gregorf4b7de12012-02-21 19:11:17 +0000774 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this)) {
Douglas Gregorbc221632009-05-28 16:34:51 +0000775 if (Record->getDescribedClassTemplate())
776 return true;
Douglas Gregorf4b7de12012-02-21 19:11:17 +0000777
778 if (Record->isDependentLambda())
779 return true;
780 }
781
John McCall0c01d182010-03-24 05:22:00 +0000782 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregorbc221632009-05-28 16:34:51 +0000783 if (Function->getDescribedFunctionTemplate())
784 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000785
John McCall0c01d182010-03-24 05:22:00 +0000786 // Friend function declarations are dependent if their *lexical*
787 // context is dependent.
788 if (cast<Decl>(this)->getFriendObjectKind())
789 return getLexicalParent()->isDependentContext();
790 }
791
Douglas Gregorbc221632009-05-28 16:34:51 +0000792 return getParent() && getParent()->isDependentContext();
793}
794
Douglas Gregor074149e2009-01-05 19:45:36 +0000795bool DeclContext::isTransparentContext() const {
796 if (DeclKind == Decl::Enum)
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000797 return !cast<EnumDecl>(this)->isScoped();
Douglas Gregor074149e2009-01-05 19:45:36 +0000798 else if (DeclKind == Decl::LinkageSpec)
799 return true;
Douglas Gregor074149e2009-01-05 19:45:36 +0000800
801 return false;
802}
803
John McCallac65c622010-10-26 04:59:26 +0000804bool DeclContext::isExternCContext() const {
805 const DeclContext *DC = this;
806 while (DC->DeclKind != Decl::TranslationUnit) {
807 if (DC->DeclKind == Decl::LinkageSpec)
808 return cast<LinkageSpecDecl>(DC)->getLanguage()
809 == LinkageSpecDecl::lang_c;
810 DC = DC->getParent();
811 }
812 return false;
813}
814
Rafael Espindola950fee22013-02-14 01:18:37 +0000815bool DeclContext::isExternCXXContext() const {
816 const DeclContext *DC = this;
817 while (DC->DeclKind != Decl::TranslationUnit) {
818 if (DC->DeclKind == Decl::LinkageSpec)
819 return cast<LinkageSpecDecl>(DC)->getLanguage()
820 == LinkageSpecDecl::lang_cxx;
821 DC = DC->getParent();
822 }
823 return false;
824}
825
Sebastian Redl7a126a42010-08-31 00:36:30 +0000826bool DeclContext::Encloses(const DeclContext *DC) const {
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000827 if (getPrimaryContext() != this)
828 return getPrimaryContext()->Encloses(DC);
Mike Stump1eb44332009-09-09 15:08:12 +0000829
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000830 for (; DC; DC = DC->getParent())
831 if (DC->getPrimaryContext() == this)
832 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000833 return false;
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000834}
835
Steve Naroff0701bbb2009-01-08 17:28:14 +0000836DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000837 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000838 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000839 case Decl::LinkageSpec:
Mike Stump1eb44332009-09-09 15:08:12 +0000840 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000841 // There is only one DeclContext for these entities.
842 return this;
843
844 case Decl::Namespace:
845 // The original namespace is our primary context.
846 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
847
Douglas Gregor44b43212008-12-11 16:49:14 +0000848 case Decl::ObjCMethod:
849 return this;
850
851 case Decl::ObjCInterface:
Douglas Gregor53df7a12011-12-15 18:03:09 +0000852 if (ObjCInterfaceDecl *Def = cast<ObjCInterfaceDecl>(this)->getDefinition())
853 return Def;
854
855 return this;
856
Steve Naroff0701bbb2009-01-08 17:28:14 +0000857 case Decl::ObjCProtocol:
Douglas Gregor1d784b22012-01-01 19:51:50 +0000858 if (ObjCProtocolDecl *Def = cast<ObjCProtocolDecl>(this)->getDefinition())
859 return Def;
860
861 return this;
Douglas Gregor53df7a12011-12-15 18:03:09 +0000862
Steve Naroff0701bbb2009-01-08 17:28:14 +0000863 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000864 return this;
865
Steve Naroff0701bbb2009-01-08 17:28:14 +0000866 case Decl::ObjCImplementation:
867 case Decl::ObjCCategoryImpl:
868 return this;
869
Douglas Gregor44b43212008-12-11 16:49:14 +0000870 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000871 if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000872 // If this is a tag type that has a definition or is currently
873 // being defined, that definition is our primary context.
John McCall3cb0ebd2010-03-10 03:28:59 +0000874 TagDecl *Tag = cast<TagDecl>(this);
875 assert(isa<TagType>(Tag->TypeForDecl) ||
876 isa<InjectedClassNameType>(Tag->TypeForDecl));
877
878 if (TagDecl *Def = Tag->getDefinition())
879 return Def;
880
881 if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) {
882 const TagType *TagTy = cast<TagType>(Tag->TypeForDecl);
883 if (TagTy->isBeingDefined())
884 // FIXME: is it necessarily being defined in the decl
885 // that owns the type?
886 return TagTy->getDecl();
887 }
888
889 return Tag;
Douglas Gregorcc636682009-02-17 23:15:12 +0000890 }
891
Sean Hunt9a555912010-05-30 07:21:58 +0000892 assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
Douglas Gregor44b43212008-12-11 16:49:14 +0000893 "Unknown DeclContext kind");
894 return this;
895 }
896}
897
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +0000898void
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000899DeclContext::collectAllContexts(SmallVectorImpl<DeclContext *> &Contexts){
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +0000900 Contexts.clear();
901
902 if (DeclKind != Decl::Namespace) {
903 Contexts.push_back(this);
904 return;
Douglas Gregor44b43212008-12-11 16:49:14 +0000905 }
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +0000906
907 NamespaceDecl *Self = static_cast<NamespaceDecl *>(this);
Douglas Gregoref96ee02012-01-14 16:38:05 +0000908 for (NamespaceDecl *N = Self->getMostRecentDecl(); N;
909 N = N->getPreviousDecl())
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +0000910 Contexts.push_back(N);
911
912 std::reverse(Contexts.begin(), Contexts.end());
Douglas Gregor44b43212008-12-11 16:49:14 +0000913}
914
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000915std::pair<Decl *, Decl *>
Bill Wendling341785e2012-02-22 09:51:33 +0000916DeclContext::BuildDeclChain(ArrayRef<Decl*> Decls,
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +0000917 bool FieldsAlreadyLoaded) {
Douglas Gregor46cd2182012-01-06 16:59:53 +0000918 // Build up a chain of declarations via the Decl::NextInContextAndBits field.
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000919 Decl *FirstNewDecl = 0;
920 Decl *PrevDecl = 0;
921 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +0000922 if (FieldsAlreadyLoaded && isa<FieldDecl>(Decls[I]))
923 continue;
924
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000925 Decl *D = Decls[I];
926 if (PrevDecl)
Douglas Gregor46cd2182012-01-06 16:59:53 +0000927 PrevDecl->NextInContextAndBits.setPointer(D);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000928 else
929 FirstNewDecl = D;
930
931 PrevDecl = D;
932 }
933
934 return std::make_pair(FirstNewDecl, PrevDecl);
935}
936
Richard Smithbbcd0f32013-02-07 03:37:08 +0000937/// \brief We have just acquired external visible storage, and we already have
938/// built a lookup map. For every name in the map, pull in the new names from
939/// the external storage.
940void DeclContext::reconcileExternalVisibleStorage() {
Richard Smith88963392013-02-11 22:02:16 +0000941 assert(NeedToReconcileExternalVisibleStorage && LookupPtr.getPointer());
Richard Smithbbcd0f32013-02-07 03:37:08 +0000942 NeedToReconcileExternalVisibleStorage = false;
943
944 StoredDeclsMap &Map = *LookupPtr.getPointer();
945 ExternalASTSource *Source = getParentASTContext().getExternalSource();
946 for (StoredDeclsMap::iterator I = Map.begin(); I != Map.end(); ++I) {
947 I->second.removeExternalDecls();
948 Source->FindExternalVisibleDeclsByName(this, I->first);
949 }
950}
951
Douglas Gregor2cf26342009-04-09 22:27:44 +0000952/// \brief Load the declarations within this lexical storage from an
953/// external source.
Mike Stump1eb44332009-09-09 15:08:12 +0000954void
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000955DeclContext::LoadLexicalDeclsFromExternalStorage() const {
956 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000957 assert(hasExternalLexicalStorage() && Source && "No external storage?");
958
Argyrios Kyrtzidis0dbbc042010-07-30 10:03:23 +0000959 // Notify that we have a DeclContext that is initializing.
960 ExternalASTSource::Deserializing ADeclContext(Source);
Douglas Gregor9fc18c92011-08-26 21:23:06 +0000961
Douglas Gregorba6ffaf2011-07-15 21:46:17 +0000962 // Load the external declarations, if any.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000963 SmallVector<Decl*, 64> Decls;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000964 ExternalLexicalStorage = false;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +0000965 switch (Source->FindExternalLexicalDecls(this, Decls)) {
966 case ELR_Success:
967 break;
968
969 case ELR_Failure:
970 case ELR_AlreadyLoaded:
971 return;
972 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000973
974 if (Decls.empty())
975 return;
976
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +0000977 // We may have already loaded just the fields of this record, in which case
978 // we need to ignore them.
979 bool FieldsAlreadyLoaded = false;
980 if (const RecordDecl *RD = dyn_cast<RecordDecl>(this))
981 FieldsAlreadyLoaded = RD->LoadedFieldsFromExternalStorage;
982
Douglas Gregor2cf26342009-04-09 22:27:44 +0000983 // Splice the newly-read declarations into the beginning of the list
984 // of declarations.
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000985 Decl *ExternalFirst, *ExternalLast;
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +0000986 llvm::tie(ExternalFirst, ExternalLast) = BuildDeclChain(Decls,
987 FieldsAlreadyLoaded);
Douglas Gregor46cd2182012-01-06 16:59:53 +0000988 ExternalLast->NextInContextAndBits.setPointer(FirstDecl);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000989 FirstDecl = ExternalFirst;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000990 if (!LastDecl)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000991 LastDecl = ExternalLast;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000992}
993
John McCall76bd1f32010-06-01 09:23:16 +0000994DeclContext::lookup_result
995ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
996 DeclarationName Name) {
997 ASTContext &Context = DC->getParentASTContext();
998 StoredDeclsMap *Map;
Richard Smithc5d3e802012-03-16 06:12:59 +0000999 if (!(Map = DC->LookupPtr.getPointer()))
John McCall76bd1f32010-06-01 09:23:16 +00001000 Map = DC->CreateStoredDeclsMap(Context);
Douglas Gregor2cf26342009-04-09 22:27:44 +00001001
Richard Smithbbcd0f32013-02-07 03:37:08 +00001002 // Add an entry to the map for this name, if it's not already present.
1003 (*Map)[Name];
Douglas Gregor2cf26342009-04-09 22:27:44 +00001004
John McCall76bd1f32010-06-01 09:23:16 +00001005 return DeclContext::lookup_result();
1006}
Douglas Gregor2cf26342009-04-09 22:27:44 +00001007
John McCall76bd1f32010-06-01 09:23:16 +00001008DeclContext::lookup_result
1009ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00001010 DeclarationName Name,
Argyrios Kyrtzidis45df9c62011-09-09 06:44:14 +00001011 ArrayRef<NamedDecl*> Decls) {
Dmitri Gribenko1ad23d62012-09-10 21:20:09 +00001012 ASTContext &Context = DC->getParentASTContext();
John McCall76bd1f32010-06-01 09:23:16 +00001013 StoredDeclsMap *Map;
Richard Smithc5d3e802012-03-16 06:12:59 +00001014 if (!(Map = DC->LookupPtr.getPointer()))
John McCall76bd1f32010-06-01 09:23:16 +00001015 Map = DC->CreateStoredDeclsMap(Context);
1016
1017 StoredDeclsList &List = (*Map)[Name];
Argyrios Kyrtzidis45df9c62011-09-09 06:44:14 +00001018 for (ArrayRef<NamedDecl*>::iterator
1019 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
John McCall76bd1f32010-06-01 09:23:16 +00001020 if (List.isNull())
Argyrios Kyrtzidis45df9c62011-09-09 06:44:14 +00001021 List.setOnlyValue(*I);
John McCall76bd1f32010-06-01 09:23:16 +00001022 else
Richard Smithbbcd0f32013-02-07 03:37:08 +00001023 // FIXME: Need declarationReplaces handling for redeclarations in modules.
Argyrios Kyrtzidis45df9c62011-09-09 06:44:14 +00001024 List.AddSubsequentDecl(*I);
John McCall76bd1f32010-06-01 09:23:16 +00001025 }
1026
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001027 return List.getLookupResult();
John McCall76bd1f32010-06-01 09:23:16 +00001028}
1029
Sebastian Redl681d7232010-07-27 00:17:23 +00001030DeclContext::decl_iterator DeclContext::noload_decls_begin() const {
1031 return decl_iterator(FirstDecl);
1032}
1033
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001034DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +00001035 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001036 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001037
Mike Stump1eb44332009-09-09 15:08:12 +00001038 return decl_iterator(FirstDecl);
Douglas Gregor6ab35242009-04-09 21:40:53 +00001039}
1040
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001041bool DeclContext::decls_empty() const {
Douglas Gregor8038d512009-04-10 17:25:41 +00001042 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001043 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor8038d512009-04-10 17:25:41 +00001044
1045 return !FirstDecl;
1046}
1047
John McCall9f54ad42009-12-10 09:41:52 +00001048void DeclContext::removeDecl(Decl *D) {
1049 assert(D->getLexicalDeclContext() == this &&
1050 "decl being removed from non-lexical context");
Douglas Gregor46cd2182012-01-06 16:59:53 +00001051 assert((D->NextInContextAndBits.getPointer() || D == LastDecl) &&
John McCall9f54ad42009-12-10 09:41:52 +00001052 "decl is not in decls list");
1053
1054 // Remove D from the decl chain. This is O(n) but hopefully rare.
1055 if (D == FirstDecl) {
1056 if (D == LastDecl)
1057 FirstDecl = LastDecl = 0;
1058 else
Douglas Gregor46cd2182012-01-06 16:59:53 +00001059 FirstDecl = D->NextInContextAndBits.getPointer();
John McCall9f54ad42009-12-10 09:41:52 +00001060 } else {
Douglas Gregor46cd2182012-01-06 16:59:53 +00001061 for (Decl *I = FirstDecl; true; I = I->NextInContextAndBits.getPointer()) {
John McCall9f54ad42009-12-10 09:41:52 +00001062 assert(I && "decl not found in linked list");
Douglas Gregor46cd2182012-01-06 16:59:53 +00001063 if (I->NextInContextAndBits.getPointer() == D) {
1064 I->NextInContextAndBits.setPointer(D->NextInContextAndBits.getPointer());
John McCall9f54ad42009-12-10 09:41:52 +00001065 if (D == LastDecl) LastDecl = I;
1066 break;
1067 }
1068 }
1069 }
1070
1071 // Mark that D is no longer in the decl chain.
Douglas Gregor46cd2182012-01-06 16:59:53 +00001072 D->NextInContextAndBits.setPointer(0);
John McCall9f54ad42009-12-10 09:41:52 +00001073
1074 // Remove D from the lookup table if necessary.
1075 if (isa<NamedDecl>(D)) {
1076 NamedDecl *ND = cast<NamedDecl>(D);
1077
Axel Naumann02368d02011-08-26 14:06:12 +00001078 // Remove only decls that have a name
1079 if (!ND->getDeclName()) return;
1080
Richard Smithc5d3e802012-03-16 06:12:59 +00001081 StoredDeclsMap *Map = getPrimaryContext()->LookupPtr.getPointer();
John McCall0c01d182010-03-24 05:22:00 +00001082 if (!Map) return;
John McCall9f54ad42009-12-10 09:41:52 +00001083
John McCall9f54ad42009-12-10 09:41:52 +00001084 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
1085 assert(Pos != Map->end() && "no lookup entry for decl");
Axel Naumannd9d137e2011-11-08 18:21:06 +00001086 if (Pos->second.getAsVector() || Pos->second.getAsDecl() == ND)
1087 Pos->second.remove(ND);
John McCall9f54ad42009-12-10 09:41:52 +00001088 }
1089}
1090
John McCall3f9a8a62009-08-11 06:59:38 +00001091void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner7f0be132009-02-20 00:56:18 +00001092 assert(D->getLexicalDeclContext() == this &&
1093 "Decl inserted into wrong lexical context");
Mike Stump1eb44332009-09-09 15:08:12 +00001094 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001095 "Decl already inserted into a DeclContext");
1096
1097 if (FirstDecl) {
Douglas Gregor46cd2182012-01-06 16:59:53 +00001098 LastDecl->NextInContextAndBits.setPointer(D);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001099 LastDecl = D;
1100 } else {
1101 FirstDecl = LastDecl = D;
1102 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +00001103
1104 // Notify a C++ record declaration that we've added a member, so it can
1105 // update it's class-specific state.
1106 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
1107 Record->addedMember(D);
Douglas Gregore6649772011-12-03 00:30:27 +00001108
1109 // If this is a newly-created (not de-serialized) import declaration, wire
1110 // it in to the list of local import declarations.
1111 if (!D->isFromASTFile()) {
1112 if (ImportDecl *Import = dyn_cast<ImportDecl>(D))
1113 D->getASTContext().addedLocalImportDecl(Import);
1114 }
John McCall3f9a8a62009-08-11 06:59:38 +00001115}
1116
1117void DeclContext::addDecl(Decl *D) {
1118 addHiddenDecl(D);
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001119
1120 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Richard Smithc5d3e802012-03-16 06:12:59 +00001121 ND->getDeclContext()->getPrimaryContext()->
1122 makeDeclVisibleInContextWithFlags(ND, false, true);
Douglas Gregor44b43212008-12-11 16:49:14 +00001123}
1124
Sean Callanan9faf8102011-10-21 02:57:43 +00001125void DeclContext::addDeclInternal(Decl *D) {
1126 addHiddenDecl(D);
1127
1128 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Richard Smithc5d3e802012-03-16 06:12:59 +00001129 ND->getDeclContext()->getPrimaryContext()->
1130 makeDeclVisibleInContextWithFlags(ND, true, true);
1131}
1132
1133/// shouldBeHidden - Determine whether a declaration which was declared
1134/// within its semantic context should be invisible to qualified name lookup.
1135static bool shouldBeHidden(NamedDecl *D) {
1136 // Skip unnamed declarations.
1137 if (!D->getDeclName())
1138 return true;
1139
1140 // Skip entities that can't be found by name lookup into a particular
1141 // context.
1142 if ((D->getIdentifierNamespace() == 0 && !isa<UsingDirectiveDecl>(D)) ||
1143 D->isTemplateParameter())
1144 return true;
1145
1146 // Skip template specializations.
1147 // FIXME: This feels like a hack. Should DeclarationName support
1148 // template-ids, or is there a better way to keep specializations
1149 // from being visible?
1150 if (isa<ClassTemplateSpecializationDecl>(D))
1151 return true;
1152 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1153 if (FD->isFunctionTemplateSpecialization())
1154 return true;
1155
1156 return false;
1157}
1158
1159/// buildLookup - Build the lookup data structure with all of the
1160/// declarations in this DeclContext (and any other contexts linked
1161/// to it or transparent contexts nested within it) and return it.
1162StoredDeclsMap *DeclContext::buildLookup() {
1163 assert(this == getPrimaryContext() && "buildLookup called on non-primary DC");
1164
Richard Smith88963392013-02-11 22:02:16 +00001165 // FIXME: Should we keep going if hasExternalVisibleStorage?
Richard Smithc5d3e802012-03-16 06:12:59 +00001166 if (!LookupPtr.getInt())
1167 return LookupPtr.getPointer();
1168
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001169 SmallVector<DeclContext *, 2> Contexts;
Richard Smithc5d3e802012-03-16 06:12:59 +00001170 collectAllContexts(Contexts);
1171 for (unsigned I = 0, N = Contexts.size(); I != N; ++I)
1172 buildLookupImpl(Contexts[I]);
1173
1174 // We no longer have any lazy decls.
1175 LookupPtr.setInt(false);
Richard Smith88963392013-02-11 22:02:16 +00001176 NeedToReconcileExternalVisibleStorage = false;
Richard Smithc5d3e802012-03-16 06:12:59 +00001177 return LookupPtr.getPointer();
1178}
1179
1180/// buildLookupImpl - Build part of the lookup data structure for the
1181/// declarations contained within DCtx, which will either be this
1182/// DeclContext, a DeclContext linked to it, or a transparent context
1183/// nested within it.
1184void DeclContext::buildLookupImpl(DeclContext *DCtx) {
1185 for (decl_iterator I = DCtx->decls_begin(), E = DCtx->decls_end();
1186 I != E; ++I) {
1187 Decl *D = *I;
1188
1189 // Insert this declaration into the lookup structure, but only if
1190 // it's semantically within its decl context. Any other decls which
1191 // should be found in this context are added eagerly.
1192 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1193 if (ND->getDeclContext() == DCtx && !shouldBeHidden(ND))
1194 makeDeclVisibleInContextImpl(ND, false);
1195
1196 // If this declaration is itself a transparent declaration context
1197 // or inline namespace, add the members of this declaration of that
1198 // context (recursively).
1199 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(D))
1200 if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
1201 buildLookupImpl(InnerCtx);
1202 }
Sean Callanan9faf8102011-10-21 02:57:43 +00001203}
1204
Mike Stump1eb44332009-09-09 15:08:12 +00001205DeclContext::lookup_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001206DeclContext::lookup(DeclarationName Name) {
Nick Lewycky65daef12012-03-13 04:12:34 +00001207 assert(DeclKind != Decl::LinkageSpec &&
1208 "Should not perform lookups into linkage specs!");
1209
Steve Naroff0701bbb2009-01-08 17:28:14 +00001210 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +00001211 if (PrimaryContext != this)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001212 return PrimaryContext->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +00001213
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001214 if (hasExternalVisibleStorage()) {
Richard Smithbbcd0f32013-02-07 03:37:08 +00001215 StoredDeclsMap *Map = LookupPtr.getPointer();
1216 if (LookupPtr.getInt())
1217 Map = buildLookup();
Richard Smith88963392013-02-11 22:02:16 +00001218 else if (NeedToReconcileExternalVisibleStorage)
1219 reconcileExternalVisibleStorage();
Richard Smithbbcd0f32013-02-07 03:37:08 +00001220
Richard Smith2bb07c12013-02-08 00:37:45 +00001221 if (!Map)
1222 Map = CreateStoredDeclsMap(getParentASTContext());
1223
Richard Smithbbcd0f32013-02-07 03:37:08 +00001224 // If a PCH/module has a result for this name, and we have a local
1225 // declaration, we will have imported the PCH/module result when adding the
1226 // local declaration or when reconciling the module.
Richard Smith2bb07c12013-02-08 00:37:45 +00001227 std::pair<StoredDeclsMap::iterator, bool> R =
1228 Map->insert(std::make_pair(Name, StoredDeclsList()));
1229 if (!R.second)
1230 return R.first->second.getLookupResult();
Richard Smithc5d3e802012-03-16 06:12:59 +00001231
John McCall76bd1f32010-06-01 09:23:16 +00001232 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Richard Smith3646c682013-02-07 03:30:24 +00001233 if (Source->FindExternalVisibleDeclsByName(this, Name)) {
1234 if (StoredDeclsMap *Map = LookupPtr.getPointer()) {
1235 StoredDeclsMap::iterator I = Map->find(Name);
1236 if (I != Map->end())
1237 return I->second.getLookupResult();
1238 }
1239 }
1240
1241 return lookup_result(lookup_iterator(0), lookup_iterator(0));
John McCall76bd1f32010-06-01 09:23:16 +00001242 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001243
Richard Smithc5d3e802012-03-16 06:12:59 +00001244 StoredDeclsMap *Map = LookupPtr.getPointer();
1245 if (LookupPtr.getInt())
1246 Map = buildLookup();
1247
1248 if (!Map)
1249 return lookup_result(lookup_iterator(0), lookup_iterator(0));
1250
1251 StoredDeclsMap::iterator I = Map->find(Name);
1252 if (I == Map->end())
1253 return lookup_result(lookup_iterator(0), lookup_iterator(0));
1254
1255 return I->second.getLookupResult();
Douglas Gregor44b43212008-12-11 16:49:14 +00001256}
1257
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001258void DeclContext::localUncachedLookup(DeclarationName Name,
1259 SmallVectorImpl<NamedDecl *> &Results) {
Douglas Gregorb75a3452011-10-15 00:10:27 +00001260 Results.clear();
1261
1262 // If there's no external storage, just perform a normal lookup and copy
1263 // the results.
Douglas Gregor93ed7cf2012-07-17 21:16:27 +00001264 if (!hasExternalVisibleStorage() && !hasExternalLexicalStorage() && Name) {
Douglas Gregorb75a3452011-10-15 00:10:27 +00001265 lookup_result LookupResults = lookup(Name);
David Blaikie3bc93e32012-12-19 00:45:41 +00001266 Results.insert(Results.end(), LookupResults.begin(), LookupResults.end());
Douglas Gregorb75a3452011-10-15 00:10:27 +00001267 return;
1268 }
1269
1270 // If we have a lookup table, check there first. Maybe we'll get lucky.
Richard Smithbbcd0f32013-02-07 03:37:08 +00001271 if (Name && !LookupPtr.getInt()) {
Douglas Gregor93ed7cf2012-07-17 21:16:27 +00001272 if (StoredDeclsMap *Map = LookupPtr.getPointer()) {
1273 StoredDeclsMap::iterator Pos = Map->find(Name);
1274 if (Pos != Map->end()) {
1275 Results.insert(Results.end(),
David Blaikie3bc93e32012-12-19 00:45:41 +00001276 Pos->second.getLookupResult().begin(),
1277 Pos->second.getLookupResult().end());
Douglas Gregor93ed7cf2012-07-17 21:16:27 +00001278 return;
1279 }
Douglas Gregorb75a3452011-10-15 00:10:27 +00001280 }
1281 }
Douglas Gregor93ed7cf2012-07-17 21:16:27 +00001282
Douglas Gregorb75a3452011-10-15 00:10:27 +00001283 // Slow case: grovel through the declarations in our chain looking for
1284 // matches.
1285 for (Decl *D = FirstDecl; D; D = D->getNextDeclInContext()) {
1286 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1287 if (ND->getDeclName() == Name)
1288 Results.push_back(ND);
1289 }
1290}
1291
Sebastian Redl7a126a42010-08-31 00:36:30 +00001292DeclContext *DeclContext::getRedeclContext() {
Chris Lattner0cf2b192009-03-27 19:19:59 +00001293 DeclContext *Ctx = this;
Sebastian Redl410c4f22010-08-31 20:53:31 +00001294 // Skip through transparent contexts.
1295 while (Ctx->isTransparentContext())
Douglas Gregorce356072009-01-06 23:51:29 +00001296 Ctx = Ctx->getParent();
1297 return Ctx;
1298}
1299
Douglas Gregor88b70942009-02-25 22:02:03 +00001300DeclContext *DeclContext::getEnclosingNamespaceContext() {
1301 DeclContext *Ctx = this;
1302 // Skip through non-namespace, non-translation-unit contexts.
Sebastian Redl51a8a372010-08-31 00:36:23 +00001303 while (!Ctx->isFileContext())
Douglas Gregor88b70942009-02-25 22:02:03 +00001304 Ctx = Ctx->getParent();
1305 return Ctx->getPrimaryContext();
1306}
1307
Sebastian Redl7a126a42010-08-31 00:36:30 +00001308bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
1309 // For non-file contexts, this is equivalent to Equals.
1310 if (!isFileContext())
1311 return O->Equals(this);
1312
1313 do {
1314 if (O->Equals(this))
1315 return true;
1316
1317 const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O);
1318 if (!NS || !NS->isInline())
1319 break;
1320 O = NS->getParent();
1321 } while (O);
1322
1323 return false;
1324}
1325
Richard Smithc5d3e802012-03-16 06:12:59 +00001326void DeclContext::makeDeclVisibleInContext(NamedDecl *D) {
1327 DeclContext *PrimaryDC = this->getPrimaryContext();
1328 DeclContext *DeclDC = D->getDeclContext()->getPrimaryContext();
1329 // If the decl is being added outside of its semantic decl context, we
1330 // need to ensure that we eagerly build the lookup information for it.
1331 PrimaryDC->makeDeclVisibleInContextWithFlags(D, false, PrimaryDC == DeclDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00001332}
1333
Richard Smithc5d3e802012-03-16 06:12:59 +00001334void DeclContext::makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal,
1335 bool Recoverable) {
1336 assert(this == getPrimaryContext() && "expected a primary DC");
Sean Callanan9faf8102011-10-21 02:57:43 +00001337
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001338 // Skip declarations within functions.
1339 // FIXME: We shouldn't need to build lookup tables for function declarations
1340 // ever, and we can't do so correctly because we can't model the nesting of
1341 // scopes which occurs within functions. We use "qualified" lookup into
1342 // function declarations when handling friend declarations inside nested
1343 // classes, and consequently accept the following invalid code:
1344 //
1345 // void f() { void g(); { int g; struct S { friend void g(); }; } }
1346 if (isFunctionOrMethod() && !isa<FunctionDecl>(D))
1347 return;
1348
Richard Smithc5d3e802012-03-16 06:12:59 +00001349 // Skip declarations which should be invisible to name lookup.
1350 if (shouldBeHidden(D))
1351 return;
1352
1353 // If we already have a lookup data structure, perform the insertion into
1354 // it. If we might have externally-stored decls with this name, look them
1355 // up and perform the insertion. If this decl was declared outside its
1356 // semantic context, buildLookup won't add it, so add it now.
1357 //
1358 // FIXME: As a performance hack, don't add such decls into the translation
1359 // unit unless we're in C++, since qualified lookup into the TU is never
1360 // performed.
1361 if (LookupPtr.getPointer() || hasExternalVisibleStorage() ||
1362 ((!Recoverable || D->getDeclContext() != D->getLexicalDeclContext()) &&
1363 (getParentASTContext().getLangOpts().CPlusPlus ||
1364 !isTranslationUnit()))) {
1365 // If we have lazily omitted any decls, they might have the same name as
1366 // the decl which we are adding, so build a full lookup table before adding
1367 // this decl.
1368 buildLookup();
1369 makeDeclVisibleInContextImpl(D, Internal);
1370 } else {
1371 LookupPtr.setInt(true);
1372 }
1373
1374 // If we are a transparent context or inline namespace, insert into our
1375 // parent context, too. This operation is recursive.
1376 if (isTransparentContext() || isInlineNamespace())
1377 getParent()->getPrimaryContext()->
1378 makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
1379
1380 Decl *DCAsDecl = cast<Decl>(this);
1381 // Notify that a decl was made visible unless we are a Tag being defined.
1382 if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined()))
1383 if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
1384 L->AddedVisibleDecl(this, D);
1385}
1386
1387void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal) {
1388 // Find or create the stored declaration map.
1389 StoredDeclsMap *Map = LookupPtr.getPointer();
1390 if (!Map) {
1391 ASTContext *C = &getParentASTContext();
1392 Map = CreateStoredDeclsMap(*C);
Argyrios Kyrtzidis5586b012010-07-04 21:44:25 +00001393 }
1394
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001395 // If there is an external AST source, load any declarations it knows about
1396 // with this declaration's name.
1397 // If the lookup table contains an entry about this name it means that we
1398 // have already checked the external source.
Sean Callanan9faf8102011-10-21 02:57:43 +00001399 if (!Internal)
1400 if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
1401 if (hasExternalVisibleStorage() &&
Richard Smithc5d3e802012-03-16 06:12:59 +00001402 Map->find(D->getDeclName()) == Map->end())
Sean Callanan9faf8102011-10-21 02:57:43 +00001403 Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001404
Douglas Gregor44b43212008-12-11 16:49:14 +00001405 // Insert this declaration into the map.
Richard Smithc5d3e802012-03-16 06:12:59 +00001406 StoredDeclsList &DeclNameEntries = (*Map)[D->getDeclName()];
Chris Lattner67762a32009-02-20 01:44:05 +00001407 if (DeclNameEntries.isNull()) {
1408 DeclNameEntries.setOnlyValue(D);
Richard Smithc5d3e802012-03-16 06:12:59 +00001409 return;
Douglas Gregor44b43212008-12-11 16:49:14 +00001410 }
Chris Lattner91942502009-02-20 00:55:03 +00001411
Richard Smithc5d3e802012-03-16 06:12:59 +00001412 if (DeclNameEntries.HandleRedeclaration(D)) {
1413 // This declaration has replaced an existing one for which
1414 // declarationReplaces returns true.
1415 return;
1416 }
Mike Stump1eb44332009-09-09 15:08:12 +00001417
Richard Smithc5d3e802012-03-16 06:12:59 +00001418 // Put this declaration into the appropriate slot.
1419 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor44b43212008-12-11 16:49:14 +00001420}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001421
1422/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
1423/// this context.
Mike Stump1eb44332009-09-09 15:08:12 +00001424DeclContext::udir_iterator_range
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001425DeclContext::getUsingDirectives() const {
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001426 // FIXME: Use something more efficient than normal lookup for using
1427 // directives. In C++, using directives are looked up more than anything else.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001428 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
David Blaikie3bc93e32012-12-19 00:45:41 +00001429 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.begin()),
1430 reinterpret_cast<udir_iterator>(Result.end()));
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001431}
Douglas Gregor2cf26342009-04-09 22:27:44 +00001432
Ted Kremenek3478eb62010-02-11 07:12:28 +00001433//===----------------------------------------------------------------------===//
1434// Creation and Destruction of StoredDeclsMaps. //
1435//===----------------------------------------------------------------------===//
1436
John McCall0c01d182010-03-24 05:22:00 +00001437StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
Richard Smithc5d3e802012-03-16 06:12:59 +00001438 assert(!LookupPtr.getPointer() && "context already has a decls map");
John McCall0c01d182010-03-24 05:22:00 +00001439 assert(getPrimaryContext() == this &&
1440 "creating decls map on non-primary context");
1441
1442 StoredDeclsMap *M;
1443 bool Dependent = isDependentContext();
1444 if (Dependent)
1445 M = new DependentStoredDeclsMap();
1446 else
1447 M = new StoredDeclsMap();
1448 M->Previous = C.LastSDM;
1449 C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
Richard Smithc5d3e802012-03-16 06:12:59 +00001450 LookupPtr.setPointer(M);
Ted Kremenek3478eb62010-02-11 07:12:28 +00001451 return M;
1452}
1453
1454void ASTContext::ReleaseDeclContextMaps() {
John McCall0c01d182010-03-24 05:22:00 +00001455 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1456 // pointer because the subclass doesn't add anything that needs to
1457 // be deleted.
John McCall0c01d182010-03-24 05:22:00 +00001458 StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1459}
1460
1461void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1462 while (Map) {
1463 // Advance the iteration before we invalidate memory.
1464 llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1465
1466 if (Dependent)
1467 delete static_cast<DependentStoredDeclsMap*>(Map);
1468 else
1469 delete Map;
1470
1471 Map = Next.getPointer();
1472 Dependent = Next.getInt();
1473 }
1474}
1475
John McCall0c01d182010-03-24 05:22:00 +00001476DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1477 DeclContext *Parent,
1478 const PartialDiagnostic &PDiag) {
1479 assert(Parent->isDependentContext()
1480 && "cannot iterate dependent diagnostics of non-dependent context");
1481 Parent = Parent->getPrimaryContext();
Richard Smithc5d3e802012-03-16 06:12:59 +00001482 if (!Parent->LookupPtr.getPointer())
John McCall0c01d182010-03-24 05:22:00 +00001483 Parent->CreateStoredDeclsMap(C);
1484
1485 DependentStoredDeclsMap *Map
Richard Smithc5d3e802012-03-16 06:12:59 +00001486 = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr.getPointer());
John McCall0c01d182010-03-24 05:22:00 +00001487
Douglas Gregorb8365182010-03-29 23:56:53 +00001488 // Allocate the copy of the PartialDiagnostic via the ASTContext's
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00001489 // BumpPtrAllocator, rather than the ASTContext itself.
Douglas Gregorb8365182010-03-29 23:56:53 +00001490 PartialDiagnostic::Storage *DiagStorage = 0;
1491 if (PDiag.hasStorage())
1492 DiagStorage = new (C) PartialDiagnostic::Storage;
1493
1494 DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
John McCall0c01d182010-03-24 05:22:00 +00001495
1496 // TODO: Maybe we shouldn't reverse the order during insertion.
1497 DD->NextDiagnostic = Map->FirstDiagnostic;
1498 Map->FirstDiagnostic = DD;
1499
1500 return DD;
Ted Kremenek3478eb62010-02-11 07:12:28 +00001501}