• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/bitcast_expression.h"
16 #include "src/ast/struct_block_decoration.h"
17 #include "src/resolver/resolver.h"
18 #include "src/resolver/resolver_test_helper.h"
19 #include "src/sem/reference_type.h"
20 
21 #include "gmock/gmock.h"
22 
23 namespace tint {
24 namespace resolver {
25 namespace {
26 
27 struct ResolverPtrRefValidationTest : public resolver::TestHelper,
28                                       public testing::Test {};
29 
TEST_F(ResolverPtrRefValidationTest,AddressOfLiteral)30 TEST_F(ResolverPtrRefValidationTest, AddressOfLiteral) {
31   // &1
32 
33   auto* expr = AddressOf(Expr(Source{{12, 34}}, 1));
34 
35   WrapInFunction(expr);
36 
37   EXPECT_FALSE(r()->Resolve());
38 
39   EXPECT_EQ(r()->error(), "12:34 error: cannot take the address of expression");
40 }
41 
TEST_F(ResolverPtrRefValidationTest,AddressOfLet)42 TEST_F(ResolverPtrRefValidationTest, AddressOfLet) {
43   // let l : i32 = 1;
44   // &l
45   auto* l = Const("l", ty.i32(), Expr(1));
46   auto* expr = AddressOf(Expr(Source{{12, 34}}, "l"));
47 
48   WrapInFunction(l, expr);
49 
50   EXPECT_FALSE(r()->Resolve());
51 
52   EXPECT_EQ(r()->error(), "12:34 error: cannot take the address of expression");
53 }
54 
TEST_F(ResolverPtrRefValidationTest,AddressOfHandle)55 TEST_F(ResolverPtrRefValidationTest, AddressOfHandle) {
56   // [[group(0), binding(0)]] var t: texture_3d<f32>;
57   // &t
58   Global("t", ty.sampled_texture(ast::TextureDimension::k3d, ty.f32()),
59          GroupAndBinding(0u, 0u));
60   auto* expr = AddressOf(Expr(Source{{12, 34}}, "t"));
61   WrapInFunction(expr);
62 
63   EXPECT_FALSE(r()->Resolve());
64   EXPECT_EQ(r()->error(),
65             "12:34 error: cannot take the address of expression in handle "
66             "storage class");
67 }
68 
TEST_F(ResolverPtrRefValidationTest,AddressOfVectorComponent_MemberAccessor)69 TEST_F(ResolverPtrRefValidationTest, AddressOfVectorComponent_MemberAccessor) {
70   // var v : vec4<i32>;
71   // &v.y
72   auto* v = Var("v", ty.vec4<i32>());
73   auto* expr = AddressOf(MemberAccessor(Source{{12, 34}}, "v", "y"));
74 
75   WrapInFunction(v, expr);
76 
77   EXPECT_FALSE(r()->Resolve());
78 
79   EXPECT_EQ(r()->error(),
80             "12:34 error: cannot take the address of a vector component");
81 }
82 
TEST_F(ResolverPtrRefValidationTest,AddressOfVectorComponent_IndexAccessor)83 TEST_F(ResolverPtrRefValidationTest, AddressOfVectorComponent_IndexAccessor) {
84   // var v : vec4<i32>;
85   // &v[2]
86   auto* v = Var("v", ty.vec4<i32>());
87   auto* expr = AddressOf(IndexAccessor(Source{{12, 34}}, "v", 2));
88 
89   WrapInFunction(v, expr);
90 
91   EXPECT_FALSE(r()->Resolve());
92 
93   EXPECT_EQ(r()->error(),
94             "12:34 error: cannot take the address of a vector component");
95 }
96 
TEST_F(ResolverPtrRefValidationTest,IndirectOfAddressOfHandle)97 TEST_F(ResolverPtrRefValidationTest, IndirectOfAddressOfHandle) {
98   // [[group(0), binding(0)]] var t: texture_3d<f32>;
99   // *&t
100   Global("t", ty.sampled_texture(ast::TextureDimension::k3d, ty.f32()),
101          GroupAndBinding(0u, 0u));
102   auto* expr = Deref(AddressOf(Expr(Source{{12, 34}}, "t")));
103   WrapInFunction(expr);
104 
105   EXPECT_FALSE(r()->Resolve());
106   EXPECT_EQ(r()->error(),
107             "12:34 error: cannot take the address of expression in handle "
108             "storage class");
109 }
110 
TEST_F(ResolverPtrRefValidationTest,DerefOfLiteral)111 TEST_F(ResolverPtrRefValidationTest, DerefOfLiteral) {
112   // *1
113 
114   auto* expr = Deref(Expr(Source{{12, 34}}, 1));
115 
116   WrapInFunction(expr);
117 
118   EXPECT_FALSE(r()->Resolve());
119 
120   EXPECT_EQ(r()->error(),
121             "12:34 error: cannot dereference expression of type 'i32'");
122 }
123 
TEST_F(ResolverPtrRefValidationTest,DerefOfVar)124 TEST_F(ResolverPtrRefValidationTest, DerefOfVar) {
125   // var v : i32 = 1;
126   // *1
127   auto* v = Var("v", ty.i32());
128   auto* expr = Deref(Expr(Source{{12, 34}}, "v"));
129 
130   WrapInFunction(v, expr);
131 
132   EXPECT_FALSE(r()->Resolve());
133 
134   EXPECT_EQ(r()->error(),
135             "12:34 error: cannot dereference expression of type 'i32'");
136 }
137 
TEST_F(ResolverPtrRefValidationTest,InferredPtrAccessMismatch)138 TEST_F(ResolverPtrRefValidationTest, InferredPtrAccessMismatch) {
139   // struct Inner {
140   //    arr: array<i32, 4>;
141   // }
142   // [[block]] struct S {
143   //    inner: Inner;
144   // }
145   // [[group(0), binding(0)]] var<storage, read_write> s : S;
146   // fn f() {
147   //   let p : pointer<storage, i32> = &s.inner.arr[2];
148   // }
149   auto* inner = Structure("Inner", {Member("arr", ty.array<i32, 4>())});
150   auto* buf = Structure("S", {Member("inner", ty.Of(inner))},
151                         {create<ast::StructBlockDecoration>()});
152   auto* storage = Global("s", ty.Of(buf), ast::StorageClass::kStorage,
153                          ast::Access::kReadWrite,
154                          ast::DecorationList{
155                              create<ast::BindingDecoration>(0),
156                              create<ast::GroupDecoration>(0),
157                          });
158 
159   auto* expr =
160       IndexAccessor(MemberAccessor(MemberAccessor(storage, "inner"), "arr"), 4);
161   auto* ptr =
162       Const(Source{{12, 34}}, "p", ty.pointer<i32>(ast::StorageClass::kStorage),
163             AddressOf(expr));
164 
165   WrapInFunction(ptr);
166 
167   EXPECT_FALSE(r()->Resolve());
168   EXPECT_EQ(r()->error(),
169             "12:34 error: cannot initialize let of type "
170             "'ptr<storage, i32, read>' with value of type "
171             "'ptr<storage, i32, read_write>'");
172 }
173 
174 }  // namespace
175 }  // namespace resolver
176 }  // namespace tint
177