ExternalASTMerger.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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(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. template <typename CallbackType>
  84. void ForEachMatchingDC(
  85. const DeclContext *DC,
  86. llvm::ArrayRef<ExternalASTMerger::ImporterPair> Importers,
  87. CallbackType Callback) {
  88. for (const ExternalASTMerger::ImporterPair &IP : Importers) {
  89. Source<TranslationUnitDecl *> SourceTU =
  90. IP.Forward->getFromContext().getTranslationUnitDecl();
  91. if (auto SourceDC = LookupSameContext(SourceTU, DC, *IP.Reverse))
  92. Callback(IP, SourceDC);
  93. }
  94. }
  95. bool HasDeclOfSameType(llvm::ArrayRef<Candidate> Decls, const Candidate &C) {
  96. return llvm::any_of(Decls, [&](const Candidate &D) {
  97. return C.first.get()->getKind() == D.first.get()->getKind();
  98. });
  99. }
  100. } // end namespace
  101. ExternalASTMerger::ExternalASTMerger(const ImporterEndpoint &Target,
  102. llvm::ArrayRef<ImporterEndpoint> Sources) {
  103. for (const ImporterEndpoint &S : Sources) {
  104. Importers.push_back(
  105. {llvm::make_unique<LazyASTImporter>(Target.AST, Target.FM, S.AST, S.FM),
  106. llvm::make_unique<ASTImporter>(S.AST, S.FM, Target.AST, Target.FM,
  107. /*MinimalImport=*/true)});
  108. }
  109. }
  110. bool ExternalASTMerger::FindExternalVisibleDeclsByName(const DeclContext *DC,
  111. DeclarationName Name) {
  112. llvm::SmallVector<NamedDecl *, 1> Decls;
  113. llvm::SmallVector<Candidate, 4> CompleteDecls;
  114. llvm::SmallVector<Candidate, 4> ForwardDecls;
  115. auto FilterFoundDecl = [&CompleteDecls, &ForwardDecls](const Candidate &C) {
  116. if (IsForwardDeclaration(C.first.get())) {
  117. if (!HasDeclOfSameType(ForwardDecls, C)) {
  118. ForwardDecls.push_back(C);
  119. }
  120. } else {
  121. CompleteDecls.push_back(C);
  122. }
  123. };
  124. ForEachMatchingDC(
  125. DC, Importers,
  126. [&](const ImporterPair &IP, Source<const DeclContext *> SourceDC) {
  127. DeclarationName FromName = IP.Reverse->Import(Name);
  128. DeclContextLookupResult Result = SourceDC.get()->lookup(FromName);
  129. for (NamedDecl *FromD : Result) {
  130. FilterFoundDecl(std::make_pair(FromD, IP.Forward.get()));
  131. }
  132. });
  133. llvm::ArrayRef<Candidate> DeclsToReport =
  134. CompleteDecls.empty() ? ForwardDecls : CompleteDecls;
  135. if (DeclsToReport.empty()) {
  136. return false;
  137. }
  138. Decls.reserve(DeclsToReport.size());
  139. for (const Candidate &C : DeclsToReport) {
  140. NamedDecl *d = cast<NamedDecl>(C.second->Import(C.first.get()));
  141. assert(d);
  142. Decls.push_back(d);
  143. }
  144. SetExternalVisibleDeclsForName(DC, Name, Decls);
  145. return true;
  146. }
  147. void ExternalASTMerger::FindExternalLexicalDecls(
  148. const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
  149. SmallVectorImpl<Decl *> &Result) {
  150. ForEachMatchingDC(
  151. DC, Importers,
  152. [&](const ImporterPair &IP, Source<const DeclContext *> SourceDC) {
  153. for (const Decl *SourceDecl : SourceDC.get()->decls()) {
  154. if (IsKindWeWant(SourceDecl->getKind())) {
  155. Decl *ImportedDecl =
  156. IP.Forward->Import(const_cast<Decl *>(SourceDecl));
  157. assert(ImportedDecl->getDeclContext() == DC);
  158. (void)ImportedDecl;
  159. }
  160. }
  161. });
  162. }
  163. void ExternalASTMerger::CompleteType(TagDecl *Tag) {
  164. SmallVector<Decl *, 0> Result;
  165. FindExternalLexicalDecls(Tag, [](Decl::Kind) { return true; }, Result);
  166. Tag->setHasExternalLexicalStorage(false);
  167. }