LLVM  4.0.0
StringTableBuilder.h
Go to the documentation of this file.
1 //===-- StringTableBuilder.h - String table building utility ------*- C++ -*-=//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_MC_STRINGTABLEBUILDER_H
11 #define LLVM_MC_STRINGTABLEBUILDER_H
12 
14 #include "llvm/ADT/DenseMap.h"
15 #include <cassert>
16 
17 namespace llvm {
18 class raw_ostream;
19 
20 /// \brief Utility for building string tables with deduplicated suffixes.
22 public:
23  enum Kind { ELF, WinCOFF, MachO, RAW };
24 
25 private:
27  size_t Size = 0;
28  Kind K;
29  unsigned Alignment;
30  bool Finalized = false;
31 
32  void finalizeStringTable(bool Optimize);
33  void initSize();
34 
35 public:
36  StringTableBuilder(Kind K, unsigned Alignment = 1);
38 
39  /// \brief Add a string to the builder. Returns the position of S in the
40  /// table. The position will be changed if finalize is used.
41  /// Can only be used before the table is finalized.
42  size_t add(CachedHashStringRef S);
43  size_t add(StringRef S) { return add(CachedHashStringRef(S)); }
44 
45  /// \brief Analyze the strings and build the final table. No more strings can
46  /// be added after this point.
47  void finalize();
48 
49  /// Finalize the string table without reording it. In this mode, offsets
50  /// returned by add will still be valid.
51  void finalizeInOrder();
52 
53  /// \brief Get the offest of a string in the string table. Can only be used
54  /// after the table is finalized.
55  size_t getOffset(CachedHashStringRef S) const;
56  size_t getOffset(StringRef S) const {
57  return getOffset(CachedHashStringRef(S));
58  }
59 
60  size_t getSize() const { return Size; }
61  void clear();
62 
63  void write(raw_ostream &OS) const;
64  void write(uint8_t *Buf) const;
65 
66 private:
67  bool isFinalized() const { return Finalized; }
68 };
69 
70 } // end llvm namespace
71 
72 #endif
A container which contains a StringRef plus a precomputed hash.
Utility for building string tables with deduplicated suffixes.
size_t add(CachedHashStringRef S)
Add a string to the builder.
size_t getOffset(StringRef S) const
void finalizeInOrder()
Finalize the string table without reording it.
void finalize()
Analyze the strings and build the final table.
void write(raw_ostream &OS) const
size_t getOffset(CachedHashStringRef S) const
Get the offest of a string in the string table.
StringTableBuilder(Kind K, unsigned Alignment=1)
size_t add(StringRef S)
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47