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>
diff --git a/src/ast/bool_literal.cc b/src/ast/bool_literal.cc
index 3fba92d..a40d99a 100644
--- a/src/ast/bool_literal.cc
+++ b/src/ast/bool_literal.cc
@@ -22,10 +22,6 @@
BoolLiteral::~BoolLiteral() = default;
-bool BoolLiteral::IsBool() const {
- return true;
-}
-
std::string BoolLiteral::to_str() const {
return value_ ? "true" : "false";
}
diff --git a/src/ast/bool_literal.h b/src/ast/bool_literal.h
index 9591470..2700531 100644
--- a/src/ast/bool_literal.h
+++ b/src/ast/bool_literal.h
@@ -31,9 +31,6 @@
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
diff --git a/src/ast/bool_literal_test.cc b/src/ast/bool_literal_test.cc
index 1a09808..63fe663 100644
--- a/src/ast/bool_literal_test.cc
+++ b/src/ast/bool_literal_test.cc
@@ -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 @@
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, 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, 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) {
diff --git a/src/ast/float_literal.cc b/src/ast/float_literal.cc
index d7b6a31..70ba92e 100644
--- a/src/ast/float_literal.cc
+++ b/src/ast/float_literal.cc
@@ -25,10 +25,6 @@
FloatLiteral::~FloatLiteral() = default;
-bool FloatLiteral::IsFloat() const {
- return true;
-}
-
std::string FloatLiteral::to_str() const {
return std::to_string(value_);
}
diff --git a/src/ast/float_literal.h b/src/ast/float_literal.h
index d8b69e4..411deaa 100644
--- a/src/ast/float_literal.h
+++ b/src/ast/float_literal.h
@@ -31,9 +31,6 @@
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_; }
diff --git a/src/ast/float_literal_test.cc b/src/ast/float_literal_test.cc
index cc5d55f..568df88 100644
--- a/src/ast/float_literal_test.cc
+++ b/src/ast/float_literal_test.cc
@@ -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 @@
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) {
diff --git a/src/ast/int_literal.cc b/src/ast/int_literal.cc
index 20480d2..bb86821 100644
--- a/src/ast/int_literal.cc
+++ b/src/ast/int_literal.cc
@@ -21,9 +21,5 @@
IntLiteral::~IntLiteral() = default;
-bool IntLiteral::IsInt() const {
- return true;
-}
-
} // namespace ast
} // namespace tint
diff --git a/src/ast/int_literal.h b/src/ast/int_literal.h
index d23fceb..e2f5c66 100644
--- a/src/ast/int_literal.h
+++ b/src/ast/int_literal.h
@@ -27,9 +27,6 @@
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
diff --git a/src/ast/int_literal_test.cc b/src/ast/int_literal_test.cc
index 8b2190e..f002d49 100644
--- a/src/ast/int_literal_test.cc
+++ b/src/ast/int_literal_test.cc
@@ -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 @@
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
diff --git a/src/ast/literal.cc b/src/ast/literal.cc
index 35733c7..9b7b892 100644
--- a/src/ast/literal.cc
+++ b/src/ast/literal.cc
@@ -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() = 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;
}
diff --git a/src/ast/literal.h b/src/ast/literal.h
index b8c1b7c..4e3ff0f 100644
--- a/src/ast/literal.h
+++ b/src/ast/literal.h
@@ -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_; }
diff --git a/src/ast/null_literal.cc b/src/ast/null_literal.cc
index 3c759ed..54b9344 100644
--- a/src/ast/null_literal.cc
+++ b/src/ast/null_literal.cc
@@ -21,10 +21,6 @@
NullLiteral::~NullLiteral() = default;
-bool NullLiteral::IsNull() const {
- return true;
-}
-
std::string NullLiteral::to_str() const {
return "null " + type()->type_name();
}
diff --git a/src/ast/null_literal.h b/src/ast/null_literal.h
index 0c396c9..93b5f89 100644
--- a/src/ast/null_literal.h
+++ b/src/ast/null_literal.h
@@ -30,9 +30,6 @@
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;
diff --git a/src/ast/null_literal_test.cc b/src/ast/null_literal_test.cc
index 2ede9c3..d45b3e2 100644
--- a/src/ast/null_literal_test.cc
+++ b/src/ast/null_literal_test.cc
@@ -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 @@
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) {
diff --git a/src/ast/sint_literal.cc b/src/ast/sint_literal.cc
index 6a5da13..ab44c21 100644
--- a/src/ast/sint_literal.cc
+++ b/src/ast/sint_literal.cc
@@ -22,10 +22,6 @@
SintLiteral::~SintLiteral() = default;
-bool SintLiteral::IsSint() const {
- return true;
-}
-
std::string SintLiteral::to_str() const {
return std::to_string(value_);
}
diff --git a/src/ast/sint_literal.h b/src/ast/sint_literal.h
index 0e4db04..90e78c3 100644
--- a/src/ast/sint_literal.h
+++ b/src/ast/sint_literal.h
@@ -31,9 +31,6 @@
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; }
diff --git a/src/ast/sint_literal_test.cc b/src/ast/sint_literal_test.cc
index 9e42a5c..cc49ce6 100644
--- a/src/ast/sint_literal_test.cc
+++ b/src/ast/sint_literal_test.cc
@@ -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 @@
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) {
diff --git a/src/ast/uint_literal.cc b/src/ast/uint_literal.cc
index 4548a4f..201f17d 100644
--- a/src/ast/uint_literal.cc
+++ b/src/ast/uint_literal.cc
@@ -22,10 +22,6 @@
UintLiteral::~UintLiteral() = default;
-bool UintLiteral::IsUint() const {
- return true;
-}
-
std::string UintLiteral::to_str() const {
return std::to_string(value_);
}
diff --git a/src/ast/uint_literal.h b/src/ast/uint_literal.h
index f15642a..56936ee 100644
--- a/src/ast/uint_literal.h
+++ b/src/ast/uint_literal.h
@@ -31,9 +31,6 @@
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; }
diff --git a/src/ast/uint_literal_test.cc b/src/ast/uint_literal_test.cc
index df60554..b066bca 100644
--- a/src/ast/uint_literal_test.cc
+++ b/src/ast/uint_literal_test.cc
@@ -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 @@
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) {