tint: Add sem::IndexAccessorExpression
Also store expression object in MemberAccessorExpression, as well as the
struct on sem::StructMember.
These are used to implement spir-v reader atomics in a follow-up CL.
Bug: tint:1441
Change-Id: Iea49cfb7f9d2e7898d89d2dac6a16a14022c546f
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/94523
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Antonio Maiorano <amaiorano@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
diff --git a/src/tint/BUILD.gn b/src/tint/BUILD.gn
index be99389..6bf1aea 100644
--- a/src/tint/BUILD.gn
+++ b/src/tint/BUILD.gn
@@ -428,6 +428,7 @@
"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 @@
"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 10d81d6..fee5592 100644
--- a/src/tint/CMakeLists.txt
+++ b/src/tint/CMakeLists.txt
@@ -326,6 +326,8 @@
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 535d1ff..0c0ee47 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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
auto* ref = TypeOf(acc)->As<sem::Reference>();
ASSERT_TRUE(ref->StoreType()->Is<sem::Vector>());
EXPECT_EQ(ref->StoreType()->As<sem::Vector>()->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 @@
auto* ref = TypeOf(acc)->As<sem::Reference>();
EXPECT_TRUE(ref->StoreType()->Is<sem::F32>());
+
+ 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 @@
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 @@
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 @@
auto* ref = TypeOf(acc)->As<sem::Reference>();
EXPECT_TRUE(ref->StoreType()->Is<sem::F32>());
+
+ 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 @@
auto* ref = TypeOf(acc)->As<sem::Reference>();
ASSERT_NE(ref, nullptr);
EXPECT_TRUE(ref->StoreType()->Is<sem::F32>());
+
+ 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 @@
auto* ref = TypeOf(acc)->As<sem::Reference>();
ASSERT_NE(ref, nullptr);
EXPECT_TRUE(ref->StoreType()->Is<sem::F32>());
+
+ 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 @@
auto* ref = TypeOf(acc)->As<sem::Reference>();
ASSERT_NE(ref, nullptr);
EXPECT_TRUE(ref->StoreType()->Is<sem::F32>());
+
+ 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 @@
auto* ref = TypeOf(acc)->As<sem::Reference>();
EXPECT_TRUE(ref->StoreType()->Is<sem::F32>());
+
+ 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 @@
ASSERT_NE(TypeOf(acc), nullptr);
EXPECT_TRUE(TypeOf(acc)->Is<sem::F32>());
+
+ 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 @@
// var f : f32 = a[idx];
auto* a = Let("a", ty.array<f32, 3>(), array<f32, 3>());
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 @@
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 @@
// let a : array<f32, 3>;
// var f : f32 = a[2i];
auto* a = Let("a", ty.array<f32, 3>(), array<f32, 3>());
- 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 @@
auto* p = Param("p", ty.pointer(ty.vec4<f32>(), 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 d19f9bd..e0dc192 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 @@
auto val = EvaluateConstantValue(expr, ty);
bool has_side_effects = idx->HasSideEffects() || obj->HasSideEffects();
- auto* sem = builder_->create<sem::Expression>(expr, ty, current_statement_, std::move(val),
- has_side_effects, obj->SourceVariable());
+ auto* sem = builder_->create<sem::IndexAccessorExpression>(
+ 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 @@
const sem::Type* ret = nullptr;
std::vector<uint32_t> 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<sem::Struct>()) {
Mark(expr->member);
@@ -1900,8 +1902,8 @@
ret = builder_->create<sem::Reference>(ret, ref->StorageClass(), ref->Access());
}
- return builder_->create<sem::StructMemberAccess>(expr, ret, current_statement_, member,
- has_side_effects, source_var);
+ return builder_->create<sem::StructMemberAccess>(expr, ret, current_statement_, object,
+ member, has_side_effects, source_var);
}
if (auto* vec = storage_ty->As<sem::Vector>()) {
@@ -1967,8 +1969,8 @@
// the swizzle.
ret = builder_->create<sem::Vector>(vec->type(), static_cast<uint32_t>(size));
}
- return builder_->create<sem::Swizzle>(expr, ret, current_statement_, std::move(swizzle),
- has_side_effects, source_var);
+ return builder_->create<sem::Swizzle>(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 @@
break;
}
}
+
+ const_cast<sem::StructMember*>(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 363fcb1..a3c01f8 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 0fecf0c..00484f3 100644
--- a/src/tint/resolver/resolver_test.cc
+++ b/src/tint/resolver/resolver_test.cc
@@ -1101,6 +1101,7 @@
auto* sma = Sem().Get(mem)->As<sem::StructMemberAccess>();
ASSERT_NE(sma, nullptr);
EXPECT_TRUE(sma->Member()->Type()->Is<sem::F32>());
+ 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 @@
EXPECT_TRUE(ref->StoreType()->Is<sem::F32>());
auto* sma = Sem().Get(mem)->As<sem::StructMemberAccess>();
ASSERT_NE(sma, nullptr);
+ EXPECT_EQ(sma->Object()->Declaration(), mem->structure);
EXPECT_TRUE(sma->Member()->Type()->Is<sem::F32>());
EXPECT_EQ(sma->Member()->Index(), 1u);
}
@@ -1139,8 +1141,10 @@
ASSERT_TRUE(TypeOf(mem)->Is<sem::Vector>());
EXPECT_TRUE(TypeOf(mem)->As<sem::Vector>()->type()->Is<sem::F32>());
EXPECT_EQ(TypeOf(mem)->As<sem::Vector>()->Width(), 4u);
- ASSERT_TRUE(Sem().Get(mem)->Is<sem::Swizzle>());
- EXPECT_THAT(Sem().Get(mem)->As<sem::Swizzle>()->Indices(), ElementsAre(0, 2, 1, 3));
+ auto* sma = Sem().Get(mem)->As<sem::Swizzle>();
+ ASSERT_NE(sma, nullptr);
+ EXPECT_EQ(sma->Object()->Declaration(), mem->structure);
+ EXPECT_THAT(sma->As<sem::Swizzle>()->Indices(), ElementsAre(0, 2, 1, 3));
}
TEST_F(ResolverTest, Expr_MemberAccessor_VectorSwizzle_SingleElement) {
@@ -1156,7 +1160,9 @@
auto* ref = TypeOf(mem)->As<sem::Reference>();
ASSERT_TRUE(ref->StoreType()->Is<sem::F32>());
- ASSERT_TRUE(Sem().Get(mem)->Is<sem::Swizzle>());
+ auto* sma = Sem().Get(mem)->As<sem::Swizzle>();
+ ASSERT_NE(sma, nullptr);
+ EXPECT_EQ(sma->Object()->Declaration(), mem->structure);
EXPECT_THAT(Sem().Get(mem)->As<sem::Swizzle>()->Indices(), ElementsAre(2));
}
diff --git a/src/tint/resolver/side_effects_test.cc b/src/tint/resolver/side_effects_test.cc
index a50d904..5f11afd 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 cd943f3..88c193d 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 854e87a..35bf612 100644
--- a/src/tint/resolver/struct_layout_test.cc
+++ b/src/tint/resolver/struct_layout_test.cc
@@ -49,6 +49,9 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 0000000..fc11b4c
--- /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 <utility>
+
+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 0000000..c77f55d
--- /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 <vector>
+
+#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<IndexAccessorExpression, Expression> {
+ 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 9dcca76..bd706a5 100644
--- a/src/tint/sem/member_accessor_expression.cc
+++ b/src/tint/sem/member_accessor_expression.cc
@@ -26,29 +26,33 @@
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<uint32_t> 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 0233541..3d60816 100644
--- a/src/tint/sem/member_accessor_expression.h
+++ b/src/tint/sem/member_accessor_expression.h
@@ -38,16 +38,24 @@
/// @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 has_side_effects whether this expression may have side effects
/// @param source_var the (optional) source variable for this expression
MemberAccessorExpression(const ast::MemberAccessorExpression* declaration,
const sem::Type* type,
const Statement* statement,
+ const Expression* object,
bool has_side_effects,
const Variable* source_var = nullptr);
/// Destructor
~MemberAccessorExpression() override;
+
+ /// @returns the object that holds the member being accessed
+ const Expression* Object() const { return object_; }
+
+ private:
+ Expression const* const object_;
};
/// StructMemberAccess holds the semantic information for a
@@ -59,12 +67,14 @@
/// @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 member the structure member
/// @param has_side_effects whether this expression may have side effects
/// @param source_var the (optional) source variable for this expression
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);
@@ -87,12 +97,14 @@
/// @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<uint32_t> 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 fe9169d..e026b11 100644
--- a/src/tint/sem/struct.h
+++ b/src/tint/sem/struct.h
@@ -194,9 +194,16 @@
/// @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 @@
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 9041bdb..7dc0ade 100644
--- a/src/tint/sem/type_mappings.h
+++ b/src/tint/sem/type_mappings.h
@@ -25,6 +25,7 @@
class ForLoopStatement;
class Function;
class IfStatement;
+class IndexAccessorExpression;
class MemberAccessorExpression;
class Node;
class Override;
@@ -44,6 +45,7 @@
class ForLoopStatement;
class Function;
class IfStatement;
+class IndexAccessorExpression;
class MemberAccessorExpression;
class Node;
class GlobalVariable;
@@ -69,6 +71,7 @@
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 46d5551..22784ba 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"