From c52f4214ad165f29e5f7c72f9f1567619545e66f 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)Pointer with Castable Change-Id: Ic1b82f1ddc61355021a80d8e72f645f830363c62 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/34269 Reviewed-by: dan sinclair --- src/ast/type/access_control_type_test.cc | 2 +- src/ast/type/alias_type_test.cc | 2 +- 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.cc | 4 -- src/ast/type/pointer_type.h | 3 - src/ast/type/pointer_type_test.cc | 2 +- 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 | 3 +- src/ast/type/type.cc | 18 +----- src/ast/type/type.h | 7 --- src/ast/type/u32_type_test.cc | 3 +- src/ast/type/vector_type_test.cc | 3 +- src/reader/spirv/function.cc | 19 ++++--- src/reader/spirv/parser_impl.cc | 7 ++- src/reader/spirv/parser_impl.h | 1 + .../spirv/parser_impl_convert_type_test.cc | 44 +++++++-------- src/reader/wgsl/parser_impl_type_decl_test.cc | 8 +-- src/type_determiner.cc | 14 ++--- src/type_determiner_test.cc | 56 ++++++++++--------- src/writer/hlsl/generator_impl.cc | 3 +- src/writer/msl/generator_impl.cc | 6 +- src/writer/spirv/builder.cc | 16 +++--- src/writer/spirv/builder.h | 1 + src/writer/wgsl/generator_impl.cc | 4 +- 32 files changed, 126 insertions(+), 130 deletions(-) diff --git a/src/ast/type/access_control_type_test.cc b/src/ast/type/access_control_type_test.cc index 94e0e73d91..5ea362dcb1 100644 --- a/src/ast/type/access_control_type_test.cc +++ b/src/ast/type/access_control_type_test.cc @@ -58,7 +58,7 @@ TEST_F(AccessControlTypeTest, Is) { EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); - EXPECT_FALSE(ty->IsPointer()); + EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->IsSampler()); EXPECT_FALSE(ty->IsStruct()); EXPECT_FALSE(ty->IsTexture()); diff --git a/src/ast/type/alias_type_test.cc b/src/ast/type/alias_type_test.cc index 99075a93b6..b4507ca819 100644 --- a/src/ast/type/alias_type_test.cc +++ b/src/ast/type/alias_type_test.cc @@ -59,7 +59,7 @@ TEST_F(AliasTypeTest, Is) { EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); - EXPECT_FALSE(ty->IsPointer()); + EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->IsSampler()); EXPECT_FALSE(ty->IsStruct()); EXPECT_FALSE(ty->IsTexture()); diff --git a/src/ast/type/array_type_test.cc b/src/ast/type/array_type_test.cc index 5c0ac8ff9e..f45bc159fa 100644 --- a/src/ast/type/array_type_test.cc +++ b/src/ast/type/array_type_test.cc @@ -24,6 +24,7 @@ #include "src/ast/type/f32_type.h" #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/u32_type.h" namespace tint { @@ -63,7 +64,7 @@ TEST_F(ArrayTypeTest, Is) { EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); - EXPECT_FALSE(ty->IsPointer()); + EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->IsSampler()); EXPECT_FALSE(ty->IsStruct()); EXPECT_FALSE(ty->IsTexture()); diff --git a/src/ast/type/bool_type_test.cc b/src/ast/type/bool_type_test.cc index 05ee0bbd79..3952c9aa72 100644 --- a/src/ast/type/bool_type_test.cc +++ b/src/ast/type/bool_type_test.cc @@ -20,6 +20,7 @@ #include "src/ast/type/f32_type.h" #include "src/ast/type/i32_type.h" #include "src/ast/type/matrix_type.h" +#include "src/ast/type/pointer_type.h" namespace tint { namespace ast { @@ -38,7 +39,7 @@ TEST_F(BoolTypeTest, Is) { EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); - EXPECT_FALSE(ty->IsPointer()); + EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->IsSampler()); EXPECT_FALSE(ty->IsStruct()); EXPECT_FALSE(ty->IsTexture()); diff --git a/src/ast/type/depth_texture_type_test.cc b/src/ast/type/depth_texture_type_test.cc index 1e467abf57..3a58197b43 100644 --- a/src/ast/type/depth_texture_type_test.cc +++ b/src/ast/type/depth_texture_type_test.cc @@ -22,6 +22,7 @@ #include "src/ast/type/f32_type.h" #include "src/ast/type/i32_type.h" #include "src/ast/type/matrix_type.h" +#include "src/ast/type/pointer_type.h" namespace tint { namespace ast { @@ -40,7 +41,7 @@ TEST_F(DepthTextureTypeTest, Is) { EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); - EXPECT_FALSE(ty->IsPointer()); + EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->IsSampler()); EXPECT_FALSE(ty->IsStruct()); EXPECT_TRUE(ty->IsTexture()); diff --git a/src/ast/type/f32_type_test.cc b/src/ast/type/f32_type_test.cc index 75e7a53229..01622af06e 100644 --- a/src/ast/type/f32_type_test.cc +++ b/src/ast/type/f32_type_test.cc @@ -20,6 +20,7 @@ #include "src/ast/type/bool_type.h" #include "src/ast/type/i32_type.h" #include "src/ast/type/matrix_type.h" +#include "src/ast/type/pointer_type.h" namespace tint { namespace ast { @@ -38,7 +39,7 @@ TEST_F(F32TypeTest, Is) { EXPECT_TRUE(ty->Is()); EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); - EXPECT_FALSE(ty->IsPointer()); + EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->IsSampler()); EXPECT_FALSE(ty->IsStruct()); EXPECT_FALSE(ty->IsTexture()); diff --git a/src/ast/type/i32_type_test.cc b/src/ast/type/i32_type_test.cc index 11000af04a..ff220006a9 100644 --- a/src/ast/type/i32_type_test.cc +++ b/src/ast/type/i32_type_test.cc @@ -20,6 +20,7 @@ #include "src/ast/type/bool_type.h" #include "src/ast/type/f32_type.h" #include "src/ast/type/matrix_type.h" +#include "src/ast/type/pointer_type.h" namespace tint { namespace ast { @@ -38,7 +39,7 @@ TEST_F(I32TypeTest, Is) { EXPECT_FALSE(ty->Is()); EXPECT_TRUE(ty->Is()); EXPECT_FALSE(ty->Is()); - EXPECT_FALSE(ty->IsPointer()); + EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->IsSampler()); EXPECT_FALSE(ty->IsStruct()); EXPECT_FALSE(ty->IsTexture()); diff --git a/src/ast/type/matrix_type_test.cc b/src/ast/type/matrix_type_test.cc index 96d7b70c30..e203c36198 100644 --- a/src/ast/type/matrix_type_test.cc +++ b/src/ast/type/matrix_type_test.cc @@ -20,6 +20,7 @@ #include "src/ast/type/bool_type.h" #include "src/ast/type/f32_type.h" #include "src/ast/type/i32_type.h" +#include "src/ast/type/pointer_type.h" namespace tint { namespace ast { @@ -47,7 +48,7 @@ TEST_F(MatrixTypeTest, Is) { EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); EXPECT_TRUE(ty->Is()); - EXPECT_FALSE(ty->IsPointer()); + EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->IsSampler()); EXPECT_FALSE(ty->IsStruct()); EXPECT_FALSE(ty->IsTexture()); diff --git a/src/ast/type/multisampled_texture_type_test.cc b/src/ast/type/multisampled_texture_type_test.cc index 1ba22dae21..1e9b67ee44 100644 --- a/src/ast/type/multisampled_texture_type_test.cc +++ b/src/ast/type/multisampled_texture_type_test.cc @@ -21,6 +21,7 @@ #include "src/ast/type/f32_type.h" #include "src/ast/type/i32_type.h" #include "src/ast/type/matrix_type.h" +#include "src/ast/type/pointer_type.h" namespace tint { namespace ast { @@ -40,7 +41,7 @@ TEST_F(MultisampledTextureTypeTest, Is) { EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); - EXPECT_FALSE(ty->IsPointer()); + EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->IsSampler()); EXPECT_FALSE(ty->IsStruct()); EXPECT_TRUE(ty->IsTexture()); diff --git a/src/ast/type/pointer_type.cc b/src/ast/type/pointer_type.cc index b81382c1da..a577e78dc6 100644 --- a/src/ast/type/pointer_type.cc +++ b/src/ast/type/pointer_type.cc @@ -21,10 +21,6 @@ namespace type { PointerType::PointerType(Type* subtype, StorageClass storage_class) : subtype_(subtype), storage_class_(storage_class) {} -bool PointerType::IsPointer() const { - return true; -} - std::string PointerType::type_name() const { std::ostringstream out; out << "__ptr_" << storage_class_ << subtype_->type_name(); diff --git a/src/ast/type/pointer_type.h b/src/ast/type/pointer_type.h index f27eab68d9..9dbf71b8f6 100644 --- a/src/ast/type/pointer_type.h +++ b/src/ast/type/pointer_type.h @@ -36,9 +36,6 @@ class PointerType : public Castable { PointerType(PointerType&&); ~PointerType() override; - /// @returns true if the type is a pointer type - bool IsPointer() const override; - /// @returns the pointee type Type* type() const { return subtype_; } /// @returns the storage class of the pointer diff --git a/src/ast/type/pointer_type_test.cc b/src/ast/type/pointer_type_test.cc index 51e4f4aa66..735082467b 100644 --- a/src/ast/type/pointer_type_test.cc +++ b/src/ast/type/pointer_type_test.cc @@ -47,7 +47,7 @@ TEST_F(PointerTypeTest, Is) { EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); - EXPECT_TRUE(ty->IsPointer()); + EXPECT_TRUE(ty->Is()); EXPECT_FALSE(ty->IsSampler()); EXPECT_FALSE(ty->IsStruct()); EXPECT_FALSE(ty->IsTexture()); diff --git a/src/ast/type/sampled_texture_type_test.cc b/src/ast/type/sampled_texture_type_test.cc index 5421005fde..be16aebeba 100644 --- a/src/ast/type/sampled_texture_type_test.cc +++ b/src/ast/type/sampled_texture_type_test.cc @@ -21,6 +21,7 @@ #include "src/ast/type/f32_type.h" #include "src/ast/type/i32_type.h" #include "src/ast/type/matrix_type.h" +#include "src/ast/type/pointer_type.h" namespace tint { namespace ast { @@ -40,7 +41,7 @@ TEST_F(SampledTextureTypeTest, Is) { EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); - EXPECT_FALSE(ty->IsPointer()); + EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->IsSampler()); EXPECT_FALSE(ty->IsStruct()); EXPECT_TRUE(ty->IsTexture()); diff --git a/src/ast/type/sampler_type_test.cc b/src/ast/type/sampler_type_test.cc index 2c03f2d176..42210bcfa6 100644 --- a/src/ast/type/sampler_type_test.cc +++ b/src/ast/type/sampler_type_test.cc @@ -21,6 +21,7 @@ #include "src/ast/type/f32_type.h" #include "src/ast/type/i32_type.h" #include "src/ast/type/matrix_type.h" +#include "src/ast/type/pointer_type.h" namespace tint { namespace ast { @@ -50,7 +51,7 @@ TEST_F(SamplerTypeTest, Is) { EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); - EXPECT_FALSE(ty->IsPointer()); + EXPECT_FALSE(ty->Is()); EXPECT_TRUE(ty->IsSampler()); EXPECT_FALSE(ty->IsStruct()); EXPECT_FALSE(ty->IsTexture()); diff --git a/src/ast/type/storage_texture_type_test.cc b/src/ast/type/storage_texture_type_test.cc index 03487de23b..f7764d1ef1 100644 --- a/src/ast/type/storage_texture_type_test.cc +++ b/src/ast/type/storage_texture_type_test.cc @@ -24,6 +24,7 @@ #include "src/ast/type/f32_type.h" #include "src/ast/type/i32_type.h" #include "src/ast/type/matrix_type.h" +#include "src/ast/type/pointer_type.h" #include "src/type_determiner.h" namespace tint { @@ -44,7 +45,7 @@ TEST_F(StorageTextureTypeTest, Is) { EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); - EXPECT_FALSE(ty->IsPointer()); + EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->IsSampler()); EXPECT_FALSE(ty->IsStruct()); EXPECT_TRUE(ty->IsTexture()); diff --git a/src/ast/type/struct_type_test.cc b/src/ast/type/struct_type_test.cc index a3dd3b2e7e..8667acf822 100644 --- a/src/ast/type/struct_type_test.cc +++ b/src/ast/type/struct_type_test.cc @@ -27,6 +27,7 @@ #include "src/ast/type/f32_type.h" #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/u32_type.h" #include "src/ast/type/vector_type.h" @@ -55,7 +56,7 @@ TEST_F(StructTypeTest, Is) { EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); - EXPECT_FALSE(ty->IsPointer()); + EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->IsSampler()); EXPECT_TRUE(ty->IsStruct()); EXPECT_FALSE(ty->IsTexture()); diff --git a/src/ast/type/type.cc b/src/ast/type/type.cc index 51b87451fa..affca72057 100644 --- a/src/ast/type/type.cc +++ b/src/ast/type/type.cc @@ -42,8 +42,8 @@ Type::Type(Type&&) = default; Type::~Type() = default; Type* Type::UnwrapPtrIfNeeded() { - if (IsPointer()) { - return AsPointer()->type(); + if (Is()) { + return As()->type(); } return this; } @@ -66,10 +66,6 @@ Type* Type::UnwrapAll() { return UnwrapIfNeeded()->UnwrapPtrIfNeeded()->UnwrapIfNeeded(); } -bool Type::IsPointer() const { - return false; -} - bool Type::IsSampler() const { return false; } @@ -146,11 +142,6 @@ bool Type::is_integer_scalar_or_vector() { return is_unsigned_scalar_or_vector() || is_signed_scalar_or_vector(); } -const PointerType* Type::AsPointer() const { - assert(IsPointer()); - return static_cast(this); -} - const SamplerType* Type::AsSampler() const { assert(IsSampler()); return static_cast(this); @@ -181,11 +172,6 @@ const VoidType* Type::AsVoid() const { return static_cast(this); } -PointerType* Type::AsPointer() { - assert(IsPointer()); - return static_cast(this); -} - SamplerType* Type::AsSampler() { assert(IsSampler()); return static_cast(this); diff --git a/src/ast/type/type.h b/src/ast/type/type.h index 1c3efdc0fc..93a9fb89e3 100644 --- a/src/ast/type/type.h +++ b/src/ast/type/type.h @@ -23,7 +23,6 @@ namespace tint { namespace ast { namespace type { -class PointerType; class SamplerType; class StructType; class TextureType; @@ -41,8 +40,6 @@ class Type : public Castable { Type(Type&&); ~Type() override; - /// @returns true if the type is a ptr type - virtual bool IsPointer() const; /// @returns true if the type is a sampler virtual bool IsSampler() const; /// @returns true if the type is a struct type @@ -110,8 +107,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 pointer type - const PointerType* AsPointer() const; /// @returns the type as a sampler type const SamplerType* AsSampler() const; /// @returns the type as a struct type @@ -125,8 +120,6 @@ class Type : public Castable { /// @returns the type as a void type const VoidType* AsVoid() const; - /// @returns the type as a pointer type - PointerType* AsPointer(); /// @returns the type as a sampler type SamplerType* AsSampler(); /// @returns the type as a struct type diff --git a/src/ast/type/u32_type_test.cc b/src/ast/type/u32_type_test.cc index bc8f21a60f..60a946b20d 100644 --- a/src/ast/type/u32_type_test.cc +++ b/src/ast/type/u32_type_test.cc @@ -21,6 +21,7 @@ #include "src/ast/type/f32_type.h" #include "src/ast/type/i32_type.h" #include "src/ast/type/matrix_type.h" +#include "src/ast/type/pointer_type.h" namespace tint { namespace ast { @@ -39,7 +40,7 @@ TEST_F(U32TypeTest, Is) { EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); - EXPECT_FALSE(ty->IsPointer()); + EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->IsSampler()); EXPECT_FALSE(ty->IsStruct()); EXPECT_FALSE(ty->IsTexture()); diff --git a/src/ast/type/vector_type_test.cc b/src/ast/type/vector_type_test.cc index 8872f7db8e..d59d96f9af 100644 --- a/src/ast/type/vector_type_test.cc +++ b/src/ast/type/vector_type_test.cc @@ -21,6 +21,7 @@ #include "src/ast/type/f32_type.h" #include "src/ast/type/i32_type.h" #include "src/ast/type/matrix_type.h" +#include "src/ast/type/pointer_type.h" namespace tint { namespace ast { @@ -47,7 +48,7 @@ TEST_F(VectorTypeTest, Is) { EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->Is()); - EXPECT_FALSE(ty->IsPointer()); + EXPECT_FALSE(ty->Is()); EXPECT_FALSE(ty->IsSampler()); EXPECT_FALSE(ty->IsStruct()); EXPECT_FALSE(ty->IsTexture()); diff --git a/src/reader/spirv/function.cc b/src/reader/spirv/function.cc index 816fac19a0..5b505314d2 100644 --- a/src/reader/spirv/function.cc +++ b/src/reader/spirv/function.cc @@ -2698,8 +2698,8 @@ bool FunctionEmitter::EmitStatement(const spvtools::opt::Instruction& inst) { // So represent a load by a new const definition. auto expr = MakeExpression(inst.GetSingleWordInOperand(0)); // The load result type is the pointee type of its operand. - assert(expr.type->IsPointer()); - expr.type = expr.type->AsPointer()->type(); + assert(expr.type->Is()); + expr.type = expr.type->As()->type(); return EmitConstDefOrWriteToHoistedVar(inst, expr); } case SpvOpCopyObject: { @@ -3059,7 +3059,7 @@ TypedExpression FunctionEmitter::MakeAccessChain( type_mgr_->FindPointerToType(pointee_type_id, storage_class); auto* ast_pointer_type = parser_impl_.ConvertType(pointer_type_id); assert(ast_pointer_type); - assert(ast_pointer_type->IsPointer()); + assert(ast_pointer_type->Is()); current_expr = TypedExpression{ast_pointer_type, next_expr}; } return current_expr; @@ -3255,8 +3255,9 @@ bool FunctionEmitter::RegisterLocallyDefinedValues() { if (type) { if (type->AsPointer()) { const auto* ast_type = parser_impl_.ConvertType(inst.type_id()); - if (ast_type && ast_type->AsPointer()) { - info->storage_class = ast_type->AsPointer()->storage_class(); + if (ast_type && ast_type->As()) { + info->storage_class = + ast_type->As()->storage_class(); } switch (inst.opcode()) { case SpvOpUndef: @@ -3298,8 +3299,8 @@ ast::StorageClass FunctionEmitter::GetStorageClassForPointerValue(uint32_t id) { const auto type_id = def_use_mgr_->GetDef(id)->type_id(); if (type_id) { auto* ast_type = parser_impl_.ConvertType(type_id); - if (ast_type && ast_type->IsPointer()) { - return ast_type->AsPointer()->storage_class(); + if (ast_type && ast_type->Is()) { + return ast_type->As()->storage_class(); } } return ast::StorageClass::kNone; @@ -3307,10 +3308,10 @@ ast::StorageClass FunctionEmitter::GetStorageClassForPointerValue(uint32_t id) { ast::type::Type* FunctionEmitter::RemapStorageClass(ast::type::Type* type, uint32_t result_id) { - if (type->IsPointer()) { + if (type->Is()) { // Remap an old-style storage buffer pointer to a new-style storage // buffer pointer. - const auto* ast_ptr_type = type->AsPointer(); + const auto* ast_ptr_type = type->As(); const auto sc = GetStorageClassForPointerValue(result_id); if (ast_ptr_type->storage_class() != sc) { return parser_impl_.get_module().create( diff --git a/src/reader/spirv/parser_impl.cc b/src/reader/spirv/parser_impl.cc index 809a38cf72..82ecf8ab70 100644 --- a/src/reader/spirv/parser_impl.cc +++ b/src/reader/spirv/parser_impl.cc @@ -1116,14 +1116,15 @@ bool ParserImpl::EmitModuleScopeVariables() { "SPIR-V type with ID: " << var.type_id(); } - if (!ast_type->IsPointer()) { + if (!ast_type->Is()) { return Fail() << "variable with ID " << var.result_id() << " has non-pointer type " << var.type_id(); } } - auto* ast_store_type = ast_type->AsPointer()->type(); - auto ast_storage_class = ast_type->AsPointer()->storage_class(); + auto* ast_store_type = ast_type->As()->type(); + auto ast_storage_class = + ast_type->As()->storage_class(); auto* ast_var = MakeVariable(var.result_id(), ast_storage_class, ast_store_type); if (var.NumInOperands() > 1) { diff --git a/src/reader/spirv/parser_impl.h b/src/reader/spirv/parser_impl.h index b1e2110c45..11b0052b1a 100644 --- a/src/reader/spirv/parser_impl.h +++ b/src/reader/spirv/parser_impl.h @@ -36,6 +36,7 @@ #include "src/ast/struct_member_decoration.h" #include "src/ast/type/alias_type.h" #include "src/ast/type/array_type.h" +#include "src/ast/type/pointer_type.h" #include "src/ast/type/type.h" #include "src/reader/reader.h" #include "src/reader/spirv/entry_point_info.h" diff --git a/src/reader/spirv/parser_impl_convert_type_test.cc b/src/reader/spirv/parser_impl_convert_type_test.cc index 787e4e74cc..05425d5540 100644 --- a/src/reader/spirv/parser_impl_convert_type_test.cc +++ b/src/reader/spirv/parser_impl_convert_type_test.cc @@ -665,8 +665,8 @@ TEST_F(SpvParserTest, ConvertType_PointerInput) { EXPECT_TRUE(p->BuildInternalModule()); auto* type = p->ConvertType(3); - EXPECT_TRUE(type->IsPointer()); - auto* ptr_ty = type->AsPointer(); + EXPECT_TRUE(type->Is()); + auto* ptr_ty = type->As(); EXPECT_NE(ptr_ty, nullptr); EXPECT_TRUE(ptr_ty->type()->Is()); EXPECT_EQ(ptr_ty->storage_class(), ast::StorageClass::kInput); @@ -681,8 +681,8 @@ TEST_F(SpvParserTest, ConvertType_PointerOutput) { EXPECT_TRUE(p->BuildInternalModule()); auto* type = p->ConvertType(3); - EXPECT_TRUE(type->IsPointer()); - auto* ptr_ty = type->AsPointer(); + EXPECT_TRUE(type->Is()); + auto* ptr_ty = type->As(); EXPECT_NE(ptr_ty, nullptr); EXPECT_TRUE(ptr_ty->type()->Is()); EXPECT_EQ(ptr_ty->storage_class(), ast::StorageClass::kOutput); @@ -697,8 +697,8 @@ TEST_F(SpvParserTest, ConvertType_PointerUniform) { EXPECT_TRUE(p->BuildInternalModule()); auto* type = p->ConvertType(3); - EXPECT_TRUE(type->IsPointer()); - auto* ptr_ty = type->AsPointer(); + EXPECT_TRUE(type->Is()); + auto* ptr_ty = type->As(); EXPECT_NE(ptr_ty, nullptr); EXPECT_TRUE(ptr_ty->type()->Is()); EXPECT_EQ(ptr_ty->storage_class(), ast::StorageClass::kUniform); @@ -713,8 +713,8 @@ TEST_F(SpvParserTest, ConvertType_PointerWorkgroup) { EXPECT_TRUE(p->BuildInternalModule()); auto* type = p->ConvertType(3); - EXPECT_TRUE(type->IsPointer()); - auto* ptr_ty = type->AsPointer(); + EXPECT_TRUE(type->Is()); + auto* ptr_ty = type->As(); EXPECT_NE(ptr_ty, nullptr); EXPECT_TRUE(ptr_ty->type()->Is()); EXPECT_EQ(ptr_ty->storage_class(), ast::StorageClass::kWorkgroup); @@ -729,8 +729,8 @@ TEST_F(SpvParserTest, ConvertType_PointerUniformConstant) { EXPECT_TRUE(p->BuildInternalModule()); auto* type = p->ConvertType(3); - EXPECT_TRUE(type->IsPointer()); - auto* ptr_ty = type->AsPointer(); + EXPECT_TRUE(type->Is()); + auto* ptr_ty = type->As(); EXPECT_NE(ptr_ty, nullptr); EXPECT_TRUE(ptr_ty->type()->Is()); EXPECT_EQ(ptr_ty->storage_class(), ast::StorageClass::kUniformConstant); @@ -745,8 +745,8 @@ TEST_F(SpvParserTest, ConvertType_PointerStorageBuffer) { EXPECT_TRUE(p->BuildInternalModule()); auto* type = p->ConvertType(3); - EXPECT_TRUE(type->IsPointer()); - auto* ptr_ty = type->AsPointer(); + EXPECT_TRUE(type->Is()); + auto* ptr_ty = type->As(); EXPECT_NE(ptr_ty, nullptr); EXPECT_TRUE(ptr_ty->type()->Is()); EXPECT_EQ(ptr_ty->storage_class(), ast::StorageClass::kStorageBuffer); @@ -761,8 +761,8 @@ TEST_F(SpvParserTest, ConvertType_PointerImage) { EXPECT_TRUE(p->BuildInternalModule()); auto* type = p->ConvertType(3); - EXPECT_TRUE(type->IsPointer()); - auto* ptr_ty = type->AsPointer(); + EXPECT_TRUE(type->Is()); + auto* ptr_ty = type->As(); EXPECT_NE(ptr_ty, nullptr); EXPECT_TRUE(ptr_ty->type()->Is()); EXPECT_EQ(ptr_ty->storage_class(), ast::StorageClass::kImage); @@ -777,8 +777,8 @@ TEST_F(SpvParserTest, ConvertType_PointerPrivate) { EXPECT_TRUE(p->BuildInternalModule()); auto* type = p->ConvertType(3); - EXPECT_TRUE(type->IsPointer()); - auto* ptr_ty = type->AsPointer(); + EXPECT_TRUE(type->Is()); + auto* ptr_ty = type->As(); EXPECT_NE(ptr_ty, nullptr); EXPECT_TRUE(ptr_ty->type()->Is()); EXPECT_EQ(ptr_ty->storage_class(), ast::StorageClass::kPrivate); @@ -793,8 +793,8 @@ TEST_F(SpvParserTest, ConvertType_PointerFunction) { EXPECT_TRUE(p->BuildInternalModule()); auto* type = p->ConvertType(3); - EXPECT_TRUE(type->IsPointer()); - auto* ptr_ty = type->AsPointer(); + EXPECT_TRUE(type->Is()); + auto* ptr_ty = type->As(); EXPECT_NE(ptr_ty, nullptr); EXPECT_TRUE(ptr_ty->type()->Is()); EXPECT_EQ(ptr_ty->storage_class(), ast::StorageClass::kFunction); @@ -812,14 +812,14 @@ TEST_F(SpvParserTest, ConvertType_PointerToPointer) { auto* type = p->ConvertType(3); EXPECT_NE(type, nullptr); - EXPECT_TRUE(type->IsPointer()); + EXPECT_TRUE(type->Is()); - auto* ptr_ty = type->AsPointer(); + auto* ptr_ty = type->As(); EXPECT_NE(ptr_ty, nullptr); EXPECT_EQ(ptr_ty->storage_class(), ast::StorageClass::kInput); - EXPECT_TRUE(ptr_ty->type()->IsPointer()); + EXPECT_TRUE(ptr_ty->type()->Is()); - auto* ptr_ptr_ty = ptr_ty->type()->AsPointer(); + auto* ptr_ptr_ty = ptr_ty->type()->As(); EXPECT_NE(ptr_ptr_ty, nullptr); EXPECT_EQ(ptr_ptr_ty->storage_class(), ast::StorageClass::kOutput); EXPECT_TRUE(ptr_ptr_ty->type()->Is()); diff --git a/src/reader/wgsl/parser_impl_type_decl_test.cc b/src/reader/wgsl/parser_impl_type_decl_test.cc index b64fbb8bbe..5943dabf57 100644 --- a/src/reader/wgsl/parser_impl_type_decl_test.cc +++ b/src/reader/wgsl/parser_impl_type_decl_test.cc @@ -239,9 +239,9 @@ TEST_F(ParserImplTest, TypeDecl_Ptr) { EXPECT_FALSE(t.errored); ASSERT_NE(t.value, nullptr) << p->error(); ASSERT_FALSE(p->has_error()); - ASSERT_TRUE(t->IsPointer()); + ASSERT_TRUE(t->Is()); - auto* ptr = t->AsPointer(); + auto* ptr = t->As(); ASSERT_TRUE(ptr->type()->Is()); ASSERT_EQ(ptr->storage_class(), ast::StorageClass::kFunction); } @@ -253,9 +253,9 @@ TEST_F(ParserImplTest, TypeDecl_Ptr_ToVec) { EXPECT_FALSE(t.errored); ASSERT_NE(t.value, nullptr) << p->error(); ASSERT_FALSE(p->has_error()); - ASSERT_TRUE(t->IsPointer()); + ASSERT_TRUE(t->Is()); - auto* ptr = t->AsPointer(); + auto* ptr = t->As(); ASSERT_TRUE(ptr->type()->IsVector()); ASSERT_EQ(ptr->storage_class(), ast::StorageClass::kFunction); diff --git a/src/type_determiner.cc b/src/type_determiner.cc index e77d0829ce..fcfc44c265 100644 --- a/src/type_determiner.cc +++ b/src/type_determiner.cc @@ -348,9 +348,9 @@ bool TypeDeterminer::DetermineArrayAccessor( } // If we're extracting from a pointer, we return a pointer. - if (res->IsPointer()) { + if (res->Is()) { ret = mod_->create( - ret, res->AsPointer()->storage_class()); + ret, res->As()->storage_class()); } else if (parent_type->Is() && !parent_type->As()->type()->is_scalar()) { // If we extract a non-scalar from an array then we also get a pointer. We @@ -850,7 +850,7 @@ bool TypeDeterminer::DetermineIdentifier(ast::IdentifierExpression* expr) { // the pointer around the variable type. if (var->is_const()) { expr->set_result_type(var->type()); - } else if (var->type()->IsPointer()) { + } else if (var->type()->Is()) { expr->set_result_type(var->type()); } else { expr->set_result_type(mod_->create( @@ -1045,9 +1045,9 @@ bool TypeDeterminer::DetermineMemberAccessor( } // If we're extracting from a pointer, we return a pointer. - if (res->IsPointer()) { + if (res->Is()) { ret = mod_->create( - ret, res->AsPointer()->storage_class()); + ret, res->As()->storage_class()); } } else if (data_type->IsVector()) { auto* vec = data_type->AsVector(); @@ -1057,9 +1057,9 @@ bool TypeDeterminer::DetermineMemberAccessor( // A single element swizzle is just the type of the vector. ret = vec->type(); // If we're extracting from a pointer, we return a pointer. - if (res->IsPointer()) { + if (res->Is()) { ret = mod_->create( - ret, res->AsPointer()->storage_class()); + ret, res->As()->storage_class()); } } else { // The vector will have a number of components equal to the length of the diff --git a/src/type_determiner_test.cc b/src/type_determiner_test.cc index 4a928b959a..3f3715e77c 100644 --- a/src/type_determiner_test.cc +++ b/src/type_determiner_test.cc @@ -438,9 +438,9 @@ TEST_F(TypeDeterminerTest, Expr_ArrayAccessor_Array) { idx); EXPECT_TRUE(td()->DetermineResultType(&acc)); ASSERT_NE(acc.result_type(), nullptr); - ASSERT_TRUE(acc.result_type()->IsPointer()); + ASSERT_TRUE(acc.result_type()->Is()); - auto* ptr = acc.result_type()->AsPointer(); + auto* ptr = acc.result_type()->As(); EXPECT_TRUE(ptr->type()->Is()); } @@ -463,9 +463,9 @@ TEST_F(TypeDeterminerTest, Expr_ArrayAccessor_Alias_Array) { idx); EXPECT_TRUE(td()->DetermineResultType(&acc)); ASSERT_NE(acc.result_type(), nullptr); - ASSERT_TRUE(acc.result_type()->IsPointer()); + ASSERT_TRUE(acc.result_type()->Is()); - auto* ptr = acc.result_type()->AsPointer(); + auto* ptr = acc.result_type()->As(); EXPECT_TRUE(ptr->type()->Is()); } @@ -509,9 +509,9 @@ TEST_F(TypeDeterminerTest, Expr_ArrayAccessor_Matrix) { idx); EXPECT_TRUE(td()->DetermineResultType(&acc)); ASSERT_NE(acc.result_type(), nullptr); - ASSERT_TRUE(acc.result_type()->IsPointer()); + ASSERT_TRUE(acc.result_type()->Is()); - auto* ptr = acc.result_type()->AsPointer(); + auto* ptr = acc.result_type()->As(); ASSERT_TRUE(ptr->type()->IsVector()); EXPECT_EQ(ptr->type()->AsVector()->size(), 3u); } @@ -538,9 +538,9 @@ TEST_F(TypeDeterminerTest, Expr_ArrayAccessor_Matrix_BothDimensions) { EXPECT_TRUE(td()->DetermineResultType(&acc)); ASSERT_NE(acc.result_type(), nullptr); - ASSERT_TRUE(acc.result_type()->IsPointer()); + ASSERT_TRUE(acc.result_type()->Is()); - auto* ptr = acc.result_type()->AsPointer(); + auto* ptr = acc.result_type()->As(); EXPECT_TRUE(ptr->type()->Is()); } @@ -561,9 +561,9 @@ TEST_F(TypeDeterminerTest, Expr_ArrayAccessor_Vector) { idx); EXPECT_TRUE(td()->DetermineResultType(&acc)); ASSERT_NE(acc.result_type(), nullptr); - ASSERT_TRUE(acc.result_type()->IsPointer()); + ASSERT_TRUE(acc.result_type()->Is()); - auto* ptr = acc.result_type()->AsPointer(); + auto* ptr = acc.result_type()->As(); EXPECT_TRUE(ptr->type()->Is()); } @@ -697,9 +697,11 @@ TEST_F(TypeDeterminerTest, Expr_Identifier_GlobalVariable) { ast::IdentifierExpression ident("my_var"); EXPECT_TRUE(td()->DetermineResultType(&ident)); ASSERT_NE(ident.result_type(), nullptr); - EXPECT_TRUE(ident.result_type()->IsPointer()); - EXPECT_TRUE( - ident.result_type()->AsPointer()->type()->Is()); + EXPECT_TRUE(ident.result_type()->Is()); + EXPECT_TRUE(ident.result_type() + ->As() + ->type() + ->Is()); } TEST_F(TypeDeterminerTest, Expr_Identifier_GlobalConstant) { @@ -755,9 +757,11 @@ TEST_F(TypeDeterminerTest, Expr_Identifier_FunctionVariable) { EXPECT_TRUE(td()->DetermineFunction(&f)); ASSERT_NE(my_var->result_type(), nullptr); - EXPECT_TRUE(my_var->result_type()->IsPointer()); - EXPECT_TRUE( - my_var->result_type()->AsPointer()->type()->Is()); + EXPECT_TRUE(my_var->result_type()->Is()); + EXPECT_TRUE(my_var->result_type() + ->As() + ->type() + ->Is()); } TEST_F(TypeDeterminerTest, Expr_Identifier_Function_Ptr) { @@ -778,9 +782,11 @@ TEST_F(TypeDeterminerTest, Expr_Identifier_Function_Ptr) { EXPECT_TRUE(td()->DetermineFunction(&f)); ASSERT_NE(my_var->result_type(), nullptr); - EXPECT_TRUE(my_var->result_type()->IsPointer()); - EXPECT_TRUE( - my_var->result_type()->AsPointer()->type()->Is()); + EXPECT_TRUE(my_var->result_type()->Is()); + EXPECT_TRUE(my_var->result_type() + ->As() + ->type() + ->Is()); } TEST_F(TypeDeterminerTest, Expr_Identifier_Function) { @@ -967,9 +973,9 @@ TEST_F(TypeDeterminerTest, Expr_MemberAccessor_Struct) { ast::MemberAccessorExpression mem(ident, mem_ident); EXPECT_TRUE(td()->DetermineResultType(&mem)); ASSERT_NE(mem.result_type(), nullptr); - ASSERT_TRUE(mem.result_type()->IsPointer()); + ASSERT_TRUE(mem.result_type()->Is()); - auto* ptr = mem.result_type()->AsPointer(); + auto* ptr = mem.result_type()->As(); EXPECT_TRUE(ptr->type()->Is()); } @@ -1001,9 +1007,9 @@ TEST_F(TypeDeterminerTest, Expr_MemberAccessor_Struct_Alias) { ast::MemberAccessorExpression mem(ident, mem_ident); EXPECT_TRUE(td()->DetermineResultType(&mem)); ASSERT_NE(mem.result_type(), nullptr); - ASSERT_TRUE(mem.result_type()->IsPointer()); + ASSERT_TRUE(mem.result_type()->Is()); - auto* ptr = mem.result_type()->AsPointer(); + auto* ptr = mem.result_type()->As(); EXPECT_TRUE(ptr->type()->Is()); } @@ -1044,9 +1050,9 @@ TEST_F(TypeDeterminerTest, Expr_MemberAccessor_VectorSwizzle_SingleElement) { ast::MemberAccessorExpression mem(ident, swizzle); EXPECT_TRUE(td()->DetermineResultType(&mem)) << td()->error(); ASSERT_NE(mem.result_type(), nullptr); - ASSERT_TRUE(mem.result_type()->IsPointer()); + ASSERT_TRUE(mem.result_type()->Is()); - auto* ptr = mem.result_type()->AsPointer(); + auto* ptr = mem.result_type()->As(); ASSERT_TRUE(ptr->type()->Is()); } diff --git a/src/writer/hlsl/generator_impl.cc b/src/writer/hlsl/generator_impl.cc index 782f471abc..a697caa226 100644 --- a/src/writer/hlsl/generator_impl.cc +++ b/src/writer/hlsl/generator_impl.cc @@ -45,6 +45,7 @@ #include "src/ast/type/f32_type.h" #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" #include "src/ast/type/struct_type.h" #include "src/ast/type/texture_type.h" @@ -2073,7 +2074,7 @@ bool GeneratorImpl::EmitType(std::ostream& out, return false; } out << mat->rows() << "x" << mat->columns(); - } else if (type->IsPointer()) { + } else if (type->Is()) { // TODO(dsinclair): What do we do with pointers in HLSL? // https://bugs.chromium.org/p/tint/issues/detail?id=183 error_ = "pointers not supported in HLSL"; diff --git a/src/writer/msl/generator_impl.cc b/src/writer/msl/generator_impl.cc index 3404522700..762cd9ab7b 100644 --- a/src/writer/msl/generator_impl.cc +++ b/src/writer/msl/generator_impl.cc @@ -197,7 +197,7 @@ uint32_t GeneratorImpl::calculate_alignment_size(ast::type::Type* type) { if (type->Is()) { return 1; } - if (type->IsPointer()) { + if (type->Is()) { return 0; } if (type->Is() || type->Is() || @@ -1825,8 +1825,8 @@ bool GeneratorImpl::EmitType(ast::type::Type* type, const std::string& name) { return false; } out_ << mat->columns() << "x" << mat->rows(); - } else if (type->IsPointer()) { - auto* ptr = type->AsPointer(); + } else if (type->Is()) { + auto* ptr = type->As(); // TODO(dsinclair): Storage class? if (!EmitType(ptr->type(), "")) { return false; diff --git a/src/writer/spirv/builder.cc b/src/writer/spirv/builder.cc index 4327476627..0906c6f2fc 100644 --- a/src/writer/spirv/builder.cc +++ b/src/writer/spirv/builder.cc @@ -819,7 +819,7 @@ bool Builder::GenerateArrayAccessor(ast::ArrayAccessorExpression* expr, // If the source is a pointer we access chain into it. We also access chain // into an array of non-scalar types. - if (info->source_type->IsPointer() || + if (info->source_type->Is() || (info->source_type->Is() && !info->source_type->As()->type()->is_scalar())) { info->access_chain_indices.push_back(idx_id); @@ -854,7 +854,7 @@ bool Builder::GenerateMemberAccessor(ast::MemberAccessorExpression* expr, // If the data_type is a structure we're accessing a member, if it's a // vector we're accessing a swizzle. if (data_type->IsStruct()) { - if (!info->source_type->IsPointer()) { + if (!info->source_type->Is()) { error_ = "Attempting to access a struct member on a non-pointer. Something is " "wrong"; @@ -895,7 +895,7 @@ bool Builder::GenerateMemberAccessor(ast::MemberAccessorExpression* expr, return false; } - if (info->source_type->IsPointer()) { + if (info->source_type->Is()) { auto idx_id = GenerateU32Literal(val); if (idx_id == 0) { return 0; @@ -1009,7 +1009,7 @@ uint32_t Builder::GenerateAccessorExpression(ast::Expression* expr) { auto* ary_res_type = accessors[0]->AsArrayAccessor()->array()->result_type(); - if (!ary_res_type->IsPointer() && + if (!ary_res_type->Is() && (ary_res_type->Is() && !ary_res_type->As()->type()->is_scalar())) { ast::type::PointerType ptr(ary_res_type, ast::StorageClass::kFunction); @@ -1087,7 +1087,7 @@ uint32_t Builder::GenerateIdentifierExpression( } uint32_t Builder::GenerateLoadIfNeeded(ast::type::Type* type, uint32_t id) { - if (!type->IsPointer()) { + if (!type->Is()) { return id; } @@ -1808,7 +1808,7 @@ uint32_t Builder::GenerateIntrinsic(ast::IdentifierExpression* ident, error_ = "missing param for runtime array length"; return 0; } else if (!call->params()[0]->IsMemberAccessor()) { - if (call->params()[0]->result_type()->IsPointer()) { + if (call->params()[0]->result_type()->Is()) { error_ = "pointer accessors not supported yet"; } else { error_ = "invalid accessor for runtime array length"; @@ -2431,8 +2431,8 @@ uint32_t Builder::GenerateTypeIfNeeded(ast::type::Type* type) { if (!GenerateMatrixType(type->As(), result)) { return 0; } - } else if (type->IsPointer()) { - if (!GeneratePointerType(type->AsPointer(), result)) { + } else if (type->Is()) { + if (!GeneratePointerType(type->As(), result)) { return 0; } } else if (type->IsStruct()) { diff --git a/src/writer/spirv/builder.h b/src/writer/spirv/builder.h index bda42b6e2a..a9d9962593 100644 --- a/src/writer/spirv/builder.h +++ b/src/writer/spirv/builder.h @@ -28,6 +28,7 @@ #include "src/ast/module.h" #include "src/ast/struct_member.h" #include "src/ast/type/access_control_type.h" +#include "src/ast/type/pointer_type.h" #include "src/ast/type/array_type.h" #include "src/ast/type/matrix_type.h" #include "src/ast/type/storage_texture_type.h" diff --git a/src/writer/wgsl/generator_impl.cc b/src/writer/wgsl/generator_impl.cc index 1ecfec399b..e9ed0e8bad 100644 --- a/src/writer/wgsl/generator_impl.cc +++ b/src/writer/wgsl/generator_impl.cc @@ -446,8 +446,8 @@ bool GeneratorImpl::EmitType(ast::type::Type* type) { return false; } out_ << ">"; - } else if (type->IsPointer()) { - auto* ptr = type->AsPointer(); + } else if (type->Is()) { + auto* ptr = type->As(); out_ << "ptr<" << ptr->storage_class() << ", "; if (!EmitType(ptr->type())) { return false;