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
; }
324 using const_symbol_range
= iterator_range
<const_symbol_iterator
>;
326 using const_filtered_symbol_iterator
=
327 filter_iterator
<const_symbol_iterator
,
328 std::function
<bool(const Symbol
*)>>;
329 using const_filtered_symbol_range
=
330 iterator_range
<const_filtered_symbol_iterator
>;
332 const_symbol_range
symbols() const {
333 return {Symbols
.begin(), Symbols
.end()};
336 const_filtered_symbol_range
exports() const {
337 std::function
<bool(const Symbol
*)> fn
= [](const Symbol
*Symbol
) {
338 return !Symbol
->isUndefined();
340 return make_filter_range(
341 make_range
<const_symbol_iterator
>({Symbols
.begin()}, {Symbols
.end()}),
345 const_filtered_symbol_range
undefineds() const {
346 std::function
<bool(const Symbol
*)> fn
= [](const Symbol
*Symbol
) {
347 return Symbol
->isUndefined();
349 return make_filter_range(
350 make_range
<const_symbol_iterator
>({Symbols
.begin()}, {Symbols
.end()}),
355 llvm::BumpPtrAllocator Allocator
;
356 StringRef
copyString(StringRef String
) {
360 void *Ptr
= Allocator
.Allocate(String
.size(), 1);
361 memcpy(Ptr
, String
.data(), String
.size());
362 return StringRef(reinterpret_cast<const char *>(Ptr
), String
.size());
367 PlatformKind Platform
;
368 ArchitectureSet Architectures
;
369 std::string InstallName
;
370 PackedVersion CurrentVersion
;
371 PackedVersion CompatibilityVersion
;
372 uint8_t SwiftABIVersion
{0};
373 bool IsTwoLevelNamespace
{false};
374 bool IsAppExtensionSafe
{false};
375 bool IsInstallAPI
{false};
376 ObjCConstraintType ObjcConstraint
= ObjCConstraintType::None
;
377 std::string ParentUmbrella
;
378 std::vector
<InterfaceFileRef
> AllowableClients
;
379 std::vector
<InterfaceFileRef
> ReexportedLibraries
;
380 std::vector
<std::pair
<Architecture
, std::string
>> UUIDs
;
381 SymbolMapType Symbols
;
384 } // end namespace MachO.
385 } // end namespace llvm.
387 #endif // LLVM_TEXTAPI_MACHO_INTERFACE_FILE_H