diff --git a/src/tint/BUILD.gn b/src/tint/BUILD.gn index be99389582..6bf1aeaa7b 100644 --- a/src/tint/BUILD.gn +++ b/src/tint/BUILD.gn @@ -428,6 +428,7 @@ libtint_source_set("libtint_core_all_src") { "sem/for_loop_statement.h", "sem/i32.h", "sem/if_statement.h", + "sem/index_accessor_expression.h", "sem/info.h", "sem/loop_statement.h", "sem/materialize.h", @@ -634,6 +635,8 @@ libtint_source_set("libtint_sem_src") { "sem/i32.h", "sem/if_statement.cc", "sem/if_statement.h", + "sem/index_accessor_expression.cc", + "sem/index_accessor_expression.h", "sem/info.cc", "sem/info.h", "sem/loop_statement.cc", diff --git a/src/tint/CMakeLists.txt b/src/tint/CMakeLists.txt index 10d81d6197..fee559274f 100644 --- a/src/tint/CMakeLists.txt +++ b/src/tint/CMakeLists.txt @@ -326,6 +326,8 @@ set(TINT_LIB_SRCS sem/i32.h sem/if_statement.cc sem/if_statement.h + sem/index_accessor_expression.cc + sem/index_accessor_expression.h sem/info.cc sem/info.h sem/loop_statement.cc diff --git a/src/tint/resolver/array_accessor_test.cc b/src/tint/resolver/array_accessor_test.cc index 535d1ff55c..0c0ee47c14 100644 --- a/src/tint/resolver/array_accessor_test.cc +++ b/src/tint/resolver/array_accessor_test.cc @@ -16,6 +16,7 @@ #include "gmock/gmock.h" #include "src/tint/resolver/resolver_test_helper.h" +#include "src/tint/sem/index_accessor_expression.h" #include "src/tint/sem/reference.h" using namespace tint::number_suffixes; // NOLINT @@ -41,6 +42,11 @@ TEST_F(ResolverIndexAccessorTest, Matrix_Dynamic_Ref) { WrapInFunction(Decl(idx), acc); EXPECT_TRUE(r()->Resolve()) << r()->error(); + + auto idx_sem = Sem().Get(acc); + ASSERT_NE(idx_sem, nullptr); + EXPECT_EQ(idx_sem->Index()->Declaration(), acc->index); + EXPECT_EQ(idx_sem->Object()->Declaration(), acc->object); } TEST_F(ResolverIndexAccessorTest, Matrix_BothDimensions_Dynamic_Ref) { @@ -51,6 +57,11 @@ TEST_F(ResolverIndexAccessorTest, Matrix_BothDimensions_Dynamic_Ref) { WrapInFunction(Decl(idx), Decl(idy), acc); EXPECT_TRUE(r()->Resolve()) << r()->error(); + + auto idx_sem = Sem().Get(acc); + ASSERT_NE(idx_sem, nullptr); + EXPECT_EQ(idx_sem->Index()->Declaration(), acc->index); + EXPECT_EQ(idx_sem->Object()->Declaration(), acc->object); } TEST_F(ResolverIndexAccessorTest, Matrix_Dynamic) { @@ -61,6 +72,11 @@ TEST_F(ResolverIndexAccessorTest, Matrix_Dynamic) { EXPECT_TRUE(r()->Resolve()); EXPECT_EQ(r()->error(), ""); + + auto idx_sem = Sem().Get(acc); + ASSERT_NE(idx_sem, nullptr); + EXPECT_EQ(idx_sem->Index()->Declaration(), acc->index); + EXPECT_EQ(idx_sem->Object()->Declaration(), acc->object); } TEST_F(ResolverIndexAccessorTest, Matrix_XDimension_Dynamic) { @@ -71,6 +87,11 @@ TEST_F(ResolverIndexAccessorTest, Matrix_XDimension_Dynamic) { EXPECT_TRUE(r()->Resolve()); EXPECT_EQ(r()->error(), ""); + + auto idx_sem = Sem().Get(acc); + ASSERT_NE(idx_sem, nullptr); + EXPECT_EQ(idx_sem->Index()->Declaration(), acc->index); + EXPECT_EQ(idx_sem->Object()->Declaration(), acc->object); } TEST_F(ResolverIndexAccessorTest, Matrix_BothDimension_Dynamic) { @@ -81,6 +102,11 @@ TEST_F(ResolverIndexAccessorTest, Matrix_BothDimension_Dynamic) { EXPECT_TRUE(r()->Resolve()); EXPECT_EQ(r()->error(), ""); + + auto idx_sem = Sem().Get(acc); + ASSERT_NE(idx_sem, nullptr); + EXPECT_EQ(idx_sem->Index()->Declaration(), acc->index); + EXPECT_EQ(idx_sem->Object()->Declaration(), acc->object); } TEST_F(ResolverIndexAccessorTest, Matrix) { @@ -97,6 +123,11 @@ TEST_F(ResolverIndexAccessorTest, Matrix) { auto* ref = TypeOf(acc)->As(); ASSERT_TRUE(ref->StoreType()->Is()); EXPECT_EQ(ref->StoreType()->As()->Width(), 3u); + + auto idx_sem = Sem().Get(acc); + ASSERT_NE(idx_sem, nullptr); + EXPECT_EQ(idx_sem->Index()->Declaration(), acc->index); + EXPECT_EQ(idx_sem->Object()->Declaration(), acc->object); } TEST_F(ResolverIndexAccessorTest, Matrix_BothDimensions) { @@ -112,6 +143,11 @@ TEST_F(ResolverIndexAccessorTest, Matrix_BothDimensions) { auto* ref = TypeOf(acc)->As(); EXPECT_TRUE(ref->StoreType()->Is()); + + auto idx_sem = Sem().Get(acc); + ASSERT_NE(idx_sem, nullptr); + EXPECT_EQ(idx_sem->Index()->Declaration(), acc->index); + EXPECT_EQ(idx_sem->Object()->Declaration(), acc->object); } TEST_F(ResolverIndexAccessorTest, Vector_F32) { @@ -130,6 +166,11 @@ TEST_F(ResolverIndexAccessorTest, Vector_Dynamic_Ref) { WrapInFunction(Decl(idx), acc); EXPECT_TRUE(r()->Resolve()); + + auto idx_sem = Sem().Get(acc); + ASSERT_NE(idx_sem, nullptr); + EXPECT_EQ(idx_sem->Index()->Declaration(), acc->index); + EXPECT_EQ(idx_sem->Object()->Declaration(), acc->object); } TEST_F(ResolverIndexAccessorTest, Vector_Dynamic) { @@ -139,6 +180,11 @@ TEST_F(ResolverIndexAccessorTest, Vector_Dynamic) { WrapInFunction(Decl(idx), acc); EXPECT_TRUE(r()->Resolve()); + + auto idx_sem = Sem().Get(acc); + ASSERT_NE(idx_sem, nullptr); + EXPECT_EQ(idx_sem->Index()->Declaration(), acc->index); + EXPECT_EQ(idx_sem->Object()->Declaration(), acc->object); } TEST_F(ResolverIndexAccessorTest, Vector) { @@ -154,6 +200,11 @@ TEST_F(ResolverIndexAccessorTest, Vector) { auto* ref = TypeOf(acc)->As(); EXPECT_TRUE(ref->StoreType()->Is()); + + auto idx_sem = Sem().Get(acc); + ASSERT_NE(idx_sem, nullptr); + EXPECT_EQ(idx_sem->Index()->Declaration(), acc->index); + EXPECT_EQ(idx_sem->Object()->Declaration(), acc->object); } TEST_F(ResolverIndexAccessorTest, Array_Literal_i32) { @@ -165,6 +216,11 @@ TEST_F(ResolverIndexAccessorTest, Array_Literal_i32) { auto* ref = TypeOf(acc)->As(); ASSERT_NE(ref, nullptr); EXPECT_TRUE(ref->StoreType()->Is()); + + auto idx_sem = Sem().Get(acc); + ASSERT_NE(idx_sem, nullptr); + EXPECT_EQ(idx_sem->Index()->Declaration(), acc->index); + EXPECT_EQ(idx_sem->Object()->Declaration(), acc->object); } TEST_F(ResolverIndexAccessorTest, Array_Literal_u32) { @@ -176,6 +232,11 @@ TEST_F(ResolverIndexAccessorTest, Array_Literal_u32) { auto* ref = TypeOf(acc)->As(); ASSERT_NE(ref, nullptr); EXPECT_TRUE(ref->StoreType()->Is()); + + auto idx_sem = Sem().Get(acc); + ASSERT_NE(idx_sem, nullptr); + EXPECT_EQ(idx_sem->Index()->Declaration(), acc->index); + EXPECT_EQ(idx_sem->Object()->Declaration(), acc->object); } TEST_F(ResolverIndexAccessorTest, Array_Literal_AInt) { @@ -187,6 +248,11 @@ TEST_F(ResolverIndexAccessorTest, Array_Literal_AInt) { auto* ref = TypeOf(acc)->As(); ASSERT_NE(ref, nullptr); EXPECT_TRUE(ref->StoreType()->Is()); + + auto idx_sem = Sem().Get(acc); + ASSERT_NE(idx_sem, nullptr); + EXPECT_EQ(idx_sem->Index()->Declaration(), acc->index); + EXPECT_EQ(idx_sem->Object()->Declaration(), acc->object); } TEST_F(ResolverIndexAccessorTest, Alias_Array) { @@ -204,6 +270,11 @@ TEST_F(ResolverIndexAccessorTest, Alias_Array) { auto* ref = TypeOf(acc)->As(); EXPECT_TRUE(ref->StoreType()->Is()); + + auto idx_sem = Sem().Get(acc); + ASSERT_NE(idx_sem, nullptr); + EXPECT_EQ(idx_sem->Index()->Declaration(), acc->index); + EXPECT_EQ(idx_sem->Object()->Declaration(), acc->object); } TEST_F(ResolverIndexAccessorTest, Array_Constant) { @@ -216,6 +287,11 @@ TEST_F(ResolverIndexAccessorTest, Array_Constant) { ASSERT_NE(TypeOf(acc), nullptr); EXPECT_TRUE(TypeOf(acc)->Is()); + + auto idx_sem = Sem().Get(acc); + ASSERT_NE(idx_sem, nullptr); + EXPECT_EQ(idx_sem->Index()->Declaration(), acc->index); + EXPECT_EQ(idx_sem->Object()->Declaration(), acc->object); } TEST_F(ResolverIndexAccessorTest, Array_Dynamic_I32) { @@ -224,7 +300,8 @@ TEST_F(ResolverIndexAccessorTest, Array_Dynamic_I32) { // var f : f32 = a[idx]; auto* a = Let("a", ty.array(), array()); auto* idx = Var("idx", ty.i32(), Construct(ty.i32())); - auto* f = Var("f", ty.f32(), IndexAccessor("a", Expr(Source{{12, 34}}, idx))); + auto* acc = IndexAccessor("a", Expr(Source{{12, 34}}, idx)); + auto* f = Var("f", ty.f32(), acc); Func("my_func", {}, ty.void_(), { Decl(a), @@ -234,6 +311,11 @@ TEST_F(ResolverIndexAccessorTest, Array_Dynamic_I32) { EXPECT_TRUE(r()->Resolve()); EXPECT_EQ(r()->error(), ""); + + auto idx_sem = Sem().Get(acc); + ASSERT_NE(idx_sem, nullptr); + EXPECT_EQ(idx_sem->Index()->Declaration(), acc->index); + EXPECT_EQ(idx_sem->Object()->Declaration(), acc->object); } TEST_F(ResolverIndexAccessorTest, Array_Literal_F32) { @@ -254,13 +336,19 @@ TEST_F(ResolverIndexAccessorTest, Array_Literal_I32) { // let a : array; // var f : f32 = a[2i]; auto* a = Let("a", ty.array(), array()); - auto* f = Var("a_2", ty.f32(), IndexAccessor("a", 2_i)); + auto* acc = IndexAccessor("a", 2_i); + auto* f = Var("a_2", ty.f32(), acc); Func("my_func", {}, ty.void_(), { Decl(a), Decl(f), }); EXPECT_TRUE(r()->Resolve()) << r()->error(); + + auto idx_sem = Sem().Get(acc); + ASSERT_NE(idx_sem, nullptr); + EXPECT_EQ(idx_sem->Index()->Declaration(), acc->index); + EXPECT_EQ(idx_sem->Object()->Declaration(), acc->object); } TEST_F(ResolverIndexAccessorTest, Expr_Deref_FuncGoodParent) { @@ -272,11 +360,16 @@ TEST_F(ResolverIndexAccessorTest, Expr_Deref_FuncGoodParent) { auto* p = Param("p", ty.pointer(ty.vec4(), ast::StorageClass::kFunction)); auto* idx = Let("idx", ty.u32(), Construct(ty.u32())); auto* star_p = Deref(p); - auto* accessor_expr = IndexAccessor(Source{{12, 34}}, star_p, idx); - auto* x = Var("x", ty.f32(), accessor_expr); + auto* acc = IndexAccessor(Source{{12, 34}}, star_p, idx); + auto* x = Var("x", ty.f32(), acc); Func("func", {p}, ty.f32(), {Decl(idx), Decl(x), Return(x)}); EXPECT_TRUE(r()->Resolve()) << r()->error(); + + auto idx_sem = Sem().Get(acc); + ASSERT_NE(idx_sem, nullptr); + EXPECT_EQ(idx_sem->Index()->Declaration(), acc->index); + EXPECT_EQ(idx_sem->Object()->Declaration(), acc->object); } TEST_F(ResolverIndexAccessorTest, Expr_Deref_FuncBadParent) { diff --git a/src/tint/resolver/resolver.cc b/src/tint/resolver/resolver.cc index d19f9bda6d..e0dc19227e 100644 --- a/src/tint/resolver/resolver.cc +++ b/src/tint/resolver/resolver.cc @@ -62,6 +62,7 @@ #include "src/tint/sem/for_loop_statement.h" #include "src/tint/sem/function.h" #include "src/tint/sem/if_statement.h" +#include "src/tint/sem/index_accessor_expression.h" #include "src/tint/sem/loop_statement.h" #include "src/tint/sem/materialize.h" #include "src/tint/sem/member_accessor_expression.h" @@ -1359,8 +1360,9 @@ sem::Expression* Resolver::IndexAccessor(const ast::IndexAccessorExpression* exp auto val = EvaluateConstantValue(expr, ty); bool has_side_effects = idx->HasSideEffects() || obj->HasSideEffects(); - auto* sem = builder_->create(expr, ty, current_statement_, std::move(val), - has_side_effects, obj->SourceVariable()); + auto* sem = builder_->create( + expr, ty, obj, idx, current_statement_, std::move(val), has_side_effects, + obj->SourceVariable()); sem->Behaviors() = idx->Behaviors() + obj->Behaviors(); return sem; } @@ -1872,9 +1874,9 @@ sem::Expression* Resolver::MemberAccessor(const ast::MemberAccessorExpression* e const sem::Type* ret = nullptr; std::vector swizzle; - // Structure may be a side-effecting expression (e.g. function call). - auto* sem_structure = sem_.Get(expr->structure); - bool has_side_effects = sem_structure && sem_structure->HasSideEffects(); + // Object may be a side-effecting expression (e.g. function call). + auto* object = sem_.Get(expr->structure); + bool has_side_effects = object && object->HasSideEffects(); if (auto* str = storage_ty->As()) { Mark(expr->member); @@ -1900,8 +1902,8 @@ sem::Expression* Resolver::MemberAccessor(const ast::MemberAccessorExpression* e ret = builder_->create(ret, ref->StorageClass(), ref->Access()); } - return builder_->create(expr, ret, current_statement_, member, - has_side_effects, source_var); + return builder_->create(expr, ret, current_statement_, object, + member, has_side_effects, source_var); } if (auto* vec = storage_ty->As()) { @@ -1967,8 +1969,8 @@ sem::Expression* Resolver::MemberAccessor(const ast::MemberAccessorExpression* e // the swizzle. ret = builder_->create(vec->type(), static_cast(size)); } - return builder_->create(expr, ret, current_statement_, std::move(swizzle), - has_side_effects, source_var); + return builder_->create(expr, ret, current_statement_, object, + std::move(swizzle), has_side_effects, source_var); } AddError("invalid member accessor expression. Expected vector or struct, got '" + @@ -2384,6 +2386,8 @@ sem::Struct* Resolver::Structure(const ast::Struct* str) { break; } } + + const_cast(sem_members[i])->SetStruct(out); } auto stage = current_function_ ? current_function_->Declaration()->PipelineStage() diff --git a/src/tint/resolver/resolver_constants_test.cc b/src/tint/resolver/resolver_constants_test.cc index 363fcb1b12..a3c01f8502 100644 --- a/src/tint/resolver/resolver_constants_test.cc +++ b/src/tint/resolver/resolver_constants_test.cc @@ -17,6 +17,7 @@ #include "gtest/gtest.h" #include "src/tint/resolver/resolver_test_helper.h" #include "src/tint/sem/expression.h" +#include "src/tint/sem/index_accessor_expression.h" using namespace tint::number_suffixes; // NOLINT diff --git a/src/tint/resolver/resolver_test.cc b/src/tint/resolver/resolver_test.cc index 0fecf0c4a9..00484f3881 100644 --- a/src/tint/resolver/resolver_test.cc +++ b/src/tint/resolver/resolver_test.cc @@ -1101,6 +1101,7 @@ TEST_F(ResolverTest, Expr_MemberAccessor_Struct) { auto* sma = Sem().Get(mem)->As(); ASSERT_NE(sma, nullptr); EXPECT_TRUE(sma->Member()->Type()->Is()); + EXPECT_EQ(sma->Object()->Declaration(), mem->structure); EXPECT_EQ(sma->Member()->Index(), 1u); EXPECT_EQ(sma->Member()->Declaration()->symbol, Symbols().Get("second_member")); } @@ -1123,6 +1124,7 @@ TEST_F(ResolverTest, Expr_MemberAccessor_Struct_Alias) { EXPECT_TRUE(ref->StoreType()->Is()); auto* sma = Sem().Get(mem)->As(); ASSERT_NE(sma, nullptr); + EXPECT_EQ(sma->Object()->Declaration(), mem->structure); EXPECT_TRUE(sma->Member()->Type()->Is()); EXPECT_EQ(sma->Member()->Index(), 1u); } @@ -1139,8 +1141,10 @@ TEST_F(ResolverTest, Expr_MemberAccessor_VectorSwizzle) { ASSERT_TRUE(TypeOf(mem)->Is()); EXPECT_TRUE(TypeOf(mem)->As()->type()->Is()); EXPECT_EQ(TypeOf(mem)->As()->Width(), 4u); - ASSERT_TRUE(Sem().Get(mem)->Is()); - EXPECT_THAT(Sem().Get(mem)->As()->Indices(), ElementsAre(0, 2, 1, 3)); + auto* sma = Sem().Get(mem)->As(); + ASSERT_NE(sma, nullptr); + EXPECT_EQ(sma->Object()->Declaration(), mem->structure); + EXPECT_THAT(sma->As()->Indices(), ElementsAre(0, 2, 1, 3)); } TEST_F(ResolverTest, Expr_MemberAccessor_VectorSwizzle_SingleElement) { @@ -1156,7 +1160,9 @@ TEST_F(ResolverTest, Expr_MemberAccessor_VectorSwizzle_SingleElement) { auto* ref = TypeOf(mem)->As(); ASSERT_TRUE(ref->StoreType()->Is()); - ASSERT_TRUE(Sem().Get(mem)->Is()); + auto* sma = Sem().Get(mem)->As(); + ASSERT_NE(sma, nullptr); + EXPECT_EQ(sma->Object()->Declaration(), mem->structure); EXPECT_THAT(Sem().Get(mem)->As()->Indices(), ElementsAre(2)); } diff --git a/src/tint/resolver/side_effects_test.cc b/src/tint/resolver/side_effects_test.cc index a50d9045f4..5f11afdefe 100644 --- a/src/tint/resolver/side_effects_test.cc +++ b/src/tint/resolver/side_effects_test.cc @@ -17,6 +17,7 @@ #include "gtest/gtest.h" #include "src/tint/resolver/resolver_test_helper.h" #include "src/tint/sem/expression.h" +#include "src/tint/sem/index_accessor_expression.h" #include "src/tint/sem/member_accessor_expression.h" using namespace tint::number_suffixes; // NOLINT diff --git a/src/tint/resolver/source_variable_test.cc b/src/tint/resolver/source_variable_test.cc index cd943f3635..88c193dbb8 100644 --- a/src/tint/resolver/source_variable_test.cc +++ b/src/tint/resolver/source_variable_test.cc @@ -15,6 +15,7 @@ #include "src/tint/resolver/resolver.h" #include "src/tint/resolver/resolver_test_helper.h" +#include "src/tint/sem/index_accessor_expression.h" #include "src/tint/sem/member_accessor_expression.h" using namespace tint::number_suffixes; // NOLINT diff --git a/src/tint/resolver/struct_layout_test.cc b/src/tint/resolver/struct_layout_test.cc index 854e87ae3e..35bf61268c 100644 --- a/src/tint/resolver/struct_layout_test.cc +++ b/src/tint/resolver/struct_layout_test.cc @@ -49,6 +49,9 @@ TEST_F(ResolverStructLayoutTest, Scalars) { EXPECT_EQ(sem->Members()[2]->Offset(), 8u); EXPECT_EQ(sem->Members()[2]->Align(), 4u); EXPECT_EQ(sem->Members()[2]->Size(), 4u); + for (auto& m : sem->Members()) { + EXPECT_EQ(m->Struct()->Declaration(), s); + } } TEST_F(ResolverStructLayoutTest, Alias) { @@ -74,6 +77,9 @@ TEST_F(ResolverStructLayoutTest, Alias) { EXPECT_EQ(sem->Members()[1]->Offset(), 4u); EXPECT_EQ(sem->Members()[1]->Align(), 4u); EXPECT_EQ(sem->Members()[1]->Size(), 4u); + for (auto& m : sem->Members()) { + EXPECT_EQ(m->Struct()->Declaration(), s); + } } TEST_F(ResolverStructLayoutTest, ImplicitStrideArrayStaticSize) { @@ -100,6 +106,9 @@ TEST_F(ResolverStructLayoutTest, ImplicitStrideArrayStaticSize) { EXPECT_EQ(sem->Members()[2]->Offset(), 32u); EXPECT_EQ(sem->Members()[2]->Align(), 4u); EXPECT_EQ(sem->Members()[2]->Size(), 4u); + for (auto& m : sem->Members()) { + EXPECT_EQ(m->Struct()->Declaration(), s); + } } TEST_F(ResolverStructLayoutTest, ExplicitStrideArrayStaticSize) { @@ -126,6 +135,9 @@ TEST_F(ResolverStructLayoutTest, ExplicitStrideArrayStaticSize) { EXPECT_EQ(sem->Members()[2]->Offset(), 104u); EXPECT_EQ(sem->Members()[2]->Align(), 4u); EXPECT_EQ(sem->Members()[2]->Size(), 32u); + for (auto& m : sem->Members()) { + EXPECT_EQ(m->Struct()->Declaration(), s); + } } TEST_F(ResolverStructLayoutTest, ImplicitStrideArrayRuntimeSized) { @@ -144,6 +156,9 @@ TEST_F(ResolverStructLayoutTest, ImplicitStrideArrayRuntimeSized) { EXPECT_EQ(sem->Members()[0]->Offset(), 0u); EXPECT_EQ(sem->Members()[0]->Align(), 4u); EXPECT_EQ(sem->Members()[0]->Size(), 4u); + for (auto& m : sem->Members()) { + EXPECT_EQ(m->Struct()->Declaration(), s); + } } TEST_F(ResolverStructLayoutTest, ExplicitStrideArrayRuntimeSized) { @@ -162,6 +177,9 @@ TEST_F(ResolverStructLayoutTest, ExplicitStrideArrayRuntimeSized) { EXPECT_EQ(sem->Members()[0]->Offset(), 0u); EXPECT_EQ(sem->Members()[0]->Align(), 4u); EXPECT_EQ(sem->Members()[0]->Size(), 32u); + for (auto& m : sem->Members()) { + EXPECT_EQ(m->Struct()->Declaration(), s); + } } TEST_F(ResolverStructLayoutTest, ImplicitStrideArrayOfExplicitStrideArray) { @@ -182,6 +200,9 @@ TEST_F(ResolverStructLayoutTest, ImplicitStrideArrayOfExplicitStrideArray) { EXPECT_EQ(sem->Members()[0]->Offset(), 0u); EXPECT_EQ(sem->Members()[0]->Align(), 4u); EXPECT_EQ(sem->Members()[0]->Size(), 384u); + for (auto& m : sem->Members()) { + EXPECT_EQ(m->Struct()->Declaration(), s); + } } TEST_F(ResolverStructLayoutTest, ImplicitStrideArrayOfStructure) { @@ -206,6 +227,9 @@ TEST_F(ResolverStructLayoutTest, ImplicitStrideArrayOfStructure) { EXPECT_EQ(sem->Members()[0]->Offset(), 0u); EXPECT_EQ(sem->Members()[0]->Align(), 16u); EXPECT_EQ(sem->Members()[0]->Size(), 576u); + for (auto& m : sem->Members()) { + EXPECT_EQ(m->Struct()->Declaration(), s); + } } TEST_F(ResolverStructLayoutTest, Vector) { @@ -232,6 +256,9 @@ TEST_F(ResolverStructLayoutTest, Vector) { EXPECT_EQ(sem->Members()[2]->Offset(), 32u); // vec4 EXPECT_EQ(sem->Members()[2]->Align(), 16u); EXPECT_EQ(sem->Members()[2]->Size(), 16u); + for (auto& m : sem->Members()) { + EXPECT_EQ(m->Struct()->Declaration(), s); + } } TEST_F(ResolverStructLayoutTest, Matrix) { @@ -282,6 +309,9 @@ TEST_F(ResolverStructLayoutTest, Matrix) { EXPECT_EQ(sem->Members()[8]->Offset(), 304u); // mat4x4 EXPECT_EQ(sem->Members()[8]->Align(), 16u); EXPECT_EQ(sem->Members()[8]->Size(), 64u); + for (auto& m : sem->Members()) { + EXPECT_EQ(m->Struct()->Declaration(), s); + } } TEST_F(ResolverStructLayoutTest, NestedStruct) { @@ -311,6 +341,9 @@ TEST_F(ResolverStructLayoutTest, NestedStruct) { EXPECT_EQ(sem->Members()[2]->Offset(), 64u); EXPECT_EQ(sem->Members()[2]->Align(), 4u); EXPECT_EQ(sem->Members()[2]->Size(), 4u); + for (auto& m : sem->Members()) { + EXPECT_EQ(m->Struct()->Declaration(), s); + } } TEST_F(ResolverStructLayoutTest, SizeAttributes) { @@ -346,6 +379,9 @@ TEST_F(ResolverStructLayoutTest, SizeAttributes) { EXPECT_EQ(sem->Members()[3]->Offset(), 44u); EXPECT_EQ(sem->Members()[3]->Align(), 4u); EXPECT_EQ(sem->Members()[3]->Size(), 32u); + for (auto& m : sem->Members()) { + EXPECT_EQ(m->Struct()->Declaration(), s); + } } TEST_F(ResolverStructLayoutTest, AlignAttributes) { @@ -381,6 +417,9 @@ TEST_F(ResolverStructLayoutTest, AlignAttributes) { EXPECT_EQ(sem->Members()[3]->Offset(), 64u); EXPECT_EQ(sem->Members()[3]->Align(), 32u); EXPECT_EQ(sem->Members()[3]->Size(), 4u); + for (auto& m : sem->Members()) { + EXPECT_EQ(m->Struct()->Declaration(), s); + } } TEST_F(ResolverStructLayoutTest, StructWithLotsOfPadding) { @@ -399,6 +438,9 @@ TEST_F(ResolverStructLayoutTest, StructWithLotsOfPadding) { EXPECT_EQ(sem->Members()[0]->Offset(), 0u); EXPECT_EQ(sem->Members()[0]->Align(), 1024u); EXPECT_EQ(sem->Members()[0]->Size(), 4u); + for (auto& m : sem->Members()) { + EXPECT_EQ(m->Struct()->Declaration(), s); + } } } // namespace diff --git a/src/tint/sem/index_accessor_expression.cc b/src/tint/sem/index_accessor_expression.cc new file mode 100644 index 0000000000..fc11b4c3d9 --- /dev/null +++ b/src/tint/sem/index_accessor_expression.cc @@ -0,0 +1,37 @@ +// 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/tint/sem/index_accessor_expression.h" + +#include + +TINT_INSTANTIATE_TYPEINFO(tint::sem::IndexAccessorExpression); + +namespace tint::sem { + +IndexAccessorExpression::IndexAccessorExpression(const ast::IndexAccessorExpression* declaration, + const sem::Type* type, + const Expression* object, + const Expression* index, + const Statement* statement, + Constant constant, + bool has_side_effects, + const Variable* source_var /* = nullptr */) + : Base(declaration, type, statement, constant, has_side_effects, source_var), + object_(object), + index_(index) {} + +IndexAccessorExpression::~IndexAccessorExpression() = default; + +} // namespace tint::sem diff --git a/src/tint/sem/index_accessor_expression.h b/src/tint/sem/index_accessor_expression.h new file mode 100644 index 0000000000..c77f55db02 --- /dev/null +++ b/src/tint/sem/index_accessor_expression.h @@ -0,0 +1,66 @@ +// Copyright 2022 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. + +#ifndef SRC_TINT_SEM_INDEX_ACCESSOR_EXPRESSION_H_ +#define SRC_TINT_SEM_INDEX_ACCESSOR_EXPRESSION_H_ + +#include + +#include "src/tint/sem/expression.h" + +// Forward declarations +namespace tint::ast { +class IndexAccessorExpression; +} // namespace tint::ast + +namespace tint::sem { + +/// IndexAccessorExpression holds the semantic information for a ast::IndexAccessorExpression node. +class IndexAccessorExpression final : public Castable { + public: + /// Constructor + /// @param declaration the AST node + /// @param type the resolved type of the expression + /// @param object the object expression that is being indexed + /// @param index the index expression + /// @param statement the statement that owns this expression + /// @param constant the constant value of the expression. May be invalid + /// @param has_side_effects whether this expression may have side effects + /// @param source_var the (optional) source variable for this expression + IndexAccessorExpression(const ast::IndexAccessorExpression* declaration, + const sem::Type* type, + const Expression* object, + const Expression* index, + const Statement* statement, + Constant constant, + bool has_side_effects, + const Variable* source_var = nullptr); + + /// Destructor + ~IndexAccessorExpression() override; + + /// @returns the object expression that is being indexed + Expression const* Object() const { return object_; } + + /// @returns the index expression + Expression const* Index() const { return index_; } + + private: + Expression const* const object_; + Expression const* const index_; +}; + +} // namespace tint::sem + +#endif // SRC_TINT_SEM_INDEX_ACCESSOR_EXPRESSION_H_ diff --git a/src/tint/sem/member_accessor_expression.cc b/src/tint/sem/member_accessor_expression.cc index 9dcca76ef0..bd706a510e 100644 --- a/src/tint/sem/member_accessor_expression.cc +++ b/src/tint/sem/member_accessor_expression.cc @@ -26,29 +26,33 @@ namespace tint::sem { MemberAccessorExpression::MemberAccessorExpression(const ast::MemberAccessorExpression* declaration, const sem::Type* type, const Statement* statement, + const Expression* object, bool has_side_effects, const Variable* source_var /* = nullptr */) - : Base(declaration, type, statement, Constant{}, has_side_effects, source_var) {} + : Base(declaration, type, statement, Constant{}, has_side_effects, source_var), + object_(object) {} MemberAccessorExpression::~MemberAccessorExpression() = default; StructMemberAccess::StructMemberAccess(const ast::MemberAccessorExpression* declaration, const sem::Type* type, const Statement* statement, + const Expression* object, const StructMember* member, bool has_side_effects, const Variable* source_var /* = nullptr */) - : Base(declaration, type, statement, has_side_effects, source_var), member_(member) {} + : Base(declaration, type, statement, object, has_side_effects, source_var), member_(member) {} StructMemberAccess::~StructMemberAccess() = default; Swizzle::Swizzle(const ast::MemberAccessorExpression* declaration, const sem::Type* type, const Statement* statement, + const Expression* object, std::vector indices, bool has_side_effects, const Variable* source_var /* = nullptr */) - : Base(declaration, type, statement, has_side_effects, source_var), + : Base(declaration, type, statement, object, has_side_effects, source_var), indices_(std::move(indices)) {} Swizzle::~Swizzle() = default; diff --git a/src/tint/sem/member_accessor_expression.h b/src/tint/sem/member_accessor_expression.h index 0233541e31..3d60816a08 100644 --- a/src/tint/sem/member_accessor_expression.h +++ b/src/tint/sem/member_accessor_expression.h @@ -38,16 +38,24 @@ class MemberAccessorExpression : public Castable { /// @param declaration the AST node /// @param type the resolved type of the expression /// @param statement the statement that owns this expression + /// @param object the object that holds the member being accessed /// @param indices the swizzle indices /// @param has_side_effects whether this expression may have side effects /// @param source_var the (optional) source variable for this expression Swizzle(const ast::MemberAccessorExpression* declaration, const sem::Type* type, const Statement* statement, + const Expression* object, std::vector indices, bool has_side_effects, const Variable* source_var = nullptr); diff --git a/src/tint/sem/struct.h b/src/tint/sem/struct.h index fe9169dc12..e026b11d08 100644 --- a/src/tint/sem/struct.h +++ b/src/tint/sem/struct.h @@ -194,9 +194,16 @@ class StructMember : public Castable { /// @returns the AST declaration node const ast::StructMember* Declaration() const { return declaration_; } - /// @returns the name of the structure + /// @returns the name of the structure member Symbol Name() const { return name_; } + /// Sets the owning structure to `s` + /// @param s the new structure owner + void SetStruct(const sem::Struct* s) { struct_ = s; } + + /// @returns the structure that owns this member + const sem::Struct* Struct() const { return struct_; } + /// @returns the type of the member sem::Type* Type() const { return type_; } @@ -215,6 +222,7 @@ class StructMember : public Castable { private: const ast::StructMember* const declaration_; const Symbol name_; + const sem::Struct* struct_; sem::Type* const type_; const uint32_t index_; const uint32_t offset_; diff --git a/src/tint/sem/type_mappings.h b/src/tint/sem/type_mappings.h index 9041bdb73e..7dc0ade0ae 100644 --- a/src/tint/sem/type_mappings.h +++ b/src/tint/sem/type_mappings.h @@ -25,6 +25,7 @@ class Expression; class ForLoopStatement; class Function; class IfStatement; +class IndexAccessorExpression; class MemberAccessorExpression; class Node; class Override; @@ -44,6 +45,7 @@ class Expression; class ForLoopStatement; class Function; class IfStatement; +class IndexAccessorExpression; class MemberAccessorExpression; class Node; class GlobalVariable; @@ -69,6 +71,7 @@ struct TypeMappings { ForLoopStatement* operator()(ast::ForLoopStatement*); Function* operator()(ast::Function*); IfStatement* operator()(ast::IfStatement*); + IndexAccessorExpression* operator()(ast::IndexAccessorExpression*); MemberAccessorExpression* operator()(ast::MemberAccessorExpression*); Node* operator()(ast::Node*); GlobalVariable* operator()(ast::Override*); diff --git a/src/tint/transform/utils/hoist_to_decl_before_test.cc b/src/tint/transform/utils/hoist_to_decl_before_test.cc index 46d5551991..22784baf5d 100644 --- a/src/tint/transform/utils/hoist_to_decl_before_test.cc +++ b/src/tint/transform/utils/hoist_to_decl_before_test.cc @@ -17,6 +17,7 @@ #include "gtest/gtest-spi.h" #include "src/tint/program_builder.h" #include "src/tint/sem/if_statement.h" +#include "src/tint/sem/index_accessor_expression.h" #include "src/tint/sem/statement.h" #include "src/tint/transform/test_helper.h" #include "src/tint/transform/utils/hoist_to_decl_before.h"