diff --git a/BUILD.gn b/BUILD.gn index e9439511c6..ad647f4da7 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -885,6 +885,7 @@ source_set("tint_unittests_wgsl_writer_src") { source_set("tint_unittests_msl_writer_src") { sources = [ + "src/writer/msl/generator_impl_return_test.cc", "src/writer/msl/generator_impl_test.cc", "src/writer/msl/generator_impl_type_test.cc", ] diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 424a4c3e07..b321263fc0 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -493,6 +493,7 @@ endif() if(${TINT_BUILD_MSL_WRITER}) list(APPEND TINT_TEST_SRCS + writer/msl/generator_impl_return_test.cc writer/msl/generator_impl_test.cc writer/msl/generator_impl_type_test.cc ) diff --git a/src/writer/msl/generator_impl.cc b/src/writer/msl/generator_impl.cc index cb2a173e0c..88e93f274e 100644 --- a/src/writer/msl/generator_impl.cc +++ b/src/writer/msl/generator_impl.cc @@ -14,6 +14,7 @@ #include "src/writer/msl/generator_impl.h" +#include "src/ast/return_statement.h" #include "src/ast/type/alias_type.h" #include "src/ast/type/array_type.h" #include "src/ast/type/bool_type.h" @@ -38,6 +39,61 @@ bool GeneratorImpl::Generate(const ast::Module&) { 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) { if (type->IsAlias()) { auto* alias = type->AsAlias(); diff --git a/src/writer/msl/generator_impl.h b/src/writer/msl/generator_impl.h index befa305118..6fe8a4670b 100644 --- a/src/writer/msl/generator_impl.h +++ b/src/writer/msl/generator_impl.h @@ -37,6 +37,26 @@ class GeneratorImpl : public TextGenerator { /// @returns true on successful generation; false otherwise 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 /// @param type the type to generate /// @param name the name of the variable, only used for array emission diff --git a/src/writer/msl/generator_impl_return_test.cc b/src/writer/msl/generator_impl_return_test.cc new file mode 100644 index 0000000000..296ba0b731 --- /dev/null +++ b/src/writer/msl/generator_impl_return_test.cc @@ -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 +#include + +#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("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