Emit Cast expression.
This CL adds Cast Expression emission to the WGSL generator.
Bug: tint:4
Change-Id: Ib48290d53423be5770cfb8f1b5f1ccb93b53a4df
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/17062
Reviewed-by: David Neto <dneto@google.com>
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 7279687..b992b60 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -408,6 +408,7 @@
writer/wgsl/generator_impl_array_accessor_test.cc
writer/wgsl/generator_impl_as_test.cc
writer/wgsl/generator_impl_call_test.cc
+ writer/wgsl/generator_impl_cast_test.cc
writer/wgsl/generator_impl_entry_point_test.cc
writer/wgsl/generator_impl_identifier_test.cc
writer/wgsl/generator_impl_import_test.cc
diff --git a/src/writer/wgsl/generator_impl.cc b/src/writer/wgsl/generator_impl.cc
index ed3a1bf..b72039e 100644
--- a/src/writer/wgsl/generator_impl.cc
+++ b/src/writer/wgsl/generator_impl.cc
@@ -23,6 +23,7 @@
#include "src/ast/bool_literal.h"
#include "src/ast/builtin_decoration.h"
#include "src/ast/call_expression.h"
+#include "src/ast/cast_expression.h"
#include "src/ast/const_initializer_expression.h"
#include "src/ast/decorated_variable.h"
#include "src/ast/float_literal.h"
@@ -126,6 +127,9 @@
if (expr->IsCall()) {
return EmitCall(expr->AsCall());
}
+ if (expr->IsCast()) {
+ return EmitCast(expr->AsCast());
+ }
if (expr->IsIdentifier()) {
return EmitIdentifier(expr->AsIdentifier());
}
@@ -190,6 +194,21 @@
return true;
}
+bool GeneratorImpl::EmitCast(ast::CastExpression* expr) {
+ out_ << "cast<";
+ if (!EmitType(expr->type())) {
+ return false;
+ }
+
+ out_ << ">(";
+ if (!EmitExpression(expr->expr())) {
+ return false;
+ }
+
+ out_ << ")";
+ return true;
+}
+
bool GeneratorImpl::EmitInitializer(ast::InitializerExpression* expr) {
if (expr->IsConstInitializer()) {
return EmitConstInitializer(expr->AsConstInitializer());
diff --git a/src/writer/wgsl/generator_impl.h b/src/writer/wgsl/generator_impl.h
index d03b8f6..188b6ee 100644
--- a/src/writer/wgsl/generator_impl.h
+++ b/src/writer/wgsl/generator_impl.h
@@ -82,6 +82,10 @@
/// @param expr the call expression
/// @returns true if the call expression is emitted
bool EmitCall(ast::CallExpression* expr);
+ /// Handles generating a cast expression
+ /// @param expr the cast expression
+ /// @returns true if the cast was emitted
+ bool EmitCast(ast::CastExpression* expr);
/// Handles generating a const initializer
/// @param expr the const initializer expression
/// @returns true if the initializer is emitted
diff --git a/src/writer/wgsl/generator_impl_cast_test.cc b/src/writer/wgsl/generator_impl_cast_test.cc
new file mode 100644
index 0000000..f011758
--- /dev/null
+++ b/src/writer/wgsl/generator_impl_cast_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/cast_expression.h"
+#include "src/ast/identifier_expression.h"
+#include "src/ast/type/f32_type.h"
+#include "src/writer/wgsl/generator_impl.h"
+
+namespace tint {
+namespace writer {
+namespace wgsl {
+namespace {
+
+using GeneratorImplTest = testing::Test;
+
+TEST_F(GeneratorImplTest, EmitExpression_Cast) {
+ ast::type::F32Type f32;
+ auto id = std::make_unique<ast::IdentifierExpression>("id");
+ ast::CastExpression cast(&f32, std::move(id));
+
+ GeneratorImpl g;
+ ASSERT_TRUE(g.EmitExpression(&cast)) << g.error();
+ EXPECT_EQ(g.result(), "cast<f32>(id)");
+}
+
+} // namespace
+} // namespace wgsl
+} // namespace writer
+} // namespace tint