#ifndef LLVM_TEXTAPI_INTERFACEFILE_H
#define LLVM_TEXTAPI_INTERFACEFILE_H
#include "llvm/ADT/BitmaskEnum.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/Hashing.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/iterator.h"
#include "llvm/Support/Allocator.h"
#include "llvm/TextAPI/ArchitectureSet.h"
#include "llvm/TextAPI/PackedVersion.h"
#include "llvm/TextAPI/Platform.h"
#include "llvm/TextAPI/Symbol.h"
#include "llvm/TextAPI/Target.h"
namespace llvm {
namespace MachO {
enum class ObjCConstraintType : unsigned {
None = 0,
Retain_Release = 1,
Retain_Release_For_Simulator = 2,
Retain_Release_Or_GC = 3,
GC = 4,
};
enum FileType : unsigned {
Invalid = 0U,
TBD_V1 = 1U << 0,
TBD_V2 = 1U << 1,
TBD_V3 = 1U << 2,
TBD_V4 = 1U << 3,
All = ~0U,
LLVM_MARK_AS_BITMASK_ENUM(All),
};
class InterfaceFileRef {
public:
InterfaceFileRef() = default;
InterfaceFileRef(StringRef InstallName) : InstallName(InstallName) {}
InterfaceFileRef(StringRef InstallName, const TargetList Targets)
: InstallName(InstallName), Targets(std::move(Targets)) {}
StringRef getInstallName() const { return InstallName; };
void addTarget(const Target &Target);
template <typename RangeT> void addTargets(RangeT &&Targets) {
for (const auto &Target : Targets)
addTarget(Target(Target));
}
using const_target_iterator = TargetList::const_iterator;
using const_target_range = llvm::iterator_range<const_target_iterator>;
const_target_range targets() const { return {Targets}; }
ArchitectureSet getArchitectures() const {
return mapToArchitectureSet(Targets);
}
PlatformSet getPlatforms() const { return mapToPlatformSet(Targets); }
bool operator==(const InterfaceFileRef &O) const {
return std::tie(InstallName, Targets) == std::tie(O.InstallName, O.Targets);
}
bool operator!=(const InterfaceFileRef &O) const {
return std::tie(InstallName, Targets) != std::tie(O.InstallName, O.Targets);
}
bool operator<(const InterfaceFileRef &O) const {
return std::tie(InstallName, Targets) < std::tie(O.InstallName, O.Targets);
}
private:
std::string InstallName;
TargetList Targets;
};
}
struct SymbolsMapKey {
MachO::SymbolKind Kind;
StringRef Name;
SymbolsMapKey(MachO::SymbolKind Kind, StringRef Name)
: Kind(Kind), Name(Name) {}
};
template <> struct DenseMapInfo<SymbolsMapKey> {
static inline SymbolsMapKey getEmptyKey() {
return SymbolsMapKey(MachO::SymbolKind::GlobalSymbol, StringRef{});
}
static inline SymbolsMapKey getTombstoneKey() {
return SymbolsMapKey(MachO::SymbolKind::ObjectiveCInstanceVariable,
StringRef{});
}
static unsigned getHashValue(const SymbolsMapKey &Key) {
return hash_combine(hash_value(Key.Kind), hash_value(Key.Name));
}
static bool isEqual(const SymbolsMapKey &LHS, const SymbolsMapKey &RHS) {
return std::tie(LHS.Kind, LHS.Name) == std::tie(RHS.Kind, RHS.Name);
}
};
namespace MachO {
class InterfaceFile {
public:
void setPath(StringRef Path_) { Path = std::string(Path_); }
StringRef getPath() const { return Path; }
void setFileType(FileType Kind) { FileKind = Kind; }
FileType getFileType() const { return FileKind; }
ArchitectureSet getArchitectures() const {
return mapToArchitectureSet(Targets);
}
PlatformSet getPlatforms() const { return mapToPlatformSet(Targets); }
void addTarget(const Target &Target);
template <typename RangeT> void addTargets(RangeT &&Targets) {
for (const auto &Target_ : Targets)
addTarget(Target(Target_));
}
using const_target_iterator = TargetList::const_iterator;
using const_target_range = llvm::iterator_range<const_target_iterator>;
const_target_range targets() const { return {Targets}; }
using const_filtered_target_iterator =
llvm::filter_iterator<const_target_iterator,
std::function<bool(const Target &)>>;
using const_filtered_target_range =
llvm::iterator_range<const_filtered_target_iterator>;
const_filtered_target_range targets(ArchitectureSet Archs) const;
void setInstallName(StringRef InstallName_) {
InstallName = std::string(InstallName_);
}
StringRef getInstallName() const { return InstallName; }
void setCurrentVersion(PackedVersion Version) { CurrentVersion = Version; }
PackedVersion getCurrentVersion() const { return CurrentVersion; }
void setCompatibilityVersion(PackedVersion Version) {
CompatibilityVersion = Version;
}
PackedVersion getCompatibilityVersion() const { return CompatibilityVersion; }
void setSwiftABIVersion(uint8_t Version) { SwiftABIVersion = Version; }
uint8_t getSwiftABIVersion() const { return SwiftABIVersion; }
void setTwoLevelNamespace(bool V = true) { IsTwoLevelNamespace = V; }
bool isTwoLevelNamespace() const { return IsTwoLevelNamespace; }
void setApplicationExtensionSafe(bool V = true) { IsAppExtensionSafe = V; }
bool isApplicationExtensionSafe() const { return IsAppExtensionSafe; }
void setObjCConstraint(ObjCConstraintType Constraint) {
ObjcConstraint = Constraint;
}
ObjCConstraintType getObjCConstraint() const { return ObjcConstraint; }
void setInstallAPI(bool V = true) { IsInstallAPI = V; }
bool isInstallAPI() const { return IsInstallAPI; }
void addParentUmbrella(const Target &Target_, StringRef Parent);
const std::vector<std::pair<Target, std::string>> &umbrellas() const {
return ParentUmbrellas;
}
void addAllowableClient(StringRef InstallName, const Target &Target);
const std::vector<InterfaceFileRef> &allowableClients() const {
return AllowableClients;
}
void addReexportedLibrary(StringRef InstallName, const Target &Target);
const std::vector<InterfaceFileRef> &reexportedLibraries() const {
return ReexportedLibraries;
}
void addUUID(const Target &Target, StringRef UUID);
void addUUID(const Target &Target, uint8_t UUID[16]);
const std::vector<std::pair<Target, std::string>> &uuids() const {
return UUIDs;
}
void addDocument(std::shared_ptr<InterfaceFile> &&Document);
InterfaceFile *getParent() const { return Parent; }
const std::vector<std::shared_ptr<InterfaceFile>> &documents() const {
return Documents;
}
void addSymbol(SymbolKind Kind, StringRef Name, const TargetList &Targets,
SymbolFlags Flags = SymbolFlags::None);
using SymbolMapType = DenseMap<SymbolsMapKey, Symbol *>;
struct const_symbol_iterator
: public iterator_adaptor_base<
const_symbol_iterator, SymbolMapType::const_iterator,
std::forward_iterator_tag, const Symbol *, ptrdiff_t,
const Symbol *, const Symbol *> {
const_symbol_iterator() = default;
template <typename U>
const_symbol_iterator(U &&u)
: iterator_adaptor_base(std::forward<U &&>(u)) {}
reference operator*() const { return I->second; }
pointer operator->() const { return I->second; }
};
using const_symbol_range = iterator_range<const_symbol_iterator>;
using const_filtered_symbol_iterator =
filter_iterator<const_symbol_iterator,
std::function<bool(const Symbol *)>>;
using const_filtered_symbol_range =
iterator_range<const_filtered_symbol_iterator>;
const_symbol_range symbols() const {
return {Symbols.begin(), Symbols.end()};
}
size_t symbolsCount() const { return Symbols.size(); }
const_filtered_symbol_range exports() const {
std::function<bool(const Symbol *)> fn = [](const Symbol *Symbol) {
return !Symbol->isUndefined();
};
return make_filter_range(
make_range<const_symbol_iterator>({Symbols.begin()}, {Symbols.end()}),
fn);
}
const_filtered_symbol_range undefineds() const {
std::function<bool(const Symbol *)> fn = [](const Symbol *Symbol) {
return Symbol->isUndefined();
};
return make_filter_range(
make_range<const_symbol_iterator>({Symbols.begin()}, {Symbols.end()}),
fn);
}
bool operator==(const InterfaceFile &O) const;
bool operator!=(const InterfaceFile &O) const { return !(*this == O); }
private:
llvm::BumpPtrAllocator Allocator;
StringRef copyString(StringRef String) {
if (String.empty())
return {};
void *Ptr = Allocator.Allocate(String.size(), 1);
memcpy(Ptr, String.data(), String.size());
return StringRef(reinterpret_cast<const char *>(Ptr), String.size());
}
TargetList Targets;
std::string Path;
FileType FileKind;
std::string InstallName;
PackedVersion CurrentVersion;
PackedVersion CompatibilityVersion;
uint8_t SwiftABIVersion{0};
bool IsTwoLevelNamespace{false};
bool IsAppExtensionSafe{false};
bool IsInstallAPI{false};
ObjCConstraintType ObjcConstraint = ObjCConstraintType::None;
std::vector<std::pair<Target, std::string>> ParentUmbrellas;
std::vector<InterfaceFileRef> AllowableClients;
std::vector<InterfaceFileRef> ReexportedLibraries;
std::vector<std::shared_ptr<InterfaceFile>> Documents;
std::vector<std::pair<Target, std::string>> UUIDs;
SymbolMapType Symbols;
InterfaceFile *Parent = nullptr;
};
template <typename DerivedT, typename KeyInfoT, typename BucketT>
bool operator==(const DenseMapBase<DerivedT, SymbolsMapKey, MachO::Symbol *,
KeyInfoT, BucketT> &LHS,
const DenseMapBase<DerivedT, SymbolsMapKey, MachO::Symbol *,
KeyInfoT, BucketT> &RHS) {
if (LHS.size() != RHS.size())
return false;
for (const auto &KV : LHS) {
auto I = RHS.find(KV.first);
if (I == RHS.end() || *I->second != *KV.second)
return false;
}
return true;
}
} }
#endif