2021-03-17 22:47:33 +00:00
|
|
|
// Copyright 2021 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 "src/resolver/resolver.h"
|
|
|
|
|
|
|
|
#include "gmock/gmock.h"
|
2021-04-19 20:20:33 +00:00
|
|
|
#include "src/ast/struct_block_decoration.h"
|
2021-03-17 22:47:33 +00:00
|
|
|
#include "src/resolver/resolver_test_helper.h"
|
2021-04-16 19:07:51 +00:00
|
|
|
#include "src/sem/struct.h"
|
2021-03-17 22:47:33 +00:00
|
|
|
|
|
|
|
using ::testing::UnorderedElementsAre;
|
|
|
|
|
|
|
|
namespace tint {
|
|
|
|
namespace resolver {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
using ResolverStorageClassUseTest = ResolverTest;
|
|
|
|
|
|
|
|
TEST_F(ResolverStorageClassUseTest, UnreachableStruct) {
|
2021-05-07 14:49:34 +00:00
|
|
|
auto* s = Structure("S", {Member("a", ty.f32())});
|
2021-03-17 22:47:33 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(r()->Resolve()) << r()->error();
|
|
|
|
|
2021-05-07 14:49:34 +00:00
|
|
|
auto* sem = TypeOf(s)->As<sem::Struct>();
|
2021-03-17 22:47:33 +00:00
|
|
|
ASSERT_NE(sem, nullptr);
|
|
|
|
EXPECT_TRUE(sem->StorageClassUsage().empty());
|
|
|
|
}
|
|
|
|
|
2021-03-30 20:06:48 +00:00
|
|
|
TEST_F(ResolverStorageClassUseTest, StructReachableFromParameter) {
|
2021-05-07 14:49:34 +00:00
|
|
|
auto* s = Structure("S", {Member("a", ty.f32())});
|
2021-03-30 20:06:48 +00:00
|
|
|
|
2021-04-08 22:15:48 +00:00
|
|
|
Func("f", {Param("param", s)}, ty.void_(), {}, {});
|
2021-03-30 20:06:48 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(r()->Resolve()) << r()->error();
|
|
|
|
|
2021-05-07 14:49:34 +00:00
|
|
|
auto* sem = TypeOf(s)->As<sem::Struct>();
|
2021-03-31 15:44:07 +00:00
|
|
|
ASSERT_NE(sem, nullptr);
|
|
|
|
EXPECT_THAT(sem->StorageClassUsage(),
|
|
|
|
UnorderedElementsAre(ast::StorageClass::kNone));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ResolverStorageClassUseTest, StructReachableFromReturnType) {
|
2021-05-07 14:49:34 +00:00
|
|
|
auto* s = Structure("S", {Member("a", ty.f32())});
|
2021-03-31 15:44:07 +00:00
|
|
|
|
|
|
|
Func("f", {}, s, {Return(Construct(s))}, {});
|
|
|
|
|
|
|
|
ASSERT_TRUE(r()->Resolve()) << r()->error();
|
|
|
|
|
2021-05-07 14:49:34 +00:00
|
|
|
auto* sem = TypeOf(s)->As<sem::Struct>();
|
2021-03-30 20:06:48 +00:00
|
|
|
ASSERT_NE(sem, nullptr);
|
|
|
|
EXPECT_THAT(sem->StorageClassUsage(),
|
|
|
|
UnorderedElementsAre(ast::StorageClass::kNone));
|
|
|
|
}
|
|
|
|
|
2021-03-17 22:47:33 +00:00
|
|
|
TEST_F(ResolverStorageClassUseTest, StructReachableFromGlobal) {
|
2021-05-07 14:49:34 +00:00
|
|
|
auto* s = Structure("S", {Member("a", ty.f32())});
|
2021-03-17 22:47:33 +00:00
|
|
|
|
2021-04-21 17:55:12 +00:00
|
|
|
Global("g", s, ast::StorageClass::kPrivate);
|
2021-03-17 22:47:33 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(r()->Resolve()) << r()->error();
|
|
|
|
|
2021-05-07 14:49:34 +00:00
|
|
|
auto* sem = TypeOf(s)->As<sem::Struct>();
|
2021-03-17 22:47:33 +00:00
|
|
|
ASSERT_NE(sem, nullptr);
|
|
|
|
EXPECT_THAT(sem->StorageClassUsage(),
|
2021-04-21 17:55:12 +00:00
|
|
|
UnorderedElementsAre(ast::StorageClass::kPrivate));
|
2021-03-17 22:47:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ResolverStorageClassUseTest, StructReachableViaGlobalAlias) {
|
2021-05-07 14:49:34 +00:00
|
|
|
auto* s = Structure("S", {Member("a", ty.f32())});
|
2021-06-09 14:32:14 +00:00
|
|
|
auto* a = Alias("A", s);
|
2021-04-21 17:55:12 +00:00
|
|
|
Global("g", a, ast::StorageClass::kPrivate);
|
2021-03-17 22:47:33 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(r()->Resolve()) << r()->error();
|
|
|
|
|
2021-05-07 14:49:34 +00:00
|
|
|
auto* sem = TypeOf(s)->As<sem::Struct>();
|
2021-03-17 22:47:33 +00:00
|
|
|
ASSERT_NE(sem, nullptr);
|
|
|
|
EXPECT_THAT(sem->StorageClassUsage(),
|
2021-04-21 17:55:12 +00:00
|
|
|
UnorderedElementsAre(ast::StorageClass::kPrivate));
|
2021-03-17 22:47:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ResolverStorageClassUseTest, StructReachableViaGlobalStruct) {
|
2021-05-07 14:49:34 +00:00
|
|
|
auto* s = Structure("S", {Member("a", ty.f32())});
|
|
|
|
auto* o = Structure("O", {Member("a", s)});
|
2021-04-21 17:55:12 +00:00
|
|
|
Global("g", o, ast::StorageClass::kPrivate);
|
2021-03-17 22:47:33 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(r()->Resolve()) << r()->error();
|
|
|
|
|
2021-05-07 14:49:34 +00:00
|
|
|
auto* sem = TypeOf(s)->As<sem::Struct>();
|
2021-03-17 22:47:33 +00:00
|
|
|
ASSERT_NE(sem, nullptr);
|
|
|
|
EXPECT_THAT(sem->StorageClassUsage(),
|
2021-04-21 17:55:12 +00:00
|
|
|
UnorderedElementsAre(ast::StorageClass::kPrivate));
|
2021-03-17 22:47:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ResolverStorageClassUseTest, StructReachableViaGlobalArray) {
|
2021-05-07 14:49:34 +00:00
|
|
|
auto* s = Structure("S", {Member("a", ty.f32())});
|
2021-05-07 20:58:34 +00:00
|
|
|
auto* a = ty.array(s, 3);
|
2021-04-21 17:55:12 +00:00
|
|
|
Global("g", a, ast::StorageClass::kPrivate);
|
2021-03-17 22:47:33 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(r()->Resolve()) << r()->error();
|
|
|
|
|
2021-05-07 14:49:34 +00:00
|
|
|
auto* sem = TypeOf(s)->As<sem::Struct>();
|
2021-03-17 22:47:33 +00:00
|
|
|
ASSERT_NE(sem, nullptr);
|
|
|
|
EXPECT_THAT(sem->StorageClassUsage(),
|
2021-04-21 17:55:12 +00:00
|
|
|
UnorderedElementsAre(ast::StorageClass::kPrivate));
|
2021-03-17 22:47:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ResolverStorageClassUseTest, StructReachableFromLocal) {
|
2021-05-07 14:49:34 +00:00
|
|
|
auto* s = Structure("S", {Member("a", ty.f32())});
|
2021-03-17 22:47:33 +00:00
|
|
|
|
2021-05-12 21:08:22 +00:00
|
|
|
WrapInFunction(Var("g", s));
|
2021-03-17 22:47:33 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(r()->Resolve()) << r()->error();
|
|
|
|
|
2021-05-07 14:49:34 +00:00
|
|
|
auto* sem = TypeOf(s)->As<sem::Struct>();
|
2021-03-17 22:47:33 +00:00
|
|
|
ASSERT_NE(sem, nullptr);
|
|
|
|
EXPECT_THAT(sem->StorageClassUsage(),
|
|
|
|
UnorderedElementsAre(ast::StorageClass::kFunction));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ResolverStorageClassUseTest, StructReachableViaLocalAlias) {
|
2021-05-07 14:49:34 +00:00
|
|
|
auto* s = Structure("S", {Member("a", ty.f32())});
|
2021-06-09 14:32:14 +00:00
|
|
|
auto* a = Alias("A", s);
|
2021-05-12 21:08:22 +00:00
|
|
|
WrapInFunction(Var("g", a));
|
2021-03-17 22:47:33 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(r()->Resolve()) << r()->error();
|
|
|
|
|
2021-05-07 14:49:34 +00:00
|
|
|
auto* sem = TypeOf(s)->As<sem::Struct>();
|
2021-03-17 22:47:33 +00:00
|
|
|
ASSERT_NE(sem, nullptr);
|
|
|
|
EXPECT_THAT(sem->StorageClassUsage(),
|
|
|
|
UnorderedElementsAre(ast::StorageClass::kFunction));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ResolverStorageClassUseTest, StructReachableViaLocalStruct) {
|
2021-05-07 14:49:34 +00:00
|
|
|
auto* s = Structure("S", {Member("a", ty.f32())});
|
|
|
|
auto* o = Structure("O", {Member("a", s)});
|
2021-05-12 21:08:22 +00:00
|
|
|
WrapInFunction(Var("g", o));
|
2021-03-17 22:47:33 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(r()->Resolve()) << r()->error();
|
|
|
|
|
2021-05-07 14:49:34 +00:00
|
|
|
auto* sem = TypeOf(s)->As<sem::Struct>();
|
2021-03-17 22:47:33 +00:00
|
|
|
ASSERT_NE(sem, nullptr);
|
|
|
|
EXPECT_THAT(sem->StorageClassUsage(),
|
|
|
|
UnorderedElementsAre(ast::StorageClass::kFunction));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ResolverStorageClassUseTest, StructReachableViaLocalArray) {
|
2021-05-07 14:49:34 +00:00
|
|
|
auto* s = Structure("S", {Member("a", ty.f32())});
|
2021-05-07 20:58:34 +00:00
|
|
|
auto* a = ty.array(s, 3);
|
2021-05-12 21:08:22 +00:00
|
|
|
WrapInFunction(Var("g", a));
|
2021-03-17 22:47:33 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(r()->Resolve()) << r()->error();
|
|
|
|
|
2021-05-07 14:49:34 +00:00
|
|
|
auto* sem = TypeOf(s)->As<sem::Struct>();
|
2021-03-17 22:47:33 +00:00
|
|
|
ASSERT_NE(sem, nullptr);
|
|
|
|
EXPECT_THAT(sem->StorageClassUsage(),
|
|
|
|
UnorderedElementsAre(ast::StorageClass::kFunction));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ResolverStorageClassUseTest, StructMultipleStorageClassUses) {
|
2021-05-07 14:49:34 +00:00
|
|
|
auto* s = Structure("S", {Member("a", ty.f32())},
|
|
|
|
{create<ast::StructBlockDecoration>()});
|
2021-06-04 19:55:08 +00:00
|
|
|
Global("x", s, ast::StorageClass::kUniform,
|
|
|
|
ast::DecorationList{
|
2021-05-10 19:16:46 +00:00
|
|
|
create<ast::BindingDecoration>(0),
|
|
|
|
create<ast::GroupDecoration>(0),
|
|
|
|
});
|
2021-06-04 20:41:47 +00:00
|
|
|
Global("y", s, ast::StorageClass::kStorage, ast::Access::kRead,
|
2021-06-04 19:55:08 +00:00
|
|
|
ast::DecorationList{
|
2021-05-10 19:16:46 +00:00
|
|
|
create<ast::BindingDecoration>(1),
|
|
|
|
create<ast::GroupDecoration>(0),
|
|
|
|
});
|
2021-05-12 21:08:22 +00:00
|
|
|
WrapInFunction(Var("g", s));
|
2021-03-17 22:47:33 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(r()->Resolve()) << r()->error();
|
|
|
|
|
2021-05-07 14:49:34 +00:00
|
|
|
auto* sem = TypeOf(s)->As<sem::Struct>();
|
2021-03-17 22:47:33 +00:00
|
|
|
ASSERT_NE(sem, nullptr);
|
|
|
|
EXPECT_THAT(sem->StorageClassUsage(),
|
2021-04-16 09:26:14 +00:00
|
|
|
UnorderedElementsAre(ast::StorageClass::kUniform,
|
|
|
|
ast::StorageClass::kStorage,
|
2021-03-17 22:47:33 +00:00
|
|
|
ast::StorageClass::kFunction));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
} // namespace resolver
|
|
|
|
} // namespace tint
|