File indexing completed on 2026-05-10 08:44:38
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef LLVM_TEXTAPI_SYMBOLSET_H
0010 #define LLVM_TEXTAPI_SYMBOLSET_H
0011
0012 #include "llvm/ADT/DenseMap.h"
0013 #include "llvm/ADT/Hashing.h"
0014 #include "llvm/ADT/StringRef.h"
0015 #include "llvm/ADT/iterator.h"
0016 #include "llvm/ADT/iterator_range.h"
0017 #include "llvm/Support/Allocator.h"
0018 #include "llvm/TextAPI/Architecture.h"
0019 #include "llvm/TextAPI/ArchitectureSet.h"
0020 #include "llvm/TextAPI/Symbol.h"
0021 #include <stddef.h>
0022
0023 namespace llvm {
0024
0025 struct SymbolsMapKey {
0026 MachO::EncodeKind Kind;
0027 StringRef Name;
0028
0029 SymbolsMapKey(MachO::EncodeKind Kind, StringRef Name)
0030 : Kind(Kind), Name(Name) {}
0031 };
0032 template <> struct DenseMapInfo<SymbolsMapKey> {
0033 static inline SymbolsMapKey getEmptyKey() {
0034 return SymbolsMapKey(MachO::EncodeKind::GlobalSymbol, StringRef{});
0035 }
0036
0037 static inline SymbolsMapKey getTombstoneKey() {
0038 return SymbolsMapKey(MachO::EncodeKind::ObjectiveCInstanceVariable,
0039 StringRef{});
0040 }
0041
0042 static unsigned getHashValue(const SymbolsMapKey &Key) {
0043 return hash_combine(hash_value(Key.Kind), hash_value(Key.Name));
0044 }
0045
0046 static bool isEqual(const SymbolsMapKey &LHS, const SymbolsMapKey &RHS) {
0047 return std::tie(LHS.Kind, LHS.Name) == std::tie(RHS.Kind, RHS.Name);
0048 }
0049 };
0050
0051 template <typename DerivedT, typename KeyInfoT, typename BucketT>
0052 bool operator==(const DenseMapBase<DerivedT, SymbolsMapKey, MachO::Symbol *,
0053 KeyInfoT, BucketT> &LHS,
0054 const DenseMapBase<DerivedT, SymbolsMapKey, MachO::Symbol *,
0055 KeyInfoT, BucketT> &RHS) {
0056 if (LHS.size() != RHS.size())
0057 return false;
0058 for (const auto &KV : LHS) {
0059 auto I = RHS.find(KV.first);
0060 if (I == RHS.end() || *I->second != *KV.second)
0061 return false;
0062 }
0063 return true;
0064 }
0065
0066 template <typename DerivedT, typename KeyInfoT, typename BucketT>
0067 bool operator!=(const DenseMapBase<DerivedT, SymbolsMapKey, MachO::Symbol *,
0068 KeyInfoT, BucketT> &LHS,
0069 const DenseMapBase<DerivedT, SymbolsMapKey, MachO::Symbol *,
0070 KeyInfoT, BucketT> &RHS) {
0071 return !(LHS == RHS);
0072 }
0073
0074 namespace MachO {
0075
0076 class SymbolSet {
0077 private:
0078 llvm::BumpPtrAllocator Allocator;
0079 StringRef copyString(StringRef String) {
0080 if (String.empty())
0081 return {};
0082 void *Ptr = Allocator.Allocate(String.size(), 1);
0083 memcpy(Ptr, String.data(), String.size());
0084 return StringRef(reinterpret_cast<const char *>(Ptr), String.size());
0085 }
0086
0087 using SymbolsMapType = llvm::DenseMap<SymbolsMapKey, Symbol *>;
0088 SymbolsMapType Symbols;
0089
0090 Symbol *addGlobalImpl(EncodeKind, StringRef Name, SymbolFlags Flags);
0091
0092 public:
0093 SymbolSet() = default;
0094 Symbol *addGlobal(EncodeKind Kind, StringRef Name, SymbolFlags Flags,
0095 const Target &Targ);
0096 size_t size() const { return Symbols.size(); }
0097
0098 template <typename RangeT, typename ElT = std::remove_reference_t<
0099 decltype(*std::begin(std::declval<RangeT>()))>>
0100 Symbol *addGlobal(EncodeKind Kind, StringRef Name, SymbolFlags Flags,
0101 RangeT &&Targets) {
0102 auto *Global = addGlobalImpl(Kind, Name, Flags);
0103 for (const auto &Targ : Targets)
0104 Global->addTarget(Targ);
0105 if (Kind == EncodeKind::ObjectiveCClassEHType)
0106 addGlobal(EncodeKind::ObjectiveCClass, Name, Flags, Targets);
0107 return Global;
0108 }
0109
0110 const Symbol *
0111 findSymbol(EncodeKind Kind, StringRef Name,
0112 ObjCIFSymbolKind ObjCIF = ObjCIFSymbolKind::None) const;
0113
0114 struct const_symbol_iterator
0115 : public iterator_adaptor_base<
0116 const_symbol_iterator, SymbolsMapType::const_iterator,
0117 std::forward_iterator_tag, const Symbol *, ptrdiff_t,
0118 const Symbol *, const Symbol *> {
0119 const_symbol_iterator() = default;
0120
0121 template <typename U>
0122 const_symbol_iterator(U &&u)
0123 : iterator_adaptor_base(std::forward<U &&>(u)) {}
0124
0125 reference operator*() const { return I->second; }
0126 pointer operator->() const { return I->second; }
0127 };
0128
0129 using const_symbol_range = iterator_range<const_symbol_iterator>;
0130
0131 using const_filtered_symbol_iterator =
0132 filter_iterator<const_symbol_iterator,
0133 std::function<bool(const Symbol *)>>;
0134 using const_filtered_symbol_range =
0135 iterator_range<const_filtered_symbol_iterator>;
0136
0137
0138 const_symbol_range symbols() const {
0139 return {Symbols.begin(), Symbols.end()};
0140 }
0141
0142
0143 const_filtered_symbol_range exports() const {
0144 std::function<bool(const Symbol *)> fn = [](const Symbol *Symbol) {
0145 return !Symbol->isUndefined() && !Symbol->isReexported();
0146 };
0147 return make_filter_range(
0148 make_range<const_symbol_iterator>({Symbols.begin()}, {Symbols.end()}),
0149 fn);
0150 }
0151
0152
0153 const_filtered_symbol_range reexports() const {
0154 std::function<bool(const Symbol *)> fn = [](const Symbol *Symbol) {
0155 return Symbol->isReexported();
0156 };
0157 return make_filter_range(
0158 make_range<const_symbol_iterator>({Symbols.begin()}, {Symbols.end()}),
0159 fn);
0160 }
0161
0162
0163 const_filtered_symbol_range undefineds() const {
0164 std::function<bool(const Symbol *)> fn = [](const Symbol *Symbol) {
0165 return Symbol->isUndefined();
0166 };
0167 return make_filter_range(
0168 make_range<const_symbol_iterator>({Symbols.begin()}, {Symbols.end()}),
0169 fn);
0170 }
0171
0172 bool operator==(const SymbolSet &O) const;
0173
0174 bool operator!=(const SymbolSet &O) const { return !(Symbols == O.Symbols); }
0175
0176 void *allocate(size_t Size, unsigned Align = 8) {
0177 return Allocator.Allocate(Size, Align);
0178 }
0179 };
0180
0181 }
0182 }
0183 #endif