writer/wgsl tests: Replace std::make_unique<T> -> create<T>

create() is currently just a simple forwarder to std::make_unique<>, but
will be later replaced with a function that returns a raw pointer,
and owned by the context.

Bug: tint:322
Change-Id: I69cb8eb0a4943831fc9233e4dcce2ee65b682738
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/32674
Commit-Queue: dan sinclair <dsinclair@chromium.org>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
Ben Clayton 2020-11-14 01:11:04 +00:00 committed by Commit Bot service account
parent 16b1b9438d
commit 9a31c641e2
22 changed files with 219 additions and 248 deletions

View File

@ -44,16 +44,14 @@ TEST_F(WgslGeneratorImplTest, EmitConstructedType_Struct) {
ast::type::F32Type f32;
ast::StructMemberList members;
members.push_back(std::make_unique<ast::StructMember>(
"a", &f32, ast::StructMemberDecorationList{}));
members.push_back(
create<ast::StructMember>("a", &f32, ast::StructMemberDecorationList{}));
ast::StructMemberDecorationList b_deco;
b_deco.push_back(
std::make_unique<ast::StructMemberOffsetDecoration>(4, Source{}));
members.push_back(
std::make_unique<ast::StructMember>("b", &i32, std::move(b_deco)));
b_deco.push_back(create<ast::StructMemberOffsetDecoration>(4, Source{}));
members.push_back(create<ast::StructMember>("b", &i32, std::move(b_deco)));
auto str = std::make_unique<ast::Struct>();
auto str = create<ast::Struct>();
str->set_members(std::move(members));
ast::type::StructType s("A", std::move(str));
@ -75,16 +73,14 @@ TEST_F(WgslGeneratorImplTest, EmitAliasType_ToStruct) {
ast::type::F32Type f32;
ast::StructMemberList members;
members.push_back(std::make_unique<ast::StructMember>(
"a", &f32, ast::StructMemberDecorationList{}));
members.push_back(
create<ast::StructMember>("a", &f32, ast::StructMemberDecorationList{}));
ast::StructMemberDecorationList b_deco;
b_deco.push_back(
std::make_unique<ast::StructMemberOffsetDecoration>(4, Source{}));
members.push_back(
std::make_unique<ast::StructMember>("b", &i32, std::move(b_deco)));
b_deco.push_back(create<ast::StructMemberOffsetDecoration>(4, Source{}));
members.push_back(create<ast::StructMember>("b", &i32, std::move(b_deco)));
auto str = std::make_unique<ast::Struct>();
auto str = create<ast::Struct>();
str->set_members(std::move(members));
ast::type::StructType s("A", std::move(str));

View File

@ -32,9 +32,9 @@ using WgslGeneratorImplTest = TestHelper;
TEST_F(WgslGeneratorImplTest, EmitExpression_ArrayAccessor) {
ast::type::I32Type i32;
auto lit = std::make_unique<ast::SintLiteral>(&i32, 5);
auto idx = std::make_unique<ast::ScalarConstructorExpression>(std::move(lit));
auto ary = std::make_unique<ast::IdentifierExpression>("ary");
auto lit = create<ast::SintLiteral>(&i32, 5);
auto idx = create<ast::ScalarConstructorExpression>(std::move(lit));
auto ary = create<ast::IdentifierExpression>("ary");
ast::ArrayAccessorExpression expr(std::move(ary), std::move(idx));
@ -43,8 +43,8 @@ TEST_F(WgslGeneratorImplTest, EmitExpression_ArrayAccessor) {
}
TEST_F(WgslGeneratorImplTest, EmitArrayAccessor) {
auto ary = std::make_unique<ast::IdentifierExpression>("ary");
auto idx = std::make_unique<ast::IdentifierExpression>("idx");
auto ary = create<ast::IdentifierExpression>("ary");
auto idx = create<ast::IdentifierExpression>("idx");
ast::ArrayAccessorExpression expr(std::move(ary), std::move(idx));

View File

@ -29,8 +29,8 @@ namespace {
using WgslGeneratorImplTest = TestHelper;
TEST_F(WgslGeneratorImplTest, Emit_Assign) {
auto lhs = std::make_unique<ast::IdentifierExpression>("lhs");
auto rhs = std::make_unique<ast::IdentifierExpression>("rhs");
auto lhs = create<ast::IdentifierExpression>("lhs");
auto rhs = create<ast::IdentifierExpression>("rhs");
ast::AssignmentStatement assign(std::move(lhs), std::move(rhs));
gen.increment_indent();

View File

@ -37,8 +37,8 @@ using WgslBinaryTest = TestParamHelper<BinaryData>;
TEST_P(WgslBinaryTest, Emit) {
auto params = GetParam();
auto left = std::make_unique<ast::IdentifierExpression>("left");
auto right = std::make_unique<ast::IdentifierExpression>("right");
auto left = create<ast::IdentifierExpression>("left");
auto right = create<ast::IdentifierExpression>("right");
ast::BinaryExpression expr(params.op, std::move(left), std::move(right));

View File

@ -30,7 +30,7 @@ using WgslGeneratorImplTest = TestHelper;
TEST_F(WgslGeneratorImplTest, EmitExpression_Bitcast) {
ast::type::F32Type f32;
auto id = std::make_unique<ast::IdentifierExpression>("id");
auto id = create<ast::IdentifierExpression>("id");
ast::BitcastExpression bitcast(&f32, std::move(id));
ASSERT_TRUE(gen.EmitExpression(&bitcast)) << gen.error();

View File

@ -29,7 +29,7 @@ using WgslGeneratorImplTest = TestHelper;
TEST_F(WgslGeneratorImplTest, Emit_Block) {
ast::BlockStatement b;
b.append(std::make_unique<ast::DiscardStatement>());
b.append(create<ast::DiscardStatement>());
gen.increment_indent();
@ -42,7 +42,7 @@ TEST_F(WgslGeneratorImplTest, Emit_Block) {
TEST_F(WgslGeneratorImplTest, Emit_Block_WithoutNewline) {
ast::BlockStatement b;
b.append(std::make_unique<ast::DiscardStatement>());
b.append(create<ast::DiscardStatement>());
gen.increment_indent();

View File

@ -29,7 +29,7 @@ namespace {
using WgslGeneratorImplTest = TestHelper;
TEST_F(WgslGeneratorImplTest, EmitExpression_Call_WithoutParams) {
auto id = std::make_unique<ast::IdentifierExpression>("my_func");
auto id = create<ast::IdentifierExpression>("my_func");
ast::CallExpression call(std::move(id), {});
ASSERT_TRUE(gen.EmitExpression(&call)) << gen.error();
@ -37,10 +37,10 @@ TEST_F(WgslGeneratorImplTest, EmitExpression_Call_WithoutParams) {
}
TEST_F(WgslGeneratorImplTest, EmitExpression_Call_WithParams) {
auto id = std::make_unique<ast::IdentifierExpression>("my_func");
auto id = create<ast::IdentifierExpression>("my_func");
ast::ExpressionList params;
params.push_back(std::make_unique<ast::IdentifierExpression>("param1"));
params.push_back(std::make_unique<ast::IdentifierExpression>("param2"));
params.push_back(create<ast::IdentifierExpression>("param1"));
params.push_back(create<ast::IdentifierExpression>("param2"));
ast::CallExpression call(std::move(id), std::move(params));
ASSERT_TRUE(gen.EmitExpression(&call)) << gen.error();
@ -48,13 +48,13 @@ TEST_F(WgslGeneratorImplTest, EmitExpression_Call_WithParams) {
}
TEST_F(WgslGeneratorImplTest, EmitStatement_Call) {
auto id = std::make_unique<ast::IdentifierExpression>("my_func");
auto id = create<ast::IdentifierExpression>("my_func");
ast::ExpressionList params;
params.push_back(std::make_unique<ast::IdentifierExpression>("param1"));
params.push_back(std::make_unique<ast::IdentifierExpression>("param2"));
params.push_back(create<ast::IdentifierExpression>("param1"));
params.push_back(create<ast::IdentifierExpression>("param2"));
ast::CallStatement call(
std::make_unique<ast::CallExpression>(std::move(id), std::move(params)));
create<ast::CallExpression>(std::move(id), std::move(params)));
gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(&call)) << gen.error();

View File

@ -33,11 +33,11 @@ using WgslGeneratorImplTest = TestHelper;
TEST_F(WgslGeneratorImplTest, Emit_Case) {
ast::type::I32Type i32;
auto body = std::make_unique<ast::BlockStatement>();
body->append(std::make_unique<ast::BreakStatement>());
auto body = create<ast::BlockStatement>();
body->append(create<ast::BreakStatement>());
ast::CaseSelectorList lit;
lit.push_back(std::make_unique<ast::SintLiteral>(&i32, 5));
lit.push_back(create<ast::SintLiteral>(&i32, 5));
ast::CaseStatement c(std::move(lit), std::move(body));
gen.increment_indent();
@ -52,12 +52,12 @@ TEST_F(WgslGeneratorImplTest, Emit_Case) {
TEST_F(WgslGeneratorImplTest, Emit_Case_MultipleSelectors) {
ast::type::I32Type i32;
auto body = std::make_unique<ast::BlockStatement>();
body->append(std::make_unique<ast::BreakStatement>());
auto body = create<ast::BlockStatement>();
body->append(create<ast::BreakStatement>());
ast::CaseSelectorList lit;
lit.push_back(std::make_unique<ast::SintLiteral>(&i32, 5));
lit.push_back(std::make_unique<ast::SintLiteral>(&i32, 6));
lit.push_back(create<ast::SintLiteral>(&i32, 5));
lit.push_back(create<ast::SintLiteral>(&i32, 6));
ast::CaseStatement c(std::move(lit), std::move(body));
gen.increment_indent();
@ -72,8 +72,8 @@ TEST_F(WgslGeneratorImplTest, Emit_Case_MultipleSelectors) {
TEST_F(WgslGeneratorImplTest, Emit_Case_Default) {
ast::CaseStatement c;
auto body = std::make_unique<ast::BlockStatement>();
body->append(std::make_unique<ast::BreakStatement>());
auto body = create<ast::BlockStatement>();
body->append(create<ast::BreakStatement>());
c.set_body(std::move(body));
gen.increment_indent();

View File

@ -32,7 +32,7 @@ TEST_F(WgslGeneratorImplTest, EmitExpression_Cast) {
ast::type::F32Type f32;
ast::ExpressionList params;
params.push_back(std::make_unique<ast::IdentifierExpression>("id"));
params.push_back(create<ast::IdentifierExpression>("id"));
ast::TypeConstructorExpression cast(&f32, std::move(params));

View File

@ -37,7 +37,7 @@ using WgslGeneratorImplTest = TestHelper;
TEST_F(WgslGeneratorImplTest, EmitConstructor_Bool) {
ast::type::BoolType bool_type;
auto lit = std::make_unique<ast::BoolLiteral>(&bool_type, false);
auto lit = create<ast::BoolLiteral>(&bool_type, false);
ast::ScalarConstructorExpression expr(std::move(lit));
ASSERT_TRUE(gen.EmitConstructor(&expr)) << gen.error();
@ -46,7 +46,7 @@ TEST_F(WgslGeneratorImplTest, EmitConstructor_Bool) {
TEST_F(WgslGeneratorImplTest, EmitConstructor_Int) {
ast::type::I32Type i32;
auto lit = std::make_unique<ast::SintLiteral>(&i32, -12345);
auto lit = create<ast::SintLiteral>(&i32, -12345);
ast::ScalarConstructorExpression expr(std::move(lit));
ASSERT_TRUE(gen.EmitConstructor(&expr)) << gen.error();
@ -55,7 +55,7 @@ TEST_F(WgslGeneratorImplTest, EmitConstructor_Int) {
TEST_F(WgslGeneratorImplTest, EmitConstructor_UInt) {
ast::type::U32Type u32;
auto lit = std::make_unique<ast::UintLiteral>(&u32, 56779);
auto lit = create<ast::UintLiteral>(&u32, 56779);
ast::ScalarConstructorExpression expr(std::move(lit));
ASSERT_TRUE(gen.EmitConstructor(&expr)) << gen.error();
@ -65,8 +65,7 @@ TEST_F(WgslGeneratorImplTest, EmitConstructor_UInt) {
TEST_F(WgslGeneratorImplTest, EmitConstructor_Float) {
ast::type::F32Type f32;
// Use a number close to 1<<30 but whose decimal representation ends in 0.
auto lit = std::make_unique<ast::FloatLiteral>(
&f32, static_cast<float>((1 << 30) - 4));
auto lit = create<ast::FloatLiteral>(&f32, static_cast<float>((1 << 30) - 4));
ast::ScalarConstructorExpression expr(std::move(lit));
ASSERT_TRUE(gen.EmitConstructor(&expr)) << gen.error();
@ -76,10 +75,9 @@ TEST_F(WgslGeneratorImplTest, EmitConstructor_Float) {
TEST_F(WgslGeneratorImplTest, EmitConstructor_Type_Float) {
ast::type::F32Type f32;
auto lit = std::make_unique<ast::FloatLiteral>(&f32, -1.2e-5);
auto lit = create<ast::FloatLiteral>(&f32, -1.2e-5);
ast::ExpressionList values;
values.push_back(
std::make_unique<ast::ScalarConstructorExpression>(std::move(lit)));
values.push_back(create<ast::ScalarConstructorExpression>(std::move(lit)));
ast::TypeConstructorExpression expr(&f32, std::move(values));
@ -90,10 +88,9 @@ TEST_F(WgslGeneratorImplTest, EmitConstructor_Type_Float) {
TEST_F(WgslGeneratorImplTest, EmitConstructor_Type_Bool) {
ast::type::BoolType b;
auto lit = std::make_unique<ast::BoolLiteral>(&b, true);
auto lit = create<ast::BoolLiteral>(&b, true);
ast::ExpressionList values;
values.push_back(
std::make_unique<ast::ScalarConstructorExpression>(std::move(lit)));
values.push_back(create<ast::ScalarConstructorExpression>(std::move(lit)));
ast::TypeConstructorExpression expr(&b, std::move(values));
@ -104,10 +101,9 @@ TEST_F(WgslGeneratorImplTest, EmitConstructor_Type_Bool) {
TEST_F(WgslGeneratorImplTest, EmitConstructor_Type_Int) {
ast::type::I32Type i32;
auto lit = std::make_unique<ast::SintLiteral>(&i32, -12345);
auto lit = create<ast::SintLiteral>(&i32, -12345);
ast::ExpressionList values;
values.push_back(
std::make_unique<ast::ScalarConstructorExpression>(std::move(lit)));
values.push_back(create<ast::ScalarConstructorExpression>(std::move(lit)));
ast::TypeConstructorExpression expr(&i32, std::move(values));
@ -118,10 +114,9 @@ TEST_F(WgslGeneratorImplTest, EmitConstructor_Type_Int) {
TEST_F(WgslGeneratorImplTest, EmitConstructor_Type_Uint) {
ast::type::U32Type u32;
auto lit = std::make_unique<ast::UintLiteral>(&u32, 12345);
auto lit = create<ast::UintLiteral>(&u32, 12345);
ast::ExpressionList values;
values.push_back(
std::make_unique<ast::ScalarConstructorExpression>(std::move(lit)));
values.push_back(create<ast::ScalarConstructorExpression>(std::move(lit)));
ast::TypeConstructorExpression expr(&u32, std::move(values));
@ -133,16 +128,13 @@ TEST_F(WgslGeneratorImplTest, EmitConstructor_Type_Vec) {
ast::type::F32Type f32;
ast::type::VectorType vec(&f32, 3);
auto lit1 = std::make_unique<ast::FloatLiteral>(&f32, 1.f);
auto lit2 = std::make_unique<ast::FloatLiteral>(&f32, 2.f);
auto lit3 = std::make_unique<ast::FloatLiteral>(&f32, 3.f);
auto lit1 = create<ast::FloatLiteral>(&f32, 1.f);
auto lit2 = create<ast::FloatLiteral>(&f32, 2.f);
auto lit3 = create<ast::FloatLiteral>(&f32, 3.f);
ast::ExpressionList values;
values.push_back(
std::make_unique<ast::ScalarConstructorExpression>(std::move(lit1)));
values.push_back(
std::make_unique<ast::ScalarConstructorExpression>(std::move(lit2)));
values.push_back(
std::make_unique<ast::ScalarConstructorExpression>(std::move(lit3)));
values.push_back(create<ast::ScalarConstructorExpression>(std::move(lit1)));
values.push_back(create<ast::ScalarConstructorExpression>(std::move(lit2)));
values.push_back(create<ast::ScalarConstructorExpression>(std::move(lit3)));
ast::TypeConstructorExpression expr(&vec, std::move(values));
@ -159,19 +151,17 @@ TEST_F(WgslGeneratorImplTest, EmitConstructor_Type_Mat) {
ast::ExpressionList mat_values;
for (size_t i = 0; i < 3; i++) {
auto lit1 = std::make_unique<ast::FloatLiteral>(
&f32, static_cast<float>(1 + (i * 2)));
auto lit2 = std::make_unique<ast::FloatLiteral>(
&f32, static_cast<float>(2 + (i * 2)));
auto lit1 =
create<ast::FloatLiteral>(&f32, static_cast<float>(1 + (i * 2)));
auto lit2 =
create<ast::FloatLiteral>(&f32, static_cast<float>(2 + (i * 2)));
ast::ExpressionList values;
values.push_back(
std::make_unique<ast::ScalarConstructorExpression>(std::move(lit1)));
values.push_back(
std::make_unique<ast::ScalarConstructorExpression>(std::move(lit2)));
values.push_back(create<ast::ScalarConstructorExpression>(std::move(lit1)));
values.push_back(create<ast::ScalarConstructorExpression>(std::move(lit2)));
mat_values.push_back(std::make_unique<ast::TypeConstructorExpression>(
&vec, std::move(values)));
mat_values.push_back(
create<ast::TypeConstructorExpression>(&vec, std::move(values)));
}
ast::TypeConstructorExpression expr(&mat, std::move(mat_values));
@ -191,23 +181,20 @@ TEST_F(WgslGeneratorImplTest, EmitConstructor_Type_Array) {
ast::ExpressionList ary_values;
for (size_t i = 0; i < 3; i++) {
auto lit1 = std::make_unique<ast::FloatLiteral>(
&f32, static_cast<float>(1 + (i * 3)));
auto lit2 = std::make_unique<ast::FloatLiteral>(
&f32, static_cast<float>(2 + (i * 3)));
auto lit3 = std::make_unique<ast::FloatLiteral>(
&f32, static_cast<float>(3 + (i * 3)));
auto lit1 =
create<ast::FloatLiteral>(&f32, static_cast<float>(1 + (i * 3)));
auto lit2 =
create<ast::FloatLiteral>(&f32, static_cast<float>(2 + (i * 3)));
auto lit3 =
create<ast::FloatLiteral>(&f32, static_cast<float>(3 + (i * 3)));
ast::ExpressionList values;
values.push_back(
std::make_unique<ast::ScalarConstructorExpression>(std::move(lit1)));
values.push_back(
std::make_unique<ast::ScalarConstructorExpression>(std::move(lit2)));
values.push_back(
std::make_unique<ast::ScalarConstructorExpression>(std::move(lit3)));
values.push_back(create<ast::ScalarConstructorExpression>(std::move(lit1)));
values.push_back(create<ast::ScalarConstructorExpression>(std::move(lit2)));
values.push_back(create<ast::ScalarConstructorExpression>(std::move(lit3)));
ary_values.push_back(std::make_unique<ast::TypeConstructorExpression>(
&vec, std::move(values)));
ary_values.push_back(
create<ast::TypeConstructorExpression>(&vec, std::move(values)));
}
ast::TypeConstructorExpression expr(&ary, std::move(ary_values));

View File

@ -43,9 +43,9 @@ namespace {
using WgslGeneratorImplTest = TestHelper;
TEST_F(WgslGeneratorImplTest, Emit_Function) {
auto body = std::make_unique<ast::BlockStatement>();
body->append(std::make_unique<ast::DiscardStatement>());
body->append(std::make_unique<ast::ReturnStatement>());
auto body = create<ast::BlockStatement>();
body->append(create<ast::DiscardStatement>());
body->append(create<ast::ReturnStatement>());
ast::type::VoidType void_type;
ast::Function func("my_func", {}, &void_type);
@ -62,17 +62,15 @@ TEST_F(WgslGeneratorImplTest, Emit_Function) {
}
TEST_F(WgslGeneratorImplTest, Emit_Function_WithParams) {
auto body = std::make_unique<ast::BlockStatement>();
body->append(std::make_unique<ast::DiscardStatement>());
body->append(std::make_unique<ast::ReturnStatement>());
auto body = create<ast::BlockStatement>();
body->append(create<ast::DiscardStatement>());
body->append(create<ast::ReturnStatement>());
ast::type::F32Type f32;
ast::type::I32Type i32;
ast::VariableList params;
params.push_back(
std::make_unique<ast::Variable>("a", ast::StorageClass::kNone, &f32));
params.push_back(
std::make_unique<ast::Variable>("b", ast::StorageClass::kNone, &i32));
params.push_back(create<ast::Variable>("a", ast::StorageClass::kNone, &f32));
params.push_back(create<ast::Variable>("b", ast::StorageClass::kNone, &i32));
ast::type::VoidType void_type;
ast::Function func("my_func", std::move(params), &void_type);
@ -89,14 +87,13 @@ TEST_F(WgslGeneratorImplTest, Emit_Function_WithParams) {
}
TEST_F(WgslGeneratorImplTest, Emit_Function_WithDecoration_WorkgroupSize) {
auto body = std::make_unique<ast::BlockStatement>();
body->append(std::make_unique<ast::DiscardStatement>());
body->append(std::make_unique<ast::ReturnStatement>());
auto body = create<ast::BlockStatement>();
body->append(create<ast::DiscardStatement>());
body->append(create<ast::ReturnStatement>());
ast::type::VoidType void_type;
ast::Function func("my_func", {}, &void_type);
func.add_decoration(
std::make_unique<ast::WorkgroupDecoration>(2u, 4u, 6u, Source{}));
func.add_decoration(create<ast::WorkgroupDecoration>(2u, 4u, 6u, Source{}));
func.set_body(std::move(body));
gen.increment_indent();
@ -111,14 +108,14 @@ TEST_F(WgslGeneratorImplTest, Emit_Function_WithDecoration_WorkgroupSize) {
}
TEST_F(WgslGeneratorImplTest, Emit_Function_WithDecoration_Stage) {
auto body = std::make_unique<ast::BlockStatement>();
body->append(std::make_unique<ast::DiscardStatement>());
body->append(std::make_unique<ast::ReturnStatement>());
auto body = create<ast::BlockStatement>();
body->append(create<ast::DiscardStatement>());
body->append(create<ast::ReturnStatement>());
ast::type::VoidType void_type;
ast::Function func("my_func", {}, &void_type);
func.add_decoration(std::make_unique<ast::StageDecoration>(
ast::PipelineStage::kFragment, Source{}));
func.add_decoration(
create<ast::StageDecoration>(ast::PipelineStage::kFragment, Source{}));
func.set_body(std::move(body));
gen.increment_indent();
@ -133,16 +130,15 @@ TEST_F(WgslGeneratorImplTest, Emit_Function_WithDecoration_Stage) {
}
TEST_F(WgslGeneratorImplTest, Emit_Function_WithDecoration_Multiple) {
auto body = std::make_unique<ast::BlockStatement>();
body->append(std::make_unique<ast::DiscardStatement>());
body->append(std::make_unique<ast::ReturnStatement>());
auto body = create<ast::BlockStatement>();
body->append(create<ast::DiscardStatement>());
body->append(create<ast::ReturnStatement>());
ast::type::VoidType void_type;
ast::Function func("my_func", {}, &void_type);
func.add_decoration(std::make_unique<ast::StageDecoration>(
ast::PipelineStage::kFragment, Source{}));
func.add_decoration(
std::make_unique<ast::WorkgroupDecoration>(2u, 4u, 6u, Source{}));
create<ast::StageDecoration>(ast::PipelineStage::kFragment, Source{}));
func.add_decoration(create<ast::WorkgroupDecoration>(2u, 4u, 6u, Source{}));
func.set_body(std::move(body));
gen.increment_indent();
@ -180,27 +176,23 @@ TEST_F(WgslGeneratorImplTest,
ast::StructMemberList members;
ast::StructMemberDecorationList a_deco;
a_deco.push_back(
std::make_unique<ast::StructMemberOffsetDecoration>(0, Source{}));
members.push_back(
std::make_unique<ast::StructMember>("d", &f32, std::move(a_deco)));
a_deco.push_back(create<ast::StructMemberOffsetDecoration>(0, Source{}));
members.push_back(create<ast::StructMember>("d", &f32, std::move(a_deco)));
ast::StructDecorationList s_decos;
s_decos.push_back(std::make_unique<ast::StructBlockDecoration>(Source{}));
s_decos.push_back(create<ast::StructBlockDecoration>(Source{}));
auto str =
std::make_unique<ast::Struct>(std::move(s_decos), std::move(members));
auto str = create<ast::Struct>(std::move(s_decos), std::move(members));
ast::type::StructType s("Data", std::move(str));
ast::type::AccessControlType ac(ast::AccessControl::kReadWrite, &s);
auto data_var =
std::make_unique<ast::DecoratedVariable>(std::make_unique<ast::Variable>(
"data", ast::StorageClass::kStorageBuffer, &ac));
auto data_var = create<ast::DecoratedVariable>(
create<ast::Variable>("data", ast::StorageClass::kStorageBuffer, &ac));
ast::VariableDecorationList decos;
decos.push_back(std::make_unique<ast::BindingDecoration>(0, Source{}));
decos.push_back(std::make_unique<ast::SetDecoration>(0, Source{}));
decos.push_back(create<ast::BindingDecoration>(0, Source{}));
decos.push_back(create<ast::SetDecoration>(0, Source{}));
data_var->set_decorations(std::move(decos));
mod.AddConstructedType(&s);
@ -210,20 +202,18 @@ TEST_F(WgslGeneratorImplTest,
{
ast::VariableList params;
auto func =
std::make_unique<ast::Function>("a", std::move(params), &void_type);
func->add_decoration(std::make_unique<ast::StageDecoration>(
ast::PipelineStage::kCompute, Source{}));
auto func = create<ast::Function>("a", std::move(params), &void_type);
func->add_decoration(
create<ast::StageDecoration>(ast::PipelineStage::kCompute, Source{}));
auto var = std::make_unique<ast::Variable>(
"v", ast::StorageClass::kFunction, &f32);
var->set_constructor(std::make_unique<ast::MemberAccessorExpression>(
std::make_unique<ast::IdentifierExpression>("data"),
std::make_unique<ast::IdentifierExpression>("d")));
auto var = create<ast::Variable>("v", ast::StorageClass::kFunction, &f32);
var->set_constructor(create<ast::MemberAccessorExpression>(
create<ast::IdentifierExpression>("data"),
create<ast::IdentifierExpression>("d")));
auto body = std::make_unique<ast::BlockStatement>();
body->append(std::make_unique<ast::VariableDeclStatement>(std::move(var)));
body->append(std::make_unique<ast::ReturnStatement>());
auto body = create<ast::BlockStatement>();
body->append(create<ast::VariableDeclStatement>(std::move(var)));
body->append(create<ast::ReturnStatement>());
func->set_body(std::move(body));
mod.AddFunction(std::move(func));
@ -231,20 +221,18 @@ TEST_F(WgslGeneratorImplTest,
{
ast::VariableList params;
auto func =
std::make_unique<ast::Function>("b", std::move(params), &void_type);
func->add_decoration(std::make_unique<ast::StageDecoration>(
ast::PipelineStage::kCompute, Source{}));
auto func = create<ast::Function>("b", std::move(params), &void_type);
func->add_decoration(
create<ast::StageDecoration>(ast::PipelineStage::kCompute, Source{}));
auto var = std::make_unique<ast::Variable>(
"v", ast::StorageClass::kFunction, &f32);
var->set_constructor(std::make_unique<ast::MemberAccessorExpression>(
std::make_unique<ast::IdentifierExpression>("data"),
std::make_unique<ast::IdentifierExpression>("d")));
auto var = create<ast::Variable>("v", ast::StorageClass::kFunction, &f32);
var->set_constructor(create<ast::MemberAccessorExpression>(
create<ast::IdentifierExpression>("data"),
create<ast::IdentifierExpression>("d")));
auto body = std::make_unique<ast::BlockStatement>();
body->append(std::make_unique<ast::VariableDeclStatement>(std::move(var)));
body->append(std::make_unique<ast::ReturnStatement>());
auto body = create<ast::BlockStatement>();
body->append(create<ast::VariableDeclStatement>(std::move(var)));
body->append(create<ast::ReturnStatement>());
func->set_body(std::move(body));
mod.AddFunction(std::move(func));

View File

@ -28,9 +28,9 @@ namespace {
using WgslGeneratorImplTest = TestHelper;
TEST_F(WgslGeneratorImplTest, Emit_If) {
auto cond = std::make_unique<ast::IdentifierExpression>("cond");
auto body = std::make_unique<ast::BlockStatement>();
body->append(std::make_unique<ast::DiscardStatement>());
auto cond = create<ast::IdentifierExpression>("cond");
auto body = create<ast::BlockStatement>();
body->append(create<ast::DiscardStatement>());
ast::IfStatement i(std::move(cond), std::move(body));
@ -44,17 +44,17 @@ TEST_F(WgslGeneratorImplTest, Emit_If) {
}
TEST_F(WgslGeneratorImplTest, Emit_IfWithElseIf) {
auto else_cond = std::make_unique<ast::IdentifierExpression>("else_cond");
auto else_body = std::make_unique<ast::BlockStatement>();
else_body->append(std::make_unique<ast::DiscardStatement>());
auto else_cond = create<ast::IdentifierExpression>("else_cond");
auto else_body = create<ast::BlockStatement>();
else_body->append(create<ast::DiscardStatement>());
ast::ElseStatementList elses;
elses.push_back(std::make_unique<ast::ElseStatement>(std::move(else_cond),
std::move(else_body)));
elses.push_back(
create<ast::ElseStatement>(std::move(else_cond), std::move(else_body)));
auto cond = std::make_unique<ast::IdentifierExpression>("cond");
auto body = std::make_unique<ast::BlockStatement>();
body->append(std::make_unique<ast::DiscardStatement>());
auto cond = create<ast::IdentifierExpression>("cond");
auto body = create<ast::BlockStatement>();
body->append(create<ast::DiscardStatement>());
ast::IfStatement i(std::move(cond), std::move(body));
i.set_else_statements(std::move(elses));
@ -71,15 +71,15 @@ TEST_F(WgslGeneratorImplTest, Emit_IfWithElseIf) {
}
TEST_F(WgslGeneratorImplTest, Emit_IfWithElse) {
auto else_body = std::make_unique<ast::BlockStatement>();
else_body->append(std::make_unique<ast::DiscardStatement>());
auto else_body = create<ast::BlockStatement>();
else_body->append(create<ast::DiscardStatement>());
ast::ElseStatementList elses;
elses.push_back(std::make_unique<ast::ElseStatement>(std::move(else_body)));
elses.push_back(create<ast::ElseStatement>(std::move(else_body)));
auto cond = std::make_unique<ast::IdentifierExpression>("cond");
auto body = std::make_unique<ast::BlockStatement>();
body->append(std::make_unique<ast::DiscardStatement>());
auto cond = create<ast::IdentifierExpression>("cond");
auto body = create<ast::BlockStatement>();
body->append(create<ast::DiscardStatement>());
ast::IfStatement i(std::move(cond), std::move(body));
i.set_else_statements(std::move(elses));
@ -96,22 +96,22 @@ TEST_F(WgslGeneratorImplTest, Emit_IfWithElse) {
}
TEST_F(WgslGeneratorImplTest, Emit_IfWithMultiple) {
auto else_cond = std::make_unique<ast::IdentifierExpression>("else_cond");
auto else_cond = create<ast::IdentifierExpression>("else_cond");
auto else_body = std::make_unique<ast::BlockStatement>();
else_body->append(std::make_unique<ast::DiscardStatement>());
auto else_body = create<ast::BlockStatement>();
else_body->append(create<ast::DiscardStatement>());
auto else_body_2 = std::make_unique<ast::BlockStatement>();
else_body_2->append(std::make_unique<ast::DiscardStatement>());
auto else_body_2 = create<ast::BlockStatement>();
else_body_2->append(create<ast::DiscardStatement>());
ast::ElseStatementList elses;
elses.push_back(std::make_unique<ast::ElseStatement>(std::move(else_cond),
std::move(else_body)));
elses.push_back(std::make_unique<ast::ElseStatement>(std::move(else_body_2)));
elses.push_back(
create<ast::ElseStatement>(std::move(else_cond), std::move(else_body)));
elses.push_back(create<ast::ElseStatement>(std::move(else_body_2)));
auto cond = std::make_unique<ast::IdentifierExpression>("cond");
auto body = std::make_unique<ast::BlockStatement>();
body->append(std::make_unique<ast::DiscardStatement>());
auto cond = create<ast::IdentifierExpression>("cond");
auto body = create<ast::BlockStatement>();
body->append(create<ast::DiscardStatement>());
ast::IfStatement i(std::move(cond), std::move(body));
i.set_else_statements(std::move(elses));

View File

@ -28,8 +28,8 @@ namespace {
using WgslGeneratorImplTest = TestHelper;
TEST_F(WgslGeneratorImplTest, Emit_Loop) {
auto body = std::make_unique<ast::BlockStatement>();
body->append(std::make_unique<ast::DiscardStatement>());
auto body = create<ast::BlockStatement>();
body->append(create<ast::DiscardStatement>());
ast::LoopStatement l(std::move(body), {});
gen.increment_indent();
@ -42,11 +42,11 @@ TEST_F(WgslGeneratorImplTest, Emit_Loop) {
}
TEST_F(WgslGeneratorImplTest, Emit_LoopWithContinuing) {
auto body = std::make_unique<ast::BlockStatement>();
body->append(std::make_unique<ast::DiscardStatement>());
auto body = create<ast::BlockStatement>();
body->append(create<ast::DiscardStatement>());
auto continuing = std::make_unique<ast::BlockStatement>();
continuing->append(std::make_unique<ast::DiscardStatement>());
auto continuing = create<ast::BlockStatement>();
continuing->append(create<ast::DiscardStatement>());
ast::LoopStatement l(std::move(body), std::move(continuing));

View File

@ -28,8 +28,8 @@ namespace {
using WgslGeneratorImplTest = TestHelper;
TEST_F(WgslGeneratorImplTest, EmitExpression_MemberAccessor) {
auto str = std::make_unique<ast::IdentifierExpression>("str");
auto mem = std::make_unique<ast::IdentifierExpression>("mem");
auto str = create<ast::IdentifierExpression>("str");
auto mem = create<ast::IdentifierExpression>("mem");
ast::MemberAccessorExpression expr(std::move(str), std::move(mem));

View File

@ -38,7 +38,7 @@ TEST_F(WgslGeneratorImplTest, Emit_Return) {
}
TEST_F(WgslGeneratorImplTest, Emit_ReturnWithValue) {
auto expr = std::make_unique<ast::IdentifierExpression>("expr");
auto expr = create<ast::IdentifierExpression>("expr");
ast::ReturnStatement r(std::move(expr));
gen.increment_indent();

View File

@ -32,26 +32,26 @@ namespace {
using WgslGeneratorImplTest = TestHelper;
TEST_F(WgslGeneratorImplTest, Emit_Switch) {
auto def = std::make_unique<ast::CaseStatement>();
auto def_body = std::make_unique<ast::BlockStatement>();
def_body->append(std::make_unique<ast::BreakStatement>());
auto def = create<ast::CaseStatement>();
auto def_body = create<ast::BlockStatement>();
def_body->append(create<ast::BreakStatement>());
def->set_body(std::move(def_body));
ast::type::I32Type i32;
ast::CaseSelectorList case_val;
case_val.push_back(std::make_unique<ast::SintLiteral>(&i32, 5));
case_val.push_back(create<ast::SintLiteral>(&i32, 5));
auto case_body = std::make_unique<ast::BlockStatement>();
case_body->append(std::make_unique<ast::BreakStatement>());
auto case_body = create<ast::BlockStatement>();
case_body->append(create<ast::BreakStatement>());
auto case_stmt = std::make_unique<ast::CaseStatement>(std::move(case_val),
std::move(case_body));
auto case_stmt =
create<ast::CaseStatement>(std::move(case_val), std::move(case_body));
ast::CaseStatementList body;
body.push_back(std::move(case_stmt));
body.push_back(std::move(def));
auto cond = std::make_unique<ast::IdentifierExpression>("cond");
auto cond = create<ast::IdentifierExpression>("cond");
ast::SwitchStatement s(std::move(cond), std::move(body));
gen.increment_indent();

View File

@ -32,8 +32,8 @@ using WgslGeneratorImplTest = TestHelper;
TEST_F(WgslGeneratorImplTest, Generate) {
ast::type::VoidType void_type;
mod.AddFunction(std::make_unique<ast::Function>(
"my_func", ast::VariableList{}, &void_type));
mod.AddFunction(
create<ast::Function>("my_func", ast::VariableList{}, &void_type));
ASSERT_TRUE(gen.Generate(mod)) << gen.error();
EXPECT_EQ(gen.result(), R"(fn my_func() -> void {

View File

@ -64,7 +64,7 @@ TEST_F(WgslGeneratorImplTest, EmitType_Array) {
TEST_F(WgslGeneratorImplTest, EmitType_Array_Decoration) {
ast::type::BoolType b;
ast::ArrayDecorationList decos;
decos.push_back(std::make_unique<ast::StrideDecoration>(16u, Source{}));
decos.push_back(create<ast::StrideDecoration>(16u, Source{}));
ast::type::ArrayType a(&b, 4);
a.set_decorations(std::move(decos));
@ -76,8 +76,8 @@ TEST_F(WgslGeneratorImplTest, EmitType_Array_Decoration) {
TEST_F(WgslGeneratorImplTest, EmitType_Array_MultipleDecorations) {
ast::type::BoolType b;
ast::ArrayDecorationList decos;
decos.push_back(std::make_unique<ast::StrideDecoration>(16u, Source{}));
decos.push_back(std::make_unique<ast::StrideDecoration>(32u, Source{}));
decos.push_back(create<ast::StrideDecoration>(16u, Source{}));
decos.push_back(create<ast::StrideDecoration>(32u, Source{}));
ast::type::ArrayType a(&b, 4);
a.set_decorations(std::move(decos));
@ -136,16 +136,14 @@ TEST_F(WgslGeneratorImplTest, EmitType_Struct) {
ast::type::F32Type f32;
ast::StructMemberList members;
members.push_back(std::make_unique<ast::StructMember>(
"a", &i32, ast::StructMemberDecorationList{}));
members.push_back(
create<ast::StructMember>("a", &i32, ast::StructMemberDecorationList{}));
ast::StructMemberDecorationList b_deco;
b_deco.push_back(
std::make_unique<ast::StructMemberOffsetDecoration>(4, Source{}));
members.push_back(
std::make_unique<ast::StructMember>("b", &f32, std::move(b_deco)));
b_deco.push_back(create<ast::StructMemberOffsetDecoration>(4, Source{}));
members.push_back(create<ast::StructMember>("b", &f32, std::move(b_deco)));
auto str = std::make_unique<ast::Struct>();
auto str = create<ast::Struct>();
str->set_members(std::move(members));
ast::type::StructType s("S", std::move(str));
@ -159,16 +157,14 @@ TEST_F(WgslGeneratorImplTest, EmitType_StructDecl) {
ast::type::F32Type f32;
ast::StructMemberList members;
members.push_back(std::make_unique<ast::StructMember>(
"a", &i32, ast::StructMemberDecorationList{}));
members.push_back(
create<ast::StructMember>("a", &i32, ast::StructMemberDecorationList{}));
ast::StructMemberDecorationList b_deco;
b_deco.push_back(
std::make_unique<ast::StructMemberOffsetDecoration>(4, Source{}));
members.push_back(
std::make_unique<ast::StructMember>("b", &f32, std::move(b_deco)));
b_deco.push_back(create<ast::StructMemberOffsetDecoration>(4, Source{}));
members.push_back(create<ast::StructMember>("b", &f32, std::move(b_deco)));
auto str = std::make_unique<ast::Struct>();
auto str = create<ast::Struct>();
str->set_members(std::move(members));
ast::type::StructType s("S", std::move(str));
@ -187,20 +183,17 @@ TEST_F(WgslGeneratorImplTest, EmitType_Struct_WithDecoration) {
ast::type::F32Type f32;
ast::StructMemberList members;
members.push_back(std::make_unique<ast::StructMember>(
"a", &i32, ast::StructMemberDecorationList{}));
members.push_back(
create<ast::StructMember>("a", &i32, ast::StructMemberDecorationList{}));
ast::StructMemberDecorationList b_deco;
b_deco.push_back(
std::make_unique<ast::StructMemberOffsetDecoration>(4, Source{}));
members.push_back(
std::make_unique<ast::StructMember>("b", &f32, std::move(b_deco)));
b_deco.push_back(create<ast::StructMemberOffsetDecoration>(4, Source{}));
members.push_back(create<ast::StructMember>("b", &f32, std::move(b_deco)));
ast::StructDecorationList decos;
decos.push_back(std::make_unique<ast::StructBlockDecoration>(Source{}));
decos.push_back(create<ast::StructBlockDecoration>(Source{}));
auto str =
std::make_unique<ast::Struct>(std::move(decos), std::move(members));
auto str = create<ast::Struct>(std::move(decos), std::move(members));
ast::type::StructType s("S", std::move(str));

View File

@ -38,7 +38,7 @@ using WgslUnaryOpTest = TestParamHelper<UnaryOpData>;
TEST_P(WgslUnaryOpTest, Emit) {
auto params = GetParam();
auto expr = std::make_unique<ast::IdentifierExpression>("expr");
auto expr = create<ast::IdentifierExpression>("expr");
ast::UnaryOpExpression op(params.op, std::move(expr));
ASSERT_TRUE(gen.EmitExpression(&op)) << gen.error();

View File

@ -32,8 +32,7 @@ using WgslGeneratorImplTest = TestHelper;
TEST_F(WgslGeneratorImplTest, Emit_VariableDeclStatement) {
ast::type::F32Type f32;
auto var =
std::make_unique<ast::Variable>("a", ast::StorageClass::kNone, &f32);
auto var = create<ast::Variable>("a", ast::StorageClass::kNone, &f32);
ast::VariableDeclStatement stmt(std::move(var));
@ -48,8 +47,7 @@ TEST_F(WgslGeneratorImplTest, Emit_VariableDeclStatement_Function) {
// storage class. Rely on defaulting.
// https://github.com/gpuweb/gpuweb/issues/654
ast::type::F32Type f32;
auto var =
std::make_unique<ast::Variable>("a", ast::StorageClass::kFunction, &f32);
auto var = create<ast::Variable>("a", ast::StorageClass::kFunction, &f32);
ast::VariableDeclStatement stmt(std::move(var));
@ -61,8 +59,7 @@ TEST_F(WgslGeneratorImplTest, Emit_VariableDeclStatement_Function) {
TEST_F(WgslGeneratorImplTest, Emit_VariableDeclStatement_Private) {
ast::type::F32Type f32;
auto var =
std::make_unique<ast::Variable>("a", ast::StorageClass::kPrivate, &f32);
auto var = create<ast::Variable>("a", ast::StorageClass::kPrivate, &f32);
ast::VariableDeclStatement stmt(std::move(var));

View File

@ -55,7 +55,7 @@ TEST_F(WgslGeneratorImplTest, EmitVariable_Decorated) {
ast::type::F32Type f32;
ast::VariableDecorationList decos;
decos.push_back(std::make_unique<ast::LocationDecoration>(2, Source{}));
decos.push_back(create<ast::LocationDecoration>(2, Source{}));
ast::DecoratedVariable dv;
dv.set_name("a");
@ -71,12 +71,12 @@ TEST_F(WgslGeneratorImplTest, EmitVariable_Decorated_Multiple) {
ast::type::F32Type f32;
ast::VariableDecorationList decos;
decos.push_back(std::make_unique<ast::BuiltinDecoration>(
ast::Builtin::kPosition, Source{}));
decos.push_back(std::make_unique<ast::BindingDecoration>(0, Source{}));
decos.push_back(std::make_unique<ast::SetDecoration>(1, Source{}));
decos.push_back(std::make_unique<ast::LocationDecoration>(2, Source{}));
decos.push_back(std::make_unique<ast::ConstantIdDecoration>(42, Source{}));
decos.push_back(
create<ast::BuiltinDecoration>(ast::Builtin::kPosition, Source{}));
decos.push_back(create<ast::BindingDecoration>(0, Source{}));
decos.push_back(create<ast::SetDecoration>(1, Source{}));
decos.push_back(create<ast::LocationDecoration>(2, Source{}));
decos.push_back(create<ast::ConstantIdDecoration>(42, Source{}));
ast::DecoratedVariable dv;
dv.set_name("a");
@ -91,7 +91,7 @@ TEST_F(WgslGeneratorImplTest, EmitVariable_Decorated_Multiple) {
}
TEST_F(WgslGeneratorImplTest, EmitVariable_Constructor) {
auto ident = std::make_unique<ast::IdentifierExpression>("initializer");
auto ident = create<ast::IdentifierExpression>("initializer");
ast::type::F32Type f32;
ast::Variable v("a", ast::StorageClass::kNone, &f32);
@ -103,7 +103,7 @@ TEST_F(WgslGeneratorImplTest, EmitVariable_Constructor) {
}
TEST_F(WgslGeneratorImplTest, EmitVariable_Const) {
auto ident = std::make_unique<ast::IdentifierExpression>("initializer");
auto ident = create<ast::IdentifierExpression>("initializer");
ast::type::F32Type f32;
ast::Variable v("a", ast::StorageClass::kNone, &f32);

View File

@ -15,6 +15,9 @@
#ifndef SRC_WRITER_WGSL_TEST_HELPER_H_
#define SRC_WRITER_WGSL_TEST_HELPER_H_
#include <memory>
#include <utility>
#include "gtest/gtest.h"
#include "src/ast/module.h"
#include "src/context.h"
@ -26,13 +29,20 @@ namespace writer {
namespace wgsl {
/// Helper class for testing
template <typename T>
class TestHelperBase : public T {
template <typename BASE>
class TestHelperBase : public BASE {
public:
TestHelperBase() : td(&ctx, &mod), gen(&ctx) {}
~TestHelperBase() = default;
/// @return a `std::unique_ptr` to a new `T` constructed with `args`
/// @param args the arguments to forward to the constructor for `T`
template <typename T, typename... ARGS>
std::unique_ptr<T> create(ARGS&&... args) {
return std::make_unique<T>(std::forward<ARGS>(args)...);
}
/// The context
Context ctx;
/// The module