Make more SymbolTable methods string_view.
This CL updates more of the SymbolTable to use string views. Change-Id: I0d9c7817ba4e3b0d0e73cfc02406481eb796c3c2 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/127320 Kokoro: Kokoro <noreply+kokoro@google.com> Commit-Queue: Dan Sinclair <dsinclair@chromium.org> Reviewed-by: Ben Clayton <bclayton@google.com>
This commit is contained in:
parent
0559005494
commit
e16ed7ccc2
|
@ -52,6 +52,10 @@ std::string Symbol::to_str() const {
|
|||
return "$" + std::to_string(val_);
|
||||
}
|
||||
|
||||
std::string_view Symbol::NameView() const {
|
||||
return name_;
|
||||
}
|
||||
|
||||
std::string Symbol::Name() const {
|
||||
return std::string(name_);
|
||||
}
|
||||
|
|
|
@ -76,6 +76,10 @@ class Symbol {
|
|||
/// @return the string representation of the symbol
|
||||
std::string to_str() const;
|
||||
|
||||
/// Converts the symbol to the registered name
|
||||
/// @returns the string_view representing the name of the symbol
|
||||
std::string_view NameView() const;
|
||||
|
||||
/// Converts the symbol to the registered name
|
||||
/// @returns the string representing the name of the symbol
|
||||
std::string Name() const;
|
||||
|
|
|
@ -26,14 +26,17 @@ SymbolTable::~SymbolTable() = default;
|
|||
|
||||
SymbolTable& SymbolTable::operator=(SymbolTable&&) = default;
|
||||
|
||||
Symbol SymbolTable::Register(const std::string& name) {
|
||||
Symbol SymbolTable::Register(std::string_view name) {
|
||||
TINT_ASSERT(Symbol, !name.empty());
|
||||
|
||||
auto it = name_to_symbol_.Find(name);
|
||||
if (it) {
|
||||
return *it;
|
||||
}
|
||||
return RegisterInternal(name);
|
||||
}
|
||||
|
||||
Symbol SymbolTable::RegisterInternal(std::string_view name) {
|
||||
char* name_mem = name_allocator_.Allocate(name.length() + 1);
|
||||
if (name_mem == nullptr) {
|
||||
return Symbol();
|
||||
|
@ -43,26 +46,28 @@ Symbol SymbolTable::Register(const std::string& name) {
|
|||
std::string_view nv(name_mem, name.length());
|
||||
|
||||
Symbol sym(next_symbol_, program_id_, nv);
|
||||
|
||||
++next_symbol_;
|
||||
|
||||
name_to_symbol_.Add(name_mem, sym);
|
||||
name_to_symbol_.Add(sym.NameView(), sym);
|
||||
|
||||
return sym;
|
||||
}
|
||||
|
||||
Symbol SymbolTable::Get(const std::string& name) const {
|
||||
Symbol SymbolTable::Get(std::string_view name) const {
|
||||
auto it = name_to_symbol_.Find(name);
|
||||
return it ? *it : Symbol();
|
||||
}
|
||||
|
||||
Symbol SymbolTable::New(std::string prefix /* = "" */) {
|
||||
if (prefix.empty()) {
|
||||
Symbol SymbolTable::New(std::string_view prefix_view /* = "" */) {
|
||||
std::string prefix;
|
||||
if (prefix_view.empty()) {
|
||||
prefix = "tint_symbol";
|
||||
} else {
|
||||
prefix = std::string(prefix_view);
|
||||
}
|
||||
|
||||
auto it = name_to_symbol_.Find(prefix);
|
||||
if (!it) {
|
||||
return Register(prefix);
|
||||
return RegisterInternal(prefix);
|
||||
}
|
||||
|
||||
size_t i = 0;
|
||||
|
@ -77,13 +82,13 @@ Symbol SymbolTable::New(std::string prefix /* = "" */) {
|
|||
name = prefix + "_" + std::to_string(i);
|
||||
} while (name_to_symbol_.Contains(name));
|
||||
|
||||
auto sym = RegisterInternal(name);
|
||||
if (last_prefix) {
|
||||
*last_prefix = i;
|
||||
} else {
|
||||
last_prefix_to_index_.Add(prefix, i);
|
||||
}
|
||||
|
||||
return Register(name);
|
||||
return sym;
|
||||
}
|
||||
|
||||
} // namespace tint
|
||||
|
|
|
@ -56,12 +56,12 @@ class SymbolTable {
|
|||
/// Registers a name into the symbol table, returning the Symbol.
|
||||
/// @param name the name to register
|
||||
/// @returns the symbol representing the given name
|
||||
Symbol Register(const std::string& name);
|
||||
Symbol Register(std::string_view name);
|
||||
|
||||
/// Returns the symbol for the given `name`
|
||||
/// @param name the name to lookup
|
||||
/// @returns the symbol for the name or Symbol() if not found.
|
||||
Symbol Get(const std::string& name) const;
|
||||
Symbol Get(std::string_view name) const;
|
||||
|
||||
/// Returns a new unique symbol with the given name, possibly suffixed with a
|
||||
/// unique number.
|
||||
|
@ -69,7 +69,7 @@ class SymbolTable {
|
|||
/// @returns a new, unnamed symbol with the given name. If the name is already
|
||||
/// taken then this will be suffixed with an underscore and a unique numerical
|
||||
/// value
|
||||
Symbol New(std::string name = "");
|
||||
Symbol New(std::string_view name = "");
|
||||
|
||||
/// Foreach calls the callback function `F` for each symbol in the table.
|
||||
/// @param callback must be a function or function-like object with the
|
||||
|
@ -88,6 +88,8 @@ class SymbolTable {
|
|||
SymbolTable(const SymbolTable&) = delete;
|
||||
SymbolTable& operator=(const SymbolTable& other) = delete;
|
||||
|
||||
Symbol RegisterInternal(std::string_view name);
|
||||
|
||||
// The value to be associated to the next registered symbol table entry.
|
||||
uint32_t next_symbol_ = 1;
|
||||
|
||||
|
|
Loading…
Reference in New Issue