diff --git a/src/program_builder.h b/src/program_builder.h index 1dec6d9f1d..5dc5accce9 100644 --- a/src/program_builder.h +++ b/src/program_builder.h @@ -18,6 +18,7 @@ #include #include +#include "src/ast/array.h" #include "src/ast/array_accessor_expression.h" #include "src/ast/assignment_statement.h" #include "src/ast/binary_expression.h" @@ -520,34 +521,39 @@ class ProgramBuilder { /// @param subtype the array element type /// @param n the array size. 0 represents a runtime-array. /// @return the tint AST type for a array of size `n` of type `T` - sem::ArrayType* array(sem::Type* subtype, uint32_t n = 0) const { - return builder->create(subtype, n, ast::DecorationList{}); + typ::Array array(typ::Type subtype, uint32_t n = 0) const { + return { + builder->create(subtype, n, ast::DecorationList{}), + builder->create(subtype, n, ast::DecorationList{})}; } /// @param subtype the array element type /// @param n the array size. 0 represents a runtime-array. /// @param stride the array stride. /// @return the tint AST type for a array of size `n` of type `T` - sem::ArrayType* array(sem::Type* subtype, - uint32_t n, - uint32_t stride) const { - return builder->create( - subtype, n, - ast::DecorationList{ - builder->create(stride), - }); + typ::Array array(typ::Type subtype, uint32_t n, uint32_t stride) const { + return {builder->create( + subtype, n, + ast::DecorationList{ + builder->create(stride), + }), + builder->create( + subtype, n, + ast::DecorationList{ + builder->create(stride), + })}; } /// @return the tint AST type for an array of size `N` of type `T` template - sem::ArrayType* array() const { + typ::Array array() const { return array(Of(), N); } /// @param stride the array stride /// @return the tint AST type for an array of size `N` of type `T` template - sem::ArrayType* array(uint32_t stride) const { + typ::Array array(uint32_t stride) const { return array(Of(), N, stride); } diff --git a/src/resolver/decoration_validation_test.cc b/src/resolver/decoration_validation_test.cc index 95f803820d..9bae7b948b 100644 --- a/src/resolver/decoration_validation_test.cc +++ b/src/resolver/decoration_validation_test.cc @@ -438,7 +438,7 @@ using StructBlockTest = ResolverTest; TEST_F(StructBlockTest, StructUsedAsArrayElement) { auto* s = Structure("S", {Member("x", ty.i32())}, {create()}); - auto* a = ty.array(s, 4); + auto a = ty.array(s, 4); Global("G", a, ast::StorageClass::kPrivate); EXPECT_FALSE(r()->Resolve()); diff --git a/src/resolver/intrinsic_test.cc b/src/resolver/intrinsic_test.cc index 3b21484677..846461e606 100644 --- a/src/resolver/intrinsic_test.cc +++ b/src/resolver/intrinsic_test.cc @@ -759,7 +759,7 @@ INSTANTIATE_TEST_SUITE_P( using ResolverIntrinsicDataTest = ResolverTest; TEST_F(ResolverIntrinsicDataTest, ArrayLength_Vector) { - auto* ary = ty.array(); + auto ary = ty.array(); auto* str = Structure("S", {Member("x", ary)}, {create()}); auto* ac = ty.access(ast::AccessControl::kReadOnly, str); diff --git a/src/resolver/storage_class_validation_test.cc b/src/resolver/storage_class_validation_test.cc index 1ce3cbf5c0..3642c73a63 100644 --- a/src/resolver/storage_class_validation_test.cc +++ b/src/resolver/storage_class_validation_test.cc @@ -61,7 +61,7 @@ TEST_F(ResolverStorageClassValidationTest, StorageBufferPointer) { TEST_F(ResolverStorageClassValidationTest, StorageBufferArray) { // var g : [[access(read)]] array; auto* s = Structure("S", {Member("a", ty.f32())}); - auto* a = ty.array(s, 3); + auto a = ty.array(s, 3); auto* ac = ty.access(ast::AccessControl::kReadOnly, a); Global(Source{{56, 78}}, "g", ac, ast::StorageClass::kStorage); @@ -166,7 +166,7 @@ TEST_F(ResolverStorageClassValidationTest, UniformBufferPointer) { TEST_F(ResolverStorageClassValidationTest, UniformBufferArray) { // var g : [[access(read)]] array; auto* s = Structure("S", {Member("a", ty.f32())}); - auto* a = ty.array(s, 3); + auto a = ty.array(s, 3); auto* ac = ty.access(ast::AccessControl::kReadOnly, a); Global(Source{{56, 78}}, "g", ac, ast::StorageClass::kUniform); diff --git a/src/resolver/struct_layout_test.cc b/src/resolver/struct_layout_test.cc index 6cf4851251..782875ceea 100644 --- a/src/resolver/struct_layout_test.cc +++ b/src/resolver/struct_layout_test.cc @@ -168,8 +168,8 @@ TEST_F(ResolverStructLayoutTest, ExplicitStrideArrayRuntimeSized) { } TEST_F(ResolverStructLayoutTest, ImplicitStrideArrayOfExplicitStrideArray) { - auto* inner = ty.array(/*stride*/ 16); // size: 32 - auto* outer = ty.array(inner, 12); // size: 12 * 32 + auto inner = ty.array(/*stride*/ 16); // size: 32 + auto outer = ty.array(inner, 12); // size: 12 * 32 auto* s = Structure("S", { Member("c", outer), }); @@ -193,7 +193,7 @@ TEST_F(ResolverStructLayoutTest, ImplicitStrideArrayOfStructure) { Member("b", ty.vec3()), Member("c", ty.vec4()), }); // size: 48 - auto* outer = ty.array(inner, 12); // size: 12 * 48 + auto outer = ty.array(inner, 12); // size: 12 * 48 auto* s = Structure("S", { Member("c", outer), }); diff --git a/src/resolver/struct_storage_class_use_test.cc b/src/resolver/struct_storage_class_use_test.cc index a34a835820..4f0eb11b7f 100644 --- a/src/resolver/struct_storage_class_use_test.cc +++ b/src/resolver/struct_storage_class_use_test.cc @@ -104,7 +104,7 @@ TEST_F(ResolverStorageClassUseTest, StructReachableViaGlobalStruct) { TEST_F(ResolverStorageClassUseTest, StructReachableViaGlobalArray) { auto* s = Structure("S", {Member("a", ty.f32())}); - auto* a = ty.array(s, 3); + auto a = ty.array(s, 3); Global("g", a, ast::StorageClass::kPrivate); ASSERT_TRUE(r()->Resolve()) << r()->error(); @@ -156,7 +156,7 @@ TEST_F(ResolverStorageClassUseTest, StructReachableViaLocalStruct) { TEST_F(ResolverStorageClassUseTest, StructReachableViaLocalArray) { auto* s = Structure("S", {Member("a", ty.f32())}); - auto* a = ty.array(s, 3); + auto a = ty.array(s, 3); WrapInFunction(Var("g", a, ast::StorageClass::kFunction)); ASSERT_TRUE(r()->Resolve()) << r()->error(); diff --git a/src/transform/spirv.cc b/src/transform/spirv.cc index 1394d41e91..c209c88bbe 100644 --- a/src/transform/spirv.cc +++ b/src/transform/spirv.cc @@ -220,7 +220,7 @@ void Spirv::HandleSampleMaskBuiltins(CloneContext& ctx) const { // Use the same name as the old variable. auto var_name = ctx.Clone(var->symbol()); // Use `array` for the new variable. - auto* type = ctx.dst->ty.array(ctx.dst->ty.u32(), 1u); + auto type = ctx.dst->ty.array(ctx.dst->ty.u32(), 1u); // Create the new variable. auto* var_arr = ctx.dst->Var(var->source(), var_name, type, var->declared_storage_class(), nullptr, diff --git a/src/typepair.h b/src/typepair.h index 9a5ee20d65..89c75872ce 100644 --- a/src/typepair.h +++ b/src/typepair.h @@ -22,6 +22,7 @@ namespace tint { namespace ast { +class Array; class Bool; class F32; class I32; @@ -32,6 +33,7 @@ class Void; } // namespace ast namespace sem { +class ArrayType; class Bool; class F32; class I32; @@ -63,8 +65,15 @@ struct TypePair { /// Constructor TypePair() = default; + /// Copy constructor + /// @param other the TypePair to copy + template + TypePair(const TypePair& other) + : ast(static_cast(other.ast)), + sem(static_cast(other.sem)) {} /// Constructor /// @param a the ast::Type pointer + template TypePair(const AST* a) : ast(a) {} // NOLINT: explicit /// Constructor /// @param s the sem::Type pointer @@ -84,6 +93,7 @@ struct TypePair { using Type = TypePair; +using Array = TypePair; using Bool = TypePair; using F32 = TypePair; using I32 = TypePair; diff --git a/src/writer/hlsl/generator_impl_type_test.cc b/src/writer/hlsl/generator_impl_type_test.cc index f24dd4df9f..68556cc6a9 100644 --- a/src/writer/hlsl/generator_impl_type_test.cc +++ b/src/writer/hlsl/generator_impl_type_test.cc @@ -43,7 +43,7 @@ TEST_F(HlslGeneratorImplTest_Type, EmitType_Alias) { } TEST_F(HlslGeneratorImplTest_Type, EmitType_Array) { - auto* arr = ty.array(); + auto arr = ty.array(); GeneratorImpl& gen = Build(); @@ -53,7 +53,7 @@ TEST_F(HlslGeneratorImplTest_Type, EmitType_Array) { } TEST_F(HlslGeneratorImplTest_Type, EmitType_ArrayOfArray) { - auto* arr = ty.array(ty.array(), 5); + auto arr = ty.array(ty.array(), 5); GeneratorImpl& gen = Build(); @@ -65,7 +65,7 @@ TEST_F(HlslGeneratorImplTest_Type, EmitType_ArrayOfArray) { // TODO(dsinclair): Is this possible? What order should it output in? TEST_F(HlslGeneratorImplTest_Type, DISABLED_EmitType_ArrayOfArrayOfRuntimeArray) { - auto* arr = ty.array(ty.array(ty.array(), 5), 0); + auto arr = ty.array(ty.array(ty.array(), 5), 0); GeneratorImpl& gen = Build(); @@ -75,7 +75,7 @@ TEST_F(HlslGeneratorImplTest_Type, } TEST_F(HlslGeneratorImplTest_Type, EmitType_ArrayOfArrayOfArray) { - auto* arr = ty.array(ty.array(ty.array(), 5), 6); + auto arr = ty.array(ty.array(ty.array(), 5), 6); GeneratorImpl& gen = Build(); @@ -85,7 +85,7 @@ TEST_F(HlslGeneratorImplTest_Type, EmitType_ArrayOfArrayOfArray) { } TEST_F(HlslGeneratorImplTest_Type, EmitType_Array_WithoutName) { - auto* arr = ty.array(); + auto arr = ty.array(); GeneratorImpl& gen = Build(); @@ -95,7 +95,7 @@ TEST_F(HlslGeneratorImplTest_Type, EmitType_Array_WithoutName) { } TEST_F(HlslGeneratorImplTest_Type, DISABLED_EmitType_RuntimeArray) { - auto* arr = ty.array(); + auto arr = ty.array(); GeneratorImpl& gen = Build(); diff --git a/src/writer/msl/generator_impl_type_test.cc b/src/writer/msl/generator_impl_type_test.cc index 33aefd0d7d..1d2842ae4f 100644 --- a/src/writer/msl/generator_impl_type_test.cc +++ b/src/writer/msl/generator_impl_type_test.cc @@ -67,7 +67,7 @@ TEST_F(MslGeneratorImplTest, EmitType_Alias) { } TEST_F(MslGeneratorImplTest, EmitType_Array) { - auto* arr = ty.array(); + auto arr = ty.array(); GeneratorImpl& gen = Build(); @@ -76,8 +76,8 @@ TEST_F(MslGeneratorImplTest, EmitType_Array) { } TEST_F(MslGeneratorImplTest, EmitType_ArrayOfArray) { - auto* a = ty.array(); - auto* b = ty.array(a, 5); + auto a = ty.array(); + auto b = ty.array(a, 5); GeneratorImpl& gen = Build(); @@ -87,9 +87,9 @@ TEST_F(MslGeneratorImplTest, EmitType_ArrayOfArray) { // TODO(dsinclair): Is this possible? What order should it output in? TEST_F(MslGeneratorImplTest, DISABLED_EmitType_ArrayOfArrayOfRuntimeArray) { - auto* a = ty.array(); - auto* b = ty.array(a, 5); - auto* c = ty.array(b, 0); + auto a = ty.array(); + auto b = ty.array(a, 5); + auto c = ty.array(b, 0); GeneratorImpl& gen = Build(); @@ -98,9 +98,9 @@ TEST_F(MslGeneratorImplTest, DISABLED_EmitType_ArrayOfArrayOfRuntimeArray) { } TEST_F(MslGeneratorImplTest, EmitType_ArrayOfArrayOfArray) { - auto* a = ty.array(); - auto* b = ty.array(a, 5); - auto* c = ty.array(b, 6); + auto a = ty.array(); + auto b = ty.array(a, 5); + auto c = ty.array(b, 6); GeneratorImpl& gen = Build(); @@ -109,7 +109,7 @@ TEST_F(MslGeneratorImplTest, EmitType_ArrayOfArrayOfArray) { } TEST_F(MslGeneratorImplTest, EmitType_Array_WithoutName) { - auto* arr = ty.array(); + auto arr = ty.array(); GeneratorImpl& gen = Build(); @@ -118,7 +118,7 @@ TEST_F(MslGeneratorImplTest, EmitType_Array_WithoutName) { } TEST_F(MslGeneratorImplTest, EmitType_RuntimeArray) { - auto* arr = ty.array(); + auto arr = ty.array(); GeneratorImpl& gen = Build(); @@ -408,13 +408,13 @@ TEST_F(MslGeneratorImplTest, EmitType_Struct_Layout_ArrayDefaultStride) { }); // array_x: size(28), align(4) - auto* array_x = ty.array(); + auto array_x = ty.array(); // array_y: size(4096), align(512) - auto* array_y = ty.array(inner, 4); + auto array_y = ty.array(inner, 4); // array_z: size(4), align(4) - auto* array_z = ty.array(); + auto array_z = ty.array(); auto* s = Structure("S", diff --git a/src/writer/spirv/builder_accessor_expression_test.cc b/src/writer/spirv/builder_accessor_expression_test.cc index e782ea6017..1872c68134 100644 --- a/src/writer/spirv/builder_accessor_expression_test.cc +++ b/src/writer/spirv/builder_accessor_expression_test.cc @@ -135,7 +135,7 @@ TEST_F(BuilderTest, ArrayAccessor_Dynamic) { } TEST_F(BuilderTest, ArrayAccessor_MultiLevel) { - auto* ary4 = ty.array(ty.vec3(), 4); + auto ary4 = ty.array(ty.vec3(), 4); // ary = array, 4> // ary[3][2]; @@ -173,7 +173,7 @@ TEST_F(BuilderTest, ArrayAccessor_MultiLevel) { } TEST_F(BuilderTest, Accessor_ArrayWithSwizzle) { - auto* ary4 = ty.array(ty.vec3(), 4); + auto ary4 = ty.array(ty.vec3(), 4); // var a : array, 4>; // a[2].xy; @@ -696,10 +696,10 @@ TEST_F(BuilderTest, Accessor_Mixed_ArrayAndMember) { auto* c_type = Structure("C", {Member("baz", ty.vec3())}); auto* b_type = Structure("B", {Member("bar", c_type)}); - auto* b_ary_type = ty.array(b_type, 3); + auto b_ary_type = ty.array(b_type, 3); auto* a_type = Structure("A", {Member("foo", b_ary_type)}); - auto* a_ary_type = ty.array(a_type, 2); + auto a_ary_type = ty.array(a_type, 2); auto* var = Global("index", a_ary_type, ast::StorageClass::kFunction); auto* expr = MemberAccessor( MemberAccessor( @@ -754,7 +754,7 @@ TEST_F(BuilderTest, Accessor_Array_Of_Vec) { // vec2(0.5, -0.5)); // pos[1] - auto* arr = ty.array(ty.vec2(), 3); + auto arr = ty.array(ty.vec2(), 3); auto* var = GlobalConst("pos", arr, diff --git a/src/writer/spirv/builder_type_test.cc b/src/writer/spirv/builder_type_test.cc index 2f4a6d32a0..66fadbd4b6 100644 --- a/src/writer/spirv/builder_type_test.cc +++ b/src/writer/spirv/builder_type_test.cc @@ -58,7 +58,7 @@ TEST_F(BuilderTest_Type, ReturnsGeneratedAlias) { } TEST_F(BuilderTest_Type, GenerateRuntimeArray) { - auto* ary = ty.array(ty.i32(), 0); + auto ary = ty.array(ty.i32(), 0); auto* str = Structure("S", {Member("x", ary)}, {create()}); auto* ac = ty.access(ast::AccessControl::kReadOnly, str); @@ -76,7 +76,7 @@ TEST_F(BuilderTest_Type, GenerateRuntimeArray) { } TEST_F(BuilderTest_Type, ReturnsGeneratedRuntimeArray) { - auto* ary = ty.array(ty.i32(), 0); + auto ary = ty.array(ty.i32(), 0); auto* str = Structure("S", {Member("x", ary)}, {create()}); auto* ac = ty.access(ast::AccessControl::kReadOnly, str); @@ -94,7 +94,7 @@ TEST_F(BuilderTest_Type, ReturnsGeneratedRuntimeArray) { } TEST_F(BuilderTest_Type, GenerateArray) { - auto* ary = ty.array(ty.i32(), 4); + auto ary = ty.array(ty.i32(), 4); Global("a", ary, ast::StorageClass::kInput); spirv::Builder& b = Build(); @@ -111,7 +111,7 @@ TEST_F(BuilderTest_Type, GenerateArray) { } TEST_F(BuilderTest_Type, GenerateArray_WithStride) { - auto* ary = ty.array(ty.i32(), 4, 16u); + auto ary = ty.array(ty.i32(), 4, 16u); Global("a", ary, ast::StorageClass::kInput); spirv::Builder& b = Build(); @@ -131,7 +131,7 @@ TEST_F(BuilderTest_Type, GenerateArray_WithStride) { } TEST_F(BuilderTest_Type, ReturnsGeneratedArray) { - auto* ary = ty.array(ty.i32(), 4); + auto ary = ty.array(ty.i32(), 4); Global("a", ary, ast::StorageClass::kInput); spirv::Builder& b = Build(); @@ -445,9 +445,9 @@ TEST_F(BuilderTest_Type, GenerateStruct_DecoratedMembers_LayoutArraysOfMatrix) { // We have to infer layout for matrix when it also has an offset. // The decoration goes on the struct member, even if the matrix is buried // in levels of arrays. - auto* arr_mat2x2 = ty.array(ty.mat2x2(), 1); // Singly nested array - auto* arr_arr_mat2x3 = ty.array(ty.mat2x3(), 1); // Doubly nested array - auto* rtarr_mat4x4 = ty.array(ty.mat4x4(), 0); // Runtime array + auto arr_mat2x2 = ty.array(ty.mat2x2(), 1); // Singly nested array + auto arr_arr_mat2x3 = ty.array(ty.mat2x3(), 1); // Doubly nested array + auto rtarr_mat4x4 = ty.array(ty.mat4x4(), 0); // Runtime array auto* s = Structure("S", diff --git a/src/writer/wgsl/generator_impl_type_test.cc b/src/writer/wgsl/generator_impl_type_test.cc index c4f3e4acc8..3733af7fac 100644 --- a/src/writer/wgsl/generator_impl_type_test.cc +++ b/src/writer/wgsl/generator_impl_type_test.cc @@ -37,7 +37,7 @@ TEST_F(WgslGeneratorImplTest, EmitType_Alias) { } TEST_F(WgslGeneratorImplTest, EmitType_Array) { - auto* arr = ty.array(); + auto arr = ty.array(); AST().AddConstructedType(ty.alias("make_type_reachable", arr)); GeneratorImpl& gen = Build();