Emit else statements.
This Cl updates the WGSL writer to generate else statements. Bug: tint:4 Change-Id: I72f6bbb6a2ab00bec3933083fd12c5da1a3dd5f8 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/17285 Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
parent
0d00402124
commit
eaf2689a44
|
@ -413,6 +413,7 @@ if(${TINT_BUILD_WGSL_WRITER})
|
||||||
writer/wgsl/generator_impl_case_test.cc
|
writer/wgsl/generator_impl_case_test.cc
|
||||||
writer/wgsl/generator_impl_cast_test.cc
|
writer/wgsl/generator_impl_cast_test.cc
|
||||||
writer/wgsl/generator_impl_continue_test.cc
|
writer/wgsl/generator_impl_continue_test.cc
|
||||||
|
writer/wgsl/generator_impl_else_test.cc
|
||||||
writer/wgsl/generator_impl_entry_point_test.cc
|
writer/wgsl/generator_impl_entry_point_test.cc
|
||||||
writer/wgsl/generator_impl_fallthrough_test.cc
|
writer/wgsl/generator_impl_fallthrough_test.cc
|
||||||
writer/wgsl/generator_impl_identifier_test.cc
|
writer/wgsl/generator_impl_identifier_test.cc
|
||||||
|
|
|
@ -20,6 +20,7 @@
|
||||||
#include "src/ast/array_accessor_expression.h"
|
#include "src/ast/array_accessor_expression.h"
|
||||||
#include "src/ast/as_expression.h"
|
#include "src/ast/as_expression.h"
|
||||||
#include "src/ast/assignment_statement.h"
|
#include "src/ast/assignment_statement.h"
|
||||||
|
#include "src/ast/else_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/break_statement.h"
|
||||||
|
@ -660,9 +661,6 @@ bool GeneratorImpl::EmitStatement(ast::Statement* stmt) {
|
||||||
if (stmt->IsBreak()) {
|
if (stmt->IsBreak()) {
|
||||||
return EmitBreak(stmt->AsBreak());
|
return EmitBreak(stmt->AsBreak());
|
||||||
}
|
}
|
||||||
if (stmt->IsCase()) {
|
|
||||||
return EmitCase(stmt->AsCase());
|
|
||||||
}
|
|
||||||
if (stmt->IsContinue()) {
|
if (stmt->IsContinue()) {
|
||||||
return EmitContinue(stmt->AsContinue());
|
return EmitContinue(stmt->AsContinue());
|
||||||
}
|
}
|
||||||
|
@ -795,6 +793,34 @@ bool GeneratorImpl::EmitContinue(ast::ContinueStatement* stmt) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool GeneratorImpl::EmitElse(ast::ElseStatement* stmt) {
|
||||||
|
if (stmt->HasCondition()) {
|
||||||
|
out_ << " elseif (";
|
||||||
|
if (!EmitExpression(stmt->condition())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
out_ << ")";
|
||||||
|
} else {
|
||||||
|
out_ << " else";
|
||||||
|
}
|
||||||
|
out_ << " {" << std::endl;
|
||||||
|
|
||||||
|
increment_indent();
|
||||||
|
|
||||||
|
for (const auto& s : stmt->body()) {
|
||||||
|
if (!EmitStatement(s.get())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
decrement_indent();
|
||||||
|
|
||||||
|
make_indent();
|
||||||
|
out_ << "}";
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
bool GeneratorImpl::EmitFallthrough(ast::FallthroughStatement*) {
|
bool GeneratorImpl::EmitFallthrough(ast::FallthroughStatement*) {
|
||||||
make_indent();
|
make_indent();
|
||||||
out_ << "fallthrough;" << std::endl;
|
out_ << "fallthrough;" << std::endl;
|
||||||
|
|
|
@ -106,6 +106,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 EmitContinue(ast::ContinueStatement* stmt);
|
bool EmitContinue(ast::ContinueStatement* stmt);
|
||||||
|
/// Handles generating an else statement
|
||||||
|
/// @param stmt the statement to emit
|
||||||
|
/// @returns true if the statement was emitted
|
||||||
|
bool EmitElse(ast::ElseStatement* stmt);
|
||||||
/// Handles generating an entry_point command
|
/// Handles generating an entry_point command
|
||||||
/// @param ep the entry point
|
/// @param ep the entry point
|
||||||
/// @returns true if the entry point was emitted
|
/// @returns true if the entry point was emitted
|
||||||
|
|
|
@ -40,7 +40,7 @@ TEST_F(GeneratorImplTest, Emit_Case) {
|
||||||
GeneratorImpl g;
|
GeneratorImpl g;
|
||||||
g.increment_indent();
|
g.increment_indent();
|
||||||
|
|
||||||
ASSERT_TRUE(g.EmitStatement(&c)) << g.error();
|
ASSERT_TRUE(g.EmitCase(&c)) << g.error();
|
||||||
EXPECT_EQ(g.result(), R"( case 5: {
|
EXPECT_EQ(g.result(), R"( case 5: {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -58,7 +58,7 @@ TEST_F(GeneratorImplTest, Emit_Case_Default) {
|
||||||
GeneratorImpl g;
|
GeneratorImpl g;
|
||||||
g.increment_indent();
|
g.increment_indent();
|
||||||
|
|
||||||
ASSERT_TRUE(g.EmitStatement(&c)) << g.error();
|
ASSERT_TRUE(g.EmitCase(&c)) << g.error();
|
||||||
EXPECT_EQ(g.result(), R"( default: {
|
EXPECT_EQ(g.result(), R"( default: {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,67 @@
|
||||||
|
// 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/kill_statement.h"
|
||||||
|
#include "src/ast/else_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_Else) {
|
||||||
|
std::vector<std::unique_ptr<ast::Statement>> body;
|
||||||
|
body.push_back(std::make_unique<ast::KillStatement>());
|
||||||
|
|
||||||
|
ast::ElseStatement e(std::move(body));
|
||||||
|
|
||||||
|
GeneratorImpl g;
|
||||||
|
g.increment_indent();
|
||||||
|
|
||||||
|
ASSERT_TRUE(g.EmitElse(&e)) << g.error();
|
||||||
|
EXPECT_EQ(g.result(), R"( else {
|
||||||
|
kill;
|
||||||
|
})");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(GeneratorImplTest, Emit_ElseWithCondition) {
|
||||||
|
auto cond = std::make_unique<ast::IdentifierExpression>("cond");
|
||||||
|
|
||||||
|
std::vector<std::unique_ptr<ast::Statement>> body;
|
||||||
|
body.push_back(std::make_unique<ast::KillStatement>());
|
||||||
|
|
||||||
|
ast::ElseStatement e(std::move(cond), std::move(body));
|
||||||
|
|
||||||
|
GeneratorImpl g;
|
||||||
|
g.increment_indent();
|
||||||
|
|
||||||
|
ASSERT_TRUE(g.EmitElse(&e)) << g.error();
|
||||||
|
EXPECT_EQ(g.result(), R"( elseif (cond) {
|
||||||
|
kill;
|
||||||
|
})");
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
} // namespace wgsl
|
||||||
|
} // namespace writer
|
||||||
|
} // namespace tint
|
||||||
|
|
Loading…
Reference in New Issue