diff --git a/src/ast/builder.cc b/src/ast/builder.cc index 612bc9e44e..3e6863b6b1 100644 --- a/src/ast/builder.cc +++ b/src/ast/builder.cc @@ -40,8 +40,8 @@ Variable* Builder::Var(const std::string& name, type::Type* type, Expression* constructor, VariableDecorationList decorations) { - auto* var = - create(name, storage, type, false, constructor, decorations); + auto* var = create(mod->RegisterSymbol(name), name, storage, type, + false, constructor, decorations); OnVariableBuilt(var); return var; } @@ -52,8 +52,8 @@ Variable* Builder::Var(const Source& source, type::Type* type, Expression* constructor, VariableDecorationList decorations) { - auto* var = create(source, name, storage, type, false, constructor, - decorations); + auto* var = create(source, mod->RegisterSymbol(name), name, storage, + type, false, constructor, decorations); OnVariableBuilt(var); return var; } @@ -69,8 +69,8 @@ Variable* Builder::Const(const std::string& name, type::Type* type, Expression* constructor, VariableDecorationList decorations) { - auto* var = - create(name, storage, type, true, constructor, decorations); + auto* var = create(mod->RegisterSymbol(name), name, storage, type, + true, constructor, decorations); OnVariableBuilt(var); return var; } @@ -81,8 +81,8 @@ Variable* Builder::Const(const Source& source, type::Type* type, Expression* constructor, VariableDecorationList decorations) { - auto* var = create(source, name, storage, type, true, constructor, - decorations); + auto* var = create(source, mod->RegisterSymbol(name), name, storage, + type, true, constructor, decorations); OnVariableBuilt(var); return var; } diff --git a/src/ast/variable.cc b/src/ast/variable.cc index c94a97d6b5..a01a9dd6d2 100644 --- a/src/ast/variable.cc +++ b/src/ast/variable.cc @@ -26,6 +26,7 @@ namespace tint { namespace ast { Variable::Variable(const Source& source, + const Symbol& sym, const std::string& name, StorageClass sc, type::Type* type, @@ -33,6 +34,7 @@ Variable::Variable(const Source& source, Expression* constructor, VariableDecorationList decorations) : Base(source), + symbol_(sym), name_(name), type_(type), is_const_(is_const), @@ -82,13 +84,14 @@ uint32_t Variable::constant_id() const { } Variable* Variable::Clone(CloneContext* ctx) const { - return ctx->mod->create( - ctx->Clone(source()), name(), storage_class(), ctx->Clone(type()), - is_const_, ctx->Clone(constructor()), ctx->Clone(decorations_)); + return ctx->mod->create(ctx->Clone(source()), ctx->Clone(symbol_), + name(), storage_class(), ctx->Clone(type()), + is_const_, ctx->Clone(constructor()), + ctx->Clone(decorations_)); } bool Variable::IsValid() const { - if (name_.length() == 0) { + if (name_.length() == 0 || !symbol_.IsValid()) { return false; } if (type_ == nullptr) { @@ -102,7 +105,7 @@ bool Variable::IsValid() const { void Variable::info_to_str(std::ostream& out, size_t indent) const { make_indent(out, indent); - out << name_ << std::endl; + out << symbol_.to_str() << std::endl; make_indent(out, indent); out << storage_class_ << std::endl; make_indent(out, indent); diff --git a/src/ast/variable.h b/src/ast/variable.h index bf8c1db837..5403264322 100644 --- a/src/ast/variable.h +++ b/src/ast/variable.h @@ -26,6 +26,7 @@ #include "src/ast/storage_class.h" #include "src/ast/type/type.h" #include "src/ast/variable_decoration.h" +#include "src/symbol.h" namespace tint { namespace ast { @@ -81,6 +82,7 @@ class Variable : public Castable { public: /// Create a variable /// @param source the variable source + /// @param sym the variable symbol /// @param name the variables name /// @param sc the variable storage class /// @param type the value type @@ -88,6 +90,7 @@ class Variable : public Castable { /// @param constructor the constructor expression /// @param decorations the variable decorations Variable(const Source& source, + const Symbol& sym, const std::string& name, StorageClass sc, type::Type* type, @@ -99,6 +102,8 @@ class Variable : public Castable { ~Variable() override; + /// @returns the variable symbol + const Symbol& symbol() const { return symbol_; } /// @returns the variable name const std::string& name() const { return name_; } @@ -141,7 +146,7 @@ class Variable : public Castable { /// @return the newly cloned node Variable* Clone(CloneContext* ctx) const override; - /// @returns true if the name and path are both present + /// @returns true if the variable is valid bool IsValid() const override; /// Writes a representation of the node to the output stream @@ -162,6 +167,7 @@ class Variable : public Castable { private: Variable(const Variable&) = delete; + Symbol const symbol_; std::string const name_; // The value type if a const or formal paramter, and the store type if a var type::Type* const type_; diff --git a/src/ast/variable_decl_statement_test.cc b/src/ast/variable_decl_statement_test.cc index 67289f69a0..a432804b5b 100644 --- a/src/ast/variable_decl_statement_test.cc +++ b/src/ast/variable_decl_statement_test.cc @@ -72,7 +72,7 @@ TEST_F(VariableDeclStatementTest, ToStr) { create(Source{Source::Location{20, 2}}, var); std::ostringstream out; stmt->to_str(out, 2); - EXPECT_EQ(out.str(), R"( VariableDeclStatement{ + EXPECT_EQ(demangle(out.str()), R"( VariableDeclStatement{ Variable{ a none diff --git a/src/ast/variable_test.cc b/src/ast/variable_test.cc index 3004d31018..c97442cd4d 100644 --- a/src/ast/variable_test.cc +++ b/src/ast/variable_test.cc @@ -29,6 +29,7 @@ using VariableTest = TestHelper; TEST_F(VariableTest, Creation) { auto* v = Var("my_var", StorageClass::kFunction, ty.i32); + EXPECT_EQ(v->symbol(), Symbol(1)); EXPECT_EQ(v->name(), "my_var"); EXPECT_EQ(v->storage_class(), StorageClass::kFunction); EXPECT_EQ(v->type(), ty.i32); @@ -43,6 +44,7 @@ TEST_F(VariableTest, CreationWithSource) { Source{Source::Range{Source::Location{27, 4}, Source::Location{27, 5}}}, "i", StorageClass::kPrivate, ty.f32, nullptr, VariableDecorationList{}); + EXPECT_EQ(v->symbol(), Symbol(1)); EXPECT_EQ(v->name(), "i"); EXPECT_EQ(v->storage_class(), StorageClass::kPrivate); EXPECT_EQ(v->type(), ty.f32); @@ -58,6 +60,7 @@ TEST_F(VariableTest, CreationEmpty) { "a_var", StorageClass::kWorkgroup, ty.i32, nullptr, VariableDecorationList{}); + EXPECT_EQ(v->symbol(), Symbol(1)); EXPECT_EQ(v->name(), "a_var"); EXPECT_EQ(v->storage_class(), StorageClass::kWorkgroup); EXPECT_EQ(v->type(), ty.i32); @@ -78,7 +81,7 @@ TEST_F(VariableTest, IsValid_WithConstructor) { EXPECT_TRUE(v->IsValid()); } -TEST_F(VariableTest, IsValid_MissinName) { +TEST_F(VariableTest, IsValid_MissingSymbol) { auto* v = Var("", StorageClass::kNone, ty.i32); EXPECT_FALSE(v->IsValid()); } @@ -104,7 +107,7 @@ TEST_F(VariableTest, to_str) { ast::VariableDecorationList{}); std::ostringstream out; v->to_str(out, 2); - EXPECT_EQ(out.str(), R"( Variable{ + EXPECT_EQ(demangle(out.str()), R"( Variable{ my_var function __f32 diff --git a/src/reader/spirv/function.cc b/src/reader/spirv/function.cc index 83ac79738a..f81cc77932 100644 --- a/src/reader/spirv/function.cc +++ b/src/reader/spirv/function.cc @@ -2248,8 +2248,9 @@ bool FunctionEmitter::EmitIfStart(const BlockInfo& block_info) { if (!guard_name.empty()) { // Declare the guard variable just before the "if", initialized to true. auto* guard_var = - create(Source{}, // source - guard_name, // name + create(Source{}, // source + ast_module_.RegisterSymbol(guard_name), // symbol + guard_name, // name ast::StorageClass::kFunction, // storage_class parser_impl_.Bool(), // type false, // is_const @@ -2777,6 +2778,7 @@ bool FunctionEmitter::EmitStatementsInBasicBlock(const BlockInfo& block_info, assert(!phi_var_name.empty()); auto* var = create( Source{}, // source + ast_module_.RegisterSymbol(phi_var_name), // symbol phi_var_name, // name ast::StorageClass::kFunction, // storage_class parser_impl_.ConvertType(def_inst->type_id()), // type diff --git a/src/reader/spirv/function_call_test.cc b/src/reader/spirv/function_call_test.cc index ce9525a3a1..251bafca09 100644 --- a/src/reader/spirv/function_call_test.cc +++ b/src/reader/spirv/function_call_test.cc @@ -216,9 +216,10 @@ TEST_F(SpvParserTest, EmitStatement_CallWithParams) { )")); ASSERT_TRUE(p->BuildAndParseInternalModule()) << p->error(); EXPECT_TRUE(p->error().empty()); - const auto module_ast_str = p->get_module().to_str(); + const auto module_ast_str = + Demangler().Demangle(p->get_module(), p->get_module().to_str()); EXPECT_THAT(module_ast_str, HasSubstr(R"(Module{ - Function tint_symbol_3 -> __u32 + Function x_50 -> __u32 ( VariableConst{ x_51 @@ -235,15 +236,14 @@ TEST_F(SpvParserTest, EmitStatement_CallWithParams) { Return{ { Binary[not set]{ - Identifier[not set]{tint_symbol_1} + Identifier[not set]{x_51} add - Identifier[not set]{tint_symbol_2} + Identifier[not set]{x_52} } } } } - Function )" + p->get_module().GetSymbol("x_100").to_str() + - R"( -> __void + Function x_100 -> __void () { VariableDeclStatement{ @@ -253,7 +253,7 @@ TEST_F(SpvParserTest, EmitStatement_CallWithParams) { __u32 { Call[not set]{ - Identifier[not set]{tint_symbol_3} + Identifier[not set]{x_50} ( ScalarConstructor[not set]{42} ScalarConstructor[not set]{84} diff --git a/src/reader/spirv/function_decl_test.cc b/src/reader/spirv/function_decl_test.cc index 398f8fa8a1..164ab13b09 100644 --- a/src/reader/spirv/function_decl_test.cc +++ b/src/reader/spirv/function_decl_test.cc @@ -59,10 +59,9 @@ TEST_F(SpvParserTest, Emit_VoidFunctionWithoutParams) { ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()); FunctionEmitter fe(p.get(), *spirv_function(p.get(), 100)); EXPECT_TRUE(fe.Emit()); - auto got = p->get_module().to_str(); - auto expect = R"(Module{ - Function )" + p->get_module().GetSymbol("x_100").to_str() + - R"( -> __void + auto got = Demangler().Demangle(p->get_module(), p->get_module().to_str()); + std::string expect = R"(Module{ + Function x_100 -> __void () { Return{} @@ -84,10 +83,9 @@ TEST_F(SpvParserTest, Emit_NonVoidResultType) { FunctionEmitter fe(p.get(), *spirv_function(p.get(), 100)); EXPECT_TRUE(fe.Emit()); - auto got = p->get_module().to_str(); - auto expect = R"(Module{ - Function )" + p->get_module().GetSymbol("x_100").to_str() + - R"( -> __f32 + auto got = Demangler().Demangle(p->get_module(), p->get_module().to_str()); + std::string expect = R"(Module{ + Function x_100 -> __f32 () { Return{ @@ -117,10 +115,9 @@ TEST_F(SpvParserTest, Emit_MixedParamTypes) { FunctionEmitter fe(p.get(), *spirv_function(p.get(), 100)); EXPECT_TRUE(fe.Emit()); - auto got = p->get_module().to_str(); - auto expect = R"(Module{ - Function )" + p->get_module().GetSymbol("x_100").to_str() + - R"( -> __void + auto got = Demangler().Demangle(p->get_module(), p->get_module().to_str()); + std::string expect = R"(Module{ + Function x_100 -> __void ( VariableConst{ a @@ -162,10 +159,9 @@ TEST_F(SpvParserTest, Emit_GenerateParamNames) { FunctionEmitter fe(p.get(), *spirv_function(p.get(), 100)); EXPECT_TRUE(fe.Emit()); - auto got = p->get_module().to_str(); - auto expect = R"(Module{ - Function )" + p->get_module().GetSymbol("x_100").to_str() + - R"( -> __void + auto got = Demangler().Demangle(p->get_module(), p->get_module().to_str()); + std::string expect = R"(Module{ + Function x_100 -> __void ( VariableConst{ x_14 diff --git a/src/reader/spirv/parser_impl.cc b/src/reader/spirv/parser_impl.cc index 4ebc6423c7..5ceea7ba17 100644 --- a/src/reader/spirv/parser_impl.cc +++ b/src/reader/spirv/parser_impl.cc @@ -1297,13 +1297,15 @@ ast::Variable* ParserImpl::MakeVariable( } } - return create(Source{}, // source - namer_.Name(id), // name - sc, // storage_class - type, // type - is_const, // is_const - constructor, // constructor - decorations); // decorations + std::string name = namer_.Name(id); + return create(Source{}, // source + ast_module_.RegisterSymbol(name), // symbol + name, // name + sc, // storage_class + type, // type + is_const, // is_const + constructor, // constructor + decorations); // decorations } TypedExpression ParserImpl::MakeConstantExpression(uint32_t id) { diff --git a/src/reader/spirv/parser_impl_function_decl_test.cc b/src/reader/spirv/parser_impl_function_decl_test.cc index ff605239ec..ef85635fef 100644 --- a/src/reader/spirv/parser_impl_function_decl_test.cc +++ b/src/reader/spirv/parser_impl_function_decl_test.cc @@ -267,10 +267,10 @@ TEST_F(SpvParserTest, EmitFunctions_NonVoidResultType) { )")); EXPECT_TRUE(p->BuildAndParseInternalModule()); EXPECT_TRUE(p->error().empty()); - const auto module_ast = p->get_module().to_str(); + const auto module_ast = + Demangler().Demangle(p->get_module(), p->get_module().to_str()); EXPECT_THAT(module_ast, HasSubstr(R"( - Function )" + p->get_module().GetSymbol("ret_float").to_str() + - R"( -> __f32 + Function ret_float -> __f32 () { Return{ @@ -297,10 +297,10 @@ TEST_F(SpvParserTest, EmitFunctions_MixedParamTypes) { )")); EXPECT_TRUE(p->BuildAndParseInternalModule()); EXPECT_TRUE(p->error().empty()); - const auto module_ast = p->get_module().to_str(); + const auto module_ast = + Demangler().Demangle(p->get_module(), p->get_module().to_str()); EXPECT_THAT(module_ast, HasSubstr(R"( - Function )" + p->get_module().GetSymbol("mixed_params").to_str() + - R"( -> __void + Function mixed_params -> __void ( VariableConst{ a @@ -337,10 +337,10 @@ TEST_F(SpvParserTest, EmitFunctions_GenerateParamNames) { )")); EXPECT_TRUE(p->BuildAndParseInternalModule()); EXPECT_TRUE(p->error().empty()); - const auto module_ast = p->get_module().to_str(); + const auto module_ast = + Demangler().Demangle(p->get_module(), p->get_module().to_str()); EXPECT_THAT(module_ast, HasSubstr(R"( - Function )" + p->get_module().GetSymbol("mixed_params").to_str() + - R"( -> __void + Function mixed_params -> __void ( VariableConst{ x_14 diff --git a/src/reader/spirv/parser_impl_module_var_test.cc b/src/reader/spirv/parser_impl_module_var_test.cc index e38bba3f18..9a178e5087 100644 --- a/src/reader/spirv/parser_impl_module_var_test.cc +++ b/src/reader/spirv/parser_impl_module_var_test.cc @@ -141,7 +141,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_AnonWorkgroupVar) { EXPECT_TRUE(p->BuildAndParseInternalModule()); EXPECT_TRUE(p->error().empty()); - const auto module_str = p->module().to_str(); + const auto module_str = + Demangler().Demangle(p->get_module(), p->get_module().to_str()); EXPECT_THAT(module_str, HasSubstr(R"( Variable{ x_52 @@ -160,7 +161,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_NamedWorkgroupVar) { EXPECT_TRUE(p->BuildAndParseInternalModule()); EXPECT_TRUE(p->error().empty()); - const auto module_str = p->module().to_str(); + const auto module_str = + Demangler().Demangle(p->get_module(), p->get_module().to_str()); EXPECT_THAT(module_str, HasSubstr(R"( Variable{ the_counter @@ -179,7 +181,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_PrivateVar) { EXPECT_TRUE(p->BuildAndParseInternalModule()); EXPECT_TRUE(p->error().empty()); - const auto module_str = p->module().to_str(); + const auto module_str = + Demangler().Demangle(p->get_module(), p->get_module().to_str()); EXPECT_THAT(module_str, HasSubstr(R"( Variable{ my_own_private_idaho @@ -198,7 +201,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_BuiltinVertexIndex) { EXPECT_TRUE(p->BuildAndParseInternalModule()); EXPECT_TRUE(p->error().empty()); - const auto module_str = p->module().to_str(); + const auto module_str = + Demangler().Demangle(p->get_module(), p->get_module().to_str()); EXPECT_THAT(module_str, HasSubstr(R"( Variable{ Decorations{ @@ -248,7 +252,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_BuiltinPosition_MapsToModuleScopeVec4Var) { EXPECT_EQ(position_info.pointer_type_id, 11u); EXPECT_EQ(position_info.storage_class, SpvStorageClassOutput); EXPECT_EQ(position_info.per_vertex_var_id, 1u); - const auto module_str = p->module().to_str(); + const auto module_str = + Demangler().Demangle(p->get_module(), p->get_module().to_str()); EXPECT_THAT(module_str, HasSubstr(R"( Variable{ Decorations{ @@ -718,7 +723,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_ScalarInitializers) { )")); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()); EXPECT_TRUE(p->error().empty()); - const auto module_str = p->module().to_str(); + const auto module_str = + Demangler().Demangle(p->get_module(), p->get_module().to_str()); EXPECT_THAT(module_str, HasSubstr(R"(Variable{ x_1 private @@ -775,7 +781,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_ScalarNullInitializers) { )")); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()); EXPECT_TRUE(p->error().empty()); - const auto module_str = p->module().to_str(); + const auto module_str = + Demangler().Demangle(p->get_module(), p->get_module().to_str()); EXPECT_THAT(module_str, HasSubstr(R"(Variable{ x_1 private @@ -824,7 +831,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_ScalarUndefInitializers) { )")); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << p->error(); EXPECT_TRUE(p->error().empty()); - const auto module_str = p->module().to_str(); + const auto module_str = + Demangler().Demangle(p->get_module(), p->get_module().to_str()); EXPECT_THAT(module_str, HasSubstr(R"(Variable{ x_1 private @@ -868,7 +876,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_VectorInitializer) { )")); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()); EXPECT_TRUE(p->error().empty()); - const auto module_str = p->module().to_str(); + const auto module_str = + Demangler().Demangle(p->get_module(), p->get_module().to_str()); EXPECT_THAT(module_str, HasSubstr(R"(Variable{ x_200 private @@ -891,7 +900,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_VectorBoolNullInitializer) { )")); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()); EXPECT_TRUE(p->error().empty()); - const auto module_str = p->module().to_str(); + const auto module_str = + Demangler().Demangle(p->get_module(), p->get_module().to_str()); EXPECT_THAT(module_str, HasSubstr(R"(Variable{ x_200 private @@ -914,7 +924,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_VectorBoolUndefInitializer) { )")); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()); EXPECT_TRUE(p->error().empty()); - const auto module_str = p->module().to_str(); + const auto module_str = + Demangler().Demangle(p->get_module(), p->get_module().to_str()); EXPECT_THAT(module_str, HasSubstr(R"(Variable{ x_200 private @@ -937,7 +948,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_VectorUintNullInitializer) { )")); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()); EXPECT_TRUE(p->error().empty()); - const auto module_str = p->module().to_str(); + const auto module_str = + Demangler().Demangle(p->get_module(), p->get_module().to_str()); EXPECT_THAT(module_str, HasSubstr(R"(Variable{ x_200 private @@ -960,7 +972,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_VectorUintUndefInitializer) { )")); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()); EXPECT_TRUE(p->error().empty()); - const auto module_str = p->module().to_str(); + const auto module_str = + Demangler().Demangle(p->get_module(), p->get_module().to_str()); EXPECT_THAT(module_str, HasSubstr(R"(Variable{ x_200 private @@ -983,7 +996,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_VectorIntNullInitializer) { )")); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()); EXPECT_TRUE(p->error().empty()); - const auto module_str = p->module().to_str(); + const auto module_str = + Demangler().Demangle(p->get_module(), p->get_module().to_str()); EXPECT_THAT(module_str, HasSubstr(R"(Variable{ x_200 private @@ -1006,7 +1020,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_VectorIntUndefInitializer) { )")); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()); EXPECT_TRUE(p->error().empty()); - const auto module_str = p->module().to_str(); + const auto module_str = + Demangler().Demangle(p->get_module(), p->get_module().to_str()); EXPECT_THAT(module_str, HasSubstr(R"(Variable{ x_200 private @@ -1029,7 +1044,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_VectorFloatNullInitializer) { )")); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()); EXPECT_TRUE(p->error().empty()); - const auto module_str = p->module().to_str(); + const auto module_str = + Demangler().Demangle(p->get_module(), p->get_module().to_str()); EXPECT_THAT(module_str, HasSubstr(R"(Variable{ x_200 private @@ -1052,7 +1068,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_VectorFloatUndefInitializer) { )")); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()); EXPECT_TRUE(p->error().empty()); - const auto module_str = p->module().to_str(); + const auto module_str = + Demangler().Demangle(p->get_module(), p->get_module().to_str()); EXPECT_THAT(module_str, HasSubstr(R"(Variable{ x_200 private @@ -1081,7 +1098,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_MatrixInitializer) { )")); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()); EXPECT_TRUE(p->error().empty()); - const auto module_str = p->module().to_str(); + const auto module_str = + Demangler().Demangle(p->get_module(), p->get_module().to_str()); EXPECT_THAT(module_str, HasSubstr(R"(Variable{ x_200 private @@ -1117,7 +1135,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_MatrixNullInitializer) { )")); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()); EXPECT_TRUE(p->error().empty()); - const auto module_str = p->module().to_str(); + const auto module_str = + Demangler().Demangle(p->get_module(), p->get_module().to_str()); EXPECT_THAT(module_str, HasSubstr(R"(Variable{ x_200 private @@ -1153,7 +1172,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_MatrixUndefInitializer) { )")); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()); EXPECT_TRUE(p->error().empty()); - const auto module_str = p->module().to_str(); + const auto module_str = + Demangler().Demangle(p->get_module(), p->get_module().to_str()); EXPECT_THAT(module_str, HasSubstr(R"(Variable{ x_200 private @@ -1190,7 +1210,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_ArrayInitializer) { )")); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()); EXPECT_TRUE(p->error().empty()); - const auto module_str = p->module().to_str(); + const auto module_str = + Demangler().Demangle(p->get_module(), p->get_module().to_str()); EXPECT_THAT(module_str, HasSubstr(R"(Variable{ x_200 private @@ -1213,7 +1234,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_ArrayNullInitializer) { )")); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()); EXPECT_TRUE(p->error().empty()); - const auto module_str = p->module().to_str(); + const auto module_str = + Demangler().Demangle(p->get_module(), p->get_module().to_str()); EXPECT_THAT(module_str, HasSubstr(R"(Variable{ x_200 private @@ -1236,7 +1258,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_ArrayUndefInitializer) { )")); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()); EXPECT_TRUE(p->error().empty()); - const auto module_str = p->module().to_str(); + const auto module_str = + Demangler().Demangle(p->get_module(), p->get_module().to_str()); EXPECT_THAT(module_str, HasSubstr(R"(Variable{ x_200 private @@ -1353,7 +1376,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_LocationDecoration_Valid) { )")); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << p->error(); EXPECT_TRUE(p->error().empty()); - const auto module_str = p->module().to_str(); + const auto module_str = + Demangler().Demangle(p->get_module(), p->get_module().to_str()); EXPECT_THAT(module_str, HasSubstr(R"( Variable{ Decorations{ @@ -1716,7 +1740,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_ScalarSpecConstant_DeclareConst_True) { )")); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << p->error(); EXPECT_TRUE(p->error().empty()); - const auto module_str = p->module().to_str(); + const auto module_str = + Demangler().Demangle(p->get_module(), p->get_module().to_str()); EXPECT_THAT(module_str, HasSubstr(R"( VariableConst{ Decorations{ @@ -1741,7 +1766,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_ScalarSpecConstant_DeclareConst_False) { )")); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << p->error(); EXPECT_TRUE(p->error().empty()); - const auto module_str = p->module().to_str(); + const auto module_str = + Demangler().Demangle(p->get_module(), p->get_module().to_str()); EXPECT_THAT(module_str, HasSubstr(R"( VariableConst{ Decorations{ @@ -1766,7 +1792,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_ScalarSpecConstant_DeclareConst_U32) { )")); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << p->error(); EXPECT_TRUE(p->error().empty()); - const auto module_str = p->module().to_str(); + const auto module_str = + Demangler().Demangle(p->get_module(), p->get_module().to_str()); EXPECT_THAT(module_str, HasSubstr(R"( VariableConst{ Decorations{ @@ -1791,7 +1818,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_ScalarSpecConstant_DeclareConst_I32) { )")); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << p->error(); EXPECT_TRUE(p->error().empty()); - const auto module_str = p->module().to_str(); + const auto module_str = + Demangler().Demangle(p->get_module(), p->get_module().to_str()); EXPECT_THAT(module_str, HasSubstr(R"( VariableConst{ Decorations{ @@ -1816,7 +1844,8 @@ TEST_F(SpvParserTest, ModuleScopeVar_ScalarSpecConstant_DeclareConst_F32) { )")); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << p->error(); EXPECT_TRUE(p->error().empty()); - const auto module_str = p->module().to_str(); + const auto module_str = + Demangler().Demangle(p->get_module(), p->get_module().to_str()); EXPECT_THAT(module_str, HasSubstr(R"( VariableConst{ Decorations{ @@ -1842,7 +1871,8 @@ TEST_F(SpvParserTest, )")); ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << p->error(); EXPECT_TRUE(p->error().empty()); - const auto module_str = p->module().to_str(); + const auto module_str = + Demangler().Demangle(p->get_module(), p->get_module().to_str()); EXPECT_THAT(module_str, HasSubstr(R"( VariableConst{ myconst diff --git a/src/reader/wgsl/parser_impl.cc b/src/reader/wgsl/parser_impl.cc index 1c3b31d6f3..9aaea82c7a 100644 --- a/src/reader/wgsl/parser_impl.cc +++ b/src/reader/wgsl/parser_impl.cc @@ -431,8 +431,9 @@ Maybe ParserImpl::global_variable_decl( constructor = expr.value; } - return create(decl->source, // source - decl->name, // name + return create(decl->source, // source + module_.RegisterSymbol(decl->name), // symbol + decl->name, // name decl->storage_class, // storage_class decl->type, // type false, // is_const @@ -459,8 +460,9 @@ Maybe ParserImpl::global_constant_decl() { if (init.errored) return Failure::kErrored; - return create(decl->source, // source - decl->name, // name + return create(decl->source, // source + module_.RegisterSymbol(decl->name), // symbol + decl->name, // name ast::StorageClass::kNone, // storage_class decl->type, // type true, // is_const @@ -1353,8 +1355,9 @@ Expect ParserImpl::expect_param_list() { ast::VariableList ret; for (;;) { auto* var = - create(decl->source, // source - decl->name, // name + create(decl->source, // source + module_.RegisterSymbol(decl->name), // symbol + decl->name, // name ast::StorageClass::kNone, // storage_class decl->type, // type true, // is_const @@ -1624,8 +1627,9 @@ Maybe ParserImpl::variable_stmt() { return add_error(peek(), "missing constructor for const declaration"); auto* var = - create(decl->source, // source - decl->name, // name + create(decl->source, // source + module_.RegisterSymbol(decl->name), // symbol + decl->name, // name ast::StorageClass::kNone, // storage_class decl->type, // type true, // is_const @@ -1653,8 +1657,9 @@ Maybe ParserImpl::variable_stmt() { } auto* var = - create(decl->source, // source - decl->name, // name + create(decl->source, // source + module_.RegisterSymbol(decl->name), // symbol + decl->name, // name decl->storage_class, // storage_class decl->type, // type false, // is_const diff --git a/src/scope_stack_test.cc b/src/scope_stack_test.cc index 5f02f4053b..b11c6f16f3 100644 --- a/src/scope_stack_test.cc +++ b/src/scope_stack_test.cc @@ -14,13 +14,14 @@ #include "src/scope_stack.h" #include "gtest/gtest.h" +#include "src/ast/builder.h" #include "src/ast/type/f32_type.h" #include "src/ast/variable.h" namespace tint { namespace { -using ScopeStackTest = testing::Test; +class ScopeStackTest : public ast::BuilderWithModule, public testing::Test {}; TEST_F(ScopeStackTest, Global) { ScopeStack s; @@ -32,12 +33,9 @@ TEST_F(ScopeStackTest, Global) { } TEST_F(ScopeStackTest, Global_SetWithPointer) { - ast::type::F32 f32; - ast::Variable v(Source{}, "my_var", ast::StorageClass::kNone, &f32, false, - nullptr, ast::VariableDecorationList{}); - + auto* v = Var("my_var", ast::StorageClass::kNone, ty.f32); ScopeStack s; - s.set_global("var", &v); + s.set_global("var", v); ast::Variable* v2 = nullptr; EXPECT_TRUE(s.get("var", &v2)); diff --git a/src/transform/emit_vertex_point_size.cc b/src/transform/emit_vertex_point_size.cc index 374bcd1326..7e48e0ad15 100644 --- a/src/transform/emit_vertex_point_size.cc +++ b/src/transform/emit_vertex_point_size.cc @@ -51,12 +51,13 @@ Transform::Output EmitVertexPointSize::Run(ast::Module* in) { // Declare the pointsize builtin output variable. auto* pointsize_var = out.module.create( - Source{}, // source - kPointSizeVar, // name - ast::StorageClass::kOutput, // storage_class - f32, // type - false, // is_const - nullptr, // constructor + Source{}, // source + out.module.RegisterSymbol(kPointSizeVar), // symbol + kPointSizeVar, // name + ast::StorageClass::kOutput, // storage_class + f32, // type + false, // is_const + nullptr, // constructor ast::VariableDecorationList{ // decorations out.module.create(Source{}, diff --git a/src/transform/first_index_offset.cc b/src/transform/first_index_offset.cc index b218609e14..db1ddd4c8d 100644 --- a/src/transform/first_index_offset.cc +++ b/src/transform/first_index_offset.cc @@ -65,13 +65,14 @@ ast::Variable* clone_variable_with_new_name(ast::CloneContext* ctx, ast::Variable* in, std::string new_name) { return ctx->mod->create( - ctx->Clone(in->source()), // source - new_name, // name - in->storage_class(), // storage_class - ctx->Clone(in->type()), // type - in->is_const(), // is_const - ctx->Clone(in->constructor()), // constructor - ctx->Clone(in->decorations())); // decorations + ctx->Clone(in->source()), // source + ctx->mod->RegisterSymbol(new_name), // symbol + new_name, // name + in->storage_class(), // storage_class + ctx->Clone(in->type()), // type + in->is_const(), // is_const + ctx->Clone(in->constructor()), // constructor + ctx->Clone(in->decorations())); // decorations } } // namespace @@ -226,12 +227,13 @@ ast::Variable* FirstIndexOffset::AddUniformBuffer(ast::Module* mod) { mod->create(Source{}, std::move(members), std::move(decos))); auto* idx_var = mod->create( - Source{}, // source - kBufferName, // name - ast::StorageClass::kUniform, // storage_class - struct_type, // type - false, // is_const - nullptr, // constructor + Source{}, // source + mod->RegisterSymbol(kBufferName), // symbol + kBufferName, // name + ast::StorageClass::kUniform, // storage_class + struct_type, // type + false, // is_const + nullptr, // constructor ast::VariableDecorationList{ mod->create(Source{}, binding_), mod->create(Source{}, set_), @@ -261,8 +263,9 @@ ast::VariableDeclStatement* FirstIndexOffset::CreateFirstIndexOffset( mod->create( Source{}, mod->RegisterSymbol(field_name), field_name))); auto* var = - mod->create(Source{}, // source - original_name, // name + mod->create(Source{}, // source + mod->RegisterSymbol(original_name), // symbol + original_name, // name ast::StorageClass::kNone, // storage_class mod->create(), // type true, // is_const diff --git a/src/transform/vertex_pulling.cc b/src/transform/vertex_pulling.cc index cba1eec449..7b986faa0b 100644 --- a/src/transform/vertex_pulling.cc +++ b/src/transform/vertex_pulling.cc @@ -165,18 +165,19 @@ void VertexPulling::State::FindOrInsertVertexIndexIfUsed() { // We didn't find a vertex index builtin, so create one vertex_index_name = kDefaultVertexIndexName; - auto* var = - out->create(Source{}, // source - vertex_index_name, // name - ast::StorageClass::kInput, // storage_class - GetI32Type(), // type - false, // is_const - nullptr, // constructor - ast::VariableDecorationList{ - // decorations - out->create( - Source{}, ast::Builtin::kVertexIdx), - }); + auto* var = out->create( + Source{}, // source + out->RegisterSymbol(vertex_index_name), // symbol + vertex_index_name, // name + ast::StorageClass::kInput, // storage_class + GetI32Type(), // type + false, // is_const + nullptr, // constructor + ast::VariableDecorationList{ + // decorations + out->create(Source{}, + ast::Builtin::kVertexIdx), + }); out->AddGlobalVariable(var); } @@ -212,18 +213,19 @@ void VertexPulling::State::FindOrInsertInstanceIndexIfUsed() { // We didn't find an instance index builtin, so create one instance_index_name = kDefaultInstanceIndexName; - auto* var = - out->create(Source{}, // source - instance_index_name, // name - ast::StorageClass::kInput, // storage_class - GetI32Type(), // type - false, // is_const - nullptr, // constructor - ast::VariableDecorationList{ - // decorations - out->create( - Source{}, ast::Builtin::kInstanceIdx), - }); + auto* var = out->create( + Source{}, // source + out->RegisterSymbol(instance_index_name), // symbol + instance_index_name, // name + ast::StorageClass::kInput, // storage_class + GetI32Type(), // type + false, // is_const + nullptr, // constructor + ast::VariableDecorationList{ + // decorations + out->create(Source{}, + ast::Builtin::kInstanceIdx), + }); out->AddGlobalVariable(var); } @@ -241,6 +243,7 @@ void VertexPulling::State::ConvertVertexInputVariablesToPrivate() { // place in the AST. v = out->create( Source{}, // source + v->symbol(), // symbol v->name(), // name ast::StorageClass::kPrivate, // storage_class v->type(), // type @@ -282,9 +285,11 @@ void VertexPulling::State::AddVertexStorageBuffers() { for (uint32_t i = 0; i < cfg.vertex_state.size(); ++i) { // The decorated variable with struct type + std::string name = GetVertexBufferName(i); auto* var = out->create( Source{}, // source - GetVertexBufferName(i), // name + out->RegisterSymbol(name), // symbol + name, // name ast::StorageClass::kStorageBuffer, // storage_class struct_type, // type false, // is_const @@ -308,13 +313,14 @@ ast::BlockStatement* VertexPulling::State::CreateVertexPullingPreamble() const { // Declare the |kPullingPosVarName| variable in the shader auto* pos_declaration = out->create( Source{}, out->create( - Source{}, // source - kPullingPosVarName, // name - ast::StorageClass::kFunction, // storage_class - GetI32Type(), // type - false, // is_const - nullptr, // constructor - ast::VariableDecorationList{})); // decorations + Source{}, // source + out->RegisterSymbol(kPullingPosVarName), // symbol + kPullingPosVarName, // name + ast::StorageClass::kFunction, // storage_class + GetI32Type(), // type + false, // is_const + nullptr, // constructor + ast::VariableDecorationList{})); // decorations // |kPullingPosVarName| refers to the byte location of the current read. We // declare a variable in the shader to avoid having to reuse Expression diff --git a/src/transform/vertex_pulling_test.cc b/src/transform/vertex_pulling_test.cc index 61a968edb5..6bbb55e334 100644 --- a/src/transform/vertex_pulling_test.cc +++ b/src/transform/vertex_pulling_test.cc @@ -50,6 +50,7 @@ class VertexPullingHelper : public ast::BuilderWithModule { Func("main", ast::VariableList{}, ty.void_, ast::StatementList{}, ast::FunctionDecorationList{ create(ast::PipelineStage::kVertex)}); + mod->AddFunction(func); } @@ -399,20 +400,17 @@ TEST_F(VertexPullingTest, OneAttributeDifferentOutputSet) { TEST_F(VertexPullingTest, ExistingVertexIndexAndInstanceIndex) { InitBasicModule(); - ast::type::F32 f32; - AddVertexInputVariable(0, "var_a", &f32); - AddVertexInputVariable(1, "var_b", &f32); - - ast::type::I32 i32; + AddVertexInputVariable(0, "var_a", ty.f32); + AddVertexInputVariable(1, "var_b", ty.f32); mod->AddGlobalVariable( - Var("custom_vertex_index", ast::StorageClass::kInput, &i32, nullptr, + Var("custom_vertex_index", ast::StorageClass::kInput, ty.i32, nullptr, ast::VariableDecorationList{ create(ast::Builtin::kVertexIdx), })); mod->AddGlobalVariable( - Var("custom_instance_index", ast::StorageClass::kInput, &i32, nullptr, + Var("custom_instance_index", ast::StorageClass::kInput, ty.i32, nullptr, ast::VariableDecorationList{ create(ast::Builtin::kInstanceIdx), })); diff --git a/src/writer/msl/generator_impl_module_constant_test.cc b/src/writer/msl/generator_impl_module_constant_test.cc index 44e39d20fb..56d1fc1a66 100644 --- a/src/writer/msl/generator_impl_module_constant_test.cc +++ b/src/writer/msl/generator_impl_module_constant_test.cc @@ -35,47 +35,22 @@ namespace { using MslGeneratorImplTest = TestHelper; TEST_F(MslGeneratorImplTest, Emit_ModuleConstant) { - ast::type::F32 f32; - ast::type::Array ary(&f32, 3, ast::ArrayDecorationList{}); - - ast::ExpressionList exprs; - exprs.push_back(create( - Source{}, create(Source{}, &f32, 1.0f))); - exprs.push_back(create( - Source{}, create(Source{}, &f32, 2.0f))); - exprs.push_back(create( - Source{}, create(Source{}, &f32, 3.0f))); - - auto* var = - create(Source{}, // source - "pos", // name - ast::StorageClass::kNone, // storage_class - &ary, // type - true, // is_const - create( - Source{}, &ary, exprs), // constructor - ast::VariableDecorationList{}); // decorations + ast::type::Array ary(ty.f32, 3, ast::ArrayDecorationList{}); + auto* var = Const( + "pos", ast::StorageClass::kNone, &ary, + create( + Source{}, &ary, ast::ExpressionList{Expr(1.f), Expr(2.f), Expr(3.f)}), + ast::VariableDecorationList{}); ASSERT_TRUE(gen.EmitProgramConstVariable(var)) << gen.error(); EXPECT_EQ(gen.result(), "constant float pos[3] = {1.0f, 2.0f, 3.0f};\n"); } TEST_F(MslGeneratorImplTest, Emit_SpecConstant) { - ast::type::F32 f32; - - auto* var = create( - Source{}, // source - "pos", // name - ast::StorageClass::kNone, // storage_class - &f32, // type - true, // is_const - create( - Source{}, - create(Source{}, &f32, 3.0f)), // constructor - ast::VariableDecorationList{ - // decorations - create(Source{}, 23), - }); + auto* var = Const("pos", ast::StorageClass::kNone, ty.f32, Expr(3.f), + ast::VariableDecorationList{ + create(Source{}, 23), + }); ASSERT_TRUE(gen.EmitProgramConstVariable(var)) << gen.error(); EXPECT_EQ(gen.result(), "constant float pos [[function_constant(23)]];\n"); diff --git a/src/writer/spirv/builder_function_variable_test.cc b/src/writer/spirv/builder_function_variable_test.cc index 5c26cffe43..1fa6cae2a3 100644 --- a/src/writer/spirv/builder_function_variable_test.cc +++ b/src/writer/spirv/builder_function_variable_test.cc @@ -45,12 +45,10 @@ namespace { using BuilderTest = TestHelper; TEST_F(BuilderTest, FunctionVar_NoStorageClass) { - ast::type::F32 f32; - ast::Variable v(Source{}, "var", ast::StorageClass::kNone, &f32, false, - nullptr, ast::VariableDecorationList{}); + auto* v = Var("var", ast::StorageClass::kNone, ty.f32); b.push_function(Function{}); - EXPECT_TRUE(b.GenerateFunctionVariable(&v)) << b.error(); + EXPECT_TRUE(b.GenerateFunctionVariable(v)) << b.error(); EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "var" )"); EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32 @@ -65,28 +63,17 @@ TEST_F(BuilderTest, FunctionVar_NoStorageClass) { } TEST_F(BuilderTest, FunctionVar_WithConstantConstructor) { - ast::type::F32 f32; - ast::type::Vector vec(&f32, 3); - - ast::ExpressionList vals; - vals.push_back(create( - Source{}, create(Source{}, &f32, 1.0f))); - vals.push_back(create( - Source{}, create(Source{}, &f32, 1.0f))); - vals.push_back(create( - Source{}, create(Source{}, &f32, 3.0f))); - - auto* init = create(Source{}, &vec, vals); - + auto* init = create( + Source{}, ty.vec3(), + ast::ExpressionList{Expr(1.f), Expr(1.f), Expr(3.f)}); EXPECT_TRUE(td.DetermineResultType(init)) << td.error(); - ast::Variable v(Source{}, "var", ast::StorageClass::kOutput, &f32, false, - init, ast::VariableDecorationList{}); - - td.RegisterVariableForTesting(&v); + auto* v = Var("var", ast::StorageClass::kOutput, ty.f32, init, + ast::VariableDecorationList{}); + td.RegisterVariableForTesting(v); b.push_function(Function{}); - EXPECT_TRUE(b.GenerateFunctionVariable(&v)) << b.error(); + EXPECT_TRUE(b.GenerateFunctionVariable(v)) << b.error(); ASSERT_FALSE(b.has_error()) << b.error(); EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %6 "var" @@ -107,31 +94,16 @@ TEST_F(BuilderTest, FunctionVar_WithConstantConstructor) { } TEST_F(BuilderTest, FunctionVar_WithNonConstantConstructor) { - ast::type::F32 f32; - ast::type::Vector vec(&f32, 2); - - auto* rel = create( - Source{}, ast::BinaryOp::kAdd, - create( - Source{}, create(Source{}, &f32, 3.0f)), - create( - Source{}, create(Source{}, &f32, 3.0f))); - - ast::ExpressionList vals; - vals.push_back(create( - Source{}, create(Source{}, &f32, 1.0f))); - vals.push_back(rel); - - auto* init = create(Source{}, &vec, vals); - + auto* init = create( + Source{}, ty.vec2(), ast::ExpressionList{Expr(1.f), Add(3.f, 3.f)}); EXPECT_TRUE(td.DetermineResultType(init)) << td.error(); - ast::Variable v(Source{}, "var", ast::StorageClass::kFunction, &vec, false, - init, ast::VariableDecorationList{}); + auto* v = Var("var", ast::StorageClass::kFunction, ty.vec2(), init, + ast::VariableDecorationList{}); - td.RegisterVariableForTesting(&v); + td.RegisterVariableForTesting(v); b.push_function(Function{}); - EXPECT_TRUE(b.GenerateFunctionVariable(&v)) << b.error(); + EXPECT_TRUE(b.GenerateFunctionVariable(v)) << b.error(); ASSERT_FALSE(b.has_error()) << b.error(); EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %7 "var"