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:
parent
d6ae990811
commit
19fe07236e
|
@ -35,6 +35,7 @@
|
||||||
#include "src/ast/type/u32_type.h"
|
#include "src/ast/type/u32_type.h"
|
||||||
#include "src/ast/type/vector_type.h"
|
#include "src/ast/type/vector_type.h"
|
||||||
#include "src/ast/type/void_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/uint_literal.h"
|
||||||
#include "src/ast/variable.h"
|
#include "src/ast/variable.h"
|
||||||
#include "src/context.h"
|
#include "src/context.h"
|
||||||
|
|
|
@ -16,9 +16,6 @@
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
#include "src/ast/scalar_constructor_expression.h"
|
|
||||||
#include "src/ast/type_constructor_expression.h"
|
|
||||||
|
|
||||||
namespace tint {
|
namespace tint {
|
||||||
namespace ast {
|
namespace ast {
|
||||||
|
|
||||||
|
@ -31,23 +28,5 @@ ConstructorExpression::ConstructorExpression(ConstructorExpression&&) = default;
|
||||||
ConstructorExpression::ConstructorExpression(const Source& source)
|
ConstructorExpression::ConstructorExpression(const Source& source)
|
||||||
: Base(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 ast
|
||||||
} // namespace tint
|
} // namespace tint
|
||||||
|
|
|
@ -20,25 +20,12 @@
|
||||||
namespace tint {
|
namespace tint {
|
||||||
namespace ast {
|
namespace ast {
|
||||||
|
|
||||||
class ScalarConstructorExpression;
|
|
||||||
class TypeConstructorExpression;
|
|
||||||
|
|
||||||
/// Base class for constructor style expressions
|
/// Base class for constructor style expressions
|
||||||
class ConstructorExpression
|
class ConstructorExpression
|
||||||
: public Castable<ConstructorExpression, Expression> {
|
: public Castable<ConstructorExpression, Expression> {
|
||||||
public:
|
public:
|
||||||
~ConstructorExpression() override;
|
~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:
|
protected:
|
||||||
/// Constructor
|
/// Constructor
|
||||||
ConstructorExpression();
|
ConstructorExpression();
|
||||||
|
|
|
@ -31,10 +31,6 @@ ScalarConstructorExpression::ScalarConstructorExpression(
|
||||||
|
|
||||||
ScalarConstructorExpression::~ScalarConstructorExpression() = default;
|
ScalarConstructorExpression::~ScalarConstructorExpression() = default;
|
||||||
|
|
||||||
bool ScalarConstructorExpression::IsScalarConstructor() const {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ScalarConstructorExpression::IsValid() const {
|
bool ScalarConstructorExpression::IsValid() const {
|
||||||
return literal_ != nullptr;
|
return literal_ != nullptr;
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,9 +41,6 @@ class ScalarConstructorExpression
|
||||||
ScalarConstructorExpression(ScalarConstructorExpression&&);
|
ScalarConstructorExpression(ScalarConstructorExpression&&);
|
||||||
~ScalarConstructorExpression() override;
|
~ScalarConstructorExpression() override;
|
||||||
|
|
||||||
/// @returns true if this is a scalar constructor
|
|
||||||
bool IsScalarConstructor() const override;
|
|
||||||
|
|
||||||
/// Set the literal value
|
/// Set the literal value
|
||||||
/// @param literal the literal
|
/// @param literal the literal
|
||||||
void set_literal(Literal* literal) { literal_ = literal; }
|
void set_literal(Literal* literal) { literal_ = literal; }
|
||||||
|
|
|
@ -33,10 +33,6 @@ TypeConstructorExpression::TypeConstructorExpression(
|
||||||
|
|
||||||
TypeConstructorExpression::~TypeConstructorExpression() = default;
|
TypeConstructorExpression::~TypeConstructorExpression() = default;
|
||||||
|
|
||||||
bool TypeConstructorExpression::IsTypeConstructor() const {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool TypeConstructorExpression::IsValid() const {
|
bool TypeConstructorExpression::IsValid() const {
|
||||||
if (values_.empty()) {
|
if (values_.empty()) {
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -44,9 +44,6 @@ class TypeConstructorExpression
|
||||||
TypeConstructorExpression(TypeConstructorExpression&&);
|
TypeConstructorExpression(TypeConstructorExpression&&);
|
||||||
~TypeConstructorExpression() override;
|
~TypeConstructorExpression() override;
|
||||||
|
|
||||||
/// @returns true if this is a type constructor
|
|
||||||
bool IsTypeConstructor() const override;
|
|
||||||
|
|
||||||
/// Set the type
|
/// Set the type
|
||||||
/// @param type the type
|
/// @param type the type
|
||||||
void set_type(type::Type* type) { type_ = type; }
|
void set_type(type::Type* type) { type_ = type; }
|
||||||
|
|
|
@ -53,7 +53,7 @@ TEST_F(TypeConstructorExpressionTest, Creation_WithSource) {
|
||||||
|
|
||||||
TEST_F(TypeConstructorExpressionTest, IsTypeConstructor) {
|
TEST_F(TypeConstructorExpressionTest, IsTypeConstructor) {
|
||||||
TypeConstructorExpression t;
|
TypeConstructorExpression t;
|
||||||
EXPECT_TRUE(t.IsTypeConstructor());
|
EXPECT_TRUE(t.Is<ast::TypeConstructorExpression>());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(TypeConstructorExpressionTest, IsValid) {
|
TEST_F(TypeConstructorExpressionTest, IsValid) {
|
||||||
|
|
|
@ -131,13 +131,14 @@ std::map<uint32_t, Scalar> Inspector::GetConstantIDs() {
|
||||||
}
|
}
|
||||||
|
|
||||||
auto* constructor = expression->As<ast::ConstructorExpression>();
|
auto* constructor = expression->As<ast::ConstructorExpression>();
|
||||||
if (!constructor->IsScalarConstructor()) {
|
if (!constructor->Is<ast::ScalarConstructorExpression>()) {
|
||||||
// This is invalid WGSL, but handling gracefully.
|
// This is invalid WGSL, but handling gracefully.
|
||||||
result[constant_id] = Scalar();
|
result[constant_id] = Scalar();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto* literal = constructor->AsScalarConstructor()->literal();
|
auto* literal =
|
||||||
|
constructor->As<ast::ScalarConstructorExpression>()->literal();
|
||||||
if (!literal) {
|
if (!literal) {
|
||||||
// This is invalid WGSL, but handling gracefully.
|
// This is invalid WGSL, but handling gracefully.
|
||||||
result[constant_id] = Scalar();
|
result[constant_id] = Scalar();
|
||||||
|
|
|
@ -150,9 +150,9 @@ bool BoundArrayAccessorsTransform::ProcessExpression(ast::Expression* expr) {
|
||||||
}
|
}
|
||||||
} else if (expr->Is<ast::IdentifierExpression>()) {
|
} else if (expr->Is<ast::IdentifierExpression>()) {
|
||||||
/* nop */
|
/* nop */
|
||||||
} else if (auto* c = expr->As<ast::ConstructorExpression>()) {
|
} else if (expr->Is<ast::ConstructorExpression>()) {
|
||||||
if (c->IsTypeConstructor()) {
|
if (auto* c = expr->As<ast::TypeConstructorExpression>()) {
|
||||||
for (auto* e : c->AsTypeConstructor()->values()) {
|
for (auto* e : c->values()) {
|
||||||
if (!ProcessExpression(e)) {
|
if (!ProcessExpression(e)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -831,8 +831,7 @@ bool TypeDeterminer::DetermineIntrinsic(ast::IdentifierExpression* ident,
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TypeDeterminer::DetermineConstructor(ast::ConstructorExpression* expr) {
|
bool TypeDeterminer::DetermineConstructor(ast::ConstructorExpression* expr) {
|
||||||
if (expr->IsTypeConstructor()) {
|
if (auto* ty = expr->As<ast::TypeConstructorExpression>()) {
|
||||||
auto* ty = expr->AsTypeConstructor();
|
|
||||||
for (auto* value : ty->values()) {
|
for (auto* value : ty->values()) {
|
||||||
if (!DetermineResultType(value)) {
|
if (!DetermineResultType(value)) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -840,7 +839,8 @@ bool TypeDeterminer::DetermineConstructor(ast::ConstructorExpression* expr) {
|
||||||
}
|
}
|
||||||
expr->set_result_type(ty->type());
|
expr->set_result_type(ty->type());
|
||||||
} else {
|
} else {
|
||||||
expr->set_result_type(expr->AsScalarConstructor()->literal()->type());
|
expr->set_result_type(
|
||||||
|
expr->As<ast::ScalarConstructorExpression>()->literal()->type());
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -904,10 +904,11 @@ bool GeneratorImpl::EmitCase(std::ostream& out, ast::CaseStatement* stmt) {
|
||||||
bool GeneratorImpl::EmitConstructor(std::ostream& pre,
|
bool GeneratorImpl::EmitConstructor(std::ostream& pre,
|
||||||
std::ostream& out,
|
std::ostream& out,
|
||||||
ast::ConstructorExpression* expr) {
|
ast::ConstructorExpression* expr) {
|
||||||
if (expr->IsScalarConstructor()) {
|
if (auto* scalar = expr->As<ast::ScalarConstructorExpression>()) {
|
||||||
return EmitScalarConstructor(pre, out, expr->AsScalarConstructor());
|
return EmitScalarConstructor(pre, out, scalar);
|
||||||
}
|
}
|
||||||
return EmitTypeConstructor(pre, out, expr->AsTypeConstructor());
|
return EmitTypeConstructor(pre, out,
|
||||||
|
expr->As<ast::TypeConstructorExpression>());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool GeneratorImpl::EmitScalarConstructor(
|
bool GeneratorImpl::EmitScalarConstructor(
|
||||||
|
|
|
@ -882,10 +882,10 @@ bool GeneratorImpl::EmitCase(ast::CaseStatement* stmt) {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool GeneratorImpl::EmitConstructor(ast::ConstructorExpression* expr) {
|
bool GeneratorImpl::EmitConstructor(ast::ConstructorExpression* expr) {
|
||||||
if (expr->IsScalarConstructor()) {
|
if (auto* scalar = expr->As<ast::ScalarConstructorExpression>()) {
|
||||||
return EmitScalarConstructor(expr->AsScalarConstructor());
|
return EmitScalarConstructor(scalar);
|
||||||
}
|
}
|
||||||
return EmitTypeConstructor(expr->AsTypeConstructor());
|
return EmitTypeConstructor(expr->As<ast::TypeConstructorExpression>());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool GeneratorImpl::EmitContinue(ast::ContinueStatement*) {
|
bool GeneratorImpl::EmitContinue(ast::ContinueStatement*) {
|
||||||
|
|
|
@ -1157,12 +1157,11 @@ uint32_t Builder::GenerateConstructorExpression(
|
||||||
ast::Variable* var,
|
ast::Variable* var,
|
||||||
ast::ConstructorExpression* expr,
|
ast::ConstructorExpression* expr,
|
||||||
bool is_global_init) {
|
bool is_global_init) {
|
||||||
if (expr->IsScalarConstructor()) {
|
if (auto* scalar = expr->As<ast::ScalarConstructorExpression>()) {
|
||||||
return GenerateLiteralIfNeeded(var, expr->AsScalarConstructor()->literal());
|
return GenerateLiteralIfNeeded(var, scalar->literal());
|
||||||
}
|
}
|
||||||
if (expr->IsTypeConstructor()) {
|
if (auto* type = expr->As<ast::TypeConstructorExpression>()) {
|
||||||
return GenerateTypeConstructorExpression(expr->AsTypeConstructor(),
|
return GenerateTypeConstructorExpression(type, is_global_init);
|
||||||
is_global_init);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
error_ = "unknown constructor expression";
|
error_ = "unknown constructor expression";
|
||||||
|
@ -1178,7 +1177,7 @@ bool Builder::is_constructor_const(ast::Expression* expr, bool is_global_init) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto* tc = constructor->AsTypeConstructor();
|
auto* tc = constructor->As<ast::TypeConstructorExpression>();
|
||||||
auto* result_type = tc->type()->UnwrapAll();
|
auto* result_type = tc->type()->UnwrapAll();
|
||||||
for (size_t i = 0; i < tc->values().size(); ++i) {
|
for (size_t i = 0; i < tc->values().size(); ++i) {
|
||||||
auto* e = tc->values()[i];
|
auto* e = tc->values()[i];
|
||||||
|
|
|
@ -286,10 +286,10 @@ bool GeneratorImpl::EmitCall(ast::CallExpression* expr) {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool GeneratorImpl::EmitConstructor(ast::ConstructorExpression* expr) {
|
bool GeneratorImpl::EmitConstructor(ast::ConstructorExpression* expr) {
|
||||||
if (expr->IsScalarConstructor()) {
|
if (auto* scalar = expr->As<ast::ScalarConstructorExpression>()) {
|
||||||
return EmitScalarConstructor(expr->AsScalarConstructor());
|
return EmitScalarConstructor(scalar);
|
||||||
}
|
}
|
||||||
return EmitTypeConstructor(expr->AsTypeConstructor());
|
return EmitTypeConstructor(expr->As<ast::TypeConstructorExpression>());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool GeneratorImpl::EmitTypeConstructor(ast::TypeConstructorExpression* expr) {
|
bool GeneratorImpl::EmitTypeConstructor(ast::TypeConstructorExpression* expr) {
|
||||||
|
|
Loading…
Reference in New Issue