SymbolTable: Change behavior of anonymous symbols

SymbolTable::New() used to build and return a symbol without a registered name. When you asked for the name of the symbol it would return tint_symbol_N, where N is the numerical identifier for the symbol.

This approach was a major tripping hazzard for  transforms that liked to fetch the source program name, and register it in the new program (in this situation, you should always use `CloneContext::Clone(Symbol)`).
Without special casing for unnamed symbols, you could end up promoting the unnamed symbol to a named symbol, and then colliding against a new unnamed symbol.
This is exactly what happened in tint:711.

Instead, with this change:

* The concept of unnamed symbols has been removed. All symbols  now have a name.
* The signature of `SymbolTable::New()` has been changed to take a name parameter (which defaults to 'tint_symbol'). This can be used to create a new, unique named symbol (possibly with a suffix), which will not collide with any existing symbols. Note these symbols may still collide if `SymbolTable::Register()` is called with the same name. All Transforms that currently use `SymbolTable::Register()` will be fixed in another change.
* The CloneContext has been updated to use `SymbolTable::New()` instead of `Register()`. This means that any symbols defined before a clone will not collide.
* `CloneContext::CloneSymbols()` has been added which allows a transform to pre-clone all the symbols from the source program. This can be used to avoid the authored identifiers being suffixed with a number, in the case a transform calls New() before the symbol is cloned.
* `Symbol::to_str()` has been changed to return `$<id>` instead of `tint_symbol_N`. This is to avoid any confusion between the actual name and the symbol ID.

Bug: tint:711
Bug: tint:712
Change-Id: I526e4b49b7027545613859de487e6a275686107a
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/47631
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
This commit is contained in:
Ben Clayton
2021-04-13 20:07:57 +00:00
committed by Commit Bot service account
parent 46bb2bc935
commit 363a2e26e3
27 changed files with 689 additions and 623 deletions

View File

@@ -48,7 +48,7 @@ TEST_F(AliasTest, Is) {
TEST_F(AliasTest, TypeName) {
auto* at = ty.alias("Particle", ty.i32());
EXPECT_EQ(at->type_name(), "__alias_tint_symbol_1__i32");
EXPECT_EQ(at->type_name(), "__alias_$1__i32");
}
TEST_F(AliasTest, FriendlyName) {

View File

@@ -54,7 +54,7 @@ TEST_F(StructTypeTest, TypeName) {
auto* impl =
create<ast::Struct>(ast::StructMemberList{}, ast::DecorationList{});
auto* s = ty.struct_("my_struct", impl);
EXPECT_EQ(s->type_name(), "__struct_tint_symbol_1");
EXPECT_EQ(s->type_name(), "__struct_$1");
}
TEST_F(StructTypeTest, FriendlyName) {