Emit WGSL Break statement.

This CL adds emitting of the break statement to the WGSL writer.

Bug: tint:4
Change-Id: Iceae8cf8c1c77d63455621ea23f566af57c9e717
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/17163
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-03-20 19:02:05 +00:00 committed by dan sinclair
parent 7f9d570a5a
commit bf5ab65e98
6 changed files with 105 additions and 2 deletions

View File

@ -408,6 +408,7 @@ if(${TINT_BUILD_WGSL_WRITER})
writer/wgsl/generator_impl_array_accessor_test.cc writer/wgsl/generator_impl_array_accessor_test.cc
writer/wgsl/generator_impl_as_test.cc writer/wgsl/generator_impl_as_test.cc
writer/wgsl/generator_impl_assign_test.cc writer/wgsl/generator_impl_assign_test.cc
writer/wgsl/generator_impl_break_test.cc
writer/wgsl/generator_impl_call_test.cc writer/wgsl/generator_impl_call_test.cc
writer/wgsl/generator_impl_cast_test.cc writer/wgsl/generator_impl_cast_test.cc
writer/wgsl/generator_impl_entry_point_test.cc writer/wgsl/generator_impl_entry_point_test.cc

View File

@ -86,7 +86,7 @@ class Function : public Node {
/// @returns the function body /// @returns the function body
const std::vector<std::unique_ptr<Statement>>& body() const { return body_; } const std::vector<std::unique_ptr<Statement>>& body() const { return body_; }
/// @returns true if the name and path are both present /// @returns true if the name and type are both present
bool IsValid() const override; bool IsValid() const override;
/// Writes a representation of the node to the output stream /// Writes a representation of the node to the output stream

View File

@ -22,6 +22,7 @@
#include "src/ast/assignment_statement.h" #include "src/ast/assignment_statement.h"
#include "src/ast/binding_decoration.h" #include "src/ast/binding_decoration.h"
#include "src/ast/bool_literal.h" #include "src/ast/bool_literal.h"
#include "src/ast/break_statement.h"
#include "src/ast/builtin_decoration.h" #include "src/ast/builtin_decoration.h"
#include "src/ast/call_expression.h" #include "src/ast/call_expression.h"
#include "src/ast/cast_expression.h" #include "src/ast/cast_expression.h"
@ -645,12 +646,17 @@ bool GeneratorImpl::EmitStatement(ast::Statement* stmt) {
if (stmt->IsAssign()) { if (stmt->IsAssign()) {
return EmitAssign(stmt->AsAssign()); return EmitAssign(stmt->AsAssign());
} }
if (stmt->IsBreak()) {
return EmitBreak(stmt->AsBreak());
}
error_ = "unknown statement type"; error_ = "unknown statement type";
return false; return false;
} }
bool GeneratorImpl::EmitAssign(ast::AssignmentStatement* stmt) { bool GeneratorImpl::EmitAssign(ast::AssignmentStatement* stmt) {
make_indent();
if (!EmitExpression(stmt->lhs())) { if (!EmitExpression(stmt->lhs())) {
return false; return false;
} }
@ -666,6 +672,31 @@ bool GeneratorImpl::EmitAssign(ast::AssignmentStatement* stmt) {
return true; return true;
} }
bool GeneratorImpl::EmitBreak(ast::BreakStatement* stmt) {
make_indent();
out_ << "break";
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_ << ";";
return true;
}
} // namespace wgsl } // namespace wgsl
} // namespace writer } // namespace writer
} // namespace tint } // namespace tint

View File

@ -82,6 +82,10 @@ class GeneratorImpl {
/// @param stmt the statement to emit /// @param stmt the statement to emit
/// @returns true if the statement was emitted successfully /// @returns true if the statement was emitted successfully
bool EmitAssign(ast::AssignmentStatement* stmt); bool EmitAssign(ast::AssignmentStatement* stmt);
/// Handles a break statement
/// @param stmt the statement to emit
/// @returns true if the statement was emitted successfully
bool EmitBreak(ast::BreakStatement* stmt);
/// Handles generating a call expression /// Handles generating a call expression
/// @param expr the call expression /// @param expr the call expression
/// @returns true if the call expression is emitted /// @returns true if the call expression is emitted

View File

@ -33,8 +33,10 @@ TEST_F(GeneratorImplTest, Emit_Assign) {
ast::AssignmentStatement assign(std::move(lhs), std::move(rhs)); ast::AssignmentStatement assign(std::move(lhs), std::move(rhs));
GeneratorImpl g; GeneratorImpl g;
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&assign)) << g.error(); ASSERT_TRUE(g.EmitStatement(&assign)) << g.error();
EXPECT_EQ(g.result(), "lhs = rhs;"); EXPECT_EQ(g.result(), " lhs = rhs;");
} }
} // namespace } // namespace

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/break_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_Break) {
ast::BreakStatement b;
GeneratorImpl g;
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&b)) << g.error();
EXPECT_EQ(g.result(), " break;");
}
TEST_F(GeneratorImplTest, Emit_BreakWithIf) {
auto expr = std::make_unique<ast::IdentifierExpression>("expr");
ast::BreakStatement b(ast::StatementCondition::kIf, std::move(expr));
GeneratorImpl g;
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&b)) << g.error();
EXPECT_EQ(g.result(), " break if (expr);");
}
TEST_F(GeneratorImplTest, Emit_BreakWithUnless) {
auto expr = std::make_unique<ast::IdentifierExpression>("expr");
ast::BreakStatement b(ast::StatementCondition::kUnless, std::move(expr));
GeneratorImpl g;
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&b)) << g.error();
EXPECT_EQ(g.result(), " break unless (expr);");
}
} // namespace
} // namespace wgsl
} // namespace writer
} // namespace tint