ExternalASTMerger.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. //===- ExternalASTMerger.cpp - Merging External AST Interface ---*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the ExternalASTMerger, which vends a combination of
  10. // ASTs from several different ASTContext/FileManager pairs
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/AST/ASTContext.h"
  14. #include "clang/AST/Decl.h"
  15. #include "clang/AST/DeclCXX.h"
  16. #include "clang/AST/DeclObjC.h"
  17. #include "clang/AST/DeclTemplate.h"
  18. #include "clang/AST/ExternalASTMerger.h"
  19. using namespace clang;
  20. namespace {
  21. template <typename T> struct Source {
  22. T t;
  23. Source(T t) : t(t) {}
  24. operator T() { return t; }
  25. template <typename U = T> U &get() { return t; }
  26. template <typename U = T> const U &get() const { return t; }
  27. template <typename U> operator Source<U>() { return Source<U>(t); }
  28. };
  29. typedef std::pair<Source<NamedDecl *>, ASTImporter *> Candidate;
  30. /// For the given DC, return the DC that is safe to perform lookups on. This is
  31. /// the DC we actually want to work with most of the time.
  32. const DeclContext *CanonicalizeDC(const DeclContext *DC) {
  33. if (isa<LinkageSpecDecl>(DC))
  34. return DC->getRedeclContext();
  35. return DC;
  36. }
  37. Source<const DeclContext *>
  38. LookupSameContext(Source<TranslationUnitDecl *> SourceTU, const DeclContext *DC,
  39. ASTImporter &ReverseImporter) {
  40. DC = CanonicalizeDC(DC);
  41. if (DC->isTranslationUnit()) {
  42. return SourceTU;
  43. }
  44. Source<const DeclContext *> SourceParentDC =
  45. LookupSameContext(SourceTU, DC->getParent(), ReverseImporter);
  46. if (!SourceParentDC) {
  47. // If we couldn't find the parent DC in this TranslationUnit, give up.
  48. return nullptr;
  49. }
  50. auto *ND = cast<NamedDecl>(DC);
  51. DeclarationName Name = ND->getDeclName();
  52. auto SourceNameOrErr = ReverseImporter.Import(Name);
  53. if (!SourceNameOrErr) {
  54. llvm::consumeError(SourceNameOrErr.takeError());
  55. return nullptr;
  56. }
  57. Source<DeclarationName> SourceName = *SourceNameOrErr;
  58. DeclContext::lookup_result SearchResult =
  59. SourceParentDC.get()->lookup(SourceName.get());
  60. // There are two cases here. First, we might not find the name.
  61. // We might also find multiple copies, in which case we have no
  62. // guarantee that the one we wanted is the one we pick. (E.g.,
  63. // if we have two specializations of the same template it is
  64. // very hard to determine which is the one you want.)
  65. //
  66. // The Origins map fixes this problem by allowing the origin to be
  67. // explicitly recorded, so we trigger that recording by returning
  68. // nothing (rather than a possibly-inaccurate guess) here.
  69. if (SearchResult.isSingleResult()) {
  70. NamedDecl *SearchResultDecl = SearchResult.front();
  71. if (isa<DeclContext>(SearchResultDecl) &&
  72. SearchResultDecl->getKind() == DC->getDeclKind())
  73. return cast<DeclContext>(SearchResultDecl)->getPrimaryContext();
  74. return nullptr; // This type of lookup is unsupported
  75. } else {
  76. return nullptr;
  77. }
  78. }
  79. /// A custom implementation of ASTImporter, for ExternalASTMerger's purposes.
  80. ///
  81. /// There are several modifications:
  82. ///
  83. /// - It enables lazy lookup (via the HasExternalLexicalStorage flag and a few
  84. /// others), which instructs Clang to refer to ExternalASTMerger. Also, it
  85. /// forces MinimalImport to true, which is necessary to make this work.
  86. /// - It maintains a reverse importer for use with names. This allows lookup of
  87. /// arbitrary names in the source context.
  88. /// - It updates the ExternalASTMerger's origin map as needed whenever a
  89. /// it sees a DeclContext.
  90. class LazyASTImporter : public ASTImporter {
  91. private:
  92. ExternalASTMerger &Parent;
  93. ASTImporter Reverse;
  94. const ExternalASTMerger::OriginMap &FromOrigins;
  95. /// @see ExternalASTMerger::ImporterSource::Temporary
  96. bool TemporarySource;
  97. /// Map of imported declarations back to the declarations they originated
  98. /// from.
  99. llvm::DenseMap<Decl *, Decl *> ToOrigin;
  100. /// @see ExternalASTMerger::ImporterSource::Merger
  101. ExternalASTMerger *SourceMerger;
  102. llvm::raw_ostream &logs() { return Parent.logs(); }
  103. public:
  104. LazyASTImporter(ExternalASTMerger &_Parent, ASTContext &ToContext,
  105. FileManager &ToFileManager,
  106. const ExternalASTMerger::ImporterSource &S,
  107. std::shared_ptr<ASTImporterSharedState> SharedState)
  108. : ASTImporter(ToContext, ToFileManager, S.getASTContext(),
  109. S.getFileManager(),
  110. /*MinimalImport=*/true, SharedState),
  111. Parent(_Parent),
  112. Reverse(S.getASTContext(), S.getFileManager(), ToContext, ToFileManager,
  113. /*MinimalImport=*/true),
  114. FromOrigins(S.getOriginMap()), TemporarySource(S.isTemporary()),
  115. SourceMerger(S.getMerger()) {}
  116. llvm::Expected<Decl *> ImportImpl(Decl *FromD) override {
  117. if (!TemporarySource || !SourceMerger)
  118. return ASTImporter::ImportImpl(FromD);
  119. // If we get here, then this source is importing from a temporary ASTContext
  120. // that also has another ExternalASTMerger attached. It could be
  121. // possible that the current ExternalASTMerger and the temporary ASTContext
  122. // share a common ImporterSource, which means that the temporary
  123. // AST could contain declarations that were imported from a source
  124. // that this ExternalASTMerger can access directly. Instead of importing
  125. // such declarations from the temporary ASTContext, they should instead
  126. // be directly imported by this ExternalASTMerger from the original
  127. // source. This way the ExternalASTMerger can safely do a minimal import
  128. // without creating incomplete declarations originated from a temporary
  129. // ASTContext. If we would try to complete such declarations later on, we
  130. // would fail to do so as their temporary AST could be deleted (which means
  131. // that the missing parts of the minimally imported declaration in that
  132. // ASTContext were also deleted).
  133. //
  134. // The following code tracks back any declaration that needs to be
  135. // imported from the temporary ASTContext to a persistent ASTContext.
  136. // Then the ExternalASTMerger tries to import from the persistent
  137. // ASTContext directly by using the associated ASTImporter. If that
  138. // succeeds, this ASTImporter just maps the declarations imported by
  139. // the other (persistent) ASTImporter to this (temporary) ASTImporter.
  140. // The steps can be visualized like this:
  141. //
  142. // Target AST <--- 3. Indirect import --- Persistent AST
  143. // ^ of persistent decl ^
  144. // | |
  145. // 1. Current import 2. Tracking back to persistent decl
  146. // 4. Map persistent decl |
  147. // & pretend we imported. |
  148. // | |
  149. // Temporary AST -------------------------------'
  150. // First, ask the ExternalASTMerger of the source where the temporary
  151. // declaration originated from.
  152. Decl *Persistent = SourceMerger->FindOriginalDecl(FromD);
  153. // FromD isn't from a persistent AST, so just do a normal import.
  154. if (!Persistent)
  155. return ASTImporter::ImportImpl(FromD);
  156. // Now ask the current ExternalASTMerger to try import the persistent
  157. // declaration into the target.
  158. ASTContext &PersistentCtx = Persistent->getASTContext();
  159. ASTImporter &OtherImporter = Parent.ImporterForOrigin(PersistentCtx);
  160. // Check that we never end up in the current Importer again.
  161. assert((&PersistentCtx != &getFromContext()) && (&OtherImporter != this) &&
  162. "Delegated to same Importer?");
  163. auto DeclOrErr = OtherImporter.Import(Persistent);
  164. // Errors when importing the persistent decl are treated as if we
  165. // had errors with importing the temporary decl.
  166. if (!DeclOrErr)
  167. return DeclOrErr.takeError();
  168. Decl *D = *DeclOrErr;
  169. // Tell the current ASTImporter that this has already been imported
  170. // to prevent any further queries for the temporary decl.
  171. MapImported(FromD, D);
  172. return D;
  173. }
  174. /// Implements the ASTImporter interface for tracking back a declaration
  175. /// to its original declaration it came from.
  176. Decl *GetOriginalDecl(Decl *To) override {
  177. auto It = ToOrigin.find(To);
  178. if (It != ToOrigin.end())
  179. return It->second;
  180. return nullptr;
  181. }
  182. /// Whenever a DeclContext is imported, ensure that ExternalASTSource's origin
  183. /// map is kept up to date. Also set the appropriate flags.
  184. void Imported(Decl *From, Decl *To) override {
  185. ToOrigin[To] = From;
  186. if (auto *ToDC = dyn_cast<DeclContext>(To)) {
  187. const bool LoggingEnabled = Parent.LoggingEnabled();
  188. if (LoggingEnabled)
  189. logs() << "(ExternalASTMerger*)" << (void*)&Parent
  190. << " imported (DeclContext*)" << (void*)ToDC
  191. << ", (ASTContext*)" << (void*)&getToContext()
  192. << " from (DeclContext*)" << (void*)llvm::cast<DeclContext>(From)
  193. << ", (ASTContext*)" << (void*)&getFromContext()
  194. << "\n";
  195. Source<DeclContext *> FromDC(
  196. cast<DeclContext>(From)->getPrimaryContext());
  197. if (FromOrigins.count(FromDC) &&
  198. Parent.HasImporterForOrigin(*FromOrigins.at(FromDC).AST)) {
  199. if (LoggingEnabled)
  200. logs() << "(ExternalASTMerger*)" << (void*)&Parent
  201. << " forced origin (DeclContext*)"
  202. << (void*)FromOrigins.at(FromDC).DC
  203. << ", (ASTContext*)"
  204. << (void*)FromOrigins.at(FromDC).AST
  205. << "\n";
  206. Parent.ForceRecordOrigin(ToDC, FromOrigins.at(FromDC));
  207. } else {
  208. if (LoggingEnabled)
  209. logs() << "(ExternalASTMerger*)" << (void*)&Parent
  210. << " maybe recording origin (DeclContext*)" << (void*)FromDC
  211. << ", (ASTContext*)" << (void*)&getFromContext()
  212. << "\n";
  213. Parent.MaybeRecordOrigin(ToDC, {FromDC, &getFromContext()});
  214. }
  215. }
  216. if (auto *ToTag = dyn_cast<TagDecl>(To)) {
  217. ToTag->setHasExternalLexicalStorage();
  218. ToTag->getPrimaryContext()->setMustBuildLookupTable();
  219. assert(Parent.CanComplete(ToTag));
  220. } else if (auto *ToNamespace = dyn_cast<NamespaceDecl>(To)) {
  221. ToNamespace->setHasExternalVisibleStorage();
  222. assert(Parent.CanComplete(ToNamespace));
  223. } else if (auto *ToContainer = dyn_cast<ObjCContainerDecl>(To)) {
  224. ToContainer->setHasExternalLexicalStorage();
  225. ToContainer->getPrimaryContext()->setMustBuildLookupTable();
  226. assert(Parent.CanComplete(ToContainer));
  227. }
  228. }
  229. ASTImporter &GetReverse() { return Reverse; }
  230. };
  231. bool HasDeclOfSameType(llvm::ArrayRef<Candidate> Decls, const Candidate &C) {
  232. if (isa<FunctionDecl>(C.first.get()))
  233. return false;
  234. return llvm::any_of(Decls, [&](const Candidate &D) {
  235. return C.first.get()->getKind() == D.first.get()->getKind();
  236. });
  237. }
  238. } // end namespace
  239. ASTImporter &ExternalASTMerger::ImporterForOrigin(ASTContext &OriginContext) {
  240. for (const std::unique_ptr<ASTImporter> &I : Importers)
  241. if (&I->getFromContext() == &OriginContext)
  242. return *I;
  243. llvm_unreachable("We should have an importer for this origin!");
  244. }
  245. namespace {
  246. LazyASTImporter &LazyImporterForOrigin(ExternalASTMerger &Merger,
  247. ASTContext &OriginContext) {
  248. return static_cast<LazyASTImporter &>(
  249. Merger.ImporterForOrigin(OriginContext));
  250. }
  251. }
  252. bool ExternalASTMerger::HasImporterForOrigin(ASTContext &OriginContext) {
  253. for (const std::unique_ptr<ASTImporter> &I : Importers)
  254. if (&I->getFromContext() == &OriginContext)
  255. return true;
  256. return false;
  257. }
  258. template <typename CallbackType>
  259. void ExternalASTMerger::ForEachMatchingDC(const DeclContext *DC,
  260. CallbackType Callback) {
  261. if (Origins.count(DC)) {
  262. ExternalASTMerger::DCOrigin Origin = Origins[DC];
  263. LazyASTImporter &Importer = LazyImporterForOrigin(*this, *Origin.AST);
  264. Callback(Importer, Importer.GetReverse(), Origin.DC);
  265. } else {
  266. bool DidCallback = false;
  267. for (const std::unique_ptr<ASTImporter> &Importer : Importers) {
  268. Source<TranslationUnitDecl *> SourceTU =
  269. Importer->getFromContext().getTranslationUnitDecl();
  270. ASTImporter &Reverse =
  271. static_cast<LazyASTImporter *>(Importer.get())->GetReverse();
  272. if (auto SourceDC = LookupSameContext(SourceTU, DC, Reverse)) {
  273. DidCallback = true;
  274. if (Callback(*Importer, Reverse, SourceDC))
  275. break;
  276. }
  277. }
  278. if (!DidCallback && LoggingEnabled())
  279. logs() << "(ExternalASTMerger*)" << (void*)this
  280. << " asserting for (DeclContext*)" << (const void*)DC
  281. << ", (ASTContext*)" << (void*)&Target.AST
  282. << "\n";
  283. assert(DidCallback && "Couldn't find a source context matching our DC");
  284. }
  285. }
  286. void ExternalASTMerger::CompleteType(TagDecl *Tag) {
  287. assert(Tag->hasExternalLexicalStorage());
  288. ForEachMatchingDC(Tag, [&](ASTImporter &Forward, ASTImporter &Reverse,
  289. Source<const DeclContext *> SourceDC) -> bool {
  290. auto *SourceTag = const_cast<TagDecl *>(cast<TagDecl>(SourceDC.get()));
  291. if (SourceTag->hasExternalLexicalStorage())
  292. SourceTag->getASTContext().getExternalSource()->CompleteType(SourceTag);
  293. if (!SourceTag->getDefinition())
  294. return false;
  295. Forward.MapImported(SourceTag, Tag);
  296. if (llvm::Error Err = Forward.ImportDefinition(SourceTag))
  297. llvm::consumeError(std::move(Err));
  298. Tag->setCompleteDefinition(SourceTag->isCompleteDefinition());
  299. return true;
  300. });
  301. }
  302. void ExternalASTMerger::CompleteType(ObjCInterfaceDecl *Interface) {
  303. assert(Interface->hasExternalLexicalStorage());
  304. ForEachMatchingDC(
  305. Interface, [&](ASTImporter &Forward, ASTImporter &Reverse,
  306. Source<const DeclContext *> SourceDC) -> bool {
  307. auto *SourceInterface = const_cast<ObjCInterfaceDecl *>(
  308. cast<ObjCInterfaceDecl>(SourceDC.get()));
  309. if (SourceInterface->hasExternalLexicalStorage())
  310. SourceInterface->getASTContext().getExternalSource()->CompleteType(
  311. SourceInterface);
  312. if (!SourceInterface->getDefinition())
  313. return false;
  314. Forward.MapImported(SourceInterface, Interface);
  315. if (llvm::Error Err = Forward.ImportDefinition(SourceInterface))
  316. llvm::consumeError(std::move(Err));
  317. return true;
  318. });
  319. }
  320. bool ExternalASTMerger::CanComplete(DeclContext *Interface) {
  321. assert(Interface->hasExternalLexicalStorage() ||
  322. Interface->hasExternalVisibleStorage());
  323. bool FoundMatchingDC = false;
  324. ForEachMatchingDC(Interface,
  325. [&](ASTImporter &Forward, ASTImporter &Reverse,
  326. Source<const DeclContext *> SourceDC) -> bool {
  327. FoundMatchingDC = true;
  328. return true;
  329. });
  330. return FoundMatchingDC;
  331. }
  332. namespace {
  333. bool IsSameDC(const DeclContext *D1, const DeclContext *D2) {
  334. if (isa<ObjCContainerDecl>(D1) && isa<ObjCContainerDecl>(D2))
  335. return true; // There are many cases where Objective-C is ambiguous.
  336. if (auto *T1 = dyn_cast<TagDecl>(D1))
  337. if (auto *T2 = dyn_cast<TagDecl>(D2))
  338. if (T1->getFirstDecl() == T2->getFirstDecl())
  339. return true;
  340. return D1 == D2 || D1 == CanonicalizeDC(D2);
  341. }
  342. }
  343. void ExternalASTMerger::MaybeRecordOrigin(const DeclContext *ToDC,
  344. DCOrigin Origin) {
  345. LazyASTImporter &Importer = LazyImporterForOrigin(*this, *Origin.AST);
  346. ASTImporter &Reverse = Importer.GetReverse();
  347. Source<const DeclContext *> FoundFromDC =
  348. LookupSameContext(Origin.AST->getTranslationUnitDecl(), ToDC, Reverse);
  349. const bool DoRecord = !FoundFromDC || !IsSameDC(FoundFromDC.get(), Origin.DC);
  350. if (DoRecord)
  351. RecordOriginImpl(ToDC, Origin, Importer);
  352. if (LoggingEnabled())
  353. logs() << "(ExternalASTMerger*)" << (void*)this
  354. << (DoRecord ? " decided " : " decided NOT")
  355. << " to record origin (DeclContext*)" << (void*)Origin.DC
  356. << ", (ASTContext*)" << (void*)&Origin.AST
  357. << "\n";
  358. }
  359. void ExternalASTMerger::ForceRecordOrigin(const DeclContext *ToDC,
  360. DCOrigin Origin) {
  361. RecordOriginImpl(ToDC, Origin, ImporterForOrigin(*Origin.AST));
  362. }
  363. void ExternalASTMerger::RecordOriginImpl(const DeclContext *ToDC, DCOrigin Origin,
  364. ASTImporter &Importer) {
  365. Origins[ToDC] = Origin;
  366. Importer.ASTImporter::MapImported(cast<Decl>(Origin.DC), const_cast<Decl*>(cast<Decl>(ToDC)));
  367. }
  368. ExternalASTMerger::ExternalASTMerger(const ImporterTarget &Target,
  369. llvm::ArrayRef<ImporterSource> Sources) : LogStream(&llvm::nulls()), Target(Target) {
  370. SharedState = std::make_shared<ASTImporterSharedState>(
  371. *Target.AST.getTranslationUnitDecl());
  372. AddSources(Sources);
  373. }
  374. Decl *ExternalASTMerger::FindOriginalDecl(Decl *D) {
  375. assert(&D->getASTContext() == &Target.AST);
  376. for (const auto &I : Importers)
  377. if (auto Result = I->GetOriginalDecl(D))
  378. return Result;
  379. return nullptr;
  380. }
  381. void ExternalASTMerger::AddSources(llvm::ArrayRef<ImporterSource> Sources) {
  382. for (const ImporterSource &S : Sources) {
  383. assert(&S.getASTContext() != &Target.AST);
  384. // Check that the associated merger actually imports into the source AST.
  385. assert(!S.getMerger() || &S.getMerger()->Target.AST == &S.getASTContext());
  386. Importers.push_back(std::make_unique<LazyASTImporter>(
  387. *this, Target.AST, Target.FM, S, SharedState));
  388. }
  389. }
  390. void ExternalASTMerger::RemoveSources(llvm::ArrayRef<ImporterSource> Sources) {
  391. if (LoggingEnabled())
  392. for (const ImporterSource &S : Sources)
  393. logs() << "(ExternalASTMerger*)" << (void *)this
  394. << " removing source (ASTContext*)" << (void *)&S.getASTContext()
  395. << "\n";
  396. llvm::erase_if(Importers,
  397. [&Sources](std::unique_ptr<ASTImporter> &Importer) -> bool {
  398. for (const ImporterSource &S : Sources) {
  399. if (&Importer->getFromContext() == &S.getASTContext())
  400. return true;
  401. }
  402. return false;
  403. });
  404. for (OriginMap::iterator OI = Origins.begin(), OE = Origins.end(); OI != OE; ) {
  405. std::pair<const DeclContext *, DCOrigin> Origin = *OI;
  406. bool Erase = false;
  407. for (const ImporterSource &S : Sources) {
  408. if (&S.getASTContext() == Origin.second.AST) {
  409. Erase = true;
  410. break;
  411. }
  412. }
  413. if (Erase)
  414. OI = Origins.erase(OI);
  415. else
  416. ++OI;
  417. }
  418. }
  419. template <typename DeclTy>
  420. static bool importSpecializations(DeclTy *D, ASTImporter *Importer) {
  421. for (auto *Spec : D->specializations()) {
  422. auto ImportedSpecOrError = Importer->Import(Spec);
  423. if (!ImportedSpecOrError) {
  424. llvm::consumeError(ImportedSpecOrError.takeError());
  425. return true;
  426. }
  427. }
  428. return false;
  429. }
  430. /// Imports specializations from template declarations that can be specialized.
  431. static bool importSpecializationsIfNeeded(Decl *D, ASTImporter *Importer) {
  432. if (!isa<TemplateDecl>(D))
  433. return false;
  434. if (auto *FunctionTD = dyn_cast<FunctionTemplateDecl>(D))
  435. return importSpecializations(FunctionTD, Importer);
  436. else if (auto *ClassTD = dyn_cast<ClassTemplateDecl>(D))
  437. return importSpecializations(ClassTD, Importer);
  438. else if (auto *VarTD = dyn_cast<VarTemplateDecl>(D))
  439. return importSpecializations(VarTD, Importer);
  440. return false;
  441. }
  442. bool ExternalASTMerger::FindExternalVisibleDeclsByName(const DeclContext *DC,
  443. DeclarationName Name) {
  444. llvm::SmallVector<NamedDecl *, 1> Decls;
  445. llvm::SmallVector<Candidate, 4> Candidates;
  446. auto FilterFoundDecl = [&Candidates](const Candidate &C) {
  447. if (!HasDeclOfSameType(Candidates, C))
  448. Candidates.push_back(C);
  449. };
  450. ForEachMatchingDC(DC,
  451. [&](ASTImporter &Forward, ASTImporter &Reverse,
  452. Source<const DeclContext *> SourceDC) -> bool {
  453. auto FromNameOrErr = Reverse.Import(Name);
  454. if (!FromNameOrErr) {
  455. llvm::consumeError(FromNameOrErr.takeError());
  456. return false;
  457. }
  458. DeclContextLookupResult Result =
  459. SourceDC.get()->lookup(*FromNameOrErr);
  460. for (NamedDecl *FromD : Result) {
  461. FilterFoundDecl(std::make_pair(FromD, &Forward));
  462. }
  463. return false;
  464. });
  465. if (Candidates.empty())
  466. return false;
  467. Decls.reserve(Candidates.size());
  468. for (const Candidate &C : Candidates) {
  469. Decl *LookupRes = C.first.get();
  470. ASTImporter *Importer = C.second;
  471. auto NDOrErr = Importer->Import(LookupRes);
  472. NamedDecl *ND = cast<NamedDecl>(llvm::cantFail(std::move(NDOrErr)));
  473. assert(ND);
  474. // If we don't import specialization, they are not available via lookup
  475. // because the lookup result is imported TemplateDecl and it does not
  476. // reference its specializations until they are imported explicitly.
  477. bool IsSpecImportFailed =
  478. importSpecializationsIfNeeded(LookupRes, Importer);
  479. assert(!IsSpecImportFailed);
  480. (void)IsSpecImportFailed;
  481. Decls.push_back(ND);
  482. }
  483. SetExternalVisibleDeclsForName(DC, Name, Decls);
  484. return true;
  485. }
  486. void ExternalASTMerger::FindExternalLexicalDecls(
  487. const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
  488. SmallVectorImpl<Decl *> &Result) {
  489. ForEachMatchingDC(DC, [&](ASTImporter &Forward, ASTImporter &Reverse,
  490. Source<const DeclContext *> SourceDC) -> bool {
  491. for (const Decl *SourceDecl : SourceDC.get()->decls()) {
  492. if (IsKindWeWant(SourceDecl->getKind())) {
  493. auto ImportedDeclOrErr = Forward.Import(SourceDecl);
  494. if (ImportedDeclOrErr)
  495. assert(!(*ImportedDeclOrErr) ||
  496. IsSameDC((*ImportedDeclOrErr)->getDeclContext(), DC));
  497. else
  498. llvm::consumeError(ImportedDeclOrErr.takeError());
  499. }
  500. }
  501. return false;
  502. });
  503. }