Emit UnaryOp expression.

This CL updates the WGSL generator to emit the UnaryOpExpression node.

Bug: tint:4
Change-Id: I54046b5238a45994f7a4ed27941bd22b0c75f836
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/17161
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-03-20 19:01:47 +00:00 committed by dan sinclair
parent 09671ab6f1
commit daff3e2616
4 changed files with 85 additions and 0 deletions

View File

@ -418,6 +418,7 @@ if(${TINT_BUILD_WGSL_WRITER})
writer/wgsl/generator_impl_type_test.cc
writer/wgsl/generator_impl_unary_derivative_test.cc
writer/wgsl/generator_impl_unary_method_test.cc
writer/wgsl/generator_impl_unary_op_test.cc
writer/wgsl/generator_impl_variable_test.cc
)
endif()

View File

@ -46,6 +46,7 @@
#include "src/ast/uint_literal.h"
#include "src/ast/unary_derivative_expression.h"
#include "src/ast/unary_method_expression.h"
#include "src/ast/unary_op_expression.h"
namespace tint {
namespace writer {
@ -152,6 +153,9 @@ bool GeneratorImpl::EmitExpression(ast::Expression* expr) {
if (expr->IsUnaryMethod()) {
return EmitUnaryMethod(expr->AsUnaryMethod());
}
if (expr->IsUnaryOp()) {
return EmitUnaryOp(expr->AsUnaryOp());
}
error_ = "unknown expression type";
return false;
@ -615,6 +619,26 @@ bool GeneratorImpl::EmitUnaryMethod(ast::UnaryMethodExpression* expr) {
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 wgsl
} // namespace writer
} // namespace tint

View File

@ -134,6 +134,10 @@ class GeneratorImpl {
/// @param expr the expression to emit
/// @returns true if the expression was emitted
bool EmitUnaryMethod(ast::UnaryMethodExpression* expr);
/// Handles a unary op expression
/// @param expr the expression to emit
/// @returns true if the expression was emitted
bool EmitUnaryOp(ast::UnaryOpExpression* expr);
/// Handles generating a variable
/// @param var the variable to generate
/// @returns true if the variable was emitted

View File

@ -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 <memory>
#include <vector>
#include "gtest/gtest.h"
#include "src/ast/identifier_expression.h"
#include "src/ast/unary_op_expression.h"
#include "src/writer/wgsl/generator_impl.h"
namespace tint {
namespace writer {
namespace wgsl {
namespace {
struct UnaryOpData {
const char* name;
ast::UnaryOp op;
};
inline std::ostream& operator<<(std::ostream& out, UnaryOpData data) {
out << data.op;
return out;
}
using UnaryOpTest = testing::TestWithParam<UnaryOpData>;
TEST_P(UnaryOpTest, Emit) {
auto params = GetParam();
auto expr = std::make_unique<ast::IdentifierExpression>("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(GeneratorImplTest,
UnaryOpTest,
testing::Values(UnaryOpData{"!", ast::UnaryOp::kNot},
UnaryOpData{"-",
ast::UnaryOp::kNegation}));
} // namespace
} // namespace wgsl
} // namespace writer
} // namespace tint