ast: Remove types from ast::Literals

A literal has an implicit type, so there should be no type on the AST node.

This highlighted that the resolver was nto canonicalizing TypeConstructorExpression types, which has been fixed.
This required preservation of the declared type name in order for error messages to contain aliased names.

Bug: tint:724
Change-Id: I21594a3e8a0fb1b73c6c5b46a14b8664b7f28512
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/49345
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
Commit-Queue: Ben Clayton <bclayton@chromium.org>
This commit is contained in:
Ben Clayton
2021-04-28 13:50:43 +00:00
committed by Commit Bot service account
parent 0bf0fb9b29
commit 109b18f504
34 changed files with 236 additions and 264 deletions

View File

@@ -21,11 +21,8 @@ TINT_INSTANTIATE_TYPEINFO(tint::ast::BoolLiteral);
namespace tint {
namespace ast {
BoolLiteral::BoolLiteral(ProgramID program_id,
const Source& source,
typ::Type type,
bool value)
: Base(program_id, source, type), value_(value) {}
BoolLiteral::BoolLiteral(ProgramID program_id, const Source& source, bool value)
: Base(program_id, source), value_(value) {}
BoolLiteral::~BoolLiteral() = default;
@@ -40,8 +37,7 @@ std::string BoolLiteral::name() const {
BoolLiteral* BoolLiteral::Clone(CloneContext* ctx) const {
// Clone arguments outside of create() call to have deterministic ordering
auto src = ctx->Clone(source());
auto ty = ctx->Clone(type());
return ctx->dst->create<BoolLiteral>(src, ty, value_);
return ctx->dst->create<BoolLiteral>(src, value_);
}
} // namespace ast

View File

@@ -28,12 +28,8 @@ class BoolLiteral : public Castable<BoolLiteral, Literal> {
/// Constructor
/// @param program_id the identifier of the program that owns this node
/// @param source the input source
/// @param type the type of the literal
/// @param value the bool literals value
BoolLiteral(ProgramID program_id,
const Source& source,
typ::Type type,
bool value);
BoolLiteral(ProgramID program_id, const Source& source, bool value);
~BoolLiteral() override;
/// @returns true if the bool literal is true

View File

@@ -21,21 +21,21 @@ namespace {
using BoolLiteralTest = TestHelper;
TEST_F(BoolLiteralTest, True) {
auto* b = create<BoolLiteral>(ty.bool_(), true);
auto* b = create<BoolLiteral>(true);
ASSERT_TRUE(b->Is<BoolLiteral>());
ASSERT_TRUE(b->IsTrue());
ASSERT_FALSE(b->IsFalse());
}
TEST_F(BoolLiteralTest, False) {
auto* b = create<BoolLiteral>(ty.bool_(), false);
auto* b = create<BoolLiteral>(false);
ASSERT_TRUE(b->Is<BoolLiteral>());
ASSERT_FALSE(b->IsTrue());
ASSERT_TRUE(b->IsFalse());
}
TEST_F(BoolLiteralTest, Is) {
ast::Literal* l = create<BoolLiteral>(ty.bool_(), false);
ast::Literal* l = create<BoolLiteral>(false);
EXPECT_TRUE(l->Is<BoolLiteral>());
EXPECT_FALSE(l->Is<SintLiteral>());
EXPECT_FALSE(l->Is<FloatLiteral>());
@@ -44,8 +44,8 @@ TEST_F(BoolLiteralTest, Is) {
}
TEST_F(BoolLiteralTest, ToStr) {
auto* t = create<BoolLiteral>(ty.bool_(), true);
auto* f = create<BoolLiteral>(ty.bool_(), false);
auto* t = create<BoolLiteral>(true);
auto* f = create<BoolLiteral>(false);
EXPECT_EQ(str(t), "true");
EXPECT_EQ(str(f), "false");

View File

@@ -27,7 +27,7 @@ using CaseStatementTest = TestHelper;
TEST_F(CaseStatementTest, Creation_i32) {
CaseSelectorList b;
auto* selector = create<SintLiteral>(ty.i32(), 2);
auto* selector = create<SintLiteral>(2);
b.push_back(selector);
auto* discard = create<DiscardStatement>();
@@ -42,7 +42,7 @@ TEST_F(CaseStatementTest, Creation_i32) {
TEST_F(CaseStatementTest, Creation_u32) {
CaseSelectorList b;
auto* selector = create<SintLiteral>(ty.u32(), 2);
auto* selector = create<UintLiteral>(2u);
b.push_back(selector);
auto* discard = create<DiscardStatement>();
@@ -57,7 +57,7 @@ TEST_F(CaseStatementTest, Creation_u32) {
TEST_F(CaseStatementTest, Creation_WithSource) {
CaseSelectorList b;
b.push_back(create<SintLiteral>(ty.i32(), 2));
b.push_back(create<SintLiteral>(2));
auto* body = create<BlockStatement>(StatementList{
create<DiscardStatement>(),
@@ -78,7 +78,7 @@ TEST_F(CaseStatementTest, IsDefault_WithoutSelectors) {
TEST_F(CaseStatementTest, IsDefault_WithSelectors) {
CaseSelectorList b;
b.push_back(create<SintLiteral>(ty.i32(), 2));
b.push_back(create<SintLiteral>(2));
auto* c = create<CaseStatement>(b, create<BlockStatement>(StatementList{}));
EXPECT_FALSE(c->IsDefault());
@@ -125,16 +125,15 @@ TEST_F(CaseStatementTest, Assert_DifferentProgramID_Selector) {
{
ProgramBuilder b1;
ProgramBuilder b2;
b1.create<CaseStatement>(
CaseSelectorList{b2.create<SintLiteral>(b2.ty.i32(), 2)},
b1.create<BlockStatement>(StatementList{}));
b1.create<CaseStatement>(CaseSelectorList{b2.create<SintLiteral>(2)},
b1.create<BlockStatement>(StatementList{}));
},
"internal compiler error");
}
TEST_F(CaseStatementTest, ToStr_WithSelectors_i32) {
CaseSelectorList b;
b.push_back(create<SintLiteral>(ty.i32(), -2));
b.push_back(create<SintLiteral>(-2));
auto* body = create<BlockStatement>(StatementList{
create<DiscardStatement>(),
@@ -149,7 +148,7 @@ TEST_F(CaseStatementTest, ToStr_WithSelectors_i32) {
TEST_F(CaseStatementTest, ToStr_WithSelectors_u32) {
CaseSelectorList b;
b.push_back(create<UintLiteral>(ty.u32(), 2));
b.push_back(create<UintLiteral>(2));
auto* body = create<BlockStatement>(StatementList{
create<DiscardStatement>(),
@@ -164,8 +163,8 @@ TEST_F(CaseStatementTest, ToStr_WithSelectors_u32) {
TEST_F(CaseStatementTest, ToStr_WithMultipleSelectors) {
CaseSelectorList b;
b.push_back(create<SintLiteral>(ty.i32(), 1));
b.push_back(create<SintLiteral>(ty.i32(), 2));
b.push_back(create<SintLiteral>(1));
b.push_back(create<SintLiteral>(2));
auto* body = create<BlockStatement>(StatementList{
create<DiscardStatement>(),

View File

@@ -25,9 +25,8 @@ namespace ast {
FloatLiteral::FloatLiteral(ProgramID program_id,
const Source& source,
typ::Type type,
float value)
: Base(program_id, source, type), value_(value) {}
: Base(program_id, source), value_(value) {}
FloatLiteral::~FloatLiteral() = default;
@@ -46,8 +45,7 @@ std::string FloatLiteral::name() const {
FloatLiteral* FloatLiteral::Clone(CloneContext* ctx) const {
// Clone arguments outside of create() call to have deterministic ordering
auto src = ctx->Clone(source());
auto ty = ctx->Clone(type());
return ctx->dst->create<FloatLiteral>(src, ty, value_);
return ctx->dst->create<FloatLiteral>(src, value_);
}
} // namespace ast

View File

@@ -28,12 +28,8 @@ class FloatLiteral : public Castable<FloatLiteral, Literal> {
/// Constructor
/// @param program_id the identifier of the program that owns this node
/// @param source the input source
/// @param type the type of the literal
/// @param value the float literals value
FloatLiteral(ProgramID program_id,
const Source& source,
typ::Type type,
float value);
FloatLiteral(ProgramID program_id, const Source& source, float value);
~FloatLiteral() override;
/// @returns the float literal value

View File

@@ -21,13 +21,13 @@ namespace {
using FloatLiteralTest = TestHelper;
TEST_F(FloatLiteralTest, Value) {
auto* f = create<FloatLiteral>(ty.f32(), 47.2f);
auto* f = create<FloatLiteral>(47.2f);
ASSERT_TRUE(f->Is<FloatLiteral>());
EXPECT_EQ(f->value(), 47.2f);
}
TEST_F(FloatLiteralTest, Is) {
ast::Literal* l = create<FloatLiteral>(ty.f32(), 42.f);
ast::Literal* l = create<FloatLiteral>(42.f);
EXPECT_FALSE(l->Is<BoolLiteral>());
EXPECT_FALSE(l->Is<SintLiteral>());
EXPECT_FALSE(l->Is<IntLiteral>());
@@ -36,12 +36,12 @@ TEST_F(FloatLiteralTest, Is) {
}
TEST_F(FloatLiteralTest, ToStr) {
auto* f = create<FloatLiteral>(ty.f32(), 42.1f);
auto* f = create<FloatLiteral>(42.1f);
EXPECT_EQ(str(f), "42.099998");
}
TEST_F(FloatLiteralTest, ToName) {
auto* f = create<FloatLiteral>(ty.f32(), 42.1f);
auto* f = create<FloatLiteral>(42.1f);
EXPECT_EQ(f->name(), "__float42.0999985");
}

View File

@@ -21,9 +21,8 @@ namespace ast {
IntLiteral::IntLiteral(ProgramID program_id,
const Source& source,
typ::Type type,
uint32_t value)
: Base(program_id, source, type), value_(value) {}
: Base(program_id, source), value_(value) {}
IntLiteral::~IntLiteral() = default;

View File

@@ -32,12 +32,8 @@ class IntLiteral : public Castable<IntLiteral, Literal> {
/// Constructor
/// @param program_id the identifier of the program that owns this node
/// @param source the input source
/// @param type the type of the literal
/// @param value value of the literal
IntLiteral(ProgramID program_id,
const Source& source,
typ::Type type,
uint32_t value);
IntLiteral(ProgramID program_id, const Source& source, uint32_t value);
private:
uint32_t const value_;

View File

@@ -21,12 +21,12 @@ namespace {
using IntLiteralTest = TestHelper;
TEST_F(IntLiteralTest, Sint_IsInt) {
auto* i = create<SintLiteral>(ty.i32(), 47);
auto* i = create<SintLiteral>(47);
ASSERT_TRUE(i->Is<IntLiteral>());
}
TEST_F(IntLiteralTest, Uint_IsInt) {
auto* i = create<UintLiteral>(ty.i32(), 42);
auto* i = create<UintLiteral>(42);
EXPECT_TRUE(i->Is<IntLiteral>());
}

View File

@@ -19,8 +19,8 @@ TINT_INSTANTIATE_TYPEINFO(tint::ast::Literal);
namespace tint {
namespace ast {
Literal::Literal(ProgramID program_id, const Source& source, typ::Type type)
: Base(program_id, source), type_(type) {}
Literal::Literal(ProgramID program_id, const Source& source)
: Base(program_id, source) {}
Literal::~Literal() = default;

View File

@@ -27,9 +27,6 @@ class Literal : public Castable<Literal, Node> {
public:
~Literal() override;
/// @returns the type of the literal
typ::Type type() const { return type_; }
/// Writes a representation of the node to the output stream
/// @param sem the semantic info for the program
/// @param out the stream to write to
@@ -49,11 +46,7 @@ class Literal : public Castable<Literal, Node> {
/// Constructor
/// @param program_id the identifier of the program that owns this node
/// @param source the input source
/// @param type the type of the literal
explicit Literal(ProgramID program_id, const Source& source, typ::Type type);
private:
typ::Type const type_;
Literal(ProgramID program_id, const Source& source);
};
} // namespace ast

View File

@@ -22,7 +22,7 @@ namespace {
using ScalarConstructorExpressionTest = TestHelper;
TEST_F(ScalarConstructorExpressionTest, Creation) {
auto* b = create<BoolLiteral>(ty.bool_(), true);
auto* b = create<BoolLiteral>(true);
auto* c = create<ScalarConstructorExpression>(b);
EXPECT_EQ(c->literal(), b);
}
@@ -45,8 +45,7 @@ TEST_F(ScalarConstructorExpressionTest, Assert_DifferentProgramID_Literal) {
{
ProgramBuilder b1;
ProgramBuilder b2;
b1.create<ScalarConstructorExpression>(
b2.create<BoolLiteral>(b2.ty.bool_(), true));
b1.create<ScalarConstructorExpression>(b2.create<BoolLiteral>(true));
},
"internal compiler error");
}

View File

@@ -23,9 +23,8 @@ namespace ast {
SintLiteral::SintLiteral(ProgramID program_id,
const Source& source,
typ::Type type,
int32_t value)
: Base(program_id, source, type, static_cast<uint32_t>(value)) {}
: Base(program_id, source, static_cast<uint32_t>(value)) {}
SintLiteral::~SintLiteral() = default;
@@ -34,14 +33,13 @@ std::string SintLiteral::to_str(const sem::Info&) const {
}
std::string SintLiteral::name() const {
return "__sint" + type()->type_name() + "_" + std::to_string(value());
return "__sint_" + std::to_string(value());
}
SintLiteral* SintLiteral::Clone(CloneContext* ctx) const {
// Clone arguments outside of create() call to have deterministic ordering
auto src = ctx->Clone(source());
auto ty = ctx->Clone(type());
return ctx->dst->create<SintLiteral>(src, ty, value());
return ctx->dst->create<SintLiteral>(src, value());
}
} // namespace ast

View File

@@ -28,12 +28,8 @@ class SintLiteral : public Castable<SintLiteral, IntLiteral> {
/// Constructor
/// @param program_id the identifier of the program that owns this node
/// @param source the input source
/// @param type the type
/// @param value the signed int literals value
SintLiteral(ProgramID program_id,
const Source& source,
typ::Type type,
int32_t value);
SintLiteral(ProgramID program_id, const Source& source, int32_t value);
~SintLiteral() override;
/// @returns the int literal value

View File

@@ -21,13 +21,13 @@ namespace {
using SintLiteralTest = TestHelper;
TEST_F(SintLiteralTest, Value) {
auto* i = create<SintLiteral>(ty.i32(), 47);
auto* i = create<SintLiteral>(47);
ASSERT_TRUE(i->Is<SintLiteral>());
EXPECT_EQ(i->value(), 47);
}
TEST_F(SintLiteralTest, Is) {
ast::Literal* l = create<SintLiteral>(ty.i32(), 42);
ast::Literal* l = create<SintLiteral>(42);
EXPECT_FALSE(l->Is<BoolLiteral>());
EXPECT_TRUE(l->Is<SintLiteral>());
EXPECT_FALSE(l->Is<FloatLiteral>());
@@ -35,18 +35,13 @@ TEST_F(SintLiteralTest, Is) {
}
TEST_F(SintLiteralTest, ToStr) {
auto* i = create<SintLiteral>(ty.i32(), -42);
auto* i = create<SintLiteral>(-42);
EXPECT_EQ(str(i), "-42");
}
TEST_F(SintLiteralTest, Name_I32) {
auto* i = create<SintLiteral>(ty.i32(), 2);
EXPECT_EQ("__sint__i32_2", i->name());
}
TEST_F(SintLiteralTest, Name_U32) {
auto* i = create<SintLiteral>(ty.u32(), 2);
EXPECT_EQ("__sint__u32_2", i->name());
auto* i = create<SintLiteral>(2);
EXPECT_EQ("__sint_2", i->name());
}
} // namespace

View File

@@ -25,7 +25,7 @@ using SwitchStatementTest = TestHelper;
TEST_F(SwitchStatementTest, Creation) {
CaseSelectorList lit;
lit.push_back(create<SintLiteral>(ty.i32(), 1));
lit.push_back(create<SintLiteral>(1));
auto* ident = Expr("ident");
CaseStatementList body;
@@ -50,7 +50,7 @@ TEST_F(SwitchStatementTest, Creation_WithSource) {
TEST_F(SwitchStatementTest, IsSwitch) {
CaseSelectorList lit;
lit.push_back(create<SintLiteral>(ty.i32(), 2));
lit.push_back(create<SintLiteral>(2));
auto* ident = Expr("ident");
CaseStatementList body;
@@ -127,7 +127,7 @@ TEST_F(SwitchStatementTest, ToStr_Empty) {
TEST_F(SwitchStatementTest, ToStr) {
CaseSelectorList lit;
lit.push_back(create<SintLiteral>(ty.i32(), 2));
lit.push_back(create<SintLiteral>(2));
auto* ident = Expr("ident");
CaseStatementList body;

View File

@@ -23,9 +23,8 @@ namespace ast {
UintLiteral::UintLiteral(ProgramID program_id,
const Source& source,
typ::Type type,
uint32_t value)
: Base(program_id, source, type, value) {}
: Base(program_id, source, value) {}
UintLiteral::~UintLiteral() = default;
@@ -40,8 +39,7 @@ std::string UintLiteral::name() const {
UintLiteral* UintLiteral::Clone(CloneContext* ctx) const {
// Clone arguments outside of create() call to have deterministic ordering
auto src = ctx->Clone(source());
auto ty = ctx->Clone(type());
return ctx->dst->create<UintLiteral>(src, ty, value());
return ctx->dst->create<UintLiteral>(src, value());
}
} // namespace ast

View File

@@ -28,12 +28,8 @@ class UintLiteral : public Castable<UintLiteral, IntLiteral> {
/// Constructor
/// @param program_id the identifier of the program that owns this node
/// @param source the input source
/// @param type the type of the literal
/// @param value the uint literals value
UintLiteral(ProgramID program_id,
const Source& source,
typ::Type type,
uint32_t value);
UintLiteral(ProgramID program_id, const Source& source, uint32_t value);
~UintLiteral() override;
/// @returns the uint literal value

View File

@@ -21,13 +21,13 @@ namespace {
using UintLiteralTest = TestHelper;
TEST_F(UintLiteralTest, Value) {
auto* u = create<UintLiteral>(ty.u32(), 47);
auto* u = create<UintLiteral>(47);
ASSERT_TRUE(u->Is<UintLiteral>());
EXPECT_EQ(u->value(), 47u);
}
TEST_F(UintLiteralTest, Is) {
ast::Literal* l = create<UintLiteral>(ty.u32(), 42);
ast::Literal* l = create<UintLiteral>(42);
EXPECT_FALSE(l->Is<BoolLiteral>());
EXPECT_FALSE(l->Is<SintLiteral>());
EXPECT_FALSE(l->Is<FloatLiteral>());
@@ -35,7 +35,7 @@ TEST_F(UintLiteralTest, Is) {
}
TEST_F(UintLiteralTest, ToStr) {
auto* u = create<UintLiteral>(ty.u32(), 42u);
auto* u = create<UintLiteral>(42u);
EXPECT_EQ(str(u), "42u");
}