InterfaceFile.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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/MachO/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. }