[msl-writer] Add generation of as casts.

This CL adds the MSL conversion of as casts to `as_cast`.

Bug: tint:8
Change-Id: Iaa8ee1fa3077e4471bbead9d24fcf1e2d68998b0
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/23821
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-06-26 21:27:59 +00:00 committed by David Neto
parent 7fe3c36376
commit 3dbc8378fa
5 changed files with 68 additions and 0 deletions

View File

@ -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",

View File

@ -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

View File

@ -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());
}

View File

@ -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

View File

@ -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 <memory>
#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<ast::IdentifierExpression>("id");
ast::AsExpression as(&f32, std::move(id));
GeneratorImpl g;
ASSERT_TRUE(g.EmitExpression(&as)) << g.error();
EXPECT_EQ(g.result(), "as_type<float>(id)");
}
} // namespace
} // namespace msl
} // namespace writer
} // namespace tint