diff --git a/src/ast/builder.h b/src/ast/builder.h index 1080ead1a5..f80a4f671e 100644 --- a/src/ast/builder.h +++ b/src/ast/builder.h @@ -35,6 +35,7 @@ #include "src/ast/type/u32_type.h" #include "src/ast/type/vector_type.h" #include "src/ast/type/void_type.h" +#include "src/ast/type_constructor_expression.h" #include "src/ast/uint_literal.h" #include "src/ast/variable.h" #include "src/context.h" diff --git a/src/ast/constructor_expression.cc b/src/ast/constructor_expression.cc index 1f19ed83f8..33266566f3 100644 --- a/src/ast/constructor_expression.cc +++ b/src/ast/constructor_expression.cc @@ -16,9 +16,6 @@ #include -#include "src/ast/scalar_constructor_expression.h" -#include "src/ast/type_constructor_expression.h" - namespace tint { namespace ast { @@ -31,23 +28,5 @@ ConstructorExpression::ConstructorExpression(ConstructorExpression&&) = default; ConstructorExpression::ConstructorExpression(const Source& source) : Base(source) {} -bool ConstructorExpression::IsScalarConstructor() const { - return false; -} - -bool ConstructorExpression::IsTypeConstructor() const { - return false; -} - -ScalarConstructorExpression* ConstructorExpression::AsScalarConstructor() { - assert(IsScalarConstructor()); - return static_cast(this); -} - -TypeConstructorExpression* ConstructorExpression::AsTypeConstructor() { - assert(IsTypeConstructor()); - return static_cast(this); -} - } // namespace ast } // namespace tint diff --git a/src/ast/constructor_expression.h b/src/ast/constructor_expression.h index 3c9e375a59..9e3e9f2268 100644 --- a/src/ast/constructor_expression.h +++ b/src/ast/constructor_expression.h @@ -20,25 +20,12 @@ namespace tint { namespace ast { -class ScalarConstructorExpression; -class TypeConstructorExpression; - /// Base class for constructor style expressions class ConstructorExpression : public Castable { public: ~ConstructorExpression() override; - /// @returns true if this is a scalar constructor - virtual bool IsScalarConstructor() const; - /// @returns true if this is a type constructor - virtual bool IsTypeConstructor() const; - - /// @returns this as a scalar constructor expression - ScalarConstructorExpression* AsScalarConstructor(); - /// @returns this as a type constructor expression - TypeConstructorExpression* AsTypeConstructor(); - protected: /// Constructor ConstructorExpression(); diff --git a/src/ast/scalar_constructor_expression.cc b/src/ast/scalar_constructor_expression.cc index 02fee32d88..2c05b4d415 100644 --- a/src/ast/scalar_constructor_expression.cc +++ b/src/ast/scalar_constructor_expression.cc @@ -31,10 +31,6 @@ ScalarConstructorExpression::ScalarConstructorExpression( ScalarConstructorExpression::~ScalarConstructorExpression() = default; -bool ScalarConstructorExpression::IsScalarConstructor() const { - return true; -} - bool ScalarConstructorExpression::IsValid() const { return literal_ != nullptr; } diff --git a/src/ast/scalar_constructor_expression.h b/src/ast/scalar_constructor_expression.h index d780fef0b3..273f7bc1a3 100644 --- a/src/ast/scalar_constructor_expression.h +++ b/src/ast/scalar_constructor_expression.h @@ -41,9 +41,6 @@ class ScalarConstructorExpression ScalarConstructorExpression(ScalarConstructorExpression&&); ~ScalarConstructorExpression() override; - /// @returns true if this is a scalar constructor - bool IsScalarConstructor() const override; - /// Set the literal value /// @param literal the literal void set_literal(Literal* literal) { literal_ = literal; } diff --git a/src/ast/type_constructor_expression.cc b/src/ast/type_constructor_expression.cc index 8f1d8a87d7..b0f111febd 100644 --- a/src/ast/type_constructor_expression.cc +++ b/src/ast/type_constructor_expression.cc @@ -33,10 +33,6 @@ TypeConstructorExpression::TypeConstructorExpression( TypeConstructorExpression::~TypeConstructorExpression() = default; -bool TypeConstructorExpression::IsTypeConstructor() const { - return true; -} - bool TypeConstructorExpression::IsValid() const { if (values_.empty()) { return true; diff --git a/src/ast/type_constructor_expression.h b/src/ast/type_constructor_expression.h index ae911e8d42..4ed73eede7 100644 --- a/src/ast/type_constructor_expression.h +++ b/src/ast/type_constructor_expression.h @@ -44,9 +44,6 @@ class TypeConstructorExpression TypeConstructorExpression(TypeConstructorExpression&&); ~TypeConstructorExpression() override; - /// @returns true if this is a type constructor - bool IsTypeConstructor() const override; - /// Set the type /// @param type the type void set_type(type::Type* type) { type_ = type; } diff --git a/src/ast/type_constructor_expression_test.cc b/src/ast/type_constructor_expression_test.cc index c576fedba7..24eb0e62d6 100644 --- a/src/ast/type_constructor_expression_test.cc +++ b/src/ast/type_constructor_expression_test.cc @@ -53,7 +53,7 @@ TEST_F(TypeConstructorExpressionTest, Creation_WithSource) { TEST_F(TypeConstructorExpressionTest, IsTypeConstructor) { TypeConstructorExpression t; - EXPECT_TRUE(t.IsTypeConstructor()); + EXPECT_TRUE(t.Is()); } TEST_F(TypeConstructorExpressionTest, IsValid) { diff --git a/src/inspector/inspector.cc b/src/inspector/inspector.cc index 655ed3a537..a9401a4fa8 100644 --- a/src/inspector/inspector.cc +++ b/src/inspector/inspector.cc @@ -131,13 +131,14 @@ std::map Inspector::GetConstantIDs() { } auto* constructor = expression->As(); - if (!constructor->IsScalarConstructor()) { + if (!constructor->Is()) { // This is invalid WGSL, but handling gracefully. result[constant_id] = Scalar(); continue; } - auto* literal = constructor->AsScalarConstructor()->literal(); + auto* literal = + constructor->As()->literal(); if (!literal) { // This is invalid WGSL, but handling gracefully. result[constant_id] = Scalar(); diff --git a/src/transform/bound_array_accessors_transform.cc b/src/transform/bound_array_accessors_transform.cc index 135518e1dd..a1ea02de5e 100644 --- a/src/transform/bound_array_accessors_transform.cc +++ b/src/transform/bound_array_accessors_transform.cc @@ -150,9 +150,9 @@ bool BoundArrayAccessorsTransform::ProcessExpression(ast::Expression* expr) { } } else if (expr->Is()) { /* nop */ - } else if (auto* c = expr->As()) { - if (c->IsTypeConstructor()) { - for (auto* e : c->AsTypeConstructor()->values()) { + } else if (expr->Is()) { + if (auto* c = expr->As()) { + for (auto* e : c->values()) { if (!ProcessExpression(e)) { return false; } diff --git a/src/type_determiner.cc b/src/type_determiner.cc index 6d3a38eaca..b0eaa9a17f 100644 --- a/src/type_determiner.cc +++ b/src/type_determiner.cc @@ -831,8 +831,7 @@ bool TypeDeterminer::DetermineIntrinsic(ast::IdentifierExpression* ident, } bool TypeDeterminer::DetermineConstructor(ast::ConstructorExpression* expr) { - if (expr->IsTypeConstructor()) { - auto* ty = expr->AsTypeConstructor(); + if (auto* ty = expr->As()) { for (auto* value : ty->values()) { if (!DetermineResultType(value)) { return false; @@ -840,7 +839,8 @@ bool TypeDeterminer::DetermineConstructor(ast::ConstructorExpression* expr) { } expr->set_result_type(ty->type()); } else { - expr->set_result_type(expr->AsScalarConstructor()->literal()->type()); + expr->set_result_type( + expr->As()->literal()->type()); } return true; } diff --git a/src/writer/hlsl/generator_impl.cc b/src/writer/hlsl/generator_impl.cc index 62b46e5878..c5c61b5186 100644 --- a/src/writer/hlsl/generator_impl.cc +++ b/src/writer/hlsl/generator_impl.cc @@ -904,10 +904,11 @@ bool GeneratorImpl::EmitCase(std::ostream& out, ast::CaseStatement* stmt) { bool GeneratorImpl::EmitConstructor(std::ostream& pre, std::ostream& out, ast::ConstructorExpression* expr) { - if (expr->IsScalarConstructor()) { - return EmitScalarConstructor(pre, out, expr->AsScalarConstructor()); + if (auto* scalar = expr->As()) { + return EmitScalarConstructor(pre, out, scalar); } - return EmitTypeConstructor(pre, out, expr->AsTypeConstructor()); + return EmitTypeConstructor(pre, out, + expr->As()); } bool GeneratorImpl::EmitScalarConstructor( diff --git a/src/writer/msl/generator_impl.cc b/src/writer/msl/generator_impl.cc index 92e44336cf..a1a9d0d714 100644 --- a/src/writer/msl/generator_impl.cc +++ b/src/writer/msl/generator_impl.cc @@ -882,10 +882,10 @@ bool GeneratorImpl::EmitCase(ast::CaseStatement* stmt) { } bool GeneratorImpl::EmitConstructor(ast::ConstructorExpression* expr) { - if (expr->IsScalarConstructor()) { - return EmitScalarConstructor(expr->AsScalarConstructor()); + if (auto* scalar = expr->As()) { + return EmitScalarConstructor(scalar); } - return EmitTypeConstructor(expr->AsTypeConstructor()); + return EmitTypeConstructor(expr->As()); } bool GeneratorImpl::EmitContinue(ast::ContinueStatement*) { diff --git a/src/writer/spirv/builder.cc b/src/writer/spirv/builder.cc index ef9dc6f76f..1f2bdc2af6 100644 --- a/src/writer/spirv/builder.cc +++ b/src/writer/spirv/builder.cc @@ -1157,12 +1157,11 @@ uint32_t Builder::GenerateConstructorExpression( ast::Variable* var, ast::ConstructorExpression* expr, bool is_global_init) { - if (expr->IsScalarConstructor()) { - return GenerateLiteralIfNeeded(var, expr->AsScalarConstructor()->literal()); + if (auto* scalar = expr->As()) { + return GenerateLiteralIfNeeded(var, scalar->literal()); } - if (expr->IsTypeConstructor()) { - return GenerateTypeConstructorExpression(expr->AsTypeConstructor(), - is_global_init); + if (auto* type = expr->As()) { + return GenerateTypeConstructorExpression(type, is_global_init); } error_ = "unknown constructor expression"; @@ -1178,7 +1177,7 @@ bool Builder::is_constructor_const(ast::Expression* expr, bool is_global_init) { return true; } - auto* tc = constructor->AsTypeConstructor(); + auto* tc = constructor->As(); auto* result_type = tc->type()->UnwrapAll(); for (size_t i = 0; i < tc->values().size(); ++i) { auto* e = tc->values()[i]; diff --git a/src/writer/wgsl/generator_impl.cc b/src/writer/wgsl/generator_impl.cc index 6942a34e57..0761d805bc 100644 --- a/src/writer/wgsl/generator_impl.cc +++ b/src/writer/wgsl/generator_impl.cc @@ -286,10 +286,10 @@ bool GeneratorImpl::EmitCall(ast::CallExpression* expr) { } bool GeneratorImpl::EmitConstructor(ast::ConstructorExpression* expr) { - if (expr->IsScalarConstructor()) { - return EmitScalarConstructor(expr->AsScalarConstructor()); + if (auto* scalar = expr->As()) { + return EmitScalarConstructor(scalar); } - return EmitTypeConstructor(expr->AsTypeConstructor()); + return EmitTypeConstructor(expr->As()); } bool GeneratorImpl::EmitTypeConstructor(ast::TypeConstructorExpression* expr) {