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"
30 
31 namespace llvm {
32 namespace MachO {
33 
34 /// Defines the list of MachO platforms.
35 enum class PlatformKind : unsigned {
36  unknown,
42 };
43 
44 /// Defines a list of Objective-C constraints.
46  /// No constraint.
47  None = 0,
48 
49  /// Retain/Release.
50  Retain_Release = 1,
51 
52  /// Retain/Release for Simulator.
54 
55  /// Retain/Release or Garbage Collection.
57 
58  /// Garbage Collection.
59  GC = 4,
60 };
61 
62 // clang-format off
63 
64 /// Defines the file type this file represents.
65 enum FileType : unsigned {
66  /// Invalid file type.
67  Invalid = 0U,
68 
69  /// Text-based stub file (.tbd) version 1.0
70  TBD_V1 = 1U << 0,
71 
72  /// Text-based stub file (.tbd) version 2.0
73  TBD_V2 = 1U << 1,
74 
75  /// Text-based stub file (.tbd) version 3.0
76  TBD_V3 = 1U << 2,
77 
78  All = ~0U,
79 
80  LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/All),
81 };
82 
83 // clang-format on
84 
85 /// Reference to an interface file.
87 public:
88  InterfaceFileRef() = default;
89 
90  InterfaceFileRef(StringRef InstallName) : InstallName(InstallName) {}
91 
93  : InstallName(InstallName), Architectures(Archs) {}
94 
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);
100  }
101 
102  bool operator==(const InterfaceFileRef &O) const {
103  return std::tie(InstallName, Architectures) ==
104  std::tie(O.InstallName, O.Architectures);
105  }
106 
107  bool operator<(const InterfaceFileRef &O) const {
108  return std::tie(InstallName, Architectures) <
109  std::tie(O.InstallName, O.Architectures);
110  }
111 
112 private:
113  std::string InstallName;
114  ArchitectureSet Architectures;
115 };
116 
117 } // end namespace MachO.
118 
122 
124  : Kind(Kind), Name(Name) {}
125 };
126 template <> struct DenseMapInfo<SymbolsMapKey> {
127  static inline SymbolsMapKey getEmptyKey() {
129  }
130 
131  static inline SymbolsMapKey getTombstoneKey() {
133  StringRef{});
134  }
135 
136  static unsigned getHashValue(const SymbolsMapKey &Key) {
137  return hash_combine(hash_value(Key.Kind), hash_value(Key.Name));
138  }
139 
140  static bool isEqual(const SymbolsMapKey &LHS, const SymbolsMapKey &RHS) {
141  return std::tie(LHS.Kind, LHS.Name) == std::tie(RHS.Kind, RHS.Name);
142  }
143 };
144 
145 namespace MachO {
146 
147 /// Defines the interface file.
149 public:
150  /// Set the path from which this file was generated (if applicable).
151  ///
152  /// \param Path_ The path to the source file.
153  void setPath(StringRef Path_) { Path = Path_; }
154 
155  /// Get the path from which this file was generated (if applicable).
156  ///
157  /// \return The path to the source file or empty.
158  StringRef getPath() const { return Path; }
159 
160  /// Set the file type.
161  ///
162  /// This is used by the YAML writer to identify the specification it should
163  /// use for writing the file.
164  ///
165  /// \param Kind The file type.
166  void setFileType(FileType Kind) { FileKind = Kind; }
167 
168  /// Get the file type.
169  ///
170  /// \return The file type.
171  FileType getFileType() const { return FileKind; }
172 
173  /// Set the platform.
174  void setPlatform(PlatformKind Platform_) { Platform = Platform_; }
175 
176  /// Get the platform.
177  PlatformKind getPlatform() const { return Platform; }
178 
179  /// Specify the set of supported architectures by this file.
180  void setArchitectures(ArchitectureSet Architectures_) {
181  Architectures = Architectures_;
182  }
183 
184  /// Add the set of supported architectures by this file.
185  void addArchitectures(ArchitectureSet Architectures_) {
186  Architectures |= Architectures_;
187  }
188 
189  /// Add supported architecture by this file..
190  void addArch(Architecture Arch) { Architectures.set(Arch); }
191 
192  /// Get the set of supported architectures.
193  ArchitectureSet getArchitectures() const { return Architectures; }
194 
195  /// Set the install name of the library.
196  void setInstallName(StringRef InstallName_) { InstallName = InstallName_; }
197 
198  /// Get the install name of the library.
199  StringRef getInstallName() const { return InstallName; }
200 
201  /// Set the current version of the library.
203 
204  /// Get the current version of the library.
206 
207  /// Set the compatibility version of the library.
209  CompatibilityVersion = Version;
210  }
211 
212  /// Get the compatibility version of the library.
213  PackedVersion getCompatibilityVersion() const { return CompatibilityVersion; }
214 
215  /// Set the Swift ABI version of the library.
216  void setSwiftABIVersion(uint8_t Version) { SwiftABIVersion = Version; }
217 
218  /// Get the Swift ABI version of the library.
219  uint8_t getSwiftABIVersion() const { return SwiftABIVersion; }
220 
221  /// Specify if the library uses two-level namespace (or flat namespace).
222  void setTwoLevelNamespace(bool V = true) { IsTwoLevelNamespace = V; }
223 
224  /// Check if the library uses two-level namespace.
225  bool isTwoLevelNamespace() const { return IsTwoLevelNamespace; }
226 
227  /// Specify if the library is application extension safe (or not).
228  void setApplicationExtensionSafe(bool V = true) { IsAppExtensionSafe = V; }
229 
230  /// Check if the library is application extension safe.
231  bool isApplicationExtensionSafe() const { return IsAppExtensionSafe; }
232 
233  /// Set the Objective-C constraint.
235  ObjcConstraint = Constraint;
236  }
237 
238  /// Get the Objective-C constraint.
239  ObjCConstraintType getObjCConstraint() const { return ObjcConstraint; }
240 
241  /// Specify if this file was generated during InstallAPI (or not).
242  void setInstallAPI(bool V = true) { IsInstallAPI = V; }
243 
244  /// Check if this file was generated during InstallAPI.
245  bool isInstallAPI() const { return IsInstallAPI; }
246 
247  /// Set the parent umbrella framework.
248  void setParentUmbrella(StringRef Parent) { ParentUmbrella = Parent; }
249 
250  /// Get the parent umbrella framework.
251  StringRef getParentUmbrella() const { return ParentUmbrella; }
252 
253  /// Add an allowable client.
254  ///
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.
259  ///
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);
263 
264  /// Get the list of allowable clients.
265  ///
266  /// \return Returns a list of allowable clients.
267  const std::vector<InterfaceFileRef> &allowableClients() const {
268  return AllowableClients;
269  }
270 
271  /// Add a re-exported library.
272  ///
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);
277 
278  /// Get the list of re-exported libraries.
279  ///
280  /// \return Returns a list of re-exported libraries.
281  const std::vector<InterfaceFileRef> &reexportedLibraries() const {
282  return ReexportedLibraries;
283  }
284 
285  /// Add an architecture/UUID pair.
286  ///
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);
290 
291  /// Add an architecture/UUID pair.
292  ///
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]);
296 
297  /// Get the list of architecture/UUID pairs.
298  ///
299  /// \return Returns a list of architecture/UUID pairs.
300  const std::vector<std::pair<Architecture, std::string>> &uuids() const {
301  return UUIDs;
302  }
303 
304  /// Add a symbol to the symbols list or extend an existing one.
305  void addSymbol(SymbolKind Kind, StringRef Name, ArchitectureSet Architectures,
307 
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;
315 
316  template <typename U>
318  : iterator_adaptor_base(std::forward<U &&>(u)) {}
319 
320  reference operator*() const { return I->second; }
321  pointer operator->() const { return I->second; }
322  };
323 
325 
328  std::function<bool(const Symbol *)>>;
331 
333  return {Symbols.begin(), Symbols.end()};
334  }
335 
337  std::function<bool(const Symbol *)> fn = [](const Symbol *Symbol) {
338  return !Symbol->isUndefined();
339  };
340  return make_filter_range(
341  make_range<const_symbol_iterator>({Symbols.begin()}, {Symbols.end()}),
342  fn);
343  }
344 
346  std::function<bool(const Symbol *)> fn = [](const Symbol *Symbol) {
347  return Symbol->isUndefined();
348  };
349  return make_filter_range(
350  make_range<const_symbol_iterator>({Symbols.begin()}, {Symbols.end()}),
351  fn);
352  }
353 
354 private:
356  StringRef copyString(StringRef String) {
357  if (String.empty())
358  return {};
359 
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());
363  }
364 
365  std::string Path;
366  FileType FileKind;
367  PlatformKind Platform;
368  ArchitectureSet Architectures;
369  std::string InstallName;
371  PackedVersion CompatibilityVersion;
372  uint8_t SwiftABIVersion{0};
373  bool IsTwoLevelNamespace{false};
374  bool IsAppExtensionSafe{false};
375  bool IsInstallAPI{false};
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;
382 };
383 
384 } // end namespace MachO.
385 } // end namespace llvm.
386 
387 #endif // LLVM_TEXTAPI_MACHO_INTERFACE_FILE_H
void setParentUmbrella(StringRef Parent)
Set the parent umbrella framework.
FileType getFileType() const
Get the file type.
PlatformKind
Defines the list of MachO platforms.
Definition: InterfaceFile.h:35
Defines the interface file.
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:73
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
static SymbolsMapKey getEmptyKey()
Reference to an interface file.
Definition: InterfaceFile.h:86
const std::vector< std::pair< Architecture, std::string > > & uuids() const
Get the list of architecture/UUID pairs.
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:95
SymbolFlags
Symbol flags.
Definition: Symbol.h:24
InterfaceFileRef(StringRef InstallName, ArchitectureSet Archs)
Definition: InterfaceFile.h:92
Text-based stub file (.tbd) version 1.0.
Definition: InterfaceFile.h:70
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.
PlatformKind getPlatform() const
Get the platform.
void setFileType(FileType Kind)
Set the file type.
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
void setCurrentVersion(PackedVersion Version)
Set the current version of the library.
ArchitectureSet getArchitectures() const
Definition: InterfaceFile.h:97
void setPlatform(PlatformKind Platform_)
Set the platform.
Invalid file type.
Definition: InterfaceFile.h:67
InterfaceFileRef(StringRef InstallName)
Definition: InterfaceFile.h:90
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
bool isTwoLevelNamespace() const
Check if the library uses two-level namespace.
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:76
bool isApplicationExtensionSafe() const
Check if the library is application extension safe.
bool operator<(const InterfaceFileRef &O) const
std::pair< llvm::MachO::Architecture, std::string > UUID
bool isInstallAPI() const
Check if this file was generated during InstallAPI.
ObjCConstraintType
Defines a list of Objective-C constraints.
Definition: InterfaceFile.h:45
StringRef getInstallName() const
Get the install name of the library.
Basic Register Allocator
bool operator==(const InterfaceFileRef &O) const
FileType
Defines the file type this file represents.
Definition: InterfaceFile.h:65
Architecture
Defines the architecture slices that are supported by Text-based Stub files.
Definition: Architecture.h:23
bool isUndefined() const
Definition: Symbol.h:77
void addArch(Architecture Arch)
Add supported architecture by this file..
bool hasArchitecture(Architecture Arch) const
Definition: InterfaceFile.h:98
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.
void addArchitectures(ArchitectureSet Archs)
Definition: InterfaceFile.h:96
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 set of supported architectures.
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
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)
void setArchitectures(ArchitectureSet Architectures_)
Specify the set of supported architectures by this file.
StringRef getParentUmbrella() const
Get the parent umbrella framework.
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
void addArchitectures(ArchitectureSet Architectures_)
Add the set of supported architectures by this file.
const uint64_t Version
Definition: InstrProf.h:984
const_filtered_symbol_range undefineds() const
static bool isEqual(const SymbolsMapKey &LHS, const SymbolsMapKey &RHS)