[metal-writer] Add return generation.
This CL adds generation of the return statement. Bug: tint:8 Change-Id: Iffee600e77a485649b987d39aab47742968e438e Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/23702 Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
parent
5dd37b2e6c
commit
8cd87513b3
1
BUILD.gn
1
BUILD.gn
|
@ -885,6 +885,7 @@ source_set("tint_unittests_wgsl_writer_src") {
|
||||||
|
|
||||||
source_set("tint_unittests_msl_writer_src") {
|
source_set("tint_unittests_msl_writer_src") {
|
||||||
sources = [
|
sources = [
|
||||||
|
"src/writer/msl/generator_impl_return_test.cc",
|
||||||
"src/writer/msl/generator_impl_test.cc",
|
"src/writer/msl/generator_impl_test.cc",
|
||||||
"src/writer/msl/generator_impl_type_test.cc",
|
"src/writer/msl/generator_impl_type_test.cc",
|
||||||
]
|
]
|
||||||
|
|
|
@ -493,6 +493,7 @@ endif()
|
||||||
|
|
||||||
if(${TINT_BUILD_MSL_WRITER})
|
if(${TINT_BUILD_MSL_WRITER})
|
||||||
list(APPEND TINT_TEST_SRCS
|
list(APPEND TINT_TEST_SRCS
|
||||||
|
writer/msl/generator_impl_return_test.cc
|
||||||
writer/msl/generator_impl_test.cc
|
writer/msl/generator_impl_test.cc
|
||||||
writer/msl/generator_impl_type_test.cc
|
writer/msl/generator_impl_type_test.cc
|
||||||
)
|
)
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
|
|
||||||
#include "src/writer/msl/generator_impl.h"
|
#include "src/writer/msl/generator_impl.h"
|
||||||
|
|
||||||
|
#include "src/ast/return_statement.h"
|
||||||
#include "src/ast/type/alias_type.h"
|
#include "src/ast/type/alias_type.h"
|
||||||
#include "src/ast/type/array_type.h"
|
#include "src/ast/type/array_type.h"
|
||||||
#include "src/ast/type/bool_type.h"
|
#include "src/ast/type/bool_type.h"
|
||||||
|
@ -38,6 +39,61 @@ bool GeneratorImpl::Generate(const ast::Module&) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool GeneratorImpl::EmitExpression(ast::Expression*) {
|
||||||
|
error_ = "unknown expression type";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool GeneratorImpl::EmitStatementBlock(const ast::StatementList& statements) {
|
||||||
|
out_ << " {" << std::endl;
|
||||||
|
|
||||||
|
increment_indent();
|
||||||
|
|
||||||
|
for (const auto& s : statements) {
|
||||||
|
if (!EmitStatement(s.get())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
decrement_indent();
|
||||||
|
make_indent();
|
||||||
|
out_ << "}";
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool GeneratorImpl::EmitStatementBlockAndNewline(
|
||||||
|
const ast::StatementList& statements) {
|
||||||
|
const bool result = EmitStatementBlock(statements);
|
||||||
|
if (result) {
|
||||||
|
out_ << std::endl;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool GeneratorImpl::EmitStatement(ast::Statement* stmt) {
|
||||||
|
if (stmt->IsReturn()) {
|
||||||
|
return EmitReturn(stmt->AsReturn());
|
||||||
|
}
|
||||||
|
|
||||||
|
error_ = "unknown statement type";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
bool GeneratorImpl::EmitType(ast::type::Type* type, const std::string& name) {
|
bool GeneratorImpl::EmitType(ast::type::Type* type, const std::string& name) {
|
||||||
if (type->IsAlias()) {
|
if (type->IsAlias()) {
|
||||||
auto* alias = type->AsAlias();
|
auto* alias = type->AsAlias();
|
||||||
|
|
|
@ -37,6 +37,26 @@ class GeneratorImpl : public TextGenerator {
|
||||||
/// @returns true on successful generation; false otherwise
|
/// @returns true on successful generation; false otherwise
|
||||||
bool Generate(const ast::Module& module);
|
bool Generate(const ast::Module& module);
|
||||||
|
|
||||||
|
/// Handles generate an Expression
|
||||||
|
/// @param expr the expression
|
||||||
|
/// @returns true if the expression was emitted
|
||||||
|
bool EmitExpression(ast::Expression* expr);
|
||||||
|
/// Handles return statements
|
||||||
|
/// @param stmt the statement to emit
|
||||||
|
/// @returns true if the statement was successfully emitted
|
||||||
|
bool EmitReturn(ast::ReturnStatement* stmt);
|
||||||
|
/// Handles a brace-enclosed list of statements.
|
||||||
|
/// @param statements the statements to output
|
||||||
|
/// @returns true if the statements were emitted
|
||||||
|
bool EmitStatementBlock(const ast::StatementList& statements);
|
||||||
|
/// Handles a brace-enclosed list of statements and trailing newline.
|
||||||
|
/// @param statements the statements to output
|
||||||
|
/// @returns true if the statements were emitted
|
||||||
|
bool EmitStatementBlockAndNewline(const ast::StatementList& statements);
|
||||||
|
/// Handles statement
|
||||||
|
/// @param stmt the statement to emit
|
||||||
|
/// @returns true if the statement was emitted
|
||||||
|
bool EmitStatement(ast::Statement* stmt);
|
||||||
/// Handles generating type
|
/// Handles generating type
|
||||||
/// @param type the type to generate
|
/// @param type the type to generate
|
||||||
/// @param name the name of the variable, only used for array emission
|
/// @param name the name of the variable, only used for array emission
|
||||||
|
|
|
@ -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/msl/generator_impl.h"
|
||||||
|
|
||||||
|
namespace tint {
|
||||||
|
namespace writer {
|
||||||
|
namespace msl {
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
using MslGeneratorImplTest = testing::Test;
|
||||||
|
|
||||||
|
TEST_F(MslGeneratorImplTest, 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(MslGeneratorImplTest, DISABLED_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 msl
|
||||||
|
} // namespace writer
|
||||||
|
} // namespace tint
|
Loading…
Reference in New Issue