Emit If statements.
This CL adds emitting of if statements to the WGSL generator.
Bug: tint:4
Change-Id: Ie79b40779a028dd40eac99c5459c971ead6cc43e
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/17286
Reviewed-by: David Neto <dneto@google.com>
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index e426902..142be9b 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -417,6 +417,7 @@
writer/wgsl/generator_impl_entry_point_test.cc
writer/wgsl/generator_impl_fallthrough_test.cc
writer/wgsl/generator_impl_identifier_test.cc
+ writer/wgsl/generator_impl_if_test.cc
writer/wgsl/generator_impl_import_test.cc
writer/wgsl/generator_impl_initializer_test.cc
writer/wgsl/generator_impl_kill_test.cc
diff --git a/src/writer/wgsl/generator_impl.cc b/src/writer/wgsl/generator_impl.cc
index 5d405a8..3c24e79 100644
--- a/src/writer/wgsl/generator_impl.cc
+++ b/src/writer/wgsl/generator_impl.cc
@@ -20,7 +20,6 @@
#include "src/ast/array_accessor_expression.h"
#include "src/ast/as_expression.h"
#include "src/ast/assignment_statement.h"
-#include "src/ast/else_statement.h"
#include "src/ast/binding_decoration.h"
#include "src/ast/bool_literal.h"
#include "src/ast/break_statement.h"
@@ -31,8 +30,10 @@
#include "src/ast/const_initializer_expression.h"
#include "src/ast/continue_statement.h"
#include "src/ast/decorated_variable.h"
+#include "src/ast/else_statement.h"
#include "src/ast/float_literal.h"
#include "src/ast/identifier_expression.h"
+#include "src/ast/if_statement.h"
#include "src/ast/initializer_expression.h"
#include "src/ast/int_literal.h"
#include "src/ast/location_decoration.h"
@@ -667,6 +668,9 @@
if (stmt->IsFallthrough()) {
return EmitFallthrough(stmt->AsFallthrough());
}
+ if (stmt->IsIf()) {
+ return EmitIf(stmt->AsIf());
+ }
if (stmt->IsKill()) {
return EmitKill(stmt->AsKill());
}
@@ -827,6 +831,38 @@
return true;
}
+bool GeneratorImpl::EmitIf(ast::IfStatement* stmt) {
+ make_indent();
+
+ out_ << "if (";
+ if (!EmitExpression(stmt->condition())) {
+ return false;
+ }
+ out_ << ") {" << std::endl;
+
+ increment_indent();
+
+ for (const auto& b : stmt->body()) {
+ if (!EmitStatement(b.get())) {
+ return false;
+ }
+ }
+
+ decrement_indent();
+ make_indent();
+ out_ << "}";
+
+ for (const auto& e : stmt->else_statements()) {
+ if (!EmitElse(e.get())) {
+ return false;
+ }
+ }
+
+ out_ << std::endl;
+
+ return true;
+}
+
bool GeneratorImpl::EmitKill(ast::KillStatement*) {
make_indent();
out_ << "kill;" << std::endl;
diff --git a/src/writer/wgsl/generator_impl.h b/src/writer/wgsl/generator_impl.h
index 4ab7e4d..e842704 100644
--- a/src/writer/wgsl/generator_impl.h
+++ b/src/writer/wgsl/generator_impl.h
@@ -126,6 +126,10 @@
/// @param expr the identifier expression
/// @returns true if the identifeir was emitted
bool EmitIdentifier(ast::IdentifierExpression* expr);
+ /// Handles an if statement
+ /// @param stmt the statement to emit
+ /// @returns true if the statement was successfully emitted
+ bool EmitIf(ast::IfStatement* stmt);
/// Handles generating an import command
/// @param import the import to generate
/// @returns true if the import was emitted
diff --git a/src/writer/wgsl/generator_impl_else_test.cc b/src/writer/wgsl/generator_impl_else_test.cc
index 69badbf..561babf 100644
--- a/src/writer/wgsl/generator_impl_else_test.cc
+++ b/src/writer/wgsl/generator_impl_else_test.cc
@@ -16,9 +16,9 @@
#include <vector>
#include "gtest/gtest.h"
-#include "src/ast/kill_statement.h"
#include "src/ast/else_statement.h"
#include "src/ast/identifier_expression.h"
+#include "src/ast/kill_statement.h"
#include "src/writer/wgsl/generator_impl.h"
namespace tint {
@@ -64,4 +64,3 @@
} // namespace wgsl
} // namespace writer
} // namespace tint
-
diff --git a/src/writer/wgsl/generator_impl_if_test.cc b/src/writer/wgsl/generator_impl_if_test.cc
new file mode 100644
index 0000000..274c7c0
--- /dev/null
+++ b/src/writer/wgsl/generator_impl_if_test.cc
@@ -0,0 +1,142 @@
+// 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/else_statement.h"
+#include "src/ast/identifier_expression.h"
+#include "src/ast/if_statement.h"
+#include "src/ast/kill_statement.h"
+#include "src/writer/wgsl/generator_impl.h"
+
+namespace tint {
+namespace writer {
+namespace wgsl {
+namespace {
+
+using GeneratorImplTest = testing::Test;
+
+TEST_F(GeneratorImplTest, Emit_If) {
+ auto cond = std::make_unique<ast::IdentifierExpression>("cond");
+ std::vector<std::unique_ptr<ast::Statement>> body;
+ body.push_back(std::make_unique<ast::KillStatement>());
+
+ ast::IfStatement i(std::move(cond), std::move(body));
+
+ GeneratorImpl g;
+ g.increment_indent();
+
+ ASSERT_TRUE(g.EmitStatement(&i)) << g.error();
+ EXPECT_EQ(g.result(), R"( if (cond) {
+ kill;
+ }
+)");
+}
+
+TEST_F(GeneratorImplTest, Emit_IfWithElseIf) {
+ auto else_cond = std::make_unique<ast::IdentifierExpression>("else_cond");
+
+ std::vector<std::unique_ptr<ast::Statement>> else_body;
+ else_body.push_back(std::make_unique<ast::KillStatement>());
+
+ std::vector<std::unique_ptr<ast::ElseStatement>> elses;
+ elses.push_back(std::make_unique<ast::ElseStatement>(std::move(else_cond),
+ std::move(else_body)));
+
+ auto cond = std::make_unique<ast::IdentifierExpression>("cond");
+ std::vector<std::unique_ptr<ast::Statement>> body;
+ body.push_back(std::make_unique<ast::KillStatement>());
+
+ ast::IfStatement i(std::move(cond), std::move(body));
+ i.set_else_statements(std::move(elses));
+
+ GeneratorImpl g;
+ g.increment_indent();
+
+ ASSERT_TRUE(g.EmitStatement(&i)) << g.error();
+ EXPECT_EQ(g.result(), R"( if (cond) {
+ kill;
+ } elseif (else_cond) {
+ kill;
+ }
+)");
+}
+
+TEST_F(GeneratorImplTest, Emit_IfWithElse) {
+ std::vector<std::unique_ptr<ast::Statement>> else_body;
+ else_body.push_back(std::make_unique<ast::KillStatement>());
+
+ std::vector<std::unique_ptr<ast::ElseStatement>> elses;
+ elses.push_back(std::make_unique<ast::ElseStatement>(std::move(else_body)));
+
+ auto cond = std::make_unique<ast::IdentifierExpression>("cond");
+ std::vector<std::unique_ptr<ast::Statement>> body;
+ body.push_back(std::make_unique<ast::KillStatement>());
+
+ ast::IfStatement i(std::move(cond), std::move(body));
+ i.set_else_statements(std::move(elses));
+
+ GeneratorImpl g;
+ g.increment_indent();
+
+ ASSERT_TRUE(g.EmitStatement(&i)) << g.error();
+ EXPECT_EQ(g.result(), R"( if (cond) {
+ kill;
+ } else {
+ kill;
+ }
+)");
+}
+
+TEST_F(GeneratorImplTest, Emit_IfWithMultiple) {
+ auto else_cond = std::make_unique<ast::IdentifierExpression>("else_cond");
+
+ std::vector<std::unique_ptr<ast::Statement>> else_body;
+ else_body.push_back(std::make_unique<ast::KillStatement>());
+
+ std::vector<std::unique_ptr<ast::Statement>> else_body_2;
+ else_body_2.push_back(std::make_unique<ast::KillStatement>());
+
+ std::vector<std::unique_ptr<ast::ElseStatement>> elses;
+ elses.push_back(std::make_unique<ast::ElseStatement>(std::move(else_cond),
+ std::move(else_body)));
+ elses.push_back(std::make_unique<ast::ElseStatement>(std::move(else_body_2)));
+
+ auto cond = std::make_unique<ast::IdentifierExpression>("cond");
+ std::vector<std::unique_ptr<ast::Statement>> body;
+ body.push_back(std::make_unique<ast::KillStatement>());
+
+ ast::IfStatement i(std::move(cond), std::move(body));
+ i.set_else_statements(std::move(elses));
+
+ GeneratorImpl g;
+ g.increment_indent();
+
+ ASSERT_TRUE(g.EmitStatement(&i)) << g.error();
+ EXPECT_EQ(g.result(), R"( if (cond) {
+ kill;
+ } elseif (else_cond) {
+ kill;
+ } else {
+ kill;
+ }
+)");
+}
+
+} // namespace
+} // namespace wgsl
+} // namespace writer
+} // namespace tint