mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-07-07 05:36:04 +00:00
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:
parent
c083af83ea
commit
2a6e275057
@ -422,6 +422,7 @@ if(${TINT_BUILD_WGSL_WRITER})
|
|||||||
writer/wgsl/generator_impl_member_accessor_test.cc
|
writer/wgsl/generator_impl_member_accessor_test.cc
|
||||||
writer/wgsl/generator_impl_nop_test.cc
|
writer/wgsl/generator_impl_nop_test.cc
|
||||||
writer/wgsl/generator_impl_relational_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_type_test.cc
|
||||||
writer/wgsl/generator_impl_unary_derivative_test.cc
|
writer/wgsl/generator_impl_unary_derivative_test.cc
|
||||||
writer/wgsl/generator_impl_unary_method_test.cc
|
writer/wgsl/generator_impl_unary_method_test.cc
|
||||||
|
@ -50,6 +50,8 @@ class ReturnStatement : public Statement {
|
|||||||
}
|
}
|
||||||
/// @returns the value
|
/// @returns the value
|
||||||
Expression* value() const { return value_.get(); }
|
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
|
/// @returns true if this is a return statement
|
||||||
bool IsReturn() const override { return true; }
|
bool IsReturn() const override { return true; }
|
||||||
|
@ -44,6 +44,17 @@ TEST_F(ReturnStatementTest, IsReturn) {
|
|||||||
EXPECT_TRUE(r.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) {
|
TEST_F(ReturnStatementTest, IsValid_WithoutValue) {
|
||||||
ReturnStatement r;
|
ReturnStatement r;
|
||||||
EXPECT_TRUE(r.IsValid());
|
EXPECT_TRUE(r.IsValid());
|
||||||
|
@ -37,6 +37,7 @@
|
|||||||
#include "src/ast/location_decoration.h"
|
#include "src/ast/location_decoration.h"
|
||||||
#include "src/ast/member_accessor_expression.h"
|
#include "src/ast/member_accessor_expression.h"
|
||||||
#include "src/ast/relational_expression.h"
|
#include "src/ast/relational_expression.h"
|
||||||
|
#include "src/ast/return_statement.h"
|
||||||
#include "src/ast/set_decoration.h"
|
#include "src/ast/set_decoration.h"
|
||||||
#include "src/ast/statement.h"
|
#include "src/ast/statement.h"
|
||||||
#include "src/ast/struct.h"
|
#include "src/ast/struct.h"
|
||||||
@ -669,6 +670,9 @@ bool GeneratorImpl::EmitStatement(ast::Statement* stmt) {
|
|||||||
if (stmt->IsNop()) {
|
if (stmt->IsNop()) {
|
||||||
return EmitNop(stmt->AsNop());
|
return EmitNop(stmt->AsNop());
|
||||||
}
|
}
|
||||||
|
if (stmt->IsReturn()) {
|
||||||
|
return EmitReturn(stmt->AsReturn());
|
||||||
|
}
|
||||||
|
|
||||||
error_ = "unknown statement type";
|
error_ = "unknown statement type";
|
||||||
return false;
|
return false;
|
||||||
@ -789,6 +793,20 @@ bool GeneratorImpl::EmitNop(ast::NopStatement*) {
|
|||||||
return true;
|
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 wgsl
|
||||||
} // namespace writer
|
} // namespace writer
|
||||||
} // namespace tint
|
} // namespace tint
|
||||||
|
@ -150,6 +150,10 @@ class GeneratorImpl {
|
|||||||
/// @param expr the relational expression
|
/// @param expr the relational expression
|
||||||
/// @returns true if the expression was emitted, false otherwise
|
/// @returns true if the expression was emitted, false otherwise
|
||||||
bool EmitRelational(ast::RelationalExpression* expr);
|
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
|
/// Handles statements
|
||||||
/// @param stmt the statement to emit
|
/// @param stmt the statement to emit
|
||||||
/// @returns true if the statement was emitted
|
/// @returns true if the statement was emitted
|
||||||
|
54
src/writer/wgsl/generator_impl_return_test.cc
Normal file
54
src/writer/wgsl/generator_impl_return_test.cc
Normal 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
|
Loading…
x
Reference in New Issue
Block a user