From 2c56dd90993d12a175655e782b406151f2f2d2e8 Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Thu, 19 Mar 2020 02:55:19 +0000 Subject: [PATCH] Emit member accessor in WGSL generator. This CL adds MemberAccessor handling into the WGSL generator. Bug: tint:4 Change-Id: Ib84f9ca1969fec48f167433af957dc1ed4de3415 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/17063 Reviewed-by: David Neto --- src/CMakeLists.txt | 1 + src/writer/wgsl/generator_impl.cc | 14 ++++++ src/writer/wgsl/generator_impl.h | 4 ++ .../generator_impl_member_accessor_test.cc | 43 +++++++++++++++++++ 4 files changed, 62 insertions(+) create mode 100644 src/writer/wgsl/generator_impl_member_accessor_test.cc diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b992b6071e..b81839251b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -413,6 +413,7 @@ if(${TINT_BUILD_WGSL_WRITER}) writer/wgsl/generator_impl_identifier_test.cc writer/wgsl/generator_impl_import_test.cc writer/wgsl/generator_impl_initializer_test.cc + writer/wgsl/generator_impl_member_accessor_test.cc writer/wgsl/generator_impl_type_test.cc writer/wgsl/generator_impl_variable_test.cc ) diff --git a/src/writer/wgsl/generator_impl.cc b/src/writer/wgsl/generator_impl.cc index b72039e7b7..b282c9e7f8 100644 --- a/src/writer/wgsl/generator_impl.cc +++ b/src/writer/wgsl/generator_impl.cc @@ -31,6 +31,7 @@ #include "src/ast/initializer_expression.h" #include "src/ast/int_literal.h" #include "src/ast/location_decoration.h" +#include "src/ast/member_accessor_expression.h" #include "src/ast/set_decoration.h" #include "src/ast/struct.h" #include "src/ast/struct_member.h" @@ -136,6 +137,9 @@ bool GeneratorImpl::EmitExpression(ast::Expression* expr) { if (expr->IsInitializer()) { return EmitInitializer(expr->AsInitializer()); } + if (expr->IsMemberAccessor()) { + return EmitMemberAccessor(expr->AsMemberAccessor()); + } error_ = "unknown expression type"; return false; @@ -155,6 +159,16 @@ bool GeneratorImpl::EmitArrayAccessor(ast::ArrayAccessorExpression* expr) { return true; } +bool GeneratorImpl::EmitMemberAccessor(ast::MemberAccessorExpression* expr) { + if (!EmitExpression(expr->structure())) { + return false; + } + + out_ << "."; + + return EmitExpression(expr->member()); +} + bool GeneratorImpl::EmitAs(ast::AsExpression* expr) { out_ << "as<"; if (!EmitType(expr->type())) { diff --git a/src/writer/wgsl/generator_impl.h b/src/writer/wgsl/generator_impl.h index 188b6eecf3..f84ff84ed7 100644 --- a/src/writer/wgsl/generator_impl.h +++ b/src/writer/wgsl/generator_impl.h @@ -110,6 +110,10 @@ class GeneratorImpl { /// @param expr the initializer expression /// @returns true if the expression was emitted bool EmitInitializer(ast::InitializerExpression* expr); + /// Handles a member accessor expression + /// @param expr the member accessor expression + /// @returns true if the member accessor was emitted + bool EmitMemberAccessor(ast::MemberAccessorExpression* expr); /// Handles generating type /// @param type the type to generate /// @returns true if the type is emitted diff --git a/src/writer/wgsl/generator_impl_member_accessor_test.cc b/src/writer/wgsl/generator_impl_member_accessor_test.cc new file mode 100644 index 0000000000..88adbe3abf --- /dev/null +++ b/src/writer/wgsl/generator_impl_member_accessor_test.cc @@ -0,0 +1,43 @@ +// Copyright 2020 The Tint Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include "gtest/gtest.h" +#include "src/ast/identifier_expression.h" +#include "src/ast/member_accessor_expression.h" +#include "src/writer/wgsl/generator_impl.h" + +namespace tint { +namespace writer { +namespace wgsl { +namespace { + +using GeneratorImplTest = testing::Test; + +TEST_F(GeneratorImplTest, EmitExpression_MemberAccessor) { + auto str = std::make_unique("str"); + auto mem = std::make_unique("mem"); + + ast::MemberAccessorExpression expr(std::move(str), std::move(mem)); + + GeneratorImpl g; + ASSERT_TRUE(g.EmitExpression(&expr)) << g.error(); + EXPECT_EQ(g.result(), "str.mem"); +} + +} // namespace +} // namespace wgsl +} // namespace writer +} // namespace tint