Emit If statements.

This CL adds emitting of if statements to the WGSL generator.

Bug: tint:4
Change-Id: Ie79b40779a028dd40eac99c5459c971ead6cc43e
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/17286
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-03-20 19:08:56 +00:00 committed by dan sinclair
parent eaf2689a44
commit c574295a42
5 changed files with 185 additions and 3 deletions

View File

@ -417,6 +417,7 @@ if(${TINT_BUILD_WGSL_WRITER})
writer/wgsl/generator_impl_entry_point_test.cc
writer/wgsl/generator_impl_fallthrough_test.cc
writer/wgsl/generator_impl_identifier_test.cc
writer/wgsl/generator_impl_if_test.cc
writer/wgsl/generator_impl_import_test.cc
writer/wgsl/generator_impl_initializer_test.cc
writer/wgsl/generator_impl_kill_test.cc

View File

@ -20,7 +20,6 @@
#include "src/ast/array_accessor_expression.h"
#include "src/ast/as_expression.h"
#include "src/ast/assignment_statement.h"
#include "src/ast/else_statement.h"
#include "src/ast/binding_decoration.h"
#include "src/ast/bool_literal.h"
#include "src/ast/break_statement.h"
@ -31,8 +30,10 @@
#include "src/ast/const_initializer_expression.h"
#include "src/ast/continue_statement.h"
#include "src/ast/decorated_variable.h"
#include "src/ast/else_statement.h"
#include "src/ast/float_literal.h"
#include "src/ast/identifier_expression.h"
#include "src/ast/if_statement.h"
#include "src/ast/initializer_expression.h"
#include "src/ast/int_literal.h"
#include "src/ast/location_decoration.h"
@ -667,6 +668,9 @@ bool GeneratorImpl::EmitStatement(ast::Statement* stmt) {
if (stmt->IsFallthrough()) {
return EmitFallthrough(stmt->AsFallthrough());
}
if (stmt->IsIf()) {
return EmitIf(stmt->AsIf());
}
if (stmt->IsKill()) {
return EmitKill(stmt->AsKill());
}
@ -827,6 +831,38 @@ bool GeneratorImpl::EmitFallthrough(ast::FallthroughStatement*) {
return true;
}
bool GeneratorImpl::EmitIf(ast::IfStatement* stmt) {
make_indent();
out_ << "if (";
if (!EmitExpression(stmt->condition())) {
return false;
}
out_ << ") {" << std::endl;
increment_indent();
for (const auto& b : stmt->body()) {
if (!EmitStatement(b.get())) {
return false;
}
}
decrement_indent();
make_indent();
out_ << "}";
for (const auto& e : stmt->else_statements()) {
if (!EmitElse(e.get())) {
return false;
}
}
out_ << std::endl;
return true;
}
bool GeneratorImpl::EmitKill(ast::KillStatement*) {
make_indent();
out_ << "kill;" << std::endl;

View File

@ -126,6 +126,10 @@ class GeneratorImpl {
/// @param expr the identifier expression
/// @returns true if the identifeir was emitted
bool EmitIdentifier(ast::IdentifierExpression* expr);
/// Handles an if statement
/// @param stmt the statement to emit
/// @returns true if the statement was successfully emitted
bool EmitIf(ast::IfStatement* stmt);
/// Handles generating an import command
/// @param import the import to generate
/// @returns true if the import was emitted

View File

@ -16,9 +16,9 @@
#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/ast/kill_statement.h"
#include "src/writer/wgsl/generator_impl.h"
namespace tint {
@ -64,4 +64,3 @@ TEST_F(GeneratorImplTest, Emit_ElseWithCondition) {
} // namespace wgsl
} // namespace writer
} // namespace tint

View File

@ -0,0 +1,142 @@
// 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/else_statement.h"
#include "src/ast/identifier_expression.h"
#include "src/ast/if_statement.h"
#include "src/ast/kill_statement.h"
#include "src/writer/wgsl/generator_impl.h"
namespace tint {
namespace writer {
namespace wgsl {
namespace {
using GeneratorImplTest = testing::Test;
TEST_F(GeneratorImplTest, Emit_If) {
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::IfStatement i(std::move(cond), std::move(body));
GeneratorImpl g;
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&i)) << g.error();
EXPECT_EQ(g.result(), R"( if (cond) {
kill;
}
)");
}
TEST_F(GeneratorImplTest, Emit_IfWithElseIf) {
auto else_cond = std::make_unique<ast::IdentifierExpression>("else_cond");
std::vector<std::unique_ptr<ast::Statement>> else_body;
else_body.push_back(std::make_unique<ast::KillStatement>());
std::vector<std::unique_ptr<ast::ElseStatement>> elses;
elses.push_back(std::make_unique<ast::ElseStatement>(std::move(else_cond),
std::move(else_body)));
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::IfStatement i(std::move(cond), std::move(body));
i.set_else_statements(std::move(elses));
GeneratorImpl g;
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&i)) << g.error();
EXPECT_EQ(g.result(), R"( if (cond) {
kill;
} elseif (else_cond) {
kill;
}
)");
}
TEST_F(GeneratorImplTest, Emit_IfWithElse) {
std::vector<std::unique_ptr<ast::Statement>> else_body;
else_body.push_back(std::make_unique<ast::KillStatement>());
std::vector<std::unique_ptr<ast::ElseStatement>> elses;
elses.push_back(std::make_unique<ast::ElseStatement>(std::move(else_body)));
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::IfStatement i(std::move(cond), std::move(body));
i.set_else_statements(std::move(elses));
GeneratorImpl g;
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&i)) << g.error();
EXPECT_EQ(g.result(), R"( if (cond) {
kill;
} else {
kill;
}
)");
}
TEST_F(GeneratorImplTest, Emit_IfWithMultiple) {
auto else_cond = std::make_unique<ast::IdentifierExpression>("else_cond");
std::vector<std::unique_ptr<ast::Statement>> else_body;
else_body.push_back(std::make_unique<ast::KillStatement>());
std::vector<std::unique_ptr<ast::Statement>> else_body_2;
else_body_2.push_back(std::make_unique<ast::KillStatement>());
std::vector<std::unique_ptr<ast::ElseStatement>> elses;
elses.push_back(std::make_unique<ast::ElseStatement>(std::move(else_cond),
std::move(else_body)));
elses.push_back(std::make_unique<ast::ElseStatement>(std::move(else_body_2)));
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::IfStatement i(std::move(cond), std::move(body));
i.set_else_statements(std::move(elses));
GeneratorImpl g;
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&i)) << g.error();
EXPECT_EQ(g.result(), R"( if (cond) {
kill;
} elseif (else_cond) {
kill;
} else {
kill;
}
)");
}
} // namespace
} // namespace wgsl
} // namespace writer
} // namespace tint