diff --git a/BUILD.gn b/BUILD.gn index cfbbe40d83..21da91103d 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -1053,6 +1053,7 @@ source_set("tint_unittests_hlsl_writer_src") { "src/writer/hlsl/generator_impl_continue_test.cc", "src/writer/hlsl/generator_impl_discard_test.cc", "src/writer/hlsl/generator_impl_identifier_test.cc", + "src/writer/hlsl/generator_impl_member_accessor_test.cc", "src/writer/hlsl/generator_impl_return_test.cc", "src/writer/hlsl/generator_impl_switch_test.cc", "src/writer/hlsl/generator_impl_test.cc", diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 959c6748aa..3d5302aec1 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -561,6 +561,7 @@ if (${TINT_BUILD_HLSL_WRITER}) writer/hlsl/generator_impl_continue_test.cc writer/hlsl/generator_impl_discard_test.cc writer/hlsl/generator_impl_identifier_test.cc + writer/hlsl/generator_impl_member_accessor_test.cc writer/hlsl/generator_impl_return_test.cc writer/hlsl/generator_impl_switch_test.cc writer/hlsl/generator_impl_test.cc diff --git a/src/writer/hlsl/generator_impl.cc b/src/writer/hlsl/generator_impl.cc index 2288d2d48d..aec13ec3f4 100644 --- a/src/writer/hlsl/generator_impl.cc +++ b/src/writer/hlsl/generator_impl.cc @@ -22,6 +22,7 @@ #include "src/ast/case_statement.h" #include "src/ast/float_literal.h" #include "src/ast/identifier_expression.h" +#include "src/ast/member_accessor_expression.h" #include "src/ast/return_statement.h" #include "src/ast/sint_literal.h" #include "src/ast/struct.h" @@ -403,6 +404,9 @@ bool GeneratorImpl::EmitExpression(ast::Expression* expr) { if (expr->IsIdentifier()) { return EmitIdentifier(expr->AsIdentifier()); } + if (expr->IsMemberAccessor()) { + return EmitMemberAccessor(expr->AsMemberAccessor()); + } if (expr->IsUnaryOp()) { return EmitUnaryOp(expr->AsUnaryOp()); } @@ -485,6 +489,14 @@ bool GeneratorImpl::EmitZeroValue(ast::type::Type* type) { return true; } +bool GeneratorImpl::EmitMemberAccessor(ast::MemberAccessorExpression* expr) { + if (!EmitExpression(expr->structure())) { + return false; + } + out_ << "."; + return EmitExpression(expr->member()); +} + bool GeneratorImpl::EmitReturn(ast::ReturnStatement* stmt) { make_indent(); diff --git a/src/writer/hlsl/generator_impl.h b/src/writer/hlsl/generator_impl.h index 2b06729af2..d2cb4f0df3 100644 --- a/src/writer/hlsl/generator_impl.h +++ b/src/writer/hlsl/generator_impl.h @@ -110,6 +110,10 @@ class GeneratorImpl : public TextGenerator { /// @param expr the identifier expression /// @returns true if the identifeir was emitted bool EmitIdentifier(ast::IdentifierExpression* 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 return statements /// @param stmt the statement to emit /// @returns true if the statement was successfully emitted diff --git a/src/writer/hlsl/generator_impl_member_accessor_test.cc b/src/writer/hlsl/generator_impl_member_accessor_test.cc new file mode 100644 index 0000000000..a4df1d8280 --- /dev/null +++ b/src/writer/hlsl/generator_impl_member_accessor_test.cc @@ -0,0 +1,45 @@ +// 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/ast/module.h" +#include "src/writer/hlsl/generator_impl.h" + +namespace tint { +namespace writer { +namespace hlsl { +namespace { + +using HlslGeneratorImplTest = testing::Test; + +TEST_F(HlslGeneratorImplTest, EmitExpression_MemberAccessor) { + auto str = std::make_unique("str"); + auto mem = std::make_unique("mem"); + + ast::MemberAccessorExpression expr(std::move(str), std::move(mem)); + + ast::Module m; + GeneratorImpl g(&m); + ASSERT_TRUE(g.EmitExpression(&expr)) << g.error(); + EXPECT_EQ(g.result(), "str.mem"); +} + +} // namespace +} // namespace hlsl +} // namespace writer +} // namespace tint