From c63a64b7957836f652cd0693e41d010339c57e42 Mon Sep 17 00:00:00 2001 From: James Price Date: Thu, 25 Mar 2021 21:32:27 +0000 Subject: [PATCH] [msl-writer] Fix struct constructor emission Zero value struct expressions may still be broken (tint:477). Change-Id: I5cf2f13ed891a50e4b8f55ce4b80d2768aa358d9 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/46101 Commit-Queue: Ben Clayton Auto-Submit: James Price Reviewed-by: Ben Clayton --- src/writer/msl/generator_impl.cc | 4 +-- .../msl/generator_impl_constructor_test.cc | 25 +++++++++++++++++-- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/writer/msl/generator_impl.cc b/src/writer/msl/generator_impl.cc index e971d50425..b3142a2e8d 100644 --- a/src/writer/msl/generator_impl.cc +++ b/src/writer/msl/generator_impl.cc @@ -872,7 +872,7 @@ bool GeneratorImpl::EmitContinue(ast::ContinueStatement*) { } bool GeneratorImpl::EmitTypeConstructor(ast::TypeConstructorExpression* expr) { - if (expr->type()->Is()) { + if (expr->type()->IsAnyOf()) { out_ << "{"; } else { if (!EmitType(expr->type(), "")) { @@ -901,7 +901,7 @@ bool GeneratorImpl::EmitTypeConstructor(ast::TypeConstructorExpression* expr) { } } - if (expr->type()->Is()) { + if (expr->type()->IsAnyOf()) { out_ << "}"; } else { out_ << ")"; diff --git a/src/writer/msl/generator_impl_constructor_test.cc b/src/writer/msl/generator_impl_constructor_test.cc index 04bb0ad996..5d43ca869b 100644 --- a/src/writer/msl/generator_impl_constructor_test.cc +++ b/src/writer/msl/generator_impl_constructor_test.cc @@ -154,8 +154,29 @@ TEST_F(MslGeneratorImplTest, EmitConstructor_Type_Array) { "float3(7.0f, 8.0f, 9.0f)}"); } -// TODO(dsinclair): Add struct constructor test. -TEST_F(MslGeneratorImplTest, DISABLED_EmitConstructor_Type_Struct) {} +TEST_F(MslGeneratorImplTest, EmitConstructor_Type_Struct) { + auto* struct_ty = Structure("S", + ast::StructMemberList{ + Member("a", ty.f32()), + Member("b", ty.u32()), + Member("c", ty.vec4()), + }, + ast::DecorationList{}); + + ast::ExpressionList struct_values; + struct_values.push_back(Expr(0.f)); + struct_values.push_back(Expr(42u)); + struct_values.push_back(Construct( + ty.vec4(), + ast::ExpressionList{Expr(1.f), Expr(2.f), Expr(3.f), Expr(4.f)})); + + auto* expr = Construct(struct_ty, struct_values); + + GeneratorImpl& gen = Build(); + + ASSERT_TRUE(gen.EmitConstructor(expr)) << gen.error(); + EXPECT_EQ(gen.result(), "{0.0f, 42u, float4(1.0f, 2.0f, 3.0f, 4.0f)}"); +} } // namespace } // namespace msl