From bf32bd4e961bf82a9246d0e74b863e66073279fe Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Mon, 1 Aug 2022 17:04:54 +0000 Subject: [PATCH] tint/ast: Tighten AST definition of runtime-sized array Just checking whether the count is nullptr is not sufficient to know whether the array is fixed-size: `array` represents a infered type, infered fixed-size array `array` represents an explicit type, runtime-sized array `array` represents an explicit type, explicit fixed-size array Bug: tint:1628 Change-Id: I03f547078c015d1d2a8a38308bb5e41c733715ec Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/97589 Reviewed-by: Dan Sinclair Commit-Queue: Ben Clayton --- src/tint/ast/array.cc | 11 +++++++---- src/tint/ast/array.h | 6 +++--- src/tint/ast/array_test.cc | 18 ++++++++++++++++++ 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/tint/ast/array.cc b/src/tint/ast/array.cc index cd1fc26783..988dfaf031 100644 --- a/src/tint/ast/array.cc +++ b/src/tint/ast/array.cc @@ -56,11 +56,14 @@ std::string Array::FriendlyName(const SymbolTable& symbols) const { out << "@stride(" << stride->stride << ") "; } } - out << "array<" << type->FriendlyName(symbols); - if (!IsRuntimeArray()) { - out << ", " << SizeExprToString(count, symbols); + out << "array"; + if (type) { + out << "<" << type->FriendlyName(symbols); + if (count) { + out << ", " << SizeExprToString(count, symbols); + } + out << ">"; } - out << ">"; return out.str(); } diff --git a/src/tint/ast/array.h b/src/tint/ast/array.h index ccc31ab229..6ad1cbc468 100644 --- a/src/tint/ast/array.h +++ b/src/tint/ast/array.h @@ -35,9 +35,9 @@ class Array final : public Castable { /// @param nid the unique node identifier /// @param src the source of this node /// @param subtype the type of the array elements - /// @param count the number of elements in the array. nullptr represents a - /// runtime-sized array. + /// @param count the number of elements in the array /// @param attributes the array attributes + /// @note a runtime-sized array is represented by a null count and a non-null type Array(ProgramID pid, NodeID nid, const Source& src, @@ -50,7 +50,7 @@ class Array final : public Castable { /// @returns true if this is a runtime array. /// i.e. the size is determined at runtime - bool IsRuntimeArray() const { return count == nullptr; } + bool IsRuntimeArray() const { return type != nullptr && count == nullptr; } /// @param symbols the program's symbol table /// @returns the name for this type that closely resembles how it would be diff --git a/src/tint/ast/array_test.cc b/src/tint/ast/array_test.cc index f396e0c79d..f8d469b9ae 100644 --- a/src/tint/ast/array_test.cc +++ b/src/tint/ast/array_test.cc @@ -42,6 +42,14 @@ TEST_F(AstArrayTest, CreateRuntimeArray) { EXPECT_TRUE(arr->IsRuntimeArray()); } +TEST_F(AstArrayTest, CreateInferredTypeArray) { + auto* arr = create(nullptr, nullptr, AttributeList{}); + EXPECT_EQ(arr->type, nullptr); + EXPECT_EQ(arr->count, nullptr); + EXPECT_TRUE(arr->Is()); + EXPECT_FALSE(arr->IsRuntimeArray()); +} + TEST_F(AstArrayTest, FriendlyName_RuntimeSized) { auto* i32 = create(); auto* arr = create(i32, nullptr, AttributeList{}); @@ -66,5 +74,15 @@ TEST_F(AstArrayTest, FriendlyName_WithStride) { EXPECT_EQ(arr->FriendlyName(Symbols()), "@stride(32) array"); } +TEST_F(AstArrayTest, FriendlyName_InferredTypeAndCount) { + auto* arr = create(nullptr, nullptr, AttributeList{}); + EXPECT_EQ(arr->FriendlyName(Symbols()), "array"); +} + +TEST_F(AstArrayTest, FriendlyName_InferredTypeAndCount_WithStrize) { + auto* arr = create(nullptr, nullptr, AttributeList{create(32u)}); + EXPECT_EQ(arr->FriendlyName(Symbols()), "@stride(32) array"); +} + } // namespace } // namespace tint::ast