blob: 4fcec53d6eb99181bbe86f0410297cc3a5b6f341 [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
Richard Smithf7981722013-11-22 09:01:48 +000048void *Decl::operator new(std::size_t Size, const ASTContext &Context,
49 unsigned ID, std::size_t Extra) {
Douglas Gregor52261fd2012-01-05 23:49:36 +000050 // Allocate an extra 8 bytes worth of storage, which ensures that the
Douglas Gregorcfe7dc62012-01-09 17:30:44 +000051 // resulting pointer will still be 8-byte aligned.
Richard Smithf7981722013-11-22 09:01:48 +000052 void *Start = Context.Allocate(Size + Extra + 8);
Douglas Gregor52261fd2012-01-05 23:49:36 +000053 void *Result = (char*)Start + 8;
Richard Smithf7981722013-11-22 09:01:48 +000054
Douglas Gregorcfe7dc62012-01-09 17:30:44 +000055 unsigned *PrefixPtr = (unsigned *)Result - 2;
Richard Smithf7981722013-11-22 09:01:48 +000056
Douglas Gregorcfe7dc62012-01-09 17:30:44 +000057 // Zero out the first 4 bytes; this is used to store the owning module ID.
58 PrefixPtr[0] = 0;
Richard Smithf7981722013-11-22 09:01:48 +000059
Douglas Gregorcfe7dc62012-01-09 17:30:44 +000060 // Store the global declaration ID in the second 4 bytes.
61 PrefixPtr[1] = ID;
Richard Smithf7981722013-11-22 09:01:48 +000062
Douglas Gregor64af53c2012-01-05 22:27:05 +000063 return Result;
Douglas Gregor72172e92012-01-05 21:55:30 +000064}
65
Richard Smithf7981722013-11-22 09:01:48 +000066void *Decl::operator new(std::size_t Size, const ASTContext &Ctx,
67 DeclContext *Parent, std::size_t Extra) {
68 assert(!Parent || &Parent->getParentASTContext() == &Ctx);
Richard Smith42413142015-05-15 20:05:43 +000069 // With local visibility enabled, we track the owning module even for local
70 // declarations.
71 if (Ctx.getLangOpts().ModulesLocalVisibility) {
72 void *Buffer = ::operator new(sizeof(Module *) + Size + Extra, Ctx);
73 return new (Buffer) Module*(nullptr) + 1;
74 }
Richard Smithf7981722013-11-22 09:01:48 +000075 return ::operator new(Size + Extra, Ctx);
76}
77
Douglas Gregorc147b0b2013-01-12 01:29:50 +000078Module *Decl::getOwningModuleSlow() const {
79 assert(isFromASTFile() && "Not from AST file?");
80 return getASTContext().getExternalSource()->getModule(getOwningModuleID());
81}
82
Richard Smith87bb5692015-06-09 00:35:49 +000083bool Decl::hasLocalOwningModuleStorage() const {
84 return getASTContext().getLangOpts().ModulesLocalVisibility;
85}
86
Eli Friedman7dbab8a2008-06-07 16:52:53 +000087const char *Decl::getDeclKindName() const {
88 switch (DeclKind) {
David Blaikie83d382b2011-09-23 05:06:16 +000089 default: llvm_unreachable("Declaration not in DeclNodes.inc!");
Alexis Hunted053252010-05-30 07:21:58 +000090#define DECL(DERIVED, BASE) case DERIVED: return #DERIVED;
91#define ABSTRACT_DECL(DECL)
92#include "clang/AST/DeclNodes.inc"
Eli Friedman7dbab8a2008-06-07 16:52:53 +000093 }
94}
95
Douglas Gregor90d47172010-03-05 00:26:45 +000096void Decl::setInvalidDecl(bool Invalid) {
97 InvalidDecl = Invalid;
Alp Tokera5d64592013-12-21 01:10:54 +000098 assert(!isa<TagDecl>(this) || !cast<TagDecl>(this)->isCompleteDefinition());
Argyrios Kyrtzidisb7d1ca22012-03-09 21:09:04 +000099 if (Invalid && !isa<ParmVarDecl>(this)) {
Douglas Gregor90d47172010-03-05 00:26:45 +0000100 // Defensive maneuver for ill-formed code: we're likely not to make it to
101 // a point where we set the access specifier, so default it to "public"
102 // to avoid triggering asserts elsewhere in the front end.
103 setAccess(AS_public);
104 }
105}
106
Steve Naroff5faaef72009-01-20 19:53:53 +0000107const char *DeclContext::getDeclKindName() const {
108 switch (DeclKind) {
David Blaikie83d382b2011-09-23 05:06:16 +0000109 default: llvm_unreachable("Declaration context not in DeclNodes.inc!");
Alexis Hunted053252010-05-30 07:21:58 +0000110#define DECL(DERIVED, BASE) case Decl::DERIVED: return #DERIVED;
111#define ABSTRACT_DECL(DECL)
112#include "clang/AST/DeclNodes.inc"
Steve Naroff5faaef72009-01-20 19:53:53 +0000113 }
114}
115
Daniel Dunbar62905572012-03-05 21:42:49 +0000116bool Decl::StatisticsEnabled = false;
117void Decl::EnableStatistics() {
118 StatisticsEnabled = true;
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000119}
120
121void Decl::PrintStats() {
Chandler Carruthbfb154a2011-07-04 06:13:27 +0000122 llvm::errs() << "\n*** Decl Stats:\n";
Mike Stump11289f42009-09-09 15:08:12 +0000123
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +0000124 int totalDecls = 0;
Alexis Hunted053252010-05-30 07:21:58 +0000125#define DECL(DERIVED, BASE) totalDecls += n##DERIVED##s;
126#define ABSTRACT_DECL(DECL)
127#include "clang/AST/DeclNodes.inc"
Chandler Carruthbfb154a2011-07-04 06:13:27 +0000128 llvm::errs() << " " << totalDecls << " decls total.\n";
Mike Stump11289f42009-09-09 15:08:12 +0000129
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +0000130 int totalBytes = 0;
Alexis Hunted053252010-05-30 07:21:58 +0000131#define DECL(DERIVED, BASE) \
132 if (n##DERIVED##s > 0) { \
133 totalBytes += (int)(n##DERIVED##s * sizeof(DERIVED##Decl)); \
Chandler Carruthbfb154a2011-07-04 06:13:27 +0000134 llvm::errs() << " " << n##DERIVED##s << " " #DERIVED " decls, " \
135 << sizeof(DERIVED##Decl) << " each (" \
136 << n##DERIVED##s * sizeof(DERIVED##Decl) \
137 << " bytes)\n"; \
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +0000138 }
Alexis Hunted053252010-05-30 07:21:58 +0000139#define ABSTRACT_DECL(DECL)
140#include "clang/AST/DeclNodes.inc"
Mike Stump11289f42009-09-09 15:08:12 +0000141
Chandler Carruthbfb154a2011-07-04 06:13:27 +0000142 llvm::errs() << "Total bytes = " << totalBytes << "\n";
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000143}
144
Alexis Hunted053252010-05-30 07:21:58 +0000145void Decl::add(Kind k) {
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000146 switch (k) {
Alexis Hunted053252010-05-30 07:21:58 +0000147#define DECL(DERIVED, BASE) case DERIVED: ++n##DERIVED##s; break;
148#define ABSTRACT_DECL(DECL)
149#include "clang/AST/DeclNodes.inc"
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000150 }
151}
152
Anders Carlssonaa73b912009-06-13 00:08:58 +0000153bool Decl::isTemplateParameterPack() const {
154 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
155 return TTP->isParameterPack();
Douglas Gregorda3cc0d2010-12-23 23:51:58 +0000156 if (const NonTypeTemplateParmDecl *NTTP
Douglas Gregorf5500772011-01-05 15:48:55 +0000157 = dyn_cast<NonTypeTemplateParmDecl>(this))
Douglas Gregorda3cc0d2010-12-23 23:51:58 +0000158 return NTTP->isParameterPack();
Douglas Gregorf5500772011-01-05 15:48:55 +0000159 if (const TemplateTemplateParmDecl *TTP
160 = dyn_cast<TemplateTemplateParmDecl>(this))
161 return TTP->isParameterPack();
Anders Carlssonaa73b912009-06-13 00:08:58 +0000162 return false;
163}
164
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +0000165bool Decl::isParameterPack() const {
166 if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(this))
167 return Parm->isParameterPack();
168
169 return isTemplateParameterPack();
170}
171
Alp Tokera2794f92014-01-22 07:29:52 +0000172FunctionDecl *Decl::getAsFunction() {
173 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
174 return FD;
175 if (const FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(this))
176 return FTD->getTemplatedDecl();
Craig Topper36250ad2014-05-12 05:36:57 +0000177 return nullptr;
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000178}
179
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000180bool Decl::isTemplateDecl() const {
181 return isa<TemplateDecl>(this);
182}
183
Argyrios Kyrtzidis0ce4c9a2011-09-28 02:45:33 +0000184const DeclContext *Decl::getParentFunctionOrMethod() const {
185 for (const DeclContext *DC = getDeclContext();
186 DC && !DC->isTranslationUnit() && !DC->isNamespace();
Douglas Gregorf5974fa2010-01-16 20:21:20 +0000187 DC = DC->getParent())
188 if (DC->isFunctionOrMethod())
Argyrios Kyrtzidis0ce4c9a2011-09-28 02:45:33 +0000189 return DC;
Douglas Gregorf5974fa2010-01-16 20:21:20 +0000190
Craig Topper36250ad2014-05-12 05:36:57 +0000191 return nullptr;
Douglas Gregorf5974fa2010-01-16 20:21:20 +0000192}
193
Douglas Gregor133eddd2011-02-17 08:47:29 +0000194
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000195//===----------------------------------------------------------------------===//
Chris Lattnereae6cb62009-03-05 08:00:35 +0000196// PrettyStackTraceDecl Implementation
197//===----------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000198
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000199void PrettyStackTraceDecl::print(raw_ostream &OS) const {
Chris Lattnereae6cb62009-03-05 08:00:35 +0000200 SourceLocation TheLoc = Loc;
201 if (TheLoc.isInvalid() && TheDecl)
202 TheLoc = TheDecl->getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000203
Chris Lattnereae6cb62009-03-05 08:00:35 +0000204 if (TheLoc.isValid()) {
205 TheLoc.print(OS, SM);
206 OS << ": ";
207 }
208
209 OS << Message;
210
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +0000211 if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl)) {
212 OS << " '";
213 DN->printQualifiedName(OS);
214 OS << '\'';
215 }
Chris Lattnereae6cb62009-03-05 08:00:35 +0000216 OS << '\n';
217}
Mike Stump11289f42009-09-09 15:08:12 +0000218
Chris Lattnereae6cb62009-03-05 08:00:35 +0000219//===----------------------------------------------------------------------===//
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000220// Decl Implementation
221//===----------------------------------------------------------------------===//
222
Douglas Gregorb11aad82011-02-19 18:51:44 +0000223// Out-of-line virtual method providing a home for Decl.
224Decl::~Decl() { }
Douglas Gregora43942a2011-02-17 07:02:32 +0000225
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000226void Decl::setDeclContext(DeclContext *DC) {
Chris Lattnerb81eb052009-03-29 06:06:59 +0000227 DeclCtx = DC;
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000228}
229
230void Decl::setLexicalDeclContext(DeclContext *DC) {
231 if (DC == getLexicalDeclContext())
232 return;
233
234 if (isInSemaDC()) {
Argyrios Kyrtzidis6f40eb72012-02-09 02:44:08 +0000235 setDeclContextsImpl(getDeclContext(), DC, getASTContext());
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000236 } else {
237 getMultipleDC()->LexicalDC = DC;
238 }
Richard Smith5327b892015-07-01 19:32:54 +0000239 Hidden = cast<Decl>(DC)->Hidden;
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000240}
241
Argyrios Kyrtzidis6f40eb72012-02-09 02:44:08 +0000242void Decl::setDeclContextsImpl(DeclContext *SemaDC, DeclContext *LexicalDC,
243 ASTContext &Ctx) {
244 if (SemaDC == LexicalDC) {
245 DeclCtx = SemaDC;
246 } else {
247 Decl::MultipleDC *MDC = new (Ctx) Decl::MultipleDC();
248 MDC->SemanticDC = SemaDC;
249 MDC->LexicalDC = LexicalDC;
250 DeclCtx = MDC;
251 }
252}
253
John McCall4fa53422009-10-01 00:25:31 +0000254bool Decl::isInAnonymousNamespace() const {
255 const DeclContext *DC = getDeclContext();
256 do {
257 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
258 if (ND->isAnonymousNamespace())
259 return true;
260 } while ((DC = DC->getParent()));
261
262 return false;
263}
264
Richard Trieuc771d5d2014-05-28 02:16:01 +0000265bool Decl::isInStdNamespace() const {
266 return getDeclContext()->isStdNamespace();
267}
268
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000269TranslationUnitDecl *Decl::getTranslationUnitDecl() {
Argyrios Kyrtzidis4e1a72b2009-06-30 02:34:53 +0000270 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
271 return TUD;
272
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000273 DeclContext *DC = getDeclContext();
274 assert(DC && "This decl is not contained in a translation unit!");
Mike Stump11289f42009-09-09 15:08:12 +0000275
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000276 while (!DC->isTranslationUnit()) {
277 DC = DC->getParent();
278 assert(DC && "This decl is not contained in a translation unit!");
279 }
Mike Stump11289f42009-09-09 15:08:12 +0000280
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000281 return cast<TranslationUnitDecl>(DC);
282}
283
284ASTContext &Decl::getASTContext() const {
Mike Stump11289f42009-09-09 15:08:12 +0000285 return getTranslationUnitDecl()->getASTContext();
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000286}
287
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +0000288ASTMutationListener *Decl::getASTMutationListener() const {
289 return getASTContext().getASTMutationListener();
290}
291
Benjamin Kramerea70eb32012-12-01 15:09:41 +0000292unsigned Decl::getMaxAlignment() const {
293 if (!hasAttrs())
294 return 0;
295
296 unsigned Align = 0;
297 const AttrVec &V = getAttrs();
298 ASTContext &Ctx = getASTContext();
299 specific_attr_iterator<AlignedAttr> I(V.begin()), E(V.end());
300 for (; I != E; ++I)
301 Align = std::max(Align, I->getAlignment(Ctx));
302 return Align;
303}
304
Douglas Gregorebada0772010-06-17 23:14:26 +0000305bool Decl::isUsed(bool CheckUsedAttr) const {
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000306 if (Used)
307 return true;
308
309 // Check for used attribute.
Douglas Gregorebada0772010-06-17 23:14:26 +0000310 if (CheckUsedAttr && hasAttr<UsedAttr>())
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000311 return true;
Rafael Espindola99e3bfb2012-11-23 16:26:30 +0000312
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000313 return false;
314}
315
Eli Friedman276dd182013-09-05 00:02:25 +0000316void Decl::markUsed(ASTContext &C) {
317 if (Used)
318 return;
319
320 if (C.getASTMutationListener())
321 C.getASTMutationListener()->DeclarationMarkedUsed(this);
322
323 Used = true;
324}
325
Argyrios Kyrtzidis16180232011-04-19 19:51:10 +0000326bool Decl::isReferenced() const {
327 if (Referenced)
328 return true;
329
330 // Check redeclarations.
Aaron Ballman86c93902014-03-06 23:45:36 +0000331 for (auto I : redecls())
Argyrios Kyrtzidis16180232011-04-19 19:51:10 +0000332 if (I->Referenced)
333 return true;
334
335 return false;
336}
337
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000338/// \brief Determine the availability of the given declaration based on
339/// the target platform.
340///
341/// When it returns an availability result other than \c AR_Available,
342/// if the \p Message parameter is non-NULL, it will be set to a
343/// string describing why the entity is unavailable.
344///
345/// FIXME: Make these strings localizable, since they end up in
346/// diagnostics.
347static AvailabilityResult CheckAvailability(ASTContext &Context,
348 const AvailabilityAttr *A,
349 std::string *Message) {
Bob Wilsonb111ec92015-03-02 19:01:14 +0000350 VersionTuple TargetMinVersion =
351 Context.getTargetInfo().getPlatformMinVersion();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000352
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000353 if (TargetMinVersion.empty())
354 return AR_Available;
355
Bob Wilsonb111ec92015-03-02 19:01:14 +0000356 // Check if this is an App Extension "platform", and if so chop off
357 // the suffix for matching with the actual platform.
358 StringRef ActualPlatform = A->getPlatform()->getName();
359 StringRef RealizedPlatform = ActualPlatform;
360 if (Context.getLangOpts().AppExt) {
361 size_t suffix = RealizedPlatform.rfind("_app_extension");
362 if (suffix != StringRef::npos)
363 RealizedPlatform = RealizedPlatform.slice(0, suffix);
364 }
365
366 StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
367
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000368 // Match the platform name.
Bob Wilsonb111ec92015-03-02 19:01:14 +0000369 if (RealizedPlatform != TargetPlatform)
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000370 return AR_Available;
Bob Wilsonb111ec92015-03-02 19:01:14 +0000371
372 StringRef PrettyPlatformName
373 = AvailabilityAttr::getPrettyPlatformName(ActualPlatform);
374
375 if (PrettyPlatformName.empty())
376 PrettyPlatformName = ActualPlatform;
377
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000378 std::string HintMessage;
379 if (!A->getMessage().empty()) {
380 HintMessage = " - ";
381 HintMessage += A->getMessage();
382 }
383
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000384 // Make sure that this declaration has not been marked 'unavailable'.
385 if (A->getUnavailable()) {
386 if (Message) {
387 Message->clear();
388 llvm::raw_string_ostream Out(*Message);
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000389 Out << "not available on " << PrettyPlatformName
390 << HintMessage;
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000391 }
392
393 return AR_Unavailable;
394 }
395
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000396 // Make sure that this declaration has already been introduced.
397 if (!A->getIntroduced().empty() &&
398 TargetMinVersion < A->getIntroduced()) {
399 if (Message) {
400 Message->clear();
401 llvm::raw_string_ostream Out(*Message);
Fariborz Jahanian2618dba2014-10-06 16:46:02 +0000402 VersionTuple VTI(A->getIntroduced());
403 VTI.UseDotAsSeparator();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000404 Out << "introduced in " << PrettyPlatformName << ' '
Fariborz Jahanian2618dba2014-10-06 16:46:02 +0000405 << VTI << HintMessage;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000406 }
407
408 return AR_NotYetIntroduced;
409 }
410
411 // Make sure that this declaration hasn't been obsoleted.
412 if (!A->getObsoleted().empty() && TargetMinVersion >= A->getObsoleted()) {
413 if (Message) {
414 Message->clear();
415 llvm::raw_string_ostream Out(*Message);
Fariborz Jahanian2618dba2014-10-06 16:46:02 +0000416 VersionTuple VTO(A->getObsoleted());
417 VTO.UseDotAsSeparator();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000418 Out << "obsoleted in " << PrettyPlatformName << ' '
Fariborz Jahanian2618dba2014-10-06 16:46:02 +0000419 << VTO << HintMessage;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000420 }
421
422 return AR_Unavailable;
423 }
424
425 // Make sure that this declaration hasn't been deprecated.
426 if (!A->getDeprecated().empty() && TargetMinVersion >= A->getDeprecated()) {
427 if (Message) {
428 Message->clear();
429 llvm::raw_string_ostream Out(*Message);
Fariborz Jahanian2618dba2014-10-06 16:46:02 +0000430 VersionTuple VTD(A->getDeprecated());
431 VTD.UseDotAsSeparator();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000432 Out << "first deprecated in " << PrettyPlatformName << ' '
Fariborz Jahanian2618dba2014-10-06 16:46:02 +0000433 << VTD << HintMessage;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000434 }
435
436 return AR_Deprecated;
437 }
438
439 return AR_Available;
440}
441
442AvailabilityResult Decl::getAvailability(std::string *Message) const {
443 AvailabilityResult Result = AR_Available;
444 std::string ResultMessage;
445
Aaron Ballmanb97112e2014-03-08 22:19:01 +0000446 for (const auto *A : attrs()) {
447 if (const auto *Deprecated = dyn_cast<DeprecatedAttr>(A)) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000448 if (Result >= AR_Deprecated)
449 continue;
450
451 if (Message)
452 ResultMessage = Deprecated->getMessage();
453
454 Result = AR_Deprecated;
455 continue;
456 }
457
Aaron Ballmanb97112e2014-03-08 22:19:01 +0000458 if (const auto *Unavailable = dyn_cast<UnavailableAttr>(A)) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000459 if (Message)
460 *Message = Unavailable->getMessage();
461 return AR_Unavailable;
462 }
463
Aaron Ballmanb97112e2014-03-08 22:19:01 +0000464 if (const auto *Availability = dyn_cast<AvailabilityAttr>(A)) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000465 AvailabilityResult AR = CheckAvailability(getASTContext(), Availability,
466 Message);
467
468 if (AR == AR_Unavailable)
469 return AR_Unavailable;
470
471 if (AR > Result) {
472 Result = AR;
473 if (Message)
474 ResultMessage.swap(*Message);
475 }
476 continue;
477 }
478 }
479
480 if (Message)
481 Message->swap(ResultMessage);
482 return Result;
483}
484
485bool Decl::canBeWeakImported(bool &IsDefinition) const {
486 IsDefinition = false;
John McCall5fb5df92012-06-20 06:18:46 +0000487
488 // Variables, if they aren't definitions.
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000489 if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000490 if (Var->isThisDeclarationADefinition()) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000491 IsDefinition = true;
492 return false;
493 }
John McCall5fb5df92012-06-20 06:18:46 +0000494 return true;
495
496 // Functions, if they aren't definitions.
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000497 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
498 if (FD->hasBody()) {
499 IsDefinition = true;
500 return false;
501 }
John McCall5fb5df92012-06-20 06:18:46 +0000502 return true;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000503
John McCall5fb5df92012-06-20 06:18:46 +0000504 // Objective-C classes, if this is the non-fragile runtime.
505 } else if (isa<ObjCInterfaceDecl>(this) &&
John McCall18ac1632012-06-20 21:58:02 +0000506 getASTContext().getLangOpts().ObjCRuntime.hasWeakClassImport()) {
John McCall5fb5df92012-06-20 06:18:46 +0000507 return true;
508
509 // Nothing else.
510 } else {
511 return false;
512 }
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000513}
514
515bool Decl::isWeakImported() const {
516 bool IsDefinition;
517 if (!canBeWeakImported(IsDefinition))
518 return false;
519
Aaron Ballmanb97112e2014-03-08 22:19:01 +0000520 for (const auto *A : attrs()) {
521 if (isa<WeakImportAttr>(A))
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000522 return true;
523
Aaron Ballmanb97112e2014-03-08 22:19:01 +0000524 if (const auto *Availability = dyn_cast<AvailabilityAttr>(A)) {
Craig Topper36250ad2014-05-12 05:36:57 +0000525 if (CheckAvailability(getASTContext(), Availability,
526 nullptr) == AR_NotYetIntroduced)
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000527 return true;
528 }
529 }
530
531 return false;
532}
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000533
Chris Lattner8e097192009-03-27 20:18:19 +0000534unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
535 switch (DeclKind) {
John McCall3f746822009-11-17 05:59:44 +0000536 case Function:
537 case CXXMethod:
538 case CXXConstructor:
539 case CXXDestructor:
540 case CXXConversion:
Chris Lattner8e097192009-03-27 20:18:19 +0000541 case EnumConstant:
542 case Var:
543 case ImplicitParam:
544 case ParmVar:
Chris Lattner8e097192009-03-27 20:18:19 +0000545 case NonTypeTemplateParm:
546 case ObjCMethod:
Daniel Dunbar45b2d8a2010-04-23 13:07:39 +0000547 case ObjCProperty:
John McCall5e77d762013-04-16 07:28:30 +0000548 case MSProperty:
Daniel Dunbar45b2d8a2010-04-23 13:07:39 +0000549 return IDNS_Ordinary;
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000550 case Label:
551 return IDNS_Label;
Francois Pichet783dd6e2010-11-21 06:08:52 +0000552 case IndirectField:
553 return IDNS_Ordinary | IDNS_Member;
554
John McCalle87beb22010-04-23 18:46:30 +0000555 case ObjCCompatibleAlias:
556 case ObjCInterface:
557 return IDNS_Ordinary | IDNS_Type;
558
559 case Typedef:
Richard Smithdda56e42011-04-15 14:24:37 +0000560 case TypeAlias:
Richard Smith3f1b5d02011-05-05 21:57:07 +0000561 case TypeAliasTemplate:
John McCalle87beb22010-04-23 18:46:30 +0000562 case UnresolvedUsingTypename:
563 case TemplateTypeParm:
Douglas Gregor85f3f952015-07-07 03:57:15 +0000564 case ObjCTypeParam:
John McCalle87beb22010-04-23 18:46:30 +0000565 return IDNS_Ordinary | IDNS_Type;
566
John McCall3f746822009-11-17 05:59:44 +0000567 case UsingShadow:
568 return 0; // we'll actually overwrite this later
569
John McCalle61f2ba2009-11-18 02:36:19 +0000570 case UnresolvedUsingValue:
John McCalle61f2ba2009-11-18 02:36:19 +0000571 return IDNS_Ordinary | IDNS_Using;
John McCall3f746822009-11-17 05:59:44 +0000572
573 case Using:
574 return IDNS_Using;
575
Chris Lattner8e097192009-03-27 20:18:19 +0000576 case ObjCProtocol:
Douglas Gregor79947a22009-04-24 00:11:27 +0000577 return IDNS_ObjCProtocol;
Mike Stump11289f42009-09-09 15:08:12 +0000578
Chris Lattner8e097192009-03-27 20:18:19 +0000579 case Field:
580 case ObjCAtDefsField:
581 case ObjCIvar:
582 return IDNS_Member;
Mike Stump11289f42009-09-09 15:08:12 +0000583
Chris Lattner8e097192009-03-27 20:18:19 +0000584 case Record:
585 case CXXRecord:
586 case Enum:
John McCalle87beb22010-04-23 18:46:30 +0000587 return IDNS_Tag | IDNS_Type;
Mike Stump11289f42009-09-09 15:08:12 +0000588
Chris Lattner8e097192009-03-27 20:18:19 +0000589 case Namespace:
John McCalle87beb22010-04-23 18:46:30 +0000590 case NamespaceAlias:
591 return IDNS_Namespace;
592
Chris Lattner8e097192009-03-27 20:18:19 +0000593 case FunctionTemplate:
Larisse Voufo39a1e502013-08-06 01:03:05 +0000594 case VarTemplate:
John McCalle87beb22010-04-23 18:46:30 +0000595 return IDNS_Ordinary;
596
Chris Lattner8e097192009-03-27 20:18:19 +0000597 case ClassTemplate:
598 case TemplateTemplateParm:
John McCalle87beb22010-04-23 18:46:30 +0000599 return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
Mike Stump11289f42009-09-09 15:08:12 +0000600
Chris Lattner8e097192009-03-27 20:18:19 +0000601 // Never have names.
John McCallaa74a0c2009-08-28 07:59:38 +0000602 case Friend:
John McCall11083da2009-09-16 22:47:08 +0000603 case FriendTemplate:
Abramo Bagnarad7340582010-06-05 05:09:32 +0000604 case AccessSpec:
Chris Lattner8e097192009-03-27 20:18:19 +0000605 case LinkageSpec:
606 case FileScopeAsm:
607 case StaticAssert:
Chris Lattner8e097192009-03-27 20:18:19 +0000608 case ObjCPropertyImpl:
Chris Lattner8e097192009-03-27 20:18:19 +0000609 case Block:
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000610 case Captured:
Chris Lattner8e097192009-03-27 20:18:19 +0000611 case TranslationUnit:
Richard Smithf19e1272015-03-07 00:04:49 +0000612 case ExternCContext:
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000613
Chris Lattner8e097192009-03-27 20:18:19 +0000614 case UsingDirective:
615 case ClassTemplateSpecialization:
Douglas Gregor2373c592009-05-31 09:31:02 +0000616 case ClassTemplatePartialSpecialization:
Francois Pichet00c7e6c2011-08-14 03:52:19 +0000617 case ClassScopeFunctionSpecialization:
Larisse Voufo39a1e502013-08-06 01:03:05 +0000618 case VarTemplateSpecialization:
619 case VarTemplatePartialSpecialization:
Douglas Gregore93525e2010-04-22 23:19:50 +0000620 case ObjCImplementation:
621 case ObjCCategory:
622 case ObjCCategoryImpl:
Douglas Gregorba345522011-12-02 23:23:56 +0000623 case Import:
Alexey Bataeva769e072013-03-22 06:34:35 +0000624 case OMPThreadPrivate:
Michael Han84324352013-02-22 17:15:32 +0000625 case Empty:
Douglas Gregore93525e2010-04-22 23:19:50 +0000626 // Never looked up by name.
Chris Lattner8e097192009-03-27 20:18:19 +0000627 return 0;
628 }
John McCall3f746822009-11-17 05:59:44 +0000629
David Blaikiee4d798f2012-01-20 21:50:17 +0000630 llvm_unreachable("Invalid DeclKind!");
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000631}
632
Argyrios Kyrtzidis6f40eb72012-02-09 02:44:08 +0000633void Decl::setAttrsImpl(const AttrVec &attrs, ASTContext &Ctx) {
Argyrios Kyrtzidis91167172010-06-11 23:09:25 +0000634 assert(!HasAttrs && "Decl already contains attrs.");
635
Argyrios Kyrtzidis6f40eb72012-02-09 02:44:08 +0000636 AttrVec &AttrBlank = Ctx.getDeclAttrs(this);
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000637 assert(AttrBlank.empty() && "HasAttrs was wrong?");
Argyrios Kyrtzidis91167172010-06-11 23:09:25 +0000638
639 AttrBlank = attrs;
640 HasAttrs = true;
641}
642
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000643void Decl::dropAttrs() {
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000644 if (!HasAttrs) return;
Mike Stump11289f42009-09-09 15:08:12 +0000645
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000646 HasAttrs = false;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000647 getASTContext().eraseDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000648}
649
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000650const AttrVec &Decl::getAttrs() const {
651 assert(HasAttrs && "No attrs to get!");
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000652 return getASTContext().getDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000653}
654
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000655Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000656 Decl::Kind DK = D->getDeclKind();
657 switch(DK) {
Alexis Hunted053252010-05-30 07:21:58 +0000658#define DECL(NAME, BASE)
659#define DECL_CONTEXT(NAME) \
660 case Decl::NAME: \
661 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
662#define DECL_CONTEXT_BASE(NAME)
663#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000664 default:
Alexis Hunted053252010-05-30 07:21:58 +0000665#define DECL(NAME, BASE)
666#define DECL_CONTEXT_BASE(NAME) \
667 if (DK >= first##NAME && DK <= last##NAME) \
668 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
669#include "clang/AST/DeclNodes.inc"
David Blaikie83d382b2011-09-23 05:06:16 +0000670 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000671 }
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000672}
673
674DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000675 Decl::Kind DK = D->getKind();
676 switch(DK) {
Alexis Hunted053252010-05-30 07:21:58 +0000677#define DECL(NAME, BASE)
678#define DECL_CONTEXT(NAME) \
679 case Decl::NAME: \
680 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
681#define DECL_CONTEXT_BASE(NAME)
682#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000683 default:
Alexis Hunted053252010-05-30 07:21:58 +0000684#define DECL(NAME, BASE)
685#define DECL_CONTEXT_BASE(NAME) \
686 if (DK >= first##NAME && DK <= last##NAME) \
687 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
688#include "clang/AST/DeclNodes.inc"
David Blaikie83d382b2011-09-23 05:06:16 +0000689 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000690 }
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000691}
692
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000693SourceLocation Decl::getBodyRBrace() const {
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +0000694 // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
695 // FunctionDecl stores EndRangeLoc for this purpose.
696 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
697 const FunctionDecl *Definition;
698 if (FD->hasBody(Definition))
699 return Definition->getSourceRange().getEnd();
700 return SourceLocation();
701 }
702
Argyrios Kyrtzidis6fbc8fa2010-07-07 11:31:27 +0000703 if (Stmt *Body = getBody())
704 return Body->getSourceRange().getEnd();
705
706 return SourceLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +0000707}
708
Alp Tokerc1086762013-12-07 13:51:35 +0000709bool Decl::AccessDeclContextSanity() const {
Douglas Gregor4b00d3b2010-12-02 00:22:25 +0000710#ifndef NDEBUG
John McCall401982f2010-01-20 21:53:11 +0000711 // Suppress this check if any of the following hold:
712 // 1. this is the translation unit (and thus has no parent)
713 // 2. this is a template parameter (and thus doesn't belong to its context)
Argyrios Kyrtzidise1778632010-09-08 21:58:42 +0000714 // 3. this is a non-type template parameter
715 // 4. the context is not a record
716 // 5. it's invalid
717 // 6. it's a C++0x static_assert.
Anders Carlssonadf36b22009-08-29 20:47:47 +0000718 if (isa<TranslationUnitDecl>(this) ||
Argyrios Kyrtzidisa45855f2010-07-02 11:55:44 +0000719 isa<TemplateTypeParmDecl>(this) ||
Argyrios Kyrtzidise1778632010-09-08 21:58:42 +0000720 isa<NonTypeTemplateParmDecl>(this) ||
Douglas Gregor2b76dd92010-02-22 17:53:38 +0000721 !isa<CXXRecordDecl>(getDeclContext()) ||
Argyrios Kyrtzidis260b4a82010-09-08 21:32:35 +0000722 isInvalidDecl() ||
723 isa<StaticAssertDecl>(this) ||
724 // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
725 // as DeclContext (?).
Argyrios Kyrtzidise1778632010-09-08 21:58:42 +0000726 isa<ParmVarDecl>(this) ||
727 // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
728 // AS_none as access specifier.
Francois Pichet09af8c32011-08-17 01:06:54 +0000729 isa<CXXRecordDecl>(this) ||
730 isa<ClassScopeFunctionSpecializationDecl>(this))
Alp Tokerc1086762013-12-07 13:51:35 +0000731 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000732
733 assert(Access != AS_none &&
Anders Carlssona28908d2009-03-25 23:38:06 +0000734 "Access specifier is AS_none inside a record decl");
Douglas Gregor4b00d3b2010-12-02 00:22:25 +0000735#endif
Alp Tokerc1086762013-12-07 13:51:35 +0000736 return true;
Anders Carlssona28908d2009-03-25 23:38:06 +0000737}
738
John McCalldec348f72013-05-03 07:33:41 +0000739static Decl::Kind getKind(const Decl *D) { return D->getKind(); }
740static Decl::Kind getKind(const DeclContext *DC) { return DC->getDeclKind(); }
741
Aaron Ballman12b9f652014-01-16 13:55:42 +0000742const FunctionType *Decl::getFunctionType(bool BlocksToo) const {
743 QualType Ty;
744 if (const ValueDecl *D = dyn_cast<ValueDecl>(this))
745 Ty = D->getType();
746 else if (const TypedefNameDecl *D = dyn_cast<TypedefNameDecl>(this))
747 Ty = D->getUnderlyingType();
748 else
Craig Topper36250ad2014-05-12 05:36:57 +0000749 return nullptr;
Aaron Ballman12b9f652014-01-16 13:55:42 +0000750
751 if (Ty->isFunctionPointerType())
752 Ty = Ty->getAs<PointerType>()->getPointeeType();
753 else if (BlocksToo && Ty->isBlockPointerType())
754 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
755
756 return Ty->getAs<FunctionType>();
757}
758
759
John McCalldec348f72013-05-03 07:33:41 +0000760/// Starting at a given context (a Decl or DeclContext), look for a
761/// code context that is not a closure (a lambda, block, etc.).
762template <class T> static Decl *getNonClosureContext(T *D) {
763 if (getKind(D) == Decl::CXXMethod) {
764 CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
John McCall55c0cee2013-05-03 17:11:14 +0000765 if (MD->getOverloadedOperator() == OO_Call &&
766 MD->getParent()->isLambda())
John McCalldec348f72013-05-03 07:33:41 +0000767 return getNonClosureContext(MD->getParent()->getParent());
768 return MD;
769 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
770 return FD;
771 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
772 return MD;
773 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
774 return getNonClosureContext(BD->getParent());
775 } else if (CapturedDecl *CD = dyn_cast<CapturedDecl>(D)) {
776 return getNonClosureContext(CD->getParent());
777 } else {
Craig Topper36250ad2014-05-12 05:36:57 +0000778 return nullptr;
John McCalldec348f72013-05-03 07:33:41 +0000779 }
John McCallfe96e0b2011-11-06 09:01:30 +0000780}
781
John McCalldec348f72013-05-03 07:33:41 +0000782Decl *Decl::getNonClosureContext() {
783 return ::getNonClosureContext(this);
784}
John McCallb67608f2011-02-22 22:25:23 +0000785
John McCalldec348f72013-05-03 07:33:41 +0000786Decl *DeclContext::getNonClosureAncestor() {
787 return ::getNonClosureContext(this);
John McCallb67608f2011-02-22 22:25:23 +0000788}
Anders Carlssona28908d2009-03-25 23:38:06 +0000789
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000790//===----------------------------------------------------------------------===//
791// DeclContext Implementation
792//===----------------------------------------------------------------------===//
793
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000794bool DeclContext::classof(const Decl *D) {
795 switch (D->getKind()) {
Alexis Hunted053252010-05-30 07:21:58 +0000796#define DECL(NAME, BASE)
797#define DECL_CONTEXT(NAME) case Decl::NAME:
798#define DECL_CONTEXT_BASE(NAME)
799#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000800 return true;
801 default:
Alexis Hunted053252010-05-30 07:21:58 +0000802#define DECL(NAME, BASE)
803#define DECL_CONTEXT_BASE(NAME) \
804 if (D->getKind() >= Decl::first##NAME && \
805 D->getKind() <= Decl::last##NAME) \
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000806 return true;
Alexis Hunted053252010-05-30 07:21:58 +0000807#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000808 return false;
809 }
810}
811
Douglas Gregor9c832f72010-07-25 18:38:02 +0000812DeclContext::~DeclContext() { }
Douglas Gregor91f84212008-12-11 16:49:14 +0000813
Douglas Gregor7f737c02009-09-10 16:57:35 +0000814/// \brief Find the parent context of this context that will be
815/// used for unqualified name lookup.
816///
817/// Generally, the parent lookup context is the semantic context. However, for
818/// a friend function the parent lookup context is the lexical context, which
819/// is the class in which the friend is declared.
820DeclContext *DeclContext::getLookupParent() {
821 // FIXME: Find a better way to identify friends
822 if (isa<FunctionDecl>(this))
Sebastian Redl50c68252010-08-31 00:36:30 +0000823 if (getParent()->getRedeclContext()->isFileContext() &&
824 getLexicalParent()->getRedeclContext()->isRecord())
Douglas Gregor7f737c02009-09-10 16:57:35 +0000825 return getLexicalParent();
826
827 return getParent();
828}
829
Sebastian Redlbd595762010-08-31 20:53:31 +0000830bool DeclContext::isInlineNamespace() const {
831 return isNamespace() &&
832 cast<NamespaceDecl>(this)->isInline();
833}
834
Richard Trieuc771d5d2014-05-28 02:16:01 +0000835bool DeclContext::isStdNamespace() const {
836 if (!isNamespace())
837 return false;
838
839 const NamespaceDecl *ND = cast<NamespaceDecl>(this);
840 if (ND->isInline()) {
841 return ND->getParent()->isStdNamespace();
842 }
843
844 if (!getParent()->getRedeclContext()->isTranslationUnit())
845 return false;
846
847 const IdentifierInfo *II = ND->getIdentifier();
848 return II && II->isStr("std");
849}
850
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000851bool DeclContext::isDependentContext() const {
852 if (isFileContext())
853 return false;
854
Douglas Gregor2373c592009-05-31 09:31:02 +0000855 if (isa<ClassTemplatePartialSpecializationDecl>(this))
856 return true;
857
Douglas Gregor680e9e02012-02-21 19:11:17 +0000858 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this)) {
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000859 if (Record->getDescribedClassTemplate())
860 return true;
Douglas Gregor680e9e02012-02-21 19:11:17 +0000861
862 if (Record->isDependentLambda())
863 return true;
864 }
865
John McCallc62bb642010-03-24 05:22:00 +0000866 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000867 if (Function->getDescribedFunctionTemplate())
868 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000869
John McCallc62bb642010-03-24 05:22:00 +0000870 // Friend function declarations are dependent if their *lexical*
871 // context is dependent.
872 if (cast<Decl>(this)->getFriendObjectKind())
873 return getLexicalParent()->isDependentContext();
874 }
875
Richard Smith2b560572015-02-07 03:11:11 +0000876 // FIXME: A variable template is a dependent context, but is not a
877 // DeclContext. A context within it (such as a lambda-expression)
878 // should be considered dependent.
879
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000880 return getParent() && getParent()->isDependentContext();
881}
882
Douglas Gregor07665a62009-01-05 19:45:36 +0000883bool DeclContext::isTransparentContext() const {
884 if (DeclKind == Decl::Enum)
Douglas Gregor0bf31402010-10-08 23:50:27 +0000885 return !cast<EnumDecl>(this)->isScoped();
Douglas Gregor07665a62009-01-05 19:45:36 +0000886 else if (DeclKind == Decl::LinkageSpec)
887 return true;
Douglas Gregor07665a62009-01-05 19:45:36 +0000888
889 return false;
890}
891
Serge Pavlov3cb80222013-11-14 02:13:03 +0000892static bool isLinkageSpecContext(const DeclContext *DC,
893 LinkageSpecDecl::LanguageIDs ID) {
894 while (DC->getDeclKind() != Decl::TranslationUnit) {
895 if (DC->getDeclKind() == Decl::LinkageSpec)
896 return cast<LinkageSpecDecl>(DC)->getLanguage() == ID;
Richard Smithac9c9a02014-04-14 20:23:58 +0000897 DC = DC->getLexicalParent();
Serge Pavlov3cb80222013-11-14 02:13:03 +0000898 }
899 return false;
900}
901
902bool DeclContext::isExternCContext() const {
903 return isLinkageSpecContext(this, clang::LinkageSpecDecl::lang_c);
904}
905
906bool DeclContext::isExternCXXContext() const {
907 return isLinkageSpecContext(this, clang::LinkageSpecDecl::lang_cxx);
908}
909
Sebastian Redl50c68252010-08-31 00:36:30 +0000910bool DeclContext::Encloses(const DeclContext *DC) const {
Douglas Gregore985a3b2009-08-27 06:03:53 +0000911 if (getPrimaryContext() != this)
912 return getPrimaryContext()->Encloses(DC);
Mike Stump11289f42009-09-09 15:08:12 +0000913
Douglas Gregore985a3b2009-08-27 06:03:53 +0000914 for (; DC; DC = DC->getParent())
915 if (DC->getPrimaryContext() == this)
916 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000917 return false;
Douglas Gregore985a3b2009-08-27 06:03:53 +0000918}
919
Steve Naroff35c62ae2009-01-08 17:28:14 +0000920DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor91f84212008-12-11 16:49:14 +0000921 switch (DeclKind) {
Douglas Gregor91f84212008-12-11 16:49:14 +0000922 case Decl::TranslationUnit:
Richard Smithf19e1272015-03-07 00:04:49 +0000923 case Decl::ExternCContext:
Douglas Gregor07665a62009-01-05 19:45:36 +0000924 case Decl::LinkageSpec:
Mike Stump11289f42009-09-09 15:08:12 +0000925 case Decl::Block:
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000926 case Decl::Captured:
Douglas Gregor91f84212008-12-11 16:49:14 +0000927 // There is only one DeclContext for these entities.
928 return this;
929
930 case Decl::Namespace:
931 // The original namespace is our primary context.
932 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
933
Douglas Gregor91f84212008-12-11 16:49:14 +0000934 case Decl::ObjCMethod:
935 return this;
936
937 case Decl::ObjCInterface:
Douglas Gregor66b310c2011-12-15 18:03:09 +0000938 if (ObjCInterfaceDecl *Def = cast<ObjCInterfaceDecl>(this)->getDefinition())
939 return Def;
940
941 return this;
942
Steve Naroff35c62ae2009-01-08 17:28:14 +0000943 case Decl::ObjCProtocol:
Douglas Gregora715bff2012-01-01 19:51:50 +0000944 if (ObjCProtocolDecl *Def = cast<ObjCProtocolDecl>(this)->getDefinition())
945 return Def;
946
947 return this;
Douglas Gregor66b310c2011-12-15 18:03:09 +0000948
Steve Naroff35c62ae2009-01-08 17:28:14 +0000949 case Decl::ObjCCategory:
Douglas Gregor91f84212008-12-11 16:49:14 +0000950 return this;
951
Steve Naroff35c62ae2009-01-08 17:28:14 +0000952 case Decl::ObjCImplementation:
953 case Decl::ObjCCategoryImpl:
954 return this;
955
Douglas Gregor91f84212008-12-11 16:49:14 +0000956 default:
Alexis Hunted053252010-05-30 07:21:58 +0000957 if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
Douglas Gregor67a65642009-02-17 23:15:12 +0000958 // If this is a tag type that has a definition or is currently
959 // being defined, that definition is our primary context.
John McCalle78aac42010-03-10 03:28:59 +0000960 TagDecl *Tag = cast<TagDecl>(this);
John McCalle78aac42010-03-10 03:28:59 +0000961
962 if (TagDecl *Def = Tag->getDefinition())
963 return Def;
964
Richard Smith5b21db82014-04-23 18:20:42 +0000965 if (const TagType *TagTy = dyn_cast<TagType>(Tag->getTypeForDecl())) {
966 // Note, TagType::getDecl returns the (partial) definition one exists.
967 TagDecl *PossiblePartialDef = TagTy->getDecl();
968 if (PossiblePartialDef->isBeingDefined())
969 return PossiblePartialDef;
970 } else {
971 assert(isa<InjectedClassNameType>(Tag->getTypeForDecl()));
John McCalle78aac42010-03-10 03:28:59 +0000972 }
973
974 return Tag;
Douglas Gregor67a65642009-02-17 23:15:12 +0000975 }
976
Alexis Hunted053252010-05-30 07:21:58 +0000977 assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
Douglas Gregor91f84212008-12-11 16:49:14 +0000978 "Unknown DeclContext kind");
979 return this;
980 }
981}
982
Douglas Gregore57e7522012-01-07 09:11:48 +0000983void
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000984DeclContext::collectAllContexts(SmallVectorImpl<DeclContext *> &Contexts){
Douglas Gregore57e7522012-01-07 09:11:48 +0000985 Contexts.clear();
986
987 if (DeclKind != Decl::Namespace) {
988 Contexts.push_back(this);
989 return;
Douglas Gregor91f84212008-12-11 16:49:14 +0000990 }
Douglas Gregore57e7522012-01-07 09:11:48 +0000991
992 NamespaceDecl *Self = static_cast<NamespaceDecl *>(this);
Douglas Gregorec9fd132012-01-14 16:38:05 +0000993 for (NamespaceDecl *N = Self->getMostRecentDecl(); N;
994 N = N->getPreviousDecl())
Douglas Gregore57e7522012-01-07 09:11:48 +0000995 Contexts.push_back(N);
996
997 std::reverse(Contexts.begin(), Contexts.end());
Douglas Gregor91f84212008-12-11 16:49:14 +0000998}
999
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001000std::pair<Decl *, Decl *>
Bill Wendling8eb771d2012-02-22 09:51:33 +00001001DeclContext::BuildDeclChain(ArrayRef<Decl*> Decls,
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +00001002 bool FieldsAlreadyLoaded) {
Douglas Gregor781f7132012-01-06 16:59:53 +00001003 // Build up a chain of declarations via the Decl::NextInContextAndBits field.
Craig Topper36250ad2014-05-12 05:36:57 +00001004 Decl *FirstNewDecl = nullptr;
1005 Decl *PrevDecl = nullptr;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001006 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +00001007 if (FieldsAlreadyLoaded && isa<FieldDecl>(Decls[I]))
1008 continue;
1009
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001010 Decl *D = Decls[I];
1011 if (PrevDecl)
Douglas Gregor781f7132012-01-06 16:59:53 +00001012 PrevDecl->NextInContextAndBits.setPointer(D);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001013 else
1014 FirstNewDecl = D;
1015
1016 PrevDecl = D;
1017 }
1018
1019 return std::make_pair(FirstNewDecl, PrevDecl);
1020}
1021
Richard Smith645d7552013-02-07 03:37:08 +00001022/// \brief We have just acquired external visible storage, and we already have
1023/// built a lookup map. For every name in the map, pull in the new names from
1024/// the external storage.
Richard Smitha8b74592014-03-25 00:34:21 +00001025void DeclContext::reconcileExternalVisibleStorage() const {
Richard Smith9e2341d2015-03-23 03:25:59 +00001026 assert(NeedToReconcileExternalVisibleStorage && LookupPtr);
Richard Smith645d7552013-02-07 03:37:08 +00001027 NeedToReconcileExternalVisibleStorage = false;
1028
Richard Smith9e2341d2015-03-23 03:25:59 +00001029 for (auto &Lookup : *LookupPtr)
Richard Smitha8b74592014-03-25 00:34:21 +00001030 Lookup.second.setHasExternalDecls();
Richard Smith645d7552013-02-07 03:37:08 +00001031}
1032
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001033/// \brief Load the declarations within this lexical storage from an
1034/// external source.
Richard Smith18b380b2015-03-24 02:44:20 +00001035/// \return \c true if any declarations were added.
1036bool
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001037DeclContext::LoadLexicalDeclsFromExternalStorage() const {
1038 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001039 assert(hasExternalLexicalStorage() && Source && "No external storage?");
1040
Argyrios Kyrtzidis98d045e2010-07-30 10:03:23 +00001041 // Notify that we have a DeclContext that is initializing.
1042 ExternalASTSource::Deserializing ADeclContext(Source);
Richard Smith9e2341d2015-03-23 03:25:59 +00001043
Douglas Gregor3d0adb32011-07-15 21:46:17 +00001044 // Load the external declarations, if any.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001045 SmallVector<Decl*, 64> Decls;
Richard Smith18b380b2015-03-24 02:44:20 +00001046 ExternalLexicalStorage = false;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00001047 switch (Source->FindExternalLexicalDecls(this, Decls)) {
1048 case ELR_Success:
1049 break;
1050
1051 case ELR_Failure:
1052 case ELR_AlreadyLoaded:
Richard Smith18b380b2015-03-24 02:44:20 +00001053 return false;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00001054 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001055
1056 if (Decls.empty())
Richard Smith18b380b2015-03-24 02:44:20 +00001057 return false;
Richard Smith9e2341d2015-03-23 03:25:59 +00001058
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +00001059 // We may have already loaded just the fields of this record, in which case
1060 // we need to ignore them.
1061 bool FieldsAlreadyLoaded = false;
1062 if (const RecordDecl *RD = dyn_cast<RecordDecl>(this))
1063 FieldsAlreadyLoaded = RD->LoadedFieldsFromExternalStorage;
1064
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001065 // Splice the newly-read declarations into the beginning of the list
1066 // of declarations.
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001067 Decl *ExternalFirst, *ExternalLast;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00001068 std::tie(ExternalFirst, ExternalLast) =
1069 BuildDeclChain(Decls, FieldsAlreadyLoaded);
Douglas Gregor781f7132012-01-06 16:59:53 +00001070 ExternalLast->NextInContextAndBits.setPointer(FirstDecl);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001071 FirstDecl = ExternalFirst;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001072 if (!LastDecl)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001073 LastDecl = ExternalLast;
Richard Smith18b380b2015-03-24 02:44:20 +00001074 return true;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001075}
1076
John McCall75b960e2010-06-01 09:23:16 +00001077DeclContext::lookup_result
1078ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
1079 DeclarationName Name) {
1080 ASTContext &Context = DC->getParentASTContext();
1081 StoredDeclsMap *Map;
Richard Smith9e2341d2015-03-23 03:25:59 +00001082 if (!(Map = DC->LookupPtr))
John McCall75b960e2010-06-01 09:23:16 +00001083 Map = DC->CreateStoredDeclsMap(Context);
Richard Smitha8b74592014-03-25 00:34:21 +00001084 if (DC->NeedToReconcileExternalVisibleStorage)
1085 DC->reconcileExternalVisibleStorage();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001086
Richard Smith4abe0a82013-09-09 07:34:56 +00001087 (*Map)[Name].removeExternalDecls();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001088
John McCall75b960e2010-06-01 09:23:16 +00001089 return DeclContext::lookup_result();
1090}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001091
John McCall75b960e2010-06-01 09:23:16 +00001092DeclContext::lookup_result
1093ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +00001094 DeclarationName Name,
Argyrios Kyrtzidis94d3f9d2011-09-09 06:44:14 +00001095 ArrayRef<NamedDecl*> Decls) {
Dmitri Gribenko76bb5cabfa2012-09-10 21:20:09 +00001096 ASTContext &Context = DC->getParentASTContext();
John McCall75b960e2010-06-01 09:23:16 +00001097 StoredDeclsMap *Map;
Richard Smith9e2341d2015-03-23 03:25:59 +00001098 if (!(Map = DC->LookupPtr))
John McCall75b960e2010-06-01 09:23:16 +00001099 Map = DC->CreateStoredDeclsMap(Context);
Richard Smitha8b74592014-03-25 00:34:21 +00001100 if (DC->NeedToReconcileExternalVisibleStorage)
1101 DC->reconcileExternalVisibleStorage();
John McCall75b960e2010-06-01 09:23:16 +00001102
1103 StoredDeclsList &List = (*Map)[Name];
Richard Smith51445cd2013-06-24 01:46:41 +00001104
1105 // Clear out any old external visible declarations, to avoid quadratic
1106 // performance in the redeclaration checks below.
1107 List.removeExternalDecls();
1108
1109 if (!List.isNull()) {
1110 // We have both existing declarations and new declarations for this name.
1111 // Some of the declarations may simply replace existing ones. Handle those
1112 // first.
1113 llvm::SmallVector<unsigned, 8> Skip;
1114 for (unsigned I = 0, N = Decls.size(); I != N; ++I)
Richard Smithe8292b12015-02-10 03:28:10 +00001115 if (List.HandleRedeclaration(Decls[I], /*IsKnownNewer*/false))
Richard Smith51445cd2013-06-24 01:46:41 +00001116 Skip.push_back(I);
1117 Skip.push_back(Decls.size());
1118
1119 // Add in any new declarations.
1120 unsigned SkipPos = 0;
1121 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
1122 if (I == Skip[SkipPos])
1123 ++SkipPos;
1124 else
1125 List.AddSubsequentDecl(Decls[I]);
1126 }
1127 } else {
1128 // Convert the array to a StoredDeclsList.
1129 for (ArrayRef<NamedDecl*>::iterator
1130 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
1131 if (List.isNull())
1132 List.setOnlyValue(*I);
1133 else
1134 List.AddSubsequentDecl(*I);
1135 }
John McCall75b960e2010-06-01 09:23:16 +00001136 }
1137
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001138 return List.getLookupResult();
John McCall75b960e2010-06-01 09:23:16 +00001139}
1140
Aaron Ballmanda634f12014-03-07 22:17:20 +00001141DeclContext::decl_iterator DeclContext::decls_begin() const {
1142 if (hasExternalLexicalStorage())
1143 LoadLexicalDeclsFromExternalStorage();
1144 return decl_iterator(FirstDecl);
1145}
1146
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001147bool DeclContext::decls_empty() const {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001148 if (hasExternalLexicalStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001149 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001150
1151 return !FirstDecl;
1152}
1153
Sean Callanan0325fb82013-05-04 02:04:27 +00001154bool DeclContext::containsDecl(Decl *D) const {
1155 return (D->getLexicalDeclContext() == this &&
1156 (D->NextInContextAndBits.getPointer() || D == LastDecl));
1157}
1158
John McCall84d87672009-12-10 09:41:52 +00001159void DeclContext::removeDecl(Decl *D) {
1160 assert(D->getLexicalDeclContext() == this &&
1161 "decl being removed from non-lexical context");
Douglas Gregor781f7132012-01-06 16:59:53 +00001162 assert((D->NextInContextAndBits.getPointer() || D == LastDecl) &&
John McCall84d87672009-12-10 09:41:52 +00001163 "decl is not in decls list");
1164
1165 // Remove D from the decl chain. This is O(n) but hopefully rare.
1166 if (D == FirstDecl) {
1167 if (D == LastDecl)
Craig Topper36250ad2014-05-12 05:36:57 +00001168 FirstDecl = LastDecl = nullptr;
John McCall84d87672009-12-10 09:41:52 +00001169 else
Douglas Gregor781f7132012-01-06 16:59:53 +00001170 FirstDecl = D->NextInContextAndBits.getPointer();
John McCall84d87672009-12-10 09:41:52 +00001171 } else {
Douglas Gregor781f7132012-01-06 16:59:53 +00001172 for (Decl *I = FirstDecl; true; I = I->NextInContextAndBits.getPointer()) {
John McCall84d87672009-12-10 09:41:52 +00001173 assert(I && "decl not found in linked list");
Douglas Gregor781f7132012-01-06 16:59:53 +00001174 if (I->NextInContextAndBits.getPointer() == D) {
1175 I->NextInContextAndBits.setPointer(D->NextInContextAndBits.getPointer());
John McCall84d87672009-12-10 09:41:52 +00001176 if (D == LastDecl) LastDecl = I;
1177 break;
1178 }
1179 }
1180 }
1181
1182 // Mark that D is no longer in the decl chain.
Craig Topper36250ad2014-05-12 05:36:57 +00001183 D->NextInContextAndBits.setPointer(nullptr);
John McCall84d87672009-12-10 09:41:52 +00001184
1185 // Remove D from the lookup table if necessary.
1186 if (isa<NamedDecl>(D)) {
1187 NamedDecl *ND = cast<NamedDecl>(D);
1188
Axel Naumanncb2c52f2011-08-26 14:06:12 +00001189 // Remove only decls that have a name
1190 if (!ND->getDeclName()) return;
1191
Richard Smith9e2341d2015-03-23 03:25:59 +00001192 StoredDeclsMap *Map = getPrimaryContext()->LookupPtr;
John McCallc62bb642010-03-24 05:22:00 +00001193 if (!Map) return;
John McCall84d87672009-12-10 09:41:52 +00001194
John McCall84d87672009-12-10 09:41:52 +00001195 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
1196 assert(Pos != Map->end() && "no lookup entry for decl");
Axel Naumannfbc7b982011-11-08 18:21:06 +00001197 if (Pos->second.getAsVector() || Pos->second.getAsDecl() == ND)
1198 Pos->second.remove(ND);
John McCall84d87672009-12-10 09:41:52 +00001199 }
1200}
1201
John McCalld1e9d832009-08-11 06:59:38 +00001202void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner33f219d2009-02-20 00:56:18 +00001203 assert(D->getLexicalDeclContext() == this &&
1204 "Decl inserted into wrong lexical context");
Mike Stump11289f42009-09-09 15:08:12 +00001205 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor020713e2009-01-09 19:42:16 +00001206 "Decl already inserted into a DeclContext");
1207
1208 if (FirstDecl) {
Douglas Gregor781f7132012-01-06 16:59:53 +00001209 LastDecl->NextInContextAndBits.setPointer(D);
Douglas Gregor020713e2009-01-09 19:42:16 +00001210 LastDecl = D;
1211 } else {
1212 FirstDecl = LastDecl = D;
1213 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +00001214
1215 // Notify a C++ record declaration that we've added a member, so it can
1216 // update it's class-specific state.
1217 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
1218 Record->addedMember(D);
Douglas Gregor0f2a3602011-12-03 00:30:27 +00001219
1220 // If this is a newly-created (not de-serialized) import declaration, wire
1221 // it in to the list of local import declarations.
1222 if (!D->isFromASTFile()) {
1223 if (ImportDecl *Import = dyn_cast<ImportDecl>(D))
1224 D->getASTContext().addedLocalImportDecl(Import);
1225 }
John McCalld1e9d832009-08-11 06:59:38 +00001226}
1227
1228void DeclContext::addDecl(Decl *D) {
1229 addHiddenDecl(D);
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001230
1231 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Richard Smithf634c902012-03-16 06:12:59 +00001232 ND->getDeclContext()->getPrimaryContext()->
1233 makeDeclVisibleInContextWithFlags(ND, false, true);
Douglas Gregor91f84212008-12-11 16:49:14 +00001234}
1235
Sean Callanan95e74be2011-10-21 02:57:43 +00001236void DeclContext::addDeclInternal(Decl *D) {
1237 addHiddenDecl(D);
1238
1239 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Richard Smithf634c902012-03-16 06:12:59 +00001240 ND->getDeclContext()->getPrimaryContext()->
1241 makeDeclVisibleInContextWithFlags(ND, true, true);
1242}
1243
1244/// shouldBeHidden - Determine whether a declaration which was declared
1245/// within its semantic context should be invisible to qualified name lookup.
1246static bool shouldBeHidden(NamedDecl *D) {
1247 // Skip unnamed declarations.
1248 if (!D->getDeclName())
1249 return true;
1250
1251 // Skip entities that can't be found by name lookup into a particular
1252 // context.
1253 if ((D->getIdentifierNamespace() == 0 && !isa<UsingDirectiveDecl>(D)) ||
1254 D->isTemplateParameter())
1255 return true;
1256
1257 // Skip template specializations.
1258 // FIXME: This feels like a hack. Should DeclarationName support
1259 // template-ids, or is there a better way to keep specializations
1260 // from being visible?
1261 if (isa<ClassTemplateSpecializationDecl>(D))
1262 return true;
1263 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1264 if (FD->isFunctionTemplateSpecialization())
1265 return true;
1266
1267 return false;
1268}
1269
1270/// buildLookup - Build the lookup data structure with all of the
1271/// declarations in this DeclContext (and any other contexts linked
1272/// to it or transparent contexts nested within it) and return it.
Richard Smitha8b74592014-03-25 00:34:21 +00001273///
1274/// Note that the produced map may miss out declarations from an
1275/// external source. If it does, those entries will be marked with
1276/// the 'hasExternalDecls' flag.
Richard Smithf634c902012-03-16 06:12:59 +00001277StoredDeclsMap *DeclContext::buildLookup() {
1278 assert(this == getPrimaryContext() && "buildLookup called on non-primary DC");
1279
Richard Smith9e2341d2015-03-23 03:25:59 +00001280 if (!HasLazyLocalLexicalLookups && !HasLazyExternalLexicalLookups)
1281 return LookupPtr;
1282
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001283 SmallVector<DeclContext *, 2> Contexts;
Richard Smithf634c902012-03-16 06:12:59 +00001284 collectAllContexts(Contexts);
Richard Smith18b380b2015-03-24 02:44:20 +00001285
1286 if (HasLazyExternalLexicalLookups) {
1287 HasLazyExternalLexicalLookups = false;
1288 for (auto *DC : Contexts) {
1289 if (DC->hasExternalLexicalStorage())
1290 HasLazyLocalLexicalLookups |=
1291 DC->LoadLexicalDeclsFromExternalStorage();
1292 }
1293
1294 if (!HasLazyLocalLexicalLookups)
1295 return LookupPtr;
1296 }
1297
1298 for (auto *DC : Contexts)
1299 buildLookupImpl(DC, hasExternalVisibleStorage());
Richard Smithf634c902012-03-16 06:12:59 +00001300
1301 // We no longer have any lazy decls.
Richard Smith9e2341d2015-03-23 03:25:59 +00001302 HasLazyLocalLexicalLookups = false;
1303 return LookupPtr;
Richard Smithf634c902012-03-16 06:12:59 +00001304}
1305
1306/// buildLookupImpl - Build part of the lookup data structure for the
1307/// declarations contained within DCtx, which will either be this
1308/// DeclContext, a DeclContext linked to it, or a transparent context
1309/// nested within it.
Richard Smitha3271c12015-02-07 00:45:52 +00001310void DeclContext::buildLookupImpl(DeclContext *DCtx, bool Internal) {
Richard Smith9e2341d2015-03-23 03:25:59 +00001311 for (Decl *D : DCtx->noload_decls()) {
Richard Smithf634c902012-03-16 06:12:59 +00001312 // Insert this declaration into the lookup structure, but only if
1313 // it's semantically within its decl context. Any other decls which
1314 // should be found in this context are added eagerly.
Richard Smithcf4ab522013-06-24 07:20:36 +00001315 //
1316 // If it's from an AST file, don't add it now. It'll get handled by
1317 // FindExternalVisibleDeclsByName if needed. Exception: if we're not
1318 // in C++, we do not track external visible decls for the TU, so in
1319 // that case we need to collect them all here.
Richard Smithf634c902012-03-16 06:12:59 +00001320 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Richard Smithcf4ab522013-06-24 07:20:36 +00001321 if (ND->getDeclContext() == DCtx && !shouldBeHidden(ND) &&
1322 (!ND->isFromASTFile() ||
1323 (isTranslationUnit() &&
1324 !getParentASTContext().getLangOpts().CPlusPlus)))
Richard Smitha3271c12015-02-07 00:45:52 +00001325 makeDeclVisibleInContextImpl(ND, Internal);
Richard Smithf634c902012-03-16 06:12:59 +00001326
1327 // If this declaration is itself a transparent declaration context
1328 // or inline namespace, add the members of this declaration of that
1329 // context (recursively).
1330 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(D))
1331 if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
Richard Smith9e2341d2015-03-23 03:25:59 +00001332 buildLookupImpl(InnerCtx, Internal);
Richard Smithf634c902012-03-16 06:12:59 +00001333 }
Sean Callanan95e74be2011-10-21 02:57:43 +00001334}
1335
Richard Smith40c78062015-02-21 02:31:57 +00001336NamedDecl *const DeclContextLookupResult::SingleElementDummyList = nullptr;
1337
Mike Stump11289f42009-09-09 15:08:12 +00001338DeclContext::lookup_result
Richard Smith40c78062015-02-21 02:31:57 +00001339DeclContext::lookup(DeclarationName Name) const {
Nick Lewycky2bd636f2012-03-13 04:12:34 +00001340 assert(DeclKind != Decl::LinkageSpec &&
1341 "Should not perform lookups into linkage specs!");
1342
Richard Smith40c78062015-02-21 02:31:57 +00001343 const DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor91f84212008-12-11 16:49:14 +00001344 if (PrimaryContext != this)
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001345 return PrimaryContext->lookup(Name);
Douglas Gregor91f84212008-12-11 16:49:14 +00001346
Richard Smith8cebe372015-02-25 22:20:13 +00001347 // If we have an external source, ensure that any later redeclarations of this
1348 // context have been loaded, since they may add names to the result of this
1349 // lookup (or add external visible storage).
1350 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1351 if (Source)
1352 (void)cast<Decl>(this)->getMostRecentDecl();
Richard Smithbb853c72014-08-13 01:23:33 +00001353
Richard Smith05afe5e2012-03-13 03:12:56 +00001354 if (hasExternalVisibleStorage()) {
Richard Smith8cebe372015-02-25 22:20:13 +00001355 assert(Source && "external visible storage but no external source?");
1356
Richard Smitha8b74592014-03-25 00:34:21 +00001357 if (NeedToReconcileExternalVisibleStorage)
1358 reconcileExternalVisibleStorage();
1359
Richard Smith9e2341d2015-03-23 03:25:59 +00001360 StoredDeclsMap *Map = LookupPtr;
Richard Smitha8b74592014-03-25 00:34:21 +00001361
Richard Smith9e2341d2015-03-23 03:25:59 +00001362 if (HasLazyLocalLexicalLookups || HasLazyExternalLexicalLookups)
Richard Smith40c78062015-02-21 02:31:57 +00001363 // FIXME: Make buildLookup const?
1364 Map = const_cast<DeclContext*>(this)->buildLookup();
Richard Smith645d7552013-02-07 03:37:08 +00001365
Richard Smith75fc3bf2013-02-08 00:37:45 +00001366 if (!Map)
1367 Map = CreateStoredDeclsMap(getParentASTContext());
1368
Richard Smith4abe0a82013-09-09 07:34:56 +00001369 // If we have a lookup result with no external decls, we are done.
Richard Smith75fc3bf2013-02-08 00:37:45 +00001370 std::pair<StoredDeclsMap::iterator, bool> R =
1371 Map->insert(std::make_pair(Name, StoredDeclsList()));
Richard Smith4abe0a82013-09-09 07:34:56 +00001372 if (!R.second && !R.first->second.hasExternalDecls())
Richard Smith75fc3bf2013-02-08 00:37:45 +00001373 return R.first->second.getLookupResult();
Richard Smithf634c902012-03-16 06:12:59 +00001374
Richard Smith309271b2014-03-04 00:21:14 +00001375 if (Source->FindExternalVisibleDeclsByName(this, Name) || !R.second) {
Richard Smith9e2341d2015-03-23 03:25:59 +00001376 if (StoredDeclsMap *Map = LookupPtr) {
Richard Smith9ce12e32013-02-07 03:30:24 +00001377 StoredDeclsMap::iterator I = Map->find(Name);
1378 if (I != Map->end())
1379 return I->second.getLookupResult();
1380 }
1381 }
1382
Richard Smith40c78062015-02-21 02:31:57 +00001383 return lookup_result();
John McCall75b960e2010-06-01 09:23:16 +00001384 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001385
Richard Smith9e2341d2015-03-23 03:25:59 +00001386 StoredDeclsMap *Map = LookupPtr;
1387 if (HasLazyLocalLexicalLookups || HasLazyExternalLexicalLookups)
Richard Smith40c78062015-02-21 02:31:57 +00001388 Map = const_cast<DeclContext*>(this)->buildLookup();
Richard Smithf634c902012-03-16 06:12:59 +00001389
1390 if (!Map)
Richard Smith40c78062015-02-21 02:31:57 +00001391 return lookup_result();
Richard Smithf634c902012-03-16 06:12:59 +00001392
1393 StoredDeclsMap::iterator I = Map->find(Name);
1394 if (I == Map->end())
Richard Smith40c78062015-02-21 02:31:57 +00001395 return lookup_result();
Richard Smithf634c902012-03-16 06:12:59 +00001396
1397 return I->second.getLookupResult();
Douglas Gregor91f84212008-12-11 16:49:14 +00001398}
1399
Richard Smith95d99302013-07-13 02:00:19 +00001400DeclContext::lookup_result
1401DeclContext::noload_lookup(DeclarationName Name) {
1402 assert(DeclKind != Decl::LinkageSpec &&
1403 "Should not perform lookups into linkage specs!");
Richard Smith95d99302013-07-13 02:00:19 +00001404
1405 DeclContext *PrimaryContext = getPrimaryContext();
1406 if (PrimaryContext != this)
1407 return PrimaryContext->noload_lookup(Name);
1408
Richard Smith9e2341d2015-03-23 03:25:59 +00001409 // If we have any lazy lexical declarations not in our lookup map, add them
1410 // now. Don't import any external declarations, not even if we know we have
1411 // some missing from the external visible lookups.
1412 if (HasLazyLocalLexicalLookups) {
Vince Harrona3ea9a42015-03-22 05:59:59 +00001413 SmallVector<DeclContext *, 2> Contexts;
1414 collectAllContexts(Contexts);
1415 for (unsigned I = 0, N = Contexts.size(); I != N; ++I)
Richard Smith9e2341d2015-03-23 03:25:59 +00001416 buildLookupImpl(Contexts[I], hasExternalVisibleStorage());
1417 HasLazyLocalLexicalLookups = false;
Vince Harrona3ea9a42015-03-22 05:59:59 +00001418 }
1419
Richard Smith9e2341d2015-03-23 03:25:59 +00001420 StoredDeclsMap *Map = LookupPtr;
Richard Smith95d99302013-07-13 02:00:19 +00001421 if (!Map)
Richard Smith40c78062015-02-21 02:31:57 +00001422 return lookup_result();
Richard Smith95d99302013-07-13 02:00:19 +00001423
1424 StoredDeclsMap::iterator I = Map->find(Name);
Craig Topper36250ad2014-05-12 05:36:57 +00001425 return I != Map->end() ? I->second.getLookupResult()
Richard Smith40c78062015-02-21 02:31:57 +00001426 : lookup_result();
Richard Smith95d99302013-07-13 02:00:19 +00001427}
1428
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001429void DeclContext::localUncachedLookup(DeclarationName Name,
1430 SmallVectorImpl<NamedDecl *> &Results) {
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001431 Results.clear();
1432
1433 // If there's no external storage, just perform a normal lookup and copy
1434 // the results.
Douglas Gregordd6006f2012-07-17 21:16:27 +00001435 if (!hasExternalVisibleStorage() && !hasExternalLexicalStorage() && Name) {
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001436 lookup_result LookupResults = lookup(Name);
David Blaikieff7d47a2012-12-19 00:45:41 +00001437 Results.insert(Results.end(), LookupResults.begin(), LookupResults.end());
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001438 return;
1439 }
1440
1441 // If we have a lookup table, check there first. Maybe we'll get lucky.
Richard Smith9e2341d2015-03-23 03:25:59 +00001442 // FIXME: Should we be checking these flags on the primary context?
1443 if (Name && !HasLazyLocalLexicalLookups && !HasLazyExternalLexicalLookups) {
1444 if (StoredDeclsMap *Map = LookupPtr) {
Douglas Gregordd6006f2012-07-17 21:16:27 +00001445 StoredDeclsMap::iterator Pos = Map->find(Name);
1446 if (Pos != Map->end()) {
1447 Results.insert(Results.end(),
David Blaikieff7d47a2012-12-19 00:45:41 +00001448 Pos->second.getLookupResult().begin(),
1449 Pos->second.getLookupResult().end());
Douglas Gregordd6006f2012-07-17 21:16:27 +00001450 return;
1451 }
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001452 }
1453 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00001454
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001455 // Slow case: grovel through the declarations in our chain looking for
1456 // matches.
Richard Smith9e2341d2015-03-23 03:25:59 +00001457 // FIXME: If we have lazy external declarations, this will not find them!
1458 // FIXME: Should we CollectAllContexts and walk them all here?
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001459 for (Decl *D = FirstDecl; D; D = D->getNextDeclInContext()) {
1460 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1461 if (ND->getDeclName() == Name)
1462 Results.push_back(ND);
1463 }
1464}
1465
Sebastian Redl50c68252010-08-31 00:36:30 +00001466DeclContext *DeclContext::getRedeclContext() {
Chris Lattner17a1bfa2009-03-27 19:19:59 +00001467 DeclContext *Ctx = this;
Sebastian Redlbd595762010-08-31 20:53:31 +00001468 // Skip through transparent contexts.
1469 while (Ctx->isTransparentContext())
Douglas Gregor6ad0ef52009-01-06 23:51:29 +00001470 Ctx = Ctx->getParent();
1471 return Ctx;
1472}
1473
Douglas Gregorf47b9112009-02-25 22:02:03 +00001474DeclContext *DeclContext::getEnclosingNamespaceContext() {
1475 DeclContext *Ctx = this;
1476 // Skip through non-namespace, non-translation-unit contexts.
Sebastian Redl4f08c962010-08-31 00:36:23 +00001477 while (!Ctx->isFileContext())
Douglas Gregorf47b9112009-02-25 22:02:03 +00001478 Ctx = Ctx->getParent();
1479 return Ctx->getPrimaryContext();
1480}
1481
Reid Klecknerd60b82f2014-11-17 23:36:45 +00001482RecordDecl *DeclContext::getOuterLexicalRecordContext() {
1483 // Loop until we find a non-record context.
1484 RecordDecl *OutermostRD = nullptr;
1485 DeclContext *DC = this;
1486 while (DC->isRecord()) {
1487 OutermostRD = cast<RecordDecl>(DC);
1488 DC = DC->getLexicalParent();
1489 }
1490 return OutermostRD;
1491}
1492
Sebastian Redl50c68252010-08-31 00:36:30 +00001493bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
1494 // For non-file contexts, this is equivalent to Equals.
1495 if (!isFileContext())
1496 return O->Equals(this);
1497
1498 do {
1499 if (O->Equals(this))
1500 return true;
1501
1502 const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O);
1503 if (!NS || !NS->isInline())
1504 break;
1505 O = NS->getParent();
1506 } while (O);
1507
1508 return false;
1509}
1510
Richard Smithf634c902012-03-16 06:12:59 +00001511void DeclContext::makeDeclVisibleInContext(NamedDecl *D) {
1512 DeclContext *PrimaryDC = this->getPrimaryContext();
1513 DeclContext *DeclDC = D->getDeclContext()->getPrimaryContext();
1514 // If the decl is being added outside of its semantic decl context, we
1515 // need to ensure that we eagerly build the lookup information for it.
1516 PrimaryDC->makeDeclVisibleInContextWithFlags(D, false, PrimaryDC == DeclDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00001517}
1518
Richard Smithf634c902012-03-16 06:12:59 +00001519void DeclContext::makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal,
1520 bool Recoverable) {
1521 assert(this == getPrimaryContext() && "expected a primary DC");
Sean Callanan95e74be2011-10-21 02:57:43 +00001522
Richard Smith05afe5e2012-03-13 03:12:56 +00001523 // Skip declarations within functions.
Richard Smith114394f2013-08-09 04:35:01 +00001524 if (isFunctionOrMethod())
Richard Smith05afe5e2012-03-13 03:12:56 +00001525 return;
1526
Richard Smithf634c902012-03-16 06:12:59 +00001527 // Skip declarations which should be invisible to name lookup.
1528 if (shouldBeHidden(D))
1529 return;
1530
1531 // If we already have a lookup data structure, perform the insertion into
1532 // it. If we might have externally-stored decls with this name, look them
1533 // up and perform the insertion. If this decl was declared outside its
1534 // semantic context, buildLookup won't add it, so add it now.
1535 //
1536 // FIXME: As a performance hack, don't add such decls into the translation
1537 // unit unless we're in C++, since qualified lookup into the TU is never
1538 // performed.
Richard Smith9e2341d2015-03-23 03:25:59 +00001539 if (LookupPtr || hasExternalVisibleStorage() ||
Richard Smithf634c902012-03-16 06:12:59 +00001540 ((!Recoverable || D->getDeclContext() != D->getLexicalDeclContext()) &&
1541 (getParentASTContext().getLangOpts().CPlusPlus ||
1542 !isTranslationUnit()))) {
1543 // If we have lazily omitted any decls, they might have the same name as
1544 // the decl which we are adding, so build a full lookup table before adding
1545 // this decl.
1546 buildLookup();
1547 makeDeclVisibleInContextImpl(D, Internal);
1548 } else {
Richard Smith9e2341d2015-03-23 03:25:59 +00001549 HasLazyLocalLexicalLookups = true;
Richard Smithf634c902012-03-16 06:12:59 +00001550 }
1551
1552 // If we are a transparent context or inline namespace, insert into our
1553 // parent context, too. This operation is recursive.
1554 if (isTransparentContext() || isInlineNamespace())
1555 getParent()->getPrimaryContext()->
1556 makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
1557
1558 Decl *DCAsDecl = cast<Decl>(this);
1559 // Notify that a decl was made visible unless we are a Tag being defined.
1560 if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined()))
1561 if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
1562 L->AddedVisibleDecl(this, D);
1563}
1564
1565void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal) {
1566 // Find or create the stored declaration map.
Richard Smith9e2341d2015-03-23 03:25:59 +00001567 StoredDeclsMap *Map = LookupPtr;
Richard Smithf634c902012-03-16 06:12:59 +00001568 if (!Map) {
1569 ASTContext *C = &getParentASTContext();
1570 Map = CreateStoredDeclsMap(*C);
Argyrios Kyrtzidise51e5542010-07-04 21:44:25 +00001571 }
1572
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001573 // If there is an external AST source, load any declarations it knows about
1574 // with this declaration's name.
1575 // If the lookup table contains an entry about this name it means that we
1576 // have already checked the external source.
Sean Callanan95e74be2011-10-21 02:57:43 +00001577 if (!Internal)
1578 if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
1579 if (hasExternalVisibleStorage() &&
Richard Smithf634c902012-03-16 06:12:59 +00001580 Map->find(D->getDeclName()) == Map->end())
Sean Callanan95e74be2011-10-21 02:57:43 +00001581 Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001582
Douglas Gregor91f84212008-12-11 16:49:14 +00001583 // Insert this declaration into the map.
Richard Smithf634c902012-03-16 06:12:59 +00001584 StoredDeclsList &DeclNameEntries = (*Map)[D->getDeclName()];
Richard Smith4abe0a82013-09-09 07:34:56 +00001585
1586 if (Internal) {
1587 // If this is being added as part of loading an external declaration,
1588 // this may not be the only external declaration with this name.
1589 // In this case, we never try to replace an existing declaration; we'll
1590 // handle that when we finalize the list of declarations for this name.
1591 DeclNameEntries.setHasExternalDecls();
1592 DeclNameEntries.AddSubsequentDecl(D);
1593 return;
1594 }
1595
Richard Smitha3271c12015-02-07 00:45:52 +00001596 if (DeclNameEntries.isNull()) {
Chris Lattnercaae7162009-02-20 01:44:05 +00001597 DeclNameEntries.setOnlyValue(D);
Richard Smithf634c902012-03-16 06:12:59 +00001598 return;
Douglas Gregor91f84212008-12-11 16:49:14 +00001599 }
Chris Lattner24e24d52009-02-20 00:55:03 +00001600
Richard Smithe8292b12015-02-10 03:28:10 +00001601 if (DeclNameEntries.HandleRedeclaration(D, /*IsKnownNewer*/!Internal)) {
Richard Smithf634c902012-03-16 06:12:59 +00001602 // This declaration has replaced an existing one for which
1603 // declarationReplaces returns true.
1604 return;
1605 }
Mike Stump11289f42009-09-09 15:08:12 +00001606
Richard Smithf634c902012-03-16 06:12:59 +00001607 // Put this declaration into the appropriate slot.
1608 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor91f84212008-12-11 16:49:14 +00001609}
Douglas Gregor889ceb72009-02-03 19:21:40 +00001610
Richard Smith40c78062015-02-21 02:31:57 +00001611UsingDirectiveDecl *DeclContext::udir_iterator::operator*() const {
1612 return cast<UsingDirectiveDecl>(*I);
1613}
1614
Douglas Gregor889ceb72009-02-03 19:21:40 +00001615/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
1616/// this context.
Aaron Ballman804a7fb2014-03-17 17:14:12 +00001617DeclContext::udir_range DeclContext::using_directives() const {
Richard Smith05afe5e2012-03-13 03:12:56 +00001618 // FIXME: Use something more efficient than normal lookup for using
1619 // directives. In C++, using directives are looked up more than anything else.
Richard Smithcf4bdde2015-02-21 02:45:19 +00001620 lookup_result Result = lookup(UsingDirectiveDecl::getName());
Richard Smith40c78062015-02-21 02:31:57 +00001621 return udir_range(Result.begin(), Result.end());
Douglas Gregor889ceb72009-02-03 19:21:40 +00001622}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001623
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001624//===----------------------------------------------------------------------===//
1625// Creation and Destruction of StoredDeclsMaps. //
1626//===----------------------------------------------------------------------===//
1627
John McCallc62bb642010-03-24 05:22:00 +00001628StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
Richard Smith9e2341d2015-03-23 03:25:59 +00001629 assert(!LookupPtr && "context already has a decls map");
John McCallc62bb642010-03-24 05:22:00 +00001630 assert(getPrimaryContext() == this &&
1631 "creating decls map on non-primary context");
1632
1633 StoredDeclsMap *M;
1634 bool Dependent = isDependentContext();
1635 if (Dependent)
1636 M = new DependentStoredDeclsMap();
1637 else
1638 M = new StoredDeclsMap();
1639 M->Previous = C.LastSDM;
1640 C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
Richard Smith9e2341d2015-03-23 03:25:59 +00001641 LookupPtr = M;
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001642 return M;
1643}
1644
1645void ASTContext::ReleaseDeclContextMaps() {
John McCallc62bb642010-03-24 05:22:00 +00001646 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1647 // pointer because the subclass doesn't add anything that needs to
1648 // be deleted.
John McCallc62bb642010-03-24 05:22:00 +00001649 StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1650}
1651
1652void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1653 while (Map) {
1654 // Advance the iteration before we invalidate memory.
1655 llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1656
1657 if (Dependent)
1658 delete static_cast<DependentStoredDeclsMap*>(Map);
1659 else
1660 delete Map;
1661
1662 Map = Next.getPointer();
1663 Dependent = Next.getInt();
1664 }
1665}
1666
John McCallc62bb642010-03-24 05:22:00 +00001667DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1668 DeclContext *Parent,
1669 const PartialDiagnostic &PDiag) {
1670 assert(Parent->isDependentContext()
1671 && "cannot iterate dependent diagnostics of non-dependent context");
1672 Parent = Parent->getPrimaryContext();
Richard Smith9e2341d2015-03-23 03:25:59 +00001673 if (!Parent->LookupPtr)
John McCallc62bb642010-03-24 05:22:00 +00001674 Parent->CreateStoredDeclsMap(C);
1675
Richard Smith9e2341d2015-03-23 03:25:59 +00001676 DependentStoredDeclsMap *Map =
1677 static_cast<DependentStoredDeclsMap *>(Parent->LookupPtr);
John McCallc62bb642010-03-24 05:22:00 +00001678
Douglas Gregora55530e2010-03-29 23:56:53 +00001679 // Allocate the copy of the PartialDiagnostic via the ASTContext's
Douglas Gregor89336232010-03-29 23:34:08 +00001680 // BumpPtrAllocator, rather than the ASTContext itself.
Craig Topper36250ad2014-05-12 05:36:57 +00001681 PartialDiagnostic::Storage *DiagStorage = nullptr;
Douglas Gregora55530e2010-03-29 23:56:53 +00001682 if (PDiag.hasStorage())
1683 DiagStorage = new (C) PartialDiagnostic::Storage;
1684
1685 DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
John McCallc62bb642010-03-24 05:22:00 +00001686
1687 // TODO: Maybe we shouldn't reverse the order during insertion.
1688 DD->NextDiagnostic = Map->FirstDiagnostic;
1689 Map->FirstDiagnostic = DD;
1690
1691 return DD;
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001692}