blob: 870b8f11d786c5b32f24b871294dec908b1f8ade [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
Rafael Espindola88806c22012-11-23 14:29:54 +0000264 // Check redeclarations. We merge attributes, so we don't need to check
265 // attributes in all redeclarations.
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000266 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Rafael Espindola88806c22012-11-23 14:29:54 +0000267 if (I->Used)
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000268 return true;
269 }
270
271 return false;
272}
273
Argyrios Kyrtzidis16180232011-04-19 19:51:10 +0000274bool Decl::isReferenced() const {
275 if (Referenced)
276 return true;
277
278 // Check redeclarations.
279 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
280 if (I->Referenced)
281 return true;
282
283 return false;
284}
285
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000286/// \brief Determine the availability of the given declaration based on
287/// the target platform.
288///
289/// When it returns an availability result other than \c AR_Available,
290/// if the \p Message parameter is non-NULL, it will be set to a
291/// string describing why the entity is unavailable.
292///
293/// FIXME: Make these strings localizable, since they end up in
294/// diagnostics.
295static AvailabilityResult CheckAvailability(ASTContext &Context,
296 const AvailabilityAttr *A,
297 std::string *Message) {
Douglas Gregore8bbc122011-09-02 00:18:52 +0000298 StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000299 StringRef PrettyPlatformName
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000300 = AvailabilityAttr::getPrettyPlatformName(TargetPlatform);
301 if (PrettyPlatformName.empty())
302 PrettyPlatformName = TargetPlatform;
303
Douglas Gregore8bbc122011-09-02 00:18:52 +0000304 VersionTuple TargetMinVersion = Context.getTargetInfo().getPlatformMinVersion();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000305 if (TargetMinVersion.empty())
306 return AR_Available;
307
308 // Match the platform name.
309 if (A->getPlatform()->getName() != TargetPlatform)
310 return AR_Available;
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000311
312 std::string HintMessage;
313 if (!A->getMessage().empty()) {
314 HintMessage = " - ";
315 HintMessage += A->getMessage();
316 }
317
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000318 // Make sure that this declaration has not been marked 'unavailable'.
319 if (A->getUnavailable()) {
320 if (Message) {
321 Message->clear();
322 llvm::raw_string_ostream Out(*Message);
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000323 Out << "not available on " << PrettyPlatformName
324 << HintMessage;
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000325 }
326
327 return AR_Unavailable;
328 }
329
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000330 // Make sure that this declaration has already been introduced.
331 if (!A->getIntroduced().empty() &&
332 TargetMinVersion < A->getIntroduced()) {
333 if (Message) {
334 Message->clear();
335 llvm::raw_string_ostream Out(*Message);
336 Out << "introduced in " << PrettyPlatformName << ' '
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000337 << A->getIntroduced() << HintMessage;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000338 }
339
340 return AR_NotYetIntroduced;
341 }
342
343 // Make sure that this declaration hasn't been obsoleted.
344 if (!A->getObsoleted().empty() && TargetMinVersion >= A->getObsoleted()) {
345 if (Message) {
346 Message->clear();
347 llvm::raw_string_ostream Out(*Message);
348 Out << "obsoleted in " << PrettyPlatformName << ' '
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000349 << A->getObsoleted() << HintMessage;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000350 }
351
352 return AR_Unavailable;
353 }
354
355 // Make sure that this declaration hasn't been deprecated.
356 if (!A->getDeprecated().empty() && TargetMinVersion >= A->getDeprecated()) {
357 if (Message) {
358 Message->clear();
359 llvm::raw_string_ostream Out(*Message);
360 Out << "first deprecated in " << PrettyPlatformName << ' '
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000361 << A->getDeprecated() << HintMessage;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000362 }
363
364 return AR_Deprecated;
365 }
366
367 return AR_Available;
368}
369
370AvailabilityResult Decl::getAvailability(std::string *Message) const {
371 AvailabilityResult Result = AR_Available;
372 std::string ResultMessage;
373
374 for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
375 if (DeprecatedAttr *Deprecated = dyn_cast<DeprecatedAttr>(*A)) {
376 if (Result >= AR_Deprecated)
377 continue;
378
379 if (Message)
380 ResultMessage = Deprecated->getMessage();
381
382 Result = AR_Deprecated;
383 continue;
384 }
385
386 if (UnavailableAttr *Unavailable = dyn_cast<UnavailableAttr>(*A)) {
387 if (Message)
388 *Message = Unavailable->getMessage();
389 return AR_Unavailable;
390 }
391
392 if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
393 AvailabilityResult AR = CheckAvailability(getASTContext(), Availability,
394 Message);
395
396 if (AR == AR_Unavailable)
397 return AR_Unavailable;
398
399 if (AR > Result) {
400 Result = AR;
401 if (Message)
402 ResultMessage.swap(*Message);
403 }
404 continue;
405 }
406 }
407
408 if (Message)
409 Message->swap(ResultMessage);
410 return Result;
411}
412
413bool Decl::canBeWeakImported(bool &IsDefinition) const {
414 IsDefinition = false;
John McCall5fb5df92012-06-20 06:18:46 +0000415
416 // Variables, if they aren't definitions.
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000417 if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
418 if (!Var->hasExternalStorage() || Var->getInit()) {
419 IsDefinition = true;
420 return false;
421 }
John McCall5fb5df92012-06-20 06:18:46 +0000422 return true;
423
424 // Functions, if they aren't definitions.
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000425 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
426 if (FD->hasBody()) {
427 IsDefinition = true;
428 return false;
429 }
John McCall5fb5df92012-06-20 06:18:46 +0000430 return true;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000431
John McCall5fb5df92012-06-20 06:18:46 +0000432 // Objective-C classes, if this is the non-fragile runtime.
433 } else if (isa<ObjCInterfaceDecl>(this) &&
John McCall18ac1632012-06-20 21:58:02 +0000434 getASTContext().getLangOpts().ObjCRuntime.hasWeakClassImport()) {
John McCall5fb5df92012-06-20 06:18:46 +0000435 return true;
436
437 // Nothing else.
438 } else {
439 return false;
440 }
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000441}
442
443bool Decl::isWeakImported() const {
444 bool IsDefinition;
445 if (!canBeWeakImported(IsDefinition))
446 return false;
447
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000448 for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
449 if (isa<WeakImportAttr>(*A))
450 return true;
451
452 if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
453 if (CheckAvailability(getASTContext(), Availability, 0)
454 == AR_NotYetIntroduced)
455 return true;
456 }
457 }
458
459 return false;
460}
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000461
Chris Lattner8e097192009-03-27 20:18:19 +0000462unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
463 switch (DeclKind) {
John McCall3f746822009-11-17 05:59:44 +0000464 case Function:
465 case CXXMethod:
466 case CXXConstructor:
467 case CXXDestructor:
468 case CXXConversion:
Chris Lattner8e097192009-03-27 20:18:19 +0000469 case EnumConstant:
470 case Var:
471 case ImplicitParam:
472 case ParmVar:
Chris Lattner8e097192009-03-27 20:18:19 +0000473 case NonTypeTemplateParm:
474 case ObjCMethod:
Daniel Dunbar45b2d8a2010-04-23 13:07:39 +0000475 case ObjCProperty:
Daniel Dunbar45b2d8a2010-04-23 13:07:39 +0000476 return IDNS_Ordinary;
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000477 case Label:
478 return IDNS_Label;
Francois Pichet783dd6e2010-11-21 06:08:52 +0000479 case IndirectField:
480 return IDNS_Ordinary | IDNS_Member;
481
John McCalle87beb22010-04-23 18:46:30 +0000482 case ObjCCompatibleAlias:
483 case ObjCInterface:
484 return IDNS_Ordinary | IDNS_Type;
485
486 case Typedef:
Richard Smithdda56e42011-04-15 14:24:37 +0000487 case TypeAlias:
Richard Smith3f1b5d02011-05-05 21:57:07 +0000488 case TypeAliasTemplate:
John McCalle87beb22010-04-23 18:46:30 +0000489 case UnresolvedUsingTypename:
490 case TemplateTypeParm:
491 return IDNS_Ordinary | IDNS_Type;
492
John McCall3f746822009-11-17 05:59:44 +0000493 case UsingShadow:
494 return 0; // we'll actually overwrite this later
495
John McCalle61f2ba2009-11-18 02:36:19 +0000496 case UnresolvedUsingValue:
John McCalle61f2ba2009-11-18 02:36:19 +0000497 return IDNS_Ordinary | IDNS_Using;
John McCall3f746822009-11-17 05:59:44 +0000498
499 case Using:
500 return IDNS_Using;
501
Chris Lattner8e097192009-03-27 20:18:19 +0000502 case ObjCProtocol:
Douglas Gregor79947a22009-04-24 00:11:27 +0000503 return IDNS_ObjCProtocol;
Mike Stump11289f42009-09-09 15:08:12 +0000504
Chris Lattner8e097192009-03-27 20:18:19 +0000505 case Field:
506 case ObjCAtDefsField:
507 case ObjCIvar:
508 return IDNS_Member;
Mike Stump11289f42009-09-09 15:08:12 +0000509
Chris Lattner8e097192009-03-27 20:18:19 +0000510 case Record:
511 case CXXRecord:
512 case Enum:
John McCalle87beb22010-04-23 18:46:30 +0000513 return IDNS_Tag | IDNS_Type;
Mike Stump11289f42009-09-09 15:08:12 +0000514
Chris Lattner8e097192009-03-27 20:18:19 +0000515 case Namespace:
John McCalle87beb22010-04-23 18:46:30 +0000516 case NamespaceAlias:
517 return IDNS_Namespace;
518
Chris Lattner8e097192009-03-27 20:18:19 +0000519 case FunctionTemplate:
John McCalle87beb22010-04-23 18:46:30 +0000520 return IDNS_Ordinary;
521
Chris Lattner8e097192009-03-27 20:18:19 +0000522 case ClassTemplate:
523 case TemplateTemplateParm:
John McCalle87beb22010-04-23 18:46:30 +0000524 return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
Mike Stump11289f42009-09-09 15:08:12 +0000525
Chris Lattner8e097192009-03-27 20:18:19 +0000526 // Never have names.
John McCallaa74a0c2009-08-28 07:59:38 +0000527 case Friend:
John McCall11083da2009-09-16 22:47:08 +0000528 case FriendTemplate:
Abramo Bagnarad7340582010-06-05 05:09:32 +0000529 case AccessSpec:
Chris Lattner8e097192009-03-27 20:18:19 +0000530 case LinkageSpec:
531 case FileScopeAsm:
532 case StaticAssert:
Chris Lattner8e097192009-03-27 20:18:19 +0000533 case ObjCPropertyImpl:
Chris Lattner8e097192009-03-27 20:18:19 +0000534 case Block:
535 case TranslationUnit:
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000536
Chris Lattner8e097192009-03-27 20:18:19 +0000537 case UsingDirective:
538 case ClassTemplateSpecialization:
Douglas Gregor2373c592009-05-31 09:31:02 +0000539 case ClassTemplatePartialSpecialization:
Francois Pichet00c7e6c2011-08-14 03:52:19 +0000540 case ClassScopeFunctionSpecialization:
Douglas Gregore93525e2010-04-22 23:19:50 +0000541 case ObjCImplementation:
542 case ObjCCategory:
543 case ObjCCategoryImpl:
Douglas Gregorba345522011-12-02 23:23:56 +0000544 case Import:
Douglas Gregore93525e2010-04-22 23:19:50 +0000545 // Never looked up by name.
Chris Lattner8e097192009-03-27 20:18:19 +0000546 return 0;
547 }
John McCall3f746822009-11-17 05:59:44 +0000548
David Blaikiee4d798f2012-01-20 21:50:17 +0000549 llvm_unreachable("Invalid DeclKind!");
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000550}
551
Argyrios Kyrtzidis6f40eb72012-02-09 02:44:08 +0000552void Decl::setAttrsImpl(const AttrVec &attrs, ASTContext &Ctx) {
Argyrios Kyrtzidis91167172010-06-11 23:09:25 +0000553 assert(!HasAttrs && "Decl already contains attrs.");
554
Argyrios Kyrtzidis6f40eb72012-02-09 02:44:08 +0000555 AttrVec &AttrBlank = Ctx.getDeclAttrs(this);
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000556 assert(AttrBlank.empty() && "HasAttrs was wrong?");
Argyrios Kyrtzidis91167172010-06-11 23:09:25 +0000557
558 AttrBlank = attrs;
559 HasAttrs = true;
560}
561
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000562void Decl::dropAttrs() {
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000563 if (!HasAttrs) return;
Mike Stump11289f42009-09-09 15:08:12 +0000564
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000565 HasAttrs = false;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000566 getASTContext().eraseDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000567}
568
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000569const AttrVec &Decl::getAttrs() const {
570 assert(HasAttrs && "No attrs to get!");
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000571 return getASTContext().getDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000572}
573
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000574void Decl::swapAttrs(Decl *RHS) {
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000575 bool HasLHSAttr = this->HasAttrs;
576 bool HasRHSAttr = RHS->HasAttrs;
Mike Stump11289f42009-09-09 15:08:12 +0000577
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000578 // Usually, neither decl has attrs, nothing to do.
579 if (!HasLHSAttr && !HasRHSAttr) return;
Mike Stump11289f42009-09-09 15:08:12 +0000580
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000581 // If 'this' has no attrs, swap the other way.
582 if (!HasLHSAttr)
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000583 return RHS->swapAttrs(this);
Mike Stump11289f42009-09-09 15:08:12 +0000584
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000585 ASTContext &Context = getASTContext();
Mike Stump11289f42009-09-09 15:08:12 +0000586
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000587 // Handle the case when both decls have attrs.
588 if (HasRHSAttr) {
Douglas Gregor78bd61f2009-06-18 16:11:24 +0000589 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000590 return;
591 }
Mike Stump11289f42009-09-09 15:08:12 +0000592
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000593 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor78bd61f2009-06-18 16:11:24 +0000594 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
595 Context.eraseDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000596 this->HasAttrs = false;
597 RHS->HasAttrs = true;
598}
599
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000600Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000601 Decl::Kind DK = D->getDeclKind();
602 switch(DK) {
Alexis Hunted053252010-05-30 07:21:58 +0000603#define DECL(NAME, BASE)
604#define DECL_CONTEXT(NAME) \
605 case Decl::NAME: \
606 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
607#define DECL_CONTEXT_BASE(NAME)
608#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000609 default:
Alexis Hunted053252010-05-30 07:21:58 +0000610#define DECL(NAME, BASE)
611#define DECL_CONTEXT_BASE(NAME) \
612 if (DK >= first##NAME && DK <= last##NAME) \
613 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
614#include "clang/AST/DeclNodes.inc"
David Blaikie83d382b2011-09-23 05:06:16 +0000615 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000616 }
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000617}
618
619DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000620 Decl::Kind DK = D->getKind();
621 switch(DK) {
Alexis Hunted053252010-05-30 07:21:58 +0000622#define DECL(NAME, BASE)
623#define DECL_CONTEXT(NAME) \
624 case Decl::NAME: \
625 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
626#define DECL_CONTEXT_BASE(NAME)
627#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000628 default:
Alexis Hunted053252010-05-30 07:21:58 +0000629#define DECL(NAME, BASE)
630#define DECL_CONTEXT_BASE(NAME) \
631 if (DK >= first##NAME && DK <= last##NAME) \
632 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
633#include "clang/AST/DeclNodes.inc"
David Blaikie83d382b2011-09-23 05:06:16 +0000634 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000635 }
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000636}
637
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000638SourceLocation Decl::getBodyRBrace() const {
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +0000639 // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
640 // FunctionDecl stores EndRangeLoc for this purpose.
641 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
642 const FunctionDecl *Definition;
643 if (FD->hasBody(Definition))
644 return Definition->getSourceRange().getEnd();
645 return SourceLocation();
646 }
647
Argyrios Kyrtzidis6fbc8fa2010-07-07 11:31:27 +0000648 if (Stmt *Body = getBody())
649 return Body->getSourceRange().getEnd();
650
651 return SourceLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +0000652}
653
Anders Carlssona28908d2009-03-25 23:38:06 +0000654void Decl::CheckAccessDeclContext() const {
Douglas Gregor4b00d3b2010-12-02 00:22:25 +0000655#ifndef NDEBUG
John McCall401982f2010-01-20 21:53:11 +0000656 // Suppress this check if any of the following hold:
657 // 1. this is the translation unit (and thus has no parent)
658 // 2. this is a template parameter (and thus doesn't belong to its context)
Argyrios Kyrtzidise1778632010-09-08 21:58:42 +0000659 // 3. this is a non-type template parameter
660 // 4. the context is not a record
661 // 5. it's invalid
662 // 6. it's a C++0x static_assert.
Anders Carlssonadf36b22009-08-29 20:47:47 +0000663 if (isa<TranslationUnitDecl>(this) ||
Argyrios Kyrtzidisa45855f2010-07-02 11:55:44 +0000664 isa<TemplateTypeParmDecl>(this) ||
Argyrios Kyrtzidise1778632010-09-08 21:58:42 +0000665 isa<NonTypeTemplateParmDecl>(this) ||
Douglas Gregor2b76dd92010-02-22 17:53:38 +0000666 !isa<CXXRecordDecl>(getDeclContext()) ||
Argyrios Kyrtzidis260b4a82010-09-08 21:32:35 +0000667 isInvalidDecl() ||
668 isa<StaticAssertDecl>(this) ||
669 // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
670 // as DeclContext (?).
Argyrios Kyrtzidise1778632010-09-08 21:58:42 +0000671 isa<ParmVarDecl>(this) ||
672 // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
673 // AS_none as access specifier.
Francois Pichet09af8c32011-08-17 01:06:54 +0000674 isa<CXXRecordDecl>(this) ||
675 isa<ClassScopeFunctionSpecializationDecl>(this))
Anders Carlssonadf36b22009-08-29 20:47:47 +0000676 return;
Mike Stump11289f42009-09-09 15:08:12 +0000677
678 assert(Access != AS_none &&
Anders Carlssona28908d2009-03-25 23:38:06 +0000679 "Access specifier is AS_none inside a record decl");
Douglas Gregor4b00d3b2010-12-02 00:22:25 +0000680#endif
Anders Carlssona28908d2009-03-25 23:38:06 +0000681}
682
John McCallb67608f2011-02-22 22:25:23 +0000683DeclContext *Decl::getNonClosureContext() {
John McCallfe96e0b2011-11-06 09:01:30 +0000684 return getDeclContext()->getNonClosureAncestor();
685}
686
687DeclContext *DeclContext::getNonClosureAncestor() {
688 DeclContext *DC = this;
John McCallb67608f2011-02-22 22:25:23 +0000689
690 // This is basically "while (DC->isClosure()) DC = DC->getParent();"
691 // except that it's significantly more efficient to cast to a known
692 // decl type and call getDeclContext() than to call getParent().
John McCall63b45fe2011-06-23 21:18:31 +0000693 while (isa<BlockDecl>(DC))
694 DC = cast<BlockDecl>(DC)->getDeclContext();
John McCallb67608f2011-02-22 22:25:23 +0000695
696 assert(!DC->isClosure());
697 return DC;
698}
Anders Carlssona28908d2009-03-25 23:38:06 +0000699
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000700//===----------------------------------------------------------------------===//
701// DeclContext Implementation
702//===----------------------------------------------------------------------===//
703
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000704bool DeclContext::classof(const Decl *D) {
705 switch (D->getKind()) {
Alexis Hunted053252010-05-30 07:21:58 +0000706#define DECL(NAME, BASE)
707#define DECL_CONTEXT(NAME) case Decl::NAME:
708#define DECL_CONTEXT_BASE(NAME)
709#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000710 return true;
711 default:
Alexis Hunted053252010-05-30 07:21:58 +0000712#define DECL(NAME, BASE)
713#define DECL_CONTEXT_BASE(NAME) \
714 if (D->getKind() >= Decl::first##NAME && \
715 D->getKind() <= Decl::last##NAME) \
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000716 return true;
Alexis Hunted053252010-05-30 07:21:58 +0000717#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000718 return false;
719 }
720}
721
Douglas Gregor9c832f72010-07-25 18:38:02 +0000722DeclContext::~DeclContext() { }
Douglas Gregor91f84212008-12-11 16:49:14 +0000723
Douglas Gregor7f737c02009-09-10 16:57:35 +0000724/// \brief Find the parent context of this context that will be
725/// used for unqualified name lookup.
726///
727/// Generally, the parent lookup context is the semantic context. However, for
728/// a friend function the parent lookup context is the lexical context, which
729/// is the class in which the friend is declared.
730DeclContext *DeclContext::getLookupParent() {
731 // FIXME: Find a better way to identify friends
732 if (isa<FunctionDecl>(this))
Sebastian Redl50c68252010-08-31 00:36:30 +0000733 if (getParent()->getRedeclContext()->isFileContext() &&
734 getLexicalParent()->getRedeclContext()->isRecord())
Douglas Gregor7f737c02009-09-10 16:57:35 +0000735 return getLexicalParent();
736
737 return getParent();
738}
739
Sebastian Redlbd595762010-08-31 20:53:31 +0000740bool DeclContext::isInlineNamespace() const {
741 return isNamespace() &&
742 cast<NamespaceDecl>(this)->isInline();
743}
744
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000745bool DeclContext::isDependentContext() const {
746 if (isFileContext())
747 return false;
748
Douglas Gregor2373c592009-05-31 09:31:02 +0000749 if (isa<ClassTemplatePartialSpecializationDecl>(this))
750 return true;
751
Douglas Gregor680e9e02012-02-21 19:11:17 +0000752 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this)) {
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000753 if (Record->getDescribedClassTemplate())
754 return true;
Douglas Gregor680e9e02012-02-21 19:11:17 +0000755
756 if (Record->isDependentLambda())
757 return true;
758 }
759
John McCallc62bb642010-03-24 05:22:00 +0000760 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000761 if (Function->getDescribedFunctionTemplate())
762 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000763
John McCallc62bb642010-03-24 05:22:00 +0000764 // Friend function declarations are dependent if their *lexical*
765 // context is dependent.
766 if (cast<Decl>(this)->getFriendObjectKind())
767 return getLexicalParent()->isDependentContext();
768 }
769
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000770 return getParent() && getParent()->isDependentContext();
771}
772
Douglas Gregor07665a62009-01-05 19:45:36 +0000773bool DeclContext::isTransparentContext() const {
774 if (DeclKind == Decl::Enum)
Douglas Gregor0bf31402010-10-08 23:50:27 +0000775 return !cast<EnumDecl>(this)->isScoped();
Douglas Gregor07665a62009-01-05 19:45:36 +0000776 else if (DeclKind == Decl::LinkageSpec)
777 return true;
Douglas Gregor07665a62009-01-05 19:45:36 +0000778
779 return false;
780}
781
John McCall5fe84122010-10-26 04:59:26 +0000782bool DeclContext::isExternCContext() const {
783 const DeclContext *DC = this;
784 while (DC->DeclKind != Decl::TranslationUnit) {
785 if (DC->DeclKind == Decl::LinkageSpec)
786 return cast<LinkageSpecDecl>(DC)->getLanguage()
787 == LinkageSpecDecl::lang_c;
788 DC = DC->getParent();
789 }
790 return false;
791}
792
Sebastian Redl50c68252010-08-31 00:36:30 +0000793bool DeclContext::Encloses(const DeclContext *DC) const {
Douglas Gregore985a3b2009-08-27 06:03:53 +0000794 if (getPrimaryContext() != this)
795 return getPrimaryContext()->Encloses(DC);
Mike Stump11289f42009-09-09 15:08:12 +0000796
Douglas Gregore985a3b2009-08-27 06:03:53 +0000797 for (; DC; DC = DC->getParent())
798 if (DC->getPrimaryContext() == this)
799 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000800 return false;
Douglas Gregore985a3b2009-08-27 06:03:53 +0000801}
802
Steve Naroff35c62ae2009-01-08 17:28:14 +0000803DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor91f84212008-12-11 16:49:14 +0000804 switch (DeclKind) {
Douglas Gregor91f84212008-12-11 16:49:14 +0000805 case Decl::TranslationUnit:
Douglas Gregor07665a62009-01-05 19:45:36 +0000806 case Decl::LinkageSpec:
Mike Stump11289f42009-09-09 15:08:12 +0000807 case Decl::Block:
Douglas Gregor91f84212008-12-11 16:49:14 +0000808 // There is only one DeclContext for these entities.
809 return this;
810
811 case Decl::Namespace:
812 // The original namespace is our primary context.
813 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
814
Douglas Gregor91f84212008-12-11 16:49:14 +0000815 case Decl::ObjCMethod:
816 return this;
817
818 case Decl::ObjCInterface:
Douglas Gregor66b310c2011-12-15 18:03:09 +0000819 if (ObjCInterfaceDecl *Def = cast<ObjCInterfaceDecl>(this)->getDefinition())
820 return Def;
821
822 return this;
823
Steve Naroff35c62ae2009-01-08 17:28:14 +0000824 case Decl::ObjCProtocol:
Douglas Gregora715bff2012-01-01 19:51:50 +0000825 if (ObjCProtocolDecl *Def = cast<ObjCProtocolDecl>(this)->getDefinition())
826 return Def;
827
828 return this;
Douglas Gregor66b310c2011-12-15 18:03:09 +0000829
Steve Naroff35c62ae2009-01-08 17:28:14 +0000830 case Decl::ObjCCategory:
Douglas Gregor91f84212008-12-11 16:49:14 +0000831 return this;
832
Steve Naroff35c62ae2009-01-08 17:28:14 +0000833 case Decl::ObjCImplementation:
834 case Decl::ObjCCategoryImpl:
835 return this;
836
Douglas Gregor91f84212008-12-11 16:49:14 +0000837 default:
Alexis Hunted053252010-05-30 07:21:58 +0000838 if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
Douglas Gregor67a65642009-02-17 23:15:12 +0000839 // If this is a tag type that has a definition or is currently
840 // being defined, that definition is our primary context.
John McCalle78aac42010-03-10 03:28:59 +0000841 TagDecl *Tag = cast<TagDecl>(this);
842 assert(isa<TagType>(Tag->TypeForDecl) ||
843 isa<InjectedClassNameType>(Tag->TypeForDecl));
844
845 if (TagDecl *Def = Tag->getDefinition())
846 return Def;
847
848 if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) {
849 const TagType *TagTy = cast<TagType>(Tag->TypeForDecl);
850 if (TagTy->isBeingDefined())
851 // FIXME: is it necessarily being defined in the decl
852 // that owns the type?
853 return TagTy->getDecl();
854 }
855
856 return Tag;
Douglas Gregor67a65642009-02-17 23:15:12 +0000857 }
858
Alexis Hunted053252010-05-30 07:21:58 +0000859 assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
Douglas Gregor91f84212008-12-11 16:49:14 +0000860 "Unknown DeclContext kind");
861 return this;
862 }
863}
864
Douglas Gregore57e7522012-01-07 09:11:48 +0000865void
866DeclContext::collectAllContexts(llvm::SmallVectorImpl<DeclContext *> &Contexts){
867 Contexts.clear();
868
869 if (DeclKind != Decl::Namespace) {
870 Contexts.push_back(this);
871 return;
Douglas Gregor91f84212008-12-11 16:49:14 +0000872 }
Douglas Gregore57e7522012-01-07 09:11:48 +0000873
874 NamespaceDecl *Self = static_cast<NamespaceDecl *>(this);
Douglas Gregorec9fd132012-01-14 16:38:05 +0000875 for (NamespaceDecl *N = Self->getMostRecentDecl(); N;
876 N = N->getPreviousDecl())
Douglas Gregore57e7522012-01-07 09:11:48 +0000877 Contexts.push_back(N);
878
879 std::reverse(Contexts.begin(), Contexts.end());
Douglas Gregor91f84212008-12-11 16:49:14 +0000880}
881
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000882std::pair<Decl *, Decl *>
Bill Wendling8eb771d2012-02-22 09:51:33 +0000883DeclContext::BuildDeclChain(ArrayRef<Decl*> Decls,
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +0000884 bool FieldsAlreadyLoaded) {
Douglas Gregor781f7132012-01-06 16:59:53 +0000885 // Build up a chain of declarations via the Decl::NextInContextAndBits field.
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000886 Decl *FirstNewDecl = 0;
887 Decl *PrevDecl = 0;
888 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +0000889 if (FieldsAlreadyLoaded && isa<FieldDecl>(Decls[I]))
890 continue;
891
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000892 Decl *D = Decls[I];
893 if (PrevDecl)
Douglas Gregor781f7132012-01-06 16:59:53 +0000894 PrevDecl->NextInContextAndBits.setPointer(D);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000895 else
896 FirstNewDecl = D;
897
898 PrevDecl = D;
899 }
900
901 return std::make_pair(FirstNewDecl, PrevDecl);
902}
903
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000904/// \brief Load the declarations within this lexical storage from an
905/// external source.
Mike Stump11289f42009-09-09 15:08:12 +0000906void
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000907DeclContext::LoadLexicalDeclsFromExternalStorage() const {
908 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000909 assert(hasExternalLexicalStorage() && Source && "No external storage?");
910
Argyrios Kyrtzidis98d045e2010-07-30 10:03:23 +0000911 // Notify that we have a DeclContext that is initializing.
912 ExternalASTSource::Deserializing ADeclContext(Source);
Douglas Gregorf337ae92011-08-26 21:23:06 +0000913
Douglas Gregor3d0adb32011-07-15 21:46:17 +0000914 // Load the external declarations, if any.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000915 SmallVector<Decl*, 64> Decls;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000916 ExternalLexicalStorage = false;
Douglas Gregor3d0adb32011-07-15 21:46:17 +0000917 switch (Source->FindExternalLexicalDecls(this, Decls)) {
918 case ELR_Success:
919 break;
920
921 case ELR_Failure:
922 case ELR_AlreadyLoaded:
923 return;
924 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000925
926 if (Decls.empty())
927 return;
928
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +0000929 // We may have already loaded just the fields of this record, in which case
930 // we need to ignore them.
931 bool FieldsAlreadyLoaded = false;
932 if (const RecordDecl *RD = dyn_cast<RecordDecl>(this))
933 FieldsAlreadyLoaded = RD->LoadedFieldsFromExternalStorage;
934
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000935 // Splice the newly-read declarations into the beginning of the list
936 // of declarations.
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000937 Decl *ExternalFirst, *ExternalLast;
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +0000938 llvm::tie(ExternalFirst, ExternalLast) = BuildDeclChain(Decls,
939 FieldsAlreadyLoaded);
Douglas Gregor781f7132012-01-06 16:59:53 +0000940 ExternalLast->NextInContextAndBits.setPointer(FirstDecl);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000941 FirstDecl = ExternalFirst;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000942 if (!LastDecl)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000943 LastDecl = ExternalLast;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000944}
945
John McCall75b960e2010-06-01 09:23:16 +0000946DeclContext::lookup_result
947ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
948 DeclarationName Name) {
949 ASTContext &Context = DC->getParentASTContext();
950 StoredDeclsMap *Map;
Richard Smithf634c902012-03-16 06:12:59 +0000951 if (!(Map = DC->LookupPtr.getPointer()))
John McCall75b960e2010-06-01 09:23:16 +0000952 Map = DC->CreateStoredDeclsMap(Context);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000953
John McCall75b960e2010-06-01 09:23:16 +0000954 StoredDeclsList &List = (*Map)[Name];
955 assert(List.isNull());
956 (void) List;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000957
John McCall75b960e2010-06-01 09:23:16 +0000958 return DeclContext::lookup_result();
959}
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000960
John McCall75b960e2010-06-01 09:23:16 +0000961DeclContext::lookup_result
962ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +0000963 DeclarationName Name,
Argyrios Kyrtzidis94d3f9d2011-09-09 06:44:14 +0000964 ArrayRef<NamedDecl*> Decls) {
Dmitri Gribenko76bb5cabfa2012-09-10 21:20:09 +0000965 ASTContext &Context = DC->getParentASTContext();
John McCall75b960e2010-06-01 09:23:16 +0000966
967 StoredDeclsMap *Map;
Richard Smithf634c902012-03-16 06:12:59 +0000968 if (!(Map = DC->LookupPtr.getPointer()))
John McCall75b960e2010-06-01 09:23:16 +0000969 Map = DC->CreateStoredDeclsMap(Context);
970
971 StoredDeclsList &List = (*Map)[Name];
Argyrios Kyrtzidis94d3f9d2011-09-09 06:44:14 +0000972 for (ArrayRef<NamedDecl*>::iterator
973 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
John McCall75b960e2010-06-01 09:23:16 +0000974 if (List.isNull())
Argyrios Kyrtzidis94d3f9d2011-09-09 06:44:14 +0000975 List.setOnlyValue(*I);
John McCall75b960e2010-06-01 09:23:16 +0000976 else
Argyrios Kyrtzidis94d3f9d2011-09-09 06:44:14 +0000977 List.AddSubsequentDecl(*I);
John McCall75b960e2010-06-01 09:23:16 +0000978 }
979
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000980 return List.getLookupResult();
John McCall75b960e2010-06-01 09:23:16 +0000981}
982
Sebastian Redl66c5eef2010-07-27 00:17:23 +0000983DeclContext::decl_iterator DeclContext::noload_decls_begin() const {
984 return decl_iterator(FirstDecl);
985}
986
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000987DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000988 if (hasExternalLexicalStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000989 LoadLexicalDeclsFromExternalStorage();
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000990
Mike Stump11289f42009-09-09 15:08:12 +0000991 return decl_iterator(FirstDecl);
Douglas Gregorbcced4e2009-04-09 21:40:53 +0000992}
993
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000994bool DeclContext::decls_empty() const {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +0000995 if (hasExternalLexicalStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000996 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +0000997
998 return !FirstDecl;
999}
1000
John McCall84d87672009-12-10 09:41:52 +00001001void DeclContext::removeDecl(Decl *D) {
1002 assert(D->getLexicalDeclContext() == this &&
1003 "decl being removed from non-lexical context");
Douglas Gregor781f7132012-01-06 16:59:53 +00001004 assert((D->NextInContextAndBits.getPointer() || D == LastDecl) &&
John McCall84d87672009-12-10 09:41:52 +00001005 "decl is not in decls list");
1006
1007 // Remove D from the decl chain. This is O(n) but hopefully rare.
1008 if (D == FirstDecl) {
1009 if (D == LastDecl)
1010 FirstDecl = LastDecl = 0;
1011 else
Douglas Gregor781f7132012-01-06 16:59:53 +00001012 FirstDecl = D->NextInContextAndBits.getPointer();
John McCall84d87672009-12-10 09:41:52 +00001013 } else {
Douglas Gregor781f7132012-01-06 16:59:53 +00001014 for (Decl *I = FirstDecl; true; I = I->NextInContextAndBits.getPointer()) {
John McCall84d87672009-12-10 09:41:52 +00001015 assert(I && "decl not found in linked list");
Douglas Gregor781f7132012-01-06 16:59:53 +00001016 if (I->NextInContextAndBits.getPointer() == D) {
1017 I->NextInContextAndBits.setPointer(D->NextInContextAndBits.getPointer());
John McCall84d87672009-12-10 09:41:52 +00001018 if (D == LastDecl) LastDecl = I;
1019 break;
1020 }
1021 }
1022 }
1023
1024 // Mark that D is no longer in the decl chain.
Douglas Gregor781f7132012-01-06 16:59:53 +00001025 D->NextInContextAndBits.setPointer(0);
John McCall84d87672009-12-10 09:41:52 +00001026
1027 // Remove D from the lookup table if necessary.
1028 if (isa<NamedDecl>(D)) {
1029 NamedDecl *ND = cast<NamedDecl>(D);
1030
Axel Naumanncb2c52f2011-08-26 14:06:12 +00001031 // Remove only decls that have a name
1032 if (!ND->getDeclName()) return;
1033
Richard Smithf634c902012-03-16 06:12:59 +00001034 StoredDeclsMap *Map = getPrimaryContext()->LookupPtr.getPointer();
John McCallc62bb642010-03-24 05:22:00 +00001035 if (!Map) return;
John McCall84d87672009-12-10 09:41:52 +00001036
John McCall84d87672009-12-10 09:41:52 +00001037 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
1038 assert(Pos != Map->end() && "no lookup entry for decl");
Axel Naumannfbc7b982011-11-08 18:21:06 +00001039 if (Pos->second.getAsVector() || Pos->second.getAsDecl() == ND)
1040 Pos->second.remove(ND);
John McCall84d87672009-12-10 09:41:52 +00001041 }
1042}
1043
John McCalld1e9d832009-08-11 06:59:38 +00001044void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner33f219d2009-02-20 00:56:18 +00001045 assert(D->getLexicalDeclContext() == this &&
1046 "Decl inserted into wrong lexical context");
Mike Stump11289f42009-09-09 15:08:12 +00001047 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor020713e2009-01-09 19:42:16 +00001048 "Decl already inserted into a DeclContext");
1049
1050 if (FirstDecl) {
Douglas Gregor781f7132012-01-06 16:59:53 +00001051 LastDecl->NextInContextAndBits.setPointer(D);
Douglas Gregor020713e2009-01-09 19:42:16 +00001052 LastDecl = D;
1053 } else {
1054 FirstDecl = LastDecl = D;
1055 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +00001056
1057 // Notify a C++ record declaration that we've added a member, so it can
1058 // update it's class-specific state.
1059 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
1060 Record->addedMember(D);
Douglas Gregor0f2a3602011-12-03 00:30:27 +00001061
1062 // If this is a newly-created (not de-serialized) import declaration, wire
1063 // it in to the list of local import declarations.
1064 if (!D->isFromASTFile()) {
1065 if (ImportDecl *Import = dyn_cast<ImportDecl>(D))
1066 D->getASTContext().addedLocalImportDecl(Import);
1067 }
John McCalld1e9d832009-08-11 06:59:38 +00001068}
1069
1070void DeclContext::addDecl(Decl *D) {
1071 addHiddenDecl(D);
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001072
1073 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Richard Smithf634c902012-03-16 06:12:59 +00001074 ND->getDeclContext()->getPrimaryContext()->
1075 makeDeclVisibleInContextWithFlags(ND, false, true);
Douglas Gregor91f84212008-12-11 16:49:14 +00001076}
1077
Sean Callanan95e74be2011-10-21 02:57:43 +00001078void DeclContext::addDeclInternal(Decl *D) {
1079 addHiddenDecl(D);
1080
1081 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Richard Smithf634c902012-03-16 06:12:59 +00001082 ND->getDeclContext()->getPrimaryContext()->
1083 makeDeclVisibleInContextWithFlags(ND, true, true);
1084}
1085
1086/// shouldBeHidden - Determine whether a declaration which was declared
1087/// within its semantic context should be invisible to qualified name lookup.
1088static bool shouldBeHidden(NamedDecl *D) {
1089 // Skip unnamed declarations.
1090 if (!D->getDeclName())
1091 return true;
1092
1093 // Skip entities that can't be found by name lookup into a particular
1094 // context.
1095 if ((D->getIdentifierNamespace() == 0 && !isa<UsingDirectiveDecl>(D)) ||
1096 D->isTemplateParameter())
1097 return true;
1098
1099 // Skip template specializations.
1100 // FIXME: This feels like a hack. Should DeclarationName support
1101 // template-ids, or is there a better way to keep specializations
1102 // from being visible?
1103 if (isa<ClassTemplateSpecializationDecl>(D))
1104 return true;
1105 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1106 if (FD->isFunctionTemplateSpecialization())
1107 return true;
1108
1109 return false;
1110}
1111
1112/// buildLookup - Build the lookup data structure with all of the
1113/// declarations in this DeclContext (and any other contexts linked
1114/// to it or transparent contexts nested within it) and return it.
1115StoredDeclsMap *DeclContext::buildLookup() {
1116 assert(this == getPrimaryContext() && "buildLookup called on non-primary DC");
1117
1118 if (!LookupPtr.getInt())
1119 return LookupPtr.getPointer();
1120
1121 llvm::SmallVector<DeclContext *, 2> Contexts;
1122 collectAllContexts(Contexts);
1123 for (unsigned I = 0, N = Contexts.size(); I != N; ++I)
1124 buildLookupImpl(Contexts[I]);
1125
1126 // We no longer have any lazy decls.
1127 LookupPtr.setInt(false);
1128 return LookupPtr.getPointer();
1129}
1130
1131/// buildLookupImpl - Build part of the lookup data structure for the
1132/// declarations contained within DCtx, which will either be this
1133/// DeclContext, a DeclContext linked to it, or a transparent context
1134/// nested within it.
1135void DeclContext::buildLookupImpl(DeclContext *DCtx) {
1136 for (decl_iterator I = DCtx->decls_begin(), E = DCtx->decls_end();
1137 I != E; ++I) {
1138 Decl *D = *I;
1139
1140 // Insert this declaration into the lookup structure, but only if
1141 // it's semantically within its decl context. Any other decls which
1142 // should be found in this context are added eagerly.
1143 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1144 if (ND->getDeclContext() == DCtx && !shouldBeHidden(ND))
1145 makeDeclVisibleInContextImpl(ND, false);
1146
1147 // If this declaration is itself a transparent declaration context
1148 // or inline namespace, add the members of this declaration of that
1149 // context (recursively).
1150 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(D))
1151 if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
1152 buildLookupImpl(InnerCtx);
1153 }
Sean Callanan95e74be2011-10-21 02:57:43 +00001154}
1155
Mike Stump11289f42009-09-09 15:08:12 +00001156DeclContext::lookup_result
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001157DeclContext::lookup(DeclarationName Name) {
Nick Lewycky2bd636f2012-03-13 04:12:34 +00001158 assert(DeclKind != Decl::LinkageSpec &&
1159 "Should not perform lookups into linkage specs!");
1160
Steve Naroff35c62ae2009-01-08 17:28:14 +00001161 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor91f84212008-12-11 16:49:14 +00001162 if (PrimaryContext != this)
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001163 return PrimaryContext->lookup(Name);
Douglas Gregor91f84212008-12-11 16:49:14 +00001164
Richard Smith05afe5e2012-03-13 03:12:56 +00001165 if (hasExternalVisibleStorage()) {
Richard Smithf634c902012-03-16 06:12:59 +00001166 // If a PCH has a result for this name, and we have a local declaration, we
1167 // will have imported the PCH result when adding the local declaration.
1168 // FIXME: For modules, we could have had more declarations added by module
1169 // imoprts since we saw the declaration of the local name.
1170 if (StoredDeclsMap *Map = LookupPtr.getPointer()) {
1171 StoredDeclsMap::iterator I = Map->find(Name);
1172 if (I != Map->end())
1173 return I->second.getLookupResult();
1174 }
1175
John McCall75b960e2010-06-01 09:23:16 +00001176 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1177 return Source->FindExternalVisibleDeclsByName(this, Name);
1178 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001179
Richard Smithf634c902012-03-16 06:12:59 +00001180 StoredDeclsMap *Map = LookupPtr.getPointer();
1181 if (LookupPtr.getInt())
1182 Map = buildLookup();
1183
1184 if (!Map)
1185 return lookup_result(lookup_iterator(0), lookup_iterator(0));
1186
1187 StoredDeclsMap::iterator I = Map->find(Name);
1188 if (I == Map->end())
1189 return lookup_result(lookup_iterator(0), lookup_iterator(0));
1190
1191 return I->second.getLookupResult();
Douglas Gregor91f84212008-12-11 16:49:14 +00001192}
1193
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001194void DeclContext::localUncachedLookup(DeclarationName Name,
1195 llvm::SmallVectorImpl<NamedDecl *> &Results) {
1196 Results.clear();
1197
1198 // If there's no external storage, just perform a normal lookup and copy
1199 // the results.
Douglas Gregordd6006f2012-07-17 21:16:27 +00001200 if (!hasExternalVisibleStorage() && !hasExternalLexicalStorage() && Name) {
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001201 lookup_result LookupResults = lookup(Name);
1202 Results.insert(Results.end(), LookupResults.first, LookupResults.second);
1203 return;
1204 }
1205
1206 // If we have a lookup table, check there first. Maybe we'll get lucky.
Douglas Gregordd6006f2012-07-17 21:16:27 +00001207 if (Name) {
1208 if (StoredDeclsMap *Map = LookupPtr.getPointer()) {
1209 StoredDeclsMap::iterator Pos = Map->find(Name);
1210 if (Pos != Map->end()) {
1211 Results.insert(Results.end(),
1212 Pos->second.getLookupResult().first,
1213 Pos->second.getLookupResult().second);
1214 return;
1215 }
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001216 }
1217 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00001218
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001219 // Slow case: grovel through the declarations in our chain looking for
1220 // matches.
1221 for (Decl *D = FirstDecl; D; D = D->getNextDeclInContext()) {
1222 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1223 if (ND->getDeclName() == Name)
1224 Results.push_back(ND);
1225 }
1226}
1227
Sebastian Redl50c68252010-08-31 00:36:30 +00001228DeclContext *DeclContext::getRedeclContext() {
Chris Lattner17a1bfa2009-03-27 19:19:59 +00001229 DeclContext *Ctx = this;
Sebastian Redlbd595762010-08-31 20:53:31 +00001230 // Skip through transparent contexts.
1231 while (Ctx->isTransparentContext())
Douglas Gregor6ad0ef52009-01-06 23:51:29 +00001232 Ctx = Ctx->getParent();
1233 return Ctx;
1234}
1235
Douglas Gregorf47b9112009-02-25 22:02:03 +00001236DeclContext *DeclContext::getEnclosingNamespaceContext() {
1237 DeclContext *Ctx = this;
1238 // Skip through non-namespace, non-translation-unit contexts.
Sebastian Redl4f08c962010-08-31 00:36:23 +00001239 while (!Ctx->isFileContext())
Douglas Gregorf47b9112009-02-25 22:02:03 +00001240 Ctx = Ctx->getParent();
1241 return Ctx->getPrimaryContext();
1242}
1243
Sebastian Redl50c68252010-08-31 00:36:30 +00001244bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
1245 // For non-file contexts, this is equivalent to Equals.
1246 if (!isFileContext())
1247 return O->Equals(this);
1248
1249 do {
1250 if (O->Equals(this))
1251 return true;
1252
1253 const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O);
1254 if (!NS || !NS->isInline())
1255 break;
1256 O = NS->getParent();
1257 } while (O);
1258
1259 return false;
1260}
1261
Richard Smithf634c902012-03-16 06:12:59 +00001262void DeclContext::makeDeclVisibleInContext(NamedDecl *D) {
1263 DeclContext *PrimaryDC = this->getPrimaryContext();
1264 DeclContext *DeclDC = D->getDeclContext()->getPrimaryContext();
1265 // If the decl is being added outside of its semantic decl context, we
1266 // need to ensure that we eagerly build the lookup information for it.
1267 PrimaryDC->makeDeclVisibleInContextWithFlags(D, false, PrimaryDC == DeclDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00001268}
1269
Richard Smithf634c902012-03-16 06:12:59 +00001270void DeclContext::makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal,
1271 bool Recoverable) {
1272 assert(this == getPrimaryContext() && "expected a primary DC");
Sean Callanan95e74be2011-10-21 02:57:43 +00001273
Richard Smith05afe5e2012-03-13 03:12:56 +00001274 // Skip declarations within functions.
1275 // FIXME: We shouldn't need to build lookup tables for function declarations
1276 // ever, and we can't do so correctly because we can't model the nesting of
1277 // scopes which occurs within functions. We use "qualified" lookup into
1278 // function declarations when handling friend declarations inside nested
1279 // classes, and consequently accept the following invalid code:
1280 //
1281 // void f() { void g(); { int g; struct S { friend void g(); }; } }
1282 if (isFunctionOrMethod() && !isa<FunctionDecl>(D))
1283 return;
1284
Richard Smithf634c902012-03-16 06:12:59 +00001285 // Skip declarations which should be invisible to name lookup.
1286 if (shouldBeHidden(D))
1287 return;
1288
1289 // If we already have a lookup data structure, perform the insertion into
1290 // it. If we might have externally-stored decls with this name, look them
1291 // up and perform the insertion. If this decl was declared outside its
1292 // semantic context, buildLookup won't add it, so add it now.
1293 //
1294 // FIXME: As a performance hack, don't add such decls into the translation
1295 // unit unless we're in C++, since qualified lookup into the TU is never
1296 // performed.
1297 if (LookupPtr.getPointer() || hasExternalVisibleStorage() ||
1298 ((!Recoverable || D->getDeclContext() != D->getLexicalDeclContext()) &&
1299 (getParentASTContext().getLangOpts().CPlusPlus ||
1300 !isTranslationUnit()))) {
1301 // If we have lazily omitted any decls, they might have the same name as
1302 // the decl which we are adding, so build a full lookup table before adding
1303 // this decl.
1304 buildLookup();
1305 makeDeclVisibleInContextImpl(D, Internal);
1306 } else {
1307 LookupPtr.setInt(true);
1308 }
1309
1310 // If we are a transparent context or inline namespace, insert into our
1311 // parent context, too. This operation is recursive.
1312 if (isTransparentContext() || isInlineNamespace())
1313 getParent()->getPrimaryContext()->
1314 makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
1315
1316 Decl *DCAsDecl = cast<Decl>(this);
1317 // Notify that a decl was made visible unless we are a Tag being defined.
1318 if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined()))
1319 if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
1320 L->AddedVisibleDecl(this, D);
1321}
1322
1323void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal) {
1324 // Find or create the stored declaration map.
1325 StoredDeclsMap *Map = LookupPtr.getPointer();
1326 if (!Map) {
1327 ASTContext *C = &getParentASTContext();
1328 Map = CreateStoredDeclsMap(*C);
Argyrios Kyrtzidise51e5542010-07-04 21:44:25 +00001329 }
1330
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001331 // If there is an external AST source, load any declarations it knows about
1332 // with this declaration's name.
1333 // If the lookup table contains an entry about this name it means that we
1334 // have already checked the external source.
Sean Callanan95e74be2011-10-21 02:57:43 +00001335 if (!Internal)
1336 if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
1337 if (hasExternalVisibleStorage() &&
Richard Smithf634c902012-03-16 06:12:59 +00001338 Map->find(D->getDeclName()) == Map->end())
Sean Callanan95e74be2011-10-21 02:57:43 +00001339 Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001340
Douglas Gregor91f84212008-12-11 16:49:14 +00001341 // Insert this declaration into the map.
Richard Smithf634c902012-03-16 06:12:59 +00001342 StoredDeclsList &DeclNameEntries = (*Map)[D->getDeclName()];
Chris Lattnercaae7162009-02-20 01:44:05 +00001343 if (DeclNameEntries.isNull()) {
1344 DeclNameEntries.setOnlyValue(D);
Richard Smithf634c902012-03-16 06:12:59 +00001345 return;
Douglas Gregor91f84212008-12-11 16:49:14 +00001346 }
Chris Lattner24e24d52009-02-20 00:55:03 +00001347
Richard Smithf634c902012-03-16 06:12:59 +00001348 if (DeclNameEntries.HandleRedeclaration(D)) {
1349 // This declaration has replaced an existing one for which
1350 // declarationReplaces returns true.
1351 return;
1352 }
Mike Stump11289f42009-09-09 15:08:12 +00001353
Richard Smithf634c902012-03-16 06:12:59 +00001354 // Put this declaration into the appropriate slot.
1355 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor91f84212008-12-11 16:49:14 +00001356}
Douglas Gregor889ceb72009-02-03 19:21:40 +00001357
1358/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
1359/// this context.
Mike Stump11289f42009-09-09 15:08:12 +00001360DeclContext::udir_iterator_range
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001361DeclContext::getUsingDirectives() const {
Richard Smith05afe5e2012-03-13 03:12:56 +00001362 // FIXME: Use something more efficient than normal lookup for using
1363 // directives. In C++, using directives are looked up more than anything else.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001364 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor889ceb72009-02-03 19:21:40 +00001365 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
1366 reinterpret_cast<udir_iterator>(Result.second));
1367}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001368
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001369//===----------------------------------------------------------------------===//
1370// Creation and Destruction of StoredDeclsMaps. //
1371//===----------------------------------------------------------------------===//
1372
John McCallc62bb642010-03-24 05:22:00 +00001373StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
Richard Smithf634c902012-03-16 06:12:59 +00001374 assert(!LookupPtr.getPointer() && "context already has a decls map");
John McCallc62bb642010-03-24 05:22:00 +00001375 assert(getPrimaryContext() == this &&
1376 "creating decls map on non-primary context");
1377
1378 StoredDeclsMap *M;
1379 bool Dependent = isDependentContext();
1380 if (Dependent)
1381 M = new DependentStoredDeclsMap();
1382 else
1383 M = new StoredDeclsMap();
1384 M->Previous = C.LastSDM;
1385 C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
Richard Smithf634c902012-03-16 06:12:59 +00001386 LookupPtr.setPointer(M);
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001387 return M;
1388}
1389
1390void ASTContext::ReleaseDeclContextMaps() {
John McCallc62bb642010-03-24 05:22:00 +00001391 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1392 // pointer because the subclass doesn't add anything that needs to
1393 // be deleted.
John McCallc62bb642010-03-24 05:22:00 +00001394 StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1395}
1396
1397void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1398 while (Map) {
1399 // Advance the iteration before we invalidate memory.
1400 llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1401
1402 if (Dependent)
1403 delete static_cast<DependentStoredDeclsMap*>(Map);
1404 else
1405 delete Map;
1406
1407 Map = Next.getPointer();
1408 Dependent = Next.getInt();
1409 }
1410}
1411
John McCallc62bb642010-03-24 05:22:00 +00001412DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1413 DeclContext *Parent,
1414 const PartialDiagnostic &PDiag) {
1415 assert(Parent->isDependentContext()
1416 && "cannot iterate dependent diagnostics of non-dependent context");
1417 Parent = Parent->getPrimaryContext();
Richard Smithf634c902012-03-16 06:12:59 +00001418 if (!Parent->LookupPtr.getPointer())
John McCallc62bb642010-03-24 05:22:00 +00001419 Parent->CreateStoredDeclsMap(C);
1420
1421 DependentStoredDeclsMap *Map
Richard Smithf634c902012-03-16 06:12:59 +00001422 = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr.getPointer());
John McCallc62bb642010-03-24 05:22:00 +00001423
Douglas Gregora55530e2010-03-29 23:56:53 +00001424 // Allocate the copy of the PartialDiagnostic via the ASTContext's
Douglas Gregor89336232010-03-29 23:34:08 +00001425 // BumpPtrAllocator, rather than the ASTContext itself.
Douglas Gregora55530e2010-03-29 23:56:53 +00001426 PartialDiagnostic::Storage *DiagStorage = 0;
1427 if (PDiag.hasStorage())
1428 DiagStorage = new (C) PartialDiagnostic::Storage;
1429
1430 DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
John McCallc62bb642010-03-24 05:22:00 +00001431
1432 // TODO: Maybe we shouldn't reverse the order during insertion.
1433 DD->NextDiagnostic = Map->FirstDiagnostic;
1434 Map->FirstDiagnostic = DD;
1435
1436 return DD;
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001437}