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