LLVM 20.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"
20#include "llvm/TextAPI/Symbol.h"
21#include <stddef.h>
22
23namespace llvm {
24
28
30 : Kind(Kind), Name(Name) {}
31};
32template <> struct DenseMapInfo<SymbolsMapKey> {
33 static inline SymbolsMapKey getEmptyKey() {
35 }
36
39 StringRef{});
40 }
41
42 static unsigned getHashValue(const SymbolsMapKey &Key) {
43 return hash_combine(hash_value(Key.Kind), hash_value(Key.Name));
44 }
45
46 static bool isEqual(const SymbolsMapKey &LHS, const SymbolsMapKey &RHS) {
47 return std::tie(LHS.Kind, LHS.Name) == std::tie(RHS.Kind, RHS.Name);
48 }
49};
50
51template <typename DerivedT, typename KeyInfoT, typename BucketT>
53 KeyInfoT, BucketT> &LHS,
54 const DenseMapBase<DerivedT, SymbolsMapKey, MachO::Symbol *,
55 KeyInfoT, BucketT> &RHS) {
56 if (LHS.size() != RHS.size())
57 return false;
58 for (const auto &KV : LHS) {
59 auto I = RHS.find(KV.first);
60 if (I == RHS.end() || *I->second != *KV.second)
61 return false;
62 }
63 return true;
64}
65
66template <typename DerivedT, typename KeyInfoT, typename BucketT>
68 KeyInfoT, BucketT> &LHS,
69 const DenseMapBase<DerivedT, SymbolsMapKey, MachO::Symbol *,
70 KeyInfoT, BucketT> &RHS) {
71 return !(LHS == RHS);
72}
73
74namespace MachO {
75
76class SymbolSet {
77private:
78 llvm::BumpPtrAllocator Allocator;
79 StringRef copyString(StringRef String) {
80 if (String.empty())
81 return {};
82 void *Ptr = Allocator.Allocate(String.size(), 1);
83 memcpy(Ptr, String.data(), String.size());
84 return StringRef(reinterpret_cast<const char *>(Ptr), String.size());
85 }
86
88 SymbolsMapType Symbols;
89
90 Symbol *addGlobalImpl(EncodeKind, StringRef Name, SymbolFlags Flags);
91
92public:
93 SymbolSet() = default;
95 const Target &Targ);
96 size_t size() const { return Symbols.size(); }
97
98 template <typename RangeT, typename ElT = std::remove_reference_t<
99 decltype(*std::begin(std::declval<RangeT>()))>>
101 RangeT &&Targets) {
102 auto *Global = addGlobalImpl(Kind, Name, Flags);
103 for (const auto &Targ : Targets)
104 Global->addTarget(Targ);
107 return Global;
108 }
109
110 const Symbol *
113
115 : public iterator_adaptor_base<
117 std::forward_iterator_tag, const Symbol *, ptrdiff_t,
118 const Symbol *, const Symbol *> {
120
121 template <typename U>
123 : iterator_adaptor_base(std::forward<U &&>(u)) {}
124
125 reference operator*() const { return I->second; }
126 pointer operator->() const { return I->second; }
127 };
128
130
133 std::function<bool(const Symbol *)>>;
136
137 // Range that contains all symbols.
139 return {Symbols.begin(), Symbols.end()};
140 }
141
142 // Range that contains all defined and exported symbols.
144 std::function<bool(const Symbol *)> fn = [](const Symbol *Symbol) {
145 return !Symbol->isUndefined() && !Symbol->isReexported();
146 };
147 return make_filter_range(
148 make_range<const_symbol_iterator>({Symbols.begin()}, {Symbols.end()}),
149 fn);
150 }
151
152 // Range that contains all reexported symbols.
154 std::function<bool(const Symbol *)> fn = [](const Symbol *Symbol) {
155 return Symbol->isReexported();
156 };
157 return make_filter_range(
158 make_range<const_symbol_iterator>({Symbols.begin()}, {Symbols.end()}),
159 fn);
160 }
161
162 // Range that contains all undefined and exported symbols.
164 std::function<bool(const Symbol *)> fn = [](const Symbol *Symbol) {
165 return Symbol->isUndefined();
166 };
167 return make_filter_range(
168 make_range<const_symbol_iterator>({Symbols.begin()}, {Symbols.end()}),
169 fn);
170 }
171
172 bool operator==(const SymbolSet &O) const;
173
174 bool operator!=(const SymbolSet &O) const { return !(Symbols == O.Symbols); }
175
176 void *allocate(size_t Size, unsigned Align = 8) {
177 return Allocator.Allocate(Size, Align);
178 }
179};
180
181} // namespace MachO
182} // namespace llvm
183#endif // LLVM_TEXTAPI_SYMBOLSET_H
This file defines the BumpPtrAllocator interface.
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:66
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT, true > const_iterator
Definition: DenseMap.h:73
Symbol * addGlobal(EncodeKind Kind, StringRef Name, SymbolFlags Flags, RangeT &&Targets)
Definition: SymbolSet.h:100
bool operator!=(const SymbolSet &O) const
Definition: SymbolSet.h:174
void * allocate(size_t Size, unsigned Align=8)
Definition: SymbolSet.h:176
Symbol * addGlobal(EncodeKind Kind, StringRef Name, SymbolFlags Flags, const Target &Targ)
Definition: SymbolSet.cpp:24
const Symbol * findSymbol(EncodeKind Kind, StringRef Name, ObjCIFSymbolKind ObjCIF=ObjCIFSymbolKind::None) const
Definition: SymbolSet.cpp:31
size_t size() const
Definition: SymbolSet.h:96
const_filtered_symbol_range undefineds() const
Definition: SymbolSet.h:163
const_symbol_range symbols() const
Definition: SymbolSet.h:138
bool operator==(const SymbolSet &O) const
const_filtered_symbol_range reexports() const
Definition: SymbolSet.h:153
const_filtered_symbol_range exports() const
Definition: SymbolSet.h:143
bool isUndefined() const
Definition: Symbol.h:122
bool isReexported() const
Definition: Symbol.h:126
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Specialization of filter_iterator_base for forward iteration only.
Definition: STLExtras.h:497
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:69
EncodeKind
Mapping of entry types in TextStubs.
Definition: Symbol.h:55
SymbolFlags
Symbol flags.
Definition: Symbol.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
hash_code hash_value(const FixedPointSemantics &Val)
Definition: APFixedPoint.h:128
bool operator!=(uint64_t V1, const APInt &V2)
Definition: APInt.h:2062
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:572
@ Global
Append to llvm.global_dtors.
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
Definition: Hashing.h:593
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
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:33
static SymbolsMapKey getTombstoneKey()
Definition: SymbolSet.h:37
static bool isEqual(const SymbolsMapKey &LHS, const SymbolsMapKey &RHS)
Definition: SymbolSet.h:46
static unsigned getHashValue(const SymbolsMapKey &Key)
Definition: SymbolSet.h:42
An information struct used to provide DenseMap with the various necessary components for a given valu...
Definition: DenseMapInfo.h:52
StringRef Name
Definition: SymbolSet.h:27
SymbolsMapKey(MachO::EncodeKind Kind, StringRef Name)
Definition: SymbolSet.h:29
MachO::EncodeKind Kind
Definition: SymbolSet.h:26