TapiUniversal.h 3.6 KB

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