StringTableBuilder.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- StringTableBuilder.h - String table building utility -----*- 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_MC_STRINGTABLEBUILDER_H
  14. #define LLVM_MC_STRINGTABLEBUILDER_H
  15. #include "llvm/ADT/CachedHashString.h"
  16. #include "llvm/ADT/DenseMap.h"
  17. #include "llvm/ADT/StringRef.h"
  18. #include <cstddef>
  19. #include <cstdint>
  20. namespace llvm {
  21. class raw_ostream;
  22. /// Utility for building string tables with deduplicated suffixes.
  23. class StringTableBuilder {
  24. public:
  25. enum Kind {
  26. ELF,
  27. WinCOFF,
  28. MachO,
  29. MachO64,
  30. MachOLinked,
  31. MachO64Linked,
  32. RAW,
  33. DWARF,
  34. XCOFF
  35. };
  36. private:
  37. DenseMap<CachedHashStringRef, size_t> StringIndexMap;
  38. size_t Size = 0;
  39. Kind K;
  40. unsigned Alignment;
  41. bool Finalized = false;
  42. void finalizeStringTable(bool Optimize);
  43. void initSize();
  44. public:
  45. StringTableBuilder(Kind K, unsigned Alignment = 1);
  46. ~StringTableBuilder();
  47. /// Add a string to the builder. Returns the position of S in the
  48. /// table. The position will be changed if finalize is used.
  49. /// Can only be used before the table is finalized.
  50. size_t add(CachedHashStringRef S);
  51. size_t add(StringRef S) { return add(CachedHashStringRef(S)); }
  52. /// Analyze the strings and build the final table. No more strings can
  53. /// be added after this point.
  54. void finalize();
  55. /// Finalize the string table without reording it. In this mode, offsets
  56. /// returned by add will still be valid.
  57. void finalizeInOrder();
  58. /// Get the offest of a string in the string table. Can only be used
  59. /// after the table is finalized.
  60. size_t getOffset(CachedHashStringRef S) const;
  61. size_t getOffset(StringRef S) const {
  62. return getOffset(CachedHashStringRef(S));
  63. }
  64. /// Check if a string is contained in the string table. Since this class
  65. /// doesn't store the string values, this function can be used to check if
  66. /// storage needs to be done prior to adding the string.
  67. bool contains(StringRef S) const {
  68. return contains(CachedHashStringRef(S));
  69. }
  70. bool contains(CachedHashStringRef S) const {
  71. return StringIndexMap.count(S);
  72. }
  73. size_t getSize() const { return Size; }
  74. void clear();
  75. void write(raw_ostream &OS) const;
  76. void write(uint8_t *Buf) const;
  77. private:
  78. bool isFinalized() const { return Finalized; }
  79. };
  80. } // end namespace llvm
  81. #endif // LLVM_MC_STRINGTABLEBUILDER_H
  82. #ifdef __GNUC__
  83. #pragma GCC diagnostic pop
  84. #endif