[InstCombine] Signed saturation tests. NFC
[llvm-core.git] / tools / dsymutil / NonRelocatableStringpool.h
blobef73242d14e2125191a828b03634c0543fa82e3d
1 //===- NonRelocatableStringpool.h - A simple stringpool --------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_TOOLS_DSYMUTIL_NONRELOCATABLESTRINGPOOL_H
10 #define LLVM_TOOLS_DSYMUTIL_NONRELOCATABLESTRINGPOOL_H
12 #include "SymbolMap.h"
14 #include "llvm/ADT/StringMap.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/CodeGen/DwarfStringPoolEntry.h"
17 #include "llvm/Support/Allocator.h"
18 #include <cstdint>
19 #include <vector>
21 namespace llvm {
22 namespace dsymutil {
24 /// A string table that doesn't need relocations.
25 ///
26 /// We are doing a final link, no need for a string table that has relocation
27 /// entries for every reference to it. This class provides this ability by just
28 /// associating offsets with strings.
29 class NonRelocatableStringpool {
30 public:
31 /// Entries are stored into the StringMap and simply linked together through
32 /// the second element of this pair in order to keep track of insertion
33 /// order.
34 using MapTy = StringMap<DwarfStringPoolEntry, BumpPtrAllocator>;
36 NonRelocatableStringpool(
37 SymbolMapTranslator Translator = SymbolMapTranslator())
38 : Translator(Translator) {
39 // Legacy dsymutil puts an empty string at the start of the line table.
40 EmptyString = getEntry("");
43 DwarfStringPoolEntryRef getEntry(StringRef S);
45 /// Get the offset of string \p S in the string table. This can insert a new
46 /// element or return the offset of a pre-existing one.
47 uint32_t getStringOffset(StringRef S) { return getEntry(S).getOffset(); }
49 /// Get permanent storage for \p S (but do not necessarily emit \p S in the
50 /// output section). A latter call to getStringOffset() with the same string
51 /// will chain it though.
52 ///
53 /// \returns The StringRef that points to permanent storage to use
54 /// in place of \p S.
55 StringRef internString(StringRef S);
57 uint64_t getSize() { return CurrentEndOffset; }
59 /// Return the list of strings to be emitted. This does not contain the
60 /// strings which were added via internString only.
61 std::vector<DwarfStringPoolEntryRef> getEntriesForEmission() const;
63 private:
64 MapTy Strings;
65 uint32_t CurrentEndOffset = 0;
66 unsigned NumEntries = 0;
67 DwarfStringPoolEntryRef EmptyString;
68 SymbolMapTranslator Translator;
71 /// Helper for making strong types.
72 template <typename T, typename S> class StrongType : public T {
73 public:
74 template <typename... Args>
75 explicit StrongType(Args... A) : T(std::forward<Args>(A)...) {}
78 /// It's very easy to introduce bugs by passing the wrong string pool in the
79 /// dwarf linker. By using strong types the interface enforces that the right
80 /// kind of pool is used.
81 struct UniqueTag {};
82 struct OffsetsTag {};
83 using UniquingStringPool = StrongType<NonRelocatableStringpool, UniqueTag>;
84 using OffsetsStringPool = StrongType<NonRelocatableStringpool, OffsetsTag>;
86 } // end namespace dsymutil
87 } // end namespace llvm
89 #endif // LLVM_TOOLS_DSYMUTIL_NONRELOCATABLESTRINGPOOL_H