OSDN Git Service

[CallSiteSplitting] Report edge deletion to DomTreeUpdater
[android-x86/external-llvm.git] / include / llvm / TextAPI / MachO / InterfaceFile.h
1 //===- llvm/TextAPI/IntefaceFile.h - TAPI Interface File --------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// \brief A generic and abstract interface representation for linkable objects.
12 ///        This could be an MachO executable, bundle, dylib, or text-based stub
13 ///        file.
14 ///
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_TEXTAPI_MACHO_INTERFACE_FILE_H
18 #define LLVM_TEXTAPI_MACHO_INTERFACE_FILE_H
19
20 #include "llvm/ADT/BitmaskEnum.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/Hashing.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/ADT/iterator.h"
25 #include "llvm/BinaryFormat/MachO.h"
26 #include "llvm/BinaryFormat/Magic.h"
27 #include "llvm/Support/Allocator.h"
28 #include "llvm/Support/Error.h"
29 #include "llvm/TextAPI/MachO/Architecture.h"
30 #include "llvm/TextAPI/MachO/ArchitectureSet.h"
31 #include "llvm/TextAPI/MachO/PackedVersion.h"
32 #include "llvm/TextAPI/MachO/Symbol.h"
33
34 namespace llvm {
35 namespace MachO {
36
37 /// Defines the list of MachO platforms.
38 enum class PlatformKind : unsigned {
39   unknown,
40   macOS = MachO::PLATFORM_MACOS,
41   iOS = MachO::PLATFORM_IOS,
42   tvOS = MachO::PLATFORM_TVOS,
43   watchOS = MachO::PLATFORM_WATCHOS,
44   bridgeOS = MachO::PLATFORM_BRIDGEOS,
45 };
46
47 /// Defines a list of Objective-C constraints.
48 enum class ObjCConstraintType : unsigned {
49   /// No constraint.
50   None = 0,
51
52   /// Retain/Release.
53   Retain_Release = 1,
54
55   /// Retain/Release for Simulator.
56   Retain_Release_For_Simulator = 2,
57
58   /// Retain/Release or Garbage Collection.
59   Retain_Release_Or_GC = 3,
60
61   /// Garbage Collection.
62   GC = 4,
63 };
64
65 // clang-format off
66
67 /// Defines the file type this file represents.
68 enum FileType : unsigned {
69   /// Invalid file type.
70   Invalid = 0U,
71
72   /// Text-based stub file (.tbd) version 1.0
73   TBD_V1  = 1U <<  0,
74
75   /// Text-based stub file (.tbd) version 2.0
76   TBD_V2  = 1U <<  1,
77
78   /// Text-based stub file (.tbd) version 3.0
79   TBD_V3  = 1U <<  2,
80
81   All     = ~0U,
82
83   LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/All),
84 };
85
86 // clang-format on
87
88 /// Reference to an interface file.
89 class InterfaceFileRef {
90 public:
91   InterfaceFileRef() = default;
92
93   InterfaceFileRef(StringRef InstallName) : InstallName(InstallName) {}
94
95   InterfaceFileRef(StringRef InstallName, ArchitectureSet Archs)
96       : InstallName(InstallName), Architectures(Archs) {}
97
98   StringRef getInstallName() const { return InstallName; };
99   void addArchitectures(ArchitectureSet Archs) { Architectures |= Archs; }
100   ArchitectureSet getArchitectures() const { return Architectures; }
101   bool hasArchitecture(Architecture Arch) const {
102     return Architectures.has(Arch);
103   }
104
105   bool operator==(const InterfaceFileRef &O) const {
106     return std::tie(InstallName, Architectures) ==
107            std::tie(O.InstallName, O.Architectures);
108   }
109
110   bool operator<(const InterfaceFileRef &O) const {
111     return std::tie(InstallName, Architectures) <
112            std::tie(O.InstallName, O.Architectures);
113   }
114
115 private:
116   std::string InstallName;
117   ArchitectureSet Architectures;
118 };
119
120 } // end namespace MachO.
121
122 struct SymbolsMapKey {
123   MachO::SymbolKind Kind;
124   StringRef Name;
125
126   SymbolsMapKey(MachO::SymbolKind Kind, StringRef Name)
127       : Kind(Kind), Name(Name) {}
128 };
129 template <> struct DenseMapInfo<SymbolsMapKey> {
130   static inline SymbolsMapKey getEmptyKey() {
131     return SymbolsMapKey(MachO::SymbolKind::GlobalSymbol, StringRef{});
132   }
133
134   static inline SymbolsMapKey getTombstoneKey() {
135     return SymbolsMapKey(MachO::SymbolKind::ObjectiveCInstanceVariable,
136                          StringRef{});
137   }
138
139   static unsigned getHashValue(const SymbolsMapKey &Key) {
140     return hash_combine(hash_value(Key.Kind), hash_value(Key.Name));
141   }
142
143   static bool isEqual(const SymbolsMapKey &LHS, const SymbolsMapKey &RHS) {
144     return std::tie(LHS.Kind, LHS.Name) == std::tie(RHS.Kind, RHS.Name);
145   }
146 };
147
148 namespace MachO {
149
150 /// Defines the interface file.
151 class InterfaceFile {
152 public:
153   /// Set the path from which this file was generated (if applicable).
154   ///
155   /// \param Path_ The path to the source file.
156   void setPath(StringRef Path_) { Path = Path_; }
157
158   /// Get the path from which this file was generated (if applicable).
159   ///
160   /// \return The path to the source file or empty.
161   StringRef getPath() const { return Path; }
162
163   /// Set the file type.
164   ///
165   /// This is used by the YAML writer to identify the specification it should
166   /// use for writing the file.
167   ///
168   /// \param Kind The file type.
169   void setFileType(FileType Kind) { FileKind = Kind; }
170
171   /// Get the file type.
172   ///
173   /// \return The file type.
174   FileType getFileType() const { return FileKind; }
175
176   /// Set the platform.
177   void setPlatform(PlatformKind Platform_) { Platform = Platform_; }
178
179   /// Get the platform.
180   PlatformKind getPlatform() const { return Platform; }
181
182   /// Specify the set of supported architectures by this file.
183   void setArchitectures(ArchitectureSet Architectures_) {
184     Architectures = Architectures_;
185   }
186
187   /// Add the set of supported architectures by this file.
188   void addArchitectures(ArchitectureSet Architectures_) {
189     Architectures |= Architectures_;
190   }
191
192   /// Add supported architecture by this file..
193   void addArch(Architecture Arch) { Architectures.set(Arch); }
194
195   /// Get the set of supported architectures.
196   ArchitectureSet getArchitectures() const { return Architectures; }
197
198   /// Set the install name of the library.
199   void setInstallName(StringRef InstallName_) { InstallName = InstallName_; }
200
201   /// Get the install name of the library.
202   StringRef getInstallName() const { return InstallName; }
203
204   /// Set the current version of the library.
205   void setCurrentVersion(PackedVersion Version) { CurrentVersion = Version; }
206
207   /// Get the current version of the library.
208   PackedVersion getCurrentVersion() const { return CurrentVersion; }
209
210   /// Set the compatibility version of the library.
211   void setCompatibilityVersion(PackedVersion Version) {
212     CompatibilityVersion = Version;
213   }
214
215   /// Get the compatibility version of the library.
216   PackedVersion getCompatibilityVersion() const { return CompatibilityVersion; }
217
218   /// Set the Swift ABI version of the library.
219   void setSwiftABIVersion(uint8_t Version) { SwiftABIVersion = Version; }
220
221   /// Get the Swift ABI version of the library.
222   uint8_t getSwiftABIVersion() const { return SwiftABIVersion; }
223
224   /// Specify if the library uses two-level namespace (or flat namespace).
225   void setTwoLevelNamespace(bool V = true) { IsTwoLevelNamespace = V; }
226
227   /// Check if the library uses two-level namespace.
228   bool isTwoLevelNamespace() const { return IsTwoLevelNamespace; }
229
230   /// Specify if the library is application extension safe (or not).
231   void setApplicationExtensionSafe(bool V = true) { IsAppExtensionSafe = V; }
232
233   /// Check if the library is application extension safe.
234   bool isApplicationExtensionSafe() const { return IsAppExtensionSafe; }
235
236   /// Set the Objective-C constraint.
237   void setObjCConstraint(ObjCConstraintType Constraint) {
238     ObjcConstraint = Constraint;
239   }
240
241   /// Get the Objective-C constraint.
242   ObjCConstraintType getObjCConstraint() const { return ObjcConstraint; }
243
244   /// Specify if this file was generated during InstallAPI (or not).
245   void setInstallAPI(bool V = true) { IsInstallAPI = V; }
246
247   /// Check if this file was generated during InstallAPI.
248   bool isInstallAPI() const { return IsInstallAPI; }
249
250   /// Set the parent umbrella framework.
251   void setParentUmbrella(StringRef Parent) { ParentUmbrella = Parent; }
252
253   /// Get the parent umbrella framework.
254   StringRef getParentUmbrella() const { return ParentUmbrella; }
255
256   /// Add an allowable client.
257   ///
258   /// Mach-O Dynamic libraries have the concept of allowable clients that are
259   /// checked during static link time. The name of the application or library
260   /// that is being generated needs to match one of the allowable clients or the
261   /// linker refuses to link this library.
262   ///
263   /// \param Name The name of the client that is allowed to link this library.
264   /// \param Architectures The set of architecture for which this applies.
265   void addAllowableClient(StringRef Name, ArchitectureSet Architectures);
266
267   /// Get the list of allowable clients.
268   ///
269   /// \return Returns a list of allowable clients.
270   const std::vector<InterfaceFileRef> &allowableClients() const {
271     return AllowableClients;
272   }
273
274   /// Add a re-exported library.
275   ///
276   /// \param InstallName The name of the library to re-export.
277   /// \param Architectures The set of architecture for which this applies.
278   void addReexportedLibrary(StringRef InstallName,
279                             ArchitectureSet Architectures);
280
281   /// Get the list of re-exported libraries.
282   ///
283   /// \return Returns a list of re-exported libraries.
284   const std::vector<InterfaceFileRef> &reexportedLibraries() const {
285     return ReexportedLibraries;
286   }
287
288   /// Add an architecture/UUID pair.
289   ///
290   /// \param Arch The architecture for which this applies.
291   /// \param UUID The UUID of the library for the specified architecture.
292   void addUUID(Architecture Arch, StringRef UUID);
293
294   /// Add an architecture/UUID pair.
295   ///
296   /// \param Arch The architecture for which this applies.
297   /// \param UUID The UUID of the library for the specified architecture.
298   void addUUID(Architecture Arch, uint8_t UUID[16]);
299
300   /// Get the list of architecture/UUID pairs.
301   ///
302   /// \return Returns a list of architecture/UUID pairs.
303   const std::vector<std::pair<Architecture, std::string>> &uuids() const {
304     return UUIDs;
305   }
306
307   /// Add a symbol to the symbols list or extend an existing one.
308   void addSymbol(SymbolKind Kind, StringRef Name, ArchitectureSet Architectures,
309                  SymbolFlags Flags = SymbolFlags::None);
310
311   using SymbolMapType = DenseMap<SymbolsMapKey, Symbol *>;
312   struct const_symbol_iterator
313       : public iterator_adaptor_base<
314             const_symbol_iterator, SymbolMapType::const_iterator,
315             std::forward_iterator_tag, const Symbol *, ptrdiff_t,
316             const Symbol *, const Symbol *> {
317     const_symbol_iterator() = default;
318
319     template <typename U>
320     const_symbol_iterator(U &&u)
321         : iterator_adaptor_base(std::forward<U &&>(u)) {}
322
323     reference operator*() const { return I->second; }
324     pointer operator->() const { return I->second; }
325   };
326   using const_symbol_range = iterator_range<const_symbol_iterator>;
327
328   // Custom iterator to return only exported symbols.
329   struct const_export_iterator
330       : public iterator_adaptor_base<
331             const_export_iterator, const_symbol_iterator,
332             std::forward_iterator_tag, const Symbol *> {
333     const_symbol_iterator _end;
334
335     void skipToNextSymbol() {
336       while (I != _end && I->isUndefined())
337         ++I;
338     }
339
340     const_export_iterator() = default;
341     template <typename U>
342     const_export_iterator(U &&it, U &&end)
343         : iterator_adaptor_base(std::forward<U &&>(it)),
344           _end(std::forward<U &&>(end)) {
345       skipToNextSymbol();
346     }
347
348     const_export_iterator &operator++() {
349       ++I;
350       skipToNextSymbol();
351       return *this;
352     }
353
354     const_export_iterator operator++(int) {
355       const_export_iterator tmp(*this);
356       ++(*this);
357       return tmp;
358     }
359   };
360   using const_export_range = llvm::iterator_range<const_export_iterator>;
361
362   // Custom iterator to return only undefined symbols.
363   struct const_undefined_iterator
364       : public iterator_adaptor_base<
365             const_undefined_iterator, const_symbol_iterator,
366             std::forward_iterator_tag, const Symbol *> {
367     const_symbol_iterator _end;
368
369     void skipToNextSymbol() {
370       while (I != _end && !I->isUndefined())
371         ++I;
372     }
373
374     const_undefined_iterator() = default;
375     template <typename U>
376     const_undefined_iterator(U &&it, U &&end)
377         : iterator_adaptor_base(std::forward<U &&>(it)),
378           _end(std::forward<U &&>(end)) {
379       skipToNextSymbol();
380     }
381
382     const_undefined_iterator &operator++() {
383       ++I;
384       skipToNextSymbol();
385       return *this;
386     }
387
388     const_undefined_iterator operator++(int) {
389       const_undefined_iterator tmp(*this);
390       ++(*this);
391       return tmp;
392     }
393   };
394   using const_undefined_range = llvm::iterator_range<const_undefined_iterator>;
395
396   const_symbol_range symbols() const {
397     return {Symbols.begin(), Symbols.end()};
398   }
399   const_export_range exports() const {
400     return {{Symbols.begin(), Symbols.end()}, {Symbols.end(), Symbols.end()}};
401   }
402   const_undefined_range undefineds() const {
403     return {{Symbols.begin(), Symbols.end()}, {Symbols.end(), Symbols.end()}};
404   }
405
406 private:
407   llvm::BumpPtrAllocator Allocator;
408   StringRef copyString(StringRef String) {
409     if (String.empty())
410       return {};
411
412     void *Ptr = Allocator.Allocate(String.size(), 1);
413     memcpy(Ptr, String.data(), String.size());
414     return StringRef(reinterpret_cast<const char *>(Ptr), String.size());
415   }
416
417   std::string Path;
418   FileType FileKind;
419   PlatformKind Platform;
420   ArchitectureSet Architectures;
421   std::string InstallName;
422   PackedVersion CurrentVersion;
423   PackedVersion CompatibilityVersion;
424   uint8_t SwiftABIVersion{0};
425   bool IsTwoLevelNamespace{false};
426   bool IsAppExtensionSafe{false};
427   bool IsInstallAPI{false};
428   ObjCConstraintType ObjcConstraint = ObjCConstraintType::None;
429   std::string ParentUmbrella;
430   std::vector<InterfaceFileRef> AllowableClients;
431   std::vector<InterfaceFileRef> ReexportedLibraries;
432   std::vector<std::pair<Architecture, std::string>> UUIDs;
433   SymbolMapType Symbols;
434 };
435
436 } // end namespace MachO.
437 } // end namespace llvm.
438
439 #endif // LLVM_TEXTAPI_MACHO_INTERFACE_FILE_H