ExternalASTMerger.cpp 17 KB

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