1 //===- llvm/TextAPI/IntefaceFile.h - TAPI Interface File --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief A generic and abstract interface representation for linkable objects.
12 /// This could be an MachO executable, bundle, dylib, or text-based stub
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_TEXTAPI_MACHO_INTERFACE_FILE_H
18 #define LLVM_TEXTAPI_MACHO_INTERFACE_FILE_H
20 #include "llvm/ADT/BitmaskEnum.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/Hashing.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/ADT/iterator.h"
25 #include "llvm/BinaryFormat/MachO.h"
26 #include "llvm/BinaryFormat/Magic.h"
27 #include "llvm/Support/Allocator.h"
28 #include "llvm/Support/Error.h"
29 #include "llvm/TextAPI/MachO/Architecture.h"
30 #include "llvm/TextAPI/MachO/ArchitectureSet.h"
31 #include "llvm/TextAPI/MachO/PackedVersion.h"
32 #include "llvm/TextAPI/MachO/Symbol.h"
37 /// Defines the list of MachO platforms.
38 enum class PlatformKind : unsigned {
40 macOS = MachO::PLATFORM_MACOS,
41 iOS = MachO::PLATFORM_IOS,
42 tvOS = MachO::PLATFORM_TVOS,
43 watchOS = MachO::PLATFORM_WATCHOS,
44 bridgeOS = MachO::PLATFORM_BRIDGEOS,
47 /// Defines a list of Objective-C constraints.
48 enum class ObjCConstraintType : unsigned {
55 /// Retain/Release for Simulator.
56 Retain_Release_For_Simulator = 2,
58 /// Retain/Release or Garbage Collection.
59 Retain_Release_Or_GC = 3,
61 /// Garbage Collection.
67 /// Defines the file type this file represents.
68 enum FileType : unsigned {
69 /// Invalid file type.
72 /// Text-based stub file (.tbd) version 1.0
75 /// Text-based stub file (.tbd) version 2.0
78 /// Text-based stub file (.tbd) version 3.0
83 LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/All),
88 /// Reference to an interface file.
89 class InterfaceFileRef {
91 InterfaceFileRef() = default;
93 InterfaceFileRef(StringRef InstallName) : InstallName(InstallName) {}
95 InterfaceFileRef(StringRef InstallName, ArchitectureSet Archs)
96 : InstallName(InstallName), Architectures(Archs) {}
98 StringRef getInstallName() const { return InstallName; };
99 void addArchitectures(ArchitectureSet Archs) { Architectures |= Archs; }
100 ArchitectureSet getArchitectures() const { return Architectures; }
101 bool hasArchitecture(Architecture Arch) const {
102 return Architectures.has(Arch);
105 bool operator==(const InterfaceFileRef &O) const {
106 return std::tie(InstallName, Architectures) ==
107 std::tie(O.InstallName, O.Architectures);
110 bool operator<(const InterfaceFileRef &O) const {
111 return std::tie(InstallName, Architectures) <
112 std::tie(O.InstallName, O.Architectures);
116 std::string InstallName;
117 ArchitectureSet Architectures;
120 } // end namespace MachO.
122 struct SymbolsMapKey {
123 MachO::SymbolKind Kind;
126 SymbolsMapKey(MachO::SymbolKind Kind, StringRef Name)
127 : Kind(Kind), Name(Name) {}
129 template <> struct DenseMapInfo<SymbolsMapKey> {
130 static inline SymbolsMapKey getEmptyKey() {
131 return SymbolsMapKey(MachO::SymbolKind::GlobalSymbol, StringRef{});
134 static inline SymbolsMapKey getTombstoneKey() {
135 return SymbolsMapKey(MachO::SymbolKind::ObjectiveCInstanceVariable,
139 static unsigned getHashValue(const SymbolsMapKey &Key) {
140 return hash_combine(hash_value(Key.Kind), hash_value(Key.Name));
143 static bool isEqual(const SymbolsMapKey &LHS, const SymbolsMapKey &RHS) {
144 return std::tie(LHS.Kind, LHS.Name) == std::tie(RHS.Kind, RHS.Name);
150 /// Defines the interface file.
151 class InterfaceFile {
153 /// Set the path from which this file was generated (if applicable).
155 /// \param Path_ The path to the source file.
156 void setPath(StringRef Path_) { Path = Path_; }
158 /// Get the path from which this file was generated (if applicable).
160 /// \return The path to the source file or empty.
161 StringRef getPath() const { return Path; }
163 /// Set the file type.
165 /// This is used by the YAML writer to identify the specification it should
166 /// use for writing the file.
168 /// \param Kind The file type.
169 void setFileType(FileType Kind) { FileKind = Kind; }
171 /// Get the file type.
173 /// \return The file type.
174 FileType getFileType() const { return FileKind; }
176 /// Set the platform.
177 void setPlatform(PlatformKind Platform_) { Platform = Platform_; }
179 /// Get the platform.
180 PlatformKind getPlatform() const { return Platform; }
182 /// Specify the set of supported architectures by this file.
183 void setArchitectures(ArchitectureSet Architectures_) {
184 Architectures = Architectures_;
187 /// Add the set of supported architectures by this file.
188 void addArchitectures(ArchitectureSet Architectures_) {
189 Architectures |= Architectures_;
192 /// Add supported architecture by this file..
193 void addArch(Architecture Arch) { Architectures.set(Arch); }
195 /// Get the set of supported architectures.
196 ArchitectureSet getArchitectures() const { return Architectures; }
198 /// Set the install name of the library.
199 void setInstallName(StringRef InstallName_) { InstallName = InstallName_; }
201 /// Get the install name of the library.
202 StringRef getInstallName() const { return InstallName; }
204 /// Set the current version of the library.
205 void setCurrentVersion(PackedVersion Version) { CurrentVersion = Version; }
207 /// Get the current version of the library.
208 PackedVersion getCurrentVersion() const { return CurrentVersion; }
210 /// Set the compatibility version of the library.
211 void setCompatibilityVersion(PackedVersion Version) {
212 CompatibilityVersion = Version;
215 /// Get the compatibility version of the library.
216 PackedVersion getCompatibilityVersion() const { return CompatibilityVersion; }
218 /// Set the Swift ABI version of the library.
219 void setSwiftABIVersion(uint8_t Version) { SwiftABIVersion = Version; }
221 /// Get the Swift ABI version of the library.
222 uint8_t getSwiftABIVersion() const { return SwiftABIVersion; }
224 /// Specify if the library uses two-level namespace (or flat namespace).
225 void setTwoLevelNamespace(bool V = true) { IsTwoLevelNamespace = V; }
227 /// Check if the library uses two-level namespace.
228 bool isTwoLevelNamespace() const { return IsTwoLevelNamespace; }
230 /// Specify if the library is application extension safe (or not).
231 void setApplicationExtensionSafe(bool V = true) { IsAppExtensionSafe = V; }
233 /// Check if the library is application extension safe.
234 bool isApplicationExtensionSafe() const { return IsAppExtensionSafe; }
236 /// Set the Objective-C constraint.
237 void setObjCConstraint(ObjCConstraintType Constraint) {
238 ObjcConstraint = Constraint;
241 /// Get the Objective-C constraint.
242 ObjCConstraintType getObjCConstraint() const { return ObjcConstraint; }
244 /// Specify if this file was generated during InstallAPI (or not).
245 void setInstallAPI(bool V = true) { IsInstallAPI = V; }
247 /// Check if this file was generated during InstallAPI.
248 bool isInstallAPI() const { return IsInstallAPI; }
250 /// Set the parent umbrella framework.
251 void setParentUmbrella(StringRef Parent) { ParentUmbrella = Parent; }
253 /// Get the parent umbrella framework.
254 StringRef getParentUmbrella() const { return ParentUmbrella; }
256 /// Add an allowable client.
258 /// Mach-O Dynamic libraries have the concept of allowable clients that are
259 /// checked during static link time. The name of the application or library
260 /// that is being generated needs to match one of the allowable clients or the
261 /// linker refuses to link this library.
263 /// \param Name The name of the client that is allowed to link this library.
264 /// \param Architectures The set of architecture for which this applies.
265 void addAllowableClient(StringRef Name, ArchitectureSet Architectures);
267 /// Get the list of allowable clients.
269 /// \return Returns a list of allowable clients.
270 const std::vector<InterfaceFileRef> &allowableClients() const {
271 return AllowableClients;
274 /// Add a re-exported library.
276 /// \param InstallName The name of the library to re-export.
277 /// \param Architectures The set of architecture for which this applies.
278 void addReexportedLibrary(StringRef InstallName,
279 ArchitectureSet Architectures);
281 /// Get the list of re-exported libraries.
283 /// \return Returns a list of re-exported libraries.
284 const std::vector<InterfaceFileRef> &reexportedLibraries() const {
285 return ReexportedLibraries;
288 /// Add an architecture/UUID pair.
290 /// \param Arch The architecture for which this applies.
291 /// \param UUID The UUID of the library for the specified architecture.
292 void addUUID(Architecture Arch, StringRef UUID);
294 /// Add an architecture/UUID pair.
296 /// \param Arch The architecture for which this applies.
297 /// \param UUID The UUID of the library for the specified architecture.
298 void addUUID(Architecture Arch, uint8_t UUID[16]);
300 /// Get the list of architecture/UUID pairs.
302 /// \return Returns a list of architecture/UUID pairs.
303 const std::vector<std::pair<Architecture, std::string>> &uuids() const {
307 /// Add a symbol to the symbols list or extend an existing one.
308 void addSymbol(SymbolKind Kind, StringRef Name, ArchitectureSet Architectures,
309 SymbolFlags Flags = SymbolFlags::None);
311 using SymbolMapType = DenseMap<SymbolsMapKey, Symbol *>;
312 struct const_symbol_iterator
313 : public iterator_adaptor_base<
314 const_symbol_iterator, SymbolMapType::const_iterator,
315 std::forward_iterator_tag, const Symbol *, ptrdiff_t,
316 const Symbol *, const Symbol *> {
317 const_symbol_iterator() = default;
319 template <typename U>
320 const_symbol_iterator(U &&u)
321 : iterator_adaptor_base(std::forward<U &&>(u)) {}
323 reference operator*() const { return I->second; }
324 pointer operator->() const { return I->second; }
326 using const_symbol_range = iterator_range<const_symbol_iterator>;
328 // Custom iterator to return only exported symbols.
329 struct const_export_iterator
330 : public iterator_adaptor_base<
331 const_export_iterator, const_symbol_iterator,
332 std::forward_iterator_tag, const Symbol *> {
333 const_symbol_iterator _end;
335 void skipToNextSymbol() {
336 while (I != _end && I->isUndefined())
340 const_export_iterator() = default;
341 template <typename U>
342 const_export_iterator(U &&it, U &&end)
343 : iterator_adaptor_base(std::forward<U &&>(it)),
344 _end(std::forward<U &&>(end)) {
348 const_export_iterator &operator++() {
354 const_export_iterator operator++(int) {
355 const_export_iterator tmp(*this);
360 using const_export_range = llvm::iterator_range<const_export_iterator>;
362 // Custom iterator to return only undefined symbols.
363 struct const_undefined_iterator
364 : public iterator_adaptor_base<
365 const_undefined_iterator, const_symbol_iterator,
366 std::forward_iterator_tag, const Symbol *> {
367 const_symbol_iterator _end;
369 void skipToNextSymbol() {
370 while (I != _end && !I->isUndefined())
374 const_undefined_iterator() = default;
375 template <typename U>
376 const_undefined_iterator(U &&it, U &&end)
377 : iterator_adaptor_base(std::forward<U &&>(it)),
378 _end(std::forward<U &&>(end)) {
382 const_undefined_iterator &operator++() {
388 const_undefined_iterator operator++(int) {
389 const_undefined_iterator tmp(*this);
394 using const_undefined_range = llvm::iterator_range<const_undefined_iterator>;
396 const_symbol_range symbols() const {
397 return {Symbols.begin(), Symbols.end()};
399 const_export_range exports() const {
400 return {{Symbols.begin(), Symbols.end()}, {Symbols.end(), Symbols.end()}};
402 const_undefined_range undefineds() const {
403 return {{Symbols.begin(), Symbols.end()}, {Symbols.end(), Symbols.end()}};
407 llvm::BumpPtrAllocator Allocator;
408 StringRef copyString(StringRef String) {
412 void *Ptr = Allocator.Allocate(String.size(), 1);
413 memcpy(Ptr, String.data(), String.size());
414 return StringRef(reinterpret_cast<const char *>(Ptr), String.size());
419 PlatformKind Platform;
420 ArchitectureSet Architectures;
421 std::string InstallName;
422 PackedVersion CurrentVersion;
423 PackedVersion CompatibilityVersion;
424 uint8_t SwiftABIVersion{0};
425 bool IsTwoLevelNamespace{false};
426 bool IsAppExtensionSafe{false};
427 bool IsInstallAPI{false};
428 ObjCConstraintType ObjcConstraint = ObjCConstraintType::None;
429 std::string ParentUmbrella;
430 std::vector<InterfaceFileRef> AllowableClients;
431 std::vector<InterfaceFileRef> ReexportedLibraries;
432 std::vector<std::pair<Architecture, std::string>> UUIDs;
433 SymbolMapType Symbols;
436 } // end namespace MachO.
437 } // end namespace llvm.
439 #endif // LLVM_TEXTAPI_MACHO_INTERFACE_FILE_H