LLVM 23.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
33template <> struct DenseMapInfo<SymbolsMapKey> {
34 static unsigned getHashValue(const SymbolsMapKey &Key) {
35 return hash_combine(hash_value(Key.Kind), hash_value(Key.Name));
36 }
37
38 static bool isEqual(const SymbolsMapKey &LHS, const SymbolsMapKey &RHS) {
39 return std::tie(LHS.Kind, LHS.Name) == std::tie(RHS.Kind, RHS.Name);
40 }
41};
42
43template <typename DerivedT, typename KeyInfoT, typename BucketT>
45 KeyInfoT, BucketT> &LHS,
46 const DenseMapBase<DerivedT, SymbolsMapKey, MachO::Symbol *,
47 KeyInfoT, BucketT> &RHS) {
48 if (LHS.size() != RHS.size())
49 return false;
50 for (const auto &KV : LHS) {
51 auto I = RHS.find(KV.first);
52 if (I == RHS.end() || *I->second != *KV.second)
53 return false;
54 }
55 return true;
56}
57
58template <typename DerivedT, typename KeyInfoT, typename BucketT>
60 KeyInfoT, BucketT> &LHS,
61 const DenseMapBase<DerivedT, SymbolsMapKey, MachO::Symbol *,
62 KeyInfoT, BucketT> &RHS) {
63 return !(LHS == RHS);
64}
65
66namespace MachO {
67
68class SymbolSet {
69private:
70 llvm::BumpPtrAllocator Allocator;
71 StringRef copyString(StringRef String) {
72 if (String.empty())
73 return {};
74 void *Ptr = Allocator.Allocate(String.size(), 1);
75 memcpy(Ptr, String.data(), String.size());
76 return StringRef(reinterpret_cast<const char *>(Ptr), String.size());
77 }
78
79 using SymbolsMapType = llvm::DenseMap<SymbolsMapKey, Symbol *>;
80 SymbolsMapType Symbols;
81
82 LLVM_ABI Symbol *addGlobalImpl(EncodeKind, StringRef Name, SymbolFlags Flags);
83
84public:
85 SymbolSet() = default;
86 SymbolSet(const SymbolSet &other) = delete;
87 SymbolSet &operator=(const SymbolSet &other) = delete;
90 const Target &Targ);
91 size_t size() const { return Symbols.size(); }
92
93 template <typename RangeT, typename ElT = std::remove_reference_t<
94 decltype(*std::begin(std::declval<RangeT>()))>>
96 RangeT &&Targets) {
97 auto *Global = addGlobalImpl(Kind, Name, Flags);
98 for (const auto &Targ : Targets)
99 Global->addTarget(Targ);
101 addGlobal(EncodeKind::ObjectiveCClass, Name, Flags, Targets);
102 return Global;
103 }
104
105 LLVM_ABI const Symbol *
108
110 : public iterator_adaptor_base<
112 std::forward_iterator_tag, const Symbol *, ptrdiff_t,
113 const Symbol *, const Symbol *> {
115
116 template <typename U>
118 : iterator_adaptor_base(std::forward<U &&>(u)) {}
119
120 reference operator*() const { return I->second; }
121 pointer operator->() const { return I->second; }
122 };
123
125
128 std::function<bool(const Symbol *)>>;
131
132 // Range that contains all symbols.
133 const_symbol_range symbols() const { return Symbols; }
134
135 // Range that contains all defined and exported symbols.
137 std::function<bool(const Symbol *)> fn = [](const Symbol *Symbol) {
138 return !Symbol->isUndefined() && !Symbol->isReexported();
139 };
140 return make_filter_range(symbols(), fn);
141 }
142
143 // Range that contains all reexported symbols.
145 std::function<bool(const Symbol *)> fn = [](const Symbol *Symbol) {
146 return Symbol->isReexported();
147 };
148 return make_filter_range(symbols(), fn);
149 }
150
151 // Range that contains all undefined and exported symbols.
153 std::function<bool(const Symbol *)> fn = [](const Symbol *Symbol) {
154 return Symbol->isUndefined();
155 };
156 return make_filter_range(symbols(), fn);
157 }
158
159 LLVM_ABI bool operator==(const SymbolSet &O) const;
160
161 bool operator!=(const SymbolSet &O) const { return !(Symbols == O.Symbols); }
162
163 void *allocate(size_t Size, unsigned Align = 8) {
164 return Allocator.Allocate(Size, Align);
165 }
166};
167
168} // namespace MachO
169} // namespace llvm
170#endif // LLVM_TEXTAPI_SYMBOLSET_H
This file defines the BumpPtrAllocator interface.
#define LLVM_ABI
Definition Compiler.h:213
This file defines the DenseMap class.
#define I(x, y, z)
Definition MD5.cpp:57
Value * RHS
Value * LHS
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT, true > const_iterator
Definition DenseMap.h:136
SymbolSet & operator=(const SymbolSet &other)=delete
iterator_range< const_filtered_symbol_iterator > const_filtered_symbol_range
Definition SymbolSet.h:129
filter_iterator< const_symbol_iterator, std::function< bool(const Symbol *)> > const_filtered_symbol_iterator
Definition SymbolSet.h:126
Symbol * addGlobal(EncodeKind Kind, StringRef Name, SymbolFlags Flags, RangeT &&Targets)
Definition SymbolSet.h:95
bool operator!=(const SymbolSet &O) const
Definition SymbolSet.h:161
void * allocate(size_t Size, unsigned Align=8)
Definition SymbolSet.h:163
LLVM_ABI bool operator==(const SymbolSet &O) const
iterator_range< const_symbol_iterator > const_symbol_range
Definition SymbolSet.h:124
SymbolSet(const SymbolSet &other)=delete
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:91
const_filtered_symbol_range undefineds() const
Definition SymbolSet.h:152
const_symbol_range symbols() const
Definition SymbolSet.h:133
const_filtered_symbol_range reexports() const
Definition SymbolSet.h:144
const_filtered_symbol_range exports() const
Definition SymbolSet.h:136
bool isUndefined() const
Definition Symbol.h:123
bool isReexported() const
Definition Symbol.h:127
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
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.
hash_code hash_value(const FixedPointSemantics &Val)
bool operator!=(uint64_t V1, const APInt &V2)
Definition APInt.h:2142
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:551
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
@ Global
Append to llvm.global_dtors.
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
Definition Hashing.h:325
filter_iterator_impl< WrappedIteratorT, PredicateT, detail::fwd_or_bidi_tag< WrappedIteratorT > > filter_iterator
Defines filter_iterator to a suitable specialization of filter_iterator_impl, based on the underlying...
Definition STLExtras.h:538
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:383
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:860
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
static bool isEqual(const SymbolsMapKey &LHS, const SymbolsMapKey &RHS)
Definition SymbolSet.h:38
static unsigned getHashValue(const SymbolsMapKey &Key)
Definition SymbolSet.h:34
An information struct used to provide DenseMap with the various necessary components for a given valu...
SymbolsMapKey(MachO::EncodeKind Kind, StringRef Name)
Definition SymbolSet.h:30
MachO::EncodeKind Kind
Definition SymbolSet.h:27