[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 <dneto@google.com>
This commit is contained in:
parent
24a46e8e69
commit
d1bf87b9d8
1
BUILD.gn
1
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_return_test.cc",
|
||||||
"src/writer/msl/generator_impl_test.cc",
|
"src/writer/msl/generator_impl_test.cc",
|
||||||
"src/writer/msl/generator_impl_type_test.cc",
|
"src/writer/msl/generator_impl_type_test.cc",
|
||||||
|
"src/writer/msl/generator_impl_unary_op_test.cc",
|
||||||
]
|
]
|
||||||
|
|
||||||
configs += [
|
configs += [
|
||||||
|
|
|
@ -504,6 +504,7 @@ if(${TINT_BUILD_MSL_WRITER})
|
||||||
writer/msl/generator_impl_return_test.cc
|
writer/msl/generator_impl_return_test.cc
|
||||||
writer/msl/generator_impl_test.cc
|
writer/msl/generator_impl_test.cc
|
||||||
writer/msl/generator_impl_type_test.cc
|
writer/msl/generator_impl_type_test.cc
|
||||||
|
writer/msl/generator_impl_unary_op_test.cc
|
||||||
)
|
)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
|
|
@ -38,6 +38,7 @@
|
||||||
#include "src/ast/type/vector_type.h"
|
#include "src/ast/type/vector_type.h"
|
||||||
#include "src/ast/type/void_type.h"
|
#include "src/ast/type/void_type.h"
|
||||||
#include "src/ast/uint_literal.h"
|
#include "src/ast/uint_literal.h"
|
||||||
|
#include "src/ast/unary_op_expression.h"
|
||||||
|
|
||||||
namespace tint {
|
namespace tint {
|
||||||
namespace writer {
|
namespace writer {
|
||||||
|
@ -265,6 +266,9 @@ bool GeneratorImpl::EmitExpression(ast::Expression* expr) {
|
||||||
if (expr->IsIdentifier()) {
|
if (expr->IsIdentifier()) {
|
||||||
return EmitIdentifier(expr->AsIdentifier());
|
return EmitIdentifier(expr->AsIdentifier());
|
||||||
}
|
}
|
||||||
|
if (expr->IsUnaryOp()) {
|
||||||
|
return EmitUnaryOp(expr->AsUnaryOp());
|
||||||
|
}
|
||||||
|
|
||||||
error_ = "unknown expression type";
|
error_ = "unknown expression type";
|
||||||
return false;
|
return false;
|
||||||
|
@ -535,6 +539,26 @@ bool GeneratorImpl::EmitType(ast::type::Type* type, const std::string& name) {
|
||||||
return true;
|
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 msl
|
||||||
} // namespace writer
|
} // namespace writer
|
||||||
} // namespace tint
|
} // namespace tint
|
||||||
|
|
|
@ -120,6 +120,10 @@ class GeneratorImpl : public TextGenerator {
|
||||||
/// @param expr the type constructor expression
|
/// @param expr the type constructor expression
|
||||||
/// @returns true if the constructor is emitted
|
/// @returns true if the constructor is emitted
|
||||||
bool EmitTypeConstructor(ast::TypeConstructorExpression* expr);
|
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:
|
private:
|
||||||
const ast::Module* module_ = nullptr;
|
const ast::Module* module_ = nullptr;
|
||||||
|
|
|
@ -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/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<UnaryOpData>;
|
||||||
|
TEST_P(MslUnaryOpTest, 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(MslGeneratorImplTest,
|
||||||
|
MslUnaryOpTest,
|
||||||
|
testing::Values(UnaryOpData{"!", ast::UnaryOp::kNot},
|
||||||
|
UnaryOpData{"-",
|
||||||
|
ast::UnaryOp::kNegation}));
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
} // namespace msl
|
||||||
|
} // namespace writer
|
||||||
|
} // namespace tint
|
|
@ -33,8 +33,8 @@ inline std::ostream& operator<<(std::ostream& out, UnaryOpData data) {
|
||||||
out << data.op;
|
out << data.op;
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
using UnaryOpTest = testing::TestWithParam<UnaryOpData>;
|
using WgslUnaryOpTest = testing::TestWithParam<UnaryOpData>;
|
||||||
TEST_P(UnaryOpTest, Emit) {
|
TEST_P(WgslUnaryOpTest, Emit) {
|
||||||
auto params = GetParam();
|
auto params = GetParam();
|
||||||
|
|
||||||
auto expr = std::make_unique<ast::IdentifierExpression>("expr");
|
auto expr = std::make_unique<ast::IdentifierExpression>("expr");
|
||||||
|
@ -45,7 +45,7 @@ TEST_P(UnaryOpTest, Emit) {
|
||||||
EXPECT_EQ(g.result(), std::string(params.name) + "(expr)");
|
EXPECT_EQ(g.result(), std::string(params.name) + "(expr)");
|
||||||
}
|
}
|
||||||
INSTANTIATE_TEST_SUITE_P(WgslGeneratorImplTest,
|
INSTANTIATE_TEST_SUITE_P(WgslGeneratorImplTest,
|
||||||
UnaryOpTest,
|
WgslUnaryOpTest,
|
||||||
testing::Values(UnaryOpData{"!", ast::UnaryOp::kNot},
|
testing::Values(UnaryOpData{"!", ast::UnaryOp::kNot},
|
||||||
UnaryOpData{"-",
|
UnaryOpData{"-",
|
||||||
ast::UnaryOp::kNegation}));
|
ast::UnaryOp::kNegation}));
|
||||||
|
|
Loading…
Reference in New Issue