diff --git a/BUILD.gn b/BUILD.gn index c329fa01ba..a2a339b8d4 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -944,6 +944,7 @@ source_set("tint_unittests_wgsl_writer_src") { "src/writer/wgsl/generator_impl_cast_test.cc", "src/writer/wgsl/generator_impl_constructor_test.cc", "src/writer/wgsl/generator_impl_continue_test.cc", + "src/writer/wgsl/generator_impl_discard_test.cc", "src/writer/wgsl/generator_impl_entry_point_test.cc", "src/writer/wgsl/generator_impl_fallthrough_test.cc", "src/writer/wgsl/generator_impl_function_test.cc", diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index aa92225c00..062b0b50bc 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -494,6 +494,7 @@ if(${TINT_BUILD_WGSL_WRITER}) writer/wgsl/generator_impl_cast_test.cc writer/wgsl/generator_impl_constructor_test.cc writer/wgsl/generator_impl_continue_test.cc + writer/wgsl/generator_impl_discard_test.cc writer/wgsl/generator_impl_entry_point_test.cc writer/wgsl/generator_impl_fallthrough_test.cc writer/wgsl/generator_impl_function_test.cc diff --git a/src/writer/wgsl/generator_impl.cc b/src/writer/wgsl/generator_impl.cc index 4eb9583acc..7d6c0f477f 100644 --- a/src/writer/wgsl/generator_impl.cc +++ b/src/writer/wgsl/generator_impl.cc @@ -643,6 +643,9 @@ bool GeneratorImpl::EmitStatement(ast::Statement* stmt) { if (stmt->IsContinue()) { return EmitContinue(stmt->AsContinue()); } + if (stmt->IsDiscard()) { + return EmitDiscard(stmt->AsDiscard()); + } if (stmt->IsFallthrough()) { return EmitFallthrough(stmt->AsFallthrough()); } @@ -768,6 +771,12 @@ bool GeneratorImpl::EmitIf(ast::IfStatement* stmt) { return true; } +bool GeneratorImpl::EmitDiscard(ast::DiscardStatement*) { + make_indent(); + out_ << "discard;" << 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 9d394ad5d0..69243c40cc 100644 --- a/src/writer/wgsl/generator_impl.h +++ b/src/writer/wgsl/generator_impl.h @@ -127,6 +127,10 @@ class GeneratorImpl : public TextGenerator { /// @param expr the constructor expression /// @returns true if the expression was emitted bool EmitConstructor(ast::ConstructorExpression* expr); + /// Handles generating a discard statement + /// @param stmt the discard statement + /// @returns true if the statement was successfully emitted + bool EmitDiscard(ast::DiscardStatement* stmt); /// Handles generating a kill statement /// @param stmt the kill statement /// @returns true if the statement was successfully emitted diff --git a/src/writer/wgsl/generator_impl_discard_test.cc b/src/writer/wgsl/generator_impl_discard_test.cc new file mode 100644 index 0000000000..9e8743764f --- /dev/null +++ b/src/writer/wgsl/generator_impl_discard_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/discard_statement.h" +#include "src/writer/wgsl/generator_impl.h" + +namespace tint { +namespace writer { +namespace wgsl { +namespace { + +using WgslGeneratorImplTest = testing::Test; + +TEST_F(WgslGeneratorImplTest, Emit_Discard) { + ast::DiscardStatement k; + + GeneratorImpl g; + g.increment_indent(); + + ASSERT_TRUE(g.EmitStatement(&k)) << g.error(); + EXPECT_EQ(g.result(), " discard;\n"); +} + +} // namespace +} // namespace wgsl +} // namespace writer +} // namespace tint