InterfaceFile.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //===- InterfaceFile.cpp --------------------------------------------------===//
  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. // Implements the Interface File.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/TextAPI/InterfaceFile.h"
  13. #include <iomanip>
  14. #include <sstream>
  15. using namespace llvm;
  16. using namespace llvm::MachO;
  17. namespace {
  18. template <typename C>
  19. typename C::iterator addEntry(C &Container, StringRef InstallName) {
  20. auto I = partition_point(Container, [=](const InterfaceFileRef &O) {
  21. return O.getInstallName() < InstallName;
  22. });
  23. if (I != Container.end() && I->getInstallName() == InstallName)
  24. return I;
  25. return Container.emplace(I, InstallName);
  26. }
  27. template <typename C>
  28. typename C::iterator addEntry(C &Container, const Target &Target_) {
  29. auto Iter =
  30. lower_bound(Container, Target_, [](const Target &LHS, const Target &RHS) {
  31. return LHS < RHS;
  32. });
  33. if ((Iter != std::end(Container)) && !(Target_ < *Iter))
  34. return Iter;
  35. return Container.insert(Iter, Target_);
  36. }
  37. } // end namespace
  38. void InterfaceFileRef::addTarget(const Target &Target) {
  39. addEntry(Targets, Target);
  40. }
  41. void InterfaceFile::addAllowableClient(StringRef InstallName,
  42. const Target &Target) {
  43. auto Client = addEntry(AllowableClients, InstallName);
  44. Client->addTarget(Target);
  45. }
  46. void InterfaceFile::addReexportedLibrary(StringRef InstallName,
  47. const Target &Target) {
  48. auto Lib = addEntry(ReexportedLibraries, InstallName);
  49. Lib->addTarget(Target);
  50. }
  51. void InterfaceFile::addParentUmbrella(const Target &Target_, StringRef Parent) {
  52. auto Iter = lower_bound(ParentUmbrellas, Target_,
  53. [](const std::pair<Target, std::string> &LHS,
  54. Target RHS) { return LHS.first < RHS; });
  55. if ((Iter != ParentUmbrellas.end()) && !(Target_ < Iter->first)) {
  56. Iter->second = std::string(Parent);
  57. return;
  58. }
  59. ParentUmbrellas.emplace(Iter, Target_, std::string(Parent));
  60. }
  61. void InterfaceFile::addUUID(const Target &Target_, StringRef UUID) {
  62. auto Iter = lower_bound(UUIDs, Target_,
  63. [](const std::pair<Target, std::string> &LHS,
  64. Target RHS) { return LHS.first < RHS; });
  65. if ((Iter != UUIDs.end()) && !(Target_ < Iter->first)) {
  66. Iter->second = std::string(UUID);
  67. return;
  68. }
  69. UUIDs.emplace(Iter, Target_, std::string(UUID));
  70. }
  71. void InterfaceFile::addUUID(const Target &Target, uint8_t UUID[16]) {
  72. std::stringstream Stream;
  73. for (unsigned i = 0; i < 16; ++i) {
  74. if (i == 4 || i == 6 || i == 8 || i == 10)
  75. Stream << '-';
  76. Stream << std::setfill('0') << std::setw(2) << std::uppercase << std::hex
  77. << static_cast<int>(UUID[i]);
  78. }
  79. addUUID(Target, Stream.str());
  80. }
  81. void InterfaceFile::addTarget(const Target &Target) {
  82. addEntry(Targets, Target);
  83. }
  84. InterfaceFile::const_filtered_target_range
  85. InterfaceFile::targets(ArchitectureSet Archs) const {
  86. std::function<bool(const Target &)> fn = [Archs](const Target &Target_) {
  87. return Archs.has(Target_.Arch);
  88. };
  89. return make_filter_range(Targets, fn);
  90. }
  91. void InterfaceFile::addSymbol(SymbolKind Kind, StringRef Name,
  92. const TargetList &Targets, SymbolFlags Flags) {
  93. Name = copyString(Name);
  94. auto result = Symbols.try_emplace(SymbolsMapKey{Kind, Name}, nullptr);
  95. if (result.second)
  96. result.first->second = new (Allocator) Symbol{Kind, Name, Targets, Flags};
  97. else
  98. for (const auto &Target : Targets)
  99. result.first->second->addTarget(Target);
  100. }
  101. void InterfaceFile::addDocument(std::shared_ptr<InterfaceFile> &&Document) {
  102. auto Pos = llvm::lower_bound(Documents, Document,
  103. [](const std::shared_ptr<InterfaceFile> &LHS,
  104. const std::shared_ptr<InterfaceFile> &RHS) {
  105. return LHS->InstallName < RHS->InstallName;
  106. });
  107. Document->Parent = this;
  108. Documents.insert(Pos, Document);
  109. }
  110. bool InterfaceFile::operator==(const InterfaceFile &O) const {
  111. if (Targets != O.Targets)
  112. return false;
  113. if (InstallName != O.InstallName)
  114. return false;
  115. if ((CurrentVersion != O.CurrentVersion) ||
  116. (CompatibilityVersion != O.CompatibilityVersion))
  117. return false;
  118. if (SwiftABIVersion != O.SwiftABIVersion)
  119. return false;
  120. if (IsTwoLevelNamespace != O.IsTwoLevelNamespace)
  121. return false;
  122. if (IsAppExtensionSafe != O.IsAppExtensionSafe)
  123. return false;
  124. if (IsInstallAPI != O.IsInstallAPI)
  125. return false;
  126. if (ParentUmbrellas != O.ParentUmbrellas)
  127. return false;
  128. if (AllowableClients != O.AllowableClients)
  129. return false;
  130. if (ReexportedLibraries != O.ReexportedLibraries)
  131. return false;
  132. if (Symbols != O.Symbols)
  133. return false;
  134. if (!std::equal(Documents.begin(), Documents.end(), O.Documents.begin(),
  135. O.Documents.end(),
  136. [](const std::shared_ptr<InterfaceFile> LHS,
  137. const std::shared_ptr<InterfaceFile> RHS) {
  138. return *LHS == *RHS;
  139. }))
  140. return false;
  141. return true;
  142. }