From 8a083ce9c8911f58a7a2509843d4599faefd984e Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Mon, 30 Nov 2020 23:30:58 +0000 Subject: [PATCH] Replace Type::(Is|As)Vector with Castable Change-Id: Ic838aa783a279d0939a972773206fee2e33c4bff Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/34274 Reviewed-by: dan sinclair --- src/ast/type/access_control_type_test.cc | 3 +- src/ast/type/alias_type_test.cc | 3 +- src/ast/type/array_type_test.cc | 3 +- src/ast/type/bool_type_test.cc | 3 +- src/ast/type/depth_texture_type_test.cc | 3 +- src/ast/type/f32_type_test.cc | 3 +- src/ast/type/i32_type_test.cc | 3 +- src/ast/type/matrix_type_test.cc | 3 +- .../type/multisampled_texture_type_test.cc | 3 +- src/ast/type/pointer_type_test.cc | 3 +- src/ast/type/sampled_texture_type_test.cc | 3 +- src/ast/type/sampler_type_test.cc | 3 +- src/ast/type/storage_texture_type_test.cc | 3 +- src/ast/type/struct_type_test.cc | 2 +- src/ast/type/type.cc | 26 +-- src/ast/type/type.h | 7 - src/ast/type/u32_type_test.cc | 3 +- src/ast/type/vector_type.cc | 4 - src/ast/type/vector_type.h | 3 - src/ast/type/vector_type_test.cc | 2 +- src/inspector/inspector.cc | 4 +- src/reader/spirv/function.cc | 8 +- src/reader/spirv/parser_impl.cc | 8 +- .../spirv/parser_impl_convert_type_test.cc | 63 ++--- .../wgsl/parser_impl_const_expr_test.cc | 4 +- src/reader/wgsl/parser_impl_type_decl_test.cc | 8 +- .../bound_array_accessors_transform.cc | 10 +- src/type_determiner.cc | 47 ++-- src/type_determiner_test.cc | 219 +++++++++++------- src/writer/hlsl/generator_impl.cc | 31 +-- src/writer/msl/generator_impl.cc | 17 +- src/writer/pack_coord_arrayidx.cc | 6 +- src/writer/spirv/builder.cc | 34 +-- src/writer/spirv/builder.h | 1 + src/writer/wgsl/generator_impl.cc | 4 +- 35 files changed, 311 insertions(+), 239 deletions(-) diff --git a/src/ast/type/access_control_type_test.cc b/src/ast/type/access_control_type_test.cc index e02ee23765..9a9bacc2e7 100644 --- a/src/ast/type/access_control_type_test.cc +++ b/src/ast/type/access_control_type_test.cc @@ -32,6 +32,7 @@ #include "src/ast/type/struct_type.h" #include "src/ast/type/texture_type.h" #include "src/ast/type/u32_type.h" +#include "src/ast/type/vector_type.h" namespace tint { namespace ast { @@ -64,7 +65,7 @@ TEST_F(AccessControlTypeTest, Is) { EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); - EXPECT_FALSE(ty->IsVector()); + EXPECT_FALSE(ty->Is()); } TEST_F(AccessControlTypeTest, AccessRead) { diff --git a/src/ast/type/alias_type_test.cc b/src/ast/type/alias_type_test.cc index 46259a02dc..2a2d35b0b6 100644 --- a/src/ast/type/alias_type_test.cc +++ b/src/ast/type/alias_type_test.cc @@ -33,6 +33,7 @@ #include "src/ast/type/struct_type.h" #include "src/ast/type/texture_type.h" #include "src/ast/type/u32_type.h" +#include "src/ast/type/vector_type.h" namespace tint { namespace ast { @@ -65,7 +66,7 @@ TEST_F(AliasTypeTest, Is) { EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); - EXPECT_FALSE(ty->IsVector()); + EXPECT_FALSE(ty->Is()); } TEST_F(AliasTypeTest, TypeName) { diff --git a/src/ast/type/array_type_test.cc b/src/ast/type/array_type_test.cc index b8119e80e4..7a3d3514de 100644 --- a/src/ast/type/array_type_test.cc +++ b/src/ast/type/array_type_test.cc @@ -28,6 +28,7 @@ #include "src/ast/type/struct_type.h" #include "src/ast/type/texture_type.h" #include "src/ast/type/u32_type.h" +#include "src/ast/type/vector_type.h" namespace tint { namespace ast { @@ -71,7 +72,7 @@ TEST_F(ArrayTypeTest, Is) { EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); - EXPECT_FALSE(ty->IsVector()); + EXPECT_FALSE(ty->Is()); } TEST_F(ArrayTypeTest, TypeName) { diff --git a/src/ast/type/bool_type_test.cc b/src/ast/type/bool_type_test.cc index 4d8fa7e54b..52e64a7b3c 100644 --- a/src/ast/type/bool_type_test.cc +++ b/src/ast/type/bool_type_test.cc @@ -24,6 +24,7 @@ #include "src/ast/type/struct_type.h" #include "src/ast/type/texture_type.h" #include "src/ast/type/u32_type.h" +#include "src/ast/type/vector_type.h" namespace tint { namespace ast { @@ -47,7 +48,7 @@ TEST_F(BoolTypeTest, Is) { EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); - EXPECT_FALSE(ty->IsVector()); + EXPECT_FALSE(ty->Is()); } TEST_F(BoolTypeTest, TypeName) { diff --git a/src/ast/type/depth_texture_type_test.cc b/src/ast/type/depth_texture_type_test.cc index 53200aead7..51c7014f9d 100644 --- a/src/ast/type/depth_texture_type_test.cc +++ b/src/ast/type/depth_texture_type_test.cc @@ -25,6 +25,7 @@ #include "src/ast/type/pointer_type.h" #include "src/ast/type/struct_type.h" #include "src/ast/type/u32_type.h" +#include "src/ast/type/vector_type.h" namespace tint { namespace ast { @@ -48,7 +49,7 @@ TEST_F(DepthTextureTypeTest, Is) { EXPECT_FALSE(ty->Is()); EXPECT_TRUE(ty->Is()); EXPECT_FALSE(ty->Is()); - EXPECT_FALSE(ty->IsVector()); + EXPECT_FALSE(ty->Is()); } TEST_F(DepthTextureTypeTest, IsTextureType) { diff --git a/src/ast/type/f32_type_test.cc b/src/ast/type/f32_type_test.cc index bd3446865f..8d356eeebc 100644 --- a/src/ast/type/f32_type_test.cc +++ b/src/ast/type/f32_type_test.cc @@ -24,6 +24,7 @@ #include "src/ast/type/struct_type.h" #include "src/ast/type/texture_type.h" #include "src/ast/type/u32_type.h" +#include "src/ast/type/vector_type.h" namespace tint { namespace ast { @@ -47,7 +48,7 @@ TEST_F(F32TypeTest, Is) { EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); - EXPECT_FALSE(ty->IsVector()); + EXPECT_FALSE(ty->Is()); } TEST_F(F32TypeTest, TypeName) { diff --git a/src/ast/type/i32_type_test.cc b/src/ast/type/i32_type_test.cc index 093ed6a66c..fc9c75a320 100644 --- a/src/ast/type/i32_type_test.cc +++ b/src/ast/type/i32_type_test.cc @@ -24,6 +24,7 @@ #include "src/ast/type/struct_type.h" #include "src/ast/type/texture_type.h" #include "src/ast/type/u32_type.h" +#include "src/ast/type/vector_type.h" namespace tint { namespace ast { @@ -47,7 +48,7 @@ TEST_F(I32TypeTest, Is) { EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); - EXPECT_FALSE(ty->IsVector()); + EXPECT_FALSE(ty->Is()); } TEST_F(I32TypeTest, TypeName) { diff --git a/src/ast/type/matrix_type_test.cc b/src/ast/type/matrix_type_test.cc index 6335674f9e..514d61d9c1 100644 --- a/src/ast/type/matrix_type_test.cc +++ b/src/ast/type/matrix_type_test.cc @@ -24,6 +24,7 @@ #include "src/ast/type/struct_type.h" #include "src/ast/type/texture_type.h" #include "src/ast/type/u32_type.h" +#include "src/ast/type/vector_type.h" namespace tint { namespace ast { @@ -56,7 +57,7 @@ TEST_F(MatrixTypeTest, Is) { EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); - EXPECT_FALSE(ty->IsVector()); + EXPECT_FALSE(ty->Is()); } TEST_F(MatrixTypeTest, TypeName) { diff --git a/src/ast/type/multisampled_texture_type_test.cc b/src/ast/type/multisampled_texture_type_test.cc index e08440d32d..32756ced87 100644 --- a/src/ast/type/multisampled_texture_type_test.cc +++ b/src/ast/type/multisampled_texture_type_test.cc @@ -24,6 +24,7 @@ #include "src/ast/type/pointer_type.h" #include "src/ast/type/struct_type.h" #include "src/ast/type/u32_type.h" +#include "src/ast/type/vector_type.h" namespace tint { namespace ast { @@ -48,7 +49,7 @@ TEST_F(MultisampledTextureTypeTest, Is) { EXPECT_FALSE(ty->Is()); EXPECT_TRUE(ty->Is()); EXPECT_FALSE(ty->Is()); - EXPECT_FALSE(ty->IsVector()); + EXPECT_FALSE(ty->Is()); } TEST_F(MultisampledTextureTypeTest, IsTextureType) { diff --git a/src/ast/type/pointer_type_test.cc b/src/ast/type/pointer_type_test.cc index 6af8570f60..f50a6a715d 100644 --- a/src/ast/type/pointer_type_test.cc +++ b/src/ast/type/pointer_type_test.cc @@ -24,6 +24,7 @@ #include "src/ast/type/struct_type.h" #include "src/ast/type/texture_type.h" #include "src/ast/type/u32_type.h" +#include "src/ast/type/vector_type.h" namespace tint { namespace ast { @@ -55,7 +56,7 @@ TEST_F(PointerTypeTest, Is) { EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); - EXPECT_FALSE(ty->IsVector()); + EXPECT_FALSE(ty->Is()); } TEST_F(PointerTypeTest, TypeName) { diff --git a/src/ast/type/sampled_texture_type_test.cc b/src/ast/type/sampled_texture_type_test.cc index 6c37a8122b..9e68b826f1 100644 --- a/src/ast/type/sampled_texture_type_test.cc +++ b/src/ast/type/sampled_texture_type_test.cc @@ -24,6 +24,7 @@ #include "src/ast/type/pointer_type.h" #include "src/ast/type/struct_type.h" #include "src/ast/type/u32_type.h" +#include "src/ast/type/vector_type.h" namespace tint { namespace ast { @@ -48,7 +49,7 @@ TEST_F(SampledTextureTypeTest, Is) { EXPECT_FALSE(ty->Is()); EXPECT_TRUE(ty->Is()); EXPECT_FALSE(ty->Is()); - EXPECT_FALSE(ty->IsVector()); + EXPECT_FALSE(ty->Is()); } TEST_F(SampledTextureTypeTest, IsTextureType) { diff --git a/src/ast/type/sampler_type_test.cc b/src/ast/type/sampler_type_test.cc index 9fd5ada417..2353c8a8cc 100644 --- a/src/ast/type/sampler_type_test.cc +++ b/src/ast/type/sampler_type_test.cc @@ -25,6 +25,7 @@ #include "src/ast/type/struct_type.h" #include "src/ast/type/texture_type.h" #include "src/ast/type/u32_type.h" +#include "src/ast/type/vector_type.h" namespace tint { namespace ast { @@ -59,7 +60,7 @@ TEST_F(SamplerTypeTest, Is) { EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); - EXPECT_FALSE(ty->IsVector()); + EXPECT_FALSE(ty->Is()); } TEST_F(SamplerTypeTest, TypeName_Sampler) { diff --git a/src/ast/type/storage_texture_type_test.cc b/src/ast/type/storage_texture_type_test.cc index 961b1cb6ca..4d71c4190a 100644 --- a/src/ast/type/storage_texture_type_test.cc +++ b/src/ast/type/storage_texture_type_test.cc @@ -27,6 +27,7 @@ #include "src/ast/type/pointer_type.h" #include "src/ast/type/struct_type.h" #include "src/ast/type/u32_type.h" +#include "src/ast/type/vector_type.h" #include "src/type_determiner.h" namespace tint { @@ -52,7 +53,7 @@ TEST_F(StorageTextureTypeTest, Is) { EXPECT_FALSE(ty->Is()); EXPECT_TRUE(ty->Is()); EXPECT_FALSE(ty->Is()); - EXPECT_FALSE(ty->IsVector()); + EXPECT_FALSE(ty->Is()); } TEST_F(StorageTextureTypeTest, IsTextureType) { diff --git a/src/ast/type/struct_type_test.cc b/src/ast/type/struct_type_test.cc index 82227b1c3b..d0ba1e806e 100644 --- a/src/ast/type/struct_type_test.cc +++ b/src/ast/type/struct_type_test.cc @@ -62,7 +62,7 @@ TEST_F(StructTypeTest, Is) { EXPECT_TRUE(ty->Is()); EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); - EXPECT_FALSE(ty->IsVector()); + EXPECT_FALSE(ty->Is()); } TEST_F(StructTypeTest, TypeName) { diff --git a/src/ast/type/type.cc b/src/ast/type/type.cc index 5204986f85..fdbc0e1757 100644 --- a/src/ast/type/type.cc +++ b/src/ast/type/type.cc @@ -66,10 +66,6 @@ Type* Type::UnwrapAll() { return UnwrapIfNeeded()->UnwrapPtrIfNeeded()->UnwrapIfNeeded(); } -bool Type::IsVector() const { - return false; -} - bool Type::IsVoid() const { return false; } @@ -95,7 +91,7 @@ bool Type::is_float_matrix() { } bool Type::is_float_vector() { - return IsVector() && AsVector()->type()->is_float_scalar(); + return Is() && As()->type()->is_float_scalar(); } bool Type::is_float_scalar_or_vector() { @@ -107,40 +103,32 @@ bool Type::is_integer_scalar() { } bool Type::is_unsigned_integer_vector() { - return IsVector() && AsVector()->type()->Is(); + return Is() && As()->type()->Is(); } bool Type::is_signed_integer_vector() { - return IsVector() && AsVector()->type()->Is(); + return Is() && As()->type()->Is(); } bool Type::is_unsigned_scalar_or_vector() { - return Is() || (IsVector() && AsVector()->type()->Is()); + return Is() || + (Is() && As()->type()->Is()); } bool Type::is_signed_scalar_or_vector() { - return Is() || (IsVector() && AsVector()->type()->Is()); + return Is() || + (Is() && As()->type()->Is()); } bool Type::is_integer_scalar_or_vector() { return is_unsigned_scalar_or_vector() || is_signed_scalar_or_vector(); } -const VectorType* Type::AsVector() const { - assert(IsVector()); - return static_cast(this); -} - const VoidType* Type::AsVoid() const { assert(IsVoid()); return static_cast(this); } -VectorType* Type::AsVector() { - assert(IsVector()); - return static_cast(this); -} - VoidType* Type::AsVoid() { assert(IsVoid()); return static_cast(this); diff --git a/src/ast/type/type.h b/src/ast/type/type.h index a78f916d40..9c056d7487 100644 --- a/src/ast/type/type.h +++ b/src/ast/type/type.h @@ -23,7 +23,6 @@ namespace tint { namespace ast { namespace type { -class VectorType; class VoidType; /// Supported memory layouts for calculating sizes @@ -36,8 +35,6 @@ class Type : public Castable { Type(Type&&); ~Type() override; - /// @returns true if the type is a vec type - virtual bool IsVector() const; /// @returns true if the type is a void type virtual bool IsVoid() const; @@ -95,13 +92,9 @@ class Type : public Castable { /// @returns true if this type is an integer scalar or vector bool is_integer_scalar_or_vector(); - /// @returns the type as a vector type - const VectorType* AsVector() const; /// @returns the type as a void type const VoidType* AsVoid() const; - /// @returns the type as a vector type - VectorType* AsVector(); /// @returns the type as a void type VoidType* AsVoid(); diff --git a/src/ast/type/u32_type_test.cc b/src/ast/type/u32_type_test.cc index 5afb341b54..19802b5a05 100644 --- a/src/ast/type/u32_type_test.cc +++ b/src/ast/type/u32_type_test.cc @@ -25,6 +25,7 @@ #include "src/ast/type/sampler_type.h" #include "src/ast/type/struct_type.h" #include "src/ast/type/texture_type.h" +#include "src/ast/type/vector_type.h" namespace tint { namespace ast { @@ -48,7 +49,7 @@ TEST_F(U32TypeTest, Is) { EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); EXPECT_TRUE(ty->Is()); - EXPECT_FALSE(ty->IsVector()); + EXPECT_FALSE(ty->Is()); } TEST_F(U32TypeTest, TypeName) { diff --git a/src/ast/type/vector_type.cc b/src/ast/type/vector_type.cc index 973471fcbc..14614c3f11 100644 --- a/src/ast/type/vector_type.cc +++ b/src/ast/type/vector_type.cc @@ -31,10 +31,6 @@ VectorType::VectorType(VectorType&&) = default; VectorType::~VectorType() = default; -bool VectorType::IsVector() const { - return true; -} - std::string VectorType::type_name() const { return "__vec_" + std::to_string(size_) + subtype_->type_name(); } diff --git a/src/ast/type/vector_type.h b/src/ast/type/vector_type.h index 257d27c499..c09d75e9f5 100644 --- a/src/ast/type/vector_type.h +++ b/src/ast/type/vector_type.h @@ -34,9 +34,6 @@ class VectorType : public Castable { VectorType(VectorType&&); ~VectorType() override; - /// @returns true if the type is a vector type - bool IsVector() const override; - /// @returns the type of the vector elements Type* type() const { return subtype_; } /// @returns the size of the vector diff --git a/src/ast/type/vector_type_test.cc b/src/ast/type/vector_type_test.cc index 8940ba9a89..14c88cdfec 100644 --- a/src/ast/type/vector_type_test.cc +++ b/src/ast/type/vector_type_test.cc @@ -56,7 +56,7 @@ TEST_F(VectorTypeTest, Is) { EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); - EXPECT_TRUE(ty->IsVector()); + EXPECT_TRUE(ty->Is()); } TEST_F(VectorTypeTest, TypeName) { diff --git a/src/inspector/inspector.cc b/src/inspector/inspector.cc index b4da984c17..678b1f6c36 100644 --- a/src/inspector/inspector.cc +++ b/src/inspector/inspector.cc @@ -391,8 +391,8 @@ std::vector Inspector::GetSampledTextureResourceBindingsImpl( base_type = base_type->As()->type(); } else if (base_type->Is()) { base_type = base_type->As()->type(); - } else if (base_type->IsVector()) { - base_type = base_type->AsVector()->type(); + } else if (base_type->Is()) { + base_type = base_type->As()->type(); } if (base_type->Is()) { diff --git a/src/reader/spirv/function.cc b/src/reader/spirv/function.cc index e008eb72ee..d20653489d 100644 --- a/src/reader/spirv/function.cc +++ b/src/reader/spirv/function.cc @@ -3206,7 +3206,7 @@ TypedExpression FunctionEmitter::MakeVectorShuffle( // Generate an ast::TypeConstructor expression. // Assume the literal indices are valid, and there is a valid number of them. ast::type::VectorType* result_type = - parser_impl_.ConvertType(inst.type_id())->AsVector(); + parser_impl_.ConvertType(inst.type_id())->As(); ast::ExpressionList values; for (uint32_t i = 2; i < inst.NumInOperands(); ++i) { const auto index = inst.GetSingleWordInOperand(i); @@ -3598,7 +3598,7 @@ TypedExpression FunctionEmitter::MakeSimpleSelect( // - you can't select over pointers or pointer vectors, unless you also have // a VariablePointers* capability, which is not allowed in by WebGPU. auto* op_ty = operand1.type; - if (op_ty->IsVector() || op_ty->is_float_scalar() || + if (op_ty->Is() || op_ty->is_float_scalar() || op_ty->is_integer_scalar() || op_ty->Is()) { ast::ExpressionList params; params.push_back(operand1.expr); @@ -3829,8 +3829,8 @@ ast::ExpressionList FunctionEmitter::MakeCoordinateOperandsForImageAccess( uint32_t num_coords_supplied = 0; if (raw_coords.type->Is()) { num_coords_supplied = 1; - } else if (raw_coords.type->IsVector()) { - num_coords_supplied = raw_coords.type->AsVector()->size(); + } else if (raw_coords.type->Is()) { + num_coords_supplied = raw_coords.type->As()->size(); } if (num_coords_supplied == 0) { Fail() << "bad or unsupported coordinate type for image access: " diff --git a/src/reader/spirv/parser_impl.cc b/src/reader/spirv/parser_impl.cc index 56c2f531ef..eb0c8fca3d 100644 --- a/src/reader/spirv/parser_impl.cc +++ b/src/reader/spirv/parser_impl.cc @@ -1351,8 +1351,8 @@ ast::Expression* ParserImpl::MakeNullValue(ast::type::Type* type) { return create( create(type, 0.0f)); } - if (type->IsVector()) { - const auto* vec_ty = type->AsVector(); + if (type->Is()) { + const auto* vec_ty = type->As(); ast::ExpressionList ast_components; for (size_t i = 0; i < vec_ty->size(); ++i) { ast_components.emplace_back(MakeNullValue(vec_ty->type())); @@ -1450,7 +1450,7 @@ ast::type::Type* ParserImpl::GetSignedIntMatchingShape(ast::type::Type* other) { other->Is()) { return i32; } - auto* vec_ty = other->AsVector(); + auto* vec_ty = other->As(); if (vec_ty) { return ast_module_.create(i32, vec_ty->size()); } @@ -1469,7 +1469,7 @@ ast::type::Type* ParserImpl::GetUnsignedIntMatchingShape( other->Is()) { return u32; } - auto* vec_ty = other->AsVector(); + auto* vec_ty = other->As(); if (vec_ty) { return ast_module_.create(u32, vec_ty->size()); } diff --git a/src/reader/spirv/parser_impl_convert_type_test.cc b/src/reader/spirv/parser_impl_convert_type_test.cc index 4a5100f6fc..966a43bc37 100644 --- a/src/reader/spirv/parser_impl_convert_type_test.cc +++ b/src/reader/spirv/parser_impl_convert_type_test.cc @@ -171,19 +171,22 @@ TEST_F(SpvParserTest, ConvertType_VecOverF32) { EXPECT_TRUE(p->BuildInternalModule()); auto* v2xf32 = p->ConvertType(20); - EXPECT_TRUE(v2xf32->IsVector()); - EXPECT_TRUE(v2xf32->AsVector()->type()->Is()); - EXPECT_EQ(v2xf32->AsVector()->size(), 2u); + EXPECT_TRUE(v2xf32->Is()); + EXPECT_TRUE( + v2xf32->As()->type()->Is()); + EXPECT_EQ(v2xf32->As()->size(), 2u); auto* v3xf32 = p->ConvertType(30); - EXPECT_TRUE(v3xf32->IsVector()); - EXPECT_TRUE(v3xf32->AsVector()->type()->Is()); - EXPECT_EQ(v3xf32->AsVector()->size(), 3u); + EXPECT_TRUE(v3xf32->Is()); + EXPECT_TRUE( + v3xf32->As()->type()->Is()); + EXPECT_EQ(v3xf32->As()->size(), 3u); auto* v4xf32 = p->ConvertType(40); - EXPECT_TRUE(v4xf32->IsVector()); - EXPECT_TRUE(v4xf32->AsVector()->type()->Is()); - EXPECT_EQ(v4xf32->AsVector()->size(), 4u); + EXPECT_TRUE(v4xf32->Is()); + EXPECT_TRUE( + v4xf32->As()->type()->Is()); + EXPECT_EQ(v4xf32->As()->size(), 4u); EXPECT_TRUE(p->error().empty()); } @@ -198,19 +201,22 @@ TEST_F(SpvParserTest, ConvertType_VecOverI32) { EXPECT_TRUE(p->BuildInternalModule()); auto* v2xi32 = p->ConvertType(20); - EXPECT_TRUE(v2xi32->IsVector()); - EXPECT_TRUE(v2xi32->AsVector()->type()->Is()); - EXPECT_EQ(v2xi32->AsVector()->size(), 2u); + EXPECT_TRUE(v2xi32->Is()); + EXPECT_TRUE( + v2xi32->As()->type()->Is()); + EXPECT_EQ(v2xi32->As()->size(), 2u); auto* v3xi32 = p->ConvertType(30); - EXPECT_TRUE(v3xi32->IsVector()); - EXPECT_TRUE(v3xi32->AsVector()->type()->Is()); - EXPECT_EQ(v3xi32->AsVector()->size(), 3u); + EXPECT_TRUE(v3xi32->Is()); + EXPECT_TRUE( + v3xi32->As()->type()->Is()); + EXPECT_EQ(v3xi32->As()->size(), 3u); auto* v4xi32 = p->ConvertType(40); - EXPECT_TRUE(v4xi32->IsVector()); - EXPECT_TRUE(v4xi32->AsVector()->type()->Is()); - EXPECT_EQ(v4xi32->AsVector()->size(), 4u); + EXPECT_TRUE(v4xi32->Is()); + EXPECT_TRUE( + v4xi32->As()->type()->Is()); + EXPECT_EQ(v4xi32->As()->size(), 4u); EXPECT_TRUE(p->error().empty()); } @@ -225,19 +231,22 @@ TEST_F(SpvParserTest, ConvertType_VecOverU32) { EXPECT_TRUE(p->BuildInternalModule()); auto* v2xu32 = p->ConvertType(20); - EXPECT_TRUE(v2xu32->IsVector()); - EXPECT_TRUE(v2xu32->AsVector()->type()->Is()); - EXPECT_EQ(v2xu32->AsVector()->size(), 2u); + EXPECT_TRUE(v2xu32->Is()); + EXPECT_TRUE( + v2xu32->As()->type()->Is()); + EXPECT_EQ(v2xu32->As()->size(), 2u); auto* v3xu32 = p->ConvertType(30); - EXPECT_TRUE(v3xu32->IsVector()); - EXPECT_TRUE(v3xu32->AsVector()->type()->Is()); - EXPECT_EQ(v3xu32->AsVector()->size(), 3u); + EXPECT_TRUE(v3xu32->Is()); + EXPECT_TRUE( + v3xu32->As()->type()->Is()); + EXPECT_EQ(v3xu32->As()->size(), 3u); auto* v4xu32 = p->ConvertType(40); - EXPECT_TRUE(v4xu32->IsVector()); - EXPECT_TRUE(v4xu32->AsVector()->type()->Is()); - EXPECT_EQ(v4xu32->AsVector()->size(), 4u); + EXPECT_TRUE(v4xu32->Is()); + EXPECT_TRUE( + v4xu32->As()->type()->Is()); + EXPECT_EQ(v4xu32->As()->size(), 4u); EXPECT_TRUE(p->error().empty()); } diff --git a/src/reader/wgsl/parser_impl_const_expr_test.cc b/src/reader/wgsl/parser_impl_const_expr_test.cc index 0635a728ff..41355154d6 100644 --- a/src/reader/wgsl/parser_impl_const_expr_test.cc +++ b/src/reader/wgsl/parser_impl_const_expr_test.cc @@ -35,8 +35,8 @@ TEST_F(ParserImplTest, ConstExpr_TypeDecl) { ASSERT_TRUE(e->AsConstructor()->IsTypeConstructor()); auto* t = e->AsConstructor()->AsTypeConstructor(); - ASSERT_TRUE(t->type()->IsVector()); - EXPECT_EQ(t->type()->AsVector()->size(), 2u); + ASSERT_TRUE(t->type()->Is()); + EXPECT_EQ(t->type()->As()->size(), 2u); ASSERT_EQ(t->values().size(), 2u); auto& v = t->values(); diff --git a/src/reader/wgsl/parser_impl_type_decl_test.cc b/src/reader/wgsl/parser_impl_type_decl_test.cc index cc27ef3368..24ac3cdcf0 100644 --- a/src/reader/wgsl/parser_impl_type_decl_test.cc +++ b/src/reader/wgsl/parser_impl_type_decl_test.cc @@ -151,8 +151,8 @@ TEST_P(VecTest, Parse) { EXPECT_FALSE(t.errored); ASSERT_NE(t.value, nullptr) << p->error(); ASSERT_FALSE(p->has_error()); - EXPECT_TRUE(t->IsVector()); - EXPECT_EQ(t->AsVector()->size(), params.count); + EXPECT_TRUE(t->Is()); + EXPECT_EQ(t->As()->size(), params.count); } INSTANTIATE_TEST_SUITE_P(ParserImplTest, VecTest, @@ -256,10 +256,10 @@ TEST_F(ParserImplTest, TypeDecl_Ptr_ToVec) { ASSERT_TRUE(t->Is()); auto* ptr = t->As(); - ASSERT_TRUE(ptr->type()->IsVector()); + ASSERT_TRUE(ptr->type()->Is()); ASSERT_EQ(ptr->storage_class(), ast::StorageClass::kFunction); - auto* vec = ptr->type()->AsVector(); + auto* vec = ptr->type()->As(); ASSERT_EQ(vec->size(), 2u); ASSERT_TRUE(vec->type()->Is()); } diff --git a/src/transform/bound_array_accessors_transform.cc b/src/transform/bound_array_accessors_transform.cc index dd9ec6f7d4..61e4a4a6d4 100644 --- a/src/transform/bound_array_accessors_transform.cc +++ b/src/transform/bound_array_accessors_transform.cc @@ -185,13 +185,15 @@ bool BoundArrayAccessorsTransform::ProcessArrayAccessor( auto* ret_type = expr->array()->result_type()->UnwrapAll(); if (!ret_type->Is() && - !ret_type->Is() && !ret_type->IsVector()) { + !ret_type->Is() && + !ret_type->Is()) { return true; } - if (ret_type->IsVector() || ret_type->Is()) { - uint32_t size = ret_type->IsVector() - ? ret_type->AsVector()->size() + if (ret_type->Is() || + ret_type->Is()) { + uint32_t size = ret_type->Is() + ? ret_type->As()->size() : ret_type->As()->size(); if (size == 0) { error_ = "invalid 0 size for array or vector"; diff --git a/src/type_determiner.cc b/src/type_determiner.cc index 8f5ab1d873..964a2fbe26 100644 --- a/src/type_determiner.cc +++ b/src/type_determiner.cc @@ -337,8 +337,8 @@ bool TypeDeterminer::DetermineArrayAccessor( ast::type::Type* ret = nullptr; if (parent_type->Is()) { ret = parent_type->As()->type(); - } else if (parent_type->IsVector()) { - ret = parent_type->AsVector()->type(); + } else if (parent_type->Is()) { + ret = parent_type->As()->type(); } else if (parent_type->Is()) { auto* m = parent_type->As(); ret = mod_->create(m->type(), m->rows()); @@ -540,9 +540,9 @@ bool TypeDeterminer::DetermineIntrinsic(ast::IdentifierExpression* ident, auto* bool_type = mod_->create(); auto* param_type = expr->params()[0]->result_type()->UnwrapPtrIfNeeded(); - if (param_type->IsVector()) { + if (param_type->Is()) { expr->func()->set_result_type(mod_->create( - bool_type, param_type->AsVector()->size())); + bool_type, param_type->As()->size())); } else { expr->func()->set_result_type(bool_type); } @@ -703,14 +703,16 @@ bool TypeDeterminer::DetermineIntrinsic(ast::IdentifierExpression* ident, auto* param0_type = expr->params()[0]->result_type()->UnwrapPtrIfNeeded(); auto* param1_type = expr->params()[1]->result_type()->UnwrapPtrIfNeeded(); - if (!param0_type->IsVector() || !param1_type->IsVector()) { + if (!param0_type->Is() || + !param1_type->Is()) { set_error(expr->source(), "invalid parameter type for " + ident->name()); return false; } expr->func()->set_result_type(mod_->create( - mod_->create(), param0_type->AsVector()->size(), - param1_type->AsVector()->size())); + mod_->create(), + param0_type->As()->size(), + param1_type->As()->size())); return true; } if (ident->intrinsic() == ast::Intrinsic::kSelect) { @@ -786,7 +788,8 @@ bool TypeDeterminer::DetermineIntrinsic(ast::IdentifierExpression* ident, return false; } if (data->vector_size > 0 && - result_types.back()->AsVector()->size() != data->vector_size) { + result_types.back()->As()->size() != + data->vector_size) { set_error(expr->source(), "incorrect vector size for " + ident->name() + ". " + "Requires " + std::to_string(data->vector_size) + @@ -817,9 +820,10 @@ bool TypeDeterminer::DetermineIntrinsic(ast::IdentifierExpression* ident, // provided. if (ident->intrinsic() == ast::Intrinsic::kLength || ident->intrinsic() == ast::Intrinsic::kDistance) { - expr->func()->set_result_type(result_types[0]->is_float_scalar() - ? result_types[0] - : result_types[0]->AsVector()->type()); + expr->func()->set_result_type( + result_types[0]->is_float_scalar() + ? result_types[0] + : result_types[0]->As()->type()); return true; } // The determinant returns the component type of the columns @@ -1054,8 +1058,8 @@ bool TypeDeterminer::DetermineMemberAccessor( ret = mod_->create( ret, res->As()->storage_class()); } - } else if (data_type->IsVector()) { - auto* vec = data_type->AsVector(); + } else if (data_type->Is()) { + auto* vec = data_type->As(); auto size = expr->member()->name().size(); if (size == 1) { @@ -1103,9 +1107,9 @@ bool TypeDeterminer::DetermineBinary(ast::BinaryExpression* expr) { expr->IsLessThanEqual() || expr->IsGreaterThanEqual()) { auto* bool_type = mod_->create(); auto* param_type = expr->lhs()->result_type()->UnwrapPtrIfNeeded(); - if (param_type->IsVector()) { + if (param_type->Is()) { expr->set_result_type(mod_->create( - bool_type, param_type->AsVector()->size())); + bool_type, param_type->As()->size())); } else { expr->set_result_type(bool_type); } @@ -1124,11 +1128,13 @@ bool TypeDeterminer::DetermineBinary(ast::BinaryExpression* expr) { lhs_type->As()->rows(), rhs_type->As()->columns())); - } else if (lhs_type->Is() && rhs_type->IsVector()) { + } else if (lhs_type->Is() && + rhs_type->Is()) { auto* mat = lhs_type->As(); expr->set_result_type( mod_->create(mat->type(), mat->rows())); - } else if (lhs_type->IsVector() && rhs_type->Is()) { + } else if (lhs_type->Is() && + rhs_type->Is()) { auto* mat = rhs_type->As(); expr->set_result_type( mod_->create(mat->type(), mat->columns())); @@ -1138,12 +1144,13 @@ bool TypeDeterminer::DetermineBinary(ast::BinaryExpression* expr) { } else if (rhs_type->Is()) { // scalar * matrix expr->set_result_type(rhs_type); - } else if (lhs_type->IsVector() && rhs_type->IsVector()) { + } else if (lhs_type->Is() && + rhs_type->Is()) { expr->set_result_type(lhs_type); - } else if (lhs_type->IsVector()) { + } else if (lhs_type->Is()) { // Vector * scalar expr->set_result_type(lhs_type); - } else if (rhs_type->IsVector()) { + } else if (rhs_type->Is()) { // Scalar * vector expr->set_result_type(rhs_type); } else { diff --git a/src/type_determiner_test.cc b/src/type_determiner_test.cc index dd3784d330..5357c823cf 100644 --- a/src/type_determiner_test.cc +++ b/src/type_determiner_test.cc @@ -512,8 +512,8 @@ TEST_F(TypeDeterminerTest, Expr_ArrayAccessor_Matrix) { ASSERT_TRUE(acc.result_type()->Is()); auto* ptr = acc.result_type()->As(); - ASSERT_TRUE(ptr->type()->IsVector()); - EXPECT_EQ(ptr->type()->AsVector()->size(), 3u); + ASSERT_TRUE(ptr->type()->Is()); + EXPECT_EQ(ptr->type()->As()->size(), 3u); } TEST_F(TypeDeterminerTest, Expr_ArrayAccessor_Matrix_BothDimensions) { @@ -681,9 +681,12 @@ TEST_F(TypeDeterminerTest, Expr_Constructor_Type) { EXPECT_TRUE(td()->DetermineResultType(&tc)); ASSERT_NE(tc.result_type(), nullptr); - ASSERT_TRUE(tc.result_type()->IsVector()); - EXPECT_TRUE(tc.result_type()->AsVector()->type()->Is()); - EXPECT_EQ(tc.result_type()->AsVector()->size(), 3u); + ASSERT_TRUE(tc.result_type()->Is()); + EXPECT_TRUE(tc.result_type() + ->As() + ->type() + ->Is()); + EXPECT_EQ(tc.result_type()->As()->size(), 3u); } TEST_F(TypeDeterminerTest, Expr_Identifier_GlobalVariable) { @@ -1029,9 +1032,12 @@ TEST_F(TypeDeterminerTest, Expr_MemberAccessor_VectorSwizzle) { ast::MemberAccessorExpression mem(ident, swizzle); EXPECT_TRUE(td()->DetermineResultType(&mem)) << td()->error(); ASSERT_NE(mem.result_type(), nullptr); - ASSERT_TRUE(mem.result_type()->IsVector()); - EXPECT_TRUE(mem.result_type()->AsVector()->type()->Is()); - EXPECT_EQ(mem.result_type()->AsVector()->size(), 2u); + ASSERT_TRUE(mem.result_type()->Is()); + EXPECT_TRUE(mem.result_type() + ->As() + ->type() + ->Is()); + EXPECT_EQ(mem.result_type()->As()->size(), 2u); } TEST_F(TypeDeterminerTest, Expr_MemberAccessor_VectorSwizzle_SingleElement) { @@ -1124,9 +1130,12 @@ TEST_F(TypeDeterminerTest, Expr_Accessor_MultiLevel) { EXPECT_TRUE(td()->DetermineResultType(&mem)) << td()->error(); ASSERT_NE(mem.result_type(), nullptr); - ASSERT_TRUE(mem.result_type()->IsVector()); - EXPECT_TRUE(mem.result_type()->AsVector()->type()->Is()); - EXPECT_EQ(mem.result_type()->AsVector()->size(), 2u); + ASSERT_TRUE(mem.result_type()->Is()); + EXPECT_TRUE(mem.result_type() + ->As() + ->type() + ->Is()); + EXPECT_EQ(mem.result_type()->As()->size(), 2u); } using Expr_Binary_BitwiseTest = TypeDeterminerTestWithParam; @@ -1166,9 +1175,12 @@ TEST_P(Expr_Binary_BitwiseTest, Vector) { ASSERT_TRUE(td()->DetermineResultType(&expr)) << td()->error(); ASSERT_NE(expr.result_type(), nullptr); - ASSERT_TRUE(expr.result_type()->IsVector()); - EXPECT_TRUE(expr.result_type()->AsVector()->type()->Is()); - EXPECT_EQ(expr.result_type()->AsVector()->size(), 3u); + ASSERT_TRUE(expr.result_type()->Is()); + EXPECT_TRUE(expr.result_type() + ->As() + ->type() + ->Is()); + EXPECT_EQ(expr.result_type()->As()->size(), 3u); } INSTANTIATE_TEST_SUITE_P(TypeDeterminerTest, Expr_Binary_BitwiseTest, @@ -1220,10 +1232,12 @@ TEST_P(Expr_Binary_LogicalTest, Vector) { ASSERT_TRUE(td()->DetermineResultType(&expr)) << td()->error(); ASSERT_NE(expr.result_type(), nullptr); - ASSERT_TRUE(expr.result_type()->IsVector()); - EXPECT_TRUE( - expr.result_type()->AsVector()->type()->Is()); - EXPECT_EQ(expr.result_type()->AsVector()->size(), 3u); + ASSERT_TRUE(expr.result_type()->Is()); + EXPECT_TRUE(expr.result_type() + ->As() + ->type() + ->Is()); + EXPECT_EQ(expr.result_type()->As()->size(), 3u); } INSTANTIATE_TEST_SUITE_P(TypeDeterminerTest, Expr_Binary_LogicalTest, @@ -1267,10 +1281,12 @@ TEST_P(Expr_Binary_CompareTest, Vector) { ASSERT_TRUE(td()->DetermineResultType(&expr)) << td()->error(); ASSERT_NE(expr.result_type(), nullptr); - ASSERT_TRUE(expr.result_type()->IsVector()); - EXPECT_TRUE( - expr.result_type()->AsVector()->type()->Is()); - EXPECT_EQ(expr.result_type()->AsVector()->size(), 3u); + ASSERT_TRUE(expr.result_type()->Is()); + EXPECT_TRUE(expr.result_type() + ->As() + ->type() + ->Is()); + EXPECT_EQ(expr.result_type()->As()->size(), 3u); } INSTANTIATE_TEST_SUITE_P(TypeDeterminerTest, Expr_Binary_CompareTest, @@ -1319,9 +1335,12 @@ TEST_F(TypeDeterminerTest, Expr_Binary_Multiply_Vector_Scalar) { ASSERT_TRUE(td()->DetermineResultType(&expr)) << td()->error(); ASSERT_NE(expr.result_type(), nullptr); - ASSERT_TRUE(expr.result_type()->IsVector()); - EXPECT_TRUE(expr.result_type()->AsVector()->type()->Is()); - EXPECT_EQ(expr.result_type()->AsVector()->size(), 3u); + ASSERT_TRUE(expr.result_type()->Is()); + EXPECT_TRUE(expr.result_type() + ->As() + ->type() + ->Is()); + EXPECT_EQ(expr.result_type()->As()->size(), 3u); } TEST_F(TypeDeterminerTest, Expr_Binary_Multiply_Scalar_Vector) { @@ -1344,9 +1363,12 @@ TEST_F(TypeDeterminerTest, Expr_Binary_Multiply_Scalar_Vector) { ASSERT_TRUE(td()->DetermineResultType(&expr)) << td()->error(); ASSERT_NE(expr.result_type(), nullptr); - ASSERT_TRUE(expr.result_type()->IsVector()); - EXPECT_TRUE(expr.result_type()->AsVector()->type()->Is()); - EXPECT_EQ(expr.result_type()->AsVector()->size(), 3u); + ASSERT_TRUE(expr.result_type()->Is()); + EXPECT_TRUE(expr.result_type() + ->As() + ->type() + ->Is()); + EXPECT_EQ(expr.result_type()->As()->size(), 3u); } TEST_F(TypeDeterminerTest, Expr_Binary_Multiply_Vector_Vector) { @@ -1366,9 +1388,12 @@ TEST_F(TypeDeterminerTest, Expr_Binary_Multiply_Vector_Vector) { ASSERT_TRUE(td()->DetermineResultType(&expr)) << td()->error(); ASSERT_NE(expr.result_type(), nullptr); - ASSERT_TRUE(expr.result_type()->IsVector()); - EXPECT_TRUE(expr.result_type()->AsVector()->type()->Is()); - EXPECT_EQ(expr.result_type()->AsVector()->size(), 3u); + ASSERT_TRUE(expr.result_type()->Is()); + EXPECT_TRUE(expr.result_type() + ->As() + ->type() + ->Is()); + EXPECT_EQ(expr.result_type()->As()->size(), 3u); } TEST_F(TypeDeterminerTest, Expr_Binary_Multiply_Matrix_Scalar) { @@ -1448,9 +1473,12 @@ TEST_F(TypeDeterminerTest, Expr_Binary_Multiply_Matrix_Vector) { ASSERT_TRUE(td()->DetermineResultType(&expr)) << td()->error(); ASSERT_NE(expr.result_type(), nullptr); - ASSERT_TRUE(expr.result_type()->IsVector()); - EXPECT_TRUE(expr.result_type()->AsVector()->type()->Is()); - EXPECT_EQ(expr.result_type()->AsVector()->size(), 3u); + ASSERT_TRUE(expr.result_type()->Is()); + EXPECT_TRUE(expr.result_type() + ->As() + ->type() + ->Is()); + EXPECT_EQ(expr.result_type()->As()->size(), 3u); } TEST_F(TypeDeterminerTest, Expr_Binary_Multiply_Vector_Matrix) { @@ -1474,9 +1502,12 @@ TEST_F(TypeDeterminerTest, Expr_Binary_Multiply_Vector_Matrix) { ASSERT_TRUE(td()->DetermineResultType(&expr)) << td()->error(); ASSERT_NE(expr.result_type(), nullptr); - ASSERT_TRUE(expr.result_type()->IsVector()); - EXPECT_TRUE(expr.result_type()->AsVector()->type()->Is()); - EXPECT_EQ(expr.result_type()->AsVector()->size(), 2u); + ASSERT_TRUE(expr.result_type()->Is()); + EXPECT_TRUE(expr.result_type() + ->As() + ->type() + ->Is()); + EXPECT_EQ(expr.result_type()->As()->size(), 2u); } TEST_F(TypeDeterminerTest, Expr_Binary_Multiply_Matrix_Matrix) { @@ -1551,9 +1582,12 @@ TEST_P(IntrinsicDerivativeTest, Vector) { EXPECT_TRUE(td()->DetermineResultType(&expr)); ASSERT_NE(expr.result_type(), nullptr); - ASSERT_TRUE(expr.result_type()->IsVector()); - EXPECT_TRUE(expr.result_type()->AsVector()->type()->Is()); - EXPECT_EQ(expr.result_type()->AsVector()->size(), 4u); + ASSERT_TRUE(expr.result_type()->Is()); + EXPECT_TRUE(expr.result_type() + ->As() + ->type() + ->Is()); + EXPECT_EQ(expr.result_type()->As()->size(), 4u); } TEST_P(IntrinsicDerivativeTest, MissingParam) { @@ -1655,10 +1689,12 @@ TEST_P(Intrinsic_FloatMethod, Vector) { EXPECT_TRUE(td()->DetermineResultType(&expr)); ASSERT_NE(expr.result_type(), nullptr); - ASSERT_TRUE(expr.result_type()->IsVector()); - EXPECT_TRUE( - expr.result_type()->AsVector()->type()->Is()); - EXPECT_EQ(expr.result_type()->AsVector()->size(), 3u); + ASSERT_TRUE(expr.result_type()->Is()); + EXPECT_TRUE(expr.result_type() + ->As() + ->type() + ->Is()); + EXPECT_EQ(expr.result_type()->As()->size(), 3u); } TEST_P(Intrinsic_FloatMethod, Scalar) { @@ -1815,18 +1851,24 @@ TEST_P(Intrinsic_StorageTextureOperation, TextureLoadRo) { EXPECT_TRUE(td()->DetermineResultType(&expr)); ASSERT_NE(expr.result_type(), nullptr); - ASSERT_TRUE(expr.result_type()->IsVector()); + ASSERT_TRUE(expr.result_type()->Is()); if (type == TextureType::kF32) { - EXPECT_TRUE( - expr.result_type()->AsVector()->type()->Is()); + EXPECT_TRUE(expr.result_type() + ->As() + ->type() + ->Is()); } else if (type == TextureType::kI32) { - EXPECT_TRUE( - expr.result_type()->AsVector()->type()->Is()); + EXPECT_TRUE(expr.result_type() + ->As() + ->type() + ->Is()); } else { - EXPECT_TRUE( - expr.result_type()->AsVector()->type()->Is()); + EXPECT_TRUE(expr.result_type() + ->As() + ->type() + ->Is()); } - EXPECT_EQ(expr.result_type()->AsVector()->size(), 4u); + EXPECT_EQ(expr.result_type()->As()->size(), 4u); } INSTANTIATE_TEST_SUITE_P( @@ -1888,18 +1930,24 @@ TEST_P(Intrinsic_SampledTextureOperation, TextureLoadSampled) { EXPECT_TRUE(td()->DetermineResultType(&expr)); ASSERT_NE(expr.result_type(), nullptr); - ASSERT_TRUE(expr.result_type()->IsVector()); + ASSERT_TRUE(expr.result_type()->Is()); if (type == TextureType::kF32) { - EXPECT_TRUE( - expr.result_type()->AsVector()->type()->Is()); + EXPECT_TRUE(expr.result_type() + ->As() + ->type() + ->Is()); } else if (type == TextureType::kI32) { - EXPECT_TRUE( - expr.result_type()->AsVector()->type()->Is()); + EXPECT_TRUE(expr.result_type() + ->As() + ->type() + ->Is()); } else { - EXPECT_TRUE( - expr.result_type()->AsVector()->type()->Is()); + EXPECT_TRUE(expr.result_type() + ->As() + ->type() + ->Is()); } - EXPECT_EQ(expr.result_type()->AsVector()->size(), 4u); + EXPECT_EQ(expr.result_type()->As()->size(), 4u); } INSTANTIATE_TEST_SUITE_P( @@ -1956,9 +2004,12 @@ TEST_F(TypeDeterminerTest, Intrinsic_Select) { EXPECT_TRUE(td()->Determine()); EXPECT_TRUE(td()->DetermineResultType(&expr)) << td()->error(); ASSERT_NE(expr.result_type(), nullptr); - EXPECT_TRUE(expr.result_type()->IsVector()); - EXPECT_EQ(expr.result_type()->AsVector()->size(), 3u); - EXPECT_TRUE(expr.result_type()->AsVector()->type()->Is()); + EXPECT_TRUE(expr.result_type()->Is()); + EXPECT_EQ(expr.result_type()->As()->size(), 3u); + EXPECT_TRUE(expr.result_type() + ->As() + ->type() + ->Is()); } TEST_F(TypeDeterminerTest, Intrinsic_Select_TooFewParams) { @@ -2093,9 +2144,12 @@ TEST_P(UnaryOpExpressionTest, Expr_UnaryOp) { ast::UnaryOpExpression der(op, create("ident")); EXPECT_TRUE(td()->DetermineResultType(&der)); ASSERT_NE(der.result_type(), nullptr); - ASSERT_TRUE(der.result_type()->IsVector()); - EXPECT_TRUE(der.result_type()->AsVector()->type()->Is()); - EXPECT_EQ(der.result_type()->AsVector()->size(), 4u); + ASSERT_TRUE(der.result_type()->Is()); + EXPECT_TRUE(der.result_type() + ->As() + ->type() + ->Is()); + EXPECT_EQ(der.result_type()->As()->size(), 4u); } INSTANTIATE_TEST_SUITE_P(TypeDeterminerTest, UnaryOpExpressionTest, @@ -2294,7 +2348,7 @@ TEST_P(ImportData_SingleParamTest, Vector) { EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); ASSERT_NE(ident->result_type(), nullptr); EXPECT_TRUE(ident->result_type()->is_float_vector()); - EXPECT_EQ(ident->result_type()->AsVector()->size(), 3u); + EXPECT_EQ(ident->result_type()->As()->size(), 3u); } TEST_P(ImportData_SingleParamTest, Error_Integer) { @@ -2418,7 +2472,7 @@ TEST_P(ImportData_SingleParam_FloatOrInt_Test, Float_Vector) { EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); ASSERT_NE(ident->result_type(), nullptr); EXPECT_TRUE(ident->result_type()->is_float_vector()); - EXPECT_EQ(ident->result_type()->AsVector()->size(), 3u); + EXPECT_EQ(ident->result_type()->As()->size(), 3u); } TEST_P(ImportData_SingleParam_FloatOrInt_Test, Sint_Scalar) { @@ -2463,7 +2517,7 @@ TEST_P(ImportData_SingleParam_FloatOrInt_Test, Sint_Vector) { EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); ASSERT_NE(ident->result_type(), nullptr); EXPECT_TRUE(ident->result_type()->is_signed_integer_vector()); - EXPECT_EQ(ident->result_type()->AsVector()->size(), 3u); + EXPECT_EQ(ident->result_type()->As()->size(), 3u); } TEST_P(ImportData_SingleParam_FloatOrInt_Test, Uint_Scalar) { @@ -2508,7 +2562,7 @@ TEST_P(ImportData_SingleParam_FloatOrInt_Test, Uint_Vector) { EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); ASSERT_NE(ident->result_type(), nullptr); EXPECT_TRUE(ident->result_type()->is_unsigned_integer_vector()); - EXPECT_EQ(ident->result_type()->AsVector()->size(), 3u); + EXPECT_EQ(ident->result_type()->As()->size(), 3u); } TEST_P(ImportData_SingleParam_FloatOrInt_Test, Error_Bool) { @@ -2708,7 +2762,7 @@ TEST_P(ImportData_TwoParamTest, Vector) { EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); ASSERT_NE(ident->result_type(), nullptr); EXPECT_TRUE(ident->result_type()->is_float_vector()); - EXPECT_EQ(ident->result_type()->AsVector()->size(), 3u); + EXPECT_EQ(ident->result_type()->As()->size(), 3u); } TEST_P(ImportData_TwoParamTest, Error_Integer) { @@ -3045,7 +3099,7 @@ TEST_F(TypeDeterminerTest, ImportData_Cross) { EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); ASSERT_NE(ident->result_type(), nullptr); EXPECT_TRUE(ident->result_type()->is_float_vector()); - EXPECT_EQ(ident->result_type()->AsVector()->size(), 3u); + EXPECT_EQ(ident->result_type()->As()->size(), 3u); } TEST_F(TypeDeterminerTest, ImportData_Cross_Error_Scalar) { @@ -3236,7 +3290,7 @@ TEST_P(ImportData_ThreeParamTest, Vector) { EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); ASSERT_NE(ident->result_type(), nullptr); EXPECT_TRUE(ident->result_type()->is_float_vector()); - EXPECT_EQ(ident->result_type()->AsVector()->size(), 3u); + EXPECT_EQ(ident->result_type()->As()->size(), 3u); } TEST_P(ImportData_ThreeParamTest, Error_Integer) { @@ -3476,7 +3530,7 @@ TEST_P(ImportData_ThreeParam_FloatOrInt_Test, Float_Vector) { EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); ASSERT_NE(ident->result_type(), nullptr); EXPECT_TRUE(ident->result_type()->is_float_vector()); - EXPECT_EQ(ident->result_type()->AsVector()->size(), 3u); + EXPECT_EQ(ident->result_type()->As()->size(), 3u); } TEST_P(ImportData_ThreeParam_FloatOrInt_Test, Sint_Scalar) { @@ -3543,7 +3597,7 @@ TEST_P(ImportData_ThreeParam_FloatOrInt_Test, Sint_Vector) { EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); ASSERT_NE(ident->result_type(), nullptr); EXPECT_TRUE(ident->result_type()->is_signed_integer_vector()); - EXPECT_EQ(ident->result_type()->AsVector()->size(), 3u); + EXPECT_EQ(ident->result_type()->As()->size(), 3u); } TEST_P(ImportData_ThreeParam_FloatOrInt_Test, Uint_Scalar) { @@ -3610,7 +3664,7 @@ TEST_P(ImportData_ThreeParam_FloatOrInt_Test, Uint_Vector) { EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); ASSERT_NE(ident->result_type(), nullptr); EXPECT_TRUE(ident->result_type()->is_unsigned_integer_vector()); - EXPECT_EQ(ident->result_type()->AsVector()->size(), 3u); + EXPECT_EQ(ident->result_type()->As()->size(), 3u); } TEST_P(ImportData_ThreeParam_FloatOrInt_Test, Error_Bool) { @@ -3824,7 +3878,7 @@ TEST_P(ImportData_Int_SingleParamTest, Vector) { EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); ASSERT_NE(ident->result_type(), nullptr); EXPECT_TRUE(ident->result_type()->is_signed_integer_vector()); - EXPECT_EQ(ident->result_type()->AsVector()->size(), 3u); + EXPECT_EQ(ident->result_type()->As()->size(), 3u); } TEST_P(ImportData_Int_SingleParamTest, Error_Float) { @@ -3980,7 +4034,7 @@ TEST_P(ImportData_FloatOrInt_TwoParamTest, Vector_Signed) { EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); ASSERT_NE(ident->result_type(), nullptr); EXPECT_TRUE(ident->result_type()->is_signed_integer_vector()); - EXPECT_EQ(ident->result_type()->AsVector()->size(), 3u); + EXPECT_EQ(ident->result_type()->As()->size(), 3u); } TEST_P(ImportData_FloatOrInt_TwoParamTest, Vector_Unsigned) { @@ -4016,7 +4070,7 @@ TEST_P(ImportData_FloatOrInt_TwoParamTest, Vector_Unsigned) { EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); ASSERT_NE(ident->result_type(), nullptr); EXPECT_TRUE(ident->result_type()->is_unsigned_integer_vector()); - EXPECT_EQ(ident->result_type()->AsVector()->size(), 3u); + EXPECT_EQ(ident->result_type()->As()->size(), 3u); } TEST_P(ImportData_FloatOrInt_TwoParamTest, Vector_Float) { @@ -4052,7 +4106,7 @@ TEST_P(ImportData_FloatOrInt_TwoParamTest, Vector_Float) { EXPECT_TRUE(td()->DetermineResultType(&call)) << td()->error(); ASSERT_NE(ident->result_type(), nullptr); EXPECT_TRUE(ident->result_type()->is_float_vector()); - EXPECT_EQ(ident->result_type()->AsVector()->size(), 3u); + EXPECT_EQ(ident->result_type()->As()->size(), 3u); } TEST_P(ImportData_FloatOrInt_TwoParamTest, Error_Bool) { @@ -4586,8 +4640,9 @@ TEST_P(TypeDeterminerTextureIntrinsicTest, Call) { switch (param.texture_kind) { case ast::intrinsic::test::TextureKind::kRegular: - ASSERT_TRUE(call.result_type()->IsVector()); - EXPECT_EQ(call.result_type()->AsVector()->type(), datatype); + ASSERT_TRUE(call.result_type()->Is()); + EXPECT_EQ(call.result_type()->As()->type(), + datatype); break; case ast::intrinsic::test::TextureKind::kDepth: diff --git a/src/writer/hlsl/generator_impl.cc b/src/writer/hlsl/generator_impl.cc index 74c3781b40..b0e3f90b30 100644 --- a/src/writer/hlsl/generator_impl.cc +++ b/src/writer/hlsl/generator_impl.cc @@ -377,8 +377,10 @@ bool GeneratorImpl::EmitBinary(std::ostream& pre, // Multiplying by a matrix requires the use of `mul` in order to get the // type of multiply we desire. if (expr->op() == ast::BinaryOp::kMultiply && - ((lhs_type->IsVector() && rhs_type->Is()) || - (lhs_type->Is() && rhs_type->IsVector()) || + ((lhs_type->Is() && + rhs_type->Is()) || + (lhs_type->Is() && + rhs_type->Is()) || (lhs_type->Is() && rhs_type->Is()))) { out << "mul("; @@ -614,13 +616,14 @@ bool GeneratorImpl::EmitCall(std::ostream& pre, // out << "("; // auto param1_type = params[1]->result_type()->UnwrapPtrIfNeeded(); - // if (!param1_type->IsVector()) { + // if (!param1_type->Is()) { // error_ = "invalid param type in outer_product got: " + // param1_type->type_name(); // return false; // } - // for (uint32_t i = 0; i < param1_type->AsVector()->size(); ++i) { + // for (uint32_t i = 0; i < + // param1_type->As()->size(); ++i) { // if (i > 0) { // out << ", "; // } @@ -1556,8 +1559,8 @@ bool GeneratorImpl::EmitZeroValue(std::ostream& out, ast::type::Type* type) { out << "0"; } else if (type->Is()) { out << "0u"; - } else if (type->IsVector()) { - return EmitZeroValue(out, type->AsVector()->type()); + } else if (type->Is()) { + return EmitZeroValue(out, type->As()->type()); } else if (type->Is()) { auto* mat = type->As(); for (uint32_t i = 0; i < (mat->rows() * mat->columns()); i++) { @@ -1697,7 +1700,7 @@ std::string GeneratorImpl::generate_storage_buffer_index_expression( } out << str_member->offset(); - } else if (res_type->IsVector()) { + } else if (res_type->Is()) { // This must be a single element swizzle if we've got a vector at this // point. if (mem->member()->name().size() != 1) { @@ -1726,7 +1729,7 @@ std::string GeneratorImpl::generate_storage_buffer_index_expression( out << "("; if (ary_type->Is()) { out << ary_type->As()->array_stride(); - } else if (ary_type->IsVector()) { + } else if (ary_type->Is()) { // TODO(dsinclair): This is a hack. Our vectors can only be f32, i32 // or u32 which are all 4 bytes. When we get f16 or other types we'll // have to ask the type for the byte size. @@ -1773,8 +1776,9 @@ bool GeneratorImpl::EmitStorageBufferAccessor(std::ostream& pre, bool is_store = rhs != nullptr; std::string access_method = is_store ? "Store" : "Load"; - if (result_type->IsVector()) { - access_method += std::to_string(result_type->AsVector()->size()); + if (result_type->Is()) { + access_method += + std::to_string(result_type->As()->size()); } else if (result_type->Is()) { access_method += std::to_string(result_type->As()->rows()); @@ -1891,7 +1895,8 @@ bool GeneratorImpl::is_storage_buffer_access( auto* data_type = structure->result_type()->UnwrapAll(); // If the data is a multi-element swizzle then we will not load the swizzle // portion through the Load command. - if (data_type->IsVector() && expr->member()->name().size() > 1) { + if (data_type->Is() && + expr->member()->name().size() > 1) { return false; } @@ -2127,8 +2132,8 @@ bool GeneratorImpl::EmitType(std::ostream& out, } else if (type->Is()) { out << "uint"; - } else if (type->IsVector()) { - auto* vec = type->AsVector(); + } else if (type->Is()) { + auto* vec = type->As(); auto size = vec->size(); if (vec->type()->Is() && size >= 1 && size <= 4) { out << "float" << size; diff --git a/src/writer/msl/generator_impl.cc b/src/writer/msl/generator_impl.cc index b90d6104f6..ee8d27ecf1 100644 --- a/src/writer/msl/generator_impl.cc +++ b/src/writer/msl/generator_impl.cc @@ -242,8 +242,8 @@ uint32_t GeneratorImpl::calculate_alignment_size(ast::type::Type* type) { count = adjust_for_alignment(count, largest_alignment); return count; } - if (type->IsVector()) { - auto* vec = type->AsVector(); + if (type->Is()) { + auto* vec = type->As(); uint32_t type_size = calculate_alignment_size(vec->type()); if (vec->size() == 2) { return 2 * type_size; @@ -519,13 +519,14 @@ bool GeneratorImpl::EmitCall(ast::CallExpression* expr) { // out_ << "("; // auto param1_type = params[1]->result_type()->UnwrapPtrIfNeeded(); - // if (!param1_type->IsVector()) { + // if (!param1_type->Is()) { // error_ = "invalid param type in outer_product got: " + // param1_type->type_name(); // return false; // } - // for (uint32_t i = 0; i < param1_type->AsVector()->size(); ++i) { + // for (uint32_t i = 0; i < + // param1_type->As()->size(); ++i) { // if (i > 0) { // out_ << ", "; // } @@ -937,8 +938,8 @@ bool GeneratorImpl::EmitZeroValue(ast::type::Type* type) { out_ << "0"; } else if (type->Is()) { out_ << "0u"; - } else if (type->IsVector()) { - return EmitZeroValue(type->AsVector()->type()); + } else if (type->Is()) { + return EmitZeroValue(type->As()->type()); } else if (type->Is()) { return EmitZeroValue(type->As()->type()); } else if (type->Is()) { @@ -1911,8 +1912,8 @@ bool GeneratorImpl::EmitType(ast::type::Type* type, const std::string& name) { } else if (type->Is()) { out_ << "uint"; - } else if (type->IsVector()) { - auto* vec = type->AsVector(); + } else if (type->Is()) { + auto* vec = type->As(); if (!EmitType(vec->type(), "")) { return false; } diff --git a/src/writer/pack_coord_arrayidx.cc b/src/writer/pack_coord_arrayidx.cc index 3384ee65c8..8004c0a5c0 100644 --- a/src/writer/pack_coord_arrayidx.cc +++ b/src/writer/pack_coord_arrayidx.cc @@ -33,7 +33,7 @@ ast::TypeConstructorExpression* AsVectorConstructor(ast::Expression* expr) { return nullptr; } auto* type_constructor = constructor->AsTypeConstructor(); - if (!type_constructor->type()->IsVector()) { + if (!type_constructor->type()->Is()) { return nullptr; } return type_constructor; @@ -47,8 +47,8 @@ bool PackCoordAndArrayIndex( std::function callback) { uint32_t packed_size; ast::type::Type* packed_el_ty; // Currenly must be f32. - if (coords->result_type()->IsVector()) { - auto* vec = coords->result_type()->AsVector(); + if (coords->result_type()->Is()) { + auto* vec = coords->result_type()->As(); packed_size = vec->size() + 1; packed_el_ty = vec->type(); } else { diff --git a/src/writer/spirv/builder.cc b/src/writer/spirv/builder.cc index 974b56dfdc..0b77675db8 100644 --- a/src/writer/spirv/builder.cc +++ b/src/writer/spirv/builder.cc @@ -881,7 +881,7 @@ bool Builder::GenerateMemberAccessor(ast::MemberAccessorExpression* expr, return true; } - if (!data_type->IsVector()) { + if (!data_type->Is()) { error_ = "Member accessor without a struct or vector. Something is wrong"; return false; } @@ -1191,7 +1191,8 @@ bool Builder::is_constructor_const(ast::Expression* expr, bool is_global_init) { return false; } - if (result_type->IsVector() && !e->AsConstructor()->IsScalarConstructor()) { + if (result_type->Is() && + !e->AsConstructor()->IsScalarConstructor()) { return false; } @@ -1202,8 +1203,8 @@ bool Builder::is_constructor_const(ast::Expression* expr, bool is_global_init) { auto* sc = e->AsConstructor()->AsScalarConstructor(); ast::type::Type* subtype = result_type->UnwrapAll(); - if (subtype->IsVector()) { - subtype = subtype->AsVector()->type()->UnwrapAll(); + if (subtype->Is()) { + subtype = subtype->As()->type()->UnwrapAll(); } else if (subtype->Is()) { subtype = subtype->As()->type()->UnwrapAll(); } else if (subtype->Is()) { @@ -1244,12 +1245,14 @@ uint32_t Builder::GenerateTypeConstructorExpression( bool can_cast_or_copy = result_type->is_scalar(); - if (result_type->IsVector() && result_type->AsVector()->type()->is_scalar()) { + if (result_type->Is() && + result_type->As()->type()->is_scalar()) { auto* value_type = values[0]->result_type()->UnwrapAll(); can_cast_or_copy = - (value_type->IsVector() && - value_type->AsVector()->type()->is_scalar() && - result_type->AsVector()->size() == value_type->AsVector()->size()); + (value_type->Is() && + value_type->As()->type()->is_scalar() && + result_type->As()->size() == + value_type->As()->size()); } if (can_cast_or_copy) { return GenerateCastOrCopyOrPassthrough(result_type, values[0]); @@ -1263,8 +1266,8 @@ uint32_t Builder::GenerateTypeConstructorExpression( bool result_is_constant_composite = constructor_is_const; bool result_is_spec_composite = false; - if (result_type->IsVector()) { - result_type = result_type->AsVector()->type(); + if (result_type->Is()) { + result_type = result_type->As()->type(); } OperandList ops; @@ -1312,8 +1315,8 @@ uint32_t Builder::GenerateTypeConstructorExpression( // // For cases 1 and 2, if the type is different we also may need to insert // a type cast. - if (value_type->IsVector()) { - auto* vec = value_type->AsVector(); + if (value_type->Is()) { + auto* vec = value_type->As(); auto* vec_type = vec->type(); auto value_type_id = GenerateTypeIfNeeded(vec_type); @@ -1426,7 +1429,8 @@ uint32_t Builder::GenerateCastOrCopyOrPassthrough(ast::type::Type* to_type, to_type->Is()) || (from_type->Is() && to_type->Is()) || - (from_type->IsVector() && (from_type == to_type))) { + (from_type->Is() && + (from_type == to_type))) { return val_id; } else if ((from_type->Is() && to_type->Is()) || @@ -2454,8 +2458,8 @@ uint32_t Builder::GenerateTypeIfNeeded(ast::type::Type* type) { } } else if (type->Is()) { push_type(spv::Op::OpTypeInt, {result, Operand::Int(32), Operand::Int(0)}); - } else if (type->IsVector()) { - if (!GenerateVectorType(type->AsVector(), result)) { + } else if (type->Is()) { + if (!GenerateVectorType(type->As(), result)) { return 0; } } else if (type->IsVoid()) { diff --git a/src/writer/spirv/builder.h b/src/writer/spirv/builder.h index ffda68c030..f523b71254 100644 --- a/src/writer/spirv/builder.h +++ b/src/writer/spirv/builder.h @@ -33,6 +33,7 @@ #include "src/ast/type/pointer_type.h" #include "src/ast/type/storage_texture_type.h" #include "src/ast/type/struct_type.h" +#include "src/ast/type/vector_type.h" #include "src/ast/type_constructor_expression.h" #include "src/context.h" #include "src/scope_stack.h" diff --git a/src/writer/wgsl/generator_impl.cc b/src/writer/wgsl/generator_impl.cc index 53e2690cbc..9411aa5b0c 100644 --- a/src/writer/wgsl/generator_impl.cc +++ b/src/writer/wgsl/generator_impl.cc @@ -547,8 +547,8 @@ bool GeneratorImpl::EmitType(ast::type::Type* type) { } else if (type->Is()) { out_ << "u32"; - } else if (type->IsVector()) { - auto* vec = type->AsVector(); + } else if (type->Is()) { + auto* vec = type->As(); out_ << "vec" << vec->size() << "<"; if (!EmitType(vec->type())) { return false;