1*67e74705SXin Li //===--- DeclBase.cpp - Declaration AST Node Implementation ---------------===//
2*67e74705SXin Li //
3*67e74705SXin Li // The LLVM Compiler Infrastructure
4*67e74705SXin Li //
5*67e74705SXin Li // This file is distributed under the University of Illinois Open Source
6*67e74705SXin Li // License. See LICENSE.TXT for details.
7*67e74705SXin Li //
8*67e74705SXin Li //===----------------------------------------------------------------------===//
9*67e74705SXin Li //
10*67e74705SXin Li // This file implements the Decl and DeclContext classes.
11*67e74705SXin Li //
12*67e74705SXin Li //===----------------------------------------------------------------------===//
13*67e74705SXin Li
14*67e74705SXin Li #include "clang/AST/DeclBase.h"
15*67e74705SXin Li #include "clang/AST/ASTContext.h"
16*67e74705SXin Li #include "clang/AST/ASTMutationListener.h"
17*67e74705SXin Li #include "clang/AST/Attr.h"
18*67e74705SXin Li #include "clang/AST/Decl.h"
19*67e74705SXin Li #include "clang/AST/DeclCXX.h"
20*67e74705SXin Li #include "clang/AST/DeclContextInternals.h"
21*67e74705SXin Li #include "clang/AST/DeclFriend.h"
22*67e74705SXin Li #include "clang/AST/DeclObjC.h"
23*67e74705SXin Li #include "clang/AST/DeclOpenMP.h"
24*67e74705SXin Li #include "clang/AST/DeclTemplate.h"
25*67e74705SXin Li #include "clang/AST/DependentDiagnostic.h"
26*67e74705SXin Li #include "clang/AST/ExternalASTSource.h"
27*67e74705SXin Li #include "clang/AST/Stmt.h"
28*67e74705SXin Li #include "clang/AST/StmtCXX.h"
29*67e74705SXin Li #include "clang/AST/Type.h"
30*67e74705SXin Li #include "clang/Basic/TargetInfo.h"
31*67e74705SXin Li #include "llvm/ADT/DenseMap.h"
32*67e74705SXin Li #include "llvm/Support/raw_ostream.h"
33*67e74705SXin Li #include <algorithm>
34*67e74705SXin Li using namespace clang;
35*67e74705SXin Li
36*67e74705SXin Li //===----------------------------------------------------------------------===//
37*67e74705SXin Li // Statistics
38*67e74705SXin Li //===----------------------------------------------------------------------===//
39*67e74705SXin Li
40*67e74705SXin Li #define DECL(DERIVED, BASE) static int n##DERIVED##s = 0;
41*67e74705SXin Li #define ABSTRACT_DECL(DECL)
42*67e74705SXin Li #include "clang/AST/DeclNodes.inc"
43*67e74705SXin Li
updateOutOfDate(IdentifierInfo & II) const44*67e74705SXin Li void Decl::updateOutOfDate(IdentifierInfo &II) const {
45*67e74705SXin Li getASTContext().getExternalSource()->updateOutOfDateIdentifier(II);
46*67e74705SXin Li }
47*67e74705SXin Li
48*67e74705SXin Li #define DECL(DERIVED, BASE) \
49*67e74705SXin Li static_assert(llvm::AlignOf<Decl>::Alignment >= \
50*67e74705SXin Li llvm::AlignOf<DERIVED##Decl>::Alignment, \
51*67e74705SXin Li "Alignment sufficient after objects prepended to " #DERIVED);
52*67e74705SXin Li #define ABSTRACT_DECL(DECL)
53*67e74705SXin Li #include "clang/AST/DeclNodes.inc"
54*67e74705SXin Li
operator new(std::size_t Size,const ASTContext & Context,unsigned ID,std::size_t Extra)55*67e74705SXin Li void *Decl::operator new(std::size_t Size, const ASTContext &Context,
56*67e74705SXin Li unsigned ID, std::size_t Extra) {
57*67e74705SXin Li // Allocate an extra 8 bytes worth of storage, which ensures that the
58*67e74705SXin Li // resulting pointer will still be 8-byte aligned.
59*67e74705SXin Li static_assert(sizeof(unsigned) * 2 >= llvm::AlignOf<Decl>::Alignment,
60*67e74705SXin Li "Decl won't be misaligned");
61*67e74705SXin Li void *Start = Context.Allocate(Size + Extra + 8);
62*67e74705SXin Li void *Result = (char*)Start + 8;
63*67e74705SXin Li
64*67e74705SXin Li unsigned *PrefixPtr = (unsigned *)Result - 2;
65*67e74705SXin Li
66*67e74705SXin Li // Zero out the first 4 bytes; this is used to store the owning module ID.
67*67e74705SXin Li PrefixPtr[0] = 0;
68*67e74705SXin Li
69*67e74705SXin Li // Store the global declaration ID in the second 4 bytes.
70*67e74705SXin Li PrefixPtr[1] = ID;
71*67e74705SXin Li
72*67e74705SXin Li return Result;
73*67e74705SXin Li }
74*67e74705SXin Li
operator new(std::size_t Size,const ASTContext & Ctx,DeclContext * Parent,std::size_t Extra)75*67e74705SXin Li void *Decl::operator new(std::size_t Size, const ASTContext &Ctx,
76*67e74705SXin Li DeclContext *Parent, std::size_t Extra) {
77*67e74705SXin Li assert(!Parent || &Parent->getParentASTContext() == &Ctx);
78*67e74705SXin Li // With local visibility enabled, we track the owning module even for local
79*67e74705SXin Li // declarations.
80*67e74705SXin Li if (Ctx.getLangOpts().ModulesLocalVisibility) {
81*67e74705SXin Li // Ensure required alignment of the resulting object by adding extra
82*67e74705SXin Li // padding at the start if required.
83*67e74705SXin Li size_t ExtraAlign =
84*67e74705SXin Li llvm::OffsetToAlignment(sizeof(Module *),
85*67e74705SXin Li llvm::AlignOf<Decl>::Alignment);
86*67e74705SXin Li char *Buffer = reinterpret_cast<char *>(
87*67e74705SXin Li ::operator new(ExtraAlign + sizeof(Module *) + Size + Extra, Ctx));
88*67e74705SXin Li Buffer += ExtraAlign;
89*67e74705SXin Li return new (Buffer) Module*(nullptr) + 1;
90*67e74705SXin Li }
91*67e74705SXin Li return ::operator new(Size + Extra, Ctx);
92*67e74705SXin Li }
93*67e74705SXin Li
getOwningModuleSlow() const94*67e74705SXin Li Module *Decl::getOwningModuleSlow() const {
95*67e74705SXin Li assert(isFromASTFile() && "Not from AST file?");
96*67e74705SXin Li return getASTContext().getExternalSource()->getModule(getOwningModuleID());
97*67e74705SXin Li }
98*67e74705SXin Li
hasLocalOwningModuleStorage() const99*67e74705SXin Li bool Decl::hasLocalOwningModuleStorage() const {
100*67e74705SXin Li return getASTContext().getLangOpts().ModulesLocalVisibility;
101*67e74705SXin Li }
102*67e74705SXin Li
getDeclKindName() const103*67e74705SXin Li const char *Decl::getDeclKindName() const {
104*67e74705SXin Li switch (DeclKind) {
105*67e74705SXin Li default: llvm_unreachable("Declaration not in DeclNodes.inc!");
106*67e74705SXin Li #define DECL(DERIVED, BASE) case DERIVED: return #DERIVED;
107*67e74705SXin Li #define ABSTRACT_DECL(DECL)
108*67e74705SXin Li #include "clang/AST/DeclNodes.inc"
109*67e74705SXin Li }
110*67e74705SXin Li }
111*67e74705SXin Li
setInvalidDecl(bool Invalid)112*67e74705SXin Li void Decl::setInvalidDecl(bool Invalid) {
113*67e74705SXin Li InvalidDecl = Invalid;
114*67e74705SXin Li assert(!isa<TagDecl>(this) || !cast<TagDecl>(this)->isCompleteDefinition());
115*67e74705SXin Li if (Invalid && !isa<ParmVarDecl>(this)) {
116*67e74705SXin Li // Defensive maneuver for ill-formed code: we're likely not to make it to
117*67e74705SXin Li // a point where we set the access specifier, so default it to "public"
118*67e74705SXin Li // to avoid triggering asserts elsewhere in the front end.
119*67e74705SXin Li setAccess(AS_public);
120*67e74705SXin Li }
121*67e74705SXin Li }
122*67e74705SXin Li
getDeclKindName() const123*67e74705SXin Li const char *DeclContext::getDeclKindName() const {
124*67e74705SXin Li switch (DeclKind) {
125*67e74705SXin Li default: llvm_unreachable("Declaration context not in DeclNodes.inc!");
126*67e74705SXin Li #define DECL(DERIVED, BASE) case Decl::DERIVED: return #DERIVED;
127*67e74705SXin Li #define ABSTRACT_DECL(DECL)
128*67e74705SXin Li #include "clang/AST/DeclNodes.inc"
129*67e74705SXin Li }
130*67e74705SXin Li }
131*67e74705SXin Li
132*67e74705SXin Li bool Decl::StatisticsEnabled = false;
EnableStatistics()133*67e74705SXin Li void Decl::EnableStatistics() {
134*67e74705SXin Li StatisticsEnabled = true;
135*67e74705SXin Li }
136*67e74705SXin Li
PrintStats()137*67e74705SXin Li void Decl::PrintStats() {
138*67e74705SXin Li llvm::errs() << "\n*** Decl Stats:\n";
139*67e74705SXin Li
140*67e74705SXin Li int totalDecls = 0;
141*67e74705SXin Li #define DECL(DERIVED, BASE) totalDecls += n##DERIVED##s;
142*67e74705SXin Li #define ABSTRACT_DECL(DECL)
143*67e74705SXin Li #include "clang/AST/DeclNodes.inc"
144*67e74705SXin Li llvm::errs() << " " << totalDecls << " decls total.\n";
145*67e74705SXin Li
146*67e74705SXin Li int totalBytes = 0;
147*67e74705SXin Li #define DECL(DERIVED, BASE) \
148*67e74705SXin Li if (n##DERIVED##s > 0) { \
149*67e74705SXin Li totalBytes += (int)(n##DERIVED##s * sizeof(DERIVED##Decl)); \
150*67e74705SXin Li llvm::errs() << " " << n##DERIVED##s << " " #DERIVED " decls, " \
151*67e74705SXin Li << sizeof(DERIVED##Decl) << " each (" \
152*67e74705SXin Li << n##DERIVED##s * sizeof(DERIVED##Decl) \
153*67e74705SXin Li << " bytes)\n"; \
154*67e74705SXin Li }
155*67e74705SXin Li #define ABSTRACT_DECL(DECL)
156*67e74705SXin Li #include "clang/AST/DeclNodes.inc"
157*67e74705SXin Li
158*67e74705SXin Li llvm::errs() << "Total bytes = " << totalBytes << "\n";
159*67e74705SXin Li }
160*67e74705SXin Li
add(Kind k)161*67e74705SXin Li void Decl::add(Kind k) {
162*67e74705SXin Li switch (k) {
163*67e74705SXin Li #define DECL(DERIVED, BASE) case DERIVED: ++n##DERIVED##s; break;
164*67e74705SXin Li #define ABSTRACT_DECL(DECL)
165*67e74705SXin Li #include "clang/AST/DeclNodes.inc"
166*67e74705SXin Li }
167*67e74705SXin Li }
168*67e74705SXin Li
isTemplateParameterPack() const169*67e74705SXin Li bool Decl::isTemplateParameterPack() const {
170*67e74705SXin Li if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
171*67e74705SXin Li return TTP->isParameterPack();
172*67e74705SXin Li if (const NonTypeTemplateParmDecl *NTTP
173*67e74705SXin Li = dyn_cast<NonTypeTemplateParmDecl>(this))
174*67e74705SXin Li return NTTP->isParameterPack();
175*67e74705SXin Li if (const TemplateTemplateParmDecl *TTP
176*67e74705SXin Li = dyn_cast<TemplateTemplateParmDecl>(this))
177*67e74705SXin Li return TTP->isParameterPack();
178*67e74705SXin Li return false;
179*67e74705SXin Li }
180*67e74705SXin Li
isParameterPack() const181*67e74705SXin Li bool Decl::isParameterPack() const {
182*67e74705SXin Li if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(this))
183*67e74705SXin Li return Parm->isParameterPack();
184*67e74705SXin Li
185*67e74705SXin Li return isTemplateParameterPack();
186*67e74705SXin Li }
187*67e74705SXin Li
getAsFunction()188*67e74705SXin Li FunctionDecl *Decl::getAsFunction() {
189*67e74705SXin Li if (FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
190*67e74705SXin Li return FD;
191*67e74705SXin Li if (const FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(this))
192*67e74705SXin Li return FTD->getTemplatedDecl();
193*67e74705SXin Li return nullptr;
194*67e74705SXin Li }
195*67e74705SXin Li
isTemplateDecl() const196*67e74705SXin Li bool Decl::isTemplateDecl() const {
197*67e74705SXin Li return isa<TemplateDecl>(this);
198*67e74705SXin Li }
199*67e74705SXin Li
getDescribedTemplate() const200*67e74705SXin Li TemplateDecl *Decl::getDescribedTemplate() const {
201*67e74705SXin Li if (auto *FD = dyn_cast<FunctionDecl>(this))
202*67e74705SXin Li return FD->getDescribedFunctionTemplate();
203*67e74705SXin Li else if (auto *RD = dyn_cast<CXXRecordDecl>(this))
204*67e74705SXin Li return RD->getDescribedClassTemplate();
205*67e74705SXin Li else if (auto *VD = dyn_cast<VarDecl>(this))
206*67e74705SXin Li return VD->getDescribedVarTemplate();
207*67e74705SXin Li
208*67e74705SXin Li return nullptr;
209*67e74705SXin Li }
210*67e74705SXin Li
getParentFunctionOrMethod() const211*67e74705SXin Li const DeclContext *Decl::getParentFunctionOrMethod() const {
212*67e74705SXin Li for (const DeclContext *DC = getDeclContext();
213*67e74705SXin Li DC && !DC->isTranslationUnit() && !DC->isNamespace();
214*67e74705SXin Li DC = DC->getParent())
215*67e74705SXin Li if (DC->isFunctionOrMethod())
216*67e74705SXin Li return DC;
217*67e74705SXin Li
218*67e74705SXin Li return nullptr;
219*67e74705SXin Li }
220*67e74705SXin Li
221*67e74705SXin Li
222*67e74705SXin Li //===----------------------------------------------------------------------===//
223*67e74705SXin Li // PrettyStackTraceDecl Implementation
224*67e74705SXin Li //===----------------------------------------------------------------------===//
225*67e74705SXin Li
print(raw_ostream & OS) const226*67e74705SXin Li void PrettyStackTraceDecl::print(raw_ostream &OS) const {
227*67e74705SXin Li SourceLocation TheLoc = Loc;
228*67e74705SXin Li if (TheLoc.isInvalid() && TheDecl)
229*67e74705SXin Li TheLoc = TheDecl->getLocation();
230*67e74705SXin Li
231*67e74705SXin Li if (TheLoc.isValid()) {
232*67e74705SXin Li TheLoc.print(OS, SM);
233*67e74705SXin Li OS << ": ";
234*67e74705SXin Li }
235*67e74705SXin Li
236*67e74705SXin Li OS << Message;
237*67e74705SXin Li
238*67e74705SXin Li if (const NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl)) {
239*67e74705SXin Li OS << " '";
240*67e74705SXin Li DN->printQualifiedName(OS);
241*67e74705SXin Li OS << '\'';
242*67e74705SXin Li }
243*67e74705SXin Li OS << '\n';
244*67e74705SXin Li }
245*67e74705SXin Li
246*67e74705SXin Li //===----------------------------------------------------------------------===//
247*67e74705SXin Li // Decl Implementation
248*67e74705SXin Li //===----------------------------------------------------------------------===//
249*67e74705SXin Li
250*67e74705SXin Li // Out-of-line virtual method providing a home for Decl.
~Decl()251*67e74705SXin Li Decl::~Decl() { }
252*67e74705SXin Li
setDeclContext(DeclContext * DC)253*67e74705SXin Li void Decl::setDeclContext(DeclContext *DC) {
254*67e74705SXin Li DeclCtx = DC;
255*67e74705SXin Li }
256*67e74705SXin Li
setLexicalDeclContext(DeclContext * DC)257*67e74705SXin Li void Decl::setLexicalDeclContext(DeclContext *DC) {
258*67e74705SXin Li if (DC == getLexicalDeclContext())
259*67e74705SXin Li return;
260*67e74705SXin Li
261*67e74705SXin Li if (isInSemaDC()) {
262*67e74705SXin Li setDeclContextsImpl(getDeclContext(), DC, getASTContext());
263*67e74705SXin Li } else {
264*67e74705SXin Li getMultipleDC()->LexicalDC = DC;
265*67e74705SXin Li }
266*67e74705SXin Li Hidden = cast<Decl>(DC)->Hidden;
267*67e74705SXin Li }
268*67e74705SXin Li
setDeclContextsImpl(DeclContext * SemaDC,DeclContext * LexicalDC,ASTContext & Ctx)269*67e74705SXin Li void Decl::setDeclContextsImpl(DeclContext *SemaDC, DeclContext *LexicalDC,
270*67e74705SXin Li ASTContext &Ctx) {
271*67e74705SXin Li if (SemaDC == LexicalDC) {
272*67e74705SXin Li DeclCtx = SemaDC;
273*67e74705SXin Li } else {
274*67e74705SXin Li Decl::MultipleDC *MDC = new (Ctx) Decl::MultipleDC();
275*67e74705SXin Li MDC->SemanticDC = SemaDC;
276*67e74705SXin Li MDC->LexicalDC = LexicalDC;
277*67e74705SXin Li DeclCtx = MDC;
278*67e74705SXin Li }
279*67e74705SXin Li }
280*67e74705SXin Li
isLexicallyWithinFunctionOrMethod() const281*67e74705SXin Li bool Decl::isLexicallyWithinFunctionOrMethod() const {
282*67e74705SXin Li const DeclContext *LDC = getLexicalDeclContext();
283*67e74705SXin Li while (true) {
284*67e74705SXin Li if (LDC->isFunctionOrMethod())
285*67e74705SXin Li return true;
286*67e74705SXin Li if (!isa<TagDecl>(LDC))
287*67e74705SXin Li return false;
288*67e74705SXin Li LDC = LDC->getLexicalParent();
289*67e74705SXin Li }
290*67e74705SXin Li return false;
291*67e74705SXin Li }
292*67e74705SXin Li
isInAnonymousNamespace() const293*67e74705SXin Li bool Decl::isInAnonymousNamespace() const {
294*67e74705SXin Li const DeclContext *DC = getDeclContext();
295*67e74705SXin Li do {
296*67e74705SXin Li if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC))
297*67e74705SXin Li if (ND->isAnonymousNamespace())
298*67e74705SXin Li return true;
299*67e74705SXin Li } while ((DC = DC->getParent()));
300*67e74705SXin Li
301*67e74705SXin Li return false;
302*67e74705SXin Li }
303*67e74705SXin Li
isInStdNamespace() const304*67e74705SXin Li bool Decl::isInStdNamespace() const {
305*67e74705SXin Li return getDeclContext()->isStdNamespace();
306*67e74705SXin Li }
307*67e74705SXin Li
getTranslationUnitDecl()308*67e74705SXin Li TranslationUnitDecl *Decl::getTranslationUnitDecl() {
309*67e74705SXin Li if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
310*67e74705SXin Li return TUD;
311*67e74705SXin Li
312*67e74705SXin Li DeclContext *DC = getDeclContext();
313*67e74705SXin Li assert(DC && "This decl is not contained in a translation unit!");
314*67e74705SXin Li
315*67e74705SXin Li while (!DC->isTranslationUnit()) {
316*67e74705SXin Li DC = DC->getParent();
317*67e74705SXin Li assert(DC && "This decl is not contained in a translation unit!");
318*67e74705SXin Li }
319*67e74705SXin Li
320*67e74705SXin Li return cast<TranslationUnitDecl>(DC);
321*67e74705SXin Li }
322*67e74705SXin Li
getASTContext() const323*67e74705SXin Li ASTContext &Decl::getASTContext() const {
324*67e74705SXin Li return getTranslationUnitDecl()->getASTContext();
325*67e74705SXin Li }
326*67e74705SXin Li
getASTMutationListener() const327*67e74705SXin Li ASTMutationListener *Decl::getASTMutationListener() const {
328*67e74705SXin Li return getASTContext().getASTMutationListener();
329*67e74705SXin Li }
330*67e74705SXin Li
getMaxAlignment() const331*67e74705SXin Li unsigned Decl::getMaxAlignment() const {
332*67e74705SXin Li if (!hasAttrs())
333*67e74705SXin Li return 0;
334*67e74705SXin Li
335*67e74705SXin Li unsigned Align = 0;
336*67e74705SXin Li const AttrVec &V = getAttrs();
337*67e74705SXin Li ASTContext &Ctx = getASTContext();
338*67e74705SXin Li specific_attr_iterator<AlignedAttr> I(V.begin()), E(V.end());
339*67e74705SXin Li for (; I != E; ++I)
340*67e74705SXin Li Align = std::max(Align, I->getAlignment(Ctx));
341*67e74705SXin Li return Align;
342*67e74705SXin Li }
343*67e74705SXin Li
isUsed(bool CheckUsedAttr) const344*67e74705SXin Li bool Decl::isUsed(bool CheckUsedAttr) const {
345*67e74705SXin Li const Decl *CanonD = getCanonicalDecl();
346*67e74705SXin Li if (CanonD->Used)
347*67e74705SXin Li return true;
348*67e74705SXin Li
349*67e74705SXin Li // Check for used attribute.
350*67e74705SXin Li // Ask the most recent decl, since attributes accumulate in the redecl chain.
351*67e74705SXin Li if (CheckUsedAttr && getMostRecentDecl()->hasAttr<UsedAttr>())
352*67e74705SXin Li return true;
353*67e74705SXin Li
354*67e74705SXin Li // The information may have not been deserialized yet. Force deserialization
355*67e74705SXin Li // to complete the needed information.
356*67e74705SXin Li return getMostRecentDecl()->getCanonicalDecl()->Used;
357*67e74705SXin Li }
358*67e74705SXin Li
markUsed(ASTContext & C)359*67e74705SXin Li void Decl::markUsed(ASTContext &C) {
360*67e74705SXin Li if (isUsed(false))
361*67e74705SXin Li return;
362*67e74705SXin Li
363*67e74705SXin Li if (C.getASTMutationListener())
364*67e74705SXin Li C.getASTMutationListener()->DeclarationMarkedUsed(this);
365*67e74705SXin Li
366*67e74705SXin Li setIsUsed();
367*67e74705SXin Li }
368*67e74705SXin Li
isReferenced() const369*67e74705SXin Li bool Decl::isReferenced() const {
370*67e74705SXin Li if (Referenced)
371*67e74705SXin Li return true;
372*67e74705SXin Li
373*67e74705SXin Li // Check redeclarations.
374*67e74705SXin Li for (auto I : redecls())
375*67e74705SXin Li if (I->Referenced)
376*67e74705SXin Li return true;
377*67e74705SXin Li
378*67e74705SXin Li return false;
379*67e74705SXin Li }
380*67e74705SXin Li
hasDefiningAttr() const381*67e74705SXin Li bool Decl::hasDefiningAttr() const {
382*67e74705SXin Li return hasAttr<AliasAttr>() || hasAttr<IFuncAttr>();
383*67e74705SXin Li }
384*67e74705SXin Li
getDefiningAttr() const385*67e74705SXin Li const Attr *Decl::getDefiningAttr() const {
386*67e74705SXin Li if (AliasAttr *AA = getAttr<AliasAttr>())
387*67e74705SXin Li return AA;
388*67e74705SXin Li if (IFuncAttr *IFA = getAttr<IFuncAttr>())
389*67e74705SXin Li return IFA;
390*67e74705SXin Li return nullptr;
391*67e74705SXin Li }
392*67e74705SXin Li
393*67e74705SXin Li /// \brief Determine the availability of the given declaration based on
394*67e74705SXin Li /// the target platform.
395*67e74705SXin Li ///
396*67e74705SXin Li /// When it returns an availability result other than \c AR_Available,
397*67e74705SXin Li /// if the \p Message parameter is non-NULL, it will be set to a
398*67e74705SXin Li /// string describing why the entity is unavailable.
399*67e74705SXin Li ///
400*67e74705SXin Li /// FIXME: Make these strings localizable, since they end up in
401*67e74705SXin Li /// diagnostics.
CheckAvailability(ASTContext & Context,const AvailabilityAttr * A,std::string * Message)402*67e74705SXin Li static AvailabilityResult CheckAvailability(ASTContext &Context,
403*67e74705SXin Li const AvailabilityAttr *A,
404*67e74705SXin Li std::string *Message) {
405*67e74705SXin Li VersionTuple TargetMinVersion =
406*67e74705SXin Li Context.getTargetInfo().getPlatformMinVersion();
407*67e74705SXin Li
408*67e74705SXin Li if (TargetMinVersion.empty())
409*67e74705SXin Li return AR_Available;
410*67e74705SXin Li
411*67e74705SXin Li // Check if this is an App Extension "platform", and if so chop off
412*67e74705SXin Li // the suffix for matching with the actual platform.
413*67e74705SXin Li StringRef ActualPlatform = A->getPlatform()->getName();
414*67e74705SXin Li StringRef RealizedPlatform = ActualPlatform;
415*67e74705SXin Li if (Context.getLangOpts().AppExt) {
416*67e74705SXin Li size_t suffix = RealizedPlatform.rfind("_app_extension");
417*67e74705SXin Li if (suffix != StringRef::npos)
418*67e74705SXin Li RealizedPlatform = RealizedPlatform.slice(0, suffix);
419*67e74705SXin Li }
420*67e74705SXin Li
421*67e74705SXin Li StringRef TargetPlatform = Context.getTargetInfo().getPlatformName();
422*67e74705SXin Li
423*67e74705SXin Li // Match the platform name.
424*67e74705SXin Li if (RealizedPlatform != TargetPlatform)
425*67e74705SXin Li return AR_Available;
426*67e74705SXin Li
427*67e74705SXin Li StringRef PrettyPlatformName
428*67e74705SXin Li = AvailabilityAttr::getPrettyPlatformName(ActualPlatform);
429*67e74705SXin Li
430*67e74705SXin Li if (PrettyPlatformName.empty())
431*67e74705SXin Li PrettyPlatformName = ActualPlatform;
432*67e74705SXin Li
433*67e74705SXin Li std::string HintMessage;
434*67e74705SXin Li if (!A->getMessage().empty()) {
435*67e74705SXin Li HintMessage = " - ";
436*67e74705SXin Li HintMessage += A->getMessage();
437*67e74705SXin Li }
438*67e74705SXin Li
439*67e74705SXin Li // Make sure that this declaration has not been marked 'unavailable'.
440*67e74705SXin Li if (A->getUnavailable()) {
441*67e74705SXin Li if (Message) {
442*67e74705SXin Li Message->clear();
443*67e74705SXin Li llvm::raw_string_ostream Out(*Message);
444*67e74705SXin Li Out << "not available on " << PrettyPlatformName
445*67e74705SXin Li << HintMessage;
446*67e74705SXin Li }
447*67e74705SXin Li
448*67e74705SXin Li return AR_Unavailable;
449*67e74705SXin Li }
450*67e74705SXin Li
451*67e74705SXin Li // Make sure that this declaration has already been introduced.
452*67e74705SXin Li if (!A->getIntroduced().empty() &&
453*67e74705SXin Li TargetMinVersion < A->getIntroduced()) {
454*67e74705SXin Li if (Message) {
455*67e74705SXin Li Message->clear();
456*67e74705SXin Li llvm::raw_string_ostream Out(*Message);
457*67e74705SXin Li VersionTuple VTI(A->getIntroduced());
458*67e74705SXin Li VTI.UseDotAsSeparator();
459*67e74705SXin Li Out << "introduced in " << PrettyPlatformName << ' '
460*67e74705SXin Li << VTI << HintMessage;
461*67e74705SXin Li }
462*67e74705SXin Li
463*67e74705SXin Li return A->getStrict() ? AR_Unavailable : AR_NotYetIntroduced;
464*67e74705SXin Li }
465*67e74705SXin Li
466*67e74705SXin Li // Make sure that this declaration hasn't been obsoleted.
467*67e74705SXin Li if (!A->getObsoleted().empty() && TargetMinVersion >= A->getObsoleted()) {
468*67e74705SXin Li if (Message) {
469*67e74705SXin Li Message->clear();
470*67e74705SXin Li llvm::raw_string_ostream Out(*Message);
471*67e74705SXin Li VersionTuple VTO(A->getObsoleted());
472*67e74705SXin Li VTO.UseDotAsSeparator();
473*67e74705SXin Li Out << "obsoleted in " << PrettyPlatformName << ' '
474*67e74705SXin Li << VTO << HintMessage;
475*67e74705SXin Li }
476*67e74705SXin Li
477*67e74705SXin Li return AR_Unavailable;
478*67e74705SXin Li }
479*67e74705SXin Li
480*67e74705SXin Li // Make sure that this declaration hasn't been deprecated.
481*67e74705SXin Li if (!A->getDeprecated().empty() && TargetMinVersion >= A->getDeprecated()) {
482*67e74705SXin Li if (Message) {
483*67e74705SXin Li Message->clear();
484*67e74705SXin Li llvm::raw_string_ostream Out(*Message);
485*67e74705SXin Li VersionTuple VTD(A->getDeprecated());
486*67e74705SXin Li VTD.UseDotAsSeparator();
487*67e74705SXin Li Out << "first deprecated in " << PrettyPlatformName << ' '
488*67e74705SXin Li << VTD << HintMessage;
489*67e74705SXin Li }
490*67e74705SXin Li
491*67e74705SXin Li return AR_Deprecated;
492*67e74705SXin Li }
493*67e74705SXin Li
494*67e74705SXin Li return AR_Available;
495*67e74705SXin Li }
496*67e74705SXin Li
getAvailability(std::string * Message) const497*67e74705SXin Li AvailabilityResult Decl::getAvailability(std::string *Message) const {
498*67e74705SXin Li if (auto *FTD = dyn_cast<FunctionTemplateDecl>(this))
499*67e74705SXin Li return FTD->getTemplatedDecl()->getAvailability(Message);
500*67e74705SXin Li
501*67e74705SXin Li AvailabilityResult Result = AR_Available;
502*67e74705SXin Li std::string ResultMessage;
503*67e74705SXin Li
504*67e74705SXin Li for (const auto *A : attrs()) {
505*67e74705SXin Li if (const auto *Deprecated = dyn_cast<DeprecatedAttr>(A)) {
506*67e74705SXin Li if (Result >= AR_Deprecated)
507*67e74705SXin Li continue;
508*67e74705SXin Li
509*67e74705SXin Li if (Message)
510*67e74705SXin Li ResultMessage = Deprecated->getMessage();
511*67e74705SXin Li
512*67e74705SXin Li Result = AR_Deprecated;
513*67e74705SXin Li continue;
514*67e74705SXin Li }
515*67e74705SXin Li
516*67e74705SXin Li if (const auto *Unavailable = dyn_cast<UnavailableAttr>(A)) {
517*67e74705SXin Li if (Message)
518*67e74705SXin Li *Message = Unavailable->getMessage();
519*67e74705SXin Li return AR_Unavailable;
520*67e74705SXin Li }
521*67e74705SXin Li
522*67e74705SXin Li if (const auto *Availability = dyn_cast<AvailabilityAttr>(A)) {
523*67e74705SXin Li AvailabilityResult AR = CheckAvailability(getASTContext(), Availability,
524*67e74705SXin Li Message);
525*67e74705SXin Li
526*67e74705SXin Li if (AR == AR_Unavailable)
527*67e74705SXin Li return AR_Unavailable;
528*67e74705SXin Li
529*67e74705SXin Li if (AR > Result) {
530*67e74705SXin Li Result = AR;
531*67e74705SXin Li if (Message)
532*67e74705SXin Li ResultMessage.swap(*Message);
533*67e74705SXin Li }
534*67e74705SXin Li continue;
535*67e74705SXin Li }
536*67e74705SXin Li }
537*67e74705SXin Li
538*67e74705SXin Li if (Message)
539*67e74705SXin Li Message->swap(ResultMessage);
540*67e74705SXin Li return Result;
541*67e74705SXin Li }
542*67e74705SXin Li
canBeWeakImported(bool & IsDefinition) const543*67e74705SXin Li bool Decl::canBeWeakImported(bool &IsDefinition) const {
544*67e74705SXin Li IsDefinition = false;
545*67e74705SXin Li
546*67e74705SXin Li // Variables, if they aren't definitions.
547*67e74705SXin Li if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
548*67e74705SXin Li if (Var->isThisDeclarationADefinition()) {
549*67e74705SXin Li IsDefinition = true;
550*67e74705SXin Li return false;
551*67e74705SXin Li }
552*67e74705SXin Li return true;
553*67e74705SXin Li
554*67e74705SXin Li // Functions, if they aren't definitions.
555*67e74705SXin Li } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
556*67e74705SXin Li if (FD->hasBody()) {
557*67e74705SXin Li IsDefinition = true;
558*67e74705SXin Li return false;
559*67e74705SXin Li }
560*67e74705SXin Li return true;
561*67e74705SXin Li
562*67e74705SXin Li // Objective-C classes, if this is the non-fragile runtime.
563*67e74705SXin Li } else if (isa<ObjCInterfaceDecl>(this) &&
564*67e74705SXin Li getASTContext().getLangOpts().ObjCRuntime.hasWeakClassImport()) {
565*67e74705SXin Li return true;
566*67e74705SXin Li
567*67e74705SXin Li // Nothing else.
568*67e74705SXin Li } else {
569*67e74705SXin Li return false;
570*67e74705SXin Li }
571*67e74705SXin Li }
572*67e74705SXin Li
isWeakImported() const573*67e74705SXin Li bool Decl::isWeakImported() const {
574*67e74705SXin Li bool IsDefinition;
575*67e74705SXin Li if (!canBeWeakImported(IsDefinition))
576*67e74705SXin Li return false;
577*67e74705SXin Li
578*67e74705SXin Li for (const auto *A : attrs()) {
579*67e74705SXin Li if (isa<WeakImportAttr>(A))
580*67e74705SXin Li return true;
581*67e74705SXin Li
582*67e74705SXin Li if (const auto *Availability = dyn_cast<AvailabilityAttr>(A)) {
583*67e74705SXin Li if (CheckAvailability(getASTContext(), Availability,
584*67e74705SXin Li nullptr) == AR_NotYetIntroduced)
585*67e74705SXin Li return true;
586*67e74705SXin Li }
587*67e74705SXin Li }
588*67e74705SXin Li
589*67e74705SXin Li return false;
590*67e74705SXin Li }
591*67e74705SXin Li
getIdentifierNamespaceForKind(Kind DeclKind)592*67e74705SXin Li unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
593*67e74705SXin Li switch (DeclKind) {
594*67e74705SXin Li case Function:
595*67e74705SXin Li case CXXMethod:
596*67e74705SXin Li case CXXConstructor:
597*67e74705SXin Li case ConstructorUsingShadow:
598*67e74705SXin Li case CXXDestructor:
599*67e74705SXin Li case CXXConversion:
600*67e74705SXin Li case EnumConstant:
601*67e74705SXin Li case Var:
602*67e74705SXin Li case ImplicitParam:
603*67e74705SXin Li case ParmVar:
604*67e74705SXin Li case ObjCMethod:
605*67e74705SXin Li case ObjCProperty:
606*67e74705SXin Li case MSProperty:
607*67e74705SXin Li return IDNS_Ordinary;
608*67e74705SXin Li case Label:
609*67e74705SXin Li return IDNS_Label;
610*67e74705SXin Li case IndirectField:
611*67e74705SXin Li return IDNS_Ordinary | IDNS_Member;
612*67e74705SXin Li
613*67e74705SXin Li case NonTypeTemplateParm:
614*67e74705SXin Li // Non-type template parameters are not found by lookups that ignore
615*67e74705SXin Li // non-types, but they are found by redeclaration lookups for tag types,
616*67e74705SXin Li // so we include them in the tag namespace.
617*67e74705SXin Li return IDNS_Ordinary | IDNS_Tag;
618*67e74705SXin Li
619*67e74705SXin Li case ObjCCompatibleAlias:
620*67e74705SXin Li case ObjCInterface:
621*67e74705SXin Li return IDNS_Ordinary | IDNS_Type;
622*67e74705SXin Li
623*67e74705SXin Li case Typedef:
624*67e74705SXin Li case TypeAlias:
625*67e74705SXin Li case TypeAliasTemplate:
626*67e74705SXin Li case UnresolvedUsingTypename:
627*67e74705SXin Li case TemplateTypeParm:
628*67e74705SXin Li case ObjCTypeParam:
629*67e74705SXin Li return IDNS_Ordinary | IDNS_Type;
630*67e74705SXin Li
631*67e74705SXin Li case UsingShadow:
632*67e74705SXin Li return 0; // we'll actually overwrite this later
633*67e74705SXin Li
634*67e74705SXin Li case UnresolvedUsingValue:
635*67e74705SXin Li return IDNS_Ordinary | IDNS_Using;
636*67e74705SXin Li
637*67e74705SXin Li case Using:
638*67e74705SXin Li return IDNS_Using;
639*67e74705SXin Li
640*67e74705SXin Li case ObjCProtocol:
641*67e74705SXin Li return IDNS_ObjCProtocol;
642*67e74705SXin Li
643*67e74705SXin Li case Field:
644*67e74705SXin Li case ObjCAtDefsField:
645*67e74705SXin Li case ObjCIvar:
646*67e74705SXin Li return IDNS_Member;
647*67e74705SXin Li
648*67e74705SXin Li case Record:
649*67e74705SXin Li case CXXRecord:
650*67e74705SXin Li case Enum:
651*67e74705SXin Li return IDNS_Tag | IDNS_Type;
652*67e74705SXin Li
653*67e74705SXin Li case Namespace:
654*67e74705SXin Li case NamespaceAlias:
655*67e74705SXin Li return IDNS_Namespace;
656*67e74705SXin Li
657*67e74705SXin Li case FunctionTemplate:
658*67e74705SXin Li case VarTemplate:
659*67e74705SXin Li return IDNS_Ordinary;
660*67e74705SXin Li
661*67e74705SXin Li case ClassTemplate:
662*67e74705SXin Li case TemplateTemplateParm:
663*67e74705SXin Li return IDNS_Ordinary | IDNS_Tag | IDNS_Type;
664*67e74705SXin Li
665*67e74705SXin Li case OMPDeclareReduction:
666*67e74705SXin Li return IDNS_OMPReduction;
667*67e74705SXin Li
668*67e74705SXin Li // Never have names.
669*67e74705SXin Li case Friend:
670*67e74705SXin Li case FriendTemplate:
671*67e74705SXin Li case AccessSpec:
672*67e74705SXin Li case LinkageSpec:
673*67e74705SXin Li case FileScopeAsm:
674*67e74705SXin Li case StaticAssert:
675*67e74705SXin Li case ObjCPropertyImpl:
676*67e74705SXin Li case PragmaComment:
677*67e74705SXin Li case PragmaDetectMismatch:
678*67e74705SXin Li case Block:
679*67e74705SXin Li case Captured:
680*67e74705SXin Li case TranslationUnit:
681*67e74705SXin Li case ExternCContext:
682*67e74705SXin Li
683*67e74705SXin Li case UsingDirective:
684*67e74705SXin Li case BuiltinTemplate:
685*67e74705SXin Li case ClassTemplateSpecialization:
686*67e74705SXin Li case ClassTemplatePartialSpecialization:
687*67e74705SXin Li case ClassScopeFunctionSpecialization:
688*67e74705SXin Li case VarTemplateSpecialization:
689*67e74705SXin Li case VarTemplatePartialSpecialization:
690*67e74705SXin Li case ObjCImplementation:
691*67e74705SXin Li case ObjCCategory:
692*67e74705SXin Li case ObjCCategoryImpl:
693*67e74705SXin Li case Import:
694*67e74705SXin Li case OMPThreadPrivate:
695*67e74705SXin Li case OMPCapturedExpr:
696*67e74705SXin Li case Empty:
697*67e74705SXin Li // Never looked up by name.
698*67e74705SXin Li return 0;
699*67e74705SXin Li }
700*67e74705SXin Li
701*67e74705SXin Li llvm_unreachable("Invalid DeclKind!");
702*67e74705SXin Li }
703*67e74705SXin Li
setAttrsImpl(const AttrVec & attrs,ASTContext & Ctx)704*67e74705SXin Li void Decl::setAttrsImpl(const AttrVec &attrs, ASTContext &Ctx) {
705*67e74705SXin Li assert(!HasAttrs && "Decl already contains attrs.");
706*67e74705SXin Li
707*67e74705SXin Li AttrVec &AttrBlank = Ctx.getDeclAttrs(this);
708*67e74705SXin Li assert(AttrBlank.empty() && "HasAttrs was wrong?");
709*67e74705SXin Li
710*67e74705SXin Li AttrBlank = attrs;
711*67e74705SXin Li HasAttrs = true;
712*67e74705SXin Li }
713*67e74705SXin Li
dropAttrs()714*67e74705SXin Li void Decl::dropAttrs() {
715*67e74705SXin Li if (!HasAttrs) return;
716*67e74705SXin Li
717*67e74705SXin Li HasAttrs = false;
718*67e74705SXin Li getASTContext().eraseDeclAttrs(this);
719*67e74705SXin Li }
720*67e74705SXin Li
getAttrs() const721*67e74705SXin Li const AttrVec &Decl::getAttrs() const {
722*67e74705SXin Li assert(HasAttrs && "No attrs to get!");
723*67e74705SXin Li return getASTContext().getDeclAttrs(this);
724*67e74705SXin Li }
725*67e74705SXin Li
castFromDeclContext(const DeclContext * D)726*67e74705SXin Li Decl *Decl::castFromDeclContext (const DeclContext *D) {
727*67e74705SXin Li Decl::Kind DK = D->getDeclKind();
728*67e74705SXin Li switch(DK) {
729*67e74705SXin Li #define DECL(NAME, BASE)
730*67e74705SXin Li #define DECL_CONTEXT(NAME) \
731*67e74705SXin Li case Decl::NAME: \
732*67e74705SXin Li return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
733*67e74705SXin Li #define DECL_CONTEXT_BASE(NAME)
734*67e74705SXin Li #include "clang/AST/DeclNodes.inc"
735*67e74705SXin Li default:
736*67e74705SXin Li #define DECL(NAME, BASE)
737*67e74705SXin Li #define DECL_CONTEXT_BASE(NAME) \
738*67e74705SXin Li if (DK >= first##NAME && DK <= last##NAME) \
739*67e74705SXin Li return static_cast<NAME##Decl*>(const_cast<DeclContext*>(D));
740*67e74705SXin Li #include "clang/AST/DeclNodes.inc"
741*67e74705SXin Li llvm_unreachable("a decl that inherits DeclContext isn't handled");
742*67e74705SXin Li }
743*67e74705SXin Li }
744*67e74705SXin Li
castToDeclContext(const Decl * D)745*67e74705SXin Li DeclContext *Decl::castToDeclContext(const Decl *D) {
746*67e74705SXin Li Decl::Kind DK = D->getKind();
747*67e74705SXin Li switch(DK) {
748*67e74705SXin Li #define DECL(NAME, BASE)
749*67e74705SXin Li #define DECL_CONTEXT(NAME) \
750*67e74705SXin Li case Decl::NAME: \
751*67e74705SXin Li return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
752*67e74705SXin Li #define DECL_CONTEXT_BASE(NAME)
753*67e74705SXin Li #include "clang/AST/DeclNodes.inc"
754*67e74705SXin Li default:
755*67e74705SXin Li #define DECL(NAME, BASE)
756*67e74705SXin Li #define DECL_CONTEXT_BASE(NAME) \
757*67e74705SXin Li if (DK >= first##NAME && DK <= last##NAME) \
758*67e74705SXin Li return static_cast<NAME##Decl*>(const_cast<Decl*>(D));
759*67e74705SXin Li #include "clang/AST/DeclNodes.inc"
760*67e74705SXin Li llvm_unreachable("a decl that inherits DeclContext isn't handled");
761*67e74705SXin Li }
762*67e74705SXin Li }
763*67e74705SXin Li
getBodyRBrace() const764*67e74705SXin Li SourceLocation Decl::getBodyRBrace() const {
765*67e74705SXin Li // Special handling of FunctionDecl to avoid de-serializing the body from PCH.
766*67e74705SXin Li // FunctionDecl stores EndRangeLoc for this purpose.
767*67e74705SXin Li if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) {
768*67e74705SXin Li const FunctionDecl *Definition;
769*67e74705SXin Li if (FD->hasBody(Definition))
770*67e74705SXin Li return Definition->getSourceRange().getEnd();
771*67e74705SXin Li return SourceLocation();
772*67e74705SXin Li }
773*67e74705SXin Li
774*67e74705SXin Li if (Stmt *Body = getBody())
775*67e74705SXin Li return Body->getSourceRange().getEnd();
776*67e74705SXin Li
777*67e74705SXin Li return SourceLocation();
778*67e74705SXin Li }
779*67e74705SXin Li
AccessDeclContextSanity() const780*67e74705SXin Li bool Decl::AccessDeclContextSanity() const {
781*67e74705SXin Li #ifndef NDEBUG
782*67e74705SXin Li // Suppress this check if any of the following hold:
783*67e74705SXin Li // 1. this is the translation unit (and thus has no parent)
784*67e74705SXin Li // 2. this is a template parameter (and thus doesn't belong to its context)
785*67e74705SXin Li // 3. this is a non-type template parameter
786*67e74705SXin Li // 4. the context is not a record
787*67e74705SXin Li // 5. it's invalid
788*67e74705SXin Li // 6. it's a C++0x static_assert.
789*67e74705SXin Li if (isa<TranslationUnitDecl>(this) ||
790*67e74705SXin Li isa<TemplateTypeParmDecl>(this) ||
791*67e74705SXin Li isa<NonTypeTemplateParmDecl>(this) ||
792*67e74705SXin Li !isa<CXXRecordDecl>(getDeclContext()) ||
793*67e74705SXin Li isInvalidDecl() ||
794*67e74705SXin Li isa<StaticAssertDecl>(this) ||
795*67e74705SXin Li // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
796*67e74705SXin Li // as DeclContext (?).
797*67e74705SXin Li isa<ParmVarDecl>(this) ||
798*67e74705SXin Li // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
799*67e74705SXin Li // AS_none as access specifier.
800*67e74705SXin Li isa<CXXRecordDecl>(this) ||
801*67e74705SXin Li isa<ClassScopeFunctionSpecializationDecl>(this))
802*67e74705SXin Li return true;
803*67e74705SXin Li
804*67e74705SXin Li assert(Access != AS_none &&
805*67e74705SXin Li "Access specifier is AS_none inside a record decl");
806*67e74705SXin Li #endif
807*67e74705SXin Li return true;
808*67e74705SXin Li }
809*67e74705SXin Li
getKind(const Decl * D)810*67e74705SXin Li static Decl::Kind getKind(const Decl *D) { return D->getKind(); }
getKind(const DeclContext * DC)811*67e74705SXin Li static Decl::Kind getKind(const DeclContext *DC) { return DC->getDeclKind(); }
812*67e74705SXin Li
getFunctionType(bool BlocksToo) const813*67e74705SXin Li const FunctionType *Decl::getFunctionType(bool BlocksToo) const {
814*67e74705SXin Li QualType Ty;
815*67e74705SXin Li if (const ValueDecl *D = dyn_cast<ValueDecl>(this))
816*67e74705SXin Li Ty = D->getType();
817*67e74705SXin Li else if (const TypedefNameDecl *D = dyn_cast<TypedefNameDecl>(this))
818*67e74705SXin Li Ty = D->getUnderlyingType();
819*67e74705SXin Li else
820*67e74705SXin Li return nullptr;
821*67e74705SXin Li
822*67e74705SXin Li if (Ty->isFunctionPointerType())
823*67e74705SXin Li Ty = Ty->getAs<PointerType>()->getPointeeType();
824*67e74705SXin Li else if (BlocksToo && Ty->isBlockPointerType())
825*67e74705SXin Li Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
826*67e74705SXin Li
827*67e74705SXin Li return Ty->getAs<FunctionType>();
828*67e74705SXin Li }
829*67e74705SXin Li
830*67e74705SXin Li
831*67e74705SXin Li /// Starting at a given context (a Decl or DeclContext), look for a
832*67e74705SXin Li /// code context that is not a closure (a lambda, block, etc.).
getNonClosureContext(T * D)833*67e74705SXin Li template <class T> static Decl *getNonClosureContext(T *D) {
834*67e74705SXin Li if (getKind(D) == Decl::CXXMethod) {
835*67e74705SXin Li CXXMethodDecl *MD = cast<CXXMethodDecl>(D);
836*67e74705SXin Li if (MD->getOverloadedOperator() == OO_Call &&
837*67e74705SXin Li MD->getParent()->isLambda())
838*67e74705SXin Li return getNonClosureContext(MD->getParent()->getParent());
839*67e74705SXin Li return MD;
840*67e74705SXin Li } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
841*67e74705SXin Li return FD;
842*67e74705SXin Li } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
843*67e74705SXin Li return MD;
844*67e74705SXin Li } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
845*67e74705SXin Li return getNonClosureContext(BD->getParent());
846*67e74705SXin Li } else if (CapturedDecl *CD = dyn_cast<CapturedDecl>(D)) {
847*67e74705SXin Li return getNonClosureContext(CD->getParent());
848*67e74705SXin Li } else {
849*67e74705SXin Li return nullptr;
850*67e74705SXin Li }
851*67e74705SXin Li }
852*67e74705SXin Li
getNonClosureContext()853*67e74705SXin Li Decl *Decl::getNonClosureContext() {
854*67e74705SXin Li return ::getNonClosureContext(this);
855*67e74705SXin Li }
856*67e74705SXin Li
getNonClosureAncestor()857*67e74705SXin Li Decl *DeclContext::getNonClosureAncestor() {
858*67e74705SXin Li return ::getNonClosureContext(this);
859*67e74705SXin Li }
860*67e74705SXin Li
861*67e74705SXin Li //===----------------------------------------------------------------------===//
862*67e74705SXin Li // DeclContext Implementation
863*67e74705SXin Li //===----------------------------------------------------------------------===//
864*67e74705SXin Li
classof(const Decl * D)865*67e74705SXin Li bool DeclContext::classof(const Decl *D) {
866*67e74705SXin Li switch (D->getKind()) {
867*67e74705SXin Li #define DECL(NAME, BASE)
868*67e74705SXin Li #define DECL_CONTEXT(NAME) case Decl::NAME:
869*67e74705SXin Li #define DECL_CONTEXT_BASE(NAME)
870*67e74705SXin Li #include "clang/AST/DeclNodes.inc"
871*67e74705SXin Li return true;
872*67e74705SXin Li default:
873*67e74705SXin Li #define DECL(NAME, BASE)
874*67e74705SXin Li #define DECL_CONTEXT_BASE(NAME) \
875*67e74705SXin Li if (D->getKind() >= Decl::first##NAME && \
876*67e74705SXin Li D->getKind() <= Decl::last##NAME) \
877*67e74705SXin Li return true;
878*67e74705SXin Li #include "clang/AST/DeclNodes.inc"
879*67e74705SXin Li return false;
880*67e74705SXin Li }
881*67e74705SXin Li }
882*67e74705SXin Li
~DeclContext()883*67e74705SXin Li DeclContext::~DeclContext() { }
884*67e74705SXin Li
885*67e74705SXin Li /// \brief Find the parent context of this context that will be
886*67e74705SXin Li /// used for unqualified name lookup.
887*67e74705SXin Li ///
888*67e74705SXin Li /// Generally, the parent lookup context is the semantic context. However, for
889*67e74705SXin Li /// a friend function the parent lookup context is the lexical context, which
890*67e74705SXin Li /// is the class in which the friend is declared.
getLookupParent()891*67e74705SXin Li DeclContext *DeclContext::getLookupParent() {
892*67e74705SXin Li // FIXME: Find a better way to identify friends
893*67e74705SXin Li if (isa<FunctionDecl>(this))
894*67e74705SXin Li if (getParent()->getRedeclContext()->isFileContext() &&
895*67e74705SXin Li getLexicalParent()->getRedeclContext()->isRecord())
896*67e74705SXin Li return getLexicalParent();
897*67e74705SXin Li
898*67e74705SXin Li return getParent();
899*67e74705SXin Li }
900*67e74705SXin Li
isInlineNamespace() const901*67e74705SXin Li bool DeclContext::isInlineNamespace() const {
902*67e74705SXin Li return isNamespace() &&
903*67e74705SXin Li cast<NamespaceDecl>(this)->isInline();
904*67e74705SXin Li }
905*67e74705SXin Li
isStdNamespace() const906*67e74705SXin Li bool DeclContext::isStdNamespace() const {
907*67e74705SXin Li if (!isNamespace())
908*67e74705SXin Li return false;
909*67e74705SXin Li
910*67e74705SXin Li const NamespaceDecl *ND = cast<NamespaceDecl>(this);
911*67e74705SXin Li if (ND->isInline()) {
912*67e74705SXin Li return ND->getParent()->isStdNamespace();
913*67e74705SXin Li }
914*67e74705SXin Li
915*67e74705SXin Li if (!getParent()->getRedeclContext()->isTranslationUnit())
916*67e74705SXin Li return false;
917*67e74705SXin Li
918*67e74705SXin Li const IdentifierInfo *II = ND->getIdentifier();
919*67e74705SXin Li return II && II->isStr("std");
920*67e74705SXin Li }
921*67e74705SXin Li
isDependentContext() const922*67e74705SXin Li bool DeclContext::isDependentContext() const {
923*67e74705SXin Li if (isFileContext())
924*67e74705SXin Li return false;
925*67e74705SXin Li
926*67e74705SXin Li if (isa<ClassTemplatePartialSpecializationDecl>(this))
927*67e74705SXin Li return true;
928*67e74705SXin Li
929*67e74705SXin Li if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this)) {
930*67e74705SXin Li if (Record->getDescribedClassTemplate())
931*67e74705SXin Li return true;
932*67e74705SXin Li
933*67e74705SXin Li if (Record->isDependentLambda())
934*67e74705SXin Li return true;
935*67e74705SXin Li }
936*67e74705SXin Li
937*67e74705SXin Li if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
938*67e74705SXin Li if (Function->getDescribedFunctionTemplate())
939*67e74705SXin Li return true;
940*67e74705SXin Li
941*67e74705SXin Li // Friend function declarations are dependent if their *lexical*
942*67e74705SXin Li // context is dependent.
943*67e74705SXin Li if (cast<Decl>(this)->getFriendObjectKind())
944*67e74705SXin Li return getLexicalParent()->isDependentContext();
945*67e74705SXin Li }
946*67e74705SXin Li
947*67e74705SXin Li // FIXME: A variable template is a dependent context, but is not a
948*67e74705SXin Li // DeclContext. A context within it (such as a lambda-expression)
949*67e74705SXin Li // should be considered dependent.
950*67e74705SXin Li
951*67e74705SXin Li return getParent() && getParent()->isDependentContext();
952*67e74705SXin Li }
953*67e74705SXin Li
isTransparentContext() const954*67e74705SXin Li bool DeclContext::isTransparentContext() const {
955*67e74705SXin Li if (DeclKind == Decl::Enum)
956*67e74705SXin Li return !cast<EnumDecl>(this)->isScoped();
957*67e74705SXin Li else if (DeclKind == Decl::LinkageSpec)
958*67e74705SXin Li return true;
959*67e74705SXin Li
960*67e74705SXin Li return false;
961*67e74705SXin Li }
962*67e74705SXin Li
isLinkageSpecContext(const DeclContext * DC,LinkageSpecDecl::LanguageIDs ID)963*67e74705SXin Li static bool isLinkageSpecContext(const DeclContext *DC,
964*67e74705SXin Li LinkageSpecDecl::LanguageIDs ID) {
965*67e74705SXin Li while (DC->getDeclKind() != Decl::TranslationUnit) {
966*67e74705SXin Li if (DC->getDeclKind() == Decl::LinkageSpec)
967*67e74705SXin Li return cast<LinkageSpecDecl>(DC)->getLanguage() == ID;
968*67e74705SXin Li DC = DC->getLexicalParent();
969*67e74705SXin Li }
970*67e74705SXin Li return false;
971*67e74705SXin Li }
972*67e74705SXin Li
isExternCContext() const973*67e74705SXin Li bool DeclContext::isExternCContext() const {
974*67e74705SXin Li return isLinkageSpecContext(this, clang::LinkageSpecDecl::lang_c);
975*67e74705SXin Li }
976*67e74705SXin Li
isExternCXXContext() const977*67e74705SXin Li bool DeclContext::isExternCXXContext() const {
978*67e74705SXin Li return isLinkageSpecContext(this, clang::LinkageSpecDecl::lang_cxx);
979*67e74705SXin Li }
980*67e74705SXin Li
Encloses(const DeclContext * DC) const981*67e74705SXin Li bool DeclContext::Encloses(const DeclContext *DC) const {
982*67e74705SXin Li if (getPrimaryContext() != this)
983*67e74705SXin Li return getPrimaryContext()->Encloses(DC);
984*67e74705SXin Li
985*67e74705SXin Li for (; DC; DC = DC->getParent())
986*67e74705SXin Li if (DC->getPrimaryContext() == this)
987*67e74705SXin Li return true;
988*67e74705SXin Li return false;
989*67e74705SXin Li }
990*67e74705SXin Li
getPrimaryContext()991*67e74705SXin Li DeclContext *DeclContext::getPrimaryContext() {
992*67e74705SXin Li switch (DeclKind) {
993*67e74705SXin Li case Decl::TranslationUnit:
994*67e74705SXin Li case Decl::ExternCContext:
995*67e74705SXin Li case Decl::LinkageSpec:
996*67e74705SXin Li case Decl::Block:
997*67e74705SXin Li case Decl::Captured:
998*67e74705SXin Li case Decl::OMPDeclareReduction:
999*67e74705SXin Li // There is only one DeclContext for these entities.
1000*67e74705SXin Li return this;
1001*67e74705SXin Li
1002*67e74705SXin Li case Decl::Namespace:
1003*67e74705SXin Li // The original namespace is our primary context.
1004*67e74705SXin Li return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
1005*67e74705SXin Li
1006*67e74705SXin Li case Decl::ObjCMethod:
1007*67e74705SXin Li return this;
1008*67e74705SXin Li
1009*67e74705SXin Li case Decl::ObjCInterface:
1010*67e74705SXin Li if (ObjCInterfaceDecl *Def = cast<ObjCInterfaceDecl>(this)->getDefinition())
1011*67e74705SXin Li return Def;
1012*67e74705SXin Li
1013*67e74705SXin Li return this;
1014*67e74705SXin Li
1015*67e74705SXin Li case Decl::ObjCProtocol:
1016*67e74705SXin Li if (ObjCProtocolDecl *Def = cast<ObjCProtocolDecl>(this)->getDefinition())
1017*67e74705SXin Li return Def;
1018*67e74705SXin Li
1019*67e74705SXin Li return this;
1020*67e74705SXin Li
1021*67e74705SXin Li case Decl::ObjCCategory:
1022*67e74705SXin Li return this;
1023*67e74705SXin Li
1024*67e74705SXin Li case Decl::ObjCImplementation:
1025*67e74705SXin Li case Decl::ObjCCategoryImpl:
1026*67e74705SXin Li return this;
1027*67e74705SXin Li
1028*67e74705SXin Li default:
1029*67e74705SXin Li if (DeclKind >= Decl::firstTag && DeclKind <= Decl::lastTag) {
1030*67e74705SXin Li // If this is a tag type that has a definition or is currently
1031*67e74705SXin Li // being defined, that definition is our primary context.
1032*67e74705SXin Li TagDecl *Tag = cast<TagDecl>(this);
1033*67e74705SXin Li
1034*67e74705SXin Li if (TagDecl *Def = Tag->getDefinition())
1035*67e74705SXin Li return Def;
1036*67e74705SXin Li
1037*67e74705SXin Li if (const TagType *TagTy = dyn_cast<TagType>(Tag->getTypeForDecl())) {
1038*67e74705SXin Li // Note, TagType::getDecl returns the (partial) definition one exists.
1039*67e74705SXin Li TagDecl *PossiblePartialDef = TagTy->getDecl();
1040*67e74705SXin Li if (PossiblePartialDef->isBeingDefined())
1041*67e74705SXin Li return PossiblePartialDef;
1042*67e74705SXin Li } else {
1043*67e74705SXin Li assert(isa<InjectedClassNameType>(Tag->getTypeForDecl()));
1044*67e74705SXin Li }
1045*67e74705SXin Li
1046*67e74705SXin Li return Tag;
1047*67e74705SXin Li }
1048*67e74705SXin Li
1049*67e74705SXin Li assert(DeclKind >= Decl::firstFunction && DeclKind <= Decl::lastFunction &&
1050*67e74705SXin Li "Unknown DeclContext kind");
1051*67e74705SXin Li return this;
1052*67e74705SXin Li }
1053*67e74705SXin Li }
1054*67e74705SXin Li
1055*67e74705SXin Li void
collectAllContexts(SmallVectorImpl<DeclContext * > & Contexts)1056*67e74705SXin Li DeclContext::collectAllContexts(SmallVectorImpl<DeclContext *> &Contexts){
1057*67e74705SXin Li Contexts.clear();
1058*67e74705SXin Li
1059*67e74705SXin Li if (DeclKind != Decl::Namespace) {
1060*67e74705SXin Li Contexts.push_back(this);
1061*67e74705SXin Li return;
1062*67e74705SXin Li }
1063*67e74705SXin Li
1064*67e74705SXin Li NamespaceDecl *Self = static_cast<NamespaceDecl *>(this);
1065*67e74705SXin Li for (NamespaceDecl *N = Self->getMostRecentDecl(); N;
1066*67e74705SXin Li N = N->getPreviousDecl())
1067*67e74705SXin Li Contexts.push_back(N);
1068*67e74705SXin Li
1069*67e74705SXin Li std::reverse(Contexts.begin(), Contexts.end());
1070*67e74705SXin Li }
1071*67e74705SXin Li
1072*67e74705SXin Li std::pair<Decl *, Decl *>
BuildDeclChain(ArrayRef<Decl * > Decls,bool FieldsAlreadyLoaded)1073*67e74705SXin Li DeclContext::BuildDeclChain(ArrayRef<Decl*> Decls,
1074*67e74705SXin Li bool FieldsAlreadyLoaded) {
1075*67e74705SXin Li // Build up a chain of declarations via the Decl::NextInContextAndBits field.
1076*67e74705SXin Li Decl *FirstNewDecl = nullptr;
1077*67e74705SXin Li Decl *PrevDecl = nullptr;
1078*67e74705SXin Li for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
1079*67e74705SXin Li if (FieldsAlreadyLoaded && isa<FieldDecl>(Decls[I]))
1080*67e74705SXin Li continue;
1081*67e74705SXin Li
1082*67e74705SXin Li Decl *D = Decls[I];
1083*67e74705SXin Li if (PrevDecl)
1084*67e74705SXin Li PrevDecl->NextInContextAndBits.setPointer(D);
1085*67e74705SXin Li else
1086*67e74705SXin Li FirstNewDecl = D;
1087*67e74705SXin Li
1088*67e74705SXin Li PrevDecl = D;
1089*67e74705SXin Li }
1090*67e74705SXin Li
1091*67e74705SXin Li return std::make_pair(FirstNewDecl, PrevDecl);
1092*67e74705SXin Li }
1093*67e74705SXin Li
1094*67e74705SXin Li /// \brief We have just acquired external visible storage, and we already have
1095*67e74705SXin Li /// built a lookup map. For every name in the map, pull in the new names from
1096*67e74705SXin Li /// the external storage.
reconcileExternalVisibleStorage() const1097*67e74705SXin Li void DeclContext::reconcileExternalVisibleStorage() const {
1098*67e74705SXin Li assert(NeedToReconcileExternalVisibleStorage && LookupPtr);
1099*67e74705SXin Li NeedToReconcileExternalVisibleStorage = false;
1100*67e74705SXin Li
1101*67e74705SXin Li for (auto &Lookup : *LookupPtr)
1102*67e74705SXin Li Lookup.second.setHasExternalDecls();
1103*67e74705SXin Li }
1104*67e74705SXin Li
1105*67e74705SXin Li /// \brief Load the declarations within this lexical storage from an
1106*67e74705SXin Li /// external source.
1107*67e74705SXin Li /// \return \c true if any declarations were added.
1108*67e74705SXin Li bool
LoadLexicalDeclsFromExternalStorage() const1109*67e74705SXin Li DeclContext::LoadLexicalDeclsFromExternalStorage() const {
1110*67e74705SXin Li ExternalASTSource *Source = getParentASTContext().getExternalSource();
1111*67e74705SXin Li assert(hasExternalLexicalStorage() && Source && "No external storage?");
1112*67e74705SXin Li
1113*67e74705SXin Li // Notify that we have a DeclContext that is initializing.
1114*67e74705SXin Li ExternalASTSource::Deserializing ADeclContext(Source);
1115*67e74705SXin Li
1116*67e74705SXin Li // Load the external declarations, if any.
1117*67e74705SXin Li SmallVector<Decl*, 64> Decls;
1118*67e74705SXin Li ExternalLexicalStorage = false;
1119*67e74705SXin Li Source->FindExternalLexicalDecls(this, Decls);
1120*67e74705SXin Li
1121*67e74705SXin Li if (Decls.empty())
1122*67e74705SXin Li return false;
1123*67e74705SXin Li
1124*67e74705SXin Li // We may have already loaded just the fields of this record, in which case
1125*67e74705SXin Li // we need to ignore them.
1126*67e74705SXin Li bool FieldsAlreadyLoaded = false;
1127*67e74705SXin Li if (const RecordDecl *RD = dyn_cast<RecordDecl>(this))
1128*67e74705SXin Li FieldsAlreadyLoaded = RD->LoadedFieldsFromExternalStorage;
1129*67e74705SXin Li
1130*67e74705SXin Li // Splice the newly-read declarations into the beginning of the list
1131*67e74705SXin Li // of declarations.
1132*67e74705SXin Li Decl *ExternalFirst, *ExternalLast;
1133*67e74705SXin Li std::tie(ExternalFirst, ExternalLast) =
1134*67e74705SXin Li BuildDeclChain(Decls, FieldsAlreadyLoaded);
1135*67e74705SXin Li ExternalLast->NextInContextAndBits.setPointer(FirstDecl);
1136*67e74705SXin Li FirstDecl = ExternalFirst;
1137*67e74705SXin Li if (!LastDecl)
1138*67e74705SXin Li LastDecl = ExternalLast;
1139*67e74705SXin Li return true;
1140*67e74705SXin Li }
1141*67e74705SXin Li
1142*67e74705SXin Li DeclContext::lookup_result
SetNoExternalVisibleDeclsForName(const DeclContext * DC,DeclarationName Name)1143*67e74705SXin Li ExternalASTSource::SetNoExternalVisibleDeclsForName(const DeclContext *DC,
1144*67e74705SXin Li DeclarationName Name) {
1145*67e74705SXin Li ASTContext &Context = DC->getParentASTContext();
1146*67e74705SXin Li StoredDeclsMap *Map;
1147*67e74705SXin Li if (!(Map = DC->LookupPtr))
1148*67e74705SXin Li Map = DC->CreateStoredDeclsMap(Context);
1149*67e74705SXin Li if (DC->NeedToReconcileExternalVisibleStorage)
1150*67e74705SXin Li DC->reconcileExternalVisibleStorage();
1151*67e74705SXin Li
1152*67e74705SXin Li (*Map)[Name].removeExternalDecls();
1153*67e74705SXin Li
1154*67e74705SXin Li return DeclContext::lookup_result();
1155*67e74705SXin Li }
1156*67e74705SXin Li
1157*67e74705SXin Li DeclContext::lookup_result
SetExternalVisibleDeclsForName(const DeclContext * DC,DeclarationName Name,ArrayRef<NamedDecl * > Decls)1158*67e74705SXin Li ExternalASTSource::SetExternalVisibleDeclsForName(const DeclContext *DC,
1159*67e74705SXin Li DeclarationName Name,
1160*67e74705SXin Li ArrayRef<NamedDecl*> Decls) {
1161*67e74705SXin Li ASTContext &Context = DC->getParentASTContext();
1162*67e74705SXin Li StoredDeclsMap *Map;
1163*67e74705SXin Li if (!(Map = DC->LookupPtr))
1164*67e74705SXin Li Map = DC->CreateStoredDeclsMap(Context);
1165*67e74705SXin Li if (DC->NeedToReconcileExternalVisibleStorage)
1166*67e74705SXin Li DC->reconcileExternalVisibleStorage();
1167*67e74705SXin Li
1168*67e74705SXin Li StoredDeclsList &List = (*Map)[Name];
1169*67e74705SXin Li
1170*67e74705SXin Li // Clear out any old external visible declarations, to avoid quadratic
1171*67e74705SXin Li // performance in the redeclaration checks below.
1172*67e74705SXin Li List.removeExternalDecls();
1173*67e74705SXin Li
1174*67e74705SXin Li if (!List.isNull()) {
1175*67e74705SXin Li // We have both existing declarations and new declarations for this name.
1176*67e74705SXin Li // Some of the declarations may simply replace existing ones. Handle those
1177*67e74705SXin Li // first.
1178*67e74705SXin Li llvm::SmallVector<unsigned, 8> Skip;
1179*67e74705SXin Li for (unsigned I = 0, N = Decls.size(); I != N; ++I)
1180*67e74705SXin Li if (List.HandleRedeclaration(Decls[I], /*IsKnownNewer*/false))
1181*67e74705SXin Li Skip.push_back(I);
1182*67e74705SXin Li Skip.push_back(Decls.size());
1183*67e74705SXin Li
1184*67e74705SXin Li // Add in any new declarations.
1185*67e74705SXin Li unsigned SkipPos = 0;
1186*67e74705SXin Li for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
1187*67e74705SXin Li if (I == Skip[SkipPos])
1188*67e74705SXin Li ++SkipPos;
1189*67e74705SXin Li else
1190*67e74705SXin Li List.AddSubsequentDecl(Decls[I]);
1191*67e74705SXin Li }
1192*67e74705SXin Li } else {
1193*67e74705SXin Li // Convert the array to a StoredDeclsList.
1194*67e74705SXin Li for (ArrayRef<NamedDecl*>::iterator
1195*67e74705SXin Li I = Decls.begin(), E = Decls.end(); I != E; ++I) {
1196*67e74705SXin Li if (List.isNull())
1197*67e74705SXin Li List.setOnlyValue(*I);
1198*67e74705SXin Li else
1199*67e74705SXin Li List.AddSubsequentDecl(*I);
1200*67e74705SXin Li }
1201*67e74705SXin Li }
1202*67e74705SXin Li
1203*67e74705SXin Li return List.getLookupResult();
1204*67e74705SXin Li }
1205*67e74705SXin Li
decls_begin() const1206*67e74705SXin Li DeclContext::decl_iterator DeclContext::decls_begin() const {
1207*67e74705SXin Li if (hasExternalLexicalStorage())
1208*67e74705SXin Li LoadLexicalDeclsFromExternalStorage();
1209*67e74705SXin Li return decl_iterator(FirstDecl);
1210*67e74705SXin Li }
1211*67e74705SXin Li
decls_empty() const1212*67e74705SXin Li bool DeclContext::decls_empty() const {
1213*67e74705SXin Li if (hasExternalLexicalStorage())
1214*67e74705SXin Li LoadLexicalDeclsFromExternalStorage();
1215*67e74705SXin Li
1216*67e74705SXin Li return !FirstDecl;
1217*67e74705SXin Li }
1218*67e74705SXin Li
containsDecl(Decl * D) const1219*67e74705SXin Li bool DeclContext::containsDecl(Decl *D) const {
1220*67e74705SXin Li return (D->getLexicalDeclContext() == this &&
1221*67e74705SXin Li (D->NextInContextAndBits.getPointer() || D == LastDecl));
1222*67e74705SXin Li }
1223*67e74705SXin Li
removeDecl(Decl * D)1224*67e74705SXin Li void DeclContext::removeDecl(Decl *D) {
1225*67e74705SXin Li assert(D->getLexicalDeclContext() == this &&
1226*67e74705SXin Li "decl being removed from non-lexical context");
1227*67e74705SXin Li assert((D->NextInContextAndBits.getPointer() || D == LastDecl) &&
1228*67e74705SXin Li "decl is not in decls list");
1229*67e74705SXin Li
1230*67e74705SXin Li // Remove D from the decl chain. This is O(n) but hopefully rare.
1231*67e74705SXin Li if (D == FirstDecl) {
1232*67e74705SXin Li if (D == LastDecl)
1233*67e74705SXin Li FirstDecl = LastDecl = nullptr;
1234*67e74705SXin Li else
1235*67e74705SXin Li FirstDecl = D->NextInContextAndBits.getPointer();
1236*67e74705SXin Li } else {
1237*67e74705SXin Li for (Decl *I = FirstDecl; true; I = I->NextInContextAndBits.getPointer()) {
1238*67e74705SXin Li assert(I && "decl not found in linked list");
1239*67e74705SXin Li if (I->NextInContextAndBits.getPointer() == D) {
1240*67e74705SXin Li I->NextInContextAndBits.setPointer(D->NextInContextAndBits.getPointer());
1241*67e74705SXin Li if (D == LastDecl) LastDecl = I;
1242*67e74705SXin Li break;
1243*67e74705SXin Li }
1244*67e74705SXin Li }
1245*67e74705SXin Li }
1246*67e74705SXin Li
1247*67e74705SXin Li // Mark that D is no longer in the decl chain.
1248*67e74705SXin Li D->NextInContextAndBits.setPointer(nullptr);
1249*67e74705SXin Li
1250*67e74705SXin Li // Remove D from the lookup table if necessary.
1251*67e74705SXin Li if (isa<NamedDecl>(D)) {
1252*67e74705SXin Li NamedDecl *ND = cast<NamedDecl>(D);
1253*67e74705SXin Li
1254*67e74705SXin Li // Remove only decls that have a name
1255*67e74705SXin Li if (!ND->getDeclName()) return;
1256*67e74705SXin Li
1257*67e74705SXin Li auto *DC = this;
1258*67e74705SXin Li do {
1259*67e74705SXin Li StoredDeclsMap *Map = DC->getPrimaryContext()->LookupPtr;
1260*67e74705SXin Li if (Map) {
1261*67e74705SXin Li StoredDeclsMap::iterator Pos = Map->find(ND->getDeclName());
1262*67e74705SXin Li assert(Pos != Map->end() && "no lookup entry for decl");
1263*67e74705SXin Li if (Pos->second.getAsVector() || Pos->second.getAsDecl() == ND)
1264*67e74705SXin Li Pos->second.remove(ND);
1265*67e74705SXin Li }
1266*67e74705SXin Li } while (DC->isTransparentContext() && (DC = DC->getParent()));
1267*67e74705SXin Li }
1268*67e74705SXin Li }
1269*67e74705SXin Li
addHiddenDecl(Decl * D)1270*67e74705SXin Li void DeclContext::addHiddenDecl(Decl *D) {
1271*67e74705SXin Li assert(D->getLexicalDeclContext() == this &&
1272*67e74705SXin Li "Decl inserted into wrong lexical context");
1273*67e74705SXin Li assert(!D->getNextDeclInContext() && D != LastDecl &&
1274*67e74705SXin Li "Decl already inserted into a DeclContext");
1275*67e74705SXin Li
1276*67e74705SXin Li if (FirstDecl) {
1277*67e74705SXin Li LastDecl->NextInContextAndBits.setPointer(D);
1278*67e74705SXin Li LastDecl = D;
1279*67e74705SXin Li } else {
1280*67e74705SXin Li FirstDecl = LastDecl = D;
1281*67e74705SXin Li }
1282*67e74705SXin Li
1283*67e74705SXin Li // Notify a C++ record declaration that we've added a member, so it can
1284*67e74705SXin Li // update its class-specific state.
1285*67e74705SXin Li if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
1286*67e74705SXin Li Record->addedMember(D);
1287*67e74705SXin Li
1288*67e74705SXin Li // If this is a newly-created (not de-serialized) import declaration, wire
1289*67e74705SXin Li // it in to the list of local import declarations.
1290*67e74705SXin Li if (!D->isFromASTFile()) {
1291*67e74705SXin Li if (ImportDecl *Import = dyn_cast<ImportDecl>(D))
1292*67e74705SXin Li D->getASTContext().addedLocalImportDecl(Import);
1293*67e74705SXin Li }
1294*67e74705SXin Li }
1295*67e74705SXin Li
addDecl(Decl * D)1296*67e74705SXin Li void DeclContext::addDecl(Decl *D) {
1297*67e74705SXin Li addHiddenDecl(D);
1298*67e74705SXin Li
1299*67e74705SXin Li if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1300*67e74705SXin Li ND->getDeclContext()->getPrimaryContext()->
1301*67e74705SXin Li makeDeclVisibleInContextWithFlags(ND, false, true);
1302*67e74705SXin Li }
1303*67e74705SXin Li
addDeclInternal(Decl * D)1304*67e74705SXin Li void DeclContext::addDeclInternal(Decl *D) {
1305*67e74705SXin Li addHiddenDecl(D);
1306*67e74705SXin Li
1307*67e74705SXin Li if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1308*67e74705SXin Li ND->getDeclContext()->getPrimaryContext()->
1309*67e74705SXin Li makeDeclVisibleInContextWithFlags(ND, true, true);
1310*67e74705SXin Li }
1311*67e74705SXin Li
1312*67e74705SXin Li /// shouldBeHidden - Determine whether a declaration which was declared
1313*67e74705SXin Li /// within its semantic context should be invisible to qualified name lookup.
shouldBeHidden(NamedDecl * D)1314*67e74705SXin Li static bool shouldBeHidden(NamedDecl *D) {
1315*67e74705SXin Li // Skip unnamed declarations.
1316*67e74705SXin Li if (!D->getDeclName())
1317*67e74705SXin Li return true;
1318*67e74705SXin Li
1319*67e74705SXin Li // Skip entities that can't be found by name lookup into a particular
1320*67e74705SXin Li // context.
1321*67e74705SXin Li if ((D->getIdentifierNamespace() == 0 && !isa<UsingDirectiveDecl>(D)) ||
1322*67e74705SXin Li D->isTemplateParameter())
1323*67e74705SXin Li return true;
1324*67e74705SXin Li
1325*67e74705SXin Li // Skip template specializations.
1326*67e74705SXin Li // FIXME: This feels like a hack. Should DeclarationName support
1327*67e74705SXin Li // template-ids, or is there a better way to keep specializations
1328*67e74705SXin Li // from being visible?
1329*67e74705SXin Li if (isa<ClassTemplateSpecializationDecl>(D))
1330*67e74705SXin Li return true;
1331*67e74705SXin Li if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1332*67e74705SXin Li if (FD->isFunctionTemplateSpecialization())
1333*67e74705SXin Li return true;
1334*67e74705SXin Li
1335*67e74705SXin Li return false;
1336*67e74705SXin Li }
1337*67e74705SXin Li
1338*67e74705SXin Li /// buildLookup - Build the lookup data structure with all of the
1339*67e74705SXin Li /// declarations in this DeclContext (and any other contexts linked
1340*67e74705SXin Li /// to it or transparent contexts nested within it) and return it.
1341*67e74705SXin Li ///
1342*67e74705SXin Li /// Note that the produced map may miss out declarations from an
1343*67e74705SXin Li /// external source. If it does, those entries will be marked with
1344*67e74705SXin Li /// the 'hasExternalDecls' flag.
buildLookup()1345*67e74705SXin Li StoredDeclsMap *DeclContext::buildLookup() {
1346*67e74705SXin Li assert(this == getPrimaryContext() && "buildLookup called on non-primary DC");
1347*67e74705SXin Li
1348*67e74705SXin Li if (!HasLazyLocalLexicalLookups && !HasLazyExternalLexicalLookups)
1349*67e74705SXin Li return LookupPtr;
1350*67e74705SXin Li
1351*67e74705SXin Li SmallVector<DeclContext *, 2> Contexts;
1352*67e74705SXin Li collectAllContexts(Contexts);
1353*67e74705SXin Li
1354*67e74705SXin Li if (HasLazyExternalLexicalLookups) {
1355*67e74705SXin Li HasLazyExternalLexicalLookups = false;
1356*67e74705SXin Li for (auto *DC : Contexts) {
1357*67e74705SXin Li if (DC->hasExternalLexicalStorage())
1358*67e74705SXin Li HasLazyLocalLexicalLookups |=
1359*67e74705SXin Li DC->LoadLexicalDeclsFromExternalStorage();
1360*67e74705SXin Li }
1361*67e74705SXin Li
1362*67e74705SXin Li if (!HasLazyLocalLexicalLookups)
1363*67e74705SXin Li return LookupPtr;
1364*67e74705SXin Li }
1365*67e74705SXin Li
1366*67e74705SXin Li for (auto *DC : Contexts)
1367*67e74705SXin Li buildLookupImpl(DC, hasExternalVisibleStorage());
1368*67e74705SXin Li
1369*67e74705SXin Li // We no longer have any lazy decls.
1370*67e74705SXin Li HasLazyLocalLexicalLookups = false;
1371*67e74705SXin Li return LookupPtr;
1372*67e74705SXin Li }
1373*67e74705SXin Li
1374*67e74705SXin Li /// buildLookupImpl - Build part of the lookup data structure for the
1375*67e74705SXin Li /// declarations contained within DCtx, which will either be this
1376*67e74705SXin Li /// DeclContext, a DeclContext linked to it, or a transparent context
1377*67e74705SXin Li /// nested within it.
buildLookupImpl(DeclContext * DCtx,bool Internal)1378*67e74705SXin Li void DeclContext::buildLookupImpl(DeclContext *DCtx, bool Internal) {
1379*67e74705SXin Li for (Decl *D : DCtx->noload_decls()) {
1380*67e74705SXin Li // Insert this declaration into the lookup structure, but only if
1381*67e74705SXin Li // it's semantically within its decl context. Any other decls which
1382*67e74705SXin Li // should be found in this context are added eagerly.
1383*67e74705SXin Li //
1384*67e74705SXin Li // If it's from an AST file, don't add it now. It'll get handled by
1385*67e74705SXin Li // FindExternalVisibleDeclsByName if needed. Exception: if we're not
1386*67e74705SXin Li // in C++, we do not track external visible decls for the TU, so in
1387*67e74705SXin Li // that case we need to collect them all here.
1388*67e74705SXin Li if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1389*67e74705SXin Li if (ND->getDeclContext() == DCtx && !shouldBeHidden(ND) &&
1390*67e74705SXin Li (!ND->isFromASTFile() ||
1391*67e74705SXin Li (isTranslationUnit() &&
1392*67e74705SXin Li !getParentASTContext().getLangOpts().CPlusPlus)))
1393*67e74705SXin Li makeDeclVisibleInContextImpl(ND, Internal);
1394*67e74705SXin Li
1395*67e74705SXin Li // If this declaration is itself a transparent declaration context
1396*67e74705SXin Li // or inline namespace, add the members of this declaration of that
1397*67e74705SXin Li // context (recursively).
1398*67e74705SXin Li if (DeclContext *InnerCtx = dyn_cast<DeclContext>(D))
1399*67e74705SXin Li if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace())
1400*67e74705SXin Li buildLookupImpl(InnerCtx, Internal);
1401*67e74705SXin Li }
1402*67e74705SXin Li }
1403*67e74705SXin Li
1404*67e74705SXin Li NamedDecl *const DeclContextLookupResult::SingleElementDummyList = nullptr;
1405*67e74705SXin Li
1406*67e74705SXin Li DeclContext::lookup_result
lookup(DeclarationName Name) const1407*67e74705SXin Li DeclContext::lookup(DeclarationName Name) const {
1408*67e74705SXin Li assert(DeclKind != Decl::LinkageSpec &&
1409*67e74705SXin Li "Should not perform lookups into linkage specs!");
1410*67e74705SXin Li
1411*67e74705SXin Li const DeclContext *PrimaryContext = getPrimaryContext();
1412*67e74705SXin Li if (PrimaryContext != this)
1413*67e74705SXin Li return PrimaryContext->lookup(Name);
1414*67e74705SXin Li
1415*67e74705SXin Li // If we have an external source, ensure that any later redeclarations of this
1416*67e74705SXin Li // context have been loaded, since they may add names to the result of this
1417*67e74705SXin Li // lookup (or add external visible storage).
1418*67e74705SXin Li ExternalASTSource *Source = getParentASTContext().getExternalSource();
1419*67e74705SXin Li if (Source)
1420*67e74705SXin Li (void)cast<Decl>(this)->getMostRecentDecl();
1421*67e74705SXin Li
1422*67e74705SXin Li if (hasExternalVisibleStorage()) {
1423*67e74705SXin Li assert(Source && "external visible storage but no external source?");
1424*67e74705SXin Li
1425*67e74705SXin Li if (NeedToReconcileExternalVisibleStorage)
1426*67e74705SXin Li reconcileExternalVisibleStorage();
1427*67e74705SXin Li
1428*67e74705SXin Li StoredDeclsMap *Map = LookupPtr;
1429*67e74705SXin Li
1430*67e74705SXin Li if (HasLazyLocalLexicalLookups || HasLazyExternalLexicalLookups)
1431*67e74705SXin Li // FIXME: Make buildLookup const?
1432*67e74705SXin Li Map = const_cast<DeclContext*>(this)->buildLookup();
1433*67e74705SXin Li
1434*67e74705SXin Li if (!Map)
1435*67e74705SXin Li Map = CreateStoredDeclsMap(getParentASTContext());
1436*67e74705SXin Li
1437*67e74705SXin Li // If we have a lookup result with no external decls, we are done.
1438*67e74705SXin Li std::pair<StoredDeclsMap::iterator, bool> R =
1439*67e74705SXin Li Map->insert(std::make_pair(Name, StoredDeclsList()));
1440*67e74705SXin Li if (!R.second && !R.first->second.hasExternalDecls())
1441*67e74705SXin Li return R.first->second.getLookupResult();
1442*67e74705SXin Li
1443*67e74705SXin Li if (Source->FindExternalVisibleDeclsByName(this, Name) || !R.second) {
1444*67e74705SXin Li if (StoredDeclsMap *Map = LookupPtr) {
1445*67e74705SXin Li StoredDeclsMap::iterator I = Map->find(Name);
1446*67e74705SXin Li if (I != Map->end())
1447*67e74705SXin Li return I->second.getLookupResult();
1448*67e74705SXin Li }
1449*67e74705SXin Li }
1450*67e74705SXin Li
1451*67e74705SXin Li return lookup_result();
1452*67e74705SXin Li }
1453*67e74705SXin Li
1454*67e74705SXin Li StoredDeclsMap *Map = LookupPtr;
1455*67e74705SXin Li if (HasLazyLocalLexicalLookups || HasLazyExternalLexicalLookups)
1456*67e74705SXin Li Map = const_cast<DeclContext*>(this)->buildLookup();
1457*67e74705SXin Li
1458*67e74705SXin Li if (!Map)
1459*67e74705SXin Li return lookup_result();
1460*67e74705SXin Li
1461*67e74705SXin Li StoredDeclsMap::iterator I = Map->find(Name);
1462*67e74705SXin Li if (I == Map->end())
1463*67e74705SXin Li return lookup_result();
1464*67e74705SXin Li
1465*67e74705SXin Li return I->second.getLookupResult();
1466*67e74705SXin Li }
1467*67e74705SXin Li
1468*67e74705SXin Li DeclContext::lookup_result
noload_lookup(DeclarationName Name)1469*67e74705SXin Li DeclContext::noload_lookup(DeclarationName Name) {
1470*67e74705SXin Li assert(DeclKind != Decl::LinkageSpec &&
1471*67e74705SXin Li "Should not perform lookups into linkage specs!");
1472*67e74705SXin Li
1473*67e74705SXin Li DeclContext *PrimaryContext = getPrimaryContext();
1474*67e74705SXin Li if (PrimaryContext != this)
1475*67e74705SXin Li return PrimaryContext->noload_lookup(Name);
1476*67e74705SXin Li
1477*67e74705SXin Li // If we have any lazy lexical declarations not in our lookup map, add them
1478*67e74705SXin Li // now. Don't import any external declarations, not even if we know we have
1479*67e74705SXin Li // some missing from the external visible lookups.
1480*67e74705SXin Li if (HasLazyLocalLexicalLookups) {
1481*67e74705SXin Li SmallVector<DeclContext *, 2> Contexts;
1482*67e74705SXin Li collectAllContexts(Contexts);
1483*67e74705SXin Li for (unsigned I = 0, N = Contexts.size(); I != N; ++I)
1484*67e74705SXin Li buildLookupImpl(Contexts[I], hasExternalVisibleStorage());
1485*67e74705SXin Li HasLazyLocalLexicalLookups = false;
1486*67e74705SXin Li }
1487*67e74705SXin Li
1488*67e74705SXin Li StoredDeclsMap *Map = LookupPtr;
1489*67e74705SXin Li if (!Map)
1490*67e74705SXin Li return lookup_result();
1491*67e74705SXin Li
1492*67e74705SXin Li StoredDeclsMap::iterator I = Map->find(Name);
1493*67e74705SXin Li return I != Map->end() ? I->second.getLookupResult()
1494*67e74705SXin Li : lookup_result();
1495*67e74705SXin Li }
1496*67e74705SXin Li
localUncachedLookup(DeclarationName Name,SmallVectorImpl<NamedDecl * > & Results)1497*67e74705SXin Li void DeclContext::localUncachedLookup(DeclarationName Name,
1498*67e74705SXin Li SmallVectorImpl<NamedDecl *> &Results) {
1499*67e74705SXin Li Results.clear();
1500*67e74705SXin Li
1501*67e74705SXin Li // If there's no external storage, just perform a normal lookup and copy
1502*67e74705SXin Li // the results.
1503*67e74705SXin Li if (!hasExternalVisibleStorage() && !hasExternalLexicalStorage() && Name) {
1504*67e74705SXin Li lookup_result LookupResults = lookup(Name);
1505*67e74705SXin Li Results.insert(Results.end(), LookupResults.begin(), LookupResults.end());
1506*67e74705SXin Li return;
1507*67e74705SXin Li }
1508*67e74705SXin Li
1509*67e74705SXin Li // If we have a lookup table, check there first. Maybe we'll get lucky.
1510*67e74705SXin Li // FIXME: Should we be checking these flags on the primary context?
1511*67e74705SXin Li if (Name && !HasLazyLocalLexicalLookups && !HasLazyExternalLexicalLookups) {
1512*67e74705SXin Li if (StoredDeclsMap *Map = LookupPtr) {
1513*67e74705SXin Li StoredDeclsMap::iterator Pos = Map->find(Name);
1514*67e74705SXin Li if (Pos != Map->end()) {
1515*67e74705SXin Li Results.insert(Results.end(),
1516*67e74705SXin Li Pos->second.getLookupResult().begin(),
1517*67e74705SXin Li Pos->second.getLookupResult().end());
1518*67e74705SXin Li return;
1519*67e74705SXin Li }
1520*67e74705SXin Li }
1521*67e74705SXin Li }
1522*67e74705SXin Li
1523*67e74705SXin Li // Slow case: grovel through the declarations in our chain looking for
1524*67e74705SXin Li // matches.
1525*67e74705SXin Li // FIXME: If we have lazy external declarations, this will not find them!
1526*67e74705SXin Li // FIXME: Should we CollectAllContexts and walk them all here?
1527*67e74705SXin Li for (Decl *D = FirstDecl; D; D = D->getNextDeclInContext()) {
1528*67e74705SXin Li if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
1529*67e74705SXin Li if (ND->getDeclName() == Name)
1530*67e74705SXin Li Results.push_back(ND);
1531*67e74705SXin Li }
1532*67e74705SXin Li }
1533*67e74705SXin Li
getRedeclContext()1534*67e74705SXin Li DeclContext *DeclContext::getRedeclContext() {
1535*67e74705SXin Li DeclContext *Ctx = this;
1536*67e74705SXin Li // Skip through transparent contexts.
1537*67e74705SXin Li while (Ctx->isTransparentContext())
1538*67e74705SXin Li Ctx = Ctx->getParent();
1539*67e74705SXin Li return Ctx;
1540*67e74705SXin Li }
1541*67e74705SXin Li
getEnclosingNamespaceContext()1542*67e74705SXin Li DeclContext *DeclContext::getEnclosingNamespaceContext() {
1543*67e74705SXin Li DeclContext *Ctx = this;
1544*67e74705SXin Li // Skip through non-namespace, non-translation-unit contexts.
1545*67e74705SXin Li while (!Ctx->isFileContext())
1546*67e74705SXin Li Ctx = Ctx->getParent();
1547*67e74705SXin Li return Ctx->getPrimaryContext();
1548*67e74705SXin Li }
1549*67e74705SXin Li
getOuterLexicalRecordContext()1550*67e74705SXin Li RecordDecl *DeclContext::getOuterLexicalRecordContext() {
1551*67e74705SXin Li // Loop until we find a non-record context.
1552*67e74705SXin Li RecordDecl *OutermostRD = nullptr;
1553*67e74705SXin Li DeclContext *DC = this;
1554*67e74705SXin Li while (DC->isRecord()) {
1555*67e74705SXin Li OutermostRD = cast<RecordDecl>(DC);
1556*67e74705SXin Li DC = DC->getLexicalParent();
1557*67e74705SXin Li }
1558*67e74705SXin Li return OutermostRD;
1559*67e74705SXin Li }
1560*67e74705SXin Li
InEnclosingNamespaceSetOf(const DeclContext * O) const1561*67e74705SXin Li bool DeclContext::InEnclosingNamespaceSetOf(const DeclContext *O) const {
1562*67e74705SXin Li // For non-file contexts, this is equivalent to Equals.
1563*67e74705SXin Li if (!isFileContext())
1564*67e74705SXin Li return O->Equals(this);
1565*67e74705SXin Li
1566*67e74705SXin Li do {
1567*67e74705SXin Li if (O->Equals(this))
1568*67e74705SXin Li return true;
1569*67e74705SXin Li
1570*67e74705SXin Li const NamespaceDecl *NS = dyn_cast<NamespaceDecl>(O);
1571*67e74705SXin Li if (!NS || !NS->isInline())
1572*67e74705SXin Li break;
1573*67e74705SXin Li O = NS->getParent();
1574*67e74705SXin Li } while (O);
1575*67e74705SXin Li
1576*67e74705SXin Li return false;
1577*67e74705SXin Li }
1578*67e74705SXin Li
makeDeclVisibleInContext(NamedDecl * D)1579*67e74705SXin Li void DeclContext::makeDeclVisibleInContext(NamedDecl *D) {
1580*67e74705SXin Li DeclContext *PrimaryDC = this->getPrimaryContext();
1581*67e74705SXin Li DeclContext *DeclDC = D->getDeclContext()->getPrimaryContext();
1582*67e74705SXin Li // If the decl is being added outside of its semantic decl context, we
1583*67e74705SXin Li // need to ensure that we eagerly build the lookup information for it.
1584*67e74705SXin Li PrimaryDC->makeDeclVisibleInContextWithFlags(D, false, PrimaryDC == DeclDC);
1585*67e74705SXin Li }
1586*67e74705SXin Li
makeDeclVisibleInContextWithFlags(NamedDecl * D,bool Internal,bool Recoverable)1587*67e74705SXin Li void DeclContext::makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal,
1588*67e74705SXin Li bool Recoverable) {
1589*67e74705SXin Li assert(this == getPrimaryContext() && "expected a primary DC");
1590*67e74705SXin Li
1591*67e74705SXin Li if (!isLookupContext()) {
1592*67e74705SXin Li if (isTransparentContext())
1593*67e74705SXin Li getParent()->getPrimaryContext()
1594*67e74705SXin Li ->makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
1595*67e74705SXin Li return;
1596*67e74705SXin Li }
1597*67e74705SXin Li
1598*67e74705SXin Li // Skip declarations which should be invisible to name lookup.
1599*67e74705SXin Li if (shouldBeHidden(D))
1600*67e74705SXin Li return;
1601*67e74705SXin Li
1602*67e74705SXin Li // If we already have a lookup data structure, perform the insertion into
1603*67e74705SXin Li // it. If we might have externally-stored decls with this name, look them
1604*67e74705SXin Li // up and perform the insertion. If this decl was declared outside its
1605*67e74705SXin Li // semantic context, buildLookup won't add it, so add it now.
1606*67e74705SXin Li //
1607*67e74705SXin Li // FIXME: As a performance hack, don't add such decls into the translation
1608*67e74705SXin Li // unit unless we're in C++, since qualified lookup into the TU is never
1609*67e74705SXin Li // performed.
1610*67e74705SXin Li if (LookupPtr || hasExternalVisibleStorage() ||
1611*67e74705SXin Li ((!Recoverable || D->getDeclContext() != D->getLexicalDeclContext()) &&
1612*67e74705SXin Li (getParentASTContext().getLangOpts().CPlusPlus ||
1613*67e74705SXin Li !isTranslationUnit()))) {
1614*67e74705SXin Li // If we have lazily omitted any decls, they might have the same name as
1615*67e74705SXin Li // the decl which we are adding, so build a full lookup table before adding
1616*67e74705SXin Li // this decl.
1617*67e74705SXin Li buildLookup();
1618*67e74705SXin Li makeDeclVisibleInContextImpl(D, Internal);
1619*67e74705SXin Li } else {
1620*67e74705SXin Li HasLazyLocalLexicalLookups = true;
1621*67e74705SXin Li }
1622*67e74705SXin Li
1623*67e74705SXin Li // If we are a transparent context or inline namespace, insert into our
1624*67e74705SXin Li // parent context, too. This operation is recursive.
1625*67e74705SXin Li if (isTransparentContext() || isInlineNamespace())
1626*67e74705SXin Li getParent()->getPrimaryContext()->
1627*67e74705SXin Li makeDeclVisibleInContextWithFlags(D, Internal, Recoverable);
1628*67e74705SXin Li
1629*67e74705SXin Li Decl *DCAsDecl = cast<Decl>(this);
1630*67e74705SXin Li // Notify that a decl was made visible unless we are a Tag being defined.
1631*67e74705SXin Li if (!(isa<TagDecl>(DCAsDecl) && cast<TagDecl>(DCAsDecl)->isBeingDefined()))
1632*67e74705SXin Li if (ASTMutationListener *L = DCAsDecl->getASTMutationListener())
1633*67e74705SXin Li L->AddedVisibleDecl(this, D);
1634*67e74705SXin Li }
1635*67e74705SXin Li
makeDeclVisibleInContextImpl(NamedDecl * D,bool Internal)1636*67e74705SXin Li void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal) {
1637*67e74705SXin Li // Find or create the stored declaration map.
1638*67e74705SXin Li StoredDeclsMap *Map = LookupPtr;
1639*67e74705SXin Li if (!Map) {
1640*67e74705SXin Li ASTContext *C = &getParentASTContext();
1641*67e74705SXin Li Map = CreateStoredDeclsMap(*C);
1642*67e74705SXin Li }
1643*67e74705SXin Li
1644*67e74705SXin Li // If there is an external AST source, load any declarations it knows about
1645*67e74705SXin Li // with this declaration's name.
1646*67e74705SXin Li // If the lookup table contains an entry about this name it means that we
1647*67e74705SXin Li // have already checked the external source.
1648*67e74705SXin Li if (!Internal)
1649*67e74705SXin Li if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
1650*67e74705SXin Li if (hasExternalVisibleStorage() &&
1651*67e74705SXin Li Map->find(D->getDeclName()) == Map->end())
1652*67e74705SXin Li Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
1653*67e74705SXin Li
1654*67e74705SXin Li // Insert this declaration into the map.
1655*67e74705SXin Li StoredDeclsList &DeclNameEntries = (*Map)[D->getDeclName()];
1656*67e74705SXin Li
1657*67e74705SXin Li if (Internal) {
1658*67e74705SXin Li // If this is being added as part of loading an external declaration,
1659*67e74705SXin Li // this may not be the only external declaration with this name.
1660*67e74705SXin Li // In this case, we never try to replace an existing declaration; we'll
1661*67e74705SXin Li // handle that when we finalize the list of declarations for this name.
1662*67e74705SXin Li DeclNameEntries.setHasExternalDecls();
1663*67e74705SXin Li DeclNameEntries.AddSubsequentDecl(D);
1664*67e74705SXin Li return;
1665*67e74705SXin Li }
1666*67e74705SXin Li
1667*67e74705SXin Li if (DeclNameEntries.isNull()) {
1668*67e74705SXin Li DeclNameEntries.setOnlyValue(D);
1669*67e74705SXin Li return;
1670*67e74705SXin Li }
1671*67e74705SXin Li
1672*67e74705SXin Li if (DeclNameEntries.HandleRedeclaration(D, /*IsKnownNewer*/!Internal)) {
1673*67e74705SXin Li // This declaration has replaced an existing one for which
1674*67e74705SXin Li // declarationReplaces returns true.
1675*67e74705SXin Li return;
1676*67e74705SXin Li }
1677*67e74705SXin Li
1678*67e74705SXin Li // Put this declaration into the appropriate slot.
1679*67e74705SXin Li DeclNameEntries.AddSubsequentDecl(D);
1680*67e74705SXin Li }
1681*67e74705SXin Li
operator *() const1682*67e74705SXin Li UsingDirectiveDecl *DeclContext::udir_iterator::operator*() const {
1683*67e74705SXin Li return cast<UsingDirectiveDecl>(*I);
1684*67e74705SXin Li }
1685*67e74705SXin Li
1686*67e74705SXin Li /// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
1687*67e74705SXin Li /// this context.
using_directives() const1688*67e74705SXin Li DeclContext::udir_range DeclContext::using_directives() const {
1689*67e74705SXin Li // FIXME: Use something more efficient than normal lookup for using
1690*67e74705SXin Li // directives. In C++, using directives are looked up more than anything else.
1691*67e74705SXin Li lookup_result Result = lookup(UsingDirectiveDecl::getName());
1692*67e74705SXin Li return udir_range(Result.begin(), Result.end());
1693*67e74705SXin Li }
1694*67e74705SXin Li
1695*67e74705SXin Li //===----------------------------------------------------------------------===//
1696*67e74705SXin Li // Creation and Destruction of StoredDeclsMaps. //
1697*67e74705SXin Li //===----------------------------------------------------------------------===//
1698*67e74705SXin Li
CreateStoredDeclsMap(ASTContext & C) const1699*67e74705SXin Li StoredDeclsMap *DeclContext::CreateStoredDeclsMap(ASTContext &C) const {
1700*67e74705SXin Li assert(!LookupPtr && "context already has a decls map");
1701*67e74705SXin Li assert(getPrimaryContext() == this &&
1702*67e74705SXin Li "creating decls map on non-primary context");
1703*67e74705SXin Li
1704*67e74705SXin Li StoredDeclsMap *M;
1705*67e74705SXin Li bool Dependent = isDependentContext();
1706*67e74705SXin Li if (Dependent)
1707*67e74705SXin Li M = new DependentStoredDeclsMap();
1708*67e74705SXin Li else
1709*67e74705SXin Li M = new StoredDeclsMap();
1710*67e74705SXin Li M->Previous = C.LastSDM;
1711*67e74705SXin Li C.LastSDM = llvm::PointerIntPair<StoredDeclsMap*,1>(M, Dependent);
1712*67e74705SXin Li LookupPtr = M;
1713*67e74705SXin Li return M;
1714*67e74705SXin Li }
1715*67e74705SXin Li
ReleaseDeclContextMaps()1716*67e74705SXin Li void ASTContext::ReleaseDeclContextMaps() {
1717*67e74705SXin Li // It's okay to delete DependentStoredDeclsMaps via a StoredDeclsMap
1718*67e74705SXin Li // pointer because the subclass doesn't add anything that needs to
1719*67e74705SXin Li // be deleted.
1720*67e74705SXin Li StoredDeclsMap::DestroyAll(LastSDM.getPointer(), LastSDM.getInt());
1721*67e74705SXin Li }
1722*67e74705SXin Li
DestroyAll(StoredDeclsMap * Map,bool Dependent)1723*67e74705SXin Li void StoredDeclsMap::DestroyAll(StoredDeclsMap *Map, bool Dependent) {
1724*67e74705SXin Li while (Map) {
1725*67e74705SXin Li // Advance the iteration before we invalidate memory.
1726*67e74705SXin Li llvm::PointerIntPair<StoredDeclsMap*,1> Next = Map->Previous;
1727*67e74705SXin Li
1728*67e74705SXin Li if (Dependent)
1729*67e74705SXin Li delete static_cast<DependentStoredDeclsMap*>(Map);
1730*67e74705SXin Li else
1731*67e74705SXin Li delete Map;
1732*67e74705SXin Li
1733*67e74705SXin Li Map = Next.getPointer();
1734*67e74705SXin Li Dependent = Next.getInt();
1735*67e74705SXin Li }
1736*67e74705SXin Li }
1737*67e74705SXin Li
Create(ASTContext & C,DeclContext * Parent,const PartialDiagnostic & PDiag)1738*67e74705SXin Li DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
1739*67e74705SXin Li DeclContext *Parent,
1740*67e74705SXin Li const PartialDiagnostic &PDiag) {
1741*67e74705SXin Li assert(Parent->isDependentContext()
1742*67e74705SXin Li && "cannot iterate dependent diagnostics of non-dependent context");
1743*67e74705SXin Li Parent = Parent->getPrimaryContext();
1744*67e74705SXin Li if (!Parent->LookupPtr)
1745*67e74705SXin Li Parent->CreateStoredDeclsMap(C);
1746*67e74705SXin Li
1747*67e74705SXin Li DependentStoredDeclsMap *Map =
1748*67e74705SXin Li static_cast<DependentStoredDeclsMap *>(Parent->LookupPtr);
1749*67e74705SXin Li
1750*67e74705SXin Li // Allocate the copy of the PartialDiagnostic via the ASTContext's
1751*67e74705SXin Li // BumpPtrAllocator, rather than the ASTContext itself.
1752*67e74705SXin Li PartialDiagnostic::Storage *DiagStorage = nullptr;
1753*67e74705SXin Li if (PDiag.hasStorage())
1754*67e74705SXin Li DiagStorage = new (C) PartialDiagnostic::Storage;
1755*67e74705SXin Li
1756*67e74705SXin Li DependentDiagnostic *DD = new (C) DependentDiagnostic(PDiag, DiagStorage);
1757*67e74705SXin Li
1758*67e74705SXin Li // TODO: Maybe we shouldn't reverse the order during insertion.
1759*67e74705SXin Li DD->NextDiagnostic = Map->FirstDiagnostic;
1760*67e74705SXin Li Map->FirstDiagnostic = DD;
1761*67e74705SXin Li
1762*67e74705SXin Li return DD;
1763*67e74705SXin Li }
1764