Start cleaning up tests (3/N)

Remove Source{} with ast::Builder::create<>
Use Builder helpers where possible

Change-Id: I7b3188fa8239bf11e88ff6b396a57c23d76a3152
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/35660
Reviewed-by: dan sinclair <dsinclair@chromium.org>
Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ben Clayton 2020-12-14 22:30:57 +00:00 committed by Commit Bot service account
parent 5180e16386
commit 1523f5c276
57 changed files with 1486 additions and 1895 deletions

View File

@ -750,6 +750,7 @@ config("tint_unittests_config") {
source_set("tint_unittests_core_src") { source_set("tint_unittests_core_src") {
sources = [ sources = [
"src/ast/access_decoration_test.cc",
"src/ast/array_accessor_expression_test.cc", "src/ast/array_accessor_expression_test.cc",
"src/ast/assignment_statement_test.cc", "src/ast/assignment_statement_test.cc",
"src/ast/binary_expression_test.cc", "src/ast/binary_expression_test.cc",

View File

@ -387,6 +387,7 @@ endif()
if(${TINT_BUILD_TESTS}) if(${TINT_BUILD_TESTS})
set(TINT_TEST_SRCS set(TINT_TEST_SRCS
ast/access_decoration_test.cc
ast/array_accessor_expression_test.cc ast/array_accessor_expression_test.cc
ast/assignment_statement_test.cc ast/assignment_statement_test.cc
ast/binding_decoration_test.cc ast/binding_decoration_test.cc

View File

@ -25,20 +25,20 @@ namespace {
using AccessDecorationTest = TestHelper; using AccessDecorationTest = TestHelper;
TEST_F(AccessDecorationTest, Creation) { TEST_F(AccessDecorationTest, Creation) {
AccessDecoration d{ast::AccessControl::kWriteOnly, Source{}}; auto* d = create<AccessDecoration>(ast::AccessControl::kWriteOnly);
EXPECT_EQ(ast::AccessControl::kWriteOnly, d.value()); EXPECT_EQ(ast::AccessControl::kWriteOnly, d->value());
} }
TEST_F(AccessDecorationTest, Is) { TEST_F(AccessDecorationTest, Is) {
AccessDecoration d{ast::AccessControl::kReadWrite, Source{}}; auto* d = create<AccessDecoration>(ast::AccessControl::kReadWrite);
EXPECT_FALSE(d.IsAccess()); EXPECT_TRUE(d->Is<ast::AccessDecoration>());
} }
TEST_F(AccessDecorationTest, ToStr) { TEST_F(AccessDecorationTest, ToStr) {
AccessDecoration d{ast::AccessControl::kReadOnly, Source{}}; auto* d = create<AccessDecoration>(ast::AccessControl::kReadOnly);
std::ostringstream out; std::ostringstream out;
d.to_str(out, 0); d->to_str(out, 0);
EXPECT_EQ(out.str(), R"(AccessDecoration{read} EXPECT_EQ(out.str(), R"(AccessDecoration{read_only}
)"); )");
} }

View File

@ -24,91 +24,76 @@ namespace {
using ArrayAccessorExpressionTest = TestHelper; using ArrayAccessorExpressionTest = TestHelper;
TEST_F(ArrayAccessorExpressionTest, Create) { TEST_F(ArrayAccessorExpressionTest, Create) {
auto* ary = auto* ary = Expr("ary");
create<IdentifierExpression>(Source{}, mod.RegisterSymbol("ary"), "ary"); auto* idx = Expr("idx");
auto* idx =
create<IdentifierExpression>(Source{}, mod.RegisterSymbol("idx"), "idx");
ArrayAccessorExpression exp(Source{}, ary, idx); auto* exp = create<ArrayAccessorExpression>(ary, idx);
ASSERT_EQ(exp.array(), ary); ASSERT_EQ(exp->array(), ary);
ASSERT_EQ(exp.idx_expr(), idx); ASSERT_EQ(exp->idx_expr(), idx);
} }
TEST_F(ArrayAccessorExpressionTest, CreateWithSource) { TEST_F(ArrayAccessorExpressionTest, CreateWithSource) {
auto* ary = auto* ary = Expr("ary");
create<IdentifierExpression>(Source{}, mod.RegisterSymbol("ary"), "ary"); auto* idx = Expr("idx");
auto* idx =
create<IdentifierExpression>(Source{}, mod.RegisterSymbol("idx"), "idx");
ArrayAccessorExpression exp(Source{Source::Location{20, 2}}, ary, idx); auto* exp = create<ArrayAccessorExpression>(Source{Source::Location{20, 2}},
auto src = exp.source(); ary, idx);
auto src = exp->source();
EXPECT_EQ(src.range.begin.line, 20u); EXPECT_EQ(src.range.begin.line, 20u);
EXPECT_EQ(src.range.begin.column, 2u); EXPECT_EQ(src.range.begin.column, 2u);
} }
TEST_F(ArrayAccessorExpressionTest, IsArrayAccessor) { TEST_F(ArrayAccessorExpressionTest, IsArrayAccessor) {
auto* ary = auto* ary = Expr("ary");
create<IdentifierExpression>(Source{}, mod.RegisterSymbol("ary"), "ary"); auto* idx = Expr("idx");
auto* idx =
create<IdentifierExpression>(Source{}, mod.RegisterSymbol("idx"), "idx");
ArrayAccessorExpression exp(Source{}, ary, idx); auto* exp = create<ArrayAccessorExpression>(ary, idx);
EXPECT_TRUE(exp.Is<ArrayAccessorExpression>()); EXPECT_TRUE(exp->Is<ArrayAccessorExpression>());
} }
TEST_F(ArrayAccessorExpressionTest, IsValid) { TEST_F(ArrayAccessorExpressionTest, IsValid) {
auto* ary = auto* ary = Expr("ary");
create<IdentifierExpression>(Source{}, mod.RegisterSymbol("ary"), "ary"); auto* idx = Expr("idx");
auto* idx =
create<IdentifierExpression>(Source{}, mod.RegisterSymbol("idx"), "idx");
ArrayAccessorExpression exp(Source{}, ary, idx); auto* exp = create<ArrayAccessorExpression>(ary, idx);
EXPECT_TRUE(exp.IsValid()); EXPECT_TRUE(exp->IsValid());
} }
TEST_F(ArrayAccessorExpressionTest, IsValid_MissingArray) { TEST_F(ArrayAccessorExpressionTest, IsValid_MissingArray) {
auto* idx = auto* idx = Expr("idx");
create<IdentifierExpression>(Source{}, mod.RegisterSymbol("idx"), "idx");
ArrayAccessorExpression exp(Source{}, nullptr, idx); auto* exp = create<ArrayAccessorExpression>(nullptr, idx);
EXPECT_FALSE(exp.IsValid()); EXPECT_FALSE(exp->IsValid());
} }
TEST_F(ArrayAccessorExpressionTest, IsValid_MissingIndex) { TEST_F(ArrayAccessorExpressionTest, IsValid_MissingIndex) {
auto* ary = auto* ary = Expr("ary");
create<IdentifierExpression>(Source{}, mod.RegisterSymbol("ary"), "ary");
ArrayAccessorExpression exp(Source{}, ary, nullptr); auto* exp = create<ArrayAccessorExpression>(ary, nullptr);
EXPECT_FALSE(exp.IsValid()); EXPECT_FALSE(exp->IsValid());
} }
TEST_F(ArrayAccessorExpressionTest, IsValid_InvalidArray) { TEST_F(ArrayAccessorExpressionTest, IsValid_InvalidArray) {
auto* ary = auto* ary = Expr("");
create<IdentifierExpression>(Source{}, mod.RegisterSymbol(""), ""); auto* idx = Expr("idx");
auto* idx = auto* exp = create<ArrayAccessorExpression>(ary, idx);
create<IdentifierExpression>(Source{}, mod.RegisterSymbol("idx"), "idx"); EXPECT_FALSE(exp->IsValid());
ArrayAccessorExpression exp(Source{}, ary, idx);
EXPECT_FALSE(exp.IsValid());
} }
TEST_F(ArrayAccessorExpressionTest, IsValid_InvalidIndex) { TEST_F(ArrayAccessorExpressionTest, IsValid_InvalidIndex) {
auto* ary = auto* ary = Expr("ary");
create<IdentifierExpression>(Source{}, mod.RegisterSymbol("ary"), "ary"); auto* idx = Expr("");
auto* idx = auto* exp = create<ArrayAccessorExpression>(ary, idx);
create<IdentifierExpression>(Source{}, mod.RegisterSymbol(""), ""); EXPECT_FALSE(exp->IsValid());
ArrayAccessorExpression exp(Source{}, ary, idx);
EXPECT_FALSE(exp.IsValid());
} }
TEST_F(ArrayAccessorExpressionTest, ToStr) { TEST_F(ArrayAccessorExpressionTest, ToStr) {
auto* ary = auto* ary = Expr("ary");
create<IdentifierExpression>(Source{}, mod.RegisterSymbol("ary"), "ary"); auto* idx = Expr("idx");
auto* idx =
create<IdentifierExpression>(Source{}, mod.RegisterSymbol("idx"), "idx");
ArrayAccessorExpression exp(Source{}, ary, idx); auto* exp = create<ArrayAccessorExpression>(ary, idx);
std::ostringstream out; std::ostringstream out;
exp.to_str(out, 2); exp->to_str(out, 2);
EXPECT_EQ(demangle(out.str()), R"( ArrayAccessor[not set]{ EXPECT_EQ(demangle(out.str()), R"( ArrayAccessor[not set]{
Identifier[not set]{ary} Identifier[not set]{ary}

View File

@ -24,91 +24,76 @@ namespace {
using AssignmentStatementTest = TestHelper; using AssignmentStatementTest = TestHelper;
TEST_F(AssignmentStatementTest, Creation) { TEST_F(AssignmentStatementTest, Creation) {
auto* lhs = auto* lhs = Expr("lhs");
create<IdentifierExpression>(Source{}, mod.RegisterSymbol("lhs"), "lhs"); auto* rhs = Expr("rhs");
auto* rhs =
create<IdentifierExpression>(Source{}, mod.RegisterSymbol("rhs"), "rhs");
AssignmentStatement stmt(Source{}, lhs, rhs); auto* stmt = create<AssignmentStatement>(lhs, rhs);
EXPECT_EQ(stmt.lhs(), lhs); EXPECT_EQ(stmt->lhs(), lhs);
EXPECT_EQ(stmt.rhs(), rhs); EXPECT_EQ(stmt->rhs(), rhs);
} }
TEST_F(AssignmentStatementTest, CreationWithSource) { TEST_F(AssignmentStatementTest, CreationWithSource) {
auto* lhs = auto* lhs = Expr("lhs");
create<IdentifierExpression>(Source{}, mod.RegisterSymbol("lhs"), "lhs"); auto* rhs = Expr("rhs");
auto* rhs =
create<IdentifierExpression>(Source{}, mod.RegisterSymbol("rhs"), "rhs");
AssignmentStatement stmt(Source{Source::Location{20, 2}}, lhs, rhs); auto* stmt =
auto src = stmt.source(); create<AssignmentStatement>(Source{Source::Location{20, 2}}, lhs, rhs);
auto src = stmt->source();
EXPECT_EQ(src.range.begin.line, 20u); EXPECT_EQ(src.range.begin.line, 20u);
EXPECT_EQ(src.range.begin.column, 2u); EXPECT_EQ(src.range.begin.column, 2u);
} }
TEST_F(AssignmentStatementTest, IsAssign) { TEST_F(AssignmentStatementTest, IsAssign) {
auto* lhs = auto* lhs = Expr("lhs");
create<IdentifierExpression>(Source{}, mod.RegisterSymbol("lhs"), "lhs"); auto* rhs = Expr("rhs");
auto* rhs =
create<IdentifierExpression>(Source{}, mod.RegisterSymbol("rhs"), "rhs");
AssignmentStatement stmt(Source{}, lhs, rhs); auto* stmt = create<AssignmentStatement>(lhs, rhs);
EXPECT_TRUE(stmt.Is<AssignmentStatement>()); EXPECT_TRUE(stmt->Is<AssignmentStatement>());
} }
TEST_F(AssignmentStatementTest, IsValid) { TEST_F(AssignmentStatementTest, IsValid) {
auto* lhs = auto* lhs = Expr("lhs");
create<IdentifierExpression>(Source{}, mod.RegisterSymbol("lhs"), "lhs"); auto* rhs = Expr("rhs");
auto* rhs =
create<IdentifierExpression>(Source{}, mod.RegisterSymbol("rhs"), "rhs");
AssignmentStatement stmt(Source{}, lhs, rhs); auto* stmt = create<AssignmentStatement>(lhs, rhs);
EXPECT_TRUE(stmt.IsValid()); EXPECT_TRUE(stmt->IsValid());
} }
TEST_F(AssignmentStatementTest, IsValid_MissingLHS) { TEST_F(AssignmentStatementTest, IsValid_MissingLHS) {
auto* rhs = auto* rhs = Expr("rhs");
create<IdentifierExpression>(Source{}, mod.RegisterSymbol("rhs"), "rhs");
AssignmentStatement stmt(Source{}, nullptr, rhs); auto* stmt = create<AssignmentStatement>(nullptr, rhs);
EXPECT_FALSE(stmt.IsValid()); EXPECT_FALSE(stmt->IsValid());
} }
TEST_F(AssignmentStatementTest, IsValid_MissingRHS) { TEST_F(AssignmentStatementTest, IsValid_MissingRHS) {
auto* lhs = auto* lhs = Expr("lhs");
create<IdentifierExpression>(Source{}, mod.RegisterSymbol("lhs"), "lhs");
AssignmentStatement stmt(Source{}, lhs, nullptr); auto* stmt = create<AssignmentStatement>(lhs, nullptr);
EXPECT_FALSE(stmt.IsValid()); EXPECT_FALSE(stmt->IsValid());
} }
TEST_F(AssignmentStatementTest, IsValid_InvalidLHS) { TEST_F(AssignmentStatementTest, IsValid_InvalidLHS) {
auto* lhs = auto* lhs = Expr("");
create<IdentifierExpression>(Source{}, mod.RegisterSymbol(""), ""); auto* rhs = Expr("rhs");
auto* rhs = auto* stmt = create<AssignmentStatement>(lhs, rhs);
create<IdentifierExpression>(Source{}, mod.RegisterSymbol("rhs"), "rhs"); EXPECT_FALSE(stmt->IsValid());
AssignmentStatement stmt(Source{}, lhs, rhs);
EXPECT_FALSE(stmt.IsValid());
} }
TEST_F(AssignmentStatementTest, IsValid_InvalidRHS) { TEST_F(AssignmentStatementTest, IsValid_InvalidRHS) {
auto* lhs = auto* lhs = Expr("lhs");
create<IdentifierExpression>(Source{}, mod.RegisterSymbol("lhs"), "lhs"); auto* rhs = Expr("");
auto* rhs = auto* stmt = create<AssignmentStatement>(lhs, rhs);
create<IdentifierExpression>(Source{}, mod.RegisterSymbol(""), ""); EXPECT_FALSE(stmt->IsValid());
AssignmentStatement stmt(Source{}, lhs, rhs);
EXPECT_FALSE(stmt.IsValid());
} }
TEST_F(AssignmentStatementTest, ToStr) { TEST_F(AssignmentStatementTest, ToStr) {
auto* lhs = auto* lhs = Expr("lhs");
create<IdentifierExpression>(Source{}, mod.RegisterSymbol("lhs"), "lhs"); auto* rhs = Expr("rhs");
auto* rhs =
create<IdentifierExpression>(Source{}, mod.RegisterSymbol("rhs"), "rhs");
AssignmentStatement stmt(Source{}, lhs, rhs); auto* stmt = create<AssignmentStatement>(lhs, rhs);
std::ostringstream out; std::ostringstream out;
stmt.to_str(out, 2); stmt->to_str(out, 2);
EXPECT_EQ(demangle(out.str()), R"( Assignment{ EXPECT_EQ(demangle(out.str()), R"( Assignment{
Identifier[not set]{lhs} Identifier[not set]{lhs}

View File

@ -26,105 +26,87 @@ namespace {
using BinaryExpressionTest = TestHelper; using BinaryExpressionTest = TestHelper;
TEST_F(BinaryExpressionTest, Creation) { TEST_F(BinaryExpressionTest, Creation) {
auto* lhs = auto* lhs = Expr("lhs");
create<IdentifierExpression>(Source{}, mod.RegisterSymbol("lhs"), "lhs"); auto* rhs = Expr("rhs");
auto* rhs =
create<IdentifierExpression>(Source{}, mod.RegisterSymbol("rhs"), "rhs");
BinaryExpression r(Source{}, BinaryOp::kEqual, lhs, rhs); auto* r = create<BinaryExpression>(BinaryOp::kEqual, lhs, rhs);
EXPECT_EQ(r.lhs(), lhs); EXPECT_EQ(r->lhs(), lhs);
EXPECT_EQ(r.rhs(), rhs); EXPECT_EQ(r->rhs(), rhs);
EXPECT_EQ(r.op(), BinaryOp::kEqual); EXPECT_EQ(r->op(), BinaryOp::kEqual);
} }
TEST_F(BinaryExpressionTest, Creation_WithSource) { TEST_F(BinaryExpressionTest, Creation_WithSource) {
auto* lhs = auto* lhs = Expr("lhs");
create<IdentifierExpression>(Source{}, mod.RegisterSymbol("lhs"), "lhs"); auto* rhs = Expr("rhs");
auto* rhs =
create<IdentifierExpression>(Source{}, mod.RegisterSymbol("rhs"), "rhs");
BinaryExpression r(Source{Source::Location{20, 2}}, BinaryOp::kEqual, lhs, auto* r = create<BinaryExpression>(Source{Source::Location{20, 2}},
rhs); BinaryOp::kEqual, lhs, rhs);
auto src = r.source(); auto src = r->source();
EXPECT_EQ(src.range.begin.line, 20u); EXPECT_EQ(src.range.begin.line, 20u);
EXPECT_EQ(src.range.begin.column, 2u); EXPECT_EQ(src.range.begin.column, 2u);
} }
TEST_F(BinaryExpressionTest, IsBinary) { TEST_F(BinaryExpressionTest, IsBinary) {
auto* lhs = auto* lhs = Expr("lhs");
create<IdentifierExpression>(Source{}, mod.RegisterSymbol("lhs"), "lhs"); auto* rhs = Expr("rhs");
auto* rhs =
create<IdentifierExpression>(Source{}, mod.RegisterSymbol("rhs"), "rhs");
BinaryExpression r(Source{}, BinaryOp::kEqual, lhs, rhs); auto* r = create<BinaryExpression>(BinaryOp::kEqual, lhs, rhs);
EXPECT_TRUE(r.Is<BinaryExpression>()); EXPECT_TRUE(r->Is<BinaryExpression>());
} }
TEST_F(BinaryExpressionTest, IsValid) { TEST_F(BinaryExpressionTest, IsValid) {
auto* lhs = auto* lhs = Expr("lhs");
create<IdentifierExpression>(Source{}, mod.RegisterSymbol("lhs"), "lhs"); auto* rhs = Expr("rhs");
auto* rhs =
create<IdentifierExpression>(Source{}, mod.RegisterSymbol("rhs"), "rhs");
BinaryExpression r(Source{}, BinaryOp::kEqual, lhs, rhs); auto* r = create<BinaryExpression>(BinaryOp::kEqual, lhs, rhs);
EXPECT_TRUE(r.IsValid()); EXPECT_TRUE(r->IsValid());
} }
TEST_F(BinaryExpressionTest, IsValid_Null_LHS) { TEST_F(BinaryExpressionTest, IsValid_Null_LHS) {
auto* rhs = auto* rhs = Expr("rhs");
create<IdentifierExpression>(Source{}, mod.RegisterSymbol("rhs"), "rhs");
BinaryExpression r(Source{}, BinaryOp::kEqual, nullptr, rhs); auto* r = create<BinaryExpression>(BinaryOp::kEqual, nullptr, rhs);
EXPECT_FALSE(r.IsValid()); EXPECT_FALSE(r->IsValid());
} }
TEST_F(BinaryExpressionTest, IsValid_Invalid_LHS) { TEST_F(BinaryExpressionTest, IsValid_Invalid_LHS) {
auto* lhs = auto* lhs = Expr("");
create<IdentifierExpression>(Source{}, mod.RegisterSymbol(""), ""); auto* rhs = Expr("rhs");
auto* rhs =
create<IdentifierExpression>(Source{}, mod.RegisterSymbol("rhs"), "rhs");
BinaryExpression r(Source{}, BinaryOp::kEqual, lhs, rhs); auto* r = create<BinaryExpression>(BinaryOp::kEqual, lhs, rhs);
EXPECT_FALSE(r.IsValid()); EXPECT_FALSE(r->IsValid());
} }
TEST_F(BinaryExpressionTest, IsValid_Null_RHS) { TEST_F(BinaryExpressionTest, IsValid_Null_RHS) {
auto* lhs = auto* lhs = Expr("lhs");
create<IdentifierExpression>(Source{}, mod.RegisterSymbol("lhs"), "lhs");
BinaryExpression r(Source{}, BinaryOp::kEqual, lhs, nullptr); auto* r = create<BinaryExpression>(BinaryOp::kEqual, lhs, nullptr);
EXPECT_FALSE(r.IsValid()); EXPECT_FALSE(r->IsValid());
} }
TEST_F(BinaryExpressionTest, IsValid_Invalid_RHS) { TEST_F(BinaryExpressionTest, IsValid_Invalid_RHS) {
auto* lhs = auto* lhs = Expr("lhs");
create<IdentifierExpression>(Source{}, mod.RegisterSymbol("lhs"), "lhs"); auto* rhs = Expr("");
auto* rhs =
create<IdentifierExpression>(Source{}, mod.RegisterSymbol(""), "");
BinaryExpression r(Source{}, BinaryOp::kEqual, lhs, rhs); auto* r = create<BinaryExpression>(BinaryOp::kEqual, lhs, rhs);
EXPECT_FALSE(r.IsValid()); EXPECT_FALSE(r->IsValid());
} }
TEST_F(BinaryExpressionTest, IsValid_Binary_None) { TEST_F(BinaryExpressionTest, IsValid_Binary_None) {
auto* lhs = auto* lhs = Expr("lhs");
create<IdentifierExpression>(Source{}, mod.RegisterSymbol("lhs"), "lhs"); auto* rhs = Expr("rhs");
auto* rhs =
create<IdentifierExpression>(Source{}, mod.RegisterSymbol("rhs"), "rhs");
BinaryExpression r(Source{}, BinaryOp::kNone, lhs, rhs); auto* r = create<BinaryExpression>(BinaryOp::kNone, lhs, rhs);
EXPECT_FALSE(r.IsValid()); EXPECT_FALSE(r->IsValid());
} }
TEST_F(BinaryExpressionTest, ToStr) { TEST_F(BinaryExpressionTest, ToStr) {
auto* lhs = auto* lhs = Expr("lhs");
create<IdentifierExpression>(Source{}, mod.RegisterSymbol("lhs"), "lhs"); auto* rhs = Expr("rhs");
auto* rhs =
create<IdentifierExpression>(Source{}, mod.RegisterSymbol("rhs"), "rhs");
BinaryExpression r(Source{}, BinaryOp::kEqual, lhs, rhs); auto* r = create<BinaryExpression>(BinaryOp::kEqual, lhs, rhs);
std::ostringstream out; std::ostringstream out;
r.to_str(out, 2); r->to_str(out, 2);
EXPECT_EQ(demangle(out.str()), R"( Binary[not set]{ EXPECT_EQ(demangle(out.str()), R"( Binary[not set]{
Identifier[not set]{lhs} Identifier[not set]{lhs}
equal equal

View File

@ -24,13 +24,12 @@ namespace {
using BindingDecorationTest = TestHelper; using BindingDecorationTest = TestHelper;
TEST_F(BindingDecorationTest, Creation) { TEST_F(BindingDecorationTest, Creation) {
BindingDecoration d{Source{}, 2}; auto* d = create<BindingDecoration>(2);
EXPECT_EQ(2u, d.value()); EXPECT_EQ(2u, d->value());
} }
TEST_F(BindingDecorationTest, Is) { TEST_F(BindingDecorationTest, Is) {
BindingDecoration bd{Source{}, 2}; Decoration* d = create<BindingDecoration>(2);
Decoration* d = &bd;
EXPECT_TRUE(d->Is<BindingDecoration>()); EXPECT_TRUE(d->Is<BindingDecoration>());
EXPECT_FALSE(d->Is<BuiltinDecoration>()); EXPECT_FALSE(d->Is<BuiltinDecoration>());
EXPECT_FALSE(d->Is<ConstantIdDecoration>()); EXPECT_FALSE(d->Is<ConstantIdDecoration>());
@ -39,9 +38,9 @@ TEST_F(BindingDecorationTest, Is) {
} }
TEST_F(BindingDecorationTest, ToStr) { TEST_F(BindingDecorationTest, ToStr) {
BindingDecoration d{Source{}, 2}; auto* d = create<BindingDecoration>(2);
std::ostringstream out; std::ostringstream out;
d.to_str(out, 0); d->to_str(out, 0);
EXPECT_EQ(out.str(), R"(BindingDecoration{2} EXPECT_EQ(out.str(), R"(BindingDecoration{2}
)"); )");
} }

View File

@ -25,75 +25,61 @@ namespace {
using BitcastExpressionTest = TestHelper; using BitcastExpressionTest = TestHelper;
TEST_F(BitcastExpressionTest, Create) { TEST_F(BitcastExpressionTest, Create) {
type::F32 f32; auto* expr = Expr("expr");
auto* expr = create<IdentifierExpression>(Source{},
mod.RegisterSymbol("expr"), "expr");
BitcastExpression exp(Source{}, &f32, expr); auto* exp = create<BitcastExpression>(ty.f32, expr);
ASSERT_EQ(exp.type(), &f32); ASSERT_EQ(exp->type(), ty.f32);
ASSERT_EQ(exp.expr(), expr); ASSERT_EQ(exp->expr(), expr);
} }
TEST_F(BitcastExpressionTest, CreateWithSource) { TEST_F(BitcastExpressionTest, CreateWithSource) {
type::F32 f32; auto* expr = Expr("expr");
auto* expr = create<IdentifierExpression>(Source{},
mod.RegisterSymbol("expr"), "expr");
BitcastExpression exp(Source{Source::Location{20, 2}}, &f32, expr); auto* exp =
auto src = exp.source(); create<BitcastExpression>(Source{Source::Location{20, 2}}, ty.f32, expr);
auto src = exp->source();
EXPECT_EQ(src.range.begin.line, 20u); EXPECT_EQ(src.range.begin.line, 20u);
EXPECT_EQ(src.range.begin.column, 2u); EXPECT_EQ(src.range.begin.column, 2u);
} }
TEST_F(BitcastExpressionTest, IsBitcast) { TEST_F(BitcastExpressionTest, IsBitcast) {
type::F32 f32; auto* expr = Expr("expr");
auto* expr = create<IdentifierExpression>(Source{},
mod.RegisterSymbol("expr"), "expr");
BitcastExpression exp(Source{}, &f32, expr); auto* exp = create<BitcastExpression>(ty.f32, expr);
EXPECT_TRUE(exp.Is<BitcastExpression>()); EXPECT_TRUE(exp->Is<BitcastExpression>());
} }
TEST_F(BitcastExpressionTest, IsValid) { TEST_F(BitcastExpressionTest, IsValid) {
type::F32 f32; auto* expr = Expr("expr");
auto* expr = create<IdentifierExpression>(Source{},
mod.RegisterSymbol("expr"), "expr");
BitcastExpression exp(Source{}, &f32, expr); auto* exp = create<BitcastExpression>(ty.f32, expr);
EXPECT_TRUE(exp.IsValid()); EXPECT_TRUE(exp->IsValid());
} }
TEST_F(BitcastExpressionTest, IsValid_MissingType) { TEST_F(BitcastExpressionTest, IsValid_MissingType) {
auto* expr = create<IdentifierExpression>(Source{}, auto* expr = Expr("expr");
mod.RegisterSymbol("expr"), "expr");
BitcastExpression exp(Source{}, nullptr, expr); auto* exp = create<BitcastExpression>(nullptr, expr);
EXPECT_FALSE(exp.IsValid()); EXPECT_FALSE(exp->IsValid());
} }
TEST_F(BitcastExpressionTest, IsValid_MissingExpr) { TEST_F(BitcastExpressionTest, IsValid_MissingExpr) {
type::F32 f32; auto* exp = create<BitcastExpression>(ty.f32, nullptr);
EXPECT_FALSE(exp->IsValid());
BitcastExpression exp(Source{}, &f32, nullptr);
EXPECT_FALSE(exp.IsValid());
} }
TEST_F(BitcastExpressionTest, IsValid_InvalidExpr) { TEST_F(BitcastExpressionTest, IsValid_InvalidExpr) {
type::F32 f32; auto* expr = Expr("");
auto* expr = auto* e = create<BitcastExpression>(ty.f32, expr);
create<IdentifierExpression>(Source{}, mod.RegisterSymbol(""), ""); EXPECT_FALSE(e->IsValid());
BitcastExpression e(Source{}, &f32, expr);
EXPECT_FALSE(e.IsValid());
} }
TEST_F(BitcastExpressionTest, ToStr) { TEST_F(BitcastExpressionTest, ToStr) {
type::F32 f32; auto* expr = Expr("expr");
auto* expr = create<IdentifierExpression>(Source{},
mod.RegisterSymbol("expr"), "expr");
BitcastExpression exp(Source{}, &f32, expr); auto* exp = create<BitcastExpression>(ty.f32, expr);
std::ostringstream out; std::ostringstream out;
exp.to_str(out, 2); exp->to_str(out, 2);
EXPECT_EQ(demangle(out.str()), R"( Bitcast[not set]<__f32>{ EXPECT_EQ(demangle(out.str()), R"( Bitcast[not set]<__f32>{
Identifier[not set]{expr} Identifier[not set]{expr}

View File

@ -28,66 +28,66 @@ namespace {
using BlockStatementTest = TestHelper; using BlockStatementTest = TestHelper;
TEST_F(BlockStatementTest, Creation) { TEST_F(BlockStatementTest, Creation) {
auto* d = create<DiscardStatement>(Source{}); auto* d = create<DiscardStatement>();
auto* ptr = d; auto* ptr = d;
BlockStatement b(Source{}, StatementList{d}); auto* b = create<BlockStatement>(StatementList{d});
ASSERT_EQ(b.size(), 1u); ASSERT_EQ(b->size(), 1u);
EXPECT_EQ(b[0], ptr); EXPECT_EQ((*b)[0], ptr);
} }
TEST_F(BlockStatementTest, Creation_WithSource) { TEST_F(BlockStatementTest, Creation_WithSource) {
BlockStatement b(Source{Source::Location{20, 2}}, ast::StatementList{}); auto* b = create<BlockStatement>(Source{Source::Location{20, 2}},
auto src = b.source(); ast::StatementList{});
auto src = b->source();
EXPECT_EQ(src.range.begin.line, 20u); EXPECT_EQ(src.range.begin.line, 20u);
EXPECT_EQ(src.range.begin.column, 2u); EXPECT_EQ(src.range.begin.column, 2u);
} }
TEST_F(BlockStatementTest, IsBlock) { TEST_F(BlockStatementTest, IsBlock) {
BlockStatement b(Source{}, ast::StatementList{}); auto* b = create<BlockStatement>(ast::StatementList{});
EXPECT_TRUE(b.Is<BlockStatement>()); EXPECT_TRUE(b->Is<BlockStatement>());
} }
TEST_F(BlockStatementTest, IsValid) { TEST_F(BlockStatementTest, IsValid) {
BlockStatement b(Source{}, ast::StatementList{ auto* b = create<BlockStatement>(ast::StatementList{
create<DiscardStatement>(Source{}), create<DiscardStatement>(),
}); });
EXPECT_TRUE(b.IsValid()); EXPECT_TRUE(b->IsValid());
} }
TEST_F(BlockStatementTest, IsValid_Empty) { TEST_F(BlockStatementTest, IsValid_Empty) {
BlockStatement b(Source{}, ast::StatementList{}); auto* b = create<BlockStatement>(ast::StatementList{});
EXPECT_TRUE(b.IsValid()); EXPECT_TRUE(b->IsValid());
} }
TEST_F(BlockStatementTest, IsValid_NullBodyStatement) { TEST_F(BlockStatementTest, IsValid_NullBodyStatement) {
BlockStatement b(Source{}, ast::StatementList{ auto* b = create<BlockStatement>(ast::StatementList{
create<DiscardStatement>(Source{}), create<DiscardStatement>(),
nullptr, nullptr,
}); });
EXPECT_FALSE(b.IsValid()); EXPECT_FALSE(b->IsValid());
} }
TEST_F(BlockStatementTest, IsValid_InvalidBodyStatement) { TEST_F(BlockStatementTest, IsValid_InvalidBodyStatement) {
BlockStatement b( auto* b = create<BlockStatement>(
Source{},
ast::StatementList{ ast::StatementList{
create<IfStatement>(Source{}, nullptr, create<IfStatement>(nullptr, create<BlockStatement>(StatementList{}),
create<BlockStatement>(Source{}, StatementList{}),
ElseStatementList{}), ElseStatementList{}),
}); });
EXPECT_FALSE(b.IsValid()); EXPECT_FALSE(b->IsValid());
} }
TEST_F(BlockStatementTest, ToStr) { TEST_F(BlockStatementTest, ToStr) {
BlockStatement b(Source{}, ast::StatementList{ auto* b = create<BlockStatement>(ast::StatementList{
create<DiscardStatement>(Source{}), create<DiscardStatement>(),
}); });
std::ostringstream out; std::ostringstream out;
b.to_str(out, 2); b->to_str(out, 2);
EXPECT_EQ(out.str(), R"( Block{ EXPECT_EQ(out.str(), R"( Block{
Discard{} Discard{}
} }

View File

@ -29,24 +29,23 @@ using BoolLiteralTest = TestHelper;
TEST_F(BoolLiteralTest, True) { TEST_F(BoolLiteralTest, True) {
type::Bool bool_type; type::Bool bool_type;
BoolLiteral b{Source{}, &bool_type, true}; auto* b = create<BoolLiteral>(&bool_type, true);
ASSERT_TRUE(b.Is<BoolLiteral>()); ASSERT_TRUE(b->Is<BoolLiteral>());
ASSERT_TRUE(b.IsTrue()); ASSERT_TRUE(b->IsTrue());
ASSERT_FALSE(b.IsFalse()); ASSERT_FALSE(b->IsFalse());
} }
TEST_F(BoolLiteralTest, False) { TEST_F(BoolLiteralTest, False) {
type::Bool bool_type; type::Bool bool_type;
BoolLiteral b{Source{}, &bool_type, false}; auto* b = create<BoolLiteral>(&bool_type, false);
ASSERT_TRUE(b.Is<BoolLiteral>()); ASSERT_TRUE(b->Is<BoolLiteral>());
ASSERT_FALSE(b.IsTrue()); ASSERT_FALSE(b->IsTrue());
ASSERT_TRUE(b.IsFalse()); ASSERT_TRUE(b->IsFalse());
} }
TEST_F(BoolLiteralTest, Is) { TEST_F(BoolLiteralTest, Is) {
type::Bool bool_type; type::Bool bool_type;
BoolLiteral b{Source{}, &bool_type, false}; ast::Literal* l = create<BoolLiteral>(&bool_type, false);
Literal* l = &b;
EXPECT_TRUE(l->Is<BoolLiteral>()); EXPECT_TRUE(l->Is<BoolLiteral>());
EXPECT_FALSE(l->Is<SintLiteral>()); EXPECT_FALSE(l->Is<SintLiteral>());
EXPECT_FALSE(l->Is<FloatLiteral>()); EXPECT_FALSE(l->Is<FloatLiteral>());
@ -57,11 +56,11 @@ TEST_F(BoolLiteralTest, Is) {
TEST_F(BoolLiteralTest, ToStr) { TEST_F(BoolLiteralTest, ToStr) {
type::Bool bool_type; type::Bool bool_type;
BoolLiteral t{Source{}, &bool_type, true}; auto* t = create<BoolLiteral>(&bool_type, true);
BoolLiteral f{Source{}, &bool_type, false}; auto* f = create<BoolLiteral>(&bool_type, false);
EXPECT_EQ(t.to_str(), "true"); EXPECT_EQ(t->to_str(), "true");
EXPECT_EQ(f.to_str(), "false"); EXPECT_EQ(f->to_str(), "false");
} }
} // namespace } // namespace

View File

@ -23,26 +23,26 @@ namespace {
using BreakStatementTest = TestHelper; using BreakStatementTest = TestHelper;
TEST_F(BreakStatementTest, Creation_WithSource) { TEST_F(BreakStatementTest, Creation_WithSource) {
BreakStatement stmt(Source{Source::Location{20, 2}}); auto* stmt = create<BreakStatement>(Source{Source::Location{20, 2}});
auto src = stmt.source(); auto src = stmt->source();
EXPECT_EQ(src.range.begin.line, 20u); EXPECT_EQ(src.range.begin.line, 20u);
EXPECT_EQ(src.range.begin.column, 2u); EXPECT_EQ(src.range.begin.column, 2u);
} }
TEST_F(BreakStatementTest, IsBreak) { TEST_F(BreakStatementTest, IsBreak) {
BreakStatement stmt(Source{}); auto* stmt = create<BreakStatement>();
EXPECT_TRUE(stmt.Is<BreakStatement>()); EXPECT_TRUE(stmt->Is<BreakStatement>());
} }
TEST_F(BreakStatementTest, IsValid) { TEST_F(BreakStatementTest, IsValid) {
BreakStatement stmt(Source{}); auto* stmt = create<BreakStatement>();
EXPECT_TRUE(stmt.IsValid()); EXPECT_TRUE(stmt->IsValid());
} }
TEST_F(BreakStatementTest, ToStr) { TEST_F(BreakStatementTest, ToStr) {
BreakStatement stmt(Source{}); auto* stmt = create<BreakStatement>();
std::ostringstream out; std::ostringstream out;
stmt.to_str(out, 2); stmt->to_str(out, 2);
EXPECT_EQ(out.str(), R"( Break{} EXPECT_EQ(out.str(), R"( Break{}
)"); )");
} }

View File

@ -40,8 +40,20 @@ Variable* Builder::Var(const std::string& name,
type::Type* type, type::Type* type,
Expression* constructor, Expression* constructor,
VariableDecorationList decorations) { VariableDecorationList decorations) {
auto* var = create<Variable>(Source{}, name, storage, type, false, auto* var =
constructor, decorations); create<Variable>(name, storage, type, false, constructor, decorations);
OnVariableBuilt(var);
return var;
}
Variable* Builder::Var(const Source& source,
const std::string& name,
StorageClass storage,
type::Type* type,
Expression* constructor,
VariableDecorationList decorations) {
auto* var = create<Variable>(source, name, storage, type, false, constructor,
decorations);
OnVariableBuilt(var); OnVariableBuilt(var);
return var; return var;
} }
@ -57,8 +69,8 @@ Variable* Builder::Const(const std::string& name,
type::Type* type, type::Type* type,
Expression* constructor, Expression* constructor,
VariableDecorationList decorations) { VariableDecorationList decorations) {
auto* var = create<Variable>(Source{}, name, storage, type, true, constructor, auto* var =
decorations); create<Variable>(name, storage, type, true, constructor, decorations);
OnVariableBuilt(var); OnVariableBuilt(var);
return var; return var;
} }

View File

@ -464,6 +464,20 @@ class Builder {
Expression* constructor, Expression* constructor,
VariableDecorationList decorations); VariableDecorationList decorations);
/// @param source the variable source
/// @param name the variable name
/// @param storage the variable storage class
/// @param type the variable type
/// @param constructor constructor expression
/// @param decorations variable decorations
/// @returns a `Variable` with the given name, storage and type
Variable* Var(const Source& source,
const std::string& name,
StorageClass storage,
type::Type* type,
Expression* constructor,
VariableDecorationList decorations);
/// @param name the variable name /// @param name the variable name
/// @param storage the variable storage class /// @param storage the variable storage class
/// @param type the variable type /// @param type the variable type

View File

@ -8,7 +8,7 @@
// //
// Unless required by applicable law or agreed to in writing, software // Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, // distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied->
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
@ -24,13 +24,12 @@ namespace {
using BuiltinDecorationTest = TestHelper; using BuiltinDecorationTest = TestHelper;
TEST_F(BuiltinDecorationTest, Creation) { TEST_F(BuiltinDecorationTest, Creation) {
BuiltinDecoration d{Source{}, Builtin::kFragDepth}; auto* d = create<BuiltinDecoration>(Builtin::kFragDepth);
EXPECT_EQ(Builtin::kFragDepth, d.value()); EXPECT_EQ(Builtin::kFragDepth, d->value());
} }
TEST_F(BuiltinDecorationTest, Is) { TEST_F(BuiltinDecorationTest, Is) {
BuiltinDecoration bd{Source{}, Builtin::kFragDepth}; Decoration* d = create<BuiltinDecoration>(Builtin::kFragDepth);
Decoration* d = &bd;
EXPECT_FALSE(d->Is<BindingDecoration>()); EXPECT_FALSE(d->Is<BindingDecoration>());
EXPECT_TRUE(d->Is<BuiltinDecoration>()); EXPECT_TRUE(d->Is<BuiltinDecoration>());
EXPECT_FALSE(d->Is<ConstantIdDecoration>()); EXPECT_FALSE(d->Is<ConstantIdDecoration>());
@ -39,9 +38,9 @@ TEST_F(BuiltinDecorationTest, Is) {
} }
TEST_F(BuiltinDecorationTest, ToStr) { TEST_F(BuiltinDecorationTest, ToStr) {
BuiltinDecoration d{Source{}, Builtin::kFragDepth}; auto* d = create<BuiltinDecoration>(Builtin::kFragDepth);
std::ostringstream out; std::ostringstream out;
d.to_str(out, 0); d->to_str(out, 0);
EXPECT_EQ(out.str(), R"(BuiltinDecoration{frag_depth} EXPECT_EQ(out.str(), R"(BuiltinDecoration{frag_depth}
)"); )");
} }

View File

@ -24,93 +24,80 @@ namespace {
using CallExpressionTest = TestHelper; using CallExpressionTest = TestHelper;
TEST_F(CallExpressionTest, Creation) { TEST_F(CallExpressionTest, Creation) {
auto* func = create<IdentifierExpression>(Source{}, auto* func = Expr("func");
mod.RegisterSymbol("func"), "func");
ExpressionList params; ExpressionList params;
params.push_back(create<IdentifierExpression>( params.push_back(Expr("param1"));
Source{}, mod.RegisterSymbol("param1"), "param1")); params.push_back(Expr("param2"));
params.push_back(create<IdentifierExpression>(
Source{}, mod.RegisterSymbol("param2"), "param2"));
CallExpression stmt(Source{}, func, params); auto* stmt = create<CallExpression>(func, params);
EXPECT_EQ(stmt.func(), func); EXPECT_EQ(stmt->func(), func);
const auto& vec = stmt.params(); const auto& vec = stmt->params();
ASSERT_EQ(vec.size(), 2u); ASSERT_EQ(vec.size(), 2u);
EXPECT_EQ(vec[0], params[0]); EXPECT_EQ(vec[0], params[0]);
EXPECT_EQ(vec[1], params[1]); EXPECT_EQ(vec[1], params[1]);
} }
TEST_F(CallExpressionTest, Creation_WithSource) { TEST_F(CallExpressionTest, Creation_WithSource) {
auto* func = create<IdentifierExpression>(Source{}, auto* func = Expr("func");
mod.RegisterSymbol("func"), "func"); auto* stmt = create<CallExpression>(Source{Source::Location{20, 2}}, func,
CallExpression stmt(Source{Source::Location{20, 2}}, func, {}); ExpressionList{});
auto src = stmt.source(); auto src = stmt->source();
EXPECT_EQ(src.range.begin.line, 20u); EXPECT_EQ(src.range.begin.line, 20u);
EXPECT_EQ(src.range.begin.column, 2u); EXPECT_EQ(src.range.begin.column, 2u);
} }
TEST_F(CallExpressionTest, IsCall) { TEST_F(CallExpressionTest, IsCall) {
auto* func = create<IdentifierExpression>(Source{}, auto* func = Expr("func");
mod.RegisterSymbol("func"), "func"); auto* stmt = create<CallExpression>(func, ExpressionList{});
CallExpression stmt(Source{}, func, {}); EXPECT_TRUE(stmt->Is<CallExpression>());
EXPECT_TRUE(stmt.Is<CallExpression>());
} }
TEST_F(CallExpressionTest, IsValid) { TEST_F(CallExpressionTest, IsValid) {
auto* func = create<IdentifierExpression>(Source{}, auto* func = Expr("func");
mod.RegisterSymbol("func"), "func"); auto* stmt = create<CallExpression>(func, ExpressionList{});
CallExpression stmt(Source{}, func, {}); EXPECT_TRUE(stmt->IsValid());
EXPECT_TRUE(stmt.IsValid());
} }
TEST_F(CallExpressionTest, IsValid_MissingFunction) { TEST_F(CallExpressionTest, IsValid_MissingFunction) {
CallExpression stmt(Source{}, nullptr, {}); auto* stmt = create<CallExpression>(nullptr, ExpressionList{});
EXPECT_FALSE(stmt.IsValid()); EXPECT_FALSE(stmt->IsValid());
} }
TEST_F(CallExpressionTest, IsValid_NullParam) { TEST_F(CallExpressionTest, IsValid_NullParam) {
auto* func = create<IdentifierExpression>(Source{}, auto* func = Expr("func");
mod.RegisterSymbol("func"), "func");
ExpressionList params; ExpressionList params;
params.push_back(create<IdentifierExpression>( params.push_back(Expr("param1"));
Source{}, mod.RegisterSymbol("param1"), "param1"));
params.push_back(nullptr); params.push_back(nullptr);
params.push_back(create<IdentifierExpression>( params.push_back(Expr("param2"));
Source{}, mod.RegisterSymbol("param2"), "param2"));
CallExpression stmt(Source{}, func, params); auto* stmt = create<CallExpression>(func, params);
EXPECT_FALSE(stmt.IsValid()); EXPECT_FALSE(stmt->IsValid());
} }
TEST_F(CallExpressionTest, IsValid_InvalidFunction) { TEST_F(CallExpressionTest, IsValid_InvalidFunction) {
auto* func = auto* func = Expr("");
create<IdentifierExpression>(Source{}, mod.RegisterSymbol(""), "");
ExpressionList params; ExpressionList params;
params.push_back(create<IdentifierExpression>( params.push_back(Expr("param1"));
Source{}, mod.RegisterSymbol("param1"), "param1"));
CallExpression stmt(Source{}, func, params); auto* stmt = create<CallExpression>(func, params);
EXPECT_FALSE(stmt.IsValid()); EXPECT_FALSE(stmt->IsValid());
} }
TEST_F(CallExpressionTest, IsValid_InvalidParam) { TEST_F(CallExpressionTest, IsValid_InvalidParam) {
auto* func = create<IdentifierExpression>(Source{}, auto* func = Expr("func");
mod.RegisterSymbol("func"), "func");
ExpressionList params; ExpressionList params;
params.push_back( params.push_back(Expr(""));
create<IdentifierExpression>(Source{}, mod.RegisterSymbol(""), ""));
CallExpression stmt(Source{}, func, params); auto* stmt = create<CallExpression>(func, params);
EXPECT_FALSE(stmt.IsValid()); EXPECT_FALSE(stmt->IsValid());
} }
TEST_F(CallExpressionTest, ToStr_NoParams) { TEST_F(CallExpressionTest, ToStr_NoParams) {
auto* func = create<IdentifierExpression>(Source{}, auto* func = Expr("func");
mod.RegisterSymbol("func"), "func"); auto* stmt = create<CallExpression>(func, ExpressionList{});
CallExpression stmt(Source{}, func, {});
std::ostringstream out; std::ostringstream out;
stmt.to_str(out, 2); stmt->to_str(out, 2);
EXPECT_EQ(demangle(out.str()), R"( Call[not set]{ EXPECT_EQ(demangle(out.str()), R"( Call[not set]{
Identifier[not set]{func} Identifier[not set]{func}
( (
@ -120,17 +107,14 @@ TEST_F(CallExpressionTest, ToStr_NoParams) {
} }
TEST_F(CallExpressionTest, ToStr_WithParams) { TEST_F(CallExpressionTest, ToStr_WithParams) {
auto* func = create<IdentifierExpression>(Source{}, auto* func = Expr("func");
mod.RegisterSymbol("func"), "func");
ExpressionList params; ExpressionList params;
params.push_back(create<IdentifierExpression>( params.push_back(Expr("param1"));
Source{}, mod.RegisterSymbol("param1"), "param1")); params.push_back(Expr("param2"));
params.push_back(create<IdentifierExpression>(
Source{}, mod.RegisterSymbol("param2"), "param2"));
CallExpression stmt(Source{}, func, params); auto* stmt = create<CallExpression>(func, params);
std::ostringstream out; std::ostringstream out;
stmt.to_str(out, 2); stmt->to_str(out, 2);
EXPECT_EQ(demangle(out.str()), R"( Call[not set]{ EXPECT_EQ(demangle(out.str()), R"( Call[not set]{
Identifier[not set]{func} Identifier[not set]{func}
( (

View File

@ -25,52 +25,40 @@ namespace {
using CallStatementTest = TestHelper; using CallStatementTest = TestHelper;
TEST_F(CallStatementTest, Creation) { TEST_F(CallStatementTest, Creation) {
auto* expr = auto* expr = create<CallExpression>(Expr("func"), ExpressionList{});
create<CallExpression>(Source{},
create<IdentifierExpression>(
Source{}, mod.RegisterSymbol("func"), "func"),
ExpressionList{});
CallStatement c(Source{}, expr); auto* c = create<CallStatement>(expr);
EXPECT_EQ(c.expr(), expr); EXPECT_EQ(c->expr(), expr);
} }
TEST_F(CallStatementTest, IsCall) { TEST_F(CallStatementTest, IsCall) {
CallStatement c(Source{}, nullptr); auto* c = create<CallStatement>(nullptr);
EXPECT_TRUE(c.Is<CallStatement>()); EXPECT_TRUE(c->Is<CallStatement>());
} }
TEST_F(CallStatementTest, IsValid) { TEST_F(CallStatementTest, IsValid) {
CallStatement c( auto* c = create<CallStatement>(
Source{}, create<CallExpression>(Expr("func"), ExpressionList{}));
create<CallExpression>(Source{}, EXPECT_TRUE(c->IsValid());
create<IdentifierExpression>(
Source{}, mod.RegisterSymbol("func"), "func"),
ExpressionList{}));
EXPECT_TRUE(c.IsValid());
} }
TEST_F(CallStatementTest, IsValid_MissingExpr) { TEST_F(CallStatementTest, IsValid_MissingExpr) {
CallStatement c(Source{}, nullptr); auto* c = create<CallStatement>(nullptr);
EXPECT_FALSE(c.IsValid()); EXPECT_FALSE(c->IsValid());
} }
TEST_F(CallStatementTest, IsValid_InvalidExpr) { TEST_F(CallStatementTest, IsValid_InvalidExpr) {
CallExpression stmt(Source{}, nullptr, {}); auto* c = create<CallStatement>(
CallStatement c(Source{}, &stmt); create<CallExpression>(nullptr, ast::ExpressionList{}));
EXPECT_FALSE(c.IsValid()); EXPECT_FALSE(c->IsValid());
} }
TEST_F(CallStatementTest, ToStr) { TEST_F(CallStatementTest, ToStr) {
CallStatement c( auto* c = create<CallStatement>(
Source{}, create<CallExpression>(Expr("func"), ExpressionList{}));
create<CallExpression>(Source{},
create<IdentifierExpression>(
Source{}, mod.RegisterSymbol("func"), "func"),
ExpressionList{}));
std::ostringstream out; std::ostringstream out;
c.to_str(out, 2); c->to_str(out, 2);
EXPECT_EQ(demangle(out.str()), R"( Call[not set]{ EXPECT_EQ(demangle(out.str()), R"( Call[not set]{
Identifier[not set]{func} Identifier[not set]{func}
( (

View File

@ -29,128 +29,113 @@ namespace {
using CaseStatementTest = TestHelper; using CaseStatementTest = TestHelper;
TEST_F(CaseStatementTest, Creation_i32) { TEST_F(CaseStatementTest, Creation_i32) {
type::I32 i32;
CaseSelectorList b; CaseSelectorList b;
auto* selector = create<SintLiteral>(Source{}, &i32, 2); auto* selector = create<SintLiteral>(ty.i32, 2);
b.push_back(selector); b.push_back(selector);
auto* discard = create<DiscardStatement>(Source{}); auto* discard = create<DiscardStatement>();
auto* body = create<BlockStatement>(Source{}, StatementList{discard}); auto* body = create<BlockStatement>(StatementList{discard});
CaseStatement c(Source{}, b, body); auto* c = create<CaseStatement>(b, body);
ASSERT_EQ(c.selectors().size(), 1u); ASSERT_EQ(c->selectors().size(), 1u);
EXPECT_EQ(c.selectors()[0], selector); EXPECT_EQ(c->selectors()[0], selector);
ASSERT_EQ(c.body()->size(), 1u); ASSERT_EQ(c->body()->size(), 1u);
EXPECT_EQ(c.body()->get(0), discard); EXPECT_EQ(c->body()->get(0), discard);
} }
TEST_F(CaseStatementTest, Creation_u32) { TEST_F(CaseStatementTest, Creation_u32) {
type::U32 u32;
CaseSelectorList b; CaseSelectorList b;
auto* selector = create<SintLiteral>(Source{}, &u32, 2); auto* selector = create<SintLiteral>(ty.u32, 2);
b.push_back(selector); b.push_back(selector);
auto* discard = create<DiscardStatement>(Source{}); auto* discard = create<DiscardStatement>();
auto* body = create<BlockStatement>(Source{}, StatementList{discard}); auto* body = create<BlockStatement>(StatementList{discard});
CaseStatement c(Source{}, b, body); auto* c = create<CaseStatement>(b, body);
ASSERT_EQ(c.selectors().size(), 1u); ASSERT_EQ(c->selectors().size(), 1u);
EXPECT_EQ(c.selectors()[0], selector); EXPECT_EQ(c->selectors()[0], selector);
ASSERT_EQ(c.body()->size(), 1u); ASSERT_EQ(c->body()->size(), 1u);
EXPECT_EQ(c.body()->get(0), discard); EXPECT_EQ(c->body()->get(0), discard);
} }
TEST_F(CaseStatementTest, Creation_WithSource) { TEST_F(CaseStatementTest, Creation_WithSource) {
type::I32 i32;
CaseSelectorList b; CaseSelectorList b;
b.push_back(create<SintLiteral>(Source{}, &i32, 2)); b.push_back(create<SintLiteral>(ty.i32, 2));
auto* body = auto* body = create<BlockStatement>(StatementList{
create<BlockStatement>(Source{}, StatementList{ create<DiscardStatement>(),
create<DiscardStatement>(Source{}),
}); });
CaseStatement c(Source{Source::Location{20, 2}}, b, body); auto* c = create<CaseStatement>(Source{Source::Location{20, 2}}, b, body);
auto src = c.source(); auto src = c->source();
EXPECT_EQ(src.range.begin.line, 20u); EXPECT_EQ(src.range.begin.line, 20u);
EXPECT_EQ(src.range.begin.column, 2u); EXPECT_EQ(src.range.begin.column, 2u);
} }
TEST_F(CaseStatementTest, IsDefault_WithoutSelectors) { TEST_F(CaseStatementTest, IsDefault_WithoutSelectors) {
auto* body = auto* body = create<BlockStatement>(StatementList{
create<BlockStatement>(Source{}, StatementList{ create<DiscardStatement>(),
create<DiscardStatement>(Source{}),
}); });
CaseStatement c(Source{}, CaseSelectorList{}, body); auto* c = create<CaseStatement>(CaseSelectorList{}, body);
EXPECT_TRUE(c.IsDefault()); EXPECT_TRUE(c->IsDefault());
} }
TEST_F(CaseStatementTest, IsDefault_WithSelectors) { TEST_F(CaseStatementTest, IsDefault_WithSelectors) {
type::I32 i32;
CaseSelectorList b; CaseSelectorList b;
b.push_back(create<SintLiteral>(Source{}, &i32, 2)); b.push_back(create<SintLiteral>(ty.i32, 2));
CaseStatement c(Source{}, b, auto* c = create<CaseStatement>(b, create<BlockStatement>(StatementList{}));
create<BlockStatement>(Source{}, StatementList{})); EXPECT_FALSE(c->IsDefault());
EXPECT_FALSE(c.IsDefault());
} }
TEST_F(CaseStatementTest, IsCase) { TEST_F(CaseStatementTest, IsCase) {
CaseStatement c(Source{}, CaseSelectorList{}, auto* c = create<CaseStatement>(CaseSelectorList{},
create<BlockStatement>(Source{}, StatementList{})); create<BlockStatement>(StatementList{}));
EXPECT_TRUE(c.Is<CaseStatement>()); EXPECT_TRUE(c->Is<CaseStatement>());
} }
TEST_F(CaseStatementTest, IsValid) { TEST_F(CaseStatementTest, IsValid) {
CaseStatement c(Source{}, CaseSelectorList{}, auto* c = create<CaseStatement>(CaseSelectorList{},
create<BlockStatement>(Source{}, StatementList{})); create<BlockStatement>(StatementList{}));
EXPECT_TRUE(c.IsValid()); EXPECT_TRUE(c->IsValid());
} }
TEST_F(CaseStatementTest, IsValid_NullBodyStatement) { TEST_F(CaseStatementTest, IsValid_NullBodyStatement) {
type::I32 i32;
CaseSelectorList b; CaseSelectorList b;
b.push_back(create<SintLiteral>(Source{}, &i32, 2)); b.push_back(create<SintLiteral>(ty.i32, 2));
auto* body = auto* body = create<BlockStatement>(StatementList{
create<BlockStatement>(Source{}, StatementList{ create<DiscardStatement>(),
create<DiscardStatement>(Source{}),
nullptr, nullptr,
}); });
CaseStatement c(Source{}, b, body); auto* c = create<CaseStatement>(b, body);
EXPECT_FALSE(c.IsValid()); EXPECT_FALSE(c->IsValid());
} }
TEST_F(CaseStatementTest, IsValid_InvalidBodyStatement) { TEST_F(CaseStatementTest, IsValid_InvalidBodyStatement) {
type::I32 i32;
CaseSelectorList b; CaseSelectorList b;
b.push_back(create<SintLiteral>(Source{}, &i32, 2)); b.push_back(create<SintLiteral>(ty.i32, 2));
auto* body = create<BlockStatement>( auto* body = create<BlockStatement>(
Source{},
StatementList{ StatementList{
create<IfStatement>(Source{}, nullptr, create<IfStatement>(nullptr, create<BlockStatement>(StatementList{}),
create<BlockStatement>(Source{}, StatementList{}),
ElseStatementList{}), ElseStatementList{}),
}); });
CaseStatement c(Source{}, {b}, body); auto* c = create<CaseStatement>(CaseSelectorList{b}, body);
EXPECT_FALSE(c.IsValid()); EXPECT_FALSE(c->IsValid());
} }
TEST_F(CaseStatementTest, ToStr_WithSelectors_i32) { TEST_F(CaseStatementTest, ToStr_WithSelectors_i32) {
type::I32 i32;
CaseSelectorList b; CaseSelectorList b;
b.push_back(create<SintLiteral>(Source{}, &i32, -2)); b.push_back(create<SintLiteral>(ty.i32, -2));
auto* body = auto* body = create<BlockStatement>(StatementList{
create<BlockStatement>(Source{}, StatementList{ create<DiscardStatement>(),
create<DiscardStatement>(Source{}),
}); });
CaseStatement c(Source{}, {b}, body); auto* c = create<CaseStatement>(CaseSelectorList{b}, body);
std::ostringstream out; std::ostringstream out;
c.to_str(out, 2); c->to_str(out, 2);
EXPECT_EQ(out.str(), R"( Case -2{ EXPECT_EQ(out.str(), R"( Case -2{
Discard{} Discard{}
} }
@ -158,18 +143,16 @@ TEST_F(CaseStatementTest, ToStr_WithSelectors_i32) {
} }
TEST_F(CaseStatementTest, ToStr_WithSelectors_u32) { TEST_F(CaseStatementTest, ToStr_WithSelectors_u32) {
type::U32 u32;
CaseSelectorList b; CaseSelectorList b;
b.push_back(create<UintLiteral>(Source{}, &u32, 2)); b.push_back(create<UintLiteral>(ty.u32, 2));
auto* body = auto* body = create<BlockStatement>(StatementList{
create<BlockStatement>(Source{}, StatementList{ create<DiscardStatement>(),
create<DiscardStatement>(Source{}),
}); });
CaseStatement c(Source{}, {b}, body); auto* c = create<CaseStatement>(CaseSelectorList{b}, body);
std::ostringstream out; std::ostringstream out;
c.to_str(out, 2); c->to_str(out, 2);
EXPECT_EQ(out.str(), R"( Case 2{ EXPECT_EQ(out.str(), R"( Case 2{
Discard{} Discard{}
} }
@ -177,20 +160,17 @@ TEST_F(CaseStatementTest, ToStr_WithSelectors_u32) {
} }
TEST_F(CaseStatementTest, ToStr_WithMultipleSelectors) { TEST_F(CaseStatementTest, ToStr_WithMultipleSelectors) {
type::I32 i32;
CaseSelectorList b; CaseSelectorList b;
b.push_back(create<SintLiteral>(Source{}, &i32, 1)); b.push_back(create<SintLiteral>(ty.i32, 1));
b.push_back(create<SintLiteral>(Source{}, &i32, 2)); b.push_back(create<SintLiteral>(ty.i32, 2));
auto* body = auto* body = create<BlockStatement>(StatementList{
create<BlockStatement>(Source{}, StatementList{ create<DiscardStatement>(),
create<DiscardStatement>(Source{}),
}); });
CaseStatement c(Source{}, b, body); auto* c = create<CaseStatement>(b, body);
std::ostringstream out; std::ostringstream out;
c.to_str(out, 2); c->to_str(out, 2);
EXPECT_EQ(out.str(), R"( Case 1, 2{ EXPECT_EQ(out.str(), R"( Case 1, 2{
Discard{} Discard{}
} }
@ -198,14 +178,13 @@ TEST_F(CaseStatementTest, ToStr_WithMultipleSelectors) {
} }
TEST_F(CaseStatementTest, ToStr_WithoutSelectors) { TEST_F(CaseStatementTest, ToStr_WithoutSelectors) {
auto* body = auto* body = create<BlockStatement>(StatementList{
create<BlockStatement>(Source{}, StatementList{ create<DiscardStatement>(),
create<DiscardStatement>(Source{}),
}); });
CaseStatement c(Source{}, CaseSelectorList{}, body); auto* c = create<CaseStatement>(CaseSelectorList{}, body);
std::ostringstream out; std::ostringstream out;
c.to_str(out, 2); c->to_str(out, 2);
EXPECT_EQ(out.str(), R"( Default{ EXPECT_EQ(out.str(), R"( Default{
Discard{} Discard{}
} }

View File

@ -78,7 +78,7 @@ class CloneContext {
/// @return the cloned source /// @return the cloned source
Source Clone(const Source& s) { return s; } Source Clone(const Source& s) { return s; }
/// Clones each of the elements of the vector `v` into the module #mod. /// Clones each of the elements of the vector `v` into the module #mod->
/// @param v the vector to clone /// @param v the vector to clone
/// @return the cloned vector /// @return the cloned vector
template <typename T> template <typename T>

View File

@ -23,13 +23,12 @@ namespace {
using ConstantIdDecorationTest = TestHelper; using ConstantIdDecorationTest = TestHelper;
TEST_F(ConstantIdDecorationTest, Creation) { TEST_F(ConstantIdDecorationTest, Creation) {
ConstantIdDecoration d{Source{}, 12}; auto* d = create<ConstantIdDecoration>(12);
EXPECT_EQ(12u, d.value()); EXPECT_EQ(12u, d->value());
} }
TEST_F(ConstantIdDecorationTest, Is) { TEST_F(ConstantIdDecorationTest, Is) {
ConstantIdDecoration cd{Source{}, 27}; Decoration* d = create<ConstantIdDecoration>(27);
Decoration* d = &cd;
EXPECT_FALSE(d->Is<BindingDecoration>()); EXPECT_FALSE(d->Is<BindingDecoration>());
EXPECT_FALSE(d->Is<BuiltinDecoration>()); EXPECT_FALSE(d->Is<BuiltinDecoration>());
EXPECT_TRUE(d->Is<ConstantIdDecoration>()); EXPECT_TRUE(d->Is<ConstantIdDecoration>());
@ -38,9 +37,9 @@ TEST_F(ConstantIdDecorationTest, Is) {
} }
TEST_F(ConstantIdDecorationTest, ToStr) { TEST_F(ConstantIdDecorationTest, ToStr) {
ConstantIdDecoration d{Source{}, 1200}; auto* d = create<ConstantIdDecoration>(1200);
std::ostringstream out; std::ostringstream out;
d.to_str(out, 0); d->to_str(out, 0);
EXPECT_EQ(out.str(), R"(ConstantIdDecoration{1200} EXPECT_EQ(out.str(), R"(ConstantIdDecoration{1200}
)"); )");
} }

View File

@ -23,26 +23,26 @@ namespace {
using ContinueStatementTest = TestHelper; using ContinueStatementTest = TestHelper;
TEST_F(ContinueStatementTest, Creation_WithSource) { TEST_F(ContinueStatementTest, Creation_WithSource) {
ContinueStatement stmt(Source{Source::Location{20, 2}}); auto* stmt = create<ContinueStatement>(Source{Source::Location{20, 2}});
auto src = stmt.source(); auto src = stmt->source();
EXPECT_EQ(src.range.begin.line, 20u); EXPECT_EQ(src.range.begin.line, 20u);
EXPECT_EQ(src.range.begin.column, 2u); EXPECT_EQ(src.range.begin.column, 2u);
} }
TEST_F(ContinueStatementTest, IsContinue) { TEST_F(ContinueStatementTest, IsContinue) {
ContinueStatement stmt(Source{}); auto* stmt = create<ContinueStatement>();
EXPECT_TRUE(stmt.Is<ContinueStatement>()); EXPECT_TRUE(stmt->Is<ContinueStatement>());
} }
TEST_F(ContinueStatementTest, IsValid) { TEST_F(ContinueStatementTest, IsValid) {
ContinueStatement stmt(Source{}); auto* stmt = create<ContinueStatement>();
EXPECT_TRUE(stmt.IsValid()); EXPECT_TRUE(stmt->IsValid());
} }
TEST_F(ContinueStatementTest, ToStr) { TEST_F(ContinueStatementTest, ToStr) {
ContinueStatement stmt(Source{}); auto* stmt = create<ContinueStatement>();
std::ostringstream out; std::ostringstream out;
stmt.to_str(out, 2); stmt->to_str(out, 2);
EXPECT_EQ(out.str(), R"( Continue{} EXPECT_EQ(out.str(), R"( Continue{}
)"); )");
} }

View File

@ -43,21 +43,21 @@ namespace {
using DecorationTest = TestHelper; using DecorationTest = TestHelper;
TEST_F(DecorationTest, AsCorrectType) { TEST_F(DecorationTest, AsCorrectType) {
auto* decoration = create<ConstantIdDecoration>(Source{}, 1); auto* decoration = create<ConstantIdDecoration>(1);
auto* upcast = static_cast<Decoration*>(decoration); auto* upcast = static_cast<Decoration*>(decoration);
auto* downcast = As<VariableDecoration>(upcast); auto* downcast = As<VariableDecoration>(upcast);
EXPECT_EQ(decoration, downcast); EXPECT_EQ(decoration, downcast);
} }
TEST_F(DecorationTest, AsIncorrectType) { TEST_F(DecorationTest, AsIncorrectType) {
auto* decoration = create<ConstantIdDecoration>(Source{}, 1); auto* decoration = create<ConstantIdDecoration>(1);
auto* upcast = static_cast<Decoration*>(decoration); auto* upcast = static_cast<Decoration*>(decoration);
auto* downcast = As<ArrayDecoration>(upcast); auto* downcast = As<ArrayDecoration>(upcast);
EXPECT_EQ(nullptr, downcast); EXPECT_EQ(nullptr, downcast);
} }
TEST_F(DecorationTest, Is) { TEST_F(DecorationTest, Is) {
Decoration* decoration = create<ConstantIdDecoration>(Source{}, 1); Decoration* decoration = create<ConstantIdDecoration>(1);
EXPECT_TRUE(decoration->Is<VariableDecoration>()); EXPECT_TRUE(decoration->Is<VariableDecoration>());
EXPECT_FALSE(decoration->Is<ArrayDecoration>()); EXPECT_FALSE(decoration->Is<ArrayDecoration>());
} }

View File

@ -25,36 +25,36 @@ namespace {
using DiscardStatementTest = TestHelper; using DiscardStatementTest = TestHelper;
TEST_F(DiscardStatementTest, Creation) { TEST_F(DiscardStatementTest, Creation) {
DiscardStatement stmt(Source{}); auto* stmt = create<DiscardStatement>();
EXPECT_EQ(stmt.source().range.begin.line, 0u); EXPECT_EQ(stmt->source().range.begin.line, 0u);
EXPECT_EQ(stmt.source().range.begin.column, 0u); EXPECT_EQ(stmt->source().range.begin.column, 0u);
EXPECT_EQ(stmt.source().range.end.line, 0u); EXPECT_EQ(stmt->source().range.end.line, 0u);
EXPECT_EQ(stmt.source().range.end.column, 0u); EXPECT_EQ(stmt->source().range.end.column, 0u);
} }
TEST_F(DiscardStatementTest, Creation_WithSource) { TEST_F(DiscardStatementTest, Creation_WithSource) {
DiscardStatement stmt( auto* stmt = create<DiscardStatement>(
Source{Source::Range{Source::Location{20, 2}, Source::Location{20, 5}}}); Source{Source::Range{Source::Location{20, 2}, Source::Location{20, 5}}});
EXPECT_EQ(stmt.source().range.begin.line, 20u); EXPECT_EQ(stmt->source().range.begin.line, 20u);
EXPECT_EQ(stmt.source().range.begin.column, 2u); EXPECT_EQ(stmt->source().range.begin.column, 2u);
EXPECT_EQ(stmt.source().range.end.line, 20u); EXPECT_EQ(stmt->source().range.end.line, 20u);
EXPECT_EQ(stmt.source().range.end.column, 5u); EXPECT_EQ(stmt->source().range.end.column, 5u);
} }
TEST_F(DiscardStatementTest, IsDiscard) { TEST_F(DiscardStatementTest, IsDiscard) {
DiscardStatement stmt(Source{}); auto* stmt = create<DiscardStatement>();
EXPECT_TRUE(stmt.Is<DiscardStatement>()); EXPECT_TRUE(stmt->Is<DiscardStatement>());
} }
TEST_F(DiscardStatementTest, IsValid) { TEST_F(DiscardStatementTest, IsValid) {
DiscardStatement stmt(Source{}); auto* stmt = create<DiscardStatement>();
EXPECT_TRUE(stmt.IsValid()); EXPECT_TRUE(stmt->IsValid());
} }
TEST_F(DiscardStatementTest, ToStr) { TEST_F(DiscardStatementTest, ToStr) {
DiscardStatement stmt(Source{}); auto* stmt = create<DiscardStatement>();
std::ostringstream out; std::ostringstream out;
stmt.to_str(out, 2); stmt->to_str(out, 2);
EXPECT_EQ(out.str(), R"( Discard{} EXPECT_EQ(out.str(), R"( Discard{}
)"); )");
} }

View File

@ -30,103 +30,98 @@ using ElseStatementTest = TestHelper;
TEST_F(ElseStatementTest, Creation) { TEST_F(ElseStatementTest, Creation) {
type::Bool bool_type; type::Bool bool_type;
auto* cond = create<ScalarConstructorExpression>( auto* cond = create<ScalarConstructorExpression>(
Source{}, create<BoolLiteral>(Source{}, &bool_type, true)); create<BoolLiteral>(&bool_type, true));
auto* body = auto* body = create<BlockStatement>(StatementList{
create<BlockStatement>(Source{}, StatementList{ create<DiscardStatement>(),
create<DiscardStatement>(Source{}),
}); });
auto* discard = body->get(0); auto* discard = body->get(0);
ElseStatement e(Source{}, cond, body); auto* e = create<ElseStatement>(cond, body);
EXPECT_EQ(e.condition(), cond); EXPECT_EQ(e->condition(), cond);
ASSERT_EQ(e.body()->size(), 1u); ASSERT_EQ(e->body()->size(), 1u);
EXPECT_EQ(e.body()->get(0), discard); EXPECT_EQ(e->body()->get(0), discard);
} }
TEST_F(ElseStatementTest, Creation_WithSource) { TEST_F(ElseStatementTest, Creation_WithSource) {
ElseStatement e(Source{Source::Location{20, 2}}, nullptr, auto* e = create<ElseStatement>(Source{Source::Location{20, 2}}, nullptr,
create<BlockStatement>(Source{}, StatementList{})); create<BlockStatement>(StatementList{}));
auto src = e.source(); auto src = e->source();
EXPECT_EQ(src.range.begin.line, 20u); EXPECT_EQ(src.range.begin.line, 20u);
EXPECT_EQ(src.range.begin.column, 2u); EXPECT_EQ(src.range.begin.column, 2u);
} }
TEST_F(ElseStatementTest, IsElse) { TEST_F(ElseStatementTest, IsElse) {
ElseStatement e(Source{}, nullptr, auto* e =
create<BlockStatement>(Source{}, StatementList{})); create<ElseStatement>(nullptr, create<BlockStatement>(StatementList{}));
EXPECT_TRUE(e.Is<ElseStatement>()); EXPECT_TRUE(e->Is<ElseStatement>());
} }
TEST_F(ElseStatementTest, HasCondition) { TEST_F(ElseStatementTest, HasCondition) {
type::Bool bool_type; type::Bool bool_type;
auto* cond = create<ScalarConstructorExpression>( auto* cond = create<ScalarConstructorExpression>(
Source{}, create<BoolLiteral>(Source{}, &bool_type, true)); create<BoolLiteral>(&bool_type, true));
ElseStatement e(Source{}, cond, auto* e =
create<BlockStatement>(Source{}, StatementList{})); create<ElseStatement>(cond, create<BlockStatement>(StatementList{}));
EXPECT_TRUE(e.HasCondition()); EXPECT_TRUE(e->HasCondition());
} }
TEST_F(ElseStatementTest, HasContition_NullCondition) { TEST_F(ElseStatementTest, HasContition_NullCondition) {
ElseStatement e(Source{}, nullptr, auto* e =
create<BlockStatement>(Source{}, StatementList{})); create<ElseStatement>(nullptr, create<BlockStatement>(StatementList{}));
EXPECT_FALSE(e.HasCondition()); EXPECT_FALSE(e->HasCondition());
} }
TEST_F(ElseStatementTest, IsValid) { TEST_F(ElseStatementTest, IsValid) {
ElseStatement e(Source{}, nullptr, auto* e =
create<BlockStatement>(Source{}, StatementList{})); create<ElseStatement>(nullptr, create<BlockStatement>(StatementList{}));
EXPECT_TRUE(e.IsValid()); EXPECT_TRUE(e->IsValid());
} }
TEST_F(ElseStatementTest, IsValid_WithBody) { TEST_F(ElseStatementTest, IsValid_WithBody) {
auto* body = auto* body = create<BlockStatement>(StatementList{
create<BlockStatement>(Source{}, StatementList{ create<DiscardStatement>(),
create<DiscardStatement>(Source{}),
}); });
ElseStatement e(Source{}, nullptr, body); auto* e = create<ElseStatement>(nullptr, body);
EXPECT_TRUE(e.IsValid()); EXPECT_TRUE(e->IsValid());
} }
TEST_F(ElseStatementTest, IsValid_WithNullBodyStatement) { TEST_F(ElseStatementTest, IsValid_WithNullBodyStatement) {
auto* body = auto* body = create<BlockStatement>(StatementList{
create<BlockStatement>(Source{}, StatementList{ create<DiscardStatement>(),
create<DiscardStatement>(Source{}),
nullptr, nullptr,
}); });
ElseStatement e(Source{}, nullptr, body); auto* e = create<ElseStatement>(nullptr, body);
EXPECT_FALSE(e.IsValid()); EXPECT_FALSE(e->IsValid());
} }
TEST_F(ElseStatementTest, IsValid_InvalidCondition) { TEST_F(ElseStatementTest, IsValid_InvalidCondition) {
auto* cond = create<ScalarConstructorExpression>(Source{}, nullptr); auto* cond = create<ScalarConstructorExpression>(nullptr);
ElseStatement e(Source{}, cond, auto* e =
create<BlockStatement>(Source{}, StatementList{})); create<ElseStatement>(cond, create<BlockStatement>(StatementList{}));
EXPECT_FALSE(e.IsValid()); EXPECT_FALSE(e->IsValid());
} }
TEST_F(ElseStatementTest, IsValid_InvalidBodyStatement) { TEST_F(ElseStatementTest, IsValid_InvalidBodyStatement) {
auto* body = create<BlockStatement>( auto* body = create<BlockStatement>(
Source{},
StatementList{ StatementList{
create<IfStatement>(Source{}, nullptr, create<IfStatement>(nullptr, create<BlockStatement>(StatementList{}),
create<BlockStatement>(Source{}, StatementList{}),
ElseStatementList{}), ElseStatementList{}),
}); });
ElseStatement e(Source{}, nullptr, body); auto* e = create<ElseStatement>(nullptr, body);
EXPECT_FALSE(e.IsValid()); EXPECT_FALSE(e->IsValid());
} }
TEST_F(ElseStatementTest, ToStr) { TEST_F(ElseStatementTest, ToStr) {
type::Bool bool_type; type::Bool bool_type;
auto* cond = create<ScalarConstructorExpression>( auto* cond = create<ScalarConstructorExpression>(
Source{}, create<BoolLiteral>(Source{}, &bool_type, true)); create<BoolLiteral>(&bool_type, true));
auto* body = auto* body = create<BlockStatement>(StatementList{
create<BlockStatement>(Source{}, StatementList{ create<DiscardStatement>(),
create<DiscardStatement>(Source{}),
}); });
ElseStatement e(Source{}, cond, body); auto* e = create<ElseStatement>(cond, body);
std::ostringstream out; std::ostringstream out;
e.to_str(out, 2); e->to_str(out, 2);
EXPECT_EQ(out.str(), R"( Else{ EXPECT_EQ(out.str(), R"( Else{
( (
ScalarConstructor[not set]{true} ScalarConstructor[not set]{true}
@ -139,13 +134,12 @@ TEST_F(ElseStatementTest, ToStr) {
} }
TEST_F(ElseStatementTest, ToStr_NoCondition) { TEST_F(ElseStatementTest, ToStr_NoCondition) {
auto* body = auto* body = create<BlockStatement>(StatementList{
create<BlockStatement>(Source{}, StatementList{ create<DiscardStatement>(),
create<DiscardStatement>(Source{}),
}); });
ElseStatement e(Source{}, nullptr, body); auto* e = create<ElseStatement>(nullptr, body);
std::ostringstream out; std::ostringstream out;
e.to_str(out, 2); e->to_str(out, 2);
EXPECT_EQ(out.str(), R"( Else{ EXPECT_EQ(out.str(), R"( Else{
{ {
Discard{} Discard{}

View File

@ -22,11 +22,11 @@ namespace tint {
namespace ast { namespace ast {
namespace { namespace {
class Expr : public Expression { class FakeExpr : public Expression {
public: public:
Expr() : Expression(Source{}) {} FakeExpr() : Expression(Source{}) {}
Expr* Clone(CloneContext*) const override { return nullptr; } FakeExpr* Clone(CloneContext*) const override { return nullptr; }
bool IsValid() const override { return true; } bool IsValid() const override { return true; }
void to_str(std::ostream&, size_t) const override {} void to_str(std::ostream&, size_t) const override {}
}; };
@ -34,20 +34,17 @@ class Expr : public Expression {
using ExpressionTest = TestHelper; using ExpressionTest = TestHelper;
TEST_F(ExpressionTest, set_result_type) { TEST_F(ExpressionTest, set_result_type) {
type::I32 i32; FakeExpr e;
e.set_result_type(ty.i32);
Expr e;
e.set_result_type(&i32);
ASSERT_NE(e.result_type(), nullptr); ASSERT_NE(e.result_type(), nullptr);
EXPECT_TRUE(e.result_type()->Is<type::I32>()); EXPECT_TRUE(e.result_type()->Is<type::I32>());
} }
TEST_F(ExpressionTest, set_result_type_alias) { TEST_F(ExpressionTest, set_result_type_alias) {
type::I32 i32; type::Alias a(mod->RegisterSymbol("a"), "a", ty.i32);
type::Alias a(mod.RegisterSymbol("a"), "a", &i32); type::Alias b(mod->RegisterSymbol("b"), "b", &a);
type::Alias b(mod.RegisterSymbol("b"), "b", &a);
Expr e; FakeExpr e;
e.set_result_type(&b); e.set_result_type(&b);
ASSERT_NE(e.result_type(), nullptr); ASSERT_NE(e.result_type(), nullptr);
EXPECT_TRUE(e.result_type()->Is<type::I32>()); EXPECT_TRUE(e.result_type()->Is<type::I32>());

View File

@ -23,34 +23,34 @@ namespace {
using FallthroughStatementTest = TestHelper; using FallthroughStatementTest = TestHelper;
TEST_F(FallthroughStatementTest, Creation) { TEST_F(FallthroughStatementTest, Creation) {
FallthroughStatement stmt(Source{}); auto* stmt = create<FallthroughStatement>();
EXPECT_EQ(stmt.source().range.begin.line, 0u); EXPECT_EQ(stmt->source().range.begin.line, 0u);
EXPECT_EQ(stmt.source().range.begin.column, 0u); EXPECT_EQ(stmt->source().range.begin.column, 0u);
EXPECT_EQ(stmt.source().range.end.line, 0u); EXPECT_EQ(stmt->source().range.end.line, 0u);
EXPECT_EQ(stmt.source().range.end.column, 0u); EXPECT_EQ(stmt->source().range.end.column, 0u);
} }
TEST_F(FallthroughStatementTest, Creation_WithSource) { TEST_F(FallthroughStatementTest, Creation_WithSource) {
FallthroughStatement stmt(Source{Source::Location{20, 2}}); auto* stmt = create<FallthroughStatement>(Source{Source::Location{20, 2}});
auto src = stmt.source(); auto src = stmt->source();
EXPECT_EQ(src.range.begin.line, 20u); EXPECT_EQ(src.range.begin.line, 20u);
EXPECT_EQ(src.range.begin.column, 2u); EXPECT_EQ(src.range.begin.column, 2u);
} }
TEST_F(FallthroughStatementTest, IsFallthrough) { TEST_F(FallthroughStatementTest, IsFallthrough) {
FallthroughStatement stmt(Source{}); auto* stmt = create<FallthroughStatement>();
EXPECT_TRUE(stmt.Is<FallthroughStatement>()); EXPECT_TRUE(stmt->Is<FallthroughStatement>());
} }
TEST_F(FallthroughStatementTest, IsValid) { TEST_F(FallthroughStatementTest, IsValid) {
FallthroughStatement stmt(Source{}); auto* stmt = create<FallthroughStatement>();
EXPECT_TRUE(stmt.IsValid()); EXPECT_TRUE(stmt->IsValid());
} }
TEST_F(FallthroughStatementTest, ToStr) { TEST_F(FallthroughStatementTest, ToStr) {
FallthroughStatement stmt(Source{}); auto* stmt = create<FallthroughStatement>();
std::ostringstream out; std::ostringstream out;
stmt.to_str(out, 2); stmt->to_str(out, 2);
EXPECT_EQ(out.str(), R"( Fallthrough{} EXPECT_EQ(out.str(), R"( Fallthrough{}
)"); )");
} }

View File

@ -28,16 +28,13 @@ namespace {
using FloatLiteralTest = TestHelper; using FloatLiteralTest = TestHelper;
TEST_F(FloatLiteralTest, Value) { TEST_F(FloatLiteralTest, Value) {
type::F32 f32; auto* f = create<FloatLiteral>(ty.f32, 47.2f);
FloatLiteral f{Source{}, &f32, 47.2f}; ASSERT_TRUE(f->Is<FloatLiteral>());
ASSERT_TRUE(f.Is<FloatLiteral>()); EXPECT_EQ(f->value(), 47.2f);
EXPECT_EQ(f.value(), 47.2f);
} }
TEST_F(FloatLiteralTest, Is) { TEST_F(FloatLiteralTest, Is) {
type::F32 f32; ast::Literal* l = create<FloatLiteral>(ty.f32, 42.f);
FloatLiteral f{Source{}, &f32, 42.f};
Literal* l = &f;
EXPECT_FALSE(l->Is<BoolLiteral>()); EXPECT_FALSE(l->Is<BoolLiteral>());
EXPECT_FALSE(l->Is<SintLiteral>()); EXPECT_FALSE(l->Is<SintLiteral>());
EXPECT_FALSE(l->Is<IntLiteral>()); EXPECT_FALSE(l->Is<IntLiteral>());
@ -47,16 +44,14 @@ TEST_F(FloatLiteralTest, Is) {
} }
TEST_F(FloatLiteralTest, ToStr) { TEST_F(FloatLiteralTest, ToStr) {
type::F32 f32; auto* f = create<FloatLiteral>(ty.f32, 42.1f);
FloatLiteral f{Source{}, &f32, 42.1f};
EXPECT_EQ(f.to_str(), "42.099998"); EXPECT_EQ(f->to_str(), "42.099998");
} }
TEST_F(FloatLiteralTest, ToName) { TEST_F(FloatLiteralTest, ToName) {
type::F32 f32; auto* f = create<FloatLiteral>(ty.f32, 42.1f);
FloatLiteral f{Source{}, &f32, 42.1f}; EXPECT_EQ(f->name(), "__float42.0999985");
EXPECT_EQ(f.name(), "__float42.0999985");
} }
} // namespace } // namespace

View File

@ -19,9 +19,6 @@
#include "src/ast/location_decoration.h" #include "src/ast/location_decoration.h"
#include "src/ast/pipeline_stage.h" #include "src/ast/pipeline_stage.h"
#include "src/ast/test_helper.h" #include "src/ast/test_helper.h"
#include "src/ast/type/f32_type.h"
#include "src/ast/type/i32_type.h"
#include "src/ast/type/void_type.h"
#include "src/ast/variable.h" #include "src/ast/variable.h"
#include "src/ast/workgroup_decoration.h" #include "src/ast/workgroup_decoration.h"
@ -32,113 +29,91 @@ namespace {
using FunctionTest = TestHelper; using FunctionTest = TestHelper;
TEST_F(FunctionTest, Creation) { TEST_F(FunctionTest, Creation) {
type::Void void_type; auto func_sym = mod->RegisterSymbol("func");
type::I32 i32;
auto func_sym = mod.RegisterSymbol("func");
VariableList params; VariableList params;
params.push_back(create<Variable>(Source{}, "var", StorageClass::kNone, &i32, params.push_back(Var("var", StorageClass::kNone, ty.i32));
false, nullptr,
ast::VariableDecorationList{}));
auto* var = params[0]; auto* var = params[0];
Function f(Source{}, func_sym, "func", params, &void_type, auto* f = create<Function>(func_sym, "func", params, ty.void_,
create<BlockStatement>(Source{}, StatementList{}), create<BlockStatement>(StatementList{}),
FunctionDecorationList{}); FunctionDecorationList{});
EXPECT_EQ(f.symbol(), func_sym); EXPECT_EQ(f->symbol(), func_sym);
EXPECT_EQ(f.name(), "func"); EXPECT_EQ(f->name(), "func");
ASSERT_EQ(f.params().size(), 1u); ASSERT_EQ(f->params().size(), 1u);
EXPECT_EQ(f.return_type(), &void_type); EXPECT_EQ(f->return_type(), ty.void_);
EXPECT_EQ(f.params()[0], var); EXPECT_EQ(f->params()[0], var);
} }
TEST_F(FunctionTest, Creation_WithSource) { TEST_F(FunctionTest, Creation_WithSource) {
type::Void void_type; auto func_sym = mod->RegisterSymbol("func");
type::I32 i32;
auto func_sym = mod.RegisterSymbol("func");
VariableList params; VariableList params;
params.push_back(create<Variable>(Source{}, "var", StorageClass::kNone, &i32, params.push_back(Var("var", StorageClass::kNone, ty.i32));
false, nullptr,
ast::VariableDecorationList{}));
Function f(Source{Source::Location{20, 2}}, func_sym, "func", params, auto* f = create<Function>(
&void_type, create<BlockStatement>(Source{}, StatementList{}), Source{Source::Location{20, 2}}, func_sym, "func", params, ty.void_,
FunctionDecorationList{}); create<BlockStatement>(StatementList{}), FunctionDecorationList{});
auto src = f.source(); auto src = f->source();
EXPECT_EQ(src.range.begin.line, 20u); EXPECT_EQ(src.range.begin.line, 20u);
EXPECT_EQ(src.range.begin.column, 2u); EXPECT_EQ(src.range.begin.column, 2u);
} }
TEST_F(FunctionTest, AddDuplicateReferencedVariables) { TEST_F(FunctionTest, AddDuplicateReferencedVariables) {
type::Void void_type; auto func_sym = mod->RegisterSymbol("func");
type::I32 i32;
auto func_sym = mod.RegisterSymbol("func"); auto* v = Var("var", StorageClass::kInput, ty.i32);
auto* f = create<Function>(func_sym, "func", VariableList{}, ty.void_,
Variable v(Source{}, "var", StorageClass::kInput, &i32, false, nullptr, create<BlockStatement>(StatementList{}),
ast::VariableDecorationList{});
Function f(Source{}, func_sym, "func", VariableList{}, &void_type,
create<BlockStatement>(Source{}, StatementList{}),
FunctionDecorationList{}); FunctionDecorationList{});
f.add_referenced_module_variable(&v); f->add_referenced_module_variable(v);
ASSERT_EQ(f.referenced_module_variables().size(), 1u); ASSERT_EQ(f->referenced_module_variables().size(), 1u);
EXPECT_EQ(f.referenced_module_variables()[0], &v); EXPECT_EQ(f->referenced_module_variables()[0], v);
f.add_referenced_module_variable(&v); f->add_referenced_module_variable(v);
ASSERT_EQ(f.referenced_module_variables().size(), 1u); ASSERT_EQ(f->referenced_module_variables().size(), 1u);
Variable v2(Source{}, "var2", StorageClass::kOutput, &i32, false, nullptr, auto* v2 = Var("var2", StorageClass::kOutput, ty.i32);
ast::VariableDecorationList{}); f->add_referenced_module_variable(v2);
f.add_referenced_module_variable(&v2); ASSERT_EQ(f->referenced_module_variables().size(), 2u);
ASSERT_EQ(f.referenced_module_variables().size(), 2u); EXPECT_EQ(f->referenced_module_variables()[1], v2);
EXPECT_EQ(f.referenced_module_variables()[1], &v2);
} }
TEST_F(FunctionTest, GetReferenceLocations) { TEST_F(FunctionTest, GetReferenceLocations) {
type::Void void_type; auto func_sym = mod->RegisterSymbol("func");
type::I32 i32;
auto func_sym = mod.RegisterSymbol("func"); auto* loc1 = Var("loc1", StorageClass::kInput, ty.i32, nullptr,
auto* loc1 = create<Variable>(Source{}, "loc1", StorageClass::kInput, &i32,
false, nullptr,
ast::VariableDecorationList{ ast::VariableDecorationList{
create<LocationDecoration>(Source{}, 0), create<LocationDecoration>(0),
}); });
auto* loc2 = create<Variable>(Source{}, "loc2", StorageClass::kInput, &i32, auto* loc2 = Var("loc2", StorageClass::kInput, ty.i32, nullptr,
false, nullptr,
ast::VariableDecorationList{ ast::VariableDecorationList{
create<LocationDecoration>(Source{}, 1), create<LocationDecoration>(1),
}); });
auto* builtin1 = create<Variable>( auto* builtin1 = Var("builtin1", StorageClass::kInput, ty.i32, nullptr,
Source{}, "builtin1", StorageClass::kInput, &i32, false, nullptr,
ast::VariableDecorationList{ ast::VariableDecorationList{
create<BuiltinDecoration>(Source{}, Builtin::kPosition), create<BuiltinDecoration>(Builtin::kPosition),
}); });
auto* builtin2 = create<Variable>( auto* builtin2 = Var("builtin2", StorageClass::kInput, ty.i32, nullptr,
Source{}, "builtin2", StorageClass::kInput, &i32, false, nullptr,
ast::VariableDecorationList{ ast::VariableDecorationList{
create<BuiltinDecoration>(Source{}, Builtin::kFragDepth), create<BuiltinDecoration>(Builtin::kFragDepth),
}); });
Function f(Source{}, func_sym, "func", VariableList{}, &void_type, auto* f = create<Function>(func_sym, "func", VariableList{}, ty.void_,
create<BlockStatement>(Source{}, StatementList{}), create<BlockStatement>(StatementList{}),
FunctionDecorationList{}); FunctionDecorationList{});
f.add_referenced_module_variable(loc1); f->add_referenced_module_variable(loc1);
f.add_referenced_module_variable(builtin1); f->add_referenced_module_variable(builtin1);
f.add_referenced_module_variable(loc2); f->add_referenced_module_variable(loc2);
f.add_referenced_module_variable(builtin2); f->add_referenced_module_variable(builtin2);
ASSERT_EQ(f.referenced_module_variables().size(), 4u); ASSERT_EQ(f->referenced_module_variables().size(), 4u);
auto ref_locs = f.referenced_location_variables(); auto ref_locs = f->referenced_location_variables();
ASSERT_EQ(ref_locs.size(), 2u); ASSERT_EQ(ref_locs.size(), 2u);
EXPECT_EQ(ref_locs[0].first, loc1); EXPECT_EQ(ref_locs[0].first, loc1);
EXPECT_EQ(ref_locs[0].second->value(), 0u); EXPECT_EQ(ref_locs[0].second->value(), 0u);
@ -147,46 +122,39 @@ TEST_F(FunctionTest, GetReferenceLocations) {
} }
TEST_F(FunctionTest, GetReferenceBuiltins) { TEST_F(FunctionTest, GetReferenceBuiltins) {
type::Void void_type; auto func_sym = mod->RegisterSymbol("func");
type::I32 i32;
auto func_sym = mod.RegisterSymbol("func"); auto* loc1 = Var("loc1", StorageClass::kInput, ty.i32, nullptr,
auto* loc1 = create<Variable>(Source{}, "loc1", StorageClass::kInput, &i32,
false, nullptr,
ast::VariableDecorationList{ ast::VariableDecorationList{
create<LocationDecoration>(Source{}, 0), create<LocationDecoration>(0),
}); });
auto* loc2 = create<Variable>(Source{}, "loc2", StorageClass::kInput, &i32, auto* loc2 = Var("loc2", StorageClass::kInput, ty.i32, nullptr,
false, nullptr,
ast::VariableDecorationList{ ast::VariableDecorationList{
create<LocationDecoration>(Source{}, 1), create<LocationDecoration>(1),
}); });
auto* builtin1 = create<Variable>( auto* builtin1 = Var("builtin1", StorageClass::kInput, ty.i32, nullptr,
Source{}, "builtin1", StorageClass::kInput, &i32, false, nullptr,
ast::VariableDecorationList{ ast::VariableDecorationList{
create<BuiltinDecoration>(Source{}, Builtin::kPosition), create<BuiltinDecoration>(Builtin::kPosition),
}); });
auto* builtin2 = create<Variable>( auto* builtin2 = Var("builtin2", StorageClass::kInput, ty.i32, nullptr,
Source{}, "builtin2", StorageClass::kInput, &i32, false, nullptr,
ast::VariableDecorationList{ ast::VariableDecorationList{
create<BuiltinDecoration>(Source{}, Builtin::kFragDepth), create<BuiltinDecoration>(Builtin::kFragDepth),
}); });
Function f(Source{}, func_sym, "func", VariableList{}, &void_type, auto* f = create<Function>(func_sym, "func", VariableList{}, ty.void_,
create<BlockStatement>(Source{}, StatementList{}), create<BlockStatement>(StatementList{}),
FunctionDecorationList{}); FunctionDecorationList{});
f.add_referenced_module_variable(loc1); f->add_referenced_module_variable(loc1);
f.add_referenced_module_variable(builtin1); f->add_referenced_module_variable(builtin1);
f.add_referenced_module_variable(loc2); f->add_referenced_module_variable(loc2);
f.add_referenced_module_variable(builtin2); f->add_referenced_module_variable(builtin2);
ASSERT_EQ(f.referenced_module_variables().size(), 4u); ASSERT_EQ(f->referenced_module_variables().size(), 4u);
auto ref_locs = f.referenced_builtin_variables(); auto ref_locs = f->referenced_builtin_variables();
ASSERT_EQ(ref_locs.size(), 2u); ASSERT_EQ(ref_locs.size(), 2u);
EXPECT_EQ(ref_locs[0].first, builtin1); EXPECT_EQ(ref_locs[0].first, builtin1);
EXPECT_EQ(ref_locs[0].second->value(), Builtin::kPosition); EXPECT_EQ(ref_locs[0].second->value(), Builtin::kPosition);
@ -195,173 +163,131 @@ TEST_F(FunctionTest, GetReferenceBuiltins) {
} }
TEST_F(FunctionTest, AddDuplicateEntryPoints) { TEST_F(FunctionTest, AddDuplicateEntryPoints) {
type::Void void_type; auto func_sym = mod->RegisterSymbol("func");
auto main_sym = mod->RegisterSymbol("main");
auto func_sym = mod.RegisterSymbol("func"); auto* f = create<Function>(func_sym, "func", VariableList{}, ty.void_,
auto main_sym = mod.RegisterSymbol("main"); create<BlockStatement>(StatementList{}),
Function f(Source{}, func_sym, "func", VariableList{}, &void_type,
create<BlockStatement>(Source{}, StatementList{}),
FunctionDecorationList{}); FunctionDecorationList{});
f.add_ancestor_entry_point(main_sym); f->add_ancestor_entry_point(main_sym);
ASSERT_EQ(1u, f.ancestor_entry_points().size()); ASSERT_EQ(1u, f->ancestor_entry_points().size());
EXPECT_EQ(main_sym, f.ancestor_entry_points()[0]); EXPECT_EQ(main_sym, f->ancestor_entry_points()[0]);
f.add_ancestor_entry_point(main_sym); f->add_ancestor_entry_point(main_sym);
ASSERT_EQ(1u, f.ancestor_entry_points().size()); ASSERT_EQ(1u, f->ancestor_entry_points().size());
EXPECT_EQ(main_sym, f.ancestor_entry_points()[0]); EXPECT_EQ(main_sym, f->ancestor_entry_points()[0]);
} }
TEST_F(FunctionTest, IsValid) { TEST_F(FunctionTest, IsValid) {
type::Void void_type; auto func_sym = mod->RegisterSymbol("func");
type::I32 i32;
auto func_sym = mod.RegisterSymbol("func");
VariableList params; VariableList params;
params.push_back(create<Variable>(Source{}, "var", StorageClass::kNone, &i32, params.push_back(Var("var", StorageClass::kNone, ty.i32));
false, nullptr,
ast::VariableDecorationList{}));
auto* body = auto* body = create<BlockStatement>(StatementList{
create<BlockStatement>(Source{}, StatementList{ create<DiscardStatement>(),
create<DiscardStatement>(Source{}),
}); });
Function f(Source{}, func_sym, "func", params, &void_type, body, auto* f = create<Function>(func_sym, "func", params, ty.void_, body,
FunctionDecorationList{}); FunctionDecorationList{});
EXPECT_TRUE(f.IsValid()); EXPECT_TRUE(f->IsValid());
} }
TEST_F(FunctionTest, IsValid_InvalidName) { TEST_F(FunctionTest, IsValid_InvalidName) {
type::Void void_type; auto func_sym = mod->RegisterSymbol("");
type::I32 i32;
auto func_sym = mod.RegisterSymbol("");
VariableList params; VariableList params;
params.push_back(create<Variable>(Source{}, "var", StorageClass::kNone, &i32, params.push_back(Var("var", StorageClass::kNone, ty.i32));
false, nullptr,
ast::VariableDecorationList{}));
Function f(Source{}, func_sym, "", params, &void_type, auto* f = create<Function>(func_sym, "", params, ty.void_,
create<BlockStatement>(Source{}, StatementList{}), create<BlockStatement>(StatementList{}),
FunctionDecorationList{}); FunctionDecorationList{});
EXPECT_FALSE(f.IsValid()); EXPECT_FALSE(f->IsValid());
} }
TEST_F(FunctionTest, IsValid_MissingReturnType) { TEST_F(FunctionTest, IsValid_MissingReturnType) {
type::I32 i32; auto func_sym = mod->RegisterSymbol("func");
auto func_sym = mod.RegisterSymbol("func");
VariableList params; VariableList params;
params.push_back(create<Variable>(Source{}, "var", StorageClass::kNone, &i32, params.push_back(Var("var", StorageClass::kNone, ty.i32));
false, nullptr,
ast::VariableDecorationList{}));
Function f(Source{}, func_sym, "func", params, nullptr, auto* f = create<Function>(func_sym, "func", params, nullptr,
create<BlockStatement>(Source{}, StatementList{}), create<BlockStatement>(StatementList{}),
FunctionDecorationList{}); FunctionDecorationList{});
EXPECT_FALSE(f.IsValid()); EXPECT_FALSE(f->IsValid());
} }
TEST_F(FunctionTest, IsValid_NullParam) { TEST_F(FunctionTest, IsValid_NullParam) {
type::Void void_type; auto func_sym = mod->RegisterSymbol("func");
type::I32 i32;
auto func_sym = mod.RegisterSymbol("func");
VariableList params; VariableList params;
params.push_back(create<Variable>(Source{}, "var", StorageClass::kNone, &i32, params.push_back(Var("var", StorageClass::kNone, ty.i32));
false, nullptr,
ast::VariableDecorationList{}));
params.push_back(nullptr); params.push_back(nullptr);
Function f(Source{}, func_sym, "func", params, &void_type, auto* f = create<Function>(func_sym, "func", params, ty.void_,
create<BlockStatement>(Source{}, StatementList{}), create<BlockStatement>(StatementList{}),
FunctionDecorationList{}); FunctionDecorationList{});
EXPECT_FALSE(f.IsValid()); EXPECT_FALSE(f->IsValid());
} }
TEST_F(FunctionTest, IsValid_InvalidParam) { TEST_F(FunctionTest, IsValid_InvalidParam) {
type::Void void_type; auto func_sym = mod->RegisterSymbol("func");
auto func_sym = mod.RegisterSymbol("func");
VariableList params; VariableList params;
params.push_back(create<Variable>(Source{}, "var", StorageClass::kNone, params.push_back(Var("var", StorageClass::kNone, nullptr));
nullptr, false, nullptr,
ast::VariableDecorationList{}));
Function f(Source{}, func_sym, "func", params, &void_type, auto* f = create<Function>(func_sym, "func", params, ty.void_,
create<BlockStatement>(Source{}, StatementList{}), create<BlockStatement>(StatementList{}),
FunctionDecorationList{}); FunctionDecorationList{});
EXPECT_FALSE(f.IsValid()); EXPECT_FALSE(f->IsValid());
} }
TEST_F(FunctionTest, IsValid_NullBodyStatement) { TEST_F(FunctionTest, IsValid_NullBodyStatement) {
type::Void void_type; auto func_sym = mod->RegisterSymbol("func");
type::I32 i32;
auto func_sym = mod.RegisterSymbol("func");
VariableList params; VariableList params;
params.push_back(create<Variable>(Source{}, "var", StorageClass::kNone, &i32, params.push_back(Var("var", StorageClass::kNone, ty.i32));
false, nullptr,
ast::VariableDecorationList{}));
auto* body = auto* body = create<BlockStatement>(StatementList{
create<BlockStatement>(Source{}, StatementList{ create<DiscardStatement>(),
create<DiscardStatement>(Source{}),
nullptr, nullptr,
}); });
Function f(Source{}, func_sym, "func", params, &void_type, body, auto* f = create<Function>(func_sym, "func", params, ty.void_, body,
FunctionDecorationList{}); FunctionDecorationList{});
EXPECT_FALSE(f.IsValid()); EXPECT_FALSE(f->IsValid());
} }
TEST_F(FunctionTest, IsValid_InvalidBodyStatement) { TEST_F(FunctionTest, IsValid_InvalidBodyStatement) {
type::Void void_type; auto func_sym = mod->RegisterSymbol("func");
type::I32 i32;
auto func_sym = mod.RegisterSymbol("func");
VariableList params; VariableList params;
params.push_back(create<Variable>(Source{}, "var", StorageClass::kNone, &i32, params.push_back(Var("var", StorageClass::kNone, ty.i32));
false, nullptr,
ast::VariableDecorationList{}));
auto* body = auto* body = create<BlockStatement>(StatementList{
create<BlockStatement>(Source{}, StatementList{ create<DiscardStatement>(),
create<DiscardStatement>(Source{}),
nullptr, nullptr,
}); });
Function f(Source{}, func_sym, "func", params, &void_type, body, auto* f = create<Function>(func_sym, "func", params, ty.void_, body,
FunctionDecorationList{}); FunctionDecorationList{});
EXPECT_FALSE(f.IsValid()); EXPECT_FALSE(f->IsValid());
} }
TEST_F(FunctionTest, ToStr) { TEST_F(FunctionTest, ToStr) {
type::Void void_type; auto func_sym = mod->RegisterSymbol("func");
type::I32 i32;
auto func_sym = mod.RegisterSymbol("func"); auto* body = create<BlockStatement>(StatementList{
create<DiscardStatement>(),
auto* body =
create<BlockStatement>(Source{}, StatementList{
create<DiscardStatement>(Source{}),
}); });
Function f(Source{}, func_sym, "func", {}, &void_type, body, auto* f = create<Function>(func_sym, "func", VariableList{}, ty.void_, body,
FunctionDecorationList{}); FunctionDecorationList{});
std::ostringstream out; std::ostringstream out;
f.to_str(out, 2); f->to_str(out, 2);
EXPECT_EQ(demangle(out.str()), R"( Function func -> __void EXPECT_EQ(demangle(out.str()), R"( Function func -> __void
() ()
{ {
@ -371,21 +297,17 @@ TEST_F(FunctionTest, ToStr) {
} }
TEST_F(FunctionTest, ToStr_WithDecoration) { TEST_F(FunctionTest, ToStr_WithDecoration) {
type::Void void_type; auto func_sym = mod->RegisterSymbol("func");
type::I32 i32;
auto func_sym = mod.RegisterSymbol("func"); auto* body = create<BlockStatement>(StatementList{
create<DiscardStatement>(),
auto* body =
create<BlockStatement>(Source{}, StatementList{
create<DiscardStatement>(Source{}),
}); });
Function f( auto* f = create<Function>(
Source{}, func_sym, "func", {}, &void_type, body, func_sym, "func", VariableList{}, ty.void_, body,
FunctionDecorationList{create<WorkgroupDecoration>(Source{}, 2, 4, 6)}); FunctionDecorationList{create<WorkgroupDecoration>(2, 4, 6)});
std::ostringstream out; std::ostringstream out;
f.to_str(out, 2); f->to_str(out, 2);
EXPECT_EQ(demangle(out.str()), R"( Function func -> __void EXPECT_EQ(demangle(out.str()), R"( Function func -> __void
WorkgroupDecoration{2 4 6} WorkgroupDecoration{2 4 6}
() ()
@ -396,25 +318,19 @@ TEST_F(FunctionTest, ToStr_WithDecoration) {
} }
TEST_F(FunctionTest, ToStr_WithParams) { TEST_F(FunctionTest, ToStr_WithParams) {
type::Void void_type; auto func_sym = mod->RegisterSymbol("func");
type::I32 i32;
auto func_sym = mod.RegisterSymbol("func");
VariableList params; VariableList params;
params.push_back(create<Variable>(Source{}, "var", StorageClass::kNone, &i32, params.push_back(Var("var", StorageClass::kNone, ty.i32));
false, nullptr,
ast::VariableDecorationList{}));
auto* body = auto* body = create<BlockStatement>(StatementList{
create<BlockStatement>(Source{}, StatementList{ create<DiscardStatement>(),
create<DiscardStatement>(Source{}),
}); });
Function f(Source{}, func_sym, "func", params, &void_type, body, auto* f = create<Function>(func_sym, "func", params, ty.void_, body,
FunctionDecorationList{}); FunctionDecorationList{});
std::ostringstream out; std::ostringstream out;
f.to_str(out, 2); f->to_str(out, 2);
EXPECT_EQ(demangle(out.str()), R"( Function func -> __void EXPECT_EQ(demangle(out.str()), R"( Function func -> __void
( (
Variable{ Variable{
@ -430,94 +346,77 @@ TEST_F(FunctionTest, ToStr_WithParams) {
} }
TEST_F(FunctionTest, TypeName) { TEST_F(FunctionTest, TypeName) {
type::Void void_type; auto func_sym = mod->RegisterSymbol("func");
auto func_sym = mod.RegisterSymbol("func"); auto* f = create<Function>(func_sym, "func", VariableList{}, ty.void_,
create<BlockStatement>(StatementList{}),
Function f(Source{}, func_sym, "func", {}, &void_type,
create<BlockStatement>(Source{}, StatementList{}),
FunctionDecorationList{}); FunctionDecorationList{});
EXPECT_EQ(f.type_name(), "__func__void"); EXPECT_EQ(f->type_name(), "__func__void");
} }
TEST_F(FunctionTest, TypeName_WithParams) { TEST_F(FunctionTest, TypeName_WithParams) {
type::Void void_type; auto func_sym = mod->RegisterSymbol("func");
type::I32 i32;
type::F32 f32;
auto func_sym = mod.RegisterSymbol("func");
VariableList params; VariableList params;
params.push_back(create<Variable>(Source{}, "var1", StorageClass::kNone, &i32, params.push_back(Var("var1", StorageClass::kNone, ty.i32));
false, nullptr, params.push_back(Var("var2", StorageClass::kNone, ty.f32));
ast::VariableDecorationList{}));
params.push_back(create<Variable>(Source{}, "var2", StorageClass::kNone, &f32,
false, nullptr,
ast::VariableDecorationList{}));
Function f(Source{}, func_sym, "func", params, &void_type, auto* f = create<Function>(func_sym, "func", params, ty.void_,
create<BlockStatement>(Source{}, StatementList{}), create<BlockStatement>(StatementList{}),
FunctionDecorationList{}); FunctionDecorationList{});
EXPECT_EQ(f.type_name(), "__func__void__i32__f32"); EXPECT_EQ(f->type_name(), "__func__void__i32__f32");
} }
TEST_F(FunctionTest, GetLastStatement) { TEST_F(FunctionTest, GetLastStatement) {
type::Void void_type; auto func_sym = mod->RegisterSymbol("func");
auto func_sym = mod.RegisterSymbol("func");
VariableList params; VariableList params;
auto* stmt = create<DiscardStatement>(Source{}); auto* stmt = create<DiscardStatement>();
auto* body = create<BlockStatement>(Source{}, StatementList{stmt}); auto* body = create<BlockStatement>(StatementList{stmt});
Function f(Source{}, func_sym, "func", params, &void_type, body, auto* f = create<Function>(func_sym, "func", params, ty.void_, body,
FunctionDecorationList{}); FunctionDecorationList{});
EXPECT_EQ(f.get_last_statement(), stmt); EXPECT_EQ(f->get_last_statement(), stmt);
} }
TEST_F(FunctionTest, GetLastStatement_nullptr) { TEST_F(FunctionTest, GetLastStatement_nullptr) {
type::Void void_type; auto func_sym = mod->RegisterSymbol("func");
auto func_sym = mod.RegisterSymbol("func");
VariableList params; VariableList params;
auto* body = create<BlockStatement>(Source{}, StatementList{}); auto* body = create<BlockStatement>(StatementList{});
Function f(Source{}, func_sym, "func", params, &void_type, body, auto* f = create<Function>(func_sym, "func", params, ty.void_, body,
FunctionDecorationList{}); FunctionDecorationList{});
EXPECT_EQ(f.get_last_statement(), nullptr); EXPECT_EQ(f->get_last_statement(), nullptr);
} }
TEST_F(FunctionTest, WorkgroupSize_NoneSet) { TEST_F(FunctionTest, WorkgroupSize_NoneSet) {
type::Void void_type; auto func_sym = mod->RegisterSymbol("func");
auto func_sym = mod.RegisterSymbol("func"); auto* f = create<Function>(func_sym, "func", VariableList{}, ty.void_,
create<BlockStatement>(StatementList{}),
Function f(Source{}, func_sym, "func", {}, &void_type,
create<BlockStatement>(Source{}, StatementList{}),
FunctionDecorationList{}); FunctionDecorationList{});
uint32_t x = 0; uint32_t x = 0;
uint32_t y = 0; uint32_t y = 0;
uint32_t z = 0; uint32_t z = 0;
std::tie(x, y, z) = f.workgroup_size(); std::tie(x, y, z) = f->workgroup_size();
EXPECT_EQ(x, 1u); EXPECT_EQ(x, 1u);
EXPECT_EQ(y, 1u); EXPECT_EQ(y, 1u);
EXPECT_EQ(z, 1u); EXPECT_EQ(z, 1u);
} }
TEST_F(FunctionTest, WorkgroupSize) { TEST_F(FunctionTest, WorkgroupSize) {
type::Void void_type; auto func_sym = mod->RegisterSymbol("func");
auto func_sym = mod.RegisterSymbol("func"); auto* f = create<Function>(
func_sym, "func", VariableList{}, ty.void_,
Function f(Source{}, func_sym, "func", {}, &void_type, create<BlockStatement>(StatementList{}),
create<BlockStatement>(Source{}, StatementList{}), FunctionDecorationList{create<WorkgroupDecoration>(2u, 4u, 6u)});
{create<WorkgroupDecoration>(Source{}, 2u, 4u, 6u)});
uint32_t x = 0; uint32_t x = 0;
uint32_t y = 0; uint32_t y = 0;
uint32_t z = 0; uint32_t z = 0;
std::tie(x, y, z) = f.workgroup_size(); std::tie(x, y, z) = f->workgroup_size();
EXPECT_EQ(x, 2u); EXPECT_EQ(x, 2u);
EXPECT_EQ(y, 4u); EXPECT_EQ(y, 4u);
EXPECT_EQ(z, 6u); EXPECT_EQ(z, 6u);

View File

@ -23,36 +23,36 @@ namespace {
using IdentifierExpressionTest = TestHelper; using IdentifierExpressionTest = TestHelper;
TEST_F(IdentifierExpressionTest, Creation) { TEST_F(IdentifierExpressionTest, Creation) {
IdentifierExpression i(Source{}, mod.RegisterSymbol("ident"), "ident"); auto* i = Expr("ident");
EXPECT_EQ(i.symbol(), Symbol(1)); EXPECT_EQ(i->symbol(), Symbol(1));
EXPECT_EQ(i.name(), "ident"); EXPECT_EQ(i->name(), "ident");
} }
TEST_F(IdentifierExpressionTest, Creation_WithSource) { TEST_F(IdentifierExpressionTest, Creation_WithSource) {
IdentifierExpression i(Source{Source::Location{20, 2}}, auto* i = create<IdentifierExpression>(Source{Source::Location{20, 2}},
mod.RegisterSymbol("ident"), "ident"); mod->RegisterSymbol("ident"), "ident");
EXPECT_EQ(i.symbol(), Symbol(1)); EXPECT_EQ(i->symbol(), Symbol(1));
EXPECT_EQ(i.name(), "ident"); EXPECT_EQ(i->name(), "ident");
auto src = i.source(); auto src = i->source();
EXPECT_EQ(src.range.begin.line, 20u); EXPECT_EQ(src.range.begin.line, 20u);
EXPECT_EQ(src.range.begin.column, 2u); EXPECT_EQ(src.range.begin.column, 2u);
} }
TEST_F(IdentifierExpressionTest, IsIdentifier) { TEST_F(IdentifierExpressionTest, IsIdentifier) {
IdentifierExpression i(Source{}, mod.RegisterSymbol("ident"), "ident"); auto* i = Expr("ident");
EXPECT_TRUE(i.Is<IdentifierExpression>()); EXPECT_TRUE(i->Is<IdentifierExpression>());
} }
TEST_F(IdentifierExpressionTest, IsValid) { TEST_F(IdentifierExpressionTest, IsValid) {
IdentifierExpression i(Source{}, mod.RegisterSymbol("ident"), "ident"); auto* i = Expr("ident");
EXPECT_TRUE(i.IsValid()); EXPECT_TRUE(i->IsValid());
} }
TEST_F(IdentifierExpressionTest, ToStr) { TEST_F(IdentifierExpressionTest, ToStr) {
IdentifierExpression i(Source{}, mod.RegisterSymbol("ident"), "ident"); auto* i = Expr("ident");
std::ostringstream out; std::ostringstream out;
i.to_str(out, 2); i->to_str(out, 2);
EXPECT_EQ(demangle(out.str()), R"( Identifier[not set]{ident} EXPECT_EQ(demangle(out.str()), R"( Identifier[not set]{ident}
)"); )");
} }

View File

@ -25,176 +25,153 @@ namespace {
using IfStatementTest = TestHelper; using IfStatementTest = TestHelper;
TEST_F(IfStatementTest, Creation) { TEST_F(IfStatementTest, Creation) {
auto* cond = create<IdentifierExpression>(Source{}, auto* cond = Expr("cond");
mod.RegisterSymbol("cond"), "cond"); auto* body =
auto* body = create<BlockStatement>( create<BlockStatement>(StatementList{create<DiscardStatement>()});
Source{}, StatementList{create<DiscardStatement>(Source{})}); auto* stmt = create<IfStatement>(Source{Source::Location{20, 2}}, cond, body,
IfStatement stmt(Source{Source::Location{20, 2}}, cond, body,
ElseStatementList{}); ElseStatementList{});
auto src = stmt.source(); auto src = stmt->source();
EXPECT_EQ(src.range.begin.line, 20u); EXPECT_EQ(src.range.begin.line, 20u);
EXPECT_EQ(src.range.begin.column, 2u); EXPECT_EQ(src.range.begin.column, 2u);
} }
TEST_F(IfStatementTest, IsIf) { TEST_F(IfStatementTest, IsIf) {
IfStatement stmt(Source{}, nullptr, auto* stmt = create<IfStatement>(
create<BlockStatement>(Source{}, StatementList{}), nullptr, create<BlockStatement>(StatementList{}), ElseStatementList{});
ElseStatementList{}); EXPECT_TRUE(stmt->Is<IfStatement>());
EXPECT_TRUE(stmt.Is<IfStatement>());
} }
TEST_F(IfStatementTest, IsValid) { TEST_F(IfStatementTest, IsValid) {
auto* cond = create<IdentifierExpression>(Source{}, auto* cond = Expr("cond");
mod.RegisterSymbol("cond"), "cond"); auto* body =
auto* body = create<BlockStatement>( create<BlockStatement>(StatementList{create<DiscardStatement>()});
Source{}, StatementList{create<DiscardStatement>(Source{})}); auto* stmt = create<IfStatement>(cond, body, ElseStatementList{});
IfStatement stmt(Source{}, cond, body, ElseStatementList{}); EXPECT_TRUE(stmt->IsValid());
EXPECT_TRUE(stmt.IsValid());
} }
TEST_F(IfStatementTest, IsValid_WithElseStatements) { TEST_F(IfStatementTest, IsValid_WithElseStatements) {
auto* cond = create<IdentifierExpression>(Source{}, auto* cond = Expr("cond");
mod.RegisterSymbol("cond"), "cond"); auto* body =
auto* body = create<BlockStatement>( create<BlockStatement>(StatementList{create<DiscardStatement>()});
Source{}, StatementList{create<DiscardStatement>(Source{})}); auto* stmt = create<IfStatement>(
IfStatement stmt(Source{}, cond, body, cond, body,
{ ElseStatementList{
create<ElseStatement>( create<ElseStatement>(create<IdentifierExpression>(
Source{}, mod->RegisterSymbol("Ident"), "Ident"),
create<IdentifierExpression>( create<BlockStatement>(StatementList{})),
Source{}, mod.RegisterSymbol("Ident"), "Ident"), create<ElseStatement>(nullptr,
create<BlockStatement>(Source{}, StatementList{})), create<BlockStatement>(StatementList{})),
create<ElseStatement>(
Source{}, nullptr,
create<BlockStatement>(Source{}, StatementList{})),
}); });
EXPECT_TRUE(stmt.IsValid()); EXPECT_TRUE(stmt->IsValid());
} }
TEST_F(IfStatementTest, IsValid_MissingCondition) { TEST_F(IfStatementTest, IsValid_MissingCondition) {
auto* body = create<BlockStatement>( auto* body =
Source{}, StatementList{create<DiscardStatement>(Source{})}); create<BlockStatement>(StatementList{create<DiscardStatement>()});
IfStatement stmt(Source{}, nullptr, body, ElseStatementList{}); auto* stmt = create<IfStatement>(nullptr, body, ElseStatementList{});
EXPECT_FALSE(stmt.IsValid()); EXPECT_FALSE(stmt->IsValid());
} }
TEST_F(IfStatementTest, IsValid_InvalidCondition) { TEST_F(IfStatementTest, IsValid_InvalidCondition) {
auto* cond = auto* cond = Expr("");
create<IdentifierExpression>(Source{}, mod.RegisterSymbol(""), ""); auto* body =
auto* body = create<BlockStatement>( create<BlockStatement>(StatementList{create<DiscardStatement>()});
Source{}, StatementList{create<DiscardStatement>(Source{})}); auto* stmt = create<IfStatement>(cond, body, ElseStatementList{});
IfStatement stmt(Source{}, cond, body, ElseStatementList{}); EXPECT_FALSE(stmt->IsValid());
EXPECT_FALSE(stmt.IsValid());
} }
TEST_F(IfStatementTest, IsValid_NullBodyStatement) { TEST_F(IfStatementTest, IsValid_NullBodyStatement) {
auto* cond = create<IdentifierExpression>(Source{}, auto* cond = Expr("cond");
mod.RegisterSymbol("cond"), "cond"); auto* body = create<BlockStatement>(StatementList{
auto* body = create<DiscardStatement>(),
create<BlockStatement>(Source{}, StatementList{
create<DiscardStatement>(Source{}),
nullptr, nullptr,
}); });
IfStatement stmt(Source{}, cond, body, ElseStatementList{}); auto* stmt = create<IfStatement>(cond, body, ElseStatementList{});
EXPECT_FALSE(stmt.IsValid()); EXPECT_FALSE(stmt->IsValid());
} }
TEST_F(IfStatementTest, IsValid_InvalidBodyStatement) { TEST_F(IfStatementTest, IsValid_InvalidBodyStatement) {
auto* cond = create<IdentifierExpression>(Source{}, auto* cond = Expr("cond");
mod.RegisterSymbol("cond"), "cond");
auto* body = create<BlockStatement>( auto* body = create<BlockStatement>(
Source{},
StatementList{ StatementList{
create<DiscardStatement>(Source{}), create<DiscardStatement>(),
create<IfStatement>(Source{}, nullptr, create<IfStatement>(nullptr, create<BlockStatement>(StatementList{}),
create<BlockStatement>(Source{}, StatementList{}),
ast::ElseStatementList{}), ast::ElseStatementList{}),
}); });
IfStatement stmt(Source{}, cond, body, ElseStatementList{}); auto* stmt = create<IfStatement>(cond, body, ElseStatementList{});
EXPECT_FALSE(stmt.IsValid()); EXPECT_FALSE(stmt->IsValid());
} }
TEST_F(IfStatementTest, IsValid_NullElseStatement) { TEST_F(IfStatementTest, IsValid_NullElseStatement) {
auto* cond = create<IdentifierExpression>(Source{}, auto* cond = Expr("cond");
mod.RegisterSymbol("cond"), "cond"); auto* body =
auto* body = create<BlockStatement>( create<BlockStatement>(StatementList{create<DiscardStatement>()});
Source{}, StatementList{create<DiscardStatement>(Source{})}); auto* stmt = create<IfStatement>(
IfStatement stmt(Source{}, cond, body, cond, body,
{ ElseStatementList{
create<ElseStatement>( create<ElseStatement>(create<IdentifierExpression>(
Source{}, mod->RegisterSymbol("Ident"), "Ident"),
create<IdentifierExpression>( create<BlockStatement>(StatementList{})),
Source{}, mod.RegisterSymbol("Ident"), "Ident"), create<ElseStatement>(nullptr,
create<BlockStatement>(Source{}, StatementList{})), create<BlockStatement>(StatementList{})),
create<ElseStatement>(
Source{}, nullptr,
create<BlockStatement>(Source{}, StatementList{})),
nullptr, nullptr,
}); });
EXPECT_FALSE(stmt.IsValid()); EXPECT_FALSE(stmt->IsValid());
} }
TEST_F(IfStatementTest, IsValid_InvalidElseStatement) { TEST_F(IfStatementTest, IsValid_InvalidElseStatement) {
auto* cond = create<IdentifierExpression>(Source{}, auto* cond = Expr("cond");
mod.RegisterSymbol("cond"), "cond"); auto* body =
auto* body = create<BlockStatement>( create<BlockStatement>(StatementList{create<DiscardStatement>()});
Source{}, StatementList{create<DiscardStatement>(Source{})}); auto* stmt = create<IfStatement>(
IfStatement stmt(Source{}, cond, body, cond, body,
{ ElseStatementList{
create<ElseStatement>( create<ElseStatement>(Expr(""),
Source{}, create<BlockStatement>(StatementList{})),
create<IdentifierExpression>(
Source{}, mod.RegisterSymbol(""), ""),
create<BlockStatement>(Source{}, StatementList{})),
}); });
EXPECT_FALSE(stmt.IsValid()); EXPECT_FALSE(stmt->IsValid());
} }
TEST_F(IfStatementTest, IsValid_MultipleElseWiththoutCondition) { TEST_F(IfStatementTest, IsValid_MultipleElseWiththoutCondition) {
auto* cond = create<IdentifierExpression>(Source{}, auto* cond = Expr("cond");
mod.RegisterSymbol("cond"), "cond"); auto* body =
auto* body = create<BlockStatement>( create<BlockStatement>(StatementList{create<DiscardStatement>()});
Source{}, StatementList{create<DiscardStatement>(Source{})}); auto* stmt = create<IfStatement>(
IfStatement stmt(Source{}, cond, body, cond, body,
{ ElseStatementList{
create<ElseStatement>( create<ElseStatement>(nullptr,
Source{}, nullptr, create<BlockStatement>(StatementList{})),
create<BlockStatement>(Source{}, StatementList{})), create<ElseStatement>(nullptr,
create<ElseStatement>( create<BlockStatement>(StatementList{})),
Source{}, nullptr,
create<BlockStatement>(Source{}, StatementList{})),
}); });
EXPECT_FALSE(stmt.IsValid()); EXPECT_FALSE(stmt->IsValid());
} }
TEST_F(IfStatementTest, IsValid_ElseNotLast) { TEST_F(IfStatementTest, IsValid_ElseNotLast) {
auto* cond = create<IdentifierExpression>(Source{}, auto* cond = Expr("cond");
mod.RegisterSymbol("cond"), "cond"); auto* body =
auto* body = create<BlockStatement>( create<BlockStatement>(StatementList{create<DiscardStatement>()});
Source{}, StatementList{create<DiscardStatement>(Source{})}); auto* stmt = create<IfStatement>(
IfStatement stmt(Source{}, cond, body, cond, body,
{ ElseStatementList{
create<ElseStatement>( create<ElseStatement>(nullptr,
Source{}, nullptr, create<BlockStatement>(StatementList{})),
create<BlockStatement>(Source{}, StatementList{})), create<ElseStatement>(create<IdentifierExpression>(
create<ElseStatement>( mod->RegisterSymbol("Ident"), "Ident"),
Source{}, create<BlockStatement>(StatementList{})),
create<IdentifierExpression>(
Source{}, mod.RegisterSymbol("Ident"), "Ident"),
create<BlockStatement>(Source{}, StatementList{})),
}); });
EXPECT_FALSE(stmt.IsValid()); EXPECT_FALSE(stmt->IsValid());
} }
TEST_F(IfStatementTest, ToStr) { TEST_F(IfStatementTest, ToStr) {
auto* cond = create<IdentifierExpression>(Source{}, auto* cond = Expr("cond");
mod.RegisterSymbol("cond"), "cond"); auto* body =
auto* body = create<BlockStatement>( create<BlockStatement>(StatementList{create<DiscardStatement>()});
Source{}, StatementList{create<DiscardStatement>(Source{})}); auto* stmt = create<IfStatement>(cond, body, ElseStatementList{});
IfStatement stmt(Source{}, cond, body, ElseStatementList{});
std::ostringstream out; std::ostringstream out;
stmt.to_str(out, 2); stmt->to_str(out, 2);
EXPECT_EQ(demangle(out.str()), R"( If{ EXPECT_EQ(demangle(out.str()), R"( If{
( (
Identifier[not set]{cond} Identifier[not set]{cond}
@ -207,27 +184,24 @@ TEST_F(IfStatementTest, ToStr) {
} }
TEST_F(IfStatementTest, ToStr_WithElseStatements) { TEST_F(IfStatementTest, ToStr_WithElseStatements) {
auto* cond = create<IdentifierExpression>(Source{}, auto* cond = Expr("cond");
mod.RegisterSymbol("cond"), "cond"); auto* body =
auto* body = create<BlockStatement>( create<BlockStatement>(StatementList{create<DiscardStatement>()});
Source{}, StatementList{create<DiscardStatement>(Source{})}); auto* else_if_body =
auto* else_if_body = create<BlockStatement>( create<BlockStatement>(StatementList{create<DiscardStatement>()});
Source{}, StatementList{create<DiscardStatement>(Source{})});
auto* else_body = create<BlockStatement>( auto* else_body = create<BlockStatement>(
Source{}, StatementList{create<DiscardStatement>(Source{}), StatementList{create<DiscardStatement>(), create<DiscardStatement>()});
create<DiscardStatement>(Source{})}); auto* stmt = create<IfStatement>(
IfStatement stmt(Source{}, cond, body, cond, body,
{ ElseStatementList{
create<ElseStatement>( create<ElseStatement>(create<IdentifierExpression>(
Source{}, mod->RegisterSymbol("ident"), "ident"),
create<IdentifierExpression>(
Source{}, mod.RegisterSymbol("ident"), "ident"),
else_if_body), else_if_body),
create<ElseStatement>(Source{}, nullptr, else_body), create<ElseStatement>(nullptr, else_body),
}); });
std::ostringstream out; std::ostringstream out;
stmt.to_str(out, 2); stmt->to_str(out, 2);
EXPECT_EQ(demangle(out.str()), R"( If{ EXPECT_EQ(demangle(out.str()), R"( If{
( (
Identifier[not set]{cond} Identifier[not set]{cond}

View File

@ -30,15 +30,13 @@ namespace {
using IntLiteralTest = TestHelper; using IntLiteralTest = TestHelper;
TEST_F(IntLiteralTest, Sint_IsInt) { TEST_F(IntLiteralTest, Sint_IsInt) {
type::I32 i32; auto* i = create<SintLiteral>(ty.i32, 47);
SintLiteral i{Source{}, &i32, 47}; ASSERT_TRUE(i->Is<IntLiteral>());
ASSERT_TRUE(i.Is<IntLiteral>());
} }
TEST_F(IntLiteralTest, Uint_IsInt) { TEST_F(IntLiteralTest, Uint_IsInt) {
type::I32 i32; auto* i = create<UintLiteral>(ty.i32, 42);
UintLiteral i{Source{}, &i32, 42}; EXPECT_TRUE(i->Is<IntLiteral>());
EXPECT_TRUE(i.Is<IntLiteral>());
} }
} // namespace } // namespace

View File

@ -26,13 +26,12 @@ namespace {
using LocationDecorationTest = TestHelper; using LocationDecorationTest = TestHelper;
TEST_F(LocationDecorationTest, Creation) { TEST_F(LocationDecorationTest, Creation) {
LocationDecoration d{Source{}, 2}; auto* d = create<LocationDecoration>(2);
EXPECT_EQ(2u, d.value()); EXPECT_EQ(2u, d->value());
} }
TEST_F(LocationDecorationTest, Is) { TEST_F(LocationDecorationTest, Is) {
LocationDecoration ld{Source{}, 2}; Decoration* d = create<LocationDecoration>(2);
Decoration* d = &ld;
EXPECT_FALSE(d->Is<BindingDecoration>()); EXPECT_FALSE(d->Is<BindingDecoration>());
EXPECT_FALSE(d->Is<BuiltinDecoration>()); EXPECT_FALSE(d->Is<BuiltinDecoration>());
EXPECT_FALSE(d->Is<ConstantIdDecoration>()); EXPECT_FALSE(d->Is<ConstantIdDecoration>());
@ -41,9 +40,9 @@ TEST_F(LocationDecorationTest, Is) {
} }
TEST_F(LocationDecorationTest, ToStr) { TEST_F(LocationDecorationTest, ToStr) {
LocationDecoration d{Source{}, 2}; auto* d = create<LocationDecoration>(2);
std::ostringstream out; std::ostringstream out;
d.to_str(out, 0); d->to_str(out, 0);
EXPECT_EQ(out.str(), R"(LocationDecoration{2} EXPECT_EQ(out.str(), R"(LocationDecoration{2}
)"); )");
} }

View File

@ -28,153 +28,150 @@ namespace {
using LoopStatementTest = TestHelper; using LoopStatementTest = TestHelper;
TEST_F(LoopStatementTest, Creation) { TEST_F(LoopStatementTest, Creation) {
auto* body = create<BlockStatement>( auto* body =
Source{}, StatementList{create<DiscardStatement>(Source{})}); create<BlockStatement>(StatementList{create<DiscardStatement>()});
auto* b = body->last(); auto* b = body->last();
auto* continuing = create<BlockStatement>( auto* continuing =
Source{}, StatementList{create<DiscardStatement>(Source{})}); create<BlockStatement>(StatementList{create<DiscardStatement>()});
LoopStatement l(Source{}, body, continuing); auto* l = create<LoopStatement>(body, continuing);
ASSERT_EQ(l.body()->size(), 1u); ASSERT_EQ(l->body()->size(), 1u);
EXPECT_EQ(l.body()->get(0), b); EXPECT_EQ(l->body()->get(0), b);
ASSERT_EQ(l.continuing()->size(), 1u); ASSERT_EQ(l->continuing()->size(), 1u);
EXPECT_EQ(l.continuing()->get(0), continuing->last()); EXPECT_EQ(l->continuing()->get(0), continuing->last());
} }
TEST_F(LoopStatementTest, Creation_WithSource) { TEST_F(LoopStatementTest, Creation_WithSource) {
auto* body = create<BlockStatement>( auto* body =
Source{}, StatementList{create<DiscardStatement>(Source{})}); create<BlockStatement>(StatementList{create<DiscardStatement>()});
auto* continuing = create<BlockStatement>( auto* continuing =
Source{}, StatementList{create<DiscardStatement>(Source{})}); create<BlockStatement>(StatementList{create<DiscardStatement>()});
LoopStatement l(Source{Source::Location{20, 2}}, body, continuing); auto* l =
auto src = l.source(); create<LoopStatement>(Source{Source::Location{20, 2}}, body, continuing);
auto src = l->source();
EXPECT_EQ(src.range.begin.line, 20u); EXPECT_EQ(src.range.begin.line, 20u);
EXPECT_EQ(src.range.begin.column, 2u); EXPECT_EQ(src.range.begin.column, 2u);
} }
TEST_F(LoopStatementTest, IsLoop) { TEST_F(LoopStatementTest, IsLoop) {
LoopStatement l(Source{}, create<BlockStatement>(Source{}, StatementList{}), auto* l = create<LoopStatement>(create<BlockStatement>(StatementList{}),
create<BlockStatement>(Source{}, StatementList{})); create<BlockStatement>(StatementList{}));
EXPECT_TRUE(l.Is<LoopStatement>()); EXPECT_TRUE(l->Is<LoopStatement>());
} }
TEST_F(LoopStatementTest, HasContinuing_WithoutContinuing) { TEST_F(LoopStatementTest, HasContinuing_WithoutContinuing) {
auto* body = create<BlockStatement>( auto* body =
Source{}, StatementList{create<DiscardStatement>(Source{})}); create<BlockStatement>(StatementList{create<DiscardStatement>()});
LoopStatement l(Source{}, body, {}); auto* l = create<LoopStatement>(body, nullptr);
EXPECT_FALSE(l.has_continuing()); EXPECT_FALSE(l->has_continuing());
} }
TEST_F(LoopStatementTest, HasContinuing_WithContinuing) { TEST_F(LoopStatementTest, HasContinuing_WithContinuing) {
auto* body = create<BlockStatement>( auto* body =
Source{}, StatementList{create<DiscardStatement>(Source{})}); create<BlockStatement>(StatementList{create<DiscardStatement>()});
auto* continuing = create<BlockStatement>( auto* continuing =
Source{}, StatementList{create<DiscardStatement>(Source{})}); create<BlockStatement>(StatementList{create<DiscardStatement>()});
LoopStatement l(Source{}, body, continuing); auto* l = create<LoopStatement>(body, continuing);
EXPECT_TRUE(l.has_continuing()); EXPECT_TRUE(l->has_continuing());
} }
TEST_F(LoopStatementTest, IsValid) { TEST_F(LoopStatementTest, IsValid) {
auto* body = create<BlockStatement>( auto* body =
Source{}, StatementList{create<DiscardStatement>(Source{})}); create<BlockStatement>(StatementList{create<DiscardStatement>()});
auto* continuing = create<BlockStatement>( auto* continuing =
Source{}, StatementList{create<DiscardStatement>(Source{})}); create<BlockStatement>(StatementList{create<DiscardStatement>()});
LoopStatement l(Source{}, body, continuing); auto* l = create<LoopStatement>(body, continuing);
EXPECT_TRUE(l.IsValid()); EXPECT_TRUE(l->IsValid());
} }
TEST_F(LoopStatementTest, IsValid_WithoutContinuing) { TEST_F(LoopStatementTest, IsValid_WithoutContinuing) {
auto* body = create<BlockStatement>( auto* body =
Source{}, StatementList{create<DiscardStatement>(Source{})}); create<BlockStatement>(StatementList{create<DiscardStatement>()});
LoopStatement l(Source{}, body, auto* l =
create<BlockStatement>(Source{}, StatementList{})); create<LoopStatement>(body, create<BlockStatement>(StatementList{}));
EXPECT_TRUE(l.IsValid()); EXPECT_TRUE(l->IsValid());
} }
TEST_F(LoopStatementTest, IsValid_WithoutBody) { TEST_F(LoopStatementTest, IsValid_WithoutBody) {
LoopStatement l(Source{}, create<BlockStatement>(Source{}, StatementList{}), auto* l = create<LoopStatement>(create<BlockStatement>(StatementList{}),
create<BlockStatement>(Source{}, StatementList{})); create<BlockStatement>(StatementList{}));
EXPECT_TRUE(l.IsValid()); EXPECT_TRUE(l->IsValid());
} }
TEST_F(LoopStatementTest, IsValid_NullBodyStatement) { TEST_F(LoopStatementTest, IsValid_NullBodyStatement) {
auto* body = auto* body = create<BlockStatement>(StatementList{
create<BlockStatement>(Source{}, StatementList{ create<DiscardStatement>(),
create<DiscardStatement>(Source{}),
nullptr, nullptr,
}); });
auto* continuing = create<BlockStatement>( auto* continuing =
Source{}, StatementList{create<DiscardStatement>(Source{})}); create<BlockStatement>(StatementList{create<DiscardStatement>()});
LoopStatement l(Source{}, body, continuing); auto* l = create<LoopStatement>(body, continuing);
EXPECT_FALSE(l.IsValid()); EXPECT_FALSE(l->IsValid());
} }
TEST_F(LoopStatementTest, IsValid_InvalidBodyStatement) { TEST_F(LoopStatementTest, IsValid_InvalidBodyStatement) {
auto* body = create<BlockStatement>( auto* body = create<BlockStatement>(
Source{},
StatementList{ StatementList{
create<DiscardStatement>(Source{}), create<DiscardStatement>(),
create<IfStatement>(Source{}, nullptr, create<IfStatement>(nullptr, create<BlockStatement>(StatementList{}),
create<BlockStatement>(Source{}, StatementList{}),
ElseStatementList{}), ElseStatementList{}),
}); });
auto* continuing = create<BlockStatement>( auto* continuing =
Source{}, StatementList{create<DiscardStatement>(Source{})}); create<BlockStatement>(StatementList{create<DiscardStatement>()});
LoopStatement l(Source{}, body, continuing); auto* l = create<LoopStatement>(body, continuing);
EXPECT_FALSE(l.IsValid()); EXPECT_FALSE(l->IsValid());
} }
TEST_F(LoopStatementTest, IsValid_NullContinuingStatement) { TEST_F(LoopStatementTest, IsValid_NullContinuingStatement) {
auto* body = create<BlockStatement>( auto* body =
Source{}, StatementList{create<DiscardStatement>(Source{})}); create<BlockStatement>(StatementList{create<DiscardStatement>()});
auto* continuing = auto* continuing = create<BlockStatement>(StatementList{
create<BlockStatement>(Source{}, StatementList{ create<DiscardStatement>(),
create<DiscardStatement>(Source{}),
nullptr, nullptr,
}); });
LoopStatement l(Source{}, body, continuing); auto* l = create<LoopStatement>(body, continuing);
EXPECT_FALSE(l.IsValid()); EXPECT_FALSE(l->IsValid());
} }
TEST_F(LoopStatementTest, IsValid_InvalidContinuingStatement) { TEST_F(LoopStatementTest, IsValid_InvalidContinuingStatement) {
auto* body = create<BlockStatement>( auto* body =
Source{}, StatementList{create<DiscardStatement>(Source{})}); create<BlockStatement>(StatementList{create<DiscardStatement>()});
auto* continuing = create<BlockStatement>( auto* continuing = create<BlockStatement>(
Source{},
StatementList{ StatementList{
create<DiscardStatement>(Source{}), create<DiscardStatement>(),
create<IfStatement>(Source{}, nullptr, create<IfStatement>(nullptr, create<BlockStatement>(StatementList{}),
create<BlockStatement>(Source{}, StatementList{}),
ElseStatementList{}), ElseStatementList{}),
}); });
LoopStatement l(Source{}, body, continuing); auto* l = create<LoopStatement>(body, continuing);
EXPECT_FALSE(l.IsValid()); EXPECT_FALSE(l->IsValid());
} }
TEST_F(LoopStatementTest, ToStr) { TEST_F(LoopStatementTest, ToStr) {
auto* body = create<BlockStatement>( auto* body =
Source{}, StatementList{create<DiscardStatement>(Source{})}); create<BlockStatement>(StatementList{create<DiscardStatement>()});
LoopStatement l(Source{}, body, {}); auto* l = create<LoopStatement>(body, nullptr);
std::ostringstream out; std::ostringstream out;
l.to_str(out, 2); l->to_str(out, 2);
EXPECT_EQ(out.str(), R"( Loop{ EXPECT_EQ(out.str(), R"( Loop{
Discard{} Discard{}
} }
@ -182,15 +179,15 @@ TEST_F(LoopStatementTest, ToStr) {
} }
TEST_F(LoopStatementTest, ToStr_WithContinuing) { TEST_F(LoopStatementTest, ToStr_WithContinuing) {
auto* body = create<BlockStatement>( auto* body =
Source{}, StatementList{create<DiscardStatement>(Source{})}); create<BlockStatement>(StatementList{create<DiscardStatement>()});
auto* continuing = create<BlockStatement>( auto* continuing =
Source{}, StatementList{create<DiscardStatement>(Source{})}); create<BlockStatement>(StatementList{create<DiscardStatement>()});
LoopStatement l(Source{}, body, continuing); auto* l = create<LoopStatement>(body, continuing);
std::ostringstream out; std::ostringstream out;
l.to_str(out, 2); l->to_str(out, 2);
EXPECT_EQ(out.str(), R"( Loop{ EXPECT_EQ(out.str(), R"( Loop{
Discard{} Discard{}
continuing { continuing {

View File

@ -26,93 +26,85 @@ namespace {
using MemberAccessorExpressionTest = TestHelper; using MemberAccessorExpressionTest = TestHelper;
TEST_F(MemberAccessorExpressionTest, Creation) { TEST_F(MemberAccessorExpressionTest, Creation) {
auto* str = create<IdentifierExpression>( auto* str = create<IdentifierExpression>(mod->RegisterSymbol("structure"),
Source{}, mod.RegisterSymbol("structure"), "structure"); "structure");
auto* mem = create<IdentifierExpression>( auto* mem = Expr("member");
Source{}, mod.RegisterSymbol("member"), "member");
MemberAccessorExpression stmt(Source{}, str, mem); auto* stmt = create<MemberAccessorExpression>(str, mem);
EXPECT_EQ(stmt.structure(), str); EXPECT_EQ(stmt->structure(), str);
EXPECT_EQ(stmt.member(), mem); EXPECT_EQ(stmt->member(), mem);
} }
TEST_F(MemberAccessorExpressionTest, Creation_WithSource) { TEST_F(MemberAccessorExpressionTest, Creation_WithSource) {
auto* str = create<IdentifierExpression>( auto* str = create<IdentifierExpression>(mod->RegisterSymbol("structure"),
Source{}, mod.RegisterSymbol("structure"), "structure"); "structure");
auto* mem = create<IdentifierExpression>( auto* mem = Expr("member");
Source{}, mod.RegisterSymbol("member"), "member");
MemberAccessorExpression stmt(Source{Source::Location{20, 2}}, str, mem); auto* stmt = create<MemberAccessorExpression>(Source{Source::Location{20, 2}},
auto src = stmt.source(); str, mem);
auto src = stmt->source();
EXPECT_EQ(src.range.begin.line, 20u); EXPECT_EQ(src.range.begin.line, 20u);
EXPECT_EQ(src.range.begin.column, 2u); EXPECT_EQ(src.range.begin.column, 2u);
} }
TEST_F(MemberAccessorExpressionTest, IsMemberAccessor) { TEST_F(MemberAccessorExpressionTest, IsMemberAccessor) {
auto* str = create<IdentifierExpression>( auto* str = create<IdentifierExpression>(mod->RegisterSymbol("structure"),
Source{}, mod.RegisterSymbol("structure"), "structure"); "structure");
auto* mem = create<IdentifierExpression>( auto* mem = Expr("member");
Source{}, mod.RegisterSymbol("member"), "member");
MemberAccessorExpression stmt(Source{}, str, mem); auto* stmt = create<MemberAccessorExpression>(str, mem);
EXPECT_TRUE(stmt.Is<MemberAccessorExpression>()); EXPECT_TRUE(stmt->Is<MemberAccessorExpression>());
} }
TEST_F(MemberAccessorExpressionTest, IsValid) { TEST_F(MemberAccessorExpressionTest, IsValid) {
auto* str = create<IdentifierExpression>( auto* str = create<IdentifierExpression>(mod->RegisterSymbol("structure"),
Source{}, mod.RegisterSymbol("structure"), "structure"); "structure");
auto* mem = create<IdentifierExpression>( auto* mem = Expr("member");
Source{}, mod.RegisterSymbol("member"), "member");
MemberAccessorExpression stmt(Source{}, str, mem); auto* stmt = create<MemberAccessorExpression>(str, mem);
EXPECT_TRUE(stmt.IsValid()); EXPECT_TRUE(stmt->IsValid());
} }
TEST_F(MemberAccessorExpressionTest, IsValid_NullStruct) { TEST_F(MemberAccessorExpressionTest, IsValid_NullStruct) {
auto* mem = create<IdentifierExpression>( auto* mem = Expr("member");
Source{}, mod.RegisterSymbol("member"), "member");
MemberAccessorExpression stmt(Source{}, nullptr, mem); auto* stmt = create<MemberAccessorExpression>(nullptr, mem);
EXPECT_FALSE(stmt.IsValid()); EXPECT_FALSE(stmt->IsValid());
} }
TEST_F(MemberAccessorExpressionTest, IsValid_InvalidStruct) { TEST_F(MemberAccessorExpressionTest, IsValid_InvalidStruct) {
auto* str = auto* str = Expr("");
create<IdentifierExpression>(Source{}, mod.RegisterSymbol(""), ""); auto* mem = Expr("member");
auto* mem = create<IdentifierExpression>(
Source{}, mod.RegisterSymbol("member"), "member");
MemberAccessorExpression stmt(Source{}, str, mem); auto* stmt = create<MemberAccessorExpression>(str, mem);
EXPECT_FALSE(stmt.IsValid()); EXPECT_FALSE(stmt->IsValid());
} }
TEST_F(MemberAccessorExpressionTest, IsValid_NullMember) { TEST_F(MemberAccessorExpressionTest, IsValid_NullMember) {
auto* str = create<IdentifierExpression>( auto* str = create<IdentifierExpression>(mod->RegisterSymbol("structure"),
Source{}, mod.RegisterSymbol("structure"), "structure"); "structure");
MemberAccessorExpression stmt(Source{}, str, nullptr); auto* stmt = create<MemberAccessorExpression>(str, nullptr);
EXPECT_FALSE(stmt.IsValid()); EXPECT_FALSE(stmt->IsValid());
} }
TEST_F(MemberAccessorExpressionTest, IsValid_InvalidMember) { TEST_F(MemberAccessorExpressionTest, IsValid_InvalidMember) {
auto* str = create<IdentifierExpression>( auto* str = create<IdentifierExpression>(mod->RegisterSymbol("structure"),
Source{}, mod.RegisterSymbol("structure"), "structure"); "structure");
auto* mem = auto* mem = Expr("");
create<IdentifierExpression>(Source{}, mod.RegisterSymbol(""), "");
MemberAccessorExpression stmt(Source{}, str, mem); auto* stmt = create<MemberAccessorExpression>(str, mem);
EXPECT_FALSE(stmt.IsValid()); EXPECT_FALSE(stmt->IsValid());
} }
TEST_F(MemberAccessorExpressionTest, ToStr) { TEST_F(MemberAccessorExpressionTest, ToStr) {
auto* str = create<IdentifierExpression>( auto* str = create<IdentifierExpression>(mod->RegisterSymbol("structure"),
Source{}, mod.RegisterSymbol("structure"), "structure"); "structure");
auto* mem = create<IdentifierExpression>( auto* mem = Expr("member");
Source{}, mod.RegisterSymbol("member"), "member");
MemberAccessorExpression stmt(Source{}, str, mem); auto* stmt = create<MemberAccessorExpression>(str, mem);
std::ostringstream out; std::ostringstream out;
stmt.to_str(out, 2); stmt->to_str(out, 2);
EXPECT_EQ(demangle(out.str()), R"( MemberAccessor[not set]{ EXPECT_EQ(demangle(out.str()), R"( MemberAccessor[not set]{
Identifier[not set]{structure} Identifier[not set]{structure}
Identifier[not set]{member} Identifier[not set]{member}

View File

@ -45,13 +45,11 @@ TEST_F(ModuleTest, ToStrEmitsPreambleAndPostamble) {
} }
TEST_F(ModuleTest, LookupFunction) { TEST_F(ModuleTest, LookupFunction) {
type::F32 f32;
Module m; Module m;
auto func_sym = m.RegisterSymbol("main"); auto func_sym = m.RegisterSymbol("main");
auto* func = auto* func = create<Function>(func_sym, "main", VariableList{}, ty.f32,
create<Function>(Source{}, func_sym, "main", VariableList{}, &f32, create<BlockStatement>(StatementList{}),
create<BlockStatement>(Source{}, StatementList{}),
ast::FunctionDecorationList{}); ast::FunctionDecorationList{});
m.AddFunction(func); m.AddFunction(func);
EXPECT_EQ(func, m.FindFunctionBySymbol(func_sym)); EXPECT_EQ(func, m.FindFunctionBySymbol(func_sym));
@ -68,9 +66,8 @@ TEST_F(ModuleTest, IsValid_Empty) {
} }
TEST_F(ModuleTest, IsValid_GlobalVariable) { TEST_F(ModuleTest, IsValid_GlobalVariable) {
type::F32 f32; auto* var = create<Variable>("var", StorageClass::kInput, ty.f32, false,
auto* var = create<Variable>(Source{}, "var", StorageClass::kInput, &f32, nullptr, ast::VariableDecorationList{});
false, nullptr, ast::VariableDecorationList{});
Module m; Module m;
m.AddGlobalVariable(var); m.AddGlobalVariable(var);
@ -84,8 +81,8 @@ TEST_F(ModuleTest, IsValid_Null_GlobalVariable) {
} }
TEST_F(ModuleTest, IsValid_Invalid_GlobalVariable) { TEST_F(ModuleTest, IsValid_Invalid_GlobalVariable) {
auto* var = create<Variable>(Source{}, "var", StorageClass::kInput, nullptr, auto* var = create<Variable>("var", StorageClass::kInput, nullptr, false,
false, nullptr, ast::VariableDecorationList{}); nullptr, ast::VariableDecorationList{});
Module m; Module m;
m.AddGlobalVariable(var); m.AddGlobalVariable(var);
@ -93,8 +90,7 @@ TEST_F(ModuleTest, IsValid_Invalid_GlobalVariable) {
} }
TEST_F(ModuleTest, IsValid_Alias) { TEST_F(ModuleTest, IsValid_Alias) {
type::F32 f32; type::Alias alias(mod->RegisterSymbol("alias"), "alias", ty.f32);
type::Alias alias(mod.RegisterSymbol("alias"), "alias", &f32);
Module m; Module m;
m.AddConstructedType(&alias); m.AddConstructedType(&alias);
@ -108,9 +104,8 @@ TEST_F(ModuleTest, IsValid_Null_Alias) {
} }
TEST_F(ModuleTest, IsValid_Struct) { TEST_F(ModuleTest, IsValid_Struct) {
type::F32 f32; type::Struct st(mod->RegisterSymbol("name"), "name", {});
type::Struct st(mod.RegisterSymbol("name"), "name", {}); type::Alias alias(mod->RegisterSymbol("name"), "name", &st);
type::Alias alias(mod.RegisterSymbol("name"), "name", &st);
Module m; Module m;
m.AddConstructedType(&alias); m.AddConstructedType(&alias);
@ -118,9 +113,8 @@ TEST_F(ModuleTest, IsValid_Struct) {
} }
TEST_F(ModuleTest, IsValid_Struct_EmptyName) { TEST_F(ModuleTest, IsValid_Struct_EmptyName) {
type::F32 f32; type::Struct st(mod->RegisterSymbol(""), "", {});
type::Struct st(mod.RegisterSymbol(""), "", {}); type::Alias alias(mod->RegisterSymbol("name"), "name", &st);
type::Alias alias(mod.RegisterSymbol("name"), "name", &st);
Module m; Module m;
m.AddConstructedType(&alias); m.AddConstructedType(&alias);
@ -128,14 +122,11 @@ TEST_F(ModuleTest, IsValid_Struct_EmptyName) {
} }
TEST_F(ModuleTest, IsValid_Function) { TEST_F(ModuleTest, IsValid_Function) {
type::F32 f32;
Module m; Module m;
auto* func = create<Function>( auto* func = create<Function>(
Source{}, m.RegisterSymbol("main"), "main", VariableList(), &f32, m.RegisterSymbol("main"), "main", VariableList(), ty.f32,
create<BlockStatement>(Source{}, StatementList{}), create<BlockStatement>(StatementList{}), ast::FunctionDecorationList{});
ast::FunctionDecorationList{});
m.AddFunction(func); m.AddFunction(func);
EXPECT_TRUE(m.IsValid()); EXPECT_TRUE(m.IsValid());
@ -152,8 +143,7 @@ TEST_F(ModuleTest, IsValid_Invalid_Function) {
Module m; Module m;
auto* func = auto* func = create<Function>(m.RegisterSymbol("main"), "main", p, nullptr,
create<Function>(Source{}, m.RegisterSymbol("main"), "main", p, nullptr,
nullptr, ast::FunctionDecorationList{}); nullptr, ast::FunctionDecorationList{});
m.AddFunction(func); m.AddFunction(func);

View File

@ -28,9 +28,7 @@ namespace {
using NullLiteralTest = TestHelper; using NullLiteralTest = TestHelper;
TEST_F(NullLiteralTest, Is) { TEST_F(NullLiteralTest, Is) {
type::I32 i32; ast::Literal* l = create<NullLiteral>(ty.i32);
NullLiteral i{Source{}, &i32};
Literal* l = &i;
EXPECT_FALSE(l->Is<BoolLiteral>()); EXPECT_FALSE(l->Is<BoolLiteral>());
EXPECT_FALSE(l->Is<SintLiteral>()); EXPECT_FALSE(l->Is<SintLiteral>());
EXPECT_FALSE(l->Is<FloatLiteral>()); EXPECT_FALSE(l->Is<FloatLiteral>());
@ -40,16 +38,14 @@ TEST_F(NullLiteralTest, Is) {
} }
TEST_F(NullLiteralTest, ToStr) { TEST_F(NullLiteralTest, ToStr) {
type::I32 i32; auto* i = create<NullLiteral>(ty.i32);
NullLiteral i{Source{}, &i32};
EXPECT_EQ(i.to_str(), "null __i32"); EXPECT_EQ(i->to_str(), "null __i32");
} }
TEST_F(NullLiteralTest, Name_I32) { TEST_F(NullLiteralTest, Name_I32) {
type::I32 i32; auto* i = create<NullLiteral>(ty.i32);
NullLiteral i{Source{}, &i32}; EXPECT_EQ("__null__i32", i->name());
EXPECT_EQ("__null__i32", i.name());
} }
} // namespace } // namespace

View File

@ -26,62 +26,57 @@ namespace {
using ReturnStatementTest = TestHelper; using ReturnStatementTest = TestHelper;
TEST_F(ReturnStatementTest, Creation) { TEST_F(ReturnStatementTest, Creation) {
auto* expr = create<IdentifierExpression>(Source{}, auto* expr = Expr("expr");
mod.RegisterSymbol("expr"), "expr");
ReturnStatement r(Source{}, expr); auto* r = create<ReturnStatement>(expr);
EXPECT_EQ(r.value(), expr); EXPECT_EQ(r->value(), expr);
} }
TEST_F(ReturnStatementTest, Creation_WithSource) { TEST_F(ReturnStatementTest, Creation_WithSource) {
ReturnStatement r(Source{Source::Location{20, 2}}); auto* r = create<ReturnStatement>(Source{Source::Location{20, 2}});
auto src = r.source(); auto src = r->source();
EXPECT_EQ(src.range.begin.line, 20u); EXPECT_EQ(src.range.begin.line, 20u);
EXPECT_EQ(src.range.begin.column, 2u); EXPECT_EQ(src.range.begin.column, 2u);
} }
TEST_F(ReturnStatementTest, IsReturn) { TEST_F(ReturnStatementTest, IsReturn) {
ReturnStatement r(Source{}); auto* r = create<ReturnStatement>();
EXPECT_TRUE(r.Is<ReturnStatement>()); EXPECT_TRUE(r->Is<ReturnStatement>());
} }
TEST_F(ReturnStatementTest, HasValue_WithoutValue) { TEST_F(ReturnStatementTest, HasValue_WithoutValue) {
ReturnStatement r(Source{}); auto* r = create<ReturnStatement>();
EXPECT_FALSE(r.has_value()); EXPECT_FALSE(r->has_value());
} }
TEST_F(ReturnStatementTest, HasValue_WithValue) { TEST_F(ReturnStatementTest, HasValue_WithValue) {
auto* expr = create<IdentifierExpression>(Source{}, auto* expr = Expr("expr");
mod.RegisterSymbol("expr"), "expr"); auto* r = create<ReturnStatement>(expr);
ReturnStatement r(Source{}, expr); EXPECT_TRUE(r->has_value());
EXPECT_TRUE(r.has_value());
} }
TEST_F(ReturnStatementTest, IsValid_WithoutValue) { TEST_F(ReturnStatementTest, IsValid_WithoutValue) {
ReturnStatement r(Source{}); auto* r = create<ReturnStatement>();
EXPECT_TRUE(r.IsValid()); EXPECT_TRUE(r->IsValid());
} }
TEST_F(ReturnStatementTest, IsValid_WithValue) { TEST_F(ReturnStatementTest, IsValid_WithValue) {
auto* expr = create<IdentifierExpression>(Source{}, auto* expr = Expr("expr");
mod.RegisterSymbol("expr"), "expr"); auto* r = create<ReturnStatement>(expr);
ReturnStatement r(Source{}, expr); EXPECT_TRUE(r->IsValid());
EXPECT_TRUE(r.IsValid());
} }
TEST_F(ReturnStatementTest, IsValid_InvalidValue) { TEST_F(ReturnStatementTest, IsValid_InvalidValue) {
auto* expr = auto* expr = Expr("");
create<IdentifierExpression>(Source{}, mod.RegisterSymbol(""), ""); auto* r = create<ReturnStatement>(expr);
ReturnStatement r(Source{}, expr); EXPECT_FALSE(r->IsValid());
EXPECT_FALSE(r.IsValid());
} }
TEST_F(ReturnStatementTest, ToStr_WithValue) { TEST_F(ReturnStatementTest, ToStr_WithValue) {
auto* expr = create<IdentifierExpression>(Source{}, auto* expr = Expr("expr");
mod.RegisterSymbol("expr"), "expr"); auto* r = create<ReturnStatement>(expr);
ReturnStatement r(Source{}, expr);
std::ostringstream out; std::ostringstream out;
r.to_str(out, 2); r->to_str(out, 2);
EXPECT_EQ(demangle(out.str()), R"( Return{ EXPECT_EQ(demangle(out.str()), R"( Return{
{ {
Identifier[not set]{expr} Identifier[not set]{expr}
@ -91,9 +86,9 @@ TEST_F(ReturnStatementTest, ToStr_WithValue) {
} }
TEST_F(ReturnStatementTest, ToStr_WithoutValue) { TEST_F(ReturnStatementTest, ToStr_WithoutValue) {
ReturnStatement r(Source{}); auto* r = create<ReturnStatement>();
std::ostringstream out; std::ostringstream out;
r.to_str(out, 2); r->to_str(out, 2);
EXPECT_EQ(out.str(), R"( Return{} EXPECT_EQ(out.str(), R"( Return{}
)"); )");
} }

View File

@ -26,38 +26,39 @@ using ScalarConstructorExpressionTest = TestHelper;
TEST_F(ScalarConstructorExpressionTest, Creation) { TEST_F(ScalarConstructorExpressionTest, Creation) {
type::Bool bool_type; type::Bool bool_type;
auto* b = create<BoolLiteral>(Source{}, &bool_type, true); auto* b = create<BoolLiteral>(&bool_type, true);
ScalarConstructorExpression c(Source{}, b); auto* c = create<ScalarConstructorExpression>(b);
EXPECT_EQ(c.literal(), b); EXPECT_EQ(c->literal(), b);
} }
TEST_F(ScalarConstructorExpressionTest, Creation_WithSource) { TEST_F(ScalarConstructorExpressionTest, Creation_WithSource) {
type::Bool bool_type; type::Bool bool_type;
auto* b = create<BoolLiteral>(Source{}, &bool_type, true); auto* b = create<BoolLiteral>(&bool_type, true);
ScalarConstructorExpression c(Source{Source::Location{20, 2}}, b); auto* c =
auto src = c.source(); create<ScalarConstructorExpression>(Source{Source::Location{20, 2}}, b);
auto src = c->source();
EXPECT_EQ(src.range.begin.line, 20u); EXPECT_EQ(src.range.begin.line, 20u);
EXPECT_EQ(src.range.begin.column, 2u); EXPECT_EQ(src.range.begin.column, 2u);
} }
TEST_F(ScalarConstructorExpressionTest, IsValid) { TEST_F(ScalarConstructorExpressionTest, IsValid) {
type::Bool bool_type; type::Bool bool_type;
auto* b = create<BoolLiteral>(Source{}, &bool_type, true); auto* b = create<BoolLiteral>(&bool_type, true);
ScalarConstructorExpression c(Source{}, b); auto* c = create<ScalarConstructorExpression>(b);
EXPECT_TRUE(c.IsValid()); EXPECT_TRUE(c->IsValid());
} }
TEST_F(ScalarConstructorExpressionTest, IsValid_MissingLiteral) { TEST_F(ScalarConstructorExpressionTest, IsValid_MissingLiteral) {
ScalarConstructorExpression c(Source{}, nullptr); auto* c = create<ScalarConstructorExpression>(nullptr);
EXPECT_FALSE(c.IsValid()); EXPECT_FALSE(c->IsValid());
} }
TEST_F(ScalarConstructorExpressionTest, ToStr) { TEST_F(ScalarConstructorExpressionTest, ToStr) {
type::Bool bool_type; type::Bool bool_type;
auto* b = create<BoolLiteral>(Source{}, &bool_type, true); auto* b = create<BoolLiteral>(&bool_type, true);
ScalarConstructorExpression c(Source{}, b); auto* c = create<ScalarConstructorExpression>(b);
std::ostringstream out; std::ostringstream out;
c.to_str(out, 2); c->to_str(out, 2);
EXPECT_EQ(out.str(), R"( ScalarConstructor[not set]{true} EXPECT_EQ(out.str(), R"( ScalarConstructor[not set]{true}
)"); )");
} }

View File

@ -24,13 +24,12 @@ namespace {
using SetDecorationTest = TestHelper; using SetDecorationTest = TestHelper;
TEST_F(SetDecorationTest, Creation) { TEST_F(SetDecorationTest, Creation) {
SetDecoration d{Source{}, 2}; auto* d = create<SetDecoration>(2);
EXPECT_EQ(2u, d.value()); EXPECT_EQ(2u, d->value());
} }
TEST_F(SetDecorationTest, Is) { TEST_F(SetDecorationTest, Is) {
SetDecoration sd{Source{}, 2}; Decoration* d = create<SetDecoration>(2);
Decoration* d = &sd;
EXPECT_FALSE(d->Is<BindingDecoration>()); EXPECT_FALSE(d->Is<BindingDecoration>());
EXPECT_FALSE(d->Is<BuiltinDecoration>()); EXPECT_FALSE(d->Is<BuiltinDecoration>());
EXPECT_FALSE(d->Is<ConstantIdDecoration>()); EXPECT_FALSE(d->Is<ConstantIdDecoration>());
@ -39,9 +38,9 @@ TEST_F(SetDecorationTest, Is) {
} }
TEST_F(SetDecorationTest, ToStr) { TEST_F(SetDecorationTest, ToStr) {
SetDecoration d{Source{}, 2}; auto* d = create<SetDecoration>(2);
std::ostringstream out; std::ostringstream out;
d.to_str(out, 0); d->to_str(out, 0);
EXPECT_EQ(out.str(), R"(SetDecoration{2} EXPECT_EQ(out.str(), R"(SetDecoration{2}
)"); )");
} }

View File

@ -29,16 +29,13 @@ namespace {
using SintLiteralTest = TestHelper; using SintLiteralTest = TestHelper;
TEST_F(SintLiteralTest, Value) { TEST_F(SintLiteralTest, Value) {
type::I32 i32; auto* i = create<SintLiteral>(ty.i32, 47);
SintLiteral i{Source{}, &i32, 47}; ASSERT_TRUE(i->Is<SintLiteral>());
ASSERT_TRUE(i.Is<SintLiteral>()); EXPECT_EQ(i->value(), 47);
EXPECT_EQ(i.value(), 47);
} }
TEST_F(SintLiteralTest, Is) { TEST_F(SintLiteralTest, Is) {
type::I32 i32; ast::Literal* l = create<SintLiteral>(ty.i32, 42);
SintLiteral i{Source{}, &i32, 42};
Literal* l = &i;
EXPECT_FALSE(l->Is<BoolLiteral>()); EXPECT_FALSE(l->Is<BoolLiteral>());
EXPECT_TRUE(l->Is<SintLiteral>()); EXPECT_TRUE(l->Is<SintLiteral>());
EXPECT_FALSE(l->Is<FloatLiteral>()); EXPECT_FALSE(l->Is<FloatLiteral>());
@ -47,22 +44,20 @@ TEST_F(SintLiteralTest, Is) {
} }
TEST_F(SintLiteralTest, ToStr) { TEST_F(SintLiteralTest, ToStr) {
type::I32 i32; auto* i = create<SintLiteral>(ty.i32, -42);
SintLiteral i{Source{}, &i32, -42};
EXPECT_EQ(i.to_str(), "-42"); EXPECT_EQ(i->to_str(), "-42");
} }
TEST_F(SintLiteralTest, Name_I32) { TEST_F(SintLiteralTest, Name_I32) {
type::I32 i32; auto* i = create<SintLiteral>(ty.i32, 2);
SintLiteral i{Source{}, &i32, 2}; EXPECT_EQ("__sint__i32_2", i->name());
EXPECT_EQ("__sint__i32_2", i.name());
} }
TEST_F(SintLiteralTest, Name_U32) { TEST_F(SintLiteralTest, Name_U32) {
type::U32 u32; type::U32 u32;
SintLiteral i{Source{}, &u32, 2}; auto* i = create<SintLiteral>(&u32, 2);
EXPECT_EQ("__sint__u32_2", i.name()); EXPECT_EQ("__sint__u32_2", i->name());
} }
} // namespace } // namespace

View File

@ -26,21 +26,20 @@ namespace {
using StageDecorationTest = TestHelper; using StageDecorationTest = TestHelper;
TEST_F(StageDecorationTest, Creation_1param) { TEST_F(StageDecorationTest, Creation_1param) {
StageDecoration d{Source{}, PipelineStage::kFragment}; auto* d = create<StageDecoration>(PipelineStage::kFragment);
EXPECT_EQ(d.value(), PipelineStage::kFragment); EXPECT_EQ(d->value(), PipelineStage::kFragment);
} }
TEST_F(StageDecorationTest, Is) { TEST_F(StageDecorationTest, Is) {
StageDecoration sd{Source{}, PipelineStage::kFragment}; Decoration* d = create<StageDecoration>(PipelineStage::kFragment);
Decoration* d = &sd;
EXPECT_FALSE(d->Is<WorkgroupDecoration>()); EXPECT_FALSE(d->Is<WorkgroupDecoration>());
EXPECT_TRUE(d->Is<StageDecoration>()); EXPECT_TRUE(d->Is<StageDecoration>());
} }
TEST_F(StageDecorationTest, ToStr) { TEST_F(StageDecorationTest, ToStr) {
StageDecoration d{Source{}, PipelineStage::kFragment}; auto* d = create<StageDecoration>(PipelineStage::kFragment);
std::ostringstream out; std::ostringstream out;
d.to_str(out, 0); d->to_str(out, 0);
EXPECT_EQ(out.str(), R"(StageDecoration{fragment} EXPECT_EQ(out.str(), R"(StageDecoration{fragment}
)"); )");
} }

View File

@ -23,22 +23,22 @@ namespace {
using StrideDecorationTest = TestHelper; using StrideDecorationTest = TestHelper;
TEST_F(StrideDecorationTest, Creation) { TEST_F(StrideDecorationTest, Creation) {
StrideDecoration d{Source{}, 2}; auto* d = create<StrideDecoration>(2);
EXPECT_EQ(2u, d.stride()); EXPECT_EQ(2u, d->stride());
} }
TEST_F(StrideDecorationTest, Is) { TEST_F(StrideDecorationTest, Is) {
StrideDecoration d{Source{}, 2}; auto* d = create<StrideDecoration>(2);
EXPECT_TRUE(d.Is<StrideDecoration>()); EXPECT_TRUE(d->Is<StrideDecoration>());
} }
TEST_F(StrideDecorationTest, Source) { TEST_F(StrideDecorationTest, Source) {
StrideDecoration d{ auto* d = create<StrideDecoration>(
Source{Source::Range{Source::Location{1, 2}, Source::Location{3, 4}}}, 2}; Source{Source::Range{Source::Location{1, 2}, Source::Location{3, 4}}}, 2);
EXPECT_EQ(d.source().range.begin.line, 1u); EXPECT_EQ(d->source().range.begin.line, 1u);
EXPECT_EQ(d.source().range.begin.column, 2u); EXPECT_EQ(d->source().range.begin.column, 2u);
EXPECT_EQ(d.source().range.end.line, 3u); EXPECT_EQ(d->source().range.end.line, 3u);
EXPECT_EQ(d.source().range.end.column, 4u); EXPECT_EQ(d->source().range.end.column, 4u);
} }
} // namespace } // namespace

View File

@ -23,13 +23,13 @@ namespace {
using StructMemberOffsetDecorationTest = TestHelper; using StructMemberOffsetDecorationTest = TestHelper;
TEST_F(StructMemberOffsetDecorationTest, Creation) { TEST_F(StructMemberOffsetDecorationTest, Creation) {
StructMemberOffsetDecoration d{Source{}, 2}; auto* d = create<StructMemberOffsetDecoration>(2);
EXPECT_EQ(2u, d.offset()); EXPECT_EQ(2u, d->offset());
} }
TEST_F(StructMemberOffsetDecorationTest, Is) { TEST_F(StructMemberOffsetDecorationTest, Is) {
StructMemberOffsetDecoration d{Source{}, 2}; auto* d = create<StructMemberOffsetDecoration>(2);
EXPECT_TRUE(d.Is<StructMemberOffsetDecoration>()); EXPECT_TRUE(d->Is<StructMemberOffsetDecoration>());
} }
} // namespace } // namespace

View File

@ -28,78 +28,71 @@ namespace {
using StructMemberTest = TestHelper; using StructMemberTest = TestHelper;
TEST_F(StructMemberTest, Creation) { TEST_F(StructMemberTest, Creation) {
type::I32 i32;
StructMemberDecorationList decorations; StructMemberDecorationList decorations;
decorations.emplace_back(create<StructMemberOffsetDecoration>(Source{}, 4)); decorations.emplace_back(create<StructMemberOffsetDecoration>(4));
StructMember st{Source{}, "a", &i32, decorations}; auto* st = create<StructMember>("a", ty.i32, decorations);
EXPECT_EQ(st.name(), "a"); EXPECT_EQ(st->name(), "a");
EXPECT_EQ(st.type(), &i32); EXPECT_EQ(st->type(), ty.i32);
EXPECT_EQ(st.decorations().size(), 1u); EXPECT_EQ(st->decorations().size(), 1u);
EXPECT_TRUE(st.decorations()[0]->Is<StructMemberOffsetDecoration>()); EXPECT_TRUE(st->decorations()[0]->Is<StructMemberOffsetDecoration>());
EXPECT_EQ(st.source().range.begin.line, 0u); EXPECT_EQ(st->source().range.begin.line, 0u);
EXPECT_EQ(st.source().range.begin.column, 0u); EXPECT_EQ(st->source().range.begin.column, 0u);
EXPECT_EQ(st.source().range.end.line, 0u); EXPECT_EQ(st->source().range.end.line, 0u);
EXPECT_EQ(st.source().range.end.column, 0u); EXPECT_EQ(st->source().range.end.column, 0u);
} }
TEST_F(StructMemberTest, CreationWithSource) { TEST_F(StructMemberTest, CreationWithSource) {
type::I32 i32; auto* st = create<StructMember>(
Source s{Source::Range{Source::Location{27, 4}, Source::Location{27, 8}}}; Source{Source::Range{Source::Location{27, 4}, Source::Location{27, 8}}},
"a", ty.i32, StructMemberDecorationList{});
StructMember st{s, "a", &i32, {}}; EXPECT_EQ(st->name(), "a");
EXPECT_EQ(st.name(), "a"); EXPECT_EQ(st->type(), ty.i32);
EXPECT_EQ(st.type(), &i32); EXPECT_EQ(st->decorations().size(), 0u);
EXPECT_EQ(st.decorations().size(), 0u); EXPECT_EQ(st->source().range.begin.line, 27u);
EXPECT_EQ(st.source().range.begin.line, 27u); EXPECT_EQ(st->source().range.begin.column, 4u);
EXPECT_EQ(st.source().range.begin.column, 4u); EXPECT_EQ(st->source().range.end.line, 27u);
EXPECT_EQ(st.source().range.end.line, 27u); EXPECT_EQ(st->source().range.end.column, 8u);
EXPECT_EQ(st.source().range.end.column, 8u);
} }
TEST_F(StructMemberTest, IsValid) { TEST_F(StructMemberTest, IsValid) {
type::I32 i32; auto* st = create<StructMember>("a", ty.i32, StructMemberDecorationList{});
StructMember st{Source{}, "a", &i32, {}}; EXPECT_TRUE(st->IsValid());
EXPECT_TRUE(st.IsValid());
} }
TEST_F(StructMemberTest, IsValid_EmptyName) { TEST_F(StructMemberTest, IsValid_EmptyName) {
type::I32 i32; auto* st = create<StructMember>("", ty.i32, StructMemberDecorationList{});
StructMember st{Source{}, "", &i32, {}}; EXPECT_FALSE(st->IsValid());
EXPECT_FALSE(st.IsValid());
} }
TEST_F(StructMemberTest, IsValid_NullType) { TEST_F(StructMemberTest, IsValid_NullType) {
StructMember st{Source{}, "a", nullptr, {}}; auto* st = create<StructMember>("a", nullptr, StructMemberDecorationList{});
EXPECT_FALSE(st.IsValid()); EXPECT_FALSE(st->IsValid());
} }
TEST_F(StructMemberTest, IsValid_Null_Decoration) { TEST_F(StructMemberTest, IsValid_Null_Decoration) {
type::I32 i32;
StructMemberDecorationList decorations; StructMemberDecorationList decorations;
decorations.emplace_back(create<StructMemberOffsetDecoration>(Source{}, 4)); decorations.emplace_back(create<StructMemberOffsetDecoration>(4));
decorations.push_back(nullptr); decorations.push_back(nullptr);
StructMember st{Source{}, "a", &i32, decorations}; auto* st = create<StructMember>("a", ty.i32, decorations);
EXPECT_FALSE(st.IsValid()); EXPECT_FALSE(st->IsValid());
} }
TEST_F(StructMemberTest, ToStr) { TEST_F(StructMemberTest, ToStr) {
type::I32 i32;
StructMemberDecorationList decorations; StructMemberDecorationList decorations;
decorations.emplace_back(create<StructMemberOffsetDecoration>(Source{}, 4)); decorations.emplace_back(create<StructMemberOffsetDecoration>(4));
StructMember st{Source{}, "a", &i32, decorations}; auto* st = create<StructMember>("a", ty.i32, decorations);
std::ostringstream out; std::ostringstream out;
st.to_str(out, 2); st->to_str(out, 2);
EXPECT_EQ(out.str(), " StructMember{[[ offset 4 ]] a: __i32}\n"); EXPECT_EQ(out.str(), " StructMember{[[ offset 4 ]] a: __i32}\n");
} }
TEST_F(StructMemberTest, ToStrNoDecorations) { TEST_F(StructMemberTest, ToStrNoDecorations) {
type::I32 i32; auto* st = create<StructMember>("a", ty.i32, StructMemberDecorationList{});
StructMember st{Source{}, "a", &i32, {}};
std::ostringstream out; std::ostringstream out;
st.to_str(out, 2); st->to_str(out, 2);
EXPECT_EQ(out.str(), " StructMember{a: __i32}\n"); EXPECT_EQ(out.str(), " StructMember{a: __i32}\n");
} }

View File

@ -30,104 +30,93 @@ namespace {
using StructTest = TestHelper; using StructTest = TestHelper;
TEST_F(StructTest, Creation) { TEST_F(StructTest, Creation) {
type::I32 i32;
StructMemberList members; StructMemberList members;
members.push_back( members.push_back(
create<StructMember>(Source{}, "a", &i32, StructMemberDecorationList())); create<StructMember>("a", ty.i32, StructMemberDecorationList{}));
Struct s{Source{}, members, ast::StructDecorationList{}}; auto* s = create<Struct>(members, ast::StructDecorationList{});
EXPECT_EQ(s.members().size(), 1u); EXPECT_EQ(s->members().size(), 1u);
EXPECT_TRUE(s.decorations().empty()); EXPECT_TRUE(s->decorations().empty());
EXPECT_EQ(s.source().range.begin.line, 0u); EXPECT_EQ(s->source().range.begin.line, 0u);
EXPECT_EQ(s.source().range.begin.column, 0u); EXPECT_EQ(s->source().range.begin.column, 0u);
EXPECT_EQ(s.source().range.end.line, 0u); EXPECT_EQ(s->source().range.end.line, 0u);
EXPECT_EQ(s.source().range.end.column, 0u); EXPECT_EQ(s->source().range.end.column, 0u);
} } // namespace
TEST_F(StructTest, Creation_WithDecorations) { TEST_F(StructTest, Creation_WithDecorations) {
type::I32 i32;
StructMemberList members; StructMemberList members;
members.push_back( members.push_back(
create<StructMember>(Source{}, "a", &i32, StructMemberDecorationList())); create<StructMember>("a", ty.i32, StructMemberDecorationList{}));
StructDecorationList decos; StructDecorationList decos;
decos.push_back(create<StructBlockDecoration>(Source{})); decos.push_back(create<StructBlockDecoration>());
Struct s{Source{}, members, decos}; auto* s = create<Struct>(members, decos);
EXPECT_EQ(s.members().size(), 1u); EXPECT_EQ(s->members().size(), 1u);
ASSERT_EQ(s.decorations().size(), 1u); ASSERT_EQ(s->decorations().size(), 1u);
EXPECT_TRUE(s.decorations()[0]->Is<StructBlockDecoration>()); EXPECT_TRUE(s->decorations()[0]->Is<StructBlockDecoration>());
EXPECT_EQ(s.source().range.begin.line, 0u); EXPECT_EQ(s->source().range.begin.line, 0u);
EXPECT_EQ(s.source().range.begin.column, 0u); EXPECT_EQ(s->source().range.begin.column, 0u);
EXPECT_EQ(s.source().range.end.line, 0u); EXPECT_EQ(s->source().range.end.line, 0u);
EXPECT_EQ(s.source().range.end.column, 0u); EXPECT_EQ(s->source().range.end.column, 0u);
} }
TEST_F(StructTest, CreationWithSourceAndDecorations) { TEST_F(StructTest, CreationWithSourceAndDecorations) {
type::I32 i32;
StructMemberList members; StructMemberList members;
members.emplace_back( members.emplace_back(
create<StructMember>(Source{}, "a", &i32, StructMemberDecorationList())); create<StructMember>("a", ty.i32, StructMemberDecorationList{}));
StructDecorationList decos; StructDecorationList decos;
decos.push_back(create<StructBlockDecoration>(Source{})); decos.push_back(create<StructBlockDecoration>());
Struct s{ auto* s = create<Struct>(
Source{Source::Range{Source::Location{27, 4}, Source::Location{27, 8}}}, Source{Source::Range{Source::Location{27, 4}, Source::Location{27, 8}}},
members, decos}; members, decos);
EXPECT_EQ(s.members().size(), 1u); EXPECT_EQ(s->members().size(), 1u);
ASSERT_EQ(s.decorations().size(), 1u); ASSERT_EQ(s->decorations().size(), 1u);
EXPECT_TRUE(s.decorations()[0]->Is<StructBlockDecoration>()); EXPECT_TRUE(s->decorations()[0]->Is<StructBlockDecoration>());
EXPECT_EQ(s.source().range.begin.line, 27u); EXPECT_EQ(s->source().range.begin.line, 27u);
EXPECT_EQ(s.source().range.begin.column, 4u); EXPECT_EQ(s->source().range.begin.column, 4u);
EXPECT_EQ(s.source().range.end.line, 27u); EXPECT_EQ(s->source().range.end.line, 27u);
EXPECT_EQ(s.source().range.end.column, 8u); EXPECT_EQ(s->source().range.end.column, 8u);
} }
TEST_F(StructTest, IsValid) { TEST_F(StructTest, IsValid) {
Struct s(Source{}, StructMemberList{}, StructDecorationList{}); auto* s = create<Struct>(StructMemberList{}, StructDecorationList{});
EXPECT_TRUE(s.IsValid()); EXPECT_TRUE(s->IsValid());
} }
TEST_F(StructTest, IsValid_Null_StructMember) { TEST_F(StructTest, IsValid_Null_StructMember) {
type::I32 i32;
StructMemberList members; StructMemberList members;
members.push_back( members.push_back(
create<StructMember>(Source{}, "a", &i32, StructMemberDecorationList())); create<StructMember>("a", ty.i32, StructMemberDecorationList{}));
members.push_back(nullptr); members.push_back(nullptr);
Struct s{Source{}, members, ast::StructDecorationList{}}; auto* s = create<Struct>(members, ast::StructDecorationList{});
EXPECT_FALSE(s.IsValid()); EXPECT_FALSE(s->IsValid());
} } // namespace ast
TEST_F(StructTest, IsValid_Invalid_StructMember) { TEST_F(StructTest, IsValid_Invalid_StructMember) {
type::I32 i32;
StructMemberList members; StructMemberList members;
members.push_back( members.push_back(
create<StructMember>(Source{}, "", &i32, StructMemberDecorationList())); create<StructMember>("", ty.i32, StructMemberDecorationList{}));
Struct s{Source{}, members, ast::StructDecorationList{}}; auto* s = create<Struct>(members, ast::StructDecorationList{});
EXPECT_FALSE(s.IsValid()); EXPECT_FALSE(s->IsValid());
} } // namespace tint
TEST_F(StructTest, ToStr) { TEST_F(StructTest, ToStr) {
type::I32 i32;
StructMemberList members; StructMemberList members;
members.emplace_back( members.emplace_back(
create<StructMember>(Source{}, "a", &i32, StructMemberDecorationList())); create<StructMember>("a", ty.i32, StructMemberDecorationList{}));
StructDecorationList decos; StructDecorationList decos;
decos.push_back(create<StructBlockDecoration>(Source{})); decos.push_back(create<StructBlockDecoration>());
Struct s{Source{}, members, decos}; auto* s = create<Struct>(members, decos);
std::ostringstream out; std::ostringstream out;
s.to_str(out, 2); s->to_str(out, 2);
EXPECT_EQ(out.str(), R"(Struct{ EXPECT_EQ(out.str(), R"(Struct{
[[block]] [[block]]
StructMember{a: __i32} StructMember{a: __i32}

View File

@ -29,136 +29,115 @@ namespace {
using SwitchStatementTest = TestHelper; using SwitchStatementTest = TestHelper;
TEST_F(SwitchStatementTest, Creation) { TEST_F(SwitchStatementTest, Creation) {
type::I32 i32;
CaseSelectorList lit; CaseSelectorList lit;
lit.push_back(create<SintLiteral>(Source{}, &i32, 1)); lit.push_back(create<SintLiteral>(ty.i32, 1));
auto* ident = create<IdentifierExpression>( auto* ident = Expr("ident");
Source{}, mod.RegisterSymbol("ident"), "ident");
CaseStatementList body; CaseStatementList body;
auto* case_stmt = create<CaseStatement>( auto* case_stmt =
Source{}, lit, create<BlockStatement>(Source{}, StatementList{})); create<CaseStatement>(lit, create<BlockStatement>(StatementList{}));
body.push_back(case_stmt); body.push_back(case_stmt);
SwitchStatement stmt(Source{}, ident, body); auto* stmt = create<SwitchStatement>(ident, body);
EXPECT_EQ(stmt.condition(), ident); EXPECT_EQ(stmt->condition(), ident);
ASSERT_EQ(stmt.body().size(), 1u); ASSERT_EQ(stmt->body().size(), 1u);
EXPECT_EQ(stmt.body()[0], case_stmt); EXPECT_EQ(stmt->body()[0], case_stmt);
} }
TEST_F(SwitchStatementTest, Creation_WithSource) { TEST_F(SwitchStatementTest, Creation_WithSource) {
auto* ident = create<IdentifierExpression>( auto* ident = Expr("ident");
Source{}, mod.RegisterSymbol("ident"), "ident");
SwitchStatement stmt(Source{Source::Location{20, 2}}, ident, auto* stmt = create<SwitchStatement>(Source{Source::Location{20, 2}}, ident,
CaseStatementList()); CaseStatementList());
auto src = stmt.source(); auto src = stmt->source();
EXPECT_EQ(src.range.begin.line, 20u); EXPECT_EQ(src.range.begin.line, 20u);
EXPECT_EQ(src.range.begin.column, 2u); EXPECT_EQ(src.range.begin.column, 2u);
} }
TEST_F(SwitchStatementTest, IsSwitch) { TEST_F(SwitchStatementTest, IsSwitch) {
type::I32 i32;
CaseSelectorList lit; CaseSelectorList lit;
lit.push_back(create<SintLiteral>(Source{}, &i32, 2)); lit.push_back(create<SintLiteral>(ty.i32, 2));
auto* ident = create<IdentifierExpression>( auto* ident = Expr("ident");
Source{}, mod.RegisterSymbol("ident"), "ident");
CaseStatementList body; CaseStatementList body;
body.push_back(create<CaseStatement>( body.push_back(
Source{}, lit, create<BlockStatement>(Source{}, StatementList{}))); create<CaseStatement>(lit, create<BlockStatement>(StatementList{})));
SwitchStatement stmt(Source{}, ident, body); auto* stmt = create<SwitchStatement>(ident, body);
EXPECT_TRUE(stmt.Is<SwitchStatement>()); EXPECT_TRUE(stmt->Is<SwitchStatement>());
} }
TEST_F(SwitchStatementTest, IsValid) { TEST_F(SwitchStatementTest, IsValid) {
type::I32 i32;
CaseSelectorList lit; CaseSelectorList lit;
lit.push_back(create<SintLiteral>(Source{}, &i32, 2)); lit.push_back(create<SintLiteral>(ty.i32, 2));
auto* ident = create<IdentifierExpression>( auto* ident = Expr("ident");
Source{}, mod.RegisterSymbol("ident"), "ident");
CaseStatementList body; CaseStatementList body;
body.push_back(create<CaseStatement>( body.push_back(
Source{}, lit, create<BlockStatement>(Source{}, StatementList{}))); create<CaseStatement>(lit, create<BlockStatement>(StatementList{})));
SwitchStatement stmt(Source{}, ident, body); auto* stmt = create<SwitchStatement>(ident, body);
EXPECT_TRUE(stmt.IsValid()); EXPECT_TRUE(stmt->IsValid());
} }
TEST_F(SwitchStatementTest, IsValid_Null_Condition) { TEST_F(SwitchStatementTest, IsValid_Null_Condition) {
type::I32 i32;
CaseSelectorList lit; CaseSelectorList lit;
lit.push_back(create<SintLiteral>(Source{}, &i32, 2)); lit.push_back(create<SintLiteral>(ty.i32, 2));
CaseStatementList body; CaseStatementList body;
body.push_back(create<CaseStatement>( body.push_back(
Source{}, lit, create<BlockStatement>(Source{}, StatementList{}))); create<CaseStatement>(lit, create<BlockStatement>(StatementList{})));
SwitchStatement stmt(Source{}, nullptr, body); auto* stmt = create<SwitchStatement>(nullptr, body);
EXPECT_FALSE(stmt.IsValid()); EXPECT_FALSE(stmt->IsValid());
} }
TEST_F(SwitchStatementTest, IsValid_Invalid_Condition) { TEST_F(SwitchStatementTest, IsValid_Invalid_Condition) {
type::I32 i32;
CaseSelectorList lit; CaseSelectorList lit;
lit.push_back(create<SintLiteral>(Source{}, &i32, 2)); lit.push_back(create<SintLiteral>(ty.i32, 2));
auto* ident = auto* ident = Expr("");
create<IdentifierExpression>(Source{}, mod.RegisterSymbol(""), "");
CaseStatementList body; CaseStatementList body;
body.push_back(create<CaseStatement>( body.push_back(
Source{}, lit, create<BlockStatement>(Source{}, StatementList{}))); create<CaseStatement>(lit, create<BlockStatement>(StatementList{})));
SwitchStatement stmt(Source{}, ident, body); auto* stmt = create<SwitchStatement>(ident, body);
EXPECT_FALSE(stmt.IsValid()); EXPECT_FALSE(stmt->IsValid());
} }
TEST_F(SwitchStatementTest, IsValid_Null_BodyStatement) { TEST_F(SwitchStatementTest, IsValid_Null_BodyStatement) {
type::I32 i32;
CaseSelectorList lit; CaseSelectorList lit;
lit.push_back(create<SintLiteral>(Source{}, &i32, 2)); lit.push_back(create<SintLiteral>(ty.i32, 2));
auto* ident = create<IdentifierExpression>( auto* ident = Expr("ident");
Source{}, mod.RegisterSymbol("ident"), "ident");
CaseStatementList body; CaseStatementList body;
body.push_back(create<CaseStatement>( body.push_back(
Source{}, lit, create<BlockStatement>(Source{}, StatementList{}))); create<CaseStatement>(lit, create<BlockStatement>(StatementList{})));
body.push_back(nullptr); body.push_back(nullptr);
SwitchStatement stmt(Source{}, ident, body); auto* stmt = create<SwitchStatement>(ident, body);
EXPECT_FALSE(stmt.IsValid()); EXPECT_FALSE(stmt->IsValid());
} }
TEST_F(SwitchStatementTest, IsValid_Invalid_BodyStatement) { TEST_F(SwitchStatementTest, IsValid_Invalid_BodyStatement) {
auto* ident = create<IdentifierExpression>( auto* ident = Expr("ident");
Source{}, mod.RegisterSymbol("ident"), "ident");
auto* case_body = create<BlockStatement>(Source{}, StatementList{ auto* case_body = create<BlockStatement>(StatementList{
nullptr, nullptr,
}); });
CaseStatementList body; CaseStatementList body;
body.push_back( body.push_back(create<CaseStatement>(CaseSelectorList{}, case_body));
create<CaseStatement>(Source{}, CaseSelectorList{}, case_body));
SwitchStatement stmt(Source{}, ident, body); auto* stmt = create<SwitchStatement>(ident, body);
EXPECT_FALSE(stmt.IsValid()); EXPECT_FALSE(stmt->IsValid());
} }
TEST_F(SwitchStatementTest, ToStr_Empty) { TEST_F(SwitchStatementTest, ToStr_Empty) {
auto* ident = create<IdentifierExpression>( auto* ident = Expr("ident");
Source{}, mod.RegisterSymbol("ident"), "ident");
SwitchStatement stmt(Source{}, ident, {}); auto* stmt = create<SwitchStatement>(ident, CaseStatementList{});
std::ostringstream out; std::ostringstream out;
stmt.to_str(out, 2); stmt->to_str(out, 2);
EXPECT_EQ(demangle(out.str()), R"( Switch{ EXPECT_EQ(demangle(out.str()), R"( Switch{
Identifier[not set]{ident} Identifier[not set]{ident}
{ {
@ -168,20 +147,17 @@ TEST_F(SwitchStatementTest, ToStr_Empty) {
} }
TEST_F(SwitchStatementTest, ToStr) { TEST_F(SwitchStatementTest, ToStr) {
type::I32 i32;
CaseSelectorList lit; CaseSelectorList lit;
lit.push_back(create<SintLiteral>(Source{}, &i32, 2)); lit.push_back(create<SintLiteral>(ty.i32, 2));
auto* ident = create<IdentifierExpression>( auto* ident = Expr("ident");
Source{}, mod.RegisterSymbol("ident"), "ident");
CaseStatementList body; CaseStatementList body;
body.push_back(create<CaseStatement>( body.push_back(
Source{}, lit, create<BlockStatement>(Source{}, StatementList{}))); create<CaseStatement>(lit, create<BlockStatement>(StatementList{})));
SwitchStatement stmt(Source{}, ident, body); auto* stmt = create<SwitchStatement>(ident, body);
std::ostringstream out; std::ostringstream out;
stmt.to_str(out, 2); stmt->to_str(out, 2);
EXPECT_EQ(demangle(out.str()), R"( Switch{ EXPECT_EQ(demangle(out.str()), R"( Switch{
Identifier[not set]{ident} Identifier[not set]{ident}
{ {

View File

@ -20,6 +20,7 @@
#include <utility> #include <utility>
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "src/ast/builder.h"
#include "src/ast/module.h" #include "src/ast/module.h"
#include "src/demangler.h" #include "src/demangler.h"
@ -28,29 +29,15 @@ namespace ast {
/// Helper class for testing /// Helper class for testing
template <typename BASE> template <typename BASE>
class TestHelperBase : public BASE { class TestHelperBase : public BASE, public BuilderWithModule {
public: public:
TestHelperBase() {}
~TestHelperBase() = default;
/// Creates a new `Node` owned by the Module. When the Module is
/// destructed, the `Node` will also be destructed.
/// @param args the arguments to pass to the type constructor
/// @returns the node pointer
template <typename T, typename... ARGS>
T* create(ARGS&&... args) {
return mod.create<T>(std::forward<ARGS>(args)...);
}
/// Demangles the given string /// Demangles the given string
/// @param s the string to demangle /// @param s the string to demangle
/// @returns the demangled string /// @returns the demangled string
std::string demangle(const std::string& s) { std::string demangle(const std::string& s) {
return demanger.Demangle(mod, s); return demanger.Demangle(*mod, s);
} }
/// The module
Module mod;
/// A demangler /// A demangler
Demangler demanger; Demangler demanger;
}; };

View File

@ -106,16 +106,14 @@ TEST_F(AccessControlTest, MinBufferBindingSizeU32) {
TEST_F(AccessControlTest, MinBufferBindingSizeArray) { TEST_F(AccessControlTest, MinBufferBindingSizeArray) {
U32 u32; U32 u32;
Array array(&u32, 4, Array array(&u32, 4, ArrayDecorationList{create<StrideDecoration>(4)});
ArrayDecorationList{create<StrideDecoration>(Source{}, 4)});
AccessControl at{ast::AccessControl::kReadOnly, &array}; AccessControl at{ast::AccessControl::kReadOnly, &array};
EXPECT_EQ(16u, at.MinBufferBindingSize(MemoryLayout::kUniformBuffer)); EXPECT_EQ(16u, at.MinBufferBindingSize(MemoryLayout::kUniformBuffer));
} }
TEST_F(AccessControlTest, MinBufferBindingSizeRuntimeArray) { TEST_F(AccessControlTest, MinBufferBindingSizeRuntimeArray) {
U32 u32; U32 u32;
Array array(&u32, 0, Array array(&u32, 0, ArrayDecorationList{create<StrideDecoration>(4)});
ArrayDecorationList{create<StrideDecoration>(Source{}, 4)});
AccessControl at{ast::AccessControl::kReadOnly, &array}; AccessControl at{ast::AccessControl::kReadOnly, &array};
EXPECT_EQ(4u, at.MinBufferBindingSize(MemoryLayout::kUniformBuffer)); EXPECT_EQ(4u, at.MinBufferBindingSize(MemoryLayout::kUniformBuffer));
} }
@ -125,17 +123,17 @@ TEST_F(AccessControlTest, MinBufferBindingSizeStruct) {
StructMemberList members; StructMemberList members;
StructMemberDecorationList deco; StructMemberDecorationList deco;
deco.push_back(create<StructMemberOffsetDecoration>(Source{}, 0)); deco.push_back(create<StructMemberOffsetDecoration>(0));
members.push_back(create<StructMember>(Source{}, "foo", &u32, deco)); members.push_back(create<StructMember>("foo", &u32, deco));
deco = StructMemberDecorationList(); deco = StructMemberDecorationList();
deco.push_back(create<StructMemberOffsetDecoration>(Source{}, 4)); deco.push_back(create<StructMemberOffsetDecoration>(4));
members.push_back(create<StructMember>(Source{}, "bar", &u32, deco)); members.push_back(create<StructMember>("bar", &u32, deco));
StructDecorationList decos; StructDecorationList decos;
auto* str = create<ast::Struct>(Source{}, members, decos); auto* str = create<ast::Struct>(members, decos);
Struct struct_type(mod.RegisterSymbol("struct_type"), "struct_type", str); Struct struct_type(mod->RegisterSymbol("struct_type"), "struct_type", str);
AccessControl at{ast::AccessControl::kReadOnly, &struct_type}; AccessControl at{ast::AccessControl::kReadOnly, &struct_type};
EXPECT_EQ(16u, at.MinBufferBindingSize(MemoryLayout::kUniformBuffer)); EXPECT_EQ(16u, at.MinBufferBindingSize(MemoryLayout::kUniformBuffer));
EXPECT_EQ(8u, at.MinBufferBindingSize(MemoryLayout::kStorageBuffer)); EXPECT_EQ(8u, at.MinBufferBindingSize(MemoryLayout::kStorageBuffer));
@ -149,16 +147,14 @@ TEST_F(AccessControlTest, BaseAlignmentU32) {
TEST_F(AccessControlTest, BaseAlignmentArray) { TEST_F(AccessControlTest, BaseAlignmentArray) {
U32 u32; U32 u32;
Array array(&u32, 4, Array array(&u32, 4, ArrayDecorationList{create<StrideDecoration>(4)});
ArrayDecorationList{create<StrideDecoration>(Source{}, 4)});
AccessControl at{ast::AccessControl::kReadOnly, &array}; AccessControl at{ast::AccessControl::kReadOnly, &array};
EXPECT_EQ(16u, at.BaseAlignment(MemoryLayout::kUniformBuffer)); EXPECT_EQ(16u, at.BaseAlignment(MemoryLayout::kUniformBuffer));
} }
TEST_F(AccessControlTest, BaseAlignmentRuntimeArray) { TEST_F(AccessControlTest, BaseAlignmentRuntimeArray) {
U32 u32; U32 u32;
Array array(&u32, 0, Array array(&u32, 0, ArrayDecorationList{create<StrideDecoration>(4)});
ArrayDecorationList{create<StrideDecoration>(Source{}, 4)});
AccessControl at{ast::AccessControl::kReadOnly, &array}; AccessControl at{ast::AccessControl::kReadOnly, &array};
EXPECT_EQ(16u, at.BaseAlignment(MemoryLayout::kUniformBuffer)); EXPECT_EQ(16u, at.BaseAlignment(MemoryLayout::kUniformBuffer));
} }
@ -169,18 +165,18 @@ TEST_F(AccessControlTest, BaseAlignmentStruct) {
{ {
StructMemberDecorationList deco; StructMemberDecorationList deco;
deco.push_back(create<StructMemberOffsetDecoration>(Source{}, 0)); deco.push_back(create<StructMemberOffsetDecoration>(0));
members.push_back(create<StructMember>(Source{}, "foo", &u32, deco)); members.push_back(create<StructMember>("foo", &u32, deco));
} }
{ {
StructMemberDecorationList deco; StructMemberDecorationList deco;
deco.push_back(create<StructMemberOffsetDecoration>(Source{}, 4)); deco.push_back(create<StructMemberOffsetDecoration>(4));
members.push_back(create<StructMember>(Source{}, "bar", &u32, deco)); members.push_back(create<StructMember>("bar", &u32, deco));
} }
StructDecorationList decos; StructDecorationList decos;
auto* str = create<ast::Struct>(Source{}, members, decos); auto* str = create<ast::Struct>(members, decos);
Struct struct_type(mod.RegisterSymbol("struct_type"), "struct_type", str); Struct struct_type(mod->RegisterSymbol("struct_type"), "struct_type", str);
AccessControl at{ast::AccessControl::kReadOnly, &struct_type}; AccessControl at{ast::AccessControl::kReadOnly, &struct_type};
EXPECT_EQ(16u, at.BaseAlignment(MemoryLayout::kUniformBuffer)); EXPECT_EQ(16u, at.BaseAlignment(MemoryLayout::kUniformBuffer));
EXPECT_EQ(4u, at.BaseAlignment(MemoryLayout::kStorageBuffer)); EXPECT_EQ(4u, at.BaseAlignment(MemoryLayout::kStorageBuffer));

View File

@ -44,7 +44,7 @@ using AliasTest = TestHelper;
TEST_F(AliasTest, Create) { TEST_F(AliasTest, Create) {
U32 u32; U32 u32;
Alias a{mod.RegisterSymbol("a_type"), "a_type", &u32}; Alias a{mod->RegisterSymbol("a_type"), "a_type", &u32};
EXPECT_EQ(a.symbol(), Symbol(1)); EXPECT_EQ(a.symbol(), Symbol(1));
// EXPECT_EQ(a.name(), "a_type"); // EXPECT_EQ(a.name(), "a_type");
EXPECT_EQ(a.type(), &u32); EXPECT_EQ(a.type(), &u32);
@ -53,7 +53,7 @@ TEST_F(AliasTest, Create) {
TEST_F(AliasTest, Is) { TEST_F(AliasTest, Is) {
I32 i32; I32 i32;
Alias at{mod.RegisterSymbol("a"), "a", &i32}; Alias at{mod->RegisterSymbol("a"), "a", &i32};
Type* ty = &at; Type* ty = &at;
EXPECT_FALSE(ty->Is<AccessControl>()); EXPECT_FALSE(ty->Is<AccessControl>());
EXPECT_TRUE(ty->Is<Alias>()); EXPECT_TRUE(ty->Is<Alias>());
@ -72,13 +72,13 @@ TEST_F(AliasTest, Is) {
TEST_F(AliasTest, TypeName) { TEST_F(AliasTest, TypeName) {
I32 i32; I32 i32;
Alias at{mod.RegisterSymbol("Particle"), "Particle", &i32}; Alias at{mod->RegisterSymbol("Particle"), "Particle", &i32};
EXPECT_EQ(at.type_name(), "__alias_tint_symbol_1__i32"); EXPECT_EQ(at.type_name(), "__alias_tint_symbol_1__i32");
} }
TEST_F(AliasTest, UnwrapIfNeeded_Alias) { TEST_F(AliasTest, UnwrapIfNeeded_Alias) {
U32 u32; U32 u32;
Alias a{mod.RegisterSymbol("a_type"), "a_type", &u32}; Alias a{mod->RegisterSymbol("a_type"), "a_type", &u32};
EXPECT_EQ(a.symbol(), Symbol(1)); EXPECT_EQ(a.symbol(), Symbol(1));
// EXPECT_EQ(a.name(), "a_type"); // EXPECT_EQ(a.name(), "a_type");
EXPECT_EQ(a.type(), &u32); EXPECT_EQ(a.type(), &u32);
@ -95,8 +95,8 @@ TEST_F(AliasTest, UnwrapIfNeeded_AccessControl) {
TEST_F(AliasTest, UnwrapIfNeeded_MultiLevel) { TEST_F(AliasTest, UnwrapIfNeeded_MultiLevel) {
U32 u32; U32 u32;
Alias a{mod.RegisterSymbol("a_type"), "a_type", &u32}; Alias a{mod->RegisterSymbol("a_type"), "a_type", &u32};
Alias aa{mod.RegisterSymbol("aa_type"), "aa_type", &a}; Alias aa{mod->RegisterSymbol("aa_type"), "aa_type", &a};
EXPECT_EQ(aa.symbol(), Symbol(2)); EXPECT_EQ(aa.symbol(), Symbol(2));
// EXPECT_EQ(aa.name(), "aa_type"); // EXPECT_EQ(aa.name(), "aa_type");
EXPECT_EQ(aa.type(), &a); EXPECT_EQ(aa.type(), &a);
@ -105,7 +105,7 @@ TEST_F(AliasTest, UnwrapIfNeeded_MultiLevel) {
TEST_F(AliasTest, UnwrapIfNeeded_MultiLevel_AliasAccessControl) { TEST_F(AliasTest, UnwrapIfNeeded_MultiLevel_AliasAccessControl) {
U32 u32; U32 u32;
Alias a{mod.RegisterSymbol("a_type"), "a_type", &u32}; Alias a{mod->RegisterSymbol("a_type"), "a_type", &u32};
AccessControl aa{ast::AccessControl::kReadWrite, &a}; AccessControl aa{ast::AccessControl::kReadWrite, &a};
EXPECT_EQ(aa.type(), &a); EXPECT_EQ(aa.type(), &a);
EXPECT_EQ(aa.UnwrapIfNeeded(), &u32); EXPECT_EQ(aa.UnwrapIfNeeded(), &u32);
@ -113,11 +113,11 @@ TEST_F(AliasTest, UnwrapIfNeeded_MultiLevel_AliasAccessControl) {
TEST_F(AliasTest, UnwrapAll_TwiceAliasPointerTwiceAlias) { TEST_F(AliasTest, UnwrapAll_TwiceAliasPointerTwiceAlias) {
U32 u32; U32 u32;
Alias a{mod.RegisterSymbol("a_type"), "a_type", &u32}; Alias a{mod->RegisterSymbol("a_type"), "a_type", &u32};
Alias aa{mod.RegisterSymbol("aa_type"), "aa_type", &a}; Alias aa{mod->RegisterSymbol("aa_type"), "aa_type", &a};
Pointer paa{&aa, StorageClass::kUniform}; Pointer paa{&aa, StorageClass::kUniform};
Alias apaa{mod.RegisterSymbol("paa_type"), "paa_type", &paa}; Alias apaa{mod->RegisterSymbol("paa_type"), "paa_type", &paa};
Alias aapaa{mod.RegisterSymbol("aapaa_type"), "aapaa_type", &apaa}; Alias aapaa{mod->RegisterSymbol("aapaa_type"), "aapaa_type", &apaa};
EXPECT_EQ(aapaa.symbol(), Symbol(4)); EXPECT_EQ(aapaa.symbol(), Symbol(4));
// EXPECT_EQ(aapaa.name(), "aapaa_type"); // EXPECT_EQ(aapaa.name(), "aapaa_type");
EXPECT_EQ(aapaa.type(), &apaa); EXPECT_EQ(aapaa.type(), &apaa);
@ -127,23 +127,23 @@ TEST_F(AliasTest, UnwrapAll_TwiceAliasPointerTwiceAlias) {
TEST_F(AliasTest, UnwrapAll_SecondConsecutivePointerBlocksUnrapping) { TEST_F(AliasTest, UnwrapAll_SecondConsecutivePointerBlocksUnrapping) {
U32 u32; U32 u32;
Alias a{mod.RegisterSymbol("a_type"), "a_type", &u32}; Alias a{mod->RegisterSymbol("a_type"), "a_type", &u32};
Alias aa{mod.RegisterSymbol("aa_type"), "aa_type", &a}; Alias aa{mod->RegisterSymbol("aa_type"), "aa_type", &a};
Pointer paa{&aa, StorageClass::kUniform}; Pointer paa{&aa, StorageClass::kUniform};
Pointer ppaa{&paa, StorageClass::kUniform}; Pointer ppaa{&paa, StorageClass::kUniform};
Alias appaa{mod.RegisterSymbol("appaa_type"), "appaa_type", &ppaa}; Alias appaa{mod->RegisterSymbol("appaa_type"), "appaa_type", &ppaa};
EXPECT_EQ(appaa.UnwrapAll(), &paa); EXPECT_EQ(appaa.UnwrapAll(), &paa);
} }
TEST_F(AliasTest, UnwrapAll_SecondNonConsecutivePointerBlocksUnrapping) { TEST_F(AliasTest, UnwrapAll_SecondNonConsecutivePointerBlocksUnrapping) {
U32 u32; U32 u32;
Alias a{mod.RegisterSymbol("a_type"), "a_type", &u32}; Alias a{mod->RegisterSymbol("a_type"), "a_type", &u32};
Alias aa{mod.RegisterSymbol("aa_type"), "aa_type", &a}; Alias aa{mod->RegisterSymbol("aa_type"), "aa_type", &a};
Pointer paa{&aa, StorageClass::kUniform}; Pointer paa{&aa, StorageClass::kUniform};
Alias apaa{mod.RegisterSymbol("apaa_type"), "apaa_type", &paa}; Alias apaa{mod->RegisterSymbol("apaa_type"), "apaa_type", &paa};
Alias aapaa{mod.RegisterSymbol("aapaa_type"), "aapaa_type", &apaa}; Alias aapaa{mod->RegisterSymbol("aapaa_type"), "aapaa_type", &apaa};
Pointer paapaa{&aapaa, StorageClass::kUniform}; Pointer paapaa{&aapaa, StorageClass::kUniform};
Alias apaapaa{mod.RegisterSymbol("apaapaa_type"), "apaapaa_type", &paapaa}; Alias apaapaa{mod->RegisterSymbol("apaapaa_type"), "apaapaa_type", &paapaa};
EXPECT_EQ(apaapaa.UnwrapAll(), &paa); EXPECT_EQ(apaapaa.UnwrapAll(), &paa);
} }
@ -167,7 +167,7 @@ TEST_F(AliasTest, UnwrapAll_PointerAccessControl) {
TEST_F(AliasTest, MinBufferBindingSizeU32) { TEST_F(AliasTest, MinBufferBindingSizeU32) {
U32 u32; U32 u32;
Alias alias{mod.RegisterSymbol("alias"), "alias", &u32}; Alias alias{mod->RegisterSymbol("alias"), "alias", &u32};
EXPECT_EQ(4u, alias.MinBufferBindingSize(MemoryLayout::kUniformBuffer)); EXPECT_EQ(4u, alias.MinBufferBindingSize(MemoryLayout::kUniformBuffer));
} }
@ -175,9 +175,9 @@ TEST_F(AliasTest, MinBufferBindingSizeArray) {
U32 u32; U32 u32;
Array array(&u32, 4, Array array(&u32, 4,
ArrayDecorationList{ ArrayDecorationList{
create<StrideDecoration>(Source{}, 4), create<StrideDecoration>(4),
}); });
Alias alias{mod.RegisterSymbol("alias"), "alias", &array}; Alias alias{mod->RegisterSymbol("alias"), "alias", &array};
EXPECT_EQ(16u, alias.MinBufferBindingSize(MemoryLayout::kUniformBuffer)); EXPECT_EQ(16u, alias.MinBufferBindingSize(MemoryLayout::kUniformBuffer));
} }
@ -185,9 +185,9 @@ TEST_F(AliasTest, MinBufferBindingSizeRuntimeArray) {
U32 u32; U32 u32;
Array array(&u32, 0, Array array(&u32, 0,
ArrayDecorationList{ ArrayDecorationList{
create<StrideDecoration>(Source{}, 4), create<StrideDecoration>(4),
}); });
Alias alias{mod.RegisterSymbol("alias"), "alias", &array}; Alias alias{mod->RegisterSymbol("alias"), "alias", &array};
EXPECT_EQ(4u, alias.MinBufferBindingSize(MemoryLayout::kUniformBuffer)); EXPECT_EQ(4u, alias.MinBufferBindingSize(MemoryLayout::kUniformBuffer));
} }
@ -197,26 +197,26 @@ TEST_F(AliasTest, MinBufferBindingSizeStruct) {
{ {
StructMemberDecorationList deco; StructMemberDecorationList deco;
deco.push_back(create<StructMemberOffsetDecoration>(Source{}, 0)); deco.push_back(create<StructMemberOffsetDecoration>(0));
members.push_back(create<StructMember>(Source{}, "foo", &u32, deco)); members.push_back(create<StructMember>("foo", &u32, deco));
} }
{ {
StructMemberDecorationList deco; StructMemberDecorationList deco;
deco.push_back(create<StructMemberOffsetDecoration>(Source{}, 4)); deco.push_back(create<StructMemberOffsetDecoration>(4));
members.push_back(create<StructMember>(Source{}, "bar", &u32, deco)); members.push_back(create<StructMember>("bar", &u32, deco));
} }
StructDecorationList decos; StructDecorationList decos;
auto* str = create<ast::Struct>(Source{}, members, decos); auto* str = create<ast::Struct>(members, decos);
Struct struct_type(mod.RegisterSymbol("struct_type"), "struct_type", str); Struct struct_type(mod->RegisterSymbol("struct_type"), "struct_type", str);
Alias alias{mod.RegisterSymbol("alias"), "alias", &struct_type}; Alias alias{mod->RegisterSymbol("alias"), "alias", &struct_type};
EXPECT_EQ(16u, alias.MinBufferBindingSize(MemoryLayout::kUniformBuffer)); EXPECT_EQ(16u, alias.MinBufferBindingSize(MemoryLayout::kUniformBuffer));
EXPECT_EQ(8u, alias.MinBufferBindingSize(MemoryLayout::kStorageBuffer)); EXPECT_EQ(8u, alias.MinBufferBindingSize(MemoryLayout::kStorageBuffer));
} }
TEST_F(AliasTest, BaseAlignmentU32) { TEST_F(AliasTest, BaseAlignmentU32) {
U32 u32; U32 u32;
Alias alias{mod.RegisterSymbol("alias"), "alias", &u32}; Alias alias{mod->RegisterSymbol("alias"), "alias", &u32};
EXPECT_EQ(4u, alias.BaseAlignment(MemoryLayout::kUniformBuffer)); EXPECT_EQ(4u, alias.BaseAlignment(MemoryLayout::kUniformBuffer));
} }
@ -224,9 +224,9 @@ TEST_F(AliasTest, BaseAlignmentArray) {
U32 u32; U32 u32;
Array array(&u32, 4, Array array(&u32, 4,
ArrayDecorationList{ ArrayDecorationList{
create<StrideDecoration>(Source{}, 4), create<StrideDecoration>(4),
}); });
Alias alias{mod.RegisterSymbol("alias"), "alias", &array}; Alias alias{mod->RegisterSymbol("alias"), "alias", &array};
EXPECT_EQ(16u, alias.BaseAlignment(MemoryLayout::kUniformBuffer)); EXPECT_EQ(16u, alias.BaseAlignment(MemoryLayout::kUniformBuffer));
} }
@ -234,9 +234,9 @@ TEST_F(AliasTest, BaseAlignmentRuntimeArray) {
U32 u32; U32 u32;
Array array(&u32, 0, Array array(&u32, 0,
ArrayDecorationList{ ArrayDecorationList{
create<StrideDecoration>(Source{}, 4), create<StrideDecoration>(4),
}); });
Alias alias{mod.RegisterSymbol("alias"), "alias", &array}; Alias alias{mod->RegisterSymbol("alias"), "alias", &array};
EXPECT_EQ(16u, alias.BaseAlignment(MemoryLayout::kUniformBuffer)); EXPECT_EQ(16u, alias.BaseAlignment(MemoryLayout::kUniformBuffer));
} }
@ -246,19 +246,19 @@ TEST_F(AliasTest, BaseAlignmentStruct) {
{ {
StructMemberDecorationList deco; StructMemberDecorationList deco;
deco.push_back(create<StructMemberOffsetDecoration>(Source{}, 0)); deco.push_back(create<StructMemberOffsetDecoration>(0));
members.push_back(create<StructMember>(Source{}, "foo", &u32, deco)); members.push_back(create<StructMember>("foo", &u32, deco));
} }
{ {
StructMemberDecorationList deco; StructMemberDecorationList deco;
deco.push_back(create<StructMemberOffsetDecoration>(Source{}, 4)); deco.push_back(create<StructMemberOffsetDecoration>(4));
members.push_back(create<StructMember>(Source{}, "bar", &u32, deco)); members.push_back(create<StructMember>("bar", &u32, deco));
} }
StructDecorationList decos; StructDecorationList decos;
auto* str = create<ast::Struct>(Source{}, members, decos); auto* str = create<ast::Struct>(members, decos);
Struct struct_type(mod.RegisterSymbol("struct_type"), "struct_type", str); Struct struct_type(mod->RegisterSymbol("struct_type"), "struct_type", str);
Alias alias{mod.RegisterSymbol("alias"), "alias", &struct_type}; Alias alias{mod->RegisterSymbol("alias"), "alias", &struct_type};
EXPECT_EQ(16u, alias.BaseAlignment(MemoryLayout::kUniformBuffer)); EXPECT_EQ(16u, alias.BaseAlignment(MemoryLayout::kUniformBuffer));
EXPECT_EQ(4u, alias.BaseAlignment(MemoryLayout::kStorageBuffer)); EXPECT_EQ(4u, alias.BaseAlignment(MemoryLayout::kStorageBuffer));
} }

View File

@ -89,8 +89,7 @@ TEST_F(ArrayTest, TypeName_RuntimeArray) {
TEST_F(ArrayTest, TypeName_WithStride) { TEST_F(ArrayTest, TypeName_WithStride) {
I32 i32; I32 i32;
Array arr{&i32, 3, Array arr{&i32, 3, ArrayDecorationList{create<StrideDecoration>(16)}};
ArrayDecorationList{create<StrideDecoration>(Source{}, 16)}};
EXPECT_EQ(arr.type_name(), "__array__i32_3_stride_16"); EXPECT_EQ(arr.type_name(), "__array__i32_3_stride_16");
} }
@ -102,30 +101,26 @@ TEST_F(ArrayTest, MinBufferBindingSizeNoStride) {
TEST_F(ArrayTest, MinBufferBindingSizeArray) { TEST_F(ArrayTest, MinBufferBindingSizeArray) {
U32 u32; U32 u32;
Array arr(&u32, 4, Array arr(&u32, 4, ArrayDecorationList{create<StrideDecoration>(4)});
ArrayDecorationList{create<StrideDecoration>(Source{}, 4)});
EXPECT_EQ(16u, arr.MinBufferBindingSize(MemoryLayout::kUniformBuffer)); EXPECT_EQ(16u, arr.MinBufferBindingSize(MemoryLayout::kUniformBuffer));
} }
TEST_F(ArrayTest, MinBufferBindingSizeRuntimeArray) { TEST_F(ArrayTest, MinBufferBindingSizeRuntimeArray) {
U32 u32; U32 u32;
Array arr(&u32, 0, Array arr(&u32, 0, ArrayDecorationList{create<StrideDecoration>(4)});
ArrayDecorationList{create<StrideDecoration>(Source{}, 4)});
EXPECT_EQ(4u, arr.MinBufferBindingSize(MemoryLayout::kUniformBuffer)); EXPECT_EQ(4u, arr.MinBufferBindingSize(MemoryLayout::kUniformBuffer));
} }
TEST_F(ArrayTest, BaseAlignmentArray) { TEST_F(ArrayTest, BaseAlignmentArray) {
U32 u32; U32 u32;
Array arr(&u32, 4, Array arr(&u32, 4, ArrayDecorationList{create<StrideDecoration>(4)});
ArrayDecorationList{create<StrideDecoration>(Source{}, 4)});
EXPECT_EQ(16u, arr.BaseAlignment(MemoryLayout::kUniformBuffer)); EXPECT_EQ(16u, arr.BaseAlignment(MemoryLayout::kUniformBuffer));
EXPECT_EQ(4u, arr.BaseAlignment(MemoryLayout::kStorageBuffer)); EXPECT_EQ(4u, arr.BaseAlignment(MemoryLayout::kStorageBuffer));
} }
TEST_F(ArrayTest, BaseAlignmentRuntimeArray) { TEST_F(ArrayTest, BaseAlignmentRuntimeArray) {
U32 u32; U32 u32;
Array arr(&u32, 0, Array arr(&u32, 0, ArrayDecorationList{create<StrideDecoration>(4)});
ArrayDecorationList{create<StrideDecoration>(Source{}, 4)});
EXPECT_EQ(16u, arr.BaseAlignment(MemoryLayout::kUniformBuffer)); EXPECT_EQ(16u, arr.BaseAlignment(MemoryLayout::kUniformBuffer));
EXPECT_EQ(4u, arr.BaseAlignment(MemoryLayout::kStorageBuffer)); EXPECT_EQ(4u, arr.BaseAlignment(MemoryLayout::kStorageBuffer));
} }

View File

@ -92,11 +92,10 @@ TEST_F(StorageTextureTest, TypeName) {
} }
TEST_F(StorageTextureTest, F32) { TEST_F(StorageTextureTest, F32) {
Module mod; Type* s = mod->create<StorageTexture>(TextureDimension::k2dArray,
Type* s = mod.create<StorageTexture>(TextureDimension::k2dArray,
ast::AccessControl::kReadOnly, ast::AccessControl::kReadOnly,
ImageFormat::kRgba32Float); ImageFormat::kRgba32Float);
TypeDeterminer td(&mod); TypeDeterminer td(mod);
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td.Determine()) << td.error();
ASSERT_TRUE(s->Is<Texture>()); ASSERT_TRUE(s->Is<Texture>());
@ -105,11 +104,10 @@ TEST_F(StorageTextureTest, F32) {
} }
TEST_F(StorageTextureTest, U32) { TEST_F(StorageTextureTest, U32) {
Module mod; Type* s = mod->create<StorageTexture>(TextureDimension::k2dArray,
Type* s = mod.create<StorageTexture>(TextureDimension::k2dArray,
ast::AccessControl::kReadOnly, ast::AccessControl::kReadOnly,
ImageFormat::kRg32Uint); ImageFormat::kRg32Uint);
TypeDeterminer td(&mod); TypeDeterminer td(mod);
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td.Determine()) << td.error();
ASSERT_TRUE(s->Is<Texture>()); ASSERT_TRUE(s->Is<Texture>());
@ -118,11 +116,10 @@ TEST_F(StorageTextureTest, U32) {
} }
TEST_F(StorageTextureTest, I32) { TEST_F(StorageTextureTest, I32) {
Module mod; Type* s = mod->create<StorageTexture>(TextureDimension::k2dArray,
Type* s = mod.create<StorageTexture>(TextureDimension::k2dArray,
ast::AccessControl::kReadOnly, ast::AccessControl::kReadOnly,
ImageFormat::kRgba32Sint); ImageFormat::kRgba32Sint);
TypeDeterminer td(&mod); TypeDeterminer td(mod);
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td.Determine()) << td.error();
ASSERT_TRUE(s->Is<Texture>()); ASSERT_TRUE(s->Is<Texture>());

View File

@ -41,18 +41,16 @@ using StructTest = TestHelper;
TEST_F(StructTest, Creation) { TEST_F(StructTest, Creation) {
StructMemberList members; StructMemberList members;
auto* impl = auto* impl = create<ast::Struct>(members, ast::StructDecorationList{});
create<ast::Struct>(Source{}, members, ast::StructDecorationList{});
auto* ptr = impl; auto* ptr = impl;
Struct s{mod.RegisterSymbol("S"), "S", impl}; Struct s{mod->RegisterSymbol("S"), "S", impl};
EXPECT_EQ(s.impl(), ptr); EXPECT_EQ(s.impl(), ptr);
} }
TEST_F(StructTest, Is) { TEST_F(StructTest, Is) {
StructMemberList members; StructMemberList members;
auto* impl = auto* impl = create<ast::Struct>(members, ast::StructDecorationList{});
create<ast::Struct>(Source{}, members, ast::StructDecorationList{}); Struct s{mod->RegisterSymbol("S"), "S", impl};
Struct s{mod.RegisterSymbol("S"), "S", impl};
Type* ty = &s; Type* ty = &s;
EXPECT_FALSE(ty->Is<AccessControl>()); EXPECT_FALSE(ty->Is<AccessControl>());
EXPECT_FALSE(ty->Is<Alias>()); EXPECT_FALSE(ty->Is<Alias>());
@ -71,9 +69,8 @@ TEST_F(StructTest, Is) {
TEST_F(StructTest, TypeName) { TEST_F(StructTest, TypeName) {
StructMemberList members; StructMemberList members;
auto* impl = auto* impl = create<ast::Struct>(members, ast::StructDecorationList{});
create<ast::Struct>(Source{}, members, ast::StructDecorationList{}); Struct s{mod->RegisterSymbol("my_struct"), "my_struct", impl};
Struct s{mod.RegisterSymbol("my_struct"), "my_struct", impl};
EXPECT_EQ(s.type_name(), "__struct_tint_symbol_1"); EXPECT_EQ(s.type_name(), "__struct_tint_symbol_1");
} }
@ -83,18 +80,18 @@ TEST_F(StructTest, MinBufferBindingSize) {
{ {
StructMemberDecorationList deco; StructMemberDecorationList deco;
deco.push_back(create<StructMemberOffsetDecoration>(Source{}, 0)); deco.push_back(create<StructMemberOffsetDecoration>(0));
members.push_back(create<StructMember>(Source{}, "foo", &u32, deco)); members.push_back(create<StructMember>("foo", &u32, deco));
} }
{ {
StructMemberDecorationList deco; StructMemberDecorationList deco;
deco.push_back(create<StructMemberOffsetDecoration>(Source{}, 4)); deco.push_back(create<StructMemberOffsetDecoration>(4));
members.push_back(create<StructMember>(Source{}, "bar", &u32, deco)); members.push_back(create<StructMember>("bar", &u32, deco));
} }
StructDecorationList decos; StructDecorationList decos;
auto* str = create<ast::Struct>(Source{}, members, decos); auto* str = create<ast::Struct>(members, decos);
Struct struct_type(mod.RegisterSymbol("struct_type"), "struct_type", str); Struct struct_type(mod->RegisterSymbol("struct_type"), "struct_type", str);
EXPECT_EQ(16u, EXPECT_EQ(16u,
struct_type.MinBufferBindingSize(MemoryLayout::kUniformBuffer)); struct_type.MinBufferBindingSize(MemoryLayout::kUniformBuffer));
EXPECT_EQ(8u, struct_type.MinBufferBindingSize(MemoryLayout::kStorageBuffer)); EXPECT_EQ(8u, struct_type.MinBufferBindingSize(MemoryLayout::kStorageBuffer));
@ -102,29 +99,28 @@ TEST_F(StructTest, MinBufferBindingSize) {
TEST_F(StructTest, MinBufferBindingSizeArray) { TEST_F(StructTest, MinBufferBindingSizeArray) {
U32 u32; U32 u32;
Array arr(&u32, 4, Array arr(&u32, 4, ArrayDecorationList{create<StrideDecoration>(4)});
ArrayDecorationList{create<StrideDecoration>(Source{}, 4)});
StructMemberList members; StructMemberList members;
{ {
StructMemberDecorationList deco; StructMemberDecorationList deco;
deco.push_back(create<StructMemberOffsetDecoration>(Source{}, 0)); deco.push_back(create<StructMemberOffsetDecoration>(0));
members.push_back(create<StructMember>(Source{}, "foo", &u32, deco)); members.push_back(create<StructMember>("foo", &u32, deco));
} }
{ {
StructMemberDecorationList deco; StructMemberDecorationList deco;
deco.push_back(create<StructMemberOffsetDecoration>(Source{}, 4)); deco.push_back(create<StructMemberOffsetDecoration>(4));
members.push_back(create<StructMember>(Source{}, "bar", &u32, deco)); members.push_back(create<StructMember>("bar", &u32, deco));
} }
{ {
StructMemberDecorationList deco; StructMemberDecorationList deco;
deco.push_back(create<StructMemberOffsetDecoration>(Source{}, 8)); deco.push_back(create<StructMemberOffsetDecoration>(8));
members.push_back(create<StructMember>(Source{}, "bar", &arr, deco)); members.push_back(create<StructMember>("bar", &arr, deco));
} }
StructDecorationList decos; StructDecorationList decos;
auto* str = create<ast::Struct>(Source{}, members, decos); auto* str = create<ast::Struct>(members, decos);
Struct struct_type(mod.RegisterSymbol("struct_type"), "struct_type", str); Struct struct_type(mod->RegisterSymbol("struct_type"), "struct_type", str);
EXPECT_EQ(32u, EXPECT_EQ(32u,
struct_type.MinBufferBindingSize(MemoryLayout::kUniformBuffer)); struct_type.MinBufferBindingSize(MemoryLayout::kUniformBuffer));
EXPECT_EQ(24u, EXPECT_EQ(24u,
@ -133,29 +129,28 @@ TEST_F(StructTest, MinBufferBindingSizeArray) {
TEST_F(StructTest, MinBufferBindingSizeRuntimeArray) { TEST_F(StructTest, MinBufferBindingSizeRuntimeArray) {
U32 u32; U32 u32;
Array arr(&u32, 0, Array arr(&u32, 0, ArrayDecorationList{create<StrideDecoration>(4)});
ArrayDecorationList{create<StrideDecoration>(Source{}, 4)});
StructMemberList members; StructMemberList members;
{ {
StructMemberDecorationList deco; StructMemberDecorationList deco;
deco.push_back(create<StructMemberOffsetDecoration>(Source{}, 0)); deco.push_back(create<StructMemberOffsetDecoration>(0));
members.push_back(create<StructMember>(Source{}, "foo", &u32, deco)); members.push_back(create<StructMember>("foo", &u32, deco));
} }
{ {
StructMemberDecorationList deco; StructMemberDecorationList deco;
deco.push_back(create<StructMemberOffsetDecoration>(Source{}, 4)); deco.push_back(create<StructMemberOffsetDecoration>(4));
members.push_back(create<StructMember>(Source{}, "bar", &u32, deco)); members.push_back(create<StructMember>("bar", &u32, deco));
} }
{ {
StructMemberDecorationList deco; StructMemberDecorationList deco;
deco.push_back(create<StructMemberOffsetDecoration>(Source{}, 8)); deco.push_back(create<StructMemberOffsetDecoration>(8));
members.push_back(create<StructMember>(Source{}, "bar", &u32, deco)); members.push_back(create<StructMember>("bar", &u32, deco));
} }
StructDecorationList decos; StructDecorationList decos;
auto* str = create<ast::Struct>(Source{}, members, decos); auto* str = create<ast::Struct>(members, decos);
Struct struct_type(mod.RegisterSymbol("struct_type"), "struct_type", str); Struct struct_type(mod->RegisterSymbol("struct_type"), "struct_type", str);
EXPECT_EQ(12u, EXPECT_EQ(12u,
struct_type.MinBufferBindingSize(MemoryLayout::kStorageBuffer)); struct_type.MinBufferBindingSize(MemoryLayout::kStorageBuffer));
} }
@ -167,13 +162,13 @@ TEST_F(StructTest, MinBufferBindingSizeVec2) {
StructMemberList members; StructMemberList members;
{ {
StructMemberDecorationList deco; StructMemberDecorationList deco;
deco.push_back(create<StructMemberOffsetDecoration>(Source{}, 0)); deco.push_back(create<StructMemberOffsetDecoration>(0));
members.push_back(create<StructMember>(Source{}, "foo", &vec2, deco)); members.push_back(create<StructMember>("foo", &vec2, deco));
} }
StructDecorationList decos; StructDecorationList decos;
auto* str = create<ast::Struct>(Source{}, members, decos); auto* str = create<ast::Struct>(members, decos);
Struct struct_type(mod.RegisterSymbol("struct_type"), "struct_type", str); Struct struct_type(mod->RegisterSymbol("struct_type"), "struct_type", str);
EXPECT_EQ(16u, EXPECT_EQ(16u,
struct_type.MinBufferBindingSize(MemoryLayout::kUniformBuffer)); struct_type.MinBufferBindingSize(MemoryLayout::kUniformBuffer));
EXPECT_EQ(8u, struct_type.MinBufferBindingSize(MemoryLayout::kStorageBuffer)); EXPECT_EQ(8u, struct_type.MinBufferBindingSize(MemoryLayout::kStorageBuffer));
@ -186,13 +181,13 @@ TEST_F(StructTest, MinBufferBindingSizeVec3) {
StructMemberList members; StructMemberList members;
{ {
StructMemberDecorationList deco; StructMemberDecorationList deco;
deco.push_back(create<StructMemberOffsetDecoration>(Source{}, 0)); deco.push_back(create<StructMemberOffsetDecoration>(0));
members.push_back(create<StructMember>(Source{}, "foo", &vec3, deco)); members.push_back(create<StructMember>("foo", &vec3, deco));
} }
StructDecorationList decos; StructDecorationList decos;
auto* str = create<ast::Struct>(Source{}, members, decos); auto* str = create<ast::Struct>(members, decos);
Struct struct_type(mod.RegisterSymbol("struct_type"), "struct_type", str); Struct struct_type(mod->RegisterSymbol("struct_type"), "struct_type", str);
EXPECT_EQ(16u, EXPECT_EQ(16u,
struct_type.MinBufferBindingSize(MemoryLayout::kUniformBuffer)); struct_type.MinBufferBindingSize(MemoryLayout::kUniformBuffer));
EXPECT_EQ(16u, EXPECT_EQ(16u,
@ -206,13 +201,13 @@ TEST_F(StructTest, MinBufferBindingSizeVec4) {
StructMemberList members; StructMemberList members;
{ {
StructMemberDecorationList deco; StructMemberDecorationList deco;
deco.push_back(create<StructMemberOffsetDecoration>(Source{}, 0)); deco.push_back(create<StructMemberOffsetDecoration>(0));
members.push_back(create<StructMember>(Source{}, "foo", &vec4, deco)); members.push_back(create<StructMember>("foo", &vec4, deco));
} }
StructDecorationList decos; StructDecorationList decos;
auto* str = create<ast::Struct>(Source{}, members, decos); auto* str = create<ast::Struct>(members, decos);
Struct struct_type(mod.RegisterSymbol("struct_type"), "struct_type", str); Struct struct_type(mod->RegisterSymbol("struct_type"), "struct_type", str);
EXPECT_EQ(16u, EXPECT_EQ(16u,
struct_type.MinBufferBindingSize(MemoryLayout::kUniformBuffer)); struct_type.MinBufferBindingSize(MemoryLayout::kUniformBuffer));
EXPECT_EQ(16u, EXPECT_EQ(16u,
@ -225,76 +220,74 @@ TEST_F(StructTest, BaseAlignment) {
{ {
StructMemberDecorationList deco; StructMemberDecorationList deco;
deco.push_back(create<StructMemberOffsetDecoration>(Source{}, 0)); deco.push_back(create<StructMemberOffsetDecoration>(0));
members.push_back(create<StructMember>(Source{}, "foo", &u32, deco)); members.push_back(create<StructMember>("foo", &u32, deco));
} }
{ {
StructMemberDecorationList deco; StructMemberDecorationList deco;
deco.push_back(create<StructMemberOffsetDecoration>(Source{}, 4)); deco.push_back(create<StructMemberOffsetDecoration>(4));
members.push_back(create<StructMember>(Source{}, "bar", &u32, deco)); members.push_back(create<StructMember>("bar", &u32, deco));
} }
StructDecorationList decos; StructDecorationList decos;
auto* str = create<ast::Struct>(Source{}, members, decos); auto* str = create<ast::Struct>(members, decos);
Struct struct_type(mod.RegisterSymbol("struct_type"), "struct_type", str); Struct struct_type(mod->RegisterSymbol("struct_type"), "struct_type", str);
EXPECT_EQ(16u, struct_type.BaseAlignment(MemoryLayout::kUniformBuffer)); EXPECT_EQ(16u, struct_type.BaseAlignment(MemoryLayout::kUniformBuffer));
EXPECT_EQ(4u, struct_type.BaseAlignment(MemoryLayout::kStorageBuffer)); EXPECT_EQ(4u, struct_type.BaseAlignment(MemoryLayout::kStorageBuffer));
} }
TEST_F(StructTest, BaseAlignmentArray) { TEST_F(StructTest, BaseAlignmentArray) {
U32 u32; U32 u32;
Array arr(&u32, 4, Array arr(&u32, 4, ArrayDecorationList{create<StrideDecoration>(4)});
ArrayDecorationList{create<StrideDecoration>(Source{}, 4)});
StructMemberList members; StructMemberList members;
{ {
StructMemberDecorationList deco; StructMemberDecorationList deco;
deco.push_back(create<StructMemberOffsetDecoration>(Source{}, 0)); deco.push_back(create<StructMemberOffsetDecoration>(0));
members.push_back(create<StructMember>(Source{}, "foo", &u32, deco)); members.push_back(create<StructMember>("foo", &u32, deco));
} }
{ {
StructMemberDecorationList deco; StructMemberDecorationList deco;
deco.push_back(create<StructMemberOffsetDecoration>(Source{}, 4)); deco.push_back(create<StructMemberOffsetDecoration>(4));
members.push_back(create<StructMember>(Source{}, "bar", &u32, deco)); members.push_back(create<StructMember>("bar", &u32, deco));
} }
{ {
StructMemberDecorationList deco; StructMemberDecorationList deco;
deco.push_back(create<StructMemberOffsetDecoration>(Source{}, 8)); deco.push_back(create<StructMemberOffsetDecoration>(8));
members.push_back(create<StructMember>(Source{}, "bar", &arr, deco)); members.push_back(create<StructMember>("bar", &arr, deco));
} }
StructDecorationList decos; StructDecorationList decos;
auto* str = create<ast::Struct>(Source{}, members, decos); auto* str = create<ast::Struct>(members, decos);
Struct struct_type(mod.RegisterSymbol("struct_type"), "struct_type", str); Struct struct_type(mod->RegisterSymbol("struct_type"), "struct_type", str);
EXPECT_EQ(16u, struct_type.BaseAlignment(MemoryLayout::kUniformBuffer)); EXPECT_EQ(16u, struct_type.BaseAlignment(MemoryLayout::kUniformBuffer));
EXPECT_EQ(4u, struct_type.BaseAlignment(MemoryLayout::kStorageBuffer)); EXPECT_EQ(4u, struct_type.BaseAlignment(MemoryLayout::kStorageBuffer));
} }
TEST_F(StructTest, BaseAlignmentRuntimeArray) { TEST_F(StructTest, BaseAlignmentRuntimeArray) {
U32 u32; U32 u32;
Array arr(&u32, 0, Array arr(&u32, 0, ArrayDecorationList{create<StrideDecoration>(4)});
ArrayDecorationList{create<StrideDecoration>(Source{}, 4)});
StructMemberList members; StructMemberList members;
{ {
StructMemberDecorationList deco; StructMemberDecorationList deco;
deco.push_back(create<StructMemberOffsetDecoration>(Source{}, 0)); deco.push_back(create<StructMemberOffsetDecoration>(0));
members.push_back(create<StructMember>(Source{}, "foo", &u32, deco)); members.push_back(create<StructMember>("foo", &u32, deco));
} }
{ {
StructMemberDecorationList deco; StructMemberDecorationList deco;
deco.push_back(create<StructMemberOffsetDecoration>(Source{}, 4)); deco.push_back(create<StructMemberOffsetDecoration>(4));
members.push_back(create<StructMember>(Source{}, "bar", &u32, deco)); members.push_back(create<StructMember>("bar", &u32, deco));
} }
{ {
StructMemberDecorationList deco; StructMemberDecorationList deco;
deco.push_back(create<StructMemberOffsetDecoration>(Source{}, 8)); deco.push_back(create<StructMemberOffsetDecoration>(8));
members.push_back(create<StructMember>(Source{}, "bar", &u32, deco)); members.push_back(create<StructMember>("bar", &u32, deco));
} }
StructDecorationList decos; StructDecorationList decos;
auto* str = create<ast::Struct>(Source{}, members, decos); auto* str = create<ast::Struct>(members, decos);
Struct struct_type(mod.RegisterSymbol("struct_type"), "struct_type", str); Struct struct_type(mod->RegisterSymbol("struct_type"), "struct_type", str);
EXPECT_EQ(4u, struct_type.BaseAlignment(MemoryLayout::kStorageBuffer)); EXPECT_EQ(4u, struct_type.BaseAlignment(MemoryLayout::kStorageBuffer));
} }
@ -305,13 +298,13 @@ TEST_F(StructTest, BaseAlignmentVec2) {
StructMemberList members; StructMemberList members;
{ {
StructMemberDecorationList deco; StructMemberDecorationList deco;
deco.push_back(create<StructMemberOffsetDecoration>(Source{}, 0)); deco.push_back(create<StructMemberOffsetDecoration>(0));
members.push_back(create<StructMember>(Source{}, "foo", &vec2, deco)); members.push_back(create<StructMember>("foo", &vec2, deco));
} }
StructDecorationList decos; StructDecorationList decos;
auto* str = create<ast::Struct>(Source{}, members, decos); auto* str = create<ast::Struct>(members, decos);
Struct struct_type(mod.RegisterSymbol("struct_type"), "struct_type", str); Struct struct_type(mod->RegisterSymbol("struct_type"), "struct_type", str);
EXPECT_EQ(16u, struct_type.BaseAlignment(MemoryLayout::kUniformBuffer)); EXPECT_EQ(16u, struct_type.BaseAlignment(MemoryLayout::kUniformBuffer));
EXPECT_EQ(8u, struct_type.BaseAlignment(MemoryLayout::kStorageBuffer)); EXPECT_EQ(8u, struct_type.BaseAlignment(MemoryLayout::kStorageBuffer));
} }
@ -323,13 +316,13 @@ TEST_F(StructTest, BaseAlignmentVec3) {
StructMemberList members; StructMemberList members;
{ {
StructMemberDecorationList deco; StructMemberDecorationList deco;
deco.push_back(create<StructMemberOffsetDecoration>(Source{}, 0)); deco.push_back(create<StructMemberOffsetDecoration>(0));
members.push_back(create<StructMember>(Source{}, "foo", &vec3, deco)); members.push_back(create<StructMember>("foo", &vec3, deco));
} }
StructDecorationList decos; StructDecorationList decos;
auto* str = create<ast::Struct>(Source{}, members, decos); auto* str = create<ast::Struct>(members, decos);
Struct struct_type(mod.RegisterSymbol("struct_type"), "struct_type", str); Struct struct_type(mod->RegisterSymbol("struct_type"), "struct_type", str);
EXPECT_EQ(16u, struct_type.BaseAlignment(MemoryLayout::kUniformBuffer)); EXPECT_EQ(16u, struct_type.BaseAlignment(MemoryLayout::kUniformBuffer));
EXPECT_EQ(16u, struct_type.BaseAlignment(MemoryLayout::kStorageBuffer)); EXPECT_EQ(16u, struct_type.BaseAlignment(MemoryLayout::kStorageBuffer));
} }
@ -341,13 +334,13 @@ TEST_F(StructTest, BaseAlignmentVec4) {
StructMemberList members; StructMemberList members;
{ {
StructMemberDecorationList deco; StructMemberDecorationList deco;
deco.push_back(create<StructMemberOffsetDecoration>(Source{}, 0)); deco.push_back(create<StructMemberOffsetDecoration>(0));
members.push_back(create<StructMember>(Source{}, "foo", &vec4, deco)); members.push_back(create<StructMember>("foo", &vec4, deco));
} }
StructDecorationList decos; StructDecorationList decos;
auto* str = create<ast::Struct>(Source{}, members, decos); auto* str = create<ast::Struct>(members, decos);
Struct struct_type(mod.RegisterSymbol("struct_type"), "struct_type", str); Struct struct_type(mod->RegisterSymbol("struct_type"), "struct_type", str);
EXPECT_EQ(16u, struct_type.BaseAlignment(MemoryLayout::kUniformBuffer)); EXPECT_EQ(16u, struct_type.BaseAlignment(MemoryLayout::kUniformBuffer));
EXPECT_EQ(16u, struct_type.BaseAlignment(MemoryLayout::kStorageBuffer)); EXPECT_EQ(16u, struct_type.BaseAlignment(MemoryLayout::kStorageBuffer));
} }

View File

@ -30,101 +30,84 @@ namespace {
using TypeConstructorExpressionTest = TestHelper; using TypeConstructorExpressionTest = TestHelper;
TEST_F(TypeConstructorExpressionTest, Creation) { TEST_F(TypeConstructorExpressionTest, Creation) {
type::F32 f32;
ExpressionList expr; ExpressionList expr;
expr.push_back(create<IdentifierExpression>( expr.push_back(Expr("expr"));
Source{}, mod.RegisterSymbol("expr"), "expr"));
TypeConstructorExpression t(Source{}, &f32, expr); auto* t = create<TypeConstructorExpression>(ty.f32, expr);
EXPECT_EQ(t.type(), &f32); EXPECT_EQ(t->type(), ty.f32);
ASSERT_EQ(t.values().size(), 1u); ASSERT_EQ(t->values().size(), 1u);
EXPECT_EQ(t.values()[0], expr[0]); EXPECT_EQ(t->values()[0], expr[0]);
} }
TEST_F(TypeConstructorExpressionTest, Creation_WithSource) { TEST_F(TypeConstructorExpressionTest, Creation_WithSource) {
type::F32 f32;
ExpressionList expr; ExpressionList expr;
expr.push_back(create<IdentifierExpression>( expr.push_back(Expr("expr"));
Source{}, mod.RegisterSymbol("expr"), "expr"));
TypeConstructorExpression t(Source{Source::Location{20, 2}}, &f32, expr); auto* t = create<TypeConstructorExpression>(Source{Source::Location{20, 2}},
auto src = t.source(); ty.f32, expr);
auto src = t->source();
EXPECT_EQ(src.range.begin.line, 20u); EXPECT_EQ(src.range.begin.line, 20u);
EXPECT_EQ(src.range.begin.column, 2u); EXPECT_EQ(src.range.begin.column, 2u);
} }
TEST_F(TypeConstructorExpressionTest, IsTypeConstructor) { TEST_F(TypeConstructorExpressionTest, IsTypeConstructor) {
type::F32 f32;
ExpressionList expr; ExpressionList expr;
expr.push_back(create<IdentifierExpression>( expr.push_back(Expr("expr"));
Source{}, mod.RegisterSymbol("expr"), "expr"));
TypeConstructorExpression t(Source{}, &f32, expr); auto* t = create<TypeConstructorExpression>(ty.f32, expr);
EXPECT_TRUE(t.Is<TypeConstructorExpression>()); EXPECT_TRUE(t->Is<TypeConstructorExpression>());
} }
TEST_F(TypeConstructorExpressionTest, IsValid) { TEST_F(TypeConstructorExpressionTest, IsValid) {
type::F32 f32;
ExpressionList expr; ExpressionList expr;
expr.push_back(create<IdentifierExpression>( expr.push_back(Expr("expr"));
Source{}, mod.RegisterSymbol("expr"), "expr"));
TypeConstructorExpression t(Source{}, &f32, expr); auto* t = create<TypeConstructorExpression>(ty.f32, expr);
EXPECT_TRUE(t.IsValid()); EXPECT_TRUE(t->IsValid());
} }
TEST_F(TypeConstructorExpressionTest, IsValid_EmptyValue) { TEST_F(TypeConstructorExpressionTest, IsValid_EmptyValue) {
type::F32 f32;
ExpressionList expr; ExpressionList expr;
TypeConstructorExpression t(Source{}, &f32, expr); auto* t = create<TypeConstructorExpression>(ty.f32, expr);
EXPECT_TRUE(t.IsValid()); EXPECT_TRUE(t->IsValid());
} }
TEST_F(TypeConstructorExpressionTest, IsValid_NullType) { TEST_F(TypeConstructorExpressionTest, IsValid_NullType) {
ExpressionList expr; ExpressionList expr;
expr.push_back(create<IdentifierExpression>( expr.push_back(Expr("expr"));
Source{}, mod.RegisterSymbol("expr"), "expr"));
TypeConstructorExpression t(Source{}, nullptr, expr); auto* t = create<TypeConstructorExpression>(nullptr, expr);
EXPECT_FALSE(t.IsValid()); EXPECT_FALSE(t->IsValid());
} }
TEST_F(TypeConstructorExpressionTest, IsValid_NullValue) { TEST_F(TypeConstructorExpressionTest, IsValid_NullValue) {
type::F32 f32;
ExpressionList expr; ExpressionList expr;
expr.push_back(create<IdentifierExpression>( expr.push_back(Expr("expr"));
Source{}, mod.RegisterSymbol("expr"), "expr"));
expr.push_back(nullptr); expr.push_back(nullptr);
TypeConstructorExpression t(Source{}, &f32, expr); auto* t = create<TypeConstructorExpression>(ty.f32, expr);
EXPECT_FALSE(t.IsValid()); EXPECT_FALSE(t->IsValid());
} }
TEST_F(TypeConstructorExpressionTest, IsValid_InvalidValue) { TEST_F(TypeConstructorExpressionTest, IsValid_InvalidValue) {
type::F32 f32;
ExpressionList expr; ExpressionList expr;
expr.push_back( expr.push_back(Expr(""));
create<IdentifierExpression>(Source{}, mod.RegisterSymbol(""), ""));
TypeConstructorExpression t(Source{}, &f32, expr); auto* t = create<TypeConstructorExpression>(ty.f32, expr);
EXPECT_FALSE(t.IsValid()); EXPECT_FALSE(t->IsValid());
} }
TEST_F(TypeConstructorExpressionTest, ToStr) { TEST_F(TypeConstructorExpressionTest, ToStr) {
type::F32 f32; type::Vector vec(ty.f32, 3);
type::Vector vec(&f32, 3);
ExpressionList expr; ExpressionList expr;
expr.push_back(create<IdentifierExpression>( expr.push_back(Expr("expr_1"));
Source{}, mod.RegisterSymbol("expr_1"), "expr_1")); expr.push_back(Expr("expr_2"));
expr.push_back(create<IdentifierExpression>( expr.push_back(Expr("expr_3"));
Source{}, mod.RegisterSymbol("expr_2"), "expr_2"));
expr.push_back(create<IdentifierExpression>(
Source{}, mod.RegisterSymbol("expr_3"), "expr_3"));
TypeConstructorExpression t(Source{}, &vec, expr); auto* t = create<TypeConstructorExpression>(&vec, expr);
std::ostringstream out; std::ostringstream out;
t.to_str(out, 2); t->to_str(out, 2);
EXPECT_EQ(demangle(out.str()), R"( TypeConstructor[not set]{ EXPECT_EQ(demangle(out.str()), R"( TypeConstructor[not set]{
__vec_3__f32 __vec_3__f32
Identifier[not set]{expr_1} Identifier[not set]{expr_1}

View File

@ -28,16 +28,13 @@ namespace {
using UintLiteralTest = TestHelper; using UintLiteralTest = TestHelper;
TEST_F(UintLiteralTest, Value) { TEST_F(UintLiteralTest, Value) {
type::U32 u32; auto* u = create<UintLiteral>(ty.u32, 47);
UintLiteral u{Source{}, &u32, 47}; ASSERT_TRUE(u->Is<UintLiteral>());
ASSERT_TRUE(u.Is<UintLiteral>()); EXPECT_EQ(u->value(), 47u);
EXPECT_EQ(u.value(), 47u);
} }
TEST_F(UintLiteralTest, Is) { TEST_F(UintLiteralTest, Is) {
type::U32 u32; ast::Literal* l = create<UintLiteral>(ty.u32, 42);
UintLiteral u{Source{}, &u32, 42};
Literal* l = &u;
EXPECT_FALSE(l->Is<BoolLiteral>()); EXPECT_FALSE(l->Is<BoolLiteral>());
EXPECT_FALSE(l->Is<SintLiteral>()); EXPECT_FALSE(l->Is<SintLiteral>());
EXPECT_FALSE(l->Is<FloatLiteral>()); EXPECT_FALSE(l->Is<FloatLiteral>());
@ -46,9 +43,8 @@ TEST_F(UintLiteralTest, Is) {
} }
TEST_F(UintLiteralTest, ToStr) { TEST_F(UintLiteralTest, ToStr) {
type::U32 u32; auto* u = create<UintLiteral>(ty.u32, 42);
UintLiteral u{Source{}, &u32, 42}; EXPECT_EQ(u->to_str(), "42");
EXPECT_EQ(u.to_str(), "42");
} }
} // namespace } // namespace

View File

@ -26,55 +26,50 @@ namespace {
using UnaryOpExpressionTest = TestHelper; using UnaryOpExpressionTest = TestHelper;
TEST_F(UnaryOpExpressionTest, Creation) { TEST_F(UnaryOpExpressionTest, Creation) {
auto* ident = create<IdentifierExpression>( auto* ident = Expr("ident");
Source{}, mod.RegisterSymbol("ident"), "ident");
UnaryOpExpression u(Source{}, UnaryOp::kNot, ident); auto* u = create<UnaryOpExpression>(UnaryOp::kNot, ident);
EXPECT_EQ(u.op(), UnaryOp::kNot); EXPECT_EQ(u->op(), UnaryOp::kNot);
EXPECT_EQ(u.expr(), ident); EXPECT_EQ(u->expr(), ident);
} }
TEST_F(UnaryOpExpressionTest, Creation_WithSource) { TEST_F(UnaryOpExpressionTest, Creation_WithSource) {
auto* ident = create<IdentifierExpression>( auto* ident = Expr("ident");
Source{}, mod.RegisterSymbol("ident"), "ident"); auto* u = create<UnaryOpExpression>(Source{Source::Location{20, 2}},
UnaryOpExpression u(Source{Source::Location{20, 2}}, UnaryOp::kNot, ident); UnaryOp::kNot, ident);
auto src = u.source(); auto src = u->source();
EXPECT_EQ(src.range.begin.line, 20u); EXPECT_EQ(src.range.begin.line, 20u);
EXPECT_EQ(src.range.begin.column, 2u); EXPECT_EQ(src.range.begin.column, 2u);
} }
TEST_F(UnaryOpExpressionTest, IsUnaryOp) { TEST_F(UnaryOpExpressionTest, IsUnaryOp) {
auto* ident = create<IdentifierExpression>( auto* ident = Expr("ident");
Source{}, mod.RegisterSymbol("ident"), "ident"); auto* u = create<UnaryOpExpression>(UnaryOp::kNot, ident);
UnaryOpExpression u(Source{}, UnaryOp::kNot, ident); EXPECT_TRUE(u->Is<UnaryOpExpression>());
EXPECT_TRUE(u.Is<UnaryOpExpression>());
} }
TEST_F(UnaryOpExpressionTest, IsValid) { TEST_F(UnaryOpExpressionTest, IsValid) {
auto* ident = create<IdentifierExpression>( auto* ident = Expr("ident");
Source{}, mod.RegisterSymbol("ident"), "ident"); auto* u = create<UnaryOpExpression>(UnaryOp::kNot, ident);
UnaryOpExpression u(Source{}, UnaryOp::kNot, ident); EXPECT_TRUE(u->IsValid());
EXPECT_TRUE(u.IsValid());
} }
TEST_F(UnaryOpExpressionTest, IsValid_NullExpression) { TEST_F(UnaryOpExpressionTest, IsValid_NullExpression) {
UnaryOpExpression u(Source{}, UnaryOp::kNot, nullptr); auto* u = create<UnaryOpExpression>(UnaryOp::kNot, nullptr);
EXPECT_FALSE(u.IsValid()); EXPECT_FALSE(u->IsValid());
} }
TEST_F(UnaryOpExpressionTest, IsValid_InvalidExpression) { TEST_F(UnaryOpExpressionTest, IsValid_InvalidExpression) {
auto* ident = auto* ident = Expr("");
create<IdentifierExpression>(Source{}, mod.RegisterSymbol(""), ""); auto* u = create<UnaryOpExpression>(UnaryOp::kNot, ident);
UnaryOpExpression u(Source{}, UnaryOp::kNot, ident); EXPECT_FALSE(u->IsValid());
EXPECT_FALSE(u.IsValid());
} }
TEST_F(UnaryOpExpressionTest, ToStr) { TEST_F(UnaryOpExpressionTest, ToStr) {
auto* ident = create<IdentifierExpression>( auto* ident = Expr("ident");
Source{}, mod.RegisterSymbol("ident"), "ident"); auto* u = create<UnaryOpExpression>(UnaryOp::kNot, ident);
UnaryOpExpression u(Source{}, UnaryOp::kNot, ident);
std::ostringstream out; std::ostringstream out;
u.to_str(out, 2); u->to_str(out, 2);
EXPECT_EQ(demangle(out.str()), R"( UnaryOp[not set]{ EXPECT_EQ(demangle(out.str()), R"( UnaryOp[not set]{
not not
Identifier[not set]{ident} Identifier[not set]{ident}

View File

@ -25,63 +25,53 @@ namespace {
using VariableDeclStatementTest = TestHelper; using VariableDeclStatementTest = TestHelper;
TEST_F(VariableDeclStatementTest, Creation) { TEST_F(VariableDeclStatementTest, Creation) {
type::F32 f32; auto* var = Var("a", StorageClass::kNone, ty.f32);
auto* var = create<Variable>(Source{}, "a", StorageClass::kNone, &f32, false,
nullptr, ast::VariableDecorationList{});
VariableDeclStatement stmt(Source{}, var); auto* stmt = create<VariableDeclStatement>(var);
EXPECT_EQ(stmt.variable(), var); EXPECT_EQ(stmt->variable(), var);
} }
TEST_F(VariableDeclStatementTest, Creation_WithSource) { TEST_F(VariableDeclStatementTest, Creation_WithSource) {
type::F32 f32; auto* var = Var("a", StorageClass::kNone, ty.f32);
auto* var = create<Variable>(Source{}, "a", StorageClass::kNone, &f32, false,
nullptr, ast::VariableDecorationList{});
VariableDeclStatement stmt(Source{Source::Location{20, 2}}, var); auto* stmt =
auto src = stmt.source(); create<VariableDeclStatement>(Source{Source::Location{20, 2}}, var);
auto src = stmt->source();
EXPECT_EQ(src.range.begin.line, 20u); EXPECT_EQ(src.range.begin.line, 20u);
EXPECT_EQ(src.range.begin.column, 2u); EXPECT_EQ(src.range.begin.column, 2u);
} }
TEST_F(VariableDeclStatementTest, IsVariableDecl) { TEST_F(VariableDeclStatementTest, IsVariableDecl) {
type::F32 f32; auto* var = Var("a", StorageClass::kNone, ty.f32);
auto* var = create<Variable>(Source{}, "a", StorageClass::kNone, &f32, false,
nullptr, ast::VariableDecorationList{});
VariableDeclStatement stmt(Source{}, var); auto* stmt = create<VariableDeclStatement>(var);
EXPECT_TRUE(stmt.Is<VariableDeclStatement>()); EXPECT_TRUE(stmt->Is<VariableDeclStatement>());
} }
TEST_F(VariableDeclStatementTest, IsValid) { TEST_F(VariableDeclStatementTest, IsValid) {
type::F32 f32; auto* var = Var("a", StorageClass::kNone, ty.f32);
auto* var = create<Variable>(Source{}, "a", StorageClass::kNone, &f32, false, auto* stmt = create<VariableDeclStatement>(var);
nullptr, ast::VariableDecorationList{}); EXPECT_TRUE(stmt->IsValid());
VariableDeclStatement stmt(Source{}, var);
EXPECT_TRUE(stmt.IsValid());
} }
TEST_F(VariableDeclStatementTest, IsValid_InvalidVariable) { TEST_F(VariableDeclStatementTest, IsValid_InvalidVariable) {
type::F32 f32; auto* var = Var("", StorageClass::kNone, ty.f32);
auto* var = create<Variable>(Source{}, "", StorageClass::kNone, &f32, false, auto* stmt = create<VariableDeclStatement>(var);
nullptr, ast::VariableDecorationList{}); EXPECT_FALSE(stmt->IsValid());
VariableDeclStatement stmt(Source{}, var);
EXPECT_FALSE(stmt.IsValid());
} }
TEST_F(VariableDeclStatementTest, IsValid_NullVariable) { TEST_F(VariableDeclStatementTest, IsValid_NullVariable) {
VariableDeclStatement stmt(Source{}, nullptr); auto* stmt = create<VariableDeclStatement>(nullptr);
EXPECT_FALSE(stmt.IsValid()); EXPECT_FALSE(stmt->IsValid());
} }
TEST_F(VariableDeclStatementTest, ToStr) { TEST_F(VariableDeclStatementTest, ToStr) {
type::F32 f32; auto* var = Var("a", StorageClass::kNone, ty.f32);
auto* var = create<Variable>(Source{}, "a", StorageClass::kNone, &f32, false,
nullptr, ast::VariableDecorationList{});
VariableDeclStatement stmt(Source{Source::Location{20, 2}}, var); auto* stmt =
create<VariableDeclStatement>(Source{Source::Location{20, 2}}, var);
std::ostringstream out; std::ostringstream out;
stmt.to_str(out, 2); stmt->to_str(out, 2);
EXPECT_EQ(out.str(), R"( VariableDeclStatement{ EXPECT_EQ(out.str(), R"( VariableDeclStatement{
Variable{ Variable{
a a

View File

@ -27,106 +27,83 @@ namespace {
using VariableTest = TestHelper; using VariableTest = TestHelper;
TEST_F(VariableTest, Creation) { TEST_F(VariableTest, Creation) {
type::I32 t; auto* v = Var("my_var", StorageClass::kFunction, ty.i32);
Variable v(Source{}, "my_var", StorageClass::kFunction, &t, false, nullptr,
ast::VariableDecorationList{});
EXPECT_EQ(v.name(), "my_var"); EXPECT_EQ(v->name(), "my_var");
EXPECT_EQ(v.storage_class(), StorageClass::kFunction); EXPECT_EQ(v->storage_class(), StorageClass::kFunction);
EXPECT_EQ(v.type(), &t); EXPECT_EQ(v->type(), ty.i32);
EXPECT_EQ(v.source().range.begin.line, 0u); EXPECT_EQ(v->source().range.begin.line, 0u);
EXPECT_EQ(v.source().range.begin.column, 0u); EXPECT_EQ(v->source().range.begin.column, 0u);
EXPECT_EQ(v.source().range.end.line, 0u); EXPECT_EQ(v->source().range.end.line, 0u);
EXPECT_EQ(v.source().range.end.column, 0u); EXPECT_EQ(v->source().range.end.column, 0u);
} }
TEST_F(VariableTest, CreationWithSource) { TEST_F(VariableTest, CreationWithSource) {
Source s{Source::Range{Source::Location{27, 4}, Source::Location{27, 5}}}; auto* v = Var(
type::F32 t; Source{Source::Range{Source::Location{27, 4}, Source::Location{27, 5}}},
Variable v(s, "i", StorageClass::kPrivate, &t, false, nullptr, "i", StorageClass::kPrivate, ty.f32, nullptr, VariableDecorationList{});
ast::VariableDecorationList{});
EXPECT_EQ(v.name(), "i"); EXPECT_EQ(v->name(), "i");
EXPECT_EQ(v.storage_class(), StorageClass::kPrivate); EXPECT_EQ(v->storage_class(), StorageClass::kPrivate);
EXPECT_EQ(v.type(), &t); EXPECT_EQ(v->type(), ty.f32);
EXPECT_EQ(v.source().range.begin.line, 27u); EXPECT_EQ(v->source().range.begin.line, 27u);
EXPECT_EQ(v.source().range.begin.column, 4u); EXPECT_EQ(v->source().range.begin.column, 4u);
EXPECT_EQ(v.source().range.end.line, 27u); EXPECT_EQ(v->source().range.end.line, 27u);
EXPECT_EQ(v.source().range.end.column, 5u); EXPECT_EQ(v->source().range.end.column, 5u);
} }
TEST_F(VariableTest, CreationEmpty) { TEST_F(VariableTest, CreationEmpty) {
type::I32 t; auto* v = Var(
Source s{Source::Range{Source::Location{27, 4}, Source::Location{27, 7}}}; Source{Source::Range{Source::Location{27, 4}, Source::Location{27, 7}}},
Variable v(s, "a_var", StorageClass::kWorkgroup, &t, false, nullptr, "a_var", StorageClass::kWorkgroup, ty.i32, nullptr,
ast::VariableDecorationList{}); VariableDecorationList{});
EXPECT_EQ(v.name(), "a_var"); EXPECT_EQ(v->name(), "a_var");
EXPECT_EQ(v.storage_class(), StorageClass::kWorkgroup); EXPECT_EQ(v->storage_class(), StorageClass::kWorkgroup);
EXPECT_EQ(v.type(), &t); EXPECT_EQ(v->type(), ty.i32);
EXPECT_EQ(v.source().range.begin.line, 27u); EXPECT_EQ(v->source().range.begin.line, 27u);
EXPECT_EQ(v.source().range.begin.column, 4u); EXPECT_EQ(v->source().range.begin.column, 4u);
EXPECT_EQ(v.source().range.end.line, 27u); EXPECT_EQ(v->source().range.end.line, 27u);
EXPECT_EQ(v.source().range.end.column, 7u); EXPECT_EQ(v->source().range.end.column, 7u);
} }
TEST_F(VariableTest, IsValid) { TEST_F(VariableTest, IsValid) {
type::I32 t; auto* v = Var("my_var", StorageClass::kNone, ty.i32);
Variable v{Source{}, "my_var", StorageClass::kNone, &t, EXPECT_TRUE(v->IsValid());
false, nullptr, ast::VariableDecorationList{}};
EXPECT_TRUE(v.IsValid());
} }
TEST_F(VariableTest, IsValid_WithConstructor) { TEST_F(VariableTest, IsValid_WithConstructor) {
type::I32 t; auto* v = Var("my_var", StorageClass::kNone, ty.i32, Expr("ident"),
Variable v{Source{}, ast::VariableDecorationList{});
"my_var", EXPECT_TRUE(v->IsValid());
StorageClass::kNone,
&t,
false,
create<IdentifierExpression>(Source{}, mod.RegisterSymbol("ident"),
"ident"),
ast::VariableDecorationList{}};
EXPECT_TRUE(v.IsValid());
} }
TEST_F(VariableTest, IsValid_MissinName) { TEST_F(VariableTest, IsValid_MissinName) {
type::I32 t; auto* v = Var("", StorageClass::kNone, ty.i32);
Variable v{Source{}, "", StorageClass::kNone, &t, EXPECT_FALSE(v->IsValid());
false, nullptr, ast::VariableDecorationList{}};
EXPECT_FALSE(v.IsValid());
} }
TEST_F(VariableTest, IsValid_MissingType) { TEST_F(VariableTest, IsValid_MissingType) {
Variable v{Source{}, "x", StorageClass::kNone, nullptr, auto* v = Var("x", StorageClass::kNone, nullptr);
false, nullptr, ast::VariableDecorationList{}}; EXPECT_FALSE(v->IsValid());
EXPECT_FALSE(v.IsValid());
} }
TEST_F(VariableTest, IsValid_MissingBoth) { TEST_F(VariableTest, IsValid_MissingBoth) {
Variable v{Source{}, "", StorageClass::kNone, nullptr, auto* v = Var("", StorageClass::kNone, nullptr);
false, nullptr, ast::VariableDecorationList{}}; EXPECT_FALSE(v->IsValid());
EXPECT_FALSE(v.IsValid());
} }
TEST_F(VariableTest, IsValid_InvalidConstructor) { TEST_F(VariableTest, IsValid_InvalidConstructor) {
type::I32 t; auto* v = Var("my_var", StorageClass::kNone, ty.i32, Expr(""),
Variable v{Source{}, ast::VariableDecorationList{});
"my_var", EXPECT_FALSE(v->IsValid());
StorageClass::kNone,
&t,
false,
create<IdentifierExpression>(Source{}, mod.RegisterSymbol(""), ""),
ast::VariableDecorationList{}};
EXPECT_FALSE(v.IsValid());
} }
TEST_F(VariableTest, to_str) { TEST_F(VariableTest, to_str) {
type::F32 t; auto* v = Var("my_var", StorageClass::kFunction, ty.f32, nullptr,
Variable v{Source{}, "my_var", StorageClass::kFunction, &t, ast::VariableDecorationList{});
false, nullptr, ast::VariableDecorationList{}};
std::ostringstream out; std::ostringstream out;
v.to_str(out, 2); v->to_str(out, 2);
EXPECT_EQ(out.str(), R"( Variable{ EXPECT_EQ(out.str(), R"( Variable{
my_var my_var
function function
@ -136,13 +113,11 @@ TEST_F(VariableTest, to_str) {
} }
TEST_F(VariableTest, WithDecorations) { TEST_F(VariableTest, WithDecorations) {
type::F32 t; auto* var = Var("my_var", StorageClass::kFunction, ty.i32, nullptr,
auto* var = create<Variable>(
Source{}, "my_var", StorageClass::kFunction, &t, false, nullptr,
VariableDecorationList{ VariableDecorationList{
create<LocationDecoration>(Source{}, 1), create<LocationDecoration>(1),
create<BuiltinDecoration>(Source{}, Builtin::kPosition), create<BuiltinDecoration>(Builtin::kPosition),
create<ConstantIdDecoration>(Source{}, 1200), create<ConstantIdDecoration>(1200),
}); });
EXPECT_TRUE(var->HasLocationDecoration()); EXPECT_TRUE(var->HasLocationDecoration());
@ -151,25 +126,19 @@ TEST_F(VariableTest, WithDecorations) {
} }
TEST_F(VariableTest, ConstantId) { TEST_F(VariableTest, ConstantId) {
type::F32 t; auto* var = Var("my_var", StorageClass::kFunction, ty.i32, nullptr,
auto* var = create<Variable>(Source{}, "my_var", StorageClass::kFunction, &t,
false, nullptr,
VariableDecorationList{ VariableDecorationList{
create<ConstantIdDecoration>(Source{}, 1200), create<ConstantIdDecoration>(1200),
}); });
EXPECT_EQ(var->constant_id(), 1200u); EXPECT_EQ(var->constant_id(), 1200u);
} }
TEST_F(VariableTest, Decorated_to_str) { TEST_F(VariableTest, Decorated_to_str) {
type::F32 t; auto* var = Var("my_var", StorageClass::kFunction, ty.f32, Expr("expr"),
auto* var =
create<Variable>(Source{}, "my_var", StorageClass::kFunction, &t, false,
create<IdentifierExpression>(
Source{}, mod.RegisterSymbol("expr"), "expr"),
VariableDecorationList{ VariableDecorationList{
create<BindingDecoration>(Source{}, 2), create<BindingDecoration>(2),
create<SetDecoration>(Source{}, 1), create<SetDecoration>(1),
}); });
std::ostringstream out; std::ostringstream out;

View File

@ -26,48 +26,47 @@ namespace {
using WorkgroupDecorationTest = TestHelper; using WorkgroupDecorationTest = TestHelper;
TEST_F(WorkgroupDecorationTest, Creation_1param) { TEST_F(WorkgroupDecorationTest, Creation_1param) {
WorkgroupDecoration d{Source{}, 2}; auto* d = create<WorkgroupDecoration>(2);
uint32_t x = 0; uint32_t x = 0;
uint32_t y = 0; uint32_t y = 0;
uint32_t z = 0; uint32_t z = 0;
std::tie(x, y, z) = d.values(); std::tie(x, y, z) = d->values();
EXPECT_EQ(x, 2u); EXPECT_EQ(x, 2u);
EXPECT_EQ(y, 1u); EXPECT_EQ(y, 1u);
EXPECT_EQ(z, 1u); EXPECT_EQ(z, 1u);
} }
TEST_F(WorkgroupDecorationTest, Creation_2param) { TEST_F(WorkgroupDecorationTest, Creation_2param) {
WorkgroupDecoration d{Source{}, 2, 4}; auto* d = create<WorkgroupDecoration>(2, 4);
uint32_t x = 0; uint32_t x = 0;
uint32_t y = 0; uint32_t y = 0;
uint32_t z = 0; uint32_t z = 0;
std::tie(x, y, z) = d.values(); std::tie(x, y, z) = d->values();
EXPECT_EQ(x, 2u); EXPECT_EQ(x, 2u);
EXPECT_EQ(y, 4u); EXPECT_EQ(y, 4u);
EXPECT_EQ(z, 1u); EXPECT_EQ(z, 1u);
} }
TEST_F(WorkgroupDecorationTest, Creation_3param) { TEST_F(WorkgroupDecorationTest, Creation_3param) {
WorkgroupDecoration d{Source{}, 2, 4, 6}; auto* d = create<WorkgroupDecoration>(2, 4, 6);
uint32_t x = 0; uint32_t x = 0;
uint32_t y = 0; uint32_t y = 0;
uint32_t z = 0; uint32_t z = 0;
std::tie(x, y, z) = d.values(); std::tie(x, y, z) = d->values();
EXPECT_EQ(x, 2u); EXPECT_EQ(x, 2u);
EXPECT_EQ(y, 4u); EXPECT_EQ(y, 4u);
EXPECT_EQ(z, 6u); EXPECT_EQ(z, 6u);
} }
TEST_F(WorkgroupDecorationTest, Is) { TEST_F(WorkgroupDecorationTest, Is) {
WorkgroupDecoration wd{Source{}, 2, 4, 6}; Decoration* d = create<WorkgroupDecoration>(2, 4, 6);
Decoration* d = &wd;
EXPECT_TRUE(d->Is<WorkgroupDecoration>()); EXPECT_TRUE(d->Is<WorkgroupDecoration>());
EXPECT_FALSE(d->Is<StageDecoration>()); EXPECT_FALSE(d->Is<StageDecoration>());
} }
TEST_F(WorkgroupDecorationTest, ToStr) { TEST_F(WorkgroupDecorationTest, ToStr) {
WorkgroupDecoration d{Source{}, 2, 4, 6}; auto* d = create<WorkgroupDecoration>(2, 4, 6);
std::ostringstream out; std::ostringstream out;
d.to_str(out, 0); d->to_str(out, 0);
EXPECT_EQ(out.str(), R"(WorkgroupDecoration{2 4 6} EXPECT_EQ(out.str(), R"(WorkgroupDecoration{2 4 6}
)"); )");
} }