TapiUniversal.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- TapiUniversal.h - Text-based Dynamic Library Stub -------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file declares the TapiUniversal interface.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_OBJECT_TAPI_UNIVERSAL_H
  18. #define LLVM_OBJECT_TAPI_UNIVERSAL_H
  19. #include "llvm/Object/Binary.h"
  20. #include "llvm/Object/TapiFile.h"
  21. #include "llvm/Support/Error.h"
  22. #include "llvm/Support/MemoryBuffer.h"
  23. #include "llvm/TextAPI/MachO/Architecture.h"
  24. #include "llvm/TextAPI/MachO/InterfaceFile.h"
  25. namespace llvm {
  26. namespace object {
  27. class TapiUniversal : public Binary {
  28. public:
  29. class ObjectForArch {
  30. const TapiUniversal *Parent;
  31. int Index;
  32. public:
  33. ObjectForArch(const TapiUniversal *Parent, int Index)
  34. : Parent(Parent), Index(Index) {}
  35. ObjectForArch getNext() const { return ObjectForArch(Parent, Index + 1); }
  36. bool operator==(const ObjectForArch &Other) const {
  37. return (Parent == Other.Parent) && (Index == Other.Index);
  38. }
  39. uint32_t getCPUType() const {
  40. auto Result =
  41. MachO::getCPUTypeFromArchitecture(Parent->Libraries[Index].Arch);
  42. return Result.first;
  43. }
  44. uint32_t getCPUSubType() const {
  45. auto Result =
  46. MachO::getCPUTypeFromArchitecture(Parent->Libraries[Index].Arch);
  47. return Result.second;
  48. }
  49. StringRef getArchFlagName() const {
  50. return MachO::getArchitectureName(Parent->Libraries[Index].Arch);
  51. }
  52. std::string getInstallName() const {
  53. return std::string(Parent->Libraries[Index].InstallName);
  54. }
  55. bool isTopLevelLib() const {
  56. return Parent->ParsedFile->getInstallName() == getInstallName();
  57. }
  58. Expected<std::unique_ptr<TapiFile>> getAsObjectFile() const;
  59. };
  60. class object_iterator {
  61. ObjectForArch Obj;
  62. public:
  63. object_iterator(const ObjectForArch &Obj) : Obj(Obj) {}
  64. const ObjectForArch *operator->() const { return &Obj; }
  65. const ObjectForArch &operator*() const { return Obj; }
  66. bool operator==(const object_iterator &Other) const {
  67. return Obj == Other.Obj;
  68. }
  69. bool operator!=(const object_iterator &Other) const {
  70. return !(*this == Other);
  71. }
  72. object_iterator &operator++() { // Preincrement
  73. Obj = Obj.getNext();
  74. return *this;
  75. }
  76. };
  77. TapiUniversal(MemoryBufferRef Source, Error &Err);
  78. static Expected<std::unique_ptr<TapiUniversal>>
  79. create(MemoryBufferRef Source);
  80. ~TapiUniversal() override;
  81. object_iterator begin_objects() const { return ObjectForArch(this, 0); }
  82. object_iterator end_objects() const {
  83. return ObjectForArch(this, Libraries.size());
  84. }
  85. iterator_range<object_iterator> objects() const {
  86. return make_range(begin_objects(), end_objects());
  87. }
  88. uint32_t getNumberOfObjects() const { return Libraries.size(); }
  89. static bool classof(const Binary *v) { return v->isTapiUniversal(); }
  90. private:
  91. struct Library {
  92. StringRef InstallName;
  93. MachO::Architecture Arch;
  94. };
  95. std::unique_ptr<MachO::InterfaceFile> ParsedFile;
  96. std::vector<Library> Libraries;
  97. };
  98. } // end namespace object.
  99. } // end namespace llvm.
  100. #endif // LLVM_OBJECT_TAPI_UNIVERSAL_H
  101. #ifdef __GNUC__
  102. #pragma GCC diagnostic pop
  103. #endif