blob: ea146c918c99e4b2f8fcbf10e41a7c96b3874380 [file] [log] [blame]
// Copyright 2020 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/ast/alias.h"
#include "src/tint/ast/array.h"
#include "src/tint/ast/matrix.h"
#include "src/tint/ast/sampler.h"
#include "src/tint/reader/wgsl/parser_impl_test_helper.h"
#include "src/tint/type/sampled_texture.h"
namespace tint::reader::wgsl {
namespace {
TEST_F(ParserImplTest, TypeDeclWithoutIdent_Invalid) {
auto p = parser("1234");
auto t = p->type_specifier_without_ident();
EXPECT_EQ(t.errored, false);
EXPECT_EQ(t.matched, false);
EXPECT_EQ(t.value, nullptr);
EXPECT_FALSE(p->has_error()) << p->error();
}
TEST_F(ParserImplTest, TypeDeclWithoutIdent_Identifier) {
auto p = parser("A");
auto t = p->type_specifier_without_ident();
EXPECT_FALSE(t.matched);
EXPECT_FALSE(t.errored);
ASSERT_EQ(t.value, nullptr);
EXPECT_FALSE(p->has_error()) << p->error();
}
TEST_F(ParserImplTest, TypeDeclWithoutIdent_Bool) {
auto p = parser("bool");
auto t = p->type_specifier_without_ident();
EXPECT_TRUE(t.matched);
EXPECT_FALSE(t.errored);
ASSERT_NE(t.value, nullptr) << p->error();
ASSERT_TRUE(t.value->Is<ast::Bool>());
EXPECT_EQ(t.value->source.range, (Source::Range{{1u, 1u}, {1u, 5u}}));
}
TEST_F(ParserImplTest, TypeDeclWithoutIdent_F16) {
auto p = parser("f16");
auto t = p->type_specifier_without_ident();
EXPECT_TRUE(t.matched);
EXPECT_FALSE(t.errored);
ASSERT_NE(t.value, nullptr) << p->error();
ASSERT_TRUE(t.value->Is<ast::F16>());
EXPECT_EQ(t.value->source.range, (Source::Range{{1u, 1u}, {1u, 4u}}));
}
TEST_F(ParserImplTest, TypeDeclWithoutIdent_F32) {
auto p = parser("f32");
auto t = p->type_specifier_without_ident();
EXPECT_TRUE(t.matched);
EXPECT_FALSE(t.errored);
ASSERT_NE(t.value, nullptr) << p->error();
ASSERT_TRUE(t.value->Is<ast::F32>());
EXPECT_EQ(t.value->source.range, (Source::Range{{1u, 1u}, {1u, 4u}}));
}
TEST_F(ParserImplTest, TypeDeclWithoutIdent_I32) {
auto p = parser("i32");
auto t = p->type_specifier_without_ident();
EXPECT_TRUE(t.matched);
EXPECT_FALSE(t.errored);
ASSERT_NE(t.value, nullptr) << p->error();
ASSERT_TRUE(t.value->Is<ast::I32>());
EXPECT_EQ(t.value->source.range, (Source::Range{{1u, 1u}, {1u, 4u}}));
}
TEST_F(ParserImplTest, TypeDeclWithoutIdent_U32) {
auto p = parser("u32");
auto t = p->type_specifier_without_ident();
EXPECT_TRUE(t.matched);
EXPECT_FALSE(t.errored);
ASSERT_NE(t.value, nullptr) << p->error();
ASSERT_TRUE(t.value->Is<ast::U32>());
EXPECT_EQ(t.value->source.range, (Source::Range{{1u, 1u}, {1u, 4u}}));
}
struct VecData {
const char* input;
size_t count;
Source::Range range;
};
inline std::ostream& operator<<(std::ostream& out, VecData data) {
out << std::string(data.input);
return out;
}
class TypeDeclWithoutIdent_VecTest : public ParserImplTestWithParam<VecData> {};
TEST_P(TypeDeclWithoutIdent_VecTest, Parse) {
auto params = GetParam();
auto p = parser(params.input);
auto t = p->type_specifier_without_ident();
EXPECT_TRUE(t.matched);
EXPECT_FALSE(t.errored);
ASSERT_NE(t.value, nullptr) << p->error();
ASSERT_FALSE(p->has_error());
EXPECT_TRUE(t.value->Is<ast::Vector>());
EXPECT_EQ(t.value->As<ast::Vector>()->width, params.count);
EXPECT_EQ(t.value->source.range, params.range);
}
INSTANTIATE_TEST_SUITE_P(ParserImplTest,
TypeDeclWithoutIdent_VecTest,
testing::Values(VecData{"vec2<f32>", 2, {{1u, 1u}, {1u, 10u}}},
VecData{"vec3<f32>", 3, {{1u, 1u}, {1u, 10u}}},
VecData{"vec4<f32>", 4, {{1u, 1u}, {1u, 10u}}}));
class TypeDeclWithoutIdent_VecMissingGreaterThanTest : public ParserImplTestWithParam<VecData> {};
TEST_P(TypeDeclWithoutIdent_VecMissingGreaterThanTest, Handles_Missing_GreaterThan) {
auto params = GetParam();
auto p = parser(params.input);
auto t = p->type_specifier_without_ident();
EXPECT_TRUE(t.errored);
EXPECT_FALSE(t.matched);
ASSERT_EQ(t.value, nullptr);
ASSERT_TRUE(p->has_error());
ASSERT_EQ(p->error(), "1:5: missing closing '>' for vector");
}
INSTANTIATE_TEST_SUITE_P(ParserImplTest,
TypeDeclWithoutIdent_VecMissingGreaterThanTest,
testing::Values(VecData{"vec2<f32", 2, {}},
VecData{"vec3<f32", 3, {}},
VecData{"vec4<f32", 4, {}}));
class TypeDeclWithoutIdent_VecMissingType : public ParserImplTestWithParam<VecData> {};
TEST_P(TypeDeclWithoutIdent_VecMissingType, Handles_Missing_Type) {
auto params = GetParam();
auto p = parser(params.input);
auto t = p->type_specifier_without_ident();
EXPECT_TRUE(t.errored);
EXPECT_FALSE(t.matched);
ASSERT_EQ(t.value, nullptr);
ASSERT_TRUE(p->has_error());
ASSERT_EQ(p->error(), "1:6: invalid type for vector");
}
INSTANTIATE_TEST_SUITE_P(ParserImplTest,
TypeDeclWithoutIdent_VecMissingType,
testing::Values(VecData{"vec2<>", 2, {}},
VecData{"vec3<>", 3, {}},
VecData{"vec4<>", 4, {}}));
TEST_F(ParserImplTest, TypeDeclWithoutIdent_Ptr) {
auto p = parser("ptr<function, f32>");
auto t = p->type_specifier_without_ident();
EXPECT_TRUE(t.matched);
EXPECT_FALSE(t.errored);
ASSERT_NE(t.value, nullptr) << p->error();
ASSERT_FALSE(p->has_error());
ASSERT_TRUE(t.value->Is<ast::Pointer>());
auto* ptr = t.value->As<ast::Pointer>();
ASSERT_TRUE(ptr->type->Is<ast::F32>());
ASSERT_EQ(ptr->address_space, type::AddressSpace::kFunction);
EXPECT_EQ(t.value->source.range, (Source::Range{{1u, 1u}, {1u, 19u}}));
}
TEST_F(ParserImplTest, TypeDeclWithoutIdent_Ptr_WithAccess) {
auto p = parser("ptr<function, f32, read>");
auto t = p->type_specifier_without_ident();
EXPECT_TRUE(t.matched);
EXPECT_FALSE(t.errored);
ASSERT_NE(t.value, nullptr) << p->error();
ASSERT_FALSE(p->has_error());
ASSERT_TRUE(t.value->Is<ast::Pointer>());
auto* ptr = t.value->As<ast::Pointer>();
ASSERT_TRUE(ptr->type->Is<ast::F32>());
ASSERT_EQ(ptr->address_space, type::AddressSpace::kFunction);
ASSERT_EQ(ptr->access, type::Access::kRead);
EXPECT_EQ(t.value->source.range, (Source::Range{{1u, 1u}, {1u, 25u}}));
}
TEST_F(ParserImplTest, TypeDeclWithoutIdent_Ptr_ToVec) {
auto p = parser("ptr<function, vec2<f32>>");
auto t = p->type_specifier_without_ident();
EXPECT_TRUE(t.matched);
EXPECT_FALSE(t.errored);
ASSERT_NE(t.value, nullptr) << p->error();
ASSERT_FALSE(p->has_error());
ASSERT_TRUE(t.value->Is<ast::Pointer>());
auto* ptr = t.value->As<ast::Pointer>();
ASSERT_TRUE(ptr->type->Is<ast::Vector>());
ASSERT_EQ(ptr->address_space, type::AddressSpace::kFunction);
auto* vec = ptr->type->As<ast::Vector>();
ASSERT_EQ(vec->width, 2u);
ASSERT_TRUE(vec->type->Is<ast::F32>());
EXPECT_EQ(t.value->source.range, (Source::Range{{1u, 1u}, {1u, 25}}));
}
TEST_F(ParserImplTest, TypeDeclWithoutIdent_Ptr_MissingLessThan) {
auto p = parser("ptr private, f32>");
auto t = p->type_specifier_without_ident();
EXPECT_TRUE(t.errored);
EXPECT_FALSE(t.matched);
ASSERT_EQ(t.value, nullptr);
ASSERT_TRUE(p->has_error());
ASSERT_EQ(p->error(), "1:5: expected '<' for ptr declaration");
}
TEST_F(ParserImplTest, TypeDeclWithoutIdent_Ptr_MissingGreaterThanAfterType) {
auto p = parser("ptr<function, f32");
auto t = p->type_specifier_without_ident();
EXPECT_TRUE(t.errored);
EXPECT_FALSE(t.matched);
ASSERT_EQ(t.value, nullptr);
ASSERT_TRUE(p->has_error());
ASSERT_EQ(p->error(), "1:4: missing closing '>' for ptr declaration");
}
TEST_F(ParserImplTest, TypeDeclWithoutIdent_Ptr_MissingGreaterThanAfterAccess) {
auto p = parser("ptr<function, f32, read");
auto t = p->type_specifier_without_ident();
EXPECT_TRUE(t.errored);
EXPECT_FALSE(t.matched);
ASSERT_EQ(t.value, nullptr);
ASSERT_TRUE(p->has_error());
ASSERT_EQ(p->error(), "1:4: missing closing '>' for ptr declaration");
}
TEST_F(ParserImplTest, TypeDeclWithoutIdent_Ptr_MissingCommaAfterAddressSpace) {
auto p = parser("ptr<function f32>");
auto t = p->type_specifier_without_ident();
EXPECT_TRUE(t.errored);
EXPECT_FALSE(t.matched);
ASSERT_EQ(t.value, nullptr);
ASSERT_TRUE(p->has_error());
ASSERT_EQ(p->error(), "1:14: expected ',' for ptr declaration");
}
TEST_F(ParserImplTest, TypeDeclWithoutIdent_Ptr_MissingCommaAfterAccess) {
auto p = parser("ptr<function, f32 read>");
auto t = p->type_specifier_without_ident();
EXPECT_TRUE(t.errored);
EXPECT_FALSE(t.matched);
ASSERT_EQ(t.value, nullptr);
ASSERT_TRUE(p->has_error());
ASSERT_EQ(p->error(), "1:19: expected '>' for ptr declaration");
}
TEST_F(ParserImplTest, TypeDeclWithoutIdent_Ptr_MissingAddressSpace) {
auto p = parser("ptr<, f32>");
auto t = p->type_specifier_without_ident();
EXPECT_TRUE(t.errored);
EXPECT_FALSE(t.matched);
ASSERT_EQ(t.value, nullptr);
ASSERT_TRUE(p->has_error());
ASSERT_EQ(p->error(), R"(1:5: expected address space for ptr declaration
Possible values: 'function', 'private', 'push_constant', 'storage', 'uniform', 'workgroup')");
}
TEST_F(ParserImplTest, TypeDeclWithoutIdent_Ptr_MissingType) {
auto p = parser("ptr<function,>");
auto t = p->type_specifier_without_ident();
EXPECT_TRUE(t.errored);
EXPECT_FALSE(t.matched);
ASSERT_EQ(t.value, nullptr);
ASSERT_TRUE(p->has_error());
ASSERT_EQ(p->error(), "1:14: invalid type for ptr declaration");
}
TEST_F(ParserImplTest, TypeDeclWithoutIdent_Ptr_MissingAccess) {
auto p = parser("ptr<function, i32, >");
auto t = p->type_specifier_without_ident();
EXPECT_TRUE(t.errored);
EXPECT_FALSE(t.matched);
ASSERT_EQ(t.value, nullptr);
ASSERT_TRUE(p->has_error());
ASSERT_EQ(p->error(), R"(1:20: expected access control for ptr declaration
Possible values: 'read', 'read_write', 'write')");
}
TEST_F(ParserImplTest, TypeDeclWithoutIdent_Ptr_MissingParams) {
auto p = parser("ptr<>");
auto t = p->type_specifier_without_ident();
EXPECT_TRUE(t.errored);
EXPECT_FALSE(t.matched);
ASSERT_EQ(t.value, nullptr);
ASSERT_TRUE(p->has_error());
ASSERT_EQ(p->error(), R"(1:5: expected address space for ptr declaration
Possible values: 'function', 'private', 'push_constant', 'storage', 'uniform', 'workgroup')");
}
TEST_F(ParserImplTest, TypeDeclWithoutIdent_Ptr_BadAddressSpace) {
auto p = parser("ptr<unknown, f32>");
auto t = p->type_specifier_without_ident();
EXPECT_TRUE(t.errored);
EXPECT_FALSE(t.matched);
ASSERT_EQ(t.value, nullptr);
ASSERT_TRUE(p->has_error());
ASSERT_EQ(p->error(),
R"(1:5: expected address space for ptr declaration. Did you mean 'uniform'?
Possible values: 'function', 'private', 'push_constant', 'storage', 'uniform', 'workgroup')");
}
TEST_F(ParserImplTest, TypeDeclWithoutIdent_Ptr_BadAccess) {
auto p = parser("ptr<function, i32, unknown>");
auto t = p->type_specifier_without_ident();
EXPECT_TRUE(t.errored);
EXPECT_FALSE(t.matched);
ASSERT_EQ(t.value, nullptr);
ASSERT_TRUE(p->has_error());
ASSERT_EQ(p->error(), R"(1:20: expected access control for ptr declaration
Possible values: 'read', 'read_write', 'write')");
}
TEST_F(ParserImplTest, TypeDeclWithoutIdent_Atomic) {
auto p = parser("atomic<f32>");
auto t = p->type_specifier_without_ident();
EXPECT_TRUE(t.matched);
EXPECT_FALSE(t.errored);
ASSERT_NE(t.value, nullptr) << p->error();
ASSERT_FALSE(p->has_error());
ASSERT_TRUE(t.value->Is<ast::Atomic>());
auto* atomic = t.value->As<ast::Atomic>();
ASSERT_TRUE(atomic->type->Is<ast::F32>());
EXPECT_EQ(t.value->source.range, (Source::Range{{1u, 1u}, {1u, 12u}}));
}
TEST_F(ParserImplTest, TypeDeclWithoutIdent_Atomic_ToVec) {
auto p = parser("atomic<vec2<f32>>");
auto t = p->type_specifier_without_ident();
EXPECT_TRUE(t.matched);
EXPECT_FALSE(t.errored);
ASSERT_NE(t.value, nullptr) << p->error();
ASSERT_FALSE(p->has_error());
ASSERT_TRUE(t.value->Is<ast::Atomic>());
auto* atomic = t.value->As<ast::Atomic>();
ASSERT_TRUE(atomic->type->Is<ast::Vector>());
auto* vec = atomic->type->As<ast::Vector>();
ASSERT_EQ(vec->width, 2u);
ASSERT_TRUE(vec->type->Is<ast::F32>());
EXPECT_EQ(t.value->source.range, (Source::Range{{1u, 1u}, {1u, 18u}}));
}
TEST_F(ParserImplTest, TypeDeclWithoutIdent_Atomic_MissingLessThan) {
auto p = parser("atomic f32>");
auto t = p->type_specifier_without_ident();
EXPECT_TRUE(t.errored);
EXPECT_FALSE(t.matched);
ASSERT_EQ(t.value, nullptr);
ASSERT_TRUE(p->has_error());
ASSERT_EQ(p->error(), "1:8: expected '<' for atomic declaration");
}
TEST_F(ParserImplTest, TypeDeclWithoutIdent_Atomic_MissingGreaterThan) {
auto p = parser("atomic<f32");
auto t = p->type_specifier_without_ident();
EXPECT_TRUE(t.errored);
EXPECT_FALSE(t.matched);
ASSERT_EQ(t.value, nullptr);
ASSERT_TRUE(p->has_error());
ASSERT_EQ(p->error(), "1:7: missing closing '>' for atomic declaration");
}
TEST_F(ParserImplTest, TypeDeclWithoutIdent_Atomic_MissingType) {
auto p = parser("atomic<>");
auto t = p->type_specifier_without_ident();
EXPECT_TRUE(t.errored);
EXPECT_FALSE(t.matched);
ASSERT_EQ(t.value, nullptr);
ASSERT_TRUE(p->has_error());
ASSERT_EQ(p->error(), "1:8: invalid type for atomic declaration");
}
TEST_F(ParserImplTest, TypeDeclWithoutIdent_Array_AbstractIntLiteralSize) {
auto p = parser("array<f32, 5>");
auto t = p->type_specifier_without_ident();
EXPECT_TRUE(t.matched);
EXPECT_FALSE(t.errored);
ASSERT_NE(t.value, nullptr) << p->error();
ASSERT_FALSE(p->has_error());
ASSERT_TRUE(t.value->Is<ast::Array>());
auto* a = t.value->As<ast::Array>();
ASSERT_FALSE(a->IsRuntimeArray());
ASSERT_TRUE(a->type->Is<ast::F32>());
EXPECT_EQ(a->attributes.Length(), 0u);
EXPECT_EQ(t.value->source.range, (Source::Range{{1u, 1u}, {1u, 14u}}));
auto* size = a->count->As<ast::IntLiteralExpression>();
ASSERT_NE(size, nullptr);
EXPECT_EQ(size->value, 5);
EXPECT_EQ(size->suffix, ast::IntLiteralExpression::Suffix::kNone);
}
TEST_F(ParserImplTest, TypeDeclWithoutIdent_Array_SintLiteralSize) {
auto p = parser("array<f32, 5i>");
auto t = p->type_specifier_without_ident();
EXPECT_TRUE(t.matched);
EXPECT_FALSE(t.errored);
ASSERT_NE(t.value, nullptr) << p->error();
ASSERT_FALSE(p->has_error());
ASSERT_TRUE(t.value->Is<ast::Array>());
auto* a = t.value->As<ast::Array>();
ASSERT_FALSE(a->IsRuntimeArray());
ASSERT_TRUE(a->type->Is<ast::F32>());
EXPECT_EQ(a->attributes.Length(), 0u);
EXPECT_EQ(t.value->source.range, (Source::Range{{1u, 1u}, {1u, 15u}}));
auto* size = a->count->As<ast::IntLiteralExpression>();
ASSERT_NE(size, nullptr);
EXPECT_EQ(size->value, 5);
EXPECT_EQ(size->suffix, ast::IntLiteralExpression::Suffix::kI);
}
TEST_F(ParserImplTest, TypeDeclWithoutIdent_Array_UintLiteralSize) {
auto p = parser("array<f32, 5u>");
auto t = p->type_specifier_without_ident();
EXPECT_TRUE(t.matched);
EXPECT_FALSE(t.errored);
ASSERT_NE(t.value, nullptr) << p->error();
ASSERT_FALSE(p->has_error());
ASSERT_TRUE(t.value->Is<ast::Array>());
auto* a = t.value->As<ast::Array>();
ASSERT_FALSE(a->IsRuntimeArray());
ASSERT_TRUE(a->type->Is<ast::F32>());
EXPECT_EQ(a->attributes.Length(), 0u);
EXPECT_EQ(t.value->source.range, (Source::Range{{1u, 1u}, {1u, 15u}}));
auto* size = a->count->As<ast::IntLiteralExpression>();
ASSERT_NE(size, nullptr);
EXPECT_EQ(size->suffix, ast::IntLiteralExpression::Suffix::kU);
}
TEST_F(ParserImplTest, TypeDeclWithoutIdent_Array_ConstantSize) {
auto p = parser("array<f32, size>");
auto t = p->type_specifier_without_ident();
EXPECT_TRUE(t.matched);
EXPECT_FALSE(t.errored);
ASSERT_NE(t.value, nullptr) << p->error();
ASSERT_FALSE(p->has_error());
ASSERT_TRUE(t.value->Is<ast::Array>());
auto* a = t.value->As<ast::Array>();
ASSERT_FALSE(a->IsRuntimeArray());
ASSERT_TRUE(a->type->Is<ast::F32>());
EXPECT_EQ(a->attributes.Length(), 0u);
EXPECT_EQ(t.value->source.range, (Source::Range{{1u, 1u}, {1u, 17u}}));
auto* count_expr = a->count->As<ast::IdentifierExpression>();
ASSERT_NE(count_expr, nullptr);
EXPECT_EQ(p->builder().Symbols().NameFor(count_expr->symbol), "size");
}
TEST_F(ParserImplTest, TypeDeclWithoutIdent_Array_ExpressionSize) {
auto p = parser("array<f32, size + 2>");
auto t = p->type_specifier_without_ident();
EXPECT_TRUE(t.matched);
EXPECT_FALSE(t.errored);
ASSERT_NE(t.value, nullptr) << p->error();
ASSERT_FALSE(p->has_error());
ASSERT_TRUE(t.value->Is<ast::Array>());
auto* a = t.value->As<ast::Array>();
ASSERT_FALSE(a->IsRuntimeArray());
ASSERT_TRUE(a->type->Is<ast::F32>());
EXPECT_EQ(a->attributes.Length(), 0u);
ASSERT_TRUE(a->count->Is<ast::BinaryExpression>());
auto* count_expr = a->count->As<ast::BinaryExpression>();
EXPECT_EQ(ast::BinaryOp::kAdd, count_expr->op);
ASSERT_TRUE(count_expr->lhs->Is<ast::IdentifierExpression>());
auto* ident = count_expr->lhs->As<ast::IdentifierExpression>();
EXPECT_EQ(p->builder().Symbols().NameFor(ident->symbol), "size");
ASSERT_TRUE(count_expr->rhs->Is<ast::IntLiteralExpression>());
auto* val = count_expr->rhs->As<ast::IntLiteralExpression>();
EXPECT_EQ(2, static_cast<int32_t>(val->value));
}
TEST_F(ParserImplTest, TypeDeclWithoutIdent_Array_Runtime) {
auto p = parser("array<u32>");
auto t = p->type_specifier_without_ident();
EXPECT_TRUE(t.matched);
EXPECT_FALSE(t.errored);
ASSERT_NE(t.value, nullptr) << p->error();
ASSERT_FALSE(p->has_error());
ASSERT_TRUE(t.value->Is<ast::Array>());
auto* a = t.value->As<ast::Array>();
ASSERT_TRUE(a->IsRuntimeArray());
ASSERT_TRUE(a->type->Is<ast::U32>());
EXPECT_EQ(t.value->source.range, (Source::Range{{1u, 1u}, {1u, 11u}}));
}
TEST_F(ParserImplTest, TypeDeclWithoutIdent_Array_Runtime_Vec) {
auto p = parser("array<vec4<u32>>");
auto t = p->type_specifier_without_ident();
EXPECT_TRUE(t.matched);
EXPECT_FALSE(t.errored);
ASSERT_NE(t.value, nullptr) << p->error();
ASSERT_FALSE(p->has_error());
ASSERT_TRUE(t.value->Is<ast::Array>());
auto* a = t.value->As<ast::Array>();
ASSERT_TRUE(a->IsRuntimeArray());
ASSERT_TRUE(a->type->Is<ast::Vector>());
EXPECT_EQ(a->type->As<ast::Vector>()->width, 4u);
EXPECT_TRUE(a->type->As<ast::Vector>()->type->Is<ast::U32>());
EXPECT_EQ(t.value->source.range, (Source::Range{{1u, 1u}, {1u, 17u}}));
}
TEST_F(ParserImplTest, TypeDeclWithoutIdent_Array_BadSize) {
auto p = parser("array<f32, !>");
auto t = p->type_specifier_without_ident();
EXPECT_TRUE(t.errored);
EXPECT_FALSE(t.matched);
ASSERT_EQ(t.value, nullptr);
ASSERT_TRUE(p->has_error());
ASSERT_EQ(p->error(), "1:13: unable to parse right side of ! expression");
}
TEST_F(ParserImplTest, TypeDeclWithoutIdent_Array_MissingSize) {
auto p = parser("array<f32,>");
auto t = p->type_specifier_without_ident();
EXPECT_TRUE(t.errored);
EXPECT_FALSE(t.matched);
ASSERT_EQ(t.value, nullptr);
ASSERT_TRUE(p->has_error());
ASSERT_EQ(p->error(), "1:11: expected array size expression");
}
TEST_F(ParserImplTest, TypeDeclWithoutIdent_Array_MissingGreaterThan) {
auto p = parser("array<f32");
auto t = p->type_specifier_without_ident();
EXPECT_TRUE(t.errored);
EXPECT_FALSE(t.matched);
ASSERT_EQ(t.value, nullptr);
ASSERT_TRUE(p->has_error());
ASSERT_EQ(p->error(), "1:6: missing closing '>' for array declaration");
}
TEST_F(ParserImplTest, TypeDeclWithoutIdent_Array_MissingComma) {
auto p = parser("array<f32 3>");
auto t = p->type_specifier_without_ident();
EXPECT_TRUE(t.errored);
EXPECT_FALSE(t.matched);
ASSERT_EQ(t.value, nullptr);
ASSERT_TRUE(p->has_error());
ASSERT_EQ(p->error(), "1:11: expected '>' for array declaration");
}
struct MatrixData {
const char* input;
size_t columns;
size_t rows;
Source::Range range;
};
inline std::ostream& operator<<(std::ostream& out, MatrixData data) {
out << std::string(data.input);
return out;
}
class TypeDeclWithoutIdent_MatrixTest : public ParserImplTestWithParam<MatrixData> {};
TEST_P(TypeDeclWithoutIdent_MatrixTest, Parse) {
auto params = GetParam();
auto p = parser(params.input);
auto t = p->type_specifier_without_ident();
EXPECT_TRUE(t.matched);
EXPECT_FALSE(t.errored);
ASSERT_NE(t.value, nullptr) << p->error();
ASSERT_FALSE(p->has_error());
EXPECT_TRUE(t.value->Is<ast::Matrix>());
auto* mat = t.value->As<ast::Matrix>();
EXPECT_EQ(mat->rows, params.rows);
EXPECT_EQ(mat->columns, params.columns);
EXPECT_EQ(t.value->source.range, params.range);
}
INSTANTIATE_TEST_SUITE_P(ParserImplTest,
TypeDeclWithoutIdent_MatrixTest,
testing::Values(MatrixData{"mat2x2<f32>", 2, 2, {{1u, 1u}, {1u, 12u}}},
MatrixData{"mat2x3<f32>", 2, 3, {{1u, 1u}, {1u, 12u}}},
MatrixData{"mat2x4<f32>", 2, 4, {{1u, 1u}, {1u, 12u}}},
MatrixData{"mat3x2<f32>", 3, 2, {{1u, 1u}, {1u, 12u}}},
MatrixData{"mat3x3<f32>", 3, 3, {{1u, 1u}, {1u, 12u}}},
MatrixData{"mat3x4<f32>", 3, 4, {{1u, 1u}, {1u, 12u}}},
MatrixData{"mat4x2<f32>", 4, 2, {{1u, 1u}, {1u, 12u}}},
MatrixData{"mat4x3<f32>", 4, 3, {{1u, 1u}, {1u, 12u}}},
MatrixData{"mat4x4<f32>", 4, 4, {{1u, 1u}, {1u, 12u}}}));
class TypeDeclWithoutIdent_MatrixMissingGreaterThanTest
: public ParserImplTestWithParam<MatrixData> {};
TEST_P(TypeDeclWithoutIdent_MatrixMissingGreaterThanTest, Handles_Missing_GreaterThan) {
auto params = GetParam();
auto p = parser(params.input);
auto t = p->type_specifier_without_ident();
EXPECT_TRUE(t.errored);
EXPECT_FALSE(t.matched);
ASSERT_EQ(t.value, nullptr);
ASSERT_TRUE(p->has_error());
ASSERT_EQ(p->error(), "1:7: missing closing '>' for matrix");
}
INSTANTIATE_TEST_SUITE_P(ParserImplTest,
TypeDeclWithoutIdent_MatrixMissingGreaterThanTest,
testing::Values(MatrixData{"mat2x2<f32", 2, 2, {}},
MatrixData{"mat2x3<f32", 2, 3, {}},
MatrixData{"mat2x4<f32", 2, 4, {}},
MatrixData{"mat3x2<f32", 3, 2, {}},
MatrixData{"mat3x3<f32", 3, 3, {}},
MatrixData{"mat3x4<f32", 3, 4, {}},
MatrixData{"mat4x2<f32", 4, 2, {}},
MatrixData{"mat4x3<f32", 4, 3, {}},
MatrixData{"mat4x4<f32", 4, 4, {}}));
class TypeDeclWithoutIdent_MatrixMissingType : public ParserImplTestWithParam<MatrixData> {};
TEST_P(TypeDeclWithoutIdent_MatrixMissingType, Handles_Missing_Type) {
auto params = GetParam();
auto p = parser(params.input);
auto t = p->type_specifier_without_ident();
EXPECT_TRUE(t.errored);
EXPECT_FALSE(t.matched);
ASSERT_EQ(t.value, nullptr);
ASSERT_TRUE(p->has_error());
ASSERT_EQ(p->error(), "1:8: invalid type for matrix");
}
INSTANTIATE_TEST_SUITE_P(ParserImplTest,
TypeDeclWithoutIdent_MatrixMissingType,
testing::Values(MatrixData{"mat2x2<>", 2, 2, {}},
MatrixData{"mat2x3<>", 2, 3, {}},
MatrixData{"mat2x4<>", 2, 4, {}},
MatrixData{"mat3x2<>", 3, 2, {}},
MatrixData{"mat3x3<>", 3, 3, {}},
MatrixData{"mat3x4<>", 3, 4, {}},
MatrixData{"mat4x2<>", 4, 2, {}},
MatrixData{"mat4x3<>", 4, 3, {}},
MatrixData{"mat4x4<>", 4, 4, {}}));
TEST_F(ParserImplTest, TypeDeclWithoutIdent_Sampler) {
auto p = parser("sampler");
auto t = p->type_specifier_without_ident();
EXPECT_TRUE(t.matched);
EXPECT_FALSE(t.errored);
ASSERT_NE(t.value, nullptr) << p->error();
ASSERT_TRUE(t.value->Is<ast::Sampler>());
ASSERT_FALSE(t.value->As<ast::Sampler>()->IsComparison());
EXPECT_EQ(t.value->source.range, (Source::Range{{1u, 1u}, {1u, 8u}}));
}
TEST_F(ParserImplTest, TypeDeclWithoutIdent_Texture) {
auto p = parser("texture_cube<f32>");
auto t = p->type_specifier_without_ident();
EXPECT_TRUE(t.matched);
EXPECT_FALSE(t.errored);
ASSERT_NE(t.value, nullptr);
ASSERT_TRUE(t.value->Is<ast::Texture>());
ASSERT_TRUE(t.value->Is<ast::SampledTexture>());
ASSERT_TRUE(t.value->As<ast::SampledTexture>()->type->Is<ast::F32>());
EXPECT_EQ(t.value->source.range, (Source::Range{{1u, 1u}, {1u, 18u}}));
}
} // namespace
} // namespace tint::reader::wgsl