Replace Type::(Is|As)Struct with Castable

Change-Id: I0bc5645d65d7b53058bda7b343a991a9614741be
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/34271
Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
Ben Clayton
2020-11-30 23:30:58 +00:00
parent 351128a41e
commit d734dd0c10
35 changed files with 116 additions and 123 deletions

View File

@@ -61,12 +61,12 @@ bool Module::IsValid() const {
if (alias->type() == nullptr) {
return false;
}
if (alias->type()->IsStruct() &&
alias->type()->AsStruct()->name().empty()) {
if (alias->type()->Is<ast::type::StructType>() &&
alias->type()->As<ast::type::StructType>()->name().empty()) {
return false;
}
} else if (ty->IsStruct()) {
auto* str = ty->AsStruct();
} else if (ty->Is<ast::type::StructType>()) {
auto* str = ty->As<ast::type::StructType>();
if (str->name().empty()) {
return false;
}
@@ -94,11 +94,11 @@ std::string Module::to_str() const {
if (ty->Is<ast::type::AliasType>()) {
auto* alias = ty->As<ast::type::AliasType>();
out << alias->name() << " -> " << alias->type()->type_name() << std::endl;
if (alias->type()->IsStruct()) {
alias->type()->AsStruct()->impl()->to_str(out, indent);
if (alias->type()->Is<ast::type::StructType>()) {
alias->type()->As<ast::type::StructType>()->impl()->to_str(out, indent);
}
} else if (ty->IsStruct()) {
auto* str = ty->AsStruct();
} else if (ty->Is<ast::type::StructType>()) {
auto* str = ty->As<ast::type::StructType>();
out << str->name() << " ";
str->impl()->to_str(out, indent);
}

View File

@@ -60,7 +60,7 @@ TEST_F(AccessControlTypeTest, Is) {
EXPECT_FALSE(ty->Is<MatrixType>());
EXPECT_FALSE(ty->Is<PointerType>());
EXPECT_FALSE(ty->Is<SamplerType>());
EXPECT_FALSE(ty->IsStruct());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_FALSE(ty->IsTexture());
EXPECT_FALSE(ty->IsU32());
EXPECT_FALSE(ty->IsVector());

View File

@@ -61,7 +61,7 @@ TEST_F(AliasTypeTest, Is) {
EXPECT_FALSE(ty->Is<MatrixType>());
EXPECT_FALSE(ty->Is<PointerType>());
EXPECT_FALSE(ty->Is<SamplerType>());
EXPECT_FALSE(ty->IsStruct());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_FALSE(ty->IsTexture());
EXPECT_FALSE(ty->IsU32());
EXPECT_FALSE(ty->IsVector());

View File

@@ -25,6 +25,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/struct_type.h"
#include "src/ast/type/u32_type.h"
namespace tint {
@@ -66,7 +67,7 @@ TEST_F(ArrayTypeTest, Is) {
EXPECT_FALSE(ty->Is<MatrixType>());
EXPECT_FALSE(ty->Is<PointerType>());
EXPECT_FALSE(ty->Is<SamplerType>());
EXPECT_FALSE(ty->IsStruct());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_FALSE(ty->IsTexture());
EXPECT_FALSE(ty->IsU32());
EXPECT_FALSE(ty->IsVector());

View File

@@ -21,6 +21,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/struct_type.h"
namespace tint {
namespace ast {
@@ -41,7 +42,7 @@ TEST_F(BoolTypeTest, Is) {
EXPECT_FALSE(ty->Is<MatrixType>());
EXPECT_FALSE(ty->Is<PointerType>());
EXPECT_FALSE(ty->Is<SamplerType>());
EXPECT_FALSE(ty->IsStruct());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_FALSE(ty->IsTexture());
EXPECT_FALSE(ty->IsU32());
EXPECT_FALSE(ty->IsVector());

View File

@@ -23,6 +23,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/struct_type.h"
namespace tint {
namespace ast {
@@ -43,7 +44,7 @@ TEST_F(DepthTextureTypeTest, Is) {
EXPECT_FALSE(ty->Is<MatrixType>());
EXPECT_FALSE(ty->Is<PointerType>());
EXPECT_FALSE(ty->Is<SamplerType>());
EXPECT_FALSE(ty->IsStruct());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_TRUE(ty->IsTexture());
EXPECT_FALSE(ty->IsU32());
EXPECT_FALSE(ty->IsVector());

View File

@@ -21,6 +21,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/struct_type.h"
namespace tint {
namespace ast {
@@ -41,7 +42,7 @@ TEST_F(F32TypeTest, Is) {
EXPECT_FALSE(ty->Is<MatrixType>());
EXPECT_FALSE(ty->Is<PointerType>());
EXPECT_FALSE(ty->Is<SamplerType>());
EXPECT_FALSE(ty->IsStruct());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_FALSE(ty->IsTexture());
EXPECT_FALSE(ty->IsU32());
EXPECT_FALSE(ty->IsVector());

View File

@@ -21,6 +21,7 @@
#include "src/ast/type/f32_type.h"
#include "src/ast/type/matrix_type.h"
#include "src/ast/type/pointer_type.h"
#include "src/ast/type/struct_type.h"
namespace tint {
namespace ast {
@@ -41,7 +42,7 @@ TEST_F(I32TypeTest, Is) {
EXPECT_FALSE(ty->Is<MatrixType>());
EXPECT_FALSE(ty->Is<PointerType>());
EXPECT_FALSE(ty->Is<SamplerType>());
EXPECT_FALSE(ty->IsStruct());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_FALSE(ty->IsTexture());
EXPECT_FALSE(ty->IsU32());
EXPECT_FALSE(ty->IsVector());

View File

@@ -21,6 +21,7 @@
#include "src/ast/type/f32_type.h"
#include "src/ast/type/i32_type.h"
#include "src/ast/type/pointer_type.h"
#include "src/ast/type/struct_type.h"
namespace tint {
namespace ast {
@@ -50,7 +51,7 @@ TEST_F(MatrixTypeTest, Is) {
EXPECT_TRUE(ty->Is<MatrixType>());
EXPECT_FALSE(ty->Is<PointerType>());
EXPECT_FALSE(ty->Is<SamplerType>());
EXPECT_FALSE(ty->IsStruct());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_FALSE(ty->IsTexture());
EXPECT_FALSE(ty->IsU32());
EXPECT_FALSE(ty->IsVector());

View File

@@ -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/struct_type.h"
namespace tint {
namespace ast {
@@ -43,7 +44,7 @@ TEST_F(MultisampledTextureTypeTest, Is) {
EXPECT_FALSE(ty->Is<MatrixType>());
EXPECT_FALSE(ty->Is<PointerType>());
EXPECT_FALSE(ty->Is<SamplerType>());
EXPECT_FALSE(ty->IsStruct());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_TRUE(ty->IsTexture());
EXPECT_FALSE(ty->IsU32());
EXPECT_FALSE(ty->IsVector());

View File

@@ -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/struct_type.h"
namespace tint {
namespace ast {
@@ -49,7 +50,7 @@ TEST_F(PointerTypeTest, Is) {
EXPECT_FALSE(ty->Is<MatrixType>());
EXPECT_TRUE(ty->Is<PointerType>());
EXPECT_FALSE(ty->Is<SamplerType>());
EXPECT_FALSE(ty->IsStruct());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_FALSE(ty->IsTexture());
EXPECT_FALSE(ty->IsU32());
EXPECT_FALSE(ty->IsVector());

View File

@@ -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/struct_type.h"
namespace tint {
namespace ast {
@@ -43,7 +44,7 @@ TEST_F(SampledTextureTypeTest, Is) {
EXPECT_FALSE(ty->Is<MatrixType>());
EXPECT_FALSE(ty->Is<PointerType>());
EXPECT_FALSE(ty->Is<SamplerType>());
EXPECT_FALSE(ty->IsStruct());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_TRUE(ty->IsTexture());
EXPECT_FALSE(ty->IsU32());
EXPECT_FALSE(ty->IsVector());

View File

@@ -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/struct_type.h"
namespace tint {
namespace ast {
@@ -53,7 +54,7 @@ TEST_F(SamplerTypeTest, Is) {
EXPECT_FALSE(ty->Is<MatrixType>());
EXPECT_FALSE(ty->Is<PointerType>());
EXPECT_TRUE(ty->Is<SamplerType>());
EXPECT_FALSE(ty->IsStruct());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_FALSE(ty->IsTexture());
EXPECT_FALSE(ty->IsU32());
EXPECT_FALSE(ty->IsVector());

View File

@@ -25,6 +25,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/struct_type.h"
#include "src/type_determiner.h"
namespace tint {
@@ -47,7 +48,7 @@ TEST_F(StorageTextureTypeTest, Is) {
EXPECT_FALSE(ty->Is<MatrixType>());
EXPECT_FALSE(ty->Is<PointerType>());
EXPECT_FALSE(ty->Is<SamplerType>());
EXPECT_FALSE(ty->IsStruct());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_TRUE(ty->IsTexture());
EXPECT_FALSE(ty->IsU32());
EXPECT_FALSE(ty->IsVector());

View File

@@ -33,10 +33,6 @@ StructType::StructType(StructType&&) = default;
StructType::~StructType() = default;
bool StructType::IsStruct() const {
return true;
}
std::string StructType::type_name() const {
return "__struct_" + name_;
}

View File

@@ -42,9 +42,6 @@ class StructType : public Castable<StructType, Type> {
/// @returns true if the struct has a block decoration
bool IsBlockDecorated() const { return struct_->IsBlockDecorated(); }
/// @returns true if the type is a struct type
bool IsStruct() const override;
/// @returns the struct name
Struct* impl() const { return struct_; }

View File

@@ -58,7 +58,7 @@ TEST_F(StructTypeTest, Is) {
EXPECT_FALSE(ty->Is<MatrixType>());
EXPECT_FALSE(ty->Is<PointerType>());
EXPECT_FALSE(ty->Is<SamplerType>());
EXPECT_TRUE(ty->IsStruct());
EXPECT_TRUE(ty->Is<StructType>());
EXPECT_FALSE(ty->IsTexture());
EXPECT_FALSE(ty->IsU32());
EXPECT_FALSE(ty->IsVector());

View File

@@ -66,10 +66,6 @@ Type* Type::UnwrapAll() {
return UnwrapIfNeeded()->UnwrapPtrIfNeeded()->UnwrapIfNeeded();
}
bool Type::IsStruct() const {
return false;
}
bool Type::IsTexture() const {
return false;
}
@@ -138,11 +134,6 @@ bool Type::is_integer_scalar_or_vector() {
return is_unsigned_scalar_or_vector() || is_signed_scalar_or_vector();
}
const StructType* Type::AsStruct() const {
assert(IsStruct());
return static_cast<const StructType*>(this);
}
const TextureType* Type::AsTexture() const {
assert(IsTexture());
return static_cast<const TextureType*>(this);
@@ -163,11 +154,6 @@ const VoidType* Type::AsVoid() const {
return static_cast<const VoidType*>(this);
}
StructType* Type::AsStruct() {
assert(IsStruct());
return static_cast<StructType*>(this);
}
TextureType* Type::AsTexture() {
assert(IsTexture());
return static_cast<TextureType*>(this);

View File

@@ -23,7 +23,6 @@ namespace tint {
namespace ast {
namespace type {
class StructType;
class TextureType;
class U32Type;
class VectorType;
@@ -39,8 +38,6 @@ class Type : public Castable<Type> {
Type(Type&&);
~Type() override;
/// @returns true if the type is a struct type
virtual bool IsStruct() const;
/// @returns true if the type is a texture type
virtual bool IsTexture() const;
/// @returns true if the type is a u32 type
@@ -104,8 +101,6 @@ class Type : public Castable<Type> {
/// @returns true if this type is an integer scalar or vector
bool is_integer_scalar_or_vector();
/// @returns the type as a struct type
const StructType* AsStruct() const;
/// @returns the type as a texture type
const TextureType* AsTexture() const;
/// @returns the type as a u32 type
@@ -115,8 +110,6 @@ class Type : public Castable<Type> {
/// @returns the type as a void type
const VoidType* AsVoid() const;
/// @returns the type as a struct type
StructType* AsStruct();
/// @returns the type as a texture type
TextureType* AsTexture();
/// @returns the type as a u32 type

View File

@@ -23,6 +23,7 @@
#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"
namespace tint {
namespace ast {
@@ -43,7 +44,7 @@ TEST_F(U32TypeTest, Is) {
EXPECT_FALSE(ty->Is<MatrixType>());
EXPECT_FALSE(ty->Is<PointerType>());
EXPECT_FALSE(ty->Is<SamplerType>());
EXPECT_FALSE(ty->IsStruct());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_FALSE(ty->IsTexture());
EXPECT_TRUE(ty->IsU32());
EXPECT_FALSE(ty->IsVector());

View File

@@ -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/struct_type.h"
namespace tint {
namespace ast {
@@ -50,7 +51,7 @@ TEST_F(VectorTypeTest, Is) {
EXPECT_FALSE(ty->Is<MatrixType>());
EXPECT_FALSE(ty->Is<PointerType>());
EXPECT_FALSE(ty->Is<SamplerType>());
EXPECT_FALSE(ty->IsStruct());
EXPECT_FALSE(ty->Is<StructType>());
EXPECT_FALSE(ty->IsTexture());
EXPECT_FALSE(ty->IsU32());
EXPECT_TRUE(ty->IsVector());