PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/rv32x-llvm/include/llvm/TextAPI/MachO/InterfaceFile.h

https://gitlab.com/williamwp/riscv-rv32x-llvm
C Header | 436 lines | 250 code | 86 blank | 100 comment | 8 complexity | d6641c5ac057e452f89081ec4f418817 MD5 | raw file
  1. //===- llvm/TextAPI/MachO/IntefaceFile.h - TAPI Interface File --*- 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. // A generic and abstract interface representation for linkable objects. This
  10. // could be an MachO executable, bundle, dylib, or text-based stub file.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_TEXTAPI_MACHO_INTERFACE_FILE_H
  14. #define LLVM_TEXTAPI_MACHO_INTERFACE_FILE_H
  15. #include "llvm/ADT/BitmaskEnum.h"
  16. #include "llvm/ADT/DenseMap.h"
  17. #include "llvm/ADT/Hashing.h"
  18. #include "llvm/ADT/StringRef.h"
  19. #include "llvm/ADT/iterator.h"
  20. #include "llvm/BinaryFormat/MachO.h"
  21. #include "llvm/BinaryFormat/Magic.h"
  22. #include "llvm/Support/Allocator.h"
  23. #include "llvm/Support/Error.h"
  24. #include "llvm/TextAPI/MachO/Architecture.h"
  25. #include "llvm/TextAPI/MachO/ArchitectureSet.h"
  26. #include "llvm/TextAPI/MachO/PackedVersion.h"
  27. #include "llvm/TextAPI/MachO/Symbol.h"
  28. namespace llvm {
  29. namespace MachO {
  30. /// Defines the list of MachO platforms.
  31. enum class PlatformKind : unsigned {
  32. unknown,
  33. macOS = MachO::PLATFORM_MACOS,
  34. iOS = MachO::PLATFORM_IOS,
  35. tvOS = MachO::PLATFORM_TVOS,
  36. watchOS = MachO::PLATFORM_WATCHOS,
  37. bridgeOS = MachO::PLATFORM_BRIDGEOS,
  38. };
  39. /// Defines a list of Objective-C constraints.
  40. enum class ObjCConstraintType : unsigned {
  41. /// No constraint.
  42. None = 0,
  43. /// Retain/Release.
  44. Retain_Release = 1,
  45. /// Retain/Release for Simulator.
  46. Retain_Release_For_Simulator = 2,
  47. /// Retain/Release or Garbage Collection.
  48. Retain_Release_Or_GC = 3,
  49. /// Garbage Collection.
  50. GC = 4,
  51. };
  52. // clang-format off
  53. /// Defines the file type this file represents.
  54. enum FileType : unsigned {
  55. /// Invalid file type.
  56. Invalid = 0U,
  57. /// Text-based stub file (.tbd) version 1.0
  58. TBD_V1 = 1U << 0,
  59. /// Text-based stub file (.tbd) version 2.0
  60. TBD_V2 = 1U << 1,
  61. /// Text-based stub file (.tbd) version 3.0
  62. TBD_V3 = 1U << 2,
  63. All = ~0U,
  64. LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/All),
  65. };
  66. // clang-format on
  67. /// Reference to an interface file.
  68. class InterfaceFileRef {
  69. public:
  70. InterfaceFileRef() = default;
  71. InterfaceFileRef(StringRef InstallName) : InstallName(InstallName) {}
  72. InterfaceFileRef(StringRef InstallName, ArchitectureSet Archs)
  73. : InstallName(InstallName), Architectures(Archs) {}
  74. StringRef getInstallName() const { return InstallName; };
  75. void addArchitectures(ArchitectureSet Archs) { Architectures |= Archs; }
  76. ArchitectureSet getArchitectures() const { return Architectures; }
  77. bool hasArchitecture(Architecture Arch) const {
  78. return Architectures.has(Arch);
  79. }
  80. bool operator==(const InterfaceFileRef &O) const {
  81. return std::tie(InstallName, Architectures) ==
  82. std::tie(O.InstallName, O.Architectures);
  83. }
  84. bool operator<(const InterfaceFileRef &O) const {
  85. return std::tie(InstallName, Architectures) <
  86. std::tie(O.InstallName, O.Architectures);
  87. }
  88. private:
  89. std::string InstallName;
  90. ArchitectureSet Architectures;
  91. };
  92. } // end namespace MachO.
  93. struct SymbolsMapKey {
  94. MachO::SymbolKind Kind;
  95. StringRef Name;
  96. SymbolsMapKey(MachO::SymbolKind Kind, StringRef Name)
  97. : Kind(Kind), Name(Name) {}
  98. };
  99. template <> struct DenseMapInfo<SymbolsMapKey> {
  100. static inline SymbolsMapKey getEmptyKey() {
  101. return SymbolsMapKey(MachO::SymbolKind::GlobalSymbol, StringRef{});
  102. }
  103. static inline SymbolsMapKey getTombstoneKey() {
  104. return SymbolsMapKey(MachO::SymbolKind::ObjectiveCInstanceVariable,
  105. StringRef{});
  106. }
  107. static unsigned getHashValue(const SymbolsMapKey &Key) {
  108. return hash_combine(hash_value(Key.Kind), hash_value(Key.Name));
  109. }
  110. static bool isEqual(const SymbolsMapKey &LHS, const SymbolsMapKey &RHS) {
  111. return std::tie(LHS.Kind, LHS.Name) == std::tie(RHS.Kind, RHS.Name);
  112. }
  113. };
  114. namespace MachO {
  115. /// Defines the interface file.
  116. class InterfaceFile {
  117. public:
  118. /// Set the path from which this file was generated (if applicable).
  119. ///
  120. /// \param Path_ The path to the source file.
  121. void setPath(StringRef Path_) { Path = Path_; }
  122. /// Get the path from which this file was generated (if applicable).
  123. ///
  124. /// \return The path to the source file or empty.
  125. StringRef getPath() const { return Path; }
  126. /// Set the file type.
  127. ///
  128. /// This is used by the YAML writer to identify the specification it should
  129. /// use for writing the file.
  130. ///
  131. /// \param Kind The file type.
  132. void setFileType(FileType Kind) { FileKind = Kind; }
  133. /// Get the file type.
  134. ///
  135. /// \return The file type.
  136. FileType getFileType() const { return FileKind; }
  137. /// Set the platform.
  138. void setPlatform(PlatformKind Platform_) { Platform = Platform_; }
  139. /// Get the platform.
  140. PlatformKind getPlatform() const { return Platform; }
  141. /// Specify the set of supported architectures by this file.
  142. void setArchitectures(ArchitectureSet Architectures_) {
  143. Architectures = Architectures_;
  144. }
  145. /// Add the set of supported architectures by this file.
  146. void addArchitectures(ArchitectureSet Architectures_) {
  147. Architectures |= Architectures_;
  148. }
  149. /// Add supported architecture by this file..
  150. void addArch(Architecture Arch) { Architectures.set(Arch); }
  151. /// Get the set of supported architectures.
  152. ArchitectureSet getArchitectures() const { return Architectures; }
  153. /// Set the install name of the library.
  154. void setInstallName(StringRef InstallName_) { InstallName = InstallName_; }
  155. /// Get the install name of the library.
  156. StringRef getInstallName() const { return InstallName; }
  157. /// Set the current version of the library.
  158. void setCurrentVersion(PackedVersion Version) { CurrentVersion = Version; }
  159. /// Get the current version of the library.
  160. PackedVersion getCurrentVersion() const { return CurrentVersion; }
  161. /// Set the compatibility version of the library.
  162. void setCompatibilityVersion(PackedVersion Version) {
  163. CompatibilityVersion = Version;
  164. }
  165. /// Get the compatibility version of the library.
  166. PackedVersion getCompatibilityVersion() const { return CompatibilityVersion; }
  167. /// Set the Swift ABI version of the library.
  168. void setSwiftABIVersion(uint8_t Version) { SwiftABIVersion = Version; }
  169. /// Get the Swift ABI version of the library.
  170. uint8_t getSwiftABIVersion() const { return SwiftABIVersion; }
  171. /// Specify if the library uses two-level namespace (or flat namespace).
  172. void setTwoLevelNamespace(bool V = true) { IsTwoLevelNamespace = V; }
  173. /// Check if the library uses two-level namespace.
  174. bool isTwoLevelNamespace() const { return IsTwoLevelNamespace; }
  175. /// Specify if the library is application extension safe (or not).
  176. void setApplicationExtensionSafe(bool V = true) { IsAppExtensionSafe = V; }
  177. /// Check if the library is application extension safe.
  178. bool isApplicationExtensionSafe() const { return IsAppExtensionSafe; }
  179. /// Set the Objective-C constraint.
  180. void setObjCConstraint(ObjCConstraintType Constraint) {
  181. ObjcConstraint = Constraint;
  182. }
  183. /// Get the Objective-C constraint.
  184. ObjCConstraintType getObjCConstraint() const { return ObjcConstraint; }
  185. /// Specify if this file was generated during InstallAPI (or not).
  186. void setInstallAPI(bool V = true) { IsInstallAPI = V; }
  187. /// Check if this file was generated during InstallAPI.
  188. bool isInstallAPI() const { return IsInstallAPI; }
  189. /// Set the parent umbrella framework.
  190. void setParentUmbrella(StringRef Parent) { ParentUmbrella = Parent; }
  191. /// Get the parent umbrella framework.
  192. StringRef getParentUmbrella() const { return ParentUmbrella; }
  193. /// Add an allowable client.
  194. ///
  195. /// Mach-O Dynamic libraries have the concept of allowable clients that are
  196. /// checked during static link time. The name of the application or library
  197. /// that is being generated needs to match one of the allowable clients or the
  198. /// linker refuses to link this library.
  199. ///
  200. /// \param Name The name of the client that is allowed to link this library.
  201. /// \param Architectures The set of architecture for which this applies.
  202. void addAllowableClient(StringRef Name, ArchitectureSet Architectures);
  203. /// Get the list of allowable clients.
  204. ///
  205. /// \return Returns a list of allowable clients.
  206. const std::vector<InterfaceFileRef> &allowableClients() const {
  207. return AllowableClients;
  208. }
  209. /// Add a re-exported library.
  210. ///
  211. /// \param InstallName The name of the library to re-export.
  212. /// \param Architectures The set of architecture for which this applies.
  213. void addReexportedLibrary(StringRef InstallName,
  214. ArchitectureSet Architectures);
  215. /// Get the list of re-exported libraries.
  216. ///
  217. /// \return Returns a list of re-exported libraries.
  218. const std::vector<InterfaceFileRef> &reexportedLibraries() const {
  219. return ReexportedLibraries;
  220. }
  221. /// Add an architecture/UUID pair.
  222. ///
  223. /// \param Arch The architecture for which this applies.
  224. /// \param UUID The UUID of the library for the specified architecture.
  225. void addUUID(Architecture Arch, StringRef UUID);
  226. /// Add an architecture/UUID pair.
  227. ///
  228. /// \param Arch The architecture for which this applies.
  229. /// \param UUID The UUID of the library for the specified architecture.
  230. void addUUID(Architecture Arch, uint8_t UUID[16]);
  231. /// Get the list of architecture/UUID pairs.
  232. ///
  233. /// \return Returns a list of architecture/UUID pairs.
  234. const std::vector<std::pair<Architecture, std::string>> &uuids() const {
  235. return UUIDs;
  236. }
  237. /// Add a symbol to the symbols list or extend an existing one.
  238. void addSymbol(SymbolKind Kind, StringRef Name, ArchitectureSet Architectures,
  239. SymbolFlags Flags = SymbolFlags::None);
  240. using SymbolMapType = DenseMap<SymbolsMapKey, Symbol *>;
  241. struct const_symbol_iterator
  242. : public iterator_adaptor_base<
  243. const_symbol_iterator, SymbolMapType::const_iterator,
  244. std::forward_iterator_tag, const Symbol *, ptrdiff_t,
  245. const Symbol *, const Symbol *> {
  246. const_symbol_iterator() = default;
  247. template <typename U>
  248. const_symbol_iterator(U &&u)
  249. : iterator_adaptor_base(std::forward<U &&>(u)) {}
  250. reference operator*() const { return I->second; }
  251. pointer operator->() const { return I->second; }
  252. };
  253. using const_symbol_range = iterator_range<const_symbol_iterator>;
  254. // Custom iterator to return only exported symbols.
  255. struct const_export_iterator
  256. : public iterator_adaptor_base<
  257. const_export_iterator, const_symbol_iterator,
  258. std::forward_iterator_tag, const Symbol *> {
  259. const_symbol_iterator _end;
  260. void skipToNextSymbol() {
  261. while (I != _end && I->isUndefined())
  262. ++I;
  263. }
  264. const_export_iterator() = default;
  265. template <typename U>
  266. const_export_iterator(U &&it, U &&end)
  267. : iterator_adaptor_base(std::forward<U &&>(it)),
  268. _end(std::forward<U &&>(end)) {
  269. skipToNextSymbol();
  270. }
  271. const_export_iterator &operator++() {
  272. ++I;
  273. skipToNextSymbol();
  274. return *this;
  275. }
  276. const_export_iterator operator++(int) {
  277. const_export_iterator tmp(*this);
  278. ++(*this);
  279. return tmp;
  280. }
  281. };
  282. using const_export_range = llvm::iterator_range<const_export_iterator>;
  283. // Custom iterator to return only undefined symbols.
  284. struct const_undefined_iterator
  285. : public iterator_adaptor_base<
  286. const_undefined_iterator, const_symbol_iterator,
  287. std::forward_iterator_tag, const Symbol *> {
  288. const_symbol_iterator _end;
  289. void skipToNextSymbol() {
  290. while (I != _end && !I->isUndefined())
  291. ++I;
  292. }
  293. const_undefined_iterator() = default;
  294. template <typename U>
  295. const_undefined_iterator(U &&it, U &&end)
  296. : iterator_adaptor_base(std::forward<U &&>(it)),
  297. _end(std::forward<U &&>(end)) {
  298. skipToNextSymbol();
  299. }
  300. const_undefined_iterator &operator++() {
  301. ++I;
  302. skipToNextSymbol();
  303. return *this;
  304. }
  305. const_undefined_iterator operator++(int) {
  306. const_undefined_iterator tmp(*this);
  307. ++(*this);
  308. return tmp;
  309. }
  310. };
  311. using const_undefined_range = llvm::iterator_range<const_undefined_iterator>;
  312. const_symbol_range symbols() const {
  313. return {Symbols.begin(), Symbols.end()};
  314. }
  315. const_export_range exports() const {
  316. return {{Symbols.begin(), Symbols.end()}, {Symbols.end(), Symbols.end()}};
  317. }
  318. const_undefined_range undefineds() const {
  319. return {{Symbols.begin(), Symbols.end()}, {Symbols.end(), Symbols.end()}};
  320. }
  321. private:
  322. llvm::BumpPtrAllocator Allocator;
  323. StringRef copyString(StringRef String) {
  324. if (String.empty())
  325. return {};
  326. void *Ptr = Allocator.Allocate(String.size(), 1);
  327. memcpy(Ptr, String.data(), String.size());
  328. return StringRef(reinterpret_cast<const char *>(Ptr), String.size());
  329. }
  330. std::string Path;
  331. FileType FileKind;
  332. PlatformKind Platform;
  333. ArchitectureSet Architectures;
  334. std::string InstallName;
  335. PackedVersion CurrentVersion;
  336. PackedVersion CompatibilityVersion;
  337. uint8_t SwiftABIVersion{0};
  338. bool IsTwoLevelNamespace{false};
  339. bool IsAppExtensionSafe{false};
  340. bool IsInstallAPI{false};
  341. ObjCConstraintType ObjcConstraint = ObjCConstraintType::None;
  342. std::string ParentUmbrella;
  343. std::vector<InterfaceFileRef> AllowableClients;
  344. std::vector<InterfaceFileRef> ReexportedLibraries;
  345. std::vector<std::pair<Architecture, std::string>> UUIDs;
  346. SymbolMapType Symbols;
  347. };
  348. } // end namespace MachO.
  349. } // end namespace llvm.
  350. #endif // LLVM_TEXTAPI_MACHO_INTERFACE_FILE_H