StringMapEntry.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- StringMapEntry.h - String Hash table map interface -------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. ///
  14. /// \file
  15. /// This file defines the StringMapEntry class - it is intended to be a low
  16. /// dependency implementation detail of StringMap that is more suitable for
  17. /// inclusion in public headers than StringMap.h itself is.
  18. ///
  19. //===----------------------------------------------------------------------===//
  20. #ifndef LLVM_ADT_STRINGMAPENTRY_H
  21. #define LLVM_ADT_STRINGMAPENTRY_H
  22. #include "llvm/ADT/None.h"
  23. #include "llvm/ADT/StringRef.h"
  24. #include "llvm/ADT/STLFunctionalExtras.h"
  25. namespace llvm {
  26. /// StringMapEntryBase - Shared base class of StringMapEntry instances.
  27. class StringMapEntryBase {
  28. size_t keyLength;
  29. public:
  30. explicit StringMapEntryBase(size_t keyLength) : keyLength(keyLength) {}
  31. size_t getKeyLength() const { return keyLength; }
  32. protected:
  33. /// Helper to tail-allocate \p Key. It'd be nice to generalize this so it
  34. /// could be reused elsewhere, maybe even taking an llvm::function_ref to
  35. /// type-erase the allocator and put it in a source file.
  36. template <typename AllocatorTy>
  37. static void *allocateWithKey(size_t EntrySize, size_t EntryAlign,
  38. StringRef Key, AllocatorTy &Allocator);
  39. };
  40. // Define out-of-line to dissuade inlining.
  41. template <typename AllocatorTy>
  42. void *StringMapEntryBase::allocateWithKey(size_t EntrySize, size_t EntryAlign,
  43. StringRef Key,
  44. AllocatorTy &Allocator) {
  45. size_t KeyLength = Key.size();
  46. // Allocate a new item with space for the string at the end and a null
  47. // terminator.
  48. size_t AllocSize = EntrySize + KeyLength + 1;
  49. void *Allocation = Allocator.Allocate(AllocSize, EntryAlign);
  50. assert(Allocation && "Unhandled out-of-memory");
  51. // Copy the string information.
  52. char *Buffer = reinterpret_cast<char *>(Allocation) + EntrySize;
  53. if (KeyLength > 0)
  54. ::memcpy(Buffer, Key.data(), KeyLength);
  55. Buffer[KeyLength] = 0; // Null terminate for convenience of clients.
  56. return Allocation;
  57. }
  58. /// StringMapEntryStorage - Holds the value in a StringMapEntry.
  59. ///
  60. /// Factored out into a separate base class to make it easier to specialize.
  61. /// This is primarily intended to support StringSet, which doesn't need a value
  62. /// stored at all.
  63. template <typename ValueTy>
  64. class StringMapEntryStorage : public StringMapEntryBase {
  65. public:
  66. ValueTy second;
  67. explicit StringMapEntryStorage(size_t keyLength)
  68. : StringMapEntryBase(keyLength), second() {}
  69. template <typename... InitTy>
  70. StringMapEntryStorage(size_t keyLength, InitTy &&... initVals)
  71. : StringMapEntryBase(keyLength),
  72. second(std::forward<InitTy>(initVals)...) {}
  73. StringMapEntryStorage(StringMapEntryStorage &e) = delete;
  74. const ValueTy &getValue() const { return second; }
  75. ValueTy &getValue() { return second; }
  76. void setValue(const ValueTy &V) { second = V; }
  77. };
  78. template <> class StringMapEntryStorage<NoneType> : public StringMapEntryBase {
  79. public:
  80. explicit StringMapEntryStorage(size_t keyLength, NoneType = None)
  81. : StringMapEntryBase(keyLength) {}
  82. StringMapEntryStorage(StringMapEntryStorage &entry) = delete;
  83. NoneType getValue() const { return None; }
  84. };
  85. /// StringMapEntry - This is used to represent one value that is inserted into
  86. /// a StringMap. It contains the Value itself and the key: the string length
  87. /// and data.
  88. template <typename ValueTy>
  89. class StringMapEntry final : public StringMapEntryStorage<ValueTy> {
  90. public:
  91. using StringMapEntryStorage<ValueTy>::StringMapEntryStorage;
  92. StringRef getKey() const {
  93. return StringRef(getKeyData(), this->getKeyLength());
  94. }
  95. /// getKeyData - Return the start of the string data that is the key for this
  96. /// value. The string data is always stored immediately after the
  97. /// StringMapEntry object.
  98. const char *getKeyData() const {
  99. return reinterpret_cast<const char *>(this + 1);
  100. }
  101. StringRef first() const {
  102. return StringRef(getKeyData(), this->getKeyLength());
  103. }
  104. /// Create a StringMapEntry for the specified key construct the value using
  105. /// \p InitiVals.
  106. template <typename AllocatorTy, typename... InitTy>
  107. static StringMapEntry *Create(StringRef key, AllocatorTy &allocator,
  108. InitTy &&... initVals) {
  109. return new (StringMapEntryBase::allocateWithKey(
  110. sizeof(StringMapEntry), alignof(StringMapEntry), key, allocator))
  111. StringMapEntry(key.size(), std::forward<InitTy>(initVals)...);
  112. }
  113. /// GetStringMapEntryFromKeyData - Given key data that is known to be embedded
  114. /// into a StringMapEntry, return the StringMapEntry itself.
  115. static StringMapEntry &GetStringMapEntryFromKeyData(const char *keyData) {
  116. char *ptr = const_cast<char *>(keyData) - sizeof(StringMapEntry<ValueTy>);
  117. return *reinterpret_cast<StringMapEntry *>(ptr);
  118. }
  119. /// Destroy - Destroy this StringMapEntry, releasing memory back to the
  120. /// specified allocator.
  121. template <typename AllocatorTy> void Destroy(AllocatorTy &allocator) {
  122. // Free memory referenced by the item.
  123. size_t AllocSize = sizeof(StringMapEntry) + this->getKeyLength() + 1;
  124. this->~StringMapEntry();
  125. allocator.Deallocate(static_cast<void *>(this), AllocSize,
  126. alignof(StringMapEntry));
  127. }
  128. };
  129. } // end namespace llvm
  130. #endif // LLVM_ADT_STRINGMAPENTRY_H
  131. #ifdef __GNUC__
  132. #pragma GCC diagnostic pop
  133. #endif