[hlsl-writer] Add assignment statement.
This CL adds the assign statement emission to the HLSL backend.
Bug: tint:7
Change-Id: I3e46ac09170ea1af7444ae89267a82e1d1c42c52
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/25224
Reviewed-by: David Neto <dneto@google.com>
diff --git a/BUILD.gn b/BUILD.gn
index 2c087a5..d179b20 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -1018,6 +1018,7 @@
source_set("tint_unittests_hlsl_writer_src") {
sources = [
+ "src/writer/hlsl/generator_impl_assign_test.cc",
"src/writer/hlsl/generator_impl_binary_test.cc",
"src/writer/hlsl/generator_impl_break_test.cc",
"src/writer/hlsl/generator_impl_case_test.cc",
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index c5520c3..a338997 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -542,6 +542,7 @@
if (${TINT_BUILD_HLSL_WRITER})
list(APPEND TINT_TEST_SRCS
+ writer/hlsl/generator_impl_assign_test.cc
writer/hlsl/generator_impl_binary_test.cc
writer/hlsl/generator_impl_break_test.cc
writer/hlsl/generator_impl_case_test.cc
diff --git a/src/writer/hlsl/generator_impl.cc b/src/writer/hlsl/generator_impl.cc
index f4105ba..d7718a3 100644
--- a/src/writer/hlsl/generator_impl.cc
+++ b/src/writer/hlsl/generator_impl.cc
@@ -14,6 +14,7 @@
#include "src/writer/hlsl/generator_impl.h"
+#include "src/ast/assignment_statement.h"
#include "src/ast/binary_expression.h"
#include "src/ast/bool_literal.h"
#include "src/ast/case_statement.h"
@@ -72,6 +73,24 @@
return name;
}
+bool GeneratorImpl::EmitAssign(ast::AssignmentStatement* stmt) {
+ make_indent();
+
+ if (!EmitExpression(stmt->lhs())) {
+ return false;
+ }
+
+ out_ << " = ";
+
+ if (!EmitExpression(stmt->rhs())) {
+ return false;
+ }
+
+ out_ << ";" << std::endl;
+
+ return true;
+}
+
bool GeneratorImpl::EmitBinary(ast::BinaryExpression* expr) {
out_ << "(";
@@ -304,6 +323,9 @@
}
bool GeneratorImpl::EmitStatement(ast::Statement* stmt) {
+ if (stmt->IsAssign()) {
+ return EmitAssign(stmt->AsAssign());
+ }
if (stmt->IsBreak()) {
return EmitBreak(stmt->AsBreak());
}
diff --git a/src/writer/hlsl/generator_impl.h b/src/writer/hlsl/generator_impl.h
index 8e4f546..9931639 100644
--- a/src/writer/hlsl/generator_impl.h
+++ b/src/writer/hlsl/generator_impl.h
@@ -36,6 +36,10 @@
/// @returns true on successful generation; false otherwise
bool Generate();
+ /// Handles an assignment statement
+ /// @param stmt the statement to emit
+ /// @returns true if the statement was emitted successfully
+ bool EmitAssign(ast::AssignmentStatement* stmt);
/// Handles generating a binary expression
/// @param expr the binary expression
/// @returns true if the expression was emitted, false otherwise
diff --git a/src/writer/hlsl/generator_impl_assign_test.cc b/src/writer/hlsl/generator_impl_assign_test.cc
new file mode 100644
index 0000000..b8e551a
--- /dev/null
+++ b/src/writer/hlsl/generator_impl_assign_test.cc
@@ -0,0 +1,47 @@
+// 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/assignment_statement.h"
+#include "src/ast/identifier_expression.h"
+#include "src/ast/module.h"
+#include "src/writer/hlsl/generator_impl.h"
+
+namespace tint {
+namespace writer {
+namespace hlsl {
+namespace {
+
+using HlslGeneratorImplTest = testing::Test;
+
+TEST_F(HlslGeneratorImplTest, Emit_Assign) {
+ auto lhs = std::make_unique<ast::IdentifierExpression>("lhs");
+ auto rhs = std::make_unique<ast::IdentifierExpression>("rhs");
+ ast::AssignmentStatement assign(std::move(lhs), std::move(rhs));
+
+ ast::Module m;
+ GeneratorImpl g(&m);
+ g.increment_indent();
+
+ ASSERT_TRUE(g.EmitStatement(&assign)) << g.error();
+ EXPECT_EQ(g.result(), " lhs = rhs;\n");
+}
+
+} // namespace
+} // namespace hlsl
+} // namespace writer
+} // namespace tint