blob: 5d130acfbedfbe39cc44cbaf83d61e8d8f5155b1 [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"
Chris Lattnereae6cb62009-03-05 08:00:35 +000031#include "llvm/Support/raw_ostream.h"
Douglas Gregor8b9ccca2008-12-23 21:05:05 +000032#include <algorithm>
Eli Friedman7dbab8a2008-06-07 16:52:53 +000033using namespace clang;
34
35//===----------------------------------------------------------------------===//
36// Statistics
37//===----------------------------------------------------------------------===//
38
Alexis Hunted053252010-05-30 07:21:58 +000039#define DECL(DERIVED, BASE) static int n##DERIVED##s = 0;
40#define ABSTRACT_DECL(DECL)
41#include "clang/AST/DeclNodes.inc"
Eli Friedman7dbab8a2008-06-07 16:52:53 +000042
Douglas Gregor7dab26b2013-02-09 01:35:03 +000043void Decl::updateOutOfDate(IdentifierInfo &II) const {
44 getASTContext().getExternalSource()->updateOutOfDateIdentifier(II);
45}
46
James Y Knight53c76162015-07-17 18:21:37 +000047#define DECL(DERIVED, BASE) \
Benjamin Kramerc3f89252016-10-20 14:27:22 +000048 static_assert(alignof(Decl) >= alignof(DERIVED##Decl), \
James Y Knight53c76162015-07-17 18:21:37 +000049 "Alignment sufficient after objects prepended to " #DERIVED);
50#define ABSTRACT_DECL(DECL)
51#include "clang/AST/DeclNodes.inc"
52
Richard Smithf7981722013-11-22 09:01:48 +000053void *Decl::operator new(std::size_t Size, const ASTContext &Context,
54 unsigned ID, std::size_t Extra) {
Douglas Gregor52261fd2012-01-05 23:49:36 +000055 // Allocate an extra 8 bytes worth of storage, which ensures that the
James Y Knight53c76162015-07-17 18:21:37 +000056 // resulting pointer will still be 8-byte aligned.
Benjamin Kramerc3f89252016-10-20 14:27:22 +000057 static_assert(sizeof(unsigned) * 2 >= alignof(Decl),
James Y Knight53c76162015-07-17 18:21:37 +000058 "Decl won't be misaligned");
Richard Smithf7981722013-11-22 09:01:48 +000059 void *Start = Context.Allocate(Size + Extra + 8);
Douglas Gregor52261fd2012-01-05 23:49:36 +000060 void *Result = (char*)Start + 8;
Richard Smithf7981722013-11-22 09:01:48 +000061
Douglas Gregorcfe7dc62012-01-09 17:30:44 +000062 unsigned *PrefixPtr = (unsigned *)Result - 2;
Richard Smithf7981722013-11-22 09:01:48 +000063
Douglas Gregorcfe7dc62012-01-09 17:30:44 +000064 // Zero out the first 4 bytes; this is used to store the owning module ID.
65 PrefixPtr[0] = 0;
Richard Smithf7981722013-11-22 09:01:48 +000066
Douglas Gregorcfe7dc62012-01-09 17:30:44 +000067 // Store the global declaration ID in the second 4 bytes.
68 PrefixPtr[1] = ID;
Richard Smithf7981722013-11-22 09:01:48 +000069
Douglas Gregor64af53c2012-01-05 22:27:05 +000070 return Result;
Douglas Gregor72172e92012-01-05 21:55:30 +000071}
72
Richard Smithf7981722013-11-22 09:01:48 +000073void *Decl::operator new(std::size_t Size, const ASTContext &Ctx,
74 DeclContext *Parent, std::size_t Extra) {
75 assert(!Parent || &Parent->getParentASTContext() == &Ctx);
Richard Smith42413142015-05-15 20:05:43 +000076 // With local visibility enabled, we track the owning module even for local
77 // declarations.
78 if (Ctx.getLangOpts().ModulesLocalVisibility) {
James Y Knight53c76162015-07-17 18:21:37 +000079 // Ensure required alignment of the resulting object by adding extra
80 // padding at the start if required.
81 size_t ExtraAlign =
Benjamin Kramerc3f89252016-10-20 14:27:22 +000082 llvm::OffsetToAlignment(sizeof(Module *), alignof(Decl));
James Y Knight53c76162015-07-17 18:21:37 +000083 char *Buffer = reinterpret_cast<char *>(
84 ::operator new(ExtraAlign + sizeof(Module *) + Size + Extra, Ctx));
85 Buffer += ExtraAlign;
Richard Smith42413142015-05-15 20:05:43 +000086 return new (Buffer) Module*(nullptr) + 1;
87 }
Richard Smithf7981722013-11-22 09:01:48 +000088 return ::operator new(Size + Extra, Ctx);
89}
90
Douglas Gregorc147b0b2013-01-12 01:29:50 +000091Module *Decl::getOwningModuleSlow() const {
92 assert(isFromASTFile() && "Not from AST file?");
93 return getASTContext().getExternalSource()->getModule(getOwningModuleID());
94}
95
Richard Smith87bb5692015-06-09 00:35:49 +000096bool Decl::hasLocalOwningModuleStorage() const {
97 return getASTContext().getLangOpts().ModulesLocalVisibility;
98}
99
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000100const char *Decl::getDeclKindName() const {
101 switch (DeclKind) {
David Blaikie83d382b2011-09-23 05:06:16 +0000102 default: llvm_unreachable("Declaration not in DeclNodes.inc!");
Alexis Hunted053252010-05-30 07:21:58 +0000103#define DECL(DERIVED, BASE) case DERIVED: return #DERIVED;
104#define ABSTRACT_DECL(DECL)
105#include "clang/AST/DeclNodes.inc"
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000106 }
107}
108
Douglas Gregor90d47172010-03-05 00:26:45 +0000109void Decl::setInvalidDecl(bool Invalid) {
110 InvalidDecl = Invalid;
Alp Tokera5d64592013-12-21 01:10:54 +0000111 assert(!isa<TagDecl>(this) || !cast<TagDecl>(this)->isCompleteDefinition());
Argyrios Kyrtzidisb7d1ca22012-03-09 21:09:04 +0000112 if (Invalid && !isa<ParmVarDecl>(this)) {
Douglas Gregor90d47172010-03-05 00:26:45 +0000113 // Defensive maneuver for ill-formed code: we're likely not to make it to
114 // a point where we set the access specifier, so default it to "public"
115 // to avoid triggering asserts elsewhere in the front end.
116 setAccess(AS_public);
117 }
118}
119
Steve Naroff5faaef72009-01-20 19:53:53 +0000120const char *DeclContext::getDeclKindName() const {
121 switch (DeclKind) {
David Blaikie83d382b2011-09-23 05:06:16 +0000122 default: llvm_unreachable("Declaration context not in DeclNodes.inc!");
Alexis Hunted053252010-05-30 07:21:58 +0000123#define DECL(DERIVED, BASE) case Decl::DERIVED: return #DERIVED;
124#define ABSTRACT_DECL(DECL)
125#include "clang/AST/DeclNodes.inc"
Steve Naroff5faaef72009-01-20 19:53:53 +0000126 }
127}
128
Daniel Dunbar62905572012-03-05 21:42:49 +0000129bool Decl::StatisticsEnabled = false;
130void Decl::EnableStatistics() {
131 StatisticsEnabled = true;
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000132}
133
134void Decl::PrintStats() {
Chandler Carruthbfb154a2011-07-04 06:13:27 +0000135 llvm::errs() << "\n*** Decl Stats:\n";
Mike Stump11289f42009-09-09 15:08:12 +0000136
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +0000137 int totalDecls = 0;
Alexis Hunted053252010-05-30 07:21:58 +0000138#define DECL(DERIVED, BASE) totalDecls += n##DERIVED##s;
139#define ABSTRACT_DECL(DECL)
140#include "clang/AST/DeclNodes.inc"
Chandler Carruthbfb154a2011-07-04 06:13:27 +0000141 llvm::errs() << " " << totalDecls << " decls total.\n";
Mike Stump11289f42009-09-09 15:08:12 +0000142
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +0000143 int totalBytes = 0;
Alexis Hunted053252010-05-30 07:21:58 +0000144#define DECL(DERIVED, BASE) \
145 if (n##DERIVED##s > 0) { \
146 totalBytes += (int)(n##DERIVED##s * sizeof(DERIVED##Decl)); \
Chandler Carruthbfb154a2011-07-04 06:13:27 +0000147 llvm::errs() << " " << n##DERIVED##s << " " #DERIVED " decls, " \
148 << sizeof(DERIVED##Decl) << " each (" \
149 << n##DERIVED##s * sizeof(DERIVED##Decl) \
150 << " bytes)\n"; \
Douglas Gregor8bd3c2e2009-02-02 23:39:07 +0000151 }
Alexis Hunted053252010-05-30 07:21:58 +0000152#define ABSTRACT_DECL(DECL)
153#include "clang/AST/DeclNodes.inc"
Mike Stump11289f42009-09-09 15:08:12 +0000154
Chandler Carruthbfb154a2011-07-04 06:13:27 +0000155 llvm::errs() << "Total bytes = " << totalBytes << "\n";
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000156}
157
Alexis Hunted053252010-05-30 07:21:58 +0000158void Decl::add(Kind k) {
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000159 switch (k) {
Alexis Hunted053252010-05-30 07:21:58 +0000160#define DECL(DERIVED, BASE) case DERIVED: ++n##DERIVED##s; break;
161#define ABSTRACT_DECL(DECL)
162#include "clang/AST/DeclNodes.inc"
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000163 }
164}
165
Anders Carlssonaa73b912009-06-13 00:08:58 +0000166bool Decl::isTemplateParameterPack() const {
167 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
168 return TTP->isParameterPack();
Douglas Gregorda3cc0d2010-12-23 23:51:58 +0000169 if (const NonTypeTemplateParmDecl *NTTP
Douglas Gregorf5500772011-01-05 15:48:55 +0000170 = dyn_cast<NonTypeTemplateParmDecl>(this))
Douglas Gregorda3cc0d2010-12-23 23:51:58 +0000171 return NTTP->isParameterPack();
Douglas Gregorf5500772011-01-05 15:48:55 +0000172 if (const TemplateTemplateParmDecl *TTP
173 = dyn_cast<TemplateTemplateParmDecl>(this))
174 return TTP->isParameterPack();
Anders Carlssonaa73b912009-06-13 00:08:58 +0000175 return false;
176}
177
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +0000178bool Decl::isParameterPack() const {
179 if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(this))
180 return Parm->isParameterPack();
181
182 return isTemplateParameterPack();
183}
184
Alp Tokera2794f92014-01-22 07:29:52 +0000185FunctionDecl *Decl::getAsFunction() {
186 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
187 return FD;
188 if (const FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(this))
189 return FTD->getTemplatedDecl();
Craig Topper36250ad2014-05-12 05:36:57 +0000190 return nullptr;
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000191}
192
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000193bool Decl::isTemplateDecl() const {
194 return isa<TemplateDecl>(this);
195}
196
Serge Pavlov7dcc97e2016-04-19 06:19:52 +0000197TemplateDecl *Decl::getDescribedTemplate() const {
198 if (auto *FD = dyn_cast<FunctionDecl>(this))
199 return FD->getDescribedFunctionTemplate();
200 else if (auto *RD = dyn_cast<CXXRecordDecl>(this))
201 return RD->getDescribedClassTemplate();
202 else if (auto *VD = dyn_cast<VarDecl>(this))
203 return VD->getDescribedVarTemplate();
204
205 return nullptr;
206}
207
Argyrios Kyrtzidis0ce4c9a2011-09-28 02:45:33 +0000208const DeclContext *Decl::getParentFunctionOrMethod() const {
209 for (const DeclContext *DC = getDeclContext();
210 DC && !DC->isTranslationUnit() && !DC->isNamespace();
Douglas Gregorf5974fa2010-01-16 20:21:20 +0000211 DC = DC->getParent())
212 if (DC->isFunctionOrMethod())
Argyrios Kyrtzidis0ce4c9a2011-09-28 02:45:33 +0000213 return DC;
Douglas Gregorf5974fa2010-01-16 20:21:20 +0000214
Craig Topper36250ad2014-05-12 05:36:57 +0000215 return nullptr;
Douglas Gregorf5974fa2010-01-16 20:21:20 +0000216}
217
Douglas Gregor133eddd2011-02-17 08:47:29 +0000218
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000219//===----------------------------------------------------------------------===//
Chris Lattnereae6cb62009-03-05 08:00:35 +0000220// PrettyStackTraceDecl Implementation
221//===----------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000222
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000223void PrettyStackTraceDecl::print(raw_ostream &OS) const {
Chris Lattnereae6cb62009-03-05 08:00:35 +0000224 SourceLocation TheLoc = Loc;
225 if (TheLoc.isInvalid() && TheDecl)
226 TheLoc = TheDecl->getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000227
Chris Lattnereae6cb62009-03-05 08:00:35 +0000228 if (TheLoc.isValid()) {
229 TheLoc.print(OS, SM);
230 OS << ": ";
231 }
232
233 OS << Message;
234
Benjamin Kramer24ebf7c2013-02-23 13:53:57 +0000235 if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl)) {
236 OS << " '";
237 DN->printQualifiedName(OS);
238 OS << '\'';
239 }
Chris Lattnereae6cb62009-03-05 08:00:35 +0000240 OS << '\n';
241}
Mike Stump11289f42009-09-09 15:08:12 +0000242
Chris Lattnereae6cb62009-03-05 08:00:35 +0000243//===----------------------------------------------------------------------===//
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000244// Decl Implementation
245//===----------------------------------------------------------------------===//
246
Douglas Gregorb11aad82011-02-19 18:51:44 +0000247// Out-of-line virtual method providing a home for Decl.
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000248Decl::~Decl() { }
Douglas Gregora43942a2011-02-17 07:02:32 +0000249
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000250void Decl::setDeclContext(DeclContext *DC) {
Chris Lattnerb81eb052009-03-29 06:06:59 +0000251 DeclCtx = DC;
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000252}
253
254void Decl::setLexicalDeclContext(DeclContext *DC) {
255 if (DC == getLexicalDeclContext())
256 return;
257
258 if (isInSemaDC()) {
Argyrios Kyrtzidis6f40eb72012-02-09 02:44:08 +0000259 setDeclContextsImpl(getDeclContext(), DC, getASTContext());
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000260 } else {
261 getMultipleDC()->LexicalDC = DC;
262 }
Richard Smith5327b892015-07-01 19:32:54 +0000263 Hidden = cast<Decl>(DC)->Hidden;
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000264}
265
Argyrios Kyrtzidis6f40eb72012-02-09 02:44:08 +0000266void Decl::setDeclContextsImpl(DeclContext *SemaDC, DeclContext *LexicalDC,
267 ASTContext &Ctx) {
268 if (SemaDC == LexicalDC) {
269 DeclCtx = SemaDC;
270 } else {
271 Decl::MultipleDC *MDC = new (Ctx) Decl::MultipleDC();
272 MDC->SemanticDC = SemaDC;
273 MDC->LexicalDC = LexicalDC;
274 DeclCtx = MDC;
275 }
276}
277
Serge Pavlov73c6a242015-08-23 10:22:28 +0000278bool Decl::isLexicallyWithinFunctionOrMethod() const {
279 const DeclContext *LDC = getLexicalDeclContext();
Serge Pavlovb24a7112015-08-23 11:09:40 +0000280 while (true) {
Serge Pavlov73c6a242015-08-23 10:22:28 +0000281 if (LDC->isFunctionOrMethod())
282 return true;
283 if (!isa<TagDecl>(LDC))
284 return false;
Serge Pavlovb24a7112015-08-23 11:09:40 +0000285 LDC = LDC->getLexicalParent();
286 }
Serge Pavlov73c6a242015-08-23 10:22:28 +0000287 return false;
288}
289
John McCall4fa53422009-10-01 00:25:31 +0000290bool Decl::isInAnonymousNamespace() const {
291 const DeclContext *DC = getDeclContext();
292 do {
293 if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
294 if (ND->isAnonymousNamespace())
295 return true;
296 } while ((DC = DC->getParent()));
297
298 return false;
299}
300
Richard Trieuc771d5d2014-05-28 02:16:01 +0000301bool Decl::isInStdNamespace() const {
302 return getDeclContext()->isStdNamespace();
303}
304
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000305TranslationUnitDecl *Decl::getTranslationUnitDecl() {
Argyrios Kyrtzidis4e1a72b2009-06-30 02:34:53 +0000306 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
307 return TUD;
308
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000309 DeclContext *DC = getDeclContext();
310 assert(DC && "This decl is not contained in a translation unit!");
Mike Stump11289f42009-09-09 15:08:12 +0000311
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000312 while (!DC->isTranslationUnit()) {
313 DC = DC->getParent();
314 assert(DC && "This decl is not contained in a translation unit!");
315 }
Mike Stump11289f42009-09-09 15:08:12 +0000316
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000317 return cast<TranslationUnitDecl>(DC);
318}
319
320ASTContext &Decl::getASTContext() const {
Mike Stump11289f42009-09-09 15:08:12 +0000321 return getTranslationUnitDecl()->getASTContext();
Argyrios Kyrtzidis743e7db2009-06-29 17:38:40 +0000322}
323
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +0000324ASTMutationListener *Decl::getASTMutationListener() const {
325 return getASTContext().getASTMutationListener();
326}
327
Benjamin Kramerea70eb32012-12-01 15:09:41 +0000328unsigned Decl::getMaxAlignment() const {
329 if (!hasAttrs())
330 return 0;
331
332 unsigned Align = 0;
333 const AttrVec &V = getAttrs();
334 ASTContext &Ctx = getASTContext();
335 specific_attr_iterator<AlignedAttr> I(V.begin()), E(V.end());
336 for (; I != E; ++I)
337 Align = std::max(Align, I->getAlignment(Ctx));
338 return Align;
339}
340
Vassil Vassilev928c8252016-04-28 14:13:28 +0000341bool Decl::isUsed(bool CheckUsedAttr) const {
342 const Decl *CanonD = getCanonicalDecl();
343 if (CanonD->Used)
Vassil Vassileva4d7d782016-04-27 10:46:06 +0000344 return true;
345
Vassil Vassilev928c8252016-04-28 14:13:28 +0000346 // Check for used attribute.
347 // Ask the most recent decl, since attributes accumulate in the redecl chain.
348 if (CheckUsedAttr && getMostRecentDecl()->hasAttr<UsedAttr>())
349 return true;
350
351 // The information may have not been deserialized yet. Force deserialization
352 // to complete the needed information.
353 return getMostRecentDecl()->getCanonicalDecl()->Used;
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000354}
355
Eli Friedman276dd182013-09-05 00:02:25 +0000356void Decl::markUsed(ASTContext &C) {
Vassil Vassilev928c8252016-04-28 14:13:28 +0000357 if (isUsed(false))
Eli Friedman276dd182013-09-05 00:02:25 +0000358 return;
359
360 if (C.getASTMutationListener())
361 C.getASTMutationListener()->DeclarationMarkedUsed(this);
362
Vassil Vassilev928c8252016-04-28 14:13:28 +0000363 setIsUsed();
Eli Friedman276dd182013-09-05 00:02:25 +0000364}
365
Argyrios Kyrtzidis16180232011-04-19 19:51:10 +0000366bool Decl::isReferenced() const {
367 if (Referenced)
368 return true;
369
370 // Check redeclarations.
Aaron Ballman86c93902014-03-06 23:45:36 +0000371 for (auto I : redecls())
Argyrios Kyrtzidis16180232011-04-19 19:51:10 +0000372 if (I->Referenced)
373 return true;
374
375 return false;
376}
377
Richard Smith3b660562016-09-26 21:27:23 +0000378bool Decl::isExported() const {
379 if (isModulePrivate())
380 return false;
381 // Namespaces are always exported.
382 if (isa<TranslationUnitDecl>(this) || isa<NamespaceDecl>(this))
383 return true;
384 // Otherwise, this is a strictly lexical check.
385 for (auto *DC = getLexicalDeclContext(); DC; DC = DC->getLexicalParent()) {
386 if (cast<Decl>(DC)->isModulePrivate())
387 return false;
388 if (isa<ExportDecl>(DC))
389 return true;
390 }
391 return false;
392}
393
Dmitry Polukhin85eda122016-04-11 07:48:59 +0000394bool Decl::hasDefiningAttr() const {
395 return hasAttr<AliasAttr>() || hasAttr<IFuncAttr>();
396}
397
398const Attr *Decl::getDefiningAttr() const {
399 if (AliasAttr *AA = getAttr<AliasAttr>())
400 return AA;
401 if (IFuncAttr *IFA = getAttr<IFuncAttr>())
402 return IFA;
403 return nullptr;
404}
405
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000406/// \brief Determine the availability of the given declaration based on
407/// the target platform.
408///
409/// When it returns an availability result other than \c AR_Available,
410/// if the \p Message parameter is non-NULL, it will be set to a
411/// string describing why the entity is unavailable.
412///
413/// FIXME: Make these strings localizable, since they end up in
414/// diagnostics.
415static AvailabilityResult CheckAvailability(ASTContext &Context,
416 const AvailabilityAttr *A,
Erik Pilkington48c7cc92016-07-29 17:37:38 +0000417 std::string *Message,
418 VersionTuple EnclosingVersion) {
419 if (EnclosingVersion.empty())
420 EnclosingVersion = Context.getTargetInfo().getPlatformMinVersion();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000421
Erik Pilkington48c7cc92016-07-29 17:37:38 +0000422 if (EnclosingVersion.empty())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000423 return AR_Available;
424
Bob Wilsonb111ec92015-03-02 19:01:14 +0000425 // Check if this is an App Extension "platform", and if so chop off
426 // the suffix for matching with the actual platform.
427 StringRef ActualPlatform = A->getPlatform()->getName();
428 StringRef RealizedPlatform = ActualPlatform;
429 if (Context.getLangOpts().AppExt) {
430 size_t suffix = RealizedPlatform.rfind("_app_extension");
431 if (suffix != StringRef::npos)
432 RealizedPlatform = RealizedPlatform.slice(0, suffix);
433 }
434
435 StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
436
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000437 // Match the platform name.
Bob Wilsonb111ec92015-03-02 19:01:14 +0000438 if (RealizedPlatform != TargetPlatform)
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000439 return AR_Available;
Bob Wilsonb111ec92015-03-02 19:01:14 +0000440
441 StringRef PrettyPlatformName
442 = AvailabilityAttr::getPrettyPlatformName(ActualPlatform);
443
444 if (PrettyPlatformName.empty())
445 PrettyPlatformName = ActualPlatform;
446
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000447 std::string HintMessage;
448 if (!A->getMessage().empty()) {
449 HintMessage = " - ";
450 HintMessage += A->getMessage();
451 }
452
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000453 // Make sure that this declaration has not been marked 'unavailable'.
454 if (A->getUnavailable()) {
455 if (Message) {
456 Message->clear();
457 llvm::raw_string_ostream Out(*Message);
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000458 Out << "not available on " << PrettyPlatformName
459 << HintMessage;
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000460 }
461
462 return AR_Unavailable;
463 }
464
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000465 // Make sure that this declaration has already been introduced.
466 if (!A->getIntroduced().empty() &&
Erik Pilkington48c7cc92016-07-29 17:37:38 +0000467 EnclosingVersion < A->getIntroduced()) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000468 if (Message) {
469 Message->clear();
470 llvm::raw_string_ostream Out(*Message);
Fariborz Jahanian2618dba2014-10-06 16:46:02 +0000471 VersionTuple VTI(A->getIntroduced());
472 VTI.UseDotAsSeparator();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000473 Out << "introduced in " << PrettyPlatformName << ' '
Fariborz Jahanian2618dba2014-10-06 16:46:02 +0000474 << VTI << HintMessage;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000475 }
476
Duncan P. N. Exon Smith5d6790c2016-03-08 06:12:54 +0000477 return A->getStrict() ? AR_Unavailable : AR_NotYetIntroduced;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000478 }
479
480 // Make sure that this declaration hasn't been obsoleted.
Erik Pilkington48c7cc92016-07-29 17:37:38 +0000481 if (!A->getObsoleted().empty() && EnclosingVersion >= A->getObsoleted()) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000482 if (Message) {
483 Message->clear();
484 llvm::raw_string_ostream Out(*Message);
Fariborz Jahanian2618dba2014-10-06 16:46:02 +0000485 VersionTuple VTO(A->getObsoleted());
486 VTO.UseDotAsSeparator();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000487 Out << "obsoleted in " << PrettyPlatformName << ' '
Fariborz Jahanian2618dba2014-10-06 16:46:02 +0000488 << VTO << HintMessage;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000489 }
490
491 return AR_Unavailable;
492 }
493
494 // Make sure that this declaration hasn't been deprecated.
Erik Pilkington48c7cc92016-07-29 17:37:38 +0000495 if (!A->getDeprecated().empty() && EnclosingVersion >= A->getDeprecated()) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000496 if (Message) {
497 Message->clear();
498 llvm::raw_string_ostream Out(*Message);
Fariborz Jahanian2618dba2014-10-06 16:46:02 +0000499 VersionTuple VTD(A->getDeprecated());
500 VTD.UseDotAsSeparator();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000501 Out << "first deprecated in " << PrettyPlatformName << ' '
Fariborz Jahanian2618dba2014-10-06 16:46:02 +0000502 << VTD << HintMessage;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000503 }
504
505 return AR_Deprecated;
506 }
507
508 return AR_Available;
509}
510
Erik Pilkington48c7cc92016-07-29 17:37:38 +0000511AvailabilityResult Decl::getAvailability(std::string *Message,
512 VersionTuple EnclosingVersion) const {
Duncan P. N. Exon Smithec599a92016-02-26 19:27:00 +0000513 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(this))
Erik Pilkington48c7cc92016-07-29 17:37:38 +0000514 return FTD->getTemplatedDecl()->getAvailability(Message, EnclosingVersion);
Duncan P. N. Exon Smithec599a92016-02-26 19:27:00 +0000515
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000516 AvailabilityResult Result = AR_Available;
517 std::string ResultMessage;
518
Aaron Ballmanb97112e2014-03-08 22:19:01 +0000519 for (const auto *A : attrs()) {
520 if (const auto *Deprecated = dyn_cast<DeprecatedAttr>(A)) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000521 if (Result >= AR_Deprecated)
522 continue;
523
524 if (Message)
525 ResultMessage = Deprecated->getMessage();
526
527 Result = AR_Deprecated;
528 continue;
529 }
530
Aaron Ballmanb97112e2014-03-08 22:19:01 +0000531 if (const auto *Unavailable = dyn_cast<UnavailableAttr>(A)) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000532 if (Message)
533 *Message = Unavailable->getMessage();
534 return AR_Unavailable;
535 }
536
Aaron Ballmanb97112e2014-03-08 22:19:01 +0000537 if (const auto *Availability = dyn_cast<AvailabilityAttr>(A)) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000538 AvailabilityResult AR = CheckAvailability(getASTContext(), Availability,
Erik Pilkington48c7cc92016-07-29 17:37:38 +0000539 Message, EnclosingVersion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000540
541 if (AR == AR_Unavailable)
542 return AR_Unavailable;
543
544 if (AR > Result) {
545 Result = AR;
546 if (Message)
547 ResultMessage.swap(*Message);
548 }
549 continue;
550 }
551 }
552
553 if (Message)
554 Message->swap(ResultMessage);
555 return Result;
556}
557
558bool Decl::canBeWeakImported(bool &IsDefinition) const {
559 IsDefinition = false;
John McCall5fb5df92012-06-20 06:18:46 +0000560
561 // Variables, if they aren't definitions.
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000562 if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
Rafael Espindola6ae7e502013-04-03 19:27:57 +0000563 if (Var->isThisDeclarationADefinition()) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000564 IsDefinition = true;
565 return false;
566 }
John McCall5fb5df92012-06-20 06:18:46 +0000567 return true;
568
569 // Functions, if they aren't definitions.
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000570 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
571 if (FD->hasBody()) {
572 IsDefinition = true;
573 return false;
574 }
John McCall5fb5df92012-06-20 06:18:46 +0000575 return true;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000576
John McCall5fb5df92012-06-20 06:18:46 +0000577 // Objective-C classes, if this is the non-fragile runtime.
578 } else if (isa<ObjCInterfaceDecl>(this) &&
John McCall18ac1632012-06-20 21:58:02 +0000579 getASTContext().getLangOpts().ObjCRuntime.hasWeakClassImport()) {
John McCall5fb5df92012-06-20 06:18:46 +0000580 return true;
581
582 // Nothing else.
583 } else {
584 return false;
585 }
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000586}
587
588bool Decl::isWeakImported() const {
589 bool IsDefinition;
590 if (!canBeWeakImported(IsDefinition))
591 return false;
592
Aaron Ballmanb97112e2014-03-08 22:19:01 +0000593 for (const auto *A : attrs()) {
594 if (isa<WeakImportAttr>(A))
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000595 return true;
596
Aaron Ballmanb97112e2014-03-08 22:19:01 +0000597 if (const auto *Availability = dyn_cast<AvailabilityAttr>(A)) {
Erik Pilkington48c7cc92016-07-29 17:37:38 +0000598 if (CheckAvailability(getASTContext(), Availability, nullptr,
599 VersionTuple()) == AR_NotYetIntroduced)
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000600 return true;
601 }
602 }
603
604 return false;
605}
Tanya Lattner8aefcbe2010-02-17 02:17:21 +0000606
Chris Lattner8e097192009-03-27 20:18:19 +0000607unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
608 switch (DeclKind) {
John McCall3f746822009-11-17 05:59:44 +0000609 case Function:
610 case CXXMethod:
611 case CXXConstructor:
Richard Smith5179eb72016-06-28 19:03:57 +0000612 case ConstructorUsingShadow:
John McCall3f746822009-11-17 05:59:44 +0000613 case CXXDestructor:
614 case CXXConversion:
Chris Lattner8e097192009-03-27 20:18:19 +0000615 case EnumConstant:
616 case Var:
Richard Smithbdb84f32016-07-22 23:36:59 +0000617 case Binding:
Chris Lattner8e097192009-03-27 20:18:19 +0000618 case ImplicitParam:
619 case ParmVar:
Chris Lattner8e097192009-03-27 20:18:19 +0000620 case ObjCMethod:
Daniel Dunbar45b2d8a2010-04-23 13:07:39 +0000621 case ObjCProperty:
John McCall5e77d762013-04-16 07:28:30 +0000622 case MSProperty:
Daniel Dunbar45b2d8a2010-04-23 13:07:39 +0000623 return IDNS_Ordinary;
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000624 case Label:
625 return IDNS_Label;
Francois Pichet783dd6e2010-11-21 06:08:52 +0000626 case IndirectField:
627 return IDNS_Ordinary | IDNS_Member;
628
Richard Smith9f951822016-01-06 22:49:11 +0000629 case NonTypeTemplateParm:
630 // Non-type template parameters are not found by lookups that ignore
631 // non-types, but they are found by redeclaration lookups for tag types,
632 // so we include them in the tag namespace.
633 return IDNS_Ordinary | IDNS_Tag;
634
John McCalle87beb22010-04-23 18:46:30 +0000635 case ObjCCompatibleAlias:
636 case ObjCInterface:
637 return IDNS_Ordinary | IDNS_Type;
638
639 case Typedef:
Richard Smithdda56e42011-04-15 14:24:37 +0000640 case TypeAlias:
Richard Smith3f1b5d02011-05-05 21:57:07 +0000641 case TypeAliasTemplate:
John McCalle87beb22010-04-23 18:46:30 +0000642 case UnresolvedUsingTypename:
643 case TemplateTypeParm:
Douglas Gregor85f3f952015-07-07 03:57:15 +0000644 case ObjCTypeParam:
John McCalle87beb22010-04-23 18:46:30 +0000645 return IDNS_Ordinary | IDNS_Type;
646
John McCall3f746822009-11-17 05:59:44 +0000647 case UsingShadow:
648 return 0; // we'll actually overwrite this later
649
John McCalle61f2ba2009-11-18 02:36:19 +0000650 case UnresolvedUsingValue:
John McCalle61f2ba2009-11-18 02:36:19 +0000651 return IDNS_Ordinary | IDNS_Using;
John McCall3f746822009-11-17 05:59:44 +0000652
653 case Using:
654 return IDNS_Using;
655
Chris Lattner8e097192009-03-27 20:18:19 +0000656 case ObjCProtocol:
Douglas Gregor79947a22009-04-24 00:11:27 +0000657 return IDNS_ObjCProtocol;
Mike Stump11289f42009-09-09 15:08:12 +0000658
Chris Lattner8e097192009-03-27 20:18:19 +0000659 case Field:
660 case ObjCAtDefsField:
661 case ObjCIvar:
662 return IDNS_Member;
Mike Stump11289f42009-09-09 15:08:12 +0000663
Chris Lattner8e097192009-03-27 20:18:19 +0000664 case Record:
665 case CXXRecord:
666 case Enum:
John McCalle87beb22010-04-23 18:46:30 +0000667 return IDNS_Tag | IDNS_Type;
Mike Stump11289f42009-09-09 15:08:12 +0000668
Chris Lattner8e097192009-03-27 20:18:19 +0000669 case Namespace:
John McCalle87beb22010-04-23 18:46:30 +0000670 case NamespaceAlias:
671 return IDNS_Namespace;
672
Chris Lattner8e097192009-03-27 20:18:19 +0000673 case FunctionTemplate:
Larisse Voufo39a1e502013-08-06 01:03:05 +0000674 case VarTemplate:
John McCalle87beb22010-04-23 18:46:30 +0000675 return IDNS_Ordinary;
676
Chris Lattner8e097192009-03-27 20:18:19 +0000677 case ClassTemplate:
678 case TemplateTemplateParm:
John McCalle87beb22010-04-23 18:46:30 +0000679 return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
Mike Stump11289f42009-09-09 15:08:12 +0000680
Alexey Bataev94a4f0c2016-03-03 05:21:39 +0000681 case OMPDeclareReduction:
682 return IDNS_OMPReduction;
683
Chris Lattner8e097192009-03-27 20:18:19 +0000684 // Never have names.
John McCallaa74a0c2009-08-28 07:59:38 +0000685 case Friend:
John McCall11083da2009-09-16 22:47:08 +0000686 case FriendTemplate:
Abramo Bagnarad7340582010-06-05 05:09:32 +0000687 case AccessSpec:
Chris Lattner8e097192009-03-27 20:18:19 +0000688 case LinkageSpec:
Richard Smith8df390f2016-09-08 23:14:54 +0000689 case Export:
Chris Lattner8e097192009-03-27 20:18:19 +0000690 case FileScopeAsm:
691 case StaticAssert:
Chris Lattner8e097192009-03-27 20:18:19 +0000692 case ObjCPropertyImpl:
Nico Weber66220292016-03-02 17:28:48 +0000693 case PragmaComment:
Nico Webercbbaeb12016-03-02 19:28:54 +0000694 case PragmaDetectMismatch:
Chris Lattner8e097192009-03-27 20:18:19 +0000695 case Block:
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000696 case Captured:
Chris Lattner8e097192009-03-27 20:18:19 +0000697 case TranslationUnit:
Richard Smithf19e1272015-03-07 00:04:49 +0000698 case ExternCContext:
Richard Smithbdb84f32016-07-22 23:36:59 +0000699 case Decomposition:
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000700
Chris Lattner8e097192009-03-27 20:18:19 +0000701 case UsingDirective:
David Majnemerd9b1a4f2015-11-04 03:40:30 +0000702 case BuiltinTemplate:
Chris Lattner8e097192009-03-27 20:18:19 +0000703 case ClassTemplateSpecialization:
Douglas Gregor2373c592009-05-31 09:31:02 +0000704 case ClassTemplatePartialSpecialization:
Francois Pichet00c7e6c2011-08-14 03:52:19 +0000705 case ClassScopeFunctionSpecialization:
Larisse Voufo39a1e502013-08-06 01:03:05 +0000706 case VarTemplateSpecialization:
707 case VarTemplatePartialSpecialization:
Douglas Gregore93525e2010-04-22 23:19:50 +0000708 case ObjCImplementation:
709 case ObjCCategory:
710 case ObjCCategoryImpl:
Douglas Gregorba345522011-12-02 23:23:56 +0000711 case Import:
Alexey Bataeva769e072013-03-22 06:34:35 +0000712 case OMPThreadPrivate:
Alexey Bataev4244be22016-02-11 05:35:55 +0000713 case OMPCapturedExpr:
Michael Han84324352013-02-22 17:15:32 +0000714 case Empty:
Douglas Gregore93525e2010-04-22 23:19:50 +0000715 // Never looked up by name.
Chris Lattner8e097192009-03-27 20:18:19 +0000716 return 0;
717 }
John McCall3f746822009-11-17 05:59:44 +0000718
David Blaikiee4d798f2012-01-20 21:50:17 +0000719 llvm_unreachable("Invalid DeclKind!");
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000720}
721
Argyrios Kyrtzidis6f40eb72012-02-09 02:44:08 +0000722void Decl::setAttrsImpl(const AttrVec &attrs, ASTContext &Ctx) {
Argyrios Kyrtzidis91167172010-06-11 23:09:25 +0000723 assert(!HasAttrs && "Decl already contains attrs.");
724
Argyrios Kyrtzidis6f40eb72012-02-09 02:44:08 +0000725 AttrVec &AttrBlank = Ctx.getDeclAttrs(this);
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000726 assert(AttrBlank.empty() && "HasAttrs was wrong?");
Argyrios Kyrtzidis91167172010-06-11 23:09:25 +0000727
728 AttrBlank = attrs;
729 HasAttrs = true;
730}
731
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000732void Decl::dropAttrs() {
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000733 if (!HasAttrs) return;
Mike Stump11289f42009-09-09 15:08:12 +0000734
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000735 HasAttrs = false;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000736 getASTContext().eraseDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000737}
738
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000739const AttrVec &Decl::getAttrs() const {
740 assert(HasAttrs && "No attrs to get!");
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000741 return getASTContext().getDeclAttrs(this);
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000742}
743
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000744Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000745 Decl::Kind DK = D->getDeclKind();
746 switch(DK) {
Alexis Hunted053252010-05-30 07:21:58 +0000747#define DECL(NAME, BASE)
748#define DECL_CONTEXT(NAME) \
749 case Decl::NAME: \
750 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
751#define DECL_CONTEXT_BASE(NAME)
752#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000753 default:
Alexis Hunted053252010-05-30 07:21:58 +0000754#define DECL(NAME, BASE)
755#define DECL_CONTEXT_BASE(NAME) \
756 if (DK >= first##NAME && DK <= last##NAME) \
757 return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
758#include "clang/AST/DeclNodes.inc"
David Blaikie83d382b2011-09-23 05:06:16 +0000759 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000760 }
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000761}
762
763DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000764 Decl::Kind DK = D->getKind();
765 switch(DK) {
Alexis Hunted053252010-05-30 07:21:58 +0000766#define DECL(NAME, BASE)
767#define DECL_CONTEXT(NAME) \
768 case Decl::NAME: \
769 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
770#define DECL_CONTEXT_BASE(NAME)
771#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000772 default:
Alexis Hunted053252010-05-30 07:21:58 +0000773#define DECL(NAME, BASE)
774#define DECL_CONTEXT_BASE(NAME) \
775 if (DK >= first##NAME && DK <= last##NAME) \
776 return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
777#include "clang/AST/DeclNodes.inc"
David Blaikie83d382b2011-09-23 05:06:16 +0000778 llvm_unreachable("a decl that inherits DeclContext isn't handled");
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000779 }
Argyrios Kyrtzidis3768ad62008-10-12 16:14:48 +0000780}
781
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000782SourceLocation Decl::getBodyRBrace() const {
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +0000783 // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
784 // FunctionDecl stores EndRangeLoc for this purpose.
785 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
786 const FunctionDecl *Definition;
787 if (FD->hasBody(Definition))
788 return Definition->getSourceRange().getEnd();
789 return SourceLocation();
790 }
791
Argyrios Kyrtzidis6fbc8fa2010-07-07 11:31:27 +0000792 if (Stmt *Body = getBody())
793 return Body->getSourceRange().getEnd();
794
795 return SourceLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +0000796}
797
Alp Tokerc1086762013-12-07 13:51:35 +0000798bool Decl::AccessDeclContextSanity() const {
Douglas Gregor4b00d3b2010-12-02 00:22:25 +0000799#ifndef NDEBUG
John McCall401982f2010-01-20 21:53:11 +0000800 // Suppress this check if any of the following hold:
801 // 1. this is the translation unit (and thus has no parent)
802 // 2. this is a template parameter (and thus doesn't belong to its context)
Argyrios Kyrtzidise1778632010-09-08 21:58:42 +0000803 // 3. this is a non-type template parameter
804 // 4. the context is not a record
805 // 5. it's invalid
806 // 6. it's a C++0x static_assert.
Anders Carlssonadf36b22009-08-29 20:47:47 +0000807 if (isa<TranslationUnitDecl>(this) ||
Argyrios Kyrtzidisa45855f2010-07-02 11:55:44 +0000808 isa<TemplateTypeParmDecl>(this) ||
Argyrios Kyrtzidise1778632010-09-08 21:58:42 +0000809 isa<NonTypeTemplateParmDecl>(this) ||
Douglas Gregor2b76dd92010-02-22 17:53:38 +0000810 !isa<CXXRecordDecl>(getDeclContext()) ||
Argyrios Kyrtzidis260b4a82010-09-08 21:32:35 +0000811 isInvalidDecl() ||
812 isa<StaticAssertDecl>(this) ||
813 // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
814 // as DeclContext (?).
Argyrios Kyrtzidise1778632010-09-08 21:58:42 +0000815 isa<ParmVarDecl>(this) ||
816 // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
817 // AS_none as access specifier.
Francois Pichet09af8c32011-08-17 01:06:54 +0000818 isa<CXXRecordDecl>(this) ||
819 isa<ClassScopeFunctionSpecializationDecl>(this))
Alp Tokerc1086762013-12-07 13:51:35 +0000820 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000821
822 assert(Access != AS_none &&
Anders Carlssona28908d2009-03-25 23:38:06 +0000823 "Access specifier is AS_none inside a record decl");
Douglas Gregor4b00d3b2010-12-02 00:22:25 +0000824#endif
Alp Tokerc1086762013-12-07 13:51:35 +0000825 return true;
Anders Carlssona28908d2009-03-25 23:38:06 +0000826}
827
John McCalldec348f72013-05-03 07:33:41 +0000828static Decl::Kind getKind(const Decl *D) { return D->getKind(); }
829static Decl::Kind getKind(const DeclContext *DC) { return DC->getDeclKind(); }
830
Aaron Ballman12b9f652014-01-16 13:55:42 +0000831const FunctionType *Decl::getFunctionType(bool BlocksToo) const {
832 QualType Ty;
833 if (const ValueDecl *D = dyn_cast<ValueDecl>(this))
834 Ty = D->getType();
835 else if (const TypedefNameDecl *D = dyn_cast<TypedefNameDecl>(this))
836 Ty = D->getUnderlyingType();
837 else
Craig Topper36250ad2014-05-12 05:36:57 +0000838 return nullptr;
Aaron Ballman12b9f652014-01-16 13:55:42 +0000839
840 if (Ty->isFunctionPointerType())
841 Ty = Ty->getAs<PointerType>()->getPointeeType();
842 else if (BlocksToo && Ty->isBlockPointerType())
843 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
844
845 return Ty->getAs<FunctionType>();
846}
847
848
John McCalldec348f72013-05-03 07:33:41 +0000849/// Starting at a given context (a Decl or DeclContext), look for a
850/// code context that is not a closure (a lambda, block, etc.).
851template <class T> static Decl *getNonClosureContext(T *D) {
852 if (getKind(D) == Decl::CXXMethod) {
853 CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
John McCall55c0cee2013-05-03 17:11:14 +0000854 if (MD->getOverloadedOperator() == OO_Call &&
855 MD->getParent()->isLambda())
John McCalldec348f72013-05-03 07:33:41 +0000856 return getNonClosureContext(MD->getParent()->getParent());
857 return MD;
858 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
859 return FD;
860 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
861 return MD;
862 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
863 return getNonClosureContext(BD->getParent());
864 } else if (CapturedDecl *CD = dyn_cast<CapturedDecl>(D)) {
865 return getNonClosureContext(CD->getParent());
866 } else {
Craig Topper36250ad2014-05-12 05:36:57 +0000867 return nullptr;
John McCalldec348f72013-05-03 07:33:41 +0000868 }
John McCallfe96e0b2011-11-06 09:01:30 +0000869}
870
John McCalldec348f72013-05-03 07:33:41 +0000871Decl *Decl::getNonClosureContext() {
872 return ::getNonClosureContext(this);
873}
John McCallb67608f2011-02-22 22:25:23 +0000874
John McCalldec348f72013-05-03 07:33:41 +0000875Decl *DeclContext::getNonClosureAncestor() {
876 return ::getNonClosureContext(this);
John McCallb67608f2011-02-22 22:25:23 +0000877}
Anders Carlssona28908d2009-03-25 23:38:06 +0000878
Eli Friedman7dbab8a2008-06-07 16:52:53 +0000879//===----------------------------------------------------------------------===//
880// DeclContext Implementation
881//===----------------------------------------------------------------------===//
882
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000883bool DeclContext::classof(const Decl *D) {
884 switch (D->getKind()) {
Alexis Hunted053252010-05-30 07:21:58 +0000885#define DECL(NAME, BASE)
886#define DECL_CONTEXT(NAME) case Decl::NAME:
887#define DECL_CONTEXT_BASE(NAME)
888#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000889 return true;
890 default:
Alexis Hunted053252010-05-30 07:21:58 +0000891#define DECL(NAME, BASE)
892#define DECL_CONTEXT_BASE(NAME) \
893 if (D->getKind() >= Decl::first##NAME && \
894 D->getKind() <= Decl::last##NAME) \
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000895 return true;
Alexis Hunted053252010-05-30 07:21:58 +0000896#include "clang/AST/DeclNodes.inc"
Argyrios Kyrtzidisafe24c82009-02-16 14:29:28 +0000897 return false;
898 }
899}
900
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000901DeclContext::~DeclContext() { }
Douglas Gregor91f84212008-12-11 16:49:14 +0000902
Douglas Gregor7f737c02009-09-10 16:57:35 +0000903/// \brief Find the parent context of this context that will be
904/// used for unqualified name lookup.
905///
906/// Generally, the parent lookup context is the semantic context. However, for
907/// a friend function the parent lookup context is the lexical context, which
908/// is the class in which the friend is declared.
909DeclContext *DeclContext::getLookupParent() {
910 // FIXME: Find a better way to identify friends
911 if (isa<FunctionDecl>(this))
Sebastian Redl50c68252010-08-31 00:36:30 +0000912 if (getParent()->getRedeclContext()->isFileContext() &&
913 getLexicalParent()->getRedeclContext()->isRecord())
Douglas Gregor7f737c02009-09-10 16:57:35 +0000914 return getLexicalParent();
915
916 return getParent();
917}
918
Sebastian Redlbd595762010-08-31 20:53:31 +0000919bool DeclContext::isInlineNamespace() const {
920 return isNamespace() &&
921 cast<NamespaceDecl>(this)->isInline();
922}
923
Richard Trieuc771d5d2014-05-28 02:16:01 +0000924bool DeclContext::isStdNamespace() const {
925 if (!isNamespace())
926 return false;
927
928 const NamespaceDecl *ND = cast<NamespaceDecl>(this);
929 if (ND->isInline()) {
930 return ND->getParent()->isStdNamespace();
931 }
932
933 if (!getParent()->getRedeclContext()->isTranslationUnit())
934 return false;
935
936 const IdentifierInfo *II = ND->getIdentifier();
937 return II && II->isStr("std");
938}
939
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000940bool DeclContext::isDependentContext() const {
941 if (isFileContext())
942 return false;
943
Douglas Gregor2373c592009-05-31 09:31:02 +0000944 if (isa<ClassTemplatePartialSpecializationDecl>(this))
945 return true;
946
Douglas Gregor680e9e02012-02-21 19:11:17 +0000947 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this)) {
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000948 if (Record->getDescribedClassTemplate())
949 return true;
Douglas Gregor680e9e02012-02-21 19:11:17 +0000950
951 if (Record->isDependentLambda())
952 return true;
953 }
954
John McCallc62bb642010-03-24 05:22:00 +0000955 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000956 if (Function->getDescribedFunctionTemplate())
957 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000958
John McCallc62bb642010-03-24 05:22:00 +0000959 // Friend function declarations are dependent if their *lexical*
960 // context is dependent.
961 if (cast<Decl>(this)->getFriendObjectKind())
962 return getLexicalParent()->isDependentContext();
963 }
964
Richard Smith2b560572015-02-07 03:11:11 +0000965 // FIXME: A variable template is a dependent context, but is not a
966 // DeclContext. A context within it (such as a lambda-expression)
967 // should be considered dependent.
968
Douglas Gregor9e927ab2009-05-28 16:34:51 +0000969 return getParent() && getParent()->isDependentContext();
970}
971
Douglas Gregor07665a62009-01-05 19:45:36 +0000972bool DeclContext::isTransparentContext() const {
973 if (DeclKind == Decl::Enum)
Douglas Gregor0bf31402010-10-08 23:50:27 +0000974 return !cast<EnumDecl>(this)->isScoped();
Richard Smith8df390f2016-09-08 23:14:54 +0000975 else if (DeclKind == Decl::LinkageSpec || DeclKind == Decl::Export)
Douglas Gregor07665a62009-01-05 19:45:36 +0000976 return true;
Douglas Gregor07665a62009-01-05 19:45:36 +0000977
978 return false;
979}
980
Serge Pavlov3cb80222013-11-14 02:13:03 +0000981static bool isLinkageSpecContext(const DeclContext *DC,
982 LinkageSpecDecl::LanguageIDs ID) {
983 while (DC->getDeclKind() != Decl::TranslationUnit) {
984 if (DC->getDeclKind() == Decl::LinkageSpec)
985 return cast<LinkageSpecDecl>(DC)->getLanguage() == ID;
Richard Smithac9c9a02014-04-14 20:23:58 +0000986 DC = DC->getLexicalParent();
Serge Pavlov3cb80222013-11-14 02:13:03 +0000987 }
988 return false;
989}
990
991bool DeclContext::isExternCContext() const {
992 return isLinkageSpecContext(this, clang::LinkageSpecDecl::lang_c);
993}
994
Alex Lorenz560ae562016-11-02 15:46:34 +0000995const LinkageSpecDecl *DeclContext::getExternCContext() const {
996 const DeclContext *DC = this;
997 while (DC->getDeclKind() != Decl::TranslationUnit) {
998 if (DC->getDeclKind() == Decl::LinkageSpec &&
999 cast<LinkageSpecDecl>(DC)->getLanguage() ==
1000 clang::LinkageSpecDecl::lang_c)
1001 return cast<LinkageSpecDecl>(DC);
1002 DC = DC->getLexicalParent();
1003 }
1004 return nullptr;
1005}
1006
Serge Pavlov3cb80222013-11-14 02:13:03 +00001007bool DeclContext::isExternCXXContext() const {
1008 return isLinkageSpecContext(this, clang::LinkageSpecDecl::lang_cxx);
1009}
1010
Sebastian Redl50c68252010-08-31 00:36:30 +00001011bool DeclContext::Encloses(const DeclContext *DC) const {
Douglas Gregore985a3b2009-08-27 06:03:53 +00001012 if (getPrimaryContext() != this)
1013 return getPrimaryContext()->Encloses(DC);
Mike Stump11289f42009-09-09 15:08:12 +00001014
Douglas Gregore985a3b2009-08-27 06:03:53 +00001015 for (; DC; DC = DC->getParent())
1016 if (DC->getPrimaryContext() == this)
1017 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001018 return false;
Douglas Gregore985a3b2009-08-27 06:03:53 +00001019}
1020
Steve Naroff35c62ae2009-01-08 17:28:14 +00001021DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor91f84212008-12-11 16:49:14 +00001022 switch (DeclKind) {
Douglas Gregor91f84212008-12-11 16:49:14 +00001023 case Decl::TranslationUnit:
Richard Smithf19e1272015-03-07 00:04:49 +00001024 case Decl::ExternCContext:
Douglas Gregor07665a62009-01-05 19:45:36 +00001025 case Decl::LinkageSpec:
Richard Smith8df390f2016-09-08 23:14:54 +00001026 case Decl::Export:
Mike Stump11289f42009-09-09 15:08:12 +00001027 case Decl::Block:
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +00001028 case Decl::Captured:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00001029 case Decl::OMPDeclareReduction:
Douglas Gregor91f84212008-12-11 16:49:14 +00001030 // There is only one DeclContext for these entities.
1031 return this;
1032
1033 case Decl::Namespace:
1034 // The original namespace is our primary context.
1035 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
1036
Douglas Gregor91f84212008-12-11 16:49:14 +00001037 case Decl::ObjCMethod:
1038 return this;
1039
1040 case Decl::ObjCInterface:
Douglas Gregor66b310c2011-12-15 18:03:09 +00001041 if (ObjCInterfaceDecl *Def = cast<ObjCInterfaceDecl>(this)->getDefinition())
1042 return Def;
1043
1044 return this;
1045
Steve Naroff35c62ae2009-01-08 17:28:14 +00001046 case Decl::ObjCProtocol:
Douglas Gregora715bff2012-01-01 19:51:50 +00001047 if (ObjCProtocolDecl *Def = cast<ObjCProtocolDecl>(this)->getDefinition())
1048 return Def;
1049
1050 return this;
Douglas Gregor66b310c2011-12-15 18:03:09 +00001051
Steve Naroff35c62ae2009-01-08 17:28:14 +00001052 case Decl::ObjCCategory:
Douglas Gregor91f84212008-12-11 16:49:14 +00001053 return this;
1054
Steve Naroff35c62ae2009-01-08 17:28:14 +00001055 case Decl::ObjCImplementation:
1056 case Decl::ObjCCategoryImpl:
1057 return this;
1058
Douglas Gregor91f84212008-12-11 16:49:14 +00001059 default:
Alexis Hunted053252010-05-30 07:21:58 +00001060 if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
Douglas Gregor67a65642009-02-17 23:15:12 +00001061 // If this is a tag type that has a definition or is currently
1062 // being defined, that definition is our primary context.
John McCalle78aac42010-03-10 03:28:59 +00001063 TagDecl *Tag = cast<TagDecl>(this);
John McCalle78aac42010-03-10 03:28:59 +00001064
1065 if (TagDecl *Def = Tag->getDefinition())
1066 return Def;
1067
Richard Smith5b21db82014-04-23 18:20:42 +00001068 if (const TagType *TagTy = dyn_cast<TagType>(Tag->getTypeForDecl())) {
1069 // Note, TagType::getDecl returns the (partial) definition one exists.
1070 TagDecl *PossiblePartialDef = TagTy->getDecl();
1071 if (PossiblePartialDef->isBeingDefined())
1072 return PossiblePartialDef;
1073 } else {
1074 assert(isa<InjectedClassNameType>(Tag->getTypeForDecl()));
John McCalle78aac42010-03-10 03:28:59 +00001075 }
1076
1077 return Tag;
Douglas Gregor67a65642009-02-17 23:15:12 +00001078 }
1079
Alexis Hunted053252010-05-30 07:21:58 +00001080 assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
Douglas Gregor91f84212008-12-11 16:49:14 +00001081 "Unknown DeclContext kind");
1082 return this;
1083 }
1084}
1085
Douglas Gregore57e7522012-01-07 09:11:48 +00001086void
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001087DeclContext::collectAllContexts(SmallVectorImpl<DeclContext *> &Contexts){
Douglas Gregore57e7522012-01-07 09:11:48 +00001088 Contexts.clear();
1089
1090 if (DeclKind != Decl::Namespace) {
1091 Contexts.push_back(this);
1092 return;
Douglas Gregor91f84212008-12-11 16:49:14 +00001093 }
Douglas Gregore57e7522012-01-07 09:11:48 +00001094
1095 NamespaceDecl *Self = static_cast<NamespaceDecl *>(this);
Douglas Gregorec9fd132012-01-14 16:38:05 +00001096 for (NamespaceDecl *N = Self->getMostRecentDecl(); N;
1097 N = N->getPreviousDecl())
Douglas Gregore57e7522012-01-07 09:11:48 +00001098 Contexts.push_back(N);
1099
1100 std::reverse(Contexts.begin(), Contexts.end());
Douglas Gregor91f84212008-12-11 16:49:14 +00001101}
1102
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001103std::pair<Decl *, Decl *>
Bill Wendling8eb771d2012-02-22 09:51:33 +00001104DeclContext::BuildDeclChain(ArrayRef<Decl*> Decls,
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +00001105 bool FieldsAlreadyLoaded) {
Douglas Gregor781f7132012-01-06 16:59:53 +00001106 // Build up a chain of declarations via the Decl::NextInContextAndBits field.
Craig Topper36250ad2014-05-12 05:36:57 +00001107 Decl *FirstNewDecl = nullptr;
1108 Decl *PrevDecl = nullptr;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001109 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +00001110 if (FieldsAlreadyLoaded && isa<FieldDecl>(Decls[I]))
1111 continue;
1112
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001113 Decl *D = Decls[I];
1114 if (PrevDecl)
Douglas Gregor781f7132012-01-06 16:59:53 +00001115 PrevDecl->NextInContextAndBits.setPointer(D);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001116 else
1117 FirstNewDecl = D;
1118
1119 PrevDecl = D;
1120 }
1121
1122 return std::make_pair(FirstNewDecl, PrevDecl);
1123}
1124
Richard Smith645d7552013-02-07 03:37:08 +00001125/// \brief We have just acquired external visible storage, and we already have
1126/// built a lookup map. For every name in the map, pull in the new names from
1127/// the external storage.
Richard Smitha8b74592014-03-25 00:34:21 +00001128void DeclContext::reconcileExternalVisibleStorage() const {
Richard Smith9e2341d2015-03-23 03:25:59 +00001129 assert(NeedToReconcileExternalVisibleStorage && LookupPtr);
Richard Smith645d7552013-02-07 03:37:08 +00001130 NeedToReconcileExternalVisibleStorage = false;
1131
Richard Smith9e2341d2015-03-23 03:25:59 +00001132 for (auto &Lookup : *LookupPtr)
Richard Smitha8b74592014-03-25 00:34:21 +00001133 Lookup.second.setHasExternalDecls();
Richard Smith645d7552013-02-07 03:37:08 +00001134}
1135
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001136/// \brief Load the declarations within this lexical storage from an
1137/// external source.
Richard Smith18b380b2015-03-24 02:44:20 +00001138/// \return \c true if any declarations were added.
1139bool
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001140DeclContext::LoadLexicalDeclsFromExternalStorage() const {
1141 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001142 assert(hasExternalLexicalStorage() && Source && "No external storage?");
1143
Argyrios Kyrtzidis98d045e2010-07-30 10:03:23 +00001144 // Notify that we have a DeclContext that is initializing.
1145 ExternalASTSource::Deserializing ADeclContext(Source);
Richard Smith9e2341d2015-03-23 03:25:59 +00001146
Douglas Gregor3d0adb32011-07-15 21:46:17 +00001147 // Load the external declarations, if any.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001148 SmallVector<Decl*, 64> Decls;
Richard Smith18b380b2015-03-24 02:44:20 +00001149 ExternalLexicalStorage = false;
Richard Smith3cb15722015-08-05 22:41:45 +00001150 Source->FindExternalLexicalDecls(this, Decls);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001151
1152 if (Decls.empty())
Richard Smith18b380b2015-03-24 02:44:20 +00001153 return false;
Richard Smith9e2341d2015-03-23 03:25:59 +00001154
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +00001155 // We may have already loaded just the fields of this record, in which case
1156 // we need to ignore them.
1157 bool FieldsAlreadyLoaded = false;
1158 if (const RecordDecl *RD = dyn_cast<RecordDecl>(this))
1159 FieldsAlreadyLoaded = RD->LoadedFieldsFromExternalStorage;
1160
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001161 // Splice the newly-read declarations into the beginning of the list
1162 // of declarations.
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001163 Decl *ExternalFirst, *ExternalLast;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00001164 std::tie(ExternalFirst, ExternalLast) =
1165 BuildDeclChain(Decls, FieldsAlreadyLoaded);
Douglas Gregor781f7132012-01-06 16:59:53 +00001166 ExternalLast->NextInContextAndBits.setPointer(FirstDecl);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001167 FirstDecl = ExternalFirst;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001168 if (!LastDecl)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001169 LastDecl = ExternalLast;
Richard Smith18b380b2015-03-24 02:44:20 +00001170 return true;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001171}
1172
John McCall75b960e2010-06-01 09:23:16 +00001173DeclContext::lookup_result
1174ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
1175 DeclarationName Name) {
1176 ASTContext &Context = DC->getParentASTContext();
1177 StoredDeclsMap *Map;
Richard Smith9e2341d2015-03-23 03:25:59 +00001178 if (!(Map = DC->LookupPtr))
John McCall75b960e2010-06-01 09:23:16 +00001179 Map = DC->CreateStoredDeclsMap(Context);
Richard Smitha8b74592014-03-25 00:34:21 +00001180 if (DC->NeedToReconcileExternalVisibleStorage)
1181 DC->reconcileExternalVisibleStorage();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001182
Richard Smith4abe0a82013-09-09 07:34:56 +00001183 (*Map)[Name].removeExternalDecls();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001184
John McCall75b960e2010-06-01 09:23:16 +00001185 return DeclContext::lookup_result();
1186}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001187
John McCall75b960e2010-06-01 09:23:16 +00001188DeclContext::lookup_result
1189ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +00001190 DeclarationName Name,
Argyrios Kyrtzidis94d3f9d2011-09-09 06:44:14 +00001191 ArrayRef<NamedDecl*> Decls) {
Dmitri Gribenko76bb5cabfa2012-09-10 21:20:09 +00001192 ASTContext &Context = DC->getParentASTContext();
John McCall75b960e2010-06-01 09:23:16 +00001193 StoredDeclsMap *Map;
Richard Smith9e2341d2015-03-23 03:25:59 +00001194 if (!(Map = DC->LookupPtr))
John McCall75b960e2010-06-01 09:23:16 +00001195 Map = DC->CreateStoredDeclsMap(Context);
Richard Smitha8b74592014-03-25 00:34:21 +00001196 if (DC->NeedToReconcileExternalVisibleStorage)
1197 DC->reconcileExternalVisibleStorage();
John McCall75b960e2010-06-01 09:23:16 +00001198
1199 StoredDeclsList &List = (*Map)[Name];
Richard Smith51445cd2013-06-24 01:46:41 +00001200
1201 // Clear out any old external visible declarations, to avoid quadratic
1202 // performance in the redeclaration checks below.
1203 List.removeExternalDecls();
1204
1205 if (!List.isNull()) {
1206 // We have both existing declarations and new declarations for this name.
1207 // Some of the declarations may simply replace existing ones. Handle those
1208 // first.
1209 llvm::SmallVector<unsigned, 8> Skip;
1210 for (unsigned I = 0, N = Decls.size(); I != N; ++I)
Richard Smithe8292b12015-02-10 03:28:10 +00001211 if (List.HandleRedeclaration(Decls[I], /*IsKnownNewer*/false))
Richard Smith51445cd2013-06-24 01:46:41 +00001212 Skip.push_back(I);
1213 Skip.push_back(Decls.size());
1214
1215 // Add in any new declarations.
1216 unsigned SkipPos = 0;
1217 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
1218 if (I == Skip[SkipPos])
1219 ++SkipPos;
1220 else
1221 List.AddSubsequentDecl(Decls[I]);
1222 }
1223 } else {
1224 // Convert the array to a StoredDeclsList.
1225 for (ArrayRef<NamedDecl*>::iterator
1226 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
1227 if (List.isNull())
1228 List.setOnlyValue(*I);
1229 else
1230 List.AddSubsequentDecl(*I);
1231 }
John McCall75b960e2010-06-01 09:23:16 +00001232 }
1233
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001234 return List.getLookupResult();
John McCall75b960e2010-06-01 09:23:16 +00001235}
1236
Aaron Ballmanda634f12014-03-07 22:17:20 +00001237DeclContext::decl_iterator DeclContext::decls_begin() const {
1238 if (hasExternalLexicalStorage())
1239 LoadLexicalDeclsFromExternalStorage();
1240 return decl_iterator(FirstDecl);
1241}
1242
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001243bool DeclContext::decls_empty() const {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001244 if (hasExternalLexicalStorage())
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001245 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001246
1247 return !FirstDecl;
1248}
1249
Sean Callanan0325fb82013-05-04 02:04:27 +00001250bool DeclContext::containsDecl(Decl *D) const {
1251 return (D->getLexicalDeclContext() == this &&
1252 (D->NextInContextAndBits.getPointer() || D == LastDecl));
1253}
1254
John McCall84d87672009-12-10 09:41:52 +00001255void DeclContext::removeDecl(Decl *D) {
1256 assert(D->getLexicalDeclContext() == this &&
1257 "decl being removed from non-lexical context");
Douglas Gregor781f7132012-01-06 16:59:53 +00001258 assert((D->NextInContextAndBits.getPointer() || D == LastDecl) &&
John McCall84d87672009-12-10 09:41:52 +00001259 "decl is not in decls list");
1260
1261 // Remove D from the decl chain. This is O(n) but hopefully rare.
1262 if (D == FirstDecl) {
1263 if (D == LastDecl)
Craig Topper36250ad2014-05-12 05:36:57 +00001264 FirstDecl = LastDecl = nullptr;
John McCall84d87672009-12-10 09:41:52 +00001265 else
Douglas Gregor781f7132012-01-06 16:59:53 +00001266 FirstDecl = D->NextInContextAndBits.getPointer();
John McCall84d87672009-12-10 09:41:52 +00001267 } else {
Douglas Gregor781f7132012-01-06 16:59:53 +00001268 for (Decl *I = FirstDecl; true; I = I->NextInContextAndBits.getPointer()) {
John McCall84d87672009-12-10 09:41:52 +00001269 assert(I && "decl not found in linked list");
Douglas Gregor781f7132012-01-06 16:59:53 +00001270 if (I->NextInContextAndBits.getPointer() == D) {
1271 I->NextInContextAndBits.setPointer(D->NextInContextAndBits.getPointer());
John McCall84d87672009-12-10 09:41:52 +00001272 if (D == LastDecl) LastDecl = I;
1273 break;
1274 }
1275 }
1276 }
1277
1278 // Mark that D is no longer in the decl chain.
Craig Topper36250ad2014-05-12 05:36:57 +00001279 D->NextInContextAndBits.setPointer(nullptr);
John McCall84d87672009-12-10 09:41:52 +00001280
1281 // Remove D from the lookup table if necessary.
1282 if (isa<NamedDecl>(D)) {
1283 NamedDecl *ND = cast<NamedDecl>(D);
1284
Axel Naumanncb2c52f2011-08-26 14:06:12 +00001285 // Remove only decls that have a name
1286 if (!ND->getDeclName()) return;
1287
Richard Smith26210db2015-11-13 03:52:13 +00001288 auto *DC = this;
1289 do {
1290 StoredDeclsMap *Map = DC->getPrimaryContext()->LookupPtr;
1291 if (Map) {
1292 StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
1293 assert(Pos != Map->end() && "no lookup entry for decl");
1294 if (Pos->second.getAsVector() || Pos->second.getAsDecl() == ND)
1295 Pos->second.remove(ND);
1296 }
1297 } while (DC->isTransparentContext() && (DC = DC->getParent()));
John McCall84d87672009-12-10 09:41:52 +00001298 }
1299}
1300
John McCalld1e9d832009-08-11 06:59:38 +00001301void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner33f219d2009-02-20 00:56:18 +00001302 assert(D->getLexicalDeclContext() == this &&
1303 "Decl inserted into wrong lexical context");
Mike Stump11289f42009-09-09 15:08:12 +00001304 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor020713e2009-01-09 19:42:16 +00001305 "Decl already inserted into a DeclContext");
1306
1307 if (FirstDecl) {
Douglas Gregor781f7132012-01-06 16:59:53 +00001308 LastDecl->NextInContextAndBits.setPointer(D);
Douglas Gregor020713e2009-01-09 19:42:16 +00001309 LastDecl = D;
1310 } else {
1311 FirstDecl = LastDecl = D;
1312 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +00001313
1314 // Notify a C++ record declaration that we've added a member, so it can
Nick Lewycky4b81fc872015-10-18 20:32:12 +00001315 // update its class-specific state.
Douglas Gregora1ce1f82010-09-27 22:06:20 +00001316 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
1317 Record->addedMember(D);
Douglas Gregor0f2a3602011-12-03 00:30:27 +00001318
1319 // If this is a newly-created (not de-serialized) import declaration, wire
1320 // it in to the list of local import declarations.
1321 if (!D->isFromASTFile()) {
1322 if (ImportDecl *Import = dyn_cast<ImportDecl>(D))
1323 D->getASTContext().addedLocalImportDecl(Import);
1324 }
John McCalld1e9d832009-08-11 06:59:38 +00001325}
1326
1327void DeclContext::addDecl(Decl *D) {
1328 addHiddenDecl(D);
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001329
1330 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Richard Smithf634c902012-03-16 06:12:59 +00001331 ND->getDeclContext()->getPrimaryContext()->
1332 makeDeclVisibleInContextWithFlags(ND, false, true);
Douglas Gregor91f84212008-12-11 16:49:14 +00001333}
1334
Sean Callanan95e74be2011-10-21 02:57:43 +00001335void DeclContext::addDeclInternal(Decl *D) {
1336 addHiddenDecl(D);
1337
1338 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Richard Smithf634c902012-03-16 06:12:59 +00001339 ND->getDeclContext()->getPrimaryContext()->
1340 makeDeclVisibleInContextWithFlags(ND, true, true);
1341}
1342
1343/// shouldBeHidden - Determine whether a declaration which was declared
1344/// within its semantic context should be invisible to qualified name lookup.
1345static bool shouldBeHidden(NamedDecl *D) {
1346 // Skip unnamed declarations.
1347 if (!D->getDeclName())
1348 return true;
1349
1350 // Skip entities that can't be found by name lookup into a particular
1351 // context.
1352 if ((D->getIdentifierNamespace() == 0 && !isa<UsingDirectiveDecl>(D)) ||
1353 D->isTemplateParameter())
1354 return true;
1355
1356 // Skip template specializations.
1357 // FIXME: This feels like a hack. Should DeclarationName support
1358 // template-ids, or is there a better way to keep specializations
1359 // from being visible?
1360 if (isa<ClassTemplateSpecializationDecl>(D))
1361 return true;
1362 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1363 if (FD->isFunctionTemplateSpecialization())
1364 return true;
1365
1366 return false;
1367}
1368
1369/// buildLookup - Build the lookup data structure with all of the
1370/// declarations in this DeclContext (and any other contexts linked
1371/// to it or transparent contexts nested within it) and return it.
Richard Smitha8b74592014-03-25 00:34:21 +00001372///
1373/// Note that the produced map may miss out declarations from an
1374/// external source. If it does, those entries will be marked with
1375/// the 'hasExternalDecls' flag.
Richard Smithf634c902012-03-16 06:12:59 +00001376StoredDeclsMap *DeclContext::buildLookup() {
1377 assert(this == getPrimaryContext() && "buildLookup called on non-primary DC");
1378
Richard Smith9e2341d2015-03-23 03:25:59 +00001379 if (!HasLazyLocalLexicalLookups && !HasLazyExternalLexicalLookups)
1380 return LookupPtr;
1381
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001382 SmallVector<DeclContext *, 2> Contexts;
Richard Smithf634c902012-03-16 06:12:59 +00001383 collectAllContexts(Contexts);
Richard Smith18b380b2015-03-24 02:44:20 +00001384
1385 if (HasLazyExternalLexicalLookups) {
1386 HasLazyExternalLexicalLookups = false;
1387 for (auto *DC : Contexts) {
1388 if (DC->hasExternalLexicalStorage())
1389 HasLazyLocalLexicalLookups |=
1390 DC->LoadLexicalDeclsFromExternalStorage();
1391 }
1392
1393 if (!HasLazyLocalLexicalLookups)
1394 return LookupPtr;
1395 }
1396
1397 for (auto *DC : Contexts)
1398 buildLookupImpl(DC, hasExternalVisibleStorage());
Richard Smithf634c902012-03-16 06:12:59 +00001399
1400 // We no longer have any lazy decls.
Richard Smith9e2341d2015-03-23 03:25:59 +00001401 HasLazyLocalLexicalLookups = false;
1402 return LookupPtr;
Richard Smithf634c902012-03-16 06:12:59 +00001403}
1404
1405/// buildLookupImpl - Build part of the lookup data structure for the
1406/// declarations contained within DCtx, which will either be this
1407/// DeclContext, a DeclContext linked to it, or a transparent context
1408/// nested within it.
Richard Smitha3271c12015-02-07 00:45:52 +00001409void DeclContext::buildLookupImpl(DeclContext *DCtx, bool Internal) {
Richard Smith9e2341d2015-03-23 03:25:59 +00001410 for (Decl *D : DCtx->noload_decls()) {
Richard Smithf634c902012-03-16 06:12:59 +00001411 // Insert this declaration into the lookup structure, but only if
1412 // it's semantically within its decl context. Any other decls which
1413 // should be found in this context are added eagerly.
Richard Smithcf4ab522013-06-24 07:20:36 +00001414 //
1415 // If it's from an AST file, don't add it now. It'll get handled by
1416 // FindExternalVisibleDeclsByName if needed. Exception: if we're not
1417 // in C++, we do not track external visible decls for the TU, so in
1418 // that case we need to collect them all here.
Richard Smithf634c902012-03-16 06:12:59 +00001419 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Richard Smithcf4ab522013-06-24 07:20:36 +00001420 if (ND->getDeclContext() == DCtx && !shouldBeHidden(ND) &&
1421 (!ND->isFromASTFile() ||
1422 (isTranslationUnit() &&
1423 !getParentASTContext().getLangOpts().CPlusPlus)))
Richard Smitha3271c12015-02-07 00:45:52 +00001424 makeDeclVisibleInContextImpl(ND, Internal);
Richard Smithf634c902012-03-16 06:12:59 +00001425
1426 // If this declaration is itself a transparent declaration context
1427 // or inline namespace, add the members of this declaration of that
1428 // context (recursively).
1429 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(D))
1430 if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
Richard Smith9e2341d2015-03-23 03:25:59 +00001431 buildLookupImpl(InnerCtx, Internal);
Richard Smithf634c902012-03-16 06:12:59 +00001432 }
Sean Callanan95e74be2011-10-21 02:57:43 +00001433}
1434
Richard Smith40c78062015-02-21 02:31:57 +00001435NamedDecl *const DeclContextLookupResult::SingleElementDummyList = nullptr;
1436
Mike Stump11289f42009-09-09 15:08:12 +00001437DeclContext::lookup_result
Richard Smith40c78062015-02-21 02:31:57 +00001438DeclContext::lookup(DeclarationName Name) const {
Richard Smith8df390f2016-09-08 23:14:54 +00001439 assert(DeclKind != Decl::LinkageSpec && DeclKind != Decl::Export &&
1440 "should not perform lookups into transparent contexts");
Nick Lewycky2bd636f2012-03-13 04:12:34 +00001441
Manman Ren7d2f5c42016-09-09 19:03:07 +00001442 const DeclContext *PrimaryContext = getPrimaryContext();
1443 if (PrimaryContext != this)
1444 return PrimaryContext->lookup(Name);
1445
Richard Smith8cebe372015-02-25 22:20:13 +00001446 // If we have an external source, ensure that any later redeclarations of this
1447 // context have been loaded, since they may add names to the result of this
1448 // lookup (or add external visible storage).
1449 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1450 if (Source)
1451 (void)cast<Decl>(this)->getMostRecentDecl();
Richard Smithbb853c72014-08-13 01:23:33 +00001452
Richard Smith05afe5e2012-03-13 03:12:56 +00001453 if (hasExternalVisibleStorage()) {
Richard Smith8cebe372015-02-25 22:20:13 +00001454 assert(Source && "external visible storage but no external source?");
1455
Richard Smitha8b74592014-03-25 00:34:21 +00001456 if (NeedToReconcileExternalVisibleStorage)
1457 reconcileExternalVisibleStorage();
1458
Richard Smith9e2341d2015-03-23 03:25:59 +00001459 StoredDeclsMap *Map = LookupPtr;
Richard Smitha8b74592014-03-25 00:34:21 +00001460
Richard Smith9e2341d2015-03-23 03:25:59 +00001461 if (HasLazyLocalLexicalLookups || HasLazyExternalLexicalLookups)
Richard Smith40c78062015-02-21 02:31:57 +00001462 // FIXME: Make buildLookup const?
1463 Map = const_cast<DeclContext*>(this)->buildLookup();
Richard Smith645d7552013-02-07 03:37:08 +00001464
Richard Smith75fc3bf2013-02-08 00:37:45 +00001465 if (!Map)
1466 Map = CreateStoredDeclsMap(getParentASTContext());
1467
Richard Smith4abe0a82013-09-09 07:34:56 +00001468 // If we have a lookup result with no external decls, we are done.
Richard Smith75fc3bf2013-02-08 00:37:45 +00001469 std::pair<StoredDeclsMap::iterator, bool> R =
1470 Map->insert(std::make_pair(Name, StoredDeclsList()));
Richard Smith4abe0a82013-09-09 07:34:56 +00001471 if (!R.second && !R.first->second.hasExternalDecls())
Richard Smith75fc3bf2013-02-08 00:37:45 +00001472 return R.first->second.getLookupResult();
Richard Smithf634c902012-03-16 06:12:59 +00001473
Richard Smith309271b2014-03-04 00:21:14 +00001474 if (Source->FindExternalVisibleDeclsByName(this, Name) || !R.second) {
Richard Smith9e2341d2015-03-23 03:25:59 +00001475 if (StoredDeclsMap *Map = LookupPtr) {
Richard Smith9ce12e32013-02-07 03:30:24 +00001476 StoredDeclsMap::iterator I = Map->find(Name);
1477 if (I != Map->end())
1478 return I->second.getLookupResult();
1479 }
1480 }
1481
Richard Smith40c78062015-02-21 02:31:57 +00001482 return lookup_result();
John McCall75b960e2010-06-01 09:23:16 +00001483 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001484
Richard Smith9e2341d2015-03-23 03:25:59 +00001485 StoredDeclsMap *Map = LookupPtr;
1486 if (HasLazyLocalLexicalLookups || HasLazyExternalLexicalLookups)
Richard Smith40c78062015-02-21 02:31:57 +00001487 Map = const_cast<DeclContext*>(this)->buildLookup();
Richard Smithf634c902012-03-16 06:12:59 +00001488
1489 if (!Map)
Richard Smith40c78062015-02-21 02:31:57 +00001490 return lookup_result();
Richard Smithf634c902012-03-16 06:12:59 +00001491
1492 StoredDeclsMap::iterator I = Map->find(Name);
1493 if (I == Map->end())
Richard Smith40c78062015-02-21 02:31:57 +00001494 return lookup_result();
Richard Smithf634c902012-03-16 06:12:59 +00001495
1496 return I->second.getLookupResult();
Douglas Gregor91f84212008-12-11 16:49:14 +00001497}
1498
Richard Smith95d99302013-07-13 02:00:19 +00001499DeclContext::lookup_result
1500DeclContext::noload_lookup(DeclarationName Name) {
Richard Smith8df390f2016-09-08 23:14:54 +00001501 assert(DeclKind != Decl::LinkageSpec && DeclKind != Decl::Export &&
1502 "should not perform lookups into transparent contexts");
Richard Smith95d99302013-07-13 02:00:19 +00001503
1504 DeclContext *PrimaryContext = getPrimaryContext();
1505 if (PrimaryContext != this)
1506 return PrimaryContext->noload_lookup(Name);
1507
Richard Smith9e2341d2015-03-23 03:25:59 +00001508 // If we have any lazy lexical declarations not in our lookup map, add them
1509 // now. Don't import any external declarations, not even if we know we have
1510 // some missing from the external visible lookups.
1511 if (HasLazyLocalLexicalLookups) {
Vince Harrona3ea9a42015-03-22 05:59:59 +00001512 SmallVector<DeclContext *, 2> Contexts;
1513 collectAllContexts(Contexts);
1514 for (unsigned I = 0, N = Contexts.size(); I != N; ++I)
Richard Smith9e2341d2015-03-23 03:25:59 +00001515 buildLookupImpl(Contexts[I], hasExternalVisibleStorage());
1516 HasLazyLocalLexicalLookups = false;
Vince Harrona3ea9a42015-03-22 05:59:59 +00001517 }
1518
Richard Smith9e2341d2015-03-23 03:25:59 +00001519 StoredDeclsMap *Map = LookupPtr;
Richard Smith95d99302013-07-13 02:00:19 +00001520 if (!Map)
Richard Smith40c78062015-02-21 02:31:57 +00001521 return lookup_result();
Richard Smith95d99302013-07-13 02:00:19 +00001522
1523 StoredDeclsMap::iterator I = Map->find(Name);
Craig Topper36250ad2014-05-12 05:36:57 +00001524 return I != Map->end() ? I->second.getLookupResult()
Richard Smith40c78062015-02-21 02:31:57 +00001525 : lookup_result();
Richard Smith95d99302013-07-13 02:00:19 +00001526}
1527
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001528void DeclContext::localUncachedLookup(DeclarationName Name,
1529 SmallVectorImpl<NamedDecl *> &Results) {
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001530 Results.clear();
1531
1532 // If there's no external storage, just perform a normal lookup and copy
1533 // the results.
Douglas Gregordd6006f2012-07-17 21:16:27 +00001534 if (!hasExternalVisibleStorage() && !hasExternalLexicalStorage() && Name) {
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001535 lookup_result LookupResults = lookup(Name);
David Blaikieff7d47a2012-12-19 00:45:41 +00001536 Results.insert(Results.end(), LookupResults.begin(), LookupResults.end());
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001537 return;
1538 }
1539
1540 // If we have a lookup table, check there first. Maybe we'll get lucky.
Richard Smith9e2341d2015-03-23 03:25:59 +00001541 // FIXME: Should we be checking these flags on the primary context?
1542 if (Name && !HasLazyLocalLexicalLookups && !HasLazyExternalLexicalLookups) {
1543 if (StoredDeclsMap *Map = LookupPtr) {
Douglas Gregordd6006f2012-07-17 21:16:27 +00001544 StoredDeclsMap::iterator Pos = Map->find(Name);
1545 if (Pos != Map->end()) {
1546 Results.insert(Results.end(),
David Blaikieff7d47a2012-12-19 00:45:41 +00001547 Pos->second.getLookupResult().begin(),
1548 Pos->second.getLookupResult().end());
Douglas Gregordd6006f2012-07-17 21:16:27 +00001549 return;
1550 }
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001551 }
1552 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00001553
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001554 // Slow case: grovel through the declarations in our chain looking for
1555 // matches.
Richard Smith9e2341d2015-03-23 03:25:59 +00001556 // FIXME: If we have lazy external declarations, this will not find them!
1557 // FIXME: Should we CollectAllContexts and walk them all here?
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00001558 for (Decl *D = FirstDecl; D; D = D->getNextDeclInContext()) {
1559 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1560 if (ND->getDeclName() == Name)
1561 Results.push_back(ND);
1562 }
1563}
1564
Sebastian Redl50c68252010-08-31 00:36:30 +00001565DeclContext *DeclContext::getRedeclContext() {
Chris Lattner17a1bfa2009-03-27 19:19:59 +00001566 DeclContext *Ctx = this;
Sebastian Redlbd595762010-08-31 20:53:31 +00001567 // Skip through transparent contexts.
1568 while (Ctx->isTransparentContext())
Douglas Gregor6ad0ef52009-01-06 23:51:29 +00001569 Ctx = Ctx->getParent();
1570 return Ctx;
1571}
1572
Douglas Gregorf47b9112009-02-25 22:02:03 +00001573DeclContext *DeclContext::getEnclosingNamespaceContext() {
1574 DeclContext *Ctx = this;
1575 // Skip through non-namespace, non-translation-unit contexts.
Sebastian Redl4f08c962010-08-31 00:36:23 +00001576 while (!Ctx->isFileContext())
Douglas Gregorf47b9112009-02-25 22:02:03 +00001577 Ctx = Ctx->getParent();
1578 return Ctx->getPrimaryContext();
1579}
1580
Reid Klecknerd60b82f2014-11-17 23:36:45 +00001581RecordDecl *DeclContext::getOuterLexicalRecordContext() {
1582 // Loop until we find a non-record context.
1583 RecordDecl *OutermostRD = nullptr;
1584 DeclContext *DC = this;
1585 while (DC->isRecord()) {
1586 OutermostRD = cast<RecordDecl>(DC);
1587 DC = DC->getLexicalParent();
1588 }
1589 return OutermostRD;
1590}
1591
Sebastian Redl50c68252010-08-31 00:36:30 +00001592bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
1593 // For non-file contexts, this is equivalent to Equals.
1594 if (!isFileContext())
1595 return O->Equals(this);
1596
1597 do {
1598 if (O->Equals(this))
1599 return true;
1600
1601 const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O);
1602 if (!NS || !NS->isInline())
1603 break;
1604 O = NS->getParent();
1605 } while (O);
1606
1607 return false;
1608}
1609
Richard Smithf634c902012-03-16 06:12:59 +00001610void DeclContext::makeDeclVisibleInContext(NamedDecl *D) {
1611 DeclContext *PrimaryDC = this->getPrimaryContext();
1612 DeclContext *DeclDC = D->getDeclContext()->getPrimaryContext();
1613 // If the decl is being added outside of its semantic decl context, we
1614 // need to ensure that we eagerly build the lookup information for it.
1615 PrimaryDC->makeDeclVisibleInContextWithFlags(D, false, PrimaryDC == DeclDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00001616}
1617
Richard Smithf634c902012-03-16 06:12:59 +00001618void DeclContext::makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal,
1619 bool Recoverable) {
1620 assert(this == getPrimaryContext() && "expected a primary DC");
Sean Callanan95e74be2011-10-21 02:57:43 +00001621
Vassil Vassilev71eafde2016-04-06 20:56:03 +00001622 if (!isLookupContext()) {
1623 if (isTransparentContext())
1624 getParent()->getPrimaryContext()
1625 ->makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
Richard Smith05afe5e2012-03-13 03:12:56 +00001626 return;
Vassil Vassilev71eafde2016-04-06 20:56:03 +00001627 }
Richard Smith05afe5e2012-03-13 03:12:56 +00001628
Richard Smithf634c902012-03-16 06:12:59 +00001629 // Skip declarations which should be invisible to name lookup.
1630 if (shouldBeHidden(D))
1631 return;
1632
1633 // If we already have a lookup data structure, perform the insertion into
1634 // it. If we might have externally-stored decls with this name, look them
1635 // up and perform the insertion. If this decl was declared outside its
1636 // semantic context, buildLookup won't add it, so add it now.
1637 //
1638 // FIXME: As a performance hack, don't add such decls into the translation
1639 // unit unless we're in C++, since qualified lookup into the TU is never
1640 // performed.
Richard Smith9e2341d2015-03-23 03:25:59 +00001641 if (LookupPtr || hasExternalVisibleStorage() ||
Richard Smithf634c902012-03-16 06:12:59 +00001642 ((!Recoverable || D->getDeclContext() != D->getLexicalDeclContext()) &&
1643 (getParentASTContext().getLangOpts().CPlusPlus ||
1644 !isTranslationUnit()))) {
1645 // If we have lazily omitted any decls, they might have the same name as
1646 // the decl which we are adding, so build a full lookup table before adding
1647 // this decl.
1648 buildLookup();
1649 makeDeclVisibleInContextImpl(D, Internal);
1650 } else {
Richard Smith9e2341d2015-03-23 03:25:59 +00001651 HasLazyLocalLexicalLookups = true;
Richard Smithf634c902012-03-16 06:12:59 +00001652 }
1653
1654 // If we are a transparent context or inline namespace, insert into our
1655 // parent context, too. This operation is recursive.
1656 if (isTransparentContext() || isInlineNamespace())
1657 getParent()->getPrimaryContext()->
1658 makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
1659
1660 Decl *DCAsDecl = cast<Decl>(this);
1661 // Notify that a decl was made visible unless we are a Tag being defined.
1662 if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined()))
1663 if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
1664 L->AddedVisibleDecl(this, D);
1665}
1666
1667void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal) {
1668 // Find or create the stored declaration map.
Richard Smith9e2341d2015-03-23 03:25:59 +00001669 StoredDeclsMap *Map = LookupPtr;
Richard Smithf634c902012-03-16 06:12:59 +00001670 if (!Map) {
1671 ASTContext *C = &getParentASTContext();
1672 Map = CreateStoredDeclsMap(*C);
Argyrios Kyrtzidise51e5542010-07-04 21:44:25 +00001673 }
1674
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001675 // If there is an external AST source, load any declarations it knows about
1676 // with this declaration's name.
1677 // If the lookup table contains an entry about this name it means that we
1678 // have already checked the external source.
Sean Callanan95e74be2011-10-21 02:57:43 +00001679 if (!Internal)
1680 if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
1681 if (hasExternalVisibleStorage() &&
Richard Smithf634c902012-03-16 06:12:59 +00001682 Map->find(D->getDeclName()) == Map->end())
Sean Callanan95e74be2011-10-21 02:57:43 +00001683 Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00001684
Douglas Gregor91f84212008-12-11 16:49:14 +00001685 // Insert this declaration into the map.
Richard Smithf634c902012-03-16 06:12:59 +00001686 StoredDeclsList &DeclNameEntries = (*Map)[D->getDeclName()];
Richard Smith4abe0a82013-09-09 07:34:56 +00001687
1688 if (Internal) {
1689 // If this is being added as part of loading an external declaration,
1690 // this may not be the only external declaration with this name.
1691 // In this case, we never try to replace an existing declaration; we'll
1692 // handle that when we finalize the list of declarations for this name.
1693 DeclNameEntries.setHasExternalDecls();
1694 DeclNameEntries.AddSubsequentDecl(D);
1695 return;
1696 }
1697
Richard Smitha3271c12015-02-07 00:45:52 +00001698 if (DeclNameEntries.isNull()) {
Chris Lattnercaae7162009-02-20 01:44:05 +00001699 DeclNameEntries.setOnlyValue(D);
Richard Smithf634c902012-03-16 06:12:59 +00001700 return;
Douglas Gregor91f84212008-12-11 16:49:14 +00001701 }
Chris Lattner24e24d52009-02-20 00:55:03 +00001702
Richard Smithe8292b12015-02-10 03:28:10 +00001703 if (DeclNameEntries.HandleRedeclaration(D, /*IsKnownNewer*/!Internal)) {
Richard Smithf634c902012-03-16 06:12:59 +00001704 // This declaration has replaced an existing one for which
1705 // declarationReplaces returns true.
1706 return;
1707 }
Mike Stump11289f42009-09-09 15:08:12 +00001708
Richard Smithf634c902012-03-16 06:12:59 +00001709 // Put this declaration into the appropriate slot.
1710 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor91f84212008-12-11 16:49:14 +00001711}
Douglas Gregor889ceb72009-02-03 19:21:40 +00001712
Richard Smith40c78062015-02-21 02:31:57 +00001713UsingDirectiveDecl *DeclContext::udir_iterator::operator*() const {
1714 return cast<UsingDirectiveDecl>(*I);
1715}
1716
Douglas Gregor889ceb72009-02-03 19:21:40 +00001717/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
1718/// this context.
Aaron Ballman804a7fb2014-03-17 17:14:12 +00001719DeclContext::udir_range DeclContext::using_directives() const {
Richard Smith05afe5e2012-03-13 03:12:56 +00001720 // FIXME: Use something more efficient than normal lookup for using
1721 // directives. In C++, using directives are looked up more than anything else.
Richard Smithcf4bdde2015-02-21 02:45:19 +00001722 lookup_result Result = lookup(UsingDirectiveDecl::getName());
Richard Smith40c78062015-02-21 02:31:57 +00001723 return udir_range(Result.begin(), Result.end());
Douglas Gregor889ceb72009-02-03 19:21:40 +00001724}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001725
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001726//===----------------------------------------------------------------------===//
1727// Creation and Destruction of StoredDeclsMaps. //
1728//===----------------------------------------------------------------------===//
1729
John McCallc62bb642010-03-24 05:22:00 +00001730StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
Richard Smith9e2341d2015-03-23 03:25:59 +00001731 assert(!LookupPtr && "context already has a decls map");
John McCallc62bb642010-03-24 05:22:00 +00001732 assert(getPrimaryContext() == this &&
1733 "creating decls map on non-primary context");
1734
1735 StoredDeclsMap *M;
1736 bool Dependent = isDependentContext();
1737 if (Dependent)
1738 M = new DependentStoredDeclsMap();
1739 else
1740 M = new StoredDeclsMap();
1741 M->Previous = C.LastSDM;
1742 C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
Richard Smith9e2341d2015-03-23 03:25:59 +00001743 LookupPtr = M;
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001744 return M;
1745}
1746
1747void ASTContext::ReleaseDeclContextMaps() {
John McCallc62bb642010-03-24 05:22:00 +00001748 // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1749 // pointer because the subclass doesn't add anything that needs to
1750 // be deleted.
John McCallc62bb642010-03-24 05:22:00 +00001751 StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1752}
1753
1754void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1755 while (Map) {
1756 // Advance the iteration before we invalidate memory.
1757 llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1758
1759 if (Dependent)
1760 delete static_cast<DependentStoredDeclsMap*>(Map);
1761 else
1762 delete Map;
1763
1764 Map = Next.getPointer();
1765 Dependent = Next.getInt();
1766 }
1767}
1768
John McCallc62bb642010-03-24 05:22:00 +00001769DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1770 DeclContext *Parent,
1771 const PartialDiagnostic &PDiag) {
1772 assert(Parent->isDependentContext()
1773 && "cannot iterate dependent diagnostics of non-dependent context");
1774 Parent = Parent->getPrimaryContext();
Richard Smith9e2341d2015-03-23 03:25:59 +00001775 if (!Parent->LookupPtr)
John McCallc62bb642010-03-24 05:22:00 +00001776 Parent->CreateStoredDeclsMap(C);
1777
Richard Smith9e2341d2015-03-23 03:25:59 +00001778 DependentStoredDeclsMap *Map =
1779 static_cast<DependentStoredDeclsMap *>(Parent->LookupPtr);
John McCallc62bb642010-03-24 05:22:00 +00001780
Douglas Gregora55530e2010-03-29 23:56:53 +00001781 // Allocate the copy of the PartialDiagnostic via the ASTContext's
Douglas Gregor89336232010-03-29 23:34:08 +00001782 // BumpPtrAllocator, rather than the ASTContext itself.
Craig Topper36250ad2014-05-12 05:36:57 +00001783 PartialDiagnostic::Storage *DiagStorage = nullptr;
Douglas Gregora55530e2010-03-29 23:56:53 +00001784 if (PDiag.hasStorage())
1785 DiagStorage = new (C) PartialDiagnostic::Storage;
1786
1787 DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
John McCallc62bb642010-03-24 05:22:00 +00001788
1789 // TODO: Maybe we shouldn't reverse the order during insertion.
1790 DD->NextDiagnostic = Map->FirstDiagnostic;
1791 Map->FirstDiagnostic = DD;
1792
1793 return DD;
Ted Kremenekda4e0d32010-02-11 07:12:28 +00001794}