Replace ConstructorExpression::(Is|As)* with Castable

Change-Id: I18e9768fef36b79cb0e65c6eb79fd147013c54f0
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/34317
Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
Ben Clayton 2020-11-30 23:30:58 +00:00
parent d6ae990811
commit 19fe07236e
15 changed files with 26 additions and 72 deletions

View File

@ -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"

View File

@ -16,9 +16,6 @@
#include <assert.h>
#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<ScalarConstructorExpression*>(this);
}
TypeConstructorExpression* ConstructorExpression::AsTypeConstructor() {
assert(IsTypeConstructor());
return static_cast<TypeConstructorExpression*>(this);
}
} // namespace ast
} // namespace tint

View File

@ -20,25 +20,12 @@
namespace tint {
namespace ast {
class ScalarConstructorExpression;
class TypeConstructorExpression;
/// Base class for constructor style expressions
class ConstructorExpression
: public Castable<ConstructorExpression, Expression> {
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();

View File

@ -31,10 +31,6 @@ ScalarConstructorExpression::ScalarConstructorExpression(
ScalarConstructorExpression::~ScalarConstructorExpression() = default;
bool ScalarConstructorExpression::IsScalarConstructor() const {
return true;
}
bool ScalarConstructorExpression::IsValid() const {
return literal_ != nullptr;
}

View File

@ -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; }

View File

@ -33,10 +33,6 @@ TypeConstructorExpression::TypeConstructorExpression(
TypeConstructorExpression::~TypeConstructorExpression() = default;
bool TypeConstructorExpression::IsTypeConstructor() const {
return true;
}
bool TypeConstructorExpression::IsValid() const {
if (values_.empty()) {
return true;

View File

@ -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; }

View File

@ -53,7 +53,7 @@ TEST_F(TypeConstructorExpressionTest, Creation_WithSource) {
TEST_F(TypeConstructorExpressionTest, IsTypeConstructor) {
TypeConstructorExpression t;
EXPECT_TRUE(t.IsTypeConstructor());
EXPECT_TRUE(t.Is<ast::TypeConstructorExpression>());
}
TEST_F(TypeConstructorExpressionTest, IsValid) {

View File

@ -131,13 +131,14 @@ std::map<uint32_t, Scalar> Inspector::GetConstantIDs() {
}
auto* constructor = expression->As<ast::ConstructorExpression>();
if (!constructor->IsScalarConstructor()) {
if (!constructor->Is<ast::ScalarConstructorExpression>()) {
// This is invalid WGSL, but handling gracefully.
result[constant_id] = Scalar();
continue;
}
auto* literal = constructor->AsScalarConstructor()->literal();
auto* literal =
constructor->As<ast::ScalarConstructorExpression>()->literal();
if (!literal) {
// This is invalid WGSL, but handling gracefully.
result[constant_id] = Scalar();

View File

@ -150,9 +150,9 @@ bool BoundArrayAccessorsTransform::ProcessExpression(ast::Expression* expr) {
}
} else if (expr->Is<ast::IdentifierExpression>()) {
/* nop */
} else if (auto* c = expr->As<ast::ConstructorExpression>()) {
if (c->IsTypeConstructor()) {
for (auto* e : c->AsTypeConstructor()->values()) {
} else if (expr->Is<ast::ConstructorExpression>()) {
if (auto* c = expr->As<ast::TypeConstructorExpression>()) {
for (auto* e : c->values()) {
if (!ProcessExpression(e)) {
return false;
}

View File

@ -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<ast::TypeConstructorExpression>()) {
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<ast::ScalarConstructorExpression>()->literal()->type());
}
return true;
}

View File

@ -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<ast::ScalarConstructorExpression>()) {
return EmitScalarConstructor(pre, out, scalar);
}
return EmitTypeConstructor(pre, out, expr->AsTypeConstructor());
return EmitTypeConstructor(pre, out,
expr->As<ast::TypeConstructorExpression>());
}
bool GeneratorImpl::EmitScalarConstructor(

View File

@ -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<ast::ScalarConstructorExpression>()) {
return EmitScalarConstructor(scalar);
}
return EmitTypeConstructor(expr->AsTypeConstructor());
return EmitTypeConstructor(expr->As<ast::TypeConstructorExpression>());
}
bool GeneratorImpl::EmitContinue(ast::ContinueStatement*) {

View File

@ -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<ast::ScalarConstructorExpression>()) {
return GenerateLiteralIfNeeded(var, scalar->literal());
}
if (expr->IsTypeConstructor()) {
return GenerateTypeConstructorExpression(expr->AsTypeConstructor(),
is_global_init);
if (auto* type = expr->As<ast::TypeConstructorExpression>()) {
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<ast::TypeConstructorExpression>();
auto* result_type = tc->type()->UnwrapAll();
for (size_t i = 0; i < tc->values().size(); ++i) {
auto* e = tc->values()[i];

View File

@ -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<ast::ScalarConstructorExpression>()) {
return EmitScalarConstructor(scalar);
}
return EmitTypeConstructor(expr->AsTypeConstructor());
return EmitTypeConstructor(expr->As<ast::TypeConstructorExpression>());
}
bool GeneratorImpl::EmitTypeConstructor(ast::TypeConstructorExpression* expr) {