Files
gps/tools/local/circt/include/llvm/TableGen/StringToOffsetTable.h
T
2026-07-12 11:01:51 +03:00

77 lines
2.8 KiB
C++

//===- StringToOffsetTable.h - Emit a big concatenated string ---*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_TABLEGEN_STRINGTOOFFSETTABLE_H
#define LLVM_TABLEGEN_STRINGTOOFFSETTABLE_H
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringMap.h"
#include <optional>
namespace llvm {
/// StringToOffsetTable - This class uniques a bunch of nul-terminated strings
/// and keeps track of their offset in a massive contiguous string allocation.
/// It can then output this string blob and use indexes into the string to
/// reference each piece.
class StringToOffsetTable {
StringMap<unsigned> StringOffset;
std::string AggregateString;
/// If this is to be a static class member, the prefix to use (i.e. class name
/// plus ::)
const StringRef ClassPrefix;
const bool AppendZero;
const bool UsePrefixForStorageMember;
public:
StringToOffsetTable(bool AppendZero = true, StringRef ClassPrefix = "",
bool UsePrefixForStorageMember = true)
: ClassPrefix(ClassPrefix), AppendZero(AppendZero),
UsePrefixForStorageMember(UsePrefixForStorageMember) {
// Ensure we always put the empty string at offset zero. That lets empty
// initialization also be zero initialization for offsets into the table.
GetOrAddStringOffset("");
}
bool empty() const { return StringOffset.empty(); }
size_t size() const { return AggregateString.size(); }
LLVM_ABI unsigned GetOrAddStringOffset(StringRef Str);
// Returns the offset of `Str` in the table if its preset, else return
// std::nullopt.
std::optional<unsigned> GetStringOffset(StringRef Str) const {
auto II = StringOffset.find(Str);
if (II == StringOffset.end())
return std::nullopt;
return II->second;
}
// Emit a string table definition with the provided name.
//
// When possible, this uses string-literal concatenation to emit the string
// contents in a readable and searchable way. However, for (very) large string
// tables MSVC cannot reliably use string literals and so there we use a large
// character array. We still use a line oriented emission and add comments to
// provide searchability even in this case.
//
// The string table, and its input string contents, are always emitted as both
// `static` and `constexpr`. Both `Name` and (`Name` + "Storage") must be
// valid identifiers to declare.
LLVM_ABI void EmitStringTableDef(raw_ostream &OS, const Twine &Name) const;
// Emit the string as one single string.
LLVM_ABI void EmitString(raw_ostream &O) const;
};
} // end namespace llvm
#endif