TapiUniversal.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //===-- TapiUniversal.h - Text-based Dynamic Library Stub -------*- C++ -*-===//
  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. // This file declares the TapiUniversal interface.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_OBJECT_TAPIUNIVERSAL_H
  13. #define LLVM_OBJECT_TAPIUNIVERSAL_H
  14. #include "llvm/Object/Binary.h"
  15. #include "llvm/Object/TapiFile.h"
  16. #include "llvm/Support/Error.h"
  17. #include "llvm/Support/MemoryBuffer.h"
  18. #include "llvm/TextAPI/Architecture.h"
  19. #include "llvm/TextAPI/InterfaceFile.h"
  20. namespace llvm {
  21. namespace object {
  22. class TapiUniversal : public Binary {
  23. public:
  24. class ObjectForArch {
  25. const TapiUniversal *Parent;
  26. int Index;
  27. public:
  28. ObjectForArch(const TapiUniversal *Parent, int Index)
  29. : Parent(Parent), Index(Index) {}
  30. ObjectForArch getNext() const { return ObjectForArch(Parent, Index + 1); }
  31. bool operator==(const ObjectForArch &Other) const {
  32. return (Parent == Other.Parent) && (Index == Other.Index);
  33. }
  34. uint32_t getCPUType() const {
  35. auto Result =
  36. MachO::getCPUTypeFromArchitecture(Parent->Libraries[Index].Arch);
  37. return Result.first;
  38. }
  39. uint32_t getCPUSubType() const {
  40. auto Result =
  41. MachO::getCPUTypeFromArchitecture(Parent->Libraries[Index].Arch);
  42. return Result.second;
  43. }
  44. StringRef getArchFlagName() const {
  45. return MachO::getArchitectureName(Parent->Libraries[Index].Arch);
  46. }
  47. std::string getInstallName() const {
  48. return std::string(Parent->Libraries[Index].InstallName);
  49. }
  50. bool isTopLevelLib() const {
  51. return Parent->ParsedFile->getInstallName() == getInstallName();
  52. }
  53. Expected<std::unique_ptr<TapiFile>> getAsObjectFile() const;
  54. };
  55. class object_iterator {
  56. ObjectForArch Obj;
  57. public:
  58. object_iterator(const ObjectForArch &Obj) : Obj(Obj) {}
  59. const ObjectForArch *operator->() const { return &Obj; }
  60. const ObjectForArch &operator*() const { return Obj; }
  61. bool operator==(const object_iterator &Other) const {
  62. return Obj == Other.Obj;
  63. }
  64. bool operator!=(const object_iterator &Other) const {
  65. return !(*this == Other);
  66. }
  67. object_iterator &operator++() { // Preincrement
  68. Obj = Obj.getNext();
  69. return *this;
  70. }
  71. };
  72. TapiUniversal(MemoryBufferRef Source, Error &Err);
  73. static Expected<std::unique_ptr<TapiUniversal>>
  74. create(MemoryBufferRef Source);
  75. ~TapiUniversal() override;
  76. object_iterator begin_objects() const { return ObjectForArch(this, 0); }
  77. object_iterator end_objects() const {
  78. return ObjectForArch(this, Libraries.size());
  79. }
  80. iterator_range<object_iterator> objects() const {
  81. return make_range(begin_objects(), end_objects());
  82. }
  83. uint32_t getNumberOfObjects() const { return Libraries.size(); }
  84. static bool classof(const Binary *v) { return v->isTapiUniversal(); }
  85. private:
  86. struct Library {
  87. StringRef InstallName;
  88. MachO::Architecture Arch;
  89. };
  90. std::unique_ptr<MachO::InterfaceFile> ParsedFile;
  91. std::vector<Library> Libraries;
  92. };
  93. } // end namespace object.
  94. } // end namespace llvm.
  95. #endif // LLVM_OBJECT_TAPIUNIVERSAL_H