From d7d71889d82bb5073d420cc11d16fd29346cd9d7 Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Mon, 5 Sep 2022 20:51:23 +0000 Subject: [PATCH] tint: Add FriendlyName() to Program Use this instead of ProgramBuilder::FriendlyName() in the Std140 transform. If this were called, we'd ICE that the program ids wouldn't match the type, as the type belongs to the source program, not the target program builder. Change-Id: I29066b18789493c231a89f7ee1dbc24d7e66d33f Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/101180 Auto-Submit: Ben Clayton Commit-Queue: Antonio Maiorano Kokoro: Kokoro Reviewed-by: Antonio Maiorano --- src/tint/program.cc | 13 +++++++++++++ src/tint/program.h | 15 +++++++++++++++ src/tint/program_builder.cc | 13 +++++++++++++ src/tint/program_builder.h | 10 +++------- src/tint/transform/std140.cc | 10 +++++----- 5 files changed, 49 insertions(+), 12 deletions(-) diff --git a/src/tint/program.cc b/src/tint/program.cc index 6109c20664..e4caca1f94 100644 --- a/src/tint/program.cc +++ b/src/tint/program.cc @@ -130,6 +130,19 @@ const sem::Type* Program::TypeOf(const ast::TypeDecl* type_decl) const { return Sem().Get(type_decl); } +std::string Program::FriendlyName(const ast::Type* type) const { + TINT_ASSERT_PROGRAM_IDS_EQUAL(Program, type, ID()); + return type ? type->FriendlyName(Symbols()) : ""; +} + +std::string Program::FriendlyName(const sem::Type* type) const { + return type ? type->FriendlyName(Symbols()) : ""; +} + +std::string Program::FriendlyName(std::nullptr_t) const { + return ""; +} + void Program::AssertNotMoved() const { TINT_ASSERT(Program, !moved_); } diff --git a/src/tint/program.h b/src/tint/program.h index b482f9050a..3390920682 100644 --- a/src/tint/program.h +++ b/src/tint/program.h @@ -151,6 +151,21 @@ class Program { /// the type declaration has no resolved type. const sem::Type* TypeOf(const ast::TypeDecl* type_decl) const; + /// @param type a type + /// @returns the name for `type` that closely resembles how it would be + /// declared in WGSL. + std::string FriendlyName(const ast::Type* type) const; + + /// @param type a type + /// @returns the name for `type` that closely resembles how it would be + /// declared in WGSL. + std::string FriendlyName(const sem::Type* type) const; + + /// Overload of FriendlyName, which removes an ambiguity when passing nullptr. + /// Simplifies test code. + /// @returns "" + std::string FriendlyName(std::nullptr_t) const; + /// A function that can be used to print a program using Printer = std::string (*)(const Program*); diff --git a/src/tint/program_builder.cc b/src/tint/program_builder.cc index 443be11966..a45d805ee4 100644 --- a/src/tint/program_builder.cc +++ b/src/tint/program_builder.cc @@ -113,6 +113,19 @@ const sem::Type* ProgramBuilder::TypeOf(const ast::TypeDecl* type_decl) const { return Sem().Get(type_decl); } +std::string ProgramBuilder::FriendlyName(const ast::Type* type) const { + TINT_ASSERT_PROGRAM_IDS_EQUAL(ProgramBuilder, type, ID()); + return type ? type->FriendlyName(Symbols()) : ""; +} + +std::string ProgramBuilder::FriendlyName(const sem::Type* type) const { + return type ? type->FriendlyName(Symbols()) : ""; +} + +std::string ProgramBuilder::FriendlyName(std::nullptr_t) const { + return ""; +} + const ast::TypeName* ProgramBuilder::TypesBuilder::Of(const ast::TypeDecl* decl) const { return type_name(decl->name); } diff --git a/src/tint/program_builder.h b/src/tint/program_builder.h index 0af3a9020b..bdd2807fb4 100644 --- a/src/tint/program_builder.h +++ b/src/tint/program_builder.h @@ -3110,21 +3110,17 @@ class ProgramBuilder { /// @param type a type /// @returns the name for `type` that closely resembles how it would be /// declared in WGSL. - std::string FriendlyName(const ast::Type* type) { - return type ? type->FriendlyName(Symbols()) : ""; - } + std::string FriendlyName(const ast::Type* type) const; /// @param type a type /// @returns the name for `type` that closely resembles how it would be /// declared in WGSL. - std::string FriendlyName(const sem::Type* type) { - return type ? type->FriendlyName(Symbols()) : ""; - } + std::string FriendlyName(const sem::Type* type) const; /// Overload of FriendlyName, which removes an ambiguity when passing nullptr. /// Simplifies test code. /// @returns "" - std::string FriendlyName(std::nullptr_t) { return ""; } + std::string FriendlyName(std::nullptr_t) const; /// Wraps the ast::Expression in a statement. This is used by tests that /// construct a partial AST and require the Resolver to reach these diff --git a/src/tint/transform/std140.cc b/src/tint/transform/std140.cc index 57da4da5a9..92d3d8bfac 100644 --- a/src/tint/transform/std140.cc +++ b/src/tint/transform/std140.cc @@ -518,7 +518,7 @@ struct Std140::State { }, [&](Default) { TINT_ICE(Transform, b.Diagnostics()) - << "unhandled type for conversion name: " << b.FriendlyName(ty); + << "unhandled type for conversion name: " << ctx.src->FriendlyName(ty); return ""; }); } @@ -600,7 +600,7 @@ struct Std140::State { }, [&](Default) { TINT_ICE(Transform, b.Diagnostics()) - << "unhandled type for conversion: " << b.FriendlyName(ty); + << "unhandled type for conversion: " << ctx.src->FriendlyName(ty); }); // Generate the function @@ -879,7 +879,7 @@ struct Std140::State { }, // [&](Default) -> ExprTypeName { TINT_ICE(Transform, b.Diagnostics()) - << "unhandled type for access chain: " << b.FriendlyName(ty); + << "unhandled type for access chain: " << ctx.src->FriendlyName(ty); return {}; }); } @@ -900,7 +900,7 @@ struct Std140::State { }, // [&](Default) -> ExprTypeName { TINT_ICE(Transform, b.Diagnostics()) - << "unhandled type for access chain: " << b.FriendlyName(ty); + << "unhandled type for access chain: " << ctx.src->FriendlyName(ty); return {}; }); } @@ -929,7 +929,7 @@ struct Std140::State { }, // [&](Default) -> ExprTypeName { TINT_ICE(Transform, b.Diagnostics()) - << "unhandled type for access chain: " << b.FriendlyName(ty); + << "unhandled type for access chain: " << ctx.src->FriendlyName(ty); return {}; }); }