Emit WGSL continue statement.

This Cl adds support for emitting the WGSL continue statement.

Bug: tint:4
Change-Id: I20f87d46e707bdd7ae2a517983e779988892c445
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/17200
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-03-20 19:04:54 +00:00 committed by dan sinclair
parent 37f4fb0f75
commit 17686fc57c
4 changed files with 99 additions and 0 deletions

View File

@ -412,6 +412,7 @@ if(${TINT_BUILD_WGSL_WRITER})
writer/wgsl/generator_impl_call_test.cc
writer/wgsl/generator_impl_case_test.cc
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_identifier_test.cc
writer/wgsl/generator_impl_import_test.cc

View File

@ -28,6 +28,7 @@
#include "src/ast/case_statement.h"
#include "src/ast/cast_expression.h"
#include "src/ast/const_initializer_expression.h"
#include "src/ast/continue_statement.h"
#include "src/ast/decorated_variable.h"
#include "src/ast/float_literal.h"
#include "src/ast/identifier_expression.h"
@ -656,6 +657,9 @@ bool GeneratorImpl::EmitStatement(ast::Statement* stmt) {
if (stmt->IsCase()) {
return EmitCase(stmt->AsCase());
}
if (stmt->IsContinue()) {
return EmitContinue(stmt->AsContinue());
}
error_ = "unknown statement type";
return false;
@ -733,6 +737,31 @@ bool GeneratorImpl::EmitCase(ast::CaseStatement* stmt) {
return true;
}
bool GeneratorImpl::EmitContinue(ast::ContinueStatement* stmt) {
make_indent();
out_ << "continue";
if (stmt->condition() != ast::StatementCondition::kNone) {
out_ << " ";
if (stmt->condition() == ast::StatementCondition::kIf) {
out_ << "if";
} else {
out_ << "unless";
}
out_ << " (";
if (!EmitExpression(stmt->conditional())) {
return false;
}
out_ << ")";
}
out_ << ";" << std::endl;
return true;
}
} // namespace wgsl
} // namespace writer
} // namespace tint

View File

@ -102,6 +102,10 @@ class GeneratorImpl {
/// @param expr the const initializer expression
/// @returns true if the initializer is emitted
bool EmitConstInitializer(ast::ConstInitializerExpression* expr);
/// Handles a continue statement
/// @param stmt the statement to emit
/// @returns true if the statement was emitted successfully
bool EmitContinue(ast::ContinueStatement* stmt);
/// Handles generating an entry_point command
/// @param ep the entry point
/// @returns true if the entry point was emitted

View File

@ -0,0 +1,65 @@
// 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/continue_statement.h"
#include "src/ast/identifier_expression.h"
#include "src/writer/wgsl/generator_impl.h"
namespace tint {
namespace writer {
namespace wgsl {
namespace {
using GeneratorImplTest = testing::Test;
TEST_F(GeneratorImplTest, Emit_Continue) {
ast::ContinueStatement c;
GeneratorImpl g;
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&c)) << g.error();
EXPECT_EQ(g.result(), " continue;\n");
}
TEST_F(GeneratorImplTest, Emit_ContinueWithIf) {
auto expr = std::make_unique<ast::IdentifierExpression>("expr");
ast::ContinueStatement c(ast::StatementCondition::kIf, std::move(expr));
GeneratorImpl g;
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&c)) << g.error();
EXPECT_EQ(g.result(), " continue if (expr);\n");
}
TEST_F(GeneratorImplTest, Emit_ContinueWithUnless) {
auto expr = std::make_unique<ast::IdentifierExpression>("expr");
ast::ContinueStatement c(ast::StatementCondition::kUnless, std::move(expr));
GeneratorImpl g;
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&c)) << g.error();
EXPECT_EQ(g.result(), " continue unless (expr);\n");
}
} // namespace
} // namespace wgsl
} // namespace writer
} // namespace tint