blob: 0312cfbc4abebee32e09bbf5e4dfdb86638a0473 [file] [log] [blame]
Eli Friedman56d29372008-06-07 16:52:53 +00001//===--- DeclBase.cpp - Declaration AST Node Implementation ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Decl and DeclContext classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclBase.h"
Douglas Gregor64650af2009-02-02 23:39:07 +000015#include "clang/AST/Decl.h"
Douglas Gregorc2ee10d2009-04-07 17:20:56 +000016#include "clang/AST/DeclContextInternals.h"
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +000017#include "clang/AST/DeclCXX.h"
John McCall92b7f702010-03-11 07:50:04 +000018#include "clang/AST/DeclFriend.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000019#include "clang/AST/DeclObjC.h"
20#include "clang/AST/DeclTemplate.h"
John McCall0c01d182010-03-24 05:22:00 +000021#include "clang/AST/DependentDiagnostic.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000022#include "clang/AST/ExternalASTSource.h"
Eli Friedman56d29372008-06-07 16:52:53 +000023#include "clang/AST/ASTContext.h"
Douglas Gregor44b43212008-12-11 16:49:14 +000024#include "clang/AST/Type.h"
Sebastian Redld3a413d2009-04-26 20:35:05 +000025#include "clang/AST/Stmt.h"
26#include "clang/AST/StmtCXX.h"
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +000027#include "clang/AST/ASTMutationListener.h"
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000028#include "clang/Basic/TargetInfo.h"
Eli Friedman56d29372008-06-07 16:52:53 +000029#include "llvm/ADT/DenseMap.h"
Chris Lattner49f28ca2009-03-05 08:00:35 +000030#include "llvm/Support/raw_ostream.h"
Douglas Gregor6ed40e32008-12-23 21:05:05 +000031#include <algorithm>
Eli Friedman56d29372008-06-07 16:52:53 +000032using namespace clang;
33
34//===----------------------------------------------------------------------===//
35// Statistics
36//===----------------------------------------------------------------------===//
37
Sean Hunt9a555912010-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 Friedman56d29372008-06-07 16:52:53 +000041
42static bool StatSwitch = false;
43
Douglas Gregor1e68ecc2012-01-05 21:55:30 +000044void *Decl::AllocateDeserializedDecl(const ASTContext &Context,
45 unsigned ID,
46 unsigned Size) {
Douglas Gregor5d1f4962012-01-05 23:49:36 +000047 // Allocate an extra 8 bytes worth of storage, which ensures that the
48 // resulting pointer will still be 8-byte aligned. At present, we're only
49 // using the latter 4 bytes of this storage.
50 void *Start = Context.Allocate(Size + 8);
51 void *Result = (char*)Start + 8;
Douglas Gregorb6b60c12012-01-05 22:27:05 +000052
53 // Store the global declaration ID
54 unsigned *IDPtr = (unsigned*)Result - 1;
55 *IDPtr = ID;
56
57 return Result;
Douglas Gregor1e68ecc2012-01-05 21:55:30 +000058}
59
Eli Friedman56d29372008-06-07 16:52:53 +000060const char *Decl::getDeclKindName() const {
61 switch (DeclKind) {
David Blaikieb219cfc2011-09-23 05:06:16 +000062 default: llvm_unreachable("Declaration not in DeclNodes.inc!");
Sean Hunt9a555912010-05-30 07:21:58 +000063#define DECL(DERIVED, BASE) case DERIVED: return #DERIVED;
64#define ABSTRACT_DECL(DECL)
65#include "clang/AST/DeclNodes.inc"
Eli Friedman56d29372008-06-07 16:52:53 +000066 }
67}
68
Douglas Gregor42738572010-03-05 00:26:45 +000069void Decl::setInvalidDecl(bool Invalid) {
70 InvalidDecl = Invalid;
71 if (Invalid) {
72 // Defensive maneuver for ill-formed code: we're likely not to make it to
73 // a point where we set the access specifier, so default it to "public"
74 // to avoid triggering asserts elsewhere in the front end.
75 setAccess(AS_public);
76 }
77}
78
Steve Naroff0a473932009-01-20 19:53:53 +000079const char *DeclContext::getDeclKindName() const {
80 switch (DeclKind) {
David Blaikieb219cfc2011-09-23 05:06:16 +000081 default: llvm_unreachable("Declaration context not in DeclNodes.inc!");
Sean Hunt9a555912010-05-30 07:21:58 +000082#define DECL(DERIVED, BASE) case Decl::DERIVED: return #DERIVED;
83#define ABSTRACT_DECL(DECL)
84#include "clang/AST/DeclNodes.inc"
Steve Naroff0a473932009-01-20 19:53:53 +000085 }
86}
87
Eli Friedman56d29372008-06-07 16:52:53 +000088bool Decl::CollectingStats(bool Enable) {
Kovarththanan Rajaratnam2024f4d2009-11-29 14:54:35 +000089 if (Enable) StatSwitch = true;
Eli Friedman56d29372008-06-07 16:52:53 +000090 return StatSwitch;
91}
92
93void Decl::PrintStats() {
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000094 llvm::errs() << "\n*** Decl Stats:\n";
Mike Stump1eb44332009-09-09 15:08:12 +000095
Douglas Gregor64650af2009-02-02 23:39:07 +000096 int totalDecls = 0;
Sean Hunt9a555912010-05-30 07:21:58 +000097#define DECL(DERIVED, BASE) totalDecls += n##DERIVED##s;
98#define ABSTRACT_DECL(DECL)
99#include "clang/AST/DeclNodes.inc"
Chandler Carruthb43c8ec2011-07-04 06:13:27 +0000100 llvm::errs() << " " << totalDecls << " decls total.\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Douglas Gregor64650af2009-02-02 23:39:07 +0000102 int totalBytes = 0;
Sean Hunt9a555912010-05-30 07:21:58 +0000103#define DECL(DERIVED, BASE) \
104 if (n##DERIVED##s > 0) { \
105 totalBytes += (int)(n##DERIVED##s * sizeof(DERIVED##Decl)); \
Chandler Carruthb43c8ec2011-07-04 06:13:27 +0000106 llvm::errs() << " " << n##DERIVED##s << " " #DERIVED " decls, " \
107 << sizeof(DERIVED##Decl) << " each (" \
108 << n##DERIVED##s * sizeof(DERIVED##Decl) \
109 << " bytes)\n"; \
Douglas Gregor64650af2009-02-02 23:39:07 +0000110 }
Sean Hunt9a555912010-05-30 07:21:58 +0000111#define ABSTRACT_DECL(DECL)
112#include "clang/AST/DeclNodes.inc"
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Chandler Carruthb43c8ec2011-07-04 06:13:27 +0000114 llvm::errs() << "Total bytes = " << totalBytes << "\n";
Eli Friedman56d29372008-06-07 16:52:53 +0000115}
116
Sean Hunt9a555912010-05-30 07:21:58 +0000117void Decl::add(Kind k) {
Eli Friedman56d29372008-06-07 16:52:53 +0000118 switch (k) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000119 default: llvm_unreachable("Declaration not in DeclNodes.inc!");
Sean Hunt9a555912010-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 Friedman56d29372008-06-07 16:52:53 +0000123 }
124}
125
Anders Carlsson67e33202009-06-13 00:08:58 +0000126bool Decl::isTemplateParameterPack() const {
127 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
128 return TTP->isParameterPack();
Douglas Gregor10738d32010-12-23 23:51:58 +0000129 if (const NonTypeTemplateParmDecl *NTTP
Douglas Gregor61c4d282011-01-05 15:48:55 +0000130 = dyn_cast<NonTypeTemplateParmDecl>(this))
Douglas Gregor10738d32010-12-23 23:51:58 +0000131 return NTTP->isParameterPack();
Douglas Gregor61c4d282011-01-05 15:48:55 +0000132 if (const TemplateTemplateParmDecl *TTP
133 = dyn_cast<TemplateTemplateParmDecl>(this))
134 return TTP->isParameterPack();
Anders Carlsson67e33202009-06-13 00:08:58 +0000135 return false;
136}
137
Douglas Gregor1fe85ea2011-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 Gregore53060f2009-06-25 22:08:12 +0000145bool Decl::isFunctionOrFunctionTemplate() const {
John McCall9488ea12009-11-17 05:59:44 +0000146 if (const UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(this))
Anders Carlsson58badb72009-06-26 05:26:50 +0000147 return UD->getTargetDecl()->isFunctionOrFunctionTemplate();
Mike Stump1eb44332009-09-09 15:08:12 +0000148
Douglas Gregore53060f2009-06-25 22:08:12 +0000149 return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this);
150}
151
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000152bool Decl::isTemplateDecl() const {
153 return isa<TemplateDecl>(this);
154}
155
Argyrios Kyrtzidisc8680f42011-09-28 02:45:33 +0000156const DeclContext *Decl::getParentFunctionOrMethod() const {
157 for (const DeclContext *DC = getDeclContext();
158 DC && !DC->isTranslationUnit() && !DC->isNamespace();
Douglas Gregor79c22782010-01-16 20:21:20 +0000159 DC = DC->getParent())
160 if (DC->isFunctionOrMethod())
Argyrios Kyrtzidisc8680f42011-09-28 02:45:33 +0000161 return DC;
Douglas Gregor79c22782010-01-16 20:21:20 +0000162
Argyrios Kyrtzidisc8680f42011-09-28 02:45:33 +0000163 return 0;
Douglas Gregor79c22782010-01-16 20:21:20 +0000164}
165
Douglas Gregor4c3e0ee2011-02-17 08:47:29 +0000166
Eli Friedman56d29372008-06-07 16:52:53 +0000167//===----------------------------------------------------------------------===//
Chris Lattner49f28ca2009-03-05 08:00:35 +0000168// PrettyStackTraceDecl Implementation
169//===----------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000170
Chris Lattner5f9e2722011-07-23 10:55:15 +0000171void PrettyStackTraceDecl::print(raw_ostream &OS) const {
Chris Lattner49f28ca2009-03-05 08:00:35 +0000172 SourceLocation TheLoc = Loc;
173 if (TheLoc.isInvalid() && TheDecl)
174 TheLoc = TheDecl->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000175
Chris Lattner49f28ca2009-03-05 08:00:35 +0000176 if (TheLoc.isValid()) {
177 TheLoc.print(OS, SM);
178 OS << ": ";
179 }
180
181 OS << Message;
182
Daniel Dunbarc5236562009-11-21 09:05:59 +0000183 if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
Chris Lattner49f28ca2009-03-05 08:00:35 +0000184 OS << " '" << DN->getQualifiedNameAsString() << '\'';
185 OS << '\n';
186}
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Chris Lattner49f28ca2009-03-05 08:00:35 +0000188//===----------------------------------------------------------------------===//
Eli Friedman56d29372008-06-07 16:52:53 +0000189// Decl Implementation
190//===----------------------------------------------------------------------===//
191
Douglas Gregorda2142f2011-02-19 18:51:44 +0000192// Out-of-line virtual method providing a home for Decl.
193Decl::~Decl() { }
Douglas Gregorf4a03cc2011-02-17 07:02:32 +0000194
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000195void Decl::setDeclContext(DeclContext *DC) {
Chris Lattneree219fd2009-03-29 06:06:59 +0000196 DeclCtx = DC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000197}
198
199void Decl::setLexicalDeclContext(DeclContext *DC) {
200 if (DC == getLexicalDeclContext())
201 return;
202
203 if (isInSemaDC()) {
Ted Kremenek94a39002009-12-01 00:07:10 +0000204 MultipleDC *MDC = new (getASTContext()) MultipleDC();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000205 MDC->SemanticDC = getDeclContext();
206 MDC->LexicalDC = DC;
Chris Lattneree219fd2009-03-29 06:06:59 +0000207 DeclCtx = MDC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000208 } else {
209 getMultipleDC()->LexicalDC = DC;
210 }
211}
212
John McCall9aeed322009-10-01 00:25:31 +0000213bool Decl::isInAnonymousNamespace() const {
214 const DeclContext *DC = getDeclContext();
215 do {
216 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
217 if (ND->isAnonymousNamespace())
218 return true;
219 } while ((DC = DC->getParent()));
220
221 return false;
222}
223
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000224TranslationUnitDecl *Decl::getTranslationUnitDecl() {
Argyrios Kyrtzidis9b346692009-06-30 02:34:53 +0000225 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
226 return TUD;
227
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000228 DeclContext *DC = getDeclContext();
229 assert(DC && "This decl is not contained in a translation unit!");
Mike Stump1eb44332009-09-09 15:08:12 +0000230
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000231 while (!DC->isTranslationUnit()) {
232 DC = DC->getParent();
233 assert(DC && "This decl is not contained in a translation unit!");
234 }
Mike Stump1eb44332009-09-09 15:08:12 +0000235
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000236 return cast<TranslationUnitDecl>(DC);
237}
238
239ASTContext &Decl::getASTContext() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000240 return getTranslationUnitDecl()->getASTContext();
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000241}
242
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +0000243ASTMutationListener *Decl::getASTMutationListener() const {
244 return getASTContext().getASTMutationListener();
245}
246
Douglas Gregorc070cc62010-06-17 23:14:26 +0000247bool Decl::isUsed(bool CheckUsedAttr) const {
Tanya Lattner12ead492010-02-17 02:17:21 +0000248 if (Used)
249 return true;
250
251 // Check for used attribute.
Douglas Gregorc070cc62010-06-17 23:14:26 +0000252 if (CheckUsedAttr && hasAttr<UsedAttr>())
Tanya Lattner12ead492010-02-17 02:17:21 +0000253 return true;
254
255 // Check redeclarations for used attribute.
256 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Douglas Gregorc070cc62010-06-17 23:14:26 +0000257 if ((CheckUsedAttr && I->hasAttr<UsedAttr>()) || I->Used)
Tanya Lattner12ead492010-02-17 02:17:21 +0000258 return true;
259 }
260
261 return false;
262}
263
Argyrios Kyrtzidis6b6b42a2011-04-19 19:51:10 +0000264bool Decl::isReferenced() const {
265 if (Referenced)
266 return true;
267
268 // Check redeclarations.
269 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
270 if (I->Referenced)
271 return true;
272
273 return false;
274}
275
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000276/// \brief Determine the availability of the given declaration based on
277/// the target platform.
278///
279/// When it returns an availability result other than \c AR_Available,
280/// if the \p Message parameter is non-NULL, it will be set to a
281/// string describing why the entity is unavailable.
282///
283/// FIXME: Make these strings localizable, since they end up in
284/// diagnostics.
285static AvailabilityResult CheckAvailability(ASTContext &Context,
286 const AvailabilityAttr *A,
287 std::string *Message) {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000288 StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000289 StringRef PrettyPlatformName
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000290 = AvailabilityAttr::getPrettyPlatformName(TargetPlatform);
291 if (PrettyPlatformName.empty())
292 PrettyPlatformName = TargetPlatform;
293
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000294 VersionTuple TargetMinVersion = Context.getTargetInfo().getPlatformMinVersion();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000295 if (TargetMinVersion.empty())
296 return AR_Available;
297
298 // Match the platform name.
299 if (A->getPlatform()->getName() != TargetPlatform)
300 return AR_Available;
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000301
302 std::string HintMessage;
303 if (!A->getMessage().empty()) {
304 HintMessage = " - ";
305 HintMessage += A->getMessage();
306 }
307
Douglas Gregorb53e4172011-03-26 03:35:55 +0000308 // Make sure that this declaration has not been marked 'unavailable'.
309 if (A->getUnavailable()) {
310 if (Message) {
311 Message->clear();
312 llvm::raw_string_ostream Out(*Message);
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000313 Out << "not available on " << PrettyPlatformName
314 << HintMessage;
Douglas Gregorb53e4172011-03-26 03:35:55 +0000315 }
316
317 return AR_Unavailable;
318 }
319
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000320 // Make sure that this declaration has already been introduced.
321 if (!A->getIntroduced().empty() &&
322 TargetMinVersion < A->getIntroduced()) {
323 if (Message) {
324 Message->clear();
325 llvm::raw_string_ostream Out(*Message);
326 Out << "introduced in " << PrettyPlatformName << ' '
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000327 << A->getIntroduced() << HintMessage;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000328 }
329
330 return AR_NotYetIntroduced;
331 }
332
333 // Make sure that this declaration hasn't been obsoleted.
334 if (!A->getObsoleted().empty() && TargetMinVersion >= A->getObsoleted()) {
335 if (Message) {
336 Message->clear();
337 llvm::raw_string_ostream Out(*Message);
338 Out << "obsoleted in " << PrettyPlatformName << ' '
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000339 << A->getObsoleted() << HintMessage;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000340 }
341
342 return AR_Unavailable;
343 }
344
345 // Make sure that this declaration hasn't been deprecated.
346 if (!A->getDeprecated().empty() && TargetMinVersion >= A->getDeprecated()) {
347 if (Message) {
348 Message->clear();
349 llvm::raw_string_ostream Out(*Message);
350 Out << "first deprecated in " << PrettyPlatformName << ' '
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000351 << A->getDeprecated() << HintMessage;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000352 }
353
354 return AR_Deprecated;
355 }
356
357 return AR_Available;
358}
359
360AvailabilityResult Decl::getAvailability(std::string *Message) const {
361 AvailabilityResult Result = AR_Available;
362 std::string ResultMessage;
363
364 for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
365 if (DeprecatedAttr *Deprecated = dyn_cast<DeprecatedAttr>(*A)) {
366 if (Result >= AR_Deprecated)
367 continue;
368
369 if (Message)
370 ResultMessage = Deprecated->getMessage();
371
372 Result = AR_Deprecated;
373 continue;
374 }
375
376 if (UnavailableAttr *Unavailable = dyn_cast<UnavailableAttr>(*A)) {
377 if (Message)
378 *Message = Unavailable->getMessage();
379 return AR_Unavailable;
380 }
381
382 if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
383 AvailabilityResult AR = CheckAvailability(getASTContext(), Availability,
384 Message);
385
386 if (AR == AR_Unavailable)
387 return AR_Unavailable;
388
389 if (AR > Result) {
390 Result = AR;
391 if (Message)
392 ResultMessage.swap(*Message);
393 }
394 continue;
395 }
396 }
397
398 if (Message)
399 Message->swap(ResultMessage);
400 return Result;
401}
402
403bool Decl::canBeWeakImported(bool &IsDefinition) const {
404 IsDefinition = false;
405 if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
406 if (!Var->hasExternalStorage() || Var->getInit()) {
407 IsDefinition = true;
408 return false;
409 }
410 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
411 if (FD->hasBody()) {
412 IsDefinition = true;
413 return false;
414 }
415 } else if (isa<ObjCPropertyDecl>(this) || isa<ObjCMethodDecl>(this))
416 return false;
417 else if (!(getASTContext().getLangOptions().ObjCNonFragileABI &&
418 isa<ObjCInterfaceDecl>(this)))
419 return false;
420
421 return true;
422}
423
424bool Decl::isWeakImported() const {
425 bool IsDefinition;
426 if (!canBeWeakImported(IsDefinition))
427 return false;
428
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000429 for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
430 if (isa<WeakImportAttr>(*A))
431 return true;
432
433 if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
434 if (CheckAvailability(getASTContext(), Availability, 0)
435 == AR_NotYetIntroduced)
436 return true;
437 }
438 }
439
440 return false;
441}
Tanya Lattner12ead492010-02-17 02:17:21 +0000442
Chris Lattner769dbdf2009-03-27 20:18:19 +0000443unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
444 switch (DeclKind) {
John McCall9488ea12009-11-17 05:59:44 +0000445 case Function:
446 case CXXMethod:
447 case CXXConstructor:
448 case CXXDestructor:
449 case CXXConversion:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000450 case EnumConstant:
451 case Var:
452 case ImplicitParam:
453 case ParmVar:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000454 case NonTypeTemplateParm:
455 case ObjCMethod:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000456 case ObjCProperty:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000457 return IDNS_Ordinary;
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000458 case Label:
459 return IDNS_Label;
Francois Pichet87c2e122010-11-21 06:08:52 +0000460 case IndirectField:
461 return IDNS_Ordinary | IDNS_Member;
462
John McCall0d6b1642010-04-23 18:46:30 +0000463 case ObjCCompatibleAlias:
464 case ObjCInterface:
465 return IDNS_Ordinary | IDNS_Type;
466
467 case Typedef:
Richard Smith162e1c12011-04-15 14:24:37 +0000468 case TypeAlias:
Richard Smith3e4c6c42011-05-05 21:57:07 +0000469 case TypeAliasTemplate:
John McCall0d6b1642010-04-23 18:46:30 +0000470 case UnresolvedUsingTypename:
471 case TemplateTypeParm:
472 return IDNS_Ordinary | IDNS_Type;
473
John McCall9488ea12009-11-17 05:59:44 +0000474 case UsingShadow:
475 return 0; // we'll actually overwrite this later
476
John McCall7ba107a2009-11-18 02:36:19 +0000477 case UnresolvedUsingValue:
John McCall7ba107a2009-11-18 02:36:19 +0000478 return IDNS_Ordinary | IDNS_Using;
John McCall9488ea12009-11-17 05:59:44 +0000479
480 case Using:
481 return IDNS_Using;
482
Chris Lattner769dbdf2009-03-27 20:18:19 +0000483 case ObjCProtocol:
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000484 return IDNS_ObjCProtocol;
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Chris Lattner769dbdf2009-03-27 20:18:19 +0000486 case Field:
487 case ObjCAtDefsField:
488 case ObjCIvar:
489 return IDNS_Member;
Mike Stump1eb44332009-09-09 15:08:12 +0000490
Chris Lattner769dbdf2009-03-27 20:18:19 +0000491 case Record:
492 case CXXRecord:
493 case Enum:
John McCall0d6b1642010-04-23 18:46:30 +0000494 return IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Chris Lattner769dbdf2009-03-27 20:18:19 +0000496 case Namespace:
John McCall0d6b1642010-04-23 18:46:30 +0000497 case NamespaceAlias:
498 return IDNS_Namespace;
499
Chris Lattner769dbdf2009-03-27 20:18:19 +0000500 case FunctionTemplate:
John McCall0d6b1642010-04-23 18:46:30 +0000501 return IDNS_Ordinary;
502
Chris Lattner769dbdf2009-03-27 20:18:19 +0000503 case ClassTemplate:
504 case TemplateTemplateParm:
John McCall0d6b1642010-04-23 18:46:30 +0000505 return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000506
Chris Lattner769dbdf2009-03-27 20:18:19 +0000507 // Never have names.
John McCall02cace72009-08-28 07:59:38 +0000508 case Friend:
John McCalldd4a3b02009-09-16 22:47:08 +0000509 case FriendTemplate:
Abramo Bagnara6206d532010-06-05 05:09:32 +0000510 case AccessSpec:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000511 case LinkageSpec:
512 case FileScopeAsm:
513 case StaticAssert:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000514 case ObjCPropertyImpl:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000515 case Block:
516 case TranslationUnit:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000517
Chris Lattner769dbdf2009-03-27 20:18:19 +0000518 case UsingDirective:
519 case ClassTemplateSpecialization:
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000520 case ClassTemplatePartialSpecialization:
Francois Pichetaf0f4d02011-08-14 03:52:19 +0000521 case ClassScopeFunctionSpecialization:
Douglas Gregorbd4187b2010-04-22 23:19:50 +0000522 case ObjCImplementation:
523 case ObjCCategory:
524 case ObjCCategoryImpl:
Douglas Gregor15de72c2011-12-02 23:23:56 +0000525 case Import:
Douglas Gregorbd4187b2010-04-22 23:19:50 +0000526 // Never looked up by name.
Chris Lattner769dbdf2009-03-27 20:18:19 +0000527 return 0;
528 }
John McCall9488ea12009-11-17 05:59:44 +0000529
530 return 0;
Eli Friedman56d29372008-06-07 16:52:53 +0000531}
532
Sean Huntcf807c42010-08-18 23:23:40 +0000533void Decl::setAttrs(const AttrVec &attrs) {
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000534 assert(!HasAttrs && "Decl already contains attrs.");
535
Sean Huntcf807c42010-08-18 23:23:40 +0000536 AttrVec &AttrBlank = getASTContext().getDeclAttrs(this);
537 assert(AttrBlank.empty() && "HasAttrs was wrong?");
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000538
539 AttrBlank = attrs;
540 HasAttrs = true;
541}
542
Sean Huntcf807c42010-08-18 23:23:40 +0000543void Decl::dropAttrs() {
Eli Friedman56d29372008-06-07 16:52:53 +0000544 if (!HasAttrs) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Eli Friedman56d29372008-06-07 16:52:53 +0000546 HasAttrs = false;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000547 getASTContext().eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000548}
549
Sean Huntcf807c42010-08-18 23:23:40 +0000550const AttrVec &Decl::getAttrs() const {
551 assert(HasAttrs && "No attrs to get!");
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000552 return getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000553}
554
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000555void Decl::swapAttrs(Decl *RHS) {
Eli Friedman56d29372008-06-07 16:52:53 +0000556 bool HasLHSAttr = this->HasAttrs;
557 bool HasRHSAttr = RHS->HasAttrs;
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Eli Friedman56d29372008-06-07 16:52:53 +0000559 // Usually, neither decl has attrs, nothing to do.
560 if (!HasLHSAttr && !HasRHSAttr) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000561
Eli Friedman56d29372008-06-07 16:52:53 +0000562 // If 'this' has no attrs, swap the other way.
563 if (!HasLHSAttr)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000564 return RHS->swapAttrs(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000565
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000566 ASTContext &Context = getASTContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000567
Eli Friedman56d29372008-06-07 16:52:53 +0000568 // Handle the case when both decls have attrs.
569 if (HasRHSAttr) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000570 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman56d29372008-06-07 16:52:53 +0000571 return;
572 }
Mike Stump1eb44332009-09-09 15:08:12 +0000573
Eli Friedman56d29372008-06-07 16:52:53 +0000574 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor68584ed2009-06-18 16:11:24 +0000575 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
576 Context.eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000577 this->HasAttrs = false;
578 RHS->HasAttrs = true;
579}
580
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000581Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000582 Decl::Kind DK = D->getDeclKind();
583 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000584#define DECL(NAME, BASE)
585#define DECL_CONTEXT(NAME) \
586 case Decl::NAME: \
587 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
588#define DECL_CONTEXT_BASE(NAME)
589#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000590 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000591#define DECL(NAME, BASE)
592#define DECL_CONTEXT_BASE(NAME) \
593 if (DK >= first##NAME && DK <= last##NAME) \
594 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
595#include "clang/AST/DeclNodes.inc"
David Blaikieb219cfc2011-09-23 05:06:16 +0000596 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000597 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000598}
599
600DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000601 Decl::Kind DK = D->getKind();
602 switch(DK) {
Sean Hunt9a555912010-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<Decl*>(D));
607#define DECL_CONTEXT_BASE(NAME)
608#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000609 default:
Sean Hunt9a555912010-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<Decl*>(D));
614#include "clang/AST/DeclNodes.inc"
David Blaikieb219cfc2011-09-23 05:06:16 +0000615 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000616 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000617}
618
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000619SourceLocation Decl::getBodyRBrace() const {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000620 // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
621 // FunctionDecl stores EndRangeLoc for this purpose.
622 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
623 const FunctionDecl *Definition;
624 if (FD->hasBody(Definition))
625 return Definition->getSourceRange().getEnd();
626 return SourceLocation();
627 }
628
Argyrios Kyrtzidis6717ef42010-07-07 11:31:27 +0000629 if (Stmt *Body = getBody())
630 return Body->getSourceRange().getEnd();
631
632 return SourceLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +0000633}
634
Anders Carlsson1329c272009-03-25 23:38:06 +0000635void Decl::CheckAccessDeclContext() const {
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000636#ifndef NDEBUG
John McCall46460a62010-01-20 21:53:11 +0000637 // Suppress this check if any of the following hold:
638 // 1. this is the translation unit (and thus has no parent)
639 // 2. this is a template parameter (and thus doesn't belong to its context)
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000640 // 3. this is a non-type template parameter
641 // 4. the context is not a record
642 // 5. it's invalid
643 // 6. it's a C++0x static_assert.
Anders Carlsson35eda442009-08-29 20:47:47 +0000644 if (isa<TranslationUnitDecl>(this) ||
Argyrios Kyrtzidis04aed0e2010-07-02 11:55:44 +0000645 isa<TemplateTypeParmDecl>(this) ||
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000646 isa<NonTypeTemplateParmDecl>(this) ||
Douglas Gregorfdd8ab12010-02-22 17:53:38 +0000647 !isa<CXXRecordDecl>(getDeclContext()) ||
Argyrios Kyrtzidis65b63ec2010-09-08 21:32:35 +0000648 isInvalidDecl() ||
649 isa<StaticAssertDecl>(this) ||
650 // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
651 // as DeclContext (?).
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000652 isa<ParmVarDecl>(this) ||
653 // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
654 // AS_none as access specifier.
Francois Pichetbc845322011-08-17 01:06:54 +0000655 isa<CXXRecordDecl>(this) ||
656 isa<ClassScopeFunctionSpecializationDecl>(this))
Anders Carlsson35eda442009-08-29 20:47:47 +0000657 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000658
659 assert(Access != AS_none &&
Anders Carlsson1329c272009-03-25 23:38:06 +0000660 "Access specifier is AS_none inside a record decl");
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000661#endif
Anders Carlsson1329c272009-03-25 23:38:06 +0000662}
663
John McCallaab9e312011-02-22 22:25:23 +0000664DeclContext *Decl::getNonClosureContext() {
John McCall4b9c2d22011-11-06 09:01:30 +0000665 return getDeclContext()->getNonClosureAncestor();
666}
667
668DeclContext *DeclContext::getNonClosureAncestor() {
669 DeclContext *DC = this;
John McCallaab9e312011-02-22 22:25:23 +0000670
671 // This is basically "while (DC->isClosure()) DC = DC->getParent();"
672 // except that it's significantly more efficient to cast to a known
673 // decl type and call getDeclContext() than to call getParent().
John McCall7b3f8532011-06-23 21:18:31 +0000674 while (isa<BlockDecl>(DC))
675 DC = cast<BlockDecl>(DC)->getDeclContext();
John McCallaab9e312011-02-22 22:25:23 +0000676
677 assert(!DC->isClosure());
678 return DC;
679}
Anders Carlsson1329c272009-03-25 23:38:06 +0000680
Eli Friedman56d29372008-06-07 16:52:53 +0000681//===----------------------------------------------------------------------===//
682// DeclContext Implementation
683//===----------------------------------------------------------------------===//
684
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000685bool DeclContext::classof(const Decl *D) {
686 switch (D->getKind()) {
Sean Hunt9a555912010-05-30 07:21:58 +0000687#define DECL(NAME, BASE)
688#define DECL_CONTEXT(NAME) case Decl::NAME:
689#define DECL_CONTEXT_BASE(NAME)
690#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000691 return true;
692 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000693#define DECL(NAME, BASE)
694#define DECL_CONTEXT_BASE(NAME) \
695 if (D->getKind() >= Decl::first##NAME && \
696 D->getKind() <= Decl::last##NAME) \
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000697 return true;
Sean Hunt9a555912010-05-30 07:21:58 +0000698#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000699 return false;
700 }
701}
702
Douglas Gregora2da7802010-07-25 18:38:02 +0000703DeclContext::~DeclContext() { }
Douglas Gregor44b43212008-12-11 16:49:14 +0000704
Douglas Gregore942bbe2009-09-10 16:57:35 +0000705/// \brief Find the parent context of this context that will be
706/// used for unqualified name lookup.
707///
708/// Generally, the parent lookup context is the semantic context. However, for
709/// a friend function the parent lookup context is the lexical context, which
710/// is the class in which the friend is declared.
711DeclContext *DeclContext::getLookupParent() {
712 // FIXME: Find a better way to identify friends
713 if (isa<FunctionDecl>(this))
Sebastian Redl7a126a42010-08-31 00:36:30 +0000714 if (getParent()->getRedeclContext()->isFileContext() &&
715 getLexicalParent()->getRedeclContext()->isRecord())
Douglas Gregore942bbe2009-09-10 16:57:35 +0000716 return getLexicalParent();
717
718 return getParent();
719}
720
Sebastian Redl410c4f22010-08-31 20:53:31 +0000721bool DeclContext::isInlineNamespace() const {
722 return isNamespace() &&
723 cast<NamespaceDecl>(this)->isInline();
724}
725
Douglas Gregorbc221632009-05-28 16:34:51 +0000726bool DeclContext::isDependentContext() const {
727 if (isFileContext())
728 return false;
729
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000730 if (isa<ClassTemplatePartialSpecializationDecl>(this))
731 return true;
732
Douglas Gregorbc221632009-05-28 16:34:51 +0000733 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
734 if (Record->getDescribedClassTemplate())
735 return true;
736
John McCall0c01d182010-03-24 05:22:00 +0000737 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregorbc221632009-05-28 16:34:51 +0000738 if (Function->getDescribedFunctionTemplate())
739 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000740
John McCall0c01d182010-03-24 05:22:00 +0000741 // Friend function declarations are dependent if their *lexical*
742 // context is dependent.
743 if (cast<Decl>(this)->getFriendObjectKind())
744 return getLexicalParent()->isDependentContext();
745 }
746
Douglas Gregorbc221632009-05-28 16:34:51 +0000747 return getParent() && getParent()->isDependentContext();
748}
749
Douglas Gregor074149e2009-01-05 19:45:36 +0000750bool DeclContext::isTransparentContext() const {
751 if (DeclKind == Decl::Enum)
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000752 return !cast<EnumDecl>(this)->isScoped();
Douglas Gregor074149e2009-01-05 19:45:36 +0000753 else if (DeclKind == Decl::LinkageSpec)
754 return true;
Douglas Gregor074149e2009-01-05 19:45:36 +0000755
756 return false;
757}
758
John McCallac65c622010-10-26 04:59:26 +0000759bool DeclContext::isExternCContext() const {
760 const DeclContext *DC = this;
761 while (DC->DeclKind != Decl::TranslationUnit) {
762 if (DC->DeclKind == Decl::LinkageSpec)
763 return cast<LinkageSpecDecl>(DC)->getLanguage()
764 == LinkageSpecDecl::lang_c;
765 DC = DC->getParent();
766 }
767 return false;
768}
769
Sebastian Redl7a126a42010-08-31 00:36:30 +0000770bool DeclContext::Encloses(const DeclContext *DC) const {
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000771 if (getPrimaryContext() != this)
772 return getPrimaryContext()->Encloses(DC);
Mike Stump1eb44332009-09-09 15:08:12 +0000773
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000774 for (; DC; DC = DC->getParent())
775 if (DC->getPrimaryContext() == this)
776 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000777 return false;
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000778}
779
Steve Naroff0701bbb2009-01-08 17:28:14 +0000780DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000781 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000782 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000783 case Decl::LinkageSpec:
Mike Stump1eb44332009-09-09 15:08:12 +0000784 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000785 // There is only one DeclContext for these entities.
786 return this;
787
788 case Decl::Namespace:
789 // The original namespace is our primary context.
790 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
791
Douglas Gregor44b43212008-12-11 16:49:14 +0000792 case Decl::ObjCMethod:
793 return this;
794
795 case Decl::ObjCInterface:
Douglas Gregor53df7a12011-12-15 18:03:09 +0000796 if (ObjCInterfaceDecl *Def = cast<ObjCInterfaceDecl>(this)->getDefinition())
797 return Def;
798
799 return this;
800
Steve Naroff0701bbb2009-01-08 17:28:14 +0000801 case Decl::ObjCProtocol:
Douglas Gregor1d784b22012-01-01 19:51:50 +0000802 if (ObjCProtocolDecl *Def = cast<ObjCProtocolDecl>(this)->getDefinition())
803 return Def;
804
805 return this;
Douglas Gregor53df7a12011-12-15 18:03:09 +0000806
Steve Naroff0701bbb2009-01-08 17:28:14 +0000807 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000808 return this;
809
Steve Naroff0701bbb2009-01-08 17:28:14 +0000810 case Decl::ObjCImplementation:
811 case Decl::ObjCCategoryImpl:
812 return this;
813
Douglas Gregor44b43212008-12-11 16:49:14 +0000814 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000815 if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000816 // If this is a tag type that has a definition or is currently
817 // being defined, that definition is our primary context.
John McCall3cb0ebd2010-03-10 03:28:59 +0000818 TagDecl *Tag = cast<TagDecl>(this);
819 assert(isa<TagType>(Tag->TypeForDecl) ||
820 isa<InjectedClassNameType>(Tag->TypeForDecl));
821
822 if (TagDecl *Def = Tag->getDefinition())
823 return Def;
824
825 if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) {
826 const TagType *TagTy = cast<TagType>(Tag->TypeForDecl);
827 if (TagTy->isBeingDefined())
828 // FIXME: is it necessarily being defined in the decl
829 // that owns the type?
830 return TagTy->getDecl();
831 }
832
833 return Tag;
Douglas Gregorcc636682009-02-17 23:15:12 +0000834 }
835
Sean Hunt9a555912010-05-30 07:21:58 +0000836 assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
Douglas Gregor44b43212008-12-11 16:49:14 +0000837 "Unknown DeclContext kind");
838 return this;
839 }
840}
841
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +0000842void
843DeclContext::collectAllContexts(llvm::SmallVectorImpl<DeclContext *> &Contexts){
844 Contexts.clear();
845
846 if (DeclKind != Decl::Namespace) {
847 Contexts.push_back(this);
848 return;
Douglas Gregor44b43212008-12-11 16:49:14 +0000849 }
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +0000850
851 NamespaceDecl *Self = static_cast<NamespaceDecl *>(this);
852 for (NamespaceDecl *N = Self->getMostRecentDeclaration(); N;
853 N = N->getPreviousDeclaration())
854 Contexts.push_back(N);
855
856 std::reverse(Contexts.begin(), Contexts.end());
Douglas Gregor44b43212008-12-11 16:49:14 +0000857}
858
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000859std::pair<Decl *, Decl *>
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +0000860DeclContext::BuildDeclChain(const SmallVectorImpl<Decl*> &Decls,
861 bool FieldsAlreadyLoaded) {
Douglas Gregor46cd2182012-01-06 16:59:53 +0000862 // Build up a chain of declarations via the Decl::NextInContextAndBits field.
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000863 Decl *FirstNewDecl = 0;
864 Decl *PrevDecl = 0;
865 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +0000866 if (FieldsAlreadyLoaded && isa<FieldDecl>(Decls[I]))
867 continue;
868
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000869 Decl *D = Decls[I];
870 if (PrevDecl)
Douglas Gregor46cd2182012-01-06 16:59:53 +0000871 PrevDecl->NextInContextAndBits.setPointer(D);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000872 else
873 FirstNewDecl = D;
874
875 PrevDecl = D;
876 }
877
878 return std::make_pair(FirstNewDecl, PrevDecl);
879}
880
Douglas Gregor2cf26342009-04-09 22:27:44 +0000881/// \brief Load the declarations within this lexical storage from an
882/// external source.
Mike Stump1eb44332009-09-09 15:08:12 +0000883void
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000884DeclContext::LoadLexicalDeclsFromExternalStorage() const {
885 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000886 assert(hasExternalLexicalStorage() && Source && "No external storage?");
887
Argyrios Kyrtzidis0dbbc042010-07-30 10:03:23 +0000888 // Notify that we have a DeclContext that is initializing.
889 ExternalASTSource::Deserializing ADeclContext(Source);
Douglas Gregor9fc18c92011-08-26 21:23:06 +0000890
Douglas Gregorba6ffaf2011-07-15 21:46:17 +0000891 // Load the external declarations, if any.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000892 SmallVector<Decl*, 64> Decls;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000893 ExternalLexicalStorage = false;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +0000894 switch (Source->FindExternalLexicalDecls(this, Decls)) {
895 case ELR_Success:
896 break;
897
898 case ELR_Failure:
899 case ELR_AlreadyLoaded:
900 return;
901 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000902
903 if (Decls.empty())
904 return;
905
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +0000906 // We may have already loaded just the fields of this record, in which case
907 // we need to ignore them.
908 bool FieldsAlreadyLoaded = false;
909 if (const RecordDecl *RD = dyn_cast<RecordDecl>(this))
910 FieldsAlreadyLoaded = RD->LoadedFieldsFromExternalStorage;
911
Douglas Gregor2cf26342009-04-09 22:27:44 +0000912 // Splice the newly-read declarations into the beginning of the list
913 // of declarations.
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000914 Decl *ExternalFirst, *ExternalLast;
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +0000915 llvm::tie(ExternalFirst, ExternalLast) = BuildDeclChain(Decls,
916 FieldsAlreadyLoaded);
Douglas Gregor46cd2182012-01-06 16:59:53 +0000917 ExternalLast->NextInContextAndBits.setPointer(FirstDecl);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000918 FirstDecl = ExternalFirst;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000919 if (!LastDecl)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000920 LastDecl = ExternalLast;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000921}
922
John McCall76bd1f32010-06-01 09:23:16 +0000923DeclContext::lookup_result
924ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
925 DeclarationName Name) {
926 ASTContext &Context = DC->getParentASTContext();
927 StoredDeclsMap *Map;
928 if (!(Map = DC->LookupPtr))
929 Map = DC->CreateStoredDeclsMap(Context);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000930
John McCall76bd1f32010-06-01 09:23:16 +0000931 StoredDeclsList &List = (*Map)[Name];
932 assert(List.isNull());
933 (void) List;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000934
John McCall76bd1f32010-06-01 09:23:16 +0000935 return DeclContext::lookup_result();
936}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000937
John McCall76bd1f32010-06-01 09:23:16 +0000938DeclContext::lookup_result
939ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +0000940 DeclarationName Name,
Argyrios Kyrtzidis45df9c62011-09-09 06:44:14 +0000941 ArrayRef<NamedDecl*> Decls) {
John McCall76bd1f32010-06-01 09:23:16 +0000942 ASTContext &Context = DC->getParentASTContext();;
943
944 StoredDeclsMap *Map;
945 if (!(Map = DC->LookupPtr))
946 Map = DC->CreateStoredDeclsMap(Context);
947
948 StoredDeclsList &List = (*Map)[Name];
Argyrios Kyrtzidis45df9c62011-09-09 06:44:14 +0000949 for (ArrayRef<NamedDecl*>::iterator
950 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
John McCall76bd1f32010-06-01 09:23:16 +0000951 if (List.isNull())
Argyrios Kyrtzidis45df9c62011-09-09 06:44:14 +0000952 List.setOnlyValue(*I);
John McCall76bd1f32010-06-01 09:23:16 +0000953 else
Argyrios Kyrtzidis45df9c62011-09-09 06:44:14 +0000954 List.AddSubsequentDecl(*I);
John McCall76bd1f32010-06-01 09:23:16 +0000955 }
956
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000957 return List.getLookupResult();
John McCall76bd1f32010-06-01 09:23:16 +0000958}
959
Sebastian Redl681d7232010-07-27 00:17:23 +0000960DeclContext::decl_iterator DeclContext::noload_decls_begin() const {
961 return decl_iterator(FirstDecl);
962}
963
964DeclContext::decl_iterator DeclContext::noload_decls_end() const {
965 return decl_iterator();
966}
967
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000968DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000969 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000970 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000971
Mike Stump1eb44332009-09-09 15:08:12 +0000972 return decl_iterator(FirstDecl);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000973}
974
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000975DeclContext::decl_iterator DeclContext::decls_end() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000976 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000977 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000978
Mike Stump1eb44332009-09-09 15:08:12 +0000979 return decl_iterator();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000980}
981
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000982bool DeclContext::decls_empty() const {
Douglas Gregor8038d512009-04-10 17:25:41 +0000983 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000984 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor8038d512009-04-10 17:25:41 +0000985
986 return !FirstDecl;
987}
988
John McCall9f54ad42009-12-10 09:41:52 +0000989void DeclContext::removeDecl(Decl *D) {
990 assert(D->getLexicalDeclContext() == this &&
991 "decl being removed from non-lexical context");
Douglas Gregor46cd2182012-01-06 16:59:53 +0000992 assert((D->NextInContextAndBits.getPointer() || D == LastDecl) &&
John McCall9f54ad42009-12-10 09:41:52 +0000993 "decl is not in decls list");
994
995 // Remove D from the decl chain. This is O(n) but hopefully rare.
996 if (D == FirstDecl) {
997 if (D == LastDecl)
998 FirstDecl = LastDecl = 0;
999 else
Douglas Gregor46cd2182012-01-06 16:59:53 +00001000 FirstDecl = D->NextInContextAndBits.getPointer();
John McCall9f54ad42009-12-10 09:41:52 +00001001 } else {
Douglas Gregor46cd2182012-01-06 16:59:53 +00001002 for (Decl *I = FirstDecl; true; I = I->NextInContextAndBits.getPointer()) {
John McCall9f54ad42009-12-10 09:41:52 +00001003 assert(I && "decl not found in linked list");
Douglas Gregor46cd2182012-01-06 16:59:53 +00001004 if (I->NextInContextAndBits.getPointer() == D) {
1005 I->NextInContextAndBits.setPointer(D->NextInContextAndBits.getPointer());
John McCall9f54ad42009-12-10 09:41:52 +00001006 if (D == LastDecl) LastDecl = I;
1007 break;
1008 }
1009 }
1010 }
1011
1012 // Mark that D is no longer in the decl chain.
Douglas Gregor46cd2182012-01-06 16:59:53 +00001013 D->NextInContextAndBits.setPointer(0);
John McCall9f54ad42009-12-10 09:41:52 +00001014
1015 // Remove D from the lookup table if necessary.
1016 if (isa<NamedDecl>(D)) {
1017 NamedDecl *ND = cast<NamedDecl>(D);
1018
Axel Naumann02368d02011-08-26 14:06:12 +00001019 // Remove only decls that have a name
1020 if (!ND->getDeclName()) return;
1021
John McCall0c01d182010-03-24 05:22:00 +00001022 StoredDeclsMap *Map = getPrimaryContext()->LookupPtr;
1023 if (!Map) return;
John McCall9f54ad42009-12-10 09:41:52 +00001024
John McCall9f54ad42009-12-10 09:41:52 +00001025 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
1026 assert(Pos != Map->end() && "no lookup entry for decl");
Axel Naumannd9d137e2011-11-08 18:21:06 +00001027 if (Pos->second.getAsVector() || Pos->second.getAsDecl() == ND)
1028 Pos->second.remove(ND);
John McCall9f54ad42009-12-10 09:41:52 +00001029 }
1030}
1031
John McCall3f9a8a62009-08-11 06:59:38 +00001032void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner7f0be132009-02-20 00:56:18 +00001033 assert(D->getLexicalDeclContext() == this &&
1034 "Decl inserted into wrong lexical context");
Mike Stump1eb44332009-09-09 15:08:12 +00001035 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001036 "Decl already inserted into a DeclContext");
1037
1038 if (FirstDecl) {
Douglas Gregor46cd2182012-01-06 16:59:53 +00001039 LastDecl->NextInContextAndBits.setPointer(D);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001040 LastDecl = D;
1041 } else {
1042 FirstDecl = LastDecl = D;
1043 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +00001044
1045 // Notify a C++ record declaration that we've added a member, so it can
1046 // update it's class-specific state.
1047 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
1048 Record->addedMember(D);
Douglas Gregore6649772011-12-03 00:30:27 +00001049
1050 // If this is a newly-created (not de-serialized) import declaration, wire
1051 // it in to the list of local import declarations.
1052 if (!D->isFromASTFile()) {
1053 if (ImportDecl *Import = dyn_cast<ImportDecl>(D))
1054 D->getASTContext().addedLocalImportDecl(Import);
1055 }
John McCall3f9a8a62009-08-11 06:59:38 +00001056}
1057
1058void DeclContext::addDecl(Decl *D) {
1059 addHiddenDecl(D);
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001060
1061 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001062 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor44b43212008-12-11 16:49:14 +00001063}
1064
Sean Callanan9faf8102011-10-21 02:57:43 +00001065void DeclContext::addDeclInternal(Decl *D) {
1066 addHiddenDecl(D);
1067
1068 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1069 ND->getDeclContext()->makeDeclVisibleInContextInternal(ND);
1070}
1071
Douglas Gregor074149e2009-01-05 19:45:36 +00001072/// buildLookup - Build the lookup data structure with all of the
1073/// declarations in DCtx (and any other contexts linked to it or
1074/// transparent contexts nested within it).
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001075void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001076 llvm::SmallVector<DeclContext *, 2> Contexts;
1077 DCtx->collectAllContexts(Contexts);
1078 for (unsigned I = 0, N = Contexts.size(); I != N; ++I) {
1079 for (decl_iterator D = Contexts[I]->decls_begin(),
1080 DEnd = Contexts[I]->decls_end();
Douglas Gregor4f3b8f82009-01-06 07:17:58 +00001081 D != DEnd; ++D) {
John McCall3f9a8a62009-08-11 06:59:38 +00001082 // Insert this declaration into the lookup structure, but only
1083 // if it's semantically in its decl context. During non-lazy
1084 // lookup building, this is implicitly enforced by addDecl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001085 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001086 if (D->getDeclContext() == Contexts[I])
Sean Callanan9faf8102011-10-21 02:57:43 +00001087 makeDeclVisibleInContextImpl(ND, false);
Douglas Gregor074149e2009-01-05 19:45:36 +00001088
Sebastian Redl410c4f22010-08-31 20:53:31 +00001089 // If this declaration is itself a transparent declaration context or
1090 // inline namespace, add its members (recursively).
Douglas Gregor074149e2009-01-05 19:45:36 +00001091 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
Sebastian Redl410c4f22010-08-31 20:53:31 +00001092 if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001093 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregor074149e2009-01-05 19:45:36 +00001094 }
1095 }
1096}
1097
Mike Stump1eb44332009-09-09 15:08:12 +00001098DeclContext::lookup_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001099DeclContext::lookup(DeclarationName Name) {
Steve Naroff0701bbb2009-01-08 17:28:14 +00001100 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +00001101 if (PrimaryContext != this)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001102 return PrimaryContext->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +00001103
John McCall76bd1f32010-06-01 09:23:16 +00001104 if (hasExternalVisibleStorage()) {
1105 // Check to see if we've already cached the lookup results.
1106 if (LookupPtr) {
1107 StoredDeclsMap::iterator I = LookupPtr->find(Name);
1108 if (I != LookupPtr->end())
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001109 return I->second.getLookupResult();
John McCall76bd1f32010-06-01 09:23:16 +00001110 }
1111
1112 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1113 return Source->FindExternalVisibleDeclsByName(this, Name);
1114 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001115
Douglas Gregor3fc749d2008-12-23 00:26:44 +00001116 /// If there is no lookup data structure, build one now by walking
Douglas Gregor44b43212008-12-11 16:49:14 +00001117 /// all of the linked DeclContexts (in declaration order!) and
1118 /// inserting their values.
Douglas Gregorc36c5402009-04-09 17:29:08 +00001119 if (!LookupPtr) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001120 buildLookup(this);
Douglas Gregor44b43212008-12-11 16:49:14 +00001121
Douglas Gregorc36c5402009-04-09 17:29:08 +00001122 if (!LookupPtr)
Douglas Gregora5fdd9c2010-05-11 06:18:17 +00001123 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Douglas Gregorc36c5402009-04-09 17:29:08 +00001124 }
Douglas Gregor44b43212008-12-11 16:49:14 +00001125
John McCall0c01d182010-03-24 05:22:00 +00001126 StoredDeclsMap::iterator Pos = LookupPtr->find(Name);
1127 if (Pos == LookupPtr->end())
Douglas Gregora5fdd9c2010-05-11 06:18:17 +00001128 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001129 return Pos->second.getLookupResult();
Douglas Gregor44b43212008-12-11 16:49:14 +00001130}
1131
Mike Stump1eb44332009-09-09 15:08:12 +00001132DeclContext::lookup_const_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001133DeclContext::lookup(DeclarationName Name) const {
1134 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +00001135}
1136
Douglas Gregorb75a3452011-10-15 00:10:27 +00001137void DeclContext::localUncachedLookup(DeclarationName Name,
1138 llvm::SmallVectorImpl<NamedDecl *> &Results) {
1139 Results.clear();
1140
1141 // If there's no external storage, just perform a normal lookup and copy
1142 // the results.
1143 if (!hasExternalVisibleStorage() && !hasExternalLexicalStorage()) {
1144 lookup_result LookupResults = lookup(Name);
1145 Results.insert(Results.end(), LookupResults.first, LookupResults.second);
1146 return;
1147 }
1148
1149 // If we have a lookup table, check there first. Maybe we'll get lucky.
1150 if (LookupPtr) {
1151 StoredDeclsMap::iterator Pos = LookupPtr->find(Name);
1152 if (Pos != LookupPtr->end()) {
1153 Results.insert(Results.end(),
1154 Pos->second.getLookupResult().first,
1155 Pos->second.getLookupResult().second);
1156 return;
1157 }
1158 }
1159
1160 // Slow case: grovel through the declarations in our chain looking for
1161 // matches.
1162 for (Decl *D = FirstDecl; D; D = D->getNextDeclInContext()) {
1163 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1164 if (ND->getDeclName() == Name)
1165 Results.push_back(ND);
1166 }
1167}
1168
Sebastian Redl7a126a42010-08-31 00:36:30 +00001169DeclContext *DeclContext::getRedeclContext() {
Chris Lattner0cf2b192009-03-27 19:19:59 +00001170 DeclContext *Ctx = this;
Sebastian Redl410c4f22010-08-31 20:53:31 +00001171 // Skip through transparent contexts.
1172 while (Ctx->isTransparentContext())
Douglas Gregorce356072009-01-06 23:51:29 +00001173 Ctx = Ctx->getParent();
1174 return Ctx;
1175}
1176
Douglas Gregor88b70942009-02-25 22:02:03 +00001177DeclContext *DeclContext::getEnclosingNamespaceContext() {
1178 DeclContext *Ctx = this;
1179 // Skip through non-namespace, non-translation-unit contexts.
Sebastian Redl51a8a372010-08-31 00:36:23 +00001180 while (!Ctx->isFileContext())
Douglas Gregor88b70942009-02-25 22:02:03 +00001181 Ctx = Ctx->getParent();
1182 return Ctx->getPrimaryContext();
1183}
1184
Sebastian Redl7a126a42010-08-31 00:36:30 +00001185bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
1186 // For non-file contexts, this is equivalent to Equals.
1187 if (!isFileContext())
1188 return O->Equals(this);
1189
1190 do {
1191 if (O->Equals(this))
1192 return true;
1193
1194 const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O);
1195 if (!NS || !NS->isInline())
1196 break;
1197 O = NS->getParent();
1198 } while (O);
1199
1200 return false;
1201}
1202
Sean Callanan9faf8102011-10-21 02:57:43 +00001203void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable)
1204{
1205 makeDeclVisibleInContextWithFlags(D, false, Recoverable);
1206}
1207
1208void DeclContext::makeDeclVisibleInContextInternal(NamedDecl *D, bool Recoverable)
1209{
1210 makeDeclVisibleInContextWithFlags(D, true, Recoverable);
1211}
1212
1213void DeclContext::makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal, bool Recoverable) {
Douglas Gregorcc636682009-02-17 23:15:12 +00001214 // FIXME: This feels like a hack. Should DeclarationName support
1215 // template-ids, or is there a better way to keep specializations
1216 // from being visible?
Douglas Gregor9a299e02011-03-04 17:52:15 +00001217 if (isa<ClassTemplateSpecializationDecl>(D) || D->isTemplateParameter())
Douglas Gregorcc636682009-02-17 23:15:12 +00001218 return;
Eli Friedman6bc20132009-12-08 05:40:03 +00001219 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1220 if (FD->isFunctionTemplateSpecialization())
1221 return;
Douglas Gregorcc636682009-02-17 23:15:12 +00001222
Steve Naroff0701bbb2009-01-08 17:28:14 +00001223 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +00001224 if (PrimaryContext != this) {
Sean Callanan9faf8102011-10-21 02:57:43 +00001225 PrimaryContext->makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
Douglas Gregor44b43212008-12-11 16:49:14 +00001226 return;
1227 }
1228
1229 // If we already have a lookup data structure, perform the insertion
Argyrios Kyrtzidis5586b012010-07-04 21:44:25 +00001230 // into it. If we haven't deserialized externally stored decls, deserialize
1231 // them so we can add the decl. Otherwise, be lazy and don't build that
1232 // structure until someone asks for it.
1233 if (LookupPtr || !Recoverable || hasExternalVisibleStorage())
Sean Callanan9faf8102011-10-21 02:57:43 +00001234 makeDeclVisibleInContextImpl(D, Internal);
Douglas Gregor074149e2009-01-05 19:45:36 +00001235
Sebastian Redl410c4f22010-08-31 20:53:31 +00001236 // If we are a transparent context or inline namespace, insert into our
1237 // parent context, too. This operation is recursive.
1238 if (isTransparentContext() || isInlineNamespace())
Sean Callanan9faf8102011-10-21 02:57:43 +00001239 getParent()->makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00001240
1241 Decl *DCAsDecl = cast<Decl>(this);
1242 // Notify that a decl was made visible unless it's a Tag being defined.
1243 if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined()))
1244 if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
1245 L->AddedVisibleDecl(this, D);
Douglas Gregor44b43212008-12-11 16:49:14 +00001246}
1247
Sean Callanan9faf8102011-10-21 02:57:43 +00001248void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal) {
Douglas Gregor074149e2009-01-05 19:45:36 +00001249 // Skip unnamed declarations.
1250 if (!D->getDeclName())
1251 return;
1252
Douglas Gregor5cb0ef42011-05-06 23:32:38 +00001253 // Skip entities that can't be found by name lookup into a particular
1254 // context.
1255 if ((D->getIdentifierNamespace() == 0 && !isa<UsingDirectiveDecl>(D)) ||
1256 D->isTemplateParameter())
Douglas Gregorcc636682009-02-17 23:15:12 +00001257 return;
1258
Argyrios Kyrtzidis5586b012010-07-04 21:44:25 +00001259 ASTContext *C = 0;
1260 if (!LookupPtr) {
1261 C = &getParentASTContext();
1262 CreateStoredDeclsMap(*C);
1263 }
1264
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001265 // If there is an external AST source, load any declarations it knows about
1266 // with this declaration's name.
1267 // If the lookup table contains an entry about this name it means that we
1268 // have already checked the external source.
Sean Callanan9faf8102011-10-21 02:57:43 +00001269 if (!Internal)
1270 if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
1271 if (hasExternalVisibleStorage() &&
1272 LookupPtr->find(D->getDeclName()) == LookupPtr->end())
1273 Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001274
Douglas Gregor44b43212008-12-11 16:49:14 +00001275 // Insert this declaration into the map.
John McCall0c01d182010-03-24 05:22:00 +00001276 StoredDeclsList &DeclNameEntries = (*LookupPtr)[D->getDeclName()];
Chris Lattner67762a32009-02-20 01:44:05 +00001277 if (DeclNameEntries.isNull()) {
1278 DeclNameEntries.setOnlyValue(D);
Chris Lattnerbd6c8002009-02-19 07:00:44 +00001279 return;
Douglas Gregor44b43212008-12-11 16:49:14 +00001280 }
Chris Lattner91942502009-02-20 00:55:03 +00001281
Chris Lattnerbdc3d002009-02-20 01:10:07 +00001282 // If it is possible that this is a redeclaration, check to see if there is
1283 // already a decl for which declarationReplaces returns true. If there is
1284 // one, just replace it and return.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001285 if (DeclNameEntries.HandleRedeclaration(D))
Chris Lattner67762a32009-02-20 01:44:05 +00001286 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001287
Chris Lattnerbd6c8002009-02-19 07:00:44 +00001288 // Put this declaration into the appropriate slot.
Chris Lattner67762a32009-02-20 01:44:05 +00001289 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor44b43212008-12-11 16:49:14 +00001290}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001291
1292/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
1293/// this context.
Mike Stump1eb44332009-09-09 15:08:12 +00001294DeclContext::udir_iterator_range
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001295DeclContext::getUsingDirectives() const {
1296 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001297 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
1298 reinterpret_cast<udir_iterator>(Result.second));
1299}
Douglas Gregor2cf26342009-04-09 22:27:44 +00001300
Ted Kremenek3478eb62010-02-11 07:12:28 +00001301//===----------------------------------------------------------------------===//
1302// Creation and Destruction of StoredDeclsMaps. //
1303//===----------------------------------------------------------------------===//
1304
John McCall0c01d182010-03-24 05:22:00 +00001305StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
1306 assert(!LookupPtr && "context already has a decls map");
1307 assert(getPrimaryContext() == this &&
1308 "creating decls map on non-primary context");
1309
1310 StoredDeclsMap *M;
1311 bool Dependent = isDependentContext();
1312 if (Dependent)
1313 M = new DependentStoredDeclsMap();
1314 else
1315 M = new StoredDeclsMap();
1316 M->Previous = C.LastSDM;
1317 C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
1318 LookupPtr = M;
Ted Kremenek3478eb62010-02-11 07:12:28 +00001319 return M;
1320}
1321
1322void ASTContext::ReleaseDeclContextMaps() {
John McCall0c01d182010-03-24 05:22:00 +00001323 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1324 // pointer because the subclass doesn't add anything that needs to
1325 // be deleted.
John McCall0c01d182010-03-24 05:22:00 +00001326 StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1327}
1328
1329void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1330 while (Map) {
1331 // Advance the iteration before we invalidate memory.
1332 llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1333
1334 if (Dependent)
1335 delete static_cast<DependentStoredDeclsMap*>(Map);
1336 else
1337 delete Map;
1338
1339 Map = Next.getPointer();
1340 Dependent = Next.getInt();
1341 }
1342}
1343
John McCall0c01d182010-03-24 05:22:00 +00001344DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1345 DeclContext *Parent,
1346 const PartialDiagnostic &PDiag) {
1347 assert(Parent->isDependentContext()
1348 && "cannot iterate dependent diagnostics of non-dependent context");
1349 Parent = Parent->getPrimaryContext();
1350 if (!Parent->LookupPtr)
1351 Parent->CreateStoredDeclsMap(C);
1352
1353 DependentStoredDeclsMap *Map
1354 = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr);
1355
Douglas Gregorb8365182010-03-29 23:56:53 +00001356 // Allocate the copy of the PartialDiagnostic via the ASTContext's
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00001357 // BumpPtrAllocator, rather than the ASTContext itself.
Douglas Gregorb8365182010-03-29 23:56:53 +00001358 PartialDiagnostic::Storage *DiagStorage = 0;
1359 if (PDiag.hasStorage())
1360 DiagStorage = new (C) PartialDiagnostic::Storage;
1361
1362 DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
John McCall0c01d182010-03-24 05:22:00 +00001363
1364 // TODO: Maybe we shouldn't reverse the order during insertion.
1365 DD->NextDiagnostic = Map->FirstDiagnostic;
1366 Map->FirstDiagnostic = DD;
1367
1368 return DD;
Ted Kremenek3478eb62010-02-11 07:12:28 +00001369}