[wgsl-writer] Add DiscardStatement support

This CL adds the DiscardStatement to the WGSL writer.

Bug: tint:165
Change-Id: I012bc030482e097fd9a957ed96f15d8360f621a5
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/25603
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-07-25 14:33:04 +00:00 committed by dan sinclair
parent a9696b511f
commit d81bebcfcc
5 changed files with 54 additions and 0 deletions

View File

@ -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",

View File

@ -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

View File

@ -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;

View File

@ -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

View File

@ -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