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/ast/struct_block_decoration.h"
16 #include "src/resolver/resolver.h"
17 #include "src/resolver/resolver_test_helper.h"
18 #include "src/sem/reference_type.h"
19
20 #include "gmock/gmock.h"
21
22 namespace tint {
23 namespace resolver {
24 namespace {
25
26 struct ResolverPtrRefTest : public resolver::TestHelper,
27 public testing::Test {};
28
TEST_F(ResolverPtrRefTest,AddressOf)29 TEST_F(ResolverPtrRefTest, AddressOf) {
30 // var v : i32;
31 // &v
32
33 auto* v = Var("v", ty.i32(), ast::StorageClass::kNone);
34 auto* expr = AddressOf(v);
35
36 WrapInFunction(v, expr);
37
38 EXPECT_TRUE(r()->Resolve()) << r()->error();
39
40 ASSERT_TRUE(TypeOf(expr)->Is<sem::Pointer>());
41 EXPECT_TRUE(TypeOf(expr)->As<sem::Pointer>()->StoreType()->Is<sem::I32>());
42 EXPECT_EQ(TypeOf(expr)->As<sem::Pointer>()->StorageClass(),
43 ast::StorageClass::kFunction);
44 }
45
TEST_F(ResolverPtrRefTest,AddressOfThenDeref)46 TEST_F(ResolverPtrRefTest, AddressOfThenDeref) {
47 // var v : i32;
48 // *(&v)
49
50 auto* v = Var("v", ty.i32(), ast::StorageClass::kNone);
51 auto* expr = Deref(AddressOf(v));
52
53 WrapInFunction(v, expr);
54
55 EXPECT_TRUE(r()->Resolve()) << r()->error();
56
57 ASSERT_TRUE(TypeOf(expr)->Is<sem::Reference>());
58 EXPECT_TRUE(TypeOf(expr)->As<sem::Reference>()->StoreType()->Is<sem::I32>());
59 }
60
TEST_F(ResolverPtrRefTest,DefaultPtrStorageClass)61 TEST_F(ResolverPtrRefTest, DefaultPtrStorageClass) {
62 // https://gpuweb.github.io/gpuweb/wgsl/#storage-class
63
64 auto* buf = Structure("S", {Member("m", ty.i32())},
65 {create<ast::StructBlockDecoration>()});
66 auto* function = Var("f", ty.i32());
67 auto* private_ = Global("p", ty.i32(), ast::StorageClass::kPrivate);
68 auto* workgroup = Global("w", ty.i32(), ast::StorageClass::kWorkgroup);
69 auto* uniform = Global("ub", ty.Of(buf), ast::StorageClass::kUniform,
70 ast::DecorationList{
71 create<ast::BindingDecoration>(0),
72 create<ast::GroupDecoration>(0),
73 });
74 auto* storage = Global("sb", ty.Of(buf), ast::StorageClass::kStorage,
75 ast::DecorationList{
76 create<ast::BindingDecoration>(1),
77 create<ast::GroupDecoration>(0),
78 });
79
80 auto* function_ptr =
81 Const("f_ptr", ty.pointer(ty.i32(), ast::StorageClass::kFunction),
82 AddressOf(function));
83 auto* private_ptr =
84 Const("p_ptr", ty.pointer(ty.i32(), ast::StorageClass::kPrivate),
85 AddressOf(private_));
86 auto* workgroup_ptr =
87 Const("w_ptr", ty.pointer(ty.i32(), ast::StorageClass::kWorkgroup),
88 AddressOf(workgroup));
89 auto* uniform_ptr =
90 Const("ub_ptr", ty.pointer(ty.Of(buf), ast::StorageClass::kUniform),
91 AddressOf(uniform));
92 auto* storage_ptr =
93 Const("sb_ptr", ty.pointer(ty.Of(buf), ast::StorageClass::kStorage),
94 AddressOf(storage));
95
96 WrapInFunction(function, function_ptr, private_ptr, workgroup_ptr,
97 uniform_ptr, storage_ptr);
98
99 EXPECT_TRUE(r()->Resolve()) << r()->error();
100
101 ASSERT_TRUE(TypeOf(function_ptr)->Is<sem::Pointer>())
102 << "function_ptr is " << TypeOf(function_ptr)->TypeInfo().name;
103 ASSERT_TRUE(TypeOf(private_ptr)->Is<sem::Pointer>())
104 << "private_ptr is " << TypeOf(private_ptr)->TypeInfo().name;
105 ASSERT_TRUE(TypeOf(workgroup_ptr)->Is<sem::Pointer>())
106 << "workgroup_ptr is " << TypeOf(workgroup_ptr)->TypeInfo().name;
107 ASSERT_TRUE(TypeOf(uniform_ptr)->Is<sem::Pointer>())
108 << "uniform_ptr is " << TypeOf(uniform_ptr)->TypeInfo().name;
109 ASSERT_TRUE(TypeOf(storage_ptr)->Is<sem::Pointer>())
110 << "storage_ptr is " << TypeOf(storage_ptr)->TypeInfo().name;
111
112 EXPECT_EQ(TypeOf(function_ptr)->As<sem::Pointer>()->Access(),
113 ast::Access::kReadWrite);
114 EXPECT_EQ(TypeOf(private_ptr)->As<sem::Pointer>()->Access(),
115 ast::Access::kReadWrite);
116 EXPECT_EQ(TypeOf(workgroup_ptr)->As<sem::Pointer>()->Access(),
117 ast::Access::kReadWrite);
118 EXPECT_EQ(TypeOf(uniform_ptr)->As<sem::Pointer>()->Access(),
119 ast::Access::kRead);
120 EXPECT_EQ(TypeOf(storage_ptr)->As<sem::Pointer>()->Access(),
121 ast::Access::kRead);
122 }
123
124 } // namespace
125 } // namespace resolver
126 } // namespace tint
127