]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/TextAPI/MachO/InterfaceFile.h
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / TextAPI / MachO / InterfaceFile.h
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"
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"
30
31 namespace llvm {
32 namespace MachO {
33
34 /// Defines the list of MachO platforms.
35 enum class PlatformKind : unsigned {
36   unknown,
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,
42 };
43
44 /// Defines a list of Objective-C constraints.
45 enum class ObjCConstraintType : unsigned {
46   /// No constraint.
47   None = 0,
48
49   /// Retain/Release.
50   Retain_Release = 1,
51
52   /// Retain/Release for Simulator.
53   Retain_Release_For_Simulator = 2,
54
55   /// Retain/Release or Garbage Collection.
56   Retain_Release_Or_GC = 3,
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.
86 class InterfaceFileRef {
87 public:
88   InterfaceFileRef() = default;
89
90   InterfaceFileRef(StringRef InstallName) : InstallName(InstallName) {}
91
92   InterfaceFileRef(StringRef InstallName, ArchitectureSet Archs)
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
119 struct SymbolsMapKey {
120   MachO::SymbolKind Kind;
121   StringRef Name;
122
123   SymbolsMapKey(MachO::SymbolKind Kind, StringRef Name)
124       : Kind(Kind), Name(Name) {}
125 };
126 template <> struct DenseMapInfo<SymbolsMapKey> {
127   static inline SymbolsMapKey getEmptyKey() {
128     return SymbolsMapKey(MachO::SymbolKind::GlobalSymbol, StringRef{});
129   }
130
131   static inline SymbolsMapKey getTombstoneKey() {
132     return SymbolsMapKey(MachO::SymbolKind::ObjectiveCInstanceVariable,
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.
148 class InterfaceFile {
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.
202   void setCurrentVersion(PackedVersion Version) { CurrentVersion = Version; }
203
204   /// Get the current version of the library.
205   PackedVersion getCurrentVersion() const { return CurrentVersion; }
206
207   /// Set the compatibility version of the library.
208   void setCompatibilityVersion(PackedVersion Version) {
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.
234   void setObjCConstraint(ObjCConstraintType 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,
306                  SymbolFlags Flags = SymbolFlags::None);
307
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;
315
316     template <typename U>
317     const_symbol_iterator(U &&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   using const_symbol_range = iterator_range<const_symbol_iterator>;
324
325   // Custom iterator to return only exported symbols.
326   struct const_export_iterator
327       : public iterator_adaptor_base<
328             const_export_iterator, const_symbol_iterator,
329             std::forward_iterator_tag, const Symbol *> {
330     const_symbol_iterator _end;
331
332     void skipToNextSymbol() {
333       while (I != _end && I->isUndefined())
334         ++I;
335     }
336
337     const_export_iterator() = default;
338     template <typename U>
339     const_export_iterator(U &&it, U &&end)
340         : iterator_adaptor_base(std::forward<U &&>(it)),
341           _end(std::forward<U &&>(end)) {
342       skipToNextSymbol();
343     }
344
345     const_export_iterator &operator++() {
346       ++I;
347       skipToNextSymbol();
348       return *this;
349     }
350
351     const_export_iterator operator++(int) {
352       const_export_iterator tmp(*this);
353       ++(*this);
354       return tmp;
355     }
356   };
357   using const_export_range = llvm::iterator_range<const_export_iterator>;
358
359   // Custom iterator to return only undefined symbols.
360   struct const_undefined_iterator
361       : public iterator_adaptor_base<
362             const_undefined_iterator, const_symbol_iterator,
363             std::forward_iterator_tag, const Symbol *> {
364     const_symbol_iterator _end;
365
366     void skipToNextSymbol() {
367       while (I != _end && !I->isUndefined())
368         ++I;
369     }
370
371     const_undefined_iterator() = default;
372     template <typename U>
373     const_undefined_iterator(U &&it, U &&end)
374         : iterator_adaptor_base(std::forward<U &&>(it)),
375           _end(std::forward<U &&>(end)) {
376       skipToNextSymbol();
377     }
378
379     const_undefined_iterator &operator++() {
380       ++I;
381       skipToNextSymbol();
382       return *this;
383     }
384
385     const_undefined_iterator operator++(int) {
386       const_undefined_iterator tmp(*this);
387       ++(*this);
388       return tmp;
389     }
390   };
391   using const_undefined_range = llvm::iterator_range<const_undefined_iterator>;
392
393   const_symbol_range symbols() const {
394     return {Symbols.begin(), Symbols.end()};
395   }
396   const_export_range exports() const {
397     return {{Symbols.begin(), Symbols.end()}, {Symbols.end(), Symbols.end()}};
398   }
399   const_undefined_range undefineds() const {
400     return {{Symbols.begin(), Symbols.end()}, {Symbols.end(), Symbols.end()}};
401   }
402
403 private:
404   llvm::BumpPtrAllocator Allocator;
405   StringRef copyString(StringRef String) {
406     if (String.empty())
407       return {};
408
409     void *Ptr = Allocator.Allocate(String.size(), 1);
410     memcpy(Ptr, String.data(), String.size());
411     return StringRef(reinterpret_cast<const char *>(Ptr), String.size());
412   }
413
414   std::string Path;
415   FileType FileKind;
416   PlatformKind Platform;
417   ArchitectureSet Architectures;
418   std::string InstallName;
419   PackedVersion CurrentVersion;
420   PackedVersion CompatibilityVersion;
421   uint8_t SwiftABIVersion{0};
422   bool IsTwoLevelNamespace{false};
423   bool IsAppExtensionSafe{false};
424   bool IsInstallAPI{false};
425   ObjCConstraintType ObjcConstraint = ObjCConstraintType::None;
426   std::string ParentUmbrella;
427   std::vector<InterfaceFileRef> AllowableClients;
428   std::vector<InterfaceFileRef> ReexportedLibraries;
429   std::vector<std::pair<Architecture, std::string>> UUIDs;
430   SymbolMapType Symbols;
431 };
432
433 } // end namespace MachO.
434 } // end namespace llvm.
435
436 #endif // LLVM_TEXTAPI_MACHO_INTERFACE_FILE_H