1 //===- StringTableBuilder.h - String table building utility -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_MC_STRINGTABLEBUILDER_H
11 #define LLVM_MC_STRINGTABLEBUILDER_H
13 #include "llvm/ADT/CachedHashString.h"
14 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/ADT/StringRef.h"
23 /// \brief Utility for building string tables with deduplicated suffixes.
24 class StringTableBuilder {
26 enum Kind { ELF, WinCOFF, MachO, RAW };
29 DenseMap<CachedHashStringRef, size_t> StringIndexMap;
33 bool Finalized = false;
35 void finalizeStringTable(bool Optimize);
39 StringTableBuilder(Kind K, unsigned Alignment = 1);
40 ~StringTableBuilder();
42 /// \brief Add a string to the builder. Returns the position of S in the
43 /// table. The position will be changed if finalize is used.
44 /// Can only be used before the table is finalized.
45 size_t add(CachedHashStringRef S);
46 size_t add(StringRef S) { return add(CachedHashStringRef(S)); }
48 /// \brief Analyze the strings and build the final table. No more strings can
49 /// be added after this point.
52 /// Finalize the string table without reording it. In this mode, offsets
53 /// returned by add will still be valid.
54 void finalizeInOrder();
56 /// \brief Get the offest of a string in the string table. Can only be used
57 /// after the table is finalized.
58 size_t getOffset(CachedHashStringRef S) const;
59 size_t getOffset(StringRef S) const {
60 return getOffset(CachedHashStringRef(S));
63 size_t getSize() const { return Size; }
66 void write(raw_ostream &OS) const;
67 void write(uint8_t *Buf) const;
70 bool isFinalized() const { return Finalized; }
73 } // end namespace llvm
75 #endif // LLVM_MC_STRINGTABLEBUILDER_H