blob: 7d7209370049c88a27fbe99d777ebba47c27d684 [file] [log] [blame]
Eli Friedman7dbab8a2008-06-07 16:52:53 +00001//===--- DeclBase.cpp - Declaration AST Node Implementation ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Decl and DeclContext classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclBase.h"
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +000015#include "clang/AST/Decl.h"
Douglas Gregorb1fe2c92009-04-07 17:20:56 +000016#include "clang/AST/DeclContextInternals.h"
Argyrios Kyrtzidis2951e142008-06-09 21:05:31 +000017#include "clang/AST/DeclCXX.h"
John McCallbbbbe4e2010-03-11 07:50:04 +000018#include "clang/AST/DeclFriend.h"
Douglas Gregorded2d7b2009-02-04 19:02:06 +000019#include "clang/AST/DeclObjC.h"
20#include "clang/AST/DeclTemplate.h"
John McCallc62bb642010-03-24 05:22:00 +000021#include "clang/AST/DependentDiagnostic.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000022#include "clang/AST/ExternalASTSource.h"
Eli Friedman7dbab8a2008-06-07 16:52:53 +000023#include "clang/AST/ASTContext.h"
Douglas Gregor91f84212008-12-11 16:49:14 +000024#include "clang/AST/Type.h"
Sebastian Redla7b98a72009-04-26 20:35:05 +000025#include "clang/AST/Stmt.h"
26#include "clang/AST/StmtCXX.h"
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +000027#include "clang/AST/ASTMutationListener.h"
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000028#include "clang/Basic/TargetInfo.h"
Eli Friedman7dbab8a2008-06-07 16:52:53 +000029#include "llvm/ADT/DenseMap.h"
Chris Lattnereae6cb62009-03-05 08:00:35 +000030#include "llvm/Support/raw_ostream.h"
Douglas Gregor8b9ccca2008-12-23 21:05:05 +000031#include <algorithm>
Eli Friedman7dbab8a2008-06-07 16:52:53 +000032using namespace clang;
33
34//===----------------------------------------------------------------------===//
35// Statistics
36//===----------------------------------------------------------------------===//
37
Alexis Hunted053252010-05-30 07:21:58 +000038#define DECL(DERIVED, BASE) static int n##DERIVED##s = 0;
39#define ABSTRACT_DECL(DECL)
40#include "clang/AST/DeclNodes.inc"
Eli Friedman7dbab8a2008-06-07 16:52:53 +000041
Douglas Gregor72172e92012-01-05 21:55:30 +000042void *Decl::AllocateDeserializedDecl(const ASTContext &Context,
43 unsigned ID,
44 unsigned Size) {
Douglas Gregor52261fd2012-01-05 23:49:36 +000045 // Allocate an extra 8 bytes worth of storage, which ensures that the
Douglas Gregorcfe7dc62012-01-09 17:30:44 +000046 // resulting pointer will still be 8-byte aligned.
Douglas Gregor52261fd2012-01-05 23:49:36 +000047 void *Start = Context.Allocate(Size + 8);
48 void *Result = (char*)Start + 8;
Douglas Gregor64af53c2012-01-05 22:27:05 +000049
Douglas Gregorcfe7dc62012-01-09 17:30:44 +000050 unsigned *PrefixPtr = (unsigned *)Result - 2;
51
52 // Zero out the first 4 bytes; this is used to store the owning module ID.
53 PrefixPtr[0] = 0;
54
55 // Store the global declaration ID in the second 4 bytes.
56 PrefixPtr[1] = ID;
Douglas Gregor64af53c2012-01-05 22:27:05 +000057
58 return Result;
Douglas Gregor72172e92012-01-05 21:55:30 +000059}
60
Eli Friedman7dbab8a2008-06-07 16:52:53 +000061const char *Decl::getDeclKindName() const {
62 switch (DeclKind) {
David Blaikie83d382b2011-09-23 05:06:16 +000063 default: llvm_unreachable("Declaration not in DeclNodes.inc!");
Alexis Hunted053252010-05-30 07:21:58 +000064#define DECL(DERIVED, BASE) case DERIVED: return #DERIVED;
65#define ABSTRACT_DECL(DECL)
66#include "clang/AST/DeclNodes.inc"
Eli Friedman7dbab8a2008-06-07 16:52:53 +000067 }
68}
69
Douglas Gregor90d47172010-03-05 00:26:45 +000070void Decl::setInvalidDecl(bool Invalid) {
71 InvalidDecl = Invalid;
Argyrios Kyrtzidisb7d1ca22012-03-09 21:09:04 +000072 if (Invalid && !isa<ParmVarDecl>(this)) {
Douglas Gregor90d47172010-03-05 00:26:45 +000073 // Defensive maneuver for ill-formed code: we're likely not to make it to
74 // a point where we set the access specifier, so default it to "public"
75 // to avoid triggering asserts elsewhere in the front end.
76 setAccess(AS_public);
77 }
78}
79
Steve Naroff5faaef72009-01-20 19:53:53 +000080const char *DeclContext::getDeclKindName() const {
81 switch (DeclKind) {
David Blaikie83d382b2011-09-23 05:06:16 +000082 default: llvm_unreachable("Declaration context not in DeclNodes.inc!");
Alexis Hunted053252010-05-30 07:21:58 +000083#define DECL(DERIVED, BASE) case Decl::DERIVED: return #DERIVED;
84#define ABSTRACT_DECL(DECL)
85#include "clang/AST/DeclNodes.inc"
Steve Naroff5faaef72009-01-20 19:53:53 +000086 }
87}
88
Daniel Dunbar62905572012-03-05 21:42:49 +000089bool Decl::StatisticsEnabled = false;
90void Decl::EnableStatistics() {
91 StatisticsEnabled = true;
Eli Friedman7dbab8a2008-06-07 16:52:53 +000092}
93
94void Decl::PrintStats() {
Chandler Carruthbfb154a2011-07-04 06:13:27 +000095 llvm::errs() << "\n*** Decl Stats:\n";
Mike Stump11289f42009-09-09 15:08:12 +000096
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +000097 int totalDecls = 0;
Alexis Hunted053252010-05-30 07:21:58 +000098#define DECL(DERIVED, BASE) totalDecls += n##DERIVED##s;
99#define ABSTRACT_DECL(DECL)
100#include "clang/AST/DeclNodes.inc"
Chandler Carruthbfb154a2011-07-04 06:13:27 +0000101 llvm::errs() << " " << totalDecls << " decls total.\n";
Mike Stump11289f42009-09-09 15:08:12 +0000102
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +0000103 int totalBytes = 0;
Alexis Hunted053252010-05-30 07:21:58 +0000104#define DECL(DERIVED, BASE) \
105 if (n##DERIVED##s > 0) { \
106 totalBytes += (int)(n##DERIVED##s * sizeof(DERIVED##Decl)); \
Chandler Carruthbfb154a2011-07-04 06:13:27 +0000107 llvm::errs() << " " << n##DERIVED##s << " " #DERIVED " decls, " \
108 << sizeof(DERIVED##Decl) << " each (" \
109 << n##DERIVED##s * sizeof(DERIVED##Decl) \
110 << " bytes)\n"; \
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +0000111 }
Alexis Hunted053252010-05-30 07:21:58 +0000112#define ABSTRACT_DECL(DECL)
113#include "clang/AST/DeclNodes.inc"
Mike Stump11289f42009-09-09 15:08:12 +0000114
Chandler Carruthbfb154a2011-07-04 06:13:27 +0000115 llvm::errs() << "Total bytes = " << totalBytes << "\n";
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000116}
117
Alexis Hunted053252010-05-30 07:21:58 +0000118void Decl::add(Kind k) {
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000119 switch (k) {
Alexis Hunted053252010-05-30 07:21:58 +0000120#define DECL(DERIVED, BASE) case DERIVED: ++n##DERIVED##s; break;
121#define ABSTRACT_DECL(DECL)
122#include "clang/AST/DeclNodes.inc"
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000123 }
124}
125
Anders Carlssonaa73b912009-06-13 00:08:58 +0000126bool Decl::isTemplateParameterPack() const {
127 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
128 return TTP->isParameterPack();
Douglas Gregorda3cc0d2010-12-23 23:51:58 +0000129 if (const NonTypeTemplateParmDecl *NTTP
Douglas Gregorf5500772011-01-05 15:48:55 +0000130 = dyn_cast<NonTypeTemplateParmDecl>(this))
Douglas Gregorda3cc0d2010-12-23 23:51:58 +0000131 return NTTP->isParameterPack();
Douglas Gregorf5500772011-01-05 15:48:55 +0000132 if (const TemplateTemplateParmDecl *TTP
133 = dyn_cast<TemplateTemplateParmDecl>(this))
134 return TTP->isParameterPack();
Anders Carlssonaa73b912009-06-13 00:08:58 +0000135 return false;
136}
137
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +0000138bool Decl::isParameterPack() const {
139 if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(this))
140 return Parm->isParameterPack();
141
142 return isTemplateParameterPack();
143}
144
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000145bool Decl::isFunctionOrFunctionTemplate() const {
John McCall3f746822009-11-17 05:59:44 +0000146 if (const UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(this))
Anders Carlssonf057cb22009-06-26 05:26:50 +0000147 return UD->getTargetDecl()->isFunctionOrFunctionTemplate();
Mike Stump11289f42009-09-09 15:08:12 +0000148
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000149 return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this);
150}
151
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000152bool Decl::isTemplateDecl() const {
153 return isa<TemplateDecl>(this);
154}
155
Argyrios Kyrtzidis0ce4c9a2011-09-28 02:45:33 +0000156const DeclContext *Decl::getParentFunctionOrMethod() const {
157 for (const DeclContext *DC = getDeclContext();
158 DC && !DC->isTranslationUnit() && !DC->isNamespace();
Douglas Gregorf5974fa2010-01-16 20:21:20 +0000159 DC = DC->getParent())
160 if (DC->isFunctionOrMethod())
Argyrios Kyrtzidis0ce4c9a2011-09-28 02:45:33 +0000161 return DC;
Douglas Gregorf5974fa2010-01-16 20:21:20 +0000162
Argyrios Kyrtzidis0ce4c9a2011-09-28 02:45:33 +0000163 return 0;
Douglas Gregorf5974fa2010-01-16 20:21:20 +0000164}
165
Douglas Gregor133eddd2011-02-17 08:47:29 +0000166
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000167//===----------------------------------------------------------------------===//
Chris Lattnereae6cb62009-03-05 08:00:35 +0000168// PrettyStackTraceDecl Implementation
169//===----------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000170
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000171void PrettyStackTraceDecl::print(raw_ostream &OS) const {
Chris Lattnereae6cb62009-03-05 08:00:35 +0000172 SourceLocation TheLoc = Loc;
173 if (TheLoc.isInvalid() && TheDecl)
174 TheLoc = TheDecl->getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000175
Chris Lattnereae6cb62009-03-05 08:00:35 +0000176 if (TheLoc.isValid()) {
177 TheLoc.print(OS, SM);
178 OS << ": ";
179 }
180
181 OS << Message;
182
Daniel Dunbar4f1054e2009-11-21 09:05:59 +0000183 if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
Chris Lattnereae6cb62009-03-05 08:00:35 +0000184 OS << " '" << DN->getQualifiedNameAsString() << '\'';
185 OS << '\n';
186}
Mike Stump11289f42009-09-09 15:08:12 +0000187
Chris Lattnereae6cb62009-03-05 08:00:35 +0000188//===----------------------------------------------------------------------===//
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000189// Decl Implementation
190//===----------------------------------------------------------------------===//
191
Douglas Gregorb11aad82011-02-19 18:51:44 +0000192// Out-of-line virtual method providing a home for Decl.
193Decl::~Decl() { }
Douglas Gregora43942a2011-02-17 07:02:32 +0000194
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000195void Decl::setDeclContext(DeclContext *DC) {
Chris Lattnerb81eb052009-03-29 06:06:59 +0000196 DeclCtx = DC;
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000197}
198
199void Decl::setLexicalDeclContext(DeclContext *DC) {
200 if (DC == getLexicalDeclContext())
201 return;
202
203 if (isInSemaDC()) {
Argyrios Kyrtzidis6f40eb72012-02-09 02:44:08 +0000204 setDeclContextsImpl(getDeclContext(), DC, getASTContext());
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000205 } else {
206 getMultipleDC()->LexicalDC = DC;
207 }
208}
209
Argyrios Kyrtzidis6f40eb72012-02-09 02:44:08 +0000210void Decl::setDeclContextsImpl(DeclContext *SemaDC, DeclContext *LexicalDC,
211 ASTContext &Ctx) {
212 if (SemaDC == LexicalDC) {
213 DeclCtx = SemaDC;
214 } else {
215 Decl::MultipleDC *MDC = new (Ctx) Decl::MultipleDC();
216 MDC->SemanticDC = SemaDC;
217 MDC->LexicalDC = LexicalDC;
218 DeclCtx = MDC;
219 }
220}
221
John McCall4fa53422009-10-01 00:25:31 +0000222bool Decl::isInAnonymousNamespace() const {
223 const DeclContext *DC = getDeclContext();
224 do {
225 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
226 if (ND->isAnonymousNamespace())
227 return true;
228 } while ((DC = DC->getParent()));
229
230 return false;
231}
232
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000233TranslationUnitDecl *Decl::getTranslationUnitDecl() {
Argyrios Kyrtzidis4e1a72b2009-06-30 02:34:53 +0000234 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
235 return TUD;
236
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000237 DeclContext *DC = getDeclContext();
238 assert(DC && "This decl is not contained in a translation unit!");
Mike Stump11289f42009-09-09 15:08:12 +0000239
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000240 while (!DC->isTranslationUnit()) {
241 DC = DC->getParent();
242 assert(DC && "This decl is not contained in a translation unit!");
243 }
Mike Stump11289f42009-09-09 15:08:12 +0000244
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000245 return cast<TranslationUnitDecl>(DC);
246}
247
248ASTContext &Decl::getASTContext() const {
Mike Stump11289f42009-09-09 15:08:12 +0000249 return getTranslationUnitDecl()->getASTContext();
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000250}
251
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +0000252ASTMutationListener *Decl::getASTMutationListener() const {
253 return getASTContext().getASTMutationListener();
254}
255
Douglas Gregorebada0772010-06-17 23:14:26 +0000256bool Decl::isUsed(bool CheckUsedAttr) const {
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000257 if (Used)
258 return true;
259
260 // Check for used attribute.
Douglas Gregorebada0772010-06-17 23:14:26 +0000261 if (CheckUsedAttr && hasAttr<UsedAttr>())
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000262 return true;
263
264 // Check redeclarations for used attribute.
265 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Douglas Gregorebada0772010-06-17 23:14:26 +0000266 if ((CheckUsedAttr && I->hasAttr<UsedAttr>()) || I->Used)
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000267 return true;
268 }
269
270 return false;
271}
272
Argyrios Kyrtzidis16180232011-04-19 19:51:10 +0000273bool Decl::isReferenced() const {
274 if (Referenced)
275 return true;
276
277 // Check redeclarations.
278 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
279 if (I->Referenced)
280 return true;
281
282 return false;
283}
284
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000285/// \brief Determine the availability of the given declaration based on
286/// the target platform.
287///
288/// When it returns an availability result other than \c AR_Available,
289/// if the \p Message parameter is non-NULL, it will be set to a
290/// string describing why the entity is unavailable.
291///
292/// FIXME: Make these strings localizable, since they end up in
293/// diagnostics.
294static AvailabilityResult CheckAvailability(ASTContext &Context,
295 const AvailabilityAttr *A,
296 std::string *Message) {
Douglas Gregore8bbc122011-09-02 00:18:52 +0000297 StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000298 StringRef PrettyPlatformName
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000299 = AvailabilityAttr::getPrettyPlatformName(TargetPlatform);
300 if (PrettyPlatformName.empty())
301 PrettyPlatformName = TargetPlatform;
302
Douglas Gregore8bbc122011-09-02 00:18:52 +0000303 VersionTuple TargetMinVersion = Context.getTargetInfo().getPlatformMinVersion();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000304 if (TargetMinVersion.empty())
305 return AR_Available;
306
307 // Match the platform name.
308 if (A->getPlatform()->getName() != TargetPlatform)
309 return AR_Available;
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000310
311 std::string HintMessage;
312 if (!A->getMessage().empty()) {
313 HintMessage = " - ";
314 HintMessage += A->getMessage();
315 }
316
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000317 // Make sure that this declaration has not been marked 'unavailable'.
318 if (A->getUnavailable()) {
319 if (Message) {
320 Message->clear();
321 llvm::raw_string_ostream Out(*Message);
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000322 Out << "not available on " << PrettyPlatformName
323 << HintMessage;
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000324 }
325
326 return AR_Unavailable;
327 }
328
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000329 // Make sure that this declaration has already been introduced.
330 if (!A->getIntroduced().empty() &&
331 TargetMinVersion < A->getIntroduced()) {
332 if (Message) {
333 Message->clear();
334 llvm::raw_string_ostream Out(*Message);
335 Out << "introduced in " << PrettyPlatformName << ' '
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000336 << A->getIntroduced() << HintMessage;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000337 }
338
339 return AR_NotYetIntroduced;
340 }
341
342 // Make sure that this declaration hasn't been obsoleted.
343 if (!A->getObsoleted().empty() && TargetMinVersion >= A->getObsoleted()) {
344 if (Message) {
345 Message->clear();
346 llvm::raw_string_ostream Out(*Message);
347 Out << "obsoleted in " << PrettyPlatformName << ' '
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000348 << A->getObsoleted() << HintMessage;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000349 }
350
351 return AR_Unavailable;
352 }
353
354 // Make sure that this declaration hasn't been deprecated.
355 if (!A->getDeprecated().empty() && TargetMinVersion >= A->getDeprecated()) {
356 if (Message) {
357 Message->clear();
358 llvm::raw_string_ostream Out(*Message);
359 Out << "first deprecated in " << PrettyPlatformName << ' '
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000360 << A->getDeprecated() << HintMessage;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000361 }
362
363 return AR_Deprecated;
364 }
365
366 return AR_Available;
367}
368
369AvailabilityResult Decl::getAvailability(std::string *Message) const {
370 AvailabilityResult Result = AR_Available;
371 std::string ResultMessage;
372
373 for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
374 if (DeprecatedAttr *Deprecated = dyn_cast<DeprecatedAttr>(*A)) {
375 if (Result >= AR_Deprecated)
376 continue;
377
378 if (Message)
379 ResultMessage = Deprecated->getMessage();
380
381 Result = AR_Deprecated;
382 continue;
383 }
384
385 if (UnavailableAttr *Unavailable = dyn_cast<UnavailableAttr>(*A)) {
386 if (Message)
387 *Message = Unavailable->getMessage();
388 return AR_Unavailable;
389 }
390
391 if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
392 AvailabilityResult AR = CheckAvailability(getASTContext(), Availability,
393 Message);
394
395 if (AR == AR_Unavailable)
396 return AR_Unavailable;
397
398 if (AR > Result) {
399 Result = AR;
400 if (Message)
401 ResultMessage.swap(*Message);
402 }
403 continue;
404 }
405 }
406
407 if (Message)
408 Message->swap(ResultMessage);
409 return Result;
410}
411
412bool Decl::canBeWeakImported(bool &IsDefinition) const {
413 IsDefinition = false;
John McCall5fb5df92012-06-20 06:18:46 +0000414
415 // Variables, if they aren't definitions.
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000416 if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
417 if (!Var->hasExternalStorage() || Var->getInit()) {
418 IsDefinition = true;
419 return false;
420 }
John McCall5fb5df92012-06-20 06:18:46 +0000421 return true;
422
423 // Functions, if they aren't definitions.
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000424 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
425 if (FD->hasBody()) {
426 IsDefinition = true;
427 return false;
428 }
John McCall5fb5df92012-06-20 06:18:46 +0000429 return true;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000430
John McCall5fb5df92012-06-20 06:18:46 +0000431 // Objective-C classes, if this is the non-fragile runtime.
432 } else if (isa<ObjCInterfaceDecl>(this) &&
433 getASTContext().getLangOpts().ObjCRuntime.isNonFragile()) {
434 return true;
435
436 // Nothing else.
437 } else {
438 return false;
439 }
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000440}
441
442bool Decl::isWeakImported() const {
443 bool IsDefinition;
444 if (!canBeWeakImported(IsDefinition))
445 return false;
446
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000447 for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
448 if (isa<WeakImportAttr>(*A))
449 return true;
450
451 if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
452 if (CheckAvailability(getASTContext(), Availability, 0)
453 == AR_NotYetIntroduced)
454 return true;
455 }
456 }
457
458 return false;
459}
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000460
Chris Lattner8e097192009-03-27 20:18:19 +0000461unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
462 switch (DeclKind) {
John McCall3f746822009-11-17 05:59:44 +0000463 case Function:
464 case CXXMethod:
465 case CXXConstructor:
466 case CXXDestructor:
467 case CXXConversion:
Chris Lattner8e097192009-03-27 20:18:19 +0000468 case EnumConstant:
469 case Var:
470 case ImplicitParam:
471 case ParmVar:
Chris Lattner8e097192009-03-27 20:18:19 +0000472 case NonTypeTemplateParm:
473 case ObjCMethod:
Daniel Dunbar45b2d8a2010-04-23 13:07:39 +0000474 case ObjCProperty:
Daniel Dunbar45b2d8a2010-04-23 13:07:39 +0000475 return IDNS_Ordinary;
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000476 case Label:
477 return IDNS_Label;
Francois Pichet783dd6e2010-11-21 06:08:52 +0000478 case IndirectField:
479 return IDNS_Ordinary | IDNS_Member;
480
John McCalle87beb22010-04-23 18:46:30 +0000481 case ObjCCompatibleAlias:
482 case ObjCInterface:
483 return IDNS_Ordinary | IDNS_Type;
484
485 case Typedef:
Richard Smithdda56e42011-04-15 14:24:37 +0000486 case TypeAlias:
Richard Smith3f1b5d02011-05-05 21:57:07 +0000487 case TypeAliasTemplate:
John McCalle87beb22010-04-23 18:46:30 +0000488 case UnresolvedUsingTypename:
489 case TemplateTypeParm:
490 return IDNS_Ordinary | IDNS_Type;
491
John McCall3f746822009-11-17 05:59:44 +0000492 case UsingShadow:
493 return 0; // we'll actually overwrite this later
494
John McCalle61f2ba2009-11-18 02:36:19 +0000495 case UnresolvedUsingValue:
John McCalle61f2ba2009-11-18 02:36:19 +0000496 return IDNS_Ordinary | IDNS_Using;
John McCall3f746822009-11-17 05:59:44 +0000497
498 case Using:
499 return IDNS_Using;
500
Chris Lattner8e097192009-03-27 20:18:19 +0000501 case ObjCProtocol:
Douglas Gregor79947a22009-04-24 00:11:27 +0000502 return IDNS_ObjCProtocol;
Mike Stump11289f42009-09-09 15:08:12 +0000503
Chris Lattner8e097192009-03-27 20:18:19 +0000504 case Field:
505 case ObjCAtDefsField:
506 case ObjCIvar:
507 return IDNS_Member;
Mike Stump11289f42009-09-09 15:08:12 +0000508
Chris Lattner8e097192009-03-27 20:18:19 +0000509 case Record:
510 case CXXRecord:
511 case Enum:
John McCalle87beb22010-04-23 18:46:30 +0000512 return IDNS_Tag | IDNS_Type;
Mike Stump11289f42009-09-09 15:08:12 +0000513
Chris Lattner8e097192009-03-27 20:18:19 +0000514 case Namespace:
John McCalle87beb22010-04-23 18:46:30 +0000515 case NamespaceAlias:
516 return IDNS_Namespace;
517
Chris Lattner8e097192009-03-27 20:18:19 +0000518 case FunctionTemplate:
John McCalle87beb22010-04-23 18:46:30 +0000519 return IDNS_Ordinary;
520
Chris Lattner8e097192009-03-27 20:18:19 +0000521 case ClassTemplate:
522 case TemplateTemplateParm:
John McCalle87beb22010-04-23 18:46:30 +0000523 return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
Mike Stump11289f42009-09-09 15:08:12 +0000524
Chris Lattner8e097192009-03-27 20:18:19 +0000525 // Never have names.
John McCallaa74a0c2009-08-28 07:59:38 +0000526 case Friend:
John McCall11083da2009-09-16 22:47:08 +0000527 case FriendTemplate:
Abramo Bagnarad7340582010-06-05 05:09:32 +0000528 case AccessSpec:
Chris Lattner8e097192009-03-27 20:18:19 +0000529 case LinkageSpec:
530 case FileScopeAsm:
531 case StaticAssert:
Chris Lattner8e097192009-03-27 20:18:19 +0000532 case ObjCPropertyImpl:
Chris Lattner8e097192009-03-27 20:18:19 +0000533 case Block:
534 case TranslationUnit:
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000535
Chris Lattner8e097192009-03-27 20:18:19 +0000536 case UsingDirective:
537 case ClassTemplateSpecialization:
Douglas Gregor2373c592009-05-31 09:31:02 +0000538 case ClassTemplatePartialSpecialization:
Francois Pichet00c7e6c2011-08-14 03:52:19 +0000539 case ClassScopeFunctionSpecialization:
Douglas Gregore93525e2010-04-22 23:19:50 +0000540 case ObjCImplementation:
541 case ObjCCategory:
542 case ObjCCategoryImpl:
Douglas Gregorba345522011-12-02 23:23:56 +0000543 case Import:
Douglas Gregore93525e2010-04-22 23:19:50 +0000544 // Never looked up by name.
Chris Lattner8e097192009-03-27 20:18:19 +0000545 return 0;
546 }
John McCall3f746822009-11-17 05:59:44 +0000547
David Blaikiee4d798f2012-01-20 21:50:17 +0000548 llvm_unreachable("Invalid DeclKind!");
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000549}
550
Argyrios Kyrtzidis6f40eb72012-02-09 02:44:08 +0000551void Decl::setAttrsImpl(const AttrVec &attrs, ASTContext &Ctx) {
Argyrios Kyrtzidis91167172010-06-11 23:09:25 +0000552 assert(!HasAttrs && "Decl already contains attrs.");
553
Argyrios Kyrtzidis6f40eb72012-02-09 02:44:08 +0000554 AttrVec &AttrBlank = Ctx.getDeclAttrs(this);
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000555 assert(AttrBlank.empty() && "HasAttrs was wrong?");
Argyrios Kyrtzidis91167172010-06-11 23:09:25 +0000556
557 AttrBlank = attrs;
558 HasAttrs = true;
559}
560
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000561void Decl::dropAttrs() {
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000562 if (!HasAttrs) return;
Mike Stump11289f42009-09-09 15:08:12 +0000563
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000564 HasAttrs = false;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000565 getASTContext().eraseDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000566}
567
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000568const AttrVec &Decl::getAttrs() const {
569 assert(HasAttrs && "No attrs to get!");
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000570 return getASTContext().getDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000571}
572
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000573void Decl::swapAttrs(Decl *RHS) {
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000574 bool HasLHSAttr = this->HasAttrs;
575 bool HasRHSAttr = RHS->HasAttrs;
Mike Stump11289f42009-09-09 15:08:12 +0000576
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000577 // Usually, neither decl has attrs, nothing to do.
578 if (!HasLHSAttr && !HasRHSAttr) return;
Mike Stump11289f42009-09-09 15:08:12 +0000579
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000580 // If 'this' has no attrs, swap the other way.
581 if (!HasLHSAttr)
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000582 return RHS->swapAttrs(this);
Mike Stump11289f42009-09-09 15:08:12 +0000583
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000584 ASTContext &Context = getASTContext();
Mike Stump11289f42009-09-09 15:08:12 +0000585
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000586 // Handle the case when both decls have attrs.
587 if (HasRHSAttr) {
Douglas Gregor78bd61f2009-06-18 16:11:24 +0000588 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000589 return;
590 }
Mike Stump11289f42009-09-09 15:08:12 +0000591
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000592 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor78bd61f2009-06-18 16:11:24 +0000593 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
594 Context.eraseDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000595 this->HasAttrs = false;
596 RHS->HasAttrs = true;
597}
598
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000599Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000600 Decl::Kind DK = D->getDeclKind();
601 switch(DK) {
Alexis Hunted053252010-05-30 07:21:58 +0000602#define DECL(NAME, BASE)
603#define DECL_CONTEXT(NAME) \
604 case Decl::NAME: \
605 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
606#define DECL_CONTEXT_BASE(NAME)
607#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000608 default:
Alexis Hunted053252010-05-30 07:21:58 +0000609#define DECL(NAME, BASE)
610#define DECL_CONTEXT_BASE(NAME) \
611 if (DK >= first##NAME && DK <= last##NAME) \
612 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
613#include "clang/AST/DeclNodes.inc"
David Blaikie83d382b2011-09-23 05:06:16 +0000614 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000615 }
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000616}
617
618DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000619 Decl::Kind DK = D->getKind();
620 switch(DK) {
Alexis Hunted053252010-05-30 07:21:58 +0000621#define DECL(NAME, BASE)
622#define DECL_CONTEXT(NAME) \
623 case Decl::NAME: \
624 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
625#define DECL_CONTEXT_BASE(NAME)
626#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000627 default:
Alexis Hunted053252010-05-30 07:21:58 +0000628#define DECL(NAME, BASE)
629#define DECL_CONTEXT_BASE(NAME) \
630 if (DK >= first##NAME && DK <= last##NAME) \
631 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
632#include "clang/AST/DeclNodes.inc"
David Blaikie83d382b2011-09-23 05:06:16 +0000633 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000634 }
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000635}
636
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000637SourceLocation Decl::getBodyRBrace() const {
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +0000638 // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
639 // FunctionDecl stores EndRangeLoc for this purpose.
640 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
641 const FunctionDecl *Definition;
642 if (FD->hasBody(Definition))
643 return Definition->getSourceRange().getEnd();
644 return SourceLocation();
645 }
646
Argyrios Kyrtzidis6fbc8fa2010-07-07 11:31:27 +0000647 if (Stmt *Body = getBody())
648 return Body->getSourceRange().getEnd();
649
650 return SourceLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +0000651}
652
Anders Carlssona28908d2009-03-25 23:38:06 +0000653void Decl::CheckAccessDeclContext() const {
Douglas Gregor4b00d3b2010-12-02 00:22:25 +0000654#ifndef NDEBUG
John McCall401982f2010-01-20 21:53:11 +0000655 // Suppress this check if any of the following hold:
656 // 1. this is the translation unit (and thus has no parent)
657 // 2. this is a template parameter (and thus doesn't belong to its context)
Argyrios Kyrtzidise1778632010-09-08 21:58:42 +0000658 // 3. this is a non-type template parameter
659 // 4. the context is not a record
660 // 5. it's invalid
661 // 6. it's a C++0x static_assert.
Anders Carlssonadf36b22009-08-29 20:47:47 +0000662 if (isa<TranslationUnitDecl>(this) ||
Argyrios Kyrtzidisa45855f2010-07-02 11:55:44 +0000663 isa<TemplateTypeParmDecl>(this) ||
Argyrios Kyrtzidise1778632010-09-08 21:58:42 +0000664 isa<NonTypeTemplateParmDecl>(this) ||
Douglas Gregor2b76dd92010-02-22 17:53:38 +0000665 !isa<CXXRecordDecl>(getDeclContext()) ||
Argyrios Kyrtzidis260b4a82010-09-08 21:32:35 +0000666 isInvalidDecl() ||
667 isa<StaticAssertDecl>(this) ||
668 // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
669 // as DeclContext (?).
Argyrios Kyrtzidise1778632010-09-08 21:58:42 +0000670 isa<ParmVarDecl>(this) ||
671 // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
672 // AS_none as access specifier.
Francois Pichet09af8c32011-08-17 01:06:54 +0000673 isa<CXXRecordDecl>(this) ||
674 isa<ClassScopeFunctionSpecializationDecl>(this))
Anders Carlssonadf36b22009-08-29 20:47:47 +0000675 return;
Mike Stump11289f42009-09-09 15:08:12 +0000676
677 assert(Access != AS_none &&
Anders Carlssona28908d2009-03-25 23:38:06 +0000678 "Access specifier is AS_none inside a record decl");
Douglas Gregor4b00d3b2010-12-02 00:22:25 +0000679#endif
Anders Carlssona28908d2009-03-25 23:38:06 +0000680}
681
John McCallb67608f2011-02-22 22:25:23 +0000682DeclContext *Decl::getNonClosureContext() {
John McCallfe96e0b2011-11-06 09:01:30 +0000683 return getDeclContext()->getNonClosureAncestor();
684}
685
686DeclContext *DeclContext::getNonClosureAncestor() {
687 DeclContext *DC = this;
John McCallb67608f2011-02-22 22:25:23 +0000688
689 // This is basically "while (DC->isClosure()) DC = DC->getParent();"
690 // except that it's significantly more efficient to cast to a known
691 // decl type and call getDeclContext() than to call getParent().
John McCall63b45fe2011-06-23 21:18:31 +0000692 while (isa<BlockDecl>(DC))
693 DC = cast<BlockDecl>(DC)->getDeclContext();
John McCallb67608f2011-02-22 22:25:23 +0000694
695 assert(!DC->isClosure());
696 return DC;
697}
Anders Carlssona28908d2009-03-25 23:38:06 +0000698
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000699//===----------------------------------------------------------------------===//
700// DeclContext Implementation
701//===----------------------------------------------------------------------===//
702
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000703bool DeclContext::classof(const Decl *D) {
704 switch (D->getKind()) {
Alexis Hunted053252010-05-30 07:21:58 +0000705#define DECL(NAME, BASE)
706#define DECL_CONTEXT(NAME) case Decl::NAME:
707#define DECL_CONTEXT_BASE(NAME)
708#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000709 return true;
710 default:
Alexis Hunted053252010-05-30 07:21:58 +0000711#define DECL(NAME, BASE)
712#define DECL_CONTEXT_BASE(NAME) \
713 if (D->getKind() >= Decl::first##NAME && \
714 D->getKind() <= Decl::last##NAME) \
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000715 return true;
Alexis Hunted053252010-05-30 07:21:58 +0000716#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000717 return false;
718 }
719}
720
Douglas Gregor9c832f72010-07-25 18:38:02 +0000721DeclContext::~DeclContext() { }
Douglas Gregor91f84212008-12-11 16:49:14 +0000722
Douglas Gregor7f737c02009-09-10 16:57:35 +0000723/// \brief Find the parent context of this context that will be
724/// used for unqualified name lookup.
725///
726/// Generally, the parent lookup context is the semantic context. However, for
727/// a friend function the parent lookup context is the lexical context, which
728/// is the class in which the friend is declared.
729DeclContext *DeclContext::getLookupParent() {
730 // FIXME: Find a better way to identify friends
731 if (isa<FunctionDecl>(this))
Sebastian Redl50c68252010-08-31 00:36:30 +0000732 if (getParent()->getRedeclContext()->isFileContext() &&
733 getLexicalParent()->getRedeclContext()->isRecord())
Douglas Gregor7f737c02009-09-10 16:57:35 +0000734 return getLexicalParent();
735
736 return getParent();
737}
738
Sebastian Redlbd595762010-08-31 20:53:31 +0000739bool DeclContext::isInlineNamespace() const {
740 return isNamespace() &&
741 cast<NamespaceDecl>(this)->isInline();
742}
743
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000744bool DeclContext::isDependentContext() const {
745 if (isFileContext())
746 return false;
747
Douglas Gregor2373c592009-05-31 09:31:02 +0000748 if (isa<ClassTemplatePartialSpecializationDecl>(this))
749 return true;
750
Douglas Gregor680e9e02012-02-21 19:11:17 +0000751 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this)) {
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000752 if (Record->getDescribedClassTemplate())
753 return true;
Douglas Gregor680e9e02012-02-21 19:11:17 +0000754
755 if (Record->isDependentLambda())
756 return true;
757 }
758
John McCallc62bb642010-03-24 05:22:00 +0000759 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000760 if (Function->getDescribedFunctionTemplate())
761 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000762
John McCallc62bb642010-03-24 05:22:00 +0000763 // Friend function declarations are dependent if their *lexical*
764 // context is dependent.
765 if (cast<Decl>(this)->getFriendObjectKind())
766 return getLexicalParent()->isDependentContext();
767 }
768
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000769 return getParent() && getParent()->isDependentContext();
770}
771
Douglas Gregor07665a62009-01-05 19:45:36 +0000772bool DeclContext::isTransparentContext() const {
773 if (DeclKind == Decl::Enum)
Douglas Gregor0bf31402010-10-08 23:50:27 +0000774 return !cast<EnumDecl>(this)->isScoped();
Douglas Gregor07665a62009-01-05 19:45:36 +0000775 else if (DeclKind == Decl::LinkageSpec)
776 return true;
Douglas Gregor07665a62009-01-05 19:45:36 +0000777
778 return false;
779}
780
John McCall5fe84122010-10-26 04:59:26 +0000781bool DeclContext::isExternCContext() const {
782 const DeclContext *DC = this;
783 while (DC->DeclKind != Decl::TranslationUnit) {
784 if (DC->DeclKind == Decl::LinkageSpec)
785 return cast<LinkageSpecDecl>(DC)->getLanguage()
786 == LinkageSpecDecl::lang_c;
787 DC = DC->getParent();
788 }
789 return false;
790}
791
Sebastian Redl50c68252010-08-31 00:36:30 +0000792bool DeclContext::Encloses(const DeclContext *DC) const {
Douglas Gregore985a3b2009-08-27 06:03:53 +0000793 if (getPrimaryContext() != this)
794 return getPrimaryContext()->Encloses(DC);
Mike Stump11289f42009-09-09 15:08:12 +0000795
Douglas Gregore985a3b2009-08-27 06:03:53 +0000796 for (; DC; DC = DC->getParent())
797 if (DC->getPrimaryContext() == this)
798 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000799 return false;
Douglas Gregore985a3b2009-08-27 06:03:53 +0000800}
801
Steve Naroff35c62ae2009-01-08 17:28:14 +0000802DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor91f84212008-12-11 16:49:14 +0000803 switch (DeclKind) {
Douglas Gregor91f84212008-12-11 16:49:14 +0000804 case Decl::TranslationUnit:
Douglas Gregor07665a62009-01-05 19:45:36 +0000805 case Decl::LinkageSpec:
Mike Stump11289f42009-09-09 15:08:12 +0000806 case Decl::Block:
Douglas Gregor91f84212008-12-11 16:49:14 +0000807 // There is only one DeclContext for these entities.
808 return this;
809
810 case Decl::Namespace:
811 // The original namespace is our primary context.
812 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
813
Douglas Gregor91f84212008-12-11 16:49:14 +0000814 case Decl::ObjCMethod:
815 return this;
816
817 case Decl::ObjCInterface:
Douglas Gregor66b310c2011-12-15 18:03:09 +0000818 if (ObjCInterfaceDecl *Def = cast<ObjCInterfaceDecl>(this)->getDefinition())
819 return Def;
820
821 return this;
822
Steve Naroff35c62ae2009-01-08 17:28:14 +0000823 case Decl::ObjCProtocol:
Douglas Gregora715bff2012-01-01 19:51:50 +0000824 if (ObjCProtocolDecl *Def = cast<ObjCProtocolDecl>(this)->getDefinition())
825 return Def;
826
827 return this;
Douglas Gregor66b310c2011-12-15 18:03:09 +0000828
Steve Naroff35c62ae2009-01-08 17:28:14 +0000829 case Decl::ObjCCategory:
Douglas Gregor91f84212008-12-11 16:49:14 +0000830 return this;
831
Steve Naroff35c62ae2009-01-08 17:28:14 +0000832 case Decl::ObjCImplementation:
833 case Decl::ObjCCategoryImpl:
834 return this;
835
Douglas Gregor91f84212008-12-11 16:49:14 +0000836 default:
Alexis Hunted053252010-05-30 07:21:58 +0000837 if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
Douglas Gregor67a65642009-02-17 23:15:12 +0000838 // If this is a tag type that has a definition or is currently
839 // being defined, that definition is our primary context.
John McCalle78aac42010-03-10 03:28:59 +0000840 TagDecl *Tag = cast<TagDecl>(this);
841 assert(isa<TagType>(Tag->TypeForDecl) ||
842 isa<InjectedClassNameType>(Tag->TypeForDecl));
843
844 if (TagDecl *Def = Tag->getDefinition())
845 return Def;
846
847 if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) {
848 const TagType *TagTy = cast<TagType>(Tag->TypeForDecl);
849 if (TagTy->isBeingDefined())
850 // FIXME: is it necessarily being defined in the decl
851 // that owns the type?
852 return TagTy->getDecl();
853 }
854
855 return Tag;
Douglas Gregor67a65642009-02-17 23:15:12 +0000856 }
857
Alexis Hunted053252010-05-30 07:21:58 +0000858 assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
Douglas Gregor91f84212008-12-11 16:49:14 +0000859 "Unknown DeclContext kind");
860 return this;
861 }
862}
863
Douglas Gregore57e7522012-01-07 09:11:48 +0000864void
865DeclContext::collectAllContexts(llvm::SmallVectorImpl<DeclContext *> &Contexts){
866 Contexts.clear();
867
868 if (DeclKind != Decl::Namespace) {
869 Contexts.push_back(this);
870 return;
Douglas Gregor91f84212008-12-11 16:49:14 +0000871 }
Douglas Gregore57e7522012-01-07 09:11:48 +0000872
873 NamespaceDecl *Self = static_cast<NamespaceDecl *>(this);
Douglas Gregorec9fd132012-01-14 16:38:05 +0000874 for (NamespaceDecl *N = Self->getMostRecentDecl(); N;
875 N = N->getPreviousDecl())
Douglas Gregore57e7522012-01-07 09:11:48 +0000876 Contexts.push_back(N);
877
878 std::reverse(Contexts.begin(), Contexts.end());
Douglas Gregor91f84212008-12-11 16:49:14 +0000879}
880
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000881std::pair<Decl *, Decl *>
Bill Wendling8eb771d2012-02-22 09:51:33 +0000882DeclContext::BuildDeclChain(ArrayRef<Decl*> Decls,
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +0000883 bool FieldsAlreadyLoaded) {
Douglas Gregor781f7132012-01-06 16:59:53 +0000884 // Build up a chain of declarations via the Decl::NextInContextAndBits field.
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000885 Decl *FirstNewDecl = 0;
886 Decl *PrevDecl = 0;
887 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +0000888 if (FieldsAlreadyLoaded && isa<FieldDecl>(Decls[I]))
889 continue;
890
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000891 Decl *D = Decls[I];
892 if (PrevDecl)
Douglas Gregor781f7132012-01-06 16:59:53 +0000893 PrevDecl->NextInContextAndBits.setPointer(D);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000894 else
895 FirstNewDecl = D;
896
897 PrevDecl = D;
898 }
899
900 return std::make_pair(FirstNewDecl, PrevDecl);
901}
902
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000903/// \brief Load the declarations within this lexical storage from an
904/// external source.
Mike Stump11289f42009-09-09 15:08:12 +0000905void
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000906DeclContext::LoadLexicalDeclsFromExternalStorage() const {
907 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000908 assert(hasExternalLexicalStorage() && Source && "No external storage?");
909
Argyrios Kyrtzidis98d045e2010-07-30 10:03:23 +0000910 // Notify that we have a DeclContext that is initializing.
911 ExternalASTSource::Deserializing ADeclContext(Source);
Douglas Gregorf337ae92011-08-26 21:23:06 +0000912
Douglas Gregor3d0adb32011-07-15 21:46:17 +0000913 // Load the external declarations, if any.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000914 SmallVector<Decl*, 64> Decls;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000915 ExternalLexicalStorage = false;
Douglas Gregor3d0adb32011-07-15 21:46:17 +0000916 switch (Source->FindExternalLexicalDecls(this, Decls)) {
917 case ELR_Success:
918 break;
919
920 case ELR_Failure:
921 case ELR_AlreadyLoaded:
922 return;
923 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000924
925 if (Decls.empty())
926 return;
927
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +0000928 // We may have already loaded just the fields of this record, in which case
929 // we need to ignore them.
930 bool FieldsAlreadyLoaded = false;
931 if (const RecordDecl *RD = dyn_cast<RecordDecl>(this))
932 FieldsAlreadyLoaded = RD->LoadedFieldsFromExternalStorage;
933
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000934 // Splice the newly-read declarations into the beginning of the list
935 // of declarations.
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000936 Decl *ExternalFirst, *ExternalLast;
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +0000937 llvm::tie(ExternalFirst, ExternalLast) = BuildDeclChain(Decls,
938 FieldsAlreadyLoaded);
Douglas Gregor781f7132012-01-06 16:59:53 +0000939 ExternalLast->NextInContextAndBits.setPointer(FirstDecl);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000940 FirstDecl = ExternalFirst;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000941 if (!LastDecl)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000942 LastDecl = ExternalLast;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000943}
944
John McCall75b960e2010-06-01 09:23:16 +0000945DeclContext::lookup_result
946ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
947 DeclarationName Name) {
948 ASTContext &Context = DC->getParentASTContext();
949 StoredDeclsMap *Map;
Richard Smithf634c902012-03-16 06:12:59 +0000950 if (!(Map = DC->LookupPtr.getPointer()))
John McCall75b960e2010-06-01 09:23:16 +0000951 Map = DC->CreateStoredDeclsMap(Context);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000952
John McCall75b960e2010-06-01 09:23:16 +0000953 StoredDeclsList &List = (*Map)[Name];
954 assert(List.isNull());
955 (void) List;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000956
John McCall75b960e2010-06-01 09:23:16 +0000957 return DeclContext::lookup_result();
958}
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000959
John McCall75b960e2010-06-01 09:23:16 +0000960DeclContext::lookup_result
961ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +0000962 DeclarationName Name,
Argyrios Kyrtzidis94d3f9d2011-09-09 06:44:14 +0000963 ArrayRef<NamedDecl*> Decls) {
John McCall75b960e2010-06-01 09:23:16 +0000964 ASTContext &Context = DC->getParentASTContext();;
965
966 StoredDeclsMap *Map;
Richard Smithf634c902012-03-16 06:12:59 +0000967 if (!(Map = DC->LookupPtr.getPointer()))
John McCall75b960e2010-06-01 09:23:16 +0000968 Map = DC->CreateStoredDeclsMap(Context);
969
970 StoredDeclsList &List = (*Map)[Name];
Argyrios Kyrtzidis94d3f9d2011-09-09 06:44:14 +0000971 for (ArrayRef<NamedDecl*>::iterator
972 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
John McCall75b960e2010-06-01 09:23:16 +0000973 if (List.isNull())
Argyrios Kyrtzidis94d3f9d2011-09-09 06:44:14 +0000974 List.setOnlyValue(*I);
John McCall75b960e2010-06-01 09:23:16 +0000975 else
Argyrios Kyrtzidis94d3f9d2011-09-09 06:44:14 +0000976 List.AddSubsequentDecl(*I);
John McCall75b960e2010-06-01 09:23:16 +0000977 }
978
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000979 return List.getLookupResult();
John McCall75b960e2010-06-01 09:23:16 +0000980}
981
Sebastian Redl66c5eef2010-07-27 00:17:23 +0000982DeclContext::decl_iterator DeclContext::noload_decls_begin() const {
983 return decl_iterator(FirstDecl);
984}
985
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000986DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000987 if (hasExternalLexicalStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000988 LoadLexicalDeclsFromExternalStorage();
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000989
Mike Stump11289f42009-09-09 15:08:12 +0000990 return decl_iterator(FirstDecl);
Douglas Gregorbcced4e2009-04-09 21:40:53 +0000991}
992
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000993bool DeclContext::decls_empty() const {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +0000994 if (hasExternalLexicalStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000995 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +0000996
997 return !FirstDecl;
998}
999
John McCall84d87672009-12-10 09:41:52 +00001000void DeclContext::removeDecl(Decl *D) {
1001 assert(D->getLexicalDeclContext() == this &&
1002 "decl being removed from non-lexical context");
Douglas Gregor781f7132012-01-06 16:59:53 +00001003 assert((D->NextInContextAndBits.getPointer() || D == LastDecl) &&
John McCall84d87672009-12-10 09:41:52 +00001004 "decl is not in decls list");
1005
1006 // Remove D from the decl chain. This is O(n) but hopefully rare.
1007 if (D == FirstDecl) {
1008 if (D == LastDecl)
1009 FirstDecl = LastDecl = 0;
1010 else
Douglas Gregor781f7132012-01-06 16:59:53 +00001011 FirstDecl = D->NextInContextAndBits.getPointer();
John McCall84d87672009-12-10 09:41:52 +00001012 } else {
Douglas Gregor781f7132012-01-06 16:59:53 +00001013 for (Decl *I = FirstDecl; true; I = I->NextInContextAndBits.getPointer()) {
John McCall84d87672009-12-10 09:41:52 +00001014 assert(I && "decl not found in linked list");
Douglas Gregor781f7132012-01-06 16:59:53 +00001015 if (I->NextInContextAndBits.getPointer() == D) {
1016 I->NextInContextAndBits.setPointer(D->NextInContextAndBits.getPointer());
John McCall84d87672009-12-10 09:41:52 +00001017 if (D == LastDecl) LastDecl = I;
1018 break;
1019 }
1020 }
1021 }
1022
1023 // Mark that D is no longer in the decl chain.
Douglas Gregor781f7132012-01-06 16:59:53 +00001024 D->NextInContextAndBits.setPointer(0);
John McCall84d87672009-12-10 09:41:52 +00001025
1026 // Remove D from the lookup table if necessary.
1027 if (isa<NamedDecl>(D)) {
1028 NamedDecl *ND = cast<NamedDecl>(D);
1029
Axel Naumanncb2c52f2011-08-26 14:06:12 +00001030 // Remove only decls that have a name
1031 if (!ND->getDeclName()) return;
1032
Richard Smithf634c902012-03-16 06:12:59 +00001033 StoredDeclsMap *Map = getPrimaryContext()->LookupPtr.getPointer();
John McCallc62bb642010-03-24 05:22:00 +00001034 if (!Map) return;
John McCall84d87672009-12-10 09:41:52 +00001035
John McCall84d87672009-12-10 09:41:52 +00001036 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
1037 assert(Pos != Map->end() && "no lookup entry for decl");
Axel Naumannfbc7b982011-11-08 18:21:06 +00001038 if (Pos->second.getAsVector() || Pos->second.getAsDecl() == ND)
1039 Pos->second.remove(ND);
John McCall84d87672009-12-10 09:41:52 +00001040 }
1041}
1042
John McCalld1e9d832009-08-11 06:59:38 +00001043void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner33f219d2009-02-20 00:56:18 +00001044 assert(D->getLexicalDeclContext() == this &&
1045 "Decl inserted into wrong lexical context");
Mike Stump11289f42009-09-09 15:08:12 +00001046 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor020713e2009-01-09 19:42:16 +00001047 "Decl already inserted into a DeclContext");
1048
1049 if (FirstDecl) {
Douglas Gregor781f7132012-01-06 16:59:53 +00001050 LastDecl->NextInContextAndBits.setPointer(D);
Douglas Gregor020713e2009-01-09 19:42:16 +00001051 LastDecl = D;
1052 } else {
1053 FirstDecl = LastDecl = D;
1054 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +00001055
1056 // Notify a C++ record declaration that we've added a member, so it can
1057 // update it's class-specific state.
1058 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
1059 Record->addedMember(D);
Douglas Gregor0f2a3602011-12-03 00:30:27 +00001060
1061 // If this is a newly-created (not de-serialized) import declaration, wire
1062 // it in to the list of local import declarations.
1063 if (!D->isFromASTFile()) {
1064 if (ImportDecl *Import = dyn_cast<ImportDecl>(D))
1065 D->getASTContext().addedLocalImportDecl(Import);
1066 }
John McCalld1e9d832009-08-11 06:59:38 +00001067}
1068
1069void DeclContext::addDecl(Decl *D) {
1070 addHiddenDecl(D);
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001071
1072 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Richard Smithf634c902012-03-16 06:12:59 +00001073 ND->getDeclContext()->getPrimaryContext()->
1074 makeDeclVisibleInContextWithFlags(ND, false, true);
Douglas Gregor91f84212008-12-11 16:49:14 +00001075}
1076
Sean Callanan95e74be2011-10-21 02:57:43 +00001077void DeclContext::addDeclInternal(Decl *D) {
1078 addHiddenDecl(D);
1079
1080 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Richard Smithf634c902012-03-16 06:12:59 +00001081 ND->getDeclContext()->getPrimaryContext()->
1082 makeDeclVisibleInContextWithFlags(ND, true, true);
1083}
1084
1085/// shouldBeHidden - Determine whether a declaration which was declared
1086/// within its semantic context should be invisible to qualified name lookup.
1087static bool shouldBeHidden(NamedDecl *D) {
1088 // Skip unnamed declarations.
1089 if (!D->getDeclName())
1090 return true;
1091
1092 // Skip entities that can't be found by name lookup into a particular
1093 // context.
1094 if ((D->getIdentifierNamespace() == 0 && !isa<UsingDirectiveDecl>(D)) ||
1095 D->isTemplateParameter())
1096 return true;
1097
1098 // Skip template specializations.
1099 // FIXME: This feels like a hack. Should DeclarationName support
1100 // template-ids, or is there a better way to keep specializations
1101 // from being visible?
1102 if (isa<ClassTemplateSpecializationDecl>(D))
1103 return true;
1104 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1105 if (FD->isFunctionTemplateSpecialization())
1106 return true;
1107
1108 return false;
1109}
1110
1111/// buildLookup - Build the lookup data structure with all of the
1112/// declarations in this DeclContext (and any other contexts linked
1113/// to it or transparent contexts nested within it) and return it.
1114StoredDeclsMap *DeclContext::buildLookup() {
1115 assert(this == getPrimaryContext() && "buildLookup called on non-primary DC");
1116
1117 if (!LookupPtr.getInt())
1118 return LookupPtr.getPointer();
1119
1120 llvm::SmallVector<DeclContext *, 2> Contexts;
1121 collectAllContexts(Contexts);
1122 for (unsigned I = 0, N = Contexts.size(); I != N; ++I)
1123 buildLookupImpl(Contexts[I]);
1124
1125 // We no longer have any lazy decls.
1126 LookupPtr.setInt(false);
1127 return LookupPtr.getPointer();
1128}
1129
1130/// buildLookupImpl - Build part of the lookup data structure for the
1131/// declarations contained within DCtx, which will either be this
1132/// DeclContext, a DeclContext linked to it, or a transparent context
1133/// nested within it.
1134void DeclContext::buildLookupImpl(DeclContext *DCtx) {
1135 for (decl_iterator I = DCtx->decls_begin(), E = DCtx->decls_end();
1136 I != E; ++I) {
1137 Decl *D = *I;
1138
1139 // Insert this declaration into the lookup structure, but only if
1140 // it's semantically within its decl context. Any other decls which
1141 // should be found in this context are added eagerly.
1142 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1143 if (ND->getDeclContext() == DCtx && !shouldBeHidden(ND))
1144 makeDeclVisibleInContextImpl(ND, false);
1145
1146 // If this declaration is itself a transparent declaration context
1147 // or inline namespace, add the members of this declaration of that
1148 // context (recursively).
1149 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(D))
1150 if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
1151 buildLookupImpl(InnerCtx);
1152 }
Sean Callanan95e74be2011-10-21 02:57:43 +00001153}
1154
Mike Stump11289f42009-09-09 15:08:12 +00001155DeclContext::lookup_result
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001156DeclContext::lookup(DeclarationName Name) {
Nick Lewycky2bd636f2012-03-13 04:12:34 +00001157 assert(DeclKind != Decl::LinkageSpec &&
1158 "Should not perform lookups into linkage specs!");
1159
Steve Naroff35c62ae2009-01-08 17:28:14 +00001160 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor91f84212008-12-11 16:49:14 +00001161 if (PrimaryContext != this)
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001162 return PrimaryContext->lookup(Name);
Douglas Gregor91f84212008-12-11 16:49:14 +00001163
Richard Smith05afe5e2012-03-13 03:12:56 +00001164 if (hasExternalVisibleStorage()) {
Richard Smithf634c902012-03-16 06:12:59 +00001165 // If a PCH has a result for this name, and we have a local declaration, we
1166 // will have imported the PCH result when adding the local declaration.
1167 // FIXME: For modules, we could have had more declarations added by module
1168 // imoprts since we saw the declaration of the local name.
1169 if (StoredDeclsMap *Map = LookupPtr.getPointer()) {
1170 StoredDeclsMap::iterator I = Map->find(Name);
1171 if (I != Map->end())
1172 return I->second.getLookupResult();
1173 }
1174
John McCall75b960e2010-06-01 09:23:16 +00001175 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1176 return Source->FindExternalVisibleDeclsByName(this, Name);
1177 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001178
Richard Smithf634c902012-03-16 06:12:59 +00001179 StoredDeclsMap *Map = LookupPtr.getPointer();
1180 if (LookupPtr.getInt())
1181 Map = buildLookup();
1182
1183 if (!Map)
1184 return lookup_result(lookup_iterator(0), lookup_iterator(0));
1185
1186 StoredDeclsMap::iterator I = Map->find(Name);
1187 if (I == Map->end())
1188 return lookup_result(lookup_iterator(0), lookup_iterator(0));
1189
1190 return I->second.getLookupResult();
Douglas Gregor91f84212008-12-11 16:49:14 +00001191}
1192
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001193void DeclContext::localUncachedLookup(DeclarationName Name,
1194 llvm::SmallVectorImpl<NamedDecl *> &Results) {
1195 Results.clear();
1196
1197 // If there's no external storage, just perform a normal lookup and copy
1198 // the results.
1199 if (!hasExternalVisibleStorage() && !hasExternalLexicalStorage()) {
1200 lookup_result LookupResults = lookup(Name);
1201 Results.insert(Results.end(), LookupResults.first, LookupResults.second);
1202 return;
1203 }
1204
1205 // If we have a lookup table, check there first. Maybe we'll get lucky.
Richard Smithf634c902012-03-16 06:12:59 +00001206 if (StoredDeclsMap *Map = LookupPtr.getPointer()) {
1207 StoredDeclsMap::iterator Pos = Map->find(Name);
1208 if (Pos != Map->end()) {
1209 Results.insert(Results.end(),
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001210 Pos->second.getLookupResult().first,
1211 Pos->second.getLookupResult().second);
1212 return;
1213 }
1214 }
1215
1216 // Slow case: grovel through the declarations in our chain looking for
1217 // matches.
1218 for (Decl *D = FirstDecl; D; D = D->getNextDeclInContext()) {
1219 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1220 if (ND->getDeclName() == Name)
1221 Results.push_back(ND);
1222 }
1223}
1224
Sebastian Redl50c68252010-08-31 00:36:30 +00001225DeclContext *DeclContext::getRedeclContext() {
Chris Lattner17a1bfa2009-03-27 19:19:59 +00001226 DeclContext *Ctx = this;
Sebastian Redlbd595762010-08-31 20:53:31 +00001227 // Skip through transparent contexts.
1228 while (Ctx->isTransparentContext())
Douglas Gregor6ad0ef52009-01-06 23:51:29 +00001229 Ctx = Ctx->getParent();
1230 return Ctx;
1231}
1232
Douglas Gregorf47b9112009-02-25 22:02:03 +00001233DeclContext *DeclContext::getEnclosingNamespaceContext() {
1234 DeclContext *Ctx = this;
1235 // Skip through non-namespace, non-translation-unit contexts.
Sebastian Redl4f08c962010-08-31 00:36:23 +00001236 while (!Ctx->isFileContext())
Douglas Gregorf47b9112009-02-25 22:02:03 +00001237 Ctx = Ctx->getParent();
1238 return Ctx->getPrimaryContext();
1239}
1240
Sebastian Redl50c68252010-08-31 00:36:30 +00001241bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
1242 // For non-file contexts, this is equivalent to Equals.
1243 if (!isFileContext())
1244 return O->Equals(this);
1245
1246 do {
1247 if (O->Equals(this))
1248 return true;
1249
1250 const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O);
1251 if (!NS || !NS->isInline())
1252 break;
1253 O = NS->getParent();
1254 } while (O);
1255
1256 return false;
1257}
1258
Richard Smithf634c902012-03-16 06:12:59 +00001259void DeclContext::makeDeclVisibleInContext(NamedDecl *D) {
1260 DeclContext *PrimaryDC = this->getPrimaryContext();
1261 DeclContext *DeclDC = D->getDeclContext()->getPrimaryContext();
1262 // If the decl is being added outside of its semantic decl context, we
1263 // need to ensure that we eagerly build the lookup information for it.
1264 PrimaryDC->makeDeclVisibleInContextWithFlags(D, false, PrimaryDC == DeclDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00001265}
1266
Richard Smithf634c902012-03-16 06:12:59 +00001267void DeclContext::makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal,
1268 bool Recoverable) {
1269 assert(this == getPrimaryContext() && "expected a primary DC");
Sean Callanan95e74be2011-10-21 02:57:43 +00001270
Richard Smith05afe5e2012-03-13 03:12:56 +00001271 // Skip declarations within functions.
1272 // FIXME: We shouldn't need to build lookup tables for function declarations
1273 // ever, and we can't do so correctly because we can't model the nesting of
1274 // scopes which occurs within functions. We use "qualified" lookup into
1275 // function declarations when handling friend declarations inside nested
1276 // classes, and consequently accept the following invalid code:
1277 //
1278 // void f() { void g(); { int g; struct S { friend void g(); }; } }
1279 if (isFunctionOrMethod() && !isa<FunctionDecl>(D))
1280 return;
1281
Richard Smithf634c902012-03-16 06:12:59 +00001282 // Skip declarations which should be invisible to name lookup.
1283 if (shouldBeHidden(D))
1284 return;
1285
1286 // If we already have a lookup data structure, perform the insertion into
1287 // it. If we might have externally-stored decls with this name, look them
1288 // up and perform the insertion. If this decl was declared outside its
1289 // semantic context, buildLookup won't add it, so add it now.
1290 //
1291 // FIXME: As a performance hack, don't add such decls into the translation
1292 // unit unless we're in C++, since qualified lookup into the TU is never
1293 // performed.
1294 if (LookupPtr.getPointer() || hasExternalVisibleStorage() ||
1295 ((!Recoverable || D->getDeclContext() != D->getLexicalDeclContext()) &&
1296 (getParentASTContext().getLangOpts().CPlusPlus ||
1297 !isTranslationUnit()))) {
1298 // If we have lazily omitted any decls, they might have the same name as
1299 // the decl which we are adding, so build a full lookup table before adding
1300 // this decl.
1301 buildLookup();
1302 makeDeclVisibleInContextImpl(D, Internal);
1303 } else {
1304 LookupPtr.setInt(true);
1305 }
1306
1307 // If we are a transparent context or inline namespace, insert into our
1308 // parent context, too. This operation is recursive.
1309 if (isTransparentContext() || isInlineNamespace())
1310 getParent()->getPrimaryContext()->
1311 makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
1312
1313 Decl *DCAsDecl = cast<Decl>(this);
1314 // Notify that a decl was made visible unless we are a Tag being defined.
1315 if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined()))
1316 if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
1317 L->AddedVisibleDecl(this, D);
1318}
1319
1320void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal) {
1321 // Find or create the stored declaration map.
1322 StoredDeclsMap *Map = LookupPtr.getPointer();
1323 if (!Map) {
1324 ASTContext *C = &getParentASTContext();
1325 Map = CreateStoredDeclsMap(*C);
Argyrios Kyrtzidise51e5542010-07-04 21:44:25 +00001326 }
1327
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001328 // If there is an external AST source, load any declarations it knows about
1329 // with this declaration's name.
1330 // If the lookup table contains an entry about this name it means that we
1331 // have already checked the external source.
Sean Callanan95e74be2011-10-21 02:57:43 +00001332 if (!Internal)
1333 if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
1334 if (hasExternalVisibleStorage() &&
Richard Smithf634c902012-03-16 06:12:59 +00001335 Map->find(D->getDeclName()) == Map->end())
Sean Callanan95e74be2011-10-21 02:57:43 +00001336 Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001337
Douglas Gregor91f84212008-12-11 16:49:14 +00001338 // Insert this declaration into the map.
Richard Smithf634c902012-03-16 06:12:59 +00001339 StoredDeclsList &DeclNameEntries = (*Map)[D->getDeclName()];
Chris Lattnercaae7162009-02-20 01:44:05 +00001340 if (DeclNameEntries.isNull()) {
1341 DeclNameEntries.setOnlyValue(D);
Richard Smithf634c902012-03-16 06:12:59 +00001342 return;
Douglas Gregor91f84212008-12-11 16:49:14 +00001343 }
Chris Lattner24e24d52009-02-20 00:55:03 +00001344
Richard Smithf634c902012-03-16 06:12:59 +00001345 if (DeclNameEntries.HandleRedeclaration(D)) {
1346 // This declaration has replaced an existing one for which
1347 // declarationReplaces returns true.
1348 return;
1349 }
Mike Stump11289f42009-09-09 15:08:12 +00001350
Richard Smithf634c902012-03-16 06:12:59 +00001351 // Put this declaration into the appropriate slot.
1352 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor91f84212008-12-11 16:49:14 +00001353}
Douglas Gregor889ceb72009-02-03 19:21:40 +00001354
1355/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
1356/// this context.
Mike Stump11289f42009-09-09 15:08:12 +00001357DeclContext::udir_iterator_range
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001358DeclContext::getUsingDirectives() const {
Richard Smith05afe5e2012-03-13 03:12:56 +00001359 // FIXME: Use something more efficient than normal lookup for using
1360 // directives. In C++, using directives are looked up more than anything else.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001361 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor889ceb72009-02-03 19:21:40 +00001362 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
1363 reinterpret_cast<udir_iterator>(Result.second));
1364}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001365
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001366//===----------------------------------------------------------------------===//
1367// Creation and Destruction of StoredDeclsMaps. //
1368//===----------------------------------------------------------------------===//
1369
John McCallc62bb642010-03-24 05:22:00 +00001370StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
Richard Smithf634c902012-03-16 06:12:59 +00001371 assert(!LookupPtr.getPointer() && "context already has a decls map");
John McCallc62bb642010-03-24 05:22:00 +00001372 assert(getPrimaryContext() == this &&
1373 "creating decls map on non-primary context");
1374
1375 StoredDeclsMap *M;
1376 bool Dependent = isDependentContext();
1377 if (Dependent)
1378 M = new DependentStoredDeclsMap();
1379 else
1380 M = new StoredDeclsMap();
1381 M->Previous = C.LastSDM;
1382 C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
Richard Smithf634c902012-03-16 06:12:59 +00001383 LookupPtr.setPointer(M);
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001384 return M;
1385}
1386
1387void ASTContext::ReleaseDeclContextMaps() {
John McCallc62bb642010-03-24 05:22:00 +00001388 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1389 // pointer because the subclass doesn't add anything that needs to
1390 // be deleted.
John McCallc62bb642010-03-24 05:22:00 +00001391 StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1392}
1393
1394void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1395 while (Map) {
1396 // Advance the iteration before we invalidate memory.
1397 llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1398
1399 if (Dependent)
1400 delete static_cast<DependentStoredDeclsMap*>(Map);
1401 else
1402 delete Map;
1403
1404 Map = Next.getPointer();
1405 Dependent = Next.getInt();
1406 }
1407}
1408
John McCallc62bb642010-03-24 05:22:00 +00001409DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1410 DeclContext *Parent,
1411 const PartialDiagnostic &PDiag) {
1412 assert(Parent->isDependentContext()
1413 && "cannot iterate dependent diagnostics of non-dependent context");
1414 Parent = Parent->getPrimaryContext();
Richard Smithf634c902012-03-16 06:12:59 +00001415 if (!Parent->LookupPtr.getPointer())
John McCallc62bb642010-03-24 05:22:00 +00001416 Parent->CreateStoredDeclsMap(C);
1417
1418 DependentStoredDeclsMap *Map
Richard Smithf634c902012-03-16 06:12:59 +00001419 = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr.getPointer());
John McCallc62bb642010-03-24 05:22:00 +00001420
Douglas Gregora55530e2010-03-29 23:56:53 +00001421 // Allocate the copy of the PartialDiagnostic via the ASTContext's
Douglas Gregor89336232010-03-29 23:34:08 +00001422 // BumpPtrAllocator, rather than the ASTContext itself.
Douglas Gregora55530e2010-03-29 23:56:53 +00001423 PartialDiagnostic::Storage *DiagStorage = 0;
1424 if (PDiag.hasStorage())
1425 DiagStorage = new (C) PartialDiagnostic::Storage;
1426
1427 DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
John McCallc62bb642010-03-24 05:22:00 +00001428
1429 // TODO: Maybe we shouldn't reverse the order during insertion.
1430 DD->NextDiagnostic = Map->FirstDiagnostic;
1431 Map->FirstDiagnostic = DD;
1432
1433 return DD;
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001434}