diff --git a/BUILD.gn b/BUILD.gn index b9b93bf127..0187d06a19 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -884,6 +884,7 @@ source_set("tint_unittests_wgsl_writer_src") { source_set("tint_unittests_msl_writer_src") { sources = [ + "src/writer/msl/generator_impl_alias_type_test.cc", "src/writer/msl/generator_impl_array_accessor_test.cc", "src/writer/msl/generator_impl_as_test.cc", "src/writer/msl/generator_impl_assign_test.cc", diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a08940d625..25adf24a26 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -492,6 +492,7 @@ endif() if(${TINT_BUILD_MSL_WRITER}) list(APPEND TINT_TEST_SRCS + writer/msl/generator_impl_alias_type_test.cc writer/msl/generator_impl_array_accessor_test.cc writer/msl/generator_impl_as_test.cc writer/msl/generator_impl_assign_test.cc diff --git a/src/writer/msl/generator_impl.cc b/src/writer/msl/generator_impl.cc index 6190f19871..9bb51c1242 100644 --- a/src/writer/msl/generator_impl.cc +++ b/src/writer/msl/generator_impl.cc @@ -52,6 +52,15 @@ GeneratorImpl::~GeneratorImpl() = default; bool GeneratorImpl::Generate(const ast::Module& module) { module_ = &module; + for (auto* const alias : module.alias_types()) { + if (!EmitAliasType(alias)) { + return false; + } + } + if (!module.alias_types().empty()) { + out_ << std::endl; + } + for (const auto& func : module.functions()) { if (!EmitFunction(func.get())) { return false; @@ -63,6 +72,17 @@ bool GeneratorImpl::Generate(const ast::Module& module) { return true; } +bool GeneratorImpl::EmitAliasType(const ast::type::AliasType* alias) { + make_indent(); + out_ << "typedef "; + if (!EmitType(alias->type(), "")) { + return false; + } + out_ << " " << alias->name() << ";" << std::endl; + + return true; +} + bool GeneratorImpl::EmitArrayAccessor(ast::ArrayAccessorExpression* expr) { if (!EmitExpression(expr->array())) { return false; @@ -531,7 +551,7 @@ bool GeneratorImpl::EmitType(ast::type::Type* type, const std::string& name) { } // Array member name will be output with the type if (!mem->type()->IsArray()) { - out_ << mem->name(); + out_ << " " << mem->name(); } out_ << ";" << std::endl; } diff --git a/src/writer/msl/generator_impl.h b/src/writer/msl/generator_impl.h index 7e93178294..12517a3199 100644 --- a/src/writer/msl/generator_impl.h +++ b/src/writer/msl/generator_impl.h @@ -40,6 +40,10 @@ class GeneratorImpl : public TextGenerator { /// @returns true on successful generation; false otherwise bool Generate(const ast::Module& module); + /// Handles generating an alias + /// @param alias the alias to generate + /// @returns true if the alias was emitted + bool EmitAliasType(const ast::type::AliasType* alias); /// Handles an array accessor expression /// @param expr the expression to emit /// @returns true if the array accessor was emitted diff --git a/src/writer/msl/generator_impl_alias_type_test.cc b/src/writer/msl/generator_impl_alias_type_test.cc new file mode 100644 index 0000000000..583924196a --- /dev/null +++ b/src/writer/msl/generator_impl_alias_type_test.cc @@ -0,0 +1,73 @@ +// 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/struct.h" +#include "src/ast/struct_member.h" +#include "src/ast/struct_member_decoration.h" +#include "src/ast/struct_member_offset_decoration.h" +#include "src/ast/type/f32_type.h" +#include "src/ast/type/i32_type.h" +#include "src/ast/type/struct_type.h" +#include "src/writer/msl/generator_impl.h" + +namespace tint { +namespace writer { +namespace msl { +namespace { + +using MslGeneratorImplTest = testing::Test; + +TEST_F(MslGeneratorImplTest, EmitAliasType_F32) { + ast::type::F32Type f32; + ast::type::AliasType alias("a", &f32); + + GeneratorImpl g; + ASSERT_TRUE(g.EmitAliasType(&alias)) << g.error(); + EXPECT_EQ(g.result(), R"(typedef float a; +)"); +} + +TEST_F(MslGeneratorImplTest, EmitAliasType_Struct) { + ast::type::I32Type i32; + ast::type::F32Type f32; + + ast::StructMemberList members; + members.push_back(std::make_unique( + "a", &f32, ast::StructMemberDecorationList{})); + + ast::StructMemberDecorationList b_deco; + b_deco.push_back(std::make_unique(4)); + members.push_back( + std::make_unique("b", &i32, std::move(b_deco))); + + auto str = std::make_unique(); + str->set_members(std::move(members)); + + ast::type::StructType s(std::move(str)); + ast::type::AliasType alias("a", &s); + + GeneratorImpl g; + ASSERT_TRUE(g.EmitAliasType(&alias)) << g.error(); + EXPECT_EQ(g.result(), R"(typedef struct { + float a; + int b; +} a; +)"); +} + +} // namespace +} // namespace msl +} // namespace writer +} // namespace tint