diff --git a/src/BUILD.gn b/src/BUILD.gn index 2696075530..89a3f9e1a3 100644 --- a/src/BUILD.gn +++ b/src/BUILD.gn @@ -382,8 +382,6 @@ libtint_source_set("libtint_core_all_src") { "ast/module.h", "ast/multisampled_texture.cc", "ast/multisampled_texture.h", - "ast/named_type.cc", - "ast/named_type.h", "ast/node.cc", "ast/node.h", "ast/override_decoration.cc", @@ -431,6 +429,8 @@ libtint_source_set("libtint_core_all_src") { "ast/type.h", "ast/type_constructor_expression.cc", "ast/type_constructor_expression.h", + "ast/type_decl.cc", + "ast/type_decl.h", "ast/type_name.cc", "ast/type_name.h", "ast/u32.cc", diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 1d5c6dee24..25c90edac3 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -126,8 +126,6 @@ set(TINT_LIB_SRCS ast/module.h ast/multisampled_texture.cc ast/multisampled_texture.h - ast/named_type.cc - ast/named_type.h ast/node.cc ast/node.h ast/override_decoration.cc @@ -178,6 +176,8 @@ set(TINT_LIB_SRCS ast/type_name.h ast/ast_type.cc # TODO(bclayton) - rename to type.cc ast/type.h + ast/type_decl.cc + ast/type_decl.h ast/type_name.cc ast/type_name.h ast/u32.cc diff --git a/src/ast/alias.h b/src/ast/alias.h index dc2dd8b76e..6e3dd8dd4e 100644 --- a/src/ast/alias.h +++ b/src/ast/alias.h @@ -17,13 +17,13 @@ #include -#include "src/ast/named_type.h" +#include "src/ast/type_decl.h" namespace tint { namespace ast { /// A type alias type. Holds a name and pointer to another type. -class Alias : public Castable { +class Alias : public Castable { public: /// Constructor /// @param program_id the identifier of the program that owns this node diff --git a/src/ast/alias_test.cc b/src/ast/alias_test.cc index e55428d624..8ae7a639b0 100644 --- a/src/ast/alias_test.cc +++ b/src/ast/alias_test.cc @@ -46,7 +46,7 @@ TEST_F(AstAliasTest, Create) { TEST_F(AstAliasTest, TypeName_LinearTime) { Type* type = ty.i32(); for (int i = 0; i < 1024; i++) { - type = Alias(Symbols().New(), type); + type = ty.Of(Alias(Symbols().New(), type)); } for (int i = 0; i < 16384; i++) { type->type_name(); @@ -58,24 +58,6 @@ TEST_F(AstAliasTest, TypeName) { EXPECT_EQ(at->type_name(), "__alias_$1__i32"); } -TEST_F(AstAliasTest, FriendlyName) { - auto* at = Alias("Particle", create()); - EXPECT_EQ(at->FriendlyName(Symbols()), "Particle"); -} - -TEST_F(AstAliasTest, UnwrapAll_TwiceAliasPointerTwiceAlias) { - auto* u32 = create(); - auto* a = create(Sym("a_type"), u32); - auto* aa = create(Sym("aa_type"), a); - auto* paa = create(aa, StorageClass::kUniform, Access::kUndefined); - auto* apaa = create(Sym("paa_type"), paa); - auto* aapaa = create(Sym("aapaa_type"), apaa); - - EXPECT_EQ(aapaa->symbol(), Symbol(4, ID())); - EXPECT_EQ(aapaa->type(), apaa); - EXPECT_EQ(aapaa->UnwrapAll(), u32); -} - } // namespace } // namespace ast } // namespace tint diff --git a/src/ast/ast_type.cc b/src/ast/ast_type.cc index 6702bd9fd6..6dbf38be03 100644 --- a/src/ast/ast_type.cc +++ b/src/ast/ast_type.cc @@ -40,9 +40,7 @@ Type::~Type() = default; Type* Type::UnwrapAll() { auto* type = this; while (true) { - if (auto* alias = type->As()) { - type = alias->type(); - } else if (auto* ptr = type->As()) { + if (auto* ptr = type->As()) { type = ptr->type(); } else { break; diff --git a/src/ast/module.cc b/src/ast/module.cc index 2de195f4d9..fcb7216556 100644 --- a/src/ast/module.cc +++ b/src/ast/module.cc @@ -16,7 +16,7 @@ #include -#include "src/ast/named_type.h" +#include "src/ast/type_decl.h" #include "src/program_builder.h" TINT_INSTANTIATE_TYPEINFO(tint::ast::Module); @@ -36,7 +36,7 @@ Module::Module(ProgramID program_id, continue; } - if (auto* ty = decl->As()) { + if (auto* ty = decl->As()) { constructed_types_.push_back(ty); } else if (auto* func = decl->As()) { functions_.push_back(func); @@ -51,7 +51,7 @@ Module::Module(ProgramID program_id, Module::~Module() = default; -const ast::NamedType* Module::LookupType(Symbol name) const { +const ast::TypeDecl* Module::LookupType(Symbol name) const { for (auto* ty : ConstructedTypes()) { if (ty->name() == name) { return ty; @@ -67,7 +67,7 @@ void Module::AddGlobalVariable(ast::Variable* var) { global_declarations_.push_back(var); } -void Module::AddConstructedType(ast::NamedType* type) { +void Module::AddConstructedType(ast::TypeDecl* type) { TINT_ASSERT(type); constructed_types_.push_back(type); global_declarations_.push_back(type); @@ -92,7 +92,7 @@ void Module::Copy(CloneContext* ctx, const Module* src) { TINT_ICE(ctx->dst->Diagnostics()) << "src global declaration was nullptr"; continue; } - if (auto* ty = decl->As()) { + if (auto* ty = decl->As()) { AddConstructedType(ty); } else if (auto* func = decl->As()) { AddFunction(func); @@ -115,9 +115,6 @@ void Module::to_str(const sem::Info& sem, if (auto* alias = ty->As()) { out << alias->symbol().to_str() << " -> " << alias->type()->type_name() << std::endl; - if (auto* str = alias->type()->As()) { - str->to_str(sem, out, indent); - } } else if (auto* str = ty->As()) { str->to_str(sem, out, indent); } diff --git a/src/ast/module.h b/src/ast/module.h index 2ea6219370..af502cfd0b 100644 --- a/src/ast/module.h +++ b/src/ast/module.h @@ -24,7 +24,7 @@ namespace tint { namespace ast { -class NamedType; +class TypeDecl; /// Module holds the top-level AST types, functions and global variables used by /// a Program. @@ -73,16 +73,16 @@ class Module : public Castable { /// @returns the global variables for the translation unit VariableList& GlobalVariables() { return global_variables_; } - /// Adds a constructed type to the Builder. - /// @param type the constructed type to add - void AddConstructedType(ast::NamedType* type); + /// Adds a type declaration to the Builder. + /// @param decl the constructed type declaration to add + void AddConstructedType(ast::TypeDecl* decl); - /// @returns the NamedType registered as a ConstructedType() + /// @returns the TypeDecl registered as a ConstructedType() /// @param name the name of the type to search for - const ast::NamedType* LookupType(Symbol name) const; + const ast::TypeDecl* LookupType(Symbol name) const; /// @returns the constructed types in the translation unit - const std::vector& ConstructedTypes() const { + const std::vector& ConstructedTypes() const { return constructed_types_; } @@ -118,7 +118,7 @@ class Module : public Castable { private: std::vector global_declarations_; - std::vector constructed_types_; + std::vector constructed_types_; FunctionList functions_; VariableList global_variables_; }; diff --git a/src/ast/struct.h b/src/ast/struct.h index d9fe744a8e..48a7e7e295 100644 --- a/src/ast/struct.h +++ b/src/ast/struct.h @@ -19,14 +19,14 @@ #include #include "src/ast/decoration.h" -#include "src/ast/named_type.h" #include "src/ast/struct_member.h" +#include "src/ast/type_decl.h" namespace tint { namespace ast { /// A struct statement. -class Struct : public Castable { +class Struct : public Castable { public: /// Create a new struct statement /// @param program_id the identifier of the program that owns this node diff --git a/src/ast/struct_test.cc b/src/ast/struct_test.cc index 3e37625cf2..1404ce8f58 100644 --- a/src/ast/struct_test.cc +++ b/src/ast/struct_test.cc @@ -144,13 +144,6 @@ TEST_F(AstStructTest, TypeName) { EXPECT_EQ(s->type_name(), "__struct_$1"); } -TEST_F(AstStructTest, FriendlyName) { - auto name = Sym("my_struct"); - auto* s = - create(name, ast::StructMemberList{}, ast::DecorationList{}); - EXPECT_EQ(s->FriendlyName(Symbols()), "my_struct"); -} - } // namespace } // namespace ast } // namespace tint diff --git a/src/ast/named_type.cc b/src/ast/type_decl.cc similarity index 66% rename from src/ast/named_type.cc rename to src/ast/type_decl.cc index 52d7125371..4337d39a69 100644 --- a/src/ast/named_type.cc +++ b/src/ast/type_decl.cc @@ -12,26 +12,29 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "src/ast/named_type.h" +#include "src/ast/type_decl.h" #include "src/program_builder.h" -TINT_INSTANTIATE_TYPEINFO(tint::ast::NamedType); +TINT_INSTANTIATE_TYPEINFO(tint::ast::TypeDecl); namespace tint { namespace ast { -NamedType::NamedType(ProgramID program_id, const Source& source, Symbol name) +TypeDecl::TypeDecl(ProgramID program_id, const Source& source, Symbol name) : Base(program_id, source), name_(name) { TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(name, program_id); } -NamedType::NamedType(NamedType&&) = default; +TypeDecl::TypeDecl(TypeDecl&&) = default; -NamedType::~NamedType() = default; +TypeDecl::~TypeDecl() = default; -std::string NamedType::FriendlyName(const SymbolTable& symbols) const { - return symbols.NameFor(name()); +void TypeDecl::to_str(const sem::Info&, + std::ostream& out, + size_t indent) const { + make_indent(out, indent); + out << type_name(); } } // namespace ast diff --git a/src/ast/named_type.h b/src/ast/type_decl.h similarity index 53% rename from src/ast/named_type.h rename to src/ast/type_decl.h index c0c157b4ea..c942b0c7f2 100644 --- a/src/ast/named_type.h +++ b/src/ast/type_decl.h @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef SRC_AST_NAMED_TYPE_H_ -#define SRC_AST_NAMED_TYPE_H_ +#ifndef SRC_AST_TYPE_DECL_H_ +#define SRC_AST_TYPE_DECL_H_ #include @@ -22,30 +22,36 @@ namespace tint { namespace ast { -/// The base class for user declared, named types. -class NamedType : public Castable { +/// The base class for type declarations. +class TypeDecl : public Castable { public: /// Create a new struct statement /// @param program_id the identifier of the program that owns this node /// @param source The input source for the import statement /// @param name The name of the structure - NamedType(ProgramID program_id, const Source& source, Symbol name); + TypeDecl(ProgramID program_id, const Source& source, Symbol name); /// Move constructor - NamedType(NamedType&&); + TypeDecl(TypeDecl&&); - ~NamedType() override; + ~TypeDecl() override; - /// @returns the name of the structure + /// @returns the name of the type declaration Symbol name() const { return name_; } - /// @param symbols the program's symbol table - /// @returns the name for this type that closely resembles how it would be - /// declared in WGSL. - std::string FriendlyName(const SymbolTable& symbols) const override; + /// Writes a representation of the node to the output stream + /// @param sem the semantic info for the program + /// @param out the stream to write to + /// @param indent number of spaces to indent the node when writing + void to_str(const sem::Info& sem, + std::ostream& out, + size_t indent) const override; + + /// @returns the name for this type. The type name is unique over all types. + virtual std::string type_name() const = 0; private: - NamedType(const NamedType&) = delete; - NamedType& operator=(const NamedType&) = delete; + TypeDecl(const TypeDecl&) = delete; + TypeDecl& operator=(const TypeDecl&) = delete; Symbol const name_; }; @@ -53,4 +59,4 @@ class NamedType : public Castable { } // namespace ast } // namespace tint -#endif // SRC_AST_NAMED_TYPE_H_ +#endif // SRC_AST_TYPE_DECL_H_ diff --git a/src/inspector/inspector_test.cc b/src/inspector/inspector_test.cc index 2dddfbeec0..994945264f 100644 --- a/src/inspector/inspector_test.cc +++ b/src/inspector/inspector_test.cc @@ -1006,8 +1006,8 @@ TEST_F(InspectorGetEntryPointTest, BuiltInsNotStageVariables) { TEST_F(InspectorGetEntryPointTest, InOutStruct) { auto* interface = MakeInOutStruct("interface", {{"a", 0u}, {"b", 1u}}); - Func("foo", {Param("param", interface)}, interface, {Return("param")}, - {Stage(ast::PipelineStage::kFragment)}); + Func("foo", {Param("param", ty.Of(interface))}, ty.Of(interface), + {Return("param")}, {Stage(ast::PipelineStage::kFragment)}); Inspector& inspector = Build(); auto result = inspector.GetEntryPoints(); @@ -1038,9 +1038,9 @@ TEST_F(InspectorGetEntryPointTest, InOutStruct) { TEST_F(InspectorGetEntryPointTest, MultipleEntryPointsInOutSharedStruct) { auto* interface = MakeInOutStruct("interface", {{"a", 0u}, {"b", 1u}}); - Func("foo", {}, interface, {Return(Construct(interface))}, + Func("foo", {}, ty.Of(interface), {Return(Construct(ty.Of(interface)))}, {Stage(ast::PipelineStage::kFragment)}); - Func("bar", {Param("param", interface)}, ty.void_(), {}, + Func("bar", {Param("param", ty.Of(interface))}, ty.void_(), {}, {Stage(ast::PipelineStage::kFragment)}); Inspector& inspector = Build(); @@ -1078,10 +1078,11 @@ TEST_F(InspectorGetEntryPointTest, MixInOutVariablesAndStruct) { auto* struct_a = MakeInOutStruct("struct_a", {{"a", 0u}, {"b", 1u}}); auto* struct_b = MakeInOutStruct("struct_b", {{"a", 2u}}); Func("foo", - {Param("param_a", struct_a), Param("param_b", struct_b), + {Param("param_a", ty.Of(struct_a)), Param("param_b", ty.Of(struct_b)), Param("param_c", ty.f32(), {Location(3u)}), Param("param_d", ty.f32(), {Location(4u)})}, - struct_a, {Return("param_a")}, {Stage(ast::PipelineStage::kFragment)}); + ty.Of(struct_a), {Return("param_a")}, + {Stage(ast::PipelineStage::kFragment)}); Inspector& inspector = Build(); auto result = inspector.GetEntryPoints(); @@ -1660,7 +1661,7 @@ TEST_F(InspectorGetResourceBindingsTest, Empty) { TEST_F(InspectorGetResourceBindingsTest, Simple) { ast::Struct* ub_struct_type = MakeUniformBufferType("ub_type", {ty.i32()}); - AddUniformBuffer("ub_var", ub_struct_type, 0, 0); + AddUniformBuffer("ub_var", ty.Of(ub_struct_type), 0, 0); MakeStructVariableReferenceBodyFunction("ub_func", "ub_var", {{0, ty.i32()}}); auto sb = MakeStorageBufferTypes("sb_type", {ty.i32()}); @@ -1768,7 +1769,7 @@ TEST_F(InspectorGetUniformBufferResourceBindingsTest, MissingEntryPoint) { TEST_F(InspectorGetUniformBufferResourceBindingsTest, NonEntryPointFunc) { ast::Struct* foo_struct_type = MakeUniformBufferType("foo_type", {ty.i32()}); - AddUniformBuffer("foo_ub", foo_struct_type, 0, 0); + AddUniformBuffer("foo_ub", ty.Of(foo_struct_type), 0, 0); MakeStructVariableReferenceBodyFunction("ub_func", "foo_ub", {{0, ty.i32()}}); @@ -1786,7 +1787,7 @@ TEST_F(InspectorGetUniformBufferResourceBindingsTest, NonEntryPointFunc) { TEST_F(InspectorGetUniformBufferResourceBindingsTest, Simple) { ast::Struct* foo_struct_type = MakeUniformBufferType("foo_type", {ty.i32()}); - AddUniformBuffer("foo_ub", foo_struct_type, 0, 0); + AddUniformBuffer("foo_ub", ty.Of(foo_struct_type), 0, 0); MakeStructVariableReferenceBodyFunction("ub_func", "foo_ub", {{0, ty.i32()}}); @@ -1812,7 +1813,7 @@ TEST_F(InspectorGetUniformBufferResourceBindingsTest, Simple) { TEST_F(InspectorGetUniformBufferResourceBindingsTest, MultipleMembers) { ast::Struct* foo_struct_type = MakeUniformBufferType("foo_type", {ty.i32(), ty.u32(), ty.f32()}); - AddUniformBuffer("foo_ub", foo_struct_type, 0, 0); + AddUniformBuffer("foo_ub", ty.Of(foo_struct_type), 0, 0); MakeStructVariableReferenceBodyFunction( "ub_func", "foo_ub", {{0, ty.i32()}, {1, ty.u32()}, {2, ty.f32()}}); @@ -1839,7 +1840,7 @@ TEST_F(InspectorGetUniformBufferResourceBindingsTest, MultipleMembers) { TEST_F(InspectorGetUniformBufferResourceBindingsTest, ContainingPadding) { ast::Struct* foo_struct_type = MakeUniformBufferType("foo_type", {ty.vec3()}); - AddUniformBuffer("foo_ub", foo_struct_type, 0, 0); + AddUniformBuffer("foo_ub", ty.Of(foo_struct_type), 0, 0); MakeStructVariableReferenceBodyFunction("ub_func", "foo_ub", {{0, ty.vec3()}}); @@ -1866,9 +1867,9 @@ TEST_F(InspectorGetUniformBufferResourceBindingsTest, ContainingPadding) { TEST_F(InspectorGetUniformBufferResourceBindingsTest, MultipleUniformBuffers) { ast::Struct* ub_struct_type = MakeUniformBufferType("ub_type", {ty.i32(), ty.u32(), ty.f32()}); - AddUniformBuffer("ub_foo", ub_struct_type, 0, 0); - AddUniformBuffer("ub_bar", ub_struct_type, 0, 1); - AddUniformBuffer("ub_baz", ub_struct_type, 2, 0); + AddUniformBuffer("ub_foo", ty.Of(ub_struct_type), 0, 0); + AddUniformBuffer("ub_bar", ty.Of(ub_struct_type), 0, 1); + AddUniformBuffer("ub_baz", ty.Of(ub_struct_type), 2, 0); auto AddReferenceFunc = [this](const std::string& func_name, const std::string& var_name) { @@ -1924,7 +1925,7 @@ TEST_F(InspectorGetUniformBufferResourceBindingsTest, ContainingArray) { // and will need to be fixed. ast::Struct* foo_struct_type = MakeUniformBufferType("foo_type", {ty.i32(), ty.array()}); - AddUniformBuffer("foo_ub", foo_struct_type, 0, 0); + AddUniformBuffer("foo_ub", ty.Of(foo_struct_type), 0, 0); MakeStructVariableReferenceBodyFunction("ub_func", "foo_ub", {{0, ty.i32()}}); diff --git a/src/program.cc b/src/program.cc index dff561617b..7a0135b507 100644 --- a/src/program.cc +++ b/src/program.cc @@ -110,6 +110,10 @@ const sem::Type* Program::TypeOf(const ast::Type* type) const { return Sem().Get(type); } +const sem::Type* Program::TypeOf(const ast::TypeDecl* type_decl) const { + return Sem().Get(type_decl); +} + std::string Program::to_str(bool demangle) const { AssertNotMoved(); auto str = ast_->to_str(Sem()); diff --git a/src/program.h b/src/program.h index c321e35a40..d2c5f827ea 100644 --- a/src/program.h +++ b/src/program.h @@ -132,10 +132,17 @@ class Program { sem::Type* TypeOf(const ast::Expression* expr) const; /// Helper for returning the resolved semantic type of the AST type `type`. - /// @param expr the AST type + /// @param type the AST type /// @return the resolved semantic type for the type, or nullptr if the type /// has no resolved type. - const sem::Type* TypeOf(const ast::Type* expr) const; + const sem::Type* TypeOf(const ast::Type* type) const; + + /// Helper for returning the resolved semantic type of the AST type + /// declaration `type_decl`. + /// @param type_decl the AST type declaration + /// @return the resolved semantic type for the type declaration, or nullptr if + /// the type declaration has no resolved type. + const sem::Type* TypeOf(const ast::TypeDecl* type_decl) const; /// @param demangle whether to automatically demangle the symbols in the /// returned string diff --git a/src/program_builder.cc b/src/program_builder.cc index 9a2cb09134..bdb57263fa 100644 --- a/src/program_builder.cc +++ b/src/program_builder.cc @@ -102,20 +102,17 @@ const sem::Type* ProgramBuilder::TypeOf(const ast::Type* type) const { return Sem().Get(type); } -ast::Type* ProgramBuilder::TypesBuilder::MaybeCreateTypename( - ast::Type* type) const { - if (auto* nt = As(type)) { - return type_name(nt->name()); - } - return type; +const sem::Type* ProgramBuilder::TypeOf(const ast::TypeDecl* type_decl) const { + return Sem().Get(type_decl); } -const ast::Type* ProgramBuilder::TypesBuilder::MaybeCreateTypename( - const ast::Type* type) const { - if (auto* nt = As(type)) { - return type_name(nt->name()); - } - return type; +ast::TypeName* ProgramBuilder::TypesBuilder::Of(ast::TypeDecl* decl) const { + return type_name(decl->name()); +} + +const ast::TypeName* ProgramBuilder::TypesBuilder::Of( + const ast::TypeDecl* decl) const { + return type_name(decl->name()); } ProgramBuilder::TypesBuilder::TypesBuilder(ProgramBuilder* pb) : builder(pb) {} diff --git a/src/program_builder.h b/src/program_builder.h index 4a9c3d0272..62c4ee80fa 100644 --- a/src/program_builder.h +++ b/src/program_builder.h @@ -434,7 +434,6 @@ class ProgramBuilder { /// @param n vector width in elements /// @return the tint AST type for a `n`-element vector of `type`. ast::Vector* vec(ast::Type* type, uint32_t n) const { - type = MaybeCreateTypename(type); return builder->create(type, n); } @@ -443,7 +442,6 @@ class ProgramBuilder { /// @param n vector width in elements /// @return the tint AST type for a `n`-element vector of `type`. ast::Vector* vec(const Source& source, ast::Type* type, uint32_t n) const { - type = MaybeCreateTypename(type); return builder->create(source, type, n); } @@ -489,7 +487,6 @@ class ProgramBuilder { /// @param rows number of rows for the matrix /// @return the tint AST type for a matrix of `type` ast::Matrix* mat(ast::Type* type, uint32_t columns, uint32_t rows) const { - type = MaybeCreateTypename(type); return builder->create(type, rows, columns); } @@ -610,7 +607,6 @@ class ProgramBuilder { ast::Array* array(ast::Type* subtype, uint32_t n = 0, ast::DecorationList decos = {}) const { - subtype = MaybeCreateTypename(subtype); return builder->create(subtype, n, decos); } @@ -623,7 +619,6 @@ class ProgramBuilder { ast::Type* subtype, uint32_t n = 0, ast::DecorationList decos = {}) const { - subtype = MaybeCreateTypename(subtype); return builder->create(source, subtype, n, decos); } @@ -632,7 +627,6 @@ class ProgramBuilder { /// @param stride the array stride. 0 represents implicit stride /// @return the tint AST type for a array of size `n` of type `T` ast::Array* array(ast::Type* subtype, uint32_t n, uint32_t stride) const { - subtype = MaybeCreateTypename(subtype); ast::DecorationList decos; if (stride) { decos.emplace_back(builder->create(stride)); @@ -649,7 +643,6 @@ class ProgramBuilder { ast::Type* subtype, uint32_t n, uint32_t stride) const { - subtype = MaybeCreateTypename(subtype); ast::DecorationList decos; if (stride) { decos.emplace_back(builder->create(stride)); @@ -695,7 +688,6 @@ class ProgramBuilder { /// @returns the alias pointer template ast::Alias* alias(NAME&& name, ast::Type* type) const { - type = MaybeCreateTypename(type); auto sym = builder->Sym(std::forward(name)); return builder->create(sym, type); } @@ -709,7 +701,6 @@ class ProgramBuilder { ast::Alias* alias(const Source& source, NAME&& name, ast::Type* type) const { - type = MaybeCreateTypename(type); auto sym = builder->Sym(std::forward(name)); return builder->create(source, sym, type); } @@ -721,7 +712,6 @@ class ProgramBuilder { ast::Pointer* pointer(ast::Type* type, ast::StorageClass storage_class, ast::Access access = ast::Access::kUndefined) const { - type = MaybeCreateTypename(type); return builder->create(type, storage_class, access); } @@ -734,7 +724,6 @@ class ProgramBuilder { ast::Type* type, ast::StorageClass storage_class, ast::Access access = ast::Access::kUndefined) const { - type = MaybeCreateTypename(type); return builder->create(source, type, storage_class, access); } @@ -848,17 +837,15 @@ class ProgramBuilder { } /// [DEPRECATED]: TODO(crbug.com/tint/745): Migrate to const AST pointers. - /// If ty is a ast::Struct or ast::Alias, the returned type is an - /// ast::TypeName of the given type's name, otherwise type is returned. + /// Constructs a TypeName for the type declaration. /// @param type the type /// @return either type or a pointer to a new ast::TypeName - ast::Type* MaybeCreateTypename(ast::Type* type) const; + ast::TypeName* Of(ast::TypeDecl* type) const; - /// If ty is a ast::Struct or ast::Alias, the returned type is an - /// ast::TypeName of the given type's name, otherwise type is returned. + /// Constructs a TypeName for the type declaration. /// @param type the type /// @return either type or a pointer to a new ast::TypeName - const ast::Type* MaybeCreateTypename(const ast::Type* type) const; + const ast::TypeName* Of(const ast::TypeDecl* type) const; /// The ProgramBuilder ProgramBuilder* const builder; @@ -1073,7 +1060,6 @@ class ProgramBuilder { /// values `args`. template ast::TypeConstructorExpression* Construct(ast::Type* type, ARGS&&... args) { - type = ty.MaybeCreateTypename(type); return create( type, ExprList(std::forward(args)...)); } @@ -1210,7 +1196,6 @@ class ProgramBuilder { ast::Variable* Var(NAME&& name, const ast::Type* type, OPTIONAL&&... optional) { - type = ty.MaybeCreateTypename(type); VarOptionals opts(std::forward(optional)...); return create(Sym(std::forward(name)), opts.storage, opts.access, type, false, opts.constructor, @@ -1234,7 +1219,6 @@ class ProgramBuilder { NAME&& name, const ast::Type* type, OPTIONAL&&... optional) { - type = ty.MaybeCreateTypename(type); VarOptionals opts(std::forward(optional)...); return create(source, Sym(std::forward(name)), opts.storage, opts.access, type, false, @@ -1251,7 +1235,6 @@ class ProgramBuilder { ast::Type* type, ast::Expression* constructor, ast::DecorationList decorations = {}) { - type = ty.MaybeCreateTypename(type); return create( Sym(std::forward(name)), ast::StorageClass::kNone, ast::Access::kUndefined, type, true, constructor, decorations); @@ -1269,7 +1252,6 @@ class ProgramBuilder { ast::Type* type, ast::Expression* constructor, ast::DecorationList decorations = {}) { - type = ty.MaybeCreateTypename(type); return create( source, Sym(std::forward(name)), ast::StorageClass::kNone, ast::Access::kUndefined, type, true, constructor, decorations); @@ -1283,7 +1265,6 @@ class ProgramBuilder { ast::Variable* Param(NAME&& name, ast::Type* type, ast::DecorationList decorations = {}) { - type = ty.MaybeCreateTypename(type); return create( Sym(std::forward(name)), ast::StorageClass::kNone, ast::Access::kUndefined, type, true, nullptr, decorations); @@ -1299,7 +1280,6 @@ class ProgramBuilder { NAME&& name, ast::Type* type, ast::DecorationList decorations = {}) { - type = ty.MaybeCreateTypename(type); return create( source, Sym(std::forward(name)), ast::StorageClass::kNone, ast::Access::kUndefined, type, true, nullptr, decorations); @@ -1559,7 +1539,6 @@ class ProgramBuilder { ast::StatementList body, ast::DecorationList decorations = {}, ast::DecorationList return_type_decorations = {}) { - type = ty.MaybeCreateTypename(type); auto* func = create(source, Sym(std::forward(name)), params, type, create(body), @@ -1584,7 +1563,6 @@ class ProgramBuilder { ast::StatementList body, ast::DecorationList decorations = {}, ast::DecorationList return_type_decorations = {}) { - type = ty.MaybeCreateTypename(type); auto* func = create(Sym(std::forward(name)), params, type, create(body), decorations, return_type_decorations); @@ -1688,7 +1666,6 @@ class ProgramBuilder { NAME&& name, ast::Type* type, ast::DecorationList decorations = {}) { - type = ty.MaybeCreateTypename(type); return create(source, Sym(std::forward(name)), type, std::move(decorations)); } @@ -1702,7 +1679,6 @@ class ProgramBuilder { ast::StructMember* Member(NAME&& name, ast::Type* type, ast::DecorationList decorations = {}) { - type = ty.MaybeCreateTypename(type); return create(source_, Sym(std::forward(name)), type, std::move(decorations)); } @@ -1714,7 +1690,6 @@ class ProgramBuilder { /// @returns the struct member pointer template ast::StructMember* Member(uint32_t offset, NAME&& name, ast::Type* type) { - type = ty.MaybeCreateTypename(type); return create( source_, Sym(std::forward(name)), type, ast::DecorationList{ @@ -1978,10 +1953,19 @@ class ProgramBuilder { /// Helper for returning the resolved semantic type of the AST type `type`. /// @note As the Resolver is run when the Program is built, this will only be /// useful for the Resolver itself and tests that use their own Resolver. - /// @param expr the AST type + /// @param type the AST type /// @return the resolved semantic type for the type, or nullptr if the type /// has no resolved type. - const sem::Type* TypeOf(const ast::Type* expr) const; + const sem::Type* TypeOf(const ast::Type* type) const; + + /// Helper for returning the resolved semantic type of the AST type + /// declaration `type_decl`. + /// @note As the Resolver is run when the Program is built, this will only be + /// useful for the Resolver itself and tests that use their own Resolver. + /// @param type_decl the AST type declaration + /// @return the resolved semantic type for the type declaration, or nullptr if + /// the type declaration has no resolved type. + const sem::Type* TypeOf(const ast::TypeDecl* type_decl) const; /// Wraps the ast::Literal in a statement. This is used by tests that /// construct a partial AST and require the Resolver to reach these diff --git a/src/reader/spirv/function.cc b/src/reader/spirv/function.cc index 4e7c4fab61..132f91f8e9 100644 --- a/src/reader/spirv/function.cc +++ b/src/reader/spirv/function.cc @@ -1042,10 +1042,10 @@ bool FunctionEmitter::EmitEntryPointAsWrapper() { } // Create and register the result type. - return_type = create(Source{}, return_struct_sym, - return_members, ast::DecorationList{}); - parser_impl_.AddConstructedType(return_struct_sym, - return_type->As()); + auto* str = create(Source{}, return_struct_sym, return_members, + ast::DecorationList{}); + parser_impl_.AddConstructedType(return_struct_sym, str); + return_type = builder_.ty.Of(str); // Add the return-value statement. stmts.push_back(create( diff --git a/src/reader/spirv/parser_impl.cc b/src/reader/spirv/parser_impl.cc index 70dd7eba14..f262de955e 100644 --- a/src/reader/spirv/parser_impl.cc +++ b/src/reader/spirv/parser_impl.cc @@ -1037,10 +1037,10 @@ const Type* ParserImpl::ConvertType( return ty_.Struct(sym, std::move(ast_member_types)); } -void ParserImpl::AddConstructedType(Symbol name, ast::NamedType* type) { +void ParserImpl::AddConstructedType(Symbol name, ast::TypeDecl* decl) { auto iter = constructed_types_.insert(name); if (iter.second) { - builder_.AST().AddConstructedType(type); + builder_.AST().AddConstructedType(decl); } } diff --git a/src/reader/spirv/parser_impl.h b/src/reader/spirv/parser_impl.h index 14250fc9f0..d6d2ccfd65 100644 --- a/src/reader/spirv/parser_impl.h +++ b/src/reader/spirv/parser_impl.h @@ -192,8 +192,8 @@ class ParserImpl : Reader { /// Adds `type` as a constructed type if it hasn't been added yet. /// @param name the type's unique name - /// @param type the type to add - void AddConstructedType(Symbol name, ast::NamedType* type); + /// @param decl the type declaration to add + void AddConstructedType(Symbol name, ast::TypeDecl* decl); /// @returns the fail stream object FailStream& fail_stream() { return fail_stream_; } diff --git a/src/reader/spirv/parser_impl_module_var_test.cc b/src/reader/spirv/parser_impl_module_var_test.cc index dcead04ca1..60eb5ef912 100644 --- a/src/reader/spirv/parser_impl_module_var_test.cc +++ b/src/reader/spirv/parser_impl_module_var_test.cc @@ -4085,7 +4085,7 @@ TEST_F(SpvModuleScopeVarParserTest, EntryPointWrapping_IOLocations) { { Return{} } - Function main -> __struct_main_out + Function main -> __type_name_main_out StageDecoration{vertex} ( VariableConst{ @@ -4124,7 +4124,7 @@ TEST_F(SpvModuleScopeVarParserTest, EntryPointWrapping_IOLocations) { Return{ { TypeConstructor[not set]{ - __struct_main_out + __type_name_main_out Identifier[not set]{x_2} Identifier[not set]{x_4} } diff --git a/src/reader/wgsl/parser_impl.cc b/src/reader/wgsl/parser_impl.cc index 6d625fd999..aa52558e91 100644 --- a/src/reader/wgsl/parser_impl.cc +++ b/src/reader/wgsl/parser_impl.cc @@ -313,11 +313,11 @@ Token ParserImpl::last_token() const { } void ParserImpl::register_constructed(const std::string& name, - const ast::Type* type) { - registered_constructs_[name] = type; + const ast::TypeDecl* type_decl) { + registered_constructs_[name] = type_decl; } -const ast::Type* ParserImpl::get_constructed(const std::string& name) { +const ast::TypeDecl* ParserImpl::get_constructed(const std::string& name) { if (registered_constructs_.find(name) == registered_constructs_.end()) { return nullptr; } diff --git a/src/reader/wgsl/parser_impl.h b/src/reader/wgsl/parser_impl.h index afbcce1e9d..dce66628c2 100644 --- a/src/reader/wgsl/parser_impl.h +++ b/src/reader/wgsl/parser_impl.h @@ -381,13 +381,14 @@ class ParserImpl { /// Registers a constructed type into the parser /// TODO(crbug.com/tint/724): Remove /// @param name the constructed name - /// @param type the constructed type - void register_constructed(const std::string& name, const ast::Type* type); + /// @param type_decl the constructed type declaration + void register_constructed(const std::string& name, + const ast::TypeDecl* type_decl); /// Retrieves a constructed type /// TODO(crbug.com/tint/724): Remove /// @param name The name to lookup /// @returns the constructed type for `name` or `nullptr` if not found - const ast::Type* get_constructed(const std::string& name); + const ast::TypeDecl* get_constructed(const std::string& name); /// Parses the `translation_unit` grammar element void translation_unit(); @@ -881,7 +882,7 @@ class ParserImpl { uint32_t sync_depth_ = 0; std::vector sync_tokens_; int silence_errors_ = 0; - std::unordered_map registered_constructs_; + std::unordered_map registered_constructs_; ProgramBuilder builder_; size_t max_errors_ = 25; }; diff --git a/src/reader/wgsl/parser_impl_test.cc b/src/reader/wgsl/parser_impl_test.cc index 3c415f6d17..632d013f5a 100644 --- a/src/reader/wgsl/parser_impl_test.cc +++ b/src/reader/wgsl/parser_impl_test.cc @@ -53,11 +53,11 @@ fn main() -> { // missing return type TEST_F(ParserImplTest, GetRegisteredType) { auto p = parser(""); - p->register_constructed("my_alias", ty.i32()); + auto* alias = create(Sym("my_alias"), ty.i32()); + p->register_constructed("my_alias", alias); - auto* alias = p->get_constructed("my_alias"); - ASSERT_NE(alias, nullptr); - EXPECT_TRUE(alias->Is()); + auto* got = p->get_constructed("my_alias"); + EXPECT_EQ(got, alias); } TEST_F(ParserImplTest, GetUnregisteredType) { diff --git a/src/resolver/assignment_validation_test.cc b/src/resolver/assignment_validation_test.cc index 7c124b4864..5879712f41 100644 --- a/src/resolver/assignment_validation_test.cc +++ b/src/resolver/assignment_validation_test.cc @@ -112,7 +112,7 @@ TEST_F(ResolverAssignmentValidationTest, // var a : myint = 2; // a = 2 auto* myint = Alias("myint", ty.i32()); - auto* var = Var("a", myint, ast::StorageClass::kNone, Expr(2)); + auto* var = Var("a", ty.Of(myint), ast::StorageClass::kNone, Expr(2)); WrapInFunction(var, Assign(Source{{12, 34}}, "a", 2)); EXPECT_TRUE(r()->Resolve()) << r()->error(); diff --git a/src/resolver/control_block_validation_test.cc b/src/resolver/control_block_validation_test.cc index a7e7847e83..9a2c7162a1 100644 --- a/src/resolver/control_block_validation_test.cc +++ b/src/resolver/control_block_validation_test.cc @@ -295,7 +295,7 @@ TEST_F(ResolverControlBlockValidationTest, SwitchCaseAlias_Pass) { // } auto* my_int = Alias("MyInt", ty.u32()); - auto* var = Var("a", my_int, ast::StorageClass::kNone, Expr(2u)); + auto* var = Var("a", ty.Of(my_int), ast::StorageClass::kNone, Expr(2u)); ast::CaseSelectorList default_csl; auto* block_default = Block(); diff --git a/src/resolver/decoration_validation_test.cc b/src/resolver/decoration_validation_test.cc index 614fec94a3..a5ca656685 100644 --- a/src/resolver/decoration_validation_test.cc +++ b/src/resolver/decoration_validation_test.cc @@ -509,7 +509,7 @@ using StructBlockTest = ResolverTest; TEST_F(StructBlockTest, StructUsedAsArrayElement) { auto* s = Structure("S", {Member("x", ty.i32())}, {create()}); - auto* a = ty.array(s, 4); + auto* a = ty.array(ty.Of(s), 4); Global("G", a, ast::StorageClass::kPrivate); EXPECT_FALSE(r()->Resolve()); @@ -528,7 +528,7 @@ using ResourceDecorationTest = ResolverTest; TEST_F(ResourceDecorationTest, UniformBufferMissingBinding) { auto* s = Structure("S", {Member("x", ty.i32())}, {create()}); - Global(Source{{12, 34}}, "G", s, ast::StorageClass::kUniform); + Global(Source{{12, 34}}, "G", ty.Of(s), ast::StorageClass::kUniform); EXPECT_FALSE(r()->Resolve()); EXPECT_EQ(r()->error(), @@ -539,7 +539,7 @@ TEST_F(ResourceDecorationTest, UniformBufferMissingBinding) { TEST_F(ResourceDecorationTest, StorageBufferMissingBinding) { auto* s = Structure("S", {Member("x", ty.i32())}, {create()}); - Global(Source{{12, 34}}, "G", s, ast::StorageClass::kStorage, + Global(Source{{12, 34}}, "G", ty.Of(s), ast::StorageClass::kStorage, ast::Access::kRead); EXPECT_FALSE(r()->Resolve()); diff --git a/src/resolver/entry_point_validation_test.cc b/src/resolver/entry_point_validation_test.cc index 80d910bd8c..8f90a224bf 100644 --- a/src/resolver/entry_point_validation_test.cc +++ b/src/resolver/entry_point_validation_test.cc @@ -108,8 +108,9 @@ TEST_F(ResolverEntryPointValidationTest, ReturnTypeAttribute_Struct) { // return Output(); // } auto* output = Structure("Output", {}); - Func(Source{{12, 34}}, "main", {}, output, {Return(Construct(output))}, - {Stage(ast::PipelineStage::kVertex)}, {Location(Source{{13, 43}}, 0)}); + Func(Source{{12, 34}}, "main", {}, ty.Of(output), + {Return(Construct(ty.Of(output)))}, {Stage(ast::PipelineStage::kVertex)}, + {Location(Source{{13, 43}}, 0)}); EXPECT_FALSE(r()->Resolve()); EXPECT_EQ( @@ -130,7 +131,8 @@ TEST_F(ResolverEntryPointValidationTest, ReturnType_Struct_Valid) { auto* output = Structure( "Output", {Member("a", ty.f32(), {Location(0)}), Member("b", ty.f32(), {Builtin(ast::Builtin::kFragDepth)})}); - Func(Source{{12, 34}}, "main", {}, output, {Return(Construct(output))}, + Func(Source{{12, 34}}, "main", {}, ty.Of(output), + {Return(Construct(ty.Of(output)))}, {Stage(ast::PipelineStage::kFragment)}); EXPECT_TRUE(r()->Resolve()) << r()->error(); @@ -150,7 +152,8 @@ TEST_F(ResolverEntryPointValidationTest, {Member("a", ty.f32(), {Location(Source{{13, 43}}, 0), Builtin(Source{{14, 52}}, ast::Builtin::kFragDepth)})}); - Func(Source{{12, 34}}, "main", {}, output, {Return(Construct(output))}, + Func(Source{{12, 34}}, "main", {}, ty.Of(output), + {Return(Construct(ty.Of(output)))}, {Stage(ast::PipelineStage::kFragment)}); EXPECT_FALSE(r()->Resolve()); @@ -172,7 +175,8 @@ TEST_F(ResolverEntryPointValidationTest, auto* output = Structure( "Output", {Member(Source{{13, 43}}, "a", ty.f32(), {Location(0)}), Member(Source{{14, 52}}, "b", ty.f32(), {})}); - Func(Source{{12, 34}}, "main", {}, output, {Return(Construct(output))}, + Func(Source{{12, 34}}, "main", {}, ty.Of(output), + {Return(Construct(ty.Of(output)))}, {Stage(ast::PipelineStage::kFragment)}); EXPECT_FALSE(r()->Resolve()); @@ -195,8 +199,9 @@ TEST_F(ResolverEntryPointValidationTest, ReturnType_Struct_NestedStruct) { auto* inner = Structure( "Inner", {Member(Source{{13, 43}}, "a", ty.f32(), {Location(0)})}); auto* output = Structure( - "Output", {Member(Source{{14, 52}}, "a", inner, {Location(0)})}); - Func(Source{{12, 34}}, "main", {}, output, {Return(Construct(output))}, + "Output", {Member(Source{{14, 52}}, "a", ty.Of(inner), {Location(0)})}); + Func(Source{{12, 34}}, "main", {}, ty.Of(output), + {Return(Construct(ty.Of(output)))}, {Stage(ast::PipelineStage::kFragment)}); EXPECT_FALSE(r()->Resolve()); @@ -219,7 +224,8 @@ TEST_F(ResolverEntryPointValidationTest, ReturnType_Struct_RuntimeArray) { "Output", {Member(Source{{13, 43}}, "a", ty.array(), {Location(0)})}, {create()}); - Func(Source{{12, 34}}, "main", {}, output, {Return(Construct(output))}, + Func(Source{{12, 34}}, "main", {}, ty.Of(output), + {Return(Construct(ty.Of(output)))}, {Stage(ast::PipelineStage::kFragment)}); EXPECT_FALSE(r()->Resolve()); @@ -241,7 +247,8 @@ TEST_F(ResolverEntryPointValidationTest, ReturnType_Struct_DuplicateBuiltins) { auto* output = Structure( "Output", {Member("a", ty.f32(), {Builtin(ast::Builtin::kFragDepth)}), Member("b", ty.f32(), {Builtin(ast::Builtin::kFragDepth)})}); - Func(Source{{12, 34}}, "main", {}, output, {Return(Construct(output))}, + Func(Source{{12, 34}}, "main", {}, ty.Of(output), + {Return(Construct(ty.Of(output)))}, {Stage(ast::PipelineStage::kFragment)}); EXPECT_FALSE(r()->Resolve()); @@ -262,7 +269,8 @@ TEST_F(ResolverEntryPointValidationTest, ReturnType_Struct_DuplicateLocation) { // } auto* output = Structure("Output", {Member("a", ty.f32(), {Location(1)}), Member("b", ty.f32(), {Location(1)})}); - Func(Source{{12, 34}}, "main", {}, output, {Return(Construct(output))}, + Func(Source{{12, 34}}, "main", {}, ty.Of(output), + {Return(Construct(ty.Of(output)))}, {Stage(ast::PipelineStage::kFragment)}); EXPECT_FALSE(r()->Resolve()); @@ -325,7 +333,7 @@ TEST_F(ResolverEntryPointValidationTest, ParameterAttribute_Struct) { // [[stage(fragment)]] // fn main([[location(0)]] param : Input) {} auto* input = Structure("Input", {}); - auto* param = Param("param", input, {Location(Source{{13, 43}}, 0)}); + auto* param = Param("param", ty.Of(input), {Location(Source{{13, 43}}, 0)}); Func(Source{{12, 34}}, "main", {param}, ty.void_(), {}, {Stage(ast::PipelineStage::kFragment)}); @@ -346,7 +354,7 @@ TEST_F(ResolverEntryPointValidationTest, Parameter_Struct_Valid) { auto* input = Structure( "Input", {Member("a", ty.f32(), {Location(0)}), Member("b", ty.u32(), {Builtin(ast::Builtin::kSampleIndex)})}); - auto* param = Param("param", input); + auto* param = Param("param", ty.Of(input)); Func(Source{{12, 34}}, "main", {param}, ty.void_(), {}, {Stage(ast::PipelineStage::kFragment)}); @@ -365,7 +373,7 @@ TEST_F(ResolverEntryPointValidationTest, {Member("a", ty.f32(), {Location(Source{{13, 43}}, 0), Builtin(Source{{14, 52}}, ast::Builtin::kSampleIndex)})}); - auto* param = Param("param", input); + auto* param = Param("param", ty.Of(input)); Func(Source{{12, 34}}, "main", {param}, ty.void_(), {}, {Stage(ast::PipelineStage::kFragment)}); @@ -386,7 +394,7 @@ TEST_F(ResolverEntryPointValidationTest, auto* input = Structure( "Input", {Member(Source{{13, 43}}, "a", ty.f32(), {Location(0)}), Member(Source{{14, 52}}, "b", ty.f32(), {})}); - auto* param = Param("param", input); + auto* param = Param("param", ty.Of(input)); Func(Source{{12, 34}}, "main", {param}, ty.void_(), {}, {Stage(ast::PipelineStage::kFragment)}); @@ -406,9 +414,9 @@ TEST_F(ResolverEntryPointValidationTest, Parameter_Struct_NestedStruct) { // fn main(param : Input) {} auto* inner = Structure( "Inner", {Member(Source{{13, 43}}, "a", ty.f32(), {Location(0)})}); - auto* input = - Structure("Input", {Member(Source{{14, 52}}, "a", inner, {Location(0)})}); - auto* param = Param("param", input); + auto* input = Structure( + "Input", {Member(Source{{14, 52}}, "a", ty.Of(inner), {Location(0)})}); + auto* param = Param("param", ty.Of(input)); Func(Source{{12, 34}}, "main", {param}, ty.void_(), {}, {Stage(ast::PipelineStage::kFragment)}); @@ -430,7 +438,7 @@ TEST_F(ResolverEntryPointValidationTest, Parameter_Struct_RuntimeArray) { "Input", {Member(Source{{13, 43}}, "a", ty.array(), {Location(0)})}, {create()}); - auto* param = Param("param", input); + auto* param = Param("param", ty.Of(input)); Func(Source{{12, 34}}, "main", {param}, ty.void_(), {}, {Stage(ast::PipelineStage::kFragment)}); @@ -472,8 +480,8 @@ TEST_F(ResolverEntryPointValidationTest, Parameter_Struct_DuplicateBuiltins) { "InputA", {Member("a", ty.u32(), {Builtin(ast::Builtin::kSampleIndex)})}); auto* input_b = Structure( "InputB", {Member("a", ty.u32(), {Builtin(ast::Builtin::kSampleIndex)})}); - auto* param_a = Param("param_a", input_a); - auto* param_b = Param("param_b", input_b); + auto* param_a = Param("param_a", ty.Of(input_a)); + auto* param_b = Param("param_b", ty.Of(input_b)); Func(Source{{12, 34}}, "main", {param_a, param_b}, ty.void_(), {}, {Stage(ast::PipelineStage::kFragment)}); @@ -510,8 +518,8 @@ TEST_F(ResolverEntryPointValidationTest, Parameter_Struct_DuplicateLocation) { // fn main(param_a : InputA, param_b : InputB) {} auto* input_a = Structure("InputA", {Member("a", ty.f32(), {Location(1)})}); auto* input_b = Structure("InputB", {Member("a", ty.f32(), {Location(1)})}); - auto* param_a = Param("param_a", input_a); - auto* param_b = Param("param_b", input_b); + auto* param_a = Param("param_a", ty.Of(input_a)); + auto* param_b = Param("param_b", ty.Of(input_b)); Func(Source{{12, 34}}, "main", {param_a, param_b}, ty.void_(), {}, {Stage(ast::PipelineStage::kFragment)}); @@ -599,7 +607,7 @@ TEST_P(TypeValidationTest, StructInputs) { auto params = GetParam(); auto* input = Structure( "Input", {Member("a", params.create_ast_type(*this), {Location(0)})}); - auto* a = Param("a", input, {}); + auto* a = Param("a", ty.Of(input), {}); Func(Source{{12, 34}}, "main", {a}, ty.void_(), {}, {Stage(ast::PipelineStage::kFragment)}); @@ -638,7 +646,8 @@ TEST_P(TypeValidationTest, StructOutputs) { auto params = GetParam(); auto* output = Structure( "Output", {Member("a", params.create_ast_type(*this), {Location(0)})}); - Func(Source{{12, 34}}, "main", {}, output, {Return(Construct(output))}, + Func(Source{{12, 34}}, "main", {}, ty.Of(output), + {Return(Construct(ty.Of(output)))}, {Stage(ast::PipelineStage::kFragment)}); if (params.is_valid) { diff --git a/src/resolver/function_validation_test.cc b/src/resolver/function_validation_test.cc index 94e67919db..cc72217246 100644 --- a/src/resolver/function_validation_test.cc +++ b/src/resolver/function_validation_test.cc @@ -231,7 +231,7 @@ TEST_F(ResolverFunctionValidationTest, // type myf32 = f32; // fn func -> myf32 { return 2.0; } auto* myf32 = Alias("myf32", ty.f32()); - Func("func", ast::VariableList{}, myf32, + Func("func", ast::VariableList{}, ty.Of(myf32), ast::StatementList{ Return(Source{Source::Location{12, 34}}, Expr(2.f)), }, @@ -245,7 +245,7 @@ TEST_F(ResolverFunctionValidationTest, // type myf32 = f32; // fn func -> myf32 { return 2; } auto* myf32 = Alias("myf32", ty.f32()); - Func("func", ast::VariableList{}, myf32, + Func("func", ast::VariableList{}, ty.Of(myf32), ast::StatementList{ Return(Source{Source::Location{12, 34}}, Expr(2u)), }, diff --git a/src/resolver/host_shareable_validation_test.cc b/src/resolver/host_shareable_validation_test.cc index e783f1d89a..680a625e33 100644 --- a/src/resolver/host_shareable_validation_test.cc +++ b/src/resolver/host_shareable_validation_test.cc @@ -29,7 +29,7 @@ TEST_F(ResolverHostShareableValidationTest, BoolMember) { auto* s = Structure("S", {Member(Source{{12, 34}}, "x", ty.bool_())}, {create()}); - Global(Source{{56, 78}}, "g", s, ast::StorageClass::kStorage, + Global(Source{{56, 78}}, "g", ty.Of(s), ast::StorageClass::kStorage, ast::Access::kRead, ast::DecorationList{ create(0), @@ -49,7 +49,7 @@ TEST_F(ResolverHostShareableValidationTest, BoolVectorMember) { auto* s = Structure("S", {Member(Source{{12, 34}}, "x", ty.vec3())}, {create()}); - Global(Source{{56, 78}}, "g", s, ast::StorageClass::kStorage, + Global(Source{{56, 78}}, "g", ty.Of(s), ast::StorageClass::kStorage, ast::Access::kRead, ast::DecorationList{ create(0), @@ -67,10 +67,10 @@ TEST_F(ResolverHostShareableValidationTest, BoolVectorMember) { TEST_F(ResolverHostShareableValidationTest, Aliases) { auto* a1 = Alias("a1", ty.bool_()); - auto* s = Structure("S", {Member(Source{{12, 34}}, "x", a1)}, + auto* s = Structure("S", {Member(Source{{12, 34}}, "x", ty.Of(a1))}, {create()}); - auto* a2 = Alias("a2", s); - Global(Source{{56, 78}}, "g", a2, ast::StorageClass::kStorage, + auto* a2 = Alias("a2", ty.Of(s)); + Global(Source{{56, 78}}, "g", ty.Of(a2), ast::StorageClass::kStorage, ast::Access::kRead, ast::DecorationList{ create(0), @@ -88,13 +88,13 @@ TEST_F(ResolverHostShareableValidationTest, Aliases) { TEST_F(ResolverHostShareableValidationTest, NestedStructures) { auto* i1 = Structure("I1", {Member(Source{{1, 2}}, "x", ty.bool_())}); - auto* i2 = Structure("I2", {Member(Source{{3, 4}}, "y", i1)}); - auto* i3 = Structure("I3", {Member(Source{{5, 6}}, "z", i2)}); + auto* i2 = Structure("I2", {Member(Source{{3, 4}}, "y", ty.Of(i1))}); + auto* i3 = Structure("I3", {Member(Source{{5, 6}}, "z", ty.Of(i2))}); - auto* s = Structure("S", {Member(Source{{7, 8}}, "m", i3)}, + auto* s = Structure("S", {Member(Source{{7, 8}}, "m", ty.Of(i3))}, {create()}); - Global(Source{{9, 10}}, "g", s, ast::StorageClass::kStorage, + Global(Source{{9, 10}}, "g", ty.Of(s), ast::StorageClass::kStorage, ast::Access::kRead, ast::DecorationList{ create(0), @@ -120,22 +120,22 @@ TEST_F(ResolverHostShareableValidationTest, NoError) { Member(Source{{2, 1}}, "y1", ty.vec3()), Member(Source{{3, 1}}, "z1", ty.array()), }); - auto* a1 = Alias("a1", i1); + auto* a1 = Alias("a1", ty.Of(i1)); auto* i2 = Structure("I2", { Member(Source{{4, 1}}, "x2", ty.mat2x2()), - Member(Source{{5, 1}}, "y2", i1), + Member(Source{{5, 1}}, "y2", ty.Of(i1)), }); - auto* a2 = Alias("a2", i2); + auto* a2 = Alias("a2", ty.Of(i2)); auto* i3 = Structure("I3", { - Member(Source{{4, 1}}, "x3", a1), - Member(Source{{5, 1}}, "y3", i2), - Member(Source{{6, 1}}, "z3", a2), + Member(Source{{4, 1}}, "x3", ty.Of(a1)), + Member(Source{{5, 1}}, "y3", ty.Of(i2)), + Member(Source{{6, 1}}, "z3", ty.Of(a2)), }); - auto* s = Structure("S", {Member(Source{{7, 8}}, "m", i3)}, + auto* s = Structure("S", {Member(Source{{7, 8}}, "m", ty.Of(i3))}, {create()}); - Global(Source{{9, 10}}, "g", s, ast::StorageClass::kStorage, + Global(Source{{9, 10}}, "g", ty.Of(s), ast::StorageClass::kStorage, ast::Access::kRead, ast::DecorationList{ create(0), diff --git a/src/resolver/inferred_type_test.cc b/src/resolver/inferred_type_test.cc index 538850deea..bca018e2c9 100644 --- a/src/resolver/inferred_type_test.cc +++ b/src/resolver/inferred_type_test.cc @@ -158,15 +158,15 @@ TEST_F(ResolverInferredTypeTest, InferArray_Pass) { TEST_F(ResolverInferredTypeTest, InferStruct_Pass) { auto* member = Member("x", ty.i32()); - auto* type = Structure("S", {member}, {create()}); + auto* str = Structure("S", {member}, {create()}); auto* expected_type = - create(type, + create(str, sem::StructMemberList{create( member, create(), 0, 0, 0, 4)}, 0, 4, 4); - auto* ctor_expr = Construct(type); + auto* ctor_expr = Construct(ty.Of(str)); auto* var = Var("a", nullptr, ast::StorageClass::kFunction, ctor_expr); WrapInFunction(var); diff --git a/src/resolver/intrinsic_test.cc b/src/resolver/intrinsic_test.cc index 35d1bf080c..740824eed1 100644 --- a/src/resolver/intrinsic_test.cc +++ b/src/resolver/intrinsic_test.cc @@ -768,7 +768,7 @@ TEST_F(ResolverIntrinsicDataTest, ArrayLength_Vector) { auto* ary = ty.array(); auto* str = Structure("S", {Member("x", ary)}, {create()}); - Global("a", str, ast::StorageClass::kStorage, ast::Access::kRead, + Global("a", ty.Of(str), ast::StorageClass::kStorage, ast::Access::kRead, ast::DecorationList{ create(0), create(0), diff --git a/src/resolver/is_storeable_test.cc b/src/resolver/is_storeable_test.cc index 209460c0e3..4d3be5cec0 100644 --- a/src/resolver/is_storeable_test.cc +++ b/src/resolver/is_storeable_test.cc @@ -105,7 +105,7 @@ TEST_F(ResolverIsStorableTest, Struct_NestedStorable) { }); Structure("S", { Member("a", ty.i32()), - Member("b", storable), + Member("b", ty.Of(storable)), }); ASSERT_TRUE(r()->Resolve()) << r()->error(); @@ -120,7 +120,7 @@ TEST_F(ResolverIsStorableTest, Struct_NestedNonStorable) { }); Structure("S", { Member("a", ty.i32()), - Member("b", non_storable), + Member("b", ty.Of(non_storable)), }); EXPECT_FALSE(r()->Resolve()); diff --git a/src/resolver/ptr_ref_test.cc b/src/resolver/ptr_ref_test.cc index f70b19bdbc..f8425495a4 100644 --- a/src/resolver/ptr_ref_test.cc +++ b/src/resolver/ptr_ref_test.cc @@ -66,12 +66,12 @@ TEST_F(ResolverPtrRefTest, DefaultPtrStorageClass) { auto* function = Var("f", ty.i32()); auto* private_ = Global("p", ty.i32(), ast::StorageClass::kPrivate); auto* workgroup = Global("w", ty.i32(), ast::StorageClass::kWorkgroup); - auto* uniform = Global("ub", buf, ast::StorageClass::kUniform, + auto* uniform = Global("ub", ty.Of(buf), ast::StorageClass::kUniform, ast::DecorationList{ create(0), create(0), }); - auto* storage = Global("sb", buf, ast::StorageClass::kStorage, + auto* storage = Global("sb", ty.Of(buf), ast::StorageClass::kStorage, ast::DecorationList{ create(1), create(0), @@ -87,10 +87,10 @@ TEST_F(ResolverPtrRefTest, DefaultPtrStorageClass) { Const("w_ptr", ty.pointer(ty.i32(), ast::StorageClass::kWorkgroup), AddressOf(workgroup)); auto* uniform_ptr = - Const("ub_ptr", ty.pointer(buf, ast::StorageClass::kUniform), + Const("ub_ptr", ty.pointer(ty.Of(buf), ast::StorageClass::kUniform), AddressOf(uniform)); auto* storage_ptr = - Const("sb_ptr", ty.pointer(buf, ast::StorageClass::kStorage), + Const("sb_ptr", ty.pointer(ty.Of(buf), ast::StorageClass::kStorage), AddressOf(storage)); WrapInFunction(function, function_ptr, private_ptr, workgroup_ptr, diff --git a/src/resolver/ptr_ref_validation_test.cc b/src/resolver/ptr_ref_validation_test.cc index c734c6ab96..c693d19f1a 100644 --- a/src/resolver/ptr_ref_validation_test.cc +++ b/src/resolver/ptr_ref_validation_test.cc @@ -90,14 +90,14 @@ TEST_F(ResolverPtrRefValidationTest, InferredPtrAccessMismatch) { // let p : pointer = &s.inner.arr[2]; // } auto* inner = Structure("Inner", {Member("arr", ty.array())}); - auto* buf = Structure("S", {Member("inner", inner)}, + auto* buf = Structure("S", {Member("inner", ty.Of(inner))}, {create()}); - auto* storage = - Global("s", buf, ast::StorageClass::kStorage, ast::Access::kReadWrite, - ast::DecorationList{ - create(0), - create(0), - }); + auto* storage = Global("s", ty.Of(buf), ast::StorageClass::kStorage, + ast::Access::kReadWrite, + ast::DecorationList{ + create(0), + create(0), + }); auto* expr = IndexAccessor(MemberAccessor(MemberAccessor(storage, "inner"), "arr"), 4); diff --git a/src/resolver/resolver.cc b/src/resolver/resolver.cc index fc25d8f05c..df95064ac9 100644 --- a/src/resolver/resolver.cc +++ b/src/resolver/resolver.cc @@ -213,9 +213,9 @@ bool Resolver::ResolveInternal() { // Process everything else in the order they appear in the module. This is // necessary for validation of use-before-declaration. for (auto* decl : builder_->AST().GlobalDeclarations()) { - if (auto* ty = decl->As()) { - Mark(ty); - if (!NamedType(ty)) { + if (auto* td = decl->As()) { + Mark(td); + if (!TypeDecl(td)) { return false; } } else if (auto* func = decl->As()) { @@ -2418,14 +2418,14 @@ bool Resolver::VariableDeclStatement(const ast::VariableDeclStatement* stmt) { return true; } -sem::Type* Resolver::NamedType(const ast::NamedType* named_type) { +sem::Type* Resolver::TypeDecl(const ast::TypeDecl* named_type) { sem::Type* result = nullptr; if (auto* alias = named_type->As()) { result = Type(alias->type()); } else if (auto* str = named_type->As()) { result = Structure(str); } else { - TINT_UNREACHABLE(diagnostics_) << "Unhandled NamedType"; + TINT_UNREACHABLE(diagnostics_) << "Unhandled TypeDecl"; } if (!result) { @@ -2433,9 +2433,9 @@ sem::Type* Resolver::NamedType(const ast::NamedType* named_type) { } named_type_info_.emplace(named_type->name(), - NamedTypeInfo{named_type, result}); + TypeDeclInfo{named_type, result}); - if (!ValidateNamedType(named_type)) { + if (!ValidateTypeDecl(named_type)) { return nullptr; } @@ -2443,10 +2443,10 @@ sem::Type* Resolver::NamedType(const ast::NamedType* named_type) { return result; } -bool Resolver::ValidateNamedType(const ast::NamedType* named_type) const { +bool Resolver::ValidateTypeDecl(const ast::TypeDecl* named_type) const { auto iter = named_type_info_.find(named_type->name()); if (iter == named_type_info_.end()) { - TINT_ICE(diagnostics_) << "ValidateNamedType called() before NamedType()"; + TINT_ICE(diagnostics_) << "ValidateTypeDecl called() before TypeDecl()"; } if (iter->second.ast != named_type) { diagnostics_.add_error("type with the name '" + diff --git a/src/resolver/resolver.h b/src/resolver/resolver.h index da9a0a4954..de33472d93 100644 --- a/src/resolver/resolver.h +++ b/src/resolver/resolver.h @@ -184,9 +184,9 @@ class Resolver { size_t first_continue = kNoContinue; }; - // Structure holding information for a NamedType - struct NamedTypeInfo { - ast::NamedType const* const ast; + // Structure holding information for a TypeDecl + struct TypeDeclInfo { + ast::TypeDecl const* const ast; sem::Type* const sem; }; @@ -270,7 +270,7 @@ class Resolver { const std::string& rhs_type_name); bool ValidateVectorConstructor(const ast::TypeConstructorExpression* ctor, const sem::Vector* vec_type); - bool ValidateNamedType(const ast::NamedType* named_type) const; + bool ValidateTypeDecl(const ast::TypeDecl* named_type) const; /// @returns the sem::Type for the ast::Type `ty`, building it if it /// hasn't been constructed already. If an error is raised, nullptr is @@ -280,7 +280,7 @@ class Resolver { /// @param named_type the named type to resolve /// @returns the resolved semantic type - sem::Type* NamedType(const ast::NamedType* named_type); + sem::Type* TypeDecl(const ast::TypeDecl* named_type); /// Builds and returns the semantic information for the array `arr`. /// This method does not mark the ast::Array node, nor attach the generated @@ -397,7 +397,7 @@ class Resolver { std::unordered_map variable_to_info_; std::unordered_map function_calls_; std::unordered_map expr_info_; - std::unordered_map named_type_info_; + std::unordered_map named_type_info_; std::unordered_set marked_; std::unordered_map constant_ids_; diff --git a/src/resolver/resolver_test.cc b/src/resolver/resolver_test.cc index f7ed724644..d225fbb976 100644 --- a/src/resolver/resolver_test.cc +++ b/src/resolver/resolver_test.cc @@ -294,7 +294,7 @@ TEST_F(ResolverTest, Stmt_VariableDecl) { TEST_F(ResolverTest, Stmt_VariableDecl_Alias) { auto* my_int = Alias("MyInt", ty.i32()); - auto* var = Var("my_var", my_int, ast::StorageClass::kNone, Expr(2)); + auto* var = Var("my_var", ty.Of(my_int), ast::StorageClass::kNone, Expr(2)); auto* init = var->constructor(); auto* decl = Decl(var); @@ -449,7 +449,7 @@ TEST_F(ResolverTest, Expr_ArrayAccessor_Array) { TEST_F(ResolverTest, Expr_ArrayAccessor_Alias_Array) { auto* aary = Alias("myarrty", ty.array()); - Global("my_var", aary, ast::StorageClass::kPrivate); + Global("my_var", ty.Of(aary), ast::StorageClass::kPrivate); auto* acc = IndexAccessor("my_var", 2); WrapInFunction(acc); @@ -813,12 +813,12 @@ TEST_F(ResolverTest, Function_RegisterInputOutputVariables) { auto* in_var = Global("in_var", ty.f32(), ast::StorageClass::kInput); auto* out_var = Global("out_var", ty.f32(), ast::StorageClass::kOutput); - auto* sb_var = - Global("sb_var", s, ast::StorageClass::kStorage, ast::Access::kRead, - ast::DecorationList{ - create(0), - create(0), - }); + auto* sb_var = Global("sb_var", ty.Of(s), ast::StorageClass::kStorage, + ast::Access::kRead, + ast::DecorationList{ + create(0), + create(0), + }); auto* wg_var = Global("wg_var", ty.f32(), ast::StorageClass::kWorkgroup); auto* priv_var = Global("priv_var", ty.f32(), ast::StorageClass::kPrivate); @@ -852,12 +852,12 @@ TEST_F(ResolverTest, Function_RegisterInputOutputVariables_SubFunction) { auto* in_var = Global("in_var", ty.f32(), ast::StorageClass::kInput); auto* out_var = Global("out_var", ty.f32(), ast::StorageClass::kOutput); - auto* sb_var = - Global("sb_var", s, ast::StorageClass::kStorage, ast::Access::kRead, - ast::DecorationList{ - create(0), - create(0), - }); + auto* sb_var = Global("sb_var", ty.Of(s), ast::StorageClass::kStorage, + ast::Access::kRead, + ast::DecorationList{ + create(0), + create(0), + }); auto* wg_var = Global("wg_var", ty.f32(), ast::StorageClass::kWorkgroup); auto* priv_var = Global("priv_var", ty.f32(), ast::StorageClass::kPrivate); @@ -1097,7 +1097,7 @@ TEST_F(ResolverTest, Function_WorkgroupSize_Mixed) { TEST_F(ResolverTest, Expr_MemberAccessor_Struct) { auto* st = Structure("S", {Member("first_member", ty.i32()), Member("second_member", ty.f32())}); - Global("my_struct", st, ast::StorageClass::kInput); + Global("my_struct", ty.Of(st), ast::StorageClass::kInput); auto* mem = MemberAccessor("my_struct", "second_member"); WrapInFunction(mem); @@ -1120,8 +1120,8 @@ TEST_F(ResolverTest, Expr_MemberAccessor_Struct) { TEST_F(ResolverTest, Expr_MemberAccessor_Struct_Alias) { auto* st = Structure("S", {Member("first_member", ty.i32()), Member("second_member", ty.f32())}); - auto* alias = Alias("alias", st); - Global("my_struct", alias, ast::StorageClass::kInput); + auto* alias = Alias("alias", ty.Of(st)); + Global("my_struct", ty.Of(alias), ast::StorageClass::kInput); auto* mem = MemberAccessor("my_struct", "second_member"); WrapInFunction(mem); @@ -1200,8 +1200,8 @@ TEST_F(ResolverTest, Expr_Accessor_MultiLevel) { // auto* stB = Structure("B", {Member("foo", ty.vec4())}); - auto* stA = Structure("A", {Member("mem", ty.vec(stB, 3))}); - Global("c", stA, ast::StorageClass::kInput); + auto* stA = Structure("A", {Member("mem", ty.vec(ty.Of(stB), 3))}); + Global("c", ty.Of(stA), ast::StorageClass::kInput); auto* mem = MemberAccessor( MemberAccessor(IndexAccessor(MemberAccessor("c", "mem"), 0), "foo"), @@ -1220,7 +1220,7 @@ TEST_F(ResolverTest, Expr_Accessor_MultiLevel) { TEST_F(ResolverTest, Expr_MemberAccessor_InBinaryOp) { auto* st = Structure("S", {Member("first_member", ty.f32()), Member("second_member", ty.f32())}); - Global("my_struct", st, ast::StorageClass::kInput); + Global("my_struct", ty.Of(st), ast::StorageClass::kInput); auto* expr = Add(MemberAccessor("my_struct", "first_member"), MemberAccessor("my_struct", "second_member")); @@ -1819,11 +1819,12 @@ TEST_F(ResolverTest, Access_SetForStorageBuffer) { // var g : S; auto* s = Structure("S", {Member(Source{{12, 34}}, "x", ty.i32())}, {create()}); - auto* var = Global(Source{{56, 78}}, "g", s, ast::StorageClass::kStorage, - ast::DecorationList{ - create(0), - create(0), - }); + auto* var = + Global(Source{{56, 78}}, "g", ty.Of(s), ast::StorageClass::kStorage, + ast::DecorationList{ + create(0), + create(0), + }); EXPECT_TRUE(r()->Resolve()) << r()->error(); diff --git a/src/resolver/storage_class_validation_test.cc b/src/resolver/storage_class_validation_test.cc index 033e7f60df..d1fdaa88a0 100644 --- a/src/resolver/storage_class_validation_test.cc +++ b/src/resolver/storage_class_validation_test.cc @@ -68,7 +68,7 @@ TEST_F(ResolverStorageClassValidationTest, StorageBufferPointer) { TEST_F(ResolverStorageClassValidationTest, StorageBufferArray) { // var g : array; auto* s = Structure("S", {Member("a", ty.f32())}); - auto* a = ty.array(s, 3); + auto* a = ty.array(ty.Of(s), 3); Global(Source{{56, 78}}, "g", a, ast::StorageClass::kStorage, ast::Access::kRead, ast::DecorationList{ @@ -87,7 +87,7 @@ TEST_F(ResolverStorageClassValidationTest, StorageBufferBoolAlias) { // type a = bool; // var g : a; auto* a = Alias("a", ty.bool_()); - Global(Source{{56, 78}}, "g", a, ast::StorageClass::kStorage, + Global(Source{{56, 78}}, "g", ty.Of(a), ast::StorageClass::kStorage, ast::DecorationList{ create(0), create(0), @@ -104,7 +104,7 @@ TEST_F(ResolverStorageClassValidationTest, StorageBufferNoBlockDecoration) { // struct S { x : i32 }; // var g : S; auto* s = Structure(Source{{12, 34}}, "S", {Member("x", ty.i32())}); - Global(Source{{56, 78}}, "g", s, ast::StorageClass::kStorage, + Global(Source{{56, 78}}, "g", ty.Of(s), ast::StorageClass::kStorage, ast::Access::kRead, ast::DecorationList{ create(0), @@ -124,7 +124,7 @@ TEST_F(ResolverStorageClassValidationTest, StorageBufferNoError_Basic) { // var g : S; auto* s = Structure("S", {Member(Source{{12, 34}}, "x", ty.i32())}, {create()}); - Global(Source{{56, 78}}, "g", s, ast::StorageClass::kStorage, + Global(Source{{56, 78}}, "g", ty.Of(s), ast::StorageClass::kStorage, ast::Access::kRead, ast::DecorationList{ create(0), @@ -140,9 +140,9 @@ TEST_F(ResolverStorageClassValidationTest, StorageBufferNoError_Aliases) { // var g : a1; auto* s = Structure("S", {Member(Source{{12, 34}}, "x", ty.i32())}, {create()}); - auto* a1 = Alias("a1", s); - auto* a2 = Alias("a2", a1); - Global(Source{{56, 78}}, "g", a2, ast::StorageClass::kStorage, + auto* a1 = Alias("a1", ty.Of(s)); + auto* a2 = Alias("a2", ty.Of(a1)); + Global(Source{{56, 78}}, "g", ty.Of(a2), ast::StorageClass::kStorage, ast::Access::kRead, ast::DecorationList{ create(0), @@ -188,7 +188,7 @@ TEST_F(ResolverStorageClassValidationTest, UniformBufferPointer) { TEST_F(ResolverStorageClassValidationTest, UniformBufferArray) { // var g : array; auto* s = Structure("S", {Member("a", ty.f32())}); - auto* a = ty.array(s, 3); + auto* a = ty.array(ty.Of(s), 3); Global(Source{{56, 78}}, "g", a, ast::StorageClass::kUniform, ast::DecorationList{ create(0), @@ -206,7 +206,7 @@ TEST_F(ResolverStorageClassValidationTest, UniformBufferBoolAlias) { // type a = bool; // var g : a; auto* a = Alias("a", ty.bool_()); - Global(Source{{56, 78}}, "g", a, ast::StorageClass::kUniform, + Global(Source{{56, 78}}, "g", ty.Of(a), ast::StorageClass::kUniform, ast::DecorationList{ create(0), create(0), @@ -223,7 +223,7 @@ TEST_F(ResolverStorageClassValidationTest, UniformBufferNoBlockDecoration) { // struct S { x : i32 }; // var g : S; auto* s = Structure(Source{{12, 34}}, "S", {Member("x", ty.i32())}); - Global(Source{{56, 78}}, "g", s, ast::StorageClass::kUniform, + Global(Source{{56, 78}}, "g", ty.Of(s), ast::StorageClass::kUniform, ast::DecorationList{ create(0), create(0), @@ -242,7 +242,7 @@ TEST_F(ResolverStorageClassValidationTest, UniformBufferNoError_Basic) { // var g : S; auto* s = Structure("S", {Member(Source{{12, 34}}, "x", ty.i32())}, {create()}); - Global(Source{{56, 78}}, "g", s, ast::StorageClass::kUniform, + Global(Source{{56, 78}}, "g", ty.Of(s), ast::StorageClass::kUniform, ast::DecorationList{ create(0), create(0), @@ -257,8 +257,8 @@ TEST_F(ResolverStorageClassValidationTest, UniformBufferNoError_Aliases) { // var g : a1; auto* s = Structure("S", {Member(Source{{12, 34}}, "x", ty.i32())}, {create()}); - auto* a1 = Alias("a1", s); - Global(Source{{56, 78}}, "g", a1, ast::StorageClass::kUniform, + auto* a1 = Alias("a1", ty.Of(s)); + Global(Source{{56, 78}}, "g", ty.Of(a1), ast::StorageClass::kUniform, ast::DecorationList{ create(0), create(0), diff --git a/src/resolver/struct_layout_test.cc b/src/resolver/struct_layout_test.cc index be0e808197..f516412d60 100644 --- a/src/resolver/struct_layout_test.cc +++ b/src/resolver/struct_layout_test.cc @@ -56,8 +56,8 @@ TEST_F(ResolverStructLayoutTest, Alias) { auto* alias_b = Alias("b", ty.f32()); auto* s = Structure("S", { - Member("a", alias_a), - Member("b", alias_b), + Member("a", ty.Of(alias_a)), + Member("b", ty.Of(alias_b)), }); ASSERT_TRUE(r()->Resolve()) << r()->error(); @@ -195,8 +195,8 @@ TEST_F(ResolverStructLayoutTest, ImplicitStrideArrayOfStructure) { Member("a", ty.vec2()), Member("b", ty.vec3()), Member("c", ty.vec4()), - }); // size: 48 - auto* outer = ty.array(inner, 12); // size: 12 * 48 + }); // size: 48 + auto* outer = ty.array(ty.Of(inner), 12); // size: 12 * 48 auto* s = Structure("S", { Member("c", outer), }); @@ -296,7 +296,7 @@ TEST_F(ResolverStructLayoutTest, NestedStruct) { }); auto* s = Structure("S", { Member("a", ty.i32()), - Member("b", inner), + Member("b", ty.Of(inner)), Member("c", ty.i32()), }); @@ -328,7 +328,7 @@ TEST_F(ResolverStructLayoutTest, SizeDecorations) { auto* s = Structure("S", { Member("a", ty.f32(), {MemberSize(4)}), Member("b", ty.u32(), {MemberSize(8)}), - Member("c", inner), + Member("c", ty.Of(inner)), Member("d", ty.i32(), {MemberSize(32)}), }); @@ -363,7 +363,7 @@ TEST_F(ResolverStructLayoutTest, AlignDecorations) { auto* s = Structure("S", { Member("a", ty.f32(), {MemberAlign(4)}), Member("b", ty.u32(), {MemberAlign(8)}), - Member("c", inner), + Member("c", ty.Of(inner)), Member("d", ty.i32(), {MemberAlign(32)}), }); diff --git a/src/resolver/struct_pipeline_stage_use_test.cc b/src/resolver/struct_pipeline_stage_use_test.cc index 4c481a5b28..ae526729b1 100644 --- a/src/resolver/struct_pipeline_stage_use_test.cc +++ b/src/resolver/struct_pipeline_stage_use_test.cc @@ -40,7 +40,7 @@ TEST_F(ResolverPipelineStageUseTest, UnusedStruct) { TEST_F(ResolverPipelineStageUseTest, StructUsedAsNonEntryPointParam) { auto* s = Structure("S", {Member("a", ty.f32(), {Location(0)})}); - Func("foo", {Param("param", s)}, ty.void_(), {}, {}); + Func("foo", {Param("param", ty.Of(s))}, ty.void_(), {}, {}); ASSERT_TRUE(r()->Resolve()) << r()->error(); @@ -52,7 +52,7 @@ TEST_F(ResolverPipelineStageUseTest, StructUsedAsNonEntryPointParam) { TEST_F(ResolverPipelineStageUseTest, StructUsedAsNonEntryPointReturnType) { auto* s = Structure("S", {Member("a", ty.f32(), {Location(0)})}); - Func("foo", {}, s, {Return(Construct(s, Expr(0.f)))}, {}); + Func("foo", {}, ty.Of(s), {Return(Construct(ty.Of(s), Expr(0.f)))}, {}); ASSERT_TRUE(r()->Resolve()) << r()->error(); @@ -64,7 +64,7 @@ TEST_F(ResolverPipelineStageUseTest, StructUsedAsNonEntryPointReturnType) { TEST_F(ResolverPipelineStageUseTest, StructUsedAsVertexShaderParam) { auto* s = Structure("S", {Member("a", ty.f32(), {Location(0)})}); - Func("main", {Param("param", s)}, ty.vec4(), + Func("main", {Param("param", ty.Of(s))}, ty.vec4(), {Return(Construct(ty.vec4()))}, {Stage(ast::PipelineStage::kVertex)}, {Builtin(ast::Builtin::kPosition)}); @@ -81,7 +81,7 @@ TEST_F(ResolverPipelineStageUseTest, StructUsedAsVertexShaderReturnType) { auto* s = Structure( "S", {Member("a", ty.f32(), {Builtin(ast::Builtin::kPosition)})}); - Func("main", {}, s, {Return(Construct(s, Expr(0.f)))}, + Func("main", {}, ty.Of(s), {Return(Construct(ty.Of(s), Expr(0.f)))}, {Stage(ast::PipelineStage::kVertex)}); ASSERT_TRUE(r()->Resolve()) << r()->error(); @@ -95,7 +95,7 @@ TEST_F(ResolverPipelineStageUseTest, StructUsedAsVertexShaderReturnType) { TEST_F(ResolverPipelineStageUseTest, StructUsedAsFragmentShaderParam) { auto* s = Structure("S", {Member("a", ty.f32(), {Location(0)})}); - Func("main", {Param("param", s)}, ty.void_(), {}, + Func("main", {Param("param", ty.Of(s))}, ty.void_(), {}, {Stage(ast::PipelineStage::kFragment)}); ASSERT_TRUE(r()->Resolve()) << r()->error(); @@ -109,7 +109,7 @@ TEST_F(ResolverPipelineStageUseTest, StructUsedAsFragmentShaderParam) { TEST_F(ResolverPipelineStageUseTest, StructUsedAsFragmentShaderReturnType) { auto* s = Structure("S", {Member("a", ty.f32(), {Location(0)})}); - Func("main", {}, s, {Return(Construct(s, Expr(0.f)))}, + Func("main", {}, ty.Of(s), {Return(Construct(ty.Of(s), Expr(0.f)))}, {Stage(ast::PipelineStage::kFragment)}); ASSERT_TRUE(r()->Resolve()) << r()->error(); @@ -125,7 +125,7 @@ TEST_F(ResolverPipelineStageUseTest, StructUsedAsComputeShaderParam) { "S", {Member("a", ty.u32(), {Builtin(ast::Builtin::kLocalInvocationIndex)})}); - Func("main", {Param("param", s)}, ty.void_(), {}, + Func("main", {Param("param", ty.Of(s))}, ty.void_(), {}, {Stage(ast::PipelineStage::kCompute)}); ASSERT_TRUE(r()->Resolve()) << r()->error(); @@ -140,10 +140,11 @@ TEST_F(ResolverPipelineStageUseTest, StructUsedMultipleStages) { auto* s = Structure( "S", {Member("a", ty.f32(), {Builtin(ast::Builtin::kPosition)})}); - Func("vert_main", {Param("param", s)}, s, {Return(Construct(s, Expr(0.f)))}, + Func("vert_main", {Param("param", ty.Of(s))}, ty.Of(s), + {Return(Construct(ty.Of(s), Expr(0.f)))}, {Stage(ast::PipelineStage::kVertex)}); - Func("frag_main", {Param("param", s)}, ty.void_(), {}, + Func("frag_main", {Param("param", ty.Of(s))}, ty.void_(), {}, {Stage(ast::PipelineStage::kFragment)}); ASSERT_TRUE(r()->Resolve()) << r()->error(); @@ -158,9 +159,9 @@ TEST_F(ResolverPipelineStageUseTest, StructUsedMultipleStages) { TEST_F(ResolverPipelineStageUseTest, StructUsedAsShaderParamViaAlias) { auto* s = Structure("S", {Member("a", ty.f32(), {Location(0)})}); - auto* s_alias = Alias("S_alias", s); + auto* s_alias = Alias("S_alias", ty.Of(s)); - Func("main", {Param("param", s_alias)}, ty.void_(), {}, + Func("main", {Param("param", ty.Of(s_alias))}, ty.void_(), {}, {Stage(ast::PipelineStage::kFragment)}); ASSERT_TRUE(r()->Resolve()) << r()->error(); @@ -173,9 +174,10 @@ TEST_F(ResolverPipelineStageUseTest, StructUsedAsShaderParamViaAlias) { TEST_F(ResolverPipelineStageUseTest, StructUsedAsShaderReturnTypeViaAlias) { auto* s = Structure("S", {Member("a", ty.f32(), {Location(0)})}); - auto* s_alias = Alias("S_alias", s); + auto* s_alias = Alias("S_alias", ty.Of(s)); - Func("main", {}, s_alias, {Return(Construct(s_alias, Expr(0.f)))}, + Func("main", {}, ty.Of(s_alias), + {Return(Construct(ty.Of(s_alias), Expr(0.f)))}, {Stage(ast::PipelineStage::kFragment)}); ASSERT_TRUE(r()->Resolve()) << r()->error(); diff --git a/src/resolver/struct_storage_class_use_test.cc b/src/resolver/struct_storage_class_use_test.cc index 8af99bef8b..598903d75d 100644 --- a/src/resolver/struct_storage_class_use_test.cc +++ b/src/resolver/struct_storage_class_use_test.cc @@ -40,7 +40,7 @@ TEST_F(ResolverStorageClassUseTest, UnreachableStruct) { TEST_F(ResolverStorageClassUseTest, StructReachableFromParameter) { auto* s = Structure("S", {Member("a", ty.f32())}); - Func("f", {Param("param", s)}, ty.void_(), {}, {}); + Func("f", {Param("param", ty.Of(s))}, ty.void_(), {}, {}); ASSERT_TRUE(r()->Resolve()) << r()->error(); @@ -53,7 +53,7 @@ TEST_F(ResolverStorageClassUseTest, StructReachableFromParameter) { TEST_F(ResolverStorageClassUseTest, StructReachableFromReturnType) { auto* s = Structure("S", {Member("a", ty.f32())}); - Func("f", {}, s, {Return(Construct(s))}, {}); + Func("f", {}, ty.Of(s), {Return(Construct(ty.Of(s)))}, {}); ASSERT_TRUE(r()->Resolve()) << r()->error(); @@ -66,7 +66,7 @@ TEST_F(ResolverStorageClassUseTest, StructReachableFromReturnType) { TEST_F(ResolverStorageClassUseTest, StructReachableFromGlobal) { auto* s = Structure("S", {Member("a", ty.f32())}); - Global("g", s, ast::StorageClass::kPrivate); + Global("g", ty.Of(s), ast::StorageClass::kPrivate); ASSERT_TRUE(r()->Resolve()) << r()->error(); @@ -78,8 +78,8 @@ TEST_F(ResolverStorageClassUseTest, StructReachableFromGlobal) { TEST_F(ResolverStorageClassUseTest, StructReachableViaGlobalAlias) { auto* s = Structure("S", {Member("a", ty.f32())}); - auto* a = Alias("A", s); - Global("g", a, ast::StorageClass::kPrivate); + auto* a = Alias("A", ty.Of(s)); + Global("g", ty.Of(a), ast::StorageClass::kPrivate); ASSERT_TRUE(r()->Resolve()) << r()->error(); @@ -91,8 +91,8 @@ TEST_F(ResolverStorageClassUseTest, StructReachableViaGlobalAlias) { TEST_F(ResolverStorageClassUseTest, StructReachableViaGlobalStruct) { auto* s = Structure("S", {Member("a", ty.f32())}); - auto* o = Structure("O", {Member("a", s)}); - Global("g", o, ast::StorageClass::kPrivate); + auto* o = Structure("O", {Member("a", ty.Of(s))}); + Global("g", ty.Of(o), ast::StorageClass::kPrivate); ASSERT_TRUE(r()->Resolve()) << r()->error(); @@ -104,7 +104,7 @@ TEST_F(ResolverStorageClassUseTest, StructReachableViaGlobalStruct) { TEST_F(ResolverStorageClassUseTest, StructReachableViaGlobalArray) { auto* s = Structure("S", {Member("a", ty.f32())}); - auto* a = ty.array(s, 3); + auto* a = ty.array(ty.Of(s), 3); Global("g", a, ast::StorageClass::kPrivate); ASSERT_TRUE(r()->Resolve()) << r()->error(); @@ -118,7 +118,7 @@ TEST_F(ResolverStorageClassUseTest, StructReachableViaGlobalArray) { TEST_F(ResolverStorageClassUseTest, StructReachableFromLocal) { auto* s = Structure("S", {Member("a", ty.f32())}); - WrapInFunction(Var("g", s)); + WrapInFunction(Var("g", ty.Of(s))); ASSERT_TRUE(r()->Resolve()) << r()->error(); @@ -130,8 +130,8 @@ TEST_F(ResolverStorageClassUseTest, StructReachableFromLocal) { TEST_F(ResolverStorageClassUseTest, StructReachableViaLocalAlias) { auto* s = Structure("S", {Member("a", ty.f32())}); - auto* a = Alias("A", s); - WrapInFunction(Var("g", a)); + auto* a = Alias("A", ty.Of(s)); + WrapInFunction(Var("g", ty.Of(a))); ASSERT_TRUE(r()->Resolve()) << r()->error(); @@ -143,8 +143,8 @@ TEST_F(ResolverStorageClassUseTest, StructReachableViaLocalAlias) { TEST_F(ResolverStorageClassUseTest, StructReachableViaLocalStruct) { auto* s = Structure("S", {Member("a", ty.f32())}); - auto* o = Structure("O", {Member("a", s)}); - WrapInFunction(Var("g", o)); + auto* o = Structure("O", {Member("a", ty.Of(s))}); + WrapInFunction(Var("g", ty.Of(o))); ASSERT_TRUE(r()->Resolve()) << r()->error(); @@ -156,7 +156,7 @@ TEST_F(ResolverStorageClassUseTest, StructReachableViaLocalStruct) { TEST_F(ResolverStorageClassUseTest, StructReachableViaLocalArray) { auto* s = Structure("S", {Member("a", ty.f32())}); - auto* a = ty.array(s, 3); + auto* a = ty.array(ty.Of(s), 3); WrapInFunction(Var("g", a)); ASSERT_TRUE(r()->Resolve()) << r()->error(); @@ -170,17 +170,17 @@ TEST_F(ResolverStorageClassUseTest, StructReachableViaLocalArray) { TEST_F(ResolverStorageClassUseTest, StructMultipleStorageClassUses) { auto* s = Structure("S", {Member("a", ty.f32())}, {create()}); - Global("x", s, ast::StorageClass::kUniform, + Global("x", ty.Of(s), ast::StorageClass::kUniform, ast::DecorationList{ create(0), create(0), }); - Global("y", s, ast::StorageClass::kStorage, ast::Access::kRead, + Global("y", ty.Of(s), ast::StorageClass::kStorage, ast::Access::kRead, ast::DecorationList{ create(1), create(0), }); - WrapInFunction(Var("g", s)); + WrapInFunction(Var("g", ty.Of(s))); ASSERT_TRUE(r()->Resolve()) << r()->error(); diff --git a/src/resolver/type_validation_test.cc b/src/resolver/type_validation_test.cc index 2eb0707753..d1b1c350eb 100644 --- a/src/resolver/type_validation_test.cc +++ b/src/resolver/type_validation_test.cc @@ -341,7 +341,7 @@ TEST_F(ResolverTypeValidationTest, AliasRuntimeArrayIsNotLast_Fail) { auto* alias = Alias("RTArr", ty.array()); Structure("s", { - Member(Source{{12, 34}}, "b", alias), + Member(Source{{12, 34}}, "b", ty.Of(alias)), Member("a", ty.u32()), }, {create()}); @@ -367,7 +367,7 @@ TEST_F(ResolverTypeValidationTest, AliasRuntimeArrayIsLast_Pass) { Structure("s", { Member("a", ty.u32()), - Member("b", alias), + Member("b", ty.Of(alias)), }, {create()}); diff --git a/src/resolver/validation_test.cc b/src/resolver/validation_test.cc index e271bdc530..a0a5f231ae 100644 --- a/src/resolver/validation_test.cc +++ b/src/resolver/validation_test.cc @@ -1709,7 +1709,7 @@ TEST_F(ResolverValidationTest, TEST_F(ResolverValidationTest, Expr_Constructor_Vector_Alias_Argument_Error) { auto* alias = Alias("UnsignedInt", ty.u32()); - Global("uint_var", alias, ast::StorageClass::kInput); + Global("uint_var", ty.Of(alias), ast::StorageClass::kInput); auto* tc = vec2(Expr(Source{{12, 34}}, "uint_var")); WrapInFunction(tc); @@ -1723,8 +1723,8 @@ TEST_F(ResolverValidationTest, Expr_Constructor_Vector_Alias_Argument_Error) { TEST_F(ResolverValidationTest, Expr_Constructor_Vector_Alias_Argument_Success) { auto* f32_alias = Alias("Float32", ty.f32()); auto* vec2_alias = Alias("VectorFloat2", ty.vec2()); - Global("my_f32", f32_alias, ast::StorageClass::kInput); - Global("my_vec2", vec2_alias, ast::StorageClass::kInput); + Global("my_f32", ty.Of(f32_alias), ast::StorageClass::kInput); + Global("my_vec2", ty.Of(vec2_alias), ast::StorageClass::kInput); auto* tc = vec3("my_vec2", "my_f32"); WrapInFunction(tc); @@ -1735,7 +1735,7 @@ TEST_F(ResolverValidationTest, Expr_Constructor_Vector_ElementTypeAlias_Error) { auto* f32_alias = Alias("Float32", ty.f32()); // vec2(1.0f, 1u) - auto* vec_type = ty.vec(f32_alias, 2); + auto* vec_type = ty.vec(ty.Of(f32_alias), 2); auto* tc = create( Source{{12, 34}}, vec_type, ExprList(1.0f, create(Source{{12, 40}}, @@ -1753,7 +1753,7 @@ TEST_F(ResolverValidationTest, auto* f32_alias = Alias("Float32", ty.f32()); // vec2(1.0f, 1.0f) - auto* vec_type = ty.vec(f32_alias, 2); + auto* vec_type = ty.vec(ty.Of(f32_alias), 2); auto* tc = create(Source{{12, 34}}, vec_type, ExprList(1.0f, 1.0f)); WrapInFunction(tc); @@ -1766,7 +1766,7 @@ TEST_F(ResolverValidationTest, auto* f32_alias = Alias("Float32", ty.f32()); // vec3(vec(), 1.0f) - auto* vec_type = ty.vec(f32_alias, 2); + auto* vec_type = ty.vec(ty.Of(f32_alias), 2); auto* tc = vec3(create( Source{{12, 34}}, vec_type, ExprList()), 1.0f); @@ -1783,7 +1783,7 @@ TEST_F(ResolverValidationTest, auto* f32_alias = Alias("Float32", ty.f32()); // vec3(vec(), 1.0f) - auto* vec_type = ty.vec(f32_alias, 2); + auto* vec_type = ty.vec(ty.Of(f32_alias), 2); auto* tc = vec3(create( Source{{12, 34}}, vec_type, ExprList()), 1.0f); @@ -2019,7 +2019,7 @@ TEST_P(MatrixConstructorTest, Expr_Constructor_ElementTypeAlias_Error) { Source{{12, i}}, vec_type, ExprList())); } - auto* matrix_type = ty.mat(f32_alias, param.columns, param.rows); + auto* matrix_type = ty.mat(ty.Of(f32_alias), param.columns, param.rows); auto* tc = create(Source{}, matrix_type, std::move(args)); WrapInFunction(tc); @@ -2044,7 +2044,7 @@ TEST_P(MatrixConstructorTest, Expr_Constructor_ElementTypeAlias_Success) { Source{{12, i}}, vec_type, ExprList())); } - auto* matrix_type = ty.mat(f32_alias, param.columns, param.rows); + auto* matrix_type = ty.mat(ty.Of(f32_alias), param.columns, param.rows); auto* tc = create(Source{}, matrix_type, std::move(args)); WrapInFunction(tc); @@ -2054,10 +2054,9 @@ TEST_P(MatrixConstructorTest, Expr_Constructor_ElementTypeAlias_Success) { TEST_F(ResolverValidationTest, Expr_MatrixConstructor_ArgumentTypeAlias_Error) { auto* alias = Alias("VectorUnsigned2", ty.vec2()); - auto* tc = mat2x2( - create( - Source{{12, 34}}, ty.MaybeCreateTypename(alias), ExprList()), - vec2()); + auto* tc = mat2x2(create( + Source{{12, 34}}, ty.Of(alias), ExprList()), + vec2()); WrapInFunction(tc); EXPECT_FALSE(r()->Resolve()); @@ -2075,7 +2074,7 @@ TEST_P(MatrixConstructorTest, Expr_Constructor_ArgumentTypeAlias_Success) { ast::ExpressionList args; for (uint32_t i = 1; i <= param.columns; i++) { args.push_back(create( - Source{{12, i}}, ty.MaybeCreateTypename(vec_alias), ExprList())); + Source{{12, i}}, ty.Of(vec_alias), ExprList())); } auto* tc = create(Source{}, matrix_type, @@ -2092,7 +2091,7 @@ TEST_P(MatrixConstructorTest, Expr_Constructor_ArgumentElementTypeAlias_Error) { ast::ExpressionList args; for (uint32_t i = 1; i <= param.columns; i++) { - auto* vec_type = ty.vec(f32_alias, param.rows); + auto* vec_type = ty.vec(ty.Of(f32_alias), param.rows); args.push_back(create( Source{{12, i}}, vec_type, ExprList())); } @@ -2115,7 +2114,7 @@ TEST_P(MatrixConstructorTest, ast::ExpressionList args; for (uint32_t i = 1; i <= param.columns; i++) { - auto* vec_type = ty.vec(f32_alias, param.rows); + auto* vec_type = ty.vec(ty.Of(f32_alias), param.rows); args.push_back(create( Source{{12, i}}, vec_type, ExprList())); } diff --git a/src/resolver/var_let_test.cc b/src/resolver/var_let_test.cc index 84b4ae797b..1b45fac41b 100644 --- a/src/resolver/var_let_test.cc +++ b/src/resolver/var_let_test.cc @@ -39,14 +39,14 @@ TEST_F(ResolverVarLetTest, TypeOfVar) { // } auto* S = Structure("S", {Member("i", ty.i32())}); - auto* A = Alias("A", S); + auto* A = Alias("A", ty.Of(S)); auto* i = Var("i", ty.i32(), ast::StorageClass::kNone); auto* u = Var("u", ty.u32(), ast::StorageClass::kNone); auto* f = Var("f", ty.f32(), ast::StorageClass::kNone); auto* b = Var("b", ty.bool_(), ast::StorageClass::kNone); - auto* s = Var("s", S, ast::StorageClass::kNone); - auto* a = Var("a", A, ast::StorageClass::kNone); + auto* s = Var("s", ty.Of(S), ast::StorageClass::kNone); + auto* a = Var("a", ty.Of(A), ast::StorageClass::kNone); Func("F", {}, ty.void_(), { @@ -90,15 +90,15 @@ TEST_F(ResolverVarLetTest, TypeOfLet) { // } auto* S = Structure("S", {Member("i", ty.i32())}); - auto* A = Alias("A", S); + auto* A = Alias("A", ty.Of(S)); auto* v = Var("v", ty.i32(), ast::StorageClass::kNone); auto* i = Const("i", ty.i32(), Expr(1)); auto* u = Const("u", ty.u32(), Expr(1u)); auto* f = Const("f", ty.f32(), Expr(1.f)); auto* b = Const("b", ty.bool_(), Expr(true)); - auto* s = Const("s", S, Construct(S, Expr(1))); - auto* a = Const("a", A, Construct(A, Expr(1))); + auto* s = Const("s", ty.Of(S), Construct(ty.Of(S), Expr(1))); + auto* a = Const("a", ty.Of(A), Construct(ty.Of(A), Expr(1))); auto* p = Const("p", ty.pointer(ast::StorageClass::kFunction), AddressOf(v)); @@ -135,12 +135,12 @@ TEST_F(ResolverVarLetTest, DefaultVarStorageClass) { auto* function = Var("f", ty.i32()); auto* private_ = Global("p", ty.i32(), ast::StorageClass::kPrivate); auto* workgroup = Global("w", ty.i32(), ast::StorageClass::kWorkgroup); - auto* uniform = Global("ub", buf, ast::StorageClass::kUniform, + auto* uniform = Global("ub", ty.Of(buf), ast::StorageClass::kUniform, ast::DecorationList{ create(0), create(0), }); - auto* storage = Global("sb", buf, ast::StorageClass::kStorage, + auto* storage = Global("sb", ty.Of(buf), ast::StorageClass::kStorage, ast::DecorationList{ create(1), create(0), @@ -180,12 +180,12 @@ TEST_F(ResolverVarLetTest, ExplicitVarStorageClass) { auto* buf = Structure("S", {Member("m", ty.i32())}, {create()}); - auto* storage = - Global("sb", buf, ast::StorageClass::kStorage, ast::Access::kReadWrite, - ast::DecorationList{ - create(1), - create(0), - }); + auto* storage = Global("sb", ty.Of(buf), ast::StorageClass::kStorage, + ast::Access::kReadWrite, + ast::DecorationList{ + create(1), + create(0), + }); EXPECT_TRUE(r()->Resolve()) << r()->error(); @@ -207,14 +207,14 @@ TEST_F(ResolverVarLetTest, LetInheritsAccessFromOriginatingVariable) { // let p = &s.inner.arr[2]; // } auto* inner = Structure("Inner", {Member("arr", ty.array())}); - auto* buf = Structure("S", {Member("inner", inner)}, + auto* buf = Structure("S", {Member("inner", ty.Of(inner))}, {create()}); - auto* storage = - Global("s", buf, ast::StorageClass::kStorage, ast::Access::kReadWrite, - ast::DecorationList{ - create(0), - create(0), - }); + auto* storage = Global("s", ty.Of(buf), ast::StorageClass::kStorage, + ast::Access::kReadWrite, + ast::DecorationList{ + create(0), + create(0), + }); auto* expr = IndexAccessor(MemberAccessor(MemberAccessor(storage, "inner"), "arr"), 4); diff --git a/src/resolver/var_let_validation_test.cc b/src/resolver/var_let_validation_test.cc index 0fa67e8bd7..f7fa006ef7 100644 --- a/src/resolver/var_let_validation_test.cc +++ b/src/resolver/var_let_validation_test.cc @@ -81,7 +81,7 @@ TEST_F(ResolverVarLetValidationTest, VarConstructorWrongType) { TEST_F(ResolverVarLetValidationTest, LetConstructorWrongTypeViaAlias) { auto* a = Alias("I32", ty.i32()); - WrapInFunction(Const(Source{{3, 3}}, "v", a, Expr(2u))); + WrapInFunction(Const(Source{{3, 3}}, "v", ty.Of(a), Expr(2u))); EXPECT_FALSE(r()->Resolve()); EXPECT_EQ( @@ -92,7 +92,7 @@ TEST_F(ResolverVarLetValidationTest, LetConstructorWrongTypeViaAlias) { TEST_F(ResolverVarLetValidationTest, VarConstructorWrongTypeViaAlias) { auto* a = Alias("I32", ty.i32()); WrapInFunction( - Var(Source{{3, 3}}, "v", a, ast::StorageClass::kNone, Expr(2u))); + Var(Source{{3, 3}}, "v", ty.Of(a), ast::StorageClass::kNone, Expr(2u))); EXPECT_FALSE(r()->Resolve()); EXPECT_EQ( @@ -228,9 +228,9 @@ TEST_F(ResolverVarLetValidationTest, InferredPtrStorageAccessMismatch) { // let p : pointer = &s.inner.arr[2]; // } auto* inner = Structure("Inner", {Member("arr", ty.array())}); - auto* buf = Structure("S", {Member("inner", inner)}, + auto* buf = Structure("S", {Member("inner", ty.Of(inner))}, {create()}); - auto* storage = Global("s", buf, ast::StorageClass::kStorage, + auto* storage = Global("s", ty.Of(buf), ast::StorageClass::kStorage, ast::DecorationList{ create(0), create(0), diff --git a/src/sem/struct.cc b/src/sem/struct.cc index f8eefc1474..536599771b 100644 --- a/src/sem/struct.cc +++ b/src/sem/struct.cc @@ -13,11 +13,13 @@ // limitations under the License. #include "src/sem/struct.h" -#include "src/ast/struct_member.h" #include #include +#include "src/ast/struct_member.h" +#include "src/symbol_table.h" + TINT_INSTANTIATE_TYPEINFO(tint::sem::Struct); TINT_INSTANTIATE_TYPEINFO(tint::sem::StructMember); @@ -51,7 +53,7 @@ std::string Struct::type_name() const { } std::string Struct::FriendlyName(const SymbolTable& symbols) const { - return declaration_->FriendlyName(symbols); + return symbols.NameFor(declaration_->name()); } StructMember::StructMember(ast::StructMember* declaration, diff --git a/src/sem/type_mappings.h b/src/sem/type_mappings.h index 7193547d1f..f4fd7bb15f 100644 --- a/src/sem/type_mappings.h +++ b/src/sem/type_mappings.h @@ -28,6 +28,7 @@ class MemberAccessorExpression; class Statement; class StructMember; class Type; +class TypeDecl; class Variable; } // namespace ast @@ -57,6 +58,7 @@ struct TypeMappings { Statement* operator()(ast::Statement*); StructMember* operator()(ast::StructMember*); Type* operator()(ast::Type*); + Type* operator()(ast::TypeDecl*); Variable* operator()(ast::Variable*); //! @endcond }; diff --git a/src/transform/decompose_storage_access.cc b/src/transform/decompose_storage_access.cc index c89288cb82..9de497cb82 100644 --- a/src/transform/decompose_storage_access.cc +++ b/src/transform/decompose_storage_access.cc @@ -332,7 +332,7 @@ void InsertGlobal(CloneContext& ctx, } /// @returns the unwrapped, user-declared constructed type of ty. -const ast::NamedType* ConstructedTypeOf(const sem::Type* ty) { +const ast::TypeDecl* ConstructedTypeOf(const sem::Type* ty) { while (true) { if (auto* ref = ty->As()) { ty = ref->StoreType(); @@ -414,7 +414,7 @@ struct DecomposeStorageAccess::State { /// @param var_user the variable user /// @return the name of the function that performs the load Symbol LoadFunc(CloneContext& ctx, - const ast::NamedType* insert_after, + const ast::TypeDecl* insert_after, const sem::Type* buf_ty, const sem::Type* el_ty, const sem::VariableUser* var_user) { @@ -490,7 +490,7 @@ struct DecomposeStorageAccess::State { /// @param var_user the variable user /// @return the name of the function that performs the store Symbol StoreFunc(CloneContext& ctx, - const ast::NamedType* insert_after, + const ast::TypeDecl* insert_after, const sem::Type* buf_ty, const sem::Type* el_ty, const sem::VariableUser* var_user) { diff --git a/src/transform/first_index_offset.cc b/src/transform/first_index_offset.cc index b8fb7c225e..4a69c63931 100644 --- a/src/transform/first_index_offset.cc +++ b/src/transform/first_index_offset.cc @@ -133,14 +133,14 @@ Output FirstIndexOffset::Run(const Program* in, const DataMap& data) { instance_index_offset = offset; offset += 4; } - auto* struct_type = + auto* struct_ = ctx.dst->Structure(ctx.dst->Sym(), std::move(members), {ctx.dst->create()}); // Create a global to hold the uniform buffer Symbol buffer_name = ctx.dst->Sym(); - ctx.dst->Global(buffer_name, struct_type, ast::StorageClass::kUniform, - nullptr, + ctx.dst->Global(buffer_name, ctx.dst->ty.Of(struct_), + ast::StorageClass::kUniform, nullptr, ast::DecorationList{ ctx.dst->create(ub_binding), ctx.dst->create(ub_group), diff --git a/src/transform/single_entry_point.cc b/src/transform/single_entry_point.cc index dec6b09a12..26f12ecf9e 100644 --- a/src/transform/single_entry_point.cc +++ b/src/transform/single_entry_point.cc @@ -69,7 +69,7 @@ Output SingleEntryPoint::Run(const Program* in, const DataMap& data) { // Clone any module-scope variables, types, and functions that are statically // referenced by the target entry point. for (auto* decl : in->AST().GlobalDeclarations()) { - if (auto* ty = decl->As()) { + if (auto* ty = decl->As()) { // TODO(jrprice): Strip unused types. out.AST().AddConstructedType(ctx.Clone(ty)); } else if (auto* var = decl->As()) { diff --git a/src/transform/vertex_pulling.cc b/src/transform/vertex_pulling.cc index 9f3b3bf5c2..713c1127f0 100644 --- a/src/transform/vertex_pulling.cc +++ b/src/transform/vertex_pulling.cc @@ -216,8 +216,8 @@ struct State { for (uint32_t i = 0; i < cfg.vertex_state.size(); ++i) { // The decorated variable with struct type ctx.dst->Global( - GetVertexBufferName(i), struct_type, ast::StorageClass::kStorage, - ast::Access::kRead, + GetVertexBufferName(i), ctx.dst->ty.Of(struct_type), + ast::StorageClass::kStorage, ast::Access::kRead, ast::DecorationList{ ctx.dst->create(i), ctx.dst->create(cfg.pulling_group), @@ -485,7 +485,8 @@ struct State { auto* new_struct = ctx.dst->Structure(ctx.dst->Sym(), new_members); // Create a new function parameter with this struct. - auto* new_param = ctx.dst->Param(ctx.dst->Sym(), new_struct); + auto* new_param = + ctx.dst->Param(ctx.dst->Sym(), ctx.dst->ty.Of(new_struct)); new_function_parameters.push_back(new_param); // Copy values from the new parameter to the function-scope variable. diff --git a/src/writer/hlsl/generator_impl.h b/src/writer/hlsl/generator_impl.h index 3aa49586b9..57de7a374f 100644 --- a/src/writer/hlsl/generator_impl.h +++ b/src/writer/hlsl/generator_impl.h @@ -392,6 +392,12 @@ class GeneratorImpl : public TextGenerator { return builder_.TypeOf(type); } + /// @returns the resolved type of the ast::TypeDecl `type_decl` + /// @param type_decl the type + const sem::Type* TypeOf(const ast::TypeDecl* type_decl) const { + return builder_.TypeOf(type_decl); + } + /// Emits `prefix`, followed by an opening brace `{`, then calls `cb` to emit /// the block body, then finally emits the closing brace `}`. /// @param out the output stream diff --git a/src/writer/hlsl/generator_impl_constructor_test.cc b/src/writer/hlsl/generator_impl_constructor_test.cc index 2bee83b1cc..8593fff3b1 100644 --- a/src/writer/hlsl/generator_impl_constructor_test.cc +++ b/src/writer/hlsl/generator_impl_constructor_test.cc @@ -201,7 +201,7 @@ TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_Type_Struct) { Member("c", ty.vec3()), }); - WrapInFunction(Construct(str, 1, 2.0f, vec3(3, 4, 5))); + WrapInFunction(Construct(ty.Of(str), 1, 2.0f, vec3(3, 4, 5))); GeneratorImpl& gen = SanitizeAndBuild(); @@ -218,7 +218,7 @@ TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_Type_Struct_Empty) { Member("c", ty.vec3()), }); - WrapInFunction(Construct(str)); + WrapInFunction(Construct(ty.Of(str))); GeneratorImpl& gen = SanitizeAndBuild(); diff --git a/src/writer/hlsl/generator_impl_function_test.cc b/src/writer/hlsl/generator_impl_function_test.cc index e3b3d377ab..7c9f54e628 100644 --- a/src/writer/hlsl/generator_impl_function_test.cc +++ b/src/writer/hlsl/generator_impl_function_test.cc @@ -205,12 +205,12 @@ TEST_F(HlslGeneratorImplTest_Function, Member("col2", ty.f32(), {Location(2)}), }); - Func("vert_main", {}, interface_struct, - {Return(Construct(interface_struct, Construct(ty.vec4()), + Func("vert_main", {}, ty.Of(interface_struct), + {Return(Construct(ty.Of(interface_struct), Construct(ty.vec4()), Expr(0.5f), Expr(0.25f)))}, {Stage(ast::PipelineStage::kVertex)}); - Func("frag_main", {Param("inputs", interface_struct)}, ty.void_(), + Func("frag_main", {Param("inputs", ty.Of(interface_struct))}, ty.void_(), { Decl(Const("r", ty.f32(), MemberAccessor("inputs", "col1"))), Decl(Const("g", ty.f32(), MemberAccessor("inputs", "col2"))), @@ -274,19 +274,20 @@ TEST_F(HlslGeneratorImplTest_Function, "VertexOutput", {Member("pos", ty.vec4(), {Builtin(ast::Builtin::kPosition)})}); - Func("foo", {Param("x", ty.f32())}, vertex_output_struct, - {Return(Construct(vertex_output_struct, + Func("foo", {Param("x", ty.f32())}, ty.Of(vertex_output_struct), + {Return(Construct(ty.Of(vertex_output_struct), Construct(ty.vec4(), "x", "x", "x", Expr(1.f))))}, {}); - Func("vert_main1", {}, vertex_output_struct, - {Return(Construct(vertex_output_struct, Expr(Call("foo", Expr(0.5f)))))}, + Func("vert_main1", {}, ty.Of(vertex_output_struct), + {Return(Construct(ty.Of(vertex_output_struct), + Expr(Call("foo", Expr(0.5f)))))}, {Stage(ast::PipelineStage::kVertex)}); - Func( - "vert_main2", {}, vertex_output_struct, - {Return(Construct(vertex_output_struct, Expr(Call("foo", Expr(0.25f)))))}, - {Stage(ast::PipelineStage::kVertex)}); + Func("vert_main2", {}, ty.Of(vertex_output_struct), + {Return(Construct(ty.Of(vertex_output_struct), + Expr(Call("foo", Expr(0.25f)))))}, + {Stage(ast::PipelineStage::kVertex)}); GeneratorImpl& gen = SanitizeAndBuild(); @@ -327,7 +328,7 @@ TEST_F(HlslGeneratorImplTest_Function, Emit_Decoration_EntryPoint_With_Uniform) { auto* ubo_ty = Structure("UBO", {Member("coord", ty.vec4())}, {create()}); - auto* ubo = Global("ubo", ubo_ty, ast::StorageClass::kUniform, + auto* ubo = Global("ubo", ty.Of(ubo_ty), ast::StorageClass::kUniform, ast::DecorationList{ create(0), create(1), @@ -382,7 +383,7 @@ TEST_F(HlslGeneratorImplTest_Function, auto* s = Structure("Uniforms", {Member("coord", ty.vec4())}, {create()}); - Global("uniforms", s, ast::StorageClass::kUniform, + Global("uniforms", ty.Of(s), ast::StorageClass::kUniform, ast::DecorationList{ create(0), create(1), @@ -428,7 +429,8 @@ TEST_F(HlslGeneratorImplTest_Function, }, {create()}); - Global("coord", s, ast::StorageClass::kStorage, ast::Access::kReadWrite, + Global("coord", ty.Of(s), ast::StorageClass::kStorage, + ast::Access::kReadWrite, ast::DecorationList{ create(0), create(1), @@ -472,7 +474,7 @@ TEST_F(HlslGeneratorImplTest_Function, }, {create()}); - Global("coord", s, ast::StorageClass::kStorage, ast::Access::kRead, + Global("coord", ty.Of(s), ast::StorageClass::kStorage, ast::Access::kRead, ast::DecorationList{ create(0), create(1), @@ -516,7 +518,7 @@ TEST_F(HlslGeneratorImplTest_Function, }, {create()}); - Global("coord", s, ast::StorageClass::kStorage, ast::Access::kWrite, + Global("coord", ty.Of(s), ast::StorageClass::kStorage, ast::Access::kWrite, ast::DecorationList{ create(0), create(1), @@ -557,7 +559,8 @@ TEST_F(HlslGeneratorImplTest_Function, }, {create()}); - Global("coord", s, ast::StorageClass::kStorage, ast::Access::kReadWrite, + Global("coord", ty.Of(s), ast::StorageClass::kStorage, + ast::Access::kReadWrite, ast::DecorationList{ create(0), create(1), @@ -756,7 +759,7 @@ TEST_F(HlslGeneratorImplTest_Function, Emit_Decoration_Called_By_EntryPoint_With_Uniform) { auto* s = Structure("S", {Member("x", ty.f32())}, {create()}); - Global("coord", s, ast::StorageClass::kUniform, + Global("coord", ty.Of(s), ast::StorageClass::kUniform, ast::DecorationList{ create(0), create(1), @@ -806,7 +809,8 @@ TEST_F(HlslGeneratorImplTest_Function, Emit_Decoration_Called_By_EntryPoint_With_StorageBuffer) { auto* s = Structure("S", {Member("x", ty.f32())}, {create()}); - Global("coord", s, ast::StorageClass::kStorage, ast::Access::kReadWrite, + Global("coord", ty.Of(s), ast::StorageClass::kStorage, + ast::Access::kReadWrite, ast::DecorationList{ create(0), create(1), @@ -1055,7 +1059,7 @@ TEST_F(HlslGeneratorImplTest_Function, auto* s = Structure("Data", {Member("d", ty.f32())}, {create()}); - Global("data", s, ast::StorageClass::kStorage, ast::Access::kReadWrite, + Global("data", ty.Of(s), ast::StorageClass::kStorage, ast::Access::kReadWrite, ast::DecorationList{ create(0), create(0), diff --git a/src/writer/hlsl/generator_impl_member_accessor_test.cc b/src/writer/hlsl/generator_impl_member_accessor_test.cc index db9d468c23..bfcfca71cb 100644 --- a/src/writer/hlsl/generator_impl_member_accessor_test.cc +++ b/src/writer/hlsl/generator_impl_member_accessor_test.cc @@ -98,7 +98,8 @@ class HlslGeneratorImplTest_MemberAccessorBase : public BASE { auto* s = b.Structure("Data", members, {b.create()}); - b.Global("data", s, ast::StorageClass::kStorage, ast::Access::kReadWrite, + b.Global("data", b.ty.Of(s), ast::StorageClass::kStorage, + ast::Access::kReadWrite, ast::DecorationList{ b.create(0), b.create(1), @@ -123,7 +124,7 @@ using HlslGeneratorImplTest_MemberAccessorWithParam = TEST_F(HlslGeneratorImplTest_MemberAccessor, EmitExpression_MemberAccessor) { auto* s = Structure("Data", {Member("mem", ty.f32())}); - Global("str", s, ast::StorageClass::kPrivate); + Global("str", ty.Of(s), ast::StorageClass::kPrivate); auto* expr = MemberAccessor("str", "mem"); WrapInFunction(Var("expr", ty.f32(), ast::StorageClass::kNone, expr)); @@ -521,7 +522,7 @@ TEST_F(HlslGeneratorImplTest_MemberAccessor, StorageBuffer_Load_MultiLevel) { }); SetupStorageBuffer({ - Member("c", ty.array(inner, 4, 32)), + Member("c", ty.array(ty.Of(inner), 4, 32)), }); SetupFunction({ @@ -567,7 +568,7 @@ TEST_F(HlslGeneratorImplTest_MemberAccessor, }); SetupStorageBuffer({ - Member("c", ty.array(inner, 4, 32)), + Member("c", ty.array(ty.Of(inner), 4, 32)), }); SetupFunction({ @@ -615,7 +616,7 @@ TEST_F(HlslGeneratorImplTest_MemberAccessor, }); SetupStorageBuffer({ - Member("c", ty.array(inner, 4, 32)), + Member("c", ty.array(ty.Of(inner), 4, 32)), }); SetupFunction({ @@ -663,7 +664,7 @@ TEST_F(HlslGeneratorImplTest_MemberAccessor, }); SetupStorageBuffer({ - Member("c", ty.array(inner, 4, 32)), + Member("c", ty.array(ty.Of(inner), 4, 32)), }); SetupFunction({ @@ -710,7 +711,7 @@ TEST_F(HlslGeneratorImplTest_MemberAccessor, StorageBuffer_Store_MultiLevel) { }); SetupStorageBuffer({ - Member("c", ty.array(inner, 4, 32)), + Member("c", ty.array(ty.Of(inner), 4, 32)), }); SetupFunction({ @@ -755,7 +756,7 @@ TEST_F(HlslGeneratorImplTest_MemberAccessor, }); SetupStorageBuffer({ - Member("c", ty.array(inner, 4, 32)), + Member("c", ty.array(ty.Of(inner), 4, 32)), }); SetupFunction({ diff --git a/src/writer/hlsl/generator_impl_sanitizer_test.cc b/src/writer/hlsl/generator_impl_sanitizer_test.cc index 25a584aee3..a848822c5e 100644 --- a/src/writer/hlsl/generator_impl_sanitizer_test.cc +++ b/src/writer/hlsl/generator_impl_sanitizer_test.cc @@ -34,7 +34,7 @@ TEST_F(HlslSanitizerTest, ArrayLength) { create(), }); - Global("sb", sb_ty, ast::StorageClass::kStorage, ast::Access::kRead, + Global("sb", ty.Of(sb_ty), ast::StorageClass::kStorage, ast::Access::kRead, ast::DecorationList{ create(0), create(1), @@ -103,7 +103,7 @@ TEST_F(HlslSanitizerTest, PromoteStructInitializerToConstVar) { Member("b", ty.vec3()), Member("c", ty.i32()), }); - auto* struct_init = Construct(str, 1, vec3(2.f, 3.f, 4.f), 4); + auto* struct_init = Construct(ty.Of(str), 1, vec3(2.f, 3.f, 4.f), 4); auto* struct_access = MemberAccessor(struct_init, "b"); auto* pos = Var("pos", ty.vec3(), ast::StorageClass::kNone, struct_access); diff --git a/src/writer/hlsl/generator_impl_type_test.cc b/src/writer/hlsl/generator_impl_type_test.cc index 9846c30eb0..24e004d5fe 100644 --- a/src/writer/hlsl/generator_impl_type_test.cc +++ b/src/writer/hlsl/generator_impl_type_test.cc @@ -170,7 +170,7 @@ TEST_F(HlslGeneratorImplTest_Type, EmitType_StructDecl) { Member("a", ty.i32()), Member("b", ty.f32()), }); - Global("g", s, ast::StorageClass::kPrivate); + Global("g", ty.Of(s), ast::StorageClass::kPrivate); GeneratorImpl& gen = Build(); @@ -190,7 +190,7 @@ TEST_F(HlslGeneratorImplTest_Type, EmitType_StructDecl_OmittedIfStorageBuffer) { Member("b", ty.f32()), }, {create()}); - Global("g", s, ast::StorageClass::kStorage, ast::Access::kReadWrite, + Global("g", ty.Of(s), ast::StorageClass::kStorage, ast::Access::kReadWrite, ast::DecorationList{ create(0), create(0), @@ -208,7 +208,7 @@ TEST_F(HlslGeneratorImplTest_Type, EmitType_Struct) { Member("a", ty.i32()), Member("b", ty.f32()), }); - Global("g", s, ast::StorageClass::kPrivate); + Global("g", ty.Of(s), ast::StorageClass::kPrivate); GeneratorImpl& gen = Build(); @@ -228,7 +228,7 @@ TEST_F(HlslGeneratorImplTest_Type, DISABLED_EmitType_Struct_InjectPadding) { Member("b", ty.f32()), Member("c", ty.f32(), {MemberAlign(128), MemberSize(128)}), }); - Global("g", s, ast::StorageClass::kPrivate); + Global("g", ty.Of(s), ast::StorageClass::kPrivate); GeneratorImpl& gen = Build(); @@ -252,7 +252,7 @@ TEST_F(HlslGeneratorImplTest_Type, EmitType_Struct_NameCollision) { Member("double", ty.i32()), Member("float", ty.f32()), }); - Global("g", s, ast::StorageClass::kPrivate); + Global("g", ty.Of(s), ast::StorageClass::kPrivate); GeneratorImpl& gen = SanitizeAndBuild(); @@ -272,7 +272,7 @@ TEST_F(HlslGeneratorImplTest_Type, DISABLED_EmitType_Struct_WithDecoration) { Member("b", ty.f32()), }, {create()}); - Global("g", s, ast::StorageClass::kPrivate); + Global("g", ty.Of(s), ast::StorageClass::kPrivate); GeneratorImpl& gen = Build(); diff --git a/src/writer/hlsl/generator_impl_workgroup_var_test.cc b/src/writer/hlsl/generator_impl_workgroup_var_test.cc index f48920de0f..19ab36f31e 100644 --- a/src/writer/hlsl/generator_impl_workgroup_var_test.cc +++ b/src/writer/hlsl/generator_impl_workgroup_var_test.cc @@ -41,7 +41,7 @@ TEST_F(HlslGeneratorImplTest_WorkgroupVar, Basic) { TEST_F(HlslGeneratorImplTest_WorkgroupVar, Aliased) { auto* alias = Alias("F32", ty.f32()); - Global("wg", alias, ast::StorageClass::kWorkgroup); + Global("wg", ty.Of(alias), ast::StorageClass::kWorkgroup); Func("main", {}, ty.void_(), {Assign("wg", 1.2f)}, {Stage(ast::PipelineStage::kCompute)}); diff --git a/src/writer/msl/generator_impl.cc b/src/writer/msl/generator_impl.cc index 90b359c555..9ebb14563d 100644 --- a/src/writer/msl/generator_impl.cc +++ b/src/writer/msl/generator_impl.cc @@ -87,9 +87,9 @@ bool GeneratorImpl::Generate() { global_variables_.set(global->symbol(), sem); } - for (auto* const ty : program_->AST().ConstructedTypes()) { - if (!ty->Is()) { - if (!EmitConstructedType(TypeOf(ty))) { + for (auto* const type_decl : program_->AST().ConstructedTypes()) { + if (!type_decl->Is()) { + if (!EmitConstructedType(TypeOf(type_decl))) { return false; } } diff --git a/src/writer/msl/generator_impl.h b/src/writer/msl/generator_impl.h index e1c420646a..72e37391b9 100644 --- a/src/writer/msl/generator_impl.h +++ b/src/writer/msl/generator_impl.h @@ -286,6 +286,12 @@ class GeneratorImpl : public TextGenerator { return program_->TypeOf(type); } + /// @returns the resolved type of the ast::TypeDecl `type_decl` + /// @param type_decl the type declaration + const sem::Type* TypeOf(const ast::TypeDecl* type_decl) const { + return program_->TypeOf(type_decl); + } + // A pair of byte size and alignment `uint32_t`s. struct SizeAndAlign { uint32_t size; diff --git a/src/writer/msl/generator_impl_constructor_test.cc b/src/writer/msl/generator_impl_constructor_test.cc index 9a44da3bdf..c26375cea3 100644 --- a/src/writer/msl/generator_impl_constructor_test.cc +++ b/src/writer/msl/generator_impl_constructor_test.cc @@ -160,7 +160,7 @@ TEST_F(MslGeneratorImplTest, EmitConstructor_Type_Struct) { Member("c", ty.vec3()), }); - WrapInFunction(Construct(str, 1, 2.0f, vec3(3, 4, 5))); + WrapInFunction(Construct(ty.Of(str), 1, 2.0f, vec3(3, 4, 5))); GeneratorImpl& gen = Build(); @@ -171,7 +171,7 @@ TEST_F(MslGeneratorImplTest, EmitConstructor_Type_Struct) { TEST_F(MslGeneratorImplTest, EmitConstructor_Type_Struct_Empty) { auto* str = Structure("S", {}); - WrapInFunction(Construct(str)); + WrapInFunction(Construct(ty.Of(str))); GeneratorImpl& gen = Build(); diff --git a/src/writer/msl/generator_impl_function_test.cc b/src/writer/msl/generator_impl_function_test.cc index 66df0a34aa..a331b4eb58 100644 --- a/src/writer/msl/generator_impl_function_test.cc +++ b/src/writer/msl/generator_impl_function_test.cc @@ -175,12 +175,12 @@ TEST_F(MslGeneratorImplTest, Member("pos", ty.vec4(), {Builtin(ast::Builtin::kPosition)}), }); - Func("vert_main", {}, interface_struct, - {Return(Construct(interface_struct, Expr(0.5f), Expr(0.25f), + Func("vert_main", {}, ty.Of(interface_struct), + {Return(Construct(ty.Of(interface_struct), Expr(0.5f), Expr(0.25f), Construct(ty.vec4())))}, {Stage(ast::PipelineStage::kVertex)}); - Func("frag_main", {Param("colors", interface_struct)}, ty.void_(), + Func("frag_main", {Param("colors", ty.Of(interface_struct))}, ty.void_(), { WrapInStatement( Const("r", ty.f32(), MemberAccessor("colors", "col1"))), @@ -246,16 +246,16 @@ TEST_F(MslGeneratorImplTest, "VertexOutput", {Member("pos", ty.vec4(), {Builtin(ast::Builtin::kPosition)})}); - Func("foo", {Param("x", ty.f32())}, vertex_output_struct, - {Return(Construct(vertex_output_struct, + Func("foo", {Param("x", ty.f32())}, ty.Of(vertex_output_struct), + {Return(Construct(ty.Of(vertex_output_struct), Construct(ty.vec4(), "x", "x", "x", Expr(1.f))))}, {}); - Func("vert_main1", {}, vertex_output_struct, + Func("vert_main1", {}, ty.Of(vertex_output_struct), {Return(Expr(Call("foo", Expr(0.5f))))}, {Stage(ast::PipelineStage::kVertex)}); - Func("vert_main2", {}, vertex_output_struct, + Func("vert_main2", {}, ty.Of(vertex_output_struct), {Return(Expr(Call("foo", Expr(0.25f))))}, {Stage(ast::PipelineStage::kVertex)}); @@ -306,7 +306,8 @@ TEST_F(MslGeneratorImplTest, }, {create()}); - Global("coord", s, ast::StorageClass::kStorage, ast::Access::kReadWrite, + Global("coord", ty.Of(s), ast::StorageClass::kStorage, + ast::Access::kReadWrite, ast::DecorationList{ create(0), create(1), @@ -352,7 +353,7 @@ TEST_F(MslGeneratorImplTest, }, {create()}); - Global("coord", s, ast::StorageClass::kStorage, ast::Access::kRead, + Global("coord", ty.Of(s), ast::StorageClass::kStorage, ast::Access::kRead, ast::DecorationList{ create(0), create(1), @@ -558,7 +559,7 @@ TEST_F(MslGeneratorImplTest, Emit_Decoration_Called_By_EntryPoint_With_Uniform) { auto* ubo_ty = Structure("UBO", {Member("coord", ty.vec4())}, {create()}); - auto* ubo = Global("ubo", ubo_ty, ast::StorageClass::kUniform, + auto* ubo = Global("ubo", ty.Of(ubo_ty), ast::StorageClass::kUniform, ast::DecorationList{ create(0), create(1), @@ -616,7 +617,8 @@ TEST_F(MslGeneratorImplTest, }, {create()}); - Global("coord", s, ast::StorageClass::kStorage, ast::Access::kReadWrite, + Global("coord", ty.Of(s), ast::StorageClass::kStorage, + ast::Access::kReadWrite, ast::DecorationList{ create(0), create(1), @@ -673,7 +675,7 @@ TEST_F(MslGeneratorImplTest, }, {create()}); - Global("coord", s, ast::StorageClass::kStorage, ast::Access::kRead, + Global("coord", ty.Of(s), ast::StorageClass::kStorage, ast::Access::kRead, ast::DecorationList{ create(0), create(1), @@ -816,7 +818,7 @@ TEST_F(MslGeneratorImplTest, auto* s = Structure("Data", {Member("d", ty.f32())}, {create()}); - Global("data", s, ast::StorageClass::kStorage, ast::Access::kReadWrite, + Global("data", ty.Of(s), ast::StorageClass::kStorage, ast::Access::kReadWrite, ast::DecorationList{ create(0), create(0), diff --git a/src/writer/msl/generator_impl_member_accessor_test.cc b/src/writer/msl/generator_impl_member_accessor_test.cc index bb7089d5a0..2047e22b18 100644 --- a/src/writer/msl/generator_impl_member_accessor_test.cc +++ b/src/writer/msl/generator_impl_member_accessor_test.cc @@ -22,7 +22,7 @@ namespace { using MslGeneratorImplTest = TestHelper; TEST_F(MslGeneratorImplTest, EmitExpression_MemberAccessor) { - Global("str", Structure("my_str", {Member("mem", ty.f32())}), + Global("str", ty.Of(Structure("my_str", {Member("mem", ty.f32())})), ast::StorageClass::kPrivate); auto* expr = MemberAccessor("str", "mem"); WrapInFunction(expr); diff --git a/src/writer/msl/generator_impl_type_test.cc b/src/writer/msl/generator_impl_type_test.cc index ffd88b68c1..349995a0e5 100644 --- a/src/writer/msl/generator_impl_type_test.cc +++ b/src/writer/msl/generator_impl_type_test.cc @@ -233,7 +233,7 @@ TEST_F(MslGeneratorImplTest, EmitType_Struct_Layout_NonComposites) { }, {create()}); - Global("G", s, ast::StorageClass::kStorage, ast::Access::kRead, + Global("G", ty.Of(s), ast::StorageClass::kStorage, ast::Access::kRead, ast::DecorationList{ create(0), create(0), @@ -335,14 +335,14 @@ TEST_F(MslGeneratorImplTest, EmitType_Struct_Layout_Structures) { auto* s = Structure("S", { Member("a", ty.i32()), - Member("b", inner_x), + Member("b", ty.Of(inner_x)), Member("c", ty.f32()), - Member("d", inner_y), + Member("d", ty.Of(inner_y)), Member("e", ty.f32()), }, {create()}); - Global("G", s, ast::StorageClass::kStorage, ast::Access::kRead, + Global("G", ty.Of(s), ast::StorageClass::kStorage, ast::Access::kRead, ast::DecorationList{ create(0), create(0), @@ -419,7 +419,7 @@ TEST_F(MslGeneratorImplTest, EmitType_Struct_Layout_ArrayDefaultStride) { auto* array_x = ty.array(); // array_y: size(4096), align(512) - auto* array_y = ty.array(inner, 4); + auto* array_y = ty.array(ty.Of(inner), 4); // array_z: size(4), align(4) auto* array_z = ty.array(); @@ -436,7 +436,7 @@ TEST_F(MslGeneratorImplTest, EmitType_Struct_Layout_ArrayDefaultStride) { }, ast::DecorationList{create()}); - Global("G", s, ast::StorageClass::kStorage, ast::Access::kRead, + Global("G", ty.Of(s), ast::StorageClass::kStorage, ast::Access::kRead, ast::DecorationList{ create(0), create(0), @@ -549,7 +549,7 @@ TEST_F(MslGeneratorImplTest, AttemptTintPadSymbolCollision) { }, {create()}); - Global("G", s, ast::StorageClass::kStorage, ast::Access::kRead, + Global("G", ty.Of(s), ast::StorageClass::kStorage, ast::Access::kRead, ast::DecorationList{ create(0), create(0), @@ -614,7 +614,7 @@ TEST_F(MslGeneratorImplTest, DISABLED_EmitType_Struct_WithDecoration) { }, {create()}); - Global("G", s, ast::StorageClass::kStorage, ast::Access::kRead, + Global("G", ty.Of(s), ast::StorageClass::kStorage, ast::Access::kRead, ast::DecorationList{ create(0), create(0), diff --git a/src/writer/msl/generator_impl_variable_decl_statement_test.cc b/src/writer/msl/generator_impl_variable_decl_statement_test.cc index f4030972ba..68e2983c78 100644 --- a/src/writer/msl/generator_impl_variable_decl_statement_test.cc +++ b/src/writer/msl/generator_impl_variable_decl_statement_test.cc @@ -70,7 +70,7 @@ TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Struct) { Member("b", ty.f32()), }); - auto* var = Var("a", s, ast::StorageClass::kNone); + auto* var = Var("a", ty.Of(s), ast::StorageClass::kNone); auto* stmt = Decl(var); WrapInFunction(stmt); diff --git a/src/writer/spirv/builder_accessor_expression_test.cc b/src/writer/spirv/builder_accessor_expression_test.cc index 19da1a4daf..b77bae2663 100644 --- a/src/writer/spirv/builder_accessor_expression_test.cc +++ b/src/writer/spirv/builder_accessor_expression_test.cc @@ -224,7 +224,7 @@ TEST_F(BuilderTest, MemberAccessor) { Member("b", ty.f32()), }); - auto* var = Var("ident", s); + auto* var = Var("ident", ty.Of(s)); auto* expr = MemberAccessor("ident", "b"); WrapInFunction(var, expr); @@ -268,9 +268,9 @@ TEST_F(BuilderTest, MemberAccessor_Nested) { Member("b", ty.f32()), }); - auto* s_type = Structure("my_struct", {Member("inner", inner_struct)}); + auto* s_type = Structure("my_struct", {Member("inner", ty.Of(inner_struct))}); - auto* var = Var("ident", s_type); + auto* var = Var("ident", ty.Of(s_type)); auto* expr = MemberAccessor(MemberAccessor("ident", "inner"), "b"); WrapInFunction(var, expr); @@ -312,7 +312,7 @@ TEST_F(BuilderTest, MemberAccessor_NonPointer) { Member("b", ty.f32()), }); - auto* var = Const("ident", s, Construct(s, 0.f, 0.f)); + auto* var = Const("ident", ty.Of(s), Construct(ty.Of(s), 0.f, 0.f)); auto* expr = MemberAccessor("ident", "b"); WrapInFunction(var, expr); @@ -350,10 +350,11 @@ TEST_F(BuilderTest, MemberAccessor_Nested_NonPointer) { Member("b", ty.f32()), }); - auto* s_type = Structure("my_struct", {Member("inner", inner_struct)}); + auto* s_type = Structure("my_struct", {Member("inner", ty.Of(inner_struct))}); - auto* var = Const("ident", s_type, - Construct(s_type, Construct(inner_struct, 0.f, 0.f))); + auto* var = + Const("ident", ty.Of(s_type), + Construct(ty.Of(s_type), Construct(ty.Of(inner_struct), 0.f, 0.f))); auto* expr = MemberAccessor(MemberAccessor("ident", "inner"), "b"); WrapInFunction(var, expr); @@ -394,10 +395,10 @@ TEST_F(BuilderTest, MemberAccessor_Nested_WithAlias) { Member("b", ty.f32()), }); - auto* alias = Alias("Alias", inner_struct); - auto* s_type = Structure("Outer", {Member("inner", alias)}); + auto* alias = Alias("Alias", ty.Of(inner_struct)); + auto* s_type = Structure("Outer", {Member("inner", ty.Of(alias))}); - auto* var = Var("ident", s_type); + auto* var = Var("ident", ty.Of(s_type)); auto* expr = MemberAccessor(MemberAccessor("ident", "inner"), "a"); WrapInFunction(var, expr); @@ -440,9 +441,9 @@ TEST_F(BuilderTest, MemberAccessor_Nested_Assignment_LHS) { Member("b", ty.f32()), }); - auto* s_type = Structure("my_struct", {Member("inner", inner_struct)}); + auto* s_type = Structure("my_struct", {Member("inner", ty.Of(inner_struct))}); - auto* var = Var("ident", s_type); + auto* var = Var("ident", ty.Of(s_type)); auto* expr = Assign(MemberAccessor(MemberAccessor("ident", "inner"), "a"), Expr(2.0f)); WrapInFunction(var, expr); @@ -489,9 +490,9 @@ TEST_F(BuilderTest, MemberAccessor_Nested_Assignment_RHS) { Member("b", ty.f32()), }); - auto* s_type = Structure("my_struct", {Member("inner", inner_struct)}); + auto* s_type = Structure("my_struct", {Member("inner", ty.Of(inner_struct))}); - auto* var = Var("ident", s_type); + auto* var = Var("ident", ty.Of(s_type)); auto* store = Var("store", ty.f32()); auto* rhs = MemberAccessor(MemberAccessor("ident", "inner"), "a"); @@ -696,11 +697,11 @@ TEST_F(BuilderTest, ArrayAccessor_Mixed_ArrayAndMember) { auto* c_type = Structure("C", {Member("baz", ty.vec3())}); - auto* b_type = Structure("B", {Member("bar", c_type)}); - auto* b_ary_type = ty.array(b_type, 3); + auto* b_type = Structure("B", {Member("bar", ty.Of(c_type))}); + auto* b_ary_type = ty.array(ty.Of(b_type), 3); auto* a_type = Structure("A", {Member("foo", b_ary_type)}); - auto* a_ary_type = ty.array(a_type, 2); + auto* a_ary_type = ty.array(ty.Of(a_type), 2); auto* var = Var("index", a_ary_type); auto* expr = MemberAccessor( MemberAccessor( diff --git a/src/writer/spirv/builder_assign_test.cc b/src/writer/spirv/builder_assign_test.cc index 4dbf7657f9..c1894b6f7c 100644 --- a/src/writer/spirv/builder_assign_test.cc +++ b/src/writer/spirv/builder_assign_test.cc @@ -181,7 +181,7 @@ TEST_F(BuilderTest, Assign_StructMember) { Member("b", ty.f32()), }); - auto* v = Var("ident", s); + auto* v = Var("ident", ty.Of(s)); auto* assign = Assign(MemberAccessor("ident", "b"), Expr(4.f)); diff --git a/src/writer/spirv/builder_constructor_expression_test.cc b/src/writer/spirv/builder_constructor_expression_test.cc index 5dd4be6a35..3f7e66bdff 100644 --- a/src/writer/spirv/builder_constructor_expression_test.cc +++ b/src/writer/spirv/builder_constructor_expression_test.cc @@ -94,7 +94,7 @@ TEST_F(SpvBuilderConstructorTest, Type_WithAlias) { // cast(2.3f) auto* alias = Alias("Int", ty.i32()); - auto* cast = Construct(alias, 2.3f); + auto* cast = Construct(ty.Of(alias), 2.3f); WrapInFunction(cast); spirv::Builder& b = Build(); @@ -1056,7 +1056,7 @@ TEST_F(SpvBuilderConstructorTest, Type_Struct) { Member("b", ty.vec3()), }); - auto* t = Construct(s, 2.0f, vec3(2.0f, 2.0f, 2.0f)); + auto* t = Construct(ty.Of(s), 2.0f, vec3(2.0f, 2.0f, 2.0f)); WrapInFunction(t); spirv::Builder& b = Build(); @@ -1202,7 +1202,7 @@ TEST_F(SpvBuilderConstructorTest, Type_ZeroInit_Array) { TEST_F(SpvBuilderConstructorTest, Type_ZeroInit_Struct) { auto* s = Structure("my_struct", {Member("a", ty.f32())}); - auto* t = Construct(s); + auto* t = Construct(ty.Of(s)); WrapInFunction(t); spirv::Builder& b = Build(); @@ -1626,7 +1626,7 @@ TEST_F(SpvBuilderConstructorTest, IsConstructorConst_Struct) { Member("b", ty.vec3()), }); - auto* t = Construct(s, 2.f, vec3(2.f, 2.f, 2.f)); + auto* t = Construct(ty.Of(s), 2.f, vec3(2.f, 2.f, 2.f)); WrapInFunction(t); spirv::Builder& b = Build(); @@ -1645,7 +1645,7 @@ TEST_F(SpvBuilderConstructorTest, Global("a", ty.f32(), ast::StorageClass::kPrivate); Global("b", ty.f32(), ast::StorageClass::kPrivate); - auto* t = Construct(s, 2.f, "a", 2.f); + auto* t = Construct(ty.Of(s), 2.f, "a", 2.f); WrapInFunction(t); spirv::Builder& b = Build(); diff --git a/src/writer/spirv/builder_entry_point_test.cc b/src/writer/spirv/builder_entry_point_test.cc index fd98a76cee..0a0720be99 100644 --- a/src/writer/spirv/builder_entry_point_test.cc +++ b/src/writer/spirv/builder_entry_point_test.cc @@ -196,11 +196,12 @@ TEST_F(BuilderTest, EntryPoint_SharedStruct) { ast::DecorationList{Builtin(ast::Builtin::kPosition)}), }); - auto* vert_retval = Construct(interface, 42.f, Construct(ty.vec4())); - Func("vert_main", ast::VariableList{}, interface, {Return(vert_retval)}, - {Stage(ast::PipelineStage::kVertex)}); + auto* vert_retval = + Construct(ty.Of(interface), 42.f, Construct(ty.vec4())); + Func("vert_main", ast::VariableList{}, ty.Of(interface), + {Return(vert_retval)}, {Stage(ast::PipelineStage::kVertex)}); - auto* frag_inputs = Param("inputs", interface); + auto* frag_inputs = Param("inputs", ty.Of(interface)); Func("frag_main", ast::VariableList{frag_inputs}, ty.f32(), {Return(MemberAccessor(Expr("inputs"), "value"))}, {Stage(ast::PipelineStage::kFragment)}, diff --git a/src/writer/spirv/builder_function_test.cc b/src/writer/spirv/builder_function_test.cc index d4ed7fb68e..ad1c8ffc74 100644 --- a/src/writer/spirv/builder_function_test.cc +++ b/src/writer/spirv/builder_function_test.cc @@ -204,7 +204,7 @@ TEST_F(BuilderTest, Emit_Multiple_EntryPoint_With_Same_ModuleVar) { auto* s = Structure("Data", {Member("d", ty.f32())}, {create()}); - Global("data", s, ast::StorageClass::kStorage, ast::Access::kReadWrite, + Global("data", ty.Of(s), ast::StorageClass::kStorage, ast::Access::kReadWrite, ast::DecorationList{ create(0), create(0), diff --git a/src/writer/spirv/builder_global_variable_test.cc b/src/writer/spirv/builder_global_variable_test.cc index 9aa0bcaa09..c11c12cf49 100644 --- a/src/writer/spirv/builder_global_variable_test.cc +++ b/src/writer/spirv/builder_global_variable_test.cc @@ -403,11 +403,12 @@ TEST_F(BuilderTest, GlobalVar_DeclReadOnly) { }, {create()}); - auto* var = Global("b", A, ast::StorageClass::kStorage, ast::Access::kRead, - ast::DecorationList{ - create(0), - create(0), - }); + auto* var = + Global("b", ty.Of(A), ast::StorageClass::kStorage, ast::Access::kRead, + ast::DecorationList{ + create(0), + create(0), + }); spirv::Builder& b = Build(); @@ -441,12 +442,13 @@ TEST_F(BuilderTest, GlobalVar_TypeAliasDeclReadOnly) { auto* A = Structure("A", {Member("a", ty.i32())}, {create()}); - auto* B = Alias("B", A); - auto* var = Global("b", B, ast::StorageClass::kStorage, ast::Access::kRead, - ast::DecorationList{ - create(0), - create(0), - }); + auto* B = Alias("B", ty.Of(A)); + auto* var = + Global("b", ty.Of(B), ast::StorageClass::kStorage, ast::Access::kRead, + ast::DecorationList{ + create(0), + create(0), + }); spirv::Builder& b = Build(); @@ -478,12 +480,13 @@ TEST_F(BuilderTest, GlobalVar_TypeAliasAssignReadOnly) { auto* A = Structure("A", {Member("a", ty.i32())}, {create()}); - auto* B = Alias("B", A); - auto* var = Global("b", B, ast::StorageClass::kStorage, ast::Access::kRead, - ast::DecorationList{ - create(0), - create(0), - }); + auto* B = Alias("B", ty.Of(A)); + auto* var = + Global("b", ty.Of(B), ast::StorageClass::kStorage, ast::Access::kRead, + ast::DecorationList{ + create(0), + create(0), + }); spirv::Builder& b = Build(); @@ -515,17 +518,18 @@ TEST_F(BuilderTest, GlobalVar_TwoVarDeclReadOnly) { auto* A = Structure("A", {Member("a", ty.i32())}, {create()}); - auto* var_b = Global("b", A, ast::StorageClass::kStorage, ast::Access::kRead, - ast::DecorationList{ - create(0), - create(0), - }); - auto* var_c = - Global("c", A, ast::StorageClass::kStorage, ast::Access::kReadWrite, + auto* var_b = + Global("b", ty.Of(A), ast::StorageClass::kStorage, ast::Access::kRead, ast::DecorationList{ - create(1), + create(0), create(0), }); + auto* var_c = Global("c", ty.Of(A), ast::StorageClass::kStorage, + ast::Access::kReadWrite, + ast::DecorationList{ + create(1), + create(0), + }); spirv::Builder& b = Build(); diff --git a/src/writer/spirv/builder_intrinsic_test.cc b/src/writer/spirv/builder_intrinsic_test.cc index a42f96cebc..c1822ec83a 100644 --- a/src/writer/spirv/builder_intrinsic_test.cc +++ b/src/writer/spirv/builder_intrinsic_test.cc @@ -1537,7 +1537,7 @@ OpFunctionEnd TEST_F(IntrinsicBuilderTest, Call_ArrayLength) { auto* s = Structure("my_struct", {Member(0, "a", ty.array(4))}, {create()}); - Global("b", s, ast::StorageClass::kStorage, ast::Access::kRead, + Global("b", ty.Of(s), ast::StorageClass::kStorage, ast::Access::kRead, ast::DecorationList{ create(1), create(2), @@ -1586,7 +1586,7 @@ TEST_F(IntrinsicBuilderTest, Call_ArrayLength_OtherMembersInStruct) { Member(4, "a", ty.array(4)), }, {create()}); - Global("b", s, ast::StorageClass::kStorage, ast::Access::kRead, + Global("b", ty.Of(s), ast::StorageClass::kStorage, ast::Access::kRead, ast::DecorationList{ create(1), create(2), diff --git a/src/writer/spirv/builder_type_test.cc b/src/writer/spirv/builder_type_test.cc index a0fca8ba89..1bae74fdfc 100644 --- a/src/writer/spirv/builder_type_test.cc +++ b/src/writer/spirv/builder_type_test.cc @@ -30,7 +30,7 @@ TEST_F(BuilderTest_Type, GenerateRuntimeArray) { auto* ary = ty.array(ty.i32(), 0); auto* str = Structure("S", {Member("x", ary)}, {create()}); - Global("a", str, ast::StorageClass::kStorage, ast::Access::kRead, + Global("a", ty.Of(str), ast::StorageClass::kStorage, ast::Access::kRead, ast::DecorationList{ create(0), create(0), @@ -51,7 +51,7 @@ TEST_F(BuilderTest_Type, ReturnsGeneratedRuntimeArray) { auto* ary = ty.array(ty.i32(), 0); auto* str = Structure("S", {Member("x", ary)}, {create()}); - Global("a", str, ast::StorageClass::kStorage, ast::Access::kRead, + Global("a", ty.Of(str), ast::StorageClass::kStorage, ast::Access::kRead, ast::DecorationList{ create(0), create(0), diff --git a/src/writer/wgsl/generator_impl.cc b/src/writer/wgsl/generator_impl.cc index 789bb51f67..006b7999dd 100644 --- a/src/writer/wgsl/generator_impl.cc +++ b/src/writer/wgsl/generator_impl.cc @@ -66,8 +66,8 @@ GeneratorImpl::~GeneratorImpl() = default; bool GeneratorImpl::Generate() { // Generate global declarations in the order they appear in the module. for (auto* decl : program_->AST().GlobalDeclarations()) { - if (auto* ty = decl->As()) { - if (!EmitConstructedType(ty)) { + if (auto* td = decl->As()) { + if (!EmitConstructedType(td)) { return false; } } else if (auto* func = decl->As()) { @@ -91,7 +91,7 @@ bool GeneratorImpl::Generate() { return true; } -bool GeneratorImpl::EmitConstructedType(const ast::Type* ty) { +bool GeneratorImpl::EmitConstructedType(const ast::TypeDecl* ty) { make_indent(); if (auto* alias = ty->As()) { @@ -105,7 +105,8 @@ bool GeneratorImpl::EmitConstructedType(const ast::Type* ty) { return false; } } else { - diagnostics_.add_error("unknown constructed type: " + ty->type_name()); + diagnostics_.add_error("unknown constructed type: " + + std::string(ty->TypeInfo().name)); return false; } return true; @@ -374,9 +375,7 @@ bool GeneratorImpl::EmitAccess(const ast::Access access) { } bool GeneratorImpl::EmitType(const ast::Type* ty) { - if (auto* alias = ty->As()) { - out_ << program_->Symbols().NameFor(alias->symbol()); - } else if (auto* ary = ty->As()) { + if (auto* ary = ty->As()) { for (auto* deco : ary->decorations()) { if (auto* stride = deco->As()) { out_ << "[[stride(" << stride->stride() << ")]] "; @@ -416,10 +415,6 @@ bool GeneratorImpl::EmitType(const ast::Type* ty) { if (sampler->IsComparison()) { out_ << "_comparison"; } - } else if (auto* str = ty->As()) { - // The struct, as a type, is just the name. We should have already emitted - // the declaration through a call to |EmitStructType| earlier. - out_ << program_->Symbols().NameFor(str->name()); } else if (ty->Is()) { out_ << "external_texture"; } else if (auto* texture = ty->As()) { diff --git a/src/writer/wgsl/generator_impl.h b/src/writer/wgsl/generator_impl.h index 1017bae022..4d48d76837 100644 --- a/src/writer/wgsl/generator_impl.h +++ b/src/writer/wgsl/generator_impl.h @@ -57,7 +57,7 @@ class GeneratorImpl : public TextGenerator { /// Handles generating a constructed type /// @param ty the constructed to generate /// @returns true if the constructed was emitted - bool EmitConstructedType(const ast::Type* ty); + bool EmitConstructedType(const ast::TypeDecl* ty); /// Handles an array accessor expression /// @param expr the expression to emit /// @returns true if the array accessor was emitted diff --git a/src/writer/wgsl/generator_impl_alias_type_test.cc b/src/writer/wgsl/generator_impl_alias_type_test.cc index f71ee45f7b..43c52706f6 100644 --- a/src/writer/wgsl/generator_impl_alias_type_test.cc +++ b/src/writer/wgsl/generator_impl_alias_type_test.cc @@ -23,6 +23,7 @@ using WgslGeneratorImplTest = TestHelper; TEST_F(WgslGeneratorImplTest, EmitAlias_F32) { auto* alias = Alias("a", ty.f32()); + GeneratorImpl& gen = Build(); ASSERT_TRUE(gen.EmitConstructedType(alias)) << gen.error(); @@ -36,7 +37,7 @@ TEST_F(WgslGeneratorImplTest, EmitConstructedType_Struct) { Member("b", ty.i32()), }); - auto* alias = Alias("B", s); + auto* alias = Alias("B", ty.Of(s)); GeneratorImpl& gen = Build(); @@ -56,7 +57,7 @@ TEST_F(WgslGeneratorImplTest, EmitAlias_ToStruct) { Member("b", ty.i32()), }); - auto* alias = Alias("B", s); + auto* alias = Alias("B", ty.Of(s)); GeneratorImpl& gen = Build(); diff --git a/src/writer/wgsl/generator_impl_function_test.cc b/src/writer/wgsl/generator_impl_function_test.cc index 5bb305a201..fc4fc30aef 100644 --- a/src/writer/wgsl/generator_impl_function_test.cc +++ b/src/writer/wgsl/generator_impl_function_test.cc @@ -175,7 +175,7 @@ TEST_F(WgslGeneratorImplTest, auto* s = Structure("Data", {Member("d", ty.f32())}, {create()}); - Global("data", s, ast::StorageClass::kStorage, ast::Access::kReadWrite, + Global("data", ty.Of(s), ast::StorageClass::kStorage, ast::Access::kReadWrite, ast::DecorationList{ create(0), create(0), diff --git a/src/writer/wgsl/generator_impl_global_decl_test.cc b/src/writer/wgsl/generator_impl_global_decl_test.cc index 5954621c50..fcb5104d2a 100644 --- a/src/writer/wgsl/generator_impl_global_decl_test.cc +++ b/src/writer/wgsl/generator_impl_global_decl_test.cc @@ -61,8 +61,8 @@ TEST_F(WgslGeneratorImplTest, Emit_GlobalsInterleaved) { Func("main", ast::VariableList{}, ty.void_(), ast::StatementList{ - Decl(Var("s0", s0)), - Decl(Var("s1", s1)), + Decl(Var("s0", ty.Of(s0))), + Decl(Var("s1", ty.Of(s1))), Assign("a1", Call("func")), }, ast::DecorationList{ diff --git a/src/writer/wgsl/generator_impl_member_accessor_test.cc b/src/writer/wgsl/generator_impl_member_accessor_test.cc index 03fccc6265..92ad0b8fca 100644 --- a/src/writer/wgsl/generator_impl_member_accessor_test.cc +++ b/src/writer/wgsl/generator_impl_member_accessor_test.cc @@ -23,7 +23,7 @@ using WgslGeneratorImplTest = TestHelper; TEST_F(WgslGeneratorImplTest, EmitExpression_MemberAccessor) { auto* s = Structure("Data", {Member("mem", ty.f32())}); - Global("str", s, ast::StorageClass::kPrivate); + Global("str", ty.Of(s), ast::StorageClass::kPrivate); auto* expr = MemberAccessor("str", "mem"); WrapInFunction(expr); @@ -36,7 +36,7 @@ TEST_F(WgslGeneratorImplTest, EmitExpression_MemberAccessor) { TEST_F(WgslGeneratorImplTest, EmitExpression_MemberAccessor_OfDref) { auto* s = Structure("Data", {Member("mem", ty.f32())}); - Global("str", s, ast::StorageClass::kPrivate); + Global("str", ty.Of(s), ast::StorageClass::kPrivate); auto* p = Const("p", nullptr, AddressOf("str")); auto* expr = MemberAccessor(Deref("p"), "mem"); diff --git a/src/writer/wgsl/generator_impl_type_test.cc b/src/writer/wgsl/generator_impl_type_test.cc index 9acd634f2d..991dc6991d 100644 --- a/src/writer/wgsl/generator_impl_type_test.cc +++ b/src/writer/wgsl/generator_impl_type_test.cc @@ -27,10 +27,12 @@ using WgslGeneratorImplTest = TestHelper; TEST_F(WgslGeneratorImplTest, EmitType_Alias) { auto* alias = Alias("alias", ty.f32()); + auto* alias_ty = ty.Of(alias); + WrapInFunction(Var("make_reachable", alias_ty)); GeneratorImpl& gen = Build(); - ASSERT_TRUE(gen.EmitType(alias)) << gen.error(); + ASSERT_TRUE(gen.EmitType(alias_ty)) << gen.error(); EXPECT_EQ(gen.result(), "alias"); } @@ -119,10 +121,12 @@ TEST_F(WgslGeneratorImplTest, EmitType_Struct) { Member("a", ty.i32()), Member("b", ty.f32()), }); + auto* s_ty = ty.Of(s); + WrapInFunction(Var("make_reachable", s_ty)); GeneratorImpl& gen = Build(); - ASSERT_TRUE(gen.EmitType(s)) << gen.error(); + ASSERT_TRUE(gen.EmitType(s_ty)) << gen.error(); EXPECT_EQ(gen.result(), "S"); } diff --git a/src/writer/wgsl/generator_impl_variable_test.cc b/src/writer/wgsl/generator_impl_variable_test.cc index 8b51c3a188..de4cd1d8b4 100644 --- a/src/writer/wgsl/generator_impl_variable_test.cc +++ b/src/writer/wgsl/generator_impl_variable_test.cc @@ -46,11 +46,12 @@ TEST_F(WgslGeneratorImplTest, EmitVariable_StorageClass) { TEST_F(WgslGeneratorImplTest, EmitVariable_Access_Read) { auto* s = Structure("S", {Member("a", ty.i32())}, {create()}); - auto* v = Global("a", s, ast::StorageClass::kStorage, ast::Access::kRead, - ast::DecorationList{ - create(0), - create(0), - }); + auto* v = + Global("a", ty.Of(s), ast::StorageClass::kStorage, ast::Access::kRead, + ast::DecorationList{ + create(0), + create(0), + }); GeneratorImpl& gen = Build(); @@ -62,11 +63,12 @@ TEST_F(WgslGeneratorImplTest, EmitVariable_Access_Read) { TEST_F(WgslGeneratorImplTest, EmitVariable_Access_Write) { auto* s = Structure("S", {Member("a", ty.i32())}, {create()}); - auto* v = Global("a", s, ast::StorageClass::kStorage, ast::Access::kWrite, - ast::DecorationList{ - create(0), - create(0), - }); + auto* v = + Global("a", ty.Of(s), ast::StorageClass::kStorage, ast::Access::kWrite, + ast::DecorationList{ + create(0), + create(0), + }); GeneratorImpl& gen = Build(); @@ -78,7 +80,8 @@ TEST_F(WgslGeneratorImplTest, EmitVariable_Access_Write) { TEST_F(WgslGeneratorImplTest, EmitVariable_Access_ReadWrite) { auto* s = Structure("S", {Member("a", ty.i32())}, {create()}); - auto* v = Global("a", s, ast::StorageClass::kStorage, ast::Access::kReadWrite, + auto* v = Global("a", ty.Of(s), ast::StorageClass::kStorage, + ast::Access::kReadWrite, ast::DecorationList{ create(0), create(0),