ast: Add body parameter to ast::Function constructors

In a near-future change, AST nodes, such as ast::BlockStatement will no longer
be std::unique_ptrs, and will have to be constructed and owned by an external
class. This means AST nodes can no longer allocate default child nodes.

Bug: tint:322
Change-Id: Iddb5605b9bc0de80ad2710ced0e429f89410af2f
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/32675
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:13:04 +00:00
committed by Commit Bot service account
parent 9a31c641e2
commit b29b09fba6
28 changed files with 545 additions and 567 deletions

View File

@@ -45,11 +45,11 @@ TEST_F(ValidateFunctionTest, VoidFunctionEndWithoutReturnStatement_Pass) {
ast::VariableList params;
ast::type::VoidType void_type;
auto func = create<ast::Function>(Source{Source::Location{12, 34}}, "func",
std::move(params), &void_type);
auto body = create<ast::BlockStatement>();
body->append(create<ast::VariableDeclStatement>(std::move(var)));
func->set_body(std::move(body));
auto func =
create<ast::Function>(Source{Source::Location{12, 34}}, "func",
std::move(params), &void_type, std::move(body));
func->add_decoration(
create<ast::StageDecoration>(ast::PipelineStage::kVertex, Source{}));
mod()->AddFunction(std::move(func));
@@ -65,7 +65,8 @@ TEST_F(ValidateFunctionTest,
ast::type::VoidType void_type;
ast::VariableList params;
auto func = create<ast::Function>(Source{Source::Location{12, 34}}, "func",
std::move(params), &void_type);
std::move(params), &void_type,
create<ast::BlockStatement>());
func->add_decoration(
create<ast::StageDecoration>(ast::PipelineStage::kVertex, Source{}));
mod()->AddFunction(std::move(func));
@@ -84,11 +85,10 @@ TEST_F(ValidateFunctionTest, FunctionEndWithoutReturnStatement_Fail) {
ast::VariableList params;
ast::type::VoidType void_type;
auto func = create<ast::Function>(Source{Source::Location{12, 34}}, "func",
std::move(params), &i32);
auto body = create<ast::BlockStatement>();
body->append(create<ast::VariableDeclStatement>(std::move(var)));
func->set_body(std::move(body));
auto func = create<ast::Function>(Source{Source::Location{12, 34}}, "func",
std::move(params), &i32, std::move(body));
mod()->AddFunction(std::move(func));
EXPECT_TRUE(td()->Determine()) << td()->error();
@@ -104,7 +104,8 @@ TEST_F(ValidateFunctionTest, FunctionEndWithoutReturnStatementEmptyBody_Fail) {
ast::type::I32Type i32;
ast::VariableList params;
auto func = create<ast::Function>(Source{Source::Location{12, 34}}, "func",
std::move(params), &i32);
std::move(params), &i32,
create<ast::BlockStatement>());
mod()->AddFunction(std::move(func));
EXPECT_TRUE(td()->Determine()) << td()->error();
@@ -120,10 +121,10 @@ TEST_F(ValidateFunctionTest, FunctionTypeMustMatchReturnStatementType_Pass) {
ast::type::VoidType void_type;
ast::VariableList params;
auto func = create<ast::Function>("func", std::move(params), &void_type);
auto body = create<ast::BlockStatement>();
body->append(create<ast::ReturnStatement>());
func->set_body(std::move(body));
auto func = create<ast::Function>("func", std::move(params), &void_type,
std::move(body));
func->add_decoration(
create<ast::StageDecoration>(ast::PipelineStage::kVertex, Source{}));
mod()->AddFunction(std::move(func));
@@ -137,14 +138,14 @@ TEST_F(ValidateFunctionTest, FunctionTypeMustMatchReturnStatementType_fail) {
ast::type::VoidType void_type;
ast::type::I32Type i32;
ast::VariableList params;
auto func = create<ast::Function>("func", std::move(params), &void_type);
auto body = create<ast::BlockStatement>();
auto return_expr = create<ast::ScalarConstructorExpression>(
create<ast::SintLiteral>(&i32, 2));
body->append(create<ast::ReturnStatement>(Source{Source::Location{12, 34}},
std::move(return_expr)));
func->set_body(std::move(body));
auto func = create<ast::Function>("func", std::move(params), &void_type,
std::move(body));
mod()->AddFunction(std::move(func));
EXPECT_TRUE(td()->Determine()) << td()->error();
@@ -160,14 +161,14 @@ TEST_F(ValidateFunctionTest, FunctionTypeMustMatchReturnStatementTypeF32_fail) {
ast::type::I32Type i32;
ast::type::F32Type f32;
ast::VariableList params;
auto func = create<ast::Function>("func", std::move(params), &f32);
auto body = create<ast::BlockStatement>();
auto return_expr = create<ast::ScalarConstructorExpression>(
create<ast::SintLiteral>(&i32, 2));
body->append(create<ast::ReturnStatement>(Source{Source::Location{12, 34}},
std::move(return_expr)));
func->set_body(std::move(body));
auto func =
create<ast::Function>("func", std::move(params), &f32, std::move(body));
mod()->AddFunction(std::move(func));
EXPECT_TRUE(td()->Determine()) << td()->error();
@@ -185,23 +186,23 @@ TEST_F(ValidateFunctionTest, FunctionNamesMustBeUnique_fail) {
ast::type::I32Type i32;
ast::VariableList params;
auto func = create<ast::Function>("func", std::move(params), &i32);
auto body = create<ast::BlockStatement>();
auto return_expr = create<ast::ScalarConstructorExpression>(
create<ast::SintLiteral>(&i32, 2));
body->append(create<ast::ReturnStatement>(std::move(return_expr)));
func->set_body(std::move(body));
auto func =
create<ast::Function>("func", std::move(params), &i32, std::move(body));
ast::VariableList params_copy;
auto func_copy = create<ast::Function>(Source{Source::Location{12, 34}},
"func", std::move(params_copy), &i32);
auto body_copy = create<ast::BlockStatement>();
auto return_expr_copy = create<ast::ScalarConstructorExpression>(
create<ast::SintLiteral>(&i32, 2));
body_copy->append(create<ast::ReturnStatement>(std::move(return_expr_copy)));
func_copy->set_body(std::move(body_copy));
auto func_copy =
create<ast::Function>(Source{Source::Location{12, 34}}, "func",
std::move(params_copy), &i32, std::move(body_copy));
mod()->AddFunction(std::move(func));
mod()->AddFunction(std::move(func_copy));
@@ -221,11 +222,11 @@ TEST_F(ValidateFunctionTest, RecursionIsNotAllowed_Fail) {
Source{Source::Location{12, 34}},
create<ast::IdentifierExpression>("func"), std::move(call_params));
ast::VariableList params0;
auto func0 = create<ast::Function>("func", std::move(params0), &f32);
auto body0 = create<ast::BlockStatement>();
body0->append(create<ast::CallStatement>(std::move(call_expr)));
body0->append(create<ast::ReturnStatement>());
func0->set_body(std::move(body0));
auto func0 =
create<ast::Function>("func", std::move(params0), &f32, std::move(body0));
mod()->AddFunction(std::move(func0));
EXPECT_TRUE(td()->Determine()) << td()->error();
@@ -243,14 +244,14 @@ TEST_F(ValidateFunctionTest, RecursionIsNotAllowedExpr_Fail) {
create<ast::IdentifierExpression>("func"), std::move(call_params));
var->set_constructor(std::move(call_expr));
ast::VariableList params0;
auto func0 = create<ast::Function>("func", std::move(params0), &i32);
auto body0 = create<ast::BlockStatement>();
body0->append(create<ast::VariableDeclStatement>(std::move(var)));
auto return_expr = create<ast::ScalarConstructorExpression>(
create<ast::SintLiteral>(&i32, 2));
body0->append(create<ast::ReturnStatement>(std::move(return_expr)));
func0->set_body(std::move(body0));
auto func0 =
create<ast::Function>("func", std::move(params0), &i32, std::move(body0));
mod()->AddFunction(std::move(func0));
EXPECT_TRUE(td()->Determine()) << td()->error();
@@ -263,14 +264,14 @@ TEST_F(ValidateFunctionTest, Function_WithPipelineStage_NotVoid_Fail) {
// fn vtx_main() -> i32 { return 0; }
ast::type::I32Type i32;
ast::VariableList params;
auto func = create<ast::Function>(Source{Source::Location{12, 34}},
"vtx_main", std::move(params), &i32);
auto return_expr = create<ast::ScalarConstructorExpression>(
create<ast::SintLiteral>(&i32, 0));
auto body = create<ast::BlockStatement>();
body->append(create<ast::ReturnStatement>(std::move(return_expr)));
func->set_body(std::move(body));
auto func =
create<ast::Function>(Source{Source::Location{12, 34}}, "vtx_main",
std::move(params), &i32, std::move(body));
func->add_decoration(
create<ast::StageDecoration>(ast::PipelineStage::kVertex, Source{}));
@@ -288,11 +289,11 @@ TEST_F(ValidateFunctionTest, Function_WithPipelineStage_WithParams_Fail) {
ast::type::VoidType void_type;
ast::VariableList params;
params.push_back(create<ast::Variable>("a", ast::StorageClass::kNone, &i32));
auto func = create<ast::Function>(Source{Source::Location{12, 34}},
"vtx_func", std::move(params), &void_type);
auto body = create<ast::BlockStatement>();
body->append(create<ast::ReturnStatement>());
func->set_body(std::move(body));
auto func =
create<ast::Function>(Source{Source::Location{12, 34}}, "vtx_func",
std::move(params), &void_type, std::move(body));
func->add_decoration(
create<ast::StageDecoration>(ast::PipelineStage::kVertex, Source{}));
@@ -310,11 +311,11 @@ TEST_F(ValidateFunctionTest, PipelineStage_MustBeUnique_Fail) {
// fn main() -> void { return; }
ast::type::VoidType void_type;
ast::VariableList params;
auto func = create<ast::Function>(Source{Source::Location{12, 34}}, "main",
std::move(params), &void_type);
auto body = create<ast::BlockStatement>();
body->append(create<ast::ReturnStatement>());
func->set_body(std::move(body));
auto func =
create<ast::Function>(Source{Source::Location{12, 34}}, "main",
std::move(params), &void_type, std::move(body));
func->add_decoration(
create<ast::StageDecoration>(ast::PipelineStage::kVertex, Source{}));
func->add_decoration(
@@ -332,10 +333,10 @@ TEST_F(ValidateFunctionTest, OnePipelineStageFunctionMustBePresent_Pass) {
// fn vtx_func() -> void { return; }
ast::type::VoidType void_type;
ast::VariableList params;
auto func = create<ast::Function>("vtx_func", std::move(params), &void_type);
auto body = create<ast::BlockStatement>();
body->append(create<ast::ReturnStatement>());
func->set_body(std::move(body));
auto func = create<ast::Function>("vtx_func", std::move(params), &void_type,
std::move(body));
func->add_decoration(
create<ast::StageDecoration>(ast::PipelineStage::kVertex, Source{}));
mod()->AddFunction(std::move(func));
@@ -348,10 +349,10 @@ TEST_F(ValidateFunctionTest, OnePipelineStageFunctionMustBePresent_Fail) {
// fn vtx_func() -> void { return; }
ast::type::VoidType void_type;
ast::VariableList params;
auto func = create<ast::Function>("vtx_func", std::move(params), &void_type);
auto body = create<ast::BlockStatement>();
body->append(create<ast::ReturnStatement>());
func->set_body(std::move(body));
auto func = create<ast::Function>("vtx_func", std::move(params), &void_type,
std::move(body));
mod()->AddFunction(std::move(func));
EXPECT_TRUE(td()->Determine()) << td()->error();

View File

@@ -297,12 +297,12 @@ TEST_F(ValidatorTest, UsingUndefinedVariableGlobalVariable_Fail) {
create<ast::FloatLiteral>(&f32, 3.14f));
ast::VariableList params;
auto func = create<ast::Function>("my_func", std::move(params), &f32);
auto body = create<ast::BlockStatement>();
body->append(create<ast::AssignmentStatement>(
Source{Source::Location{12, 34}}, std::move(lhs), std::move(rhs)));
func->set_body(std::move(body));
auto func = create<ast::Function>("my_func", std::move(params), &f32,
std::move(body));
mod()->AddFunction(std::move(func));
EXPECT_FALSE(v()->Validate(mod()));
@@ -329,13 +329,13 @@ TEST_F(ValidatorTest, UsingUndefinedVariableGlobalVariable_Pass) {
create<ast::FloatLiteral>(&f32, 3.14f));
ast::VariableList params;
auto func = create<ast::Function>("my_func", std::move(params), &void_type);
auto body = create<ast::BlockStatement>();
body->append(create<ast::AssignmentStatement>(
Source{Source::Location{12, 34}}, std::move(lhs), std::move(rhs)));
body->append(create<ast::ReturnStatement>());
func->set_body(std::move(body));
auto func = create<ast::Function>("my_func", std::move(params), &void_type,
std::move(body));
func->add_decoration(
create<ast::StageDecoration>(ast::PipelineStage::kVertex, Source{}));
mod()->AddFunction(std::move(func));
@@ -507,11 +507,11 @@ TEST_F(ValidatorTest, GlobalVariableFunctionVariableNotUnique_Fail) {
var->set_constructor(create<ast::ScalarConstructorExpression>(
create<ast::FloatLiteral>(&f32, 2.0)));
ast::VariableList params;
auto func = create<ast::Function>("my_func", std::move(params), &void_type);
auto body = create<ast::BlockStatement>();
body->append(create<ast::VariableDeclStatement>(
Source{Source::Location{12, 34}}, std::move(var)));
func->set_body(std::move(body));
auto func = create<ast::Function>("my_func", std::move(params), &void_type,
std::move(body));
auto* func_ptr = func.get();
mod()->AddFunction(std::move(func));
@@ -538,12 +538,12 @@ TEST_F(ValidatorTest, RedeclaredIndentifier_Fail) {
create<ast::FloatLiteral>(&f32, 0.1)));
ast::VariableList params;
auto func = create<ast::Function>("my_func", std::move(params), &void_type);
auto body = create<ast::BlockStatement>();
body->append(create<ast::VariableDeclStatement>(std::move(var)));
body->append(create<ast::VariableDeclStatement>(
Source{Source::Location{12, 34}}, std::move(var_a_float)));
func->set_body(std::move(body));
auto func = create<ast::Function>("my_func", std::move(params), &void_type,
std::move(body));
auto* func_ptr = func.get();
mod()->AddFunction(std::move(func));
@@ -631,20 +631,20 @@ TEST_F(ValidatorTest, RedeclaredIdentifierDifferentFunctions_Pass) {
create<ast::FloatLiteral>(&f32, 1.0)));
ast::VariableList params0;
auto func0 = create<ast::Function>("func0", std::move(params0), &void_type);
auto body0 = create<ast::BlockStatement>();
body0->append(create<ast::VariableDeclStatement>(
Source{Source::Location{12, 34}}, std::move(var0)));
body0->append(create<ast::ReturnStatement>());
func0->set_body(std::move(body0));
auto func0 = create<ast::Function>("func0", std::move(params0), &void_type,
std::move(body0));
ast::VariableList params1;
auto func1 = create<ast::Function>("func1", std::move(params1), &void_type);
auto body1 = create<ast::BlockStatement>();
body1->append(create<ast::VariableDeclStatement>(
Source{Source::Location{13, 34}}, std::move(var1)));
body1->append(create<ast::ReturnStatement>());
func1->set_body(std::move(body1));
auto func1 = create<ast::Function>("func1", std::move(params1), &void_type,
std::move(body1));
func1->add_decoration(
create<ast::StageDecoration>(ast::PipelineStage::kVertex, Source{}));