blob: 47d2def39634c6fcd8039261b653ed943f23d879 [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
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +000048 // resulting pointer will still be 8-byte aligned.
Douglas Gregor5d1f4962012-01-05 23:49:36 +000049 void *Start = Context.Allocate(Size + 8);
50 void *Result = (char*)Start + 8;
Douglas Gregorb6b60c12012-01-05 22:27:05 +000051
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +000052 unsigned *PrefixPtr = (unsigned *)Result - 2;
53
54 // Zero out the first 4 bytes; this is used to store the owning module ID.
55 PrefixPtr[0] = 0;
56
57 // Store the global declaration ID in the second 4 bytes.
58 PrefixPtr[1] = ID;
Douglas Gregorb6b60c12012-01-05 22:27:05 +000059
60 return Result;
Douglas Gregor1e68ecc2012-01-05 21:55:30 +000061}
62
Eli Friedman56d29372008-06-07 16:52:53 +000063const char *Decl::getDeclKindName() const {
64 switch (DeclKind) {
David Blaikieb219cfc2011-09-23 05:06:16 +000065 default: llvm_unreachable("Declaration not in DeclNodes.inc!");
Sean Hunt9a555912010-05-30 07:21:58 +000066#define DECL(DERIVED, BASE) case DERIVED: return #DERIVED;
67#define ABSTRACT_DECL(DECL)
68#include "clang/AST/DeclNodes.inc"
Eli Friedman56d29372008-06-07 16:52:53 +000069 }
70}
71
Douglas Gregor42738572010-03-05 00:26:45 +000072void Decl::setInvalidDecl(bool Invalid) {
73 InvalidDecl = Invalid;
74 if (Invalid) {
75 // Defensive maneuver for ill-formed code: we're likely not to make it to
76 // a point where we set the access specifier, so default it to "public"
77 // to avoid triggering asserts elsewhere in the front end.
78 setAccess(AS_public);
79 }
80}
81
Steve Naroff0a473932009-01-20 19:53:53 +000082const char *DeclContext::getDeclKindName() const {
83 switch (DeclKind) {
David Blaikieb219cfc2011-09-23 05:06:16 +000084 default: llvm_unreachable("Declaration context not in DeclNodes.inc!");
Sean Hunt9a555912010-05-30 07:21:58 +000085#define DECL(DERIVED, BASE) case Decl::DERIVED: return #DERIVED;
86#define ABSTRACT_DECL(DECL)
87#include "clang/AST/DeclNodes.inc"
Steve Naroff0a473932009-01-20 19:53:53 +000088 }
89}
90
Eli Friedman56d29372008-06-07 16:52:53 +000091bool Decl::CollectingStats(bool Enable) {
Kovarththanan Rajaratnam2024f4d2009-11-29 14:54:35 +000092 if (Enable) StatSwitch = true;
Eli Friedman56d29372008-06-07 16:52:53 +000093 return StatSwitch;
94}
95
96void Decl::PrintStats() {
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000097 llvm::errs() << "\n*** Decl Stats:\n";
Mike Stump1eb44332009-09-09 15:08:12 +000098
Douglas Gregor64650af2009-02-02 23:39:07 +000099 int totalDecls = 0;
Sean Hunt9a555912010-05-30 07:21:58 +0000100#define DECL(DERIVED, BASE) totalDecls += n##DERIVED##s;
101#define ABSTRACT_DECL(DECL)
102#include "clang/AST/DeclNodes.inc"
Chandler Carruthb43c8ec2011-07-04 06:13:27 +0000103 llvm::errs() << " " << totalDecls << " decls total.\n";
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Douglas Gregor64650af2009-02-02 23:39:07 +0000105 int totalBytes = 0;
Sean Hunt9a555912010-05-30 07:21:58 +0000106#define DECL(DERIVED, BASE) \
107 if (n##DERIVED##s > 0) { \
108 totalBytes += (int)(n##DERIVED##s * sizeof(DERIVED##Decl)); \
Chandler Carruthb43c8ec2011-07-04 06:13:27 +0000109 llvm::errs() << " " << n##DERIVED##s << " " #DERIVED " decls, " \
110 << sizeof(DERIVED##Decl) << " each (" \
111 << n##DERIVED##s * sizeof(DERIVED##Decl) \
112 << " bytes)\n"; \
Douglas Gregor64650af2009-02-02 23:39:07 +0000113 }
Sean Hunt9a555912010-05-30 07:21:58 +0000114#define ABSTRACT_DECL(DECL)
115#include "clang/AST/DeclNodes.inc"
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Chandler Carruthb43c8ec2011-07-04 06:13:27 +0000117 llvm::errs() << "Total bytes = " << totalBytes << "\n";
Eli Friedman56d29372008-06-07 16:52:53 +0000118}
119
Sean Hunt9a555912010-05-30 07:21:58 +0000120void Decl::add(Kind k) {
Eli Friedman56d29372008-06-07 16:52:53 +0000121 switch (k) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000122 default: llvm_unreachable("Declaration not in DeclNodes.inc!");
Sean Hunt9a555912010-05-30 07:21:58 +0000123#define DECL(DERIVED, BASE) case DERIVED: ++n##DERIVED##s; break;
124#define ABSTRACT_DECL(DECL)
125#include "clang/AST/DeclNodes.inc"
Eli Friedman56d29372008-06-07 16:52:53 +0000126 }
127}
128
Anders Carlsson67e33202009-06-13 00:08:58 +0000129bool Decl::isTemplateParameterPack() const {
130 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
131 return TTP->isParameterPack();
Douglas Gregor10738d32010-12-23 23:51:58 +0000132 if (const NonTypeTemplateParmDecl *NTTP
Douglas Gregor61c4d282011-01-05 15:48:55 +0000133 = dyn_cast<NonTypeTemplateParmDecl>(this))
Douglas Gregor10738d32010-12-23 23:51:58 +0000134 return NTTP->isParameterPack();
Douglas Gregor61c4d282011-01-05 15:48:55 +0000135 if (const TemplateTemplateParmDecl *TTP
136 = dyn_cast<TemplateTemplateParmDecl>(this))
137 return TTP->isParameterPack();
Anders Carlsson67e33202009-06-13 00:08:58 +0000138 return false;
139}
140
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000141bool Decl::isParameterPack() const {
142 if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(this))
143 return Parm->isParameterPack();
144
145 return isTemplateParameterPack();
146}
147
Douglas Gregore53060f2009-06-25 22:08:12 +0000148bool Decl::isFunctionOrFunctionTemplate() const {
John McCall9488ea12009-11-17 05:59:44 +0000149 if (const UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(this))
Anders Carlsson58badb72009-06-26 05:26:50 +0000150 return UD->getTargetDecl()->isFunctionOrFunctionTemplate();
Mike Stump1eb44332009-09-09 15:08:12 +0000151
Douglas Gregore53060f2009-06-25 22:08:12 +0000152 return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this);
153}
154
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000155bool Decl::isTemplateDecl() const {
156 return isa<TemplateDecl>(this);
157}
158
Argyrios Kyrtzidisc8680f42011-09-28 02:45:33 +0000159const DeclContext *Decl::getParentFunctionOrMethod() const {
160 for (const DeclContext *DC = getDeclContext();
161 DC && !DC->isTranslationUnit() && !DC->isNamespace();
Douglas Gregor79c22782010-01-16 20:21:20 +0000162 DC = DC->getParent())
163 if (DC->isFunctionOrMethod())
Argyrios Kyrtzidisc8680f42011-09-28 02:45:33 +0000164 return DC;
Douglas Gregor79c22782010-01-16 20:21:20 +0000165
Argyrios Kyrtzidisc8680f42011-09-28 02:45:33 +0000166 return 0;
Douglas Gregor79c22782010-01-16 20:21:20 +0000167}
168
Douglas Gregor4c3e0ee2011-02-17 08:47:29 +0000169
Eli Friedman56d29372008-06-07 16:52:53 +0000170//===----------------------------------------------------------------------===//
Chris Lattner49f28ca2009-03-05 08:00:35 +0000171// PrettyStackTraceDecl Implementation
172//===----------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000173
Chris Lattner5f9e2722011-07-23 10:55:15 +0000174void PrettyStackTraceDecl::print(raw_ostream &OS) const {
Chris Lattner49f28ca2009-03-05 08:00:35 +0000175 SourceLocation TheLoc = Loc;
176 if (TheLoc.isInvalid() && TheDecl)
177 TheLoc = TheDecl->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Chris Lattner49f28ca2009-03-05 08:00:35 +0000179 if (TheLoc.isValid()) {
180 TheLoc.print(OS, SM);
181 OS << ": ";
182 }
183
184 OS << Message;
185
Daniel Dunbarc5236562009-11-21 09:05:59 +0000186 if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
Chris Lattner49f28ca2009-03-05 08:00:35 +0000187 OS << " '" << DN->getQualifiedNameAsString() << '\'';
188 OS << '\n';
189}
Mike Stump1eb44332009-09-09 15:08:12 +0000190
Chris Lattner49f28ca2009-03-05 08:00:35 +0000191//===----------------------------------------------------------------------===//
Eli Friedman56d29372008-06-07 16:52:53 +0000192// Decl Implementation
193//===----------------------------------------------------------------------===//
194
Douglas Gregorda2142f2011-02-19 18:51:44 +0000195// Out-of-line virtual method providing a home for Decl.
196Decl::~Decl() { }
Douglas Gregorf4a03cc2011-02-17 07:02:32 +0000197
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000198void Decl::setDeclContext(DeclContext *DC) {
Chris Lattneree219fd2009-03-29 06:06:59 +0000199 DeclCtx = DC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000200}
201
202void Decl::setLexicalDeclContext(DeclContext *DC) {
203 if (DC == getLexicalDeclContext())
204 return;
205
206 if (isInSemaDC()) {
Ted Kremenek94a39002009-12-01 00:07:10 +0000207 MultipleDC *MDC = new (getASTContext()) MultipleDC();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000208 MDC->SemanticDC = getDeclContext();
209 MDC->LexicalDC = DC;
Chris Lattneree219fd2009-03-29 06:06:59 +0000210 DeclCtx = MDC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000211 } else {
212 getMultipleDC()->LexicalDC = DC;
213 }
214}
215
John McCall9aeed322009-10-01 00:25:31 +0000216bool Decl::isInAnonymousNamespace() const {
217 const DeclContext *DC = getDeclContext();
218 do {
219 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
220 if (ND->isAnonymousNamespace())
221 return true;
222 } while ((DC = DC->getParent()));
223
224 return false;
225}
226
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000227TranslationUnitDecl *Decl::getTranslationUnitDecl() {
Argyrios Kyrtzidis9b346692009-06-30 02:34:53 +0000228 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
229 return TUD;
230
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000231 DeclContext *DC = getDeclContext();
232 assert(DC && "This decl is not contained in a translation unit!");
Mike Stump1eb44332009-09-09 15:08:12 +0000233
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000234 while (!DC->isTranslationUnit()) {
235 DC = DC->getParent();
236 assert(DC && "This decl is not contained in a translation unit!");
237 }
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000239 return cast<TranslationUnitDecl>(DC);
240}
241
242ASTContext &Decl::getASTContext() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000243 return getTranslationUnitDecl()->getASTContext();
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000244}
245
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +0000246ASTMutationListener *Decl::getASTMutationListener() const {
247 return getASTContext().getASTMutationListener();
248}
249
Douglas Gregorc070cc62010-06-17 23:14:26 +0000250bool Decl::isUsed(bool CheckUsedAttr) const {
Tanya Lattner12ead492010-02-17 02:17:21 +0000251 if (Used)
252 return true;
253
254 // Check for used attribute.
Douglas Gregorc070cc62010-06-17 23:14:26 +0000255 if (CheckUsedAttr && hasAttr<UsedAttr>())
Tanya Lattner12ead492010-02-17 02:17:21 +0000256 return true;
257
258 // Check redeclarations for used attribute.
259 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Douglas Gregorc070cc62010-06-17 23:14:26 +0000260 if ((CheckUsedAttr && I->hasAttr<UsedAttr>()) || I->Used)
Tanya Lattner12ead492010-02-17 02:17:21 +0000261 return true;
262 }
263
264 return false;
265}
266
Argyrios Kyrtzidis6b6b42a2011-04-19 19:51:10 +0000267bool Decl::isReferenced() const {
268 if (Referenced)
269 return true;
270
271 // Check redeclarations.
272 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
273 if (I->Referenced)
274 return true;
275
276 return false;
277}
278
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000279/// \brief Determine the availability of the given declaration based on
280/// the target platform.
281///
282/// When it returns an availability result other than \c AR_Available,
283/// if the \p Message parameter is non-NULL, it will be set to a
284/// string describing why the entity is unavailable.
285///
286/// FIXME: Make these strings localizable, since they end up in
287/// diagnostics.
288static AvailabilityResult CheckAvailability(ASTContext &Context,
289 const AvailabilityAttr *A,
290 std::string *Message) {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000291 StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000292 StringRef PrettyPlatformName
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000293 = AvailabilityAttr::getPrettyPlatformName(TargetPlatform);
294 if (PrettyPlatformName.empty())
295 PrettyPlatformName = TargetPlatform;
296
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000297 VersionTuple TargetMinVersion = Context.getTargetInfo().getPlatformMinVersion();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000298 if (TargetMinVersion.empty())
299 return AR_Available;
300
301 // Match the platform name.
302 if (A->getPlatform()->getName() != TargetPlatform)
303 return AR_Available;
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000304
305 std::string HintMessage;
306 if (!A->getMessage().empty()) {
307 HintMessage = " - ";
308 HintMessage += A->getMessage();
309 }
310
Douglas Gregorb53e4172011-03-26 03:35:55 +0000311 // Make sure that this declaration has not been marked 'unavailable'.
312 if (A->getUnavailable()) {
313 if (Message) {
314 Message->clear();
315 llvm::raw_string_ostream Out(*Message);
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000316 Out << "not available on " << PrettyPlatformName
317 << HintMessage;
Douglas Gregorb53e4172011-03-26 03:35:55 +0000318 }
319
320 return AR_Unavailable;
321 }
322
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000323 // Make sure that this declaration has already been introduced.
324 if (!A->getIntroduced().empty() &&
325 TargetMinVersion < A->getIntroduced()) {
326 if (Message) {
327 Message->clear();
328 llvm::raw_string_ostream Out(*Message);
329 Out << "introduced in " << PrettyPlatformName << ' '
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000330 << A->getIntroduced() << HintMessage;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000331 }
332
333 return AR_NotYetIntroduced;
334 }
335
336 // Make sure that this declaration hasn't been obsoleted.
337 if (!A->getObsoleted().empty() && TargetMinVersion >= A->getObsoleted()) {
338 if (Message) {
339 Message->clear();
340 llvm::raw_string_ostream Out(*Message);
341 Out << "obsoleted in " << PrettyPlatformName << ' '
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000342 << A->getObsoleted() << HintMessage;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000343 }
344
345 return AR_Unavailable;
346 }
347
348 // Make sure that this declaration hasn't been deprecated.
349 if (!A->getDeprecated().empty() && TargetMinVersion >= A->getDeprecated()) {
350 if (Message) {
351 Message->clear();
352 llvm::raw_string_ostream Out(*Message);
353 Out << "first deprecated in " << PrettyPlatformName << ' '
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000354 << A->getDeprecated() << HintMessage;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000355 }
356
357 return AR_Deprecated;
358 }
359
360 return AR_Available;
361}
362
363AvailabilityResult Decl::getAvailability(std::string *Message) const {
364 AvailabilityResult Result = AR_Available;
365 std::string ResultMessage;
366
367 for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
368 if (DeprecatedAttr *Deprecated = dyn_cast<DeprecatedAttr>(*A)) {
369 if (Result >= AR_Deprecated)
370 continue;
371
372 if (Message)
373 ResultMessage = Deprecated->getMessage();
374
375 Result = AR_Deprecated;
376 continue;
377 }
378
379 if (UnavailableAttr *Unavailable = dyn_cast<UnavailableAttr>(*A)) {
380 if (Message)
381 *Message = Unavailable->getMessage();
382 return AR_Unavailable;
383 }
384
385 if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
386 AvailabilityResult AR = CheckAvailability(getASTContext(), Availability,
387 Message);
388
389 if (AR == AR_Unavailable)
390 return AR_Unavailable;
391
392 if (AR > Result) {
393 Result = AR;
394 if (Message)
395 ResultMessage.swap(*Message);
396 }
397 continue;
398 }
399 }
400
401 if (Message)
402 Message->swap(ResultMessage);
403 return Result;
404}
405
406bool Decl::canBeWeakImported(bool &IsDefinition) const {
407 IsDefinition = false;
408 if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
409 if (!Var->hasExternalStorage() || Var->getInit()) {
410 IsDefinition = true;
411 return false;
412 }
413 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
414 if (FD->hasBody()) {
415 IsDefinition = true;
416 return false;
417 }
418 } else if (isa<ObjCPropertyDecl>(this) || isa<ObjCMethodDecl>(this))
419 return false;
420 else if (!(getASTContext().getLangOptions().ObjCNonFragileABI &&
421 isa<ObjCInterfaceDecl>(this)))
422 return false;
423
424 return true;
425}
426
427bool Decl::isWeakImported() const {
428 bool IsDefinition;
429 if (!canBeWeakImported(IsDefinition))
430 return false;
431
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000432 for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
433 if (isa<WeakImportAttr>(*A))
434 return true;
435
436 if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
437 if (CheckAvailability(getASTContext(), Availability, 0)
438 == AR_NotYetIntroduced)
439 return true;
440 }
441 }
442
443 return false;
444}
Tanya Lattner12ead492010-02-17 02:17:21 +0000445
Chris Lattner769dbdf2009-03-27 20:18:19 +0000446unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
447 switch (DeclKind) {
John McCall9488ea12009-11-17 05:59:44 +0000448 case Function:
449 case CXXMethod:
450 case CXXConstructor:
451 case CXXDestructor:
452 case CXXConversion:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000453 case EnumConstant:
454 case Var:
455 case ImplicitParam:
456 case ParmVar:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000457 case NonTypeTemplateParm:
458 case ObjCMethod:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000459 case ObjCProperty:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000460 return IDNS_Ordinary;
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000461 case Label:
462 return IDNS_Label;
Francois Pichet87c2e122010-11-21 06:08:52 +0000463 case IndirectField:
464 return IDNS_Ordinary | IDNS_Member;
465
John McCall0d6b1642010-04-23 18:46:30 +0000466 case ObjCCompatibleAlias:
467 case ObjCInterface:
468 return IDNS_Ordinary | IDNS_Type;
469
470 case Typedef:
Richard Smith162e1c12011-04-15 14:24:37 +0000471 case TypeAlias:
Richard Smith3e4c6c42011-05-05 21:57:07 +0000472 case TypeAliasTemplate:
John McCall0d6b1642010-04-23 18:46:30 +0000473 case UnresolvedUsingTypename:
474 case TemplateTypeParm:
475 return IDNS_Ordinary | IDNS_Type;
476
John McCall9488ea12009-11-17 05:59:44 +0000477 case UsingShadow:
478 return 0; // we'll actually overwrite this later
479
John McCall7ba107a2009-11-18 02:36:19 +0000480 case UnresolvedUsingValue:
John McCall7ba107a2009-11-18 02:36:19 +0000481 return IDNS_Ordinary | IDNS_Using;
John McCall9488ea12009-11-17 05:59:44 +0000482
483 case Using:
484 return IDNS_Using;
485
Chris Lattner769dbdf2009-03-27 20:18:19 +0000486 case ObjCProtocol:
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000487 return IDNS_ObjCProtocol;
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Chris Lattner769dbdf2009-03-27 20:18:19 +0000489 case Field:
490 case ObjCAtDefsField:
491 case ObjCIvar:
492 return IDNS_Member;
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Chris Lattner769dbdf2009-03-27 20:18:19 +0000494 case Record:
495 case CXXRecord:
496 case Enum:
John McCall0d6b1642010-04-23 18:46:30 +0000497 return IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000498
Chris Lattner769dbdf2009-03-27 20:18:19 +0000499 case Namespace:
John McCall0d6b1642010-04-23 18:46:30 +0000500 case NamespaceAlias:
501 return IDNS_Namespace;
502
Chris Lattner769dbdf2009-03-27 20:18:19 +0000503 case FunctionTemplate:
John McCall0d6b1642010-04-23 18:46:30 +0000504 return IDNS_Ordinary;
505
Chris Lattner769dbdf2009-03-27 20:18:19 +0000506 case ClassTemplate:
507 case TemplateTemplateParm:
John McCall0d6b1642010-04-23 18:46:30 +0000508 return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000509
Chris Lattner769dbdf2009-03-27 20:18:19 +0000510 // Never have names.
John McCall02cace72009-08-28 07:59:38 +0000511 case Friend:
John McCalldd4a3b02009-09-16 22:47:08 +0000512 case FriendTemplate:
Abramo Bagnara6206d532010-06-05 05:09:32 +0000513 case AccessSpec:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000514 case LinkageSpec:
515 case FileScopeAsm:
516 case StaticAssert:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000517 case ObjCPropertyImpl:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000518 case Block:
519 case TranslationUnit:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000520
Chris Lattner769dbdf2009-03-27 20:18:19 +0000521 case UsingDirective:
522 case ClassTemplateSpecialization:
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000523 case ClassTemplatePartialSpecialization:
Francois Pichetaf0f4d02011-08-14 03:52:19 +0000524 case ClassScopeFunctionSpecialization:
Douglas Gregorbd4187b2010-04-22 23:19:50 +0000525 case ObjCImplementation:
526 case ObjCCategory:
527 case ObjCCategoryImpl:
Douglas Gregor15de72c2011-12-02 23:23:56 +0000528 case Import:
Douglas Gregorbd4187b2010-04-22 23:19:50 +0000529 // Never looked up by name.
Chris Lattner769dbdf2009-03-27 20:18:19 +0000530 return 0;
531 }
John McCall9488ea12009-11-17 05:59:44 +0000532
533 return 0;
Eli Friedman56d29372008-06-07 16:52:53 +0000534}
535
Sean Huntcf807c42010-08-18 23:23:40 +0000536void Decl::setAttrs(const AttrVec &attrs) {
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000537 assert(!HasAttrs && "Decl already contains attrs.");
538
Sean Huntcf807c42010-08-18 23:23:40 +0000539 AttrVec &AttrBlank = getASTContext().getDeclAttrs(this);
540 assert(AttrBlank.empty() && "HasAttrs was wrong?");
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000541
542 AttrBlank = attrs;
543 HasAttrs = true;
544}
545
Sean Huntcf807c42010-08-18 23:23:40 +0000546void Decl::dropAttrs() {
Eli Friedman56d29372008-06-07 16:52:53 +0000547 if (!HasAttrs) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000548
Eli Friedman56d29372008-06-07 16:52:53 +0000549 HasAttrs = false;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000550 getASTContext().eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000551}
552
Sean Huntcf807c42010-08-18 23:23:40 +0000553const AttrVec &Decl::getAttrs() const {
554 assert(HasAttrs && "No attrs to get!");
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000555 return getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000556}
557
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000558void Decl::swapAttrs(Decl *RHS) {
Eli Friedman56d29372008-06-07 16:52:53 +0000559 bool HasLHSAttr = this->HasAttrs;
560 bool HasRHSAttr = RHS->HasAttrs;
Mike Stump1eb44332009-09-09 15:08:12 +0000561
Eli Friedman56d29372008-06-07 16:52:53 +0000562 // Usually, neither decl has attrs, nothing to do.
563 if (!HasLHSAttr && !HasRHSAttr) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000564
Eli Friedman56d29372008-06-07 16:52:53 +0000565 // If 'this' has no attrs, swap the other way.
566 if (!HasLHSAttr)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000567 return RHS->swapAttrs(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000568
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000569 ASTContext &Context = getASTContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000570
Eli Friedman56d29372008-06-07 16:52:53 +0000571 // Handle the case when both decls have attrs.
572 if (HasRHSAttr) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000573 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman56d29372008-06-07 16:52:53 +0000574 return;
575 }
Mike Stump1eb44332009-09-09 15:08:12 +0000576
Eli Friedman56d29372008-06-07 16:52:53 +0000577 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor68584ed2009-06-18 16:11:24 +0000578 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
579 Context.eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000580 this->HasAttrs = false;
581 RHS->HasAttrs = true;
582}
583
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000584Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000585 Decl::Kind DK = D->getDeclKind();
586 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000587#define DECL(NAME, BASE)
588#define DECL_CONTEXT(NAME) \
589 case Decl::NAME: \
590 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
591#define DECL_CONTEXT_BASE(NAME)
592#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000593 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000594#define DECL(NAME, BASE)
595#define DECL_CONTEXT_BASE(NAME) \
596 if (DK >= first##NAME && DK <= last##NAME) \
597 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
598#include "clang/AST/DeclNodes.inc"
David Blaikieb219cfc2011-09-23 05:06:16 +0000599 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000600 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000601}
602
603DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000604 Decl::Kind DK = D->getKind();
605 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000606#define DECL(NAME, BASE)
607#define DECL_CONTEXT(NAME) \
608 case Decl::NAME: \
609 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
610#define DECL_CONTEXT_BASE(NAME)
611#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000612 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000613#define DECL(NAME, BASE)
614#define DECL_CONTEXT_BASE(NAME) \
615 if (DK >= first##NAME && DK <= last##NAME) \
616 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
617#include "clang/AST/DeclNodes.inc"
David Blaikieb219cfc2011-09-23 05:06:16 +0000618 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000619 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000620}
621
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000622SourceLocation Decl::getBodyRBrace() const {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000623 // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
624 // FunctionDecl stores EndRangeLoc for this purpose.
625 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
626 const FunctionDecl *Definition;
627 if (FD->hasBody(Definition))
628 return Definition->getSourceRange().getEnd();
629 return SourceLocation();
630 }
631
Argyrios Kyrtzidis6717ef42010-07-07 11:31:27 +0000632 if (Stmt *Body = getBody())
633 return Body->getSourceRange().getEnd();
634
635 return SourceLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +0000636}
637
Anders Carlsson1329c272009-03-25 23:38:06 +0000638void Decl::CheckAccessDeclContext() const {
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000639#ifndef NDEBUG
John McCall46460a62010-01-20 21:53:11 +0000640 // Suppress this check if any of the following hold:
641 // 1. this is the translation unit (and thus has no parent)
642 // 2. this is a template parameter (and thus doesn't belong to its context)
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000643 // 3. this is a non-type template parameter
644 // 4. the context is not a record
645 // 5. it's invalid
646 // 6. it's a C++0x static_assert.
Anders Carlsson35eda442009-08-29 20:47:47 +0000647 if (isa<TranslationUnitDecl>(this) ||
Argyrios Kyrtzidis04aed0e2010-07-02 11:55:44 +0000648 isa<TemplateTypeParmDecl>(this) ||
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000649 isa<NonTypeTemplateParmDecl>(this) ||
Douglas Gregorfdd8ab12010-02-22 17:53:38 +0000650 !isa<CXXRecordDecl>(getDeclContext()) ||
Argyrios Kyrtzidis65b63ec2010-09-08 21:32:35 +0000651 isInvalidDecl() ||
652 isa<StaticAssertDecl>(this) ||
653 // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
654 // as DeclContext (?).
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000655 isa<ParmVarDecl>(this) ||
656 // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
657 // AS_none as access specifier.
Francois Pichetbc845322011-08-17 01:06:54 +0000658 isa<CXXRecordDecl>(this) ||
659 isa<ClassScopeFunctionSpecializationDecl>(this))
Anders Carlsson35eda442009-08-29 20:47:47 +0000660 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000661
662 assert(Access != AS_none &&
Anders Carlsson1329c272009-03-25 23:38:06 +0000663 "Access specifier is AS_none inside a record decl");
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000664#endif
Anders Carlsson1329c272009-03-25 23:38:06 +0000665}
666
John McCallaab9e312011-02-22 22:25:23 +0000667DeclContext *Decl::getNonClosureContext() {
John McCall4b9c2d22011-11-06 09:01:30 +0000668 return getDeclContext()->getNonClosureAncestor();
669}
670
671DeclContext *DeclContext::getNonClosureAncestor() {
672 DeclContext *DC = this;
John McCallaab9e312011-02-22 22:25:23 +0000673
674 // This is basically "while (DC->isClosure()) DC = DC->getParent();"
675 // except that it's significantly more efficient to cast to a known
676 // decl type and call getDeclContext() than to call getParent().
John McCall7b3f8532011-06-23 21:18:31 +0000677 while (isa<BlockDecl>(DC))
678 DC = cast<BlockDecl>(DC)->getDeclContext();
John McCallaab9e312011-02-22 22:25:23 +0000679
680 assert(!DC->isClosure());
681 return DC;
682}
Anders Carlsson1329c272009-03-25 23:38:06 +0000683
Eli Friedman56d29372008-06-07 16:52:53 +0000684//===----------------------------------------------------------------------===//
685// DeclContext Implementation
686//===----------------------------------------------------------------------===//
687
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000688bool DeclContext::classof(const Decl *D) {
689 switch (D->getKind()) {
Sean Hunt9a555912010-05-30 07:21:58 +0000690#define DECL(NAME, BASE)
691#define DECL_CONTEXT(NAME) case Decl::NAME:
692#define DECL_CONTEXT_BASE(NAME)
693#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000694 return true;
695 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000696#define DECL(NAME, BASE)
697#define DECL_CONTEXT_BASE(NAME) \
698 if (D->getKind() >= Decl::first##NAME && \
699 D->getKind() <= Decl::last##NAME) \
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000700 return true;
Sean Hunt9a555912010-05-30 07:21:58 +0000701#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000702 return false;
703 }
704}
705
Douglas Gregora2da7802010-07-25 18:38:02 +0000706DeclContext::~DeclContext() { }
Douglas Gregor44b43212008-12-11 16:49:14 +0000707
Douglas Gregore942bbe2009-09-10 16:57:35 +0000708/// \brief Find the parent context of this context that will be
709/// used for unqualified name lookup.
710///
711/// Generally, the parent lookup context is the semantic context. However, for
712/// a friend function the parent lookup context is the lexical context, which
713/// is the class in which the friend is declared.
714DeclContext *DeclContext::getLookupParent() {
715 // FIXME: Find a better way to identify friends
716 if (isa<FunctionDecl>(this))
Sebastian Redl7a126a42010-08-31 00:36:30 +0000717 if (getParent()->getRedeclContext()->isFileContext() &&
718 getLexicalParent()->getRedeclContext()->isRecord())
Douglas Gregore942bbe2009-09-10 16:57:35 +0000719 return getLexicalParent();
720
721 return getParent();
722}
723
Sebastian Redl410c4f22010-08-31 20:53:31 +0000724bool DeclContext::isInlineNamespace() const {
725 return isNamespace() &&
726 cast<NamespaceDecl>(this)->isInline();
727}
728
Douglas Gregorbc221632009-05-28 16:34:51 +0000729bool DeclContext::isDependentContext() const {
730 if (isFileContext())
731 return false;
732
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000733 if (isa<ClassTemplatePartialSpecializationDecl>(this))
734 return true;
735
Douglas Gregorbc221632009-05-28 16:34:51 +0000736 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
737 if (Record->getDescribedClassTemplate())
738 return true;
739
John McCall0c01d182010-03-24 05:22:00 +0000740 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregorbc221632009-05-28 16:34:51 +0000741 if (Function->getDescribedFunctionTemplate())
742 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000743
John McCall0c01d182010-03-24 05:22:00 +0000744 // Friend function declarations are dependent if their *lexical*
745 // context is dependent.
746 if (cast<Decl>(this)->getFriendObjectKind())
747 return getLexicalParent()->isDependentContext();
748 }
749
Douglas Gregorbc221632009-05-28 16:34:51 +0000750 return getParent() && getParent()->isDependentContext();
751}
752
Douglas Gregor074149e2009-01-05 19:45:36 +0000753bool DeclContext::isTransparentContext() const {
754 if (DeclKind == Decl::Enum)
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000755 return !cast<EnumDecl>(this)->isScoped();
Douglas Gregor074149e2009-01-05 19:45:36 +0000756 else if (DeclKind == Decl::LinkageSpec)
757 return true;
Douglas Gregor074149e2009-01-05 19:45:36 +0000758
759 return false;
760}
761
John McCallac65c622010-10-26 04:59:26 +0000762bool DeclContext::isExternCContext() const {
763 const DeclContext *DC = this;
764 while (DC->DeclKind != Decl::TranslationUnit) {
765 if (DC->DeclKind == Decl::LinkageSpec)
766 return cast<LinkageSpecDecl>(DC)->getLanguage()
767 == LinkageSpecDecl::lang_c;
768 DC = DC->getParent();
769 }
770 return false;
771}
772
Sebastian Redl7a126a42010-08-31 00:36:30 +0000773bool DeclContext::Encloses(const DeclContext *DC) const {
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000774 if (getPrimaryContext() != this)
775 return getPrimaryContext()->Encloses(DC);
Mike Stump1eb44332009-09-09 15:08:12 +0000776
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000777 for (; DC; DC = DC->getParent())
778 if (DC->getPrimaryContext() == this)
779 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000780 return false;
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000781}
782
Steve Naroff0701bbb2009-01-08 17:28:14 +0000783DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000784 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000785 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000786 case Decl::LinkageSpec:
Mike Stump1eb44332009-09-09 15:08:12 +0000787 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000788 // There is only one DeclContext for these entities.
789 return this;
790
791 case Decl::Namespace:
792 // The original namespace is our primary context.
793 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
794
Douglas Gregor44b43212008-12-11 16:49:14 +0000795 case Decl::ObjCMethod:
796 return this;
797
798 case Decl::ObjCInterface:
Douglas Gregor53df7a12011-12-15 18:03:09 +0000799 if (ObjCInterfaceDecl *Def = cast<ObjCInterfaceDecl>(this)->getDefinition())
800 return Def;
801
802 return this;
803
Steve Naroff0701bbb2009-01-08 17:28:14 +0000804 case Decl::ObjCProtocol:
Douglas Gregor1d784b22012-01-01 19:51:50 +0000805 if (ObjCProtocolDecl *Def = cast<ObjCProtocolDecl>(this)->getDefinition())
806 return Def;
807
808 return this;
Douglas Gregor53df7a12011-12-15 18:03:09 +0000809
Steve Naroff0701bbb2009-01-08 17:28:14 +0000810 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000811 return this;
812
Steve Naroff0701bbb2009-01-08 17:28:14 +0000813 case Decl::ObjCImplementation:
814 case Decl::ObjCCategoryImpl:
815 return this;
816
Douglas Gregor44b43212008-12-11 16:49:14 +0000817 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000818 if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000819 // If this is a tag type that has a definition or is currently
820 // being defined, that definition is our primary context.
John McCall3cb0ebd2010-03-10 03:28:59 +0000821 TagDecl *Tag = cast<TagDecl>(this);
822 assert(isa<TagType>(Tag->TypeForDecl) ||
823 isa<InjectedClassNameType>(Tag->TypeForDecl));
824
825 if (TagDecl *Def = Tag->getDefinition())
826 return Def;
827
828 if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) {
829 const TagType *TagTy = cast<TagType>(Tag->TypeForDecl);
830 if (TagTy->isBeingDefined())
831 // FIXME: is it necessarily being defined in the decl
832 // that owns the type?
833 return TagTy->getDecl();
834 }
835
836 return Tag;
Douglas Gregorcc636682009-02-17 23:15:12 +0000837 }
838
Sean Hunt9a555912010-05-30 07:21:58 +0000839 assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
Douglas Gregor44b43212008-12-11 16:49:14 +0000840 "Unknown DeclContext kind");
841 return this;
842 }
843}
844
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +0000845void
846DeclContext::collectAllContexts(llvm::SmallVectorImpl<DeclContext *> &Contexts){
847 Contexts.clear();
848
849 if (DeclKind != Decl::Namespace) {
850 Contexts.push_back(this);
851 return;
Douglas Gregor44b43212008-12-11 16:49:14 +0000852 }
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +0000853
854 NamespaceDecl *Self = static_cast<NamespaceDecl *>(this);
Douglas Gregoref96ee02012-01-14 16:38:05 +0000855 for (NamespaceDecl *N = Self->getMostRecentDecl(); N;
856 N = N->getPreviousDecl())
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +0000857 Contexts.push_back(N);
858
859 std::reverse(Contexts.begin(), Contexts.end());
Douglas Gregor44b43212008-12-11 16:49:14 +0000860}
861
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000862std::pair<Decl *, Decl *>
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +0000863DeclContext::BuildDeclChain(const SmallVectorImpl<Decl*> &Decls,
864 bool FieldsAlreadyLoaded) {
Douglas Gregor46cd2182012-01-06 16:59:53 +0000865 // Build up a chain of declarations via the Decl::NextInContextAndBits field.
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000866 Decl *FirstNewDecl = 0;
867 Decl *PrevDecl = 0;
868 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +0000869 if (FieldsAlreadyLoaded && isa<FieldDecl>(Decls[I]))
870 continue;
871
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000872 Decl *D = Decls[I];
873 if (PrevDecl)
Douglas Gregor46cd2182012-01-06 16:59:53 +0000874 PrevDecl->NextInContextAndBits.setPointer(D);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000875 else
876 FirstNewDecl = D;
877
878 PrevDecl = D;
879 }
880
881 return std::make_pair(FirstNewDecl, PrevDecl);
882}
883
Douglas Gregor2cf26342009-04-09 22:27:44 +0000884/// \brief Load the declarations within this lexical storage from an
885/// external source.
Mike Stump1eb44332009-09-09 15:08:12 +0000886void
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000887DeclContext::LoadLexicalDeclsFromExternalStorage() const {
888 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000889 assert(hasExternalLexicalStorage() && Source && "No external storage?");
890
Argyrios Kyrtzidis0dbbc042010-07-30 10:03:23 +0000891 // Notify that we have a DeclContext that is initializing.
892 ExternalASTSource::Deserializing ADeclContext(Source);
Douglas Gregor9fc18c92011-08-26 21:23:06 +0000893
Douglas Gregorba6ffaf2011-07-15 21:46:17 +0000894 // Load the external declarations, if any.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000895 SmallVector<Decl*, 64> Decls;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000896 ExternalLexicalStorage = false;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +0000897 switch (Source->FindExternalLexicalDecls(this, Decls)) {
898 case ELR_Success:
899 break;
900
901 case ELR_Failure:
902 case ELR_AlreadyLoaded:
903 return;
904 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000905
906 if (Decls.empty())
907 return;
908
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +0000909 // We may have already loaded just the fields of this record, in which case
910 // we need to ignore them.
911 bool FieldsAlreadyLoaded = false;
912 if (const RecordDecl *RD = dyn_cast<RecordDecl>(this))
913 FieldsAlreadyLoaded = RD->LoadedFieldsFromExternalStorage;
914
Douglas Gregor2cf26342009-04-09 22:27:44 +0000915 // Splice the newly-read declarations into the beginning of the list
916 // of declarations.
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000917 Decl *ExternalFirst, *ExternalLast;
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +0000918 llvm::tie(ExternalFirst, ExternalLast) = BuildDeclChain(Decls,
919 FieldsAlreadyLoaded);
Douglas Gregor46cd2182012-01-06 16:59:53 +0000920 ExternalLast->NextInContextAndBits.setPointer(FirstDecl);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000921 FirstDecl = ExternalFirst;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000922 if (!LastDecl)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000923 LastDecl = ExternalLast;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000924}
925
John McCall76bd1f32010-06-01 09:23:16 +0000926DeclContext::lookup_result
927ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
928 DeclarationName Name) {
929 ASTContext &Context = DC->getParentASTContext();
930 StoredDeclsMap *Map;
931 if (!(Map = DC->LookupPtr))
932 Map = DC->CreateStoredDeclsMap(Context);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000933
John McCall76bd1f32010-06-01 09:23:16 +0000934 StoredDeclsList &List = (*Map)[Name];
935 assert(List.isNull());
936 (void) List;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000937
John McCall76bd1f32010-06-01 09:23:16 +0000938 return DeclContext::lookup_result();
939}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000940
John McCall76bd1f32010-06-01 09:23:16 +0000941DeclContext::lookup_result
942ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +0000943 DeclarationName Name,
Argyrios Kyrtzidis45df9c62011-09-09 06:44:14 +0000944 ArrayRef<NamedDecl*> Decls) {
John McCall76bd1f32010-06-01 09:23:16 +0000945 ASTContext &Context = DC->getParentASTContext();;
946
947 StoredDeclsMap *Map;
948 if (!(Map = DC->LookupPtr))
949 Map = DC->CreateStoredDeclsMap(Context);
950
951 StoredDeclsList &List = (*Map)[Name];
Argyrios Kyrtzidis45df9c62011-09-09 06:44:14 +0000952 for (ArrayRef<NamedDecl*>::iterator
953 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
John McCall76bd1f32010-06-01 09:23:16 +0000954 if (List.isNull())
Argyrios Kyrtzidis45df9c62011-09-09 06:44:14 +0000955 List.setOnlyValue(*I);
John McCall76bd1f32010-06-01 09:23:16 +0000956 else
Argyrios Kyrtzidis45df9c62011-09-09 06:44:14 +0000957 List.AddSubsequentDecl(*I);
John McCall76bd1f32010-06-01 09:23:16 +0000958 }
959
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000960 return List.getLookupResult();
John McCall76bd1f32010-06-01 09:23:16 +0000961}
962
Sebastian Redl681d7232010-07-27 00:17:23 +0000963DeclContext::decl_iterator DeclContext::noload_decls_begin() const {
964 return decl_iterator(FirstDecl);
965}
966
967DeclContext::decl_iterator DeclContext::noload_decls_end() const {
968 return decl_iterator();
969}
970
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000971DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000972 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000973 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000974
Mike Stump1eb44332009-09-09 15:08:12 +0000975 return decl_iterator(FirstDecl);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000976}
977
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000978DeclContext::decl_iterator DeclContext::decls_end() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000979 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000980 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000981
Mike Stump1eb44332009-09-09 15:08:12 +0000982 return decl_iterator();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000983}
984
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000985bool DeclContext::decls_empty() const {
Douglas Gregor8038d512009-04-10 17:25:41 +0000986 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000987 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor8038d512009-04-10 17:25:41 +0000988
989 return !FirstDecl;
990}
991
John McCall9f54ad42009-12-10 09:41:52 +0000992void DeclContext::removeDecl(Decl *D) {
993 assert(D->getLexicalDeclContext() == this &&
994 "decl being removed from non-lexical context");
Douglas Gregor46cd2182012-01-06 16:59:53 +0000995 assert((D->NextInContextAndBits.getPointer() || D == LastDecl) &&
John McCall9f54ad42009-12-10 09:41:52 +0000996 "decl is not in decls list");
997
998 // Remove D from the decl chain. This is O(n) but hopefully rare.
999 if (D == FirstDecl) {
1000 if (D == LastDecl)
1001 FirstDecl = LastDecl = 0;
1002 else
Douglas Gregor46cd2182012-01-06 16:59:53 +00001003 FirstDecl = D->NextInContextAndBits.getPointer();
John McCall9f54ad42009-12-10 09:41:52 +00001004 } else {
Douglas Gregor46cd2182012-01-06 16:59:53 +00001005 for (Decl *I = FirstDecl; true; I = I->NextInContextAndBits.getPointer()) {
John McCall9f54ad42009-12-10 09:41:52 +00001006 assert(I && "decl not found in linked list");
Douglas Gregor46cd2182012-01-06 16:59:53 +00001007 if (I->NextInContextAndBits.getPointer() == D) {
1008 I->NextInContextAndBits.setPointer(D->NextInContextAndBits.getPointer());
John McCall9f54ad42009-12-10 09:41:52 +00001009 if (D == LastDecl) LastDecl = I;
1010 break;
1011 }
1012 }
1013 }
1014
1015 // Mark that D is no longer in the decl chain.
Douglas Gregor46cd2182012-01-06 16:59:53 +00001016 D->NextInContextAndBits.setPointer(0);
John McCall9f54ad42009-12-10 09:41:52 +00001017
1018 // Remove D from the lookup table if necessary.
1019 if (isa<NamedDecl>(D)) {
1020 NamedDecl *ND = cast<NamedDecl>(D);
1021
Axel Naumann02368d02011-08-26 14:06:12 +00001022 // Remove only decls that have a name
1023 if (!ND->getDeclName()) return;
1024
John McCall0c01d182010-03-24 05:22:00 +00001025 StoredDeclsMap *Map = getPrimaryContext()->LookupPtr;
1026 if (!Map) return;
John McCall9f54ad42009-12-10 09:41:52 +00001027
John McCall9f54ad42009-12-10 09:41:52 +00001028 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
1029 assert(Pos != Map->end() && "no lookup entry for decl");
Axel Naumannd9d137e2011-11-08 18:21:06 +00001030 if (Pos->second.getAsVector() || Pos->second.getAsDecl() == ND)
1031 Pos->second.remove(ND);
John McCall9f54ad42009-12-10 09:41:52 +00001032 }
1033}
1034
John McCall3f9a8a62009-08-11 06:59:38 +00001035void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner7f0be132009-02-20 00:56:18 +00001036 assert(D->getLexicalDeclContext() == this &&
1037 "Decl inserted into wrong lexical context");
Mike Stump1eb44332009-09-09 15:08:12 +00001038 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001039 "Decl already inserted into a DeclContext");
1040
1041 if (FirstDecl) {
Douglas Gregor46cd2182012-01-06 16:59:53 +00001042 LastDecl->NextInContextAndBits.setPointer(D);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001043 LastDecl = D;
1044 } else {
1045 FirstDecl = LastDecl = D;
1046 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +00001047
1048 // Notify a C++ record declaration that we've added a member, so it can
1049 // update it's class-specific state.
1050 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
1051 Record->addedMember(D);
Douglas Gregore6649772011-12-03 00:30:27 +00001052
1053 // If this is a newly-created (not de-serialized) import declaration, wire
1054 // it in to the list of local import declarations.
1055 if (!D->isFromASTFile()) {
1056 if (ImportDecl *Import = dyn_cast<ImportDecl>(D))
1057 D->getASTContext().addedLocalImportDecl(Import);
1058 }
John McCall3f9a8a62009-08-11 06:59:38 +00001059}
1060
1061void DeclContext::addDecl(Decl *D) {
1062 addHiddenDecl(D);
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001063
1064 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001065 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor44b43212008-12-11 16:49:14 +00001066}
1067
Sean Callanan9faf8102011-10-21 02:57:43 +00001068void DeclContext::addDeclInternal(Decl *D) {
1069 addHiddenDecl(D);
1070
1071 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1072 ND->getDeclContext()->makeDeclVisibleInContextInternal(ND);
1073}
1074
Douglas Gregor074149e2009-01-05 19:45:36 +00001075/// buildLookup - Build the lookup data structure with all of the
1076/// declarations in DCtx (and any other contexts linked to it or
1077/// transparent contexts nested within it).
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001078void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001079 llvm::SmallVector<DeclContext *, 2> Contexts;
1080 DCtx->collectAllContexts(Contexts);
1081 for (unsigned I = 0, N = Contexts.size(); I != N; ++I) {
1082 for (decl_iterator D = Contexts[I]->decls_begin(),
1083 DEnd = Contexts[I]->decls_end();
Douglas Gregor4f3b8f82009-01-06 07:17:58 +00001084 D != DEnd; ++D) {
John McCall3f9a8a62009-08-11 06:59:38 +00001085 // Insert this declaration into the lookup structure, but only
1086 // if it's semantically in its decl context. During non-lazy
1087 // lookup building, this is implicitly enforced by addDecl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001088 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001089 if (D->getDeclContext() == Contexts[I])
Sean Callanan9faf8102011-10-21 02:57:43 +00001090 makeDeclVisibleInContextImpl(ND, false);
Douglas Gregor074149e2009-01-05 19:45:36 +00001091
Sebastian Redl410c4f22010-08-31 20:53:31 +00001092 // If this declaration is itself a transparent declaration context or
1093 // inline namespace, add its members (recursively).
Douglas Gregor074149e2009-01-05 19:45:36 +00001094 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
Sebastian Redl410c4f22010-08-31 20:53:31 +00001095 if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001096 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregor074149e2009-01-05 19:45:36 +00001097 }
1098 }
1099}
1100
Mike Stump1eb44332009-09-09 15:08:12 +00001101DeclContext::lookup_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001102DeclContext::lookup(DeclarationName Name) {
Steve Naroff0701bbb2009-01-08 17:28:14 +00001103 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +00001104 if (PrimaryContext != this)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001105 return PrimaryContext->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +00001106
John McCall76bd1f32010-06-01 09:23:16 +00001107 if (hasExternalVisibleStorage()) {
1108 // Check to see if we've already cached the lookup results.
1109 if (LookupPtr) {
1110 StoredDeclsMap::iterator I = LookupPtr->find(Name);
1111 if (I != LookupPtr->end())
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001112 return I->second.getLookupResult();
John McCall76bd1f32010-06-01 09:23:16 +00001113 }
1114
1115 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1116 return Source->FindExternalVisibleDeclsByName(this, Name);
1117 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001118
Douglas Gregor3fc749d2008-12-23 00:26:44 +00001119 /// If there is no lookup data structure, build one now by walking
Douglas Gregor44b43212008-12-11 16:49:14 +00001120 /// all of the linked DeclContexts (in declaration order!) and
1121 /// inserting their values.
Douglas Gregorc36c5402009-04-09 17:29:08 +00001122 if (!LookupPtr) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001123 buildLookup(this);
Douglas Gregor44b43212008-12-11 16:49:14 +00001124
Douglas Gregorc36c5402009-04-09 17:29:08 +00001125 if (!LookupPtr)
Douglas Gregora5fdd9c2010-05-11 06:18:17 +00001126 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Douglas Gregorc36c5402009-04-09 17:29:08 +00001127 }
Douglas Gregor44b43212008-12-11 16:49:14 +00001128
John McCall0c01d182010-03-24 05:22:00 +00001129 StoredDeclsMap::iterator Pos = LookupPtr->find(Name);
1130 if (Pos == LookupPtr->end())
Douglas Gregora5fdd9c2010-05-11 06:18:17 +00001131 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001132 return Pos->second.getLookupResult();
Douglas Gregor44b43212008-12-11 16:49:14 +00001133}
1134
Mike Stump1eb44332009-09-09 15:08:12 +00001135DeclContext::lookup_const_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001136DeclContext::lookup(DeclarationName Name) const {
1137 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +00001138}
1139
Douglas Gregorb75a3452011-10-15 00:10:27 +00001140void DeclContext::localUncachedLookup(DeclarationName Name,
1141 llvm::SmallVectorImpl<NamedDecl *> &Results) {
1142 Results.clear();
1143
1144 // If there's no external storage, just perform a normal lookup and copy
1145 // the results.
1146 if (!hasExternalVisibleStorage() && !hasExternalLexicalStorage()) {
1147 lookup_result LookupResults = lookup(Name);
1148 Results.insert(Results.end(), LookupResults.first, LookupResults.second);
1149 return;
1150 }
1151
1152 // If we have a lookup table, check there first. Maybe we'll get lucky.
1153 if (LookupPtr) {
1154 StoredDeclsMap::iterator Pos = LookupPtr->find(Name);
1155 if (Pos != LookupPtr->end()) {
1156 Results.insert(Results.end(),
1157 Pos->second.getLookupResult().first,
1158 Pos->second.getLookupResult().second);
1159 return;
1160 }
1161 }
1162
1163 // Slow case: grovel through the declarations in our chain looking for
1164 // matches.
1165 for (Decl *D = FirstDecl; D; D = D->getNextDeclInContext()) {
1166 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1167 if (ND->getDeclName() == Name)
1168 Results.push_back(ND);
1169 }
1170}
1171
Sebastian Redl7a126a42010-08-31 00:36:30 +00001172DeclContext *DeclContext::getRedeclContext() {
Chris Lattner0cf2b192009-03-27 19:19:59 +00001173 DeclContext *Ctx = this;
Sebastian Redl410c4f22010-08-31 20:53:31 +00001174 // Skip through transparent contexts.
1175 while (Ctx->isTransparentContext())
Douglas Gregorce356072009-01-06 23:51:29 +00001176 Ctx = Ctx->getParent();
1177 return Ctx;
1178}
1179
Douglas Gregor88b70942009-02-25 22:02:03 +00001180DeclContext *DeclContext::getEnclosingNamespaceContext() {
1181 DeclContext *Ctx = this;
1182 // Skip through non-namespace, non-translation-unit contexts.
Sebastian Redl51a8a372010-08-31 00:36:23 +00001183 while (!Ctx->isFileContext())
Douglas Gregor88b70942009-02-25 22:02:03 +00001184 Ctx = Ctx->getParent();
1185 return Ctx->getPrimaryContext();
1186}
1187
Sebastian Redl7a126a42010-08-31 00:36:30 +00001188bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
1189 // For non-file contexts, this is equivalent to Equals.
1190 if (!isFileContext())
1191 return O->Equals(this);
1192
1193 do {
1194 if (O->Equals(this))
1195 return true;
1196
1197 const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O);
1198 if (!NS || !NS->isInline())
1199 break;
1200 O = NS->getParent();
1201 } while (O);
1202
1203 return false;
1204}
1205
Sean Callanan9faf8102011-10-21 02:57:43 +00001206void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable)
1207{
1208 makeDeclVisibleInContextWithFlags(D, false, Recoverable);
1209}
1210
1211void DeclContext::makeDeclVisibleInContextInternal(NamedDecl *D, bool Recoverable)
1212{
1213 makeDeclVisibleInContextWithFlags(D, true, Recoverable);
1214}
1215
1216void DeclContext::makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal, bool Recoverable) {
Douglas Gregorcc636682009-02-17 23:15:12 +00001217 // FIXME: This feels like a hack. Should DeclarationName support
1218 // template-ids, or is there a better way to keep specializations
1219 // from being visible?
Douglas Gregor9a299e02011-03-04 17:52:15 +00001220 if (isa<ClassTemplateSpecializationDecl>(D) || D->isTemplateParameter())
Douglas Gregorcc636682009-02-17 23:15:12 +00001221 return;
Eli Friedman6bc20132009-12-08 05:40:03 +00001222 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1223 if (FD->isFunctionTemplateSpecialization())
1224 return;
Douglas Gregorcc636682009-02-17 23:15:12 +00001225
Steve Naroff0701bbb2009-01-08 17:28:14 +00001226 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +00001227 if (PrimaryContext != this) {
Sean Callanan9faf8102011-10-21 02:57:43 +00001228 PrimaryContext->makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
Douglas Gregor44b43212008-12-11 16:49:14 +00001229 return;
1230 }
1231
1232 // If we already have a lookup data structure, perform the insertion
Argyrios Kyrtzidis5586b012010-07-04 21:44:25 +00001233 // into it. If we haven't deserialized externally stored decls, deserialize
1234 // them so we can add the decl. Otherwise, be lazy and don't build that
1235 // structure until someone asks for it.
1236 if (LookupPtr || !Recoverable || hasExternalVisibleStorage())
Sean Callanan9faf8102011-10-21 02:57:43 +00001237 makeDeclVisibleInContextImpl(D, Internal);
Douglas Gregor074149e2009-01-05 19:45:36 +00001238
Sebastian Redl410c4f22010-08-31 20:53:31 +00001239 // If we are a transparent context or inline namespace, insert into our
1240 // parent context, too. This operation is recursive.
1241 if (isTransparentContext() || isInlineNamespace())
Sean Callanan9faf8102011-10-21 02:57:43 +00001242 getParent()->makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00001243
1244 Decl *DCAsDecl = cast<Decl>(this);
1245 // Notify that a decl was made visible unless it's a Tag being defined.
1246 if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined()))
1247 if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
1248 L->AddedVisibleDecl(this, D);
Douglas Gregor44b43212008-12-11 16:49:14 +00001249}
1250
Sean Callanan9faf8102011-10-21 02:57:43 +00001251void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal) {
Douglas Gregor074149e2009-01-05 19:45:36 +00001252 // Skip unnamed declarations.
1253 if (!D->getDeclName())
1254 return;
1255
Douglas Gregor5cb0ef42011-05-06 23:32:38 +00001256 // Skip entities that can't be found by name lookup into a particular
1257 // context.
1258 if ((D->getIdentifierNamespace() == 0 && !isa<UsingDirectiveDecl>(D)) ||
1259 D->isTemplateParameter())
Douglas Gregorcc636682009-02-17 23:15:12 +00001260 return;
1261
Argyrios Kyrtzidis5586b012010-07-04 21:44:25 +00001262 ASTContext *C = 0;
1263 if (!LookupPtr) {
1264 C = &getParentASTContext();
1265 CreateStoredDeclsMap(*C);
1266 }
1267
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001268 // If there is an external AST source, load any declarations it knows about
1269 // with this declaration's name.
1270 // If the lookup table contains an entry about this name it means that we
1271 // have already checked the external source.
Sean Callanan9faf8102011-10-21 02:57:43 +00001272 if (!Internal)
1273 if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
1274 if (hasExternalVisibleStorage() &&
1275 LookupPtr->find(D->getDeclName()) == LookupPtr->end())
1276 Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001277
Douglas Gregor44b43212008-12-11 16:49:14 +00001278 // Insert this declaration into the map.
John McCall0c01d182010-03-24 05:22:00 +00001279 StoredDeclsList &DeclNameEntries = (*LookupPtr)[D->getDeclName()];
Chris Lattner67762a32009-02-20 01:44:05 +00001280 if (DeclNameEntries.isNull()) {
1281 DeclNameEntries.setOnlyValue(D);
Chris Lattnerbd6c8002009-02-19 07:00:44 +00001282 return;
Douglas Gregor44b43212008-12-11 16:49:14 +00001283 }
Chris Lattner91942502009-02-20 00:55:03 +00001284
Chris Lattnerbdc3d002009-02-20 01:10:07 +00001285 // If it is possible that this is a redeclaration, check to see if there is
1286 // already a decl for which declarationReplaces returns true. If there is
1287 // one, just replace it and return.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001288 if (DeclNameEntries.HandleRedeclaration(D))
Chris Lattner67762a32009-02-20 01:44:05 +00001289 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001290
Chris Lattnerbd6c8002009-02-19 07:00:44 +00001291 // Put this declaration into the appropriate slot.
Chris Lattner67762a32009-02-20 01:44:05 +00001292 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor44b43212008-12-11 16:49:14 +00001293}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001294
1295/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
1296/// this context.
Mike Stump1eb44332009-09-09 15:08:12 +00001297DeclContext::udir_iterator_range
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001298DeclContext::getUsingDirectives() const {
1299 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001300 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
1301 reinterpret_cast<udir_iterator>(Result.second));
1302}
Douglas Gregor2cf26342009-04-09 22:27:44 +00001303
Ted Kremenek3478eb62010-02-11 07:12:28 +00001304//===----------------------------------------------------------------------===//
1305// Creation and Destruction of StoredDeclsMaps. //
1306//===----------------------------------------------------------------------===//
1307
John McCall0c01d182010-03-24 05:22:00 +00001308StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
1309 assert(!LookupPtr && "context already has a decls map");
1310 assert(getPrimaryContext() == this &&
1311 "creating decls map on non-primary context");
1312
1313 StoredDeclsMap *M;
1314 bool Dependent = isDependentContext();
1315 if (Dependent)
1316 M = new DependentStoredDeclsMap();
1317 else
1318 M = new StoredDeclsMap();
1319 M->Previous = C.LastSDM;
1320 C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
1321 LookupPtr = M;
Ted Kremenek3478eb62010-02-11 07:12:28 +00001322 return M;
1323}
1324
1325void ASTContext::ReleaseDeclContextMaps() {
John McCall0c01d182010-03-24 05:22:00 +00001326 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1327 // pointer because the subclass doesn't add anything that needs to
1328 // be deleted.
John McCall0c01d182010-03-24 05:22:00 +00001329 StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1330}
1331
1332void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1333 while (Map) {
1334 // Advance the iteration before we invalidate memory.
1335 llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1336
1337 if (Dependent)
1338 delete static_cast<DependentStoredDeclsMap*>(Map);
1339 else
1340 delete Map;
1341
1342 Map = Next.getPointer();
1343 Dependent = Next.getInt();
1344 }
1345}
1346
John McCall0c01d182010-03-24 05:22:00 +00001347DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1348 DeclContext *Parent,
1349 const PartialDiagnostic &PDiag) {
1350 assert(Parent->isDependentContext()
1351 && "cannot iterate dependent diagnostics of non-dependent context");
1352 Parent = Parent->getPrimaryContext();
1353 if (!Parent->LookupPtr)
1354 Parent->CreateStoredDeclsMap(C);
1355
1356 DependentStoredDeclsMap *Map
1357 = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr);
1358
Douglas Gregorb8365182010-03-29 23:56:53 +00001359 // Allocate the copy of the PartialDiagnostic via the ASTContext's
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00001360 // BumpPtrAllocator, rather than the ASTContext itself.
Douglas Gregorb8365182010-03-29 23:56:53 +00001361 PartialDiagnostic::Storage *DiagStorage = 0;
1362 if (PDiag.hasStorage())
1363 DiagStorage = new (C) PartialDiagnostic::Storage;
1364
1365 DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
John McCall0c01d182010-03-24 05:22:00 +00001366
1367 // TODO: Maybe we shouldn't reverse the order during insertion.
1368 DD->NextDiagnostic = Map->FirstDiagnostic;
1369 Map->FirstDiagnostic = DD;
1370
1371 return DD;
Ted Kremenek3478eb62010-02-11 07:12:28 +00001372}