diff --git a/BUILD.gn b/BUILD.gn index 35ffa97efd..ae14e27494 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -885,6 +885,7 @@ source_set("tint_unittests_wgsl_writer_src") { source_set("tint_unittests_msl_writer_src") { sources = [ + "src/writer/msl/generator_impl_as_test.cc", "src/writer/msl/generator_impl_assign_test.cc", "src/writer/msl/generator_impl_binary_test.cc", "src/writer/msl/generator_impl_constructor_test.cc", diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 42c3ad2d7e..b16315c804 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -493,6 +493,7 @@ endif() if(${TINT_BUILD_MSL_WRITER}) list(APPEND TINT_TEST_SRCS + writer/msl/generator_impl_as_test.cc writer/msl/generator_impl_assign_test.cc writer/msl/generator_impl_binary_test.cc writer/msl/generator_impl_constructor_test.cc diff --git a/src/writer/msl/generator_impl.cc b/src/writer/msl/generator_impl.cc index 61e560d3fb..b82090a765 100644 --- a/src/writer/msl/generator_impl.cc +++ b/src/writer/msl/generator_impl.cc @@ -14,6 +14,7 @@ #include "src/writer/msl/generator_impl.h" +#include "src/ast/as_expression.h" #include "src/ast/assignment_statement.h" #include "src/ast/binary_expression.h" #include "src/ast/bool_literal.h" @@ -57,6 +58,21 @@ bool GeneratorImpl::Generate(const ast::Module& module) { return true; } +bool GeneratorImpl::EmitAs(ast::AsExpression* expr) { + out_ << "as_type<"; + if (!EmitType(expr->type(), "")) { + return false; + } + + out_ << ">("; + if (!EmitExpression(expr->expr())) { + return false; + } + + out_ << ")"; + return true; +} + bool GeneratorImpl::EmitAssign(ast::AssignmentStatement* stmt) { make_indent(); @@ -217,6 +233,9 @@ bool GeneratorImpl::EmitLiteral(ast::Literal* lit) { } bool GeneratorImpl::EmitExpression(ast::Expression* expr) { + if (expr->IsAs()) { + return EmitAs(expr->AsAs()); + } if (expr->IsBinary()) { return EmitBinary(expr->AsBinary()); } diff --git a/src/writer/msl/generator_impl.h b/src/writer/msl/generator_impl.h index 10d907324b..3f1cba7101 100644 --- a/src/writer/msl/generator_impl.h +++ b/src/writer/msl/generator_impl.h @@ -40,6 +40,10 @@ class GeneratorImpl : public TextGenerator { /// @returns true on successful generation; false otherwise bool Generate(const ast::Module& module); + /// Handles generating an as expression + /// @param expr the as expression + /// @returns true if the as was emitted + bool EmitAs(ast::AsExpression* expr); /// Handles an assignment statement /// @param stmt the statement to emit /// @returns true if the statement was emitted successfully diff --git a/src/writer/msl/generator_impl_as_test.cc b/src/writer/msl/generator_impl_as_test.cc new file mode 100644 index 0000000000..35aab83875 --- /dev/null +++ b/src/writer/msl/generator_impl_as_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/as_expression.h" +#include "src/ast/identifier_expression.h" +#include "src/ast/type/f32_type.h" +#include "src/writer/msl/generator_impl.h" + +namespace tint { +namespace writer { +namespace msl { +namespace { + +using MslGeneratorImplTest = testing::Test; + +TEST_F(MslGeneratorImplTest, EmitExpression_As) { + ast::type::F32Type f32; + auto id = std::make_unique("id"); + ast::AsExpression as(&f32, std::move(id)); + + GeneratorImpl g; + ASSERT_TRUE(g.EmitExpression(&as)) << g.error(); + EXPECT_EQ(g.result(), "as_type(id)"); +} + +} // namespace +} // namespace msl +} // namespace writer +} // namespace tint