LLVM 22.0.0git
StringMapEntry.h
Go to the documentation of this file.
1//===- StringMapEntry.h - String Hash table map interface -------*- 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/// \file
10/// This file defines the StringMapEntry class - it is intended to be a low
11/// dependency implementation detail of StringMap that is more suitable for
12/// inclusion in public headers than StringMap.h itself is.
13///
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_ADT_STRINGMAPENTRY_H
17#define LLVM_ADT_STRINGMAPENTRY_H
18
19#include "llvm/ADT/StringRef.h"
20#include <utility>
21
22namespace llvm {
23
24/// The "value type" of StringSet represented as an empty struct.
26
27/// StringMapEntryBase - Shared base class of StringMapEntry instances.
29 size_t keyLength;
30
31public:
32 explicit StringMapEntryBase(size_t keyLength) : keyLength(keyLength) {}
33
34 size_t getKeyLength() const { return keyLength; }
35
36protected:
37 /// Helper to tail-allocate \p Key. It'd be nice to generalize this so it
38 /// could be reused elsewhere, maybe even taking an llvm::function_ref to
39 /// type-erase the allocator and put it in a source file.
40 template <typename AllocatorTy>
41 static void *allocateWithKey(size_t EntrySize, size_t EntryAlign,
42 StringRef Key, AllocatorTy &Allocator);
43};
44
45// Define out-of-line to dissuade inlining.
46template <typename AllocatorTy>
47void *StringMapEntryBase::allocateWithKey(size_t EntrySize, size_t EntryAlign,
49 AllocatorTy &Allocator) {
50 size_t KeyLength = Key.size();
51
52 // Allocate a new item with space for the string at the end and a null
53 // terminator.
54 size_t AllocSize = EntrySize + KeyLength + 1;
55 void *Allocation = Allocator.Allocate(AllocSize, EntryAlign);
56 assert(Allocation && "Unhandled out-of-memory");
57
58 // Copy the string information.
59 char *Buffer = reinterpret_cast<char *>(Allocation) + EntrySize;
60 if (KeyLength > 0)
61 ::memcpy(Buffer, Key.data(), KeyLength);
62 Buffer[KeyLength] = 0; // Null terminate for convenience of clients.
63 return Allocation;
64}
65
66/// StringMapEntryStorage - Holds the value in a StringMapEntry.
67///
68/// Factored out into a separate base class to make it easier to specialize.
69/// This is primarily intended to support StringSet, which doesn't need a value
70/// stored at all.
71template <typename ValueTy>
73public:
74 ValueTy second;
75
76 explicit StringMapEntryStorage(size_t keyLength)
77 : StringMapEntryBase(keyLength), second() {}
78 template <typename... InitTy>
79 StringMapEntryStorage(size_t keyLength, InitTy &&...initVals)
80 : StringMapEntryBase(keyLength),
81 second(std::forward<InitTy>(initVals)...) {}
83
84 const ValueTy &getValue() const { return second; }
85 ValueTy &getValue() { return second; }
86
87 void setValue(const ValueTy &V) { second = V; }
88};
89
90template <>
92public:
93 explicit StringMapEntryStorage(size_t keyLength, EmptyStringSetTag = {})
94 : StringMapEntryBase(keyLength) {}
96
97 EmptyStringSetTag getValue() const { return {}; }
98};
99
100/// StringMapEntry - This is used to represent one value that is inserted into
101/// a StringMap. It contains the Value itself and the key: the string length
102/// and data.
103template <typename ValueTy>
104class StringMapEntry final : public StringMapEntryStorage<ValueTy> {
105public:
107
108 using ValueType = ValueTy;
109
111 return StringRef(getKeyData(), this->getKeyLength());
112 }
113
114 /// getKeyData - Return the start of the string data that is the key for this
115 /// value. The string data is always stored immediately after the
116 /// StringMapEntry object.
117 const char *getKeyData() const {
118 return reinterpret_cast<const char *>(this + 1);
119 }
120
121 StringRef first() const { return getKey(); }
122
123 /// Create a StringMapEntry for the specified key construct the value using
124 /// \p InitiVals.
125 template <typename AllocatorTy, typename... InitTy>
126 static StringMapEntry *create(StringRef key, AllocatorTy &allocator,
127 InitTy &&...initVals) {
129 sizeof(StringMapEntry), alignof(StringMapEntry), key, allocator))
130 StringMapEntry(key.size(), std::forward<InitTy>(initVals)...);
131 }
132
133 /// GetStringMapEntryFromKeyData - Given key data that is known to be embedded
134 /// into a StringMapEntry, return the StringMapEntry itself.
135 static StringMapEntry &GetStringMapEntryFromKeyData(const char *keyData) {
136 char *ptr = const_cast<char *>(keyData) - sizeof(StringMapEntry<ValueTy>);
137 return *reinterpret_cast<StringMapEntry *>(ptr);
138 }
139
140 /// Destroy - Destroy this StringMapEntry, releasing memory back to the
141 /// specified allocator.
142 template <typename AllocatorTy> void Destroy(AllocatorTy &allocator) {
143 // Free memory referenced by the item.
144 size_t AllocSize = sizeof(StringMapEntry) + this->getKeyLength() + 1;
145 this->~StringMapEntry();
146 allocator.Deallocate(static_cast<void *>(this), AllocSize,
147 alignof(StringMapEntry));
148 }
149};
150
151// Allow structured bindings on StringMapEntry.
152
153template <std::size_t Index, typename ValueTy>
154decltype(auto) get(StringMapEntry<ValueTy> &E) {
155 static_assert(Index < 2);
156 if constexpr (Index == 0)
157 return E.getKey();
158 else
159 return E.getValue();
160}
161
162template <std::size_t Index, typename ValueTy>
163decltype(auto) get(const StringMapEntry<ValueTy> &E) {
164 static_assert(Index < 2);
165 if constexpr (Index == 0)
166 return E.getKey();
167 else
168 return E.getValue();
169}
170
171} // end namespace llvm
172
173template <typename ValueTy>
174struct std::tuple_size<llvm::StringMapEntry<ValueTy>>
175 : std::integral_constant<std::size_t, 2> {};
176
177template <std::size_t Index, typename ValueTy>
178struct std::tuple_element<Index, llvm::StringMapEntry<ValueTy>>
179 : std::tuple_element<Index, std::pair<llvm::StringRef, ValueTy>> {};
180
181#endif // LLVM_ADT_STRINGMAPENTRY_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Basic Register Allocator
StringMapEntryBase - Shared base class of StringMapEntry instances.
size_t getKeyLength() const
static void * allocateWithKey(size_t EntrySize, size_t EntryAlign, StringRef Key, AllocatorTy &Allocator)
Helper to tail-allocate Key.
StringMapEntryBase(size_t keyLength)
StringMapEntryStorage(size_t keyLength, EmptyStringSetTag={})
StringMapEntryStorage(StringMapEntryStorage &entry)=delete
StringMapEntryStorage(size_t keyLength)
StringMapEntryStorage(StringMapEntryStorage &e)=delete
StringMapEntryStorage(size_t keyLength, InitTy &&...initVals)
const ValueTy & getValue() const
void setValue(const ValueTy &V)
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
static StringMapEntry * create(StringRef key, AllocatorTy &allocator, InitTy &&...initVals)
Create a StringMapEntry for the specified key construct the value using InitiVals.
StringRef getKey() const
StringRef first() const
void Destroy(AllocatorTy &allocator)
Destroy - Destroy this StringMapEntry, releasing memory back to the specified allocator.
static StringMapEntry & GetStringMapEntryFromKeyData(const char *keyData)
GetStringMapEntryFromKeyData - Given key data that is known to be embedded into a StringMapEntry,...
const char * getKeyData() const
getKeyData - Return the start of the string data that is the key for this value.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr size_t size() const
size - Get the string size.
Definition StringRef.h:146
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:867
The "value type" of StringSet represented as an empty struct.