| // 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 "gmock/gmock.h" |
| #include "src/tint/resolver/resolver_test_helper.h" |
| #include "src/tint/sem/reference.h" |
| #include "src/tint/sem/type_constructor.h" |
| #include "src/tint/sem/type_conversion.h" |
| |
| using namespace tint::number_suffixes; // NOLINT |
| |
| namespace tint::resolver { |
| namespace { |
| |
| using ::testing::HasSubstr; |
| |
| // Helpers and typedefs |
| using builder::alias; |
| using builder::alias1; |
| using builder::alias2; |
| using builder::alias3; |
| using builder::CreatePtrs; |
| using builder::CreatePtrsFor; |
| using builder::DataType; |
| using builder::mat2x2; |
| using builder::mat2x3; |
| using builder::mat3x2; |
| using builder::mat3x3; |
| using builder::mat4x4; |
| using builder::vec2; |
| using builder::vec3; |
| using builder::vec4; |
| |
| class ResolverTypeConstructorValidationTest : public resolver::TestHelper, public testing::Test {}; |
| |
| namespace InferTypeTest { |
| struct Params { |
| builder::ast_type_func_ptr create_rhs_ast_type; |
| builder::ast_expr_func_ptr create_rhs_ast_value; |
| builder::sem_type_func_ptr create_rhs_sem_type; |
| }; |
| |
| template <typename T> |
| constexpr Params ParamsFor() { |
| return Params{DataType<T>::AST, DataType<T>::Expr, DataType<T>::Sem}; |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, InferTypeTest_Simple) { |
| // var a = 1i; |
| // var b = a; |
| auto* a = Var("a", nullptr, ast::StorageClass::kNone, Expr(1_i)); |
| auto* b = Var("b", nullptr, ast::StorageClass::kNone, Expr("a")); |
| auto* a_ident = Expr("a"); |
| auto* b_ident = Expr("b"); |
| |
| WrapInFunction(a, b, Assign(a_ident, "a"), Assign(b_ident, "b")); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| ASSERT_TRUE(TypeOf(a_ident)->Is<sem::Reference>()); |
| EXPECT_TRUE(TypeOf(a_ident)->As<sem::Reference>()->StoreType()->Is<sem::I32>()); |
| EXPECT_EQ(TypeOf(a_ident)->As<sem::Reference>()->StorageClass(), ast::StorageClass::kFunction); |
| ASSERT_TRUE(TypeOf(b_ident)->Is<sem::Reference>()); |
| EXPECT_TRUE(TypeOf(b_ident)->As<sem::Reference>()->StoreType()->Is<sem::I32>()); |
| EXPECT_EQ(TypeOf(b_ident)->As<sem::Reference>()->StorageClass(), ast::StorageClass::kFunction); |
| } |
| |
| using InferTypeTest_FromConstructorExpression = ResolverTestWithParam<Params>; |
| TEST_P(InferTypeTest_FromConstructorExpression, All) { |
| // e.g. for vec3<f32> |
| // { |
| // var a = vec3<f32>(0.0, 0.0, 0.0) |
| // } |
| auto& params = GetParam(); |
| |
| Enable(ast::Extension::kF16); |
| |
| auto* constructor_expr = params.create_rhs_ast_value(*this, 0); |
| |
| auto* a = Var("a", nullptr, ast::StorageClass::kNone, constructor_expr); |
| // Self-assign 'a' to force the expression to be resolved so we can test its |
| // type below |
| auto* a_ident = Expr("a"); |
| WrapInFunction(Decl(a), Assign(a_ident, "a")); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| auto* got = TypeOf(a_ident); |
| auto* expected = create<sem::Reference>(params.create_rhs_sem_type(*this), |
| ast::StorageClass::kFunction, ast::Access::kReadWrite); |
| ASSERT_EQ(got, expected) << "got: " << FriendlyName(got) << "\n" |
| << "expected: " << FriendlyName(expected) << "\n"; |
| } |
| |
| static constexpr Params from_constructor_expression_cases[] = { |
| ParamsFor<bool>(), |
| ParamsFor<i32>(), |
| ParamsFor<u32>(), |
| ParamsFor<f32>(), |
| ParamsFor<f16>(), |
| ParamsFor<vec3<i32>>(), |
| ParamsFor<vec3<u32>>(), |
| ParamsFor<vec3<f32>>(), |
| ParamsFor<vec3<f16>>(), |
| ParamsFor<mat3x3<f32>>(), |
| ParamsFor<mat3x3<f16>>(), |
| ParamsFor<alias<bool>>(), |
| ParamsFor<alias<i32>>(), |
| ParamsFor<alias<u32>>(), |
| ParamsFor<alias<f32>>(), |
| ParamsFor<alias<f16>>(), |
| ParamsFor<alias<vec3<i32>>>(), |
| ParamsFor<alias<vec3<u32>>>(), |
| ParamsFor<alias<vec3<f32>>>(), |
| ParamsFor<alias<vec3<f16>>>(), |
| ParamsFor<alias<mat3x3<f32>>>(), |
| ParamsFor<alias<mat3x3<f16>>>(), |
| }; |
| INSTANTIATE_TEST_SUITE_P(ResolverTypeConstructorValidationTest, |
| InferTypeTest_FromConstructorExpression, |
| testing::ValuesIn(from_constructor_expression_cases)); |
| |
| using InferTypeTest_FromArithmeticExpression = ResolverTestWithParam<Params>; |
| TEST_P(InferTypeTest_FromArithmeticExpression, All) { |
| // e.g. for vec3<f32> |
| // { |
| // var a = vec3<f32>(2.0, 2.0, 2.0) * 3.0; |
| // } |
| auto& params = GetParam(); |
| |
| auto* arith_lhs_expr = params.create_rhs_ast_value(*this, 2); |
| auto* arith_rhs_expr = params.create_rhs_ast_value(*this, 3); |
| auto* constructor_expr = Mul(arith_lhs_expr, arith_rhs_expr); |
| |
| auto* a = Var("a", nullptr, constructor_expr); |
| // Self-assign 'a' to force the expression to be resolved so we can test its |
| // type below |
| auto* a_ident = Expr("a"); |
| WrapInFunction(Decl(a), Assign(a_ident, "a")); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| auto* got = TypeOf(a_ident); |
| auto* expected = create<sem::Reference>(params.create_rhs_sem_type(*this), |
| ast::StorageClass::kFunction, ast::Access::kReadWrite); |
| ASSERT_EQ(got, expected) << "got: " << FriendlyName(got) << "\n" |
| << "expected: " << FriendlyName(expected) << "\n"; |
| } |
| static constexpr Params from_arithmetic_expression_cases[] = { |
| ParamsFor<i32>(), |
| ParamsFor<u32>(), |
| ParamsFor<f32>(), |
| ParamsFor<vec3<f32>>(), |
| ParamsFor<mat3x3<f32>>(), |
| ParamsFor<alias<i32>>(), |
| ParamsFor<alias<u32>>(), |
| ParamsFor<alias<f32>>(), |
| ParamsFor<alias<vec3<f32>>>(), |
| ParamsFor<alias<mat3x3<f32>>>(), |
| }; |
| INSTANTIATE_TEST_SUITE_P(ResolverTypeConstructorValidationTest, |
| InferTypeTest_FromArithmeticExpression, |
| testing::ValuesIn(from_arithmetic_expression_cases)); |
| |
| using InferTypeTest_FromCallExpression = ResolverTestWithParam<Params>; |
| TEST_P(InferTypeTest_FromCallExpression, All) { |
| // e.g. for vec3<f32> |
| // |
| // fn foo() -> vec3<f32> { |
| // return vec3<f32>(); |
| // } |
| // |
| // fn bar() |
| // { |
| // var a = foo(); |
| // } |
| auto& params = GetParam(); |
| |
| Enable(ast::Extension::kF16); |
| |
| Func("foo", {}, params.create_rhs_ast_type(*this), |
| {Return(Construct(params.create_rhs_ast_type(*this)))}, {}); |
| |
| auto* a = Var("a", nullptr, Call("foo")); |
| // Self-assign 'a' to force the expression to be resolved so we can test its |
| // type below |
| auto* a_ident = Expr("a"); |
| WrapInFunction(Decl(a), Assign(a_ident, "a")); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| auto* got = TypeOf(a_ident); |
| auto* expected = create<sem::Reference>(params.create_rhs_sem_type(*this), |
| ast::StorageClass::kFunction, ast::Access::kReadWrite); |
| ASSERT_EQ(got, expected) << "got: " << FriendlyName(got) << "\n" |
| << "expected: " << FriendlyName(expected) << "\n"; |
| } |
| static constexpr Params from_call_expression_cases[] = { |
| ParamsFor<bool>(), |
| ParamsFor<i32>(), |
| ParamsFor<u32>(), |
| ParamsFor<f32>(), |
| ParamsFor<f16>(), |
| ParamsFor<vec3<i32>>(), |
| ParamsFor<vec3<u32>>(), |
| ParamsFor<vec3<f32>>(), |
| ParamsFor<vec3<f16>>(), |
| ParamsFor<mat3x3<f32>>(), |
| ParamsFor<mat3x3<f16>>(), |
| ParamsFor<alias<bool>>(), |
| ParamsFor<alias<i32>>(), |
| ParamsFor<alias<u32>>(), |
| ParamsFor<alias<f32>>(), |
| ParamsFor<alias<f16>>(), |
| ParamsFor<alias<vec3<i32>>>(), |
| ParamsFor<alias<vec3<u32>>>(), |
| ParamsFor<alias<vec3<f32>>>(), |
| ParamsFor<alias<vec3<f16>>>(), |
| ParamsFor<alias<mat3x3<f32>>>(), |
| ParamsFor<alias<mat3x3<f16>>>(), |
| }; |
| INSTANTIATE_TEST_SUITE_P(ResolverTypeConstructorValidationTest, |
| InferTypeTest_FromCallExpression, |
| testing::ValuesIn(from_call_expression_cases)); |
| |
| } // namespace InferTypeTest |
| |
| namespace ConversionConstructTest { |
| enum class Kind { |
| Construct, |
| Conversion, |
| }; |
| |
| struct Params { |
| Kind kind; |
| builder::ast_type_func_ptr lhs_type; |
| builder::ast_type_func_ptr rhs_type; |
| builder::ast_expr_func_ptr rhs_value_expr; |
| }; |
| |
| template <typename LhsType, typename RhsType> |
| constexpr Params ParamsFor(Kind kind) { |
| return Params{kind, DataType<LhsType>::AST, DataType<RhsType>::AST, DataType<RhsType>::Expr}; |
| } |
| |
| static constexpr Params valid_cases[] = { |
| // Identity |
| ParamsFor<bool, bool>(Kind::Construct), // |
| ParamsFor<i32, i32>(Kind::Construct), // |
| ParamsFor<u32, u32>(Kind::Construct), // |
| ParamsFor<f32, f32>(Kind::Construct), // |
| ParamsFor<f16, f16>(Kind::Construct), // |
| ParamsFor<vec3<bool>, vec3<bool>>(Kind::Construct), // |
| ParamsFor<vec3<i32>, vec3<i32>>(Kind::Construct), // |
| ParamsFor<vec3<u32>, vec3<u32>>(Kind::Construct), // |
| ParamsFor<vec3<f32>, vec3<f32>>(Kind::Construct), // |
| ParamsFor<vec3<f16>, vec3<f16>>(Kind::Construct), // |
| ParamsFor<mat3x3<f32>, mat3x3<f32>>(Kind::Construct), // |
| ParamsFor<mat2x3<f32>, mat2x3<f32>>(Kind::Construct), // |
| ParamsFor<mat3x2<f32>, mat3x2<f32>>(Kind::Construct), // |
| ParamsFor<mat3x3<f16>, mat3x3<f16>>(Kind::Construct), // |
| ParamsFor<mat2x3<f16>, mat2x3<f16>>(Kind::Construct), // |
| ParamsFor<mat3x2<f16>, mat3x2<f16>>(Kind::Construct), // |
| |
| // Splat |
| ParamsFor<vec3<bool>, bool>(Kind::Construct), // |
| ParamsFor<vec3<i32>, i32>(Kind::Construct), // |
| ParamsFor<vec3<u32>, u32>(Kind::Construct), // |
| ParamsFor<vec3<f32>, f32>(Kind::Construct), // |
| ParamsFor<vec3<f16>, f16>(Kind::Construct), // |
| |
| ParamsFor<mat3x3<f32>, f32>(Kind::Construct), // |
| ParamsFor<mat2x3<f32>, f32>(Kind::Construct), // |
| ParamsFor<mat3x2<f32>, f32>(Kind::Construct), // |
| |
| // Conversion |
| ParamsFor<bool, u32>(Kind::Conversion), // |
| ParamsFor<bool, i32>(Kind::Conversion), // |
| ParamsFor<bool, f32>(Kind::Conversion), // |
| ParamsFor<bool, f16>(Kind::Conversion), // |
| |
| ParamsFor<i32, bool>(Kind::Conversion), // |
| ParamsFor<i32, u32>(Kind::Conversion), // |
| ParamsFor<i32, f32>(Kind::Conversion), // |
| ParamsFor<i32, f16>(Kind::Conversion), // |
| |
| ParamsFor<u32, bool>(Kind::Conversion), // |
| ParamsFor<u32, i32>(Kind::Conversion), // |
| ParamsFor<u32, f32>(Kind::Conversion), // |
| ParamsFor<u32, f16>(Kind::Conversion), // |
| |
| ParamsFor<f32, bool>(Kind::Conversion), // |
| ParamsFor<f32, u32>(Kind::Conversion), // |
| ParamsFor<f32, i32>(Kind::Conversion), // |
| ParamsFor<f32, f16>(Kind::Conversion), // |
| |
| ParamsFor<f16, bool>(Kind::Conversion), // |
| ParamsFor<f16, u32>(Kind::Conversion), // |
| ParamsFor<f16, i32>(Kind::Conversion), // |
| ParamsFor<f16, f32>(Kind::Conversion), // |
| |
| ParamsFor<vec3<bool>, vec3<u32>>(Kind::Conversion), // |
| ParamsFor<vec3<bool>, vec3<i32>>(Kind::Conversion), // |
| ParamsFor<vec3<bool>, vec3<f32>>(Kind::Conversion), // |
| ParamsFor<vec3<bool>, vec3<f16>>(Kind::Conversion), // |
| |
| ParamsFor<vec3<i32>, vec3<bool>>(Kind::Conversion), // |
| ParamsFor<vec3<i32>, vec3<u32>>(Kind::Conversion), // |
| ParamsFor<vec3<i32>, vec3<f32>>(Kind::Conversion), // |
| ParamsFor<vec3<i32>, vec3<f16>>(Kind::Conversion), // |
| |
| ParamsFor<vec3<u32>, vec3<bool>>(Kind::Conversion), // |
| ParamsFor<vec3<u32>, vec3<i32>>(Kind::Conversion), // |
| ParamsFor<vec3<u32>, vec3<f32>>(Kind::Conversion), // |
| ParamsFor<vec3<u32>, vec3<f16>>(Kind::Conversion), // |
| |
| ParamsFor<vec3<f32>, vec3<bool>>(Kind::Conversion), // |
| ParamsFor<vec3<f32>, vec3<u32>>(Kind::Conversion), // |
| ParamsFor<vec3<f32>, vec3<i32>>(Kind::Conversion), // |
| ParamsFor<vec3<f32>, vec3<f16>>(Kind::Conversion), // |
| |
| ParamsFor<vec3<f16>, vec3<bool>>(Kind::Conversion), // |
| ParamsFor<vec3<f16>, vec3<u32>>(Kind::Conversion), // |
| ParamsFor<vec3<f16>, vec3<i32>>(Kind::Conversion), // |
| ParamsFor<vec3<f16>, vec3<f32>>(Kind::Conversion), // |
| |
| ParamsFor<mat3x3<f16>, mat3x3<f32>>(Kind::Conversion), // |
| ParamsFor<mat2x3<f16>, mat2x3<f32>>(Kind::Conversion), // |
| ParamsFor<mat3x2<f16>, mat3x2<f32>>(Kind::Conversion), // |
| |
| ParamsFor<mat3x3<f32>, mat3x3<f16>>(Kind::Conversion), // |
| ParamsFor<mat2x3<f32>, mat2x3<f16>>(Kind::Conversion), // |
| ParamsFor<mat3x2<f32>, mat3x2<f16>>(Kind::Conversion), // |
| }; |
| |
| using ConversionConstructorValidTest = ResolverTestWithParam<Params>; |
| TEST_P(ConversionConstructorValidTest, All) { |
| auto& params = GetParam(); |
| |
| Enable(ast::Extension::kF16); |
| |
| // var a : <lhs_type1> = <lhs_type2>(<rhs_type>(<rhs_value_expr>)); |
| auto* lhs_type1 = params.lhs_type(*this); |
| auto* lhs_type2 = params.lhs_type(*this); |
| auto* rhs_type = params.rhs_type(*this); |
| auto* rhs_value_expr = params.rhs_value_expr(*this, 0); |
| |
| std::stringstream ss; |
| ss << FriendlyName(lhs_type1) << " = " << FriendlyName(lhs_type2) << "(" |
| << FriendlyName(rhs_type) << "(<rhs value expr>))"; |
| SCOPED_TRACE(ss.str()); |
| |
| auto* arg = Construct(rhs_type, rhs_value_expr); |
| auto* tc = Construct(lhs_type2, arg); |
| auto* a = Var("a", lhs_type1, ast::StorageClass::kNone, tc); |
| |
| // Self-assign 'a' to force the expression to be resolved so we can test its |
| // type below |
| auto* a_ident = Expr("a"); |
| WrapInFunction(Decl(a), Assign(a_ident, "a")); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| auto* call = Sem().Get<sem::Call>(tc); |
| ASSERT_NE(call, nullptr); |
| switch (params.kind) { |
| case Kind::Construct: { |
| auto* ctor = call->Target()->As<sem::TypeConstructor>(); |
| ASSERT_NE(ctor, nullptr); |
| EXPECT_EQ(call->Type(), ctor->ReturnType()); |
| ASSERT_EQ(ctor->Parameters().size(), 1u); |
| EXPECT_EQ(ctor->Parameters()[0]->Type(), TypeOf(arg)); |
| break; |
| } |
| case Kind::Conversion: { |
| auto* conv = call->Target()->As<sem::TypeConversion>(); |
| ASSERT_NE(conv, nullptr); |
| EXPECT_EQ(call->Type(), conv->ReturnType()); |
| ASSERT_EQ(conv->Parameters().size(), 1u); |
| EXPECT_EQ(conv->Parameters()[0]->Type(), TypeOf(arg)); |
| break; |
| } |
| } |
| } |
| INSTANTIATE_TEST_SUITE_P(ResolverTypeConstructorValidationTest, |
| ConversionConstructorValidTest, |
| testing::ValuesIn(valid_cases)); |
| |
| constexpr CreatePtrs all_types[] = { |
| CreatePtrsFor<bool>(), // |
| CreatePtrsFor<u32>(), // |
| CreatePtrsFor<i32>(), // |
| CreatePtrsFor<f32>(), // |
| CreatePtrsFor<f16>(), // |
| CreatePtrsFor<vec3<bool>>(), // |
| CreatePtrsFor<vec3<i32>>(), // |
| CreatePtrsFor<vec3<u32>>(), // |
| CreatePtrsFor<vec3<f32>>(), // |
| CreatePtrsFor<vec3<f16>>(), // |
| CreatePtrsFor<mat3x3<i32>>(), // |
| CreatePtrsFor<mat3x3<u32>>(), // |
| CreatePtrsFor<mat3x3<f32>>(), // |
| CreatePtrsFor<mat3x3<f16>>(), // |
| CreatePtrsFor<mat2x3<i32>>(), // |
| CreatePtrsFor<mat2x3<u32>>(), // |
| CreatePtrsFor<mat2x3<f32>>(), // |
| CreatePtrsFor<mat2x3<f16>>(), // |
| CreatePtrsFor<mat3x2<i32>>(), // |
| CreatePtrsFor<mat3x2<u32>>(), // |
| CreatePtrsFor<mat3x2<f32>>(), // |
| CreatePtrsFor<mat3x2<f16>>(), // |
| }; |
| |
| using ConversionConstructorInvalidTest = ResolverTestWithParam<std::tuple<CreatePtrs, // lhs |
| CreatePtrs // rhs |
| >>; |
| TEST_P(ConversionConstructorInvalidTest, All) { |
| auto& params = GetParam(); |
| |
| auto& lhs_params = std::get<0>(params); |
| auto& rhs_params = std::get<1>(params); |
| |
| // Skip test for valid cases |
| for (auto& v : valid_cases) { |
| if (v.lhs_type == lhs_params.ast && v.rhs_type == rhs_params.ast && |
| v.rhs_value_expr == rhs_params.expr) { |
| return; |
| } |
| } |
| // Skip non-conversions |
| if (lhs_params.ast == rhs_params.ast) { |
| return; |
| } |
| |
| // var a : <lhs_type1> = <lhs_type2>(<rhs_type>(<rhs_value_expr>)); |
| auto* lhs_type1 = lhs_params.ast(*this); |
| auto* lhs_type2 = lhs_params.ast(*this); |
| auto* rhs_type = rhs_params.ast(*this); |
| auto* rhs_value_expr = rhs_params.expr(*this, 0); |
| |
| std::stringstream ss; |
| ss << FriendlyName(lhs_type1) << " = " << FriendlyName(lhs_type2) << "(" |
| << FriendlyName(rhs_type) << "(<rhs value expr>))"; |
| SCOPED_TRACE(ss.str()); |
| |
| Enable(ast::Extension::kF16); |
| |
| auto* a = Var("a", lhs_type1, ast::StorageClass::kNone, |
| Construct(lhs_type2, Construct(rhs_type, rhs_value_expr))); |
| |
| // Self-assign 'a' to force the expression to be resolved so we can test its |
| // type below |
| auto* a_ident = Expr("a"); |
| WrapInFunction(Decl(a), Assign(a_ident, "a")); |
| |
| ASSERT_FALSE(r()->Resolve()); |
| } |
| INSTANTIATE_TEST_SUITE_P(ResolverTypeConstructorValidationTest, |
| ConversionConstructorInvalidTest, |
| testing::Combine(testing::ValuesIn(all_types), |
| testing::ValuesIn(all_types))); |
| |
| TEST_F(ResolverTypeConstructorValidationTest, ConversionConstructorInvalid_TooManyInitializers) { |
| auto* a = Var("a", ty.f32(), ast::StorageClass::kNone, |
| Construct(Source{{12, 34}}, ty.f32(), Expr(1_f), Expr(2_f))); |
| WrapInFunction(a); |
| |
| ASSERT_FALSE(r()->Resolve()); |
| EXPECT_THAT(r()->error(), HasSubstr("12:34 error: no matching constructor for f32(f32, f32)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, ConversionConstructorInvalid_InvalidInitializer) { |
| auto* a = Var("a", ty.f32(), ast::StorageClass::kNone, |
| Construct(Source{{12, 34}}, ty.f32(), Construct(ty.array<f32, 4>()))); |
| WrapInFunction(a); |
| |
| ASSERT_FALSE(r()->Resolve()); |
| EXPECT_THAT(r()->error(), |
| HasSubstr("12:34 error: no matching constructor for f32(array<f32, 4>)")); |
| } |
| |
| } // namespace ConversionConstructTest |
| |
| namespace ArrayConstructor { |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Array_ZeroValue_Pass) { |
| // array<u32, 10u>(); |
| auto* tc = array<u32, 10>(); |
| WrapInFunction(tc); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| auto* call = Sem().Get<sem::Call>(tc); |
| ASSERT_NE(call, nullptr); |
| EXPECT_TRUE(call->Type()->Is<sem::Array>()); |
| auto* ctor = call->Target()->As<sem::TypeConstructor>(); |
| ASSERT_NE(ctor, nullptr); |
| EXPECT_EQ(call->Type(), ctor->ReturnType()); |
| ASSERT_EQ(ctor->Parameters().size(), 0u); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Array_type_match) { |
| // array<u32, 3u>(0u, 10u. 20u); |
| auto* tc = array<u32, 3>(Expr(0_u), Expr(10_u), Expr(20_u)); |
| WrapInFunction(tc); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| auto* call = Sem().Get<sem::Call>(tc); |
| ASSERT_NE(call, nullptr); |
| EXPECT_TRUE(call->Type()->Is<sem::Array>()); |
| auto* ctor = call->Target()->As<sem::TypeConstructor>(); |
| ASSERT_NE(ctor, nullptr); |
| EXPECT_EQ(call->Type(), ctor->ReturnType()); |
| ASSERT_EQ(ctor->Parameters().size(), 3u); |
| EXPECT_TRUE(ctor->Parameters()[0]->Type()->Is<sem::U32>()); |
| EXPECT_TRUE(ctor->Parameters()[1]->Type()->Is<sem::U32>()); |
| EXPECT_TRUE(ctor->Parameters()[2]->Type()->Is<sem::U32>()); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Array_type_Mismatch_U32F32) { |
| // array<u32, 3u>(0u, 1.0f, 20u); |
| auto* tc = array<u32, 3>(Expr(0_u), Expr(Source{{12, 34}}, 1_f), Expr(20_u)); |
| WrapInFunction(tc); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_EQ(r()->error(), |
| "12:34 error: type in array constructor does not match array type: " |
| "expected 'u32', found 'f32'"); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, |
| Expr_Constructor_Array_ScalarArgumentTypeMismatch_F32I32) { |
| // array<f32, 1u>(1i); |
| auto* tc = array<f32, 1>(Expr(Source{{12, 34}}, 1_i)); |
| WrapInFunction(tc); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_EQ(r()->error(), |
| "12:34 error: type in array constructor does not match array type: " |
| "expected 'f32', found 'i32'"); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, |
| Expr_Constructor_Array_ScalarArgumentTypeMismatch_U32I32) { |
| // array<u32, 1u>(1i, 0u, 0u, 0u, 0u, 0u); |
| auto* tc = |
| array<u32, 1>(Expr(Source{{12, 34}}, 1_i), Expr(0_u), Expr(0_u), Expr(0_u), Expr(0_u)); |
| WrapInFunction(tc); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_EQ(r()->error(), |
| "12:34 error: type in array constructor does not match array type: " |
| "expected 'u32', found 'i32'"); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, |
| Expr_Constructor_Array_ScalarArgumentTypeMismatch_Vec2) { |
| // array<i32, 3u>(1i, vec2<i32>()); |
| auto* tc = array<i32, 3>(Expr(1_i), Construct(Source{{12, 34}}, ty.vec2<i32>())); |
| WrapInFunction(tc); |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_EQ(r()->error(), |
| "12:34 error: type in array constructor does not match array type: " |
| "expected 'i32', found 'vec2<i32>'"); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, |
| Expr_Constructor_ArrayOfVector_SubElemTypeMismatch_I32U32) { |
| // array<vec3<i32>, 2u>(vec3<i32>(), vec3<u32>()); |
| auto* e0 = vec3<i32>(); |
| SetSource(Source::Location({12, 34})); |
| auto* e1 = vec3<u32>(); |
| auto* t = Construct(ty.array(ty.vec3<i32>(), 2_i), e0, e1); |
| WrapInFunction(t); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_EQ(r()->error(), |
| "12:34 error: type in array constructor does not match array type: " |
| "expected 'vec3<i32>', found 'vec3<u32>'"); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, |
| Expr_Constructor_ArrayOfVector_SubElemTypeMismatch_I32Bool) { |
| // array<vec3<i32>, 2u>(vec3<i32>(), vec3<bool>(true, true, false)); |
| SetSource(Source::Location({12, 34})); |
| auto* e0 = vec3<bool>(true, true, false); |
| auto* e1 = vec3<i32>(); |
| auto* t = Construct(ty.array(ty.vec3<i32>(), 2_i), e0, e1); |
| WrapInFunction(t); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_EQ(r()->error(), |
| "12:34 error: type in array constructor does not match array type: " |
| "expected 'vec3<i32>', found 'vec3<bool>'"); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_ArrayOfArray_SubElemSizeMismatch) { |
| // array<array<i32, 2u>, 2u>(array<i32, 3u>(), array<i32, 2u>()); |
| SetSource(Source::Location({12, 34})); |
| auto* e0 = array<i32, 3>(); |
| auto* e1 = array<i32, 2>(); |
| auto* t = Construct(ty.array(ty.array<i32, 2>(), 2_i), e0, e1); |
| WrapInFunction(t); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_EQ(r()->error(), |
| "12:34 error: type in array constructor does not match array type: " |
| "expected 'array<i32, 2>', found 'array<i32, 3>'"); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_ArrayOfArray_SubElemTypeMismatch) { |
| // array<array<i32, 2u>, 2u>(array<i32, 2u>(), array<u32, 2u>()); |
| auto* e0 = array<i32, 2>(); |
| SetSource(Source::Location({12, 34})); |
| auto* e1 = array<u32, 2>(); |
| auto* t = Construct(ty.array(ty.array<i32, 2>(), 2_i), e0, e1); |
| WrapInFunction(t); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_EQ(r()->error(), |
| "12:34 error: type in array constructor does not match array type: " |
| "expected 'array<i32, 2>', found 'array<u32, 2>'"); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Array_TooFewElements) { |
| // array<i32, 4u>(1i, 2i, 3i); |
| SetSource(Source::Location({12, 34})); |
| auto* tc = array<i32, 4>(Expr(1_i), Expr(2_i), Expr(3_i)); |
| WrapInFunction(tc); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_EQ(r()->error(), |
| "12:34 error: array constructor has too few elements: expected 4, found 3"); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Array_TooManyElements) { |
| // array<i32, 4u>(1i, 2i, 3i, 4i, 5i); |
| SetSource(Source::Location({12, 34})); |
| auto* tc = array<i32, 4>(Expr(1_i), Expr(2_i), Expr(3_i), Expr(4_i), Expr(5_i)); |
| WrapInFunction(tc); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_EQ(r()->error(), |
| "12:34 error: array constructor has too many " |
| "elements: expected 4, " |
| "found 5"); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Array_Runtime) { |
| // array<i32>(1i); |
| auto* tc = array(ty.i32(), nullptr, Expr(Source{{12, 34}}, 1_i)); |
| WrapInFunction(tc); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_EQ(r()->error(), "error: cannot init a runtime-sized array"); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Array_RuntimeZeroValue) { |
| // array<i32>(); |
| auto* tc = array(ty.i32(), nullptr); |
| WrapInFunction(tc); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_EQ(r()->error(), "error: cannot init a runtime-sized array"); |
| } |
| |
| } // namespace ArrayConstructor |
| |
| namespace ScalarConstructor { |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Construct_i32_Success) { |
| auto* expr = Construct<i32>(Expr(123_i)); |
| WrapInFunction(expr); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_NE(TypeOf(expr), nullptr); |
| ASSERT_TRUE(TypeOf(expr)->Is<sem::I32>()); |
| |
| auto* call = Sem().Get<sem::Call>(expr); |
| ASSERT_NE(call, nullptr); |
| auto* ctor = call->Target()->As<sem::TypeConstructor>(); |
| ASSERT_NE(ctor, nullptr); |
| EXPECT_EQ(call->Type(), ctor->ReturnType()); |
| ASSERT_EQ(ctor->Parameters().size(), 1u); |
| EXPECT_TRUE(ctor->Parameters()[0]->Type()->Is<sem::I32>()); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Construct_u32_Success) { |
| auto* expr = Construct<u32>(Expr(123_u)); |
| WrapInFunction(expr); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_NE(TypeOf(expr), nullptr); |
| ASSERT_TRUE(TypeOf(expr)->Is<sem::U32>()); |
| |
| auto* call = Sem().Get<sem::Call>(expr); |
| ASSERT_NE(call, nullptr); |
| auto* ctor = call->Target()->As<sem::TypeConstructor>(); |
| ASSERT_NE(ctor, nullptr); |
| EXPECT_EQ(call->Type(), ctor->ReturnType()); |
| ASSERT_EQ(ctor->Parameters().size(), 1u); |
| EXPECT_TRUE(ctor->Parameters()[0]->Type()->Is<sem::U32>()); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Construct_f32_Success) { |
| auto* expr = Construct<f32>(Expr(1.23_f)); |
| WrapInFunction(expr); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_NE(TypeOf(expr), nullptr); |
| ASSERT_TRUE(TypeOf(expr)->Is<sem::F32>()); |
| |
| auto* call = Sem().Get<sem::Call>(expr); |
| ASSERT_NE(call, nullptr); |
| auto* ctor = call->Target()->As<sem::TypeConstructor>(); |
| ASSERT_NE(ctor, nullptr); |
| EXPECT_EQ(call->Type(), ctor->ReturnType()); |
| ASSERT_EQ(ctor->Parameters().size(), 1u); |
| EXPECT_TRUE(ctor->Parameters()[0]->Type()->Is<sem::F32>()); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Construct_f16_Success) { |
| Enable(ast::Extension::kF16); |
| |
| auto* expr = Construct<f16>(Expr(1.5_h)); |
| WrapInFunction(expr); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_NE(TypeOf(expr), nullptr); |
| ASSERT_TRUE(TypeOf(expr)->Is<sem::F16>()); |
| |
| auto* call = Sem().Get<sem::Call>(expr); |
| ASSERT_NE(call, nullptr); |
| auto* ctor = call->Target()->As<sem::TypeConstructor>(); |
| ASSERT_NE(ctor, nullptr); |
| EXPECT_EQ(call->Type(), ctor->ReturnType()); |
| ASSERT_EQ(ctor->Parameters().size(), 1u); |
| EXPECT_TRUE(ctor->Parameters()[0]->Type()->Is<sem::F16>()); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Convert_f32_to_i32_Success) { |
| auto* expr = Construct<i32>(1.23_f); |
| WrapInFunction(expr); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_NE(TypeOf(expr), nullptr); |
| ASSERT_TRUE(TypeOf(expr)->Is<sem::I32>()); |
| |
| auto* call = Sem().Get<sem::Call>(expr); |
| ASSERT_NE(call, nullptr); |
| auto* ctor = call->Target()->As<sem::TypeConversion>(); |
| ASSERT_NE(ctor, nullptr); |
| EXPECT_EQ(call->Type(), ctor->ReturnType()); |
| ASSERT_EQ(ctor->Parameters().size(), 1u); |
| EXPECT_TRUE(ctor->Parameters()[0]->Type()->Is<sem::F32>()); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Convert_i32_to_u32_Success) { |
| auto* expr = Construct<u32>(123_i); |
| WrapInFunction(expr); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_NE(TypeOf(expr), nullptr); |
| ASSERT_TRUE(TypeOf(expr)->Is<sem::U32>()); |
| |
| auto* call = Sem().Get<sem::Call>(expr); |
| ASSERT_NE(call, nullptr); |
| auto* ctor = call->Target()->As<sem::TypeConversion>(); |
| ASSERT_NE(ctor, nullptr); |
| EXPECT_EQ(call->Type(), ctor->ReturnType()); |
| ASSERT_EQ(ctor->Parameters().size(), 1u); |
| EXPECT_TRUE(ctor->Parameters()[0]->Type()->Is<sem::I32>()); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Convert_u32_to_f16_Success) { |
| Enable(ast::Extension::kF16); |
| |
| auto* expr = Construct<f16>(123_u); |
| WrapInFunction(expr); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_NE(TypeOf(expr), nullptr); |
| ASSERT_TRUE(TypeOf(expr)->Is<sem::F16>()); |
| |
| auto* call = Sem().Get<sem::Call>(expr); |
| ASSERT_NE(call, nullptr); |
| auto* ctor = call->Target()->As<sem::TypeConversion>(); |
| ASSERT_NE(ctor, nullptr); |
| EXPECT_EQ(call->Type(), ctor->ReturnType()); |
| ASSERT_EQ(ctor->Parameters().size(), 1u); |
| EXPECT_TRUE(ctor->Parameters()[0]->Type()->Is<sem::U32>()); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Convert_f16_to_f32_Success) { |
| Enable(ast::Extension::kF16); |
| |
| auto* expr = Construct<f32>(123_h); |
| WrapInFunction(expr); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_NE(TypeOf(expr), nullptr); |
| ASSERT_TRUE(TypeOf(expr)->Is<sem::F32>()); |
| |
| auto* call = Sem().Get<sem::Call>(expr); |
| ASSERT_NE(call, nullptr); |
| auto* ctor = call->Target()->As<sem::TypeConversion>(); |
| ASSERT_NE(ctor, nullptr); |
| EXPECT_EQ(call->Type(), ctor->ReturnType()); |
| ASSERT_EQ(ctor->Parameters().size(), 1u); |
| EXPECT_TRUE(ctor->Parameters()[0]->Type()->Is<sem::F16>()); |
| } |
| |
| } // namespace ScalarConstructor |
| |
| namespace VectorConstructor { |
| |
| TEST_F(ResolverTypeConstructorValidationTest, |
| Expr_Constructor_Vec2F32_Error_ScalarArgumentTypeMismatch) { |
| WrapInFunction(Construct(Source{{12, 34}}, ty.vec2<f32>(), 1_i, 2_f)); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT(r()->error(), |
| HasSubstr("12:34 error: no matching constructor for vec2<f32>(i32, f32)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, |
| Expr_Constructor_Vec2F16_Error_ScalarArgumentTypeMismatch) { |
| Enable(ast::Extension::kF16); |
| |
| WrapInFunction(Construct(Source{{12, 34}}, ty.vec2<f16>(), 1_h, 2_f)); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT(r()->error(), |
| HasSubstr("12:34 error: no matching constructor for vec2<f16>(f16, f32)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, |
| Expr_Constructor_Vec2U32_Error_ScalarArgumentTypeMismatch) { |
| WrapInFunction(Construct(Source{{12, 34}}, ty.vec2<u32>(), 1_u, 2_i)); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT(r()->error(), |
| HasSubstr("12:34 error: no matching constructor for vec2<u32>(u32, i32)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, |
| Expr_Constructor_Vec2I32_Error_ScalarArgumentTypeMismatch) { |
| WrapInFunction(Construct(Source{{12, 34}}, ty.vec2<i32>(), 1_u, 2_i)); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT(r()->error(), |
| HasSubstr("12:34 error: no matching constructor for vec2<i32>(u32, i32)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, |
| Expr_Constructor_Vec2Bool_Error_ScalarArgumentTypeMismatch) { |
| WrapInFunction(Construct(Source{{12, 34}}, ty.vec2<bool>(), true, 1_i)); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT(r()->error(), |
| HasSubstr("12:34 error: no matching constructor for vec2<bool>(bool, i32)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, |
| Expr_Constructor_Vec2_Error_Vec3ArgumentCardinalityTooLarge) { |
| WrapInFunction(Construct(Source{{12, 34}}, ty.vec2<f32>(), vec3<f32>())); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT(r()->error(), |
| HasSubstr("12:34 error: no matching constructor for vec2<f32>(vec3<f32>)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, |
| Expr_Constructor_Vec2_Error_Vec4ArgumentCardinalityTooLarge) { |
| WrapInFunction(Construct(Source{{12, 34}}, ty.vec2<f32>(), vec4<f32>())); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT(r()->error(), |
| HasSubstr("12:34 error: no matching constructor for vec2<f32>(vec4<f32>)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec2_Error_TooManyArgumentsScalar) { |
| WrapInFunction(Construct(Source{{12, 34}}, ty.vec2<f32>(), 1_f, 2_f, 3_f)); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT(r()->error(), |
| HasSubstr("12:34 error: no matching constructor for vec2<f32>(f32, f32, f32)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec2_Error_TooManyArgumentsVector) { |
| WrapInFunction(Construct(Source{{12, 34}}, ty.vec2<f32>(), vec2<f32>(), vec2<f32>())); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT( |
| r()->error(), |
| HasSubstr("12:34 error: no matching constructor for vec2<f32>(vec2<f32>, vec2<f32>)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, |
| Expr_Constructor_Vec2_Error_TooManyArgumentsVectorAndScalar) { |
| WrapInFunction(Construct(Source{{12, 34}}, ty.vec2<f32>(), vec2<f32>(), 1_f)); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT(r()->error(), |
| HasSubstr("12:34 error: no matching constructor for vec2<f32>(vec2<f32>, f32)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec2_Error_InvalidArgumentType) { |
| WrapInFunction(Construct(Source{{12, 34}}, ty.vec2<f32>(), mat2x2<f32>())); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT(r()->error(), |
| HasSubstr("12:34 error: no matching constructor for vec2<f32>(mat2x2<f32>)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec2_Success_ZeroValue) { |
| auto* tc = vec2<f32>(); |
| WrapInFunction(tc); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_NE(TypeOf(tc), nullptr); |
| ASSERT_TRUE(TypeOf(tc)->Is<sem::Vector>()); |
| EXPECT_TRUE(TypeOf(tc)->As<sem::Vector>()->type()->Is<sem::F32>()); |
| EXPECT_EQ(TypeOf(tc)->As<sem::Vector>()->Width(), 2u); |
| |
| auto* call = Sem().Get<sem::Call>(tc); |
| ASSERT_NE(call, nullptr); |
| auto* ctor = call->Target()->As<sem::TypeConstructor>(); |
| ASSERT_NE(ctor, nullptr); |
| EXPECT_EQ(call->Type(), ctor->ReturnType()); |
| ASSERT_EQ(ctor->Parameters().size(), 0u); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec2F32_Success_Scalar) { |
| auto* tc = vec2<f32>(1_f, 1_f); |
| WrapInFunction(tc); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_NE(TypeOf(tc), nullptr); |
| ASSERT_TRUE(TypeOf(tc)->Is<sem::Vector>()); |
| EXPECT_TRUE(TypeOf(tc)->As<sem::Vector>()->type()->Is<sem::F32>()); |
| EXPECT_EQ(TypeOf(tc)->As<sem::Vector>()->Width(), 2u); |
| |
| auto* call = Sem().Get<sem::Call>(tc); |
| ASSERT_NE(call, nullptr); |
| auto* ctor = call->Target()->As<sem::TypeConstructor>(); |
| ASSERT_NE(ctor, nullptr); |
| EXPECT_EQ(call->Type(), ctor->ReturnType()); |
| ASSERT_EQ(ctor->Parameters().size(), 2u); |
| EXPECT_TRUE(ctor->Parameters()[0]->Type()->Is<sem::F32>()); |
| EXPECT_TRUE(ctor->Parameters()[1]->Type()->Is<sem::F32>()); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec2F16_Success_Scalar) { |
| Enable(ast::Extension::kF16); |
| |
| auto* tc = vec2<f16>(1_h, 1_h); |
| WrapInFunction(tc); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_NE(TypeOf(tc), nullptr); |
| ASSERT_TRUE(TypeOf(tc)->Is<sem::Vector>()); |
| EXPECT_TRUE(TypeOf(tc)->As<sem::Vector>()->type()->Is<sem::F16>()); |
| EXPECT_EQ(TypeOf(tc)->As<sem::Vector>()->Width(), 2u); |
| |
| auto* call = Sem().Get<sem::Call>(tc); |
| ASSERT_NE(call, nullptr); |
| auto* ctor = call->Target()->As<sem::TypeConstructor>(); |
| ASSERT_NE(ctor, nullptr); |
| EXPECT_EQ(call->Type(), ctor->ReturnType()); |
| ASSERT_EQ(ctor->Parameters().size(), 2u); |
| EXPECT_TRUE(ctor->Parameters()[0]->Type()->Is<sem::F16>()); |
| EXPECT_TRUE(ctor->Parameters()[1]->Type()->Is<sem::F16>()); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec2U32_Success_Scalar) { |
| auto* tc = vec2<u32>(1_u, 1_u); |
| WrapInFunction(tc); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_NE(TypeOf(tc), nullptr); |
| ASSERT_TRUE(TypeOf(tc)->Is<sem::Vector>()); |
| EXPECT_TRUE(TypeOf(tc)->As<sem::Vector>()->type()->Is<sem::U32>()); |
| EXPECT_EQ(TypeOf(tc)->As<sem::Vector>()->Width(), 2u); |
| |
| auto* call = Sem().Get<sem::Call>(tc); |
| ASSERT_NE(call, nullptr); |
| auto* ctor = call->Target()->As<sem::TypeConstructor>(); |
| ASSERT_NE(ctor, nullptr); |
| EXPECT_EQ(call->Type(), ctor->ReturnType()); |
| ASSERT_EQ(ctor->Parameters().size(), 2u); |
| EXPECT_TRUE(ctor->Parameters()[0]->Type()->Is<sem::U32>()); |
| EXPECT_TRUE(ctor->Parameters()[1]->Type()->Is<sem::U32>()); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec2I32_Success_Scalar) { |
| auto* tc = vec2<i32>(1_i, 1_i); |
| WrapInFunction(tc); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_NE(TypeOf(tc), nullptr); |
| ASSERT_TRUE(TypeOf(tc)->Is<sem::Vector>()); |
| EXPECT_TRUE(TypeOf(tc)->As<sem::Vector>()->type()->Is<sem::I32>()); |
| EXPECT_EQ(TypeOf(tc)->As<sem::Vector>()->Width(), 2u); |
| |
| auto* call = Sem().Get<sem::Call>(tc); |
| ASSERT_NE(call, nullptr); |
| auto* ctor = call->Target()->As<sem::TypeConstructor>(); |
| ASSERT_NE(ctor, nullptr); |
| EXPECT_EQ(call->Type(), ctor->ReturnType()); |
| ASSERT_EQ(ctor->Parameters().size(), 2u); |
| EXPECT_TRUE(ctor->Parameters()[0]->Type()->Is<sem::I32>()); |
| EXPECT_TRUE(ctor->Parameters()[1]->Type()->Is<sem::I32>()); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec2Bool_Success_Scalar) { |
| auto* tc = vec2<bool>(true, false); |
| WrapInFunction(tc); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_NE(TypeOf(tc), nullptr); |
| ASSERT_TRUE(TypeOf(tc)->Is<sem::Vector>()); |
| EXPECT_TRUE(TypeOf(tc)->As<sem::Vector>()->type()->Is<sem::Bool>()); |
| EXPECT_EQ(TypeOf(tc)->As<sem::Vector>()->Width(), 2u); |
| |
| auto* call = Sem().Get<sem::Call>(tc); |
| ASSERT_NE(call, nullptr); |
| auto* ctor = call->Target()->As<sem::TypeConstructor>(); |
| ASSERT_NE(ctor, nullptr); |
| EXPECT_EQ(call->Type(), ctor->ReturnType()); |
| ASSERT_EQ(ctor->Parameters().size(), 2u); |
| EXPECT_TRUE(ctor->Parameters()[0]->Type()->Is<sem::Bool>()); |
| EXPECT_TRUE(ctor->Parameters()[1]->Type()->Is<sem::Bool>()); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec2_Success_Identity) { |
| auto* tc = vec2<f32>(vec2<f32>()); |
| WrapInFunction(tc); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_NE(TypeOf(tc), nullptr); |
| ASSERT_TRUE(TypeOf(tc)->Is<sem::Vector>()); |
| EXPECT_TRUE(TypeOf(tc)->As<sem::Vector>()->type()->Is<sem::F32>()); |
| EXPECT_EQ(TypeOf(tc)->As<sem::Vector>()->Width(), 2u); |
| |
| auto* call = Sem().Get<sem::Call>(tc); |
| ASSERT_NE(call, nullptr); |
| auto* ctor = call->Target()->As<sem::TypeConstructor>(); |
| ASSERT_NE(ctor, nullptr); |
| EXPECT_EQ(call->Type(), ctor->ReturnType()); |
| ASSERT_EQ(ctor->Parameters().size(), 1u); |
| EXPECT_TRUE(ctor->Parameters()[0]->Type()->Is<sem::Vector>()); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec2_Success_Vec2TypeConversion) { |
| auto* tc = vec2<f32>(vec2<i32>()); |
| WrapInFunction(tc); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_NE(TypeOf(tc), nullptr); |
| ASSERT_TRUE(TypeOf(tc)->Is<sem::Vector>()); |
| EXPECT_TRUE(TypeOf(tc)->As<sem::Vector>()->type()->Is<sem::F32>()); |
| EXPECT_EQ(TypeOf(tc)->As<sem::Vector>()->Width(), 2u); |
| |
| auto* call = Sem().Get<sem::Call>(tc); |
| ASSERT_NE(call, nullptr); |
| auto* ctor = call->Target()->As<sem::TypeConversion>(); |
| ASSERT_NE(ctor, nullptr); |
| EXPECT_EQ(call->Type(), ctor->ReturnType()); |
| ASSERT_EQ(ctor->Parameters().size(), 1u); |
| EXPECT_TRUE(ctor->Parameters()[0]->Type()->Is<sem::Vector>()); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, |
| Expr_Constructor_Vec3F32_Error_ScalarArgumentTypeMismatch) { |
| WrapInFunction(Construct(Source{{12, 34}}, ty.vec3<f32>(), 1_f, 2_f, 3_i)); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT(r()->error(), |
| HasSubstr("12:34 error: no matching constructor for vec3<f32>(f32, f32, i32)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, |
| Expr_Constructor_Vec3F16_Error_ScalarArgumentTypeMismatch) { |
| Enable(ast::Extension::kF16); |
| |
| WrapInFunction(Construct(Source{{12, 34}}, ty.vec3<f16>(), 1_h, 2_h, 3_f)); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT(r()->error(), |
| HasSubstr("12:34 error: no matching constructor for vec3<f16>(f16, f16, f32)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, |
| Expr_Constructor_Vec3U32_Error_ScalarArgumentTypeMismatch) { |
| WrapInFunction(Construct(Source{{12, 34}}, ty.vec3<u32>(), 1_u, 2_i, 3_u)); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT(r()->error(), |
| HasSubstr("12:34 error: no matching constructor for vec3<u32>(u32, i32, u32)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, |
| Expr_Constructor_Vec3I32_Error_ScalarArgumentTypeMismatch) { |
| WrapInFunction(Construct(Source{{12, 34}}, ty.vec3<i32>(), 1_i, 2_u, 3_i)); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT(r()->error(), |
| HasSubstr("12:34 error: no matching constructor for vec3<i32>(i32, u32, i32)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, |
| Expr_Constructor_Vec3Bool_Error_ScalarArgumentTypeMismatch) { |
| WrapInFunction(Construct(Source{{12, 34}}, ty.vec3<bool>(), false, 1_i, true)); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT(r()->error(), |
| HasSubstr("12:34 error: no matching constructor for vec3<bool>(bool, i32, bool)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, |
| Expr_Constructor_Vec3_Error_Vec4ArgumentCardinalityTooLarge) { |
| WrapInFunction(Construct(Source{{12, 34}}, ty.vec3<f32>(), vec4<f32>())); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT(r()->error(), |
| HasSubstr("12:34 error: no matching constructor for vec3<f32>(vec4<f32>)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec3_Error_TooFewArgumentsScalar) { |
| WrapInFunction(Construct(Source{{12, 34}}, ty.vec3<f32>(), 1_f, 2_f)); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT(r()->error(), |
| HasSubstr("12:34 error: no matching constructor for vec3<f32>(f32, f32)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec3_Error_TooManyArgumentsScalar) { |
| WrapInFunction(Construct(Source{{12, 34}}, ty.vec3<f32>(), 1_f, 2_f, 3_f, 4_f)); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT( |
| r()->error(), |
| HasSubstr("12:34 error: no matching constructor for vec3<f32>(f32, f32, f32, f32)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec3_Error_TooFewArgumentsVec2) { |
| WrapInFunction(Construct(Source{{12, 34}}, ty.vec3<f32>(), vec2<f32>())); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT(r()->error(), |
| HasSubstr("12:34 error: no matching constructor for vec3<f32>(vec2<f32>)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec3_Error_TooManyArgumentsVec2) { |
| WrapInFunction(Construct(Source{{12, 34}}, ty.vec3<f32>(), vec2<f32>(), vec2<f32>())); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT( |
| r()->error(), |
| HasSubstr("12:34 error: no matching constructor for vec3<f32>(vec2<f32>, vec2<f32>)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, |
| Expr_Constructor_Vec3_Error_TooManyArgumentsVec2AndScalar) { |
| WrapInFunction(Construct(Source{{12, 34}}, ty.vec3<f32>(), vec2<f32>(), 1_f, 1_f)); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT( |
| r()->error(), |
| HasSubstr("12:34 error: no matching constructor for vec3<f32>(vec2<f32>, f32, f32)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec3_Error_TooManyArgumentsVec3) { |
| WrapInFunction(Construct(Source{{12, 34}}, ty.vec3<f32>(), vec3<f32>(), 1_f)); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT(r()->error(), |
| HasSubstr("12:34 error: no matching constructor for vec3<f32>(vec3<f32>, f32)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec3_Error_InvalidArgumentType) { |
| WrapInFunction(Construct(Source{{12, 34}}, ty.vec3<f32>(), mat2x2<f32>())); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT(r()->error(), |
| HasSubstr("12:34 error: no matching constructor for vec3<f32>(mat2x2<f32>)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec3_Success_ZeroValue) { |
| auto* tc = vec3<f32>(); |
| WrapInFunction(tc); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_NE(TypeOf(tc), nullptr); |
| ASSERT_TRUE(TypeOf(tc)->Is<sem::Vector>()); |
| EXPECT_TRUE(TypeOf(tc)->As<sem::Vector>()->type()->Is<sem::F32>()); |
| EXPECT_EQ(TypeOf(tc)->As<sem::Vector>()->Width(), 3u); |
| |
| auto* call = Sem().Get<sem::Call>(tc); |
| ASSERT_NE(call, nullptr); |
| auto* ctor = call->Target()->As<sem::TypeConstructor>(); |
| ASSERT_NE(ctor, nullptr); |
| EXPECT_EQ(call->Type(), ctor->ReturnType()); |
| ASSERT_EQ(ctor->Parameters().size(), 0u); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec3F32_Success_Scalar) { |
| auto* tc = vec3<f32>(1_f, 1_f, 1_f); |
| WrapInFunction(tc); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_NE(TypeOf(tc), nullptr); |
| ASSERT_TRUE(TypeOf(tc)->Is<sem::Vector>()); |
| EXPECT_TRUE(TypeOf(tc)->As<sem::Vector>()->type()->Is<sem::F32>()); |
| EXPECT_EQ(TypeOf(tc)->As<sem::Vector>()->Width(), 3u); |
| |
| auto* call = Sem().Get<sem::Call>(tc); |
| ASSERT_NE(call, nullptr); |
| auto* ctor = call->Target()->As<sem::TypeConstructor>(); |
| ASSERT_NE(ctor, nullptr); |
| EXPECT_EQ(call->Type(), ctor->ReturnType()); |
| ASSERT_EQ(ctor->Parameters().size(), 3u); |
| EXPECT_TRUE(ctor->Parameters()[0]->Type()->Is<sem::F32>()); |
| EXPECT_TRUE(ctor->Parameters()[1]->Type()->Is<sem::F32>()); |
| EXPECT_TRUE(ctor->Parameters()[2]->Type()->Is<sem::F32>()); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec3F16_Success_Scalar) { |
| Enable(ast::Extension::kF16); |
| |
| auto* tc = vec3<f16>(1_h, 1_h, 1_h); |
| WrapInFunction(tc); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_NE(TypeOf(tc), nullptr); |
| ASSERT_TRUE(TypeOf(tc)->Is<sem::Vector>()); |
| EXPECT_TRUE(TypeOf(tc)->As<sem::Vector>()->type()->Is<sem::F16>()); |
| EXPECT_EQ(TypeOf(tc)->As<sem::Vector>()->Width(), 3u); |
| |
| auto* call = Sem().Get<sem::Call>(tc); |
| ASSERT_NE(call, nullptr); |
| auto* ctor = call->Target()->As<sem::TypeConstructor>(); |
| ASSERT_NE(ctor, nullptr); |
| EXPECT_EQ(call->Type(), ctor->ReturnType()); |
| ASSERT_EQ(ctor->Parameters().size(), 3u); |
| EXPECT_TRUE(ctor->Parameters()[0]->Type()->Is<sem::F16>()); |
| EXPECT_TRUE(ctor->Parameters()[1]->Type()->Is<sem::F16>()); |
| EXPECT_TRUE(ctor->Parameters()[2]->Type()->Is<sem::F16>()); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec3U32_Success_Scalar) { |
| auto* tc = vec3<u32>(1_u, 1_u, 1_u); |
| WrapInFunction(tc); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_NE(TypeOf(tc), nullptr); |
| ASSERT_TRUE(TypeOf(tc)->Is<sem::Vector>()); |
| EXPECT_TRUE(TypeOf(tc)->As<sem::Vector>()->type()->Is<sem::U32>()); |
| EXPECT_EQ(TypeOf(tc)->As<sem::Vector>()->Width(), 3u); |
| |
| auto* call = Sem().Get<sem::Call>(tc); |
| ASSERT_NE(call, nullptr); |
| auto* ctor = call->Target()->As<sem::TypeConstructor>(); |
| ASSERT_NE(ctor, nullptr); |
| EXPECT_EQ(call->Type(), ctor->ReturnType()); |
| ASSERT_EQ(ctor->Parameters().size(), 3u); |
| EXPECT_TRUE(ctor->Parameters()[0]->Type()->Is<sem::U32>()); |
| EXPECT_TRUE(ctor->Parameters()[1]->Type()->Is<sem::U32>()); |
| EXPECT_TRUE(ctor->Parameters()[2]->Type()->Is<sem::U32>()); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec3I32_Success_Scalar) { |
| auto* tc = vec3<i32>(1_i, 1_i, 1_i); |
| WrapInFunction(tc); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_NE(TypeOf(tc), nullptr); |
| ASSERT_TRUE(TypeOf(tc)->Is<sem::Vector>()); |
| EXPECT_TRUE(TypeOf(tc)->As<sem::Vector>()->type()->Is<sem::I32>()); |
| EXPECT_EQ(TypeOf(tc)->As<sem::Vector>()->Width(), 3u); |
| |
| auto* call = Sem().Get<sem::Call>(tc); |
| ASSERT_NE(call, nullptr); |
| auto* ctor = call->Target()->As<sem::TypeConstructor>(); |
| ASSERT_NE(ctor, nullptr); |
| EXPECT_EQ(call->Type(), ctor->ReturnType()); |
| ASSERT_EQ(ctor->Parameters().size(), 3u); |
| EXPECT_TRUE(ctor->Parameters()[0]->Type()->Is<sem::I32>()); |
| EXPECT_TRUE(ctor->Parameters()[1]->Type()->Is<sem::I32>()); |
| EXPECT_TRUE(ctor->Parameters()[2]->Type()->Is<sem::I32>()); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec3Bool_Success_Scalar) { |
| auto* tc = vec3<bool>(true, false, true); |
| WrapInFunction(tc); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_NE(TypeOf(tc), nullptr); |
| ASSERT_TRUE(TypeOf(tc)->Is<sem::Vector>()); |
| EXPECT_TRUE(TypeOf(tc)->As<sem::Vector>()->type()->Is<sem::Bool>()); |
| EXPECT_EQ(TypeOf(tc)->As<sem::Vector>()->Width(), 3u); |
| |
| auto* call = Sem().Get<sem::Call>(tc); |
| ASSERT_NE(call, nullptr); |
| auto* ctor = call->Target()->As<sem::TypeConstructor>(); |
| ASSERT_NE(ctor, nullptr); |
| EXPECT_EQ(call->Type(), ctor->ReturnType()); |
| ASSERT_EQ(ctor->Parameters().size(), 3u); |
| EXPECT_TRUE(ctor->Parameters()[0]->Type()->Is<sem::Bool>()); |
| EXPECT_TRUE(ctor->Parameters()[1]->Type()->Is<sem::Bool>()); |
| EXPECT_TRUE(ctor->Parameters()[2]->Type()->Is<sem::Bool>()); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec3_Success_Vec2AndScalar) { |
| auto* tc = vec3<f32>(vec2<f32>(), 1_f); |
| WrapInFunction(tc); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_NE(TypeOf(tc), nullptr); |
| ASSERT_TRUE(TypeOf(tc)->Is<sem::Vector>()); |
| EXPECT_TRUE(TypeOf(tc)->As<sem::Vector>()->type()->Is<sem::F32>()); |
| EXPECT_EQ(TypeOf(tc)->As<sem::Vector>()->Width(), 3u); |
| |
| auto* call = Sem().Get<sem::Call>(tc); |
| ASSERT_NE(call, nullptr); |
| auto* ctor = call->Target()->As<sem::TypeConstructor>(); |
| ASSERT_NE(ctor, nullptr); |
| EXPECT_EQ(call->Type(), ctor->ReturnType()); |
| ASSERT_EQ(ctor->Parameters().size(), 2u); |
| EXPECT_TRUE(ctor->Parameters()[0]->Type()->Is<sem::Vector>()); |
| EXPECT_TRUE(ctor->Parameters()[1]->Type()->Is<sem::F32>()); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec3_Success_ScalarAndVec2) { |
| auto* tc = vec3<f32>(1_f, vec2<f32>()); |
| WrapInFunction(tc); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_NE(TypeOf(tc), nullptr); |
| ASSERT_TRUE(TypeOf(tc)->Is<sem::Vector>()); |
| EXPECT_TRUE(TypeOf(tc)->As<sem::Vector>()->type()->Is<sem::F32>()); |
| EXPECT_EQ(TypeOf(tc)->As<sem::Vector>()->Width(), 3u); |
| |
| auto* call = Sem().Get<sem::Call>(tc); |
| ASSERT_NE(call, nullptr); |
| auto* ctor = call->Target()->As<sem::TypeConstructor>(); |
| ASSERT_NE(ctor, nullptr); |
| EXPECT_EQ(call->Type(), ctor->ReturnType()); |
| ASSERT_EQ(ctor->Parameters().size(), 2u); |
| EXPECT_TRUE(ctor->Parameters()[0]->Type()->Is<sem::F32>()); |
| EXPECT_TRUE(ctor->Parameters()[1]->Type()->Is<sem::Vector>()); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec3_Success_Identity) { |
| auto* tc = vec3<f32>(vec3<f32>()); |
| WrapInFunction(tc); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_NE(TypeOf(tc), nullptr); |
| ASSERT_TRUE(TypeOf(tc)->Is<sem::Vector>()); |
| EXPECT_TRUE(TypeOf(tc)->As<sem::Vector>()->type()->Is<sem::F32>()); |
| EXPECT_EQ(TypeOf(tc)->As<sem::Vector>()->Width(), 3u); |
| |
| auto* call = Sem().Get<sem::Call>(tc); |
| ASSERT_NE(call, nullptr); |
| auto* ctor = call->Target()->As<sem::TypeConstructor>(); |
| ASSERT_NE(ctor, nullptr); |
| EXPECT_EQ(call->Type(), ctor->ReturnType()); |
| ASSERT_EQ(ctor->Parameters().size(), 1u); |
| EXPECT_TRUE(ctor->Parameters()[0]->Type()->Is<sem::Vector>()); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec3_Success_Vec3TypeConversion) { |
| auto* tc = vec3<f32>(vec3<i32>()); |
| WrapInFunction(tc); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_NE(TypeOf(tc), nullptr); |
| ASSERT_TRUE(TypeOf(tc)->Is<sem::Vector>()); |
| EXPECT_TRUE(TypeOf(tc)->As<sem::Vector>()->type()->Is<sem::F32>()); |
| EXPECT_EQ(TypeOf(tc)->As<sem::Vector>()->Width(), 3u); |
| |
| auto* call = Sem().Get<sem::Call>(tc); |
| ASSERT_NE(call, nullptr); |
| auto* ctor = call->Target()->As<sem::TypeConversion>(); |
| ASSERT_NE(ctor, nullptr); |
| EXPECT_EQ(call->Type(), ctor->ReturnType()); |
| ASSERT_EQ(ctor->Parameters().size(), 1u); |
| EXPECT_TRUE(ctor->Parameters()[0]->Type()->Is<sem::Vector>()); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, |
| Expr_Constructor_Vec4F32_Error_ScalarArgumentTypeMismatch) { |
| WrapInFunction(Construct(Source{{12, 34}}, ty.vec4<f32>(), 1_f, 1_f, 1_i, 1_f)); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT( |
| r()->error(), |
| HasSubstr("12:34 error: no matching constructor for vec4<f32>(f32, f32, i32, f32)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, |
| Expr_Constructor_Vec4F16_Error_ScalarArgumentTypeMismatch) { |
| Enable(ast::Extension::kF16); |
| |
| WrapInFunction(Construct(Source{{12, 34}}, ty.vec4<f16>(), 1_h, 1_h, 1_f, 1_h)); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT( |
| r()->error(), |
| HasSubstr("12:34 error: no matching constructor for vec4<f16>(f16, f16, f32, f16)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, |
| Expr_Constructor_Vec4U32_Error_ScalarArgumentTypeMismatch) { |
| WrapInFunction(Construct(Source{{12, 34}}, ty.vec4<u32>(), 1_u, 1_u, 1_i, 1_u)); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT( |
| r()->error(), |
| HasSubstr("12:34 error: no matching constructor for vec4<u32>(u32, u32, i32, u32)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, |
| Expr_Constructor_Vec4I32_Error_ScalarArgumentTypeMismatch) { |
| WrapInFunction(Construct(Source{{12, 34}}, ty.vec4<i32>(), 1_i, 1_i, 1_u, 1_i)); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT( |
| r()->error(), |
| HasSubstr("12:34 error: no matching constructor for vec4<i32>(i32, i32, u32, i32)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, |
| Expr_Constructor_Vec4Bool_Error_ScalarArgumentTypeMismatch) { |
| WrapInFunction(Construct(Source{{12, 34}}, ty.vec4<bool>(), true, false, 1_i, true)); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT( |
| r()->error(), |
| HasSubstr("12:34 error: no matching constructor for vec4<bool>(bool, bool, i32, bool)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec4_Error_TooFewArgumentsScalar) { |
| WrapInFunction(Construct(Source{{12, 34}}, ty.vec4<f32>(), 1_f, 2_f, 3_f)); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT(r()->error(), |
| HasSubstr("12:34 error: no matching constructor for vec4<f32>(f32, f32, f32)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec4_Error_TooManyArgumentsScalar) { |
| WrapInFunction(Construct(Source{{12, 34}}, ty.vec4<f32>(), 1_f, 2_f, 3_f, 4_f, 5_f)); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT( |
| r()->error(), |
| HasSubstr("12:34 error: no matching constructor for vec4<f32>(f32, f32, f32, f32, f32)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, |
| Expr_Constructor_Vec4_Error_TooFewArgumentsVec2AndScalar) { |
| WrapInFunction(Construct(Source{{12, 34}}, ty.vec4<f32>(), vec2<f32>(), 1_f)); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT(r()->error(), |
| HasSubstr("12:34 error: no matching constructor for vec4<f32>(vec2<f32>, f32)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, |
| Expr_Constructor_Vec4_Error_TooManyArgumentsVec2AndScalars) { |
| WrapInFunction(Construct(Source{{12, 34}}, ty.vec4<f32>(), vec2<f32>(), 1_f, 2_f, 3_f)); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT( |
| r()->error(), |
| HasSubstr("12:34 error: no matching constructor for vec4<f32>(vec2<f32>, f32, f32, f32)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, |
| Expr_Constructor_Vec4_Error_TooManyArgumentsVec2Vec2Scalar) { |
| WrapInFunction(Construct(Source{{12, 34}}, ty.vec4<f32>(), vec2<f32>(), vec2<f32>(), 1_f)); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT( |
| r()->error(), |
| HasSubstr("12:34 error: no matching constructor for vec4<f32>(vec2<f32>, vec2<f32>, f32)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, |
| Expr_Constructor_Vec4_Error_TooManyArgumentsVec2Vec2Vec2) { |
| WrapInFunction( |
| Construct(Source{{12, 34}}, ty.vec4<f32>(), vec2<f32>(), vec2<f32>(), vec2<f32>())); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT( |
| r()->error(), |
| HasSubstr( |
| "12:34 error: no matching constructor for vec4<f32>(vec2<f32>, vec2<f32>, vec2<f32>)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec4_Error_TooFewArgumentsVec3) { |
| WrapInFunction(Construct(Source{{12, 34}}, ty.vec4<f32>(), vec3<f32>())); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT(r()->error(), |
| HasSubstr("12:34 error: no matching constructor for vec4<f32>(vec3<f32>)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, |
| Expr_Constructor_Vec4_Error_TooManyArgumentsVec3AndScalars) { |
| WrapInFunction(Construct(Source{{12, 34}}, ty.vec4<f32>(), vec3<f32>(), 1_f, 2_f)); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT( |
| r()->error(), |
| HasSubstr("12:34 error: no matching constructor for vec4<f32>(vec3<f32>, f32, f32)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, |
| Expr_Constructor_Vec4_Error_TooManyArgumentsVec3AndVec2) { |
| WrapInFunction(Construct(Source{{12, 34}}, ty.vec4<f32>(), vec3<f32>(), vec2<f32>())); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT( |
| r()->error(), |
| HasSubstr("12:34 error: no matching constructor for vec4<f32>(vec3<f32>, vec2<f32>)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, |
| Expr_Constructor_Vec4_Error_TooManyArgumentsVec2AndVec3) { |
| WrapInFunction(Construct(Source{{12, 34}}, ty.vec4<f32>(), vec2<f32>(), vec3<f32>())); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT( |
| r()->error(), |
| HasSubstr("12:34 error: no matching constructor for vec4<f32>(vec2<f32>, vec3<f32>)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, |
| Expr_Constructor_Vec4_Error_TooManyArgumentsVec3AndVec3) { |
| WrapInFunction(Construct(Source{{12, 34}}, ty.vec4<f32>(), vec3<f32>(), vec3<f32>())); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT( |
| r()->error(), |
| HasSubstr("12:34 error: no matching constructor for vec4<f32>(vec3<f32>, vec3<f32>)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec4_Error_InvalidArgumentType) { |
| WrapInFunction(Construct(Source{{12, 34}}, ty.vec4<f32>(), mat2x2<f32>())); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT(r()->error(), |
| HasSubstr("12:34 error: no matching constructor for vec4<f32>(mat2x2<f32>)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec4_Success_ZeroValue) { |
| auto* tc = vec4<f32>(); |
| WrapInFunction(tc); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_NE(TypeOf(tc), nullptr); |
| ASSERT_TRUE(TypeOf(tc)->Is<sem::Vector>()); |
| EXPECT_TRUE(TypeOf(tc)->As<sem::Vector>()->type()->Is<sem::F32>()); |
| EXPECT_EQ(TypeOf(tc)->As<sem::Vector>()->Width(), 4u); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec4F32_Success_Scalar) { |
| auto* tc = vec4<f32>(1_f, 1_f, 1_f, 1_f); |
| WrapInFunction(tc); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_NE(TypeOf(tc), nullptr); |
| ASSERT_TRUE(TypeOf(tc)->Is<sem::Vector>()); |
| EXPECT_TRUE(TypeOf(tc)->As<sem::Vector>()->type()->Is<sem::F32>()); |
| EXPECT_EQ(TypeOf(tc)->As<sem::Vector>()->Width(), 4u); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec4F16_Success_Scalar) { |
| Enable(ast::Extension::kF16); |
| |
| auto* tc = vec4<f16>(1_h, 1_h, 1_h, 1_h); |
| WrapInFunction(tc); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_NE(TypeOf(tc), nullptr); |
| ASSERT_TRUE(TypeOf(tc)->Is<sem::Vector>()); |
| EXPECT_TRUE(TypeOf(tc)->As<sem::Vector>()->type()->Is<sem::F16>()); |
| EXPECT_EQ(TypeOf(tc)->As<sem::Vector>()->Width(), 4u); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec4U32_Success_Scalar) { |
| auto* tc = vec4<u32>(1_u, 1_u, 1_u, 1_u); |
| WrapInFunction(tc); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_NE(TypeOf(tc), nullptr); |
| ASSERT_TRUE(TypeOf(tc)->Is<sem::Vector>()); |
| EXPECT_TRUE(TypeOf(tc)->As<sem::Vector>()->type()->Is<sem::U32>()); |
| EXPECT_EQ(TypeOf(tc)->As<sem::Vector>()->Width(), 4u); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec4I32_Success_Scalar) { |
| auto* tc = vec4<i32>(1_i, 1_i, 1_i, 1_i); |
| WrapInFunction(tc); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_NE(TypeOf(tc), nullptr); |
| ASSERT_TRUE(TypeOf(tc)->Is<sem::Vector>()); |
| EXPECT_TRUE(TypeOf(tc)->As<sem::Vector>()->type()->Is<sem::I32>()); |
| EXPECT_EQ(TypeOf(tc)->As<sem::Vector>()->Width(), 4u); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec4Bool_Success_Scalar) { |
| auto* tc = vec4<bool>(true, false, true, false); |
| WrapInFunction(tc); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_NE(TypeOf(tc), nullptr); |
| ASSERT_TRUE(TypeOf(tc)->Is<sem::Vector>()); |
| EXPECT_TRUE(TypeOf(tc)->As<sem::Vector>()->type()->Is<sem::Bool>()); |
| EXPECT_EQ(TypeOf(tc)->As<sem::Vector>()->Width(), 4u); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec4_Success_Vec2ScalarScalar) { |
| auto* tc = vec4<f32>(vec2<f32>(), 1_f, 1_f); |
| WrapInFunction(tc); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_NE(TypeOf(tc), nullptr); |
| ASSERT_TRUE(TypeOf(tc)->Is<sem::Vector>()); |
| EXPECT_TRUE(TypeOf(tc)->As<sem::Vector>()->type()->Is<sem::F32>()); |
| EXPECT_EQ(TypeOf(tc)->As<sem::Vector>()->Width(), 4u); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec4_Success_ScalarVec2Scalar) { |
| auto* tc = vec4<f32>(1_f, vec2<f32>(), 1_f); |
| WrapInFunction(tc); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_NE(TypeOf(tc), nullptr); |
| ASSERT_TRUE(TypeOf(tc)->Is<sem::Vector>()); |
| EXPECT_TRUE(TypeOf(tc)->As<sem::Vector>()->type()->Is<sem::F32>()); |
| EXPECT_EQ(TypeOf(tc)->As<sem::Vector>()->Width(), 4u); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec4_Success_ScalarScalarVec2) { |
| auto* tc = vec4<f32>(1_f, 1_f, vec2<f32>()); |
| WrapInFunction(tc); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_NE(TypeOf(tc), nullptr); |
| ASSERT_TRUE(TypeOf(tc)->Is<sem::Vector>()); |
| EXPECT_TRUE(TypeOf(tc)->As<sem::Vector>()->type()->Is<sem::F32>()); |
| EXPECT_EQ(TypeOf(tc)->As<sem::Vector>()->Width(), 4u); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec4_Success_Vec2AndVec2) { |
| auto* tc = vec4<f32>(vec2<f32>(), vec2<f32>()); |
| WrapInFunction(tc); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_NE(TypeOf(tc), nullptr); |
| ASSERT_TRUE(TypeOf(tc)->Is<sem::Vector>()); |
| EXPECT_TRUE(TypeOf(tc)->As<sem::Vector>()->type()->Is<sem::F32>()); |
| EXPECT_EQ(TypeOf(tc)->As<sem::Vector>()->Width(), 4u); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec4_Success_Vec3AndScalar) { |
| auto* tc = vec4<f32>(vec3<f32>(), 1_f); |
| WrapInFunction(tc); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_NE(TypeOf(tc), nullptr); |
| ASSERT_TRUE(TypeOf(tc)->Is<sem::Vector>()); |
| EXPECT_TRUE(TypeOf(tc)->As<sem::Vector>()->type()->Is<sem::F32>()); |
| EXPECT_EQ(TypeOf(tc)->As<sem::Vector>()->Width(), 4u); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec4_Success_ScalarAndVec3) { |
| auto* tc = vec4<f32>(1_f, vec3<f32>()); |
| WrapInFunction(tc); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_NE(TypeOf(tc), nullptr); |
| ASSERT_TRUE(TypeOf(tc)->Is<sem::Vector>()); |
| EXPECT_TRUE(TypeOf(tc)->As<sem::Vector>()->type()->Is<sem::F32>()); |
| EXPECT_EQ(TypeOf(tc)->As<sem::Vector>()->Width(), 4u); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec4_Success_Identity) { |
| auto* tc = vec4<f32>(vec4<f32>()); |
| WrapInFunction(tc); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_NE(TypeOf(tc), nullptr); |
| ASSERT_TRUE(TypeOf(tc)->Is<sem::Vector>()); |
| EXPECT_TRUE(TypeOf(tc)->As<sem::Vector>()->type()->Is<sem::F32>()); |
| EXPECT_EQ(TypeOf(tc)->As<sem::Vector>()->Width(), 4u); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vec4_Success_Vec4TypeConversion) { |
| auto* tc = vec4<f32>(vec4<i32>()); |
| WrapInFunction(tc); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_NE(TypeOf(tc), nullptr); |
| ASSERT_TRUE(TypeOf(tc)->Is<sem::Vector>()); |
| EXPECT_TRUE(TypeOf(tc)->As<sem::Vector>()->type()->Is<sem::F32>()); |
| EXPECT_EQ(TypeOf(tc)->As<sem::Vector>()->Width(), 4u); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, |
| Expr_Constructor_NestedVectorConstructors_InnerError) { |
| WrapInFunction(vec4<f32>(vec4<f32>(1_f, 1_f, // |
| Construct(Source{{12, 34}}, ty.vec3<f32>(), 1_f, 1_f)), |
| 1_f)); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT(r()->error(), |
| HasSubstr("12:34 error: no matching constructor for vec3<f32>(f32, f32)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_NestedVectorConstructors_Success) { |
| auto* tc = vec4<f32>(vec3<f32>(vec2<f32>(1_f, 1_f), 1_f), 1_f); |
| WrapInFunction(tc); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_NE(TypeOf(tc), nullptr); |
| ASSERT_TRUE(TypeOf(tc)->Is<sem::Vector>()); |
| EXPECT_TRUE(TypeOf(tc)->As<sem::Vector>()->type()->Is<sem::F32>()); |
| EXPECT_EQ(TypeOf(tc)->As<sem::Vector>()->Width(), 4u); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vector_Alias_Argument_Error) { |
| auto* alias = Alias("UnsignedInt", ty.u32()); |
| GlobalVar("uint_var", ty.Of(alias), ast::StorageClass::kPrivate); |
| |
| auto* tc = Construct(Source{{12, 34}}, ty.vec2<f32>(), "uint_var"); |
| WrapInFunction(tc); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT(r()->error(), HasSubstr("12:34 error: no matching constructor for vec2<f32>(u32)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vector_Alias_Argument_Success) { |
| auto* f32_alias = Alias("Float32", ty.f32()); |
| auto* vec2_alias = Alias("VectorFloat2", ty.vec2<f32>()); |
| GlobalVar("my_f32", ty.Of(f32_alias), ast::StorageClass::kPrivate); |
| GlobalVar("my_vec2", ty.Of(vec2_alias), ast::StorageClass::kPrivate); |
| |
| auto* tc = vec3<f32>("my_vec2", "my_f32"); |
| WrapInFunction(tc); |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vector_ElementTypeAlias_Error) { |
| auto* f32_alias = Alias("Float32", ty.f32()); |
| |
| // vec2<Float32>(1.0f, 1u) |
| auto* vec_type = ty.vec(ty.Of(f32_alias), 2); |
| WrapInFunction(Construct(Source{{12, 34}}, vec_type, 1_f, 1_u)); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT(r()->error(), |
| HasSubstr("12:34 error: no matching constructor for vec2<f32>(f32, u32)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, Expr_Constructor_Vector_ElementTypeAlias_Success) { |
| auto* f32_alias = Alias("Float32", ty.f32()); |
| |
| // vec2<Float32>(1.0f, 1.0f) |
| auto* vec_type = ty.vec(ty.Of(f32_alias), 2); |
| auto* tc = Construct(Source{{12, 34}}, vec_type, 1_f, 1_f); |
| WrapInFunction(tc); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, |
| Expr_Constructor_Vector_ArgumentElementTypeAlias_Error) { |
| auto* f32_alias = Alias("Float32", ty.f32()); |
| |
| // vec3<u32>(vec<Float32>(), 1.0f) |
| auto* vec_type = ty.vec(ty.Of(f32_alias), 2); |
| WrapInFunction(Construct(Source{{12, 34}}, ty.vec3<u32>(), Construct(vec_type), 1_f)); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT(r()->error(), |
| HasSubstr("12:34 error: no matching constructor for vec3<u32>(vec2<f32>, f32)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, |
| Expr_Constructor_Vector_ArgumentElementTypeAlias_Success) { |
| auto* f32_alias = Alias("Float32", ty.f32()); |
| |
| // vec3<f32>(vec<Float32>(), 1.0f) |
| auto* vec_type = ty.vec(ty.Of(f32_alias), 2); |
| auto* tc = vec3<f32>(Construct(Source{{12, 34}}, vec_type), 1_f); |
| WrapInFunction(tc); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, InferVec2ElementTypeFromScalars) { |
| Enable(ast::Extension::kF16); |
| |
| auto* vec2_bool = Construct(create<ast::Vector>(nullptr, 2u), Expr(true), Expr(false)); |
| auto* vec2_i32 = Construct(create<ast::Vector>(nullptr, 2u), Expr(1_i), Expr(2_i)); |
| auto* vec2_u32 = Construct(create<ast::Vector>(nullptr, 2u), Expr(1_u), Expr(2_u)); |
| auto* vec2_f32 = Construct(create<ast::Vector>(nullptr, 2u), Expr(1_f), Expr(2_f)); |
| auto* vec2_f16 = Construct(create<ast::Vector>(nullptr, 2u), Expr(1_h), Expr(2_h)); |
| WrapInFunction(vec2_bool, vec2_i32, vec2_u32, vec2_f32, vec2_f16); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_TRUE(TypeOf(vec2_bool)->Is<sem::Vector>()); |
| ASSERT_TRUE(TypeOf(vec2_i32)->Is<sem::Vector>()); |
| ASSERT_TRUE(TypeOf(vec2_u32)->Is<sem::Vector>()); |
| ASSERT_TRUE(TypeOf(vec2_f32)->Is<sem::Vector>()); |
| ASSERT_TRUE(TypeOf(vec2_f16)->Is<sem::Vector>()); |
| EXPECT_TRUE(TypeOf(vec2_bool)->As<sem::Vector>()->type()->Is<sem::Bool>()); |
| EXPECT_TRUE(TypeOf(vec2_i32)->As<sem::Vector>()->type()->Is<sem::I32>()); |
| EXPECT_TRUE(TypeOf(vec2_u32)->As<sem::Vector>()->type()->Is<sem::U32>()); |
| EXPECT_TRUE(TypeOf(vec2_f32)->As<sem::Vector>()->type()->Is<sem::F32>()); |
| EXPECT_TRUE(TypeOf(vec2_f16)->As<sem::Vector>()->type()->Is<sem::F16>()); |
| EXPECT_EQ(TypeOf(vec2_bool)->As<sem::Vector>()->Width(), 2u); |
| EXPECT_EQ(TypeOf(vec2_i32)->As<sem::Vector>()->Width(), 2u); |
| EXPECT_EQ(TypeOf(vec2_u32)->As<sem::Vector>()->Width(), 2u); |
| EXPECT_EQ(TypeOf(vec2_f32)->As<sem::Vector>()->Width(), 2u); |
| EXPECT_EQ(TypeOf(vec2_f16)->As<sem::Vector>()->Width(), 2u); |
| EXPECT_EQ(TypeOf(vec2_bool), TypeOf(vec2_bool->target.type)); |
| EXPECT_EQ(TypeOf(vec2_i32), TypeOf(vec2_i32->target.type)); |
| EXPECT_EQ(TypeOf(vec2_u32), TypeOf(vec2_u32->target.type)); |
| EXPECT_EQ(TypeOf(vec2_f32), TypeOf(vec2_f32->target.type)); |
| EXPECT_EQ(TypeOf(vec2_f16), TypeOf(vec2_f16->target.type)); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, InferVec2ElementTypeFromVec2) { |
| Enable(ast::Extension::kF16); |
| |
| auto* vec2_bool = Construct(create<ast::Vector>(nullptr, 2u), vec2<bool>(true, false)); |
| auto* vec2_i32 = Construct(create<ast::Vector>(nullptr, 2u), vec2<i32>(1_i, 2_i)); |
| auto* vec2_u32 = Construct(create<ast::Vector>(nullptr, 2u), vec2<u32>(1_u, 2_u)); |
| auto* vec2_f32 = Construct(create<ast::Vector>(nullptr, 2u), vec2<f32>(1_f, 2_f)); |
| auto* vec2_f16 = Construct(create<ast::Vector>(nullptr, 2u), vec2<f16>(1_h, 2_h)); |
| WrapInFunction(vec2_bool, vec2_i32, vec2_u32, vec2_f32, vec2_f16); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_TRUE(TypeOf(vec2_bool)->Is<sem::Vector>()); |
| ASSERT_TRUE(TypeOf(vec2_i32)->Is<sem::Vector>()); |
| ASSERT_TRUE(TypeOf(vec2_u32)->Is<sem::Vector>()); |
| ASSERT_TRUE(TypeOf(vec2_f32)->Is<sem::Vector>()); |
| ASSERT_TRUE(TypeOf(vec2_f16)->Is<sem::Vector>()); |
| EXPECT_TRUE(TypeOf(vec2_bool)->As<sem::Vector>()->type()->Is<sem::Bool>()); |
| EXPECT_TRUE(TypeOf(vec2_i32)->As<sem::Vector>()->type()->Is<sem::I32>()); |
| EXPECT_TRUE(TypeOf(vec2_u32)->As<sem::Vector>()->type()->Is<sem::U32>()); |
| EXPECT_TRUE(TypeOf(vec2_f32)->As<sem::Vector>()->type()->Is<sem::F32>()); |
| EXPECT_TRUE(TypeOf(vec2_f16)->As<sem::Vector>()->type()->Is<sem::F16>()); |
| EXPECT_EQ(TypeOf(vec2_bool)->As<sem::Vector>()->Width(), 2u); |
| EXPECT_EQ(TypeOf(vec2_i32)->As<sem::Vector>()->Width(), 2u); |
| EXPECT_EQ(TypeOf(vec2_u32)->As<sem::Vector>()->Width(), 2u); |
| EXPECT_EQ(TypeOf(vec2_f32)->As<sem::Vector>()->Width(), 2u); |
| EXPECT_EQ(TypeOf(vec2_f16)->As<sem::Vector>()->Width(), 2u); |
| EXPECT_EQ(TypeOf(vec2_bool), TypeOf(vec2_bool->target.type)); |
| EXPECT_EQ(TypeOf(vec2_i32), TypeOf(vec2_i32->target.type)); |
| EXPECT_EQ(TypeOf(vec2_u32), TypeOf(vec2_u32->target.type)); |
| EXPECT_EQ(TypeOf(vec2_f32), TypeOf(vec2_f32->target.type)); |
| EXPECT_EQ(TypeOf(vec2_f16), TypeOf(vec2_f16->target.type)); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, InferVec3ElementTypeFromScalars) { |
| Enable(ast::Extension::kF16); |
| |
| auto* vec3_bool = |
| Construct(create<ast::Vector>(nullptr, 3u), Expr(true), Expr(false), Expr(true)); |
| auto* vec3_i32 = Construct(create<ast::Vector>(nullptr, 3u), Expr(1_i), Expr(2_i), Expr(3_i)); |
| auto* vec3_u32 = Construct(create<ast::Vector>(nullptr, 3u), Expr(1_u), Expr(2_u), Expr(3_u)); |
| auto* vec3_f32 = Construct(create<ast::Vector>(nullptr, 3u), Expr(1_f), Expr(2_f), Expr(3_f)); |
| auto* vec3_f16 = Construct(create<ast::Vector>(nullptr, 3u), Expr(1_h), Expr(2_h), Expr(3_h)); |
| WrapInFunction(vec3_bool, vec3_i32, vec3_u32, vec3_f32, vec3_f16); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_TRUE(TypeOf(vec3_bool)->Is<sem::Vector>()); |
| ASSERT_TRUE(TypeOf(vec3_i32)->Is<sem::Vector>()); |
| ASSERT_TRUE(TypeOf(vec3_u32)->Is<sem::Vector>()); |
| ASSERT_TRUE(TypeOf(vec3_f32)->Is<sem::Vector>()); |
| ASSERT_TRUE(TypeOf(vec3_f16)->Is<sem::Vector>()); |
| EXPECT_TRUE(TypeOf(vec3_bool)->As<sem::Vector>()->type()->Is<sem::Bool>()); |
| EXPECT_TRUE(TypeOf(vec3_i32)->As<sem::Vector>()->type()->Is<sem::I32>()); |
| EXPECT_TRUE(TypeOf(vec3_u32)->As<sem::Vector>()->type()->Is<sem::U32>()); |
| EXPECT_TRUE(TypeOf(vec3_f32)->As<sem::Vector>()->type()->Is<sem::F32>()); |
| EXPECT_TRUE(TypeOf(vec3_f16)->As<sem::Vector>()->type()->Is<sem::F16>()); |
| EXPECT_EQ(TypeOf(vec3_bool)->As<sem::Vector>()->Width(), 3u); |
| EXPECT_EQ(TypeOf(vec3_i32)->As<sem::Vector>()->Width(), 3u); |
| EXPECT_EQ(TypeOf(vec3_u32)->As<sem::Vector>()->Width(), 3u); |
| EXPECT_EQ(TypeOf(vec3_f32)->As<sem::Vector>()->Width(), 3u); |
| EXPECT_EQ(TypeOf(vec3_f16)->As<sem::Vector>()->Width(), 3u); |
| EXPECT_EQ(TypeOf(vec3_bool), TypeOf(vec3_bool->target.type)); |
| EXPECT_EQ(TypeOf(vec3_i32), TypeOf(vec3_i32->target.type)); |
| EXPECT_EQ(TypeOf(vec3_u32), TypeOf(vec3_u32->target.type)); |
| EXPECT_EQ(TypeOf(vec3_f32), TypeOf(vec3_f32->target.type)); |
| EXPECT_EQ(TypeOf(vec3_f16), TypeOf(vec3_f16->target.type)); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, InferVec3ElementTypeFromVec3) { |
| Enable(ast::Extension::kF16); |
| |
| auto* vec3_bool = Construct(create<ast::Vector>(nullptr, 3u), vec3<bool>(true, false, true)); |
| auto* vec3_i32 = Construct(create<ast::Vector>(nullptr, 3u), vec3<i32>(1_i, 2_i, 3_i)); |
| auto* vec3_u32 = Construct(create<ast::Vector>(nullptr, 3u), vec3<u32>(1_u, 2_u, 3_u)); |
| auto* vec3_f32 = Construct(create<ast::Vector>(nullptr, 3u), vec3<f32>(1_f, 2_f, 3_f)); |
| auto* vec3_f16 = Construct(create<ast::Vector>(nullptr, 3u), vec3<f16>(1_h, 2_h, 3_h)); |
| WrapInFunction(vec3_bool, vec3_i32, vec3_u32, vec3_f32, vec3_f16); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_TRUE(TypeOf(vec3_bool)->Is<sem::Vector>()); |
| ASSERT_TRUE(TypeOf(vec3_i32)->Is<sem::Vector>()); |
| ASSERT_TRUE(TypeOf(vec3_u32)->Is<sem::Vector>()); |
| ASSERT_TRUE(TypeOf(vec3_f32)->Is<sem::Vector>()); |
| ASSERT_TRUE(TypeOf(vec3_f16)->Is<sem::Vector>()); |
| EXPECT_TRUE(TypeOf(vec3_bool)->As<sem::Vector>()->type()->Is<sem::Bool>()); |
| EXPECT_TRUE(TypeOf(vec3_i32)->As<sem::Vector>()->type()->Is<sem::I32>()); |
| EXPECT_TRUE(TypeOf(vec3_u32)->As<sem::Vector>()->type()->Is<sem::U32>()); |
| EXPECT_TRUE(TypeOf(vec3_f32)->As<sem::Vector>()->type()->Is<sem::F32>()); |
| EXPECT_TRUE(TypeOf(vec3_f16)->As<sem::Vector>()->type()->Is<sem::F16>()); |
| EXPECT_EQ(TypeOf(vec3_bool)->As<sem::Vector>()->Width(), 3u); |
| EXPECT_EQ(TypeOf(vec3_i32)->As<sem::Vector>()->Width(), 3u); |
| EXPECT_EQ(TypeOf(vec3_u32)->As<sem::Vector>()->Width(), 3u); |
| EXPECT_EQ(TypeOf(vec3_f32)->As<sem::Vector>()->Width(), 3u); |
| EXPECT_EQ(TypeOf(vec3_f16)->As<sem::Vector>()->Width(), 3u); |
| EXPECT_EQ(TypeOf(vec3_bool), TypeOf(vec3_bool->target.type)); |
| EXPECT_EQ(TypeOf(vec3_i32), TypeOf(vec3_i32->target.type)); |
| EXPECT_EQ(TypeOf(vec3_u32), TypeOf(vec3_u32->target.type)); |
| EXPECT_EQ(TypeOf(vec3_f32), TypeOf(vec3_f32->target.type)); |
| EXPECT_EQ(TypeOf(vec3_f16), TypeOf(vec3_f16->target.type)); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, InferVec3ElementTypeFromScalarAndVec2) { |
| Enable(ast::Extension::kF16); |
| |
| auto* vec3_bool = |
| Construct(create<ast::Vector>(nullptr, 3u), Expr(true), vec2<bool>(false, true)); |
| auto* vec3_i32 = Construct(create<ast::Vector>(nullptr, 3u), Expr(1_i), vec2<i32>(2_i, 3_i)); |
| auto* vec3_u32 = Construct(create<ast::Vector>(nullptr, 3u), Expr(1_u), vec2<u32>(2_u, 3_u)); |
| auto* vec3_f32 = Construct(create<ast::Vector>(nullptr, 3u), Expr(1_f), vec2<f32>(2_f, 3_f)); |
| auto* vec3_f16 = Construct(create<ast::Vector>(nullptr, 3u), Expr(1_h), vec2<f16>(2_h, 3_h)); |
| WrapInFunction(vec3_bool, vec3_i32, vec3_u32, vec3_f32, vec3_f16); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_TRUE(TypeOf(vec3_bool)->Is<sem::Vector>()); |
| ASSERT_TRUE(TypeOf(vec3_i32)->Is<sem::Vector>()); |
| ASSERT_TRUE(TypeOf(vec3_u32)->Is<sem::Vector>()); |
| ASSERT_TRUE(TypeOf(vec3_f32)->Is<sem::Vector>()); |
| ASSERT_TRUE(TypeOf(vec3_f16)->Is<sem::Vector>()); |
| EXPECT_TRUE(TypeOf(vec3_bool)->As<sem::Vector>()->type()->Is<sem::Bool>()); |
| EXPECT_TRUE(TypeOf(vec3_i32)->As<sem::Vector>()->type()->Is<sem::I32>()); |
| EXPECT_TRUE(TypeOf(vec3_u32)->As<sem::Vector>()->type()->Is<sem::U32>()); |
| EXPECT_TRUE(TypeOf(vec3_f32)->As<sem::Vector>()->type()->Is<sem::F32>()); |
| EXPECT_TRUE(TypeOf(vec3_f16)->As<sem::Vector>()->type()->Is<sem::F16>()); |
| EXPECT_EQ(TypeOf(vec3_bool)->As<sem::Vector>()->Width(), 3u); |
| EXPECT_EQ(TypeOf(vec3_i32)->As<sem::Vector>()->Width(), 3u); |
| EXPECT_EQ(TypeOf(vec3_u32)->As<sem::Vector>()->Width(), 3u); |
| EXPECT_EQ(TypeOf(vec3_f32)->As<sem::Vector>()->Width(), 3u); |
| EXPECT_EQ(TypeOf(vec3_f16)->As<sem::Vector>()->Width(), 3u); |
| EXPECT_EQ(TypeOf(vec3_bool), TypeOf(vec3_bool->target.type)); |
| EXPECT_EQ(TypeOf(vec3_i32), TypeOf(vec3_i32->target.type)); |
| EXPECT_EQ(TypeOf(vec3_u32), TypeOf(vec3_u32->target.type)); |
| EXPECT_EQ(TypeOf(vec3_f32), TypeOf(vec3_f32->target.type)); |
| EXPECT_EQ(TypeOf(vec3_f16), TypeOf(vec3_f16->target.type)); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, InferVec4ElementTypeFromScalars) { |
| Enable(ast::Extension::kF16); |
| |
| auto* vec4_bool = Construct(create<ast::Vector>(nullptr, 4u), Expr(true), Expr(false), |
| Expr(true), Expr(false)); |
| auto* vec4_i32 = |
| Construct(create<ast::Vector>(nullptr, 4u), Expr(1_i), Expr(2_i), Expr(3_i), Expr(4_i)); |
| auto* vec4_u32 = |
| Construct(create<ast::Vector>(nullptr, 4u), Expr(1_u), Expr(2_u), Expr(3_u), Expr(4_u)); |
| auto* vec4_f32 = |
| Construct(create<ast::Vector>(nullptr, 4u), Expr(1_f), Expr(2_f), Expr(3_f), Expr(4_f)); |
| auto* vec4_f16 = |
| Construct(create<ast::Vector>(nullptr, 4u), Expr(1_h), Expr(2_h), Expr(3_h), Expr(4_h)); |
| WrapInFunction(vec4_bool, vec4_i32, vec4_u32, vec4_f32, vec4_f16); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_TRUE(TypeOf(vec4_bool)->Is<sem::Vector>()); |
| ASSERT_TRUE(TypeOf(vec4_i32)->Is<sem::Vector>()); |
| ASSERT_TRUE(TypeOf(vec4_u32)->Is<sem::Vector>()); |
| ASSERT_TRUE(TypeOf(vec4_f32)->Is<sem::Vector>()); |
| ASSERT_TRUE(TypeOf(vec4_f16)->Is<sem::Vector>()); |
| EXPECT_TRUE(TypeOf(vec4_bool)->As<sem::Vector>()->type()->Is<sem::Bool>()); |
| EXPECT_TRUE(TypeOf(vec4_i32)->As<sem::Vector>()->type()->Is<sem::I32>()); |
| EXPECT_TRUE(TypeOf(vec4_u32)->As<sem::Vector>()->type()->Is<sem::U32>()); |
| EXPECT_TRUE(TypeOf(vec4_f32)->As<sem::Vector>()->type()->Is<sem::F32>()); |
| EXPECT_TRUE(TypeOf(vec4_f16)->As<sem::Vector>()->type()->Is<sem::F16>()); |
| EXPECT_EQ(TypeOf(vec4_bool)->As<sem::Vector>()->Width(), 4u); |
| EXPECT_EQ(TypeOf(vec4_i32)->As<sem::Vector>()->Width(), 4u); |
| EXPECT_EQ(TypeOf(vec4_u32)->As<sem::Vector>()->Width(), 4u); |
| EXPECT_EQ(TypeOf(vec4_f32)->As<sem::Vector>()->Width(), 4u); |
| EXPECT_EQ(TypeOf(vec4_f16)->As<sem::Vector>()->Width(), 4u); |
| EXPECT_EQ(TypeOf(vec4_bool), TypeOf(vec4_bool->target.type)); |
| EXPECT_EQ(TypeOf(vec4_i32), TypeOf(vec4_i32->target.type)); |
| EXPECT_EQ(TypeOf(vec4_u32), TypeOf(vec4_u32->target.type)); |
| EXPECT_EQ(TypeOf(vec4_f32), TypeOf(vec4_f32->target.type)); |
| EXPECT_EQ(TypeOf(vec4_f16), TypeOf(vec4_f16->target.type)); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, InferVec4ElementTypeFromVec4) { |
| Enable(ast::Extension::kF16); |
| |
| auto* vec4_bool = |
| Construct(create<ast::Vector>(nullptr, 4u), vec4<bool>(true, false, true, false)); |
| auto* vec4_i32 = Construct(create<ast::Vector>(nullptr, 4u), vec4<i32>(1_i, 2_i, 3_i, 4_i)); |
| auto* vec4_u32 = Construct(create<ast::Vector>(nullptr, 4u), vec4<u32>(1_u, 2_u, 3_u, 4_u)); |
| auto* vec4_f32 = Construct(create<ast::Vector>(nullptr, 4u), vec4<f32>(1_f, 2_f, 3_f, 4_f)); |
| auto* vec4_f16 = Construct(create<ast::Vector>(nullptr, 4u), vec4<f16>(1_h, 2_h, 3_h, 4_h)); |
| WrapInFunction(vec4_bool, vec4_i32, vec4_u32, vec4_f32, vec4_f16); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_TRUE(TypeOf(vec4_bool)->Is<sem::Vector>()); |
| ASSERT_TRUE(TypeOf(vec4_i32)->Is<sem::Vector>()); |
| ASSERT_TRUE(TypeOf(vec4_u32)->Is<sem::Vector>()); |
| ASSERT_TRUE(TypeOf(vec4_f32)->Is<sem::Vector>()); |
| ASSERT_TRUE(TypeOf(vec4_f16)->Is<sem::Vector>()); |
| EXPECT_TRUE(TypeOf(vec4_bool)->As<sem::Vector>()->type()->Is<sem::Bool>()); |
| EXPECT_TRUE(TypeOf(vec4_i32)->As<sem::Vector>()->type()->Is<sem::I32>()); |
| EXPECT_TRUE(TypeOf(vec4_u32)->As<sem::Vector>()->type()->Is<sem::U32>()); |
| EXPECT_TRUE(TypeOf(vec4_f32)->As<sem::Vector>()->type()->Is<sem::F32>()); |
| EXPECT_TRUE(TypeOf(vec4_f16)->As<sem::Vector>()->type()->Is<sem::F16>()); |
| EXPECT_EQ(TypeOf(vec4_bool)->As<sem::Vector>()->Width(), 4u); |
| EXPECT_EQ(TypeOf(vec4_i32)->As<sem::Vector>()->Width(), 4u); |
| EXPECT_EQ(TypeOf(vec4_u32)->As<sem::Vector>()->Width(), 4u); |
| EXPECT_EQ(TypeOf(vec4_f32)->As<sem::Vector>()->Width(), 4u); |
| EXPECT_EQ(TypeOf(vec4_f16)->As<sem::Vector>()->Width(), 4u); |
| EXPECT_EQ(TypeOf(vec4_bool), TypeOf(vec4_bool->target.type)); |
| EXPECT_EQ(TypeOf(vec4_i32), TypeOf(vec4_i32->target.type)); |
| EXPECT_EQ(TypeOf(vec4_u32), TypeOf(vec4_u32->target.type)); |
| EXPECT_EQ(TypeOf(vec4_f32), TypeOf(vec4_f32->target.type)); |
| EXPECT_EQ(TypeOf(vec4_f16), TypeOf(vec4_f16->target.type)); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, InferVec4ElementTypeFromScalarAndVec3) { |
| Enable(ast::Extension::kF16); |
| |
| auto* vec4_bool = |
| Construct(create<ast::Vector>(nullptr, 4u), Expr(true), vec3<bool>(false, true, false)); |
| auto* vec4_i32 = |
| Construct(create<ast::Vector>(nullptr, 4u), Expr(1_i), vec3<i32>(2_i, 3_i, 4_i)); |
| auto* vec4_u32 = |
| Construct(create<ast::Vector>(nullptr, 4u), Expr(1_u), vec3<u32>(2_u, 3_u, 4_u)); |
| auto* vec4_f32 = |
| Construct(create<ast::Vector>(nullptr, 4u), Expr(1_f), vec3<f32>(2_f, 3_f, 4_f)); |
| auto* vec4_f16 = |
| Construct(create<ast::Vector>(nullptr, 4u), Expr(1_h), vec3<f16>(2_h, 3_h, 4_h)); |
| WrapInFunction(vec4_bool, vec4_i32, vec4_u32, vec4_f32, vec4_f16); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_TRUE(TypeOf(vec4_bool)->Is<sem::Vector>()); |
| ASSERT_TRUE(TypeOf(vec4_i32)->Is<sem::Vector>()); |
| ASSERT_TRUE(TypeOf(vec4_u32)->Is<sem::Vector>()); |
| ASSERT_TRUE(TypeOf(vec4_f32)->Is<sem::Vector>()); |
| ASSERT_TRUE(TypeOf(vec4_f16)->Is<sem::Vector>()); |
| EXPECT_TRUE(TypeOf(vec4_bool)->As<sem::Vector>()->type()->Is<sem::Bool>()); |
| EXPECT_TRUE(TypeOf(vec4_i32)->As<sem::Vector>()->type()->Is<sem::I32>()); |
| EXPECT_TRUE(TypeOf(vec4_u32)->As<sem::Vector>()->type()->Is<sem::U32>()); |
| EXPECT_TRUE(TypeOf(vec4_f32)->As<sem::Vector>()->type()->Is<sem::F32>()); |
| EXPECT_TRUE(TypeOf(vec4_f16)->As<sem::Vector>()->type()->Is<sem::F16>()); |
| EXPECT_EQ(TypeOf(vec4_bool)->As<sem::Vector>()->Width(), 4u); |
| EXPECT_EQ(TypeOf(vec4_i32)->As<sem::Vector>()->Width(), 4u); |
| EXPECT_EQ(TypeOf(vec4_u32)->As<sem::Vector>()->Width(), 4u); |
| EXPECT_EQ(TypeOf(vec4_f32)->As<sem::Vector>()->Width(), 4u); |
| EXPECT_EQ(TypeOf(vec4_f16)->As<sem::Vector>()->Width(), 4u); |
| EXPECT_EQ(TypeOf(vec4_bool), TypeOf(vec4_bool->target.type)); |
| EXPECT_EQ(TypeOf(vec4_i32), TypeOf(vec4_i32->target.type)); |
| EXPECT_EQ(TypeOf(vec4_u32), TypeOf(vec4_u32->target.type)); |
| EXPECT_EQ(TypeOf(vec4_f32), TypeOf(vec4_f32->target.type)); |
| EXPECT_EQ(TypeOf(vec4_f16), TypeOf(vec4_f16->target.type)); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, InferVec4ElementTypeFromVec2AndVec2) { |
| Enable(ast::Extension::kF16); |
| |
| auto* vec4_bool = Construct(create<ast::Vector>(nullptr, 4u), vec2<bool>(true, false), |
| vec2<bool>(true, false)); |
| auto* vec4_i32 = |
| Construct(create<ast::Vector>(nullptr, 4u), vec2<i32>(1_i, 2_i), vec2<i32>(3_i, 4_i)); |
| auto* vec4_u32 = |
| Construct(create<ast::Vector>(nullptr, 4u), vec2<u32>(1_u, 2_u), vec2<u32>(3_u, 4_u)); |
| auto* vec4_f32 = |
| Construct(create<ast::Vector>(nullptr, 4u), vec2<f32>(1_f, 2_f), vec2<f32>(3_f, 4_f)); |
| auto* vec4_f16 = |
| Construct(create<ast::Vector>(nullptr, 4u), vec2<f16>(1_h, 2_h), vec2<f16>(3_h, 4_h)); |
| WrapInFunction(vec4_bool, vec4_i32, vec4_u32, vec4_f32, vec4_f16); |
| |
| ASSERT_TRUE(r()->Resolve()) << r()->error(); |
| |
| ASSERT_TRUE(TypeOf(vec4_bool)->Is<sem::Vector>()); |
| ASSERT_TRUE(TypeOf(vec4_i32)->Is<sem::Vector>()); |
| ASSERT_TRUE(TypeOf(vec4_u32)->Is<sem::Vector>()); |
| ASSERT_TRUE(TypeOf(vec4_f32)->Is<sem::Vector>()); |
| ASSERT_TRUE(TypeOf(vec4_f16)->Is<sem::Vector>()); |
| EXPECT_TRUE(TypeOf(vec4_bool)->As<sem::Vector>()->type()->Is<sem::Bool>()); |
| EXPECT_TRUE(TypeOf(vec4_i32)->As<sem::Vector>()->type()->Is<sem::I32>()); |
| EXPECT_TRUE(TypeOf(vec4_u32)->As<sem::Vector>()->type()->Is<sem::U32>()); |
| EXPECT_TRUE(TypeOf(vec4_f32)->As<sem::Vector>()->type()->Is<sem::F32>()); |
| EXPECT_TRUE(TypeOf(vec4_f16)->As<sem::Vector>()->type()->Is<sem::F16>()); |
| EXPECT_EQ(TypeOf(vec4_bool)->As<sem::Vector>()->Width(), 4u); |
| EXPECT_EQ(TypeOf(vec4_i32)->As<sem::Vector>()->Width(), 4u); |
| EXPECT_EQ(TypeOf(vec4_u32)->As<sem::Vector>()->Width(), 4u); |
| EXPECT_EQ(TypeOf(vec4_f32)->As<sem::Vector>()->Width(), 4u); |
| EXPECT_EQ(TypeOf(vec4_f16)->As<sem::Vector>()->Width(), 4u); |
| EXPECT_EQ(TypeOf(vec4_bool), TypeOf(vec4_bool->target.type)); |
| EXPECT_EQ(TypeOf(vec4_i32), TypeOf(vec4_i32->target.type)); |
| EXPECT_EQ(TypeOf(vec4_u32), TypeOf(vec4_u32->target.type)); |
| EXPECT_EQ(TypeOf(vec4_f32), TypeOf(vec4_f32->target.type)); |
| EXPECT_EQ(TypeOf(vec4_f16), TypeOf(vec4_f16->target.type)); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, CannotInferVectorElementTypeWithoutArgs) { |
| WrapInFunction(Construct(Source{{12, 34}}, create<ast::Vector>(nullptr, 3u))); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT(r()->error(), HasSubstr("12:34 error: no matching constructor for vec3()")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, CannotInferVec2ElementTypeFromScalarsMismatch) { |
| WrapInFunction(Construct(Source{{1, 1}}, create<ast::Vector>(nullptr, 2u), |
| Expr(Source{{1, 2}}, 1_i), // |
| Expr(Source{{1, 3}}, 2_u))); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT(r()->error(), HasSubstr("1:1 error: no matching constructor for vec2(i32, u32)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, CannotInferVec3ElementTypeFromScalarsMismatch) { |
| WrapInFunction(Construct(Source{{1, 1}}, create<ast::Vector>(nullptr, 3u), |
| Expr(Source{{1, 2}}, 1_i), // |
| Expr(Source{{1, 3}}, 2_u), // |
| Expr(Source{{1, 4}}, 3_i))); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT(r()->error(), |
| HasSubstr("1:1 error: no matching constructor for vec3(i32, u32, i32)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, CannotInferVec3ElementTypeFromScalarAndVec2Mismatch) { |
| WrapInFunction(Construct(Source{{1, 1}}, create<ast::Vector>(nullptr, 3u), |
| Expr(Source{{1, 2}}, 1_i), // |
| Construct(Source{{1, 3}}, ty.vec2<f32>(), 2_f, 3_f))); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT(r()->error(), |
| HasSubstr("1:1 error: no matching constructor for vec3(i32, vec2<f32>)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, CannotInferVec4ElementTypeFromScalarsMismatch) { |
| WrapInFunction(Construct(Source{{1, 1}}, create<ast::Vector>(nullptr, 4u), |
| Expr(Source{{1, 2}}, 1_i), // |
| Expr(Source{{1, 3}}, 2_i), // |
| Expr(Source{{1, 4}}, 3_f), // |
| Expr(Source{{1, 5}}, 4_i))); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT(r()->error(), |
| HasSubstr("1:1 error: no matching constructor for vec4(i32, i32, f32, i32)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, CannotInferVec4ElementTypeFromScalarAndVec3Mismatch) { |
| WrapInFunction(Construct(Source{{1, 1}}, create<ast::Vector>(nullptr, 4u), |
| Expr(Source{{1, 2}}, 1_i), // |
| Construct(Source{{1, 3}}, ty.vec3<u32>(), 2_u, 3_u, 4_u))); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT(r()->error(), |
| HasSubstr("1:1 error: no matching constructor for vec4(i32, vec3<u32>)")); |
| } |
| |
| TEST_F(ResolverTypeConstructorValidationTest, CannotInferVec4ElementTypeFromVec2AndVec2Mismatch) { |
| WrapInFunction(Construct(Source{{1, 1}}, create<ast::Vector>(nullptr, 4u), |
| Construct(Source{{1, 2}}, ty.vec2<i32>(), 3_i, 4_i), // |
| Construct(Source{{1, 3}}, ty.vec2<u32>(), 3_u, 4_u))); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT(r()->error(), |
| HasSubstr("1:1 error: no matching constructor for vec4(vec2<i32>, vec2<u32>)")); |
| } |
| |
| } // namespace VectorConstructor |
| |
| namespace MatrixConstructor { |
| |
| struct MatrixParams { |
| using name_func_ptr = std::string (*)(); |
| |
| uint32_t rows; |
| uint32_t columns; |
| name_func_ptr get_element_type_name; |
| builder::ast_type_func_ptr create_element_ast_type; |
| builder::ast_expr_func_ptr create_element_ast_value; |
| builder::ast_type_func_ptr create_column_ast_type; |
| builder::ast_type_func_ptr create_mat_ast_type; |
| }; |
| |
| template <typename T, uint32_t R, uint32_t C> |
| constexpr MatrixParams MatrixParamsFor() { |
| return MatrixParams{ |
| R, |
| C, |
| DataType<T>::Name, |
| DataType<T>::AST, |
| DataType<T>::Expr, |
| DataType<tint::resolver::builder::vec<R, T>>::AST, |
| DataType<tint::resolver::builder::mat<C, R, T>>::AST, |
| }; |
| } |
| |
| static std::string MatrixStr(const MatrixParams& param) { |
| return "mat" + std::to_string(param.columns) + "x" + std::to_string(param.rows) + "<" + |
| param.get_element_type_name() + ">"; |
| } |
| |
| using MatrixConstructorTest = ResolverTestWithParam<MatrixParams>; |
| |
| TEST_P(MatrixConstructorTest, Expr_ColumnConstructor_Error_TooFewArguments) { |
| // matNxM<f32>(vecM<f32>(), ...); with N - 1 arguments |
| // matNxM<f16>(vecM<f16>(), ...); with N - 1 arguments |
| |
| const auto param = GetParam(); |
| |
| Enable(ast::Extension::kF16); |
| |
| const std::string element_type_name = param.get_element_type_name(); |
| std::stringstream args_tys; |
| ast::ExpressionList args; |
| for (uint32_t i = 0; i < param.columns - 1; i++) { |
| auto* vec_type = param.create_column_ast_type(*this); |
| args.push_back(Construct(vec_type)); |
| if (i > 0) { |
| args_tys << ", "; |
| } |
| args_tys << "vec" << param.rows << "<" + element_type_name + ">"; |
| } |
| |
| auto* matrix_type = param.create_mat_ast_type(*this); |
| auto* tc = Construct(Source{{12, 34}}, matrix_type, std::move(args)); |
| WrapInFunction(tc); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT(r()->error(), HasSubstr("12:34 error: no matching constructor for " + |
| MatrixStr(param) + "(" + args_tys.str() + ")")); |
| } |
| |
| TEST_P(MatrixConstructorTest, Expr_ElementConstructor_Error_TooFewArguments) { |
| // matNxM<f32>(f32,...,f32); with N*M - 1 arguments |
| // matNxM<f16>(f16,...,f16); with N*M - 1 arguments |
| |
| const auto param = GetParam(); |
| |
| Enable(ast::Extension::kF16); |
| |
| const std::string element_type_name = param.get_element_type_name(); |
| std::stringstream args_tys; |
| ast::ExpressionList args; |
| for (uint32_t i = 0; i < param.columns * param.rows - 1; i++) { |
| args.push_back(Construct(param.create_element_ast_type(*this))); |
| if (i > 0) { |
| args_tys << ", "; |
| } |
| args_tys << element_type_name; |
| } |
| |
| auto* matrix_type = param.create_mat_ast_type(*this); |
| auto* tc = Construct(Source{{12, 34}}, matrix_type, std::move(args)); |
| WrapInFunction(tc); |
| |
| EXPECT_FALSE(r()->Resolve()); |
| EXPECT_THAT(r()->error(), HasSubstr("12:34 error: no matching constructor for " + |
| MatrixStr(param) + "(" + args_tys.str() + ")")); |
| } |
| |
| TEST_P(MatrixConstructorTest, Expr_ColumnConstructor_Error_TooManyArguments) { |
| |