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

Change-Id: I842483890b369d63c23dba475b6738bffe5cfdbd
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/34319
Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
Ben Clayton
2020-11-30 23:30:58 +00:00
parent aedca4288c
commit acf7643518
45 changed files with 250 additions and 359 deletions

View File

@@ -22,10 +22,6 @@ BoolLiteral::BoolLiteral(ast::type::Type* type, bool value)
BoolLiteral::~BoolLiteral() = default;
bool BoolLiteral::IsBool() const {
return true;
}
std::string BoolLiteral::to_str() const {
return value_ ? "true" : "false";
}

View File

@@ -31,9 +31,6 @@ class BoolLiteral : public Castable<BoolLiteral, Literal> {
BoolLiteral(ast::type::Type* type, bool value);
~BoolLiteral() override;
/// @returns true if this is a bool literal
bool IsBool() const override;
/// @returns true if the bool literal is true
bool IsTrue() const { return value_; }
/// @returns true if the bool literal is false

View File

@@ -14,8 +14,12 @@
#include "src/ast/bool_literal.h"
#include "src/ast/float_literal.h"
#include "src/ast/null_literal.h"
#include "src/ast/sint_literal.h"
#include "src/ast/test_helper.h"
#include "src/ast/type/bool_type.h"
#include "src/ast/uint_literal.h"
namespace tint {
namespace ast {
@@ -26,7 +30,7 @@ using BoolLiteralTest = TestHelper;
TEST_F(BoolLiteralTest, True) {
ast::type::BoolType bool_type;
BoolLiteral b{&bool_type, true};
ASSERT_TRUE(b.IsBool());
ASSERT_TRUE(b.Is<BoolLiteral>());
ASSERT_TRUE(b.IsTrue());
ASSERT_FALSE(b.IsFalse());
}
@@ -34,7 +38,7 @@ TEST_F(BoolLiteralTest, True) {
TEST_F(BoolLiteralTest, False) {
ast::type::BoolType bool_type;
BoolLiteral b{&bool_type, false};
ASSERT_TRUE(b.IsBool());
ASSERT_TRUE(b.Is<BoolLiteral>());
ASSERT_FALSE(b.IsTrue());
ASSERT_TRUE(b.IsFalse());
}
@@ -42,12 +46,13 @@ TEST_F(BoolLiteralTest, False) {
TEST_F(BoolLiteralTest, Is) {
ast::type::BoolType bool_type;
BoolLiteral b{&bool_type, false};
EXPECT_TRUE(b.IsBool());
EXPECT_FALSE(b.IsSint());
EXPECT_FALSE(b.IsFloat());
EXPECT_FALSE(b.IsUint());
EXPECT_FALSE(b.IsInt());
EXPECT_FALSE(b.IsNull());
Literal* l = &b;
EXPECT_TRUE(l->Is<BoolLiteral>());
EXPECT_FALSE(l->Is<SintLiteral>());
EXPECT_FALSE(l->Is<FloatLiteral>());
EXPECT_FALSE(l->Is<UintLiteral>());
EXPECT_FALSE(l->Is<IntLiteral>());
EXPECT_FALSE(l->Is<NullLiteral>());
}
TEST_F(BoolLiteralTest, ToStr) {

View File

@@ -25,10 +25,6 @@ FloatLiteral::FloatLiteral(ast::type::Type* type, float value)
FloatLiteral::~FloatLiteral() = default;
bool FloatLiteral::IsFloat() const {
return true;
}
std::string FloatLiteral::to_str() const {
return std::to_string(value_);
}

View File

@@ -31,9 +31,6 @@ class FloatLiteral : public Castable<FloatLiteral, Literal> {
FloatLiteral(ast::type::Type* type, float value);
~FloatLiteral() override;
/// @returns true if this is a float literal
bool IsFloat() const override;
/// @returns the float literal value
float value() const { return value_; }

View File

@@ -14,8 +14,12 @@
#include "src/ast/float_literal.h"
#include "src/ast/bool_literal.h"
#include "src/ast/null_literal.h"
#include "src/ast/sint_literal.h"
#include "src/ast/test_helper.h"
#include "src/ast/type/f32_type.h"
#include "src/ast/uint_literal.h"
namespace tint {
namespace ast {
@@ -26,19 +30,20 @@ using FloatLiteralTest = TestHelper;
TEST_F(FloatLiteralTest, Value) {
ast::type::F32Type f32;
FloatLiteral f{&f32, 47.2f};
ASSERT_TRUE(f.IsFloat());
ASSERT_TRUE(f.Is<FloatLiteral>());
EXPECT_EQ(f.value(), 47.2f);
}
TEST_F(FloatLiteralTest, Is) {
ast::type::F32Type f32;
FloatLiteral f{&f32, 42.f};
EXPECT_FALSE(f.IsBool());
EXPECT_FALSE(f.IsSint());
EXPECT_FALSE(f.IsInt());
EXPECT_TRUE(f.IsFloat());
EXPECT_FALSE(f.IsUint());
EXPECT_FALSE(f.IsNull());
Literal* l = &f;
EXPECT_FALSE(l->Is<BoolLiteral>());
EXPECT_FALSE(l->Is<SintLiteral>());
EXPECT_FALSE(l->Is<IntLiteral>());
EXPECT_TRUE(l->Is<FloatLiteral>());
EXPECT_FALSE(l->Is<UintLiteral>());
EXPECT_FALSE(l->Is<NullLiteral>());
}
TEST_F(FloatLiteralTest, ToStr) {

View File

@@ -21,9 +21,5 @@ IntLiteral::IntLiteral(ast::type::Type* type) : Base(type) {}
IntLiteral::~IntLiteral() = default;
bool IntLiteral::IsInt() const {
return true;
}
} // namespace ast
} // namespace tint

View File

@@ -27,9 +27,6 @@ class IntLiteral : public Castable<IntLiteral, Literal> {
public:
~IntLiteral() override;
/// @returns true if this is a signed or unsigned integer.
bool IsInt() const override;
protected:
/// Constructor
/// @param type the type of the literal

View File

@@ -14,6 +14,9 @@
#include "src/ast/int_literal.h"
#include "src/ast/bool_literal.h"
#include "src/ast/float_literal.h"
#include "src/ast/null_literal.h"
#include "src/ast/sint_literal.h"
#include "src/ast/test_helper.h"
#include "src/ast/type/i32_type.h"
@@ -29,13 +32,13 @@ using IntLiteralTest = TestHelper;
TEST_F(IntLiteralTest, Sint_IsInt) {
ast::type::I32Type i32;
SintLiteral i{&i32, 47};
ASSERT_TRUE(i.IsInt());
ASSERT_TRUE(i.Is<IntLiteral>());
}
TEST_F(IntLiteralTest, Uint_IsInt) {
ast::type::I32Type i32;
UintLiteral i{&i32, 42};
EXPECT_TRUE(i.IsInt());
EXPECT_TRUE(i.Is<IntLiteral>());
}
} // namespace

View File

@@ -14,15 +14,6 @@
#include "src/ast/literal.h"
#include <assert.h>
#include "src/ast/bool_literal.h"
#include "src/ast/float_literal.h"
#include "src/ast/int_literal.h"
#include "src/ast/null_literal.h"
#include "src/ast/sint_literal.h"
#include "src/ast/uint_literal.h"
namespace tint {
namespace ast {
@@ -30,60 +21,6 @@ Literal::Literal(ast::type::Type* type) : type_(type) {}
Literal::~Literal() = default;
bool Literal::IsBool() const {
return false;
}
bool Literal::IsFloat() const {
return false;
}
bool Literal::IsInt() const {
return false;
}
bool Literal::IsSint() const {
return false;
}
bool Literal::IsNull() const {
return false;
}
bool Literal::IsUint() const {
return false;
}
BoolLiteral* Literal::AsBool() {
assert(IsBool());
return static_cast<BoolLiteral*>(this);
}
FloatLiteral* Literal::AsFloat() {
assert(IsFloat());
return static_cast<FloatLiteral*>(this);
}
IntLiteral* Literal::AsInt() {
assert(IsInt());
return static_cast<IntLiteral*>(this);
}
SintLiteral* Literal::AsSint() {
assert(IsSint());
return static_cast<SintLiteral*>(this);
}
NullLiteral* Literal::AsNull() {
assert(IsNull());
return static_cast<NullLiteral*>(this);
}
UintLiteral* Literal::AsUint() {
assert(IsUint());
return static_cast<UintLiteral*>(this);
}
bool Literal::IsValid() const {
return true;
}

View File

@@ -23,44 +23,11 @@
namespace tint {
namespace ast {
class BoolLiteral;
class FloatLiteral;
class NullLiteral;
class SintLiteral;
class IntLiteral;
class UintLiteral;
/// Base class for a literal value
class Literal : public Castable<Literal, Node> {
public:
~Literal() override;
/// @returns true if this is a bool literal
virtual bool IsBool() const;
/// @returns true if this is a float literal
virtual bool IsFloat() const;
/// @returns thre if this is an int literal (either sint or uint)
virtual bool IsInt() const;
/// @returns true if this is a signed int literal
virtual bool IsSint() const;
/// @returns true if this is a null literal
virtual bool IsNull() const;
/// @returns true if this is a unsigned int literal
virtual bool IsUint() const;
/// @returns the literal as a boolean literal
BoolLiteral* AsBool();
/// @returns the literal as a float literal
FloatLiteral* AsFloat();
/// @returns the literal as an int literal
IntLiteral* AsInt();
/// @returns the literal as a signed int literal
SintLiteral* AsSint();
/// @returns the literal as a null literal
NullLiteral* AsNull();
/// @returns the literal as a unsigned int literal
UintLiteral* AsUint();
/// @returns the type of the literal
ast::type::Type* type() const { return type_; }

View File

@@ -21,10 +21,6 @@ NullLiteral::NullLiteral(ast::type::Type* type) : Base(type) {}
NullLiteral::~NullLiteral() = default;
bool NullLiteral::IsNull() const {
return true;
}
std::string NullLiteral::to_str() const {
return "null " + type()->type_name();
}

View File

@@ -30,9 +30,6 @@ class NullLiteral : public Castable<NullLiteral, Literal> {
explicit NullLiteral(ast::type::Type* type);
~NullLiteral() override;
/// @returns true if this is a null literal
bool IsNull() const override;
/// @returns the name for this literal. This name is unique to this value.
std::string name() const override;

View File

@@ -14,8 +14,12 @@
#include "src/ast/null_literal.h"
#include "src/ast/bool_literal.h"
#include "src/ast/float_literal.h"
#include "src/ast/sint_literal.h"
#include "src/ast/test_helper.h"
#include "src/ast/type/i32_type.h"
#include "src/ast/uint_literal.h"
namespace tint {
namespace ast {
@@ -26,12 +30,13 @@ using NullLiteralTest = TestHelper;
TEST_F(NullLiteralTest, Is) {
ast::type::I32Type i32;
NullLiteral i{&i32};
EXPECT_FALSE(i.IsBool());
EXPECT_FALSE(i.IsSint());
EXPECT_FALSE(i.IsFloat());
EXPECT_FALSE(i.IsUint());
EXPECT_FALSE(i.IsInt());
EXPECT_TRUE(i.IsNull());
Literal* l = &i;
EXPECT_FALSE(l->Is<BoolLiteral>());
EXPECT_FALSE(l->Is<SintLiteral>());
EXPECT_FALSE(l->Is<FloatLiteral>());
EXPECT_FALSE(l->Is<UintLiteral>());
EXPECT_FALSE(l->Is<IntLiteral>());
EXPECT_TRUE(l->Is<NullLiteral>());
}
TEST_F(NullLiteralTest, ToStr) {

View File

@@ -22,10 +22,6 @@ SintLiteral::SintLiteral(ast::type::Type* type, int32_t value)
SintLiteral::~SintLiteral() = default;
bool SintLiteral::IsSint() const {
return true;
}
std::string SintLiteral::to_str() const {
return std::to_string(value_);
}

View File

@@ -31,9 +31,6 @@ class SintLiteral : public Castable<SintLiteral, IntLiteral> {
SintLiteral(ast::type::Type* type, int32_t value);
~SintLiteral() override;
/// @returns true if this is a signed int literal
bool IsSint() const override;
/// Updates the literals value
/// @param val the value to set
void set_value(int32_t val) { value_ = val; }

View File

@@ -14,9 +14,13 @@
#include "src/ast/sint_literal.h"
#include "src/ast/bool_literal.h"
#include "src/ast/float_literal.h"
#include "src/ast/null_literal.h"
#include "src/ast/test_helper.h"
#include "src/ast/type/i32_type.h"
#include "src/ast/type/u32_type.h"
#include "src/ast/uint_literal.h"
namespace tint {
namespace ast {
@@ -27,18 +31,19 @@ using SintLiteralTest = TestHelper;
TEST_F(SintLiteralTest, Value) {
ast::type::I32Type i32;
SintLiteral i{&i32, 47};
ASSERT_TRUE(i.IsSint());
ASSERT_TRUE(i.Is<SintLiteral>());
EXPECT_EQ(i.value(), 47);
}
TEST_F(SintLiteralTest, Is) {
ast::type::I32Type i32;
SintLiteral i{&i32, 42};
EXPECT_FALSE(i.IsBool());
EXPECT_TRUE(i.IsSint());
EXPECT_FALSE(i.IsFloat());
EXPECT_FALSE(i.IsUint());
EXPECT_FALSE(i.IsNull());
Literal* l = &i;
EXPECT_FALSE(l->Is<BoolLiteral>());
EXPECT_TRUE(l->Is<SintLiteral>());
EXPECT_FALSE(l->Is<FloatLiteral>());
EXPECT_FALSE(l->Is<UintLiteral>());
EXPECT_FALSE(l->Is<NullLiteral>());
}
TEST_F(SintLiteralTest, ToStr) {

View File

@@ -22,10 +22,6 @@ UintLiteral::UintLiteral(ast::type::Type* type, uint32_t value)
UintLiteral::~UintLiteral() = default;
bool UintLiteral::IsUint() const {
return true;
}
std::string UintLiteral::to_str() const {
return std::to_string(value_);
}

View File

@@ -31,9 +31,6 @@ class UintLiteral : public Castable<UintLiteral, IntLiteral> {
UintLiteral(ast::type::Type* type, uint32_t value);
~UintLiteral() override;
/// @returns true if this is a uint literal
bool IsUint() const override;
/// Updates the literals value
/// @param val the value to set
void set_value(uint32_t val) { value_ = val; }

View File

@@ -14,6 +14,10 @@
#include "src/ast/uint_literal.h"
#include "src/ast/bool_literal.h"
#include "src/ast/float_literal.h"
#include "src/ast/null_literal.h"
#include "src/ast/sint_literal.h"
#include "src/ast/test_helper.h"
#include "src/ast/type/u32_type.h"
@@ -26,18 +30,19 @@ using UintLiteralTest = TestHelper;
TEST_F(UintLiteralTest, Value) {
ast::type::U32Type u32;
UintLiteral u{&u32, 47};
ASSERT_TRUE(u.IsUint());
ASSERT_TRUE(u.Is<UintLiteral>());
EXPECT_EQ(u.value(), 47u);
}
TEST_F(UintLiteralTest, Is) {
ast::type::U32Type u32;
UintLiteral u{&u32, 42};
EXPECT_FALSE(u.IsBool());
EXPECT_FALSE(u.IsSint());
EXPECT_FALSE(u.IsFloat());
EXPECT_TRUE(u.IsUint());
EXPECT_FALSE(u.IsNull());
Literal* l = &u;
EXPECT_FALSE(l->Is<BoolLiteral>());
EXPECT_FALSE(l->Is<SintLiteral>());
EXPECT_FALSE(l->Is<FloatLiteral>());
EXPECT_TRUE(l->Is<UintLiteral>());
EXPECT_FALSE(l->Is<NullLiteral>());
}
TEST_F(UintLiteralTest, ToStr) {