InterfaceFile.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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::MachO;
  16. namespace llvm {
  17. namespace MachO {
  18. namespace detail {
  19. template <typename C>
  20. typename C::iterator addEntry(C &Container, StringRef InstallName) {
  21. auto I = llvm::bsearch(Container, [=](const InterfaceFileRef &O) {
  22. return InstallName <= O.getInstallName();
  23. });
  24. if ((I != std::end(Container)) && !(InstallName < I->getInstallName()))
  25. return I;
  26. return Container.emplace(I, InstallName);
  27. }
  28. } // end namespace detail.
  29. void InterfaceFile::addAllowableClient(StringRef Name,
  30. ArchitectureSet Architectures) {
  31. auto Client = detail::addEntry(AllowableClients, Name);
  32. Client->addArchitectures(Architectures);
  33. }
  34. void InterfaceFile::addReexportedLibrary(StringRef InstallName,
  35. ArchitectureSet Architectures) {
  36. auto Lib = detail::addEntry(ReexportedLibraries, InstallName);
  37. Lib->addArchitectures(Architectures);
  38. }
  39. void InterfaceFile::addUUID(Architecture Arch, StringRef UUID) {
  40. auto I =
  41. llvm::bsearch(UUIDs, [=](const std::pair<Architecture, std::string> &O) {
  42. return Arch <= O.first;
  43. });
  44. if (I != UUIDs.end() && Arch == I->first) {
  45. I->second = UUID;
  46. return;
  47. }
  48. UUIDs.emplace(I, Arch, UUID);
  49. return;
  50. }
  51. void InterfaceFile::addUUID(Architecture Arch, uint8_t UUID[16]) {
  52. std::stringstream Stream;
  53. for (unsigned i = 0; i < 16; ++i) {
  54. if (i == 4 || i == 6 || i == 8 || i == 10)
  55. Stream << '-';
  56. Stream << std::setfill('0') << std::setw(2) << std::uppercase << std::hex
  57. << static_cast<int>(UUID[i]);
  58. }
  59. addUUID(Arch, Stream.str());
  60. }
  61. void InterfaceFile::addSymbol(SymbolKind Kind, StringRef Name,
  62. ArchitectureSet Archs, SymbolFlags Flags) {
  63. Name = copyString(Name);
  64. auto result = Symbols.try_emplace(SymbolsMapKey{Kind, Name}, nullptr);
  65. if (result.second)
  66. result.first->second = new (Allocator) Symbol{Kind, Name, Archs, Flags};
  67. else
  68. result.first->second->addArchitectures(Archs);
  69. }
  70. } // end namespace MachO.
  71. } // end namespace llvm.