1 // Copyright 2021 The Tint Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "src/transform/transform.h"
16 #include "src/clone_context.h"
17 #include "src/program_builder.h"
18
19 #include "gtest/gtest.h"
20
21 namespace tint {
22 namespace transform {
23 namespace {
24
25 // Inherit from Transform so we have access to protected methods
26 struct CreateASTTypeForTest : public testing::Test, public Transform {
Runtint::transform::__anonf695a9970111::CreateASTTypeForTest27 Output Run(const Program*, const DataMap&) override { return {}; }
28
createtint::transform::__anonf695a9970111::CreateASTTypeForTest29 const ast::Type* create(
30 std::function<sem::Type*(ProgramBuilder&)> create_sem_type) {
31 ProgramBuilder sem_type_builder;
32 auto* sem_type = create_sem_type(sem_type_builder);
33 Program program(std::move(sem_type_builder));
34 CloneContext ctx(&ast_type_builder, &program, false);
35 return CreateASTTypeFor(ctx, sem_type);
36 }
37
38 ProgramBuilder ast_type_builder;
39 };
40
TEST_F(CreateASTTypeForTest,Basic)41 TEST_F(CreateASTTypeForTest, Basic) {
42 EXPECT_TRUE(create([](ProgramBuilder& b) {
43 return b.create<sem::I32>();
44 })->Is<ast::I32>());
45 EXPECT_TRUE(create([](ProgramBuilder& b) {
46 return b.create<sem::U32>();
47 })->Is<ast::U32>());
48 EXPECT_TRUE(create([](ProgramBuilder& b) {
49 return b.create<sem::F32>();
50 })->Is<ast::F32>());
51 EXPECT_TRUE(create([](ProgramBuilder& b) {
52 return b.create<sem::Bool>();
53 })->Is<ast::Bool>());
54 EXPECT_TRUE(create([](ProgramBuilder& b) {
55 return b.create<sem::Void>();
56 })->Is<ast::Void>());
57 }
58
TEST_F(CreateASTTypeForTest,Matrix)59 TEST_F(CreateASTTypeForTest, Matrix) {
60 auto* mat = create([](ProgramBuilder& b) {
61 auto* column_type = b.create<sem::Vector>(b.create<sem::F32>(), 2u);
62 return b.create<sem::Matrix>(column_type, 3u);
63 });
64 ASSERT_TRUE(mat->Is<ast::Matrix>());
65 ASSERT_TRUE(mat->As<ast::Matrix>()->type->Is<ast::F32>());
66 ASSERT_EQ(mat->As<ast::Matrix>()->columns, 3u);
67 ASSERT_EQ(mat->As<ast::Matrix>()->rows, 2u);
68 }
69
TEST_F(CreateASTTypeForTest,Vector)70 TEST_F(CreateASTTypeForTest, Vector) {
71 auto* vec = create([](ProgramBuilder& b) {
72 return b.create<sem::Vector>(b.create<sem::F32>(), 2);
73 });
74 ASSERT_TRUE(vec->Is<ast::Vector>());
75 ASSERT_TRUE(vec->As<ast::Vector>()->type->Is<ast::F32>());
76 ASSERT_EQ(vec->As<ast::Vector>()->width, 2u);
77 }
78
TEST_F(CreateASTTypeForTest,ArrayImplicitStride)79 TEST_F(CreateASTTypeForTest, ArrayImplicitStride) {
80 auto* arr = create([](ProgramBuilder& b) {
81 return b.create<sem::Array>(b.create<sem::F32>(), 2, 4, 4, 32u, 32u);
82 });
83 ASSERT_TRUE(arr->Is<ast::Array>());
84 ASSERT_TRUE(arr->As<ast::Array>()->type->Is<ast::F32>());
85 ASSERT_EQ(arr->As<ast::Array>()->decorations.size(), 0u);
86
87 auto* size = arr->As<ast::Array>()->count->As<ast::IntLiteralExpression>();
88 ASSERT_NE(size, nullptr);
89 EXPECT_EQ(size->ValueAsI32(), 2);
90 }
91
TEST_F(CreateASTTypeForTest,ArrayNonImplicitStride)92 TEST_F(CreateASTTypeForTest, ArrayNonImplicitStride) {
93 auto* arr = create([](ProgramBuilder& b) {
94 return b.create<sem::Array>(b.create<sem::F32>(), 2, 4, 4, 64u, 32u);
95 });
96 ASSERT_TRUE(arr->Is<ast::Array>());
97 ASSERT_TRUE(arr->As<ast::Array>()->type->Is<ast::F32>());
98 ASSERT_EQ(arr->As<ast::Array>()->decorations.size(), 1u);
99 ASSERT_TRUE(
100 arr->As<ast::Array>()->decorations[0]->Is<ast::StrideDecoration>());
101 ASSERT_EQ(arr->As<ast::Array>()
102 ->decorations[0]
103 ->As<ast::StrideDecoration>()
104 ->stride,
105 64u);
106
107 auto* size = arr->As<ast::Array>()->count->As<ast::IntLiteralExpression>();
108 ASSERT_NE(size, nullptr);
109 EXPECT_EQ(size->ValueAsI32(), 2);
110 }
111
TEST_F(CreateASTTypeForTest,Struct)112 TEST_F(CreateASTTypeForTest, Struct) {
113 auto* str = create([](ProgramBuilder& b) {
114 auto* decl = b.Structure("S", {}, {});
115 return b.create<sem::Struct>(decl, decl->name, sem::StructMemberList{},
116 4 /* align */, 4 /* size */,
117 4 /* size_no_padding */);
118 });
119 ASSERT_TRUE(str->Is<ast::TypeName>());
120 EXPECT_EQ(ast_type_builder.Symbols().NameFor(str->As<ast::TypeName>()->name),
121 "S");
122 }
123
124 } // namespace
125 } // namespace transform
126 } // namespace tint
127