blob: d20451d1badc134a350ddff6b7416b5e3ee3bf68 [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:
564 return IDNS_Ordinary | IDNS_Type;
565
John McCall3f746822009-11-17 05:59:44 +0000566 case UsingShadow:
567 return 0; // we'll actually overwrite this later
568
John McCalle61f2ba2009-11-18 02:36:19 +0000569 case UnresolvedUsingValue:
John McCalle61f2ba2009-11-18 02:36:19 +0000570 return IDNS_Ordinary | IDNS_Using;
John McCall3f746822009-11-17 05:59:44 +0000571
572 case Using:
573 return IDNS_Using;
574
Chris Lattner8e097192009-03-27 20:18:19 +0000575 case ObjCProtocol:
Douglas Gregor79947a22009-04-24 00:11:27 +0000576 return IDNS_ObjCProtocol;
Mike Stump11289f42009-09-09 15:08:12 +0000577
Chris Lattner8e097192009-03-27 20:18:19 +0000578 case Field:
579 case ObjCAtDefsField:
580 case ObjCIvar:
581 return IDNS_Member;
Mike Stump11289f42009-09-09 15:08:12 +0000582
Chris Lattner8e097192009-03-27 20:18:19 +0000583 case Record:
584 case CXXRecord:
585 case Enum:
John McCalle87beb22010-04-23 18:46:30 +0000586 return IDNS_Tag | IDNS_Type;
Mike Stump11289f42009-09-09 15:08:12 +0000587
Chris Lattner8e097192009-03-27 20:18:19 +0000588 case Namespace:
John McCalle87beb22010-04-23 18:46:30 +0000589 case NamespaceAlias:
590 return IDNS_Namespace;
591
Chris Lattner8e097192009-03-27 20:18:19 +0000592 case FunctionTemplate:
Larisse Voufo39a1e502013-08-06 01:03:05 +0000593 case VarTemplate:
John McCalle87beb22010-04-23 18:46:30 +0000594 return IDNS_Ordinary;
595
Chris Lattner8e097192009-03-27 20:18:19 +0000596 case ClassTemplate:
597 case TemplateTemplateParm:
John McCalle87beb22010-04-23 18:46:30 +0000598 return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
Mike Stump11289f42009-09-09 15:08:12 +0000599
Chris Lattner8e097192009-03-27 20:18:19 +0000600 // Never have names.
John McCallaa74a0c2009-08-28 07:59:38 +0000601 case Friend:
John McCall11083da2009-09-16 22:47:08 +0000602 case FriendTemplate:
Abramo Bagnarad7340582010-06-05 05:09:32 +0000603 case AccessSpec:
Chris Lattner8e097192009-03-27 20:18:19 +0000604 case LinkageSpec:
605 case FileScopeAsm:
606 case StaticAssert:
Chris Lattner8e097192009-03-27 20:18:19 +0000607 case ObjCPropertyImpl:
Chris Lattner8e097192009-03-27 20:18:19 +0000608 case Block:
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000609 case Captured:
Chris Lattner8e097192009-03-27 20:18:19 +0000610 case TranslationUnit:
Richard Smithf19e1272015-03-07 00:04:49 +0000611 case ExternCContext:
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000612
Chris Lattner8e097192009-03-27 20:18:19 +0000613 case UsingDirective:
614 case ClassTemplateSpecialization:
Douglas Gregor2373c592009-05-31 09:31:02 +0000615 case ClassTemplatePartialSpecialization:
Francois Pichet00c7e6c2011-08-14 03:52:19 +0000616 case ClassScopeFunctionSpecialization:
Larisse Voufo39a1e502013-08-06 01:03:05 +0000617 case VarTemplateSpecialization:
618 case VarTemplatePartialSpecialization:
Douglas Gregore93525e2010-04-22 23:19:50 +0000619 case ObjCImplementation:
620 case ObjCCategory:
621 case ObjCCategoryImpl:
Douglas Gregorba345522011-12-02 23:23:56 +0000622 case Import:
Alexey Bataeva769e072013-03-22 06:34:35 +0000623 case OMPThreadPrivate:
Michael Han84324352013-02-22 17:15:32 +0000624 case Empty:
Douglas Gregore93525e2010-04-22 23:19:50 +0000625 // Never looked up by name.
Chris Lattner8e097192009-03-27 20:18:19 +0000626 return 0;
627 }
John McCall3f746822009-11-17 05:59:44 +0000628
David Blaikiee4d798f2012-01-20 21:50:17 +0000629 llvm_unreachable("Invalid DeclKind!");
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000630}
631
Argyrios Kyrtzidis6f40eb72012-02-09 02:44:08 +0000632void Decl::setAttrsImpl(const AttrVec &attrs, ASTContext &Ctx) {
Argyrios Kyrtzidis91167172010-06-11 23:09:25 +0000633 assert(!HasAttrs && "Decl already contains attrs.");
634
Argyrios Kyrtzidis6f40eb72012-02-09 02:44:08 +0000635 AttrVec &AttrBlank = Ctx.getDeclAttrs(this);
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000636 assert(AttrBlank.empty() && "HasAttrs was wrong?");
Argyrios Kyrtzidis91167172010-06-11 23:09:25 +0000637
638 AttrBlank = attrs;
639 HasAttrs = true;
640}
641
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000642void Decl::dropAttrs() {
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000643 if (!HasAttrs) return;
Mike Stump11289f42009-09-09 15:08:12 +0000644
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000645 HasAttrs = false;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000646 getASTContext().eraseDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000647}
648
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000649const AttrVec &Decl::getAttrs() const {
650 assert(HasAttrs && "No attrs to get!");
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000651 return getASTContext().getDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000652}
653
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000654Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000655 Decl::Kind DK = D->getDeclKind();
656 switch(DK) {
Alexis Hunted053252010-05-30 07:21:58 +0000657#define DECL(NAME, BASE)
658#define DECL_CONTEXT(NAME) \
659 case Decl::NAME: \
660 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
661#define DECL_CONTEXT_BASE(NAME)
662#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000663 default:
Alexis Hunted053252010-05-30 07:21:58 +0000664#define DECL(NAME, BASE)
665#define DECL_CONTEXT_BASE(NAME) \
666 if (DK >= first##NAME && DK <= last##NAME) \
667 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
668#include "clang/AST/DeclNodes.inc"
David Blaikie83d382b2011-09-23 05:06:16 +0000669 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000670 }
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000671}
672
673DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000674 Decl::Kind DK = D->getKind();
675 switch(DK) {
Alexis Hunted053252010-05-30 07:21:58 +0000676#define DECL(NAME, BASE)
677#define DECL_CONTEXT(NAME) \
678 case Decl::NAME: \
679 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
680#define DECL_CONTEXT_BASE(NAME)
681#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000682 default:
Alexis Hunted053252010-05-30 07:21:58 +0000683#define DECL(NAME, BASE)
684#define DECL_CONTEXT_BASE(NAME) \
685 if (DK >= first##NAME && DK <= last##NAME) \
686 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
687#include "clang/AST/DeclNodes.inc"
David Blaikie83d382b2011-09-23 05:06:16 +0000688 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000689 }
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000690}
691
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000692SourceLocation Decl::getBodyRBrace() const {
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +0000693 // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
694 // FunctionDecl stores EndRangeLoc for this purpose.
695 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
696 const FunctionDecl *Definition;
697 if (FD->hasBody(Definition))
698 return Definition->getSourceRange().getEnd();
699 return SourceLocation();
700 }
701
Argyrios Kyrtzidis6fbc8fa2010-07-07 11:31:27 +0000702 if (Stmt *Body = getBody())
703 return Body->getSourceRange().getEnd();
704
705 return SourceLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +0000706}
707
Alp Tokerc1086762013-12-07 13:51:35 +0000708bool Decl::AccessDeclContextSanity() const {
Douglas Gregor4b00d3b2010-12-02 00:22:25 +0000709#ifndef NDEBUG
John McCall401982f2010-01-20 21:53:11 +0000710 // Suppress this check if any of the following hold:
711 // 1. this is the translation unit (and thus has no parent)
712 // 2. this is a template parameter (and thus doesn't belong to its context)
Argyrios Kyrtzidise1778632010-09-08 21:58:42 +0000713 // 3. this is a non-type template parameter
714 // 4. the context is not a record
715 // 5. it's invalid
716 // 6. it's a C++0x static_assert.
Anders Carlssonadf36b22009-08-29 20:47:47 +0000717 if (isa<TranslationUnitDecl>(this) ||
Argyrios Kyrtzidisa45855f2010-07-02 11:55:44 +0000718 isa<TemplateTypeParmDecl>(this) ||
Argyrios Kyrtzidise1778632010-09-08 21:58:42 +0000719 isa<NonTypeTemplateParmDecl>(this) ||
Douglas Gregor2b76dd92010-02-22 17:53:38 +0000720 !isa<CXXRecordDecl>(getDeclContext()) ||
Argyrios Kyrtzidis260b4a82010-09-08 21:32:35 +0000721 isInvalidDecl() ||
722 isa<StaticAssertDecl>(this) ||
723 // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
724 // as DeclContext (?).
Argyrios Kyrtzidise1778632010-09-08 21:58:42 +0000725 isa<ParmVarDecl>(this) ||
726 // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
727 // AS_none as access specifier.
Francois Pichet09af8c32011-08-17 01:06:54 +0000728 isa<CXXRecordDecl>(this) ||
729 isa<ClassScopeFunctionSpecializationDecl>(this))
Alp Tokerc1086762013-12-07 13:51:35 +0000730 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000731
732 assert(Access != AS_none &&
Anders Carlssona28908d2009-03-25 23:38:06 +0000733 "Access specifier is AS_none inside a record decl");
Douglas Gregor4b00d3b2010-12-02 00:22:25 +0000734#endif
Alp Tokerc1086762013-12-07 13:51:35 +0000735 return true;
Anders Carlssona28908d2009-03-25 23:38:06 +0000736}
737
John McCalldec348f72013-05-03 07:33:41 +0000738static Decl::Kind getKind(const Decl *D) { return D->getKind(); }
739static Decl::Kind getKind(const DeclContext *DC) { return DC->getDeclKind(); }
740
Aaron Ballman12b9f652014-01-16 13:55:42 +0000741const FunctionType *Decl::getFunctionType(bool BlocksToo) const {
742 QualType Ty;
743 if (const ValueDecl *D = dyn_cast<ValueDecl>(this))
744 Ty = D->getType();
745 else if (const TypedefNameDecl *D = dyn_cast<TypedefNameDecl>(this))
746 Ty = D->getUnderlyingType();
747 else
Craig Topper36250ad2014-05-12 05:36:57 +0000748 return nullptr;
Aaron Ballman12b9f652014-01-16 13:55:42 +0000749
750 if (Ty->isFunctionPointerType())
751 Ty = Ty->getAs<PointerType>()->getPointeeType();
752 else if (BlocksToo && Ty->isBlockPointerType())
753 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
754
755 return Ty->getAs<FunctionType>();
756}
757
758
John McCalldec348f72013-05-03 07:33:41 +0000759/// Starting at a given context (a Decl or DeclContext), look for a
760/// code context that is not a closure (a lambda, block, etc.).
761template <class T> static Decl *getNonClosureContext(T *D) {
762 if (getKind(D) == Decl::CXXMethod) {
763 CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
John McCall55c0cee2013-05-03 17:11:14 +0000764 if (MD->getOverloadedOperator() == OO_Call &&
765 MD->getParent()->isLambda())
John McCalldec348f72013-05-03 07:33:41 +0000766 return getNonClosureContext(MD->getParent()->getParent());
767 return MD;
768 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
769 return FD;
770 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
771 return MD;
772 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
773 return getNonClosureContext(BD->getParent());
774 } else if (CapturedDecl *CD = dyn_cast<CapturedDecl>(D)) {
775 return getNonClosureContext(CD->getParent());
776 } else {
Craig Topper36250ad2014-05-12 05:36:57 +0000777 return nullptr;
John McCalldec348f72013-05-03 07:33:41 +0000778 }
John McCallfe96e0b2011-11-06 09:01:30 +0000779}
780
John McCalldec348f72013-05-03 07:33:41 +0000781Decl *Decl::getNonClosureContext() {
782 return ::getNonClosureContext(this);
783}
John McCallb67608f2011-02-22 22:25:23 +0000784
John McCalldec348f72013-05-03 07:33:41 +0000785Decl *DeclContext::getNonClosureAncestor() {
786 return ::getNonClosureContext(this);
John McCallb67608f2011-02-22 22:25:23 +0000787}
Anders Carlssona28908d2009-03-25 23:38:06 +0000788
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000789//===----------------------------------------------------------------------===//
790// DeclContext Implementation
791//===----------------------------------------------------------------------===//
792
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000793bool DeclContext::classof(const Decl *D) {
794 switch (D->getKind()) {
Alexis Hunted053252010-05-30 07:21:58 +0000795#define DECL(NAME, BASE)
796#define DECL_CONTEXT(NAME) case Decl::NAME:
797#define DECL_CONTEXT_BASE(NAME)
798#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000799 return true;
800 default:
Alexis Hunted053252010-05-30 07:21:58 +0000801#define DECL(NAME, BASE)
802#define DECL_CONTEXT_BASE(NAME) \
803 if (D->getKind() >= Decl::first##NAME && \
804 D->getKind() <= Decl::last##NAME) \
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000805 return true;
Alexis Hunted053252010-05-30 07:21:58 +0000806#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000807 return false;
808 }
809}
810
Douglas Gregor9c832f72010-07-25 18:38:02 +0000811DeclContext::~DeclContext() { }
Douglas Gregor91f84212008-12-11 16:49:14 +0000812
Douglas Gregor7f737c02009-09-10 16:57:35 +0000813/// \brief Find the parent context of this context that will be
814/// used for unqualified name lookup.
815///
816/// Generally, the parent lookup context is the semantic context. However, for
817/// a friend function the parent lookup context is the lexical context, which
818/// is the class in which the friend is declared.
819DeclContext *DeclContext::getLookupParent() {
820 // FIXME: Find a better way to identify friends
821 if (isa<FunctionDecl>(this))
Sebastian Redl50c68252010-08-31 00:36:30 +0000822 if (getParent()->getRedeclContext()->isFileContext() &&
823 getLexicalParent()->getRedeclContext()->isRecord())
Douglas Gregor7f737c02009-09-10 16:57:35 +0000824 return getLexicalParent();
825
826 return getParent();
827}
828
Sebastian Redlbd595762010-08-31 20:53:31 +0000829bool DeclContext::isInlineNamespace() const {
830 return isNamespace() &&
831 cast<NamespaceDecl>(this)->isInline();
832}
833
Richard Trieuc771d5d2014-05-28 02:16:01 +0000834bool DeclContext::isStdNamespace() const {
835 if (!isNamespace())
836 return false;
837
838 const NamespaceDecl *ND = cast<NamespaceDecl>(this);
839 if (ND->isInline()) {
840 return ND->getParent()->isStdNamespace();
841 }
842
843 if (!getParent()->getRedeclContext()->isTranslationUnit())
844 return false;
845
846 const IdentifierInfo *II = ND->getIdentifier();
847 return II && II->isStr("std");
848}
849
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000850bool DeclContext::isDependentContext() const {
851 if (isFileContext())
852 return false;
853
Douglas Gregor2373c592009-05-31 09:31:02 +0000854 if (isa<ClassTemplatePartialSpecializationDecl>(this))
855 return true;
856
Douglas Gregor680e9e02012-02-21 19:11:17 +0000857 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this)) {
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000858 if (Record->getDescribedClassTemplate())
859 return true;
Douglas Gregor680e9e02012-02-21 19:11:17 +0000860
861 if (Record->isDependentLambda())
862 return true;
863 }
864
John McCallc62bb642010-03-24 05:22:00 +0000865 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000866 if (Function->getDescribedFunctionTemplate())
867 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000868
John McCallc62bb642010-03-24 05:22:00 +0000869 // Friend function declarations are dependent if their *lexical*
870 // context is dependent.
871 if (cast<Decl>(this)->getFriendObjectKind())
872 return getLexicalParent()->isDependentContext();
873 }
874
Richard Smith2b560572015-02-07 03:11:11 +0000875 // FIXME: A variable template is a dependent context, but is not a
876 // DeclContext. A context within it (such as a lambda-expression)
877 // should be considered dependent.
878
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000879 return getParent() && getParent()->isDependentContext();
880}
881
Douglas Gregor07665a62009-01-05 19:45:36 +0000882bool DeclContext::isTransparentContext() const {
883 if (DeclKind == Decl::Enum)
Douglas Gregor0bf31402010-10-08 23:50:27 +0000884 return !cast<EnumDecl>(this)->isScoped();
Douglas Gregor07665a62009-01-05 19:45:36 +0000885 else if (DeclKind == Decl::LinkageSpec)
886 return true;
Douglas Gregor07665a62009-01-05 19:45:36 +0000887
888 return false;
889}
890
Serge Pavlov3cb80222013-11-14 02:13:03 +0000891static bool isLinkageSpecContext(const DeclContext *DC,
892 LinkageSpecDecl::LanguageIDs ID) {
893 while (DC->getDeclKind() != Decl::TranslationUnit) {
894 if (DC->getDeclKind() == Decl::LinkageSpec)
895 return cast<LinkageSpecDecl>(DC)->getLanguage() == ID;
Richard Smithac9c9a02014-04-14 20:23:58 +0000896 DC = DC->getLexicalParent();
Serge Pavlov3cb80222013-11-14 02:13:03 +0000897 }
898 return false;
899}
900
901bool DeclContext::isExternCContext() const {
902 return isLinkageSpecContext(this, clang::LinkageSpecDecl::lang_c);
903}
904
905bool DeclContext::isExternCXXContext() const {
906 return isLinkageSpecContext(this, clang::LinkageSpecDecl::lang_cxx);
907}
908
Sebastian Redl50c68252010-08-31 00:36:30 +0000909bool DeclContext::Encloses(const DeclContext *DC) const {
Douglas Gregore985a3b2009-08-27 06:03:53 +0000910 if (getPrimaryContext() != this)
911 return getPrimaryContext()->Encloses(DC);
Mike Stump11289f42009-09-09 15:08:12 +0000912
Douglas Gregore985a3b2009-08-27 06:03:53 +0000913 for (; DC; DC = DC->getParent())
914 if (DC->getPrimaryContext() == this)
915 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000916 return false;
Douglas Gregore985a3b2009-08-27 06:03:53 +0000917}
918
Steve Naroff35c62ae2009-01-08 17:28:14 +0000919DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor91f84212008-12-11 16:49:14 +0000920 switch (DeclKind) {
Douglas Gregor91f84212008-12-11 16:49:14 +0000921 case Decl::TranslationUnit:
Richard Smithf19e1272015-03-07 00:04:49 +0000922 case Decl::ExternCContext:
Douglas Gregor07665a62009-01-05 19:45:36 +0000923 case Decl::LinkageSpec:
Mike Stump11289f42009-09-09 15:08:12 +0000924 case Decl::Block:
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000925 case Decl::Captured:
Douglas Gregor91f84212008-12-11 16:49:14 +0000926 // There is only one DeclContext for these entities.
927 return this;
928
929 case Decl::Namespace:
930 // The original namespace is our primary context.
931 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
932
Douglas Gregor91f84212008-12-11 16:49:14 +0000933 case Decl::ObjCMethod:
934 return this;
935
936 case Decl::ObjCInterface:
Douglas Gregor66b310c2011-12-15 18:03:09 +0000937 if (ObjCInterfaceDecl *Def = cast<ObjCInterfaceDecl>(this)->getDefinition())
938 return Def;
939
940 return this;
941
Steve Naroff35c62ae2009-01-08 17:28:14 +0000942 case Decl::ObjCProtocol:
Douglas Gregora715bff2012-01-01 19:51:50 +0000943 if (ObjCProtocolDecl *Def = cast<ObjCProtocolDecl>(this)->getDefinition())
944 return Def;
945
946 return this;
Douglas Gregor66b310c2011-12-15 18:03:09 +0000947
Steve Naroff35c62ae2009-01-08 17:28:14 +0000948 case Decl::ObjCCategory:
Douglas Gregor91f84212008-12-11 16:49:14 +0000949 return this;
950
Steve Naroff35c62ae2009-01-08 17:28:14 +0000951 case Decl::ObjCImplementation:
952 case Decl::ObjCCategoryImpl:
953 return this;
954
Douglas Gregor91f84212008-12-11 16:49:14 +0000955 default:
Alexis Hunted053252010-05-30 07:21:58 +0000956 if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
Douglas Gregor67a65642009-02-17 23:15:12 +0000957 // If this is a tag type that has a definition or is currently
958 // being defined, that definition is our primary context.
John McCalle78aac42010-03-10 03:28:59 +0000959 TagDecl *Tag = cast<TagDecl>(this);
John McCalle78aac42010-03-10 03:28:59 +0000960
961 if (TagDecl *Def = Tag->getDefinition())
962 return Def;
963
Richard Smith5b21db82014-04-23 18:20:42 +0000964 if (const TagType *TagTy = dyn_cast<TagType>(Tag->getTypeForDecl())) {
965 // Note, TagType::getDecl returns the (partial) definition one exists.
966 TagDecl *PossiblePartialDef = TagTy->getDecl();
967 if (PossiblePartialDef->isBeingDefined())
968 return PossiblePartialDef;
969 } else {
970 assert(isa<InjectedClassNameType>(Tag->getTypeForDecl()));
John McCalle78aac42010-03-10 03:28:59 +0000971 }
972
973 return Tag;
Douglas Gregor67a65642009-02-17 23:15:12 +0000974 }
975
Alexis Hunted053252010-05-30 07:21:58 +0000976 assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
Douglas Gregor91f84212008-12-11 16:49:14 +0000977 "Unknown DeclContext kind");
978 return this;
979 }
980}
981
Douglas Gregore57e7522012-01-07 09:11:48 +0000982void
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000983DeclContext::collectAllContexts(SmallVectorImpl<DeclContext *> &Contexts){
Douglas Gregore57e7522012-01-07 09:11:48 +0000984 Contexts.clear();
985
986 if (DeclKind != Decl::Namespace) {
987 Contexts.push_back(this);
988 return;
Douglas Gregor91f84212008-12-11 16:49:14 +0000989 }
Douglas Gregore57e7522012-01-07 09:11:48 +0000990
991 NamespaceDecl *Self = static_cast<NamespaceDecl *>(this);
Douglas Gregorec9fd132012-01-14 16:38:05 +0000992 for (NamespaceDecl *N = Self->getMostRecentDecl(); N;
993 N = N->getPreviousDecl())
Douglas Gregore57e7522012-01-07 09:11:48 +0000994 Contexts.push_back(N);
995
996 std::reverse(Contexts.begin(), Contexts.end());
Douglas Gregor91f84212008-12-11 16:49:14 +0000997}
998
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000999std::pair<Decl *, Decl *>
Bill Wendling8eb771d2012-02-22 09:51:33 +00001000DeclContext::BuildDeclChain(ArrayRef<Decl*> Decls,
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +00001001 bool FieldsAlreadyLoaded) {
Douglas Gregor781f7132012-01-06 16:59:53 +00001002 // Build up a chain of declarations via the Decl::NextInContextAndBits field.
Craig Topper36250ad2014-05-12 05:36:57 +00001003 Decl *FirstNewDecl = nullptr;
1004 Decl *PrevDecl = nullptr;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001005 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +00001006 if (FieldsAlreadyLoaded && isa<FieldDecl>(Decls[I]))
1007 continue;
1008
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001009 Decl *D = Decls[I];
1010 if (PrevDecl)
Douglas Gregor781f7132012-01-06 16:59:53 +00001011 PrevDecl->NextInContextAndBits.setPointer(D);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001012 else
1013 FirstNewDecl = D;
1014
1015 PrevDecl = D;
1016 }
1017
1018 return std::make_pair(FirstNewDecl, PrevDecl);
1019}
1020
Richard Smith645d7552013-02-07 03:37:08 +00001021/// \brief We have just acquired external visible storage, and we already have
1022/// built a lookup map. For every name in the map, pull in the new names from
1023/// the external storage.
Richard Smitha8b74592014-03-25 00:34:21 +00001024void DeclContext::reconcileExternalVisibleStorage() const {
Richard Smith9e2341d2015-03-23 03:25:59 +00001025 assert(NeedToReconcileExternalVisibleStorage && LookupPtr);
Richard Smith645d7552013-02-07 03:37:08 +00001026 NeedToReconcileExternalVisibleStorage = false;
1027
Richard Smith9e2341d2015-03-23 03:25:59 +00001028 for (auto &Lookup : *LookupPtr)
Richard Smitha8b74592014-03-25 00:34:21 +00001029 Lookup.second.setHasExternalDecls();
Richard Smith645d7552013-02-07 03:37:08 +00001030}
1031
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001032/// \brief Load the declarations within this lexical storage from an
1033/// external source.
Richard Smith18b380b2015-03-24 02:44:20 +00001034/// \return \c true if any declarations were added.
1035bool
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001036DeclContext::LoadLexicalDeclsFromExternalStorage() const {
1037 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001038 assert(hasExternalLexicalStorage() && Source && "No external storage?");
1039
Argyrios Kyrtzidis98d045e2010-07-30 10:03:23 +00001040 // Notify that we have a DeclContext that is initializing.
1041 ExternalASTSource::Deserializing ADeclContext(Source);
Richard Smith9e2341d2015-03-23 03:25:59 +00001042
Douglas Gregor3d0adb32011-07-15 21:46:17 +00001043 // Load the external declarations, if any.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001044 SmallVector<Decl*, 64> Decls;
Richard Smith18b380b2015-03-24 02:44:20 +00001045 ExternalLexicalStorage = false;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00001046 switch (Source->FindExternalLexicalDecls(this, Decls)) {
1047 case ELR_Success:
1048 break;
1049
1050 case ELR_Failure:
1051 case ELR_AlreadyLoaded:
Richard Smith18b380b2015-03-24 02:44:20 +00001052 return false;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00001053 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001054
1055 if (Decls.empty())
Richard Smith18b380b2015-03-24 02:44:20 +00001056 return false;
Richard Smith9e2341d2015-03-23 03:25:59 +00001057
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +00001058 // We may have already loaded just the fields of this record, in which case
1059 // we need to ignore them.
1060 bool FieldsAlreadyLoaded = false;
1061 if (const RecordDecl *RD = dyn_cast<RecordDecl>(this))
1062 FieldsAlreadyLoaded = RD->LoadedFieldsFromExternalStorage;
1063
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001064 // Splice the newly-read declarations into the beginning of the list
1065 // of declarations.
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001066 Decl *ExternalFirst, *ExternalLast;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00001067 std::tie(ExternalFirst, ExternalLast) =
1068 BuildDeclChain(Decls, FieldsAlreadyLoaded);
Douglas Gregor781f7132012-01-06 16:59:53 +00001069 ExternalLast->NextInContextAndBits.setPointer(FirstDecl);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001070 FirstDecl = ExternalFirst;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001071 if (!LastDecl)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001072 LastDecl = ExternalLast;
Richard Smith18b380b2015-03-24 02:44:20 +00001073 return true;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001074}
1075
John McCall75b960e2010-06-01 09:23:16 +00001076DeclContext::lookup_result
1077ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
1078 DeclarationName Name) {
1079 ASTContext &Context = DC->getParentASTContext();
1080 StoredDeclsMap *Map;
Richard Smith9e2341d2015-03-23 03:25:59 +00001081 if (!(Map = DC->LookupPtr))
John McCall75b960e2010-06-01 09:23:16 +00001082 Map = DC->CreateStoredDeclsMap(Context);
Richard Smitha8b74592014-03-25 00:34:21 +00001083 if (DC->NeedToReconcileExternalVisibleStorage)
1084 DC->reconcileExternalVisibleStorage();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001085
Richard Smith4abe0a82013-09-09 07:34:56 +00001086 (*Map)[Name].removeExternalDecls();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001087
John McCall75b960e2010-06-01 09:23:16 +00001088 return DeclContext::lookup_result();
1089}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001090
John McCall75b960e2010-06-01 09:23:16 +00001091DeclContext::lookup_result
1092ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +00001093 DeclarationName Name,
Argyrios Kyrtzidis94d3f9d2011-09-09 06:44:14 +00001094 ArrayRef<NamedDecl*> Decls) {
Dmitri Gribenko76bb5cabfa2012-09-10 21:20:09 +00001095 ASTContext &Context = DC->getParentASTContext();
John McCall75b960e2010-06-01 09:23:16 +00001096 StoredDeclsMap *Map;
Richard Smith9e2341d2015-03-23 03:25:59 +00001097 if (!(Map = DC->LookupPtr))
John McCall75b960e2010-06-01 09:23:16 +00001098 Map = DC->CreateStoredDeclsMap(Context);
Richard Smitha8b74592014-03-25 00:34:21 +00001099 if (DC->NeedToReconcileExternalVisibleStorage)
1100 DC->reconcileExternalVisibleStorage();
John McCall75b960e2010-06-01 09:23:16 +00001101
1102 StoredDeclsList &List = (*Map)[Name];
Richard Smith51445cd2013-06-24 01:46:41 +00001103
1104 // Clear out any old external visible declarations, to avoid quadratic
1105 // performance in the redeclaration checks below.
1106 List.removeExternalDecls();
1107
1108 if (!List.isNull()) {
1109 // We have both existing declarations and new declarations for this name.
1110 // Some of the declarations may simply replace existing ones. Handle those
1111 // first.
1112 llvm::SmallVector<unsigned, 8> Skip;
1113 for (unsigned I = 0, N = Decls.size(); I != N; ++I)
Richard Smithe8292b12015-02-10 03:28:10 +00001114 if (List.HandleRedeclaration(Decls[I], /*IsKnownNewer*/false))
Richard Smith51445cd2013-06-24 01:46:41 +00001115 Skip.push_back(I);
1116 Skip.push_back(Decls.size());
1117
1118 // Add in any new declarations.
1119 unsigned SkipPos = 0;
1120 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
1121 if (I == Skip[SkipPos])
1122 ++SkipPos;
1123 else
1124 List.AddSubsequentDecl(Decls[I]);
1125 }
1126 } else {
1127 // Convert the array to a StoredDeclsList.
1128 for (ArrayRef<NamedDecl*>::iterator
1129 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
1130 if (List.isNull())
1131 List.setOnlyValue(*I);
1132 else
1133 List.AddSubsequentDecl(*I);
1134 }
John McCall75b960e2010-06-01 09:23:16 +00001135 }
1136
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001137 return List.getLookupResult();
John McCall75b960e2010-06-01 09:23:16 +00001138}
1139
Aaron Ballmanda634f12014-03-07 22:17:20 +00001140DeclContext::decl_iterator DeclContext::decls_begin() const {
1141 if (hasExternalLexicalStorage())
1142 LoadLexicalDeclsFromExternalStorage();
1143 return decl_iterator(FirstDecl);
1144}
1145
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001146bool DeclContext::decls_empty() const {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001147 if (hasExternalLexicalStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001148 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001149
1150 return !FirstDecl;
1151}
1152
Sean Callanan0325fb82013-05-04 02:04:27 +00001153bool DeclContext::containsDecl(Decl *D) const {
1154 return (D->getLexicalDeclContext() == this &&
1155 (D->NextInContextAndBits.getPointer() || D == LastDecl));
1156}
1157
John McCall84d87672009-12-10 09:41:52 +00001158void DeclContext::removeDecl(Decl *D) {
1159 assert(D->getLexicalDeclContext() == this &&
1160 "decl being removed from non-lexical context");
Douglas Gregor781f7132012-01-06 16:59:53 +00001161 assert((D->NextInContextAndBits.getPointer() || D == LastDecl) &&
John McCall84d87672009-12-10 09:41:52 +00001162 "decl is not in decls list");
1163
1164 // Remove D from the decl chain. This is O(n) but hopefully rare.
1165 if (D == FirstDecl) {
1166 if (D == LastDecl)
Craig Topper36250ad2014-05-12 05:36:57 +00001167 FirstDecl = LastDecl = nullptr;
John McCall84d87672009-12-10 09:41:52 +00001168 else
Douglas Gregor781f7132012-01-06 16:59:53 +00001169 FirstDecl = D->NextInContextAndBits.getPointer();
John McCall84d87672009-12-10 09:41:52 +00001170 } else {
Douglas Gregor781f7132012-01-06 16:59:53 +00001171 for (Decl *I = FirstDecl; true; I = I->NextInContextAndBits.getPointer()) {
John McCall84d87672009-12-10 09:41:52 +00001172 assert(I && "decl not found in linked list");
Douglas Gregor781f7132012-01-06 16:59:53 +00001173 if (I->NextInContextAndBits.getPointer() == D) {
1174 I->NextInContextAndBits.setPointer(D->NextInContextAndBits.getPointer());
John McCall84d87672009-12-10 09:41:52 +00001175 if (D == LastDecl) LastDecl = I;
1176 break;
1177 }
1178 }
1179 }
1180
1181 // Mark that D is no longer in the decl chain.
Craig Topper36250ad2014-05-12 05:36:57 +00001182 D->NextInContextAndBits.setPointer(nullptr);
John McCall84d87672009-12-10 09:41:52 +00001183
1184 // Remove D from the lookup table if necessary.
1185 if (isa<NamedDecl>(D)) {
1186 NamedDecl *ND = cast<NamedDecl>(D);
1187
Axel Naumanncb2c52f2011-08-26 14:06:12 +00001188 // Remove only decls that have a name
1189 if (!ND->getDeclName()) return;
1190
Richard Smith9e2341d2015-03-23 03:25:59 +00001191 StoredDeclsMap *Map = getPrimaryContext()->LookupPtr;
John McCallc62bb642010-03-24 05:22:00 +00001192 if (!Map) return;
John McCall84d87672009-12-10 09:41:52 +00001193
John McCall84d87672009-12-10 09:41:52 +00001194 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
1195 assert(Pos != Map->end() && "no lookup entry for decl");
Axel Naumannfbc7b982011-11-08 18:21:06 +00001196 if (Pos->second.getAsVector() || Pos->second.getAsDecl() == ND)
1197 Pos->second.remove(ND);
John McCall84d87672009-12-10 09:41:52 +00001198 }
1199}
1200
John McCalld1e9d832009-08-11 06:59:38 +00001201void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner33f219d2009-02-20 00:56:18 +00001202 assert(D->getLexicalDeclContext() == this &&
1203 "Decl inserted into wrong lexical context");
Mike Stump11289f42009-09-09 15:08:12 +00001204 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor020713e2009-01-09 19:42:16 +00001205 "Decl already inserted into a DeclContext");
1206
1207 if (FirstDecl) {
Douglas Gregor781f7132012-01-06 16:59:53 +00001208 LastDecl->NextInContextAndBits.setPointer(D);
Douglas Gregor020713e2009-01-09 19:42:16 +00001209 LastDecl = D;
1210 } else {
1211 FirstDecl = LastDecl = D;
1212 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +00001213
1214 // Notify a C++ record declaration that we've added a member, so it can
1215 // update it's class-specific state.
1216 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
1217 Record->addedMember(D);
Douglas Gregor0f2a3602011-12-03 00:30:27 +00001218
1219 // If this is a newly-created (not de-serialized) import declaration, wire
1220 // it in to the list of local import declarations.
1221 if (!D->isFromASTFile()) {
1222 if (ImportDecl *Import = dyn_cast<ImportDecl>(D))
1223 D->getASTContext().addedLocalImportDecl(Import);
1224 }
John McCalld1e9d832009-08-11 06:59:38 +00001225}
1226
1227void DeclContext::addDecl(Decl *D) {
1228 addHiddenDecl(D);
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001229
1230 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Richard Smithf634c902012-03-16 06:12:59 +00001231 ND->getDeclContext()->getPrimaryContext()->
1232 makeDeclVisibleInContextWithFlags(ND, false, true);
Douglas Gregor91f84212008-12-11 16:49:14 +00001233}
1234
Sean Callanan95e74be2011-10-21 02:57:43 +00001235void DeclContext::addDeclInternal(Decl *D) {
1236 addHiddenDecl(D);
1237
1238 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Richard Smithf634c902012-03-16 06:12:59 +00001239 ND->getDeclContext()->getPrimaryContext()->
1240 makeDeclVisibleInContextWithFlags(ND, true, true);
1241}
1242
1243/// shouldBeHidden - Determine whether a declaration which was declared
1244/// within its semantic context should be invisible to qualified name lookup.
1245static bool shouldBeHidden(NamedDecl *D) {
1246 // Skip unnamed declarations.
1247 if (!D->getDeclName())
1248 return true;
1249
1250 // Skip entities that can't be found by name lookup into a particular
1251 // context.
1252 if ((D->getIdentifierNamespace() == 0 && !isa<UsingDirectiveDecl>(D)) ||
1253 D->isTemplateParameter())
1254 return true;
1255
1256 // Skip template specializations.
1257 // FIXME: This feels like a hack. Should DeclarationName support
1258 // template-ids, or is there a better way to keep specializations
1259 // from being visible?
1260 if (isa<ClassTemplateSpecializationDecl>(D))
1261 return true;
1262 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1263 if (FD->isFunctionTemplateSpecialization())
1264 return true;
1265
1266 return false;
1267}
1268
1269/// buildLookup - Build the lookup data structure with all of the
1270/// declarations in this DeclContext (and any other contexts linked
1271/// to it or transparent contexts nested within it) and return it.
Richard Smitha8b74592014-03-25 00:34:21 +00001272///
1273/// Note that the produced map may miss out declarations from an
1274/// external source. If it does, those entries will be marked with
1275/// the 'hasExternalDecls' flag.
Richard Smithf634c902012-03-16 06:12:59 +00001276StoredDeclsMap *DeclContext::buildLookup() {
1277 assert(this == getPrimaryContext() && "buildLookup called on non-primary DC");
1278
Richard Smith9e2341d2015-03-23 03:25:59 +00001279 if (!HasLazyLocalLexicalLookups && !HasLazyExternalLexicalLookups)
1280 return LookupPtr;
1281
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001282 SmallVector<DeclContext *, 2> Contexts;
Richard Smithf634c902012-03-16 06:12:59 +00001283 collectAllContexts(Contexts);
Richard Smith18b380b2015-03-24 02:44:20 +00001284
1285 if (HasLazyExternalLexicalLookups) {
1286 HasLazyExternalLexicalLookups = false;
1287 for (auto *DC : Contexts) {
1288 if (DC->hasExternalLexicalStorage())
1289 HasLazyLocalLexicalLookups |=
1290 DC->LoadLexicalDeclsFromExternalStorage();
1291 }
1292
1293 if (!HasLazyLocalLexicalLookups)
1294 return LookupPtr;
1295 }
1296
1297 for (auto *DC : Contexts)
1298 buildLookupImpl(DC, hasExternalVisibleStorage());
Richard Smithf634c902012-03-16 06:12:59 +00001299
1300 // We no longer have any lazy decls.
Richard Smith9e2341d2015-03-23 03:25:59 +00001301 HasLazyLocalLexicalLookups = false;
1302 return LookupPtr;
Richard Smithf634c902012-03-16 06:12:59 +00001303}
1304
1305/// buildLookupImpl - Build part of the lookup data structure for the
1306/// declarations contained within DCtx, which will either be this
1307/// DeclContext, a DeclContext linked to it, or a transparent context
1308/// nested within it.
Richard Smitha3271c12015-02-07 00:45:52 +00001309void DeclContext::buildLookupImpl(DeclContext *DCtx, bool Internal) {
Richard Smith9e2341d2015-03-23 03:25:59 +00001310 for (Decl *D : DCtx->noload_decls()) {
Richard Smithf634c902012-03-16 06:12:59 +00001311 // Insert this declaration into the lookup structure, but only if
1312 // it's semantically within its decl context. Any other decls which
1313 // should be found in this context are added eagerly.
Richard Smithcf4ab522013-06-24 07:20:36 +00001314 //
1315 // If it's from an AST file, don't add it now. It'll get handled by
1316 // FindExternalVisibleDeclsByName if needed. Exception: if we're not
1317 // in C++, we do not track external visible decls for the TU, so in
1318 // that case we need to collect them all here.
Richard Smithf634c902012-03-16 06:12:59 +00001319 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Richard Smithcf4ab522013-06-24 07:20:36 +00001320 if (ND->getDeclContext() == DCtx && !shouldBeHidden(ND) &&
1321 (!ND->isFromASTFile() ||
1322 (isTranslationUnit() &&
1323 !getParentASTContext().getLangOpts().CPlusPlus)))
Richard Smitha3271c12015-02-07 00:45:52 +00001324 makeDeclVisibleInContextImpl(ND, Internal);
Richard Smithf634c902012-03-16 06:12:59 +00001325
1326 // If this declaration is itself a transparent declaration context
1327 // or inline namespace, add the members of this declaration of that
1328 // context (recursively).
1329 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(D))
1330 if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
Richard Smith9e2341d2015-03-23 03:25:59 +00001331 buildLookupImpl(InnerCtx, Internal);
Richard Smithf634c902012-03-16 06:12:59 +00001332 }
Sean Callanan95e74be2011-10-21 02:57:43 +00001333}
1334
Richard Smith40c78062015-02-21 02:31:57 +00001335NamedDecl *const DeclContextLookupResult::SingleElementDummyList = nullptr;
1336
Mike Stump11289f42009-09-09 15:08:12 +00001337DeclContext::lookup_result
Richard Smith40c78062015-02-21 02:31:57 +00001338DeclContext::lookup(DeclarationName Name) const {
Nick Lewycky2bd636f2012-03-13 04:12:34 +00001339 assert(DeclKind != Decl::LinkageSpec &&
1340 "Should not perform lookups into linkage specs!");
1341
Richard Smith40c78062015-02-21 02:31:57 +00001342 const DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor91f84212008-12-11 16:49:14 +00001343 if (PrimaryContext != this)
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001344 return PrimaryContext->lookup(Name);
Douglas Gregor91f84212008-12-11 16:49:14 +00001345
Richard Smith8cebe372015-02-25 22:20:13 +00001346 // If we have an external source, ensure that any later redeclarations of this
1347 // context have been loaded, since they may add names to the result of this
1348 // lookup (or add external visible storage).
1349 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1350 if (Source)
1351 (void)cast<Decl>(this)->getMostRecentDecl();
Richard Smithbb853c72014-08-13 01:23:33 +00001352
Richard Smith05afe5e2012-03-13 03:12:56 +00001353 if (hasExternalVisibleStorage()) {
Richard Smith8cebe372015-02-25 22:20:13 +00001354 assert(Source && "external visible storage but no external source?");
1355
Richard Smitha8b74592014-03-25 00:34:21 +00001356 if (NeedToReconcileExternalVisibleStorage)
1357 reconcileExternalVisibleStorage();
1358
Richard Smith9e2341d2015-03-23 03:25:59 +00001359 StoredDeclsMap *Map = LookupPtr;
Richard Smitha8b74592014-03-25 00:34:21 +00001360
Richard Smith9e2341d2015-03-23 03:25:59 +00001361 if (HasLazyLocalLexicalLookups || HasLazyExternalLexicalLookups)
Richard Smith40c78062015-02-21 02:31:57 +00001362 // FIXME: Make buildLookup const?
1363 Map = const_cast<DeclContext*>(this)->buildLookup();
Richard Smith645d7552013-02-07 03:37:08 +00001364
Richard Smith75fc3bf2013-02-08 00:37:45 +00001365 if (!Map)
1366 Map = CreateStoredDeclsMap(getParentASTContext());
1367
Richard Smith4abe0a82013-09-09 07:34:56 +00001368 // If we have a lookup result with no external decls, we are done.
Richard Smith75fc3bf2013-02-08 00:37:45 +00001369 std::pair<StoredDeclsMap::iterator, bool> R =
1370 Map->insert(std::make_pair(Name, StoredDeclsList()));
Richard Smith4abe0a82013-09-09 07:34:56 +00001371 if (!R.second && !R.first->second.hasExternalDecls())
Richard Smith75fc3bf2013-02-08 00:37:45 +00001372 return R.first->second.getLookupResult();
Richard Smithf634c902012-03-16 06:12:59 +00001373
Richard Smith309271b2014-03-04 00:21:14 +00001374 if (Source->FindExternalVisibleDeclsByName(this, Name) || !R.second) {
Richard Smith9e2341d2015-03-23 03:25:59 +00001375 if (StoredDeclsMap *Map = LookupPtr) {
Richard Smith9ce12e32013-02-07 03:30:24 +00001376 StoredDeclsMap::iterator I = Map->find(Name);
1377 if (I != Map->end())
1378 return I->second.getLookupResult();
1379 }
1380 }
1381
Richard Smith40c78062015-02-21 02:31:57 +00001382 return lookup_result();
John McCall75b960e2010-06-01 09:23:16 +00001383 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001384
Richard Smith9e2341d2015-03-23 03:25:59 +00001385 StoredDeclsMap *Map = LookupPtr;
1386 if (HasLazyLocalLexicalLookups || HasLazyExternalLexicalLookups)
Richard Smith40c78062015-02-21 02:31:57 +00001387 Map = const_cast<DeclContext*>(this)->buildLookup();
Richard Smithf634c902012-03-16 06:12:59 +00001388
1389 if (!Map)
Richard Smith40c78062015-02-21 02:31:57 +00001390 return lookup_result();
Richard Smithf634c902012-03-16 06:12:59 +00001391
1392 StoredDeclsMap::iterator I = Map->find(Name);
1393 if (I == Map->end())
Richard Smith40c78062015-02-21 02:31:57 +00001394 return lookup_result();
Richard Smithf634c902012-03-16 06:12:59 +00001395
1396 return I->second.getLookupResult();
Douglas Gregor91f84212008-12-11 16:49:14 +00001397}
1398
Richard Smith95d99302013-07-13 02:00:19 +00001399DeclContext::lookup_result
1400DeclContext::noload_lookup(DeclarationName Name) {
1401 assert(DeclKind != Decl::LinkageSpec &&
1402 "Should not perform lookups into linkage specs!");
Richard Smith95d99302013-07-13 02:00:19 +00001403
1404 DeclContext *PrimaryContext = getPrimaryContext();
1405 if (PrimaryContext != this)
1406 return PrimaryContext->noload_lookup(Name);
1407
Richard Smith9e2341d2015-03-23 03:25:59 +00001408 // If we have any lazy lexical declarations not in our lookup map, add them
1409 // now. Don't import any external declarations, not even if we know we have
1410 // some missing from the external visible lookups.
1411 if (HasLazyLocalLexicalLookups) {
Vince Harrona3ea9a42015-03-22 05:59:59 +00001412 SmallVector<DeclContext *, 2> Contexts;
1413 collectAllContexts(Contexts);
1414 for (unsigned I = 0, N = Contexts.size(); I != N; ++I)
Richard Smith9e2341d2015-03-23 03:25:59 +00001415 buildLookupImpl(Contexts[I], hasExternalVisibleStorage());
1416 HasLazyLocalLexicalLookups = false;
Vince Harrona3ea9a42015-03-22 05:59:59 +00001417 }
1418
Richard Smith9e2341d2015-03-23 03:25:59 +00001419 StoredDeclsMap *Map = LookupPtr;
Richard Smith95d99302013-07-13 02:00:19 +00001420 if (!Map)
Richard Smith40c78062015-02-21 02:31:57 +00001421 return lookup_result();
Richard Smith95d99302013-07-13 02:00:19 +00001422
1423 StoredDeclsMap::iterator I = Map->find(Name);
Craig Topper36250ad2014-05-12 05:36:57 +00001424 return I != Map->end() ? I->second.getLookupResult()
Richard Smith40c78062015-02-21 02:31:57 +00001425 : lookup_result();
Richard Smith95d99302013-07-13 02:00:19 +00001426}
1427
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001428void DeclContext::localUncachedLookup(DeclarationName Name,
1429 SmallVectorImpl<NamedDecl *> &Results) {
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001430 Results.clear();
1431
1432 // If there's no external storage, just perform a normal lookup and copy
1433 // the results.
Douglas Gregordd6006f2012-07-17 21:16:27 +00001434 if (!hasExternalVisibleStorage() && !hasExternalLexicalStorage() && Name) {
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001435 lookup_result LookupResults = lookup(Name);
David Blaikieff7d47a2012-12-19 00:45:41 +00001436 Results.insert(Results.end(), LookupResults.begin(), LookupResults.end());
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001437 return;
1438 }
1439
1440 // If we have a lookup table, check there first. Maybe we'll get lucky.
Richard Smith9e2341d2015-03-23 03:25:59 +00001441 // FIXME: Should we be checking these flags on the primary context?
1442 if (Name && !HasLazyLocalLexicalLookups && !HasLazyExternalLexicalLookups) {
1443 if (StoredDeclsMap *Map = LookupPtr) {
Douglas Gregordd6006f2012-07-17 21:16:27 +00001444 StoredDeclsMap::iterator Pos = Map->find(Name);
1445 if (Pos != Map->end()) {
1446 Results.insert(Results.end(),
David Blaikieff7d47a2012-12-19 00:45:41 +00001447 Pos->second.getLookupResult().begin(),
1448 Pos->second.getLookupResult().end());
Douglas Gregordd6006f2012-07-17 21:16:27 +00001449 return;
1450 }
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001451 }
1452 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00001453
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001454 // Slow case: grovel through the declarations in our chain looking for
1455 // matches.
Richard Smith9e2341d2015-03-23 03:25:59 +00001456 // FIXME: If we have lazy external declarations, this will not find them!
1457 // FIXME: Should we CollectAllContexts and walk them all here?
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001458 for (Decl *D = FirstDecl; D; D = D->getNextDeclInContext()) {
1459 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1460 if (ND->getDeclName() == Name)
1461 Results.push_back(ND);
1462 }
1463}
1464
Sebastian Redl50c68252010-08-31 00:36:30 +00001465DeclContext *DeclContext::getRedeclContext() {
Chris Lattner17a1bfa2009-03-27 19:19:59 +00001466 DeclContext *Ctx = this;
Sebastian Redlbd595762010-08-31 20:53:31 +00001467 // Skip through transparent contexts.
1468 while (Ctx->isTransparentContext())
Douglas Gregor6ad0ef52009-01-06 23:51:29 +00001469 Ctx = Ctx->getParent();
1470 return Ctx;
1471}
1472
Douglas Gregorf47b9112009-02-25 22:02:03 +00001473DeclContext *DeclContext::getEnclosingNamespaceContext() {
1474 DeclContext *Ctx = this;
1475 // Skip through non-namespace, non-translation-unit contexts.
Sebastian Redl4f08c962010-08-31 00:36:23 +00001476 while (!Ctx->isFileContext())
Douglas Gregorf47b9112009-02-25 22:02:03 +00001477 Ctx = Ctx->getParent();
1478 return Ctx->getPrimaryContext();
1479}
1480
Reid Klecknerd60b82f2014-11-17 23:36:45 +00001481RecordDecl *DeclContext::getOuterLexicalRecordContext() {
1482 // Loop until we find a non-record context.
1483 RecordDecl *OutermostRD = nullptr;
1484 DeclContext *DC = this;
1485 while (DC->isRecord()) {
1486 OutermostRD = cast<RecordDecl>(DC);
1487 DC = DC->getLexicalParent();
1488 }
1489 return OutermostRD;
1490}
1491
Sebastian Redl50c68252010-08-31 00:36:30 +00001492bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
1493 // For non-file contexts, this is equivalent to Equals.
1494 if (!isFileContext())
1495 return O->Equals(this);
1496
1497 do {
1498 if (O->Equals(this))
1499 return true;
1500
1501 const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O);
1502 if (!NS || !NS->isInline())
1503 break;
1504 O = NS->getParent();
1505 } while (O);
1506
1507 return false;
1508}
1509
Richard Smithf634c902012-03-16 06:12:59 +00001510void DeclContext::makeDeclVisibleInContext(NamedDecl *D) {
1511 DeclContext *PrimaryDC = this->getPrimaryContext();
1512 DeclContext *DeclDC = D->getDeclContext()->getPrimaryContext();
1513 // If the decl is being added outside of its semantic decl context, we
1514 // need to ensure that we eagerly build the lookup information for it.
1515 PrimaryDC->makeDeclVisibleInContextWithFlags(D, false, PrimaryDC == DeclDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00001516}
1517
Richard Smithf634c902012-03-16 06:12:59 +00001518void DeclContext::makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal,
1519 bool Recoverable) {
1520 assert(this == getPrimaryContext() && "expected a primary DC");
Sean Callanan95e74be2011-10-21 02:57:43 +00001521
Richard Smith05afe5e2012-03-13 03:12:56 +00001522 // Skip declarations within functions.
Richard Smith114394f2013-08-09 04:35:01 +00001523 if (isFunctionOrMethod())
Richard Smith05afe5e2012-03-13 03:12:56 +00001524 return;
1525
Richard Smithf634c902012-03-16 06:12:59 +00001526 // Skip declarations which should be invisible to name lookup.
1527 if (shouldBeHidden(D))
1528 return;
1529
1530 // If we already have a lookup data structure, perform the insertion into
1531 // it. If we might have externally-stored decls with this name, look them
1532 // up and perform the insertion. If this decl was declared outside its
1533 // semantic context, buildLookup won't add it, so add it now.
1534 //
1535 // FIXME: As a performance hack, don't add such decls into the translation
1536 // unit unless we're in C++, since qualified lookup into the TU is never
1537 // performed.
Richard Smith9e2341d2015-03-23 03:25:59 +00001538 if (LookupPtr || hasExternalVisibleStorage() ||
Richard Smithf634c902012-03-16 06:12:59 +00001539 ((!Recoverable || D->getDeclContext() != D->getLexicalDeclContext()) &&
1540 (getParentASTContext().getLangOpts().CPlusPlus ||
1541 !isTranslationUnit()))) {
1542 // If we have lazily omitted any decls, they might have the same name as
1543 // the decl which we are adding, so build a full lookup table before adding
1544 // this decl.
1545 buildLookup();
1546 makeDeclVisibleInContextImpl(D, Internal);
1547 } else {
Richard Smith9e2341d2015-03-23 03:25:59 +00001548 HasLazyLocalLexicalLookups = true;
Richard Smithf634c902012-03-16 06:12:59 +00001549 }
1550
1551 // If we are a transparent context or inline namespace, insert into our
1552 // parent context, too. This operation is recursive.
1553 if (isTransparentContext() || isInlineNamespace())
1554 getParent()->getPrimaryContext()->
1555 makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
1556
1557 Decl *DCAsDecl = cast<Decl>(this);
1558 // Notify that a decl was made visible unless we are a Tag being defined.
1559 if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined()))
1560 if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
1561 L->AddedVisibleDecl(this, D);
1562}
1563
1564void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal) {
1565 // Find or create the stored declaration map.
Richard Smith9e2341d2015-03-23 03:25:59 +00001566 StoredDeclsMap *Map = LookupPtr;
Richard Smithf634c902012-03-16 06:12:59 +00001567 if (!Map) {
1568 ASTContext *C = &getParentASTContext();
1569 Map = CreateStoredDeclsMap(*C);
Argyrios Kyrtzidise51e5542010-07-04 21:44:25 +00001570 }
1571
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001572 // If there is an external AST source, load any declarations it knows about
1573 // with this declaration's name.
1574 // If the lookup table contains an entry about this name it means that we
1575 // have already checked the external source.
Sean Callanan95e74be2011-10-21 02:57:43 +00001576 if (!Internal)
1577 if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
1578 if (hasExternalVisibleStorage() &&
Richard Smithf634c902012-03-16 06:12:59 +00001579 Map->find(D->getDeclName()) == Map->end())
Sean Callanan95e74be2011-10-21 02:57:43 +00001580 Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001581
Douglas Gregor91f84212008-12-11 16:49:14 +00001582 // Insert this declaration into the map.
Richard Smithf634c902012-03-16 06:12:59 +00001583 StoredDeclsList &DeclNameEntries = (*Map)[D->getDeclName()];
Richard Smith4abe0a82013-09-09 07:34:56 +00001584
1585 if (Internal) {
1586 // If this is being added as part of loading an external declaration,
1587 // this may not be the only external declaration with this name.
1588 // In this case, we never try to replace an existing declaration; we'll
1589 // handle that when we finalize the list of declarations for this name.
1590 DeclNameEntries.setHasExternalDecls();
1591 DeclNameEntries.AddSubsequentDecl(D);
1592 return;
1593 }
1594
Richard Smitha3271c12015-02-07 00:45:52 +00001595 if (DeclNameEntries.isNull()) {
Chris Lattnercaae7162009-02-20 01:44:05 +00001596 DeclNameEntries.setOnlyValue(D);
Richard Smithf634c902012-03-16 06:12:59 +00001597 return;
Douglas Gregor91f84212008-12-11 16:49:14 +00001598 }
Chris Lattner24e24d52009-02-20 00:55:03 +00001599
Richard Smithe8292b12015-02-10 03:28:10 +00001600 if (DeclNameEntries.HandleRedeclaration(D, /*IsKnownNewer*/!Internal)) {
Richard Smithf634c902012-03-16 06:12:59 +00001601 // This declaration has replaced an existing one for which
1602 // declarationReplaces returns true.
1603 return;
1604 }
Mike Stump11289f42009-09-09 15:08:12 +00001605
Richard Smithf634c902012-03-16 06:12:59 +00001606 // Put this declaration into the appropriate slot.
1607 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor91f84212008-12-11 16:49:14 +00001608}
Douglas Gregor889ceb72009-02-03 19:21:40 +00001609
Richard Smith40c78062015-02-21 02:31:57 +00001610UsingDirectiveDecl *DeclContext::udir_iterator::operator*() const {
1611 return cast<UsingDirectiveDecl>(*I);
1612}
1613
Douglas Gregor889ceb72009-02-03 19:21:40 +00001614/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
1615/// this context.
Aaron Ballman804a7fb2014-03-17 17:14:12 +00001616DeclContext::udir_range DeclContext::using_directives() const {
Richard Smith05afe5e2012-03-13 03:12:56 +00001617 // FIXME: Use something more efficient than normal lookup for using
1618 // directives. In C++, using directives are looked up more than anything else.
Richard Smithcf4bdde2015-02-21 02:45:19 +00001619 lookup_result Result = lookup(UsingDirectiveDecl::getName());
Richard Smith40c78062015-02-21 02:31:57 +00001620 return udir_range(Result.begin(), Result.end());
Douglas Gregor889ceb72009-02-03 19:21:40 +00001621}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001622
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001623//===----------------------------------------------------------------------===//
1624// Creation and Destruction of StoredDeclsMaps. //
1625//===----------------------------------------------------------------------===//
1626
John McCallc62bb642010-03-24 05:22:00 +00001627StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
Richard Smith9e2341d2015-03-23 03:25:59 +00001628 assert(!LookupPtr && "context already has a decls map");
John McCallc62bb642010-03-24 05:22:00 +00001629 assert(getPrimaryContext() == this &&
1630 "creating decls map on non-primary context");
1631
1632 StoredDeclsMap *M;
1633 bool Dependent = isDependentContext();
1634 if (Dependent)
1635 M = new DependentStoredDeclsMap();
1636 else
1637 M = new StoredDeclsMap();
1638 M->Previous = C.LastSDM;
1639 C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
Richard Smith9e2341d2015-03-23 03:25:59 +00001640 LookupPtr = M;
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001641 return M;
1642}
1643
1644void ASTContext::ReleaseDeclContextMaps() {
John McCallc62bb642010-03-24 05:22:00 +00001645 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1646 // pointer because the subclass doesn't add anything that needs to
1647 // be deleted.
John McCallc62bb642010-03-24 05:22:00 +00001648 StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1649}
1650
1651void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1652 while (Map) {
1653 // Advance the iteration before we invalidate memory.
1654 llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1655
1656 if (Dependent)
1657 delete static_cast<DependentStoredDeclsMap*>(Map);
1658 else
1659 delete Map;
1660
1661 Map = Next.getPointer();
1662 Dependent = Next.getInt();
1663 }
1664}
1665
John McCallc62bb642010-03-24 05:22:00 +00001666DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1667 DeclContext *Parent,
1668 const PartialDiagnostic &PDiag) {
1669 assert(Parent->isDependentContext()
1670 && "cannot iterate dependent diagnostics of non-dependent context");
1671 Parent = Parent->getPrimaryContext();
Richard Smith9e2341d2015-03-23 03:25:59 +00001672 if (!Parent->LookupPtr)
John McCallc62bb642010-03-24 05:22:00 +00001673 Parent->CreateStoredDeclsMap(C);
1674
Richard Smith9e2341d2015-03-23 03:25:59 +00001675 DependentStoredDeclsMap *Map =
1676 static_cast<DependentStoredDeclsMap *>(Parent->LookupPtr);
John McCallc62bb642010-03-24 05:22:00 +00001677
Douglas Gregora55530e2010-03-29 23:56:53 +00001678 // Allocate the copy of the PartialDiagnostic via the ASTContext's
Douglas Gregor89336232010-03-29 23:34:08 +00001679 // BumpPtrAllocator, rather than the ASTContext itself.
Craig Topper36250ad2014-05-12 05:36:57 +00001680 PartialDiagnostic::Storage *DiagStorage = nullptr;
Douglas Gregora55530e2010-03-29 23:56:53 +00001681 if (PDiag.hasStorage())
1682 DiagStorage = new (C) PartialDiagnostic::Storage;
1683
1684 DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
John McCallc62bb642010-03-24 05:22:00 +00001685
1686 // TODO: Maybe we shouldn't reverse the order during insertion.
1687 DD->NextDiagnostic = Map->FirstDiagnostic;
1688 Map->FirstDiagnostic = DD;
1689
1690 return DD;
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001691}