[msl-writer] Emit alias types.
This CL adds emission of alias types to the MSL writer. They are output as `typedef`s. Bug: tint:8 Change-Id: I18b839ed0238c4636e3975f35f5d88badd412fe4 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/24001 Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
parent
dc841f1bf2
commit
d4eafb631d
1
BUILD.gn
1
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",
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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<ast::StructMember>(
|
||||
"a", &f32, ast::StructMemberDecorationList{}));
|
||||
|
||||
ast::StructMemberDecorationList b_deco;
|
||||
b_deco.push_back(std::make_unique<ast::StructMemberOffsetDecoration>(4));
|
||||
members.push_back(
|
||||
std::make_unique<ast::StructMember>("b", &i32, std::move(b_deco)));
|
||||
|
||||
auto str = std::make_unique<ast::Struct>();
|
||||
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
|
Loading…
Reference in New Issue