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) {

View File

@ -145,23 +145,23 @@ std::map<uint32_t, Scalar> Inspector::GetConstantIDs() {
continue;
}
if (literal->IsBool()) {
result[constant_id] = Scalar(literal->AsBool()->IsTrue());
if (literal->Is<ast::BoolLiteral>()) {
result[constant_id] = Scalar(literal->As<ast::BoolLiteral>()->IsTrue());
continue;
}
if (literal->IsUint()) {
result[constant_id] = Scalar(literal->AsUint()->value());
if (literal->Is<ast::UintLiteral>()) {
result[constant_id] = Scalar(literal->As<ast::UintLiteral>()->value());
continue;
}
if (literal->IsSint()) {
result[constant_id] = Scalar(literal->AsSint()->value());
if (literal->Is<ast::SintLiteral>()) {
result[constant_id] = Scalar(literal->As<ast::SintLiteral>()->value());
continue;
}
if (literal->IsFloat()) {
result[constant_id] = Scalar(literal->AsFloat()->value());
if (literal->Is<ast::FloatLiteral>()) {
result[constant_id] = Scalar(literal->As<ast::FloatLiteral>()->value());
continue;
}

View File

@ -1753,10 +1753,10 @@ Expect<ast::CaseSelectorList> ParserImpl::expect_case_selectors() {
return Failure::kErrored;
if (!cond.matched)
break;
if (!cond->IsInt())
if (!cond->Is<ast::IntLiteral>())
return add_error(t, "invalid case selector must be an integer value");
selectors.push_back(cond.value->AsInt());
selectors.push_back(cond.value->As<ast::IntLiteral>());
}
if (selectors.empty())

View File

@ -44,8 +44,8 @@ TEST_F(ParserImplTest, AdditiveExpression_Parses_Plus) {
ASSERT_TRUE(rel->rhs()->Is<ast::ConstructorExpression>());
ASSERT_TRUE(rel->rhs()->Is<ast::ScalarConstructorExpression>());
auto* init = rel->rhs()->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(init->literal()->IsBool());
ASSERT_TRUE(init->literal()->AsBool()->IsTrue());
ASSERT_TRUE(init->literal()->Is<ast::BoolLiteral>());
ASSERT_TRUE(init->literal()->As<ast::BoolLiteral>()->IsTrue());
}
TEST_F(ParserImplTest, AdditiveExpression_Parses_Minus) {
@ -67,8 +67,8 @@ TEST_F(ParserImplTest, AdditiveExpression_Parses_Minus) {
ASSERT_TRUE(rel->rhs()->Is<ast::ConstructorExpression>());
ASSERT_TRUE(rel->rhs()->Is<ast::ScalarConstructorExpression>());
auto* init = rel->rhs()->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(init->literal()->IsBool());
ASSERT_TRUE(init->literal()->AsBool()->IsTrue());
ASSERT_TRUE(init->literal()->Is<ast::BoolLiteral>());
ASSERT_TRUE(init->literal()->As<ast::BoolLiteral>()->IsTrue());
}
TEST_F(ParserImplTest, AdditiveExpression_InvalidLHS) {

View File

@ -44,8 +44,8 @@ TEST_F(ParserImplTest, AndExpression_Parses) {
ASSERT_TRUE(rel->rhs()->Is<ast::ConstructorExpression>());
ASSERT_TRUE(rel->rhs()->Is<ast::ScalarConstructorExpression>());
auto* init = rel->rhs()->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(init->literal()->IsBool());
ASSERT_TRUE(init->literal()->AsBool()->IsTrue());
ASSERT_TRUE(init->literal()->Is<ast::BoolLiteral>());
ASSERT_TRUE(init->literal()->As<ast::BoolLiteral>()->IsTrue());
}
TEST_F(ParserImplTest, AndExpression_InvalidLHS) {

View File

@ -49,8 +49,8 @@ TEST_F(ParserImplTest, AssignmentStmt_Parses_ToVariable) {
auto* init = e->rhs()->As<ast::ScalarConstructorExpression>();
ASSERT_NE(init->literal(), nullptr);
ASSERT_TRUE(init->literal()->IsSint());
EXPECT_EQ(init->literal()->AsSint()->value(), 123);
ASSERT_TRUE(init->literal()->Is<ast::SintLiteral>());
EXPECT_EQ(init->literal()->As<ast::SintLiteral>()->value(), 123);
}
TEST_F(ParserImplTest, AssignmentStmt_Parses_ToMember) {
@ -69,8 +69,8 @@ TEST_F(ParserImplTest, AssignmentStmt_Parses_ToMember) {
ASSERT_TRUE(e->rhs()->Is<ast::ScalarConstructorExpression>());
auto* init = e->rhs()->As<ast::ScalarConstructorExpression>();
ASSERT_NE(init->literal(), nullptr);
ASSERT_TRUE(init->literal()->IsSint());
EXPECT_EQ(init->literal()->AsSint()->value(), 123);
ASSERT_TRUE(init->literal()->Is<ast::SintLiteral>());
EXPECT_EQ(init->literal()->As<ast::SintLiteral>()->value(), 123);
ASSERT_TRUE(e->lhs()->Is<ast::MemberAccessorExpression>());
auto* mem = e->lhs()->As<ast::MemberAccessorExpression>();
@ -86,8 +86,8 @@ TEST_F(ParserImplTest, AssignmentStmt_Parses_ToMember) {
ASSERT_TRUE(ary->idx_expr()->Is<ast::ScalarConstructorExpression>());
init = ary->idx_expr()->As<ast::ScalarConstructorExpression>();
ASSERT_NE(init->literal(), nullptr);
ASSERT_TRUE(init->literal()->IsSint());
EXPECT_EQ(init->literal()->AsSint()->value(), 2);
ASSERT_TRUE(init->literal()->Is<ast::SintLiteral>());
EXPECT_EQ(init->literal()->As<ast::SintLiteral>()->value(), 2);
ASSERT_TRUE(ary->array()->Is<ast::MemberAccessorExpression>());
mem = ary->array()->As<ast::MemberAccessorExpression>();

View File

@ -44,14 +44,14 @@ TEST_F(ParserImplTest, ConstExpr_TypeDecl) {
ASSERT_TRUE(v[0]->Is<ast::ConstructorExpression>());
ASSERT_TRUE(v[0]->Is<ast::ScalarConstructorExpression>());
auto* c = v[0]->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(c->literal()->IsFloat());
EXPECT_FLOAT_EQ(c->literal()->AsFloat()->value(), 1.);
ASSERT_TRUE(c->literal()->Is<ast::FloatLiteral>());
EXPECT_FLOAT_EQ(c->literal()->As<ast::FloatLiteral>()->value(), 1.);
ASSERT_TRUE(v[1]->Is<ast::ConstructorExpression>());
ASSERT_TRUE(v[1]->Is<ast::ScalarConstructorExpression>());
c = v[1]->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(c->literal()->IsFloat());
EXPECT_FLOAT_EQ(c->literal()->AsFloat()->value(), 2.);
ASSERT_TRUE(c->literal()->Is<ast::FloatLiteral>());
EXPECT_FLOAT_EQ(c->literal()->As<ast::FloatLiteral>()->value(), 2.);
}
TEST_F(ParserImplTest, ConstExpr_TypeDecl_MissingRightParen) {
@ -117,8 +117,8 @@ TEST_F(ParserImplTest, ConstExpr_ConstLiteral) {
ASSERT_TRUE(e->Is<ast::ConstructorExpression>());
ASSERT_TRUE(e->Is<ast::ScalarConstructorExpression>());
auto* c = e->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(c->literal()->IsBool());
EXPECT_TRUE(c->literal()->AsBool()->IsTrue());
ASSERT_TRUE(c->literal()->Is<ast::BoolLiteral>());
EXPECT_TRUE(c->literal()->As<ast::BoolLiteral>()->IsTrue());
}
TEST_F(ParserImplTest, ConstExpr_ConstLiteral_Invalid) {

View File

@ -32,8 +32,8 @@ TEST_F(ParserImplTest, ConstLiteral_Int) {
EXPECT_FALSE(c.errored);
EXPECT_FALSE(p->has_error());
ASSERT_NE(c.value, nullptr);
ASSERT_TRUE(c->IsSint());
EXPECT_EQ(c->AsSint()->value(), -234);
ASSERT_TRUE(c->Is<ast::SintLiteral>());
EXPECT_EQ(c->As<ast::SintLiteral>()->value(), -234);
}
TEST_F(ParserImplTest, ConstLiteral_Uint) {
@ -43,8 +43,8 @@ TEST_F(ParserImplTest, ConstLiteral_Uint) {
EXPECT_FALSE(c.errored);
EXPECT_FALSE(p->has_error());
ASSERT_NE(c.value, nullptr);
ASSERT_TRUE(c->IsUint());
EXPECT_EQ(c->AsUint()->value(), 234u);
ASSERT_TRUE(c->Is<ast::UintLiteral>());
EXPECT_EQ(c->As<ast::UintLiteral>()->value(), 234u);
}
TEST_F(ParserImplTest, ConstLiteral_Float) {
@ -54,8 +54,8 @@ TEST_F(ParserImplTest, ConstLiteral_Float) {
EXPECT_FALSE(c.errored);
EXPECT_FALSE(p->has_error());
ASSERT_NE(c.value, nullptr);
ASSERT_TRUE(c->IsFloat());
EXPECT_FLOAT_EQ(c->AsFloat()->value(), 234e12f);
ASSERT_TRUE(c->Is<ast::FloatLiteral>());
EXPECT_FLOAT_EQ(c->As<ast::FloatLiteral>()->value(), 234e12f);
}
TEST_F(ParserImplTest, ConstLiteral_InvalidFloat) {
@ -73,8 +73,8 @@ TEST_F(ParserImplTest, ConstLiteral_True) {
EXPECT_FALSE(c.errored);
EXPECT_FALSE(p->has_error());
ASSERT_NE(c.value, nullptr);
ASSERT_TRUE(c->IsBool());
EXPECT_TRUE(c->AsBool()->IsTrue());
ASSERT_TRUE(c->Is<ast::BoolLiteral>());
EXPECT_TRUE(c->As<ast::BoolLiteral>()->IsTrue());
}
TEST_F(ParserImplTest, ConstLiteral_False) {
@ -84,8 +84,8 @@ TEST_F(ParserImplTest, ConstLiteral_False) {
EXPECT_FALSE(c.errored);
EXPECT_FALSE(p->has_error());
ASSERT_NE(c.value, nullptr);
ASSERT_TRUE(c->IsBool());
EXPECT_TRUE(c->AsBool()->IsFalse());
ASSERT_TRUE(c->Is<ast::BoolLiteral>());
EXPECT_TRUE(c->As<ast::BoolLiteral>()->IsFalse());
}
TEST_F(ParserImplTest, ConstLiteral_NoMatch) {

View File

@ -44,8 +44,8 @@ TEST_F(ParserImplTest, EqualityExpression_Parses_Equal) {
ASSERT_TRUE(rel->rhs()->Is<ast::ConstructorExpression>());
ASSERT_TRUE(rel->rhs()->Is<ast::ScalarConstructorExpression>());
auto* init = rel->rhs()->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(init->literal()->IsBool());
ASSERT_TRUE(init->literal()->AsBool()->IsTrue());
ASSERT_TRUE(init->literal()->Is<ast::BoolLiteral>());
ASSERT_TRUE(init->literal()->As<ast::BoolLiteral>()->IsTrue());
}
TEST_F(ParserImplTest, EqualityExpression_Parses_NotEqual) {
@ -67,8 +67,8 @@ TEST_F(ParserImplTest, EqualityExpression_Parses_NotEqual) {
ASSERT_TRUE(rel->rhs()->Is<ast::ConstructorExpression>());
ASSERT_TRUE(rel->rhs()->Is<ast::ScalarConstructorExpression>());
auto* init = rel->rhs()->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(init->literal()->IsBool());
ASSERT_TRUE(init->literal()->AsBool()->IsTrue());
ASSERT_TRUE(init->literal()->Is<ast::BoolLiteral>());
ASSERT_TRUE(init->literal()->As<ast::BoolLiteral>()->IsTrue());
}
TEST_F(ParserImplTest, EqualityExpression_InvalidLHS) {

View File

@ -44,8 +44,8 @@ TEST_F(ParserImplTest, ExclusiveOrExpression_Parses) {
ASSERT_TRUE(rel->rhs()->Is<ast::ConstructorExpression>());
ASSERT_TRUE(rel->rhs()->Is<ast::ScalarConstructorExpression>());
auto* init = rel->rhs()->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(init->literal()->IsBool());
ASSERT_TRUE(init->literal()->AsBool()->IsTrue());
ASSERT_TRUE(init->literal()->Is<ast::BoolLiteral>());
ASSERT_TRUE(init->literal()->As<ast::BoolLiteral>()->IsTrue());
}
TEST_F(ParserImplTest, ExclusiveOrExpression_InvalidLHS) {

View File

@ -44,8 +44,8 @@ TEST_F(ParserImplTest, InclusiveOrExpression_Parses) {
ASSERT_TRUE(rel->rhs()->Is<ast::ConstructorExpression>());
ASSERT_TRUE(rel->rhs()->Is<ast::ScalarConstructorExpression>());
auto* init = rel->rhs()->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(init->literal()->IsBool());
ASSERT_TRUE(init->literal()->AsBool()->IsTrue());
ASSERT_TRUE(init->literal()->Is<ast::BoolLiteral>());
ASSERT_TRUE(init->literal()->As<ast::BoolLiteral>()->IsTrue());
}
TEST_F(ParserImplTest, InclusiveOrExpression_InvalidLHS) {

View File

@ -44,8 +44,8 @@ TEST_F(ParserImplTest, LogicalAndExpression_Parses) {
ASSERT_TRUE(rel->rhs()->Is<ast::ConstructorExpression>());
ASSERT_TRUE(rel->rhs()->Is<ast::ScalarConstructorExpression>());
auto* init = rel->rhs()->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(init->literal()->IsBool());
ASSERT_TRUE(init->literal()->AsBool()->IsTrue());
ASSERT_TRUE(init->literal()->Is<ast::BoolLiteral>());
ASSERT_TRUE(init->literal()->As<ast::BoolLiteral>()->IsTrue());
}
TEST_F(ParserImplTest, LogicalAndExpression_InvalidLHS) {

View File

@ -44,8 +44,8 @@ TEST_F(ParserImplTest, LogicalOrExpression_Parses) {
ASSERT_TRUE(rel->rhs()->Is<ast::ConstructorExpression>());
ASSERT_TRUE(rel->rhs()->Is<ast::ScalarConstructorExpression>());
auto* init = rel->rhs()->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(init->literal()->IsBool());
ASSERT_TRUE(init->literal()->AsBool()->IsTrue());
ASSERT_TRUE(init->literal()->Is<ast::BoolLiteral>());
ASSERT_TRUE(init->literal()->As<ast::BoolLiteral>()->IsTrue());
}
TEST_F(ParserImplTest, LogicalOrExpression_InvalidLHS) {

View File

@ -44,8 +44,8 @@ TEST_F(ParserImplTest, MultiplicativeExpression_Parses_Multiply) {
ASSERT_TRUE(rel->rhs()->Is<ast::ConstructorExpression>());
ASSERT_TRUE(rel->rhs()->Is<ast::ScalarConstructorExpression>());
auto* init = rel->rhs()->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(init->literal()->IsBool());
ASSERT_TRUE(init->literal()->AsBool()->IsTrue());
ASSERT_TRUE(init->literal()->Is<ast::BoolLiteral>());
ASSERT_TRUE(init->literal()->As<ast::BoolLiteral>()->IsTrue());
}
TEST_F(ParserImplTest, MultiplicativeExpression_Parses_Divide) {
@ -67,8 +67,8 @@ TEST_F(ParserImplTest, MultiplicativeExpression_Parses_Divide) {
ASSERT_TRUE(rel->rhs()->Is<ast::ConstructorExpression>());
ASSERT_TRUE(rel->rhs()->Is<ast::ScalarConstructorExpression>());
auto* init = rel->rhs()->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(init->literal()->IsBool());
ASSERT_TRUE(init->literal()->AsBool()->IsTrue());
ASSERT_TRUE(init->literal()->Is<ast::BoolLiteral>());
ASSERT_TRUE(init->literal()->As<ast::BoolLiteral>()->IsTrue());
}
TEST_F(ParserImplTest, MultiplicativeExpression_Parses_Modulo) {
@ -90,8 +90,8 @@ TEST_F(ParserImplTest, MultiplicativeExpression_Parses_Modulo) {
ASSERT_TRUE(rel->rhs()->Is<ast::ConstructorExpression>());
ASSERT_TRUE(rel->rhs()->Is<ast::ScalarConstructorExpression>());
auto* init = rel->rhs()->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(init->literal()->IsBool());
ASSERT_TRUE(init->literal()->AsBool()->IsTrue());
ASSERT_TRUE(init->literal()->Is<ast::BoolLiteral>());
ASSERT_TRUE(init->literal()->As<ast::BoolLiteral>()->IsTrue());
}
TEST_F(ParserImplTest, MultiplicativeExpression_InvalidLHS) {

View File

@ -47,8 +47,8 @@ TEST_F(ParserImplTest, PostfixExpression_Array_ConstantIndex) {
ASSERT_TRUE(ary->idx_expr()->Is<ast::ConstructorExpression>());
ASSERT_TRUE(ary->idx_expr()->Is<ast::ScalarConstructorExpression>());
auto* c = ary->idx_expr()->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(c->literal()->IsSint());
EXPECT_EQ(c->literal()->AsSint()->value(), 1);
ASSERT_TRUE(c->literal()->Is<ast::SintLiteral>());
EXPECT_EQ(c->literal()->As<ast::SintLiteral>()->value(), 1);
}
TEST_F(ParserImplTest, PostfixExpression_Array_ExpressionIndex) {

View File

@ -60,26 +60,26 @@ TEST_F(ParserImplTest, PrimaryExpression_TypeDecl) {
ASSERT_TRUE(val[0]->Is<ast::ConstructorExpression>());
ASSERT_TRUE(val[0]->Is<ast::ScalarConstructorExpression>());
auto* ident = val[0]->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(ident->literal()->IsSint());
EXPECT_EQ(ident->literal()->AsSint()->value(), 1);
ASSERT_TRUE(ident->literal()->Is<ast::SintLiteral>());
EXPECT_EQ(ident->literal()->As<ast::SintLiteral>()->value(), 1);
ASSERT_TRUE(val[1]->Is<ast::ConstructorExpression>());
ASSERT_TRUE(val[1]->Is<ast::ScalarConstructorExpression>());
ident = val[1]->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(ident->literal()->IsSint());
EXPECT_EQ(ident->literal()->AsSint()->value(), 2);
ASSERT_TRUE(ident->literal()->Is<ast::SintLiteral>());
EXPECT_EQ(ident->literal()->As<ast::SintLiteral>()->value(), 2);
ASSERT_TRUE(val[2]->Is<ast::ConstructorExpression>());
ASSERT_TRUE(val[2]->Is<ast::ScalarConstructorExpression>());
ident = val[2]->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(ident->literal()->IsSint());
EXPECT_EQ(ident->literal()->AsSint()->value(), 3);
ASSERT_TRUE(ident->literal()->Is<ast::SintLiteral>());
EXPECT_EQ(ident->literal()->As<ast::SintLiteral>()->value(), 3);
ASSERT_TRUE(val[3]->Is<ast::ConstructorExpression>());
ASSERT_TRUE(val[3]->Is<ast::ScalarConstructorExpression>());
ident = val[3]->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(ident->literal()->IsSint());
EXPECT_EQ(ident->literal()->AsSint()->value(), 4);
ASSERT_TRUE(ident->literal()->Is<ast::SintLiteral>());
EXPECT_EQ(ident->literal()->As<ast::SintLiteral>()->value(), 4);
}
TEST_F(ParserImplTest, PrimaryExpression_TypeDecl_ZeroConstructor) {
@ -146,8 +146,8 @@ TEST_F(ParserImplTest, PrimaryExpression_ConstLiteral_True) {
ASSERT_TRUE(e->Is<ast::ConstructorExpression>());
ASSERT_TRUE(e->Is<ast::ScalarConstructorExpression>());
auto* init = e->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(init->literal()->IsBool());
EXPECT_TRUE(init->literal()->AsBool()->IsTrue());
ASSERT_TRUE(init->literal()->Is<ast::BoolLiteral>());
EXPECT_TRUE(init->literal()->As<ast::BoolLiteral>()->IsTrue());
}
TEST_F(ParserImplTest, PrimaryExpression_ParenExpr) {

View File

@ -44,8 +44,8 @@ TEST_F(ParserImplTest, RelationalExpression_Parses_LessThan) {
ASSERT_TRUE(rel->rhs()->Is<ast::ConstructorExpression>());
ASSERT_TRUE(rel->rhs()->Is<ast::ScalarConstructorExpression>());
auto* init = rel->rhs()->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(init->literal()->IsBool());
ASSERT_TRUE(init->literal()->AsBool()->IsTrue());
ASSERT_TRUE(init->literal()->Is<ast::BoolLiteral>());
ASSERT_TRUE(init->literal()->As<ast::BoolLiteral>()->IsTrue());
}
TEST_F(ParserImplTest, RelationalExpression_Parses_GreaterThan) {
@ -67,8 +67,8 @@ TEST_F(ParserImplTest, RelationalExpression_Parses_GreaterThan) {
ASSERT_TRUE(rel->rhs()->Is<ast::ConstructorExpression>());
ASSERT_TRUE(rel->rhs()->Is<ast::ScalarConstructorExpression>());
auto* init = rel->rhs()->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(init->literal()->IsBool());
ASSERT_TRUE(init->literal()->AsBool()->IsTrue());
ASSERT_TRUE(init->literal()->Is<ast::BoolLiteral>());
ASSERT_TRUE(init->literal()->As<ast::BoolLiteral>()->IsTrue());
}
TEST_F(ParserImplTest, RelationalExpression_Parses_LessThanEqual) {
@ -90,8 +90,8 @@ TEST_F(ParserImplTest, RelationalExpression_Parses_LessThanEqual) {
ASSERT_TRUE(rel->rhs()->Is<ast::ConstructorExpression>());
ASSERT_TRUE(rel->rhs()->Is<ast::ScalarConstructorExpression>());
auto* init = rel->rhs()->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(init->literal()->IsBool());
ASSERT_TRUE(init->literal()->AsBool()->IsTrue());
ASSERT_TRUE(init->literal()->Is<ast::BoolLiteral>());
ASSERT_TRUE(init->literal()->As<ast::BoolLiteral>()->IsTrue());
}
TEST_F(ParserImplTest, RelationalExpression_Parses_GreaterThanEqual) {
@ -113,8 +113,8 @@ TEST_F(ParserImplTest, RelationalExpression_Parses_GreaterThanEqual) {
ASSERT_TRUE(rel->rhs()->Is<ast::ConstructorExpression>());
ASSERT_TRUE(rel->rhs()->Is<ast::ScalarConstructorExpression>());
auto* init = rel->rhs()->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(init->literal()->IsBool());
ASSERT_TRUE(init->literal()->AsBool()->IsTrue());
ASSERT_TRUE(init->literal()->Is<ast::BoolLiteral>());
ASSERT_TRUE(init->literal()->As<ast::BoolLiteral>()->IsTrue());
}
TEST_F(ParserImplTest, RelationalExpression_InvalidLHS) {

View File

@ -44,8 +44,8 @@ TEST_F(ParserImplTest, ShiftExpression_Parses_ShiftLeft) {
ASSERT_TRUE(rel->rhs()->Is<ast::ConstructorExpression>());
ASSERT_TRUE(rel->rhs()->Is<ast::ScalarConstructorExpression>());
auto* init = rel->rhs()->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(init->literal()->IsBool());
ASSERT_TRUE(init->literal()->AsBool()->IsTrue());
ASSERT_TRUE(init->literal()->Is<ast::BoolLiteral>());
ASSERT_TRUE(init->literal()->As<ast::BoolLiteral>()->IsTrue());
}
TEST_F(ParserImplTest, ShiftExpression_Parses_ShiftRight) {
@ -67,8 +67,8 @@ TEST_F(ParserImplTest, ShiftExpression_Parses_ShiftRight) {
ASSERT_TRUE(rel->rhs()->Is<ast::ConstructorExpression>());
ASSERT_TRUE(rel->rhs()->Is<ast::ScalarConstructorExpression>());
auto* init = rel->rhs()->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(init->literal()->IsBool());
ASSERT_TRUE(init->literal()->AsBool()->IsTrue());
ASSERT_TRUE(init->literal()->Is<ast::BoolLiteral>());
ASSERT_TRUE(init->literal()->As<ast::BoolLiteral>()->IsTrue());
}
TEST_F(ParserImplTest, ShiftExpression_InvalidLHS) {

View File

@ -43,8 +43,8 @@ TEST_F(ParserImplTest, UnaryExpression_Postix) {
ASSERT_TRUE(ary->idx_expr()->Is<ast::ConstructorExpression>());
ASSERT_TRUE(ary->idx_expr()->Is<ast::ScalarConstructorExpression>());
auto* init = ary->idx_expr()->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(init->literal()->IsSint());
ASSERT_EQ(init->literal()->AsSint()->value(), 2);
ASSERT_TRUE(init->literal()->Is<ast::SintLiteral>());
ASSERT_EQ(init->literal()->As<ast::SintLiteral>()->value(), 2);
}
TEST_F(ParserImplTest, UnaryExpression_Minus) {
@ -63,8 +63,8 @@ TEST_F(ParserImplTest, UnaryExpression_Minus) {
ASSERT_TRUE(u->expr()->Is<ast::ScalarConstructorExpression>());
auto* init = u->expr()->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(init->literal()->IsSint());
EXPECT_EQ(init->literal()->AsSint()->value(), 1);
ASSERT_TRUE(init->literal()->Is<ast::SintLiteral>());
EXPECT_EQ(init->literal()->As<ast::SintLiteral>()->value(), 1);
}
TEST_F(ParserImplTest, UnaryExpression_Minus_InvalidRHS) {
@ -93,8 +93,8 @@ TEST_F(ParserImplTest, UnaryExpression_Bang) {
ASSERT_TRUE(u->expr()->Is<ast::ScalarConstructorExpression>());
auto* init = u->expr()->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(init->literal()->IsSint());
EXPECT_EQ(init->literal()->AsSint()->value(), 1);
ASSERT_TRUE(init->literal()->Is<ast::SintLiteral>());
EXPECT_EQ(init->literal()->As<ast::SintLiteral>()->value(), 1);
}
TEST_F(ParserImplTest, UnaryExpression_Bang_InvalidRHS) {

View File

@ -215,20 +215,20 @@ bool BoundArrayAccessorsTransform::ProcessAccessExpression(
// Scalar constructor we can re-write the value to be within bounds.
if (auto* c = expr->idx_expr()->As<ast::ScalarConstructorExpression>()) {
auto* lit = c->literal();
if (lit->IsSint()) {
int32_t val = lit->AsSint()->value();
if (lit->Is<ast::SintLiteral>()) {
int32_t val = lit->As<ast::SintLiteral>()->value();
if (val < 0) {
val = 0;
} else if (val >= int32_t(size)) {
val = int32_t(size) - 1;
}
lit->AsSint()->set_value(val);
} else if (lit->IsUint()) {
uint32_t val = lit->AsUint()->value();
lit->As<ast::SintLiteral>()->set_value(val);
} else if (lit->Is<ast::UintLiteral>()) {
uint32_t val = lit->As<ast::UintLiteral>()->value();
if (val >= size - 1) {
val = size - 1;
}
lit->AsUint()->set_value(val);
lit->As<ast::UintLiteral>()->set_value(val);
} else {
error_ = "unknown scalar constructor type for accessor";
return false;

View File

@ -148,8 +148,8 @@ TEST_F(BoundArrayAccessorsTest, Ptrs_Clamp) {
ASSERT_TRUE(idx->params()[1]->Is<ast::ConstructorExpression>());
ASSERT_TRUE(idx->params()[1]->Is<ast::ScalarConstructorExpression>());
auto* scalar = idx->params()[1]->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(scalar->literal()->IsUint());
EXPECT_EQ(scalar->literal()->AsUint()->value(), 2u);
ASSERT_TRUE(scalar->literal()->Is<ast::UintLiteral>());
EXPECT_EQ(scalar->literal()->As<ast::UintLiteral>()->value(), 2u);
ASSERT_NE(ptr->idx_expr()->result_type(), nullptr);
ASSERT_TRUE(ptr->idx_expr()->result_type()->Is<ast::type::U32Type>());
@ -229,14 +229,14 @@ TEST_F(BoundArrayAccessorsTest, Array_Idx_Nested_Scalar) {
ASSERT_TRUE(sub_idx->params()[1]->Is<ast::ConstructorExpression>());
ASSERT_TRUE(sub_idx->params()[1]->Is<ast::ScalarConstructorExpression>());
auto* scalar = sub_idx->params()[1]->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(scalar->literal()->IsUint());
EXPECT_EQ(scalar->literal()->AsUint()->value(), 4u);
ASSERT_TRUE(scalar->literal()->Is<ast::UintLiteral>());
EXPECT_EQ(scalar->literal()->As<ast::UintLiteral>()->value(), 4u);
ASSERT_TRUE(idx->params()[1]->Is<ast::ConstructorExpression>());
ASSERT_TRUE(idx->params()[1]->Is<ast::ScalarConstructorExpression>());
scalar = idx->params()[1]->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(scalar->literal()->IsUint());
EXPECT_EQ(scalar->literal()->AsUint()->value(), 2u);
ASSERT_TRUE(scalar->literal()->Is<ast::UintLiteral>());
EXPECT_EQ(scalar->literal()->As<ast::UintLiteral>()->value(), 2u);
ASSERT_NE(ptr->idx_expr()->result_type(), nullptr);
ASSERT_TRUE(ptr->idx_expr()->result_type()->Is<ast::type::U32Type>());
@ -274,8 +274,8 @@ TEST_F(BoundArrayAccessorsTest, Array_Idx_Scalar) {
ASSERT_TRUE(ptr->idx_expr()->Is<ast::ScalarConstructorExpression>());
auto* scalar = ptr->idx_expr()->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(scalar->literal()->IsUint());
EXPECT_EQ(scalar->literal()->AsUint()->value(), 1u);
ASSERT_TRUE(scalar->literal()->Is<ast::UintLiteral>());
EXPECT_EQ(scalar->literal()->As<ast::UintLiteral>()->value(), 1u);
ASSERT_NE(ptr->idx_expr()->result_type(), nullptr);
ASSERT_TRUE(ptr->idx_expr()->result_type()->Is<ast::type::U32Type>());
@ -336,8 +336,8 @@ TEST_F(BoundArrayAccessorsTest, Array_Idx_Expr) {
ASSERT_TRUE(idx->params()[1]->Is<ast::ConstructorExpression>());
ASSERT_TRUE(idx->params()[1]->Is<ast::ScalarConstructorExpression>());
auto* scalar = idx->params()[1]->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(scalar->literal()->IsUint());
EXPECT_EQ(scalar->literal()->AsUint()->value(), 2u);
ASSERT_TRUE(scalar->literal()->Is<ast::UintLiteral>());
EXPECT_EQ(scalar->literal()->As<ast::UintLiteral>()->value(), 2u);
ASSERT_NE(ptr->idx_expr()->result_type(), nullptr);
ASSERT_TRUE(ptr->idx_expr()->result_type()->Is<ast::type::U32Type>());
@ -375,8 +375,8 @@ TEST_F(BoundArrayAccessorsTest, Array_Idx_Negative) {
ASSERT_TRUE(ptr->idx_expr()->Is<ast::ScalarConstructorExpression>());
auto* scalar = ptr->idx_expr()->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(scalar->literal()->IsSint());
EXPECT_EQ(scalar->literal()->AsSint()->value(), 0);
ASSERT_TRUE(scalar->literal()->Is<ast::SintLiteral>());
EXPECT_EQ(scalar->literal()->As<ast::SintLiteral>()->value(), 0);
ASSERT_NE(ptr->idx_expr()->result_type(), nullptr);
ASSERT_TRUE(ptr->idx_expr()->result_type()->Is<ast::type::I32Type>());
@ -414,8 +414,8 @@ TEST_F(BoundArrayAccessorsTest, Array_Idx_OutOfBounds) {
ASSERT_TRUE(ptr->idx_expr()->Is<ast::ScalarConstructorExpression>());
auto* scalar = ptr->idx_expr()->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(scalar->literal()->IsUint());
EXPECT_EQ(scalar->literal()->AsUint()->value(), 2u);
ASSERT_TRUE(scalar->literal()->Is<ast::UintLiteral>());
EXPECT_EQ(scalar->literal()->As<ast::UintLiteral>()->value(), 2u);
ASSERT_NE(ptr->idx_expr()->result_type(), nullptr);
ASSERT_TRUE(ptr->idx_expr()->result_type()->Is<ast::type::U32Type>());
@ -453,8 +453,8 @@ TEST_F(BoundArrayAccessorsTest, Vector_Idx_Scalar) {
ASSERT_TRUE(ptr->idx_expr()->Is<ast::ScalarConstructorExpression>());
auto* scalar = ptr->idx_expr()->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(scalar->literal()->IsUint());
EXPECT_EQ(scalar->literal()->AsUint()->value(), 1u);
ASSERT_TRUE(scalar->literal()->Is<ast::UintLiteral>());
EXPECT_EQ(scalar->literal()->As<ast::UintLiteral>()->value(), 1u);
ASSERT_NE(ptr->idx_expr()->result_type(), nullptr);
ASSERT_TRUE(ptr->idx_expr()->result_type()->Is<ast::type::U32Type>());
@ -514,8 +514,8 @@ TEST_F(BoundArrayAccessorsTest, Vector_Idx_Expr) {
ASSERT_TRUE(idx->params()[1]->Is<ast::ConstructorExpression>());
ASSERT_TRUE(idx->params()[1]->Is<ast::ScalarConstructorExpression>());
auto* scalar = idx->params()[1]->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(scalar->literal()->IsUint());
EXPECT_EQ(scalar->literal()->AsUint()->value(), 2u);
ASSERT_TRUE(scalar->literal()->Is<ast::UintLiteral>());
EXPECT_EQ(scalar->literal()->As<ast::UintLiteral>()->value(), 2u);
ASSERT_NE(ptr->idx_expr()->result_type(), nullptr);
ASSERT_TRUE(ptr->idx_expr()->result_type()->Is<ast::type::U32Type>());
@ -553,8 +553,8 @@ TEST_F(BoundArrayAccessorsTest, Vector_Idx_Negative) {
ASSERT_TRUE(ptr->idx_expr()->Is<ast::ScalarConstructorExpression>());
auto* scalar = ptr->idx_expr()->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(scalar->literal()->IsSint());
EXPECT_EQ(scalar->literal()->AsSint()->value(), 0);
ASSERT_TRUE(scalar->literal()->Is<ast::SintLiteral>());
EXPECT_EQ(scalar->literal()->As<ast::SintLiteral>()->value(), 0);
ASSERT_NE(ptr->idx_expr()->result_type(), nullptr);
ASSERT_TRUE(ptr->idx_expr()->result_type()->Is<ast::type::I32Type>());
@ -592,8 +592,8 @@ TEST_F(BoundArrayAccessorsTest, Vector_Idx_OutOfBounds) {
ASSERT_TRUE(ptr->idx_expr()->Is<ast::ScalarConstructorExpression>());
auto* scalar = ptr->idx_expr()->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(scalar->literal()->IsUint());
EXPECT_EQ(scalar->literal()->AsUint()->value(), 2u);
ASSERT_TRUE(scalar->literal()->Is<ast::UintLiteral>());
EXPECT_EQ(scalar->literal()->As<ast::UintLiteral>()->value(), 2u);
ASSERT_NE(ptr->idx_expr()->result_type(), nullptr);
ASSERT_TRUE(ptr->idx_expr()->result_type()->Is<ast::type::U32Type>());
@ -637,8 +637,8 @@ TEST_F(BoundArrayAccessorsTest, Matrix_Idx_Scalar) {
ASSERT_TRUE(ary->idx_expr()->Is<ast::ScalarConstructorExpression>());
auto* scalar = ary->idx_expr()->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(scalar->literal()->IsUint());
EXPECT_EQ(scalar->literal()->AsUint()->value(), 2u);
ASSERT_TRUE(scalar->literal()->Is<ast::UintLiteral>());
EXPECT_EQ(scalar->literal()->As<ast::UintLiteral>()->value(), 2u);
ASSERT_NE(ary->idx_expr()->result_type(), nullptr);
ASSERT_TRUE(ary->idx_expr()->result_type()->Is<ast::type::U32Type>());
@ -647,8 +647,8 @@ TEST_F(BoundArrayAccessorsTest, Matrix_Idx_Scalar) {
ASSERT_TRUE(ptr->idx_expr()->Is<ast::ScalarConstructorExpression>());
scalar = ptr->idx_expr()->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(scalar->literal()->IsUint());
EXPECT_EQ(scalar->literal()->AsUint()->value(), 1u);
ASSERT_TRUE(scalar->literal()->Is<ast::UintLiteral>());
EXPECT_EQ(scalar->literal()->As<ast::UintLiteral>()->value(), 1u);
ASSERT_NE(ptr->idx_expr()->result_type(), nullptr);
ASSERT_TRUE(ptr->idx_expr()->result_type()->Is<ast::type::U32Type>());
@ -715,8 +715,8 @@ TEST_F(BoundArrayAccessorsTest, Matrix_Idx_Expr_Column) {
ASSERT_TRUE(idx->params()[1]->Is<ast::ConstructorExpression>());
ASSERT_TRUE(idx->params()[1]->Is<ast::ScalarConstructorExpression>());
auto* scalar = idx->params()[1]->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(scalar->literal()->IsUint());
EXPECT_EQ(scalar->literal()->AsUint()->value(), 2u);
ASSERT_TRUE(scalar->literal()->Is<ast::UintLiteral>());
EXPECT_EQ(scalar->literal()->As<ast::UintLiteral>()->value(), 2u);
ASSERT_NE(ary->idx_expr()->result_type(), nullptr);
ASSERT_TRUE(ary->idx_expr()->result_type()->Is<ast::type::U32Type>());
@ -725,8 +725,8 @@ TEST_F(BoundArrayAccessorsTest, Matrix_Idx_Expr_Column) {
ASSERT_TRUE(ptr->idx_expr()->Is<ast::ScalarConstructorExpression>());
scalar = ptr->idx_expr()->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(scalar->literal()->IsUint());
EXPECT_EQ(scalar->literal()->AsUint()->value(), 1u);
ASSERT_TRUE(scalar->literal()->Is<ast::UintLiteral>());
EXPECT_EQ(scalar->literal()->As<ast::UintLiteral>()->value(), 1u);
ASSERT_NE(ptr->idx_expr()->result_type(), nullptr);
ASSERT_TRUE(ptr->idx_expr()->result_type()->Is<ast::type::U32Type>());
@ -781,8 +781,8 @@ TEST_F(BoundArrayAccessorsTest, Matrix_Idx_Expr_Row) {
ASSERT_TRUE(ary->idx_expr()->Is<ast::ScalarConstructorExpression>());
auto* scalar = ary->idx_expr()->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(scalar->literal()->IsUint());
EXPECT_EQ(scalar->literal()->AsUint()->value(), 1u);
ASSERT_TRUE(scalar->literal()->Is<ast::UintLiteral>());
EXPECT_EQ(scalar->literal()->As<ast::UintLiteral>()->value(), 1u);
ASSERT_TRUE(ptr->idx_expr()->Is<ast::CallExpression>());
auto* idx = ptr->idx_expr()->As<ast::CallExpression>();
@ -801,8 +801,8 @@ TEST_F(BoundArrayAccessorsTest, Matrix_Idx_Expr_Row) {
ASSERT_TRUE(idx->params()[1]->Is<ast::ConstructorExpression>());
ASSERT_TRUE(idx->params()[1]->Is<ast::ScalarConstructorExpression>());
scalar = idx->params()[1]->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(scalar->literal()->IsUint());
EXPECT_EQ(scalar->literal()->AsUint()->value(), 1u);
ASSERT_TRUE(scalar->literal()->Is<ast::UintLiteral>());
EXPECT_EQ(scalar->literal()->As<ast::UintLiteral>()->value(), 1u);
ASSERT_NE(ary->idx_expr()->result_type(), nullptr);
ASSERT_TRUE(ary->idx_expr()->result_type()->Is<ast::type::U32Type>());
@ -848,8 +848,8 @@ TEST_F(BoundArrayAccessorsTest, Matrix_Idx_Negative_Column) {
ASSERT_TRUE(ary->idx_expr()->Is<ast::ScalarConstructorExpression>());
auto* scalar = ary->idx_expr()->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(scalar->literal()->IsSint());
EXPECT_EQ(scalar->literal()->AsSint()->value(), 0);
ASSERT_TRUE(scalar->literal()->Is<ast::SintLiteral>());
EXPECT_EQ(scalar->literal()->As<ast::SintLiteral>()->value(), 0);
ASSERT_NE(ary->idx_expr()->result_type(), nullptr);
ASSERT_TRUE(ary->idx_expr()->result_type()->Is<ast::type::I32Type>());
@ -858,8 +858,8 @@ TEST_F(BoundArrayAccessorsTest, Matrix_Idx_Negative_Column) {
ASSERT_TRUE(ptr->idx_expr()->Is<ast::ScalarConstructorExpression>());
scalar = ptr->idx_expr()->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(scalar->literal()->IsSint());
EXPECT_EQ(scalar->literal()->AsSint()->value(), 1);
ASSERT_TRUE(scalar->literal()->Is<ast::SintLiteral>());
EXPECT_EQ(scalar->literal()->As<ast::SintLiteral>()->value(), 1);
ASSERT_NE(ptr->idx_expr()->result_type(), nullptr);
ASSERT_TRUE(ptr->idx_expr()->result_type()->Is<ast::type::I32Type>());
@ -902,8 +902,8 @@ TEST_F(BoundArrayAccessorsTest, Matrix_Idx_Negative_Row) {
ASSERT_TRUE(ary->idx_expr()->Is<ast::ScalarConstructorExpression>());
auto* scalar = ary->idx_expr()->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(scalar->literal()->IsSint());
EXPECT_EQ(scalar->literal()->AsSint()->value(), 2);
ASSERT_TRUE(scalar->literal()->Is<ast::SintLiteral>());
EXPECT_EQ(scalar->literal()->As<ast::SintLiteral>()->value(), 2);
ASSERT_NE(ary->idx_expr()->result_type(), nullptr);
ASSERT_TRUE(ary->idx_expr()->result_type()->Is<ast::type::I32Type>());
@ -912,8 +912,8 @@ TEST_F(BoundArrayAccessorsTest, Matrix_Idx_Negative_Row) {
ASSERT_TRUE(ptr->idx_expr()->Is<ast::ScalarConstructorExpression>());
scalar = ptr->idx_expr()->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(scalar->literal()->IsSint());
EXPECT_EQ(scalar->literal()->AsSint()->value(), 0);
ASSERT_TRUE(scalar->literal()->Is<ast::SintLiteral>());
EXPECT_EQ(scalar->literal()->As<ast::SintLiteral>()->value(), 0);
ASSERT_NE(ptr->idx_expr()->result_type(), nullptr);
ASSERT_TRUE(ptr->idx_expr()->result_type()->Is<ast::type::I32Type>());
@ -957,8 +957,8 @@ TEST_F(BoundArrayAccessorsTest, Matrix_Idx_OutOfBounds_Column) {
ASSERT_TRUE(ary->idx_expr()->Is<ast::ScalarConstructorExpression>());
auto* scalar = ary->idx_expr()->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(scalar->literal()->IsUint());
EXPECT_EQ(scalar->literal()->AsUint()->value(), 2u);
ASSERT_TRUE(scalar->literal()->Is<ast::UintLiteral>());
EXPECT_EQ(scalar->literal()->As<ast::UintLiteral>()->value(), 2u);
ASSERT_NE(ary->idx_expr()->result_type(), nullptr);
ASSERT_TRUE(ary->idx_expr()->result_type()->Is<ast::type::U32Type>());
@ -967,8 +967,8 @@ TEST_F(BoundArrayAccessorsTest, Matrix_Idx_OutOfBounds_Column) {
ASSERT_TRUE(ptr->idx_expr()->Is<ast::ScalarConstructorExpression>());
scalar = ptr->idx_expr()->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(scalar->literal()->IsUint());
EXPECT_EQ(scalar->literal()->AsUint()->value(), 1u);
ASSERT_TRUE(scalar->literal()->Is<ast::UintLiteral>());
EXPECT_EQ(scalar->literal()->As<ast::UintLiteral>()->value(), 1u);
ASSERT_NE(ptr->idx_expr()->result_type(), nullptr);
ASSERT_TRUE(ptr->idx_expr()->result_type()->Is<ast::type::U32Type>());
@ -1012,8 +1012,8 @@ TEST_F(BoundArrayAccessorsTest, Matrix_Idx_OutOfBounds_Row) {
ASSERT_TRUE(ary->idx_expr()->Is<ast::ScalarConstructorExpression>());
auto* scalar = ary->idx_expr()->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(scalar->literal()->IsUint());
EXPECT_EQ(scalar->literal()->AsUint()->value(), 2u);
ASSERT_TRUE(scalar->literal()->Is<ast::UintLiteral>());
EXPECT_EQ(scalar->literal()->As<ast::UintLiteral>()->value(), 2u);
ASSERT_NE(ary->idx_expr()->result_type(), nullptr);
ASSERT_TRUE(ary->idx_expr()->result_type()->Is<ast::type::U32Type>());
@ -1022,8 +1022,8 @@ TEST_F(BoundArrayAccessorsTest, Matrix_Idx_OutOfBounds_Row) {
ASSERT_TRUE(ptr->idx_expr()->Is<ast::ScalarConstructorExpression>());
scalar = ptr->idx_expr()->As<ast::ScalarConstructorExpression>();
ASSERT_TRUE(scalar->literal()->IsUint());
EXPECT_EQ(scalar->literal()->AsUint()->value(), 1u);
ASSERT_TRUE(scalar->literal()->Is<ast::UintLiteral>());
EXPECT_EQ(scalar->literal()->As<ast::UintLiteral>()->value(), 1u);
ASSERT_NE(ptr->idx_expr()->result_type(), nullptr);
ASSERT_TRUE(ptr->idx_expr()->result_type()->Is<ast::type::U32Type>());

View File

@ -344,13 +344,14 @@ bool ValidatorImpl::ValidateSwitch(const ast::SwitchStatement* s) {
return false;
}
auto v = static_cast<int32_t>(selector->type()->Is<ast::type::U32Type>()
? selector->AsUint()->value()
: selector->AsSint()->value());
auto v =
static_cast<int32_t>(selector->type()->Is<ast::type::U32Type>()
? selector->As<ast::UintLiteral>()->value()
: selector->As<ast::SintLiteral>()->value());
if (selector_set.count(v)) {
auto v_str = selector->type()->Is<ast::type::U32Type>()
? selector->AsUint()->to_str()
: selector->AsSint()->to_str();
? selector->As<ast::UintLiteral>()->to_str()
: selector->As<ast::SintLiteral>()->to_str();
add_error(case_stmt->source(), "v-0027",
"a literal value must not appear more than once in "
"the case selectors for a switch statement: '" +

View File

@ -1538,14 +1538,14 @@ bool GeneratorImpl::EmitEntryPointFunction(std::ostream& out,
}
bool GeneratorImpl::EmitLiteral(std::ostream& out, ast::Literal* lit) {
if (lit->IsBool()) {
out << (lit->AsBool()->IsTrue() ? "true" : "false");
} else if (lit->IsFloat()) {
out << FloatToString(lit->AsFloat()->value()) << "f";
} else if (lit->IsSint()) {
out << lit->AsSint()->value();
} else if (lit->IsUint()) {
out << lit->AsUint()->value() << "u";
if (lit->Is<ast::BoolLiteral>()) {
out << (lit->As<ast::BoolLiteral>()->IsTrue() ? "true" : "false");
} else if (lit->Is<ast::FloatLiteral>()) {
out << FloatToString(lit->As<ast::FloatLiteral>()->value()) << "f";
} else if (lit->Is<ast::SintLiteral>()) {
out << lit->As<ast::SintLiteral>()->value();
} else if (lit->Is<ast::UintLiteral>()) {
out << lit->As<ast::UintLiteral>()->value() << "u";
} else {
error_ = "unknown literal type";
return false;

View File

@ -966,14 +966,14 @@ bool GeneratorImpl::EmitScalarConstructor(
}
bool GeneratorImpl::EmitLiteral(ast::Literal* lit) {
if (lit->IsBool()) {
out_ << (lit->AsBool()->IsTrue() ? "true" : "false");
} else if (lit->IsFloat()) {
out_ << FloatToString(lit->AsFloat()->value()) << "f";
} else if (lit->IsSint()) {
out_ << lit->AsSint()->value();
} else if (lit->IsUint()) {
out_ << lit->AsUint()->value() << "u";
if (lit->Is<ast::BoolLiteral>()) {
out_ << (lit->As<ast::BoolLiteral>()->IsTrue() ? "true" : "false");
} else if (lit->Is<ast::FloatLiteral>()) {
out_ << FloatToString(lit->As<ast::FloatLiteral>()->value()) << "f";
} else if (lit->Is<ast::SintLiteral>()) {
out_ << lit->As<ast::SintLiteral>()->value();
} else if (lit->Is<ast::UintLiteral>()) {
out_ << lit->As<ast::UintLiteral>()->value() << "u";
} else {
error_ = "unknown literal type";
return false;

View File

@ -1490,8 +1490,8 @@ uint32_t Builder::GenerateLiteralIfNeeded(ast::Variable* var,
Operand::Int(var->As<ast::DecoratedVariable>()->constant_id())});
}
if (lit->IsBool()) {
if (lit->AsBool()->IsTrue()) {
if (lit->Is<ast::BoolLiteral>()) {
if (lit->As<ast::BoolLiteral>()->IsTrue()) {
push_type(is_spec_constant ? spv::Op::OpSpecConstantTrue
: spv::Op::OpConstantTrue,
{Operand::Int(type_id), result});
@ -1500,19 +1500,19 @@ uint32_t Builder::GenerateLiteralIfNeeded(ast::Variable* var,
: spv::Op::OpConstantFalse,
{Operand::Int(type_id), result});
}
} else if (lit->IsSint()) {
push_type(
is_spec_constant ? spv::Op::OpSpecConstant : spv::Op::OpConstant,
{Operand::Int(type_id), result, Operand::Int(lit->AsSint()->value())});
} else if (lit->IsUint()) {
push_type(
is_spec_constant ? spv::Op::OpSpecConstant : spv::Op::OpConstant,
{Operand::Int(type_id), result, Operand::Int(lit->AsUint()->value())});
} else if (lit->IsFloat()) {
} else if (lit->Is<ast::SintLiteral>()) {
push_type(is_spec_constant ? spv::Op::OpSpecConstant : spv::Op::OpConstant,
{Operand::Int(type_id), result,
Operand::Float(lit->AsFloat()->value())});
} else if (lit->IsNull()) {
Operand::Int(lit->As<ast::SintLiteral>()->value())});
} else if (lit->Is<ast::UintLiteral>()) {
push_type(is_spec_constant ? spv::Op::OpSpecConstant : spv::Op::OpConstant,
{Operand::Int(type_id), result,
Operand::Int(lit->As<ast::UintLiteral>()->value())});
} else if (lit->Is<ast::FloatLiteral>()) {
push_type(is_spec_constant ? spv::Op::OpSpecConstant : spv::Op::OpConstant,
{Operand::Int(type_id), result,
Operand::Float(lit->As<ast::FloatLiteral>()->value())});
} else if (lit->Is<ast::NullLiteral>()) {
push_type(spv::Op::OpConstantNull, {Operand::Int(type_id), result});
} else {
error_ = "unknown literal type";
@ -2247,12 +2247,12 @@ bool Builder::GenerateSwitchStatement(ast::SwitchStatement* stmt) {
case_ids.push_back(block_id);
for (auto* selector : item->selectors()) {
if (!selector->IsSint()) {
if (!selector->Is<ast::SintLiteral>()) {
error_ = "expected integer literal for switch case label";
return false;
}
params.push_back(Operand::Int(selector->AsSint()->value()));
params.push_back(Operand::Int(selector->As<ast::SintLiteral>()->value()));
params.push_back(Operand::Int(block_id));
}
}

View File

@ -321,14 +321,14 @@ bool GeneratorImpl::EmitScalarConstructor(
}
bool GeneratorImpl::EmitLiteral(ast::Literal* lit) {
if (lit->IsBool()) {
out_ << (lit->AsBool()->IsTrue() ? "true" : "false");
} else if (lit->IsFloat()) {
out_ << FloatToString(lit->AsFloat()->value());
} else if (lit->IsSint()) {
out_ << lit->AsSint()->value();
} else if (lit->IsUint()) {
out_ << lit->AsUint()->value() << "u";
if (lit->Is<ast::BoolLiteral>()) {
out_ << (lit->As<ast::BoolLiteral>()->IsTrue() ? "true" : "false");
} else if (lit->Is<ast::FloatLiteral>()) {
out_ << FloatToString(lit->As<ast::FloatLiteral>()->value());
} else if (lit->Is<ast::SintLiteral>()) {
out_ << lit->As<ast::SintLiteral>()->value();
} else if (lit->Is<ast::UintLiteral>()) {
out_ << lit->As<ast::UintLiteral>()->value() << "u";
} else {
error_ = "unknown literal type";
return false;