blob: 0db520e7d63b1a31eba11c4a5b2087c2b07dc312 [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 Gregor6bd99292013-02-09 01:35:03 +000043void Decl::updateOutOfDate(IdentifierInfo &II) const {
44 getASTContext().getExternalSource()->updateOutOfDateIdentifier(II);
45}
46
Douglas Gregor1e68ecc2012-01-05 21:55:30 +000047void *Decl::AllocateDeserializedDecl(const ASTContext &Context,
48 unsigned ID,
49 unsigned Size) {
Douglas Gregor5d1f4962012-01-05 23:49:36 +000050 // Allocate an extra 8 bytes worth of storage, which ensures that the
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +000051 // resulting pointer will still be 8-byte aligned.
Douglas Gregor5d1f4962012-01-05 23:49:36 +000052 void *Start = Context.Allocate(Size + 8);
53 void *Result = (char*)Start + 8;
Douglas Gregorb6b60c12012-01-05 22:27:05 +000054
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +000055 unsigned *PrefixPtr = (unsigned *)Result - 2;
56
57 // Zero out the first 4 bytes; this is used to store the owning module ID.
58 PrefixPtr[0] = 0;
59
60 // Store the global declaration ID in the second 4 bytes.
61 PrefixPtr[1] = ID;
Douglas Gregorb6b60c12012-01-05 22:27:05 +000062
63 return Result;
Douglas Gregor1e68ecc2012-01-05 21:55:30 +000064}
65
Douglas Gregorca2ab452013-01-12 01:29:50 +000066Module *Decl::getOwningModuleSlow() const {
67 assert(isFromASTFile() && "Not from AST file?");
68 return getASTContext().getExternalSource()->getModule(getOwningModuleID());
69}
70
Eli Friedman56d29372008-06-07 16:52:53 +000071const char *Decl::getDeclKindName() const {
72 switch (DeclKind) {
David Blaikieb219cfc2011-09-23 05:06:16 +000073 default: llvm_unreachable("Declaration not in DeclNodes.inc!");
Sean Hunt9a555912010-05-30 07:21:58 +000074#define DECL(DERIVED, BASE) case DERIVED: return #DERIVED;
75#define ABSTRACT_DECL(DECL)
76#include "clang/AST/DeclNodes.inc"
Eli Friedman56d29372008-06-07 16:52:53 +000077 }
78}
79
Douglas Gregor42738572010-03-05 00:26:45 +000080void Decl::setInvalidDecl(bool Invalid) {
81 InvalidDecl = Invalid;
Argyrios Kyrtzidisba50b3e2012-03-09 21:09:04 +000082 if (Invalid && !isa<ParmVarDecl>(this)) {
Douglas Gregor42738572010-03-05 00:26:45 +000083 // Defensive maneuver for ill-formed code: we're likely not to make it to
84 // a point where we set the access specifier, so default it to "public"
85 // to avoid triggering asserts elsewhere in the front end.
86 setAccess(AS_public);
87 }
88}
89
Steve Naroff0a473932009-01-20 19:53:53 +000090const char *DeclContext::getDeclKindName() const {
91 switch (DeclKind) {
David Blaikieb219cfc2011-09-23 05:06:16 +000092 default: llvm_unreachable("Declaration context not in DeclNodes.inc!");
Sean Hunt9a555912010-05-30 07:21:58 +000093#define DECL(DERIVED, BASE) case Decl::DERIVED: return #DERIVED;
94#define ABSTRACT_DECL(DECL)
95#include "clang/AST/DeclNodes.inc"
Steve Naroff0a473932009-01-20 19:53:53 +000096 }
97}
98
Daniel Dunbar02892a62012-03-05 21:42:49 +000099bool Decl::StatisticsEnabled = false;
100void Decl::EnableStatistics() {
101 StatisticsEnabled = true;
Eli Friedman56d29372008-06-07 16:52:53 +0000102}
103
104void Decl::PrintStats() {
Chandler Carruthb43c8ec2011-07-04 06:13:27 +0000105 llvm::errs() << "\n*** Decl Stats:\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Douglas Gregor64650af2009-02-02 23:39:07 +0000107 int totalDecls = 0;
Sean Hunt9a555912010-05-30 07:21:58 +0000108#define DECL(DERIVED, BASE) totalDecls += n##DERIVED##s;
109#define ABSTRACT_DECL(DECL)
110#include "clang/AST/DeclNodes.inc"
Chandler Carruthb43c8ec2011-07-04 06:13:27 +0000111 llvm::errs() << " " << totalDecls << " decls total.\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Douglas Gregor64650af2009-02-02 23:39:07 +0000113 int totalBytes = 0;
Sean Hunt9a555912010-05-30 07:21:58 +0000114#define DECL(DERIVED, BASE) \
115 if (n##DERIVED##s > 0) { \
116 totalBytes += (int)(n##DERIVED##s * sizeof(DERIVED##Decl)); \
Chandler Carruthb43c8ec2011-07-04 06:13:27 +0000117 llvm::errs() << " " << n##DERIVED##s << " " #DERIVED " decls, " \
118 << sizeof(DERIVED##Decl) << " each (" \
119 << n##DERIVED##s * sizeof(DERIVED##Decl) \
120 << " bytes)\n"; \
Douglas Gregor64650af2009-02-02 23:39:07 +0000121 }
Sean Hunt9a555912010-05-30 07:21:58 +0000122#define ABSTRACT_DECL(DECL)
123#include "clang/AST/DeclNodes.inc"
Mike Stump1eb44332009-09-09 15:08:12 +0000124
Chandler Carruthb43c8ec2011-07-04 06:13:27 +0000125 llvm::errs() << "Total bytes = " << totalBytes << "\n";
Eli Friedman56d29372008-06-07 16:52:53 +0000126}
127
Sean Hunt9a555912010-05-30 07:21:58 +0000128void Decl::add(Kind k) {
Eli Friedman56d29372008-06-07 16:52:53 +0000129 switch (k) {
Sean Hunt9a555912010-05-30 07:21:58 +0000130#define DECL(DERIVED, BASE) case DERIVED: ++n##DERIVED##s; break;
131#define ABSTRACT_DECL(DECL)
132#include "clang/AST/DeclNodes.inc"
Eli Friedman56d29372008-06-07 16:52:53 +0000133 }
134}
135
Anders Carlsson67e33202009-06-13 00:08:58 +0000136bool Decl::isTemplateParameterPack() const {
137 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
138 return TTP->isParameterPack();
Douglas Gregor10738d32010-12-23 23:51:58 +0000139 if (const NonTypeTemplateParmDecl *NTTP
Douglas Gregor61c4d282011-01-05 15:48:55 +0000140 = dyn_cast<NonTypeTemplateParmDecl>(this))
Douglas Gregor10738d32010-12-23 23:51:58 +0000141 return NTTP->isParameterPack();
Douglas Gregor61c4d282011-01-05 15:48:55 +0000142 if (const TemplateTemplateParmDecl *TTP
143 = dyn_cast<TemplateTemplateParmDecl>(this))
144 return TTP->isParameterPack();
Anders Carlsson67e33202009-06-13 00:08:58 +0000145 return false;
146}
147
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000148bool Decl::isParameterPack() const {
149 if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(this))
150 return Parm->isParameterPack();
151
152 return isTemplateParameterPack();
153}
154
Douglas Gregore53060f2009-06-25 22:08:12 +0000155bool Decl::isFunctionOrFunctionTemplate() const {
John McCall9488ea12009-11-17 05:59:44 +0000156 if (const UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(this))
Anders Carlsson58badb72009-06-26 05:26:50 +0000157 return UD->getTargetDecl()->isFunctionOrFunctionTemplate();
Mike Stump1eb44332009-09-09 15:08:12 +0000158
Douglas Gregore53060f2009-06-25 22:08:12 +0000159 return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this);
160}
161
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000162bool Decl::isTemplateDecl() const {
163 return isa<TemplateDecl>(this);
164}
165
Argyrios Kyrtzidisc8680f42011-09-28 02:45:33 +0000166const DeclContext *Decl::getParentFunctionOrMethod() const {
167 for (const DeclContext *DC = getDeclContext();
168 DC && !DC->isTranslationUnit() && !DC->isNamespace();
Douglas Gregor79c22782010-01-16 20:21:20 +0000169 DC = DC->getParent())
170 if (DC->isFunctionOrMethod())
Argyrios Kyrtzidisc8680f42011-09-28 02:45:33 +0000171 return DC;
Douglas Gregor79c22782010-01-16 20:21:20 +0000172
Argyrios Kyrtzidisc8680f42011-09-28 02:45:33 +0000173 return 0;
Douglas Gregor79c22782010-01-16 20:21:20 +0000174}
175
Douglas Gregor4c3e0ee2011-02-17 08:47:29 +0000176
Eli Friedman56d29372008-06-07 16:52:53 +0000177//===----------------------------------------------------------------------===//
Chris Lattner49f28ca2009-03-05 08:00:35 +0000178// PrettyStackTraceDecl Implementation
179//===----------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Chris Lattner5f9e2722011-07-23 10:55:15 +0000181void PrettyStackTraceDecl::print(raw_ostream &OS) const {
Chris Lattner49f28ca2009-03-05 08:00:35 +0000182 SourceLocation TheLoc = Loc;
183 if (TheLoc.isInvalid() && TheDecl)
184 TheLoc = TheDecl->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Chris Lattner49f28ca2009-03-05 08:00:35 +0000186 if (TheLoc.isValid()) {
187 TheLoc.print(OS, SM);
188 OS << ": ";
189 }
190
191 OS << Message;
192
Benjamin Kramerb063ef02013-02-23 13:53:57 +0000193 if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl)) {
194 OS << " '";
195 DN->printQualifiedName(OS);
196 OS << '\'';
197 }
Chris Lattner49f28ca2009-03-05 08:00:35 +0000198 OS << '\n';
199}
Mike Stump1eb44332009-09-09 15:08:12 +0000200
Chris Lattner49f28ca2009-03-05 08:00:35 +0000201//===----------------------------------------------------------------------===//
Eli Friedman56d29372008-06-07 16:52:53 +0000202// Decl Implementation
203//===----------------------------------------------------------------------===//
204
Douglas Gregorda2142f2011-02-19 18:51:44 +0000205// Out-of-line virtual method providing a home for Decl.
206Decl::~Decl() { }
Douglas Gregorf4a03cc2011-02-17 07:02:32 +0000207
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000208void Decl::setDeclContext(DeclContext *DC) {
Chris Lattneree219fd2009-03-29 06:06:59 +0000209 DeclCtx = DC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000210}
211
212void Decl::setLexicalDeclContext(DeclContext *DC) {
213 if (DC == getLexicalDeclContext())
214 return;
215
216 if (isInSemaDC()) {
Argyrios Kyrtzidis4bbb8502012-02-09 02:44:08 +0000217 setDeclContextsImpl(getDeclContext(), DC, getASTContext());
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000218 } else {
219 getMultipleDC()->LexicalDC = DC;
220 }
221}
222
Argyrios Kyrtzidis4bbb8502012-02-09 02:44:08 +0000223void Decl::setDeclContextsImpl(DeclContext *SemaDC, DeclContext *LexicalDC,
224 ASTContext &Ctx) {
225 if (SemaDC == LexicalDC) {
226 DeclCtx = SemaDC;
227 } else {
228 Decl::MultipleDC *MDC = new (Ctx) Decl::MultipleDC();
229 MDC->SemanticDC = SemaDC;
230 MDC->LexicalDC = LexicalDC;
231 DeclCtx = MDC;
232 }
233}
234
John McCall9aeed322009-10-01 00:25:31 +0000235bool Decl::isInAnonymousNamespace() const {
236 const DeclContext *DC = getDeclContext();
237 do {
238 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
239 if (ND->isAnonymousNamespace())
240 return true;
241 } while ((DC = DC->getParent()));
242
243 return false;
244}
245
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000246TranslationUnitDecl *Decl::getTranslationUnitDecl() {
Argyrios Kyrtzidis9b346692009-06-30 02:34:53 +0000247 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
248 return TUD;
249
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000250 DeclContext *DC = getDeclContext();
251 assert(DC && "This decl is not contained in a translation unit!");
Mike Stump1eb44332009-09-09 15:08:12 +0000252
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000253 while (!DC->isTranslationUnit()) {
254 DC = DC->getParent();
255 assert(DC && "This decl is not contained in a translation unit!");
256 }
Mike Stump1eb44332009-09-09 15:08:12 +0000257
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000258 return cast<TranslationUnitDecl>(DC);
259}
260
261ASTContext &Decl::getASTContext() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000262 return getTranslationUnitDecl()->getASTContext();
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000263}
264
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +0000265ASTMutationListener *Decl::getASTMutationListener() const {
266 return getASTContext().getASTMutationListener();
267}
268
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +0000269unsigned Decl::getMaxAlignment() const {
270 if (!hasAttrs())
271 return 0;
272
273 unsigned Align = 0;
274 const AttrVec &V = getAttrs();
275 ASTContext &Ctx = getASTContext();
276 specific_attr_iterator<AlignedAttr> I(V.begin()), E(V.end());
277 for (; I != E; ++I)
278 Align = std::max(Align, I->getAlignment(Ctx));
279 return Align;
280}
281
Douglas Gregorc070cc62010-06-17 23:14:26 +0000282bool Decl::isUsed(bool CheckUsedAttr) const {
Tanya Lattner12ead492010-02-17 02:17:21 +0000283 if (Used)
284 return true;
285
286 // Check for used attribute.
Douglas Gregorc070cc62010-06-17 23:14:26 +0000287 if (CheckUsedAttr && hasAttr<UsedAttr>())
Tanya Lattner12ead492010-02-17 02:17:21 +0000288 return true;
Rafael Espindola919b7e62012-11-23 16:26:30 +0000289
Tanya Lattner12ead492010-02-17 02:17:21 +0000290 return false;
291}
292
Argyrios Kyrtzidis6b6b42a2011-04-19 19:51:10 +0000293bool Decl::isReferenced() const {
294 if (Referenced)
295 return true;
296
297 // Check redeclarations.
298 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
299 if (I->Referenced)
300 return true;
301
302 return false;
303}
304
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000305/// \brief Determine the availability of the given declaration based on
306/// the target platform.
307///
308/// When it returns an availability result other than \c AR_Available,
309/// if the \p Message parameter is non-NULL, it will be set to a
310/// string describing why the entity is unavailable.
311///
312/// FIXME: Make these strings localizable, since they end up in
313/// diagnostics.
314static AvailabilityResult CheckAvailability(ASTContext &Context,
315 const AvailabilityAttr *A,
316 std::string *Message) {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000317 StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000318 StringRef PrettyPlatformName
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000319 = AvailabilityAttr::getPrettyPlatformName(TargetPlatform);
320 if (PrettyPlatformName.empty())
321 PrettyPlatformName = TargetPlatform;
322
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000323 VersionTuple TargetMinVersion = Context.getTargetInfo().getPlatformMinVersion();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000324 if (TargetMinVersion.empty())
325 return AR_Available;
326
327 // Match the platform name.
328 if (A->getPlatform()->getName() != TargetPlatform)
329 return AR_Available;
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000330
331 std::string HintMessage;
332 if (!A->getMessage().empty()) {
333 HintMessage = " - ";
334 HintMessage += A->getMessage();
335 }
336
Douglas Gregorb53e4172011-03-26 03:35:55 +0000337 // Make sure that this declaration has not been marked 'unavailable'.
338 if (A->getUnavailable()) {
339 if (Message) {
340 Message->clear();
341 llvm::raw_string_ostream Out(*Message);
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000342 Out << "not available on " << PrettyPlatformName
343 << HintMessage;
Douglas Gregorb53e4172011-03-26 03:35:55 +0000344 }
345
346 return AR_Unavailable;
347 }
348
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000349 // Make sure that this declaration has already been introduced.
350 if (!A->getIntroduced().empty() &&
351 TargetMinVersion < A->getIntroduced()) {
352 if (Message) {
353 Message->clear();
354 llvm::raw_string_ostream Out(*Message);
355 Out << "introduced in " << PrettyPlatformName << ' '
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000356 << A->getIntroduced() << HintMessage;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000357 }
358
359 return AR_NotYetIntroduced;
360 }
361
362 // Make sure that this declaration hasn't been obsoleted.
363 if (!A->getObsoleted().empty() && TargetMinVersion >= A->getObsoleted()) {
364 if (Message) {
365 Message->clear();
366 llvm::raw_string_ostream Out(*Message);
367 Out << "obsoleted in " << PrettyPlatformName << ' '
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000368 << A->getObsoleted() << HintMessage;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000369 }
370
371 return AR_Unavailable;
372 }
373
374 // Make sure that this declaration hasn't been deprecated.
375 if (!A->getDeprecated().empty() && TargetMinVersion >= A->getDeprecated()) {
376 if (Message) {
377 Message->clear();
378 llvm::raw_string_ostream Out(*Message);
379 Out << "first deprecated in " << PrettyPlatformName << ' '
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000380 << A->getDeprecated() << HintMessage;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000381 }
382
383 return AR_Deprecated;
384 }
385
386 return AR_Available;
387}
388
389AvailabilityResult Decl::getAvailability(std::string *Message) const {
390 AvailabilityResult Result = AR_Available;
391 std::string ResultMessage;
392
393 for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
394 if (DeprecatedAttr *Deprecated = dyn_cast<DeprecatedAttr>(*A)) {
395 if (Result >= AR_Deprecated)
396 continue;
397
398 if (Message)
399 ResultMessage = Deprecated->getMessage();
400
401 Result = AR_Deprecated;
402 continue;
403 }
404
405 if (UnavailableAttr *Unavailable = dyn_cast<UnavailableAttr>(*A)) {
406 if (Message)
407 *Message = Unavailable->getMessage();
408 return AR_Unavailable;
409 }
410
411 if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
412 AvailabilityResult AR = CheckAvailability(getASTContext(), Availability,
413 Message);
414
415 if (AR == AR_Unavailable)
416 return AR_Unavailable;
417
418 if (AR > Result) {
419 Result = AR;
420 if (Message)
421 ResultMessage.swap(*Message);
422 }
423 continue;
424 }
425 }
426
427 if (Message)
428 Message->swap(ResultMessage);
429 return Result;
430}
431
432bool Decl::canBeWeakImported(bool &IsDefinition) const {
433 IsDefinition = false;
John McCall260611a2012-06-20 06:18:46 +0000434
435 // Variables, if they aren't definitions.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000436 if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
437 if (!Var->hasExternalStorage() || Var->getInit()) {
438 IsDefinition = true;
439 return false;
440 }
John McCall260611a2012-06-20 06:18:46 +0000441 return true;
442
443 // Functions, if they aren't definitions.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000444 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
445 if (FD->hasBody()) {
446 IsDefinition = true;
447 return false;
448 }
John McCall260611a2012-06-20 06:18:46 +0000449 return true;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000450
John McCall260611a2012-06-20 06:18:46 +0000451 // Objective-C classes, if this is the non-fragile runtime.
452 } else if (isa<ObjCInterfaceDecl>(this) &&
John McCall0b92fcb2012-06-20 21:58:02 +0000453 getASTContext().getLangOpts().ObjCRuntime.hasWeakClassImport()) {
John McCall260611a2012-06-20 06:18:46 +0000454 return true;
455
456 // Nothing else.
457 } else {
458 return false;
459 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000460}
461
462bool Decl::isWeakImported() const {
463 bool IsDefinition;
464 if (!canBeWeakImported(IsDefinition))
465 return false;
466
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000467 for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
468 if (isa<WeakImportAttr>(*A))
469 return true;
470
471 if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
472 if (CheckAvailability(getASTContext(), Availability, 0)
473 == AR_NotYetIntroduced)
474 return true;
475 }
476 }
477
478 return false;
479}
Tanya Lattner12ead492010-02-17 02:17:21 +0000480
Chris Lattner769dbdf2009-03-27 20:18:19 +0000481unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
482 switch (DeclKind) {
John McCall9488ea12009-11-17 05:59:44 +0000483 case Function:
484 case CXXMethod:
485 case CXXConstructor:
486 case CXXDestructor:
487 case CXXConversion:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000488 case EnumConstant:
489 case Var:
490 case ImplicitParam:
491 case ParmVar:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000492 case NonTypeTemplateParm:
493 case ObjCMethod:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000494 case ObjCProperty:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000495 return IDNS_Ordinary;
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000496 case Label:
497 return IDNS_Label;
Francois Pichet87c2e122010-11-21 06:08:52 +0000498 case IndirectField:
499 return IDNS_Ordinary | IDNS_Member;
500
John McCall0d6b1642010-04-23 18:46:30 +0000501 case ObjCCompatibleAlias:
502 case ObjCInterface:
503 return IDNS_Ordinary | IDNS_Type;
504
505 case Typedef:
Richard Smith162e1c12011-04-15 14:24:37 +0000506 case TypeAlias:
Richard Smith3e4c6c42011-05-05 21:57:07 +0000507 case TypeAliasTemplate:
John McCall0d6b1642010-04-23 18:46:30 +0000508 case UnresolvedUsingTypename:
509 case TemplateTypeParm:
510 return IDNS_Ordinary | IDNS_Type;
511
John McCall9488ea12009-11-17 05:59:44 +0000512 case UsingShadow:
513 return 0; // we'll actually overwrite this later
514
John McCall7ba107a2009-11-18 02:36:19 +0000515 case UnresolvedUsingValue:
John McCall7ba107a2009-11-18 02:36:19 +0000516 return IDNS_Ordinary | IDNS_Using;
John McCall9488ea12009-11-17 05:59:44 +0000517
518 case Using:
519 return IDNS_Using;
520
Chris Lattner769dbdf2009-03-27 20:18:19 +0000521 case ObjCProtocol:
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000522 return IDNS_ObjCProtocol;
Mike Stump1eb44332009-09-09 15:08:12 +0000523
Chris Lattner769dbdf2009-03-27 20:18:19 +0000524 case Field:
525 case ObjCAtDefsField:
526 case ObjCIvar:
527 return IDNS_Member;
Mike Stump1eb44332009-09-09 15:08:12 +0000528
Chris Lattner769dbdf2009-03-27 20:18:19 +0000529 case Record:
530 case CXXRecord:
531 case Enum:
John McCall0d6b1642010-04-23 18:46:30 +0000532 return IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000533
Chris Lattner769dbdf2009-03-27 20:18:19 +0000534 case Namespace:
John McCall0d6b1642010-04-23 18:46:30 +0000535 case NamespaceAlias:
536 return IDNS_Namespace;
537
Chris Lattner769dbdf2009-03-27 20:18:19 +0000538 case FunctionTemplate:
John McCall0d6b1642010-04-23 18:46:30 +0000539 return IDNS_Ordinary;
540
Chris Lattner769dbdf2009-03-27 20:18:19 +0000541 case ClassTemplate:
542 case TemplateTemplateParm:
John McCall0d6b1642010-04-23 18:46:30 +0000543 return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Chris Lattner769dbdf2009-03-27 20:18:19 +0000545 // Never have names.
John McCall02cace72009-08-28 07:59:38 +0000546 case Friend:
John McCalldd4a3b02009-09-16 22:47:08 +0000547 case FriendTemplate:
Abramo Bagnara6206d532010-06-05 05:09:32 +0000548 case AccessSpec:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000549 case LinkageSpec:
550 case FileScopeAsm:
551 case StaticAssert:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000552 case ObjCPropertyImpl:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000553 case Block:
554 case TranslationUnit:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000555
Chris Lattner769dbdf2009-03-27 20:18:19 +0000556 case UsingDirective:
557 case ClassTemplateSpecialization:
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000558 case ClassTemplatePartialSpecialization:
Francois Pichetaf0f4d02011-08-14 03:52:19 +0000559 case ClassScopeFunctionSpecialization:
Douglas Gregorbd4187b2010-04-22 23:19:50 +0000560 case ObjCImplementation:
561 case ObjCCategory:
562 case ObjCCategoryImpl:
Douglas Gregor15de72c2011-12-02 23:23:56 +0000563 case Import:
Michael Han684aa732013-02-22 17:15:32 +0000564 case Empty:
Douglas Gregorbd4187b2010-04-22 23:19:50 +0000565 // Never looked up by name.
Chris Lattner769dbdf2009-03-27 20:18:19 +0000566 return 0;
567 }
John McCall9488ea12009-11-17 05:59:44 +0000568
David Blaikie30263482012-01-20 21:50:17 +0000569 llvm_unreachable("Invalid DeclKind!");
Eli Friedman56d29372008-06-07 16:52:53 +0000570}
571
Argyrios Kyrtzidis4bbb8502012-02-09 02:44:08 +0000572void Decl::setAttrsImpl(const AttrVec &attrs, ASTContext &Ctx) {
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000573 assert(!HasAttrs && "Decl already contains attrs.");
574
Argyrios Kyrtzidis4bbb8502012-02-09 02:44:08 +0000575 AttrVec &AttrBlank = Ctx.getDeclAttrs(this);
Sean Huntcf807c42010-08-18 23:23:40 +0000576 assert(AttrBlank.empty() && "HasAttrs was wrong?");
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000577
578 AttrBlank = attrs;
579 HasAttrs = true;
580}
581
Sean Huntcf807c42010-08-18 23:23:40 +0000582void Decl::dropAttrs() {
Eli Friedman56d29372008-06-07 16:52:53 +0000583 if (!HasAttrs) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Eli Friedman56d29372008-06-07 16:52:53 +0000585 HasAttrs = false;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000586 getASTContext().eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000587}
588
Sean Huntcf807c42010-08-18 23:23:40 +0000589const AttrVec &Decl::getAttrs() const {
590 assert(HasAttrs && "No attrs to get!");
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000591 return getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000592}
593
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000594void Decl::swapAttrs(Decl *RHS) {
Eli Friedman56d29372008-06-07 16:52:53 +0000595 bool HasLHSAttr = this->HasAttrs;
596 bool HasRHSAttr = RHS->HasAttrs;
Mike Stump1eb44332009-09-09 15:08:12 +0000597
Eli Friedman56d29372008-06-07 16:52:53 +0000598 // Usually, neither decl has attrs, nothing to do.
599 if (!HasLHSAttr && !HasRHSAttr) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000600
Eli Friedman56d29372008-06-07 16:52:53 +0000601 // If 'this' has no attrs, swap the other way.
602 if (!HasLHSAttr)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000603 return RHS->swapAttrs(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000604
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000605 ASTContext &Context = getASTContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000606
Eli Friedman56d29372008-06-07 16:52:53 +0000607 // Handle the case when both decls have attrs.
608 if (HasRHSAttr) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000609 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman56d29372008-06-07 16:52:53 +0000610 return;
611 }
Mike Stump1eb44332009-09-09 15:08:12 +0000612
Eli Friedman56d29372008-06-07 16:52:53 +0000613 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor68584ed2009-06-18 16:11:24 +0000614 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
615 Context.eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000616 this->HasAttrs = false;
617 RHS->HasAttrs = true;
618}
619
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000620Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000621 Decl::Kind DK = D->getDeclKind();
622 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000623#define DECL(NAME, BASE)
624#define DECL_CONTEXT(NAME) \
625 case Decl::NAME: \
626 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
627#define DECL_CONTEXT_BASE(NAME)
628#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000629 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000630#define DECL(NAME, BASE)
631#define DECL_CONTEXT_BASE(NAME) \
632 if (DK >= first##NAME && DK <= last##NAME) \
633 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
634#include "clang/AST/DeclNodes.inc"
David Blaikieb219cfc2011-09-23 05:06:16 +0000635 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000636 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000637}
638
639DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000640 Decl::Kind DK = D->getKind();
641 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000642#define DECL(NAME, BASE)
643#define DECL_CONTEXT(NAME) \
644 case Decl::NAME: \
645 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
646#define DECL_CONTEXT_BASE(NAME)
647#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000648 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000649#define DECL(NAME, BASE)
650#define DECL_CONTEXT_BASE(NAME) \
651 if (DK >= first##NAME && DK <= last##NAME) \
652 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
653#include "clang/AST/DeclNodes.inc"
David Blaikieb219cfc2011-09-23 05:06:16 +0000654 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000655 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000656}
657
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000658SourceLocation Decl::getBodyRBrace() const {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000659 // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
660 // FunctionDecl stores EndRangeLoc for this purpose.
661 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
662 const FunctionDecl *Definition;
663 if (FD->hasBody(Definition))
664 return Definition->getSourceRange().getEnd();
665 return SourceLocation();
666 }
667
Argyrios Kyrtzidis6717ef42010-07-07 11:31:27 +0000668 if (Stmt *Body = getBody())
669 return Body->getSourceRange().getEnd();
670
671 return SourceLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +0000672}
673
Anders Carlsson1329c272009-03-25 23:38:06 +0000674void Decl::CheckAccessDeclContext() const {
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000675#ifndef NDEBUG
John McCall46460a62010-01-20 21:53:11 +0000676 // Suppress this check if any of the following hold:
677 // 1. this is the translation unit (and thus has no parent)
678 // 2. this is a template parameter (and thus doesn't belong to its context)
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000679 // 3. this is a non-type template parameter
680 // 4. the context is not a record
681 // 5. it's invalid
682 // 6. it's a C++0x static_assert.
Anders Carlsson35eda442009-08-29 20:47:47 +0000683 if (isa<TranslationUnitDecl>(this) ||
Argyrios Kyrtzidis04aed0e2010-07-02 11:55:44 +0000684 isa<TemplateTypeParmDecl>(this) ||
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000685 isa<NonTypeTemplateParmDecl>(this) ||
Douglas Gregorfdd8ab12010-02-22 17:53:38 +0000686 !isa<CXXRecordDecl>(getDeclContext()) ||
Argyrios Kyrtzidis65b63ec2010-09-08 21:32:35 +0000687 isInvalidDecl() ||
688 isa<StaticAssertDecl>(this) ||
689 // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
690 // as DeclContext (?).
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000691 isa<ParmVarDecl>(this) ||
692 // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
693 // AS_none as access specifier.
Francois Pichetbc845322011-08-17 01:06:54 +0000694 isa<CXXRecordDecl>(this) ||
695 isa<ClassScopeFunctionSpecializationDecl>(this))
Anders Carlsson35eda442009-08-29 20:47:47 +0000696 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000697
698 assert(Access != AS_none &&
Anders Carlsson1329c272009-03-25 23:38:06 +0000699 "Access specifier is AS_none inside a record decl");
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000700#endif
Anders Carlsson1329c272009-03-25 23:38:06 +0000701}
702
John McCallaab9e312011-02-22 22:25:23 +0000703DeclContext *Decl::getNonClosureContext() {
John McCall4b9c2d22011-11-06 09:01:30 +0000704 return getDeclContext()->getNonClosureAncestor();
705}
706
707DeclContext *DeclContext::getNonClosureAncestor() {
708 DeclContext *DC = this;
John McCallaab9e312011-02-22 22:25:23 +0000709
710 // This is basically "while (DC->isClosure()) DC = DC->getParent();"
711 // except that it's significantly more efficient to cast to a known
712 // decl type and call getDeclContext() than to call getParent().
John McCall7b3f8532011-06-23 21:18:31 +0000713 while (isa<BlockDecl>(DC))
714 DC = cast<BlockDecl>(DC)->getDeclContext();
John McCallaab9e312011-02-22 22:25:23 +0000715
716 assert(!DC->isClosure());
717 return DC;
718}
Anders Carlsson1329c272009-03-25 23:38:06 +0000719
Eli Friedman56d29372008-06-07 16:52:53 +0000720//===----------------------------------------------------------------------===//
721// DeclContext Implementation
722//===----------------------------------------------------------------------===//
723
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000724bool DeclContext::classof(const Decl *D) {
725 switch (D->getKind()) {
Sean Hunt9a555912010-05-30 07:21:58 +0000726#define DECL(NAME, BASE)
727#define DECL_CONTEXT(NAME) case Decl::NAME:
728#define DECL_CONTEXT_BASE(NAME)
729#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000730 return true;
731 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000732#define DECL(NAME, BASE)
733#define DECL_CONTEXT_BASE(NAME) \
734 if (D->getKind() >= Decl::first##NAME && \
735 D->getKind() <= Decl::last##NAME) \
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000736 return true;
Sean Hunt9a555912010-05-30 07:21:58 +0000737#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000738 return false;
739 }
740}
741
Douglas Gregora2da7802010-07-25 18:38:02 +0000742DeclContext::~DeclContext() { }
Douglas Gregor44b43212008-12-11 16:49:14 +0000743
Douglas Gregore942bbe2009-09-10 16:57:35 +0000744/// \brief Find the parent context of this context that will be
745/// used for unqualified name lookup.
746///
747/// Generally, the parent lookup context is the semantic context. However, for
748/// a friend function the parent lookup context is the lexical context, which
749/// is the class in which the friend is declared.
750DeclContext *DeclContext::getLookupParent() {
751 // FIXME: Find a better way to identify friends
752 if (isa<FunctionDecl>(this))
Sebastian Redl7a126a42010-08-31 00:36:30 +0000753 if (getParent()->getRedeclContext()->isFileContext() &&
754 getLexicalParent()->getRedeclContext()->isRecord())
Douglas Gregore942bbe2009-09-10 16:57:35 +0000755 return getLexicalParent();
756
757 return getParent();
758}
759
Sebastian Redl410c4f22010-08-31 20:53:31 +0000760bool DeclContext::isInlineNamespace() const {
761 return isNamespace() &&
762 cast<NamespaceDecl>(this)->isInline();
763}
764
Douglas Gregorbc221632009-05-28 16:34:51 +0000765bool DeclContext::isDependentContext() const {
766 if (isFileContext())
767 return false;
768
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000769 if (isa<ClassTemplatePartialSpecializationDecl>(this))
770 return true;
771
Douglas Gregorf4b7de12012-02-21 19:11:17 +0000772 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this)) {
Douglas Gregorbc221632009-05-28 16:34:51 +0000773 if (Record->getDescribedClassTemplate())
774 return true;
Douglas Gregorf4b7de12012-02-21 19:11:17 +0000775
776 if (Record->isDependentLambda())
777 return true;
778 }
779
John McCall0c01d182010-03-24 05:22:00 +0000780 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregorbc221632009-05-28 16:34:51 +0000781 if (Function->getDescribedFunctionTemplate())
782 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000783
John McCall0c01d182010-03-24 05:22:00 +0000784 // Friend function declarations are dependent if their *lexical*
785 // context is dependent.
786 if (cast<Decl>(this)->getFriendObjectKind())
787 return getLexicalParent()->isDependentContext();
788 }
789
Douglas Gregorbc221632009-05-28 16:34:51 +0000790 return getParent() && getParent()->isDependentContext();
791}
792
Douglas Gregor074149e2009-01-05 19:45:36 +0000793bool DeclContext::isTransparentContext() const {
794 if (DeclKind == Decl::Enum)
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000795 return !cast<EnumDecl>(this)->isScoped();
Douglas Gregor074149e2009-01-05 19:45:36 +0000796 else if (DeclKind == Decl::LinkageSpec)
797 return true;
Douglas Gregor074149e2009-01-05 19:45:36 +0000798
799 return false;
800}
801
John McCallac65c622010-10-26 04:59:26 +0000802bool DeclContext::isExternCContext() const {
803 const DeclContext *DC = this;
804 while (DC->DeclKind != Decl::TranslationUnit) {
805 if (DC->DeclKind == Decl::LinkageSpec)
806 return cast<LinkageSpecDecl>(DC)->getLanguage()
807 == LinkageSpecDecl::lang_c;
808 DC = DC->getParent();
809 }
810 return false;
811}
812
Rafael Espindola950fee22013-02-14 01:18:37 +0000813bool DeclContext::isExternCXXContext() const {
814 const DeclContext *DC = this;
815 while (DC->DeclKind != Decl::TranslationUnit) {
816 if (DC->DeclKind == Decl::LinkageSpec)
817 return cast<LinkageSpecDecl>(DC)->getLanguage()
818 == LinkageSpecDecl::lang_cxx;
819 DC = DC->getParent();
820 }
821 return false;
822}
823
Sebastian Redl7a126a42010-08-31 00:36:30 +0000824bool DeclContext::Encloses(const DeclContext *DC) const {
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000825 if (getPrimaryContext() != this)
826 return getPrimaryContext()->Encloses(DC);
Mike Stump1eb44332009-09-09 15:08:12 +0000827
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000828 for (; DC; DC = DC->getParent())
829 if (DC->getPrimaryContext() == this)
830 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000831 return false;
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000832}
833
Steve Naroff0701bbb2009-01-08 17:28:14 +0000834DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000835 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000836 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000837 case Decl::LinkageSpec:
Mike Stump1eb44332009-09-09 15:08:12 +0000838 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000839 // There is only one DeclContext for these entities.
840 return this;
841
842 case Decl::Namespace:
843 // The original namespace is our primary context.
844 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
845
Douglas Gregor44b43212008-12-11 16:49:14 +0000846 case Decl::ObjCMethod:
847 return this;
848
849 case Decl::ObjCInterface:
Douglas Gregor53df7a12011-12-15 18:03:09 +0000850 if (ObjCInterfaceDecl *Def = cast<ObjCInterfaceDecl>(this)->getDefinition())
851 return Def;
852
853 return this;
854
Steve Naroff0701bbb2009-01-08 17:28:14 +0000855 case Decl::ObjCProtocol:
Douglas Gregor1d784b22012-01-01 19:51:50 +0000856 if (ObjCProtocolDecl *Def = cast<ObjCProtocolDecl>(this)->getDefinition())
857 return Def;
858
859 return this;
Douglas Gregor53df7a12011-12-15 18:03:09 +0000860
Steve Naroff0701bbb2009-01-08 17:28:14 +0000861 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000862 return this;
863
Steve Naroff0701bbb2009-01-08 17:28:14 +0000864 case Decl::ObjCImplementation:
865 case Decl::ObjCCategoryImpl:
866 return this;
867
Douglas Gregor44b43212008-12-11 16:49:14 +0000868 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000869 if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000870 // If this is a tag type that has a definition or is currently
871 // being defined, that definition is our primary context.
John McCall3cb0ebd2010-03-10 03:28:59 +0000872 TagDecl *Tag = cast<TagDecl>(this);
873 assert(isa<TagType>(Tag->TypeForDecl) ||
874 isa<InjectedClassNameType>(Tag->TypeForDecl));
875
876 if (TagDecl *Def = Tag->getDefinition())
877 return Def;
878
879 if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) {
880 const TagType *TagTy = cast<TagType>(Tag->TypeForDecl);
881 if (TagTy->isBeingDefined())
882 // FIXME: is it necessarily being defined in the decl
883 // that owns the type?
884 return TagTy->getDecl();
885 }
886
887 return Tag;
Douglas Gregorcc636682009-02-17 23:15:12 +0000888 }
889
Sean Hunt9a555912010-05-30 07:21:58 +0000890 assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
Douglas Gregor44b43212008-12-11 16:49:14 +0000891 "Unknown DeclContext kind");
892 return this;
893 }
894}
895
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +0000896void
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000897DeclContext::collectAllContexts(SmallVectorImpl<DeclContext *> &Contexts){
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +0000898 Contexts.clear();
899
900 if (DeclKind != Decl::Namespace) {
901 Contexts.push_back(this);
902 return;
Douglas Gregor44b43212008-12-11 16:49:14 +0000903 }
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +0000904
905 NamespaceDecl *Self = static_cast<NamespaceDecl *>(this);
Douglas Gregoref96ee02012-01-14 16:38:05 +0000906 for (NamespaceDecl *N = Self->getMostRecentDecl(); N;
907 N = N->getPreviousDecl())
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +0000908 Contexts.push_back(N);
909
910 std::reverse(Contexts.begin(), Contexts.end());
Douglas Gregor44b43212008-12-11 16:49:14 +0000911}
912
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000913std::pair<Decl *, Decl *>
Bill Wendling341785e2012-02-22 09:51:33 +0000914DeclContext::BuildDeclChain(ArrayRef<Decl*> Decls,
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +0000915 bool FieldsAlreadyLoaded) {
Douglas Gregor46cd2182012-01-06 16:59:53 +0000916 // Build up a chain of declarations via the Decl::NextInContextAndBits field.
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000917 Decl *FirstNewDecl = 0;
918 Decl *PrevDecl = 0;
919 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +0000920 if (FieldsAlreadyLoaded && isa<FieldDecl>(Decls[I]))
921 continue;
922
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000923 Decl *D = Decls[I];
924 if (PrevDecl)
Douglas Gregor46cd2182012-01-06 16:59:53 +0000925 PrevDecl->NextInContextAndBits.setPointer(D);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000926 else
927 FirstNewDecl = D;
928
929 PrevDecl = D;
930 }
931
932 return std::make_pair(FirstNewDecl, PrevDecl);
933}
934
Richard Smithbbcd0f32013-02-07 03:37:08 +0000935/// \brief We have just acquired external visible storage, and we already have
936/// built a lookup map. For every name in the map, pull in the new names from
937/// the external storage.
938void DeclContext::reconcileExternalVisibleStorage() {
Richard Smith88963392013-02-11 22:02:16 +0000939 assert(NeedToReconcileExternalVisibleStorage && LookupPtr.getPointer());
Richard Smithbbcd0f32013-02-07 03:37:08 +0000940 NeedToReconcileExternalVisibleStorage = false;
941
942 StoredDeclsMap &Map = *LookupPtr.getPointer();
943 ExternalASTSource *Source = getParentASTContext().getExternalSource();
944 for (StoredDeclsMap::iterator I = Map.begin(); I != Map.end(); ++I) {
945 I->second.removeExternalDecls();
946 Source->FindExternalVisibleDeclsByName(this, I->first);
947 }
948}
949
Douglas Gregor2cf26342009-04-09 22:27:44 +0000950/// \brief Load the declarations within this lexical storage from an
951/// external source.
Mike Stump1eb44332009-09-09 15:08:12 +0000952void
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000953DeclContext::LoadLexicalDeclsFromExternalStorage() const {
954 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000955 assert(hasExternalLexicalStorage() && Source && "No external storage?");
956
Argyrios Kyrtzidis0dbbc042010-07-30 10:03:23 +0000957 // Notify that we have a DeclContext that is initializing.
958 ExternalASTSource::Deserializing ADeclContext(Source);
Douglas Gregor9fc18c92011-08-26 21:23:06 +0000959
Douglas Gregorba6ffaf2011-07-15 21:46:17 +0000960 // Load the external declarations, if any.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000961 SmallVector<Decl*, 64> Decls;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000962 ExternalLexicalStorage = false;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +0000963 switch (Source->FindExternalLexicalDecls(this, Decls)) {
964 case ELR_Success:
965 break;
966
967 case ELR_Failure:
968 case ELR_AlreadyLoaded:
969 return;
970 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000971
972 if (Decls.empty())
973 return;
974
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +0000975 // We may have already loaded just the fields of this record, in which case
976 // we need to ignore them.
977 bool FieldsAlreadyLoaded = false;
978 if (const RecordDecl *RD = dyn_cast<RecordDecl>(this))
979 FieldsAlreadyLoaded = RD->LoadedFieldsFromExternalStorage;
980
Douglas Gregor2cf26342009-04-09 22:27:44 +0000981 // Splice the newly-read declarations into the beginning of the list
982 // of declarations.
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000983 Decl *ExternalFirst, *ExternalLast;
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +0000984 llvm::tie(ExternalFirst, ExternalLast) = BuildDeclChain(Decls,
985 FieldsAlreadyLoaded);
Douglas Gregor46cd2182012-01-06 16:59:53 +0000986 ExternalLast->NextInContextAndBits.setPointer(FirstDecl);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000987 FirstDecl = ExternalFirst;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000988 if (!LastDecl)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000989 LastDecl = ExternalLast;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000990}
991
John McCall76bd1f32010-06-01 09:23:16 +0000992DeclContext::lookup_result
993ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
994 DeclarationName Name) {
995 ASTContext &Context = DC->getParentASTContext();
996 StoredDeclsMap *Map;
Richard Smithc5d3e802012-03-16 06:12:59 +0000997 if (!(Map = DC->LookupPtr.getPointer()))
John McCall76bd1f32010-06-01 09:23:16 +0000998 Map = DC->CreateStoredDeclsMap(Context);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000999
Richard Smithbbcd0f32013-02-07 03:37:08 +00001000 // Add an entry to the map for this name, if it's not already present.
1001 (*Map)[Name];
Douglas Gregor2cf26342009-04-09 22:27:44 +00001002
John McCall76bd1f32010-06-01 09:23:16 +00001003 return DeclContext::lookup_result();
1004}
Douglas Gregor2cf26342009-04-09 22:27:44 +00001005
John McCall76bd1f32010-06-01 09:23:16 +00001006DeclContext::lookup_result
1007ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00001008 DeclarationName Name,
Argyrios Kyrtzidis45df9c62011-09-09 06:44:14 +00001009 ArrayRef<NamedDecl*> Decls) {
Dmitri Gribenko1ad23d62012-09-10 21:20:09 +00001010 ASTContext &Context = DC->getParentASTContext();
John McCall76bd1f32010-06-01 09:23:16 +00001011 StoredDeclsMap *Map;
Richard Smithc5d3e802012-03-16 06:12:59 +00001012 if (!(Map = DC->LookupPtr.getPointer()))
John McCall76bd1f32010-06-01 09:23:16 +00001013 Map = DC->CreateStoredDeclsMap(Context);
1014
1015 StoredDeclsList &List = (*Map)[Name];
Argyrios Kyrtzidis45df9c62011-09-09 06:44:14 +00001016 for (ArrayRef<NamedDecl*>::iterator
1017 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
John McCall76bd1f32010-06-01 09:23:16 +00001018 if (List.isNull())
Argyrios Kyrtzidis45df9c62011-09-09 06:44:14 +00001019 List.setOnlyValue(*I);
John McCall76bd1f32010-06-01 09:23:16 +00001020 else
Richard Smithbbcd0f32013-02-07 03:37:08 +00001021 // FIXME: Need declarationReplaces handling for redeclarations in modules.
Argyrios Kyrtzidis45df9c62011-09-09 06:44:14 +00001022 List.AddSubsequentDecl(*I);
John McCall76bd1f32010-06-01 09:23:16 +00001023 }
1024
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001025 return List.getLookupResult();
John McCall76bd1f32010-06-01 09:23:16 +00001026}
1027
Sebastian Redl681d7232010-07-27 00:17:23 +00001028DeclContext::decl_iterator DeclContext::noload_decls_begin() const {
1029 return decl_iterator(FirstDecl);
1030}
1031
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001032DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +00001033 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001034 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001035
Mike Stump1eb44332009-09-09 15:08:12 +00001036 return decl_iterator(FirstDecl);
Douglas Gregor6ab35242009-04-09 21:40:53 +00001037}
1038
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001039bool DeclContext::decls_empty() const {
Douglas Gregor8038d512009-04-10 17:25:41 +00001040 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001041 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor8038d512009-04-10 17:25:41 +00001042
1043 return !FirstDecl;
1044}
1045
John McCall9f54ad42009-12-10 09:41:52 +00001046void DeclContext::removeDecl(Decl *D) {
1047 assert(D->getLexicalDeclContext() == this &&
1048 "decl being removed from non-lexical context");
Douglas Gregor46cd2182012-01-06 16:59:53 +00001049 assert((D->NextInContextAndBits.getPointer() || D == LastDecl) &&
John McCall9f54ad42009-12-10 09:41:52 +00001050 "decl is not in decls list");
1051
1052 // Remove D from the decl chain. This is O(n) but hopefully rare.
1053 if (D == FirstDecl) {
1054 if (D == LastDecl)
1055 FirstDecl = LastDecl = 0;
1056 else
Douglas Gregor46cd2182012-01-06 16:59:53 +00001057 FirstDecl = D->NextInContextAndBits.getPointer();
John McCall9f54ad42009-12-10 09:41:52 +00001058 } else {
Douglas Gregor46cd2182012-01-06 16:59:53 +00001059 for (Decl *I = FirstDecl; true; I = I->NextInContextAndBits.getPointer()) {
John McCall9f54ad42009-12-10 09:41:52 +00001060 assert(I && "decl not found in linked list");
Douglas Gregor46cd2182012-01-06 16:59:53 +00001061 if (I->NextInContextAndBits.getPointer() == D) {
1062 I->NextInContextAndBits.setPointer(D->NextInContextAndBits.getPointer());
John McCall9f54ad42009-12-10 09:41:52 +00001063 if (D == LastDecl) LastDecl = I;
1064 break;
1065 }
1066 }
1067 }
1068
1069 // Mark that D is no longer in the decl chain.
Douglas Gregor46cd2182012-01-06 16:59:53 +00001070 D->NextInContextAndBits.setPointer(0);
John McCall9f54ad42009-12-10 09:41:52 +00001071
1072 // Remove D from the lookup table if necessary.
1073 if (isa<NamedDecl>(D)) {
1074 NamedDecl *ND = cast<NamedDecl>(D);
1075
Axel Naumann02368d02011-08-26 14:06:12 +00001076 // Remove only decls that have a name
1077 if (!ND->getDeclName()) return;
1078
Richard Smithc5d3e802012-03-16 06:12:59 +00001079 StoredDeclsMap *Map = getPrimaryContext()->LookupPtr.getPointer();
John McCall0c01d182010-03-24 05:22:00 +00001080 if (!Map) return;
John McCall9f54ad42009-12-10 09:41:52 +00001081
John McCall9f54ad42009-12-10 09:41:52 +00001082 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
1083 assert(Pos != Map->end() && "no lookup entry for decl");
Axel Naumannd9d137e2011-11-08 18:21:06 +00001084 if (Pos->second.getAsVector() || Pos->second.getAsDecl() == ND)
1085 Pos->second.remove(ND);
John McCall9f54ad42009-12-10 09:41:52 +00001086 }
1087}
1088
John McCall3f9a8a62009-08-11 06:59:38 +00001089void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner7f0be132009-02-20 00:56:18 +00001090 assert(D->getLexicalDeclContext() == this &&
1091 "Decl inserted into wrong lexical context");
Mike Stump1eb44332009-09-09 15:08:12 +00001092 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001093 "Decl already inserted into a DeclContext");
1094
1095 if (FirstDecl) {
Douglas Gregor46cd2182012-01-06 16:59:53 +00001096 LastDecl->NextInContextAndBits.setPointer(D);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001097 LastDecl = D;
1098 } else {
1099 FirstDecl = LastDecl = D;
1100 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +00001101
1102 // Notify a C++ record declaration that we've added a member, so it can
1103 // update it's class-specific state.
1104 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
1105 Record->addedMember(D);
Douglas Gregore6649772011-12-03 00:30:27 +00001106
1107 // If this is a newly-created (not de-serialized) import declaration, wire
1108 // it in to the list of local import declarations.
1109 if (!D->isFromASTFile()) {
1110 if (ImportDecl *Import = dyn_cast<ImportDecl>(D))
1111 D->getASTContext().addedLocalImportDecl(Import);
1112 }
John McCall3f9a8a62009-08-11 06:59:38 +00001113}
1114
1115void DeclContext::addDecl(Decl *D) {
1116 addHiddenDecl(D);
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001117
1118 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Richard Smithc5d3e802012-03-16 06:12:59 +00001119 ND->getDeclContext()->getPrimaryContext()->
1120 makeDeclVisibleInContextWithFlags(ND, false, true);
Douglas Gregor44b43212008-12-11 16:49:14 +00001121}
1122
Sean Callanan9faf8102011-10-21 02:57:43 +00001123void DeclContext::addDeclInternal(Decl *D) {
1124 addHiddenDecl(D);
1125
1126 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Richard Smithc5d3e802012-03-16 06:12:59 +00001127 ND->getDeclContext()->getPrimaryContext()->
1128 makeDeclVisibleInContextWithFlags(ND, true, true);
1129}
1130
1131/// shouldBeHidden - Determine whether a declaration which was declared
1132/// within its semantic context should be invisible to qualified name lookup.
1133static bool shouldBeHidden(NamedDecl *D) {
1134 // Skip unnamed declarations.
1135 if (!D->getDeclName())
1136 return true;
1137
1138 // Skip entities that can't be found by name lookup into a particular
1139 // context.
1140 if ((D->getIdentifierNamespace() == 0 && !isa<UsingDirectiveDecl>(D)) ||
1141 D->isTemplateParameter())
1142 return true;
1143
1144 // Skip template specializations.
1145 // FIXME: This feels like a hack. Should DeclarationName support
1146 // template-ids, or is there a better way to keep specializations
1147 // from being visible?
1148 if (isa<ClassTemplateSpecializationDecl>(D))
1149 return true;
1150 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1151 if (FD->isFunctionTemplateSpecialization())
1152 return true;
1153
1154 return false;
1155}
1156
1157/// buildLookup - Build the lookup data structure with all of the
1158/// declarations in this DeclContext (and any other contexts linked
1159/// to it or transparent contexts nested within it) and return it.
1160StoredDeclsMap *DeclContext::buildLookup() {
1161 assert(this == getPrimaryContext() && "buildLookup called on non-primary DC");
1162
Richard Smith88963392013-02-11 22:02:16 +00001163 // FIXME: Should we keep going if hasExternalVisibleStorage?
Richard Smithc5d3e802012-03-16 06:12:59 +00001164 if (!LookupPtr.getInt())
1165 return LookupPtr.getPointer();
1166
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001167 SmallVector<DeclContext *, 2> Contexts;
Richard Smithc5d3e802012-03-16 06:12:59 +00001168 collectAllContexts(Contexts);
1169 for (unsigned I = 0, N = Contexts.size(); I != N; ++I)
1170 buildLookupImpl(Contexts[I]);
1171
1172 // We no longer have any lazy decls.
1173 LookupPtr.setInt(false);
Richard Smith88963392013-02-11 22:02:16 +00001174 NeedToReconcileExternalVisibleStorage = false;
Richard Smithc5d3e802012-03-16 06:12:59 +00001175 return LookupPtr.getPointer();
1176}
1177
1178/// buildLookupImpl - Build part of the lookup data structure for the
1179/// declarations contained within DCtx, which will either be this
1180/// DeclContext, a DeclContext linked to it, or a transparent context
1181/// nested within it.
1182void DeclContext::buildLookupImpl(DeclContext *DCtx) {
1183 for (decl_iterator I = DCtx->decls_begin(), E = DCtx->decls_end();
1184 I != E; ++I) {
1185 Decl *D = *I;
1186
1187 // Insert this declaration into the lookup structure, but only if
1188 // it's semantically within its decl context. Any other decls which
1189 // should be found in this context are added eagerly.
1190 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1191 if (ND->getDeclContext() == DCtx && !shouldBeHidden(ND))
1192 makeDeclVisibleInContextImpl(ND, false);
1193
1194 // If this declaration is itself a transparent declaration context
1195 // or inline namespace, add the members of this declaration of that
1196 // context (recursively).
1197 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(D))
1198 if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
1199 buildLookupImpl(InnerCtx);
1200 }
Sean Callanan9faf8102011-10-21 02:57:43 +00001201}
1202
Mike Stump1eb44332009-09-09 15:08:12 +00001203DeclContext::lookup_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001204DeclContext::lookup(DeclarationName Name) {
Nick Lewycky65daef12012-03-13 04:12:34 +00001205 assert(DeclKind != Decl::LinkageSpec &&
1206 "Should not perform lookups into linkage specs!");
1207
Steve Naroff0701bbb2009-01-08 17:28:14 +00001208 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +00001209 if (PrimaryContext != this)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001210 return PrimaryContext->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +00001211
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001212 if (hasExternalVisibleStorage()) {
Richard Smithbbcd0f32013-02-07 03:37:08 +00001213 StoredDeclsMap *Map = LookupPtr.getPointer();
1214 if (LookupPtr.getInt())
1215 Map = buildLookup();
Richard Smith88963392013-02-11 22:02:16 +00001216 else if (NeedToReconcileExternalVisibleStorage)
1217 reconcileExternalVisibleStorage();
Richard Smithbbcd0f32013-02-07 03:37:08 +00001218
Richard Smith2bb07c12013-02-08 00:37:45 +00001219 if (!Map)
1220 Map = CreateStoredDeclsMap(getParentASTContext());
1221
Richard Smithbbcd0f32013-02-07 03:37:08 +00001222 // If a PCH/module has a result for this name, and we have a local
1223 // declaration, we will have imported the PCH/module result when adding the
1224 // local declaration or when reconciling the module.
Richard Smith2bb07c12013-02-08 00:37:45 +00001225 std::pair<StoredDeclsMap::iterator, bool> R =
1226 Map->insert(std::make_pair(Name, StoredDeclsList()));
1227 if (!R.second)
1228 return R.first->second.getLookupResult();
Richard Smithc5d3e802012-03-16 06:12:59 +00001229
John McCall76bd1f32010-06-01 09:23:16 +00001230 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Richard Smith3646c682013-02-07 03:30:24 +00001231 if (Source->FindExternalVisibleDeclsByName(this, Name)) {
1232 if (StoredDeclsMap *Map = LookupPtr.getPointer()) {
1233 StoredDeclsMap::iterator I = Map->find(Name);
1234 if (I != Map->end())
1235 return I->second.getLookupResult();
1236 }
1237 }
1238
1239 return lookup_result(lookup_iterator(0), lookup_iterator(0));
John McCall76bd1f32010-06-01 09:23:16 +00001240 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001241
Richard Smithc5d3e802012-03-16 06:12:59 +00001242 StoredDeclsMap *Map = LookupPtr.getPointer();
1243 if (LookupPtr.getInt())
1244 Map = buildLookup();
1245
1246 if (!Map)
1247 return lookup_result(lookup_iterator(0), lookup_iterator(0));
1248
1249 StoredDeclsMap::iterator I = Map->find(Name);
1250 if (I == Map->end())
1251 return lookup_result(lookup_iterator(0), lookup_iterator(0));
1252
1253 return I->second.getLookupResult();
Douglas Gregor44b43212008-12-11 16:49:14 +00001254}
1255
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001256void DeclContext::localUncachedLookup(DeclarationName Name,
1257 SmallVectorImpl<NamedDecl *> &Results) {
Douglas Gregorb75a3452011-10-15 00:10:27 +00001258 Results.clear();
1259
1260 // If there's no external storage, just perform a normal lookup and copy
1261 // the results.
Douglas Gregor93ed7cf2012-07-17 21:16:27 +00001262 if (!hasExternalVisibleStorage() && !hasExternalLexicalStorage() && Name) {
Douglas Gregorb75a3452011-10-15 00:10:27 +00001263 lookup_result LookupResults = lookup(Name);
David Blaikie3bc93e32012-12-19 00:45:41 +00001264 Results.insert(Results.end(), LookupResults.begin(), LookupResults.end());
Douglas Gregorb75a3452011-10-15 00:10:27 +00001265 return;
1266 }
1267
1268 // If we have a lookup table, check there first. Maybe we'll get lucky.
Richard Smithbbcd0f32013-02-07 03:37:08 +00001269 if (Name && !LookupPtr.getInt()) {
Douglas Gregor93ed7cf2012-07-17 21:16:27 +00001270 if (StoredDeclsMap *Map = LookupPtr.getPointer()) {
1271 StoredDeclsMap::iterator Pos = Map->find(Name);
1272 if (Pos != Map->end()) {
1273 Results.insert(Results.end(),
David Blaikie3bc93e32012-12-19 00:45:41 +00001274 Pos->second.getLookupResult().begin(),
1275 Pos->second.getLookupResult().end());
Douglas Gregor93ed7cf2012-07-17 21:16:27 +00001276 return;
1277 }
Douglas Gregorb75a3452011-10-15 00:10:27 +00001278 }
1279 }
Douglas Gregor93ed7cf2012-07-17 21:16:27 +00001280
Douglas Gregorb75a3452011-10-15 00:10:27 +00001281 // Slow case: grovel through the declarations in our chain looking for
1282 // matches.
1283 for (Decl *D = FirstDecl; D; D = D->getNextDeclInContext()) {
1284 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1285 if (ND->getDeclName() == Name)
1286 Results.push_back(ND);
1287 }
1288}
1289
Sebastian Redl7a126a42010-08-31 00:36:30 +00001290DeclContext *DeclContext::getRedeclContext() {
Chris Lattner0cf2b192009-03-27 19:19:59 +00001291 DeclContext *Ctx = this;
Sebastian Redl410c4f22010-08-31 20:53:31 +00001292 // Skip through transparent contexts.
1293 while (Ctx->isTransparentContext())
Douglas Gregorce356072009-01-06 23:51:29 +00001294 Ctx = Ctx->getParent();
1295 return Ctx;
1296}
1297
Douglas Gregor88b70942009-02-25 22:02:03 +00001298DeclContext *DeclContext::getEnclosingNamespaceContext() {
1299 DeclContext *Ctx = this;
1300 // Skip through non-namespace, non-translation-unit contexts.
Sebastian Redl51a8a372010-08-31 00:36:23 +00001301 while (!Ctx->isFileContext())
Douglas Gregor88b70942009-02-25 22:02:03 +00001302 Ctx = Ctx->getParent();
1303 return Ctx->getPrimaryContext();
1304}
1305
Sebastian Redl7a126a42010-08-31 00:36:30 +00001306bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
1307 // For non-file contexts, this is equivalent to Equals.
1308 if (!isFileContext())
1309 return O->Equals(this);
1310
1311 do {
1312 if (O->Equals(this))
1313 return true;
1314
1315 const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O);
1316 if (!NS || !NS->isInline())
1317 break;
1318 O = NS->getParent();
1319 } while (O);
1320
1321 return false;
1322}
1323
Richard Smithc5d3e802012-03-16 06:12:59 +00001324void DeclContext::makeDeclVisibleInContext(NamedDecl *D) {
1325 DeclContext *PrimaryDC = this->getPrimaryContext();
1326 DeclContext *DeclDC = D->getDeclContext()->getPrimaryContext();
1327 // If the decl is being added outside of its semantic decl context, we
1328 // need to ensure that we eagerly build the lookup information for it.
1329 PrimaryDC->makeDeclVisibleInContextWithFlags(D, false, PrimaryDC == DeclDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00001330}
1331
Richard Smithc5d3e802012-03-16 06:12:59 +00001332void DeclContext::makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal,
1333 bool Recoverable) {
1334 assert(this == getPrimaryContext() && "expected a primary DC");
Sean Callanan9faf8102011-10-21 02:57:43 +00001335
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001336 // Skip declarations within functions.
1337 // FIXME: We shouldn't need to build lookup tables for function declarations
1338 // ever, and we can't do so correctly because we can't model the nesting of
1339 // scopes which occurs within functions. We use "qualified" lookup into
1340 // function declarations when handling friend declarations inside nested
1341 // classes, and consequently accept the following invalid code:
1342 //
1343 // void f() { void g(); { int g; struct S { friend void g(); }; } }
1344 if (isFunctionOrMethod() && !isa<FunctionDecl>(D))
1345 return;
1346
Richard Smithc5d3e802012-03-16 06:12:59 +00001347 // Skip declarations which should be invisible to name lookup.
1348 if (shouldBeHidden(D))
1349 return;
1350
1351 // If we already have a lookup data structure, perform the insertion into
1352 // it. If we might have externally-stored decls with this name, look them
1353 // up and perform the insertion. If this decl was declared outside its
1354 // semantic context, buildLookup won't add it, so add it now.
1355 //
1356 // FIXME: As a performance hack, don't add such decls into the translation
1357 // unit unless we're in C++, since qualified lookup into the TU is never
1358 // performed.
1359 if (LookupPtr.getPointer() || hasExternalVisibleStorage() ||
1360 ((!Recoverable || D->getDeclContext() != D->getLexicalDeclContext()) &&
1361 (getParentASTContext().getLangOpts().CPlusPlus ||
1362 !isTranslationUnit()))) {
1363 // If we have lazily omitted any decls, they might have the same name as
1364 // the decl which we are adding, so build a full lookup table before adding
1365 // this decl.
1366 buildLookup();
1367 makeDeclVisibleInContextImpl(D, Internal);
1368 } else {
1369 LookupPtr.setInt(true);
1370 }
1371
1372 // If we are a transparent context or inline namespace, insert into our
1373 // parent context, too. This operation is recursive.
1374 if (isTransparentContext() || isInlineNamespace())
1375 getParent()->getPrimaryContext()->
1376 makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
1377
1378 Decl *DCAsDecl = cast<Decl>(this);
1379 // Notify that a decl was made visible unless we are a Tag being defined.
1380 if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined()))
1381 if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
1382 L->AddedVisibleDecl(this, D);
1383}
1384
1385void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal) {
1386 // Find or create the stored declaration map.
1387 StoredDeclsMap *Map = LookupPtr.getPointer();
1388 if (!Map) {
1389 ASTContext *C = &getParentASTContext();
1390 Map = CreateStoredDeclsMap(*C);
Argyrios Kyrtzidis5586b012010-07-04 21:44:25 +00001391 }
1392
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001393 // If there is an external AST source, load any declarations it knows about
1394 // with this declaration's name.
1395 // If the lookup table contains an entry about this name it means that we
1396 // have already checked the external source.
Sean Callanan9faf8102011-10-21 02:57:43 +00001397 if (!Internal)
1398 if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
1399 if (hasExternalVisibleStorage() &&
Richard Smithc5d3e802012-03-16 06:12:59 +00001400 Map->find(D->getDeclName()) == Map->end())
Sean Callanan9faf8102011-10-21 02:57:43 +00001401 Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001402
Douglas Gregor44b43212008-12-11 16:49:14 +00001403 // Insert this declaration into the map.
Richard Smithc5d3e802012-03-16 06:12:59 +00001404 StoredDeclsList &DeclNameEntries = (*Map)[D->getDeclName()];
Chris Lattner67762a32009-02-20 01:44:05 +00001405 if (DeclNameEntries.isNull()) {
1406 DeclNameEntries.setOnlyValue(D);
Richard Smithc5d3e802012-03-16 06:12:59 +00001407 return;
Douglas Gregor44b43212008-12-11 16:49:14 +00001408 }
Chris Lattner91942502009-02-20 00:55:03 +00001409
Richard Smithc5d3e802012-03-16 06:12:59 +00001410 if (DeclNameEntries.HandleRedeclaration(D)) {
1411 // This declaration has replaced an existing one for which
1412 // declarationReplaces returns true.
1413 return;
1414 }
Mike Stump1eb44332009-09-09 15:08:12 +00001415
Richard Smithc5d3e802012-03-16 06:12:59 +00001416 // Put this declaration into the appropriate slot.
1417 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor44b43212008-12-11 16:49:14 +00001418}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001419
1420/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
1421/// this context.
Mike Stump1eb44332009-09-09 15:08:12 +00001422DeclContext::udir_iterator_range
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001423DeclContext::getUsingDirectives() const {
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001424 // FIXME: Use something more efficient than normal lookup for using
1425 // directives. In C++, using directives are looked up more than anything else.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001426 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
David Blaikie3bc93e32012-12-19 00:45:41 +00001427 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.begin()),
1428 reinterpret_cast<udir_iterator>(Result.end()));
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001429}
Douglas Gregor2cf26342009-04-09 22:27:44 +00001430
Ted Kremenek3478eb62010-02-11 07:12:28 +00001431//===----------------------------------------------------------------------===//
1432// Creation and Destruction of StoredDeclsMaps. //
1433//===----------------------------------------------------------------------===//
1434
John McCall0c01d182010-03-24 05:22:00 +00001435StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
Richard Smithc5d3e802012-03-16 06:12:59 +00001436 assert(!LookupPtr.getPointer() && "context already has a decls map");
John McCall0c01d182010-03-24 05:22:00 +00001437 assert(getPrimaryContext() == this &&
1438 "creating decls map on non-primary context");
1439
1440 StoredDeclsMap *M;
1441 bool Dependent = isDependentContext();
1442 if (Dependent)
1443 M = new DependentStoredDeclsMap();
1444 else
1445 M = new StoredDeclsMap();
1446 M->Previous = C.LastSDM;
1447 C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
Richard Smithc5d3e802012-03-16 06:12:59 +00001448 LookupPtr.setPointer(M);
Ted Kremenek3478eb62010-02-11 07:12:28 +00001449 return M;
1450}
1451
1452void ASTContext::ReleaseDeclContextMaps() {
John McCall0c01d182010-03-24 05:22:00 +00001453 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1454 // pointer because the subclass doesn't add anything that needs to
1455 // be deleted.
John McCall0c01d182010-03-24 05:22:00 +00001456 StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1457}
1458
1459void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1460 while (Map) {
1461 // Advance the iteration before we invalidate memory.
1462 llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1463
1464 if (Dependent)
1465 delete static_cast<DependentStoredDeclsMap*>(Map);
1466 else
1467 delete Map;
1468
1469 Map = Next.getPointer();
1470 Dependent = Next.getInt();
1471 }
1472}
1473
John McCall0c01d182010-03-24 05:22:00 +00001474DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1475 DeclContext *Parent,
1476 const PartialDiagnostic &PDiag) {
1477 assert(Parent->isDependentContext()
1478 && "cannot iterate dependent diagnostics of non-dependent context");
1479 Parent = Parent->getPrimaryContext();
Richard Smithc5d3e802012-03-16 06:12:59 +00001480 if (!Parent->LookupPtr.getPointer())
John McCall0c01d182010-03-24 05:22:00 +00001481 Parent->CreateStoredDeclsMap(C);
1482
1483 DependentStoredDeclsMap *Map
Richard Smithc5d3e802012-03-16 06:12:59 +00001484 = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr.getPointer());
John McCall0c01d182010-03-24 05:22:00 +00001485
Douglas Gregorb8365182010-03-29 23:56:53 +00001486 // Allocate the copy of the PartialDiagnostic via the ASTContext's
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00001487 // BumpPtrAllocator, rather than the ASTContext itself.
Douglas Gregorb8365182010-03-29 23:56:53 +00001488 PartialDiagnostic::Storage *DiagStorage = 0;
1489 if (PDiag.hasStorage())
1490 DiagStorage = new (C) PartialDiagnostic::Storage;
1491
1492 DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
John McCall0c01d182010-03-24 05:22:00 +00001493
1494 // TODO: Maybe we shouldn't reverse the order during insertion.
1495 DD->NextDiagnostic = Map->FirstDiagnostic;
1496 Map->FirstDiagnostic = DD;
1497
1498 return DD;
Ted Kremenek3478eb62010-02-11 07:12:28 +00001499}