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/Platform.h"
30 #include "llvm/TextAPI/MachO/Symbol.h"
31 #include "llvm/TextAPI/MachO/Target.h"
36 /// Defines a list of Objective-C constraints.
37 enum class ObjCConstraintType
: unsigned {
44 /// Retain/Release for Simulator.
45 Retain_Release_For_Simulator
= 2,
47 /// Retain/Release or Garbage Collection.
48 Retain_Release_Or_GC
= 3,
50 /// Garbage Collection.
56 /// Defines the file type this file represents.
57 enum FileType
: unsigned {
58 /// Invalid file type.
61 /// Text-based stub file (.tbd) version 1.0
64 /// Text-based stub file (.tbd) version 2.0
67 /// Text-based stub file (.tbd) version 3.0
72 LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/All
),
77 /// Reference to an interface file.
78 class InterfaceFileRef
{
80 InterfaceFileRef() = default;
82 InterfaceFileRef(StringRef InstallName
) : InstallName(InstallName
) {}
84 InterfaceFileRef(StringRef InstallName
, const TargetList Targets
)
85 : InstallName(InstallName
), Targets(std::move(Targets
)) {}
87 StringRef
getInstallName() const { return InstallName
; };
89 void addTarget(const Target
&Target
);
90 template <typename RangeT
> void addTargets(RangeT
&&Targets
) {
91 for (const auto &Target
: Targets
)
92 addTarget(Target(Target
));
95 using const_target_iterator
= TargetList::const_iterator
;
96 using const_target_range
= llvm::iterator_range
<const_target_iterator
>;
97 const_target_range
targets() const { return {Targets
}; }
99 ArchitectureSet
getArchitectures() const {
100 return mapToArchitectureSet(Targets
);
103 PlatformSet
getPlatforms() const { return mapToPlatformSet(Targets
); }
105 bool operator==(const InterfaceFileRef
&O
) const {
106 return std::tie(InstallName
, Targets
) == std::tie(O
.InstallName
, O
.Targets
);
109 bool operator!=(const InterfaceFileRef
&O
) const {
110 return std::tie(InstallName
, Targets
) != std::tie(O
.InstallName
, O
.Targets
);
113 bool operator<(const InterfaceFileRef
&O
) const {
114 return std::tie(InstallName
, Targets
) < std::tie(O
.InstallName
, O
.Targets
);
118 std::string InstallName
;
122 } // end namespace MachO.
124 struct SymbolsMapKey
{
125 MachO::SymbolKind Kind
;
128 SymbolsMapKey(MachO::SymbolKind Kind
, StringRef Name
)
129 : Kind(Kind
), Name(Name
) {}
131 template <> struct DenseMapInfo
<SymbolsMapKey
> {
132 static inline SymbolsMapKey
getEmptyKey() {
133 return SymbolsMapKey(MachO::SymbolKind::GlobalSymbol
, StringRef
{});
136 static inline SymbolsMapKey
getTombstoneKey() {
137 return SymbolsMapKey(MachO::SymbolKind::ObjectiveCInstanceVariable
,
141 static unsigned getHashValue(const SymbolsMapKey
&Key
) {
142 return hash_combine(hash_value(Key
.Kind
), hash_value(Key
.Name
));
145 static bool isEqual(const SymbolsMapKey
&LHS
, const SymbolsMapKey
&RHS
) {
146 return std::tie(LHS
.Kind
, LHS
.Name
) == std::tie(RHS
.Kind
, RHS
.Name
);
152 /// Defines the interface file.
153 class InterfaceFile
{
155 /// Set the path from which this file was generated (if applicable).
157 /// \param Path_ The path to the source file.
158 void setPath(StringRef Path_
) { Path
= Path_
; }
160 /// Get the path from which this file was generated (if applicable).
162 /// \return The path to the source file or empty.
163 StringRef
getPath() const { return Path
; }
165 /// Set the file type.
167 /// This is used by the YAML writer to identify the specification it should
168 /// use for writing the file.
170 /// \param Kind The file type.
171 void setFileType(FileType Kind
) { FileKind
= Kind
; }
173 /// Get the file type.
175 /// \return The file type.
176 FileType
getFileType() const { return FileKind
; }
178 /// Get the architectures.
180 /// \return The applicable architectures.
181 ArchitectureSet
getArchitectures() const {
182 return mapToArchitectureSet(Targets
);
185 /// Get the platforms.
187 /// \return The applicable platforms.
188 PlatformSet
getPlatforms() const { return mapToPlatformSet(Targets
); }
190 /// Set and add target.
192 /// \param Target the target to add into.
193 void addTarget(const Target
&Target
);
195 /// Set and add targets.
197 /// Add the subset of llvm::triples that is supported by Tapi
199 /// \param Targets the collection of targets.
200 template <typename RangeT
> void addTargets(RangeT
&&Targets
) {
201 for (const auto &Target_
: Targets
)
202 addTarget(Target(Target_
));
205 using const_target_iterator
= TargetList::const_iterator
;
206 using const_target_range
= llvm::iterator_range
<const_target_iterator
>;
207 const_target_range
targets() const { return {Targets
}; }
209 using const_filtered_target_iterator
=
210 llvm::filter_iterator
<const_target_iterator
,
211 std::function
<bool(const Target
&)>>;
212 using const_filtered_target_range
=
213 llvm::iterator_range
<const_filtered_target_iterator
>;
214 const_filtered_target_range
targets(ArchitectureSet Archs
) const;
216 /// Set the install name of the library.
217 void setInstallName(StringRef InstallName_
) { InstallName
= InstallName_
; }
219 /// Get the install name of the library.
220 StringRef
getInstallName() const { return InstallName
; }
222 /// Set the current version of the library.
223 void setCurrentVersion(PackedVersion Version
) { CurrentVersion
= Version
; }
225 /// Get the current version of the library.
226 PackedVersion
getCurrentVersion() const { return CurrentVersion
; }
228 /// Set the compatibility version of the library.
229 void setCompatibilityVersion(PackedVersion Version
) {
230 CompatibilityVersion
= Version
;
233 /// Get the compatibility version of the library.
234 PackedVersion
getCompatibilityVersion() const { return CompatibilityVersion
; }
236 /// Set the Swift ABI version of the library.
237 void setSwiftABIVersion(uint8_t Version
) { SwiftABIVersion
= Version
; }
239 /// Get the Swift ABI version of the library.
240 uint8_t getSwiftABIVersion() const { return SwiftABIVersion
; }
242 /// Specify if the library uses two-level namespace (or flat namespace).
243 void setTwoLevelNamespace(bool V
= true) { IsTwoLevelNamespace
= V
; }
245 /// Check if the library uses two-level namespace.
246 bool isTwoLevelNamespace() const { return IsTwoLevelNamespace
; }
248 /// Specify if the library is application extension safe (or not).
249 void setApplicationExtensionSafe(bool V
= true) { IsAppExtensionSafe
= V
; }
251 /// Check if the library is application extension safe.
252 bool isApplicationExtensionSafe() const { return IsAppExtensionSafe
; }
254 /// Set the Objective-C constraint.
255 void setObjCConstraint(ObjCConstraintType Constraint
) {
256 ObjcConstraint
= Constraint
;
259 /// Get the Objective-C constraint.
260 ObjCConstraintType
getObjCConstraint() const { return ObjcConstraint
; }
262 /// Specify if this file was generated during InstallAPI (or not).
263 void setInstallAPI(bool V
= true) { IsInstallAPI
= V
; }
265 /// Check if this file was generated during InstallAPI.
266 bool isInstallAPI() const { return IsInstallAPI
; }
268 /// Set the parent umbrella frameworks.
269 /// \param Target_ The target applicable to Parent
270 /// \param Parent The name of Parent
271 void addParentUmbrella(const Target
&Target_
, StringRef Parent
);
272 const std::vector
<std::pair
<Target
, std::string
>> &umbrellas() const {
273 return ParentUmbrellas
;
276 /// Get the parent umbrella framework.
277 const std::vector
<std::pair
<Target
, std::string
>> getParentUmbrellas() const {
278 return ParentUmbrellas
;
281 /// Add an allowable client.
283 /// Mach-O Dynamic libraries have the concept of allowable clients that are
284 /// checked during static link time. The name of the application or library
285 /// that is being generated needs to match one of the allowable clients or the
286 /// linker refuses to link this library.
288 /// \param InstallName The name of the client that is allowed to link this library.
289 /// \param Target The target triple for which this applies.
290 void addAllowableClient(StringRef InstallName
, const Target
&Target
);
292 /// Get the list of allowable clients.
294 /// \return Returns a list of allowable clients.
295 const std::vector
<InterfaceFileRef
> &allowableClients() const {
296 return AllowableClients
;
299 /// Add a re-exported library.
301 /// \param InstallName The name of the library to re-export.
302 /// \param Target The target triple for which this applies.
303 void addReexportedLibrary(StringRef InstallName
, const Target
&Target
);
305 /// Get the list of re-exported libraries.
307 /// \return Returns a list of re-exported libraries.
308 const std::vector
<InterfaceFileRef
> &reexportedLibraries() const {
309 return ReexportedLibraries
;
312 /// Add an Target/UUID pair.
314 /// \param Target The target triple for which this applies.
315 /// \param UUID The UUID of the library for the specified architecture.
316 void addUUID(const Target
&Target
, StringRef UUID
);
318 /// Add an Target/UUID pair.
320 /// \param Target The target triple for which this applies.
321 /// \param UUID The UUID of the library for the specified architecture.
322 void addUUID(const Target
&Target
, uint8_t UUID
[16]);
324 /// Get the list of Target/UUID pairs.
326 /// \return Returns a list of Target/UUID pairs.
327 const std::vector
<std::pair
<Target
, std::string
>> &uuids() const {
331 /// Add a symbol to the symbols list or extend an existing one.
332 void addSymbol(SymbolKind Kind
, StringRef Name
, const TargetList
&Targets
,
333 SymbolFlags Flags
= SymbolFlags::None
);
335 using SymbolMapType
= DenseMap
<SymbolsMapKey
, Symbol
*>;
336 struct const_symbol_iterator
337 : public iterator_adaptor_base
<
338 const_symbol_iterator
, SymbolMapType::const_iterator
,
339 std::forward_iterator_tag
, const Symbol
*, ptrdiff_t,
340 const Symbol
*, const Symbol
*> {
341 const_symbol_iterator() = default;
343 template <typename U
>
344 const_symbol_iterator(U
&&u
)
345 : iterator_adaptor_base(std::forward
<U
&&>(u
)) {}
347 reference
operator*() const { return I
->second
; }
348 pointer
operator->() const { return I
->second
; }
351 using const_symbol_range
= iterator_range
<const_symbol_iterator
>;
353 using const_filtered_symbol_iterator
=
354 filter_iterator
<const_symbol_iterator
,
355 std::function
<bool(const Symbol
*)>>;
356 using const_filtered_symbol_range
=
357 iterator_range
<const_filtered_symbol_iterator
>;
359 const_symbol_range
symbols() const {
360 return {Symbols
.begin(), Symbols
.end()};
363 const_filtered_symbol_range
exports() const {
364 std::function
<bool(const Symbol
*)> fn
= [](const Symbol
*Symbol
) {
365 return !Symbol
->isUndefined();
367 return make_filter_range(
368 make_range
<const_symbol_iterator
>({Symbols
.begin()}, {Symbols
.end()}),
372 const_filtered_symbol_range
undefineds() const {
373 std::function
<bool(const Symbol
*)> fn
= [](const Symbol
*Symbol
) {
374 return Symbol
->isUndefined();
376 return make_filter_range(
377 make_range
<const_symbol_iterator
>({Symbols
.begin()}, {Symbols
.end()}),
382 llvm::BumpPtrAllocator Allocator
;
383 StringRef
copyString(StringRef String
) {
387 void *Ptr
= Allocator
.Allocate(String
.size(), 1);
388 memcpy(Ptr
, String
.data(), String
.size());
389 return StringRef(reinterpret_cast<const char *>(Ptr
), String
.size());
395 std::string InstallName
;
396 PackedVersion CurrentVersion
;
397 PackedVersion CompatibilityVersion
;
398 uint8_t SwiftABIVersion
{0};
399 bool IsTwoLevelNamespace
{false};
400 bool IsAppExtensionSafe
{false};
401 bool IsInstallAPI
{false};
402 ObjCConstraintType ObjcConstraint
= ObjCConstraintType::None
;
403 std::vector
<std::pair
<Target
, std::string
>> ParentUmbrellas
;
404 std::vector
<InterfaceFileRef
> AllowableClients
;
405 std::vector
<InterfaceFileRef
> ReexportedLibraries
;
406 std::vector
<std::pair
<Target
, std::string
>> UUIDs
;
407 SymbolMapType Symbols
;
410 } // end namespace MachO.
411 } // end namespace llvm.
413 #endif // LLVM_TEXTAPI_MACHO_INTERFACE_FILE_H