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/SmallString.h"
14 #include "llvm/ADT/DenseMap.h"
19 /// \brief Utility for building string tables with deduplicated suffixes.
20 class StringTableBuilder {
22 enum Kind { ELF, WinCOFF, MachO, RAW };
25 SmallString<256> StringTable;
26 DenseMap<StringRef, size_t> StringIndexMap;
31 StringTableBuilder(Kind K);
33 /// \brief Add a string to the builder. Returns the position of S in the
34 /// table. The position will be changed if finalize is used.
35 /// Can only be used before the table is finalized.
36 size_t add(StringRef S);
38 /// \brief Analyze the strings and build the final table. No more strings can
39 /// be added after this point.
42 /// \brief Retrieve the string table data. Can only be used after the table
44 StringRef data() const {
45 assert(isFinalized());
49 /// \brief Get the offest of a string in the string table. Can only be used
50 /// after the table is finalized.
51 size_t getOffset(StringRef S) const;
53 const DenseMap<StringRef, size_t> &getMap() const { return StringIndexMap; }
54 size_t getSize() const { return Size; }
58 bool isFinalized() const {
59 return !StringTable.empty();
63 } // end llvm namespace