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<T>` represents an explicit type, runtime-sized array
`array<T, N>` 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 <dsinclair@chromium.org>
Commit-Queue: Ben Clayton <bclayton@chromium.org>
This commit is contained in:
Ben Clayton 2022-08-01 17:04:54 +00:00 committed by Dawn LUCI CQ
parent b779b2b13d
commit bf32bd4e96
3 changed files with 28 additions and 7 deletions

View File

@ -56,11 +56,14 @@ std::string Array::FriendlyName(const SymbolTable& symbols) const {
out << "@stride(" << stride->stride << ") "; out << "@stride(" << stride->stride << ") ";
} }
} }
out << "array<" << type->FriendlyName(symbols); out << "array";
if (!IsRuntimeArray()) { if (type) {
out << "<" << type->FriendlyName(symbols);
if (count) {
out << ", " << SizeExprToString(count, symbols); out << ", " << SizeExprToString(count, symbols);
} }
out << ">"; out << ">";
}
return out.str(); return out.str();
} }

View File

@ -35,9 +35,9 @@ class Array final : public Castable<Array, Type> {
/// @param nid the unique node identifier /// @param nid the unique node identifier
/// @param src the source of this node /// @param src the source of this node
/// @param subtype the type of the array elements /// @param subtype the type of the array elements
/// @param count the number of elements in the array. nullptr represents a /// @param count the number of elements in the array
/// runtime-sized array.
/// @param attributes the array attributes /// @param attributes the array attributes
/// @note a runtime-sized array is represented by a null count and a non-null type
Array(ProgramID pid, Array(ProgramID pid,
NodeID nid, NodeID nid,
const Source& src, const Source& src,
@ -50,7 +50,7 @@ class Array final : public Castable<Array, Type> {
/// @returns true if this is a runtime array. /// @returns true if this is a runtime array.
/// i.e. the size is determined at runtime /// 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 /// @param symbols the program's symbol table
/// @returns the name for this type that closely resembles how it would be /// @returns the name for this type that closely resembles how it would be

View File

@ -42,6 +42,14 @@ TEST_F(AstArrayTest, CreateRuntimeArray) {
EXPECT_TRUE(arr->IsRuntimeArray()); EXPECT_TRUE(arr->IsRuntimeArray());
} }
TEST_F(AstArrayTest, CreateInferredTypeArray) {
auto* arr = create<Array>(nullptr, nullptr, AttributeList{});
EXPECT_EQ(arr->type, nullptr);
EXPECT_EQ(arr->count, nullptr);
EXPECT_TRUE(arr->Is<Array>());
EXPECT_FALSE(arr->IsRuntimeArray());
}
TEST_F(AstArrayTest, FriendlyName_RuntimeSized) { TEST_F(AstArrayTest, FriendlyName_RuntimeSized) {
auto* i32 = create<I32>(); auto* i32 = create<I32>();
auto* arr = create<Array>(i32, nullptr, AttributeList{}); auto* arr = create<Array>(i32, nullptr, AttributeList{});
@ -66,5 +74,15 @@ TEST_F(AstArrayTest, FriendlyName_WithStride) {
EXPECT_EQ(arr->FriendlyName(Symbols()), "@stride(32) array<i32, 5>"); EXPECT_EQ(arr->FriendlyName(Symbols()), "@stride(32) array<i32, 5>");
} }
TEST_F(AstArrayTest, FriendlyName_InferredTypeAndCount) {
auto* arr = create<Array>(nullptr, nullptr, AttributeList{});
EXPECT_EQ(arr->FriendlyName(Symbols()), "array");
}
TEST_F(AstArrayTest, FriendlyName_InferredTypeAndCount_WithStrize) {
auto* arr = create<Array>(nullptr, nullptr, AttributeList{create<StrideAttribute>(32u)});
EXPECT_EQ(arr->FriendlyName(Symbols()), "@stride(32) array");
}
} // namespace } // namespace
} // namespace tint::ast } // namespace tint::ast