From d1bf87b9d87f84fc0072cb549b7556366cd6fb1e Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Fri, 26 Jun 2020 21:27:59 +0000 Subject: [PATCH] [msl-writer] Add unary op emission. This CL adds emission of the unary `-` and `!` operators. Bug: tint:8 Change-Id: I9dda066111cc8f115b593127cf070c6ca37bdc66 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/23842 Reviewed-by: David Neto --- BUILD.gn | 1 + src/CMakeLists.txt | 1 + src/writer/msl/generator_impl.cc | 24 ++++++++ src/writer/msl/generator_impl.h | 4 ++ .../msl/generator_impl_unary_op_test.cc | 56 +++++++++++++++++++ .../wgsl/generator_impl_unary_op_test.cc | 6 +- 6 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 src/writer/msl/generator_impl_unary_op_test.cc diff --git a/BUILD.gn b/BUILD.gn index 4c1a7b2979..69cf8b805a 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -896,6 +896,7 @@ source_set("tint_unittests_msl_writer_src") { "src/writer/msl/generator_impl_return_test.cc", "src/writer/msl/generator_impl_test.cc", "src/writer/msl/generator_impl_type_test.cc", + "src/writer/msl/generator_impl_unary_op_test.cc", ] configs += [ diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 75136f2c46..fbf8a75e95 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -504,6 +504,7 @@ if(${TINT_BUILD_MSL_WRITER}) writer/msl/generator_impl_return_test.cc writer/msl/generator_impl_test.cc writer/msl/generator_impl_type_test.cc + writer/msl/generator_impl_unary_op_test.cc ) endif() diff --git a/src/writer/msl/generator_impl.cc b/src/writer/msl/generator_impl.cc index 23171dcfc0..7935c4607e 100644 --- a/src/writer/msl/generator_impl.cc +++ b/src/writer/msl/generator_impl.cc @@ -38,6 +38,7 @@ #include "src/ast/type/vector_type.h" #include "src/ast/type/void_type.h" #include "src/ast/uint_literal.h" +#include "src/ast/unary_op_expression.h" namespace tint { namespace writer { @@ -265,6 +266,9 @@ bool GeneratorImpl::EmitExpression(ast::Expression* expr) { if (expr->IsIdentifier()) { return EmitIdentifier(expr->AsIdentifier()); } + if (expr->IsUnaryOp()) { + return EmitUnaryOp(expr->AsUnaryOp()); + } error_ = "unknown expression type"; return false; @@ -535,6 +539,26 @@ bool GeneratorImpl::EmitType(ast::type::Type* type, const std::string& name) { return true; } +bool GeneratorImpl::EmitUnaryOp(ast::UnaryOpExpression* expr) { + switch (expr->op()) { + case ast::UnaryOp::kNot: + out_ << "!"; + break; + case ast::UnaryOp::kNegation: + out_ << "-"; + break; + } + out_ << "("; + + if (!EmitExpression(expr->expr())) { + return false; + } + + out_ << ")"; + + return true; +} + } // namespace msl } // namespace writer } // namespace tint diff --git a/src/writer/msl/generator_impl.h b/src/writer/msl/generator_impl.h index 0a3fc76cfd..319690e495 100644 --- a/src/writer/msl/generator_impl.h +++ b/src/writer/msl/generator_impl.h @@ -120,6 +120,10 @@ class GeneratorImpl : public TextGenerator { /// @param expr the type constructor expression /// @returns true if the constructor is emitted bool EmitTypeConstructor(ast::TypeConstructorExpression* expr); + /// Handles a unary op expression + /// @param expr the expression to emit + /// @returns true if the expression was emitted + bool EmitUnaryOp(ast::UnaryOpExpression* expr); private: const ast::Module* module_ = nullptr; diff --git a/src/writer/msl/generator_impl_unary_op_test.cc b/src/writer/msl/generator_impl_unary_op_test.cc new file mode 100644 index 0000000000..6a01597a0f --- /dev/null +++ b/src/writer/msl/generator_impl_unary_op_test.cc @@ -0,0 +1,56 @@ +// 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 + +#include "gtest/gtest.h" +#include "src/ast/identifier_expression.h" +#include "src/ast/unary_op_expression.h" +#include "src/writer/msl/generator_impl.h" + +namespace tint { +namespace writer { +namespace msl { +namespace { + +struct UnaryOpData { + const char* name; + ast::UnaryOp op; +}; +inline std::ostream& operator<<(std::ostream& out, UnaryOpData data) { + out << data.op; + return out; +} +using MslUnaryOpTest = testing::TestWithParam; +TEST_P(MslUnaryOpTest, Emit) { + auto params = GetParam(); + + auto expr = std::make_unique("expr"); + ast::UnaryOpExpression op(params.op, std::move(expr)); + + GeneratorImpl g; + ASSERT_TRUE(g.EmitExpression(&op)) << g.error(); + EXPECT_EQ(g.result(), std::string(params.name) + "(expr)"); +} +INSTANTIATE_TEST_SUITE_P(MslGeneratorImplTest, + MslUnaryOpTest, + testing::Values(UnaryOpData{"!", ast::UnaryOp::kNot}, + UnaryOpData{"-", + ast::UnaryOp::kNegation})); + +} // namespace +} // namespace msl +} // namespace writer +} // namespace tint diff --git a/src/writer/wgsl/generator_impl_unary_op_test.cc b/src/writer/wgsl/generator_impl_unary_op_test.cc index 73d835d492..44bc4a2564 100644 --- a/src/writer/wgsl/generator_impl_unary_op_test.cc +++ b/src/writer/wgsl/generator_impl_unary_op_test.cc @@ -33,8 +33,8 @@ inline std::ostream& operator<<(std::ostream& out, UnaryOpData data) { out << data.op; return out; } -using UnaryOpTest = testing::TestWithParam; -TEST_P(UnaryOpTest, Emit) { +using WgslUnaryOpTest = testing::TestWithParam; +TEST_P(WgslUnaryOpTest, Emit) { auto params = GetParam(); auto expr = std::make_unique("expr"); @@ -45,7 +45,7 @@ TEST_P(UnaryOpTest, Emit) { EXPECT_EQ(g.result(), std::string(params.name) + "(expr)"); } INSTANTIATE_TEST_SUITE_P(WgslGeneratorImplTest, - UnaryOpTest, + WgslUnaryOpTest, testing::Values(UnaryOpData{"!", ast::UnaryOp::kNot}, UnaryOpData{"-", ast::UnaryOp::kNegation}));