LLVM 20.0.0git
StringTable.h
Go to the documentation of this file.
1//===- StringTable.h - Table of strings tracked by offset ----------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_ADT_STRING_TABLE_H
10#define LLVM_ADT_STRING_TABLE_H
11
12#include "llvm/ADT/StringRef.h"
13#include <limits>
14
15namespace llvm {
16
17/// A table of densely packed, null-terminated strings indexed by offset.
18///
19/// This table abstracts a densely concatenated list of null-terminated strings,
20/// each of which can be referenced using an offset into the table.
21///
22/// This requires and ensures that the string at offset 0 is also the empty
23/// string. This helps allow zero-initialized offsets form empty strings and
24/// avoids non-zero initialization when using a string literal pointer would
25/// allow a null pointer.
26///
27/// The primary use case is having a single global string literal for the table
28/// contents, and offsets into it in other global data structures to avoid
29/// dynamic relocations of individual string literal pointers in those global
30/// data structures.
32 StringRef Table;
33
34public:
35 // An offset into one of these packed string tables, used to select a string
36 // within the table.
37 //
38 // Typically these are created by TableGen or other code generator from
39 // computed offsets, and it just wraps that integer into a type until it is
40 // used with the relevant table.
41 //
42 // We also ensure that the empty string is at offset zero and default
43 // constructing this class gives you an offset of zero. This makes default
44 // constructing this type work similarly (after indexing the table) to default
45 // constructing a `StringRef`.
46 class Offset {
47 // Note that we ensure the empty string is at offset zero.
48 unsigned Value = 0;
49
50 public:
51 constexpr Offset() = default;
52 constexpr Offset(unsigned Value) : Value(Value) {}
53
54 constexpr unsigned value() const { return Value; }
55 };
56
57 // We directly handle string literals with a templated converting constructor
58 // because we *don't* want to do `strlen` on them -- we fully expect null
59 // bytes in this input. This is somewhat the opposite of how `StringLiteral`
60 // works.
61 template <size_t N>
62 constexpr StringTable(const char (&RawTable)[N]) : Table(RawTable, N) {
63 static_assert(N <= std::numeric_limits<unsigned>::max(),
64 "We only support table sizes that can be indexed by an "
65 "`unsigned` offset.");
66
67 // Note that we can only use `empty`, `data`, and `size` in these asserts to
68 // support `constexpr`.
69 assert(!Table.empty() && "Requires at least a valid empty string.");
70 assert(Table.data()[0] == '\0' && "Offset zero must be the empty string.");
71 // Ensure that `strlen` from any offset cannot overflow the end of the table
72 // by insisting on a null byte at the end.
73 assert(Table.data()[Table.size() - 1] == '\0' &&
74 "Last byte must be a null byte.");
75 }
76
77 // Get a string from the table starting with the provided offset. The returned
78 // `StringRef` is in fact null terminated, and so can be converted safely to a
79 // C-string if necessary for a system API.
80 constexpr StringRef operator[](Offset O) const {
81 assert(O.value() < Table.size() && "Out of bounds offset!");
82 return Table.data() + O.value();
83 }
84
85 /// Returns the byte size of the table.
86 constexpr size_t size() const { return Table.size(); }
87};
88
89} // namespace llvm
90
91#endif // LLVM_ADT_STRING_TABLE_H
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:147
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:150
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:144
constexpr Offset(unsigned Value)
Definition: StringTable.h:52
constexpr unsigned value() const
Definition: StringTable.h:54
constexpr Offset()=default
A table of densely packed, null-terminated strings indexed by offset.
Definition: StringTable.h:31
constexpr size_t size() const
Returns the byte size of the table.
Definition: StringTable.h:86
constexpr StringRef operator[](Offset O) const
Definition: StringTable.h:80
constexpr StringTable(const char(&RawTable)[N])
Definition: StringTable.h:62
LLVM Value Representation.
Definition: Value.h:74
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
#define N