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

Change-Id: I10f1d3d0757661a41b44517d55d6f159aced04ce
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/34263
Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
Ben Clayton 2020-11-30 23:30:58 +00:00
parent a8d87788da
commit 67864267c2
29 changed files with 62 additions and 89 deletions

View File

@ -56,8 +56,8 @@ bool Module::IsValid() const {
if (ty == nullptr) {
return false;
}
if (ty->IsAlias()) {
auto* alias = ty->AsAlias();
if (ty->Is<ast::type::AliasType>()) {
auto* alias = ty->As<ast::type::AliasType>();
if (alias->type() == nullptr) {
return false;
}
@ -91,8 +91,8 @@ std::string Module::to_str() const {
for (size_t i = 0; i < indent; ++i) {
out << " ";
}
if (ty->IsAlias()) {
auto* alias = ty->AsAlias();
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);

View File

@ -47,19 +47,20 @@ TEST_F(AccessControlTypeTest, Is) {
I32Type i32;
AccessControlType at{AccessControl::kReadOnly, &i32};
EXPECT_TRUE(at.Is<AccessControlType>());
EXPECT_FALSE(at.IsAlias());
EXPECT_FALSE(at.IsArray());
EXPECT_FALSE(at.IsBool());
EXPECT_FALSE(at.IsF32());
EXPECT_FALSE(at.IsI32());
EXPECT_FALSE(at.IsMatrix());
EXPECT_FALSE(at.IsPointer());
EXPECT_FALSE(at.IsSampler());
EXPECT_FALSE(at.IsStruct());
EXPECT_FALSE(at.IsTexture());
EXPECT_FALSE(at.IsU32());
EXPECT_FALSE(at.IsVector());
Type* ty = &at;
EXPECT_TRUE(ty->Is<AccessControlType>());
EXPECT_FALSE(ty->Is<AliasType>());
EXPECT_FALSE(ty->IsArray());
EXPECT_FALSE(ty->IsBool());
EXPECT_FALSE(ty->IsF32());
EXPECT_FALSE(ty->IsI32());
EXPECT_FALSE(ty->IsMatrix());
EXPECT_FALSE(ty->IsPointer());
EXPECT_FALSE(ty->IsSampler());
EXPECT_FALSE(ty->IsStruct());
EXPECT_FALSE(ty->IsTexture());
EXPECT_FALSE(ty->IsU32());
EXPECT_FALSE(ty->IsVector());
}
TEST_F(AccessControlTypeTest, AccessRead) {

View File

@ -29,10 +29,6 @@ AliasType::AliasType(AliasType&&) = default;
AliasType::~AliasType() = default;
bool AliasType::IsAlias() const {
return true;
}
std::string AliasType::type_name() const {
return "__alias_" + name_ + subtype_->type_name();
}

View File

@ -34,9 +34,6 @@ class AliasType : public Castable<AliasType, Type> {
AliasType(AliasType&&);
~AliasType() override;
/// @returns true if the type is an alias type
bool IsAlias() const override;
/// @returns the alias name
const std::string& name() const { return name_; }
/// @returns the alias type

View File

@ -50,7 +50,7 @@ TEST_F(AliasTypeTest, Is) {
AliasType at{"a", &i32};
Type* ty = &at;
EXPECT_FALSE(ty->Is<AccessControlType>());
EXPECT_TRUE(ty->IsAlias());
EXPECT_TRUE(ty->Is<AliasType>());
EXPECT_FALSE(ty->IsArray());
EXPECT_FALSE(ty->IsBool());
EXPECT_FALSE(ty->IsF32());

View File

@ -54,7 +54,7 @@ TEST_F(ArrayTypeTest, Is) {
ArrayType arr{&i32, 3};
Type* ty = &arr;
EXPECT_FALSE(ty->Is<AccessControlType>());
EXPECT_FALSE(ty->IsAlias());
EXPECT_FALSE(ty->Is<AliasType>());
EXPECT_TRUE(ty->IsArray());
EXPECT_FALSE(ty->IsBool());
EXPECT_FALSE(ty->IsF32());

View File

@ -28,7 +28,7 @@ TEST_F(BoolTypeTest, Is) {
BoolType b;
Type* ty = &b;
EXPECT_FALSE(ty->Is<AccessControlType>());
EXPECT_FALSE(ty->IsAlias());
EXPECT_FALSE(ty->Is<AliasType>());
EXPECT_FALSE(ty->IsArray());
EXPECT_TRUE(ty->IsBool());
EXPECT_FALSE(ty->IsF32());

View File

@ -29,7 +29,7 @@ TEST_F(DepthTextureTypeTest, Is) {
DepthTextureType d(TextureDimension::kCube);
Type* ty = &d;
EXPECT_FALSE(ty->Is<AccessControlType>());
EXPECT_FALSE(ty->IsAlias());
EXPECT_FALSE(ty->Is<AliasType>());
EXPECT_FALSE(ty->IsArray());
EXPECT_FALSE(ty->IsBool());
EXPECT_FALSE(ty->IsF32());

View File

@ -28,7 +28,7 @@ TEST_F(F32TypeTest, Is) {
F32Type f;
Type* ty = &f;
EXPECT_FALSE(ty->Is<AccessControlType>());
EXPECT_FALSE(ty->IsAlias());
EXPECT_FALSE(ty->Is<AliasType>());
EXPECT_FALSE(ty->IsArray());
EXPECT_FALSE(ty->IsBool());
EXPECT_TRUE(ty->IsF32());

View File

@ -28,7 +28,7 @@ TEST_F(I32TypeTest, Is) {
I32Type i;
Type* ty = &i;
EXPECT_FALSE(ty->Is<AccessControlType>());
EXPECT_FALSE(ty->IsAlias());
EXPECT_FALSE(ty->Is<AliasType>());
EXPECT_FALSE(ty->IsArray());
EXPECT_FALSE(ty->IsBool());
EXPECT_FALSE(ty->IsF32());

View File

@ -38,7 +38,7 @@ TEST_F(MatrixTypeTest, Is) {
MatrixType m{&i32, 2, 3};
Type* ty = &m;
EXPECT_FALSE(ty->Is<AccessControlType>());
EXPECT_FALSE(ty->IsAlias());
EXPECT_FALSE(ty->Is<AliasType>());
EXPECT_FALSE(ty->IsArray());
EXPECT_FALSE(ty->IsBool());
EXPECT_FALSE(ty->IsF32());

View File

@ -30,7 +30,7 @@ TEST_F(MultisampledTextureTypeTest, Is) {
MultisampledTextureType s(TextureDimension::kCube, &f32);
Type* ty = &s;
EXPECT_FALSE(ty->Is<AccessControlType>());
EXPECT_FALSE(ty->IsAlias());
EXPECT_FALSE(ty->Is<AliasType>());
EXPECT_FALSE(ty->IsArray());
EXPECT_FALSE(ty->IsBool());
EXPECT_FALSE(ty->IsF32());

View File

@ -37,7 +37,7 @@ TEST_F(PointerTypeTest, Is) {
PointerType p{&i32, StorageClass::kFunction};
Type* ty = &p;
EXPECT_FALSE(ty->Is<AccessControlType>());
EXPECT_FALSE(ty->IsAlias());
EXPECT_FALSE(ty->Is<AliasType>());
EXPECT_FALSE(ty->IsArray());
EXPECT_FALSE(ty->IsBool());
EXPECT_FALSE(ty->IsF32());

View File

@ -30,7 +30,7 @@ TEST_F(SampledTextureTypeTest, Is) {
SampledTextureType s(TextureDimension::kCube, &f32);
Type* ty = &s;
EXPECT_FALSE(ty->Is<AccessControlType>());
EXPECT_FALSE(ty->IsAlias());
EXPECT_FALSE(ty->Is<AliasType>());
EXPECT_FALSE(ty->IsArray());
EXPECT_FALSE(ty->IsBool());
EXPECT_FALSE(ty->IsF32());

View File

@ -39,7 +39,7 @@ TEST_F(SamplerTypeTest, Is) {
SamplerType s{SamplerKind::kSampler};
Type* ty = &s;
EXPECT_FALSE(ty->Is<AccessControlType>());
EXPECT_FALSE(ty->IsAlias());
EXPECT_FALSE(ty->Is<AliasType>());
EXPECT_FALSE(ty->IsArray());
EXPECT_FALSE(ty->IsBool());
EXPECT_FALSE(ty->IsF32());

View File

@ -33,7 +33,7 @@ TEST_F(StorageTextureTypeTest, Is) {
ImageFormat::kRgba32Float);
Type* ty = &s;
EXPECT_FALSE(ty->Is<AccessControlType>());
EXPECT_FALSE(ty->IsAlias());
EXPECT_FALSE(ty->Is<AliasType>());
EXPECT_FALSE(ty->IsArray());
EXPECT_FALSE(ty->IsBool());
EXPECT_FALSE(ty->IsF32());

View File

@ -46,7 +46,7 @@ TEST_F(StructTypeTest, Is) {
StructType s{"S", impl};
Type* ty = &s;
EXPECT_FALSE(ty->Is<AccessControlType>());
EXPECT_FALSE(ty->IsAlias());
EXPECT_FALSE(ty->Is<AliasType>());
EXPECT_FALSE(ty->IsArray());
EXPECT_FALSE(ty->IsBool());
EXPECT_FALSE(ty->IsF32());

View File

@ -51,8 +51,8 @@ Type* Type::UnwrapPtrIfNeeded() {
Type* Type::UnwrapIfNeeded() {
auto* where = this;
while (true) {
if (where->IsAlias()) {
where = where->AsAlias()->type();
if (where->Is<AliasType>()) {
where = where->As<AliasType>()->type();
} else if (where->Is<AccessControlType>()) {
where = where->As<AccessControlType>()->type();
} else {
@ -66,10 +66,6 @@ Type* Type::UnwrapAll() {
return UnwrapIfNeeded()->UnwrapPtrIfNeeded()->UnwrapIfNeeded();
}
bool Type::IsAlias() const {
return false;
}
bool Type::IsArray() const {
return false;
}
@ -170,11 +166,6 @@ bool Type::is_integer_scalar_or_vector() {
return is_unsigned_scalar_or_vector() || is_signed_scalar_or_vector();
}
const AliasType* Type::AsAlias() const {
assert(IsAlias());
return static_cast<const AliasType*>(this);
}
const ArrayType* Type::AsArray() const {
assert(IsArray());
return static_cast<const ArrayType*>(this);
@ -235,11 +226,6 @@ const VoidType* Type::AsVoid() const {
return static_cast<const VoidType*>(this);
}
AliasType* Type::AsAlias() {
assert(IsAlias());
return static_cast<AliasType*>(this);
}
ArrayType* Type::AsArray() {
assert(IsArray());
return static_cast<ArrayType*>(this);

View File

@ -23,7 +23,6 @@ namespace tint {
namespace ast {
namespace type {
class AliasType;
class ArrayType;
class BoolType;
class F32Type;
@ -47,8 +46,6 @@ class Type : public Castable<Type> {
Type(Type&&);
~Type() override;
/// @returns true if the type is an alias type
virtual bool IsAlias() const;
/// @returns true if the type is an array type
virtual bool IsArray() const;
/// @returns true if the type is a bool type
@ -128,8 +125,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 an alias type
const AliasType* AsAlias() const;
/// @returns the type as an array type
const ArrayType* AsArray() const;
/// @returns the type as a bool type
@ -155,8 +150,6 @@ class Type : public Castable<Type> {
/// @returns the type as a void type
const VoidType* AsVoid() const;
/// @returns the type as an alias type
AliasType* AsAlias();
/// @returns the type as an array type
ArrayType* AsArray();
/// @returns the type as a bool type

View File

@ -28,7 +28,7 @@ TEST_F(U32TypeTest, Is) {
U32Type u;
Type* ty = &u;
EXPECT_FALSE(ty->Is<AccessControlType>());
EXPECT_FALSE(ty->IsAlias());
EXPECT_FALSE(ty->Is<AliasType>());
EXPECT_FALSE(ty->IsArray());
EXPECT_FALSE(ty->IsBool());
EXPECT_FALSE(ty->IsF32());

View File

@ -37,7 +37,7 @@ TEST_F(VectorTypeTest, Is) {
VectorType v{&i32, 4};
Type* ty = &v;
EXPECT_FALSE(ty->Is<AccessControlType>());
EXPECT_FALSE(ty->IsAlias());
EXPECT_FALSE(ty->Is<AliasType>());
EXPECT_FALSE(ty->IsArray());
EXPECT_FALSE(ty->IsBool());
EXPECT_FALSE(ty->IsF32());

View File

@ -903,7 +903,7 @@ Maybe<ast::type::Type*> ParserImpl::type_alias() {
auto* alias = module_.create<ast::type::AliasType>(name.value, type.value);
register_constructed(name.value, alias);
return alias->AsAlias();
return alias;
}
// type_decl

View File

@ -88,8 +88,8 @@ TEST_F(ParserImplTest, GlobalDecl_TypeAlias) {
auto& m = p->get_module();
ASSERT_EQ(m.constructed_types().size(), 1u);
ASSERT_TRUE(m.constructed_types()[0]->IsAlias());
EXPECT_EQ(m.constructed_types()[0]->AsAlias()->name(), "A");
ASSERT_TRUE(m.constructed_types()[0]->Is<ast::type::AliasType>());
EXPECT_EQ(m.constructed_types()[0]->As<ast::type::AliasType>()->name(), "A");
}
TEST_F(ParserImplTest, GlobalDecl_TypeAlias_StructIdent) {
@ -107,8 +107,8 @@ type B = A;)");
auto* str = m.constructed_types()[0]->AsStruct();
EXPECT_EQ(str->name(), "A");
ASSERT_TRUE(m.constructed_types()[1]->IsAlias());
auto* alias = m.constructed_types()[1]->AsAlias();
ASSERT_TRUE(m.constructed_types()[1]->Is<ast::type::AliasType>());
auto* alias = m.constructed_types()[1]->As<ast::type::AliasType>();
EXPECT_EQ(alias->name(), "B");
EXPECT_EQ(alias->type(), str);
}

View File

@ -36,8 +36,8 @@ TEST_F(ParserImplTest, TypeDecl_ParsesType) {
EXPECT_FALSE(t.errored);
EXPECT_TRUE(t.matched);
ASSERT_NE(t.value, nullptr);
ASSERT_TRUE(t->IsAlias());
auto* alias = t->AsAlias();
ASSERT_TRUE(t->Is<ast::type::AliasType>());
auto* alias = t->As<ast::type::AliasType>();
ASSERT_TRUE(alias->type()->IsI32());
ASSERT_EQ(alias->type(), i32);
}
@ -53,8 +53,8 @@ TEST_F(ParserImplTest, TypeDecl_ParsesStruct_Ident) {
EXPECT_FALSE(t.errored);
EXPECT_TRUE(t.matched);
ASSERT_NE(t.value, nullptr);
ASSERT_TRUE(t->IsAlias());
auto* alias = t->AsAlias();
ASSERT_TRUE(t->Is<ast::type::AliasType>());
auto* alias = t->As<ast::type::AliasType>();
EXPECT_EQ(alias->name(), "a");
ASSERT_TRUE(alias->type()->IsStruct());

View File

@ -58,9 +58,9 @@ TEST_F(ParserImplTest, TypeDecl_Identifier) {
EXPECT_FALSE(t.errored);
ASSERT_NE(t.value, nullptr) << p->error();
EXPECT_EQ(t.value, alias_type);
ASSERT_TRUE(t->IsAlias());
ASSERT_TRUE(t->Is<ast::type::AliasType>());
auto* alias = t->AsAlias();
auto* alias = t->As<ast::type::AliasType>();
EXPECT_EQ(alias->name(), "A");
EXPECT_EQ(alias->type(), int_type);
}

View File

@ -212,8 +212,8 @@ bool GeneratorImpl::EmitConstructedType(std::ostream& out,
const ast::type::Type* ty) {
make_indent(out);
if (ty->IsAlias()) {
auto* alias = ty->AsAlias();
if (ty->Is<ast::type::AliasType>()) {
auto* alias = ty->As<ast::type::AliasType>();
// HLSL typedef is for intrinsic types only. For an alias'd struct,
// generate a secondary struct with the new name.
if (alias->type()->IsStruct()) {
@ -2029,8 +2029,8 @@ bool GeneratorImpl::EmitSwitch(std::ostream& out, ast::SwitchStatement* stmt) {
bool GeneratorImpl::EmitType(std::ostream& out,
ast::type::Type* type,
const std::string& name) {
if (type->IsAlias()) {
auto* alias = type->AsAlias();
if (type->Is<ast::type::AliasType>()) {
auto* alias = type->As<ast::type::AliasType>();
out << namer_.NameFor(alias->name());
} else if (type->IsArray()) {
auto* ary = type->AsArray();

View File

@ -185,8 +185,8 @@ uint32_t GeneratorImpl::calculate_largest_alignment(
}
uint32_t GeneratorImpl::calculate_alignment_size(ast::type::Type* type) {
if (type->IsAlias()) {
return calculate_alignment_size(type->AsAlias()->type());
if (type->Is<ast::type::AliasType>()) {
return calculate_alignment_size(type->As<ast::type::AliasType>()->type());
}
if (type->IsArray()) {
auto* ary = type->AsArray();
@ -255,8 +255,8 @@ uint32_t GeneratorImpl::calculate_alignment_size(ast::type::Type* type) {
bool GeneratorImpl::EmitConstructedType(const ast::type::Type* ty) {
make_indent();
if (ty->IsAlias()) {
auto* alias = ty->AsAlias();
if (ty->Is<ast::type::AliasType>()) {
auto* alias = ty->As<ast::type::AliasType>();
out_ << "typedef ";
if (!EmitType(alias->type(), "")) {
@ -1787,8 +1787,8 @@ bool GeneratorImpl::EmitSwitch(ast::SwitchStatement* stmt) {
}
bool GeneratorImpl::EmitType(ast::type::Type* type, const std::string& name) {
if (type->IsAlias()) {
auto* alias = type->AsAlias();
if (type->Is<ast::type::AliasType>()) {
auto* alias = type->As<ast::type::AliasType>();
out_ << namer_.NameFor(alias->name());
} else if (type->IsArray()) {
auto* ary = type->AsArray();

View File

@ -2387,8 +2387,8 @@ uint32_t Builder::GenerateTypeIfNeeded(ast::type::Type* type) {
}
// The alias is a wrapper around the subtype, so emit the subtype
if (type->IsAlias()) {
return GenerateTypeIfNeeded(type->AsAlias()->type());
if (type->Is<ast::type::AliasType>()) {
return GenerateTypeIfNeeded(type->As<ast::type::AliasType>()->type());
}
auto val = type_name_to_id_.find(type->type_name());

View File

@ -168,8 +168,8 @@ bool GeneratorImpl::GenerateEntryPoint(const ast::Module& module,
bool GeneratorImpl::EmitConstructedType(const ast::type::Type* ty) {
make_indent();
if (ty->IsAlias()) {
auto* alias = ty->AsAlias();
if (ty->Is<ast::type::AliasType>()) {
auto* alias = ty->As<ast::type::AliasType>();
out_ << "type " << alias->name() << " = ";
if (!EmitType(alias->type())) {
return false;
@ -410,8 +410,8 @@ bool GeneratorImpl::EmitType(ast::type::Type* type) {
if (!EmitType(ac->type())) {
return false;
}
} else if (type->IsAlias()) {
out_ << type->AsAlias()->name();
} else if (type->Is<ast::type::AliasType>()) {
out_ << type->As<ast::type::AliasType>()->name();
} else if (type->IsArray()) {
auto* ary = type->AsArray();