[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>
diff --git a/BUILD.gn b/BUILD.gn
index 35ffa97..ae14e27 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -885,6 +885,7 @@
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",
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 42c3ad2..b16315c 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -493,6 +493,7 @@
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
diff --git a/src/writer/msl/generator_impl.cc b/src/writer/msl/generator_impl.cc
index 61e560d..b82090a 100644
--- a/src/writer/msl/generator_impl.cc
+++ b/src/writer/msl/generator_impl.cc
@@ -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 @@
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::EmitExpression(ast::Expression* expr) {
+ if (expr->IsAs()) {
+ return EmitAs(expr->AsAs());
+ }
if (expr->IsBinary()) {
return EmitBinary(expr->AsBinary());
}
diff --git a/src/writer/msl/generator_impl.h b/src/writer/msl/generator_impl.h
index 10d9073..3f1cba7 100644
--- a/src/writer/msl/generator_impl.h
+++ b/src/writer/msl/generator_impl.h
@@ -40,6 +40,10 @@
/// @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
diff --git a/src/writer/msl/generator_impl_as_test.cc b/src/writer/msl/generator_impl_as_test.cc
new file mode 100644
index 0000000..35aab83
--- /dev/null
+++ b/src/writer/msl/generator_impl_as_test.cc
@@ -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