Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:37

0001 //===- llvm/TextAPI/InterfaceFile.h - TAPI Interface File -------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // A generic and abstract interface representation for linkable objects. This
0010 // could be an MachO executable, bundle, dylib, or text-based stub file.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_TEXTAPI_INTERFACEFILE_H
0015 #define LLVM_TEXTAPI_INTERFACEFILE_H
0016 
0017 #include "llvm/ADT/Hashing.h"
0018 #include "llvm/ADT/StringRef.h"
0019 #include "llvm/ADT/iterator.h"
0020 #include "llvm/Support/Allocator.h"
0021 #include "llvm/TextAPI/ArchitectureSet.h"
0022 #include "llvm/TextAPI/FileTypes.h"
0023 #include "llvm/TextAPI/PackedVersion.h"
0024 #include "llvm/TextAPI/Platform.h"
0025 #include "llvm/TextAPI/RecordsSlice.h"
0026 #include "llvm/TextAPI/Symbol.h"
0027 #include "llvm/TextAPI/SymbolSet.h"
0028 #include "llvm/TextAPI/Target.h"
0029 
0030 namespace llvm {
0031 namespace MachO {
0032 
0033 /// Defines a list of Objective-C constraints.
0034 enum class ObjCConstraintType : unsigned {
0035   /// No constraint.
0036   None = 0,
0037 
0038   /// Retain/Release.
0039   Retain_Release = 1,
0040 
0041   /// Retain/Release for Simulator.
0042   Retain_Release_For_Simulator = 2,
0043 
0044   /// Retain/Release or Garbage Collection.
0045   Retain_Release_Or_GC = 3,
0046 
0047   /// Garbage Collection.
0048   GC = 4,
0049 };
0050 
0051 /// Reference to an interface file.
0052 class InterfaceFileRef {
0053 public:
0054   InterfaceFileRef() = default;
0055 
0056   InterfaceFileRef(StringRef InstallName) : InstallName(InstallName) {}
0057 
0058   InterfaceFileRef(StringRef InstallName, const TargetList Targets)
0059       : InstallName(InstallName), Targets(std::move(Targets)) {}
0060 
0061   StringRef getInstallName() const { return InstallName; };
0062 
0063   void addTarget(const Target &Target);
0064   template <typename RangeT> void addTargets(RangeT &&Targets) {
0065     for (const auto &Target : Targets)
0066       addTarget(Target(Target));
0067   }
0068 
0069   bool hasTarget(Target &Targ) const {
0070     return llvm::is_contained(Targets, Targ);
0071   }
0072 
0073   using const_target_iterator = TargetList::const_iterator;
0074   using const_target_range = llvm::iterator_range<const_target_iterator>;
0075   const_target_range targets() const { return {Targets}; }
0076 
0077   ArchitectureSet getArchitectures() const {
0078     return mapToArchitectureSet(Targets);
0079   }
0080 
0081   PlatformSet getPlatforms() const { return mapToPlatformSet(Targets); }
0082 
0083   bool operator==(const InterfaceFileRef &O) const {
0084     return std::tie(InstallName, Targets) == std::tie(O.InstallName, O.Targets);
0085   }
0086 
0087   bool operator!=(const InterfaceFileRef &O) const {
0088     return std::tie(InstallName, Targets) != std::tie(O.InstallName, O.Targets);
0089   }
0090 
0091   bool operator<(const InterfaceFileRef &O) const {
0092     return std::tie(InstallName, Targets) < std::tie(O.InstallName, O.Targets);
0093   }
0094 
0095 private:
0096   std::string InstallName;
0097   TargetList Targets;
0098 };
0099 
0100 } // end namespace MachO.
0101 
0102 namespace MachO {
0103 
0104 /// Defines the interface file.
0105 class InterfaceFile {
0106 public:
0107   InterfaceFile(std::unique_ptr<SymbolSet> &&InputSymbols)
0108       : SymbolsSet(std::move(InputSymbols)) {}
0109 
0110   InterfaceFile() : SymbolsSet(std::make_unique<SymbolSet>()){};
0111   /// Set the path from which this file was generated (if applicable).
0112   ///
0113   /// \param Path_ The path to the source file.
0114   void setPath(StringRef Path_) { Path = std::string(Path_); }
0115 
0116   /// Get the path from which this file was generated (if applicable).
0117   ///
0118   /// \return The path to the source file or empty.
0119   StringRef getPath() const { return Path; }
0120 
0121   /// Set the file type.
0122   ///
0123   /// This is used by the YAML writer to identify the specification it should
0124   /// use for writing the file.
0125   ///
0126   /// \param Kind The file type.
0127   void setFileType(FileType Kind) { FileKind = Kind; }
0128 
0129   /// Get the file type.
0130   ///
0131   /// \return The file type.
0132   FileType getFileType() const { return FileKind; }
0133 
0134   /// Get the architectures.
0135   ///
0136   /// \return The applicable architectures.
0137   ArchitectureSet getArchitectures() const {
0138     return mapToArchitectureSet(Targets);
0139   }
0140 
0141   /// Get the platforms.
0142   ///
0143   /// \return The applicable platforms.
0144   PlatformSet getPlatforms() const { return mapToPlatformSet(Targets); }
0145 
0146   /// Set and add target.
0147   ///
0148   /// \param Target the target to add into.
0149   void addTarget(const Target &Target);
0150 
0151   /// Determine if target triple slice exists in file.
0152   ///
0153   /// \param Targ the value to find.
0154   bool hasTarget(const Target &Targ) const {
0155     return llvm::is_contained(Targets, Targ);
0156   }
0157 
0158   /// Set and add targets.
0159   ///
0160   /// Add the subset of llvm::triples that is supported by Tapi
0161   ///
0162   /// \param Targets the collection of targets.
0163   template <typename RangeT> void addTargets(RangeT &&Targets) {
0164     for (const auto &Target_ : Targets)
0165       addTarget(Target(Target_));
0166   }
0167 
0168   using const_target_iterator = TargetList::const_iterator;
0169   using const_target_range = llvm::iterator_range<const_target_iterator>;
0170   const_target_range targets() const { return {Targets}; }
0171 
0172   using const_filtered_target_iterator =
0173       llvm::filter_iterator<const_target_iterator,
0174                             std::function<bool(const Target &)>>;
0175   using const_filtered_target_range =
0176       llvm::iterator_range<const_filtered_target_iterator>;
0177   const_filtered_target_range targets(ArchitectureSet Archs) const;
0178 
0179   /// Set the install name of the library.
0180   void setInstallName(StringRef InstallName_) {
0181     InstallName = std::string(InstallName_);
0182   }
0183 
0184   /// Get the install name of the library.
0185   StringRef getInstallName() const { return InstallName; }
0186 
0187   /// Set the current version of the library.
0188   void setCurrentVersion(PackedVersion Version) { CurrentVersion = Version; }
0189 
0190   /// Get the current version of the library.
0191   PackedVersion getCurrentVersion() const { return CurrentVersion; }
0192 
0193   /// Set the compatibility version of the library.
0194   void setCompatibilityVersion(PackedVersion Version) {
0195     CompatibilityVersion = Version;
0196   }
0197 
0198   /// Get the compatibility version of the library.
0199   PackedVersion getCompatibilityVersion() const { return CompatibilityVersion; }
0200 
0201   /// Set the Swift ABI version of the library.
0202   void setSwiftABIVersion(uint8_t Version) { SwiftABIVersion = Version; }
0203 
0204   /// Get the Swift ABI version of the library.
0205   uint8_t getSwiftABIVersion() const { return SwiftABIVersion; }
0206 
0207   /// Specify if the library uses two-level namespace (or flat namespace).
0208   void setTwoLevelNamespace(bool V = true) { IsTwoLevelNamespace = V; }
0209 
0210   /// Check if the library uses two-level namespace.
0211   bool isTwoLevelNamespace() const { return IsTwoLevelNamespace; }
0212 
0213   /// Specify if the library is an OS library but not shared cache eligible.
0214   void setOSLibNotForSharedCache(bool V = true) {
0215     IsOSLibNotForSharedCache = V;
0216   }
0217 
0218   /// Check if the library is an OS library that is not shared cache eligible.
0219   bool isOSLibNotForSharedCache() const { return IsOSLibNotForSharedCache; }
0220 
0221   /// Specify if the library is application extension safe (or not).
0222   void setApplicationExtensionSafe(bool V = true) { IsAppExtensionSafe = V; }
0223 
0224   /// Check if the library is application extension safe.
0225   bool isApplicationExtensionSafe() const { return IsAppExtensionSafe; }
0226 
0227   /// Check if the library has simulator support.
0228   bool hasSimulatorSupport() const { return HasSimSupport; }
0229 
0230   /// Specify if the library has simulator support.
0231   void setSimulatorSupport(bool V = true) { HasSimSupport = V; }
0232 
0233   /// Set the Objective-C constraint.
0234   void setObjCConstraint(ObjCConstraintType Constraint) {
0235     ObjcConstraint = Constraint;
0236   }
0237 
0238   /// Get the Objective-C constraint.
0239   ObjCConstraintType getObjCConstraint() const { return ObjcConstraint; }
0240 
0241   /// Set the parent umbrella frameworks.
0242   /// \param Target_ The target applicable to Parent
0243   /// \param Parent  The name of Parent
0244   void addParentUmbrella(const Target &Target_, StringRef Parent);
0245 
0246   /// Get the list of Parent Umbrella frameworks.
0247   ///
0248   /// \return Returns a list of target information and install name of parent
0249   /// umbrellas.
0250   const std::vector<std::pair<Target, std::string>> &umbrellas() const {
0251     return ParentUmbrellas;
0252   }
0253 
0254   /// Add an allowable client.
0255   ///
0256   /// Mach-O Dynamic libraries have the concept of allowable clients that are
0257   /// checked during static link time. The name of the application or library
0258   /// that is being generated needs to match one of the allowable clients or the
0259   /// linker refuses to link this library.
0260   ///
0261   /// \param InstallName The name of the client that is allowed to link this
0262   /// library.
0263   /// \param Target The target triple for which this applies.
0264   void addAllowableClient(StringRef InstallName, const Target &Target);
0265 
0266   /// Get the list of allowable clients.
0267   ///
0268   /// \return Returns a list of allowable clients.
0269   const std::vector<InterfaceFileRef> &allowableClients() const {
0270     return AllowableClients;
0271   }
0272 
0273   /// Add a re-exported library.
0274   ///
0275   /// \param InstallName The name of the library to re-export.
0276   /// \param Target The target triple for which this applies.
0277   void addReexportedLibrary(StringRef InstallName, const Target &Target);
0278 
0279   /// Get the list of re-exported libraries.
0280   ///
0281   /// \return Returns a list of re-exported libraries.
0282   const std::vector<InterfaceFileRef> &reexportedLibraries() const {
0283     return ReexportedLibraries;
0284   }
0285 
0286   /// Add a library for inlining to top level library.
0287   ///
0288   ///\param Document The library to inline with top level library.
0289   void addDocument(std::shared_ptr<InterfaceFile> &&Document);
0290 
0291   /// Returns the pointer to parent document if exists or nullptr otherwise.
0292   InterfaceFile *getParent() const { return Parent; }
0293 
0294   /// Get the list of inlined libraries.
0295   ///
0296   /// \return Returns a list of the inlined frameworks.
0297   const std::vector<std::shared_ptr<InterfaceFile>> &documents() const {
0298     return Documents;
0299   }
0300 
0301   /// Set the runpath search paths.
0302   /// \param RPath The name of runpath.
0303   /// \param InputTarget The target applicable to runpath search path.
0304   void addRPath(StringRef RPath, const Target &InputTarget);
0305 
0306   /// Get the list of runpath search paths.
0307   ///
0308   /// \return Returns a list of the rpaths per target.
0309   const std::vector<std::pair<Target, std::string>> &rpaths() const {
0310     return RPaths;
0311   }
0312 
0313   /// Get symbol if exists in file.
0314   ///
0315   /// \param Kind The kind of global symbol to record.
0316   /// \param Name The name of the symbol.
0317   /// \param ObjCIF The ObjCInterface symbol type, if applicable.
0318   std::optional<const Symbol *>
0319   getSymbol(EncodeKind Kind, StringRef Name,
0320             ObjCIFSymbolKind ObjCIF = ObjCIFSymbolKind::None) const {
0321     if (auto *Sym = SymbolsSet->findSymbol(Kind, Name, ObjCIF))
0322       return Sym;
0323     return std::nullopt;
0324   }
0325 
0326   /// Add a symbol to the symbols list or extend an existing one.
0327   template <typename RangeT, typename ElT = std::remove_reference_t<
0328                                  decltype(*std::begin(std::declval<RangeT>()))>>
0329   void addSymbol(EncodeKind Kind, StringRef Name, RangeT &&Targets,
0330                  SymbolFlags Flags = SymbolFlags::None) {
0331     SymbolsSet->addGlobal(Kind, Name, Flags, Targets);
0332   }
0333 
0334   /// Add Symbol with multiple targets.
0335   ///
0336   /// \param Kind The kind of global symbol to record.
0337   /// \param Name The name of the symbol.
0338   /// \param Targets The list of targets the symbol is defined in.
0339   /// \param Flags The properties the symbol holds.
0340   void addSymbol(EncodeKind Kind, StringRef Name, TargetList &&Targets,
0341                  SymbolFlags Flags = SymbolFlags::None) {
0342     SymbolsSet->addGlobal(Kind, Name, Flags, Targets);
0343   }
0344 
0345   /// Add Symbol with single target.
0346   ///
0347   /// \param Kind The kind of global symbol to record.
0348   /// \param Name The name of the symbol.
0349   /// \param Target The target the symbol is defined in.
0350   /// \param Flags The properties the symbol holds.
0351   void addSymbol(EncodeKind Kind, StringRef Name, Target &Target,
0352                  SymbolFlags Flags = SymbolFlags::None) {
0353     SymbolsSet->addGlobal(Kind, Name, Flags, Target);
0354   }
0355 
0356   /// Get size of symbol set.
0357   /// \return The number of symbols the file holds.
0358   size_t symbolsCount() const { return SymbolsSet->size(); }
0359 
0360   using const_symbol_range = SymbolSet::const_symbol_range;
0361   using const_filtered_symbol_range = SymbolSet::const_filtered_symbol_range;
0362 
0363   const_symbol_range symbols() const { return SymbolsSet->symbols(); };
0364   const_filtered_symbol_range exports() const { return SymbolsSet->exports(); };
0365   const_filtered_symbol_range reexports() const {
0366     return SymbolsSet->reexports();
0367   };
0368   const_filtered_symbol_range undefineds() const {
0369     return SymbolsSet->undefineds();
0370   };
0371 
0372   /// Extract architecture slice from Interface.
0373   ///
0374   /// \param Arch architecture to extract from.
0375   /// \return New InterfaceFile with extracted architecture slice.
0376   llvm::Expected<std::unique_ptr<InterfaceFile>>
0377   extract(Architecture Arch) const;
0378 
0379   /// Remove architecture slice from Interface.
0380   ///
0381   /// \param Arch architecture to remove.
0382   /// \return New Interface File with removed architecture slice.
0383   llvm::Expected<std::unique_ptr<InterfaceFile>>
0384   remove(Architecture Arch) const;
0385 
0386   /// Merge Interfaces for the same library. The following library attributes
0387   /// must match.
0388   /// * Install name, Current & Compatibility version,
0389   /// * Two-level namespace enablement, and App extension enablement.
0390   ///
0391   /// \param O The Interface to merge.
0392   /// \return New Interface File that was merged.
0393   llvm::Expected<std::unique_ptr<InterfaceFile>>
0394   merge(const InterfaceFile *O) const;
0395 
0396   /// Inline reexported library into Interface.
0397   ///
0398   /// \param Library Interface of reexported library.
0399   /// \param Overwrite Whether to overwrite preexisting inlined library.
0400   void inlineLibrary(std::shared_ptr<InterfaceFile> Library,
0401                      bool Overwrite = false);
0402 
0403   /// Set InterfaceFile properties from pre-gathered binary attributes,
0404   /// if they are not set already.
0405   ///
0406   /// \param BA Attributes typically represented in load commands.
0407   /// \param Targ MachO Target slice to add attributes to.
0408   void setFromBinaryAttrs(const RecordsSlice::BinaryAttrs &BA,
0409                           const Target &Targ);
0410 
0411   /// The equality is determined by attributes that impact linking
0412   /// compatibilities. Path, & FileKind are irrelevant since these by
0413   /// itself should not impact linking.
0414   /// This is an expensive operation.
0415   bool operator==(const InterfaceFile &O) const;
0416 
0417   bool operator!=(const InterfaceFile &O) const { return !(*this == O); }
0418 
0419 private:
0420   llvm::BumpPtrAllocator Allocator;
0421   StringRef copyString(StringRef String) {
0422     if (String.empty())
0423       return {};
0424 
0425     void *Ptr = Allocator.Allocate(String.size(), 1);
0426     memcpy(Ptr, String.data(), String.size());
0427     return StringRef(reinterpret_cast<const char *>(Ptr), String.size());
0428   }
0429 
0430   TargetList Targets;
0431   std::string Path;
0432   FileType FileKind{FileType::Invalid};
0433   std::string InstallName;
0434   PackedVersion CurrentVersion;
0435   PackedVersion CompatibilityVersion;
0436   uint8_t SwiftABIVersion{0};
0437   bool IsTwoLevelNamespace{false};
0438   bool IsOSLibNotForSharedCache{false};
0439   bool IsAppExtensionSafe{false};
0440   bool HasSimSupport{false};
0441   ObjCConstraintType ObjcConstraint = ObjCConstraintType::None;
0442   std::vector<std::pair<Target, std::string>> ParentUmbrellas;
0443   std::vector<InterfaceFileRef> AllowableClients;
0444   std::vector<InterfaceFileRef> ReexportedLibraries;
0445   std::vector<std::shared_ptr<InterfaceFile>> Documents;
0446   std::vector<std::pair<Target, std::string>> RPaths;
0447   std::unique_ptr<SymbolSet> SymbolsSet;
0448   InterfaceFile *Parent = nullptr;
0449 };
0450 
0451 // Keep containers that hold InterfaceFileRefs in sorted order and uniqued.
0452 template <typename C>
0453 typename C::iterator addEntry(C &Container, StringRef InstallName) {
0454   auto I = partition_point(Container, [=](const InterfaceFileRef &O) {
0455     return O.getInstallName() < InstallName;
0456   });
0457   if (I != Container.end() && I->getInstallName() == InstallName)
0458     return I;
0459 
0460   return Container.emplace(I, InstallName);
0461 }
0462 
0463 } // end namespace MachO.
0464 } // end namespace llvm.
0465 
0466 #endif // LLVM_TEXTAPI_INTERFACEFILE_H