blob: 3f23f3d855cf09083f78eef3998b8e56f3274c51 [file] [log] [blame]
Eli Friedman7dbab8a2008-06-07 16:52:53 +00001//===--- DeclBase.cpp - Declaration AST Node Implementation ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Decl and DeclContext classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclBase.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000015#include "clang/AST/ASTContext.h"
16#include "clang/AST/ASTMutationListener.h"
17#include "clang/AST/Attr.h"
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +000018#include "clang/AST/Decl.h"
Argyrios Kyrtzidis2951e142008-06-09 21:05:31 +000019#include "clang/AST/DeclCXX.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/AST/DeclContextInternals.h"
John McCallbbbbe4e2010-03-11 07:50:04 +000021#include "clang/AST/DeclFriend.h"
Douglas Gregorded2d7b2009-02-04 19:02:06 +000022#include "clang/AST/DeclObjC.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000023#include "clang/AST/DeclOpenMP.h"
Douglas Gregorded2d7b2009-02-04 19:02:06 +000024#include "clang/AST/DeclTemplate.h"
John McCallc62bb642010-03-24 05:22:00 +000025#include "clang/AST/DependentDiagnostic.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000026#include "clang/AST/ExternalASTSource.h"
Sebastian Redla7b98a72009-04-26 20:35:05 +000027#include "clang/AST/Stmt.h"
28#include "clang/AST/StmtCXX.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000029#include "clang/AST/Type.h"
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000030#include "clang/Basic/TargetInfo.h"
Eli Friedman7dbab8a2008-06-07 16:52:53 +000031#include "llvm/ADT/DenseMap.h"
Chris Lattnereae6cb62009-03-05 08:00:35 +000032#include "llvm/Support/raw_ostream.h"
Douglas Gregor8b9ccca2008-12-23 21:05:05 +000033#include <algorithm>
Eli Friedman7dbab8a2008-06-07 16:52:53 +000034using namespace clang;
35
36//===----------------------------------------------------------------------===//
37// Statistics
38//===----------------------------------------------------------------------===//
39
Alexis Hunted053252010-05-30 07:21:58 +000040#define DECL(DERIVED, BASE) static int n##DERIVED##s = 0;
41#define ABSTRACT_DECL(DECL)
42#include "clang/AST/DeclNodes.inc"
Eli Friedman7dbab8a2008-06-07 16:52:53 +000043
Douglas Gregor7dab26b2013-02-09 01:35:03 +000044void Decl::updateOutOfDate(IdentifierInfo &II) const {
45 getASTContext().getExternalSource()->updateOutOfDateIdentifier(II);
46}
47
Douglas Gregor72172e92012-01-05 21:55:30 +000048void *Decl::AllocateDeserializedDecl(const ASTContext &Context,
49 unsigned ID,
50 unsigned Size) {
Douglas Gregor52261fd2012-01-05 23:49:36 +000051 // Allocate an extra 8 bytes worth of storage, which ensures that the
Douglas Gregorcfe7dc62012-01-09 17:30:44 +000052 // resulting pointer will still be 8-byte aligned.
Douglas Gregor52261fd2012-01-05 23:49:36 +000053 void *Start = Context.Allocate(Size + 8);
54 void *Result = (char*)Start + 8;
Douglas Gregor64af53c2012-01-05 22:27:05 +000055
Douglas Gregorcfe7dc62012-01-09 17:30:44 +000056 unsigned *PrefixPtr = (unsigned *)Result - 2;
57
58 // Zero out the first 4 bytes; this is used to store the owning module ID.
59 PrefixPtr[0] = 0;
60
61 // Store the global declaration ID in the second 4 bytes.
62 PrefixPtr[1] = ID;
Douglas Gregor64af53c2012-01-05 22:27:05 +000063
64 return Result;
Douglas Gregor72172e92012-01-05 21:55:30 +000065}
66
Douglas Gregorc147b0b2013-01-12 01:29:50 +000067Module *Decl::getOwningModuleSlow() const {
68 assert(isFromASTFile() && "Not from AST file?");
69 return getASTContext().getExternalSource()->getModule(getOwningModuleID());
70}
71
Eli Friedman7dbab8a2008-06-07 16:52:53 +000072const char *Decl::getDeclKindName() const {
73 switch (DeclKind) {
David Blaikie83d382b2011-09-23 05:06:16 +000074 default: llvm_unreachable("Declaration not in DeclNodes.inc!");
Alexis Hunted053252010-05-30 07:21:58 +000075#define DECL(DERIVED, BASE) case DERIVED: return #DERIVED;
76#define ABSTRACT_DECL(DECL)
77#include "clang/AST/DeclNodes.inc"
Eli Friedman7dbab8a2008-06-07 16:52:53 +000078 }
79}
80
Douglas Gregor90d47172010-03-05 00:26:45 +000081void Decl::setInvalidDecl(bool Invalid) {
82 InvalidDecl = Invalid;
Argyrios Kyrtzidisb7d1ca22012-03-09 21:09:04 +000083 if (Invalid && !isa<ParmVarDecl>(this)) {
Douglas Gregor90d47172010-03-05 00:26:45 +000084 // Defensive maneuver for ill-formed code: we're likely not to make it to
85 // a point where we set the access specifier, so default it to "public"
86 // to avoid triggering asserts elsewhere in the front end.
87 setAccess(AS_public);
88 }
89}
90
Steve Naroff5faaef72009-01-20 19:53:53 +000091const char *DeclContext::getDeclKindName() const {
92 switch (DeclKind) {
David Blaikie83d382b2011-09-23 05:06:16 +000093 default: llvm_unreachable("Declaration context not in DeclNodes.inc!");
Alexis Hunted053252010-05-30 07:21:58 +000094#define DECL(DERIVED, BASE) case Decl::DERIVED: return #DERIVED;
95#define ABSTRACT_DECL(DECL)
96#include "clang/AST/DeclNodes.inc"
Steve Naroff5faaef72009-01-20 19:53:53 +000097 }
98}
99
Daniel Dunbar62905572012-03-05 21:42:49 +0000100bool Decl::StatisticsEnabled = false;
101void Decl::EnableStatistics() {
102 StatisticsEnabled = true;
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000103}
104
105void Decl::PrintStats() {
Chandler Carruthbfb154a2011-07-04 06:13:27 +0000106 llvm::errs() << "\n*** Decl Stats:\n";
Mike Stump11289f42009-09-09 15:08:12 +0000107
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +0000108 int totalDecls = 0;
Alexis Hunted053252010-05-30 07:21:58 +0000109#define DECL(DERIVED, BASE) totalDecls += n##DERIVED##s;
110#define ABSTRACT_DECL(DECL)
111#include "clang/AST/DeclNodes.inc"
Chandler Carruthbfb154a2011-07-04 06:13:27 +0000112 llvm::errs() << " " << totalDecls << " decls total.\n";
Mike Stump11289f42009-09-09 15:08:12 +0000113
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +0000114 int totalBytes = 0;
Alexis Hunted053252010-05-30 07:21:58 +0000115#define DECL(DERIVED, BASE) \
116 if (n##DERIVED##s > 0) { \
117 totalBytes += (int)(n##DERIVED##s * sizeof(DERIVED##Decl)); \
Chandler Carruthbfb154a2011-07-04 06:13:27 +0000118 llvm::errs() << " " << n##DERIVED##s << " " #DERIVED " decls, " \
119 << sizeof(DERIVED##Decl) << " each (" \
120 << n##DERIVED##s * sizeof(DERIVED##Decl) \
121 << " bytes)\n"; \
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +0000122 }
Alexis Hunted053252010-05-30 07:21:58 +0000123#define ABSTRACT_DECL(DECL)
124#include "clang/AST/DeclNodes.inc"
Mike Stump11289f42009-09-09 15:08:12 +0000125
Chandler Carruthbfb154a2011-07-04 06:13:27 +0000126 llvm::errs() << "Total bytes = " << totalBytes << "\n";
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000127}
128
Alexis Hunted053252010-05-30 07:21:58 +0000129void Decl::add(Kind k) {
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000130 switch (k) {
Alexis Hunted053252010-05-30 07:21:58 +0000131#define DECL(DERIVED, BASE) case DERIVED: ++n##DERIVED##s; break;
132#define ABSTRACT_DECL(DECL)
133#include "clang/AST/DeclNodes.inc"
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000134 }
135}
136
Anders Carlssonaa73b912009-06-13 00:08:58 +0000137bool Decl::isTemplateParameterPack() const {
138 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
139 return TTP->isParameterPack();
Douglas Gregorda3cc0d2010-12-23 23:51:58 +0000140 if (const NonTypeTemplateParmDecl *NTTP
Douglas Gregorf5500772011-01-05 15:48:55 +0000141 = dyn_cast<NonTypeTemplateParmDecl>(this))
Douglas Gregorda3cc0d2010-12-23 23:51:58 +0000142 return NTTP->isParameterPack();
Douglas Gregorf5500772011-01-05 15:48:55 +0000143 if (const TemplateTemplateParmDecl *TTP
144 = dyn_cast<TemplateTemplateParmDecl>(this))
145 return TTP->isParameterPack();
Anders Carlssonaa73b912009-06-13 00:08:58 +0000146 return false;
147}
148
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +0000149bool Decl::isParameterPack() const {
150 if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(this))
151 return Parm->isParameterPack();
152
153 return isTemplateParameterPack();
154}
155
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000156bool Decl::isFunctionOrFunctionTemplate() const {
John McCall3f746822009-11-17 05:59:44 +0000157 if (const UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(this))
Anders Carlssonf057cb22009-06-26 05:26:50 +0000158 return UD->getTargetDecl()->isFunctionOrFunctionTemplate();
Mike Stump11289f42009-09-09 15:08:12 +0000159
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000160 return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this);
161}
162
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000163bool Decl::isTemplateDecl() const {
164 return isa<TemplateDecl>(this);
165}
166
Argyrios Kyrtzidis0ce4c9a2011-09-28 02:45:33 +0000167const DeclContext *Decl::getParentFunctionOrMethod() const {
168 for (const DeclContext *DC = getDeclContext();
169 DC && !DC->isTranslationUnit() && !DC->isNamespace();
Douglas Gregorf5974fa2010-01-16 20:21:20 +0000170 DC = DC->getParent())
171 if (DC->isFunctionOrMethod())
Argyrios Kyrtzidis0ce4c9a2011-09-28 02:45:33 +0000172 return DC;
Douglas Gregorf5974fa2010-01-16 20:21:20 +0000173
Argyrios Kyrtzidis0ce4c9a2011-09-28 02:45:33 +0000174 return 0;
Douglas Gregorf5974fa2010-01-16 20:21:20 +0000175}
176
Douglas Gregor133eddd2011-02-17 08:47:29 +0000177
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000178//===----------------------------------------------------------------------===//
Chris Lattnereae6cb62009-03-05 08:00:35 +0000179// PrettyStackTraceDecl Implementation
180//===----------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000181
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000182void PrettyStackTraceDecl::print(raw_ostream &OS) const {
Chris Lattnereae6cb62009-03-05 08:00:35 +0000183 SourceLocation TheLoc = Loc;
184 if (TheLoc.isInvalid() && TheDecl)
185 TheLoc = TheDecl->getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000186
Chris Lattnereae6cb62009-03-05 08:00:35 +0000187 if (TheLoc.isValid()) {
188 TheLoc.print(OS, SM);
189 OS << ": ";
190 }
191
192 OS << Message;
193
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +0000194 if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl)) {
195 OS << " '";
196 DN->printQualifiedName(OS);
197 OS << '\'';
198 }
Chris Lattnereae6cb62009-03-05 08:00:35 +0000199 OS << '\n';
200}
Mike Stump11289f42009-09-09 15:08:12 +0000201
Chris Lattnereae6cb62009-03-05 08:00:35 +0000202//===----------------------------------------------------------------------===//
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000203// Decl Implementation
204//===----------------------------------------------------------------------===//
205
Douglas Gregorb11aad82011-02-19 18:51:44 +0000206// Out-of-line virtual method providing a home for Decl.
207Decl::~Decl() { }
Douglas Gregora43942a2011-02-17 07:02:32 +0000208
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000209void Decl::setDeclContext(DeclContext *DC) {
Chris Lattnerb81eb052009-03-29 06:06:59 +0000210 DeclCtx = DC;
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000211}
212
213void Decl::setLexicalDeclContext(DeclContext *DC) {
214 if (DC == getLexicalDeclContext())
215 return;
216
217 if (isInSemaDC()) {
Argyrios Kyrtzidis6f40eb72012-02-09 02:44:08 +0000218 setDeclContextsImpl(getDeclContext(), DC, getASTContext());
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000219 } else {
220 getMultipleDC()->LexicalDC = DC;
221 }
222}
223
Argyrios Kyrtzidis6f40eb72012-02-09 02:44:08 +0000224void Decl::setDeclContextsImpl(DeclContext *SemaDC, DeclContext *LexicalDC,
225 ASTContext &Ctx) {
226 if (SemaDC == LexicalDC) {
227 DeclCtx = SemaDC;
228 } else {
229 Decl::MultipleDC *MDC = new (Ctx) Decl::MultipleDC();
230 MDC->SemanticDC = SemaDC;
231 MDC->LexicalDC = LexicalDC;
232 DeclCtx = MDC;
233 }
234}
235
John McCall4fa53422009-10-01 00:25:31 +0000236bool Decl::isInAnonymousNamespace() const {
237 const DeclContext *DC = getDeclContext();
238 do {
239 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
240 if (ND->isAnonymousNamespace())
241 return true;
242 } while ((DC = DC->getParent()));
243
244 return false;
245}
246
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000247TranslationUnitDecl *Decl::getTranslationUnitDecl() {
Argyrios Kyrtzidis4e1a72b2009-06-30 02:34:53 +0000248 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
249 return TUD;
250
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000251 DeclContext *DC = getDeclContext();
252 assert(DC && "This decl is not contained in a translation unit!");
Mike Stump11289f42009-09-09 15:08:12 +0000253
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000254 while (!DC->isTranslationUnit()) {
255 DC = DC->getParent();
256 assert(DC && "This decl is not contained in a translation unit!");
257 }
Mike Stump11289f42009-09-09 15:08:12 +0000258
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000259 return cast<TranslationUnitDecl>(DC);
260}
261
262ASTContext &Decl::getASTContext() const {
Mike Stump11289f42009-09-09 15:08:12 +0000263 return getTranslationUnitDecl()->getASTContext();
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000264}
265
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +0000266ASTMutationListener *Decl::getASTMutationListener() const {
267 return getASTContext().getASTMutationListener();
268}
269
Benjamin Kramerea70eb32012-12-01 15:09:41 +0000270unsigned Decl::getMaxAlignment() const {
271 if (!hasAttrs())
272 return 0;
273
274 unsigned Align = 0;
275 const AttrVec &V = getAttrs();
276 ASTContext &Ctx = getASTContext();
277 specific_attr_iterator<AlignedAttr> I(V.begin()), E(V.end());
278 for (; I != E; ++I)
279 Align = std::max(Align, I->getAlignment(Ctx));
280 return Align;
281}
282
Douglas Gregorebada0772010-06-17 23:14:26 +0000283bool Decl::isUsed(bool CheckUsedAttr) const {
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000284 if (Used)
285 return true;
286
287 // Check for used attribute.
Douglas Gregorebada0772010-06-17 23:14:26 +0000288 if (CheckUsedAttr && hasAttr<UsedAttr>())
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000289 return true;
Rafael Espindola99e3bfb2012-11-23 16:26:30 +0000290
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000291 return false;
292}
293
Argyrios Kyrtzidis16180232011-04-19 19:51:10 +0000294bool Decl::isReferenced() const {
295 if (Referenced)
296 return true;
297
298 // Check redeclarations.
299 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
300 if (I->Referenced)
301 return true;
302
303 return false;
304}
305
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000306/// \brief Determine the availability of the given declaration based on
307/// the target platform.
308///
309/// When it returns an availability result other than \c AR_Available,
310/// if the \p Message parameter is non-NULL, it will be set to a
311/// string describing why the entity is unavailable.
312///
313/// FIXME: Make these strings localizable, since they end up in
314/// diagnostics.
315static AvailabilityResult CheckAvailability(ASTContext &Context,
316 const AvailabilityAttr *A,
317 std::string *Message) {
Douglas Gregore8bbc122011-09-02 00:18:52 +0000318 StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000319 StringRef PrettyPlatformName
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000320 = AvailabilityAttr::getPrettyPlatformName(TargetPlatform);
321 if (PrettyPlatformName.empty())
322 PrettyPlatformName = TargetPlatform;
323
Douglas Gregore8bbc122011-09-02 00:18:52 +0000324 VersionTuple TargetMinVersion = Context.getTargetInfo().getPlatformMinVersion();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000325 if (TargetMinVersion.empty())
326 return AR_Available;
327
328 // Match the platform name.
329 if (A->getPlatform()->getName() != TargetPlatform)
330 return AR_Available;
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000331
332 std::string HintMessage;
333 if (!A->getMessage().empty()) {
334 HintMessage = " - ";
335 HintMessage += A->getMessage();
336 }
337
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000338 // Make sure that this declaration has not been marked 'unavailable'.
339 if (A->getUnavailable()) {
340 if (Message) {
341 Message->clear();
342 llvm::raw_string_ostream Out(*Message);
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000343 Out << "not available on " << PrettyPlatformName
344 << HintMessage;
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000345 }
346
347 return AR_Unavailable;
348 }
349
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000350 // Make sure that this declaration has already been introduced.
351 if (!A->getIntroduced().empty() &&
352 TargetMinVersion < A->getIntroduced()) {
353 if (Message) {
354 Message->clear();
355 llvm::raw_string_ostream Out(*Message);
356 Out << "introduced in " << PrettyPlatformName << ' '
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000357 << A->getIntroduced() << HintMessage;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000358 }
359
360 return AR_NotYetIntroduced;
361 }
362
363 // Make sure that this declaration hasn't been obsoleted.
364 if (!A->getObsoleted().empty() && TargetMinVersion >= A->getObsoleted()) {
365 if (Message) {
366 Message->clear();
367 llvm::raw_string_ostream Out(*Message);
368 Out << "obsoleted in " << PrettyPlatformName << ' '
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000369 << A->getObsoleted() << HintMessage;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000370 }
371
372 return AR_Unavailable;
373 }
374
375 // Make sure that this declaration hasn't been deprecated.
376 if (!A->getDeprecated().empty() && TargetMinVersion >= A->getDeprecated()) {
377 if (Message) {
378 Message->clear();
379 llvm::raw_string_ostream Out(*Message);
380 Out << "first deprecated in " << PrettyPlatformName << ' '
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000381 << A->getDeprecated() << HintMessage;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000382 }
383
384 return AR_Deprecated;
385 }
386
387 return AR_Available;
388}
389
390AvailabilityResult Decl::getAvailability(std::string *Message) const {
391 AvailabilityResult Result = AR_Available;
392 std::string ResultMessage;
393
394 for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
395 if (DeprecatedAttr *Deprecated = dyn_cast<DeprecatedAttr>(*A)) {
396 if (Result >= AR_Deprecated)
397 continue;
398
399 if (Message)
400 ResultMessage = Deprecated->getMessage();
401
402 Result = AR_Deprecated;
403 continue;
404 }
405
406 if (UnavailableAttr *Unavailable = dyn_cast<UnavailableAttr>(*A)) {
407 if (Message)
408 *Message = Unavailable->getMessage();
409 return AR_Unavailable;
410 }
411
412 if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
413 AvailabilityResult AR = CheckAvailability(getASTContext(), Availability,
414 Message);
415
416 if (AR == AR_Unavailable)
417 return AR_Unavailable;
418
419 if (AR > Result) {
420 Result = AR;
421 if (Message)
422 ResultMessage.swap(*Message);
423 }
424 continue;
425 }
426 }
427
428 if (Message)
429 Message->swap(ResultMessage);
430 return Result;
431}
432
433bool Decl::canBeWeakImported(bool &IsDefinition) const {
434 IsDefinition = false;
John McCall5fb5df92012-06-20 06:18:46 +0000435
436 // Variables, if they aren't definitions.
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000437 if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000438 if (Var->isThisDeclarationADefinition()) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000439 IsDefinition = true;
440 return false;
441 }
John McCall5fb5df92012-06-20 06:18:46 +0000442 return true;
443
444 // Functions, if they aren't definitions.
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000445 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
446 if (FD->hasBody()) {
447 IsDefinition = true;
448 return false;
449 }
John McCall5fb5df92012-06-20 06:18:46 +0000450 return true;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000451
John McCall5fb5df92012-06-20 06:18:46 +0000452 // Objective-C classes, if this is the non-fragile runtime.
453 } else if (isa<ObjCInterfaceDecl>(this) &&
John McCall18ac1632012-06-20 21:58:02 +0000454 getASTContext().getLangOpts().ObjCRuntime.hasWeakClassImport()) {
John McCall5fb5df92012-06-20 06:18:46 +0000455 return true;
456
457 // Nothing else.
458 } else {
459 return false;
460 }
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000461}
462
463bool Decl::isWeakImported() const {
464 bool IsDefinition;
465 if (!canBeWeakImported(IsDefinition))
466 return false;
467
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000468 for (attr_iterator A = attr_begin(), AEnd = attr_end(); A != AEnd; ++A) {
469 if (isa<WeakImportAttr>(*A))
470 return true;
471
472 if (AvailabilityAttr *Availability = dyn_cast<AvailabilityAttr>(*A)) {
473 if (CheckAvailability(getASTContext(), Availability, 0)
474 == AR_NotYetIntroduced)
475 return true;
476 }
477 }
478
479 return false;
480}
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000481
Chris Lattner8e097192009-03-27 20:18:19 +0000482unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
483 switch (DeclKind) {
John McCall3f746822009-11-17 05:59:44 +0000484 case Function:
485 case CXXMethod:
486 case CXXConstructor:
487 case CXXDestructor:
488 case CXXConversion:
Chris Lattner8e097192009-03-27 20:18:19 +0000489 case EnumConstant:
490 case Var:
491 case ImplicitParam:
492 case ParmVar:
Chris Lattner8e097192009-03-27 20:18:19 +0000493 case NonTypeTemplateParm:
494 case ObjCMethod:
Daniel Dunbar45b2d8a2010-04-23 13:07:39 +0000495 case ObjCProperty:
John McCall5e77d762013-04-16 07:28:30 +0000496 case MSProperty:
Daniel Dunbar45b2d8a2010-04-23 13:07:39 +0000497 return IDNS_Ordinary;
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000498 case Label:
499 return IDNS_Label;
Francois Pichet783dd6e2010-11-21 06:08:52 +0000500 case IndirectField:
501 return IDNS_Ordinary | IDNS_Member;
502
John McCalle87beb22010-04-23 18:46:30 +0000503 case ObjCCompatibleAlias:
504 case ObjCInterface:
505 return IDNS_Ordinary | IDNS_Type;
506
507 case Typedef:
Richard Smithdda56e42011-04-15 14:24:37 +0000508 case TypeAlias:
Richard Smith3f1b5d02011-05-05 21:57:07 +0000509 case TypeAliasTemplate:
John McCalle87beb22010-04-23 18:46:30 +0000510 case UnresolvedUsingTypename:
511 case TemplateTypeParm:
512 return IDNS_Ordinary | IDNS_Type;
513
John McCall3f746822009-11-17 05:59:44 +0000514 case UsingShadow:
515 return 0; // we'll actually overwrite this later
516
John McCalle61f2ba2009-11-18 02:36:19 +0000517 case UnresolvedUsingValue:
John McCalle61f2ba2009-11-18 02:36:19 +0000518 return IDNS_Ordinary | IDNS_Using;
John McCall3f746822009-11-17 05:59:44 +0000519
520 case Using:
521 return IDNS_Using;
522
Chris Lattner8e097192009-03-27 20:18:19 +0000523 case ObjCProtocol:
Douglas Gregor79947a22009-04-24 00:11:27 +0000524 return IDNS_ObjCProtocol;
Mike Stump11289f42009-09-09 15:08:12 +0000525
Chris Lattner8e097192009-03-27 20:18:19 +0000526 case Field:
527 case ObjCAtDefsField:
528 case ObjCIvar:
529 return IDNS_Member;
Mike Stump11289f42009-09-09 15:08:12 +0000530
Chris Lattner8e097192009-03-27 20:18:19 +0000531 case Record:
532 case CXXRecord:
533 case Enum:
John McCalle87beb22010-04-23 18:46:30 +0000534 return IDNS_Tag | IDNS_Type;
Mike Stump11289f42009-09-09 15:08:12 +0000535
Chris Lattner8e097192009-03-27 20:18:19 +0000536 case Namespace:
John McCalle87beb22010-04-23 18:46:30 +0000537 case NamespaceAlias:
538 return IDNS_Namespace;
539
Chris Lattner8e097192009-03-27 20:18:19 +0000540 case FunctionTemplate:
John McCalle87beb22010-04-23 18:46:30 +0000541 return IDNS_Ordinary;
542
Chris Lattner8e097192009-03-27 20:18:19 +0000543 case ClassTemplate:
544 case TemplateTemplateParm:
John McCalle87beb22010-04-23 18:46:30 +0000545 return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
Mike Stump11289f42009-09-09 15:08:12 +0000546
Chris Lattner8e097192009-03-27 20:18:19 +0000547 // Never have names.
John McCallaa74a0c2009-08-28 07:59:38 +0000548 case Friend:
John McCall11083da2009-09-16 22:47:08 +0000549 case FriendTemplate:
Abramo Bagnarad7340582010-06-05 05:09:32 +0000550 case AccessSpec:
Chris Lattner8e097192009-03-27 20:18:19 +0000551 case LinkageSpec:
552 case FileScopeAsm:
553 case StaticAssert:
Chris Lattner8e097192009-03-27 20:18:19 +0000554 case ObjCPropertyImpl:
Chris Lattner8e097192009-03-27 20:18:19 +0000555 case Block:
556 case TranslationUnit:
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000557
Chris Lattner8e097192009-03-27 20:18:19 +0000558 case UsingDirective:
559 case ClassTemplateSpecialization:
Douglas Gregor2373c592009-05-31 09:31:02 +0000560 case ClassTemplatePartialSpecialization:
Francois Pichet00c7e6c2011-08-14 03:52:19 +0000561 case ClassScopeFunctionSpecialization:
Douglas Gregore93525e2010-04-22 23:19:50 +0000562 case ObjCImplementation:
563 case ObjCCategory:
564 case ObjCCategoryImpl:
Douglas Gregorba345522011-12-02 23:23:56 +0000565 case Import:
Alexey Bataeva769e072013-03-22 06:34:35 +0000566 case OMPThreadPrivate:
Michael Han84324352013-02-22 17:15:32 +0000567 case Empty:
Douglas Gregore93525e2010-04-22 23:19:50 +0000568 // Never looked up by name.
Chris Lattner8e097192009-03-27 20:18:19 +0000569 return 0;
570 }
John McCall3f746822009-11-17 05:59:44 +0000571
David Blaikiee4d798f2012-01-20 21:50:17 +0000572 llvm_unreachable("Invalid DeclKind!");
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000573}
574
Argyrios Kyrtzidis6f40eb72012-02-09 02:44:08 +0000575void Decl::setAttrsImpl(const AttrVec &attrs, ASTContext &Ctx) {
Argyrios Kyrtzidis91167172010-06-11 23:09:25 +0000576 assert(!HasAttrs && "Decl already contains attrs.");
577
Argyrios Kyrtzidis6f40eb72012-02-09 02:44:08 +0000578 AttrVec &AttrBlank = Ctx.getDeclAttrs(this);
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000579 assert(AttrBlank.empty() && "HasAttrs was wrong?");
Argyrios Kyrtzidis91167172010-06-11 23:09:25 +0000580
581 AttrBlank = attrs;
582 HasAttrs = true;
583}
584
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000585void Decl::dropAttrs() {
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000586 if (!HasAttrs) return;
Mike Stump11289f42009-09-09 15:08:12 +0000587
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000588 HasAttrs = false;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000589 getASTContext().eraseDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000590}
591
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000592const AttrVec &Decl::getAttrs() const {
593 assert(HasAttrs && "No attrs to get!");
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000594 return getASTContext().getDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000595}
596
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000597void Decl::swapAttrs(Decl *RHS) {
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000598 bool HasLHSAttr = this->HasAttrs;
599 bool HasRHSAttr = RHS->HasAttrs;
Mike Stump11289f42009-09-09 15:08:12 +0000600
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000601 // Usually, neither decl has attrs, nothing to do.
602 if (!HasLHSAttr && !HasRHSAttr) return;
Mike Stump11289f42009-09-09 15:08:12 +0000603
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000604 // If 'this' has no attrs, swap the other way.
605 if (!HasLHSAttr)
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000606 return RHS->swapAttrs(this);
Mike Stump11289f42009-09-09 15:08:12 +0000607
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000608 ASTContext &Context = getASTContext();
Mike Stump11289f42009-09-09 15:08:12 +0000609
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000610 // Handle the case when both decls have attrs.
611 if (HasRHSAttr) {
Douglas Gregor78bd61f2009-06-18 16:11:24 +0000612 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000613 return;
614 }
Mike Stump11289f42009-09-09 15:08:12 +0000615
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000616 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor78bd61f2009-06-18 16:11:24 +0000617 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
618 Context.eraseDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000619 this->HasAttrs = false;
620 RHS->HasAttrs = true;
621}
622
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000623Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000624 Decl::Kind DK = D->getDeclKind();
625 switch(DK) {
Alexis Hunted053252010-05-30 07:21:58 +0000626#define DECL(NAME, BASE)
627#define DECL_CONTEXT(NAME) \
628 case Decl::NAME: \
629 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
630#define DECL_CONTEXT_BASE(NAME)
631#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000632 default:
Alexis Hunted053252010-05-30 07:21:58 +0000633#define DECL(NAME, BASE)
634#define DECL_CONTEXT_BASE(NAME) \
635 if (DK >= first##NAME && DK <= last##NAME) \
636 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
637#include "clang/AST/DeclNodes.inc"
David Blaikie83d382b2011-09-23 05:06:16 +0000638 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000639 }
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000640}
641
642DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000643 Decl::Kind DK = D->getKind();
644 switch(DK) {
Alexis Hunted053252010-05-30 07:21:58 +0000645#define DECL(NAME, BASE)
646#define DECL_CONTEXT(NAME) \
647 case Decl::NAME: \
648 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
649#define DECL_CONTEXT_BASE(NAME)
650#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000651 default:
Alexis Hunted053252010-05-30 07:21:58 +0000652#define DECL(NAME, BASE)
653#define DECL_CONTEXT_BASE(NAME) \
654 if (DK >= first##NAME && DK <= last##NAME) \
655 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
656#include "clang/AST/DeclNodes.inc"
David Blaikie83d382b2011-09-23 05:06:16 +0000657 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000658 }
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000659}
660
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000661SourceLocation Decl::getBodyRBrace() const {
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +0000662 // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
663 // FunctionDecl stores EndRangeLoc for this purpose.
664 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
665 const FunctionDecl *Definition;
666 if (FD->hasBody(Definition))
667 return Definition->getSourceRange().getEnd();
668 return SourceLocation();
669 }
670
Argyrios Kyrtzidis6fbc8fa2010-07-07 11:31:27 +0000671 if (Stmt *Body = getBody())
672 return Body->getSourceRange().getEnd();
673
674 return SourceLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +0000675}
676
Anders Carlssona28908d2009-03-25 23:38:06 +0000677void Decl::CheckAccessDeclContext() const {
Douglas Gregor4b00d3b2010-12-02 00:22:25 +0000678#ifndef NDEBUG
John McCall401982f2010-01-20 21:53:11 +0000679 // Suppress this check if any of the following hold:
680 // 1. this is the translation unit (and thus has no parent)
681 // 2. this is a template parameter (and thus doesn't belong to its context)
Argyrios Kyrtzidise1778632010-09-08 21:58:42 +0000682 // 3. this is a non-type template parameter
683 // 4. the context is not a record
684 // 5. it's invalid
685 // 6. it's a C++0x static_assert.
Anders Carlssonadf36b22009-08-29 20:47:47 +0000686 if (isa<TranslationUnitDecl>(this) ||
Argyrios Kyrtzidisa45855f2010-07-02 11:55:44 +0000687 isa<TemplateTypeParmDecl>(this) ||
Argyrios Kyrtzidise1778632010-09-08 21:58:42 +0000688 isa<NonTypeTemplateParmDecl>(this) ||
Douglas Gregor2b76dd92010-02-22 17:53:38 +0000689 !isa<CXXRecordDecl>(getDeclContext()) ||
Argyrios Kyrtzidis260b4a82010-09-08 21:32:35 +0000690 isInvalidDecl() ||
691 isa<StaticAssertDecl>(this) ||
692 // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
693 // as DeclContext (?).
Argyrios Kyrtzidise1778632010-09-08 21:58:42 +0000694 isa<ParmVarDecl>(this) ||
695 // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
696 // AS_none as access specifier.
Francois Pichet09af8c32011-08-17 01:06:54 +0000697 isa<CXXRecordDecl>(this) ||
698 isa<ClassScopeFunctionSpecializationDecl>(this))
Anders Carlssonadf36b22009-08-29 20:47:47 +0000699 return;
Mike Stump11289f42009-09-09 15:08:12 +0000700
701 assert(Access != AS_none &&
Anders Carlssona28908d2009-03-25 23:38:06 +0000702 "Access specifier is AS_none inside a record decl");
Douglas Gregor4b00d3b2010-12-02 00:22:25 +0000703#endif
Anders Carlssona28908d2009-03-25 23:38:06 +0000704}
705
John McCallb67608f2011-02-22 22:25:23 +0000706DeclContext *Decl::getNonClosureContext() {
John McCallfe96e0b2011-11-06 09:01:30 +0000707 return getDeclContext()->getNonClosureAncestor();
708}
709
710DeclContext *DeclContext::getNonClosureAncestor() {
711 DeclContext *DC = this;
John McCallb67608f2011-02-22 22:25:23 +0000712
713 // This is basically "while (DC->isClosure()) DC = DC->getParent();"
714 // except that it's significantly more efficient to cast to a known
715 // decl type and call getDeclContext() than to call getParent().
John McCall63b45fe2011-06-23 21:18:31 +0000716 while (isa<BlockDecl>(DC))
717 DC = cast<BlockDecl>(DC)->getDeclContext();
John McCallb67608f2011-02-22 22:25:23 +0000718
719 assert(!DC->isClosure());
720 return DC;
721}
Anders Carlssona28908d2009-03-25 23:38:06 +0000722
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000723//===----------------------------------------------------------------------===//
724// DeclContext Implementation
725//===----------------------------------------------------------------------===//
726
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000727bool DeclContext::classof(const Decl *D) {
728 switch (D->getKind()) {
Alexis Hunted053252010-05-30 07:21:58 +0000729#define DECL(NAME, BASE)
730#define DECL_CONTEXT(NAME) case Decl::NAME:
731#define DECL_CONTEXT_BASE(NAME)
732#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000733 return true;
734 default:
Alexis Hunted053252010-05-30 07:21:58 +0000735#define DECL(NAME, BASE)
736#define DECL_CONTEXT_BASE(NAME) \
737 if (D->getKind() >= Decl::first##NAME && \
738 D->getKind() <= Decl::last##NAME) \
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000739 return true;
Alexis Hunted053252010-05-30 07:21:58 +0000740#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000741 return false;
742 }
743}
744
Douglas Gregor9c832f72010-07-25 18:38:02 +0000745DeclContext::~DeclContext() { }
Douglas Gregor91f84212008-12-11 16:49:14 +0000746
Douglas Gregor7f737c02009-09-10 16:57:35 +0000747/// \brief Find the parent context of this context that will be
748/// used for unqualified name lookup.
749///
750/// Generally, the parent lookup context is the semantic context. However, for
751/// a friend function the parent lookup context is the lexical context, which
752/// is the class in which the friend is declared.
753DeclContext *DeclContext::getLookupParent() {
754 // FIXME: Find a better way to identify friends
755 if (isa<FunctionDecl>(this))
Sebastian Redl50c68252010-08-31 00:36:30 +0000756 if (getParent()->getRedeclContext()->isFileContext() &&
757 getLexicalParent()->getRedeclContext()->isRecord())
Douglas Gregor7f737c02009-09-10 16:57:35 +0000758 return getLexicalParent();
759
760 return getParent();
761}
762
Sebastian Redlbd595762010-08-31 20:53:31 +0000763bool DeclContext::isInlineNamespace() const {
764 return isNamespace() &&
765 cast<NamespaceDecl>(this)->isInline();
766}
767
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000768bool DeclContext::isDependentContext() const {
769 if (isFileContext())
770 return false;
771
Douglas Gregor2373c592009-05-31 09:31:02 +0000772 if (isa<ClassTemplatePartialSpecializationDecl>(this))
773 return true;
774
Douglas Gregor680e9e02012-02-21 19:11:17 +0000775 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this)) {
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000776 if (Record->getDescribedClassTemplate())
777 return true;
Douglas Gregor680e9e02012-02-21 19:11:17 +0000778
779 if (Record->isDependentLambda())
780 return true;
781 }
782
John McCallc62bb642010-03-24 05:22:00 +0000783 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000784 if (Function->getDescribedFunctionTemplate())
785 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000786
John McCallc62bb642010-03-24 05:22:00 +0000787 // Friend function declarations are dependent if their *lexical*
788 // context is dependent.
789 if (cast<Decl>(this)->getFriendObjectKind())
790 return getLexicalParent()->isDependentContext();
791 }
792
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000793 return getParent() && getParent()->isDependentContext();
794}
795
Douglas Gregor07665a62009-01-05 19:45:36 +0000796bool DeclContext::isTransparentContext() const {
797 if (DeclKind == Decl::Enum)
Douglas Gregor0bf31402010-10-08 23:50:27 +0000798 return !cast<EnumDecl>(this)->isScoped();
Douglas Gregor07665a62009-01-05 19:45:36 +0000799 else if (DeclKind == Decl::LinkageSpec)
800 return true;
Douglas Gregor07665a62009-01-05 19:45:36 +0000801
802 return false;
803}
804
John McCall5fe84122010-10-26 04:59:26 +0000805bool DeclContext::isExternCContext() const {
806 const DeclContext *DC = this;
807 while (DC->DeclKind != Decl::TranslationUnit) {
808 if (DC->DeclKind == Decl::LinkageSpec)
809 return cast<LinkageSpecDecl>(DC)->getLanguage()
810 == LinkageSpecDecl::lang_c;
811 DC = DC->getParent();
812 }
813 return false;
814}
815
Rafael Espindolaf4187652013-02-14 01:18:37 +0000816bool DeclContext::isExternCXXContext() const {
817 const DeclContext *DC = this;
818 while (DC->DeclKind != Decl::TranslationUnit) {
819 if (DC->DeclKind == Decl::LinkageSpec)
820 return cast<LinkageSpecDecl>(DC)->getLanguage()
821 == LinkageSpecDecl::lang_cxx;
822 DC = DC->getParent();
823 }
824 return false;
825}
826
Sebastian Redl50c68252010-08-31 00:36:30 +0000827bool DeclContext::Encloses(const DeclContext *DC) const {
Douglas Gregore985a3b2009-08-27 06:03:53 +0000828 if (getPrimaryContext() != this)
829 return getPrimaryContext()->Encloses(DC);
Mike Stump11289f42009-09-09 15:08:12 +0000830
Douglas Gregore985a3b2009-08-27 06:03:53 +0000831 for (; DC; DC = DC->getParent())
832 if (DC->getPrimaryContext() == this)
833 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000834 return false;
Douglas Gregore985a3b2009-08-27 06:03:53 +0000835}
836
Steve Naroff35c62ae2009-01-08 17:28:14 +0000837DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor91f84212008-12-11 16:49:14 +0000838 switch (DeclKind) {
Douglas Gregor91f84212008-12-11 16:49:14 +0000839 case Decl::TranslationUnit:
Douglas Gregor07665a62009-01-05 19:45:36 +0000840 case Decl::LinkageSpec:
Mike Stump11289f42009-09-09 15:08:12 +0000841 case Decl::Block:
Douglas Gregor91f84212008-12-11 16:49:14 +0000842 // There is only one DeclContext for these entities.
843 return this;
844
845 case Decl::Namespace:
846 // The original namespace is our primary context.
847 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
848
Douglas Gregor91f84212008-12-11 16:49:14 +0000849 case Decl::ObjCMethod:
850 return this;
851
852 case Decl::ObjCInterface:
Douglas Gregor66b310c2011-12-15 18:03:09 +0000853 if (ObjCInterfaceDecl *Def = cast<ObjCInterfaceDecl>(this)->getDefinition())
854 return Def;
855
856 return this;
857
Steve Naroff35c62ae2009-01-08 17:28:14 +0000858 case Decl::ObjCProtocol:
Douglas Gregora715bff2012-01-01 19:51:50 +0000859 if (ObjCProtocolDecl *Def = cast<ObjCProtocolDecl>(this)->getDefinition())
860 return Def;
861
862 return this;
Douglas Gregor66b310c2011-12-15 18:03:09 +0000863
Steve Naroff35c62ae2009-01-08 17:28:14 +0000864 case Decl::ObjCCategory:
Douglas Gregor91f84212008-12-11 16:49:14 +0000865 return this;
866
Steve Naroff35c62ae2009-01-08 17:28:14 +0000867 case Decl::ObjCImplementation:
868 case Decl::ObjCCategoryImpl:
869 return this;
870
Douglas Gregor91f84212008-12-11 16:49:14 +0000871 default:
Alexis Hunted053252010-05-30 07:21:58 +0000872 if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
Douglas Gregor67a65642009-02-17 23:15:12 +0000873 // If this is a tag type that has a definition or is currently
874 // being defined, that definition is our primary context.
John McCalle78aac42010-03-10 03:28:59 +0000875 TagDecl *Tag = cast<TagDecl>(this);
876 assert(isa<TagType>(Tag->TypeForDecl) ||
877 isa<InjectedClassNameType>(Tag->TypeForDecl));
878
879 if (TagDecl *Def = Tag->getDefinition())
880 return Def;
881
882 if (!isa<InjectedClassNameType>(Tag->TypeForDecl)) {
883 const TagType *TagTy = cast<TagType>(Tag->TypeForDecl);
884 if (TagTy->isBeingDefined())
885 // FIXME: is it necessarily being defined in the decl
886 // that owns the type?
887 return TagTy->getDecl();
888 }
889
890 return Tag;
Douglas Gregor67a65642009-02-17 23:15:12 +0000891 }
892
Alexis Hunted053252010-05-30 07:21:58 +0000893 assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
Douglas Gregor91f84212008-12-11 16:49:14 +0000894 "Unknown DeclContext kind");
895 return this;
896 }
897}
898
Douglas Gregore57e7522012-01-07 09:11:48 +0000899void
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000900DeclContext::collectAllContexts(SmallVectorImpl<DeclContext *> &Contexts){
Douglas Gregore57e7522012-01-07 09:11:48 +0000901 Contexts.clear();
902
903 if (DeclKind != Decl::Namespace) {
904 Contexts.push_back(this);
905 return;
Douglas Gregor91f84212008-12-11 16:49:14 +0000906 }
Douglas Gregore57e7522012-01-07 09:11:48 +0000907
908 NamespaceDecl *Self = static_cast<NamespaceDecl *>(this);
Douglas Gregorec9fd132012-01-14 16:38:05 +0000909 for (NamespaceDecl *N = Self->getMostRecentDecl(); N;
910 N = N->getPreviousDecl())
Douglas Gregore57e7522012-01-07 09:11:48 +0000911 Contexts.push_back(N);
912
913 std::reverse(Contexts.begin(), Contexts.end());
Douglas Gregor91f84212008-12-11 16:49:14 +0000914}
915
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000916std::pair<Decl *, Decl *>
Bill Wendling8eb771d2012-02-22 09:51:33 +0000917DeclContext::BuildDeclChain(ArrayRef<Decl*> Decls,
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +0000918 bool FieldsAlreadyLoaded) {
Douglas Gregor781f7132012-01-06 16:59:53 +0000919 // Build up a chain of declarations via the Decl::NextInContextAndBits field.
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000920 Decl *FirstNewDecl = 0;
921 Decl *PrevDecl = 0;
922 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +0000923 if (FieldsAlreadyLoaded && isa<FieldDecl>(Decls[I]))
924 continue;
925
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000926 Decl *D = Decls[I];
927 if (PrevDecl)
Douglas Gregor781f7132012-01-06 16:59:53 +0000928 PrevDecl->NextInContextAndBits.setPointer(D);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000929 else
930 FirstNewDecl = D;
931
932 PrevDecl = D;
933 }
934
935 return std::make_pair(FirstNewDecl, PrevDecl);
936}
937
Richard Smith645d7552013-02-07 03:37:08 +0000938/// \brief We have just acquired external visible storage, and we already have
939/// built a lookup map. For every name in the map, pull in the new names from
940/// the external storage.
941void DeclContext::reconcileExternalVisibleStorage() {
Richard Smith86a12012013-02-11 22:02:16 +0000942 assert(NeedToReconcileExternalVisibleStorage && LookupPtr.getPointer());
Richard Smith645d7552013-02-07 03:37:08 +0000943 NeedToReconcileExternalVisibleStorage = false;
944
945 StoredDeclsMap &Map = *LookupPtr.getPointer();
946 ExternalASTSource *Source = getParentASTContext().getExternalSource();
947 for (StoredDeclsMap::iterator I = Map.begin(); I != Map.end(); ++I) {
948 I->second.removeExternalDecls();
949 Source->FindExternalVisibleDeclsByName(this, I->first);
950 }
951}
952
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000953/// \brief Load the declarations within this lexical storage from an
954/// external source.
Mike Stump11289f42009-09-09 15:08:12 +0000955void
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000956DeclContext::LoadLexicalDeclsFromExternalStorage() const {
957 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000958 assert(hasExternalLexicalStorage() && Source && "No external storage?");
959
Argyrios Kyrtzidis98d045e2010-07-30 10:03:23 +0000960 // Notify that we have a DeclContext that is initializing.
961 ExternalASTSource::Deserializing ADeclContext(Source);
Douglas Gregorf337ae92011-08-26 21:23:06 +0000962
Douglas Gregor3d0adb32011-07-15 21:46:17 +0000963 // Load the external declarations, if any.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000964 SmallVector<Decl*, 64> Decls;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000965 ExternalLexicalStorage = false;
Douglas Gregor3d0adb32011-07-15 21:46:17 +0000966 switch (Source->FindExternalLexicalDecls(this, Decls)) {
967 case ELR_Success:
968 break;
969
970 case ELR_Failure:
971 case ELR_AlreadyLoaded:
972 return;
973 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000974
975 if (Decls.empty())
976 return;
977
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +0000978 // We may have already loaded just the fields of this record, in which case
979 // we need to ignore them.
980 bool FieldsAlreadyLoaded = false;
981 if (const RecordDecl *RD = dyn_cast<RecordDecl>(this))
982 FieldsAlreadyLoaded = RD->LoadedFieldsFromExternalStorage;
983
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000984 // Splice the newly-read declarations into the beginning of the list
985 // of declarations.
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000986 Decl *ExternalFirst, *ExternalLast;
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +0000987 llvm::tie(ExternalFirst, ExternalLast) = BuildDeclChain(Decls,
988 FieldsAlreadyLoaded);
Douglas Gregor781f7132012-01-06 16:59:53 +0000989 ExternalLast->NextInContextAndBits.setPointer(FirstDecl);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000990 FirstDecl = ExternalFirst;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000991 if (!LastDecl)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000992 LastDecl = ExternalLast;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000993}
994
John McCall75b960e2010-06-01 09:23:16 +0000995DeclContext::lookup_result
996ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
997 DeclarationName Name) {
998 ASTContext &Context = DC->getParentASTContext();
999 StoredDeclsMap *Map;
Richard Smithf634c902012-03-16 06:12:59 +00001000 if (!(Map = DC->LookupPtr.getPointer()))
John McCall75b960e2010-06-01 09:23:16 +00001001 Map = DC->CreateStoredDeclsMap(Context);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001002
Richard Smith645d7552013-02-07 03:37:08 +00001003 // Add an entry to the map for this name, if it's not already present.
1004 (*Map)[Name];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001005
John McCall75b960e2010-06-01 09:23:16 +00001006 return DeclContext::lookup_result();
1007}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001008
John McCall75b960e2010-06-01 09:23:16 +00001009DeclContext::lookup_result
1010ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +00001011 DeclarationName Name,
Argyrios Kyrtzidis94d3f9d2011-09-09 06:44:14 +00001012 ArrayRef<NamedDecl*> Decls) {
Dmitri Gribenko76bb5cabfa2012-09-10 21:20:09 +00001013 ASTContext &Context = DC->getParentASTContext();
John McCall75b960e2010-06-01 09:23:16 +00001014 StoredDeclsMap *Map;
Richard Smithf634c902012-03-16 06:12:59 +00001015 if (!(Map = DC->LookupPtr.getPointer()))
John McCall75b960e2010-06-01 09:23:16 +00001016 Map = DC->CreateStoredDeclsMap(Context);
1017
1018 StoredDeclsList &List = (*Map)[Name];
Argyrios Kyrtzidis94d3f9d2011-09-09 06:44:14 +00001019 for (ArrayRef<NamedDecl*>::iterator
1020 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
John McCall75b960e2010-06-01 09:23:16 +00001021 if (List.isNull())
Argyrios Kyrtzidis94d3f9d2011-09-09 06:44:14 +00001022 List.setOnlyValue(*I);
John McCall75b960e2010-06-01 09:23:16 +00001023 else
Richard Smith645d7552013-02-07 03:37:08 +00001024 // FIXME: Need declarationReplaces handling for redeclarations in modules.
Argyrios Kyrtzidis94d3f9d2011-09-09 06:44:14 +00001025 List.AddSubsequentDecl(*I);
John McCall75b960e2010-06-01 09:23:16 +00001026 }
1027
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001028 return List.getLookupResult();
John McCall75b960e2010-06-01 09:23:16 +00001029}
1030
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001031DeclContext::decl_iterator DeclContext::noload_decls_begin() const {
1032 return decl_iterator(FirstDecl);
1033}
1034
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001035DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001036 if (hasExternalLexicalStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001037 LoadLexicalDeclsFromExternalStorage();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001038
Mike Stump11289f42009-09-09 15:08:12 +00001039 return decl_iterator(FirstDecl);
Douglas Gregorbcced4e2009-04-09 21:40:53 +00001040}
1041
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001042bool DeclContext::decls_empty() const {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001043 if (hasExternalLexicalStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001044 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001045
1046 return !FirstDecl;
1047}
1048
John McCall84d87672009-12-10 09:41:52 +00001049void DeclContext::removeDecl(Decl *D) {
1050 assert(D->getLexicalDeclContext() == this &&
1051 "decl being removed from non-lexical context");
Douglas Gregor781f7132012-01-06 16:59:53 +00001052 assert((D->NextInContextAndBits.getPointer() || D == LastDecl) &&
John McCall84d87672009-12-10 09:41:52 +00001053 "decl is not in decls list");
1054
1055 // Remove D from the decl chain. This is O(n) but hopefully rare.
1056 if (D == FirstDecl) {
1057 if (D == LastDecl)
1058 FirstDecl = LastDecl = 0;
1059 else
Douglas Gregor781f7132012-01-06 16:59:53 +00001060 FirstDecl = D->NextInContextAndBits.getPointer();
John McCall84d87672009-12-10 09:41:52 +00001061 } else {
Douglas Gregor781f7132012-01-06 16:59:53 +00001062 for (Decl *I = FirstDecl; true; I = I->NextInContextAndBits.getPointer()) {
John McCall84d87672009-12-10 09:41:52 +00001063 assert(I && "decl not found in linked list");
Douglas Gregor781f7132012-01-06 16:59:53 +00001064 if (I->NextInContextAndBits.getPointer() == D) {
1065 I->NextInContextAndBits.setPointer(D->NextInContextAndBits.getPointer());
John McCall84d87672009-12-10 09:41:52 +00001066 if (D == LastDecl) LastDecl = I;
1067 break;
1068 }
1069 }
1070 }
1071
1072 // Mark that D is no longer in the decl chain.
Douglas Gregor781f7132012-01-06 16:59:53 +00001073 D->NextInContextAndBits.setPointer(0);
John McCall84d87672009-12-10 09:41:52 +00001074
1075 // Remove D from the lookup table if necessary.
1076 if (isa<NamedDecl>(D)) {
1077 NamedDecl *ND = cast<NamedDecl>(D);
1078
Axel Naumanncb2c52f2011-08-26 14:06:12 +00001079 // Remove only decls that have a name
1080 if (!ND->getDeclName()) return;
1081
Richard Smithf634c902012-03-16 06:12:59 +00001082 StoredDeclsMap *Map = getPrimaryContext()->LookupPtr.getPointer();
John McCallc62bb642010-03-24 05:22:00 +00001083 if (!Map) return;
John McCall84d87672009-12-10 09:41:52 +00001084
John McCall84d87672009-12-10 09:41:52 +00001085 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
1086 assert(Pos != Map->end() && "no lookup entry for decl");
Axel Naumannfbc7b982011-11-08 18:21:06 +00001087 if (Pos->second.getAsVector() || Pos->second.getAsDecl() == ND)
1088 Pos->second.remove(ND);
John McCall84d87672009-12-10 09:41:52 +00001089 }
1090}
1091
John McCalld1e9d832009-08-11 06:59:38 +00001092void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner33f219d2009-02-20 00:56:18 +00001093 assert(D->getLexicalDeclContext() == this &&
1094 "Decl inserted into wrong lexical context");
Mike Stump11289f42009-09-09 15:08:12 +00001095 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor020713e2009-01-09 19:42:16 +00001096 "Decl already inserted into a DeclContext");
1097
1098 if (FirstDecl) {
Douglas Gregor781f7132012-01-06 16:59:53 +00001099 LastDecl->NextInContextAndBits.setPointer(D);
Douglas Gregor020713e2009-01-09 19:42:16 +00001100 LastDecl = D;
1101 } else {
1102 FirstDecl = LastDecl = D;
1103 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +00001104
1105 // Notify a C++ record declaration that we've added a member, so it can
1106 // update it's class-specific state.
1107 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
1108 Record->addedMember(D);
Douglas Gregor0f2a3602011-12-03 00:30:27 +00001109
1110 // If this is a newly-created (not de-serialized) import declaration, wire
1111 // it in to the list of local import declarations.
1112 if (!D->isFromASTFile()) {
1113 if (ImportDecl *Import = dyn_cast<ImportDecl>(D))
1114 D->getASTContext().addedLocalImportDecl(Import);
1115 }
John McCalld1e9d832009-08-11 06:59:38 +00001116}
1117
1118void DeclContext::addDecl(Decl *D) {
1119 addHiddenDecl(D);
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001120
1121 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Richard Smithf634c902012-03-16 06:12:59 +00001122 ND->getDeclContext()->getPrimaryContext()->
1123 makeDeclVisibleInContextWithFlags(ND, false, true);
Douglas Gregor91f84212008-12-11 16:49:14 +00001124}
1125
Sean Callanan95e74be2011-10-21 02:57:43 +00001126void DeclContext::addDeclInternal(Decl *D) {
1127 addHiddenDecl(D);
1128
1129 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Richard Smithf634c902012-03-16 06:12:59 +00001130 ND->getDeclContext()->getPrimaryContext()->
1131 makeDeclVisibleInContextWithFlags(ND, true, true);
1132}
1133
1134/// shouldBeHidden - Determine whether a declaration which was declared
1135/// within its semantic context should be invisible to qualified name lookup.
1136static bool shouldBeHidden(NamedDecl *D) {
1137 // Skip unnamed declarations.
1138 if (!D->getDeclName())
1139 return true;
1140
1141 // Skip entities that can't be found by name lookup into a particular
1142 // context.
1143 if ((D->getIdentifierNamespace() == 0 && !isa<UsingDirectiveDecl>(D)) ||
1144 D->isTemplateParameter())
1145 return true;
1146
1147 // Skip template specializations.
1148 // FIXME: This feels like a hack. Should DeclarationName support
1149 // template-ids, or is there a better way to keep specializations
1150 // from being visible?
1151 if (isa<ClassTemplateSpecializationDecl>(D))
1152 return true;
1153 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1154 if (FD->isFunctionTemplateSpecialization())
1155 return true;
1156
1157 return false;
1158}
1159
1160/// buildLookup - Build the lookup data structure with all of the
1161/// declarations in this DeclContext (and any other contexts linked
1162/// to it or transparent contexts nested within it) and return it.
1163StoredDeclsMap *DeclContext::buildLookup() {
1164 assert(this == getPrimaryContext() && "buildLookup called on non-primary DC");
1165
Richard Smith86a12012013-02-11 22:02:16 +00001166 // FIXME: Should we keep going if hasExternalVisibleStorage?
Richard Smithf634c902012-03-16 06:12:59 +00001167 if (!LookupPtr.getInt())
1168 return LookupPtr.getPointer();
1169
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001170 SmallVector<DeclContext *, 2> Contexts;
Richard Smithf634c902012-03-16 06:12:59 +00001171 collectAllContexts(Contexts);
1172 for (unsigned I = 0, N = Contexts.size(); I != N; ++I)
1173 buildLookupImpl(Contexts[I]);
1174
1175 // We no longer have any lazy decls.
1176 LookupPtr.setInt(false);
Richard Smith86a12012013-02-11 22:02:16 +00001177 NeedToReconcileExternalVisibleStorage = false;
Richard Smithf634c902012-03-16 06:12:59 +00001178 return LookupPtr.getPointer();
1179}
1180
1181/// buildLookupImpl - Build part of the lookup data structure for the
1182/// declarations contained within DCtx, which will either be this
1183/// DeclContext, a DeclContext linked to it, or a transparent context
1184/// nested within it.
1185void DeclContext::buildLookupImpl(DeclContext *DCtx) {
1186 for (decl_iterator I = DCtx->decls_begin(), E = DCtx->decls_end();
1187 I != E; ++I) {
1188 Decl *D = *I;
1189
1190 // Insert this declaration into the lookup structure, but only if
1191 // it's semantically within its decl context. Any other decls which
1192 // should be found in this context are added eagerly.
1193 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1194 if (ND->getDeclContext() == DCtx && !shouldBeHidden(ND))
1195 makeDeclVisibleInContextImpl(ND, false);
1196
1197 // If this declaration is itself a transparent declaration context
1198 // or inline namespace, add the members of this declaration of that
1199 // context (recursively).
1200 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(D))
1201 if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
1202 buildLookupImpl(InnerCtx);
1203 }
Sean Callanan95e74be2011-10-21 02:57:43 +00001204}
1205
Mike Stump11289f42009-09-09 15:08:12 +00001206DeclContext::lookup_result
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001207DeclContext::lookup(DeclarationName Name) {
Nick Lewycky2bd636f2012-03-13 04:12:34 +00001208 assert(DeclKind != Decl::LinkageSpec &&
1209 "Should not perform lookups into linkage specs!");
1210
Steve Naroff35c62ae2009-01-08 17:28:14 +00001211 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor91f84212008-12-11 16:49:14 +00001212 if (PrimaryContext != this)
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001213 return PrimaryContext->lookup(Name);
Douglas Gregor91f84212008-12-11 16:49:14 +00001214
Richard Smith05afe5e2012-03-13 03:12:56 +00001215 if (hasExternalVisibleStorage()) {
Richard Smith645d7552013-02-07 03:37:08 +00001216 StoredDeclsMap *Map = LookupPtr.getPointer();
1217 if (LookupPtr.getInt())
1218 Map = buildLookup();
Richard Smith86a12012013-02-11 22:02:16 +00001219 else if (NeedToReconcileExternalVisibleStorage)
1220 reconcileExternalVisibleStorage();
Richard Smith645d7552013-02-07 03:37:08 +00001221
Richard Smith75fc3bf2013-02-08 00:37:45 +00001222 if (!Map)
1223 Map = CreateStoredDeclsMap(getParentASTContext());
1224
Richard Smith645d7552013-02-07 03:37:08 +00001225 // If a PCH/module has a result for this name, and we have a local
1226 // declaration, we will have imported the PCH/module result when adding the
1227 // local declaration or when reconciling the module.
Richard Smith75fc3bf2013-02-08 00:37:45 +00001228 std::pair<StoredDeclsMap::iterator, bool> R =
1229 Map->insert(std::make_pair(Name, StoredDeclsList()));
1230 if (!R.second)
1231 return R.first->second.getLookupResult();
Richard Smithf634c902012-03-16 06:12:59 +00001232
John McCall75b960e2010-06-01 09:23:16 +00001233 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Richard Smith9ce12e32013-02-07 03:30:24 +00001234 if (Source->FindExternalVisibleDeclsByName(this, Name)) {
1235 if (StoredDeclsMap *Map = LookupPtr.getPointer()) {
1236 StoredDeclsMap::iterator I = Map->find(Name);
1237 if (I != Map->end())
1238 return I->second.getLookupResult();
1239 }
1240 }
1241
1242 return lookup_result(lookup_iterator(0), lookup_iterator(0));
John McCall75b960e2010-06-01 09:23:16 +00001243 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001244
Richard Smithf634c902012-03-16 06:12:59 +00001245 StoredDeclsMap *Map = LookupPtr.getPointer();
1246 if (LookupPtr.getInt())
1247 Map = buildLookup();
1248
1249 if (!Map)
1250 return lookup_result(lookup_iterator(0), lookup_iterator(0));
1251
1252 StoredDeclsMap::iterator I = Map->find(Name);
1253 if (I == Map->end())
1254 return lookup_result(lookup_iterator(0), lookup_iterator(0));
1255
1256 return I->second.getLookupResult();
Douglas Gregor91f84212008-12-11 16:49:14 +00001257}
1258
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001259void DeclContext::localUncachedLookup(DeclarationName Name,
1260 SmallVectorImpl<NamedDecl *> &Results) {
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001261 Results.clear();
1262
1263 // If there's no external storage, just perform a normal lookup and copy
1264 // the results.
Douglas Gregordd6006f2012-07-17 21:16:27 +00001265 if (!hasExternalVisibleStorage() && !hasExternalLexicalStorage() && Name) {
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001266 lookup_result LookupResults = lookup(Name);
David Blaikieff7d47a2012-12-19 00:45:41 +00001267 Results.insert(Results.end(), LookupResults.begin(), LookupResults.end());
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001268 return;
1269 }
1270
1271 // If we have a lookup table, check there first. Maybe we'll get lucky.
Richard Smith645d7552013-02-07 03:37:08 +00001272 if (Name && !LookupPtr.getInt()) {
Douglas Gregordd6006f2012-07-17 21:16:27 +00001273 if (StoredDeclsMap *Map = LookupPtr.getPointer()) {
1274 StoredDeclsMap::iterator Pos = Map->find(Name);
1275 if (Pos != Map->end()) {
1276 Results.insert(Results.end(),
David Blaikieff7d47a2012-12-19 00:45:41 +00001277 Pos->second.getLookupResult().begin(),
1278 Pos->second.getLookupResult().end());
Douglas Gregordd6006f2012-07-17 21:16:27 +00001279 return;
1280 }
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001281 }
1282 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00001283
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001284 // Slow case: grovel through the declarations in our chain looking for
1285 // matches.
1286 for (Decl *D = FirstDecl; D; D = D->getNextDeclInContext()) {
1287 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1288 if (ND->getDeclName() == Name)
1289 Results.push_back(ND);
1290 }
1291}
1292
Sebastian Redl50c68252010-08-31 00:36:30 +00001293DeclContext *DeclContext::getRedeclContext() {
Chris Lattner17a1bfa2009-03-27 19:19:59 +00001294 DeclContext *Ctx = this;
Sebastian Redlbd595762010-08-31 20:53:31 +00001295 // Skip through transparent contexts.
1296 while (Ctx->isTransparentContext())
Douglas Gregor6ad0ef52009-01-06 23:51:29 +00001297 Ctx = Ctx->getParent();
1298 return Ctx;
1299}
1300
Douglas Gregorf47b9112009-02-25 22:02:03 +00001301DeclContext *DeclContext::getEnclosingNamespaceContext() {
1302 DeclContext *Ctx = this;
1303 // Skip through non-namespace, non-translation-unit contexts.
Sebastian Redl4f08c962010-08-31 00:36:23 +00001304 while (!Ctx->isFileContext())
Douglas Gregorf47b9112009-02-25 22:02:03 +00001305 Ctx = Ctx->getParent();
1306 return Ctx->getPrimaryContext();
1307}
1308
Sebastian Redl50c68252010-08-31 00:36:30 +00001309bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
1310 // For non-file contexts, this is equivalent to Equals.
1311 if (!isFileContext())
1312 return O->Equals(this);
1313
1314 do {
1315 if (O->Equals(this))
1316 return true;
1317
1318 const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O);
1319 if (!NS || !NS->isInline())
1320 break;
1321 O = NS->getParent();
1322 } while (O);
1323
1324 return false;
1325}
1326
Richard Smithf634c902012-03-16 06:12:59 +00001327void DeclContext::makeDeclVisibleInContext(NamedDecl *D) {
1328 DeclContext *PrimaryDC = this->getPrimaryContext();
1329 DeclContext *DeclDC = D->getDeclContext()->getPrimaryContext();
1330 // If the decl is being added outside of its semantic decl context, we
1331 // need to ensure that we eagerly build the lookup information for it.
1332 PrimaryDC->makeDeclVisibleInContextWithFlags(D, false, PrimaryDC == DeclDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00001333}
1334
Richard Smithf634c902012-03-16 06:12:59 +00001335void DeclContext::makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal,
1336 bool Recoverable) {
1337 assert(this == getPrimaryContext() && "expected a primary DC");
Sean Callanan95e74be2011-10-21 02:57:43 +00001338
Richard Smith05afe5e2012-03-13 03:12:56 +00001339 // Skip declarations within functions.
1340 // FIXME: We shouldn't need to build lookup tables for function declarations
1341 // ever, and we can't do so correctly because we can't model the nesting of
1342 // scopes which occurs within functions. We use "qualified" lookup into
1343 // function declarations when handling friend declarations inside nested
1344 // classes, and consequently accept the following invalid code:
1345 //
1346 // void f() { void g(); { int g; struct S { friend void g(); }; } }
1347 if (isFunctionOrMethod() && !isa<FunctionDecl>(D))
1348 return;
1349
Richard Smithf634c902012-03-16 06:12:59 +00001350 // Skip declarations which should be invisible to name lookup.
1351 if (shouldBeHidden(D))
1352 return;
1353
1354 // If we already have a lookup data structure, perform the insertion into
1355 // it. If we might have externally-stored decls with this name, look them
1356 // up and perform the insertion. If this decl was declared outside its
1357 // semantic context, buildLookup won't add it, so add it now.
1358 //
1359 // FIXME: As a performance hack, don't add such decls into the translation
1360 // unit unless we're in C++, since qualified lookup into the TU is never
1361 // performed.
1362 if (LookupPtr.getPointer() || hasExternalVisibleStorage() ||
1363 ((!Recoverable || D->getDeclContext() != D->getLexicalDeclContext()) &&
1364 (getParentASTContext().getLangOpts().CPlusPlus ||
1365 !isTranslationUnit()))) {
1366 // If we have lazily omitted any decls, they might have the same name as
1367 // the decl which we are adding, so build a full lookup table before adding
1368 // this decl.
1369 buildLookup();
1370 makeDeclVisibleInContextImpl(D, Internal);
1371 } else {
1372 LookupPtr.setInt(true);
1373 }
1374
1375 // If we are a transparent context or inline namespace, insert into our
1376 // parent context, too. This operation is recursive.
1377 if (isTransparentContext() || isInlineNamespace())
1378 getParent()->getPrimaryContext()->
1379 makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
1380
1381 Decl *DCAsDecl = cast<Decl>(this);
1382 // Notify that a decl was made visible unless we are a Tag being defined.
1383 if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined()))
1384 if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
1385 L->AddedVisibleDecl(this, D);
1386}
1387
1388void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal) {
1389 // Find or create the stored declaration map.
1390 StoredDeclsMap *Map = LookupPtr.getPointer();
1391 if (!Map) {
1392 ASTContext *C = &getParentASTContext();
1393 Map = CreateStoredDeclsMap(*C);
Argyrios Kyrtzidise51e5542010-07-04 21:44:25 +00001394 }
1395
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001396 // If there is an external AST source, load any declarations it knows about
1397 // with this declaration's name.
1398 // If the lookup table contains an entry about this name it means that we
1399 // have already checked the external source.
Sean Callanan95e74be2011-10-21 02:57:43 +00001400 if (!Internal)
1401 if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
1402 if (hasExternalVisibleStorage() &&
Richard Smithf634c902012-03-16 06:12:59 +00001403 Map->find(D->getDeclName()) == Map->end())
Sean Callanan95e74be2011-10-21 02:57:43 +00001404 Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001405
Douglas Gregor91f84212008-12-11 16:49:14 +00001406 // Insert this declaration into the map.
Richard Smithf634c902012-03-16 06:12:59 +00001407 StoredDeclsList &DeclNameEntries = (*Map)[D->getDeclName()];
Chris Lattnercaae7162009-02-20 01:44:05 +00001408 if (DeclNameEntries.isNull()) {
1409 DeclNameEntries.setOnlyValue(D);
Richard Smithf634c902012-03-16 06:12:59 +00001410 return;
Douglas Gregor91f84212008-12-11 16:49:14 +00001411 }
Chris Lattner24e24d52009-02-20 00:55:03 +00001412
Richard Smithf634c902012-03-16 06:12:59 +00001413 if (DeclNameEntries.HandleRedeclaration(D)) {
1414 // This declaration has replaced an existing one for which
1415 // declarationReplaces returns true.
1416 return;
1417 }
Mike Stump11289f42009-09-09 15:08:12 +00001418
Richard Smithf634c902012-03-16 06:12:59 +00001419 // Put this declaration into the appropriate slot.
1420 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor91f84212008-12-11 16:49:14 +00001421}
Douglas Gregor889ceb72009-02-03 19:21:40 +00001422
1423/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
1424/// this context.
Mike Stump11289f42009-09-09 15:08:12 +00001425DeclContext::udir_iterator_range
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001426DeclContext::getUsingDirectives() const {
Richard Smith05afe5e2012-03-13 03:12:56 +00001427 // FIXME: Use something more efficient than normal lookup for using
1428 // directives. In C++, using directives are looked up more than anything else.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001429 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
David Blaikieff7d47a2012-12-19 00:45:41 +00001430 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.begin()),
1431 reinterpret_cast<udir_iterator>(Result.end()));
Douglas Gregor889ceb72009-02-03 19:21:40 +00001432}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001433
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001434//===----------------------------------------------------------------------===//
1435// Creation and Destruction of StoredDeclsMaps. //
1436//===----------------------------------------------------------------------===//
1437
John McCallc62bb642010-03-24 05:22:00 +00001438StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
Richard Smithf634c902012-03-16 06:12:59 +00001439 assert(!LookupPtr.getPointer() && "context already has a decls map");
John McCallc62bb642010-03-24 05:22:00 +00001440 assert(getPrimaryContext() == this &&
1441 "creating decls map on non-primary context");
1442
1443 StoredDeclsMap *M;
1444 bool Dependent = isDependentContext();
1445 if (Dependent)
1446 M = new DependentStoredDeclsMap();
1447 else
1448 M = new StoredDeclsMap();
1449 M->Previous = C.LastSDM;
1450 C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
Richard Smithf634c902012-03-16 06:12:59 +00001451 LookupPtr.setPointer(M);
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001452 return M;
1453}
1454
1455void ASTContext::ReleaseDeclContextMaps() {
John McCallc62bb642010-03-24 05:22:00 +00001456 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1457 // pointer because the subclass doesn't add anything that needs to
1458 // be deleted.
John McCallc62bb642010-03-24 05:22:00 +00001459 StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1460}
1461
1462void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1463 while (Map) {
1464 // Advance the iteration before we invalidate memory.
1465 llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1466
1467 if (Dependent)
1468 delete static_cast<DependentStoredDeclsMap*>(Map);
1469 else
1470 delete Map;
1471
1472 Map = Next.getPointer();
1473 Dependent = Next.getInt();
1474 }
1475}
1476
John McCallc62bb642010-03-24 05:22:00 +00001477DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1478 DeclContext *Parent,
1479 const PartialDiagnostic &PDiag) {
1480 assert(Parent->isDependentContext()
1481 && "cannot iterate dependent diagnostics of non-dependent context");
1482 Parent = Parent->getPrimaryContext();
Richard Smithf634c902012-03-16 06:12:59 +00001483 if (!Parent->LookupPtr.getPointer())
John McCallc62bb642010-03-24 05:22:00 +00001484 Parent->CreateStoredDeclsMap(C);
1485
1486 DependentStoredDeclsMap *Map
Richard Smithf634c902012-03-16 06:12:59 +00001487 = static_cast<DependentStoredDeclsMap*>(Parent->LookupPtr.getPointer());
John McCallc62bb642010-03-24 05:22:00 +00001488
Douglas Gregora55530e2010-03-29 23:56:53 +00001489 // Allocate the copy of the PartialDiagnostic via the ASTContext's
Douglas Gregor89336232010-03-29 23:34:08 +00001490 // BumpPtrAllocator, rather than the ASTContext itself.
Douglas Gregora55530e2010-03-29 23:56:53 +00001491 PartialDiagnostic::Storage *DiagStorage = 0;
1492 if (PDiag.hasStorage())
1493 DiagStorage = new (C) PartialDiagnostic::Storage;
1494
1495 DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
John McCallc62bb642010-03-24 05:22:00 +00001496
1497 // TODO: Maybe we shouldn't reverse the order during insertion.
1498 DD->NextDiagnostic = Map->FirstDiagnostic;
1499 Map->FirstDiagnostic = DD;
1500
1501 return DD;
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001502}