From ee449e2625ab17852a03de95df346dfa5f45356a Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Wed, 19 May 2021 08:29:58 +0000 Subject: [PATCH] writer/wgsl: Add paren around LHS of member accessors When the LHS type is not one of the common expression types Fixes operator precedence issues around pointers and member accesses. Change-Id: I81b01c0c1050d087d5d68a93d15a66ee14b2fed6 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/51365 Commit-Queue: Ben Clayton Reviewed-by: David Neto Reviewed-by: James Price --- src/writer/wgsl/generator_impl.cc | 11 +++++++++++ .../wgsl/generator_impl_member_accessor_test.cc | 14 ++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/writer/wgsl/generator_impl.cc b/src/writer/wgsl/generator_impl.cc index a9607b2e01..45a77b83a2 100644 --- a/src/writer/wgsl/generator_impl.cc +++ b/src/writer/wgsl/generator_impl.cc @@ -155,9 +155,20 @@ bool GeneratorImpl::EmitArrayAccessor(ast::ArrayAccessorExpression* expr) { } bool GeneratorImpl::EmitMemberAccessor(ast::MemberAccessorExpression* expr) { + bool paren_lhs = + !expr->structure() + ->IsAnyOf(); + if (paren_lhs) { + out_ << "("; + } if (!EmitExpression(expr->structure())) { return false; } + if (paren_lhs) { + out_ << ")"; + } out_ << "."; diff --git a/src/writer/wgsl/generator_impl_member_accessor_test.cc b/src/writer/wgsl/generator_impl_member_accessor_test.cc index 1a2801164a..03fccc6265 100644 --- a/src/writer/wgsl/generator_impl_member_accessor_test.cc +++ b/src/writer/wgsl/generator_impl_member_accessor_test.cc @@ -34,6 +34,20 @@ TEST_F(WgslGeneratorImplTest, EmitExpression_MemberAccessor) { EXPECT_EQ(gen.result(), "str.mem"); } +TEST_F(WgslGeneratorImplTest, EmitExpression_MemberAccessor_OfDref) { + auto* s = Structure("Data", {Member("mem", ty.f32())}); + Global("str", s, ast::StorageClass::kPrivate); + + auto* p = Const("p", nullptr, AddressOf("str")); + auto* expr = MemberAccessor(Deref("p"), "mem"); + WrapInFunction(p, expr); + + GeneratorImpl& gen = Build(); + + ASSERT_TRUE(gen.EmitExpression(expr)) << gen.error(); + EXPECT_EQ(gen.result(), "(*(p)).mem"); +} + } // namespace } // namespace wgsl } // namespace writer