xref: /freebsd-src/contrib/llvm-project/llvm/include/llvm/ADT/StringMapEntry.h (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
15ffd83dbSDimitry Andric //===- StringMapEntry.h - String Hash table map interface -------*- C++ -*-===//
25ffd83dbSDimitry Andric //
35ffd83dbSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45ffd83dbSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55ffd83dbSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65ffd83dbSDimitry Andric //
75ffd83dbSDimitry Andric //===----------------------------------------------------------------------===//
81fd87a68SDimitry Andric ///
91fd87a68SDimitry Andric /// \file
101fd87a68SDimitry Andric /// This file defines the StringMapEntry class - it is intended to be a low
111fd87a68SDimitry Andric /// dependency implementation detail of StringMap that is more suitable for
121fd87a68SDimitry Andric /// inclusion in public headers than StringMap.h itself is.
131fd87a68SDimitry Andric ///
145ffd83dbSDimitry Andric //===----------------------------------------------------------------------===//
155ffd83dbSDimitry Andric 
165ffd83dbSDimitry Andric #ifndef LLVM_ADT_STRINGMAPENTRY_H
175ffd83dbSDimitry Andric #define LLVM_ADT_STRINGMAPENTRY_H
185ffd83dbSDimitry Andric 
195ffd83dbSDimitry Andric #include "llvm/ADT/StringRef.h"
20*bdd1243dSDimitry Andric #include <optional>
215ffd83dbSDimitry Andric 
225ffd83dbSDimitry Andric namespace llvm {
235ffd83dbSDimitry Andric 
245ffd83dbSDimitry Andric /// StringMapEntryBase - Shared base class of StringMapEntry instances.
255ffd83dbSDimitry Andric class StringMapEntryBase {
265ffd83dbSDimitry Andric   size_t keyLength;
275ffd83dbSDimitry Andric 
285ffd83dbSDimitry Andric public:
StringMapEntryBase(size_t keyLength)295ffd83dbSDimitry Andric   explicit StringMapEntryBase(size_t keyLength) : keyLength(keyLength) {}
305ffd83dbSDimitry Andric 
getKeyLength()315ffd83dbSDimitry Andric   size_t getKeyLength() const { return keyLength; }
32fe6060f1SDimitry Andric 
33fe6060f1SDimitry Andric protected:
34fe6060f1SDimitry Andric   /// Helper to tail-allocate \p Key. It'd be nice to generalize this so it
35fe6060f1SDimitry Andric   /// could be reused elsewhere, maybe even taking an llvm::function_ref to
36fe6060f1SDimitry Andric   /// type-erase the allocator and put it in a source file.
37fe6060f1SDimitry Andric   template <typename AllocatorTy>
38fe6060f1SDimitry Andric   static void *allocateWithKey(size_t EntrySize, size_t EntryAlign,
39fe6060f1SDimitry Andric                                StringRef Key, AllocatorTy &Allocator);
405ffd83dbSDimitry Andric };
415ffd83dbSDimitry Andric 
42fe6060f1SDimitry Andric // Define out-of-line to dissuade inlining.
43fe6060f1SDimitry Andric template <typename AllocatorTy>
allocateWithKey(size_t EntrySize,size_t EntryAlign,StringRef Key,AllocatorTy & Allocator)44fe6060f1SDimitry Andric void *StringMapEntryBase::allocateWithKey(size_t EntrySize, size_t EntryAlign,
45fe6060f1SDimitry Andric                                           StringRef Key,
46fe6060f1SDimitry Andric                                           AllocatorTy &Allocator) {
47fe6060f1SDimitry Andric   size_t KeyLength = Key.size();
48fe6060f1SDimitry Andric 
49fe6060f1SDimitry Andric   // Allocate a new item with space for the string at the end and a null
50fe6060f1SDimitry Andric   // terminator.
51fe6060f1SDimitry Andric   size_t AllocSize = EntrySize + KeyLength + 1;
52fe6060f1SDimitry Andric   void *Allocation = Allocator.Allocate(AllocSize, EntryAlign);
53fe6060f1SDimitry Andric   assert(Allocation && "Unhandled out-of-memory");
54fe6060f1SDimitry Andric 
55fe6060f1SDimitry Andric   // Copy the string information.
56fe6060f1SDimitry Andric   char *Buffer = reinterpret_cast<char *>(Allocation) + EntrySize;
57fe6060f1SDimitry Andric   if (KeyLength > 0)
58fe6060f1SDimitry Andric     ::memcpy(Buffer, Key.data(), KeyLength);
59fe6060f1SDimitry Andric   Buffer[KeyLength] = 0; // Null terminate for convenience of clients.
60fe6060f1SDimitry Andric   return Allocation;
61fe6060f1SDimitry Andric }
62fe6060f1SDimitry Andric 
635ffd83dbSDimitry Andric /// StringMapEntryStorage - Holds the value in a StringMapEntry.
645ffd83dbSDimitry Andric ///
655ffd83dbSDimitry Andric /// Factored out into a separate base class to make it easier to specialize.
665ffd83dbSDimitry Andric /// This is primarily intended to support StringSet, which doesn't need a value
675ffd83dbSDimitry Andric /// stored at all.
685ffd83dbSDimitry Andric template <typename ValueTy>
695ffd83dbSDimitry Andric class StringMapEntryStorage : public StringMapEntryBase {
705ffd83dbSDimitry Andric public:
715ffd83dbSDimitry Andric   ValueTy second;
725ffd83dbSDimitry Andric 
StringMapEntryStorage(size_t keyLength)735ffd83dbSDimitry Andric   explicit StringMapEntryStorage(size_t keyLength)
745ffd83dbSDimitry Andric       : StringMapEntryBase(keyLength), second() {}
755ffd83dbSDimitry Andric   template <typename... InitTy>
StringMapEntryStorage(size_t keyLength,InitTy &&...initVals)765ffd83dbSDimitry Andric   StringMapEntryStorage(size_t keyLength, InitTy &&...initVals)
775ffd83dbSDimitry Andric       : StringMapEntryBase(keyLength),
785ffd83dbSDimitry Andric         second(std::forward<InitTy>(initVals)...) {}
795ffd83dbSDimitry Andric   StringMapEntryStorage(StringMapEntryStorage &e) = delete;
805ffd83dbSDimitry Andric 
getValue()815ffd83dbSDimitry Andric   const ValueTy &getValue() const { return second; }
getValue()825ffd83dbSDimitry Andric   ValueTy &getValue() { return second; }
835ffd83dbSDimitry Andric 
setValue(const ValueTy & V)845ffd83dbSDimitry Andric   void setValue(const ValueTy &V) { second = V; }
855ffd83dbSDimitry Andric };
865ffd83dbSDimitry Andric 
87*bdd1243dSDimitry Andric template <>
88*bdd1243dSDimitry Andric class StringMapEntryStorage<std::nullopt_t> : public StringMapEntryBase {
895ffd83dbSDimitry Andric public:
90*bdd1243dSDimitry Andric   explicit StringMapEntryStorage(size_t keyLength,
91*bdd1243dSDimitry Andric                                  std::nullopt_t = std::nullopt)
StringMapEntryBase(keyLength)925ffd83dbSDimitry Andric       : StringMapEntryBase(keyLength) {}
935ffd83dbSDimitry Andric   StringMapEntryStorage(StringMapEntryStorage &entry) = delete;
945ffd83dbSDimitry Andric 
getValue()95*bdd1243dSDimitry Andric   std::nullopt_t getValue() const { return std::nullopt; }
965ffd83dbSDimitry Andric };
975ffd83dbSDimitry Andric 
985ffd83dbSDimitry Andric /// StringMapEntry - This is used to represent one value that is inserted into
995ffd83dbSDimitry Andric /// a StringMap.  It contains the Value itself and the key: the string length
1005ffd83dbSDimitry Andric /// and data.
1015ffd83dbSDimitry Andric template <typename ValueTy>
1025ffd83dbSDimitry Andric class StringMapEntry final : public StringMapEntryStorage<ValueTy> {
1035ffd83dbSDimitry Andric public:
1045ffd83dbSDimitry Andric   using StringMapEntryStorage<ValueTy>::StringMapEntryStorage;
1055ffd83dbSDimitry Andric 
106*bdd1243dSDimitry Andric   using ValueType = ValueTy;
107*bdd1243dSDimitry Andric 
getKey()1085ffd83dbSDimitry Andric   StringRef getKey() const {
1095ffd83dbSDimitry Andric     return StringRef(getKeyData(), this->getKeyLength());
1105ffd83dbSDimitry Andric   }
1115ffd83dbSDimitry Andric 
1125ffd83dbSDimitry Andric   /// getKeyData - Return the start of the string data that is the key for this
1135ffd83dbSDimitry Andric   /// value.  The string data is always stored immediately after the
1145ffd83dbSDimitry Andric   /// StringMapEntry object.
getKeyData()1155ffd83dbSDimitry Andric   const char *getKeyData() const {
1165ffd83dbSDimitry Andric     return reinterpret_cast<const char *>(this + 1);
1175ffd83dbSDimitry Andric   }
1185ffd83dbSDimitry Andric 
first()1195ffd83dbSDimitry Andric   StringRef first() const {
1205ffd83dbSDimitry Andric     return StringRef(getKeyData(), this->getKeyLength());
1215ffd83dbSDimitry Andric   }
1225ffd83dbSDimitry Andric 
1235ffd83dbSDimitry Andric   /// Create a StringMapEntry for the specified key construct the value using
1245ffd83dbSDimitry Andric   /// \p InitiVals.
1255ffd83dbSDimitry Andric   template <typename AllocatorTy, typename... InitTy>
create(StringRef key,AllocatorTy & allocator,InitTy &&...initVals)126*bdd1243dSDimitry Andric   static StringMapEntry *create(StringRef key, AllocatorTy &allocator,
1275ffd83dbSDimitry Andric                                 InitTy &&...initVals) {
128fe6060f1SDimitry Andric     return new (StringMapEntryBase::allocateWithKey(
129fe6060f1SDimitry Andric         sizeof(StringMapEntry), alignof(StringMapEntry), key, allocator))
130fe6060f1SDimitry Andric         StringMapEntry(key.size(), std::forward<InitTy>(initVals)...);
1315ffd83dbSDimitry Andric   }
1325ffd83dbSDimitry Andric 
1335ffd83dbSDimitry Andric   /// GetStringMapEntryFromKeyData - Given key data that is known to be embedded
1345ffd83dbSDimitry Andric   /// into a StringMapEntry, return the StringMapEntry itself.
GetStringMapEntryFromKeyData(const char * keyData)1355ffd83dbSDimitry Andric   static StringMapEntry &GetStringMapEntryFromKeyData(const char *keyData) {
1365ffd83dbSDimitry Andric     char *ptr = const_cast<char *>(keyData) - sizeof(StringMapEntry<ValueTy>);
1375ffd83dbSDimitry Andric     return *reinterpret_cast<StringMapEntry *>(ptr);
1385ffd83dbSDimitry Andric   }
1395ffd83dbSDimitry Andric 
1405ffd83dbSDimitry Andric   /// Destroy - Destroy this StringMapEntry, releasing memory back to the
1415ffd83dbSDimitry Andric   /// specified allocator.
Destroy(AllocatorTy & allocator)1425ffd83dbSDimitry Andric   template <typename AllocatorTy> void Destroy(AllocatorTy &allocator) {
1435ffd83dbSDimitry Andric     // Free memory referenced by the item.
1445ffd83dbSDimitry Andric     size_t AllocSize = sizeof(StringMapEntry) + this->getKeyLength() + 1;
1455ffd83dbSDimitry Andric     this->~StringMapEntry();
1465ffd83dbSDimitry Andric     allocator.Deallocate(static_cast<void *>(this), AllocSize,
1475ffd83dbSDimitry Andric                          alignof(StringMapEntry));
1485ffd83dbSDimitry Andric   }
1495ffd83dbSDimitry Andric };
1505ffd83dbSDimitry Andric 
151*bdd1243dSDimitry Andric // Allow structured bindings on StringMapEntry.
152*bdd1243dSDimitry Andric template <std::size_t Index, typename ValueTy>
decltype(auto)153*bdd1243dSDimitry Andric decltype(auto) get(const StringMapEntry<ValueTy> &E) {
154*bdd1243dSDimitry Andric   static_assert(Index < 2);
155*bdd1243dSDimitry Andric   if constexpr (Index == 0)
156*bdd1243dSDimitry Andric     return E.first();
157*bdd1243dSDimitry Andric   else
158*bdd1243dSDimitry Andric     return E.second;
159*bdd1243dSDimitry Andric }
160*bdd1243dSDimitry Andric 
1615ffd83dbSDimitry Andric } // end namespace llvm
1625ffd83dbSDimitry Andric 
163*bdd1243dSDimitry Andric namespace std {
164*bdd1243dSDimitry Andric template <typename ValueTy>
165*bdd1243dSDimitry Andric struct tuple_size<llvm::StringMapEntry<ValueTy>>
166*bdd1243dSDimitry Andric     : std::integral_constant<std::size_t, 2> {};
167*bdd1243dSDimitry Andric 
168*bdd1243dSDimitry Andric template <std::size_t I, typename ValueTy>
169*bdd1243dSDimitry Andric struct tuple_element<I, llvm::StringMapEntry<ValueTy>>
170*bdd1243dSDimitry Andric     : std::conditional<I == 0, llvm::StringRef, ValueTy> {};
171*bdd1243dSDimitry Andric } // namespace std
172*bdd1243dSDimitry Andric 
1735ffd83dbSDimitry Andric #endif // LLVM_ADT_STRINGMAPENTRY_H
174