[hlsl-writer] Add AsExpression to the HLSL backend.
This CL adds as casts to the HLSL backend.
Bug: tint:7
Change-Id: I599527d665a3ec1ab6cf80b4f550f7aee8fdf294
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/25843
Reviewed-by: David Neto <dneto@google.com>
diff --git a/BUILD.gn b/BUILD.gn
index bd007cc..cfbbe40 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -1043,6 +1043,7 @@
sources = [
"src/writer/hlsl/generator_impl_alias_type_test.cc",
"src/writer/hlsl/generator_impl_array_accessor_test.cc",
+ "src/writer/hlsl/generator_impl_as_test.cc",
"src/writer/hlsl/generator_impl_assign_test.cc",
"src/writer/hlsl/generator_impl_binary_test.cc",
"src/writer/hlsl/generator_impl_block_test.cc",
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 7af9c12..959c674 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -551,6 +551,7 @@
list(APPEND TINT_TEST_SRCS
writer/hlsl/generator_impl_alias_type_test.cc
writer/hlsl/generator_impl_array_accessor_test.cc
+ writer/hlsl/generator_impl_as_test.cc
writer/hlsl/generator_impl_assign_test.cc
writer/hlsl/generator_impl_binary_test.cc
writer/hlsl/generator_impl_block_test.cc
diff --git a/src/writer/hlsl/generator_impl.cc b/src/writer/hlsl/generator_impl.cc
index a431001..2288d2d 100644
--- a/src/writer/hlsl/generator_impl.cc
+++ b/src/writer/hlsl/generator_impl.cc
@@ -15,6 +15,7 @@
#include "src/writer/hlsl/generator_impl.h"
#include "src/ast/array_accessor_expression.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"
@@ -117,6 +118,25 @@
return true;
}
+bool GeneratorImpl::EmitAs(ast::AsExpression* expr) {
+ if (!expr->type()->IsF32() && !expr->type()->IsI32() &&
+ !expr->type()->IsU32()) {
+ error_ = "Unable to do as cast to type " + expr->type()->type_name();
+ return false;
+ }
+
+ out_ << "as";
+ if (!EmitType(expr->type(), "")) {
+ return false;
+ }
+ out_ << "(";
+ if (!EmitExpression(expr->expr())) {
+ return false;
+ }
+ out_ << ")";
+ return true;
+}
+
bool GeneratorImpl::EmitAssign(ast::AssignmentStatement* stmt) {
make_indent();
@@ -368,6 +388,9 @@
}
bool GeneratorImpl::EmitExpression(ast::Expression* expr) {
+ if (expr->IsAs()) {
+ return EmitAs(expr->AsAs());
+ }
if (expr->IsArrayAccessor()) {
return EmitArrayAccessor(expr->AsArrayAccessor());
}
diff --git a/src/writer/hlsl/generator_impl.h b/src/writer/hlsl/generator_impl.h
index 24fadb8..2b06729 100644
--- a/src/writer/hlsl/generator_impl.h
+++ b/src/writer/hlsl/generator_impl.h
@@ -46,6 +46,10 @@
/// @param expr the expression to emit
/// @returns true if the array accessor was emitted
bool EmitArrayAccessor(ast::ArrayAccessorExpression* expr);
+ /// 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/hlsl/generator_impl_as_test.cc b/src/writer/hlsl/generator_impl_as_test.cc
new file mode 100644
index 0000000..c7975ce
--- /dev/null
+++ b/src/writer/hlsl/generator_impl_as_test.cc
@@ -0,0 +1,69 @@
+// 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/module.h"
+#include "src/ast/type/f32_type.h"
+#include "src/ast/type/i32_type.h"
+#include "src/ast/type/u32_type.h"
+#include "src/writer/hlsl/generator_impl.h"
+
+namespace tint {
+namespace writer {
+namespace hlsl {
+namespace {
+
+using HlslGeneratorImplTest = testing::Test;
+
+TEST_F(HlslGeneratorImplTest, EmitExpression_As_Float) {
+ ast::type::F32Type f32;
+ auto id = std::make_unique<ast::IdentifierExpression>("id");
+ ast::AsExpression as(&f32, std::move(id));
+
+ ast::Module m;
+ GeneratorImpl g(&m);
+ ASSERT_TRUE(g.EmitExpression(&as)) << g.error();
+ EXPECT_EQ(g.result(), "asfloat(id)");
+}
+
+TEST_F(HlslGeneratorImplTest, EmitExpression_As_Int) {
+ ast::type::I32Type i32;
+ auto id = std::make_unique<ast::IdentifierExpression>("id");
+ ast::AsExpression as(&i32, std::move(id));
+
+ ast::Module m;
+ GeneratorImpl g(&m);
+ ASSERT_TRUE(g.EmitExpression(&as)) << g.error();
+ EXPECT_EQ(g.result(), "asint(id)");
+}
+
+TEST_F(HlslGeneratorImplTest, EmitExpression_As_Uint) {
+ ast::type::U32Type u32;
+ auto id = std::make_unique<ast::IdentifierExpression>("id");
+ ast::AsExpression as(&u32, std::move(id));
+
+ ast::Module m;
+ GeneratorImpl g(&m);
+ ASSERT_TRUE(g.EmitExpression(&as)) << g.error();
+ EXPECT_EQ(g.result(), "asuint(id)");
+}
+
+} // namespace
+} // namespace hlsl
+} // namespace writer
+} // namespace tint