InterfaceFile.cpp 2.8 KB

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