LLVM  10.0.0svn
InterfaceFile.h
Go to the documentation of this file.
1 //===- llvm/TextAPI/MachO/IntefaceFile.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_MACHO_INTERFACE_FILE_H
15 #define LLVM_TEXTAPI_MACHO_INTERFACE_FILE_H
16 
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"
24 #include "llvm/Support/Allocator.h"
25 #include "llvm/Support/Error.h"
32 
33 namespace llvm {
34 namespace MachO {
35 
36 /// Defines a list of Objective-C constraints.
38  /// No constraint.
39  None = 0,
40 
41  /// Retain/Release.
42  Retain_Release = 1,
43 
44  /// Retain/Release for Simulator.
46 
47  /// Retain/Release or Garbage Collection.
49 
50  /// Garbage Collection.
51  GC = 4,
52 };
53 
54 // clang-format off
55 
56 /// Defines the file type this file represents.
57 enum FileType : unsigned {
58  /// Invalid file type.
59  Invalid = 0U,
60 
61  /// Text-based stub file (.tbd) version 1.0
62  TBD_V1 = 1U << 0,
63 
64  /// Text-based stub file (.tbd) version 2.0
65  TBD_V2 = 1U << 1,
66 
67  /// Text-based stub file (.tbd) version 3.0
68  TBD_V3 = 1U << 2,
69 
70  All = ~0U,
71 
72  LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/All),
73 };
74 
75 // clang-format on
76 
77 /// Reference to an interface file.
79 public:
80  InterfaceFileRef() = default;
81 
82  InterfaceFileRef(StringRef InstallName) : InstallName(InstallName) {}
83 
84  InterfaceFileRef(StringRef InstallName, const TargetList Targets)
85  : InstallName(InstallName), Targets(std::move(Targets)) {}
86 
87  StringRef getInstallName() const { return InstallName; };
88 
89  void addTarget(const Target &Target);
90  template <typename RangeT> void addTargets(RangeT &&Targets) {
91  for (const auto &Target : Targets)
92  addTarget(Target(Target));
93  }
94 
97  const_target_range targets() const { return {Targets}; }
98 
100  return mapToArchitectureSet(Targets);
101  }
102 
103  PlatformSet getPlatforms() const { return mapToPlatformSet(Targets); }
104 
105  bool operator==(const InterfaceFileRef &O) const {
106  return std::tie(InstallName, Targets) == std::tie(O.InstallName, O.Targets);
107  }
108 
109  bool operator!=(const InterfaceFileRef &O) const {
110  return std::tie(InstallName, Targets) != std::tie(O.InstallName, O.Targets);
111  }
112 
113  bool operator<(const InterfaceFileRef &O) const {
114  return std::tie(InstallName, Targets) < std::tie(O.InstallName, O.Targets);
115  }
116 
117 private:
118  std::string InstallName;
119  TargetList Targets;
120 };
121 
122 } // end namespace MachO.
123 
127 
129  : Kind(Kind), Name(Name) {}
130 };
131 template <> struct DenseMapInfo<SymbolsMapKey> {
132  static inline SymbolsMapKey getEmptyKey() {
134  }
135 
136  static inline SymbolsMapKey getTombstoneKey() {
138  StringRef{});
139  }
140 
141  static unsigned getHashValue(const SymbolsMapKey &Key) {
142  return hash_combine(hash_value(Key.Kind), hash_value(Key.Name));
143  }
144 
145  static bool isEqual(const SymbolsMapKey &LHS, const SymbolsMapKey &RHS) {
146  return std::tie(LHS.Kind, LHS.Name) == std::tie(RHS.Kind, RHS.Name);
147  }
148 };
149 
150 namespace MachO {
151 
152 /// Defines the interface file.
154 public:
155  /// Set the path from which this file was generated (if applicable).
156  ///
157  /// \param Path_ The path to the source file.
158  void setPath(StringRef Path_) { Path = Path_; }
159 
160  /// Get the path from which this file was generated (if applicable).
161  ///
162  /// \return The path to the source file or empty.
163  StringRef getPath() const { return Path; }
164 
165  /// Set the file type.
166  ///
167  /// This is used by the YAML writer to identify the specification it should
168  /// use for writing the file.
169  ///
170  /// \param Kind The file type.
171  void setFileType(FileType Kind) { FileKind = Kind; }
172 
173  /// Get the file type.
174  ///
175  /// \return The file type.
176  FileType getFileType() const { return FileKind; }
177 
178  /// Get the architectures.
179  ///
180  /// \return The applicable architectures.
182  return mapToArchitectureSet(Targets);
183  }
184 
185  /// Get the platforms.
186  ///
187  /// \return The applicable platforms.
188  PlatformSet getPlatforms() const { return mapToPlatformSet(Targets); }
189 
190  /// Set and add target.
191  ///
192  /// \param Target the target to add into.
193  void addTarget(const Target &Target);
194 
195  /// Set and add targets.
196  ///
197  /// Add the subset of llvm::triples that is supported by Tapi
198  ///
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_));
203  }
204 
207  const_target_range targets() const { return {Targets}; }
208 
211  std::function<bool(const Target &)>>;
214  const_filtered_target_range targets(ArchitectureSet Archs) const;
215 
216  /// Set the install name of the library.
217  void setInstallName(StringRef InstallName_) { InstallName = InstallName_; }
218 
219  /// Get the install name of the library.
220  StringRef getInstallName() const { return InstallName; }
221 
222  /// Set the current version of the library.
224 
225  /// Get the current version of the library.
227 
228  /// Set the compatibility version of the library.
230  CompatibilityVersion = Version;
231  }
232 
233  /// Get the compatibility version of the library.
234  PackedVersion getCompatibilityVersion() const { return CompatibilityVersion; }
235 
236  /// Set the Swift ABI version of the library.
237  void setSwiftABIVersion(uint8_t Version) { SwiftABIVersion = Version; }
238 
239  /// Get the Swift ABI version of the library.
240  uint8_t getSwiftABIVersion() const { return SwiftABIVersion; }
241 
242  /// Specify if the library uses two-level namespace (or flat namespace).
243  void setTwoLevelNamespace(bool V = true) { IsTwoLevelNamespace = V; }
244 
245  /// Check if the library uses two-level namespace.
246  bool isTwoLevelNamespace() const { return IsTwoLevelNamespace; }
247 
248  /// Specify if the library is application extension safe (or not).
249  void setApplicationExtensionSafe(bool V = true) { IsAppExtensionSafe = V; }
250 
251  /// Check if the library is application extension safe.
252  bool isApplicationExtensionSafe() const { return IsAppExtensionSafe; }
253 
254  /// Set the Objective-C constraint.
256  ObjcConstraint = Constraint;
257  }
258 
259  /// Get the Objective-C constraint.
260  ObjCConstraintType getObjCConstraint() const { return ObjcConstraint; }
261 
262  /// Specify if this file was generated during InstallAPI (or not).
263  void setInstallAPI(bool V = true) { IsInstallAPI = V; }
264 
265  /// Check if this file was generated during InstallAPI.
266  bool isInstallAPI() const { return IsInstallAPI; }
267 
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;
274  }
275 
276  /// Get the parent umbrella framework.
277  const std::vector<std::pair<Target, std::string>> getParentUmbrellas() const {
278  return ParentUmbrellas;
279  }
280 
281  /// Add an allowable client.
282  ///
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.
287  ///
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);
291 
292  /// Get the list of allowable clients.
293  ///
294  /// \return Returns a list of allowable clients.
295  const std::vector<InterfaceFileRef> &allowableClients() const {
296  return AllowableClients;
297  }
298 
299  /// Add a re-exported library.
300  ///
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);
304 
305  /// Get the list of re-exported libraries.
306  ///
307  /// \return Returns a list of re-exported libraries.
308  const std::vector<InterfaceFileRef> &reexportedLibraries() const {
309  return ReexportedLibraries;
310  }
311 
312  /// Add an Target/UUID pair.
313  ///
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);
317 
318  /// Add an Target/UUID pair.
319  ///
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]);
323 
324  /// Get the list of Target/UUID pairs.
325  ///
326  /// \return Returns a list of Target/UUID pairs.
327  const std::vector<std::pair<Target, std::string>> &uuids() const {
328  return UUIDs;
329  }
330 
331  /// Add a symbol to the symbols list or extend an existing one.
332  void addSymbol(SymbolKind Kind, StringRef Name, const TargetList &Targets,
334 
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;
342 
343  template <typename U>
345  : iterator_adaptor_base(std::forward<U &&>(u)) {}
346 
347  reference operator*() const { return I->second; }
348  pointer operator->() const { return I->second; }
349  };
350 
352 
355  std::function<bool(const Symbol *)>>;
358 
360  return {Symbols.begin(), Symbols.end()};
361  }
362 
364  std::function<bool(const Symbol *)> fn = [](const Symbol *Symbol) {
365  return !Symbol->isUndefined();
366  };
367  return make_filter_range(
368  make_range<const_symbol_iterator>({Symbols.begin()}, {Symbols.end()}),
369  fn);
370  }
371 
373  std::function<bool(const Symbol *)> fn = [](const Symbol *Symbol) {
374  return Symbol->isUndefined();
375  };
376  return make_filter_range(
377  make_range<const_symbol_iterator>({Symbols.begin()}, {Symbols.end()}),
378  fn);
379  }
380 
381 private:
383  StringRef copyString(StringRef String) {
384  if (String.empty())
385  return {};
386 
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());
390  }
391 
392  TargetList Targets;
393  std::string Path;
394  FileType FileKind;
395  std::string InstallName;
397  PackedVersion CompatibilityVersion;
398  uint8_t SwiftABIVersion{0};
399  bool IsTwoLevelNamespace{false};
400  bool IsAppExtensionSafe{false};
401  bool IsInstallAPI{false};
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;
408 };
409 
410 } // end namespace MachO.
411 } // end namespace llvm.
412 
413 #endif // LLVM_TEXTAPI_MACHO_INTERFACE_FILE_H
FileType getFileType() const
Get the file type.
TargetList::const_iterator const_target_iterator
Defines the interface file.
typename SuperClass::const_iterator const_iterator
Definition: SmallVector.h:320
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Retain/Release or Garbage Collection.
Text-based stub file (.tbd) version 2.0.
Definition: InterfaceFile.h:65
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
ArchitectureSet mapToArchitectureSet(ArrayRef< Target > Targets)
Definition: Target.cpp:37
static SymbolsMapKey getEmptyKey()
Reference to an interface file.
Definition: InterfaceFile.h:78
void setInstallAPI(bool V=true)
Specify if this file was generated during InstallAPI (or not).
This file defines the MallocAllocator and BumpPtrAllocator interfaces.
const std::vector< InterfaceFileRef > & allowableClients() const
Get the list of allowable clients.
void setTwoLevelNamespace(bool V=true)
Specify if the library uses two-level namespace (or flat namespace).
Definition: BitVector.h:937
const std::vector< InterfaceFileRef > & reexportedLibraries() const
Get the list of re-exported libraries.
StringRef getInstallName() const
Definition: InterfaceFile.h:87
SymbolFlags
Symbol flags.
Definition: Symbol.h:25
Text-based stub file (.tbd) version 1.0.
Definition: InterfaceFile.h:62
void addTargets(RangeT &&Targets)
Set and add targets.
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:140
void setObjCConstraint(ObjCConstraintType Constraint)
Set the Objective-C constraint.
static SymbolsMapKey getTombstoneKey()
Key
PAL metadata keys.
const std::vector< std::pair< Target, std::string > > getParentUmbrellas() const
Get the parent umbrella framework.
void setFileType(FileType Kind)
Set the file type.
InterfaceFileRef(StringRef InstallName, const TargetList Targets)
Definition: InterfaceFile.h:84
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:144
hash_code hash_value(const APFloat &Arg)
See friend declarations above.
Definition: APFloat.cpp:4470
const std::vector< std::pair< Target, std::string > > & uuids() const
Get the list of Target/UUID pairs.
void setCurrentVersion(PackedVersion Version)
Set the current version of the library.
ArchitectureSet getArchitectures() const
Definition: InterfaceFile.h:99
void addTargets(RangeT &&Targets)
Definition: InterfaceFile.h:90
Invalid file type.
Definition: InterfaceFile.h:59
InterfaceFileRef(StringRef InstallName)
Definition: InterfaceFile.h:82
const_filtered_symbol_range exports() const
StringRef getPath() const
Get the path from which this file was generated (if applicable).
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:140
CRTP base class for adapting an iterator to a different type.
Definition: iterator.h:205
PlatformSet getPlatforms() const
bool isTwoLevelNamespace() const
Check if the library uses two-level namespace.
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:134
LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void * Allocate(size_t Size, size_t Alignment)
Allocate space at the specified alignment.
Definition: Allocator.h:214
uint8_t getSwiftABIVersion() const
Get the Swift ABI version of the library.
SymbolsMapKey(MachO::SymbolKind Kind, StringRef Name)
Text-based stub file (.tbd) version 3.0.
Definition: InterfaceFile.h:68
bool isApplicationExtensionSafe() const
Check if the library is application extension safe.
bool operator<(const InterfaceFileRef &O) const
bool isInstallAPI() const
Check if this file was generated during InstallAPI.
ObjCConstraintType
Defines a list of Objective-C constraints.
Definition: InterfaceFile.h:37
StringRef getInstallName() const
Get the install name of the library.
TargetList::const_iterator const_target_iterator
Definition: InterfaceFile.h:95
Basic Register Allocator
bool operator==(const InterfaceFileRef &O) const
FileType
Defines the file type this file represents.
Definition: InterfaceFile.h:57
const_target_range targets() const
bool isUndefined() const
Definition: Symbol.h:80
std::pair< llvm::MachO::Target, std::string > UUID
void setInstallName(StringRef InstallName_)
Set the install name of the library.
A range adaptor for a pair of iterators.
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
Definition: Hashing.h:600
void setSwiftABIVersion(uint8_t Version)
Set the Swift ABI version of the library.
const std::vector< std::pair< Target, std::string > > & umbrellas() const
void setApplicationExtensionSafe(bool V=true)
Specify if the library is application extension safe (or not).
void setPath(StringRef Path_)
Set the path from which this file was generated (if applicable).
ArchitectureSet getArchitectures() const
Get the architectures.
PlatformSet mapToPlatformSet(ArrayRef< Triple > Targets)
Definition: Platform.cpp:36
void setCompatibilityVersion(PackedVersion Version)
Set the compatibility version of the library.
ObjCConstraintType getObjCConstraint() const
Get the Objective-C constraint.
#define I(x, y, z)
Definition: MD5.cpp:58
bool operator!=(const InterfaceFileRef &O) const
const_target_range targets() const
Definition: InterfaceFile.h:97
PlatformSet getPlatforms() const
Get the platforms.
MachO::SymbolKind Kind
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:422
PackedVersion getCompatibilityVersion() const
Get the compatibility version of the library.
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:136
static unsigned getHashValue(const SymbolsMapKey &Key)
PackedVersion getCurrentVersion() const
Get the current version of the library.
const_symbol_range symbols() const
Specialization of filter_iterator_base for forward iteration only.
Definition: STLExtras.h:348
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
const uint64_t Version
Definition: InstrProf.h:984
const_filtered_symbol_range undefineds() const
static bool isEqual(const SymbolsMapKey &LHS, const SymbolsMapKey &RHS)