ExternalASTMerger.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //===- ExternalASTMerger.cpp - Merging External AST Interface ---*- C++ -*-===//
  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 ExternalASTMerger, which vends a combination of
  11. // ASTs from several different ASTContext/FileManager pairs
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "clang/AST/ASTContext.h"
  15. #include "clang/AST/Decl.h"
  16. #include "clang/AST/DeclObjC.h"
  17. #include "clang/AST/ExternalASTMerger.h"
  18. using namespace clang;
  19. namespace {
  20. template <typename T> struct Source {
  21. T t;
  22. Source(T &&t) : t(std::move(t)) {}
  23. operator T() { return t; }
  24. template <typename U = T> U &get() { return t; }
  25. template <typename U = T> const U &get() const { return t; }
  26. template <typename U> operator Source<U>() { return Source<U>(t); }
  27. };
  28. typedef std::pair<Source<NamedDecl *>, ASTImporter *> Candidate;
  29. class LazyASTImporter : public ASTImporter {
  30. public:
  31. LazyASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
  32. ASTContext &FromContext, FileManager &FromFileManager)
  33. : ASTImporter(ToContext, ToFileManager, FromContext, FromFileManager,
  34. /*MinimalImport=*/true) {}
  35. Decl *Imported(Decl *From, Decl *To) override {
  36. if (auto ToTag = dyn_cast<TagDecl>(To)) {
  37. ToTag->setHasExternalLexicalStorage();
  38. } else if (auto ToNamespace = dyn_cast<NamespaceDecl>(To)) {
  39. ToNamespace->setHasExternalVisibleStorage();
  40. }
  41. return ASTImporter::Imported(From, To);
  42. }
  43. };
  44. Source<const DeclContext *>
  45. LookupSameContext(Source<TranslationUnitDecl *> SourceTU, const DeclContext *DC,
  46. ASTImporter &ReverseImporter) {
  47. if (DC->isTranslationUnit()) {
  48. return SourceTU;
  49. }
  50. Source<const DeclContext *> SourceParentDC =
  51. LookupSameContext(SourceTU, DC->getParent(), ReverseImporter);
  52. if (!SourceParentDC) {
  53. // If we couldn't find the parent DC in this TranslationUnit, give up.
  54. return nullptr;
  55. }
  56. auto ND = cast<NamedDecl>(DC);
  57. DeclarationName Name = ND->getDeclName();
  58. Source<DeclarationName> SourceName = ReverseImporter.Import(Name);
  59. DeclContext::lookup_result SearchResult =
  60. SourceParentDC.get()->lookup(SourceName.get());
  61. size_t SearchResultSize = SearchResult.size();
  62. // Handle multiple candidates once we have a test for it.
  63. // This may turn up when we import template specializations correctly.
  64. assert(SearchResultSize < 2);
  65. if (SearchResultSize == 0) {
  66. // couldn't find the name, so we have to give up
  67. return nullptr;
  68. } else {
  69. NamedDecl *SearchResultDecl = SearchResult[0];
  70. return dyn_cast<DeclContext>(SearchResultDecl);
  71. }
  72. }
  73. bool IsForwardDeclaration(Decl *D) {
  74. assert(!isa<ObjCInterfaceDecl>(D)); // TODO handle this case
  75. if (auto TD = dyn_cast<TagDecl>(D)) {
  76. return !TD->isThisDeclarationADefinition();
  77. } else if (auto FD = dyn_cast<FunctionDecl>(D)) {
  78. return !FD->isThisDeclarationADefinition();
  79. } else {
  80. return false;
  81. }
  82. }
  83. void ForEachMatchingDC(
  84. const DeclContext *DC,
  85. llvm::ArrayRef<ExternalASTMerger::ImporterPair> Importers,
  86. std::function<void(const ExternalASTMerger::ImporterPair &IP,
  87. Source<const DeclContext *> SourceDC)>
  88. Callback) {
  89. for (const ExternalASTMerger::ImporterPair &IP : Importers) {
  90. Source<TranslationUnitDecl *> SourceTU(
  91. IP.Forward->getFromContext().getTranslationUnitDecl());
  92. Source<const DeclContext *> SourceDC =
  93. LookupSameContext(SourceTU, DC, *IP.Reverse);
  94. if (SourceDC.get()) {
  95. Callback(IP, SourceDC);
  96. }
  97. }
  98. }
  99. bool HasDeclOfSameType(llvm::ArrayRef<Candidate> Decls, const Candidate &C) {
  100. return std::any_of(Decls.begin(), Decls.end(), [&C](const Candidate &D) {
  101. return C.first.get()->getKind() == D.first.get()->getKind();
  102. });
  103. }
  104. } // end namespace
  105. ExternalASTMerger::ExternalASTMerger(const ImporterEndpoint &Target,
  106. llvm::ArrayRef<ImporterEndpoint> Sources) {
  107. for (const ImporterEndpoint &S : Sources) {
  108. Importers.push_back(
  109. {llvm::make_unique<LazyASTImporter>(Target.AST, Target.FM, S.AST, S.FM),
  110. llvm::make_unique<ASTImporter>(S.AST, S.FM, Target.AST, Target.FM,
  111. /*MinimalImport=*/true)});
  112. }
  113. }
  114. bool ExternalASTMerger::FindExternalVisibleDeclsByName(const DeclContext *DC,
  115. DeclarationName Name) {
  116. llvm::SmallVector<NamedDecl *, 1> Decls;
  117. llvm::SmallVector<Candidate, 4> CompleteDecls;
  118. llvm::SmallVector<Candidate, 4> ForwardDecls;
  119. auto FilterFoundDecl = [&CompleteDecls, &ForwardDecls](const Candidate &C) {
  120. if (IsForwardDeclaration(C.first.get())) {
  121. if (!HasDeclOfSameType(ForwardDecls, C)) {
  122. ForwardDecls.push_back(C);
  123. }
  124. } else {
  125. CompleteDecls.push_back(C);
  126. }
  127. };
  128. ForEachMatchingDC(DC, Importers, [Name, &FilterFoundDecl](
  129. const ImporterPair &IP,
  130. Source<const DeclContext *> SourceDC) {
  131. DeclarationName FromName = IP.Reverse->Import(Name);
  132. DeclContextLookupResult Result = SourceDC.get()->lookup(FromName);
  133. for (NamedDecl *FromD : Result) {
  134. FilterFoundDecl(std::make_pair(FromD, IP.Forward.get()));
  135. }
  136. });
  137. llvm::ArrayRef<Candidate> DeclsToReport =
  138. CompleteDecls.empty() ? ForwardDecls : CompleteDecls;
  139. if (DeclsToReport.empty()) {
  140. return false;
  141. }
  142. Decls.reserve(DeclsToReport.size());
  143. for (const Candidate &C : DeclsToReport) {
  144. NamedDecl *d = cast<NamedDecl>(C.second->Import(C.first.get()));
  145. assert(d);
  146. Decls.push_back(d);
  147. }
  148. SetExternalVisibleDeclsForName(DC, Name, Decls);
  149. return true;
  150. }
  151. void ExternalASTMerger::FindExternalLexicalDecls(
  152. const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
  153. SmallVectorImpl<Decl *> &Result) {
  154. ForEachMatchingDC(
  155. DC, Importers, [DC, IsKindWeWant](const ImporterPair &IP,
  156. Source<const DeclContext *> SourceDC) {
  157. for (const Decl *SourceDecl : SourceDC.get()->decls()) {
  158. if (IsKindWeWant(SourceDecl->getKind())) {
  159. Decl *ImportedDecl =
  160. IP.Forward->Import(const_cast<Decl *>(SourceDecl));
  161. assert(ImportedDecl->getDeclContext() == DC);
  162. }
  163. }
  164. });
  165. }