Add SymbolTable::New()

Generates a new unnamed symbol.
Useful for creating temporaries in transforms.

Change-Id: I3aa037e4a6b2d400f24e01bbb3555af68da26748
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/41480
Reviewed-by: dan sinclair <dsinclair@chromium.org>
Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ben Clayton 2021-02-11 14:54:41 +00:00 committed by Commit Bot service account
parent 5d64f60a60
commit 5585f0789c
3 changed files with 11 additions and 3 deletions

View File

@ -52,10 +52,15 @@ Symbol SymbolTable::Get(const std::string& name) const {
std::string SymbolTable::NameFor(const Symbol symbol) const {
auto it = symbol_to_name_.find(symbol);
if (it == symbol_to_name_.end())
return "";
if (it == symbol_to_name_.end()) {
return symbol.to_str();
}
return it->second;
}
Symbol SymbolTable::New() {
return Symbol(next_symbol_++);
}
} // namespace tint

View File

@ -58,6 +58,9 @@ class SymbolTable {
/// @returns the symbol name or "" if not found
std::string NameFor(const Symbol symbol) const;
/// @returns a new, unnamed symbol
Symbol New();
private:
// The value to be associated to the next registered symbol table entry.
uint32_t next_symbol_ = 1;

View File

@ -42,7 +42,7 @@ TEST_F(SymbolTableTest, ReturnsNameForSymbol) {
TEST_F(SymbolTableTest, ReturnsBlankForMissingSymbol) {
SymbolTable s;
EXPECT_EQ("", s.NameFor(Symbol(2)));
EXPECT_EQ("tint_symbol_2", s.NameFor(Symbol(2)));
}
TEST_F(SymbolTableTest, ReturnsInvalidForBlankString) {