LLVM 17.0.0git
InterfaceFile.h
Go to the documentation of this file.
1//===- llvm/TextAPI/InterfaceFile.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
14#ifndef LLVM_TEXTAPI_INTERFACEFILE_H
15#define LLVM_TEXTAPI_INTERFACEFILE_H
16
18#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/Hashing.h"
20#include "llvm/ADT/StringRef.h"
21#include "llvm/ADT/iterator.h"
26#include "llvm/TextAPI/Symbol.h"
27#include "llvm/TextAPI/Target.h"
28
29namespace llvm {
30namespace MachO {
31
32/// Defines a list of Objective-C constraints.
33enum class ObjCConstraintType : unsigned {
34 /// No constraint.
35 None = 0,
36
37 /// Retain/Release.
39
40 /// Retain/Release for Simulator.
42
43 /// Retain/Release or Garbage Collection.
45
46 /// Garbage Collection.
47 GC = 4,
48};
49
50// clang-format off
51
52/// Defines the file type this file represents.
53enum FileType : unsigned {
54 /// Invalid file type.
55 Invalid = 0U,
56
57 /// Text-based stub file (.tbd) version 1.0
58 TBD_V1 = 1U << 0,
59
60 /// Text-based stub file (.tbd) version 2.0
61 TBD_V2 = 1U << 1,
62
63 /// Text-based stub file (.tbd) version 3.0
64 TBD_V3 = 1U << 2,
65
66 /// Text-based stub file (.tbd) version 4.0
67 TBD_V4 = 1U << 3,
68
69 /// Text-based stub file (.tbd) version 5.0
70 TBD_V5 = 1U << 4,
71
72 All = ~0U,
73
74 LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/All),
75};
76
77// clang-format on
78
79/// Reference to an interface file.
81public:
82 InterfaceFileRef() = default;
83
85
86 InterfaceFileRef(StringRef InstallName, const TargetList Targets)
88
89 StringRef getInstallName() const { return InstallName; };
90
91 void addTarget(const Target &Target);
92 template <typename RangeT> void addTargets(RangeT &&Targets) {
93 for (const auto &Target : Targets)
95 }
96
99 const_target_range targets() const { return {Targets}; }
100
102 return mapToArchitectureSet(Targets);
103 }
104
105 PlatformSet getPlatforms() const { return mapToPlatformSet(Targets); }
106
107 bool operator==(const InterfaceFileRef &O) const {
108 return std::tie(InstallName, Targets) == std::tie(O.InstallName, O.Targets);
109 }
110
111 bool operator!=(const InterfaceFileRef &O) const {
112 return std::tie(InstallName, Targets) != std::tie(O.InstallName, O.Targets);
113 }
114
115 bool operator<(const InterfaceFileRef &O) const {
116 return std::tie(InstallName, Targets) < std::tie(O.InstallName, O.Targets);
117 }
118
119private:
120 std::string InstallName;
121 TargetList Targets;
122};
123
124} // end namespace MachO.
125
129
131 : Kind(Kind), Name(Name) {}
132};
133template <> struct DenseMapInfo<SymbolsMapKey> {
134 static inline SymbolsMapKey getEmptyKey() {
136 }
137
140 StringRef{});
141 }
142
143 static unsigned getHashValue(const SymbolsMapKey &Key) {
144 return hash_combine(hash_value(Key.Kind), hash_value(Key.Name));
145 }
146
147 static bool isEqual(const SymbolsMapKey &LHS, const SymbolsMapKey &RHS) {
148 return std::tie(LHS.Kind, LHS.Name) == std::tie(RHS.Kind, RHS.Name);
149 }
150};
151
152namespace MachO {
153
154/// Defines the interface file.
156public:
157 /// Set the path from which this file was generated (if applicable).
158 ///
159 /// \param Path_ The path to the source file.
160 void setPath(StringRef Path_) { Path = std::string(Path_); }
161
162 /// Get the path from which this file was generated (if applicable).
163 ///
164 /// \return The path to the source file or empty.
165 StringRef getPath() const { return Path; }
166
167 /// Set the file type.
168 ///
169 /// This is used by the YAML writer to identify the specification it should
170 /// use for writing the file.
171 ///
172 /// \param Kind The file type.
173 void setFileType(FileType Kind) { FileKind = Kind; }
174
175 /// Get the file type.
176 ///
177 /// \return The file type.
178 FileType getFileType() const { return FileKind; }
179
180 /// Get the architectures.
181 ///
182 /// \return The applicable architectures.
184 return mapToArchitectureSet(Targets);
185 }
186
187 /// Get the platforms.
188 ///
189 /// \return The applicable platforms.
190 PlatformSet getPlatforms() const { return mapToPlatformSet(Targets); }
191
192 /// Set and add target.
193 ///
194 /// \param Target the target to add into.
195 void addTarget(const Target &Target);
196
197 /// Set and add targets.
198 ///
199 /// Add the subset of llvm::triples that is supported by Tapi
200 ///
201 /// \param Targets the collection of targets.
202 template <typename RangeT> void addTargets(RangeT &&Targets) {
203 for (const auto &Target_ : Targets)
204 addTarget(Target(Target_));
205 }
206
209 const_target_range targets() const { return {Targets}; }
210
213 std::function<bool(const Target &)>>;
217
218 /// Set the install name of the library.
219 void setInstallName(StringRef InstallName_) {
220 InstallName = std::string(InstallName_);
221 }
222
223 /// Get the install name of the library.
224 StringRef getInstallName() const { return InstallName; }
225
226 /// Set the current version of the library.
227 void setCurrentVersion(PackedVersion Version) { CurrentVersion = Version; }
228
229 /// Get the current version of the library.
230 PackedVersion getCurrentVersion() const { return CurrentVersion; }
231
232 /// Set the compatibility version of the library.
234 CompatibilityVersion = Version;
235 }
236
237 /// Get the compatibility version of the library.
238 PackedVersion getCompatibilityVersion() const { return CompatibilityVersion; }
239
240 /// Set the Swift ABI version of the library.
241 void setSwiftABIVersion(uint8_t Version) { SwiftABIVersion = Version; }
242
243 /// Get the Swift ABI version of the library.
244 uint8_t getSwiftABIVersion() const { return SwiftABIVersion; }
245
246 /// Specify if the library uses two-level namespace (or flat namespace).
247 void setTwoLevelNamespace(bool V = true) { IsTwoLevelNamespace = V; }
248
249 /// Check if the library uses two-level namespace.
250 bool isTwoLevelNamespace() const { return IsTwoLevelNamespace; }
251
252 /// Specify if the library is application extension safe (or not).
253 void setApplicationExtensionSafe(bool V = true) { IsAppExtensionSafe = V; }
254
255 /// Check if the library is application extension safe.
256 bool isApplicationExtensionSafe() const { return IsAppExtensionSafe; }
257
258 /// Set the Objective-C constraint.
260 ObjcConstraint = Constraint;
261 }
262
263 /// Get the Objective-C constraint.
264 ObjCConstraintType getObjCConstraint() const { return ObjcConstraint; }
265
266 /// Specify if this file was generated during InstallAPI (or not).
267 void setInstallAPI(bool V = true) { IsInstallAPI = V; }
268
269 /// Check if this file was generated during InstallAPI.
270 bool isInstallAPI() const { return IsInstallAPI; }
271
272 /// Set the parent umbrella frameworks.
273 /// \param Target_ The target applicable to Parent
274 /// \param Parent The name of Parent
275 void addParentUmbrella(const Target &Target_, StringRef Parent);
276
277 /// Get the list of Parent Umbrella frameworks.
278 ///
279 /// \return Returns a list of target information and install name of parent
280 /// umbrellas.
281 const std::vector<std::pair<Target, std::string>> &umbrellas() const {
282 return ParentUmbrellas;
283 }
284
285 /// Add an allowable client.
286 ///
287 /// Mach-O Dynamic libraries have the concept of allowable clients that are
288 /// checked during static link time. The name of the application or library
289 /// that is being generated needs to match one of the allowable clients or the
290 /// linker refuses to link this library.
291 ///
292 /// \param InstallName The name of the client that is allowed to link this
293 /// library.
294 /// \param Target The target triple for which this applies.
295 void addAllowableClient(StringRef InstallName, const Target &Target);
296
297 /// Get the list of allowable clients.
298 ///
299 /// \return Returns a list of allowable clients.
300 const std::vector<InterfaceFileRef> &allowableClients() const {
301 return AllowableClients;
302 }
303
304 /// Add a re-exported library.
305 ///
306 /// \param InstallName The name of the library to re-export.
307 /// \param Target The target triple for which this applies.
308 void addReexportedLibrary(StringRef InstallName, const Target &Target);
309
310 /// Get the list of re-exported libraries.
311 ///
312 /// \return Returns a list of re-exported libraries.
313 const std::vector<InterfaceFileRef> &reexportedLibraries() const {
314 return ReexportedLibraries;
315 }
316
317 /// Add an Target/UUID pair.
318 ///
319 /// \param Target The target triple for which this applies.
320 /// \param UUID The UUID of the library for the specified architecture.
321 void addUUID(const Target &Target, StringRef UUID);
322
323 /// Add an Target/UUID pair.
324 ///
325 /// \param Target The target triple for which this applies.
326 /// \param UUID The UUID of the library for the specified architecture.
327 void addUUID(const Target &Target, uint8_t UUID[16]);
328
329 /// Get the list of Target/UUID pairs.
330 ///
331 /// \return Returns a list of Target/UUID pairs.
332 const std::vector<std::pair<Target, std::string>> &uuids() const {
333 return UUIDs;
334 }
335
336 /// Add a library for inlining to top level library.
337 ///
338 ///\param Document The library to inline with top level library.
339 void addDocument(std::shared_ptr<InterfaceFile> &&Document);
340
341 /// Returns the pointer to parent document if exists or nullptr otherwise.
342 InterfaceFile *getParent() const { return Parent; }
343
344 /// Get the list of inlined libraries.
345 ///
346 /// \return Returns a list of the inlined frameworks.
347 const std::vector<std::shared_ptr<InterfaceFile>> &documents() const {
348 return Documents;
349 }
350
351 /// Set the runpath search paths.
352 /// \param InputTarget The target applicable to runpath search path.
353 /// \param RPath The name of runpath.
354 void addRPath(const Target &InputTarget, StringRef RPath);
355
356 /// Get the list of runpath search paths.
357 ///
358 /// \return Returns a list of the rpaths per target.
359 const std::vector<std::pair<Target, std::string>> &rpaths() const {
360 return RPaths;
361 }
362
363 /// Add a symbol to the symbols list or extend an existing one.
364 void addSymbol(SymbolKind Kind, StringRef Name, const TargetList &Targets,
366
369 : public iterator_adaptor_base<
370 const_symbol_iterator, SymbolMapType::const_iterator,
371 std::forward_iterator_tag, const Symbol *, ptrdiff_t,
372 const Symbol *, const Symbol *> {
374
375 template <typename U>
377 : iterator_adaptor_base(std::forward<U &&>(u)) {}
378
379 reference operator*() const { return I->second; }
380 pointer operator->() const { return I->second; }
381 };
382
384
387 std::function<bool(const Symbol *)>>;
390
392 return {Symbols.begin(), Symbols.end()};
393 }
394
395 size_t symbolsCount() const { return Symbols.size(); }
396
398 std::function<bool(const Symbol *)> fn = [](const Symbol *Symbol) {
399 return !Symbol->isUndefined() && !Symbol->isReexported();
400 };
401 return make_filter_range(
402 make_range<const_symbol_iterator>({Symbols.begin()}, {Symbols.end()}),
403 fn);
404 }
405
407 std::function<bool(const Symbol *)> fn = [](const Symbol *Symbol) {
408 return Symbol->isReexported();
409 };
410 return make_filter_range(
411 make_range<const_symbol_iterator>({Symbols.begin()}, {Symbols.end()}),
412 fn);
413 }
414
416 std::function<bool(const Symbol *)> fn = [](const Symbol *Symbol) {
417 return Symbol->isUndefined();
418 };
419 return make_filter_range(
420 make_range<const_symbol_iterator>({Symbols.begin()}, {Symbols.end()}),
421 fn);
422 }
423
424 /// The equality is determined by attributes that impact linking
425 /// compatibilities. UUIDs, Path, & FileKind are irrelevant since these by
426 /// itself should not impact linking.
427 /// This is an expensive operation.
428 bool operator==(const InterfaceFile &O) const;
429
430 bool operator!=(const InterfaceFile &O) const { return !(*this == O); }
431
432private:
433 llvm::BumpPtrAllocator Allocator;
434 StringRef copyString(StringRef String) {
435 if (String.empty())
436 return {};
437
438 void *Ptr = Allocator.Allocate(String.size(), 1);
439 memcpy(Ptr, String.data(), String.size());
440 return StringRef(reinterpret_cast<const char *>(Ptr), String.size());
441 }
442
443 TargetList Targets;
444 std::string Path;
445 FileType FileKind{FileType::Invalid};
446 std::string InstallName;
447 PackedVersion CurrentVersion;
448 PackedVersion CompatibilityVersion;
449 uint8_t SwiftABIVersion{0};
450 bool IsTwoLevelNamespace{false};
451 bool IsAppExtensionSafe{false};
452 bool IsInstallAPI{false};
454 std::vector<std::pair<Target, std::string>> ParentUmbrellas;
455 std::vector<InterfaceFileRef> AllowableClients;
456 std::vector<InterfaceFileRef> ReexportedLibraries;
457 std::vector<std::shared_ptr<InterfaceFile>> Documents;
458 std::vector<std::pair<Target, std::string>> UUIDs;
459 std::vector<std::pair<Target, std::string>> RPaths;
460 SymbolMapType Symbols;
461 InterfaceFile *Parent = nullptr;
462};
463
464template <typename DerivedT, typename KeyInfoT, typename BucketT>
466 KeyInfoT, BucketT> &LHS,
467 const DenseMapBase<DerivedT, SymbolsMapKey, MachO::Symbol *,
468 KeyInfoT, BucketT> &RHS) {
469 if (LHS.size() != RHS.size())
470 return false;
471 for (const auto &KV : LHS) {
472 auto I = RHS.find(KV.first);
473 if (I == RHS.end() || *I->second != *KV.second)
474 return false;
475 }
476 return true;
477}
478
479} // end namespace MachO.
480} // end namespace llvm.
481
482#endif // LLVM_TEXTAPI_INTERFACEFILE_H
This file defines the BumpPtrAllocator interface.
This file defines the DenseMap class.
std::string Name
#define I(x, y, z)
Definition: MD5.cpp:58
Basic Register Allocator
std::pair< llvm::MachO::Target, std::string > UUID
@ Targets
Definition: TextStubV5.cpp:90
@ InstallName
Definition: TextStubV5.cpp:95
@ Flags
Definition: TextStubV5.cpp:93
@ RPath
Definition: TextStubV5.cpp:119
Value * RHS
Value * LHS
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:66
unsigned size() const
Definition: DenseMap.h:99
iterator begin()
Definition: DenseMap.h:75
iterator end()
Definition: DenseMap.h:84
Reference to an interface file.
Definition: InterfaceFile.h:80
InterfaceFileRef(StringRef InstallName, const TargetList Targets)
Definition: InterfaceFile.h:86
bool operator!=(const InterfaceFileRef &O) const
void addTargets(RangeT &&Targets)
Definition: InterfaceFile.h:92
PlatformSet getPlatforms() const
void addTarget(const Target &Target)
const_target_range targets() const
Definition: InterfaceFile.h:99
StringRef getInstallName() const
Definition: InterfaceFile.h:89
bool operator==(const InterfaceFileRef &O) const
ArchitectureSet getArchitectures() const
InterfaceFileRef(StringRef InstallName)
Definition: InterfaceFile.h:84
bool operator<(const InterfaceFileRef &O) const
Defines the interface file.
void addDocument(std::shared_ptr< InterfaceFile > &&Document)
Add a library for inlining to top level library.
StringRef getPath() const
Get the path from which this file was generated (if applicable).
void addReexportedLibrary(StringRef InstallName, const Target &Target)
Add a re-exported library.
void setPath(StringRef Path_)
Set the path from which this file was generated (if applicable).
void addParentUmbrella(const Target &Target_, StringRef Parent)
Set the parent umbrella frameworks.
const_target_range targets() const
void setInstallAPI(bool V=true)
Specify if this file was generated during InstallAPI (or not).
const_filtered_symbol_range reexports() const
void setObjCConstraint(ObjCConstraintType Constraint)
Set the Objective-C constraint.
bool isTwoLevelNamespace() const
Check if the library uses two-level namespace.
bool operator==(const InterfaceFile &O) const
The equality is determined by attributes that impact linking compatibilities.
PackedVersion getCompatibilityVersion() const
Get the compatibility version of the library.
bool operator!=(const InterfaceFile &O) const
void addTarget(const Target &Target)
Set and add target.
const_filtered_symbol_range exports() const
bool isApplicationExtensionSafe() const
Check if the library is application extension safe.
const std::vector< std::pair< Target, std::string > > & uuids() const
Get the list of Target/UUID pairs.
PlatformSet getPlatforms() const
Get the platforms.
const std::vector< std::pair< Target, std::string > > & rpaths() const
Get the list of runpath search paths.
void addUUID(const Target &Target, StringRef UUID)
Add an Target/UUID pair.
const std::vector< InterfaceFileRef > & allowableClients() const
Get the list of allowable clients.
void setInstallName(StringRef InstallName_)
Set the install name of the library.
void addTargets(RangeT &&Targets)
Set and add targets.
const std::vector< std::shared_ptr< InterfaceFile > > & documents() const
Get the list of inlined libraries.
const std::vector< std::pair< Target, std::string > > & umbrellas() const
Get the list of Parent Umbrella frameworks.
const std::vector< InterfaceFileRef > & reexportedLibraries() const
Get the list of re-exported libraries.
InterfaceFile * getParent() const
Returns the pointer to parent document if exists or nullptr otherwise.
void addSymbol(SymbolKind Kind, StringRef Name, const TargetList &Targets, SymbolFlags Flags=SymbolFlags::None)
Add a symbol to the symbols list or extend an existing one.
const_symbol_range symbols() const
void setFileType(FileType Kind)
Set the file type.
uint8_t getSwiftABIVersion() const
Get the Swift ABI version of the library.
PackedVersion getCurrentVersion() const
Get the current version of the library.
const_filtered_symbol_range undefineds() const
void setCompatibilityVersion(PackedVersion Version)
Set the compatibility version of the library.
ArchitectureSet getArchitectures() const
Get the architectures.
StringRef getInstallName() const
Get the install name of the library.
ObjCConstraintType getObjCConstraint() const
Get the Objective-C constraint.
FileType getFileType() const
Get the file type.
void setApplicationExtensionSafe(bool V=true)
Specify if the library is application extension safe (or not).
void addAllowableClient(StringRef InstallName, const Target &Target)
Add an allowable client.
void setSwiftABIVersion(uint8_t Version)
Set the Swift ABI version of the library.
void addRPath(const Target &InputTarget, StringRef RPath)
Set the runpath search paths.
DenseMap< SymbolsMapKey, Symbol * > SymbolMapType
void setCurrentVersion(PackedVersion Version)
Set the current version of the library.
TargetList::const_iterator const_target_iterator
bool isInstallAPI() const
Check if this file was generated during InstallAPI.
void setTwoLevelNamespace(bool V=true)
Specify if the library uses two-level namespace (or flat namespace).
bool isUndefined() const
Definition: Symbol.h:94
bool isReexported() const
Definition: Symbol.h:98
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:135
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Specialization of filter_iterator_base for forward iteration only.
Definition: STLExtras.h:589
CRTP base class for adapting an iterator to a different type.
Definition: iterator.h:237
A range adaptor for a pair of iterators.
FileType
Defines the file type this file represents.
Definition: InterfaceFile.h:53
@ Invalid
Invalid file type.
Definition: InterfaceFile.h:55
@ TBD_V1
Text-based stub file (.tbd) version 1.0.
Definition: InterfaceFile.h:58
@ LLVM_MARK_AS_BITMASK_ENUM
Definition: InterfaceFile.h:74
@ TBD_V3
Text-based stub file (.tbd) version 3.0.
Definition: InterfaceFile.h:64
@ TBD_V5
Text-based stub file (.tbd) version 5.0.
Definition: InterfaceFile.h:70
@ TBD_V4
Text-based stub file (.tbd) version 4.0.
Definition: InterfaceFile.h:67
@ TBD_V2
Text-based stub file (.tbd) version 2.0.
Definition: InterfaceFile.h:61
PlatformSet mapToPlatformSet(ArrayRef< Triple > Targets)
Definition: Platform.cpp:56
ObjCConstraintType
Defines a list of Objective-C constraints.
Definition: InterfaceFile.h:33
@ Retain_Release_For_Simulator
Retain/Release for Simulator.
@ Retain_Release_Or_GC
Retain/Release or Garbage Collection.
bool operator==(const DenseMapBase< DerivedT, SymbolsMapKey, MachO::Symbol *, KeyInfoT, BucketT > &LHS, const DenseMapBase< DerivedT, SymbolsMapKey, MachO::Symbol *, KeyInfoT, BucketT > &RHS)
SmallVector< Target, 5 > TargetList
Definition: Symbol.h:67
SymbolFlags
Symbol flags.
Definition: Symbol.h:24
ArchitectureSet mapToArchitectureSet(ArrayRef< Target > Targets)
Definition: Target.cpp:75
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
hash_code hash_value(const FixedPointSemantics &Val)
Definition: APFixedPoint.h:128
iterator_range< filter_iterator< detail::IterOfRange< RangeT >, PredicateT > > make_filter_range(RangeT &&Range, PredicateT Pred)
Convenience function that takes a range of elements and a predicate, and return a new filter_iterator...
Definition: STLExtras.h:664
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1946
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
Definition: Hashing.h:613
Definition: BitVector.h:858
static SymbolsMapKey getEmptyKey()
static SymbolsMapKey getTombstoneKey()
static bool isEqual(const SymbolsMapKey &LHS, const SymbolsMapKey &RHS)
static unsigned getHashValue(const SymbolsMapKey &Key)
An information struct used to provide DenseMap with the various necessary components for a given valu...
Definition: DenseMapInfo.h:51
SymbolsMapKey(MachO::SymbolKind Kind, StringRef Name)
MachO::SymbolKind Kind