LLVM 22.0.0git
SymbolSet.h
Go to the documentation of this file.
1//===- llvm/TextAPI/SymbolSet.h - TAPI Symbol Set --------------*- 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#ifndef LLVM_TEXTAPI_SYMBOLSET_H
10#define LLVM_TEXTAPI_SYMBOLSET_H
11
12#include "llvm/ADT/DenseMap.h"
13#include "llvm/ADT/Hashing.h"
14#include "llvm/ADT/StringRef.h"
15#include "llvm/ADT/iterator.h"
21#include "llvm/TextAPI/Symbol.h"
22#include <stddef.h>
23
24namespace llvm {
25
29
31 : Kind(Kind), Name(Name) {}
32};
33template <> struct DenseMapInfo<SymbolsMapKey> {
34 static inline SymbolsMapKey getEmptyKey() {
36 }
37
40 StringRef{});
41 }
42
43 static unsigned getHashValue(const SymbolsMapKey &Key) {
44 return hash_combine(hash_value(Key.Kind), hash_value(Key.Name));
45 }
46
47 static bool isEqual(const SymbolsMapKey &LHS, const SymbolsMapKey &RHS) {
48 return std::tie(LHS.Kind, LHS.Name) == std::tie(RHS.Kind, RHS.Name);
49 }
50};
51
52template <typename DerivedT, typename KeyInfoT, typename BucketT>
54 KeyInfoT, BucketT> &LHS,
55 const DenseMapBase<DerivedT, SymbolsMapKey, MachO::Symbol *,
56 KeyInfoT, BucketT> &RHS) {
57 if (LHS.size() != RHS.size())
58 return false;
59 for (const auto &KV : LHS) {
60 auto I = RHS.find(KV.first);
61 if (I == RHS.end() || *I->second != *KV.second)
62 return false;
63 }
64 return true;
65}
66
67template <typename DerivedT, typename KeyInfoT, typename BucketT>
69 KeyInfoT, BucketT> &LHS,
70 const DenseMapBase<DerivedT, SymbolsMapKey, MachO::Symbol *,
71 KeyInfoT, BucketT> &RHS) {
72 return !(LHS == RHS);
73}
74
75namespace MachO {
76
77class SymbolSet {
78private:
79 llvm::BumpPtrAllocator Allocator;
80 StringRef copyString(StringRef String) {
81 if (String.empty())
82 return {};
83 void *Ptr = Allocator.Allocate(String.size(), 1);
84 memcpy(Ptr, String.data(), String.size());
85 return StringRef(reinterpret_cast<const char *>(Ptr), String.size());
86 }
87
89 SymbolsMapType Symbols;
90
91 LLVM_ABI Symbol *addGlobalImpl(EncodeKind, StringRef Name, SymbolFlags Flags);
92
93public:
94 SymbolSet() = default;
97 const Target &Targ);
98 size_t size() const { return Symbols.size(); }
99
100 template <typename RangeT, typename ElT = std::remove_reference_t<
101 decltype(*std::begin(std::declval<RangeT>()))>>
103 RangeT &&Targets) {
104 auto *Global = addGlobalImpl(Kind, Name, Flags);
105 for (const auto &Targ : Targets)
106 Global->addTarget(Targ);
109 return Global;
110 }
111
112 LLVM_ABI const Symbol *
115
117 : public iterator_adaptor_base<
119 std::forward_iterator_tag, const Symbol *, ptrdiff_t,
120 const Symbol *, const Symbol *> {
122
123 template <typename U>
125 : iterator_adaptor_base(std::forward<U &&>(u)) {}
126
127 reference operator*() const { return I->second; }
128 pointer operator->() const { return I->second; }
129 };
130
132
135 std::function<bool(const Symbol *)>>;
138
139 // Range that contains all symbols.
141 return {Symbols.begin(), Symbols.end()};
142 }
143
144 // Range that contains all defined and exported symbols.
146 std::function<bool(const Symbol *)> fn = [](const Symbol *Symbol) {
147 return !Symbol->isUndefined() && !Symbol->isReexported();
148 };
149 return make_filter_range(
150 make_range<const_symbol_iterator>({Symbols.begin()}, {Symbols.end()}),
151 fn);
152 }
153
154 // Range that contains all reexported symbols.
156 std::function<bool(const Symbol *)> fn = [](const Symbol *Symbol) {
157 return Symbol->isReexported();
158 };
159 return make_filter_range(
160 make_range<const_symbol_iterator>({Symbols.begin()}, {Symbols.end()}),
161 fn);
162 }
163
164 // Range that contains all undefined and exported symbols.
166 std::function<bool(const Symbol *)> fn = [](const Symbol *Symbol) {
167 return Symbol->isUndefined();
168 };
169 return make_filter_range(
170 make_range<const_symbol_iterator>({Symbols.begin()}, {Symbols.end()}),
171 fn);
172 }
173
174 LLVM_ABI bool operator==(const SymbolSet &O) const;
175
176 bool operator!=(const SymbolSet &O) const { return !(Symbols == O.Symbols); }
177
178 void *allocate(size_t Size, unsigned Align = 8) {
179 return Allocator.Allocate(Size, Align);
180 }
181};
182
183} // namespace MachO
184} // namespace llvm
185#endif // LLVM_TEXTAPI_SYMBOLSET_H
This file defines the BumpPtrAllocator interface.
#define LLVM_ABI
Definition: Compiler.h:213
This file defines the DenseMap class.
std::string Name
uint64_t Size
#define I(x, y, z)
Definition: MD5.cpp:58
Basic Register Allocator
Value * RHS
Value * LHS
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:67
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT, true > const_iterator
Definition: DenseMap.h:76
Symbol * addGlobal(EncodeKind Kind, StringRef Name, SymbolFlags Flags, RangeT &&Targets)
Definition: SymbolSet.h:102
bool operator!=(const SymbolSet &O) const
Definition: SymbolSet.h:176
void * allocate(size_t Size, unsigned Align=8)
Definition: SymbolSet.h:178
LLVM_ABI bool operator==(const SymbolSet &O) const
LLVM_ABI Symbol * addGlobal(EncodeKind Kind, StringRef Name, SymbolFlags Flags, const Target &Targ)
Definition: SymbolSet.cpp:29
LLVM_ABI const Symbol * findSymbol(EncodeKind Kind, StringRef Name, ObjCIFSymbolKind ObjCIF=ObjCIFSymbolKind::None) const
Definition: SymbolSet.cpp:36
size_t size() const
Definition: SymbolSet.h:98
const_filtered_symbol_range undefineds() const
Definition: SymbolSet.h:165
const_symbol_range symbols() const
Definition: SymbolSet.h:140
const_filtered_symbol_range reexports() const
Definition: SymbolSet.h:155
const_filtered_symbol_range exports() const
Definition: SymbolSet.h:145
bool isUndefined() const
Definition: Symbol.h:123
bool isReexported() const
Definition: Symbol.h:127
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
Specialization of filter_iterator_base for forward iteration only.
Definition: STLExtras.h:506
CRTP base class for adapting an iterator to a different type.
Definition: iterator.h:237
A range adaptor for a pair of iterators.
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
ObjCIFSymbolKind
ObjC Interface symbol mappings.
Definition: Symbol.h:70
EncodeKind
Mapping of entry types in TextStubs.
Definition: Symbol.h:56
SymbolFlags
Symbol flags.
Definition: Symbol.h:25
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
hash_code hash_value(const FixedPointSemantics &Val)
Definition: APFixedPoint.h:137
bool operator!=(uint64_t V1, const APInt &V2)
Definition: APInt.h:2113
bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)
iterator_range< filter_iterator< detail::IterOfRange< RangeT >, PredicateT > > make_filter_range(RangeT &&Range, PredicateT Pred)
Convenience function that takes a range of elements and a predicate, and return a new filter_iterator...
Definition: STLExtras.h:581
@ Global
Append to llvm.global_dtors.
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
Definition: Hashing.h:595
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:856
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
static SymbolsMapKey getEmptyKey()
Definition: SymbolSet.h:34
static SymbolsMapKey getTombstoneKey()
Definition: SymbolSet.h:38
static bool isEqual(const SymbolsMapKey &LHS, const SymbolsMapKey &RHS)
Definition: SymbolSet.h:47
static unsigned getHashValue(const SymbolsMapKey &Key)
Definition: SymbolSet.h:43
An information struct used to provide DenseMap with the various necessary components for a given valu...
Definition: DenseMapInfo.h:54
StringRef Name
Definition: SymbolSet.h:28
SymbolsMapKey(MachO::EncodeKind Kind, StringRef Name)
Definition: SymbolSet.h:30
MachO::EncodeKind Kind
Definition: SymbolSet.h:27