From 351128a41e5990cdbbd3f950ee9699b4e15a787f 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)Sampler with Castable Change-Id: Iaa43e607199e25308ecb121c61bb7a37abff197c Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/34270 Reviewed-by: dan sinclair --- src/ast/function.cc | 4 ++-- src/ast/type/access_control_type_test.cc | 2 +- src/ast/type/alias_type_test.cc | 2 +- src/ast/type/array_type_test.cc | 2 +- src/ast/type/bool_type_test.cc | 2 +- src/ast/type/depth_texture_type_test.cc | 2 +- src/ast/type/f32_type_test.cc | 2 +- src/ast/type/i32_type_test.cc | 2 +- src/ast/type/matrix_type_test.cc | 2 +- src/ast/type/multisampled_texture_type_test.cc | 2 +- src/ast/type/pointer_type_test.cc | 2 +- src/ast/type/sampled_texture_type_test.cc | 2 +- src/ast/type/sampler_type.cc | 4 ---- src/ast/type/sampler_type.h | 3 --- src/ast/type/sampler_type_test.cc | 2 +- src/ast/type/storage_texture_type_test.cc | 2 +- src/ast/type/struct_type_test.cc | 2 +- src/ast/type/type.cc | 14 -------------- src/ast/type/type.h | 7 ------- src/ast/type/u32_type_test.cc | 3 ++- src/ast/type/vector_type_test.cc | 2 +- src/reader/wgsl/parser_impl_sampler_type_test.cc | 8 ++++---- .../wgsl/parser_impl_texture_sampler_types_test.cc | 8 ++++---- src/reader/wgsl/parser_impl_type_decl_test.cc | 4 ++-- src/writer/hlsl/generator_impl.cc | 4 ++-- src/writer/msl/generator_impl.cc | 2 +- src/writer/spirv/builder.cc | 4 ++-- src/writer/wgsl/generator_impl.cc | 4 ++-- 28 files changed, 36 insertions(+), 63 deletions(-) diff --git a/src/ast/function.cc b/src/ast/function.cc index da18d53ca7..2dc9c2160a 100644 --- a/src/ast/function.cc +++ b/src/ast/function.cc @@ -281,8 +281,8 @@ Function::ReferencedSamplerVariablesImpl(type::SamplerKind kind) const { for (auto* var : referenced_module_variables()) { auto* unwrapped_type = var->type()->UnwrapIfNeeded(); - if (!var->IsDecorated() || !unwrapped_type->IsSampler() || - unwrapped_type->AsSampler()->kind() != kind) { + if (!var->IsDecorated() || !unwrapped_type->Is() || + unwrapped_type->As()->kind() != kind) { continue; } diff --git a/src/ast/type/access_control_type_test.cc b/src/ast/type/access_control_type_test.cc index 5ea362dcb1..2557a533bb 100644 --- a/src/ast/type/access_control_type_test.cc +++ b/src/ast/type/access_control_type_test.cc @@ -59,7 +59,7 @@ TEST_F(AccessControlTypeTest, Is) { EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); - EXPECT_FALSE(ty->IsSampler()); + EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->IsStruct()); EXPECT_FALSE(ty->IsTexture()); EXPECT_FALSE(ty->IsU32()); diff --git a/src/ast/type/alias_type_test.cc b/src/ast/type/alias_type_test.cc index b4507ca819..8be31d00a9 100644 --- a/src/ast/type/alias_type_test.cc +++ b/src/ast/type/alias_type_test.cc @@ -60,7 +60,7 @@ TEST_F(AliasTypeTest, Is) { EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); - EXPECT_FALSE(ty->IsSampler()); + EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->IsStruct()); EXPECT_FALSE(ty->IsTexture()); EXPECT_FALSE(ty->IsU32()); diff --git a/src/ast/type/array_type_test.cc b/src/ast/type/array_type_test.cc index f45bc159fa..8ade7c4366 100644 --- a/src/ast/type/array_type_test.cc +++ b/src/ast/type/array_type_test.cc @@ -65,7 +65,7 @@ TEST_F(ArrayTypeTest, Is) { EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); - EXPECT_FALSE(ty->IsSampler()); + EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->IsStruct()); EXPECT_FALSE(ty->IsTexture()); EXPECT_FALSE(ty->IsU32()); diff --git a/src/ast/type/bool_type_test.cc b/src/ast/type/bool_type_test.cc index 3952c9aa72..9aa14a4277 100644 --- a/src/ast/type/bool_type_test.cc +++ b/src/ast/type/bool_type_test.cc @@ -40,7 +40,7 @@ TEST_F(BoolTypeTest, Is) { EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); - EXPECT_FALSE(ty->IsSampler()); + EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->IsStruct()); EXPECT_FALSE(ty->IsTexture()); EXPECT_FALSE(ty->IsU32()); diff --git a/src/ast/type/depth_texture_type_test.cc b/src/ast/type/depth_texture_type_test.cc index 3a58197b43..089c6962f7 100644 --- a/src/ast/type/depth_texture_type_test.cc +++ b/src/ast/type/depth_texture_type_test.cc @@ -42,7 +42,7 @@ TEST_F(DepthTextureTypeTest, Is) { EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); - EXPECT_FALSE(ty->IsSampler()); + EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->IsStruct()); EXPECT_TRUE(ty->IsTexture()); EXPECT_FALSE(ty->IsU32()); diff --git a/src/ast/type/f32_type_test.cc b/src/ast/type/f32_type_test.cc index 01622af06e..6918a1f347 100644 --- a/src/ast/type/f32_type_test.cc +++ b/src/ast/type/f32_type_test.cc @@ -40,7 +40,7 @@ TEST_F(F32TypeTest, Is) { EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); - EXPECT_FALSE(ty->IsSampler()); + EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->IsStruct()); EXPECT_FALSE(ty->IsTexture()); EXPECT_FALSE(ty->IsU32()); diff --git a/src/ast/type/i32_type_test.cc b/src/ast/type/i32_type_test.cc index ff220006a9..d0fa3fe274 100644 --- a/src/ast/type/i32_type_test.cc +++ b/src/ast/type/i32_type_test.cc @@ -40,7 +40,7 @@ TEST_F(I32TypeTest, Is) { EXPECT_TRUE(ty->Is()); EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); - EXPECT_FALSE(ty->IsSampler()); + EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->IsStruct()); EXPECT_FALSE(ty->IsTexture()); EXPECT_FALSE(ty->IsU32()); diff --git a/src/ast/type/matrix_type_test.cc b/src/ast/type/matrix_type_test.cc index e203c36198..c1af49ffca 100644 --- a/src/ast/type/matrix_type_test.cc +++ b/src/ast/type/matrix_type_test.cc @@ -49,7 +49,7 @@ TEST_F(MatrixTypeTest, Is) { EXPECT_FALSE(ty->Is()); EXPECT_TRUE(ty->Is()); EXPECT_FALSE(ty->Is()); - EXPECT_FALSE(ty->IsSampler()); + EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->IsStruct()); EXPECT_FALSE(ty->IsTexture()); EXPECT_FALSE(ty->IsU32()); diff --git a/src/ast/type/multisampled_texture_type_test.cc b/src/ast/type/multisampled_texture_type_test.cc index 1e9b67ee44..52fca6fe34 100644 --- a/src/ast/type/multisampled_texture_type_test.cc +++ b/src/ast/type/multisampled_texture_type_test.cc @@ -42,7 +42,7 @@ TEST_F(MultisampledTextureTypeTest, Is) { EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); - EXPECT_FALSE(ty->IsSampler()); + EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->IsStruct()); EXPECT_TRUE(ty->IsTexture()); EXPECT_FALSE(ty->IsU32()); diff --git a/src/ast/type/pointer_type_test.cc b/src/ast/type/pointer_type_test.cc index 735082467b..3166db53ae 100644 --- a/src/ast/type/pointer_type_test.cc +++ b/src/ast/type/pointer_type_test.cc @@ -48,7 +48,7 @@ TEST_F(PointerTypeTest, Is) { EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); EXPECT_TRUE(ty->Is()); - EXPECT_FALSE(ty->IsSampler()); + EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->IsStruct()); EXPECT_FALSE(ty->IsTexture()); EXPECT_FALSE(ty->IsU32()); diff --git a/src/ast/type/sampled_texture_type_test.cc b/src/ast/type/sampled_texture_type_test.cc index be16aebeba..47902294b9 100644 --- a/src/ast/type/sampled_texture_type_test.cc +++ b/src/ast/type/sampled_texture_type_test.cc @@ -42,7 +42,7 @@ TEST_F(SampledTextureTypeTest, Is) { EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); - EXPECT_FALSE(ty->IsSampler()); + EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->IsStruct()); EXPECT_TRUE(ty->IsTexture()); EXPECT_FALSE(ty->IsU32()); diff --git a/src/ast/type/sampler_type.cc b/src/ast/type/sampler_type.cc index ccbd3fa89b..c183b432d0 100644 --- a/src/ast/type/sampler_type.cc +++ b/src/ast/type/sampler_type.cc @@ -36,10 +36,6 @@ SamplerType::SamplerType(SamplerType&&) = default; SamplerType::~SamplerType() = default; -bool SamplerType::IsSampler() const { - return true; -} - std::string SamplerType::type_name() const { return std::string("__sampler_") + (kind_ == SamplerKind::kSampler ? "sampler" : "comparison"); diff --git a/src/ast/type/sampler_type.h b/src/ast/type/sampler_type.h index d064a88395..c83c44a24f 100644 --- a/src/ast/type/sampler_type.h +++ b/src/ast/type/sampler_type.h @@ -43,9 +43,6 @@ class SamplerType : public Castable { SamplerType(SamplerType&&); ~SamplerType() override; - /// @returns true if the type is a sampler type - bool IsSampler() const override; - /// @returns the sampler type SamplerKind kind() const { return kind_; } diff --git a/src/ast/type/sampler_type_test.cc b/src/ast/type/sampler_type_test.cc index 42210bcfa6..4bdc6e441f 100644 --- a/src/ast/type/sampler_type_test.cc +++ b/src/ast/type/sampler_type_test.cc @@ -52,7 +52,7 @@ TEST_F(SamplerTypeTest, Is) { EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); - EXPECT_TRUE(ty->IsSampler()); + EXPECT_TRUE(ty->Is()); EXPECT_FALSE(ty->IsStruct()); EXPECT_FALSE(ty->IsTexture()); EXPECT_FALSE(ty->IsU32()); diff --git a/src/ast/type/storage_texture_type_test.cc b/src/ast/type/storage_texture_type_test.cc index f7764d1ef1..43a0d29d9f 100644 --- a/src/ast/type/storage_texture_type_test.cc +++ b/src/ast/type/storage_texture_type_test.cc @@ -46,7 +46,7 @@ TEST_F(StorageTextureTypeTest, Is) { EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); - EXPECT_FALSE(ty->IsSampler()); + EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->IsStruct()); EXPECT_TRUE(ty->IsTexture()); EXPECT_FALSE(ty->IsU32()); diff --git a/src/ast/type/struct_type_test.cc b/src/ast/type/struct_type_test.cc index 8667acf822..c13caa219e 100644 --- a/src/ast/type/struct_type_test.cc +++ b/src/ast/type/struct_type_test.cc @@ -57,7 +57,7 @@ TEST_F(StructTypeTest, Is) { EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); - EXPECT_FALSE(ty->IsSampler()); + EXPECT_FALSE(ty->Is()); EXPECT_TRUE(ty->IsStruct()); EXPECT_FALSE(ty->IsTexture()); EXPECT_FALSE(ty->IsU32()); diff --git a/src/ast/type/type.cc b/src/ast/type/type.cc index affca72057..1e8ed5c9f7 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::IsSampler() const { - return false; -} - bool Type::IsStruct() const { return false; } @@ -142,11 +138,6 @@ bool Type::is_integer_scalar_or_vector() { return is_unsigned_scalar_or_vector() || is_signed_scalar_or_vector(); } -const SamplerType* Type::AsSampler() const { - assert(IsSampler()); - return static_cast(this); -} - const StructType* Type::AsStruct() const { assert(IsStruct()); return static_cast(this); @@ -172,11 +163,6 @@ const VoidType* Type::AsVoid() const { return static_cast(this); } -SamplerType* Type::AsSampler() { - assert(IsSampler()); - return static_cast(this); -} - StructType* Type::AsStruct() { assert(IsStruct()); return static_cast(this); diff --git a/src/ast/type/type.h b/src/ast/type/type.h index 93a9fb89e3..805c058ffb 100644 --- a/src/ast/type/type.h +++ b/src/ast/type/type.h @@ -23,7 +23,6 @@ namespace tint { namespace ast { namespace type { -class SamplerType; class StructType; class TextureType; class U32Type; @@ -40,8 +39,6 @@ class Type : public Castable { Type(Type&&); ~Type() override; - /// @returns true if the type is a sampler - virtual bool IsSampler() const; /// @returns true if the type is a struct type virtual bool IsStruct() const; /// @returns true if the type is a texture type @@ -107,8 +104,6 @@ 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 sampler type - const SamplerType* AsSampler() const; /// @returns the type as a struct type const StructType* AsStruct() const; /// @returns the type as a texture type @@ -120,8 +115,6 @@ class Type : public Castable { /// @returns the type as a void type const VoidType* AsVoid() const; - /// @returns the type as a sampler type - SamplerType* AsSampler(); /// @returns the type as a struct type StructType* AsStruct(); /// @returns the type as a texture type diff --git a/src/ast/type/u32_type_test.cc b/src/ast/type/u32_type_test.cc index 60a946b20d..77b2e40f60 100644 --- a/src/ast/type/u32_type_test.cc +++ b/src/ast/type/u32_type_test.cc @@ -22,6 +22,7 @@ #include "src/ast/type/i32_type.h" #include "src/ast/type/matrix_type.h" #include "src/ast/type/pointer_type.h" +#include "src/ast/type/sampler_type.h" namespace tint { namespace ast { @@ -41,7 +42,7 @@ TEST_F(U32TypeTest, Is) { EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); - EXPECT_FALSE(ty->IsSampler()); + EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->IsStruct()); EXPECT_FALSE(ty->IsTexture()); EXPECT_TRUE(ty->IsU32()); diff --git a/src/ast/type/vector_type_test.cc b/src/ast/type/vector_type_test.cc index d59d96f9af..94a8b3f0a6 100644 --- a/src/ast/type/vector_type_test.cc +++ b/src/ast/type/vector_type_test.cc @@ -49,7 +49,7 @@ TEST_F(VectorTypeTest, Is) { EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); - EXPECT_FALSE(ty->IsSampler()); + EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->IsStruct()); EXPECT_FALSE(ty->IsTexture()); EXPECT_FALSE(ty->IsU32()); diff --git a/src/reader/wgsl/parser_impl_sampler_type_test.cc b/src/reader/wgsl/parser_impl_sampler_type_test.cc index 76e376a959..5b17627208 100644 --- a/src/reader/wgsl/parser_impl_sampler_type_test.cc +++ b/src/reader/wgsl/parser_impl_sampler_type_test.cc @@ -37,8 +37,8 @@ TEST_F(ParserImplTest, SamplerType_Sampler) { EXPECT_TRUE(t.matched); EXPECT_FALSE(t.errored); ASSERT_NE(t.value, nullptr); - ASSERT_TRUE(t->IsSampler()); - EXPECT_FALSE(t->AsSampler()->IsComparison()); + ASSERT_TRUE(t->Is()); + EXPECT_FALSE(t->As()->IsComparison()); EXPECT_FALSE(p->has_error()); } @@ -48,8 +48,8 @@ TEST_F(ParserImplTest, SamplerType_ComparisonSampler) { EXPECT_TRUE(t.matched); EXPECT_FALSE(t.errored); ASSERT_NE(t.value, nullptr); - ASSERT_TRUE(t->IsSampler()); - EXPECT_TRUE(t->AsSampler()->IsComparison()); + ASSERT_TRUE(t->Is()); + EXPECT_TRUE(t->As()->IsComparison()); EXPECT_FALSE(p->has_error()); } diff --git a/src/reader/wgsl/parser_impl_texture_sampler_types_test.cc b/src/reader/wgsl/parser_impl_texture_sampler_types_test.cc index c1eb2c6a22..463533ddc2 100644 --- a/src/reader/wgsl/parser_impl_texture_sampler_types_test.cc +++ b/src/reader/wgsl/parser_impl_texture_sampler_types_test.cc @@ -42,8 +42,8 @@ TEST_F(ParserImplTest, TextureSamplerTypes_Sampler) { EXPECT_TRUE(t.matched); EXPECT_FALSE(t.errored); ASSERT_NE(t.value, nullptr); - ASSERT_TRUE(t->IsSampler()); - ASSERT_FALSE(t->AsSampler()->IsComparison()); + ASSERT_TRUE(t->Is()); + ASSERT_FALSE(t->As()->IsComparison()); } TEST_F(ParserImplTest, TextureSamplerTypes_SamplerComparison) { @@ -53,8 +53,8 @@ TEST_F(ParserImplTest, TextureSamplerTypes_SamplerComparison) { EXPECT_TRUE(t.matched); EXPECT_FALSE(t.errored); ASSERT_NE(t.value, nullptr); - ASSERT_TRUE(t->IsSampler()); - ASSERT_TRUE(t->AsSampler()->IsComparison()); + ASSERT_TRUE(t->Is()); + ASSERT_TRUE(t->As()->IsComparison()); } TEST_F(ParserImplTest, TextureSamplerTypes_DepthTexture) { diff --git a/src/reader/wgsl/parser_impl_type_decl_test.cc b/src/reader/wgsl/parser_impl_type_decl_test.cc index 5943dabf57..0472dae28b 100644 --- a/src/reader/wgsl/parser_impl_type_decl_test.cc +++ b/src/reader/wgsl/parser_impl_type_decl_test.cc @@ -747,8 +747,8 @@ TEST_F(ParserImplTest, TypeDecl_Sampler) { EXPECT_FALSE(t.errored); ASSERT_NE(t.value, nullptr) << p->error(); EXPECT_EQ(t.value, type); - ASSERT_TRUE(t->IsSampler()); - ASSERT_FALSE(t->AsSampler()->IsComparison()); + ASSERT_TRUE(t->Is()); + ASSERT_FALSE(t->As()->IsComparison()); } TEST_F(ParserImplTest, TypeDecl_Texture_Old) { diff --git a/src/writer/hlsl/generator_impl.cc b/src/writer/hlsl/generator_impl.cc index a697caa226..525d8a133e 100644 --- a/src/writer/hlsl/generator_impl.cc +++ b/src/writer/hlsl/generator_impl.cc @@ -2079,8 +2079,8 @@ bool GeneratorImpl::EmitType(std::ostream& out, // https://bugs.chromium.org/p/tint/issues/detail?id=183 error_ = "pointers not supported in HLSL"; return false; - } else if (type->IsSampler()) { - auto* sampler = type->AsSampler(); + } else if (type->Is()) { + auto* sampler = type->As(); out << "Sampler"; if (sampler->IsComparison()) { out << "Comparison"; diff --git a/src/writer/msl/generator_impl.cc b/src/writer/msl/generator_impl.cc index 762cd9ab7b..695ccfdb80 100644 --- a/src/writer/msl/generator_impl.cc +++ b/src/writer/msl/generator_impl.cc @@ -1832,7 +1832,7 @@ bool GeneratorImpl::EmitType(ast::type::Type* type, const std::string& name) { return false; } out_ << "*"; - } else if (type->IsSampler()) { + } else if (type->Is()) { out_ << "sampler"; } else if (type->IsStruct()) { // The struct type emits as just the name. The declaration would be emitted diff --git a/src/writer/spirv/builder.cc b/src/writer/spirv/builder.cc index 0906c6f2fc..590f4677bc 100644 --- a/src/writer/spirv/builder.cc +++ b/src/writer/spirv/builder.cc @@ -734,7 +734,7 @@ bool Builder::GenerateGlobalVariable(ast::Variable* var) { break; } } - } else if (!type->IsSampler()) { + } else if (!type->Is()) { // Certain cases require us to generate a constructor value. // // 1- ConstantId's must be attached to the OpConstant, if we have a @@ -2452,7 +2452,7 @@ uint32_t Builder::GenerateTypeIfNeeded(ast::type::Type* type) { if (!GenerateTextureType(type->AsTexture(), result)) { return 0; } - } else if (type->IsSampler()) { + } else if (type->Is()) { push_type(spv::Op::OpTypeSampler, {result}); // Register both of the sampler type names. In SPIR-V they're the same diff --git a/src/writer/wgsl/generator_impl.cc b/src/writer/wgsl/generator_impl.cc index e9ed0e8bad..52fa96177b 100644 --- a/src/writer/wgsl/generator_impl.cc +++ b/src/writer/wgsl/generator_impl.cc @@ -453,8 +453,8 @@ bool GeneratorImpl::EmitType(ast::type::Type* type) { return false; } out_ << ">"; - } else if (type->IsSampler()) { - auto* sampler = type->AsSampler(); + } else if (type->Is()) { + auto* sampler = type->As(); out_ << "sampler"; if (sampler->IsComparison()) {