blob: da54a44784e235ef4be2e32a4cfa8128b8d2a5f3 [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) {
Sean Hunt9a555912010-05-30 07:21:58 +0000122#define DECL(DERIVED, BASE) case DERIVED: ++n##DERIVED##s; break;
123#define ABSTRACT_DECL(DECL)
124#include "clang/AST/DeclNodes.inc"
Eli Friedman56d29372008-06-07 16:52:53 +0000125 }
126}
127
Anders Carlsson67e33202009-06-13 00:08:58 +0000128bool Decl::isTemplateParameterPack() const {
129 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
130 return TTP->isParameterPack();
Douglas Gregor10738d32010-12-23 23:51:58 +0000131 if (const NonTypeTemplateParmDecl *NTTP
Douglas Gregor61c4d282011-01-05 15:48:55 +0000132 = dyn_cast<NonTypeTemplateParmDecl>(this))
Douglas Gregor10738d32010-12-23 23:51:58 +0000133 return NTTP->isParameterPack();
Douglas Gregor61c4d282011-01-05 15:48:55 +0000134 if (const TemplateTemplateParmDecl *TTP
135 = dyn_cast<TemplateTemplateParmDecl>(this))
136 return TTP->isParameterPack();
Anders Carlsson67e33202009-06-13 00:08:58 +0000137 return false;
138}
139
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000140bool Decl::isParameterPack() const {
141 if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(this))
142 return Parm->isParameterPack();
143
144 return isTemplateParameterPack();
145}
146
Douglas Gregore53060f2009-06-25 22:08:12 +0000147bool Decl::isFunctionOrFunctionTemplate() const {
John McCall9488ea12009-11-17 05:59:44 +0000148 if (const UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(this))
Anders Carlsson58badb72009-06-26 05:26:50 +0000149 return UD->getTargetDecl()->isFunctionOrFunctionTemplate();
Mike Stump1eb44332009-09-09 15:08:12 +0000150
Douglas Gregore53060f2009-06-25 22:08:12 +0000151 return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this);
152}
153
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000154bool Decl::isTemplateDecl() const {
155 return isa<TemplateDecl>(this);
156}
157
Argyrios Kyrtzidisc8680f42011-09-28 02:45:33 +0000158const DeclContext *Decl::getParentFunctionOrMethod() const {
159 for (const DeclContext *DC = getDeclContext();
160 DC && !DC->isTranslationUnit() && !DC->isNamespace();
Douglas Gregor79c22782010-01-16 20:21:20 +0000161 DC = DC->getParent())
162 if (DC->isFunctionOrMethod())
Argyrios Kyrtzidisc8680f42011-09-28 02:45:33 +0000163 return DC;
Douglas Gregor79c22782010-01-16 20:21:20 +0000164
Argyrios Kyrtzidisc8680f42011-09-28 02:45:33 +0000165 return 0;
Douglas Gregor79c22782010-01-16 20:21:20 +0000166}
167
Douglas Gregor4c3e0ee2011-02-17 08:47:29 +0000168
Eli Friedman56d29372008-06-07 16:52:53 +0000169//===----------------------------------------------------------------------===//
Chris Lattner49f28ca2009-03-05 08:00:35 +0000170// PrettyStackTraceDecl Implementation
171//===----------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Chris Lattner5f9e2722011-07-23 10:55:15 +0000173void PrettyStackTraceDecl::print(raw_ostream &OS) const {
Chris Lattner49f28ca2009-03-05 08:00:35 +0000174 SourceLocation TheLoc = Loc;
175 if (TheLoc.isInvalid() && TheDecl)
176 TheLoc = TheDecl->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000177
Chris Lattner49f28ca2009-03-05 08:00:35 +0000178 if (TheLoc.isValid()) {
179 TheLoc.print(OS, SM);
180 OS << ": ";
181 }
182
183 OS << Message;
184
Daniel Dunbarc5236562009-11-21 09:05:59 +0000185 if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
Chris Lattner49f28ca2009-03-05 08:00:35 +0000186 OS << " '" << DN->getQualifiedNameAsString() << '\'';
187 OS << '\n';
188}
Mike Stump1eb44332009-09-09 15:08:12 +0000189
Chris Lattner49f28ca2009-03-05 08:00:35 +0000190//===----------------------------------------------------------------------===//
Eli Friedman56d29372008-06-07 16:52:53 +0000191// Decl Implementation
192//===----------------------------------------------------------------------===//
193
Douglas Gregorda2142f2011-02-19 18:51:44 +0000194// Out-of-line virtual method providing a home for Decl.
195Decl::~Decl() { }
Douglas Gregorf4a03cc2011-02-17 07:02:32 +0000196
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000197void Decl::setDeclContext(DeclContext *DC) {
Chris Lattneree219fd2009-03-29 06:06:59 +0000198 DeclCtx = DC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000199}
200
201void Decl::setLexicalDeclContext(DeclContext *DC) {
202 if (DC == getLexicalDeclContext())
203 return;
204
205 if (isInSemaDC()) {
Ted Kremenek94a39002009-12-01 00:07:10 +0000206 MultipleDC *MDC = new (getASTContext()) MultipleDC();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000207 MDC->SemanticDC = getDeclContext();
208 MDC->LexicalDC = DC;
Chris Lattneree219fd2009-03-29 06:06:59 +0000209 DeclCtx = MDC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000210 } else {
211 getMultipleDC()->LexicalDC = DC;
212 }
213}
214
John McCall9aeed322009-10-01 00:25:31 +0000215bool Decl::isInAnonymousNamespace() const {
216 const DeclContext *DC = getDeclContext();
217 do {
218 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
219 if (ND->isAnonymousNamespace())
220 return true;
221 } while ((DC = DC->getParent()));
222
223 return false;
224}
225
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000226TranslationUnitDecl *Decl::getTranslationUnitDecl() {
Argyrios Kyrtzidis9b346692009-06-30 02:34:53 +0000227 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
228 return TUD;
229
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000230 DeclContext *DC = getDeclContext();
231 assert(DC && "This decl is not contained in a translation unit!");
Mike Stump1eb44332009-09-09 15:08:12 +0000232
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000233 while (!DC->isTranslationUnit()) {
234 DC = DC->getParent();
235 assert(DC && "This decl is not contained in a translation unit!");
236 }
Mike Stump1eb44332009-09-09 15:08:12 +0000237
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000238 return cast<TranslationUnitDecl>(DC);
239}
240
241ASTContext &Decl::getASTContext() const {
Mike Stump1eb44332009-09-09 15:08:12 +0000242 return getTranslationUnitDecl()->getASTContext();
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000243}
244
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +0000245ASTMutationListener *Decl::getASTMutationListener() const {
246 return getASTContext().getASTMutationListener();
247}
248
Douglas Gregorc070cc62010-06-17 23:14:26 +0000249bool Decl::isUsed(bool CheckUsedAttr) const {
Tanya Lattner12ead492010-02-17 02:17:21 +0000250 if (Used)
251 return true;
252
253 // Check for used attribute.
Douglas Gregorc070cc62010-06-17 23:14:26 +0000254 if (CheckUsedAttr && hasAttr<UsedAttr>())
Tanya Lattner12ead492010-02-17 02:17:21 +0000255 return true;
256
257 // Check redeclarations for used attribute.
258 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Douglas Gregorc070cc62010-06-17 23:14:26 +0000259 if ((CheckUsedAttr && I->hasAttr<UsedAttr>()) || I->Used)
Tanya Lattner12ead492010-02-17 02:17:21 +0000260 return true;
261 }
262
263 return false;
264}
265
Argyrios Kyrtzidis6b6b42a2011-04-19 19:51:10 +0000266bool Decl::isReferenced() const {
267 if (Referenced)
268 return true;
269
270 // Check redeclarations.
271 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
272 if (I->Referenced)
273 return true;
274
275 return false;
276}
277
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000278/// \brief Determine the availability of the given declaration based on
279/// the target platform.
280///
281/// When it returns an availability result other than \c AR_Available,
282/// if the \p Message parameter is non-NULL, it will be set to a
283/// string describing why the entity is unavailable.
284///
285/// FIXME: Make these strings localizable, since they end up in
286/// diagnostics.
287static AvailabilityResult CheckAvailability(ASTContext &Context,
288 const AvailabilityAttr *A,
289 std::string *Message) {
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000290 StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000291 StringRef PrettyPlatformName
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000292 = AvailabilityAttr::getPrettyPlatformName(TargetPlatform);
293 if (PrettyPlatformName.empty())
294 PrettyPlatformName = TargetPlatform;
295
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000296 VersionTuple TargetMinVersion = Context.getTargetInfo().getPlatformMinVersion();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000297 if (TargetMinVersion.empty())
298 return AR_Available;
299
300 // Match the platform name.
301 if (A->getPlatform()->getName() != TargetPlatform)
302 return AR_Available;
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000303
304 std::string HintMessage;
305 if (!A->getMessage().empty()) {
306 HintMessage = " - ";
307 HintMessage += A->getMessage();
308 }
309
Douglas Gregorb53e4172011-03-26 03:35:55 +0000310 // Make sure that this declaration has not been marked 'unavailable'.
311 if (A->getUnavailable()) {
312 if (Message) {
313 Message->clear();
314 llvm::raw_string_ostream Out(*Message);
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000315 Out << "not available on " << PrettyPlatformName
316 << HintMessage;
Douglas Gregorb53e4172011-03-26 03:35:55 +0000317 }
318
319 return AR_Unavailable;
320 }
321
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000322 // Make sure that this declaration has already been introduced.
323 if (!A->getIntroduced().empty() &&
324 TargetMinVersion < A->getIntroduced()) {
325 if (Message) {
326 Message->clear();
327 llvm::raw_string_ostream Out(*Message);
328 Out << "introduced in " << PrettyPlatformName << ' '
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000329 << A->getIntroduced() << HintMessage;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000330 }
331
332 return AR_NotYetIntroduced;
333 }
334
335 // Make sure that this declaration hasn't been obsoleted.
336 if (!A->getObsoleted().empty() && TargetMinVersion >= A->getObsoleted()) {
337 if (Message) {
338 Message->clear();
339 llvm::raw_string_ostream Out(*Message);
340 Out << "obsoleted in " << PrettyPlatformName << ' '
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000341 << A->getObsoleted() << HintMessage;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000342 }
343
344 return AR_Unavailable;
345 }
346
347 // Make sure that this declaration hasn't been deprecated.
348 if (!A->getDeprecated().empty() && TargetMinVersion >= A->getDeprecated()) {
349 if (Message) {
350 Message->clear();
351 llvm::raw_string_ostream Out(*Message);
352 Out << "first deprecated in " << PrettyPlatformName << ' '
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000353 << A->getDeprecated() << HintMessage;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000354 }
355
356 return AR_Deprecated;
357 }
358
359 return AR_Available;
360}
361
362AvailabilityResult Decl::getAvailability(std::string *Message) const {
363 AvailabilityResult Result = AR_Available;
364 std::string ResultMessage;
365
366 for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
367 if (DeprecatedAttr *Deprecated = dyn_cast<DeprecatedAttr>(*A)) {
368 if (Result >= AR_Deprecated)
369 continue;
370
371 if (Message)
372 ResultMessage = Deprecated->getMessage();
373
374 Result = AR_Deprecated;
375 continue;
376 }
377
378 if (UnavailableAttr *Unavailable = dyn_cast<UnavailableAttr>(*A)) {
379 if (Message)
380 *Message = Unavailable->getMessage();
381 return AR_Unavailable;
382 }
383
384 if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
385 AvailabilityResult AR = CheckAvailability(getASTContext(), Availability,
386 Message);
387
388 if (AR == AR_Unavailable)
389 return AR_Unavailable;
390
391 if (AR > Result) {
392 Result = AR;
393 if (Message)
394 ResultMessage.swap(*Message);
395 }
396 continue;
397 }
398 }
399
400 if (Message)
401 Message->swap(ResultMessage);
402 return Result;
403}
404
405bool Decl::canBeWeakImported(bool &IsDefinition) const {
406 IsDefinition = false;
407 if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
408 if (!Var->hasExternalStorage() || Var->getInit()) {
409 IsDefinition = true;
410 return false;
411 }
412 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
413 if (FD->hasBody()) {
414 IsDefinition = true;
415 return false;
416 }
417 } else if (isa<ObjCPropertyDecl>(this) || isa<ObjCMethodDecl>(this))
418 return false;
419 else if (!(getASTContext().getLangOptions().ObjCNonFragileABI &&
420 isa<ObjCInterfaceDecl>(this)))
421 return false;
422
423 return true;
424}
425
426bool Decl::isWeakImported() const {
427 bool IsDefinition;
428 if (!canBeWeakImported(IsDefinition))
429 return false;
430
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000431 for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
432 if (isa<WeakImportAttr>(*A))
433 return true;
434
435 if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
436 if (CheckAvailability(getASTContext(), Availability, 0)
437 == AR_NotYetIntroduced)
438 return true;
439 }
440 }
441
442 return false;
443}
Tanya Lattner12ead492010-02-17 02:17:21 +0000444
Chris Lattner769dbdf2009-03-27 20:18:19 +0000445unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
446 switch (DeclKind) {
John McCall9488ea12009-11-17 05:59:44 +0000447 case Function:
448 case CXXMethod:
449 case CXXConstructor:
450 case CXXDestructor:
451 case CXXConversion:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000452 case EnumConstant:
453 case Var:
454 case ImplicitParam:
455 case ParmVar:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000456 case NonTypeTemplateParm:
457 case ObjCMethod:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000458 case ObjCProperty:
Daniel Dunbar00b40d32010-04-23 13:07:39 +0000459 return IDNS_Ordinary;
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000460 case Label:
461 return IDNS_Label;
Francois Pichet87c2e122010-11-21 06:08:52 +0000462 case IndirectField:
463 return IDNS_Ordinary | IDNS_Member;
464
John McCall0d6b1642010-04-23 18:46:30 +0000465 case ObjCCompatibleAlias:
466 case ObjCInterface:
467 return IDNS_Ordinary | IDNS_Type;
468
469 case Typedef:
Richard Smith162e1c12011-04-15 14:24:37 +0000470 case TypeAlias:
Richard Smith3e4c6c42011-05-05 21:57:07 +0000471 case TypeAliasTemplate:
John McCall0d6b1642010-04-23 18:46:30 +0000472 case UnresolvedUsingTypename:
473 case TemplateTypeParm:
474 return IDNS_Ordinary | IDNS_Type;
475
John McCall9488ea12009-11-17 05:59:44 +0000476 case UsingShadow:
477 return 0; // we'll actually overwrite this later
478
John McCall7ba107a2009-11-18 02:36:19 +0000479 case UnresolvedUsingValue:
John McCall7ba107a2009-11-18 02:36:19 +0000480 return IDNS_Ordinary | IDNS_Using;
John McCall9488ea12009-11-17 05:59:44 +0000481
482 case Using:
483 return IDNS_Using;
484
Chris Lattner769dbdf2009-03-27 20:18:19 +0000485 case ObjCProtocol:
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000486 return IDNS_ObjCProtocol;
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Chris Lattner769dbdf2009-03-27 20:18:19 +0000488 case Field:
489 case ObjCAtDefsField:
490 case ObjCIvar:
491 return IDNS_Member;
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Chris Lattner769dbdf2009-03-27 20:18:19 +0000493 case Record:
494 case CXXRecord:
495 case Enum:
John McCall0d6b1642010-04-23 18:46:30 +0000496 return IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000497
Chris Lattner769dbdf2009-03-27 20:18:19 +0000498 case Namespace:
John McCall0d6b1642010-04-23 18:46:30 +0000499 case NamespaceAlias:
500 return IDNS_Namespace;
501
Chris Lattner769dbdf2009-03-27 20:18:19 +0000502 case FunctionTemplate:
John McCall0d6b1642010-04-23 18:46:30 +0000503 return IDNS_Ordinary;
504
Chris Lattner769dbdf2009-03-27 20:18:19 +0000505 case ClassTemplate:
506 case TemplateTemplateParm:
John McCall0d6b1642010-04-23 18:46:30 +0000507 return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
Mike Stump1eb44332009-09-09 15:08:12 +0000508
Chris Lattner769dbdf2009-03-27 20:18:19 +0000509 // Never have names.
John McCall02cace72009-08-28 07:59:38 +0000510 case Friend:
John McCalldd4a3b02009-09-16 22:47:08 +0000511 case FriendTemplate:
Abramo Bagnara6206d532010-06-05 05:09:32 +0000512 case AccessSpec:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000513 case LinkageSpec:
514 case FileScopeAsm:
515 case StaticAssert:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000516 case ObjCPropertyImpl:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000517 case Block:
518 case TranslationUnit:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000519
Chris Lattner769dbdf2009-03-27 20:18:19 +0000520 case UsingDirective:
521 case ClassTemplateSpecialization:
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000522 case ClassTemplatePartialSpecialization:
Francois Pichetaf0f4d02011-08-14 03:52:19 +0000523 case ClassScopeFunctionSpecialization:
Douglas Gregorbd4187b2010-04-22 23:19:50 +0000524 case ObjCImplementation:
525 case ObjCCategory:
526 case ObjCCategoryImpl:
Douglas Gregor15de72c2011-12-02 23:23:56 +0000527 case Import:
Douglas Gregorbd4187b2010-04-22 23:19:50 +0000528 // Never looked up by name.
Chris Lattner769dbdf2009-03-27 20:18:19 +0000529 return 0;
530 }
John McCall9488ea12009-11-17 05:59:44 +0000531
David Blaikie30263482012-01-20 21:50:17 +0000532 llvm_unreachable("Invalid DeclKind!");
Eli Friedman56d29372008-06-07 16:52:53 +0000533}
534
Sean Huntcf807c42010-08-18 23:23:40 +0000535void Decl::setAttrs(const AttrVec &attrs) {
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000536 assert(!HasAttrs && "Decl already contains attrs.");
537
Sean Huntcf807c42010-08-18 23:23:40 +0000538 AttrVec &AttrBlank = getASTContext().getDeclAttrs(this);
539 assert(AttrBlank.empty() && "HasAttrs was wrong?");
Argyrios Kyrtzidis1715bf52010-06-11 23:09:25 +0000540
541 AttrBlank = attrs;
542 HasAttrs = true;
543}
544
Sean Huntcf807c42010-08-18 23:23:40 +0000545void Decl::dropAttrs() {
Eli Friedman56d29372008-06-07 16:52:53 +0000546 if (!HasAttrs) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Eli Friedman56d29372008-06-07 16:52:53 +0000548 HasAttrs = false;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000549 getASTContext().eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000550}
551
Sean Huntcf807c42010-08-18 23:23:40 +0000552const AttrVec &Decl::getAttrs() const {
553 assert(HasAttrs && "No attrs to get!");
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000554 return getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000555}
556
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000557void Decl::swapAttrs(Decl *RHS) {
Eli Friedman56d29372008-06-07 16:52:53 +0000558 bool HasLHSAttr = this->HasAttrs;
559 bool HasRHSAttr = RHS->HasAttrs;
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Eli Friedman56d29372008-06-07 16:52:53 +0000561 // Usually, neither decl has attrs, nothing to do.
562 if (!HasLHSAttr && !HasRHSAttr) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000563
Eli Friedman56d29372008-06-07 16:52:53 +0000564 // If 'this' has no attrs, swap the other way.
565 if (!HasLHSAttr)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000566 return RHS->swapAttrs(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000567
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000568 ASTContext &Context = getASTContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000569
Eli Friedman56d29372008-06-07 16:52:53 +0000570 // Handle the case when both decls have attrs.
571 if (HasRHSAttr) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000572 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman56d29372008-06-07 16:52:53 +0000573 return;
574 }
Mike Stump1eb44332009-09-09 15:08:12 +0000575
Eli Friedman56d29372008-06-07 16:52:53 +0000576 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor68584ed2009-06-18 16:11:24 +0000577 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
578 Context.eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000579 this->HasAttrs = false;
580 RHS->HasAttrs = true;
581}
582
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000583Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000584 Decl::Kind DK = D->getDeclKind();
585 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000586#define DECL(NAME, BASE)
587#define DECL_CONTEXT(NAME) \
588 case Decl::NAME: \
589 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
590#define DECL_CONTEXT_BASE(NAME)
591#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000592 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000593#define DECL(NAME, BASE)
594#define DECL_CONTEXT_BASE(NAME) \
595 if (DK >= first##NAME && DK <= last##NAME) \
596 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
597#include "clang/AST/DeclNodes.inc"
David Blaikieb219cfc2011-09-23 05:06:16 +0000598 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000599 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000600}
601
602DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000603 Decl::Kind DK = D->getKind();
604 switch(DK) {
Sean Hunt9a555912010-05-30 07:21:58 +0000605#define DECL(NAME, BASE)
606#define DECL_CONTEXT(NAME) \
607 case Decl::NAME: \
608 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
609#define DECL_CONTEXT_BASE(NAME)
610#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000611 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000612#define DECL(NAME, BASE)
613#define DECL_CONTEXT_BASE(NAME) \
614 if (DK >= first##NAME && DK <= last##NAME) \
615 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
616#include "clang/AST/DeclNodes.inc"
David Blaikieb219cfc2011-09-23 05:06:16 +0000617 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000618 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000619}
620
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000621SourceLocation Decl::getBodyRBrace() const {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +0000622 // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
623 // FunctionDecl stores EndRangeLoc for this purpose.
624 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
625 const FunctionDecl *Definition;
626 if (FD->hasBody(Definition))
627 return Definition->getSourceRange().getEnd();
628 return SourceLocation();
629 }
630
Argyrios Kyrtzidis6717ef42010-07-07 11:31:27 +0000631 if (Stmt *Body = getBody())
632 return Body->getSourceRange().getEnd();
633
634 return SourceLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +0000635}
636
Anders Carlsson1329c272009-03-25 23:38:06 +0000637void Decl::CheckAccessDeclContext() const {
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000638#ifndef NDEBUG
John McCall46460a62010-01-20 21:53:11 +0000639 // Suppress this check if any of the following hold:
640 // 1. this is the translation unit (and thus has no parent)
641 // 2. this is a template parameter (and thus doesn't belong to its context)
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000642 // 3. this is a non-type template parameter
643 // 4. the context is not a record
644 // 5. it's invalid
645 // 6. it's a C++0x static_assert.
Anders Carlsson35eda442009-08-29 20:47:47 +0000646 if (isa<TranslationUnitDecl>(this) ||
Argyrios Kyrtzidis04aed0e2010-07-02 11:55:44 +0000647 isa<TemplateTypeParmDecl>(this) ||
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000648 isa<NonTypeTemplateParmDecl>(this) ||
Douglas Gregorfdd8ab12010-02-22 17:53:38 +0000649 !isa<CXXRecordDecl>(getDeclContext()) ||
Argyrios Kyrtzidis65b63ec2010-09-08 21:32:35 +0000650 isInvalidDecl() ||
651 isa<StaticAssertDecl>(this) ||
652 // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
653 // as DeclContext (?).
Argyrios Kyrtzidisd580e562010-09-08 21:58:42 +0000654 isa<ParmVarDecl>(this) ||
655 // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
656 // AS_none as access specifier.
Francois Pichetbc845322011-08-17 01:06:54 +0000657 isa<CXXRecordDecl>(this) ||
658 isa<ClassScopeFunctionSpecializationDecl>(this))
Anders Carlsson35eda442009-08-29 20:47:47 +0000659 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000660
661 assert(Access != AS_none &&
Anders Carlsson1329c272009-03-25 23:38:06 +0000662 "Access specifier is AS_none inside a record decl");
Douglas Gregor3a1c36c2010-12-02 00:22:25 +0000663#endif
Anders Carlsson1329c272009-03-25 23:38:06 +0000664}
665
John McCallaab9e312011-02-22 22:25:23 +0000666DeclContext *Decl::getNonClosureContext() {
John McCall4b9c2d22011-11-06 09:01:30 +0000667 return getDeclContext()->getNonClosureAncestor();
668}
669
670DeclContext *DeclContext::getNonClosureAncestor() {
671 DeclContext *DC = this;
John McCallaab9e312011-02-22 22:25:23 +0000672
673 // This is basically "while (DC->isClosure()) DC = DC->getParent();"
674 // except that it's significantly more efficient to cast to a known
675 // decl type and call getDeclContext() than to call getParent().
John McCall7b3f8532011-06-23 21:18:31 +0000676 while (isa<BlockDecl>(DC))
677 DC = cast<BlockDecl>(DC)->getDeclContext();
John McCallaab9e312011-02-22 22:25:23 +0000678
679 assert(!DC->isClosure());
680 return DC;
681}
Anders Carlsson1329c272009-03-25 23:38:06 +0000682
Eli Friedman56d29372008-06-07 16:52:53 +0000683//===----------------------------------------------------------------------===//
684// DeclContext Implementation
685//===----------------------------------------------------------------------===//
686
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000687bool DeclContext::classof(const Decl *D) {
688 switch (D->getKind()) {
Sean Hunt9a555912010-05-30 07:21:58 +0000689#define DECL(NAME, BASE)
690#define DECL_CONTEXT(NAME) case Decl::NAME:
691#define DECL_CONTEXT_BASE(NAME)
692#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000693 return true;
694 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000695#define DECL(NAME, BASE)
696#define DECL_CONTEXT_BASE(NAME) \
697 if (D->getKind() >= Decl::first##NAME && \
698 D->getKind() <= Decl::last##NAME) \
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000699 return true;
Sean Hunt9a555912010-05-30 07:21:58 +0000700#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000701 return false;
702 }
703}
704
Douglas Gregora2da7802010-07-25 18:38:02 +0000705DeclContext::~DeclContext() { }
Douglas Gregor44b43212008-12-11 16:49:14 +0000706
Douglas Gregore942bbe2009-09-10 16:57:35 +0000707/// \brief Find the parent context of this context that will be
708/// used for unqualified name lookup.
709///
710/// Generally, the parent lookup context is the semantic context. However, for
711/// a friend function the parent lookup context is the lexical context, which
712/// is the class in which the friend is declared.
713DeclContext *DeclContext::getLookupParent() {
714 // FIXME: Find a better way to identify friends
715 if (isa<FunctionDecl>(this))
Sebastian Redl7a126a42010-08-31 00:36:30 +0000716 if (getParent()->getRedeclContext()->isFileContext() &&
717 getLexicalParent()->getRedeclContext()->isRecord())
Douglas Gregore942bbe2009-09-10 16:57:35 +0000718 return getLexicalParent();
719
720 return getParent();
721}
722
Sebastian Redl410c4f22010-08-31 20:53:31 +0000723bool DeclContext::isInlineNamespace() const {
724 return isNamespace() &&
725 cast<NamespaceDecl>(this)->isInline();
726}
727
Douglas Gregorbc221632009-05-28 16:34:51 +0000728bool DeclContext::isDependentContext() const {
729 if (isFileContext())
730 return false;
731
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000732 if (isa<ClassTemplatePartialSpecializationDecl>(this))
733 return true;
734
Douglas Gregorbc221632009-05-28 16:34:51 +0000735 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
736 if (Record->getDescribedClassTemplate())
737 return true;
738
John McCall0c01d182010-03-24 05:22:00 +0000739 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregorbc221632009-05-28 16:34:51 +0000740 if (Function->getDescribedFunctionTemplate())
741 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000742
John McCall0c01d182010-03-24 05:22:00 +0000743 // Friend function declarations are dependent if their *lexical*
744 // context is dependent.
745 if (cast<Decl>(this)->getFriendObjectKind())
746 return getLexicalParent()->isDependentContext();
747 }
748
Douglas Gregorbc221632009-05-28 16:34:51 +0000749 return getParent() && getParent()->isDependentContext();
750}
751
Douglas Gregor074149e2009-01-05 19:45:36 +0000752bool DeclContext::isTransparentContext() const {
753 if (DeclKind == Decl::Enum)
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000754 return !cast<EnumDecl>(this)->isScoped();
Douglas Gregor074149e2009-01-05 19:45:36 +0000755 else if (DeclKind == Decl::LinkageSpec)
756 return true;
Douglas Gregor074149e2009-01-05 19:45:36 +0000757
758 return false;
759}
760
John McCallac65c622010-10-26 04:59:26 +0000761bool DeclContext::isExternCContext() const {
762 const DeclContext *DC = this;
763 while (DC->DeclKind != Decl::TranslationUnit) {
764 if (DC->DeclKind == Decl::LinkageSpec)
765 return cast<LinkageSpecDecl>(DC)->getLanguage()
766 == LinkageSpecDecl::lang_c;
767 DC = DC->getParent();
768 }
769 return false;
770}
771
Sebastian Redl7a126a42010-08-31 00:36:30 +0000772bool DeclContext::Encloses(const DeclContext *DC) const {
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000773 if (getPrimaryContext() != this)
774 return getPrimaryContext()->Encloses(DC);
Mike Stump1eb44332009-09-09 15:08:12 +0000775
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000776 for (; DC; DC = DC->getParent())
777 if (DC->getPrimaryContext() == this)
778 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000779 return false;
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000780}
781
Steve Naroff0701bbb2009-01-08 17:28:14 +0000782DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000783 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000784 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000785 case Decl::LinkageSpec:
Mike Stump1eb44332009-09-09 15:08:12 +0000786 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000787 // There is only one DeclContext for these entities.
788 return this;
789
790 case Decl::Namespace:
791 // The original namespace is our primary context.
792 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
793
Douglas Gregor44b43212008-12-11 16:49:14 +0000794 case Decl::ObjCMethod:
795 return this;
796
797 case Decl::ObjCInterface:
Douglas Gregor53df7a12011-12-15 18:03:09 +0000798 if (ObjCInterfaceDecl *Def = cast<ObjCInterfaceDecl>(this)->getDefinition())
799 return Def;
800
801 return this;
802
Steve Naroff0701bbb2009-01-08 17:28:14 +0000803 case Decl::ObjCProtocol:
Douglas Gregor1d784b22012-01-01 19:51:50 +0000804 if (ObjCProtocolDecl *Def = cast<ObjCProtocolDecl>(this)->getDefinition())
805 return Def;
806
807 return this;
Douglas Gregor53df7a12011-12-15 18:03:09 +0000808
Steve Naroff0701bbb2009-01-08 17:28:14 +0000809 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000810 return this;
811
Steve Naroff0701bbb2009-01-08 17:28:14 +0000812 case Decl::ObjCImplementation:
813 case Decl::ObjCCategoryImpl:
814 return this;
815
Douglas Gregor44b43212008-12-11 16:49:14 +0000816 default:
Sean Hunt9a555912010-05-30 07:21:58 +0000817 if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000818 // If this is a tag type that has a definition or is currently
819 // being defined, that definition is our primary context.
John McCall3cb0ebd2010-03-10 03:28:59 +0000820 TagDecl *Tag = cast<TagDecl>(this);
821 assert(isa<TagType>(Tag->TypeForDecl) ||
822 isa<InjectedClassNameType>(Tag->TypeForDecl));
823
824 if (TagDecl *Def = Tag->getDefinition())
825 return Def;
826
827 if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) {
828 const TagType *TagTy = cast<TagType>(Tag->TypeForDecl);
829 if (TagTy->isBeingDefined())
830 // FIXME: is it necessarily being defined in the decl
831 // that owns the type?
832 return TagTy->getDecl();
833 }
834
835 return Tag;
Douglas Gregorcc636682009-02-17 23:15:12 +0000836 }
837
Sean Hunt9a555912010-05-30 07:21:58 +0000838 assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
Douglas Gregor44b43212008-12-11 16:49:14 +0000839 "Unknown DeclContext kind");
840 return this;
841 }
842}
843
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +0000844void
845DeclContext::collectAllContexts(llvm::SmallVectorImpl<DeclContext *> &Contexts){
846 Contexts.clear();
847
848 if (DeclKind != Decl::Namespace) {
849 Contexts.push_back(this);
850 return;
Douglas Gregor44b43212008-12-11 16:49:14 +0000851 }
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +0000852
853 NamespaceDecl *Self = static_cast<NamespaceDecl *>(this);
Douglas Gregoref96ee02012-01-14 16:38:05 +0000854 for (NamespaceDecl *N = Self->getMostRecentDecl(); N;
855 N = N->getPreviousDecl())
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +0000856 Contexts.push_back(N);
857
858 std::reverse(Contexts.begin(), Contexts.end());
Douglas Gregor44b43212008-12-11 16:49:14 +0000859}
860
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000861std::pair<Decl *, Decl *>
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +0000862DeclContext::BuildDeclChain(const SmallVectorImpl<Decl*> &Decls,
863 bool FieldsAlreadyLoaded) {
Douglas Gregor46cd2182012-01-06 16:59:53 +0000864 // Build up a chain of declarations via the Decl::NextInContextAndBits field.
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000865 Decl *FirstNewDecl = 0;
866 Decl *PrevDecl = 0;
867 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +0000868 if (FieldsAlreadyLoaded && isa<FieldDecl>(Decls[I]))
869 continue;
870
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000871 Decl *D = Decls[I];
872 if (PrevDecl)
Douglas Gregor46cd2182012-01-06 16:59:53 +0000873 PrevDecl->NextInContextAndBits.setPointer(D);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000874 else
875 FirstNewDecl = D;
876
877 PrevDecl = D;
878 }
879
880 return std::make_pair(FirstNewDecl, PrevDecl);
881}
882
Douglas Gregor2cf26342009-04-09 22:27:44 +0000883/// \brief Load the declarations within this lexical storage from an
884/// external source.
Mike Stump1eb44332009-09-09 15:08:12 +0000885void
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000886DeclContext::LoadLexicalDeclsFromExternalStorage() const {
887 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000888 assert(hasExternalLexicalStorage() && Source && "No external storage?");
889
Argyrios Kyrtzidis0dbbc042010-07-30 10:03:23 +0000890 // Notify that we have a DeclContext that is initializing.
891 ExternalASTSource::Deserializing ADeclContext(Source);
Douglas Gregor9fc18c92011-08-26 21:23:06 +0000892
Douglas Gregorba6ffaf2011-07-15 21:46:17 +0000893 // Load the external declarations, if any.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000894 SmallVector<Decl*, 64> Decls;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000895 ExternalLexicalStorage = false;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +0000896 switch (Source->FindExternalLexicalDecls(this, Decls)) {
897 case ELR_Success:
898 break;
899
900 case ELR_Failure:
901 case ELR_AlreadyLoaded:
902 return;
903 }
Douglas Gregor2cf26342009-04-09 22:27:44 +0000904
905 if (Decls.empty())
906 return;
907
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +0000908 // We may have already loaded just the fields of this record, in which case
909 // we need to ignore them.
910 bool FieldsAlreadyLoaded = false;
911 if (const RecordDecl *RD = dyn_cast<RecordDecl>(this))
912 FieldsAlreadyLoaded = RD->LoadedFieldsFromExternalStorage;
913
Douglas Gregor2cf26342009-04-09 22:27:44 +0000914 // Splice the newly-read declarations into the beginning of the list
915 // of declarations.
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000916 Decl *ExternalFirst, *ExternalLast;
Argyrios Kyrtzidisec2ec1f2011-10-07 21:55:43 +0000917 llvm::tie(ExternalFirst, ExternalLast) = BuildDeclChain(Decls,
918 FieldsAlreadyLoaded);
Douglas Gregor46cd2182012-01-06 16:59:53 +0000919 ExternalLast->NextInContextAndBits.setPointer(FirstDecl);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000920 FirstDecl = ExternalFirst;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000921 if (!LastDecl)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000922 LastDecl = ExternalLast;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000923}
924
John McCall76bd1f32010-06-01 09:23:16 +0000925DeclContext::lookup_result
926ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
927 DeclarationName Name) {
928 ASTContext &Context = DC->getParentASTContext();
929 StoredDeclsMap *Map;
930 if (!(Map = DC->LookupPtr))
931 Map = DC->CreateStoredDeclsMap(Context);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000932
John McCall76bd1f32010-06-01 09:23:16 +0000933 StoredDeclsList &List = (*Map)[Name];
934 assert(List.isNull());
935 (void) List;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000936
John McCall76bd1f32010-06-01 09:23:16 +0000937 return DeclContext::lookup_result();
938}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000939
John McCall76bd1f32010-06-01 09:23:16 +0000940DeclContext::lookup_result
941ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +0000942 DeclarationName Name,
Argyrios Kyrtzidis45df9c62011-09-09 06:44:14 +0000943 ArrayRef<NamedDecl*> Decls) {
John McCall76bd1f32010-06-01 09:23:16 +0000944 ASTContext &Context = DC->getParentASTContext();;
945
946 StoredDeclsMap *Map;
947 if (!(Map = DC->LookupPtr))
948 Map = DC->CreateStoredDeclsMap(Context);
949
950 StoredDeclsList &List = (*Map)[Name];
Argyrios Kyrtzidis45df9c62011-09-09 06:44:14 +0000951 for (ArrayRef<NamedDecl*>::iterator
952 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
John McCall76bd1f32010-06-01 09:23:16 +0000953 if (List.isNull())
Argyrios Kyrtzidis45df9c62011-09-09 06:44:14 +0000954 List.setOnlyValue(*I);
John McCall76bd1f32010-06-01 09:23:16 +0000955 else
Argyrios Kyrtzidis45df9c62011-09-09 06:44:14 +0000956 List.AddSubsequentDecl(*I);
John McCall76bd1f32010-06-01 09:23:16 +0000957 }
958
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000959 return List.getLookupResult();
John McCall76bd1f32010-06-01 09:23:16 +0000960}
961
Sebastian Redl681d7232010-07-27 00:17:23 +0000962DeclContext::decl_iterator DeclContext::noload_decls_begin() const {
963 return decl_iterator(FirstDecl);
964}
965
966DeclContext::decl_iterator DeclContext::noload_decls_end() const {
967 return decl_iterator();
968}
969
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000970DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000971 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000972 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000973
Mike Stump1eb44332009-09-09 15:08:12 +0000974 return decl_iterator(FirstDecl);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000975}
976
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000977DeclContext::decl_iterator DeclContext::decls_end() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000978 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000979 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000980
Mike Stump1eb44332009-09-09 15:08:12 +0000981 return decl_iterator();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000982}
983
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000984bool DeclContext::decls_empty() const {
Douglas Gregor8038d512009-04-10 17:25:41 +0000985 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000986 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor8038d512009-04-10 17:25:41 +0000987
988 return !FirstDecl;
989}
990
John McCall9f54ad42009-12-10 09:41:52 +0000991void DeclContext::removeDecl(Decl *D) {
992 assert(D->getLexicalDeclContext() == this &&
993 "decl being removed from non-lexical context");
Douglas Gregor46cd2182012-01-06 16:59:53 +0000994 assert((D->NextInContextAndBits.getPointer() || D == LastDecl) &&
John McCall9f54ad42009-12-10 09:41:52 +0000995 "decl is not in decls list");
996
997 // Remove D from the decl chain. This is O(n) but hopefully rare.
998 if (D == FirstDecl) {
999 if (D == LastDecl)
1000 FirstDecl = LastDecl = 0;
1001 else
Douglas Gregor46cd2182012-01-06 16:59:53 +00001002 FirstDecl = D->NextInContextAndBits.getPointer();
John McCall9f54ad42009-12-10 09:41:52 +00001003 } else {
Douglas Gregor46cd2182012-01-06 16:59:53 +00001004 for (Decl *I = FirstDecl; true; I = I->NextInContextAndBits.getPointer()) {
John McCall9f54ad42009-12-10 09:41:52 +00001005 assert(I && "decl not found in linked list");
Douglas Gregor46cd2182012-01-06 16:59:53 +00001006 if (I->NextInContextAndBits.getPointer() == D) {
1007 I->NextInContextAndBits.setPointer(D->NextInContextAndBits.getPointer());
John McCall9f54ad42009-12-10 09:41:52 +00001008 if (D == LastDecl) LastDecl = I;
1009 break;
1010 }
1011 }
1012 }
1013
1014 // Mark that D is no longer in the decl chain.
Douglas Gregor46cd2182012-01-06 16:59:53 +00001015 D->NextInContextAndBits.setPointer(0);
John McCall9f54ad42009-12-10 09:41:52 +00001016
1017 // Remove D from the lookup table if necessary.
1018 if (isa<NamedDecl>(D)) {
1019 NamedDecl *ND = cast<NamedDecl>(D);
1020
Axel Naumann02368d02011-08-26 14:06:12 +00001021 // Remove only decls that have a name
1022 if (!ND->getDeclName()) return;
1023
John McCall0c01d182010-03-24 05:22:00 +00001024 StoredDeclsMap *Map = getPrimaryContext()->LookupPtr;
1025 if (!Map) return;
John McCall9f54ad42009-12-10 09:41:52 +00001026
John McCall9f54ad42009-12-10 09:41:52 +00001027 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
1028 assert(Pos != Map->end() && "no lookup entry for decl");
Axel Naumannd9d137e2011-11-08 18:21:06 +00001029 if (Pos->second.getAsVector() || Pos->second.getAsDecl() == ND)
1030 Pos->second.remove(ND);
John McCall9f54ad42009-12-10 09:41:52 +00001031 }
1032}
1033
John McCall3f9a8a62009-08-11 06:59:38 +00001034void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner7f0be132009-02-20 00:56:18 +00001035 assert(D->getLexicalDeclContext() == this &&
1036 "Decl inserted into wrong lexical context");
Mike Stump1eb44332009-09-09 15:08:12 +00001037 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001038 "Decl already inserted into a DeclContext");
1039
1040 if (FirstDecl) {
Douglas Gregor46cd2182012-01-06 16:59:53 +00001041 LastDecl->NextInContextAndBits.setPointer(D);
Douglas Gregor6037fcb2009-01-09 19:42:16 +00001042 LastDecl = D;
1043 } else {
1044 FirstDecl = LastDecl = D;
1045 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +00001046
1047 // Notify a C++ record declaration that we've added a member, so it can
1048 // update it's class-specific state.
1049 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
1050 Record->addedMember(D);
Douglas Gregore6649772011-12-03 00:30:27 +00001051
1052 // If this is a newly-created (not de-serialized) import declaration, wire
1053 // it in to the list of local import declarations.
1054 if (!D->isFromASTFile()) {
1055 if (ImportDecl *Import = dyn_cast<ImportDecl>(D))
1056 D->getASTContext().addedLocalImportDecl(Import);
1057 }
John McCall3f9a8a62009-08-11 06:59:38 +00001058}
1059
1060void DeclContext::addDecl(Decl *D) {
1061 addHiddenDecl(D);
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001062
1063 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001064 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor44b43212008-12-11 16:49:14 +00001065}
1066
Sean Callanan9faf8102011-10-21 02:57:43 +00001067void DeclContext::addDeclInternal(Decl *D) {
1068 addHiddenDecl(D);
1069
1070 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1071 ND->getDeclContext()->makeDeclVisibleInContextInternal(ND);
1072}
1073
Douglas Gregor074149e2009-01-05 19:45:36 +00001074/// buildLookup - Build the lookup data structure with all of the
1075/// declarations in DCtx (and any other contexts linked to it or
1076/// transparent contexts nested within it).
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001077void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001078 llvm::SmallVector<DeclContext *, 2> Contexts;
1079 DCtx->collectAllContexts(Contexts);
1080 for (unsigned I = 0, N = Contexts.size(); I != N; ++I) {
1081 for (decl_iterator D = Contexts[I]->decls_begin(),
1082 DEnd = Contexts[I]->decls_end();
Douglas Gregor4f3b8f82009-01-06 07:17:58 +00001083 D != DEnd; ++D) {
John McCall3f9a8a62009-08-11 06:59:38 +00001084 // Insert this declaration into the lookup structure, but only
1085 // if it's semantically in its decl context. During non-lazy
1086 // lookup building, this is implicitly enforced by addDecl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001087 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001088 if (D->getDeclContext() == Contexts[I])
Sean Callanan9faf8102011-10-21 02:57:43 +00001089 makeDeclVisibleInContextImpl(ND, false);
Douglas Gregor074149e2009-01-05 19:45:36 +00001090
Sebastian Redl410c4f22010-08-31 20:53:31 +00001091 // If this declaration is itself a transparent declaration context or
1092 // inline namespace, add its members (recursively).
Douglas Gregor074149e2009-01-05 19:45:36 +00001093 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
Sebastian Redl410c4f22010-08-31 20:53:31 +00001094 if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001095 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregor074149e2009-01-05 19:45:36 +00001096 }
1097 }
1098}
1099
Mike Stump1eb44332009-09-09 15:08:12 +00001100DeclContext::lookup_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001101DeclContext::lookup(DeclarationName Name) {
Steve Naroff0701bbb2009-01-08 17:28:14 +00001102 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +00001103 if (PrimaryContext != this)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001104 return PrimaryContext->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +00001105
John McCall76bd1f32010-06-01 09:23:16 +00001106 if (hasExternalVisibleStorage()) {
1107 // Check to see if we've already cached the lookup results.
1108 if (LookupPtr) {
1109 StoredDeclsMap::iterator I = LookupPtr->find(Name);
1110 if (I != LookupPtr->end())
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001111 return I->second.getLookupResult();
John McCall76bd1f32010-06-01 09:23:16 +00001112 }
1113
1114 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1115 return Source->FindExternalVisibleDeclsByName(this, Name);
1116 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001117
Douglas Gregor3fc749d2008-12-23 00:26:44 +00001118 /// If there is no lookup data structure, build one now by walking
Douglas Gregor44b43212008-12-11 16:49:14 +00001119 /// all of the linked DeclContexts (in declaration order!) and
1120 /// inserting their values.
Douglas Gregorc36c5402009-04-09 17:29:08 +00001121 if (!LookupPtr) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001122 buildLookup(this);
Douglas Gregor44b43212008-12-11 16:49:14 +00001123
Douglas Gregorc36c5402009-04-09 17:29:08 +00001124 if (!LookupPtr)
Douglas Gregora5fdd9c2010-05-11 06:18:17 +00001125 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Douglas Gregorc36c5402009-04-09 17:29:08 +00001126 }
Douglas Gregor44b43212008-12-11 16:49:14 +00001127
John McCall0c01d182010-03-24 05:22:00 +00001128 StoredDeclsMap::iterator Pos = LookupPtr->find(Name);
1129 if (Pos == LookupPtr->end())
Douglas Gregora5fdd9c2010-05-11 06:18:17 +00001130 return lookup_result(lookup_iterator(0), lookup_iterator(0));
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001131 return Pos->second.getLookupResult();
Douglas Gregor44b43212008-12-11 16:49:14 +00001132}
1133
Mike Stump1eb44332009-09-09 15:08:12 +00001134DeclContext::lookup_const_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001135DeclContext::lookup(DeclarationName Name) const {
1136 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +00001137}
1138
Douglas Gregorb75a3452011-10-15 00:10:27 +00001139void DeclContext::localUncachedLookup(DeclarationName Name,
1140 llvm::SmallVectorImpl<NamedDecl *> &Results) {
1141 Results.clear();
1142
1143 // If there's no external storage, just perform a normal lookup and copy
1144 // the results.
1145 if (!hasExternalVisibleStorage() && !hasExternalLexicalStorage()) {
1146 lookup_result LookupResults = lookup(Name);
1147 Results.insert(Results.end(), LookupResults.first, LookupResults.second);
1148 return;
1149 }
1150
1151 // If we have a lookup table, check there first. Maybe we'll get lucky.
1152 if (LookupPtr) {
1153 StoredDeclsMap::iterator Pos = LookupPtr->find(Name);
1154 if (Pos != LookupPtr->end()) {
1155 Results.insert(Results.end(),
1156 Pos->second.getLookupResult().first,
1157 Pos->second.getLookupResult().second);
1158 return;
1159 }
1160 }
1161
1162 // Slow case: grovel through the declarations in our chain looking for
1163 // matches.
1164 for (Decl *D = FirstDecl; D; D = D->getNextDeclInContext()) {
1165 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1166 if (ND->getDeclName() == Name)
1167 Results.push_back(ND);
1168 }
1169}
1170
Sebastian Redl7a126a42010-08-31 00:36:30 +00001171DeclContext *DeclContext::getRedeclContext() {
Chris Lattner0cf2b192009-03-27 19:19:59 +00001172 DeclContext *Ctx = this;
Sebastian Redl410c4f22010-08-31 20:53:31 +00001173 // Skip through transparent contexts.
1174 while (Ctx->isTransparentContext())
Douglas Gregorce356072009-01-06 23:51:29 +00001175 Ctx = Ctx->getParent();
1176 return Ctx;
1177}
1178
Douglas Gregor88b70942009-02-25 22:02:03 +00001179DeclContext *DeclContext::getEnclosingNamespaceContext() {
1180 DeclContext *Ctx = this;
1181 // Skip through non-namespace, non-translation-unit contexts.
Sebastian Redl51a8a372010-08-31 00:36:23 +00001182 while (!Ctx->isFileContext())
Douglas Gregor88b70942009-02-25 22:02:03 +00001183 Ctx = Ctx->getParent();
1184 return Ctx->getPrimaryContext();
1185}
1186
Sebastian Redl7a126a42010-08-31 00:36:30 +00001187bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
1188 // For non-file contexts, this is equivalent to Equals.
1189 if (!isFileContext())
1190 return O->Equals(this);
1191
1192 do {
1193 if (O->Equals(this))
1194 return true;
1195
1196 const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O);
1197 if (!NS || !NS->isInline())
1198 break;
1199 O = NS->getParent();
1200 } while (O);
1201
1202 return false;
1203}
1204
Sean Callanan9faf8102011-10-21 02:57:43 +00001205void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable)
1206{
1207 makeDeclVisibleInContextWithFlags(D, false, Recoverable);
1208}
1209
1210void DeclContext::makeDeclVisibleInContextInternal(NamedDecl *D, bool Recoverable)
1211{
1212 makeDeclVisibleInContextWithFlags(D, true, Recoverable);
1213}
1214
1215void DeclContext::makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal, bool Recoverable) {
Douglas Gregorcc636682009-02-17 23:15:12 +00001216 // FIXME: This feels like a hack. Should DeclarationName support
1217 // template-ids, or is there a better way to keep specializations
1218 // from being visible?
Douglas Gregor9a299e02011-03-04 17:52:15 +00001219 if (isa<ClassTemplateSpecializationDecl>(D) || D->isTemplateParameter())
Douglas Gregorcc636682009-02-17 23:15:12 +00001220 return;
Eli Friedman6bc20132009-12-08 05:40:03 +00001221 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1222 if (FD->isFunctionTemplateSpecialization())
1223 return;
Douglas Gregorcc636682009-02-17 23:15:12 +00001224
Steve Naroff0701bbb2009-01-08 17:28:14 +00001225 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +00001226 if (PrimaryContext != this) {
Sean Callanan9faf8102011-10-21 02:57:43 +00001227 PrimaryContext->makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
Douglas Gregor44b43212008-12-11 16:49:14 +00001228 return;
1229 }
1230
1231 // If we already have a lookup data structure, perform the insertion
Argyrios Kyrtzidis5586b012010-07-04 21:44:25 +00001232 // into it. If we haven't deserialized externally stored decls, deserialize
1233 // them so we can add the decl. Otherwise, be lazy and don't build that
1234 // structure until someone asks for it.
1235 if (LookupPtr || !Recoverable || hasExternalVisibleStorage())
Sean Callanan9faf8102011-10-21 02:57:43 +00001236 makeDeclVisibleInContextImpl(D, Internal);
Douglas Gregor074149e2009-01-05 19:45:36 +00001237
Sebastian Redl410c4f22010-08-31 20:53:31 +00001238 // If we are a transparent context or inline namespace, insert into our
1239 // parent context, too. This operation is recursive.
1240 if (isTransparentContext() || isInlineNamespace())
Sean Callanan9faf8102011-10-21 02:57:43 +00001241 getParent()->makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00001242
1243 Decl *DCAsDecl = cast<Decl>(this);
1244 // Notify that a decl was made visible unless it's a Tag being defined.
1245 if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined()))
1246 if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
1247 L->AddedVisibleDecl(this, D);
Douglas Gregor44b43212008-12-11 16:49:14 +00001248}
1249
Sean Callanan9faf8102011-10-21 02:57:43 +00001250void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal) {
Douglas Gregor074149e2009-01-05 19:45:36 +00001251 // Skip unnamed declarations.
1252 if (!D->getDeclName())
1253 return;
1254
Douglas Gregor5cb0ef42011-05-06 23:32:38 +00001255 // Skip entities that can't be found by name lookup into a particular
1256 // context.
1257 if ((D->getIdentifierNamespace() == 0 && !isa<UsingDirectiveDecl>(D)) ||
1258 D->isTemplateParameter())
Douglas Gregorcc636682009-02-17 23:15:12 +00001259 return;
1260
Argyrios Kyrtzidis5586b012010-07-04 21:44:25 +00001261 ASTContext *C = 0;
1262 if (!LookupPtr) {
1263 C = &getParentASTContext();
1264 CreateStoredDeclsMap(*C);
1265 }
1266
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001267 // If there is an external AST source, load any declarations it knows about
1268 // with this declaration's name.
1269 // If the lookup table contains an entry about this name it means that we
1270 // have already checked the external source.
Sean Callanan9faf8102011-10-21 02:57:43 +00001271 if (!Internal)
1272 if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
1273 if (hasExternalVisibleStorage() &&
1274 LookupPtr->find(D->getDeclName()) == LookupPtr->end())
1275 Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001276
Douglas Gregor44b43212008-12-11 16:49:14 +00001277 // Insert this declaration into the map.
John McCall0c01d182010-03-24 05:22:00 +00001278 StoredDeclsList &DeclNameEntries = (*LookupPtr)[D->getDeclName()];
Chris Lattner67762a32009-02-20 01:44:05 +00001279 if (DeclNameEntries.isNull()) {
1280 DeclNameEntries.setOnlyValue(D);
Chris Lattnerbd6c8002009-02-19 07:00:44 +00001281 return;
Douglas Gregor44b43212008-12-11 16:49:14 +00001282 }
Chris Lattner91942502009-02-20 00:55:03 +00001283
Chris Lattnerbdc3d002009-02-20 01:10:07 +00001284 // If it is possible that this is a redeclaration, check to see if there is
1285 // already a decl for which declarationReplaces returns true. If there is
1286 // one, just replace it and return.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00001287 if (DeclNameEntries.HandleRedeclaration(D))
Chris Lattner67762a32009-02-20 01:44:05 +00001288 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001289
Chris Lattnerbd6c8002009-02-19 07:00:44 +00001290 // Put this declaration into the appropriate slot.
Chris Lattner67762a32009-02-20 01:44:05 +00001291 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor44b43212008-12-11 16:49:14 +00001292}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001293
1294/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
1295/// this context.
Mike Stump1eb44332009-09-09 15:08:12 +00001296DeclContext::udir_iterator_range
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001297DeclContext::getUsingDirectives() const {
1298 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001299 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
1300 reinterpret_cast<udir_iterator>(Result.second));
1301}
Douglas Gregor2cf26342009-04-09 22:27:44 +00001302
Ted Kremenek3478eb62010-02-11 07:12:28 +00001303//===----------------------------------------------------------------------===//
1304// Creation and Destruction of StoredDeclsMaps. //
1305//===----------------------------------------------------------------------===//
1306
John McCall0c01d182010-03-24 05:22:00 +00001307StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
1308 assert(!LookupPtr && "context already has a decls map");
1309 assert(getPrimaryContext() == this &&
1310 "creating decls map on non-primary context");
1311
1312 StoredDeclsMap *M;
1313 bool Dependent = isDependentContext();
1314 if (Dependent)
1315 M = new DependentStoredDeclsMap();
1316 else
1317 M = new StoredDeclsMap();
1318 M->Previous = C.LastSDM;
1319 C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
1320 LookupPtr = M;
Ted Kremenek3478eb62010-02-11 07:12:28 +00001321 return M;
1322}
1323
1324void ASTContext::ReleaseDeclContextMaps() {
John McCall0c01d182010-03-24 05:22:00 +00001325 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1326 // pointer because the subclass doesn't add anything that needs to
1327 // be deleted.
John McCall0c01d182010-03-24 05:22:00 +00001328 StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1329}
1330
1331void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1332 while (Map) {
1333 // Advance the iteration before we invalidate memory.
1334 llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1335
1336 if (Dependent)
1337 delete static_cast<DependentStoredDeclsMap*>(Map);
1338 else
1339 delete Map;
1340
1341 Map = Next.getPointer();
1342 Dependent = Next.getInt();
1343 }
1344}
1345
John McCall0c01d182010-03-24 05:22:00 +00001346DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1347 DeclContext *Parent,
1348 const PartialDiagnostic &PDiag) {
1349 assert(Parent->isDependentContext()
1350 && "cannot iterate dependent diagnostics of non-dependent context");
1351 Parent = Parent->getPrimaryContext();
1352 if (!Parent->LookupPtr)
1353 Parent->CreateStoredDeclsMap(C);
1354
1355 DependentStoredDeclsMap *Map
1356 = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr);
1357
Douglas Gregorb8365182010-03-29 23:56:53 +00001358 // Allocate the copy of the PartialDiagnostic via the ASTContext's
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00001359 // BumpPtrAllocator, rather than the ASTContext itself.
Douglas Gregorb8365182010-03-29 23:56:53 +00001360 PartialDiagnostic::Storage *DiagStorage = 0;
1361 if (PDiag.hasStorage())
1362 DiagStorage = new (C) PartialDiagnostic::Storage;
1363
1364 DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
John McCall0c01d182010-03-24 05:22:00 +00001365
1366 // TODO: Maybe we shouldn't reverse the order during insertion.
1367 DD->NextDiagnostic = Map->FirstDiagnostic;
1368 Map->FirstDiagnostic = DD;
1369
1370 return DD;
Ted Kremenek3478eb62010-02-11 07:12:28 +00001371}