1 //===- NonRelocatableStringpool.h -------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_CODEGEN_NONRELOCATABLESTRINGPOOL_H
10 #define LLVM_CODEGEN_NONRELOCATABLESTRINGPOOL_H
12 #include "llvm/CodeGen/DwarfStringPoolEntry.h"
13 #include "llvm/Support/Allocator.h"
19 /// A string table that doesn't need relocations.
21 /// Use this class when a string table doesn't need relocations.
22 /// This class provides this ability by just associating offsets with strings.
23 class NonRelocatableStringpool {
25 /// Entries are stored into the StringMap and simply linked together through
26 /// the second element of this pair in order to keep track of insertion
28 using MapTy = StringMap<DwarfStringPoolEntry, BumpPtrAllocator>;
30 NonRelocatableStringpool(
31 std::function<StringRef(StringRef Input)> Translator = nullptr,
32 bool PutEmptyString = false)
33 : Translator(Translator) {
35 EmptyString = getEntry("");
38 DwarfStringPoolEntryRef getEntry(StringRef S);
40 /// Get the offset of string \p S in the string table. This can insert a new
41 /// element or return the offset of a pre-existing one.
42 uint32_t getStringOffset(StringRef S) { return getEntry(S).getOffset(); }
44 /// Get permanent storage for \p S (but do not necessarily emit \p S in the
45 /// output section). A latter call to getStringOffset() with the same string
46 /// will chain it though.
48 /// \returns The StringRef that points to permanent storage to use
50 StringRef internString(StringRef S);
52 uint64_t getSize() { return CurrentEndOffset; }
54 /// Return the list of strings to be emitted. This does not contain the
55 /// strings which were added via internString only.
56 std::vector<DwarfStringPoolEntryRef> getEntriesForEmission() const;
60 uint32_t CurrentEndOffset = 0;
61 unsigned NumEntries = 0;
62 DwarfStringPoolEntryRef EmptyString;
63 std::function<StringRef(StringRef Input)> Translator;
66 /// Helper for making strong types.
67 template <typename T, typename S> class StrongType : public T {
69 template <typename... Args>
70 explicit StrongType(Args... A) : T(std::forward<Args>(A)...) {}
73 /// It's very easy to introduce bugs by passing the wrong string pool.
74 /// By using strong types the interface enforces that the right
75 /// kind of pool is used.
78 using UniquingStringPool = StrongType<NonRelocatableStringpool, UniqueTag>;
79 using OffsetsStringPool = StrongType<NonRelocatableStringpool, OffsetsTag>;
81 } // end namespace llvm
83 #endif // LLVM_CODEGEN_NONRELOCATABLESTRINGPOOL_H