Emit Return statements.

This CL adds emitting return statements into the WGSL writer.

Bug: tint:4
Change-Id: Ia2331f7c20e3a3e3af1aa1553cef83bec315eae3
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/17260
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-03-20 19:05:10 +00:00 committed by dan sinclair
parent c083af83ea
commit 2a6e275057
6 changed files with 90 additions and 0 deletions

View File

@ -422,6 +422,7 @@ if(${TINT_BUILD_WGSL_WRITER})
writer/wgsl/generator_impl_member_accessor_test.cc
writer/wgsl/generator_impl_nop_test.cc
writer/wgsl/generator_impl_relational_test.cc
writer/wgsl/generator_impl_return_test.cc
writer/wgsl/generator_impl_type_test.cc
writer/wgsl/generator_impl_unary_derivative_test.cc
writer/wgsl/generator_impl_unary_method_test.cc

View File

@ -50,6 +50,8 @@ class ReturnStatement : public Statement {
}
/// @returns the value
Expression* value() const { return value_.get(); }
/// @returns true if the return has a value
bool has_value() const { return value_ != nullptr; }
/// @returns true if this is a return statement
bool IsReturn() const override { return true; }

View File

@ -44,6 +44,17 @@ TEST_F(ReturnStatementTest, IsReturn) {
EXPECT_TRUE(r.IsReturn());
}
TEST_F(ReturnStatementTest, HasValue_WithoutValue) {
ReturnStatement r;
EXPECT_FALSE(r.has_value());
}
TEST_F(ReturnStatementTest, HasValue_WithValue) {
auto expr = std::make_unique<IdentifierExpression>("expr");
ReturnStatement r(std::move(expr));
EXPECT_TRUE(r.has_value());
}
TEST_F(ReturnStatementTest, IsValid_WithoutValue) {
ReturnStatement r;
EXPECT_TRUE(r.IsValid());

View File

@ -37,6 +37,7 @@
#include "src/ast/location_decoration.h"
#include "src/ast/member_accessor_expression.h"
#include "src/ast/relational_expression.h"
#include "src/ast/return_statement.h"
#include "src/ast/set_decoration.h"
#include "src/ast/statement.h"
#include "src/ast/struct.h"
@ -669,6 +670,9 @@ bool GeneratorImpl::EmitStatement(ast::Statement* stmt) {
if (stmt->IsNop()) {
return EmitNop(stmt->AsNop());
}
if (stmt->IsReturn()) {
return EmitReturn(stmt->AsReturn());
}
error_ = "unknown statement type";
return false;
@ -789,6 +793,20 @@ bool GeneratorImpl::EmitNop(ast::NopStatement*) {
return true;
}
bool GeneratorImpl::EmitReturn(ast::ReturnStatement* stmt) {
make_indent();
out_ << "return";
if (stmt->has_value()) {
out_ << " ";
if (!EmitExpression(stmt->value())) {
return false;
}
}
out_ << ";" << std::endl;
return true;
}
} // namespace wgsl
} // namespace writer
} // namespace tint

View File

@ -150,6 +150,10 @@ class GeneratorImpl {
/// @param expr the relational expression
/// @returns true if the expression was emitted, false otherwise
bool EmitRelational(ast::RelationalExpression* expr);
/// Handles return statements
/// @param stmt the statement to emit
/// @returns true if the statement was successfully emitted
bool EmitReturn(ast::ReturnStatement* stmt);
/// Handles statements
/// @param stmt the statement to emit
/// @returns true if the statement was emitted

View File

@ -0,0 +1,54 @@
// 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/identifier_expression.h"
#include "src/ast/return_statement.h"
#include "src/writer/wgsl/generator_impl.h"
namespace tint {
namespace writer {
namespace wgsl {
namespace {
using GeneratorImplTest = testing::Test;
TEST_F(GeneratorImplTest, Emit_Return) {
ast::ReturnStatement r;
GeneratorImpl g;
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&r)) << g.error();
EXPECT_EQ(g.result(), " return;\n");
}
TEST_F(GeneratorImplTest, Emit_ReturnWithValue) {
auto expr = std::make_unique<ast::IdentifierExpression>("expr");
ast::ReturnStatement r(std::move(expr));
GeneratorImpl g;
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&r)) << g.error();
EXPECT_EQ(g.result(), " return expr;\n");
}
} // namespace
} // namespace wgsl
} // namespace writer
} // namespace tint