Add Kill, Nop and Fallthrough to WGSL writer
This CL adds the Kill, Nop and Fallthrough statements to the WGSL
writer.
Bug: tint:4
Change-Id: Ic0c635cfa7ca4c3195c593b119f9436ffeb1f9dc
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/17201
Reviewed-by: David Neto <dneto@google.com>
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 7d68e42..cc8a03a 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -414,10 +414,13 @@
writer/wgsl/generator_impl_cast_test.cc
writer/wgsl/generator_impl_continue_test.cc
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_import_test.cc
writer/wgsl/generator_impl_initializer_test.cc
+ writer/wgsl/generator_impl_kill_test.cc
writer/wgsl/generator_impl_member_accessor_test.cc
+ writer/wgsl/generator_impl_nop_test.cc
writer/wgsl/generator_impl_relational_test.cc
writer/wgsl/generator_impl_type_test.cc
writer/wgsl/generator_impl_unary_derivative_test.cc
diff --git a/src/writer/wgsl/generator_impl.cc b/src/writer/wgsl/generator_impl.cc
index d0a3fb6..d1d992d 100644
--- a/src/writer/wgsl/generator_impl.cc
+++ b/src/writer/wgsl/generator_impl.cc
@@ -660,6 +660,15 @@
if (stmt->IsContinue()) {
return EmitContinue(stmt->AsContinue());
}
+ if (stmt->IsFallthrough()) {
+ return EmitFallthrough(stmt->AsFallthrough());
+ }
+ if (stmt->IsKill()) {
+ return EmitKill(stmt->AsKill());
+ }
+ if (stmt->IsNop()) {
+ return EmitNop(stmt->AsNop());
+ }
error_ = "unknown statement type";
return false;
@@ -762,6 +771,24 @@
return true;
}
+bool GeneratorImpl::EmitFallthrough(ast::FallthroughStatement*) {
+ make_indent();
+ out_ << "fallthrough;" << std::endl;
+ return true;
+}
+
+bool GeneratorImpl::EmitKill(ast::KillStatement*) {
+ make_indent();
+ out_ << "kill;" << std::endl;
+ return true;
+}
+
+bool GeneratorImpl::EmitNop(ast::NopStatement*) {
+ make_indent();
+ out_ << "nop;" << std::endl;
+ return true;
+}
+
} // namespace wgsl
} // namespace writer
} // namespace tint
diff --git a/src/writer/wgsl/generator_impl.h b/src/writer/wgsl/generator_impl.h
index d69c32e..3503954 100644
--- a/src/writer/wgsl/generator_impl.h
+++ b/src/writer/wgsl/generator_impl.h
@@ -114,6 +114,10 @@
/// @param expr the expression
/// @returns true if the expression was emitted
bool EmitExpression(ast::Expression* expr);
+ /// Handles generating a fallthrough statement
+ /// @param stmt the fallthrough statement
+ /// @returns true if the statement was successfully emitted
+ bool EmitFallthrough(ast::FallthroughStatement* stmt);
/// Handles generating an identifier expression
/// @param expr the identifier expression
/// @returns true if the identifeir was emitted
@@ -126,6 +130,10 @@
/// @param expr the initializer expression
/// @returns true if the expression was emitted
bool EmitInitializer(ast::InitializerExpression* expr);
+ /// Handles generating a kill statement
+ /// @param stmt the kill statement
+ /// @returns true if the statement was successfully emitted
+ bool EmitKill(ast::KillStatement* stmt);
/// Handles a literal
/// @param lit the literal to emit
/// @returns true if the literal was successfully emitted
@@ -134,6 +142,10 @@
/// @param expr the member accessor expression
/// @returns true if the member accessor was emitted
bool EmitMemberAccessor(ast::MemberAccessorExpression* expr);
+ /// Handles generating a nop statement
+ /// @param stmt the nop statement
+ /// @returns true if the statement was successfully emitted
+ bool EmitNop(ast::NopStatement* stmt);
/// Handles generating a relational expression
/// @param expr the relational expression
/// @returns true if the expression was emitted, false otherwise
diff --git a/src/writer/wgsl/generator_impl_fallthrough_test.cc b/src/writer/wgsl/generator_impl_fallthrough_test.cc
new file mode 100644
index 0000000..63fac29
--- /dev/null
+++ b/src/writer/wgsl/generator_impl_fallthrough_test.cc
@@ -0,0 +1,39 @@
+// 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 "gtest/gtest.h"
+#include "src/ast/fallthrough_statement.h"
+#include "src/writer/wgsl/generator_impl.h"
+
+namespace tint {
+namespace writer {
+namespace wgsl {
+namespace {
+
+using GeneratorImplTest = testing::Test;
+
+TEST_F(GeneratorImplTest, Emit_Fallthrough) {
+ ast::FallthroughStatement f;
+
+ GeneratorImpl g;
+ g.increment_indent();
+
+ ASSERT_TRUE(g.EmitStatement(&f)) << g.error();
+ EXPECT_EQ(g.result(), " fallthrough;\n");
+}
+
+} // namespace
+} // namespace wgsl
+} // namespace writer
+} // namespace tint
diff --git a/src/writer/wgsl/generator_impl_kill_test.cc b/src/writer/wgsl/generator_impl_kill_test.cc
new file mode 100644
index 0000000..4371452
--- /dev/null
+++ b/src/writer/wgsl/generator_impl_kill_test.cc
@@ -0,0 +1,39 @@
+// 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 "gtest/gtest.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_kill) {
+ ast::KillStatement k;
+
+ GeneratorImpl g;
+ g.increment_indent();
+
+ ASSERT_TRUE(g.EmitStatement(&k)) << g.error();
+ EXPECT_EQ(g.result(), " kill;\n");
+}
+
+} // namespace
+} // namespace wgsl
+} // namespace writer
+} // namespace tint
diff --git a/src/writer/wgsl/generator_impl_nop_test.cc b/src/writer/wgsl/generator_impl_nop_test.cc
new file mode 100644
index 0000000..0c50ea7
--- /dev/null
+++ b/src/writer/wgsl/generator_impl_nop_test.cc
@@ -0,0 +1,39 @@
+// 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 "gtest/gtest.h"
+#include "src/ast/nop_statement.h"
+#include "src/writer/wgsl/generator_impl.h"
+
+namespace tint {
+namespace writer {
+namespace wgsl {
+namespace {
+
+using GeneratorImplTest = testing::Test;
+
+TEST_F(GeneratorImplTest, Emit_Nop) {
+ ast::NopStatement n;
+
+ GeneratorImpl g;
+ g.increment_indent();
+
+ ASSERT_TRUE(g.EmitStatement(&n)) << g.error();
+ EXPECT_EQ(g.result(), " nop;\n");
+}
+
+} // namespace
+} // namespace wgsl
+} // namespace writer
+} // namespace tint