NonRelocatableStringpool.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- NonRelocatableStringpool.h -------------------------------*- 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. #ifndef LLVM_CODEGEN_NONRELOCATABLESTRINGPOOL_H
  14. #define LLVM_CODEGEN_NONRELOCATABLESTRINGPOOL_H
  15. #include "llvm/CodeGen/DwarfStringPoolEntry.h"
  16. #include "llvm/Support/Allocator.h"
  17. #include <cstdint>
  18. #include <vector>
  19. namespace llvm {
  20. /// A string table that doesn't need relocations.
  21. ///
  22. /// Use this class when a string table doesn't need relocations.
  23. /// This class provides this ability by just associating offsets with strings.
  24. class NonRelocatableStringpool {
  25. public:
  26. /// Entries are stored into the StringMap and simply linked together through
  27. /// the second element of this pair in order to keep track of insertion
  28. /// order.
  29. using MapTy = StringMap<DwarfStringPoolEntry, BumpPtrAllocator>;
  30. NonRelocatableStringpool(
  31. std::function<StringRef(StringRef Input)> Translator = nullptr,
  32. bool PutEmptyString = false)
  33. : Translator(Translator) {
  34. if (PutEmptyString)
  35. EmptyString = getEntry("");
  36. }
  37. DwarfStringPoolEntryRef getEntry(StringRef S);
  38. /// Get the offset of string \p S in the string table. This can insert a new
  39. /// element or return the offset of a pre-existing one.
  40. uint64_t getStringOffset(StringRef S) { return getEntry(S).getOffset(); }
  41. /// Get permanent storage for \p S (but do not necessarily emit \p S in the
  42. /// output section). A latter call to getStringOffset() with the same string
  43. /// will chain it though.
  44. ///
  45. /// \returns The StringRef that points to permanent storage to use
  46. /// in place of \p S.
  47. StringRef internString(StringRef S);
  48. uint64_t getSize() { return CurrentEndOffset; }
  49. /// Return the list of strings to be emitted. This does not contain the
  50. /// strings which were added via internString only.
  51. std::vector<DwarfStringPoolEntryRef> getEntriesForEmission() const;
  52. private:
  53. MapTy Strings;
  54. uint64_t CurrentEndOffset = 0;
  55. unsigned NumEntries = 0;
  56. DwarfStringPoolEntryRef EmptyString;
  57. std::function<StringRef(StringRef Input)> Translator;
  58. };
  59. /// Helper for making strong types.
  60. template <typename T, typename S> class StrongType : public T {
  61. public:
  62. template <typename... Args>
  63. explicit StrongType(Args... A) : T(std::forward<Args>(A)...) {}
  64. };
  65. /// It's very easy to introduce bugs by passing the wrong string pool.
  66. /// By using strong types the interface enforces that the right
  67. /// kind of pool is used.
  68. struct UniqueTag {};
  69. struct OffsetsTag {};
  70. using UniquingStringPool = StrongType<NonRelocatableStringpool, UniqueTag>;
  71. using OffsetsStringPool = StrongType<NonRelocatableStringpool, OffsetsTag>;
  72. } // end namespace llvm
  73. #endif // LLVM_CODEGEN_NONRELOCATABLESTRINGPOOL_H
  74. #ifdef __GNUC__
  75. #pragma GCC diagnostic pop
  76. #endif