diff --git a/BUILD.gn b/BUILD.gn index 372d8c39db..11a1c8ad94 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -1024,6 +1024,7 @@ source_set("tint_unittests_hlsl_writer_src") { "src/writer/hlsl/generator_impl_identifier_test.cc", "src/writer/hlsl/generator_impl_return_test.cc", "src/writer/hlsl/generator_impl_test.cc", + "src/writer/hlsl/generator_impl_unary_op_test.cc", "src/writer/hlsl/namer_test.cc", ] diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ca0f3ec2ce..a67b67447d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -548,6 +548,7 @@ if (${TINT_BUILD_HLSL_WRITER}) writer/hlsl/generator_impl_identifier_test.cc writer/hlsl/generator_impl_return_test.cc writer/hlsl/generator_impl_test.cc + writer/hlsl/generator_impl_unary_op_test.cc writer/hlsl/namer_test.cc ) endif() diff --git a/src/writer/hlsl/generator_impl.cc b/src/writer/hlsl/generator_impl.cc index 076aa7ba25..741110fc32 100644 --- a/src/writer/hlsl/generator_impl.cc +++ b/src/writer/hlsl/generator_impl.cc @@ -17,6 +17,7 @@ #include "src/ast/binary_expression.h" #include "src/ast/identifier_expression.h" #include "src/ast/return_statement.h" +#include "src/ast/unary_op_expression.h" namespace tint { namespace writer { @@ -157,6 +158,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: " + expr->str(); return false; @@ -228,6 +232,26 @@ bool GeneratorImpl::EmitStatement(ast::Statement* stmt) { return false; } +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 hlsl } // namespace writer } // namespace tint diff --git a/src/writer/hlsl/generator_impl.h b/src/writer/hlsl/generator_impl.h index b6565ead27..095165c581 100644 --- a/src/writer/hlsl/generator_impl.h +++ b/src/writer/hlsl/generator_impl.h @@ -63,6 +63,10 @@ class GeneratorImpl : public TextGenerator { /// @param stmt the statement to emit /// @returns true if the statement was emitted bool EmitStatement(ast::Statement* stmt); + /// Handles a unary op expression + /// @param expr the expression to emit + /// @returns true if the expression was emitted + bool EmitUnaryOp(ast::UnaryOpExpression* expr); /// Checks if the global variable is in an input or output struct /// @param var the variable to check diff --git a/src/writer/hlsl/generator_impl_unary_op_test.cc b/src/writer/hlsl/generator_impl_unary_op_test.cc new file mode 100644 index 0000000000..4f9317745f --- /dev/null +++ b/src/writer/hlsl/generator_impl_unary_op_test.cc @@ -0,0 +1,58 @@ +// 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/module.h" +#include "src/ast/unary_op_expression.h" +#include "src/writer/hlsl/generator_impl.h" + +namespace tint { +namespace writer { +namespace hlsl { +namespace { + +struct UnaryOpData { + const char* name; + ast::UnaryOp op; +}; +inline std::ostream& operator<<(std::ostream& out, UnaryOpData data) { + out << data.op; + return out; +} +using HlslUnaryOpTest = testing::TestWithParam; +TEST_P(HlslUnaryOpTest, Emit) { + auto params = GetParam(); + + auto expr = std::make_unique("expr"); + ast::UnaryOpExpression op(params.op, std::move(expr)); + + ast::Module m; + GeneratorImpl g(&m); + ASSERT_TRUE(g.EmitExpression(&op)) << g.error(); + EXPECT_EQ(g.result(), std::string(params.name) + "(expr)"); +} +INSTANTIATE_TEST_SUITE_P(HlslGeneratorImplTest, + HlslUnaryOpTest, + testing::Values(UnaryOpData{"!", ast::UnaryOp::kNot}, + UnaryOpData{"-", + ast::UnaryOp::kNegation})); + +} // namespace +} // namespace hlsl +} // namespace writer +} // namespace tint