1 //===- llvm/TextAPI/MachO/IntefaceFile.h - TAPI Interface File --*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // A generic and abstract interface representation for linkable objects. This
10 // could be an MachO executable, bundle, dylib, or text-based stub file.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_TEXTAPI_MACHO_INTERFACE_FILE_H
15 #define LLVM_TEXTAPI_MACHO_INTERFACE_FILE_H
17 #include "llvm/ADT/BitmaskEnum.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/Hashing.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/iterator.h"
22 #include "llvm/BinaryFormat/MachO.h"
23 #include "llvm/BinaryFormat/Magic.h"
24 #include "llvm/Support/Allocator.h"
25 #include "llvm/Support/Error.h"
26 #include "llvm/TextAPI/MachO/Architecture.h"
27 #include "llvm/TextAPI/MachO/ArchitectureSet.h"
28 #include "llvm/TextAPI/MachO/PackedVersion.h"
29 #include "llvm/TextAPI/MachO/Symbol.h"
34 /// Defines the list of MachO platforms.
35 enum class PlatformKind : unsigned {
37 macOS = MachO::PLATFORM_MACOS,
38 iOS = MachO::PLATFORM_IOS,
39 tvOS = MachO::PLATFORM_TVOS,
40 watchOS = MachO::PLATFORM_WATCHOS,
41 bridgeOS = MachO::PLATFORM_BRIDGEOS,
44 /// Defines a list of Objective-C constraints.
45 enum class ObjCConstraintType : unsigned {
52 /// Retain/Release for Simulator.
53 Retain_Release_For_Simulator = 2,
55 /// Retain/Release or Garbage Collection.
56 Retain_Release_Or_GC = 3,
58 /// Garbage Collection.
64 /// Defines the file type this file represents.
65 enum FileType : unsigned {
66 /// Invalid file type.
69 /// Text-based stub file (.tbd) version 1.0
72 /// Text-based stub file (.tbd) version 2.0
75 /// Text-based stub file (.tbd) version 3.0
80 LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/All),
85 /// Reference to an interface file.
86 class InterfaceFileRef {
88 InterfaceFileRef() = default;
90 InterfaceFileRef(StringRef InstallName) : InstallName(InstallName) {}
92 InterfaceFileRef(StringRef InstallName, ArchitectureSet Archs)
93 : InstallName(InstallName), Architectures(Archs) {}
95 StringRef getInstallName() const { return InstallName; };
96 void addArchitectures(ArchitectureSet Archs) { Architectures |= Archs; }
97 ArchitectureSet getArchitectures() const { return Architectures; }
98 bool hasArchitecture(Architecture Arch) const {
99 return Architectures.has(Arch);
102 bool operator==(const InterfaceFileRef &O) const {
103 return std::tie(InstallName, Architectures) ==
104 std::tie(O.InstallName, O.Architectures);
107 bool operator<(const InterfaceFileRef &O) const {
108 return std::tie(InstallName, Architectures) <
109 std::tie(O.InstallName, O.Architectures);
113 std::string InstallName;
114 ArchitectureSet Architectures;
117 } // end namespace MachO.
119 struct SymbolsMapKey {
120 MachO::SymbolKind Kind;
123 SymbolsMapKey(MachO::SymbolKind Kind, StringRef Name)
124 : Kind(Kind), Name(Name) {}
126 template <> struct DenseMapInfo<SymbolsMapKey> {
127 static inline SymbolsMapKey getEmptyKey() {
128 return SymbolsMapKey(MachO::SymbolKind::GlobalSymbol, StringRef{});
131 static inline SymbolsMapKey getTombstoneKey() {
132 return SymbolsMapKey(MachO::SymbolKind::ObjectiveCInstanceVariable,
136 static unsigned getHashValue(const SymbolsMapKey &Key) {
137 return hash_combine(hash_value(Key.Kind), hash_value(Key.Name));
140 static bool isEqual(const SymbolsMapKey &LHS, const SymbolsMapKey &RHS) {
141 return std::tie(LHS.Kind, LHS.Name) == std::tie(RHS.Kind, RHS.Name);
147 /// Defines the interface file.
148 class InterfaceFile {
150 /// Set the path from which this file was generated (if applicable).
152 /// \param Path_ The path to the source file.
153 void setPath(StringRef Path_) { Path = Path_; }
155 /// Get the path from which this file was generated (if applicable).
157 /// \return The path to the source file or empty.
158 StringRef getPath() const { return Path; }
160 /// Set the file type.
162 /// This is used by the YAML writer to identify the specification it should
163 /// use for writing the file.
165 /// \param Kind The file type.
166 void setFileType(FileType Kind) { FileKind = Kind; }
168 /// Get the file type.
170 /// \return The file type.
171 FileType getFileType() const { return FileKind; }
173 /// Set the platform.
174 void setPlatform(PlatformKind Platform_) { Platform = Platform_; }
176 /// Get the platform.
177 PlatformKind getPlatform() const { return Platform; }
179 /// Specify the set of supported architectures by this file.
180 void setArchitectures(ArchitectureSet Architectures_) {
181 Architectures = Architectures_;
184 /// Add the set of supported architectures by this file.
185 void addArchitectures(ArchitectureSet Architectures_) {
186 Architectures |= Architectures_;
189 /// Add supported architecture by this file..
190 void addArch(Architecture Arch) { Architectures.set(Arch); }
192 /// Get the set of supported architectures.
193 ArchitectureSet getArchitectures() const { return Architectures; }
195 /// Set the install name of the library.
196 void setInstallName(StringRef InstallName_) { InstallName = InstallName_; }
198 /// Get the install name of the library.
199 StringRef getInstallName() const { return InstallName; }
201 /// Set the current version of the library.
202 void setCurrentVersion(PackedVersion Version) { CurrentVersion = Version; }
204 /// Get the current version of the library.
205 PackedVersion getCurrentVersion() const { return CurrentVersion; }
207 /// Set the compatibility version of the library.
208 void setCompatibilityVersion(PackedVersion Version) {
209 CompatibilityVersion = Version;
212 /// Get the compatibility version of the library.
213 PackedVersion getCompatibilityVersion() const { return CompatibilityVersion; }
215 /// Set the Swift ABI version of the library.
216 void setSwiftABIVersion(uint8_t Version) { SwiftABIVersion = Version; }
218 /// Get the Swift ABI version of the library.
219 uint8_t getSwiftABIVersion() const { return SwiftABIVersion; }
221 /// Specify if the library uses two-level namespace (or flat namespace).
222 void setTwoLevelNamespace(bool V = true) { IsTwoLevelNamespace = V; }
224 /// Check if the library uses two-level namespace.
225 bool isTwoLevelNamespace() const { return IsTwoLevelNamespace; }
227 /// Specify if the library is application extension safe (or not).
228 void setApplicationExtensionSafe(bool V = true) { IsAppExtensionSafe = V; }
230 /// Check if the library is application extension safe.
231 bool isApplicationExtensionSafe() const { return IsAppExtensionSafe; }
233 /// Set the Objective-C constraint.
234 void setObjCConstraint(ObjCConstraintType Constraint) {
235 ObjcConstraint = Constraint;
238 /// Get the Objective-C constraint.
239 ObjCConstraintType getObjCConstraint() const { return ObjcConstraint; }
241 /// Specify if this file was generated during InstallAPI (or not).
242 void setInstallAPI(bool V = true) { IsInstallAPI = V; }
244 /// Check if this file was generated during InstallAPI.
245 bool isInstallAPI() const { return IsInstallAPI; }
247 /// Set the parent umbrella framework.
248 void setParentUmbrella(StringRef Parent) { ParentUmbrella = Parent; }
250 /// Get the parent umbrella framework.
251 StringRef getParentUmbrella() const { return ParentUmbrella; }
253 /// Add an allowable client.
255 /// Mach-O Dynamic libraries have the concept of allowable clients that are
256 /// checked during static link time. The name of the application or library
257 /// that is being generated needs to match one of the allowable clients or the
258 /// linker refuses to link this library.
260 /// \param Name The name of the client that is allowed to link this library.
261 /// \param Architectures The set of architecture for which this applies.
262 void addAllowableClient(StringRef Name, ArchitectureSet Architectures);
264 /// Get the list of allowable clients.
266 /// \return Returns a list of allowable clients.
267 const std::vector<InterfaceFileRef> &allowableClients() const {
268 return AllowableClients;
271 /// Add a re-exported library.
273 /// \param InstallName The name of the library to re-export.
274 /// \param Architectures The set of architecture for which this applies.
275 void addReexportedLibrary(StringRef InstallName,
276 ArchitectureSet Architectures);
278 /// Get the list of re-exported libraries.
280 /// \return Returns a list of re-exported libraries.
281 const std::vector<InterfaceFileRef> &reexportedLibraries() const {
282 return ReexportedLibraries;
285 /// Add an architecture/UUID pair.
287 /// \param Arch The architecture for which this applies.
288 /// \param UUID The UUID of the library for the specified architecture.
289 void addUUID(Architecture Arch, StringRef UUID);
291 /// Add an architecture/UUID pair.
293 /// \param Arch The architecture for which this applies.
294 /// \param UUID The UUID of the library for the specified architecture.
295 void addUUID(Architecture Arch, uint8_t UUID[16]);
297 /// Get the list of architecture/UUID pairs.
299 /// \return Returns a list of architecture/UUID pairs.
300 const std::vector<std::pair<Architecture, std::string>> &uuids() const {
304 /// Add a symbol to the symbols list or extend an existing one.
305 void addSymbol(SymbolKind Kind, StringRef Name, ArchitectureSet Architectures,
306 SymbolFlags Flags = SymbolFlags::None);
308 using SymbolMapType = DenseMap<SymbolsMapKey, Symbol *>;
309 struct const_symbol_iterator
310 : public iterator_adaptor_base<
311 const_symbol_iterator, SymbolMapType::const_iterator,
312 std::forward_iterator_tag, const Symbol *, ptrdiff_t,
313 const Symbol *, const Symbol *> {
314 const_symbol_iterator() = default;
316 template <typename U>
317 const_symbol_iterator(U &&u)
318 : iterator_adaptor_base(std::forward<U &&>(u)) {}
320 reference operator*() const { return I->second; }
321 pointer operator->() const { return I->second; }
323 using const_symbol_range = iterator_range<const_symbol_iterator>;
325 // Custom iterator to return only exported symbols.
326 struct const_export_iterator
327 : public iterator_adaptor_base<
328 const_export_iterator, const_symbol_iterator,
329 std::forward_iterator_tag, const Symbol *> {
330 const_symbol_iterator _end;
332 void skipToNextSymbol() {
333 while (I != _end && I->isUndefined())
337 const_export_iterator() = default;
338 template <typename U>
339 const_export_iterator(U &&it, U &&end)
340 : iterator_adaptor_base(std::forward<U &&>(it)),
341 _end(std::forward<U &&>(end)) {
345 const_export_iterator &operator++() {
351 const_export_iterator operator++(int) {
352 const_export_iterator tmp(*this);
357 using const_export_range = llvm::iterator_range<const_export_iterator>;
359 // Custom iterator to return only undefined symbols.
360 struct const_undefined_iterator
361 : public iterator_adaptor_base<
362 const_undefined_iterator, const_symbol_iterator,
363 std::forward_iterator_tag, const Symbol *> {
364 const_symbol_iterator _end;
366 void skipToNextSymbol() {
367 while (I != _end && !I->isUndefined())
371 const_undefined_iterator() = default;
372 template <typename U>
373 const_undefined_iterator(U &&it, U &&end)
374 : iterator_adaptor_base(std::forward<U &&>(it)),
375 _end(std::forward<U &&>(end)) {
379 const_undefined_iterator &operator++() {
385 const_undefined_iterator operator++(int) {
386 const_undefined_iterator tmp(*this);
391 using const_undefined_range = llvm::iterator_range<const_undefined_iterator>;
393 const_symbol_range symbols() const {
394 return {Symbols.begin(), Symbols.end()};
396 const_export_range exports() const {
397 return {{Symbols.begin(), Symbols.end()}, {Symbols.end(), Symbols.end()}};
399 const_undefined_range undefineds() const {
400 return {{Symbols.begin(), Symbols.end()}, {Symbols.end(), Symbols.end()}};
404 llvm::BumpPtrAllocator Allocator;
405 StringRef copyString(StringRef String) {
409 void *Ptr = Allocator.Allocate(String.size(), 1);
410 memcpy(Ptr, String.data(), String.size());
411 return StringRef(reinterpret_cast<const char *>(Ptr), String.size());
416 PlatformKind Platform;
417 ArchitectureSet Architectures;
418 std::string InstallName;
419 PackedVersion CurrentVersion;
420 PackedVersion CompatibilityVersion;
421 uint8_t SwiftABIVersion{0};
422 bool IsTwoLevelNamespace{false};
423 bool IsAppExtensionSafe{false};
424 bool IsInstallAPI{false};
425 ObjCConstraintType ObjcConstraint = ObjCConstraintType::None;
426 std::string ParentUmbrella;
427 std::vector<InterfaceFileRef> AllowableClients;
428 std::vector<InterfaceFileRef> ReexportedLibraries;
429 std::vector<std::pair<Architecture, std::string>> UUIDs;
430 SymbolMapType Symbols;
433 } // end namespace MachO.
434 } // end namespace llvm.
436 #endif // LLVM_TEXTAPI_MACHO_INTERFACE_FILE_H