StringMapEntry.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. // This file defines the StringMapEntry class - it is intended to be a low
  15. // dependency implementation detail of StringMap that is more suitable for
  16. // inclusion in public headers than StringMap.h itself is.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_ADT_STRINGMAPENTRY_H
  20. #define LLVM_ADT_STRINGMAPENTRY_H
  21. #include "llvm/ADT/StringRef.h"
  22. namespace llvm {
  23. /// StringMapEntryBase - Shared base class of StringMapEntry instances.
  24. class StringMapEntryBase {
  25. size_t keyLength;
  26. public:
  27. explicit StringMapEntryBase(size_t keyLength) : keyLength(keyLength) {}
  28. size_t getKeyLength() const { return keyLength; }
  29. };
  30. /// StringMapEntryStorage - Holds the value in a StringMapEntry.
  31. ///
  32. /// Factored out into a separate base class to make it easier to specialize.
  33. /// This is primarily intended to support StringSet, which doesn't need a value
  34. /// stored at all.
  35. template <typename ValueTy>
  36. class StringMapEntryStorage : public StringMapEntryBase {
  37. public:
  38. ValueTy second;
  39. explicit StringMapEntryStorage(size_t keyLength)
  40. : StringMapEntryBase(keyLength), second() {}
  41. template <typename... InitTy>
  42. StringMapEntryStorage(size_t keyLength, InitTy &&... initVals)
  43. : StringMapEntryBase(keyLength),
  44. second(std::forward<InitTy>(initVals)...) {}
  45. StringMapEntryStorage(StringMapEntryStorage &e) = delete;
  46. const ValueTy &getValue() const { return second; }
  47. ValueTy &getValue() { return second; }
  48. void setValue(const ValueTy &V) { second = V; }
  49. };
  50. template <> class StringMapEntryStorage<NoneType> : public StringMapEntryBase {
  51. public:
  52. explicit StringMapEntryStorage(size_t keyLength, NoneType none = None)
  53. : StringMapEntryBase(keyLength) {}
  54. StringMapEntryStorage(StringMapEntryStorage &entry) = delete;
  55. NoneType getValue() const { return None; }
  56. };
  57. /// StringMapEntry - This is used to represent one value that is inserted into
  58. /// a StringMap. It contains the Value itself and the key: the string length
  59. /// and data.
  60. template <typename ValueTy>
  61. class StringMapEntry final : public StringMapEntryStorage<ValueTy> {
  62. public:
  63. using StringMapEntryStorage<ValueTy>::StringMapEntryStorage;
  64. StringRef getKey() const {
  65. return StringRef(getKeyData(), this->getKeyLength());
  66. }
  67. /// getKeyData - Return the start of the string data that is the key for this
  68. /// value. The string data is always stored immediately after the
  69. /// StringMapEntry object.
  70. const char *getKeyData() const {
  71. return reinterpret_cast<const char *>(this + 1);
  72. }
  73. StringRef first() const {
  74. return StringRef(getKeyData(), this->getKeyLength());
  75. }
  76. /// Create a StringMapEntry for the specified key construct the value using
  77. /// \p InitiVals.
  78. template <typename AllocatorTy, typename... InitTy>
  79. static StringMapEntry *Create(StringRef key, AllocatorTy &allocator,
  80. InitTy &&... initVals) {
  81. size_t keyLength = key.size();
  82. // Allocate a new item with space for the string at the end and a null
  83. // terminator.
  84. size_t allocSize = sizeof(StringMapEntry) + keyLength + 1;
  85. size_t alignment = alignof(StringMapEntry);
  86. StringMapEntry *newItem =
  87. static_cast<StringMapEntry *>(allocator.Allocate(allocSize, alignment));
  88. assert(newItem && "Unhandled out-of-memory");
  89. // Construct the value.
  90. new (newItem) StringMapEntry(keyLength, std::forward<InitTy>(initVals)...);
  91. // Copy the string information.
  92. char *strBuffer = const_cast<char *>(newItem->getKeyData());
  93. if (keyLength > 0)
  94. memcpy(strBuffer, key.data(), keyLength);
  95. strBuffer[keyLength] = 0; // Null terminate for convenience of clients.
  96. return newItem;
  97. }
  98. /// GetStringMapEntryFromKeyData - Given key data that is known to be embedded
  99. /// into a StringMapEntry, return the StringMapEntry itself.
  100. static StringMapEntry &GetStringMapEntryFromKeyData(const char *keyData) {
  101. char *ptr = const_cast<char *>(keyData) - sizeof(StringMapEntry<ValueTy>);
  102. return *reinterpret_cast<StringMapEntry *>(ptr);
  103. }
  104. /// Destroy - Destroy this StringMapEntry, releasing memory back to the
  105. /// specified allocator.
  106. template <typename AllocatorTy> void Destroy(AllocatorTy &allocator) {
  107. // Free memory referenced by the item.
  108. size_t AllocSize = sizeof(StringMapEntry) + this->getKeyLength() + 1;
  109. this->~StringMapEntry();
  110. allocator.Deallocate(static_cast<void *>(this), AllocSize,
  111. alignof(StringMapEntry));
  112. }
  113. };
  114. } // end namespace llvm
  115. #endif // LLVM_ADT_STRINGMAPENTRY_H
  116. #ifdef __GNUC__
  117. #pragma GCC diagnostic pop
  118. #endif