diff --git a/BUILD.gn b/BUILD.gn index 994aac72e6..77f2c59e4b 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_function_test.cc", "src/writer/msl/generator_impl_identifier_test.cc", "src/writer/msl/generator_impl_return_test.cc", "src/writer/msl/generator_impl_test.cc", diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 317b523e72..ba497fc651 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_function_test.cc writer/msl/generator_impl_identifier_test.cc writer/msl/generator_impl_return_test.cc writer/msl/generator_impl_test.cc diff --git a/src/writer/msl/generator_impl.cc b/src/writer/msl/generator_impl.cc index 46f123cd29..a8caafbc5e 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/function.h" #include "src/ast/identifier_expression.h" #include "src/ast/return_statement.h" #include "src/ast/type/alias_type.h" @@ -36,7 +37,13 @@ GeneratorImpl::GeneratorImpl() = default; GeneratorImpl::~GeneratorImpl() = default; -bool GeneratorImpl::Generate(const ast::Module&) { +bool GeneratorImpl::Generate(const ast::Module& module) { + for (const auto& func : module.functions()) { + if (!EmitFunction(func.get())) { + return false; + } + out_ << std::endl; + } return true; } @@ -49,6 +56,35 @@ bool GeneratorImpl::EmitExpression(ast::Expression* expr) { return false; } +bool GeneratorImpl::EmitFunction(ast::Function* func) { + make_indent(); + + if (!EmitType(func->return_type(), "")) { + return false; + } + + out_ << " " << func->name() << "("; + + bool first = true; + for (const auto& v : func->params()) { + if (!first) { + out_ << ", "; + } + first = false; + + if (!EmitType(v->type(), v->name())) { + return false; + } + // Array name is output as part of the type + if (!v->type()->IsArray()) { + out_ << " " << v->name(); + } + } + out_ << ")"; + + return EmitStatementBlockAndNewline(func->body()); +} + bool GeneratorImpl::EmitIdentifier(ast::IdentifierExpression* expr) { auto* ident = expr->AsIdentifier(); if (ident->has_path()) { diff --git a/src/writer/msl/generator_impl.h b/src/writer/msl/generator_impl.h index 4157dd4906..e91b0c5c21 100644 --- a/src/writer/msl/generator_impl.h +++ b/src/writer/msl/generator_impl.h @@ -41,6 +41,10 @@ class GeneratorImpl : public TextGenerator { /// @param expr the expression /// @returns true if the expression was emitted bool EmitExpression(ast::Expression* expr); + /// Handles generating a function + /// @param func the function to generate + /// @returns true if the function was emitted + bool EmitFunction(ast::Function* func); /// Handles generating an identifier expression /// @param expr the identifier expression /// @returns true if the identifeir was emitted diff --git a/src/writer/msl/generator_impl_function_test.cc b/src/writer/msl/generator_impl_function_test.cc new file mode 100644 index 0000000000..d47864ebde --- /dev/null +++ b/src/writer/msl/generator_impl_function_test.cc @@ -0,0 +1,111 @@ +// 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 "gtest/gtest.h" +#include "src/ast/function.h" +#include "src/ast/return_statement.h" +#include "src/ast/type/array_type.h" +#include "src/ast/type/f32_type.h" +#include "src/ast/type/i32_type.h" +#include "src/ast/type/void_type.h" +#include "src/ast/variable.h" +#include "src/writer/msl/generator_impl.h" + +namespace tint { +namespace writer { +namespace msl { +namespace { + +using MslGeneratorImplTest = testing::Test; + +TEST_F(MslGeneratorImplTest, Emit_Function) { + ast::type::VoidType void_type; + + ast::Function func("my_func", {}, &void_type); + + ast::StatementList body; + body.push_back(std::make_unique()); + func.set_body(std::move(body)); + + GeneratorImpl g; + g.increment_indent(); + + ASSERT_TRUE(g.EmitFunction(&func)); + EXPECT_EQ(g.result(), R"( void my_func() { + return; + } +)"); +} + +TEST_F(MslGeneratorImplTest, Emit_Function_WithParams) { + ast::type::F32Type f32; + ast::type::I32Type i32; + + ast::VariableList params; + params.push_back( + std::make_unique("a", ast::StorageClass::kNone, &f32)); + params.push_back( + std::make_unique("b", ast::StorageClass::kNone, &i32)); + + ast::type::VoidType void_type; + ast::Function func("my_func", std::move(params), &void_type); + + ast::StatementList body; + body.push_back(std::make_unique()); + func.set_body(std::move(body)); + + GeneratorImpl g; + g.increment_indent(); + + ASSERT_TRUE(g.EmitFunction(&func)); + EXPECT_EQ(g.result(), R"( void my_func(float a, int b) { + return; + } +)"); +} + +TEST_F(MslGeneratorImplTest, Emit_Function_WithArrayParams) { + ast::type::F32Type f32; + ast::type::ArrayType ary(&f32, 5); + + ast::VariableList params; + params.push_back( + std::make_unique("a", ast::StorageClass::kNone, &ary)); + + ast::type::VoidType void_type; + auto func = + std::make_unique("my_func", std::move(params), &void_type); + + ast::StatementList body; + body.push_back(std::make_unique()); + func->set_body(std::move(body)); + + ast::Module m; + m.AddFunction(std::move(func)); + + GeneratorImpl g; + g.increment_indent(); + + ASSERT_TRUE(g.Generate(m)) << g.error(); + EXPECT_EQ(g.result(), R"( void my_func(float a[5]) { + return; + } + +)"); +} + +} // namespace +} // namespace msl +} // namespace writer +} // namespace tint