blob: 79cadcfcb167bccfd7f730a8fb8c41494bdda1f7 [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
Eli Friedman7dbab8a2008-06-07 16:52:53 +000083const char *Decl::getDeclKindName() const {
84 switch (DeclKind) {
David Blaikie83d382b2011-09-23 05:06:16 +000085 default: llvm_unreachable("Declaration not in DeclNodes.inc!");
Alexis Hunted053252010-05-30 07:21:58 +000086#define DECL(DERIVED, BASE) case DERIVED: return #DERIVED;
87#define ABSTRACT_DECL(DECL)
88#include "clang/AST/DeclNodes.inc"
Eli Friedman7dbab8a2008-06-07 16:52:53 +000089 }
90}
91
Douglas Gregor90d47172010-03-05 00:26:45 +000092void Decl::setInvalidDecl(bool Invalid) {
93 InvalidDecl = Invalid;
Alp Tokera5d64592013-12-21 01:10:54 +000094 assert(!isa<TagDecl>(this) || !cast<TagDecl>(this)->isCompleteDefinition());
Argyrios Kyrtzidisb7d1ca22012-03-09 21:09:04 +000095 if (Invalid && !isa<ParmVarDecl>(this)) {
Douglas Gregor90d47172010-03-05 00:26:45 +000096 // Defensive maneuver for ill-formed code: we're likely not to make it to
97 // a point where we set the access specifier, so default it to "public"
98 // to avoid triggering asserts elsewhere in the front end.
99 setAccess(AS_public);
100 }
101}
102
Steve Naroff5faaef72009-01-20 19:53:53 +0000103const char *DeclContext::getDeclKindName() const {
104 switch (DeclKind) {
David Blaikie83d382b2011-09-23 05:06:16 +0000105 default: llvm_unreachable("Declaration context not in DeclNodes.inc!");
Alexis Hunted053252010-05-30 07:21:58 +0000106#define DECL(DERIVED, BASE) case Decl::DERIVED: return #DERIVED;
107#define ABSTRACT_DECL(DECL)
108#include "clang/AST/DeclNodes.inc"
Steve Naroff5faaef72009-01-20 19:53:53 +0000109 }
110}
111
Daniel Dunbar62905572012-03-05 21:42:49 +0000112bool Decl::StatisticsEnabled = false;
113void Decl::EnableStatistics() {
114 StatisticsEnabled = true;
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000115}
116
117void Decl::PrintStats() {
Chandler Carruthbfb154a2011-07-04 06:13:27 +0000118 llvm::errs() << "\n*** Decl Stats:\n";
Mike Stump11289f42009-09-09 15:08:12 +0000119
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +0000120 int totalDecls = 0;
Alexis Hunted053252010-05-30 07:21:58 +0000121#define DECL(DERIVED, BASE) totalDecls += n##DERIVED##s;
122#define ABSTRACT_DECL(DECL)
123#include "clang/AST/DeclNodes.inc"
Chandler Carruthbfb154a2011-07-04 06:13:27 +0000124 llvm::errs() << " " << totalDecls << " decls total.\n";
Mike Stump11289f42009-09-09 15:08:12 +0000125
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +0000126 int totalBytes = 0;
Alexis Hunted053252010-05-30 07:21:58 +0000127#define DECL(DERIVED, BASE) \
128 if (n##DERIVED##s > 0) { \
129 totalBytes += (int)(n##DERIVED##s * sizeof(DERIVED##Decl)); \
Chandler Carruthbfb154a2011-07-04 06:13:27 +0000130 llvm::errs() << " " << n##DERIVED##s << " " #DERIVED " decls, " \
131 << sizeof(DERIVED##Decl) << " each (" \
132 << n##DERIVED##s * sizeof(DERIVED##Decl) \
133 << " bytes)\n"; \
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +0000134 }
Alexis Hunted053252010-05-30 07:21:58 +0000135#define ABSTRACT_DECL(DECL)
136#include "clang/AST/DeclNodes.inc"
Mike Stump11289f42009-09-09 15:08:12 +0000137
Chandler Carruthbfb154a2011-07-04 06:13:27 +0000138 llvm::errs() << "Total bytes = " << totalBytes << "\n";
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000139}
140
Alexis Hunted053252010-05-30 07:21:58 +0000141void Decl::add(Kind k) {
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000142 switch (k) {
Alexis Hunted053252010-05-30 07:21:58 +0000143#define DECL(DERIVED, BASE) case DERIVED: ++n##DERIVED##s; break;
144#define ABSTRACT_DECL(DECL)
145#include "clang/AST/DeclNodes.inc"
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000146 }
147}
148
Anders Carlssonaa73b912009-06-13 00:08:58 +0000149bool Decl::isTemplateParameterPack() const {
150 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
151 return TTP->isParameterPack();
Douglas Gregorda3cc0d2010-12-23 23:51:58 +0000152 if (const NonTypeTemplateParmDecl *NTTP
Douglas Gregorf5500772011-01-05 15:48:55 +0000153 = dyn_cast<NonTypeTemplateParmDecl>(this))
Douglas Gregorda3cc0d2010-12-23 23:51:58 +0000154 return NTTP->isParameterPack();
Douglas Gregorf5500772011-01-05 15:48:55 +0000155 if (const TemplateTemplateParmDecl *TTP
156 = dyn_cast<TemplateTemplateParmDecl>(this))
157 return TTP->isParameterPack();
Anders Carlssonaa73b912009-06-13 00:08:58 +0000158 return false;
159}
160
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +0000161bool Decl::isParameterPack() const {
162 if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(this))
163 return Parm->isParameterPack();
164
165 return isTemplateParameterPack();
166}
167
Alp Tokera2794f92014-01-22 07:29:52 +0000168FunctionDecl *Decl::getAsFunction() {
169 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
170 return FD;
171 if (const FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(this))
172 return FTD->getTemplatedDecl();
Craig Topper36250ad2014-05-12 05:36:57 +0000173 return nullptr;
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000174}
175
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000176bool Decl::isTemplateDecl() const {
177 return isa<TemplateDecl>(this);
178}
179
Argyrios Kyrtzidis0ce4c9a2011-09-28 02:45:33 +0000180const DeclContext *Decl::getParentFunctionOrMethod() const {
181 for (const DeclContext *DC = getDeclContext();
182 DC && !DC->isTranslationUnit() && !DC->isNamespace();
Douglas Gregorf5974fa2010-01-16 20:21:20 +0000183 DC = DC->getParent())
184 if (DC->isFunctionOrMethod())
Argyrios Kyrtzidis0ce4c9a2011-09-28 02:45:33 +0000185 return DC;
Douglas Gregorf5974fa2010-01-16 20:21:20 +0000186
Craig Topper36250ad2014-05-12 05:36:57 +0000187 return nullptr;
Douglas Gregorf5974fa2010-01-16 20:21:20 +0000188}
189
Douglas Gregor133eddd2011-02-17 08:47:29 +0000190
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000191//===----------------------------------------------------------------------===//
Chris Lattnereae6cb62009-03-05 08:00:35 +0000192// PrettyStackTraceDecl Implementation
193//===----------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000194
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000195void PrettyStackTraceDecl::print(raw_ostream &OS) const {
Chris Lattnereae6cb62009-03-05 08:00:35 +0000196 SourceLocation TheLoc = Loc;
197 if (TheLoc.isInvalid() && TheDecl)
198 TheLoc = TheDecl->getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000199
Chris Lattnereae6cb62009-03-05 08:00:35 +0000200 if (TheLoc.isValid()) {
201 TheLoc.print(OS, SM);
202 OS << ": ";
203 }
204
205 OS << Message;
206
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +0000207 if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl)) {
208 OS << " '";
209 DN->printQualifiedName(OS);
210 OS << '\'';
211 }
Chris Lattnereae6cb62009-03-05 08:00:35 +0000212 OS << '\n';
213}
Mike Stump11289f42009-09-09 15:08:12 +0000214
Chris Lattnereae6cb62009-03-05 08:00:35 +0000215//===----------------------------------------------------------------------===//
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000216// Decl Implementation
217//===----------------------------------------------------------------------===//
218
Douglas Gregorb11aad82011-02-19 18:51:44 +0000219// Out-of-line virtual method providing a home for Decl.
220Decl::~Decl() { }
Douglas Gregora43942a2011-02-17 07:02:32 +0000221
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000222void Decl::setDeclContext(DeclContext *DC) {
Chris Lattnerb81eb052009-03-29 06:06:59 +0000223 DeclCtx = DC;
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000224}
225
226void Decl::setLexicalDeclContext(DeclContext *DC) {
227 if (DC == getLexicalDeclContext())
228 return;
229
230 if (isInSemaDC()) {
Argyrios Kyrtzidis6f40eb72012-02-09 02:44:08 +0000231 setDeclContextsImpl(getDeclContext(), DC, getASTContext());
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000232 } else {
233 getMultipleDC()->LexicalDC = DC;
234 }
235}
236
Argyrios Kyrtzidis6f40eb72012-02-09 02:44:08 +0000237void Decl::setDeclContextsImpl(DeclContext *SemaDC, DeclContext *LexicalDC,
238 ASTContext &Ctx) {
239 if (SemaDC == LexicalDC) {
240 DeclCtx = SemaDC;
241 } else {
242 Decl::MultipleDC *MDC = new (Ctx) Decl::MultipleDC();
243 MDC->SemanticDC = SemaDC;
244 MDC->LexicalDC = LexicalDC;
245 DeclCtx = MDC;
246 }
247}
248
John McCall4fa53422009-10-01 00:25:31 +0000249bool Decl::isInAnonymousNamespace() const {
250 const DeclContext *DC = getDeclContext();
251 do {
252 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
253 if (ND->isAnonymousNamespace())
254 return true;
255 } while ((DC = DC->getParent()));
256
257 return false;
258}
259
Richard Trieuc771d5d2014-05-28 02:16:01 +0000260bool Decl::isInStdNamespace() const {
261 return getDeclContext()->isStdNamespace();
262}
263
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000264TranslationUnitDecl *Decl::getTranslationUnitDecl() {
Argyrios Kyrtzidis4e1a72b2009-06-30 02:34:53 +0000265 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
266 return TUD;
267
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000268 DeclContext *DC = getDeclContext();
269 assert(DC && "This decl is not contained in a translation unit!");
Mike Stump11289f42009-09-09 15:08:12 +0000270
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000271 while (!DC->isTranslationUnit()) {
272 DC = DC->getParent();
273 assert(DC && "This decl is not contained in a translation unit!");
274 }
Mike Stump11289f42009-09-09 15:08:12 +0000275
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000276 return cast<TranslationUnitDecl>(DC);
277}
278
279ASTContext &Decl::getASTContext() const {
Mike Stump11289f42009-09-09 15:08:12 +0000280 return getTranslationUnitDecl()->getASTContext();
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000281}
282
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +0000283ASTMutationListener *Decl::getASTMutationListener() const {
284 return getASTContext().getASTMutationListener();
285}
286
Benjamin Kramerea70eb32012-12-01 15:09:41 +0000287unsigned Decl::getMaxAlignment() const {
288 if (!hasAttrs())
289 return 0;
290
291 unsigned Align = 0;
292 const AttrVec &V = getAttrs();
293 ASTContext &Ctx = getASTContext();
294 specific_attr_iterator<AlignedAttr> I(V.begin()), E(V.end());
295 for (; I != E; ++I)
296 Align = std::max(Align, I->getAlignment(Ctx));
297 return Align;
298}
299
Douglas Gregorebada0772010-06-17 23:14:26 +0000300bool Decl::isUsed(bool CheckUsedAttr) const {
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000301 if (Used)
302 return true;
303
304 // Check for used attribute.
Douglas Gregorebada0772010-06-17 23:14:26 +0000305 if (CheckUsedAttr && hasAttr<UsedAttr>())
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000306 return true;
Rafael Espindola99e3bfb2012-11-23 16:26:30 +0000307
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000308 return false;
309}
310
Eli Friedman276dd182013-09-05 00:02:25 +0000311void Decl::markUsed(ASTContext &C) {
312 if (Used)
313 return;
314
315 if (C.getASTMutationListener())
316 C.getASTMutationListener()->DeclarationMarkedUsed(this);
317
318 Used = true;
319}
320
Argyrios Kyrtzidis16180232011-04-19 19:51:10 +0000321bool Decl::isReferenced() const {
322 if (Referenced)
323 return true;
324
325 // Check redeclarations.
Aaron Ballman86c93902014-03-06 23:45:36 +0000326 for (auto I : redecls())
Argyrios Kyrtzidis16180232011-04-19 19:51:10 +0000327 if (I->Referenced)
328 return true;
329
330 return false;
331}
332
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000333/// \brief Determine the availability of the given declaration based on
334/// the target platform.
335///
336/// When it returns an availability result other than \c AR_Available,
337/// if the \p Message parameter is non-NULL, it will be set to a
338/// string describing why the entity is unavailable.
339///
340/// FIXME: Make these strings localizable, since they end up in
341/// diagnostics.
342static AvailabilityResult CheckAvailability(ASTContext &Context,
343 const AvailabilityAttr *A,
344 std::string *Message) {
Bob Wilsonb111ec92015-03-02 19:01:14 +0000345 VersionTuple TargetMinVersion =
346 Context.getTargetInfo().getPlatformMinVersion();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000347
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000348 if (TargetMinVersion.empty())
349 return AR_Available;
350
Bob Wilsonb111ec92015-03-02 19:01:14 +0000351 // Check if this is an App Extension "platform", and if so chop off
352 // the suffix for matching with the actual platform.
353 StringRef ActualPlatform = A->getPlatform()->getName();
354 StringRef RealizedPlatform = ActualPlatform;
355 if (Context.getLangOpts().AppExt) {
356 size_t suffix = RealizedPlatform.rfind("_app_extension");
357 if (suffix != StringRef::npos)
358 RealizedPlatform = RealizedPlatform.slice(0, suffix);
359 }
360
361 StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
362
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000363 // Match the platform name.
Bob Wilsonb111ec92015-03-02 19:01:14 +0000364 if (RealizedPlatform != TargetPlatform)
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000365 return AR_Available;
Bob Wilsonb111ec92015-03-02 19:01:14 +0000366
367 StringRef PrettyPlatformName
368 = AvailabilityAttr::getPrettyPlatformName(ActualPlatform);
369
370 if (PrettyPlatformName.empty())
371 PrettyPlatformName = ActualPlatform;
372
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000373 std::string HintMessage;
374 if (!A->getMessage().empty()) {
375 HintMessage = " - ";
376 HintMessage += A->getMessage();
377 }
378
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000379 // Make sure that this declaration has not been marked 'unavailable'.
380 if (A->getUnavailable()) {
381 if (Message) {
382 Message->clear();
383 llvm::raw_string_ostream Out(*Message);
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000384 Out << "not available on " << PrettyPlatformName
385 << HintMessage;
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000386 }
387
388 return AR_Unavailable;
389 }
390
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000391 // Make sure that this declaration has already been introduced.
392 if (!A->getIntroduced().empty() &&
393 TargetMinVersion < A->getIntroduced()) {
394 if (Message) {
395 Message->clear();
396 llvm::raw_string_ostream Out(*Message);
Fariborz Jahanian2618dba2014-10-06 16:46:02 +0000397 VersionTuple VTI(A->getIntroduced());
398 VTI.UseDotAsSeparator();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000399 Out << "introduced in " << PrettyPlatformName << ' '
Fariborz Jahanian2618dba2014-10-06 16:46:02 +0000400 << VTI << HintMessage;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000401 }
402
403 return AR_NotYetIntroduced;
404 }
405
406 // Make sure that this declaration hasn't been obsoleted.
407 if (!A->getObsoleted().empty() && TargetMinVersion >= A->getObsoleted()) {
408 if (Message) {
409 Message->clear();
410 llvm::raw_string_ostream Out(*Message);
Fariborz Jahanian2618dba2014-10-06 16:46:02 +0000411 VersionTuple VTO(A->getObsoleted());
412 VTO.UseDotAsSeparator();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000413 Out << "obsoleted in " << PrettyPlatformName << ' '
Fariborz Jahanian2618dba2014-10-06 16:46:02 +0000414 << VTO << HintMessage;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000415 }
416
417 return AR_Unavailable;
418 }
419
420 // Make sure that this declaration hasn't been deprecated.
421 if (!A->getDeprecated().empty() && TargetMinVersion >= A->getDeprecated()) {
422 if (Message) {
423 Message->clear();
424 llvm::raw_string_ostream Out(*Message);
Fariborz Jahanian2618dba2014-10-06 16:46:02 +0000425 VersionTuple VTD(A->getDeprecated());
426 VTD.UseDotAsSeparator();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000427 Out << "first deprecated in " << PrettyPlatformName << ' '
Fariborz Jahanian2618dba2014-10-06 16:46:02 +0000428 << VTD << HintMessage;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000429 }
430
431 return AR_Deprecated;
432 }
433
434 return AR_Available;
435}
436
437AvailabilityResult Decl::getAvailability(std::string *Message) const {
438 AvailabilityResult Result = AR_Available;
439 std::string ResultMessage;
440
Aaron Ballmanb97112e2014-03-08 22:19:01 +0000441 for (const auto *A : attrs()) {
442 if (const auto *Deprecated = dyn_cast<DeprecatedAttr>(A)) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000443 if (Result >= AR_Deprecated)
444 continue;
445
446 if (Message)
447 ResultMessage = Deprecated->getMessage();
448
449 Result = AR_Deprecated;
450 continue;
451 }
452
Aaron Ballmanb97112e2014-03-08 22:19:01 +0000453 if (const auto *Unavailable = dyn_cast<UnavailableAttr>(A)) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000454 if (Message)
455 *Message = Unavailable->getMessage();
456 return AR_Unavailable;
457 }
458
Aaron Ballmanb97112e2014-03-08 22:19:01 +0000459 if (const auto *Availability = dyn_cast<AvailabilityAttr>(A)) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000460 AvailabilityResult AR = CheckAvailability(getASTContext(), Availability,
461 Message);
462
463 if (AR == AR_Unavailable)
464 return AR_Unavailable;
465
466 if (AR > Result) {
467 Result = AR;
468 if (Message)
469 ResultMessage.swap(*Message);
470 }
471 continue;
472 }
473 }
474
475 if (Message)
476 Message->swap(ResultMessage);
477 return Result;
478}
479
480bool Decl::canBeWeakImported(bool &IsDefinition) const {
481 IsDefinition = false;
John McCall5fb5df92012-06-20 06:18:46 +0000482
483 // Variables, if they aren't definitions.
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000484 if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000485 if (Var->isThisDeclarationADefinition()) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000486 IsDefinition = true;
487 return false;
488 }
John McCall5fb5df92012-06-20 06:18:46 +0000489 return true;
490
491 // Functions, if they aren't definitions.
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000492 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
493 if (FD->hasBody()) {
494 IsDefinition = true;
495 return false;
496 }
John McCall5fb5df92012-06-20 06:18:46 +0000497 return true;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000498
John McCall5fb5df92012-06-20 06:18:46 +0000499 // Objective-C classes, if this is the non-fragile runtime.
500 } else if (isa<ObjCInterfaceDecl>(this) &&
John McCall18ac1632012-06-20 21:58:02 +0000501 getASTContext().getLangOpts().ObjCRuntime.hasWeakClassImport()) {
John McCall5fb5df92012-06-20 06:18:46 +0000502 return true;
503
504 // Nothing else.
505 } else {
506 return false;
507 }
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000508}
509
510bool Decl::isWeakImported() const {
511 bool IsDefinition;
512 if (!canBeWeakImported(IsDefinition))
513 return false;
514
Aaron Ballmanb97112e2014-03-08 22:19:01 +0000515 for (const auto *A : attrs()) {
516 if (isa<WeakImportAttr>(A))
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000517 return true;
518
Aaron Ballmanb97112e2014-03-08 22:19:01 +0000519 if (const auto *Availability = dyn_cast<AvailabilityAttr>(A)) {
Craig Topper36250ad2014-05-12 05:36:57 +0000520 if (CheckAvailability(getASTContext(), Availability,
521 nullptr) == AR_NotYetIntroduced)
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000522 return true;
523 }
524 }
525
526 return false;
527}
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000528
Chris Lattner8e097192009-03-27 20:18:19 +0000529unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
530 switch (DeclKind) {
John McCall3f746822009-11-17 05:59:44 +0000531 case Function:
532 case CXXMethod:
533 case CXXConstructor:
534 case CXXDestructor:
535 case CXXConversion:
Chris Lattner8e097192009-03-27 20:18:19 +0000536 case EnumConstant:
537 case Var:
538 case ImplicitParam:
539 case ParmVar:
Chris Lattner8e097192009-03-27 20:18:19 +0000540 case NonTypeTemplateParm:
541 case ObjCMethod:
Daniel Dunbar45b2d8a2010-04-23 13:07:39 +0000542 case ObjCProperty:
John McCall5e77d762013-04-16 07:28:30 +0000543 case MSProperty:
Daniel Dunbar45b2d8a2010-04-23 13:07:39 +0000544 return IDNS_Ordinary;
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000545 case Label:
546 return IDNS_Label;
Francois Pichet783dd6e2010-11-21 06:08:52 +0000547 case IndirectField:
548 return IDNS_Ordinary | IDNS_Member;
549
John McCalle87beb22010-04-23 18:46:30 +0000550 case ObjCCompatibleAlias:
551 case ObjCInterface:
552 return IDNS_Ordinary | IDNS_Type;
553
554 case Typedef:
Richard Smithdda56e42011-04-15 14:24:37 +0000555 case TypeAlias:
Richard Smith3f1b5d02011-05-05 21:57:07 +0000556 case TypeAliasTemplate:
John McCalle87beb22010-04-23 18:46:30 +0000557 case UnresolvedUsingTypename:
558 case TemplateTypeParm:
559 return IDNS_Ordinary | IDNS_Type;
560
John McCall3f746822009-11-17 05:59:44 +0000561 case UsingShadow:
562 return 0; // we'll actually overwrite this later
563
John McCalle61f2ba2009-11-18 02:36:19 +0000564 case UnresolvedUsingValue:
John McCalle61f2ba2009-11-18 02:36:19 +0000565 return IDNS_Ordinary | IDNS_Using;
John McCall3f746822009-11-17 05:59:44 +0000566
567 case Using:
568 return IDNS_Using;
569
Chris Lattner8e097192009-03-27 20:18:19 +0000570 case ObjCProtocol:
Douglas Gregor79947a22009-04-24 00:11:27 +0000571 return IDNS_ObjCProtocol;
Mike Stump11289f42009-09-09 15:08:12 +0000572
Chris Lattner8e097192009-03-27 20:18:19 +0000573 case Field:
574 case ObjCAtDefsField:
575 case ObjCIvar:
576 return IDNS_Member;
Mike Stump11289f42009-09-09 15:08:12 +0000577
Chris Lattner8e097192009-03-27 20:18:19 +0000578 case Record:
579 case CXXRecord:
580 case Enum:
John McCalle87beb22010-04-23 18:46:30 +0000581 return IDNS_Tag | IDNS_Type;
Mike Stump11289f42009-09-09 15:08:12 +0000582
Chris Lattner8e097192009-03-27 20:18:19 +0000583 case Namespace:
John McCalle87beb22010-04-23 18:46:30 +0000584 case NamespaceAlias:
585 return IDNS_Namespace;
586
Chris Lattner8e097192009-03-27 20:18:19 +0000587 case FunctionTemplate:
Larisse Voufo39a1e502013-08-06 01:03:05 +0000588 case VarTemplate:
John McCalle87beb22010-04-23 18:46:30 +0000589 return IDNS_Ordinary;
590
Chris Lattner8e097192009-03-27 20:18:19 +0000591 case ClassTemplate:
592 case TemplateTemplateParm:
John McCalle87beb22010-04-23 18:46:30 +0000593 return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
Mike Stump11289f42009-09-09 15:08:12 +0000594
Chris Lattner8e097192009-03-27 20:18:19 +0000595 // Never have names.
John McCallaa74a0c2009-08-28 07:59:38 +0000596 case Friend:
John McCall11083da2009-09-16 22:47:08 +0000597 case FriendTemplate:
Abramo Bagnarad7340582010-06-05 05:09:32 +0000598 case AccessSpec:
Chris Lattner8e097192009-03-27 20:18:19 +0000599 case LinkageSpec:
600 case FileScopeAsm:
601 case StaticAssert:
Chris Lattner8e097192009-03-27 20:18:19 +0000602 case ObjCPropertyImpl:
Chris Lattner8e097192009-03-27 20:18:19 +0000603 case Block:
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000604 case Captured:
Chris Lattner8e097192009-03-27 20:18:19 +0000605 case TranslationUnit:
Richard Smithf19e1272015-03-07 00:04:49 +0000606 case ExternCContext:
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000607
Chris Lattner8e097192009-03-27 20:18:19 +0000608 case UsingDirective:
609 case ClassTemplateSpecialization:
Douglas Gregor2373c592009-05-31 09:31:02 +0000610 case ClassTemplatePartialSpecialization:
Francois Pichet00c7e6c2011-08-14 03:52:19 +0000611 case ClassScopeFunctionSpecialization:
Larisse Voufo39a1e502013-08-06 01:03:05 +0000612 case VarTemplateSpecialization:
613 case VarTemplatePartialSpecialization:
Douglas Gregore93525e2010-04-22 23:19:50 +0000614 case ObjCImplementation:
615 case ObjCCategory:
616 case ObjCCategoryImpl:
Douglas Gregorba345522011-12-02 23:23:56 +0000617 case Import:
Alexey Bataeva769e072013-03-22 06:34:35 +0000618 case OMPThreadPrivate:
Michael Han84324352013-02-22 17:15:32 +0000619 case Empty:
Douglas Gregore93525e2010-04-22 23:19:50 +0000620 // Never looked up by name.
Chris Lattner8e097192009-03-27 20:18:19 +0000621 return 0;
622 }
John McCall3f746822009-11-17 05:59:44 +0000623
David Blaikiee4d798f2012-01-20 21:50:17 +0000624 llvm_unreachable("Invalid DeclKind!");
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000625}
626
Argyrios Kyrtzidis6f40eb72012-02-09 02:44:08 +0000627void Decl::setAttrsImpl(const AttrVec &attrs, ASTContext &Ctx) {
Argyrios Kyrtzidis91167172010-06-11 23:09:25 +0000628 assert(!HasAttrs && "Decl already contains attrs.");
629
Argyrios Kyrtzidis6f40eb72012-02-09 02:44:08 +0000630 AttrVec &AttrBlank = Ctx.getDeclAttrs(this);
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000631 assert(AttrBlank.empty() && "HasAttrs was wrong?");
Argyrios Kyrtzidis91167172010-06-11 23:09:25 +0000632
633 AttrBlank = attrs;
634 HasAttrs = true;
635}
636
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000637void Decl::dropAttrs() {
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000638 if (!HasAttrs) return;
Mike Stump11289f42009-09-09 15:08:12 +0000639
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000640 HasAttrs = false;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000641 getASTContext().eraseDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000642}
643
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000644const AttrVec &Decl::getAttrs() const {
645 assert(HasAttrs && "No attrs to get!");
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000646 return getASTContext().getDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000647}
648
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000649Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000650 Decl::Kind DK = D->getDeclKind();
651 switch(DK) {
Alexis Hunted053252010-05-30 07:21:58 +0000652#define DECL(NAME, BASE)
653#define DECL_CONTEXT(NAME) \
654 case Decl::NAME: \
655 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
656#define DECL_CONTEXT_BASE(NAME)
657#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000658 default:
Alexis Hunted053252010-05-30 07:21:58 +0000659#define DECL(NAME, BASE)
660#define DECL_CONTEXT_BASE(NAME) \
661 if (DK >= first##NAME && DK <= last##NAME) \
662 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
663#include "clang/AST/DeclNodes.inc"
David Blaikie83d382b2011-09-23 05:06:16 +0000664 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000665 }
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000666}
667
668DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000669 Decl::Kind DK = D->getKind();
670 switch(DK) {
Alexis Hunted053252010-05-30 07:21:58 +0000671#define DECL(NAME, BASE)
672#define DECL_CONTEXT(NAME) \
673 case Decl::NAME: \
674 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
675#define DECL_CONTEXT_BASE(NAME)
676#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000677 default:
Alexis Hunted053252010-05-30 07:21:58 +0000678#define DECL(NAME, BASE)
679#define DECL_CONTEXT_BASE(NAME) \
680 if (DK >= first##NAME && DK <= last##NAME) \
681 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
682#include "clang/AST/DeclNodes.inc"
David Blaikie83d382b2011-09-23 05:06:16 +0000683 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000684 }
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000685}
686
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000687SourceLocation Decl::getBodyRBrace() const {
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +0000688 // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
689 // FunctionDecl stores EndRangeLoc for this purpose.
690 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
691 const FunctionDecl *Definition;
692 if (FD->hasBody(Definition))
693 return Definition->getSourceRange().getEnd();
694 return SourceLocation();
695 }
696
Argyrios Kyrtzidis6fbc8fa2010-07-07 11:31:27 +0000697 if (Stmt *Body = getBody())
698 return Body->getSourceRange().getEnd();
699
700 return SourceLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +0000701}
702
Alp Tokerc1086762013-12-07 13:51:35 +0000703bool Decl::AccessDeclContextSanity() const {
Douglas Gregor4b00d3b2010-12-02 00:22:25 +0000704#ifndef NDEBUG
John McCall401982f2010-01-20 21:53:11 +0000705 // Suppress this check if any of the following hold:
706 // 1. this is the translation unit (and thus has no parent)
707 // 2. this is a template parameter (and thus doesn't belong to its context)
Argyrios Kyrtzidise1778632010-09-08 21:58:42 +0000708 // 3. this is a non-type template parameter
709 // 4. the context is not a record
710 // 5. it's invalid
711 // 6. it's a C++0x static_assert.
Anders Carlssonadf36b22009-08-29 20:47:47 +0000712 if (isa<TranslationUnitDecl>(this) ||
Argyrios Kyrtzidisa45855f2010-07-02 11:55:44 +0000713 isa<TemplateTypeParmDecl>(this) ||
Argyrios Kyrtzidise1778632010-09-08 21:58:42 +0000714 isa<NonTypeTemplateParmDecl>(this) ||
Douglas Gregor2b76dd92010-02-22 17:53:38 +0000715 !isa<CXXRecordDecl>(getDeclContext()) ||
Argyrios Kyrtzidis260b4a82010-09-08 21:32:35 +0000716 isInvalidDecl() ||
717 isa<StaticAssertDecl>(this) ||
718 // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
719 // as DeclContext (?).
Argyrios Kyrtzidise1778632010-09-08 21:58:42 +0000720 isa<ParmVarDecl>(this) ||
721 // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
722 // AS_none as access specifier.
Francois Pichet09af8c32011-08-17 01:06:54 +0000723 isa<CXXRecordDecl>(this) ||
724 isa<ClassScopeFunctionSpecializationDecl>(this))
Alp Tokerc1086762013-12-07 13:51:35 +0000725 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000726
727 assert(Access != AS_none &&
Anders Carlssona28908d2009-03-25 23:38:06 +0000728 "Access specifier is AS_none inside a record decl");
Douglas Gregor4b00d3b2010-12-02 00:22:25 +0000729#endif
Alp Tokerc1086762013-12-07 13:51:35 +0000730 return true;
Anders Carlssona28908d2009-03-25 23:38:06 +0000731}
732
John McCalldec348f72013-05-03 07:33:41 +0000733static Decl::Kind getKind(const Decl *D) { return D->getKind(); }
734static Decl::Kind getKind(const DeclContext *DC) { return DC->getDeclKind(); }
735
Aaron Ballman12b9f652014-01-16 13:55:42 +0000736const FunctionType *Decl::getFunctionType(bool BlocksToo) const {
737 QualType Ty;
738 if (const ValueDecl *D = dyn_cast<ValueDecl>(this))
739 Ty = D->getType();
740 else if (const TypedefNameDecl *D = dyn_cast<TypedefNameDecl>(this))
741 Ty = D->getUnderlyingType();
742 else
Craig Topper36250ad2014-05-12 05:36:57 +0000743 return nullptr;
Aaron Ballman12b9f652014-01-16 13:55:42 +0000744
745 if (Ty->isFunctionPointerType())
746 Ty = Ty->getAs<PointerType>()->getPointeeType();
747 else if (BlocksToo && Ty->isBlockPointerType())
748 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
749
750 return Ty->getAs<FunctionType>();
751}
752
753
John McCalldec348f72013-05-03 07:33:41 +0000754/// Starting at a given context (a Decl or DeclContext), look for a
755/// code context that is not a closure (a lambda, block, etc.).
756template <class T> static Decl *getNonClosureContext(T *D) {
757 if (getKind(D) == Decl::CXXMethod) {
758 CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
John McCall55c0cee2013-05-03 17:11:14 +0000759 if (MD->getOverloadedOperator() == OO_Call &&
760 MD->getParent()->isLambda())
John McCalldec348f72013-05-03 07:33:41 +0000761 return getNonClosureContext(MD->getParent()->getParent());
762 return MD;
763 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
764 return FD;
765 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
766 return MD;
767 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
768 return getNonClosureContext(BD->getParent());
769 } else if (CapturedDecl *CD = dyn_cast<CapturedDecl>(D)) {
770 return getNonClosureContext(CD->getParent());
771 } else {
Craig Topper36250ad2014-05-12 05:36:57 +0000772 return nullptr;
John McCalldec348f72013-05-03 07:33:41 +0000773 }
John McCallfe96e0b2011-11-06 09:01:30 +0000774}
775
John McCalldec348f72013-05-03 07:33:41 +0000776Decl *Decl::getNonClosureContext() {
777 return ::getNonClosureContext(this);
778}
John McCallb67608f2011-02-22 22:25:23 +0000779
John McCalldec348f72013-05-03 07:33:41 +0000780Decl *DeclContext::getNonClosureAncestor() {
781 return ::getNonClosureContext(this);
John McCallb67608f2011-02-22 22:25:23 +0000782}
Anders Carlssona28908d2009-03-25 23:38:06 +0000783
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000784//===----------------------------------------------------------------------===//
785// DeclContext Implementation
786//===----------------------------------------------------------------------===//
787
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000788bool DeclContext::classof(const Decl *D) {
789 switch (D->getKind()) {
Alexis Hunted053252010-05-30 07:21:58 +0000790#define DECL(NAME, BASE)
791#define DECL_CONTEXT(NAME) case Decl::NAME:
792#define DECL_CONTEXT_BASE(NAME)
793#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000794 return true;
795 default:
Alexis Hunted053252010-05-30 07:21:58 +0000796#define DECL(NAME, BASE)
797#define DECL_CONTEXT_BASE(NAME) \
798 if (D->getKind() >= Decl::first##NAME && \
799 D->getKind() <= Decl::last##NAME) \
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000800 return true;
Alexis Hunted053252010-05-30 07:21:58 +0000801#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000802 return false;
803 }
804}
805
Douglas Gregor9c832f72010-07-25 18:38:02 +0000806DeclContext::~DeclContext() { }
Douglas Gregor91f84212008-12-11 16:49:14 +0000807
Douglas Gregor7f737c02009-09-10 16:57:35 +0000808/// \brief Find the parent context of this context that will be
809/// used for unqualified name lookup.
810///
811/// Generally, the parent lookup context is the semantic context. However, for
812/// a friend function the parent lookup context is the lexical context, which
813/// is the class in which the friend is declared.
814DeclContext *DeclContext::getLookupParent() {
815 // FIXME: Find a better way to identify friends
816 if (isa<FunctionDecl>(this))
Sebastian Redl50c68252010-08-31 00:36:30 +0000817 if (getParent()->getRedeclContext()->isFileContext() &&
818 getLexicalParent()->getRedeclContext()->isRecord())
Douglas Gregor7f737c02009-09-10 16:57:35 +0000819 return getLexicalParent();
820
821 return getParent();
822}
823
Sebastian Redlbd595762010-08-31 20:53:31 +0000824bool DeclContext::isInlineNamespace() const {
825 return isNamespace() &&
826 cast<NamespaceDecl>(this)->isInline();
827}
828
Richard Trieuc771d5d2014-05-28 02:16:01 +0000829bool DeclContext::isStdNamespace() const {
830 if (!isNamespace())
831 return false;
832
833 const NamespaceDecl *ND = cast<NamespaceDecl>(this);
834 if (ND->isInline()) {
835 return ND->getParent()->isStdNamespace();
836 }
837
838 if (!getParent()->getRedeclContext()->isTranslationUnit())
839 return false;
840
841 const IdentifierInfo *II = ND->getIdentifier();
842 return II && II->isStr("std");
843}
844
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000845bool DeclContext::isDependentContext() const {
846 if (isFileContext())
847 return false;
848
Douglas Gregor2373c592009-05-31 09:31:02 +0000849 if (isa<ClassTemplatePartialSpecializationDecl>(this))
850 return true;
851
Douglas Gregor680e9e02012-02-21 19:11:17 +0000852 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this)) {
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000853 if (Record->getDescribedClassTemplate())
854 return true;
Douglas Gregor680e9e02012-02-21 19:11:17 +0000855
856 if (Record->isDependentLambda())
857 return true;
858 }
859
John McCallc62bb642010-03-24 05:22:00 +0000860 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000861 if (Function->getDescribedFunctionTemplate())
862 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000863
John McCallc62bb642010-03-24 05:22:00 +0000864 // Friend function declarations are dependent if their *lexical*
865 // context is dependent.
866 if (cast<Decl>(this)->getFriendObjectKind())
867 return getLexicalParent()->isDependentContext();
868 }
869
Richard Smith2b560572015-02-07 03:11:11 +0000870 // FIXME: A variable template is a dependent context, but is not a
871 // DeclContext. A context within it (such as a lambda-expression)
872 // should be considered dependent.
873
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000874 return getParent() && getParent()->isDependentContext();
875}
876
Douglas Gregor07665a62009-01-05 19:45:36 +0000877bool DeclContext::isTransparentContext() const {
878 if (DeclKind == Decl::Enum)
Douglas Gregor0bf31402010-10-08 23:50:27 +0000879 return !cast<EnumDecl>(this)->isScoped();
Douglas Gregor07665a62009-01-05 19:45:36 +0000880 else if (DeclKind == Decl::LinkageSpec)
881 return true;
Douglas Gregor07665a62009-01-05 19:45:36 +0000882
883 return false;
884}
885
Serge Pavlov3cb80222013-11-14 02:13:03 +0000886static bool isLinkageSpecContext(const DeclContext *DC,
887 LinkageSpecDecl::LanguageIDs ID) {
888 while (DC->getDeclKind() != Decl::TranslationUnit) {
889 if (DC->getDeclKind() == Decl::LinkageSpec)
890 return cast<LinkageSpecDecl>(DC)->getLanguage() == ID;
Richard Smithac9c9a02014-04-14 20:23:58 +0000891 DC = DC->getLexicalParent();
Serge Pavlov3cb80222013-11-14 02:13:03 +0000892 }
893 return false;
894}
895
896bool DeclContext::isExternCContext() const {
897 return isLinkageSpecContext(this, clang::LinkageSpecDecl::lang_c);
898}
899
900bool DeclContext::isExternCXXContext() const {
901 return isLinkageSpecContext(this, clang::LinkageSpecDecl::lang_cxx);
902}
903
Sebastian Redl50c68252010-08-31 00:36:30 +0000904bool DeclContext::Encloses(const DeclContext *DC) const {
Douglas Gregore985a3b2009-08-27 06:03:53 +0000905 if (getPrimaryContext() != this)
906 return getPrimaryContext()->Encloses(DC);
Mike Stump11289f42009-09-09 15:08:12 +0000907
Douglas Gregore985a3b2009-08-27 06:03:53 +0000908 for (; DC; DC = DC->getParent())
909 if (DC->getPrimaryContext() == this)
910 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000911 return false;
Douglas Gregore985a3b2009-08-27 06:03:53 +0000912}
913
Steve Naroff35c62ae2009-01-08 17:28:14 +0000914DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor91f84212008-12-11 16:49:14 +0000915 switch (DeclKind) {
Douglas Gregor91f84212008-12-11 16:49:14 +0000916 case Decl::TranslationUnit:
Richard Smithf19e1272015-03-07 00:04:49 +0000917 case Decl::ExternCContext:
Douglas Gregor07665a62009-01-05 19:45:36 +0000918 case Decl::LinkageSpec:
Mike Stump11289f42009-09-09 15:08:12 +0000919 case Decl::Block:
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000920 case Decl::Captured:
Douglas Gregor91f84212008-12-11 16:49:14 +0000921 // There is only one DeclContext for these entities.
922 return this;
923
924 case Decl::Namespace:
925 // The original namespace is our primary context.
926 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
927
Douglas Gregor91f84212008-12-11 16:49:14 +0000928 case Decl::ObjCMethod:
929 return this;
930
931 case Decl::ObjCInterface:
Douglas Gregor66b310c2011-12-15 18:03:09 +0000932 if (ObjCInterfaceDecl *Def = cast<ObjCInterfaceDecl>(this)->getDefinition())
933 return Def;
934
935 return this;
936
Steve Naroff35c62ae2009-01-08 17:28:14 +0000937 case Decl::ObjCProtocol:
Douglas Gregora715bff2012-01-01 19:51:50 +0000938 if (ObjCProtocolDecl *Def = cast<ObjCProtocolDecl>(this)->getDefinition())
939 return Def;
940
941 return this;
Douglas Gregor66b310c2011-12-15 18:03:09 +0000942
Steve Naroff35c62ae2009-01-08 17:28:14 +0000943 case Decl::ObjCCategory:
Douglas Gregor91f84212008-12-11 16:49:14 +0000944 return this;
945
Steve Naroff35c62ae2009-01-08 17:28:14 +0000946 case Decl::ObjCImplementation:
947 case Decl::ObjCCategoryImpl:
948 return this;
949
Douglas Gregor91f84212008-12-11 16:49:14 +0000950 default:
Alexis Hunted053252010-05-30 07:21:58 +0000951 if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
Douglas Gregor67a65642009-02-17 23:15:12 +0000952 // If this is a tag type that has a definition or is currently
953 // being defined, that definition is our primary context.
John McCalle78aac42010-03-10 03:28:59 +0000954 TagDecl *Tag = cast<TagDecl>(this);
John McCalle78aac42010-03-10 03:28:59 +0000955
956 if (TagDecl *Def = Tag->getDefinition())
957 return Def;
958
Richard Smith5b21db82014-04-23 18:20:42 +0000959 if (const TagType *TagTy = dyn_cast<TagType>(Tag->getTypeForDecl())) {
960 // Note, TagType::getDecl returns the (partial) definition one exists.
961 TagDecl *PossiblePartialDef = TagTy->getDecl();
962 if (PossiblePartialDef->isBeingDefined())
963 return PossiblePartialDef;
964 } else {
965 assert(isa<InjectedClassNameType>(Tag->getTypeForDecl()));
John McCalle78aac42010-03-10 03:28:59 +0000966 }
967
968 return Tag;
Douglas Gregor67a65642009-02-17 23:15:12 +0000969 }
970
Alexis Hunted053252010-05-30 07:21:58 +0000971 assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
Douglas Gregor91f84212008-12-11 16:49:14 +0000972 "Unknown DeclContext kind");
973 return this;
974 }
975}
976
Douglas Gregore57e7522012-01-07 09:11:48 +0000977void
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000978DeclContext::collectAllContexts(SmallVectorImpl<DeclContext *> &Contexts){
Douglas Gregore57e7522012-01-07 09:11:48 +0000979 Contexts.clear();
980
981 if (DeclKind != Decl::Namespace) {
982 Contexts.push_back(this);
983 return;
Douglas Gregor91f84212008-12-11 16:49:14 +0000984 }
Douglas Gregore57e7522012-01-07 09:11:48 +0000985
986 NamespaceDecl *Self = static_cast<NamespaceDecl *>(this);
Douglas Gregorec9fd132012-01-14 16:38:05 +0000987 for (NamespaceDecl *N = Self->getMostRecentDecl(); N;
988 N = N->getPreviousDecl())
Douglas Gregore57e7522012-01-07 09:11:48 +0000989 Contexts.push_back(N);
990
991 std::reverse(Contexts.begin(), Contexts.end());
Douglas Gregor91f84212008-12-11 16:49:14 +0000992}
993
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000994std::pair<Decl *, Decl *>
Bill Wendling8eb771d2012-02-22 09:51:33 +0000995DeclContext::BuildDeclChain(ArrayRef<Decl*> Decls,
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +0000996 bool FieldsAlreadyLoaded) {
Douglas Gregor781f7132012-01-06 16:59:53 +0000997 // Build up a chain of declarations via the Decl::NextInContextAndBits field.
Craig Topper36250ad2014-05-12 05:36:57 +0000998 Decl *FirstNewDecl = nullptr;
999 Decl *PrevDecl = nullptr;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001000 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +00001001 if (FieldsAlreadyLoaded && isa<FieldDecl>(Decls[I]))
1002 continue;
1003
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001004 Decl *D = Decls[I];
1005 if (PrevDecl)
Douglas Gregor781f7132012-01-06 16:59:53 +00001006 PrevDecl->NextInContextAndBits.setPointer(D);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001007 else
1008 FirstNewDecl = D;
1009
1010 PrevDecl = D;
1011 }
1012
1013 return std::make_pair(FirstNewDecl, PrevDecl);
1014}
1015
Richard Smith645d7552013-02-07 03:37:08 +00001016/// \brief We have just acquired external visible storage, and we already have
1017/// built a lookup map. For every name in the map, pull in the new names from
1018/// the external storage.
Richard Smitha8b74592014-03-25 00:34:21 +00001019void DeclContext::reconcileExternalVisibleStorage() const {
Richard Smith9e2341d2015-03-23 03:25:59 +00001020 assert(NeedToReconcileExternalVisibleStorage && LookupPtr);
Richard Smith645d7552013-02-07 03:37:08 +00001021 NeedToReconcileExternalVisibleStorage = false;
1022
Richard Smith9e2341d2015-03-23 03:25:59 +00001023 for (auto &Lookup : *LookupPtr)
Richard Smitha8b74592014-03-25 00:34:21 +00001024 Lookup.second.setHasExternalDecls();
Richard Smith645d7552013-02-07 03:37:08 +00001025}
1026
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001027/// \brief Load the declarations within this lexical storage from an
1028/// external source.
Richard Smith18b380b2015-03-24 02:44:20 +00001029/// \return \c true if any declarations were added.
1030bool
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001031DeclContext::LoadLexicalDeclsFromExternalStorage() const {
1032 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001033 assert(hasExternalLexicalStorage() && Source && "No external storage?");
1034
Argyrios Kyrtzidis98d045e2010-07-30 10:03:23 +00001035 // Notify that we have a DeclContext that is initializing.
1036 ExternalASTSource::Deserializing ADeclContext(Source);
Richard Smith9e2341d2015-03-23 03:25:59 +00001037
Douglas Gregor3d0adb32011-07-15 21:46:17 +00001038 // Load the external declarations, if any.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001039 SmallVector<Decl*, 64> Decls;
Richard Smith18b380b2015-03-24 02:44:20 +00001040 ExternalLexicalStorage = false;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00001041 switch (Source->FindExternalLexicalDecls(this, Decls)) {
1042 case ELR_Success:
1043 break;
1044
1045 case ELR_Failure:
1046 case ELR_AlreadyLoaded:
Richard Smith18b380b2015-03-24 02:44:20 +00001047 return false;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00001048 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001049
1050 if (Decls.empty())
Richard Smith18b380b2015-03-24 02:44:20 +00001051 return false;
Richard Smith9e2341d2015-03-23 03:25:59 +00001052
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +00001053 // We may have already loaded just the fields of this record, in which case
1054 // we need to ignore them.
1055 bool FieldsAlreadyLoaded = false;
1056 if (const RecordDecl *RD = dyn_cast<RecordDecl>(this))
1057 FieldsAlreadyLoaded = RD->LoadedFieldsFromExternalStorage;
1058
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001059 // Splice the newly-read declarations into the beginning of the list
1060 // of declarations.
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001061 Decl *ExternalFirst, *ExternalLast;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00001062 std::tie(ExternalFirst, ExternalLast) =
1063 BuildDeclChain(Decls, FieldsAlreadyLoaded);
Douglas Gregor781f7132012-01-06 16:59:53 +00001064 ExternalLast->NextInContextAndBits.setPointer(FirstDecl);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001065 FirstDecl = ExternalFirst;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001066 if (!LastDecl)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001067 LastDecl = ExternalLast;
Richard Smith18b380b2015-03-24 02:44:20 +00001068 return true;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001069}
1070
John McCall75b960e2010-06-01 09:23:16 +00001071DeclContext::lookup_result
1072ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
1073 DeclarationName Name) {
1074 ASTContext &Context = DC->getParentASTContext();
1075 StoredDeclsMap *Map;
Richard Smith9e2341d2015-03-23 03:25:59 +00001076 if (!(Map = DC->LookupPtr))
John McCall75b960e2010-06-01 09:23:16 +00001077 Map = DC->CreateStoredDeclsMap(Context);
Richard Smitha8b74592014-03-25 00:34:21 +00001078 if (DC->NeedToReconcileExternalVisibleStorage)
1079 DC->reconcileExternalVisibleStorage();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001080
Richard Smith4abe0a82013-09-09 07:34:56 +00001081 (*Map)[Name].removeExternalDecls();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001082
John McCall75b960e2010-06-01 09:23:16 +00001083 return DeclContext::lookup_result();
1084}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001085
John McCall75b960e2010-06-01 09:23:16 +00001086DeclContext::lookup_result
1087ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +00001088 DeclarationName Name,
Argyrios Kyrtzidis94d3f9d2011-09-09 06:44:14 +00001089 ArrayRef<NamedDecl*> Decls) {
Dmitri Gribenko76bb5cabfa2012-09-10 21:20:09 +00001090 ASTContext &Context = DC->getParentASTContext();
John McCall75b960e2010-06-01 09:23:16 +00001091 StoredDeclsMap *Map;
Richard Smith9e2341d2015-03-23 03:25:59 +00001092 if (!(Map = DC->LookupPtr))
John McCall75b960e2010-06-01 09:23:16 +00001093 Map = DC->CreateStoredDeclsMap(Context);
Richard Smitha8b74592014-03-25 00:34:21 +00001094 if (DC->NeedToReconcileExternalVisibleStorage)
1095 DC->reconcileExternalVisibleStorage();
John McCall75b960e2010-06-01 09:23:16 +00001096
1097 StoredDeclsList &List = (*Map)[Name];
Richard Smith51445cd2013-06-24 01:46:41 +00001098
1099 // Clear out any old external visible declarations, to avoid quadratic
1100 // performance in the redeclaration checks below.
1101 List.removeExternalDecls();
1102
1103 if (!List.isNull()) {
1104 // We have both existing declarations and new declarations for this name.
1105 // Some of the declarations may simply replace existing ones. Handle those
1106 // first.
1107 llvm::SmallVector<unsigned, 8> Skip;
1108 for (unsigned I = 0, N = Decls.size(); I != N; ++I)
Richard Smithe8292b12015-02-10 03:28:10 +00001109 if (List.HandleRedeclaration(Decls[I], /*IsKnownNewer*/false))
Richard Smith51445cd2013-06-24 01:46:41 +00001110 Skip.push_back(I);
1111 Skip.push_back(Decls.size());
1112
1113 // Add in any new declarations.
1114 unsigned SkipPos = 0;
1115 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
1116 if (I == Skip[SkipPos])
1117 ++SkipPos;
1118 else
1119 List.AddSubsequentDecl(Decls[I]);
1120 }
1121 } else {
1122 // Convert the array to a StoredDeclsList.
1123 for (ArrayRef<NamedDecl*>::iterator
1124 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
1125 if (List.isNull())
1126 List.setOnlyValue(*I);
1127 else
1128 List.AddSubsequentDecl(*I);
1129 }
John McCall75b960e2010-06-01 09:23:16 +00001130 }
1131
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001132 return List.getLookupResult();
John McCall75b960e2010-06-01 09:23:16 +00001133}
1134
Aaron Ballmanda634f12014-03-07 22:17:20 +00001135DeclContext::decl_iterator DeclContext::decls_begin() const {
1136 if (hasExternalLexicalStorage())
1137 LoadLexicalDeclsFromExternalStorage();
1138 return decl_iterator(FirstDecl);
1139}
1140
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001141bool DeclContext::decls_empty() const {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001142 if (hasExternalLexicalStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001143 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001144
1145 return !FirstDecl;
1146}
1147
Sean Callanan0325fb82013-05-04 02:04:27 +00001148bool DeclContext::containsDecl(Decl *D) const {
1149 return (D->getLexicalDeclContext() == this &&
1150 (D->NextInContextAndBits.getPointer() || D == LastDecl));
1151}
1152
John McCall84d87672009-12-10 09:41:52 +00001153void DeclContext::removeDecl(Decl *D) {
1154 assert(D->getLexicalDeclContext() == this &&
1155 "decl being removed from non-lexical context");
Douglas Gregor781f7132012-01-06 16:59:53 +00001156 assert((D->NextInContextAndBits.getPointer() || D == LastDecl) &&
John McCall84d87672009-12-10 09:41:52 +00001157 "decl is not in decls list");
1158
1159 // Remove D from the decl chain. This is O(n) but hopefully rare.
1160 if (D == FirstDecl) {
1161 if (D == LastDecl)
Craig Topper36250ad2014-05-12 05:36:57 +00001162 FirstDecl = LastDecl = nullptr;
John McCall84d87672009-12-10 09:41:52 +00001163 else
Douglas Gregor781f7132012-01-06 16:59:53 +00001164 FirstDecl = D->NextInContextAndBits.getPointer();
John McCall84d87672009-12-10 09:41:52 +00001165 } else {
Douglas Gregor781f7132012-01-06 16:59:53 +00001166 for (Decl *I = FirstDecl; true; I = I->NextInContextAndBits.getPointer()) {
John McCall84d87672009-12-10 09:41:52 +00001167 assert(I && "decl not found in linked list");
Douglas Gregor781f7132012-01-06 16:59:53 +00001168 if (I->NextInContextAndBits.getPointer() == D) {
1169 I->NextInContextAndBits.setPointer(D->NextInContextAndBits.getPointer());
John McCall84d87672009-12-10 09:41:52 +00001170 if (D == LastDecl) LastDecl = I;
1171 break;
1172 }
1173 }
1174 }
1175
1176 // Mark that D is no longer in the decl chain.
Craig Topper36250ad2014-05-12 05:36:57 +00001177 D->NextInContextAndBits.setPointer(nullptr);
John McCall84d87672009-12-10 09:41:52 +00001178
1179 // Remove D from the lookup table if necessary.
1180 if (isa<NamedDecl>(D)) {
1181 NamedDecl *ND = cast<NamedDecl>(D);
1182
Axel Naumanncb2c52f2011-08-26 14:06:12 +00001183 // Remove only decls that have a name
1184 if (!ND->getDeclName()) return;
1185
Richard Smith9e2341d2015-03-23 03:25:59 +00001186 StoredDeclsMap *Map = getPrimaryContext()->LookupPtr;
John McCallc62bb642010-03-24 05:22:00 +00001187 if (!Map) return;
John McCall84d87672009-12-10 09:41:52 +00001188
John McCall84d87672009-12-10 09:41:52 +00001189 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
1190 assert(Pos != Map->end() && "no lookup entry for decl");
Axel Naumannfbc7b982011-11-08 18:21:06 +00001191 if (Pos->second.getAsVector() || Pos->second.getAsDecl() == ND)
1192 Pos->second.remove(ND);
John McCall84d87672009-12-10 09:41:52 +00001193 }
1194}
1195
John McCalld1e9d832009-08-11 06:59:38 +00001196void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner33f219d2009-02-20 00:56:18 +00001197 assert(D->getLexicalDeclContext() == this &&
1198 "Decl inserted into wrong lexical context");
Mike Stump11289f42009-09-09 15:08:12 +00001199 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor020713e2009-01-09 19:42:16 +00001200 "Decl already inserted into a DeclContext");
1201
1202 if (FirstDecl) {
Douglas Gregor781f7132012-01-06 16:59:53 +00001203 LastDecl->NextInContextAndBits.setPointer(D);
Douglas Gregor020713e2009-01-09 19:42:16 +00001204 LastDecl = D;
1205 } else {
1206 FirstDecl = LastDecl = D;
1207 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +00001208
1209 // Notify a C++ record declaration that we've added a member, so it can
1210 // update it's class-specific state.
1211 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
1212 Record->addedMember(D);
Douglas Gregor0f2a3602011-12-03 00:30:27 +00001213
1214 // If this is a newly-created (not de-serialized) import declaration, wire
1215 // it in to the list of local import declarations.
1216 if (!D->isFromASTFile()) {
1217 if (ImportDecl *Import = dyn_cast<ImportDecl>(D))
1218 D->getASTContext().addedLocalImportDecl(Import);
1219 }
John McCalld1e9d832009-08-11 06:59:38 +00001220}
1221
1222void DeclContext::addDecl(Decl *D) {
1223 addHiddenDecl(D);
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001224
1225 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Richard Smithf634c902012-03-16 06:12:59 +00001226 ND->getDeclContext()->getPrimaryContext()->
1227 makeDeclVisibleInContextWithFlags(ND, false, true);
Douglas Gregor91f84212008-12-11 16:49:14 +00001228}
1229
Sean Callanan95e74be2011-10-21 02:57:43 +00001230void DeclContext::addDeclInternal(Decl *D) {
1231 addHiddenDecl(D);
1232
1233 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Richard Smithf634c902012-03-16 06:12:59 +00001234 ND->getDeclContext()->getPrimaryContext()->
1235 makeDeclVisibleInContextWithFlags(ND, true, true);
1236}
1237
1238/// shouldBeHidden - Determine whether a declaration which was declared
1239/// within its semantic context should be invisible to qualified name lookup.
1240static bool shouldBeHidden(NamedDecl *D) {
1241 // Skip unnamed declarations.
1242 if (!D->getDeclName())
1243 return true;
1244
1245 // Skip entities that can't be found by name lookup into a particular
1246 // context.
1247 if ((D->getIdentifierNamespace() == 0 && !isa<UsingDirectiveDecl>(D)) ||
1248 D->isTemplateParameter())
1249 return true;
1250
1251 // Skip template specializations.
1252 // FIXME: This feels like a hack. Should DeclarationName support
1253 // template-ids, or is there a better way to keep specializations
1254 // from being visible?
1255 if (isa<ClassTemplateSpecializationDecl>(D))
1256 return true;
1257 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1258 if (FD->isFunctionTemplateSpecialization())
1259 return true;
1260
1261 return false;
1262}
1263
1264/// buildLookup - Build the lookup data structure with all of the
1265/// declarations in this DeclContext (and any other contexts linked
1266/// to it or transparent contexts nested within it) and return it.
Richard Smitha8b74592014-03-25 00:34:21 +00001267///
1268/// Note that the produced map may miss out declarations from an
1269/// external source. If it does, those entries will be marked with
1270/// the 'hasExternalDecls' flag.
Richard Smithf634c902012-03-16 06:12:59 +00001271StoredDeclsMap *DeclContext::buildLookup() {
1272 assert(this == getPrimaryContext() && "buildLookup called on non-primary DC");
1273
Richard Smith9e2341d2015-03-23 03:25:59 +00001274 if (!HasLazyLocalLexicalLookups && !HasLazyExternalLexicalLookups)
1275 return LookupPtr;
1276
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001277 SmallVector<DeclContext *, 2> Contexts;
Richard Smithf634c902012-03-16 06:12:59 +00001278 collectAllContexts(Contexts);
Richard Smith18b380b2015-03-24 02:44:20 +00001279
1280 if (HasLazyExternalLexicalLookups) {
1281 HasLazyExternalLexicalLookups = false;
1282 for (auto *DC : Contexts) {
1283 if (DC->hasExternalLexicalStorage())
1284 HasLazyLocalLexicalLookups |=
1285 DC->LoadLexicalDeclsFromExternalStorage();
1286 }
1287
1288 if (!HasLazyLocalLexicalLookups)
1289 return LookupPtr;
1290 }
1291
1292 for (auto *DC : Contexts)
1293 buildLookupImpl(DC, hasExternalVisibleStorage());
Richard Smithf634c902012-03-16 06:12:59 +00001294
1295 // We no longer have any lazy decls.
Richard Smith9e2341d2015-03-23 03:25:59 +00001296 HasLazyLocalLexicalLookups = false;
1297 return LookupPtr;
Richard Smithf634c902012-03-16 06:12:59 +00001298}
1299
1300/// buildLookupImpl - Build part of the lookup data structure for the
1301/// declarations contained within DCtx, which will either be this
1302/// DeclContext, a DeclContext linked to it, or a transparent context
1303/// nested within it.
Richard Smitha3271c12015-02-07 00:45:52 +00001304void DeclContext::buildLookupImpl(DeclContext *DCtx, bool Internal) {
Richard Smith9e2341d2015-03-23 03:25:59 +00001305 for (Decl *D : DCtx->noload_decls()) {
Richard Smithf634c902012-03-16 06:12:59 +00001306 // Insert this declaration into the lookup structure, but only if
1307 // it's semantically within its decl context. Any other decls which
1308 // should be found in this context are added eagerly.
Richard Smithcf4ab522013-06-24 07:20:36 +00001309 //
1310 // If it's from an AST file, don't add it now. It'll get handled by
1311 // FindExternalVisibleDeclsByName if needed. Exception: if we're not
1312 // in C++, we do not track external visible decls for the TU, so in
1313 // that case we need to collect them all here.
Richard Smithf634c902012-03-16 06:12:59 +00001314 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Richard Smithcf4ab522013-06-24 07:20:36 +00001315 if (ND->getDeclContext() == DCtx && !shouldBeHidden(ND) &&
1316 (!ND->isFromASTFile() ||
1317 (isTranslationUnit() &&
1318 !getParentASTContext().getLangOpts().CPlusPlus)))
Richard Smitha3271c12015-02-07 00:45:52 +00001319 makeDeclVisibleInContextImpl(ND, Internal);
Richard Smithf634c902012-03-16 06:12:59 +00001320
1321 // If this declaration is itself a transparent declaration context
1322 // or inline namespace, add the members of this declaration of that
1323 // context (recursively).
1324 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(D))
1325 if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
Richard Smith9e2341d2015-03-23 03:25:59 +00001326 buildLookupImpl(InnerCtx, Internal);
Richard Smithf634c902012-03-16 06:12:59 +00001327 }
Sean Callanan95e74be2011-10-21 02:57:43 +00001328}
1329
Richard Smith40c78062015-02-21 02:31:57 +00001330NamedDecl *const DeclContextLookupResult::SingleElementDummyList = nullptr;
1331
Mike Stump11289f42009-09-09 15:08:12 +00001332DeclContext::lookup_result
Richard Smith40c78062015-02-21 02:31:57 +00001333DeclContext::lookup(DeclarationName Name) const {
Nick Lewycky2bd636f2012-03-13 04:12:34 +00001334 assert(DeclKind != Decl::LinkageSpec &&
1335 "Should not perform lookups into linkage specs!");
1336
Richard Smith40c78062015-02-21 02:31:57 +00001337 const DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor91f84212008-12-11 16:49:14 +00001338 if (PrimaryContext != this)
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001339 return PrimaryContext->lookup(Name);
Douglas Gregor91f84212008-12-11 16:49:14 +00001340
Richard Smith8cebe372015-02-25 22:20:13 +00001341 // If we have an external source, ensure that any later redeclarations of this
1342 // context have been loaded, since they may add names to the result of this
1343 // lookup (or add external visible storage).
1344 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1345 if (Source)
1346 (void)cast<Decl>(this)->getMostRecentDecl();
Richard Smithbb853c72014-08-13 01:23:33 +00001347
Richard Smith05afe5e2012-03-13 03:12:56 +00001348 if (hasExternalVisibleStorage()) {
Richard Smith8cebe372015-02-25 22:20:13 +00001349 assert(Source && "external visible storage but no external source?");
1350
Richard Smitha8b74592014-03-25 00:34:21 +00001351 if (NeedToReconcileExternalVisibleStorage)
1352 reconcileExternalVisibleStorage();
1353
Richard Smith9e2341d2015-03-23 03:25:59 +00001354 StoredDeclsMap *Map = LookupPtr;
Richard Smitha8b74592014-03-25 00:34:21 +00001355
Richard Smith9e2341d2015-03-23 03:25:59 +00001356 if (HasLazyLocalLexicalLookups || HasLazyExternalLexicalLookups)
Richard Smith40c78062015-02-21 02:31:57 +00001357 // FIXME: Make buildLookup const?
1358 Map = const_cast<DeclContext*>(this)->buildLookup();
Richard Smith645d7552013-02-07 03:37:08 +00001359
Richard Smith75fc3bf2013-02-08 00:37:45 +00001360 if (!Map)
1361 Map = CreateStoredDeclsMap(getParentASTContext());
1362
Richard Smith4abe0a82013-09-09 07:34:56 +00001363 // If we have a lookup result with no external decls, we are done.
Richard Smith75fc3bf2013-02-08 00:37:45 +00001364 std::pair<StoredDeclsMap::iterator, bool> R =
1365 Map->insert(std::make_pair(Name, StoredDeclsList()));
Richard Smith4abe0a82013-09-09 07:34:56 +00001366 if (!R.second && !R.first->second.hasExternalDecls())
Richard Smith75fc3bf2013-02-08 00:37:45 +00001367 return R.first->second.getLookupResult();
Richard Smithf634c902012-03-16 06:12:59 +00001368
Richard Smith309271b2014-03-04 00:21:14 +00001369 if (Source->FindExternalVisibleDeclsByName(this, Name) || !R.second) {
Richard Smith9e2341d2015-03-23 03:25:59 +00001370 if (StoredDeclsMap *Map = LookupPtr) {
Richard Smith9ce12e32013-02-07 03:30:24 +00001371 StoredDeclsMap::iterator I = Map->find(Name);
1372 if (I != Map->end())
1373 return I->second.getLookupResult();
1374 }
1375 }
1376
Richard Smith40c78062015-02-21 02:31:57 +00001377 return lookup_result();
John McCall75b960e2010-06-01 09:23:16 +00001378 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001379
Richard Smith9e2341d2015-03-23 03:25:59 +00001380 StoredDeclsMap *Map = LookupPtr;
1381 if (HasLazyLocalLexicalLookups || HasLazyExternalLexicalLookups)
Richard Smith40c78062015-02-21 02:31:57 +00001382 Map = const_cast<DeclContext*>(this)->buildLookup();
Richard Smithf634c902012-03-16 06:12:59 +00001383
1384 if (!Map)
Richard Smith40c78062015-02-21 02:31:57 +00001385 return lookup_result();
Richard Smithf634c902012-03-16 06:12:59 +00001386
1387 StoredDeclsMap::iterator I = Map->find(Name);
1388 if (I == Map->end())
Richard Smith40c78062015-02-21 02:31:57 +00001389 return lookup_result();
Richard Smithf634c902012-03-16 06:12:59 +00001390
1391 return I->second.getLookupResult();
Douglas Gregor91f84212008-12-11 16:49:14 +00001392}
1393
Richard Smith95d99302013-07-13 02:00:19 +00001394DeclContext::lookup_result
1395DeclContext::noload_lookup(DeclarationName Name) {
1396 assert(DeclKind != Decl::LinkageSpec &&
1397 "Should not perform lookups into linkage specs!");
Richard Smith95d99302013-07-13 02:00:19 +00001398
1399 DeclContext *PrimaryContext = getPrimaryContext();
1400 if (PrimaryContext != this)
1401 return PrimaryContext->noload_lookup(Name);
1402
Richard Smith9e2341d2015-03-23 03:25:59 +00001403 // If we have any lazy lexical declarations not in our lookup map, add them
1404 // now. Don't import any external declarations, not even if we know we have
1405 // some missing from the external visible lookups.
1406 if (HasLazyLocalLexicalLookups) {
Vince Harrona3ea9a42015-03-22 05:59:59 +00001407 SmallVector<DeclContext *, 2> Contexts;
1408 collectAllContexts(Contexts);
1409 for (unsigned I = 0, N = Contexts.size(); I != N; ++I)
Richard Smith9e2341d2015-03-23 03:25:59 +00001410 buildLookupImpl(Contexts[I], hasExternalVisibleStorage());
1411 HasLazyLocalLexicalLookups = false;
Vince Harrona3ea9a42015-03-22 05:59:59 +00001412 }
1413
Richard Smith9e2341d2015-03-23 03:25:59 +00001414 StoredDeclsMap *Map = LookupPtr;
Richard Smith95d99302013-07-13 02:00:19 +00001415 if (!Map)
Richard Smith40c78062015-02-21 02:31:57 +00001416 return lookup_result();
Richard Smith95d99302013-07-13 02:00:19 +00001417
1418 StoredDeclsMap::iterator I = Map->find(Name);
Craig Topper36250ad2014-05-12 05:36:57 +00001419 return I != Map->end() ? I->second.getLookupResult()
Richard Smith40c78062015-02-21 02:31:57 +00001420 : lookup_result();
Richard Smith95d99302013-07-13 02:00:19 +00001421}
1422
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001423void DeclContext::localUncachedLookup(DeclarationName Name,
1424 SmallVectorImpl<NamedDecl *> &Results) {
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001425 Results.clear();
1426
1427 // If there's no external storage, just perform a normal lookup and copy
1428 // the results.
Douglas Gregordd6006f2012-07-17 21:16:27 +00001429 if (!hasExternalVisibleStorage() && !hasExternalLexicalStorage() && Name) {
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001430 lookup_result LookupResults = lookup(Name);
David Blaikieff7d47a2012-12-19 00:45:41 +00001431 Results.insert(Results.end(), LookupResults.begin(), LookupResults.end());
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001432 return;
1433 }
1434
1435 // If we have a lookup table, check there first. Maybe we'll get lucky.
Richard Smith9e2341d2015-03-23 03:25:59 +00001436 // FIXME: Should we be checking these flags on the primary context?
1437 if (Name && !HasLazyLocalLexicalLookups && !HasLazyExternalLexicalLookups) {
1438 if (StoredDeclsMap *Map = LookupPtr) {
Douglas Gregordd6006f2012-07-17 21:16:27 +00001439 StoredDeclsMap::iterator Pos = Map->find(Name);
1440 if (Pos != Map->end()) {
1441 Results.insert(Results.end(),
David Blaikieff7d47a2012-12-19 00:45:41 +00001442 Pos->second.getLookupResult().begin(),
1443 Pos->second.getLookupResult().end());
Douglas Gregordd6006f2012-07-17 21:16:27 +00001444 return;
1445 }
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001446 }
1447 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00001448
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001449 // Slow case: grovel through the declarations in our chain looking for
1450 // matches.
Richard Smith9e2341d2015-03-23 03:25:59 +00001451 // FIXME: If we have lazy external declarations, this will not find them!
1452 // FIXME: Should we CollectAllContexts and walk them all here?
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001453 for (Decl *D = FirstDecl; D; D = D->getNextDeclInContext()) {
1454 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1455 if (ND->getDeclName() == Name)
1456 Results.push_back(ND);
1457 }
1458}
1459
Sebastian Redl50c68252010-08-31 00:36:30 +00001460DeclContext *DeclContext::getRedeclContext() {
Chris Lattner17a1bfa2009-03-27 19:19:59 +00001461 DeclContext *Ctx = this;
Sebastian Redlbd595762010-08-31 20:53:31 +00001462 // Skip through transparent contexts.
1463 while (Ctx->isTransparentContext())
Douglas Gregor6ad0ef52009-01-06 23:51:29 +00001464 Ctx = Ctx->getParent();
1465 return Ctx;
1466}
1467
Douglas Gregorf47b9112009-02-25 22:02:03 +00001468DeclContext *DeclContext::getEnclosingNamespaceContext() {
1469 DeclContext *Ctx = this;
1470 // Skip through non-namespace, non-translation-unit contexts.
Sebastian Redl4f08c962010-08-31 00:36:23 +00001471 while (!Ctx->isFileContext())
Douglas Gregorf47b9112009-02-25 22:02:03 +00001472 Ctx = Ctx->getParent();
1473 return Ctx->getPrimaryContext();
1474}
1475
Reid Klecknerd60b82f2014-11-17 23:36:45 +00001476RecordDecl *DeclContext::getOuterLexicalRecordContext() {
1477 // Loop until we find a non-record context.
1478 RecordDecl *OutermostRD = nullptr;
1479 DeclContext *DC = this;
1480 while (DC->isRecord()) {
1481 OutermostRD = cast<RecordDecl>(DC);
1482 DC = DC->getLexicalParent();
1483 }
1484 return OutermostRD;
1485}
1486
Sebastian Redl50c68252010-08-31 00:36:30 +00001487bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
1488 // For non-file contexts, this is equivalent to Equals.
1489 if (!isFileContext())
1490 return O->Equals(this);
1491
1492 do {
1493 if (O->Equals(this))
1494 return true;
1495
1496 const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O);
1497 if (!NS || !NS->isInline())
1498 break;
1499 O = NS->getParent();
1500 } while (O);
1501
1502 return false;
1503}
1504
Richard Smithf634c902012-03-16 06:12:59 +00001505void DeclContext::makeDeclVisibleInContext(NamedDecl *D) {
1506 DeclContext *PrimaryDC = this->getPrimaryContext();
1507 DeclContext *DeclDC = D->getDeclContext()->getPrimaryContext();
1508 // If the decl is being added outside of its semantic decl context, we
1509 // need to ensure that we eagerly build the lookup information for it.
1510 PrimaryDC->makeDeclVisibleInContextWithFlags(D, false, PrimaryDC == DeclDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00001511}
1512
Richard Smithf634c902012-03-16 06:12:59 +00001513void DeclContext::makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal,
1514 bool Recoverable) {
1515 assert(this == getPrimaryContext() && "expected a primary DC");
Sean Callanan95e74be2011-10-21 02:57:43 +00001516
Richard Smith05afe5e2012-03-13 03:12:56 +00001517 // Skip declarations within functions.
Richard Smith114394f2013-08-09 04:35:01 +00001518 if (isFunctionOrMethod())
Richard Smith05afe5e2012-03-13 03:12:56 +00001519 return;
1520
Richard Smithf634c902012-03-16 06:12:59 +00001521 // Skip declarations which should be invisible to name lookup.
1522 if (shouldBeHidden(D))
1523 return;
1524
1525 // If we already have a lookup data structure, perform the insertion into
1526 // it. If we might have externally-stored decls with this name, look them
1527 // up and perform the insertion. If this decl was declared outside its
1528 // semantic context, buildLookup won't add it, so add it now.
1529 //
1530 // FIXME: As a performance hack, don't add such decls into the translation
1531 // unit unless we're in C++, since qualified lookup into the TU is never
1532 // performed.
Richard Smith9e2341d2015-03-23 03:25:59 +00001533 if (LookupPtr || hasExternalVisibleStorage() ||
Richard Smithf634c902012-03-16 06:12:59 +00001534 ((!Recoverable || D->getDeclContext() != D->getLexicalDeclContext()) &&
1535 (getParentASTContext().getLangOpts().CPlusPlus ||
1536 !isTranslationUnit()))) {
1537 // If we have lazily omitted any decls, they might have the same name as
1538 // the decl which we are adding, so build a full lookup table before adding
1539 // this decl.
1540 buildLookup();
1541 makeDeclVisibleInContextImpl(D, Internal);
1542 } else {
Richard Smith9e2341d2015-03-23 03:25:59 +00001543 HasLazyLocalLexicalLookups = true;
Richard Smithf634c902012-03-16 06:12:59 +00001544 }
1545
1546 // If we are a transparent context or inline namespace, insert into our
1547 // parent context, too. This operation is recursive.
1548 if (isTransparentContext() || isInlineNamespace())
1549 getParent()->getPrimaryContext()->
1550 makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
1551
1552 Decl *DCAsDecl = cast<Decl>(this);
1553 // Notify that a decl was made visible unless we are a Tag being defined.
1554 if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined()))
1555 if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
1556 L->AddedVisibleDecl(this, D);
1557}
1558
1559void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal) {
1560 // Find or create the stored declaration map.
Richard Smith9e2341d2015-03-23 03:25:59 +00001561 StoredDeclsMap *Map = LookupPtr;
Richard Smithf634c902012-03-16 06:12:59 +00001562 if (!Map) {
1563 ASTContext *C = &getParentASTContext();
1564 Map = CreateStoredDeclsMap(*C);
Argyrios Kyrtzidise51e5542010-07-04 21:44:25 +00001565 }
1566
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001567 // If there is an external AST source, load any declarations it knows about
1568 // with this declaration's name.
1569 // If the lookup table contains an entry about this name it means that we
1570 // have already checked the external source.
Sean Callanan95e74be2011-10-21 02:57:43 +00001571 if (!Internal)
1572 if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
1573 if (hasExternalVisibleStorage() &&
Richard Smithf634c902012-03-16 06:12:59 +00001574 Map->find(D->getDeclName()) == Map->end())
Sean Callanan95e74be2011-10-21 02:57:43 +00001575 Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001576
Douglas Gregor91f84212008-12-11 16:49:14 +00001577 // Insert this declaration into the map.
Richard Smithf634c902012-03-16 06:12:59 +00001578 StoredDeclsList &DeclNameEntries = (*Map)[D->getDeclName()];
Richard Smith4abe0a82013-09-09 07:34:56 +00001579
1580 if (Internal) {
1581 // If this is being added as part of loading an external declaration,
1582 // this may not be the only external declaration with this name.
1583 // In this case, we never try to replace an existing declaration; we'll
1584 // handle that when we finalize the list of declarations for this name.
1585 DeclNameEntries.setHasExternalDecls();
1586 DeclNameEntries.AddSubsequentDecl(D);
1587 return;
1588 }
1589
Richard Smitha3271c12015-02-07 00:45:52 +00001590 if (DeclNameEntries.isNull()) {
Chris Lattnercaae7162009-02-20 01:44:05 +00001591 DeclNameEntries.setOnlyValue(D);
Richard Smithf634c902012-03-16 06:12:59 +00001592 return;
Douglas Gregor91f84212008-12-11 16:49:14 +00001593 }
Chris Lattner24e24d52009-02-20 00:55:03 +00001594
Richard Smithe8292b12015-02-10 03:28:10 +00001595 if (DeclNameEntries.HandleRedeclaration(D, /*IsKnownNewer*/!Internal)) {
Richard Smithf634c902012-03-16 06:12:59 +00001596 // This declaration has replaced an existing one for which
1597 // declarationReplaces returns true.
1598 return;
1599 }
Mike Stump11289f42009-09-09 15:08:12 +00001600
Richard Smithf634c902012-03-16 06:12:59 +00001601 // Put this declaration into the appropriate slot.
1602 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor91f84212008-12-11 16:49:14 +00001603}
Douglas Gregor889ceb72009-02-03 19:21:40 +00001604
Richard Smith40c78062015-02-21 02:31:57 +00001605UsingDirectiveDecl *DeclContext::udir_iterator::operator*() const {
1606 return cast<UsingDirectiveDecl>(*I);
1607}
1608
Douglas Gregor889ceb72009-02-03 19:21:40 +00001609/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
1610/// this context.
Aaron Ballman804a7fb2014-03-17 17:14:12 +00001611DeclContext::udir_range DeclContext::using_directives() const {
Richard Smith05afe5e2012-03-13 03:12:56 +00001612 // FIXME: Use something more efficient than normal lookup for using
1613 // directives. In C++, using directives are looked up more than anything else.
Richard Smithcf4bdde2015-02-21 02:45:19 +00001614 lookup_result Result = lookup(UsingDirectiveDecl::getName());
Richard Smith40c78062015-02-21 02:31:57 +00001615 return udir_range(Result.begin(), Result.end());
Douglas Gregor889ceb72009-02-03 19:21:40 +00001616}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001617
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001618//===----------------------------------------------------------------------===//
1619// Creation and Destruction of StoredDeclsMaps. //
1620//===----------------------------------------------------------------------===//
1621
John McCallc62bb642010-03-24 05:22:00 +00001622StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
Richard Smith9e2341d2015-03-23 03:25:59 +00001623 assert(!LookupPtr && "context already has a decls map");
John McCallc62bb642010-03-24 05:22:00 +00001624 assert(getPrimaryContext() == this &&
1625 "creating decls map on non-primary context");
1626
1627 StoredDeclsMap *M;
1628 bool Dependent = isDependentContext();
1629 if (Dependent)
1630 M = new DependentStoredDeclsMap();
1631 else
1632 M = new StoredDeclsMap();
1633 M->Previous = C.LastSDM;
1634 C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
Richard Smith9e2341d2015-03-23 03:25:59 +00001635 LookupPtr = M;
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001636 return M;
1637}
1638
1639void ASTContext::ReleaseDeclContextMaps() {
John McCallc62bb642010-03-24 05:22:00 +00001640 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1641 // pointer because the subclass doesn't add anything that needs to
1642 // be deleted.
John McCallc62bb642010-03-24 05:22:00 +00001643 StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1644}
1645
1646void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1647 while (Map) {
1648 // Advance the iteration before we invalidate memory.
1649 llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1650
1651 if (Dependent)
1652 delete static_cast<DependentStoredDeclsMap*>(Map);
1653 else
1654 delete Map;
1655
1656 Map = Next.getPointer();
1657 Dependent = Next.getInt();
1658 }
1659}
1660
John McCallc62bb642010-03-24 05:22:00 +00001661DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1662 DeclContext *Parent,
1663 const PartialDiagnostic &PDiag) {
1664 assert(Parent->isDependentContext()
1665 && "cannot iterate dependent diagnostics of non-dependent context");
1666 Parent = Parent->getPrimaryContext();
Richard Smith9e2341d2015-03-23 03:25:59 +00001667 if (!Parent->LookupPtr)
John McCallc62bb642010-03-24 05:22:00 +00001668 Parent->CreateStoredDeclsMap(C);
1669
Richard Smith9e2341d2015-03-23 03:25:59 +00001670 DependentStoredDeclsMap *Map =
1671 static_cast<DependentStoredDeclsMap *>(Parent->LookupPtr);
John McCallc62bb642010-03-24 05:22:00 +00001672
Douglas Gregora55530e2010-03-29 23:56:53 +00001673 // Allocate the copy of the PartialDiagnostic via the ASTContext's
Douglas Gregor89336232010-03-29 23:34:08 +00001674 // BumpPtrAllocator, rather than the ASTContext itself.
Craig Topper36250ad2014-05-12 05:36:57 +00001675 PartialDiagnostic::Storage *DiagStorage = nullptr;
Douglas Gregora55530e2010-03-29 23:56:53 +00001676 if (PDiag.hasStorage())
1677 DiagStorage = new (C) PartialDiagnostic::Storage;
1678
1679 DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
John McCallc62bb642010-03-24 05:22:00 +00001680
1681 // TODO: Maybe we shouldn't reverse the order during insertion.
1682 DD->NextDiagnostic = Map->FirstDiagnostic;
1683 Map->FirstDiagnostic = DD;
1684
1685 return DD;
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001686}