LLVM 22.0.0git
StringToOffsetTable.h
Go to the documentation of this file.
1//===- StringToOffsetTable.h - Emit a big concatenated string ---*- 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//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_TABLEGEN_STRINGTOOFFSETTABLE_H
10#define LLVM_TABLEGEN_STRINGTOOFFSETTABLE_H
11
14#include "llvm/ADT/StringMap.h"
15#include <optional>
16
17namespace llvm {
18
19/// StringToOffsetTable - This class uniques a bunch of nul-terminated strings
20/// and keeps track of their offset in a massive contiguous string allocation.
21/// It can then output this string blob and use indexes into the string to
22/// reference each piece.
24 StringMap<unsigned> StringOffset;
25 std::string AggregateString;
26
27 /// If this is to be a static class member, the prefix to use (i.e. class name
28 /// plus ::)
29 const StringRef ClassPrefix;
30 const bool AppendZero;
31 const bool UsePrefixForStorageMember;
32
33public:
34 StringToOffsetTable(bool AppendZero = true, StringRef ClassPrefix = "",
35 bool UsePrefixForStorageMember = true)
36 : ClassPrefix(ClassPrefix), AppendZero(AppendZero),
37 UsePrefixForStorageMember(UsePrefixForStorageMember) {
38 // Ensure we always put the empty string at offset zero. That lets empty
39 // initialization also be zero initialization for offsets into the table.
41 }
42
43 bool empty() const { return StringOffset.empty(); }
44 size_t size() const { return AggregateString.size(); }
45
46 unsigned GetOrAddStringOffset(StringRef Str);
47
48 // Returns the offset of `Str` in the table if its preset, else return
49 // std::nullopt.
50 std::optional<unsigned> GetStringOffset(StringRef Str) const {
51 auto II = StringOffset.find(Str);
52 if (II == StringOffset.end())
53 return std::nullopt;
54 return II->second;
55 }
56
57 // Emit a string table definition with the provided name.
58 //
59 // When possible, this uses string-literal concatenation to emit the string
60 // contents in a readable and searchable way. However, for (very) large string
61 // tables MSVC cannot reliably use string literals and so there we use a large
62 // character array. We still use a line oriented emission and add comments to
63 // provide searchability even in this case.
64 //
65 // The string table, and its input string contents, are always emitted as both
66 // `static` and `constexpr`. Both `Name` and (`Name` + "Storage") must be
67 // valid identifiers to declare.
68 void EmitStringTableDef(raw_ostream &OS, const Twine &Name) const;
69
70 // Emit the string as one single string.
71 void EmitString(raw_ostream &O) const;
72};
73
74} // end namespace llvm
75
76#endif
This file defines the StringMap class.
uint64_t IntrinsicInst * II
This file defines the SmallString class.
This file contains some functions that are useful when dealing with strings.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition StringMap.h:133
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
unsigned GetOrAddStringOffset(StringRef Str)
void EmitStringTableDef(raw_ostream &OS, const Twine &Name) const
void EmitString(raw_ostream &O) const
StringToOffsetTable(bool AppendZero=true, StringRef ClassPrefix="", bool UsePrefixForStorageMember=true)
std::optional< unsigned > GetStringOffset(StringRef Str) const
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.