ExternalASTMerger.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. size_t SearchResultSize = SearchResult.size();
  61. if (SearchResultSize == 0 || SearchResultSize > 1) {
  62. // There are two cases here. First, we might not find the name.
  63. // We might also find multiple copies, in which case we have no
  64. // guarantee that the one we wanted is the one we pick. (E.g.,
  65. // if we have two specializations of the same template it is
  66. // very hard to determine which is the one you want.)
  67. //
  68. // The Origins map fixes this problem by allowing the origin to be
  69. // explicitly recorded, so we trigger that recording by returning
  70. // nothing (rather than a possibly-inaccurate guess) here.
  71. return nullptr;
  72. } else {
  73. NamedDecl *SearchResultDecl = SearchResult[0];
  74. if (isa<DeclContext>(SearchResultDecl) &&
  75. SearchResultDecl->getKind() == DC->getDeclKind())
  76. return cast<DeclContext>(SearchResultDecl)->getPrimaryContext();
  77. return nullptr; // This type of lookup is unsupported
  78. }
  79. }
  80. /// A custom implementation of ASTImporter, for ExternalASTMerger's purposes.
  81. ///
  82. /// There are several modifications:
  83. ///
  84. /// - It enables lazy lookup (via the HasExternalLexicalStorage flag and a few
  85. /// others), which instructs Clang to refer to ExternalASTMerger. Also, it
  86. /// forces MinimalImport to true, which is necessary to make this work.
  87. /// - It maintains a reverse importer for use with names. This allows lookup of
  88. /// arbitrary names in the source context.
  89. /// - It updates the ExternalASTMerger's origin map as needed whenever a
  90. /// it sees a DeclContext.
  91. class LazyASTImporter : public ASTImporter {
  92. private:
  93. ExternalASTMerger &Parent;
  94. ASTImporter Reverse;
  95. const ExternalASTMerger::OriginMap &FromOrigins;
  96. llvm::raw_ostream &logs() { return Parent.logs(); }
  97. public:
  98. LazyASTImporter(ExternalASTMerger &_Parent, ASTContext &ToContext,
  99. FileManager &ToFileManager,
  100. const ExternalASTMerger::ImporterSource &_Source,
  101. std::shared_ptr<ASTImporterSharedState> SharedState)
  102. : ASTImporter(ToContext, ToFileManager, _Source.getASTContext(),
  103. _Source.getFileManager(),
  104. /*MinimalImport=*/true, SharedState),
  105. Parent(_Parent),
  106. Reverse(_Source.getASTContext(), _Source.getFileManager(), ToContext,
  107. ToFileManager, /*MinimalImport=*/true),
  108. FromOrigins(_Source.getOriginMap()) {}
  109. /// Whenever a DeclContext is imported, ensure that ExternalASTSource's origin
  110. /// map is kept up to date. Also set the appropriate flags.
  111. void Imported(Decl *From, Decl *To) override {
  112. if (auto *ToDC = dyn_cast<DeclContext>(To)) {
  113. const bool LoggingEnabled = Parent.LoggingEnabled();
  114. if (LoggingEnabled)
  115. logs() << "(ExternalASTMerger*)" << (void*)&Parent
  116. << " imported (DeclContext*)" << (void*)ToDC
  117. << ", (ASTContext*)" << (void*)&getToContext()
  118. << " from (DeclContext*)" << (void*)llvm::cast<DeclContext>(From)
  119. << ", (ASTContext*)" << (void*)&getFromContext()
  120. << "\n";
  121. Source<DeclContext *> FromDC(
  122. cast<DeclContext>(From)->getPrimaryContext());
  123. if (FromOrigins.count(FromDC) &&
  124. Parent.HasImporterForOrigin(*FromOrigins.at(FromDC).AST)) {
  125. if (LoggingEnabled)
  126. logs() << "(ExternalASTMerger*)" << (void*)&Parent
  127. << " forced origin (DeclContext*)"
  128. << (void*)FromOrigins.at(FromDC).DC
  129. << ", (ASTContext*)"
  130. << (void*)FromOrigins.at(FromDC).AST
  131. << "\n";
  132. Parent.ForceRecordOrigin(ToDC, FromOrigins.at(FromDC));
  133. } else {
  134. if (LoggingEnabled)
  135. logs() << "(ExternalASTMerger*)" << (void*)&Parent
  136. << " maybe recording origin (DeclContext*)" << (void*)FromDC
  137. << ", (ASTContext*)" << (void*)&getFromContext()
  138. << "\n";
  139. Parent.MaybeRecordOrigin(ToDC, {FromDC, &getFromContext()});
  140. }
  141. }
  142. if (auto *ToTag = dyn_cast<TagDecl>(To)) {
  143. ToTag->setHasExternalLexicalStorage();
  144. ToTag->getPrimaryContext()->setMustBuildLookupTable();
  145. assert(Parent.CanComplete(ToTag));
  146. } else if (auto *ToNamespace = dyn_cast<NamespaceDecl>(To)) {
  147. ToNamespace->setHasExternalVisibleStorage();
  148. assert(Parent.CanComplete(ToNamespace));
  149. } else if (auto *ToContainer = dyn_cast<ObjCContainerDecl>(To)) {
  150. ToContainer->setHasExternalLexicalStorage();
  151. ToContainer->getPrimaryContext()->setMustBuildLookupTable();
  152. assert(Parent.CanComplete(ToContainer));
  153. }
  154. }
  155. ASTImporter &GetReverse() { return Reverse; }
  156. };
  157. bool HasDeclOfSameType(llvm::ArrayRef<Candidate> Decls, const Candidate &C) {
  158. if (isa<FunctionDecl>(C.first.get()))
  159. return false;
  160. return llvm::any_of(Decls, [&](const Candidate &D) {
  161. return C.first.get()->getKind() == D.first.get()->getKind();
  162. });
  163. }
  164. } // end namespace
  165. ASTImporter &ExternalASTMerger::ImporterForOrigin(ASTContext &OriginContext) {
  166. for (const std::unique_ptr<ASTImporter> &I : Importers)
  167. if (&I->getFromContext() == &OriginContext)
  168. return *I;
  169. llvm_unreachable("We should have an importer for this origin!");
  170. }
  171. namespace {
  172. LazyASTImporter &LazyImporterForOrigin(ExternalASTMerger &Merger,
  173. ASTContext &OriginContext) {
  174. return static_cast<LazyASTImporter &>(
  175. Merger.ImporterForOrigin(OriginContext));
  176. }
  177. }
  178. bool ExternalASTMerger::HasImporterForOrigin(ASTContext &OriginContext) {
  179. for (const std::unique_ptr<ASTImporter> &I : Importers)
  180. if (&I->getFromContext() == &OriginContext)
  181. return true;
  182. return false;
  183. }
  184. template <typename CallbackType>
  185. void ExternalASTMerger::ForEachMatchingDC(const DeclContext *DC,
  186. CallbackType Callback) {
  187. if (Origins.count(DC)) {
  188. ExternalASTMerger::DCOrigin Origin = Origins[DC];
  189. LazyASTImporter &Importer = LazyImporterForOrigin(*this, *Origin.AST);
  190. Callback(Importer, Importer.GetReverse(), Origin.DC);
  191. } else {
  192. bool DidCallback = false;
  193. for (const std::unique_ptr<ASTImporter> &Importer : Importers) {
  194. Source<TranslationUnitDecl *> SourceTU =
  195. Importer->getFromContext().getTranslationUnitDecl();
  196. ASTImporter &Reverse =
  197. static_cast<LazyASTImporter *>(Importer.get())->GetReverse();
  198. if (auto SourceDC = LookupSameContext(SourceTU, DC, Reverse)) {
  199. DidCallback = true;
  200. if (Callback(*Importer, Reverse, SourceDC))
  201. break;
  202. }
  203. }
  204. if (!DidCallback && LoggingEnabled())
  205. logs() << "(ExternalASTMerger*)" << (void*)this
  206. << " asserting for (DeclContext*)" << (const void*)DC
  207. << ", (ASTContext*)" << (void*)&Target.AST
  208. << "\n";
  209. assert(DidCallback && "Couldn't find a source context matching our DC");
  210. }
  211. }
  212. void ExternalASTMerger::CompleteType(TagDecl *Tag) {
  213. assert(Tag->hasExternalLexicalStorage());
  214. ForEachMatchingDC(Tag, [&](ASTImporter &Forward, ASTImporter &Reverse,
  215. Source<const DeclContext *> SourceDC) -> bool {
  216. auto *SourceTag = const_cast<TagDecl *>(cast<TagDecl>(SourceDC.get()));
  217. if (SourceTag->hasExternalLexicalStorage())
  218. SourceTag->getASTContext().getExternalSource()->CompleteType(SourceTag);
  219. if (!SourceTag->getDefinition())
  220. return false;
  221. Forward.MapImported(SourceTag, Tag);
  222. if (llvm::Error Err = Forward.ImportDefinition(SourceTag))
  223. llvm::consumeError(std::move(Err));
  224. Tag->setCompleteDefinition(SourceTag->isCompleteDefinition());
  225. return true;
  226. });
  227. }
  228. void ExternalASTMerger::CompleteType(ObjCInterfaceDecl *Interface) {
  229. assert(Interface->hasExternalLexicalStorage());
  230. ForEachMatchingDC(
  231. Interface, [&](ASTImporter &Forward, ASTImporter &Reverse,
  232. Source<const DeclContext *> SourceDC) -> bool {
  233. auto *SourceInterface = const_cast<ObjCInterfaceDecl *>(
  234. cast<ObjCInterfaceDecl>(SourceDC.get()));
  235. if (SourceInterface->hasExternalLexicalStorage())
  236. SourceInterface->getASTContext().getExternalSource()->CompleteType(
  237. SourceInterface);
  238. if (!SourceInterface->getDefinition())
  239. return false;
  240. Forward.MapImported(SourceInterface, Interface);
  241. if (llvm::Error Err = Forward.ImportDefinition(SourceInterface))
  242. llvm::consumeError(std::move(Err));
  243. return true;
  244. });
  245. }
  246. bool ExternalASTMerger::CanComplete(DeclContext *Interface) {
  247. assert(Interface->hasExternalLexicalStorage() ||
  248. Interface->hasExternalVisibleStorage());
  249. bool FoundMatchingDC = false;
  250. ForEachMatchingDC(Interface,
  251. [&](ASTImporter &Forward, ASTImporter &Reverse,
  252. Source<const DeclContext *> SourceDC) -> bool {
  253. FoundMatchingDC = true;
  254. return true;
  255. });
  256. return FoundMatchingDC;
  257. }
  258. namespace {
  259. bool IsSameDC(const DeclContext *D1, const DeclContext *D2) {
  260. if (isa<ObjCContainerDecl>(D1) && isa<ObjCContainerDecl>(D2))
  261. return true; // There are many cases where Objective-C is ambiguous.
  262. if (auto *T1 = dyn_cast<TagDecl>(D1))
  263. if (auto *T2 = dyn_cast<TagDecl>(D2))
  264. if (T1->getFirstDecl() == T2->getFirstDecl())
  265. return true;
  266. return D1 == D2 || D1 == CanonicalizeDC(D2);
  267. }
  268. }
  269. void ExternalASTMerger::MaybeRecordOrigin(const DeclContext *ToDC,
  270. DCOrigin Origin) {
  271. LazyASTImporter &Importer = LazyImporterForOrigin(*this, *Origin.AST);
  272. ASTImporter &Reverse = Importer.GetReverse();
  273. Source<const DeclContext *> FoundFromDC =
  274. LookupSameContext(Origin.AST->getTranslationUnitDecl(), ToDC, Reverse);
  275. const bool DoRecord = !FoundFromDC || !IsSameDC(FoundFromDC.get(), Origin.DC);
  276. if (DoRecord)
  277. RecordOriginImpl(ToDC, Origin, Importer);
  278. if (LoggingEnabled())
  279. logs() << "(ExternalASTMerger*)" << (void*)this
  280. << (DoRecord ? " decided " : " decided NOT")
  281. << " to record origin (DeclContext*)" << (void*)Origin.DC
  282. << ", (ASTContext*)" << (void*)&Origin.AST
  283. << "\n";
  284. }
  285. void ExternalASTMerger::ForceRecordOrigin(const DeclContext *ToDC,
  286. DCOrigin Origin) {
  287. RecordOriginImpl(ToDC, Origin, ImporterForOrigin(*Origin.AST));
  288. }
  289. void ExternalASTMerger::RecordOriginImpl(const DeclContext *ToDC, DCOrigin Origin,
  290. ASTImporter &Importer) {
  291. Origins[ToDC] = Origin;
  292. Importer.ASTImporter::MapImported(cast<Decl>(Origin.DC), const_cast<Decl*>(cast<Decl>(ToDC)));
  293. }
  294. ExternalASTMerger::ExternalASTMerger(const ImporterTarget &Target,
  295. llvm::ArrayRef<ImporterSource> Sources) : LogStream(&llvm::nulls()), Target(Target) {
  296. SharedState = std::make_shared<ASTImporterSharedState>(
  297. *Target.AST.getTranslationUnitDecl());
  298. AddSources(Sources);
  299. }
  300. void ExternalASTMerger::AddSources(llvm::ArrayRef<ImporterSource> Sources) {
  301. for (const ImporterSource &S : Sources) {
  302. assert(&S.getASTContext() != &Target.AST);
  303. Importers.push_back(std::make_unique<LazyASTImporter>(
  304. *this, Target.AST, Target.FM, S, SharedState));
  305. }
  306. }
  307. void ExternalASTMerger::RemoveSources(llvm::ArrayRef<ImporterSource> Sources) {
  308. if (LoggingEnabled())
  309. for (const ImporterSource &S : Sources)
  310. logs() << "(ExternalASTMerger*)" << (void *)this
  311. << " removing source (ASTContext*)" << (void *)&S.getASTContext()
  312. << "\n";
  313. Importers.erase(
  314. std::remove_if(Importers.begin(), Importers.end(),
  315. [&Sources](std::unique_ptr<ASTImporter> &Importer) -> bool {
  316. for (const ImporterSource &S : Sources) {
  317. if (&Importer->getFromContext() == &S.getASTContext())
  318. return true;
  319. }
  320. return false;
  321. }),
  322. Importers.end());
  323. for (OriginMap::iterator OI = Origins.begin(), OE = Origins.end(); OI != OE; ) {
  324. std::pair<const DeclContext *, DCOrigin> Origin = *OI;
  325. bool Erase = false;
  326. for (const ImporterSource &S : Sources) {
  327. if (&S.getASTContext() == Origin.second.AST) {
  328. Erase = true;
  329. break;
  330. }
  331. }
  332. if (Erase)
  333. OI = Origins.erase(OI);
  334. else
  335. ++OI;
  336. }
  337. }
  338. template <typename DeclTy>
  339. static bool importSpecializations(DeclTy *D, ASTImporter *Importer) {
  340. for (auto *Spec : D->specializations()) {
  341. auto ImportedSpecOrError = Importer->Import(Spec);
  342. if (!ImportedSpecOrError) {
  343. llvm::consumeError(ImportedSpecOrError.takeError());
  344. return true;
  345. }
  346. }
  347. return false;
  348. }
  349. /// Imports specializations from template declarations that can be specialized.
  350. static bool importSpecializationsIfNeeded(Decl *D, ASTImporter *Importer) {
  351. if (!isa<TemplateDecl>(D))
  352. return false;
  353. if (auto *FunctionTD = dyn_cast<FunctionTemplateDecl>(D))
  354. return importSpecializations(FunctionTD, Importer);
  355. else if (auto *ClassTD = dyn_cast<ClassTemplateDecl>(D))
  356. return importSpecializations(ClassTD, Importer);
  357. else if (auto *VarTD = dyn_cast<VarTemplateDecl>(D))
  358. return importSpecializations(VarTD, Importer);
  359. return false;
  360. }
  361. bool ExternalASTMerger::FindExternalVisibleDeclsByName(const DeclContext *DC,
  362. DeclarationName Name) {
  363. llvm::SmallVector<NamedDecl *, 1> Decls;
  364. llvm::SmallVector<Candidate, 4> Candidates;
  365. auto FilterFoundDecl = [&Candidates](const Candidate &C) {
  366. if (!HasDeclOfSameType(Candidates, C))
  367. Candidates.push_back(C);
  368. };
  369. ForEachMatchingDC(DC,
  370. [&](ASTImporter &Forward, ASTImporter &Reverse,
  371. Source<const DeclContext *> SourceDC) -> bool {
  372. auto FromNameOrErr = Reverse.Import(Name);
  373. if (!FromNameOrErr) {
  374. llvm::consumeError(FromNameOrErr.takeError());
  375. return false;
  376. }
  377. DeclContextLookupResult Result =
  378. SourceDC.get()->lookup(*FromNameOrErr);
  379. for (NamedDecl *FromD : Result) {
  380. FilterFoundDecl(std::make_pair(FromD, &Forward));
  381. }
  382. return false;
  383. });
  384. if (Candidates.empty())
  385. return false;
  386. Decls.reserve(Candidates.size());
  387. for (const Candidate &C : Candidates) {
  388. Decl *LookupRes = C.first.get();
  389. ASTImporter *Importer = C.second;
  390. auto NDOrErr = Importer->Import(LookupRes);
  391. assert(NDOrErr);
  392. (void)static_cast<bool>(NDOrErr);
  393. NamedDecl *ND = cast_or_null<NamedDecl>(*NDOrErr);
  394. assert(ND);
  395. // If we don't import specialization, they are not available via lookup
  396. // because the lookup result is imported TemplateDecl and it does not
  397. // reference its specializations until they are imported explicitly.
  398. bool IsSpecImportFailed =
  399. importSpecializationsIfNeeded(LookupRes, Importer);
  400. assert(!IsSpecImportFailed);
  401. (void)IsSpecImportFailed;
  402. Decls.push_back(ND);
  403. }
  404. SetExternalVisibleDeclsForName(DC, Name, Decls);
  405. return true;
  406. }
  407. void ExternalASTMerger::FindExternalLexicalDecls(
  408. const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
  409. SmallVectorImpl<Decl *> &Result) {
  410. ForEachMatchingDC(DC, [&](ASTImporter &Forward, ASTImporter &Reverse,
  411. Source<const DeclContext *> SourceDC) -> bool {
  412. for (const Decl *SourceDecl : SourceDC.get()->decls()) {
  413. if (IsKindWeWant(SourceDecl->getKind())) {
  414. auto ImportedDeclOrErr = Forward.Import(SourceDecl);
  415. if (ImportedDeclOrErr)
  416. assert(!(*ImportedDeclOrErr) ||
  417. IsSameDC((*ImportedDeclOrErr)->getDeclContext(), DC));
  418. else
  419. llvm::consumeError(ImportedDeclOrErr.takeError());
  420. }
  421. }
  422. return false;
  423. });
  424. }