ast::Builder: Add shortcuts to the Program methods

This builder will be merged into ProgramBuilder, where these will become methods.
To breakup this change, perform the refactoring as a separate change.

Bug: tint:390
Change-Id: I2c9151cd9f198e99d88eaf296dd994293df6c425
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/38720
Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
Ben Clayton
2021-01-26 16:57:10 +00:00
parent 8d391f7a10
commit 1f7e18bbc0
29 changed files with 496 additions and 498 deletions

View File

@@ -375,7 +375,7 @@ TEST_F(ValidateControlBlockTest, SwitchCaseAlias_Pass) {
create<ast::VariableDeclStatement>(var),
create<ast::SwitchStatement>(Expr("a"), body),
});
mod->AST().AddConstructedType(my_int);
AST().AddConstructedType(my_int);
EXPECT_TRUE(td()->DetermineStatements(block)) << td()->error();

View File

@@ -49,7 +49,7 @@ TEST_F(ValidateFunctionTest, VoidFunctionEndWithoutReturnStatement_Pass) {
ast::FunctionDecorationList{
create<ast::StageDecoration>(ast::PipelineStage::kVertex),
});
mod->AST().Functions().Add(func);
AST().Functions().Add(func);
EXPECT_TRUE(td()->Determine()) << td()->error();
@@ -68,7 +68,7 @@ TEST_F(ValidateFunctionTest,
ast::FunctionDecorationList{
create<ast::StageDecoration>(ast::PipelineStage::kVertex),
});
mod->AST().Functions().Add(func);
AST().Functions().Add(func);
EXPECT_TRUE(td()->Determine()) << td()->error();
@@ -89,7 +89,7 @@ TEST_F(ValidateFunctionTest, FunctionEndWithoutReturnStatement_Fail) {
create<ast::VariableDeclStatement>(var),
},
ast::FunctionDecorationList{});
mod->AST().Functions().Add(func);
AST().Functions().Add(func);
EXPECT_TRUE(td()->Determine()) << td()->error();
@@ -105,7 +105,7 @@ TEST_F(ValidateFunctionTest, FunctionEndWithoutReturnStatementEmptyBody_Fail) {
auto* func =
Func(Source{Source::Location{12, 34}}, "func", ast::VariableList{},
ty.i32(), ast::StatementList{}, ast::FunctionDecorationList{});
mod->AST().Functions().Add(func);
AST().Functions().Add(func);
EXPECT_TRUE(td()->Determine()) << td()->error();
@@ -127,14 +127,13 @@ TEST_F(ValidateFunctionTest, FunctionTypeMustMatchReturnStatementType_Pass) {
ast::FunctionDecorationList{
create<ast::StageDecoration>(ast::PipelineStage::kVertex),
});
mod->AST().Functions().Add(func);
AST().Functions().Add(func);
EXPECT_TRUE(td()->DetermineFunctions(mod->AST().Functions()))
<< td()->error();
EXPECT_TRUE(td()->DetermineFunctions(AST().Functions())) << td()->error();
ValidatorImpl& v = Build();
EXPECT_TRUE(v.ValidateFunctions(mod->AST().Functions())) << v.error();
EXPECT_TRUE(v.ValidateFunctions(AST().Functions())) << v.error();
}
TEST_F(ValidateFunctionTest, FunctionTypeMustMatchReturnStatementType_fail) {
@@ -145,7 +144,7 @@ TEST_F(ValidateFunctionTest, FunctionTypeMustMatchReturnStatementType_fail) {
Source{Source::Location{12, 34}}, Expr(2)),
},
ast::FunctionDecorationList{});
mod->AST().Functions().Add(func);
AST().Functions().Add(func);
EXPECT_TRUE(td()->Determine()) << td()->error();
@@ -166,7 +165,7 @@ TEST_F(ValidateFunctionTest, FunctionTypeMustMatchReturnStatementTypeF32_fail) {
Source{Source::Location{12, 34}}, Expr(2)),
},
ast::FunctionDecorationList{});
mod->AST().Functions().Add(func);
AST().Functions().Add(func);
EXPECT_TRUE(td()->Determine()) << td()->error();
@@ -195,8 +194,8 @@ TEST_F(ValidateFunctionTest, FunctionNamesMustBeUnique_fail) {
},
ast::FunctionDecorationList{});
mod->AST().Functions().Add(func);
mod->AST().Functions().Add(func_copy);
AST().Functions().Add(func);
AST().Functions().Add(func_copy);
EXPECT_TRUE(td()->Determine()) << td()->error();
@@ -218,7 +217,7 @@ TEST_F(ValidateFunctionTest, RecursionIsNotAllowed_Fail) {
create<ast::ReturnStatement>(),
},
ast::FunctionDecorationList{});
mod->AST().Functions().Add(func0);
AST().Functions().Add(func0);
EXPECT_TRUE(td()->Determine()) << td()->error();
@@ -242,7 +241,7 @@ TEST_F(ValidateFunctionTest, RecursionIsNotAllowedExpr_Fail) {
create<ast::ReturnStatement>(Expr(2)),
},
ast::FunctionDecorationList{});
mod->AST().Functions().Add(func0);
AST().Functions().Add(func0);
EXPECT_TRUE(td()->Determine()) << td()->error();
@@ -265,7 +264,7 @@ TEST_F(ValidateFunctionTest, Function_WithPipelineStage_NotVoid_Fail) {
create<ast::StageDecoration>(ast::PipelineStage::kVertex),
});
mod->AST().Functions().Add(func);
AST().Functions().Add(func);
EXPECT_TRUE(td()->Determine()) << td()->error();
ValidatorImpl& v = Build();
@@ -290,7 +289,7 @@ TEST_F(ValidateFunctionTest, Function_WithPipelineStage_WithParams_Fail) {
create<ast::StageDecoration>(ast::PipelineStage::kVertex),
});
mod->AST().Functions().Add(func);
AST().Functions().Add(func);
EXPECT_TRUE(td()->Determine()) << td()->error();
ValidatorImpl& v = Build();
@@ -315,7 +314,7 @@ TEST_F(ValidateFunctionTest, PipelineStage_MustBeUnique_Fail) {
create<ast::StageDecoration>(ast::PipelineStage::kFragment),
});
mod->AST().Functions().Add(func);
AST().Functions().Add(func);
EXPECT_TRUE(td()->Determine()) << td()->error();
ValidatorImpl& v = Build();
@@ -337,7 +336,7 @@ TEST_F(ValidateFunctionTest, OnePipelineStageFunctionMustBePresent_Pass) {
ast::FunctionDecorationList{
create<ast::StageDecoration>(ast::PipelineStage::kVertex),
});
mod->AST().Functions().Add(func);
AST().Functions().Add(func);
EXPECT_TRUE(td()->Determine()) << td()->error();
@@ -353,7 +352,7 @@ TEST_F(ValidateFunctionTest, OnePipelineStageFunctionMustBePresent_Fail) {
create<ast::ReturnStatement>(),
},
ast::FunctionDecorationList{});
mod->AST().Functions().Add(func);
AST().Functions().Add(func);
EXPECT_TRUE(td()->Determine()) << td()->error();

View File

@@ -324,21 +324,20 @@ TEST_F(ValidatorTest, AssignIncompatibleTypesInBlockStatement_Fail) {
TEST_F(ValidatorTest, GlobalVariableWithStorageClass_Pass) {
// var<in> gloabl_var: f32;
mod->AST().AddGlobalVariable(Var(
Source{Source::Location{12, 34}}, "global_var", ast::StorageClass::kInput,
ty.f32(), nullptr, ast::VariableDecorationList{}));
AST().AddGlobalVariable(Var(Source{Source::Location{12, 34}}, "global_var",
ast::StorageClass::kInput, ty.f32(), nullptr,
ast::VariableDecorationList{}));
ValidatorImpl& v = Build();
EXPECT_TRUE(v.ValidateGlobalVariables(mod->AST().GlobalVariables()))
<< v.error();
EXPECT_TRUE(v.ValidateGlobalVariables(AST().GlobalVariables())) << v.error();
}
TEST_F(ValidatorTest, GlobalVariableNoStorageClass_Fail) {
// var gloabl_var: f32;
mod->AST().AddGlobalVariable(Var(
Source{Source::Location{12, 34}}, "global_var", ast::StorageClass::kNone,
ty.f32(), nullptr, ast::VariableDecorationList{}));
AST().AddGlobalVariable(Var(Source{Source::Location{12, 34}}, "global_var",
ast::StorageClass::kNone, ty.f32(), nullptr,
ast::VariableDecorationList{}));
EXPECT_TRUE(td()->Determine()) << td()->error();
ValidatorImpl& v = Build();
@@ -350,9 +349,9 @@ TEST_F(ValidatorTest, GlobalVariableNoStorageClass_Fail) {
TEST_F(ValidatorTest, GlobalConstantWithStorageClass_Fail) {
// const<in> gloabl_var: f32;
mod->AST().AddGlobalVariable(Const(
Source{Source::Location{12, 34}}, "global_var", ast::StorageClass::kInput,
ty.f32(), nullptr, ast::VariableDecorationList{}));
AST().AddGlobalVariable(Const(Source{Source::Location{12, 34}}, "global_var",
ast::StorageClass::kInput, ty.f32(), nullptr,
ast::VariableDecorationList{}));
EXPECT_TRUE(td()->Determine()) << td()->error();
ValidatorImpl& v = Build();
@@ -365,9 +364,9 @@ TEST_F(ValidatorTest, GlobalConstantWithStorageClass_Fail) {
TEST_F(ValidatorTest, GlobalConstNoStorageClass_Pass) {
// const gloabl_var: f32;
mod->AST().AddGlobalVariable(Const(
Source{Source::Location{12, 34}}, "global_var", ast::StorageClass::kNone,
ty.f32(), nullptr, ast::VariableDecorationList{}));
AST().AddGlobalVariable(Const(Source{Source::Location{12, 34}}, "global_var",
ast::StorageClass::kNone, ty.f32(), nullptr,
ast::VariableDecorationList{}));
EXPECT_TRUE(td()->Determine()) << td()->error();
ValidatorImpl& v = Build();
@@ -380,9 +379,9 @@ TEST_F(ValidatorTest, UsingUndefinedVariableGlobalVariable_Fail) {
// fn my_func() -> f32 {
// not_global_var = 3.14f;
// }
mod->AST().AddGlobalVariable(Var("global_var", ast::StorageClass::kPrivate,
ty.f32(), Expr(2.1f),
ast::VariableDecorationList{}));
AST().AddGlobalVariable(Var("global_var", ast::StorageClass::kPrivate,
ty.f32(), Expr(2.1f),
ast::VariableDecorationList{}));
SetSource(Source{Source::Location{12, 34}});
auto* lhs = Expr("not_global_var");
@@ -394,7 +393,7 @@ TEST_F(ValidatorTest, UsingUndefinedVariableGlobalVariable_Fail) {
Source{Source::Location{12, 34}}, lhs, rhs),
},
ast::FunctionDecorationList{});
mod->AST().Functions().Add(func);
AST().Functions().Add(func);
ValidatorImpl& v = Build();
@@ -409,9 +408,9 @@ TEST_F(ValidatorTest, UsingUndefinedVariableGlobalVariable_Pass) {
// return;
// }
mod->AST().AddGlobalVariable(Var("global_var", ast::StorageClass::kPrivate,
ty.f32(), Expr(2.1f),
ast::VariableDecorationList{}));
AST().AddGlobalVariable(Var("global_var", ast::StorageClass::kPrivate,
ty.f32(), Expr(2.1f),
ast::VariableDecorationList{}));
auto* func = Func(
"my_func", ast::VariableList{}, ty.void_(),
@@ -423,7 +422,7 @@ TEST_F(ValidatorTest, UsingUndefinedVariableGlobalVariable_Pass) {
ast::FunctionDecorationList{
create<ast::StageDecoration>(ast::PipelineStage::kVertex),
});
mod->AST().Functions().Add(func);
AST().Functions().Add(func);
EXPECT_TRUE(td()->Determine()) << td()->error();
@@ -502,17 +501,16 @@ TEST_F(ValidatorTest, GlobalVariableUnique_Pass) {
// var global_var1 : i32 = 0;
auto* var0 = Var("global_var0", ast::StorageClass::kPrivate, ty.f32(),
Expr(0.1f), ast::VariableDecorationList{});
mod->AST().AddGlobalVariable(var0);
AST().AddGlobalVariable(var0);
auto* var1 = Var(Source{Source::Location{12, 34}}, "global_var1",
ast::StorageClass::kPrivate, ty.f32(), Expr(0),
ast::VariableDecorationList{});
mod->AST().AddGlobalVariable(var1);
AST().AddGlobalVariable(var1);
ValidatorImpl& v = Build();
EXPECT_TRUE(v.ValidateGlobalVariables(mod->AST().GlobalVariables()))
<< v.error();
EXPECT_TRUE(v.ValidateGlobalVariables(AST().GlobalVariables())) << v.error();
}
TEST_F(ValidatorTest, GlobalVariableNotUnique_Fail) {
@@ -520,16 +518,16 @@ TEST_F(ValidatorTest, GlobalVariableNotUnique_Fail) {
// var global_var : i32 = 0;
auto* var0 = Var("global_var", ast::StorageClass::kPrivate, ty.f32(),
Expr(0.1f), ast::VariableDecorationList{});
mod->AST().AddGlobalVariable(var0);
AST().AddGlobalVariable(var0);
auto* var1 = Var(Source{Source::Location{12, 34}}, "global_var",
ast::StorageClass::kPrivate, ty.i32(), Expr(0),
ast::VariableDecorationList{});
mod->AST().AddGlobalVariable(var1);
AST().AddGlobalVariable(var1);
ValidatorImpl& v = Build();
EXPECT_FALSE(v.ValidateGlobalVariables(mod->AST().GlobalVariables()));
EXPECT_FALSE(v.ValidateGlobalVariables(AST().GlobalVariables()));
EXPECT_EQ(v.error(),
"12:34 v-0011: redeclared global identifier 'global_var'");
}
@@ -570,7 +568,7 @@ TEST_F(ValidatorTest, GlobalVariableFunctionVariableNotUnique_Fail) {
auto* global_var = Var("a", ast::StorageClass::kPrivate, ty.f32(), Expr(2.1f),
ast::VariableDecorationList{});
mod->AST().AddGlobalVariable(global_var);
AST().AddGlobalVariable(global_var);
auto* var = Var("a", ast::StorageClass::kNone, ty.f32(), Expr(2.0f),
ast::VariableDecorationList{});
@@ -582,7 +580,7 @@ TEST_F(ValidatorTest, GlobalVariableFunctionVariableNotUnique_Fail) {
},
ast::FunctionDecorationList{});
mod->AST().Functions().Add(func);
AST().Functions().Add(func);
EXPECT_TRUE(td()->Determine()) << td()->error();
EXPECT_TRUE(td()->DetermineFunction(func)) << td()->error();
@@ -612,7 +610,7 @@ TEST_F(ValidatorTest, RedeclaredIndentifier_Fail) {
},
ast::FunctionDecorationList{});
mod->AST().Functions().Add(func);
AST().Functions().Add(func);
EXPECT_TRUE(td()->Determine()) << td()->error();
EXPECT_TRUE(td()->DetermineFunction(func)) << td()->error();
@@ -711,8 +709,8 @@ TEST_F(ValidatorTest, RedeclaredIdentifierDifferentFunctions_Pass) {
create<ast::StageDecoration>(ast::PipelineStage::kVertex),
});
mod->AST().Functions().Add(func0);
mod->AST().Functions().Add(func1);
AST().Functions().Add(func0);
AST().Functions().Add(func1);
EXPECT_TRUE(td()->Determine()) << td()->error();

View File

@@ -51,11 +51,11 @@ TEST_F(ValidatorTypeTest, RuntimeArrayIsLast_Pass) {
auto* struct_type = ty.struct_("Foo", st);
mod->AST().AddConstructedType(struct_type);
AST().AddConstructedType(struct_type);
ValidatorImpl& v = Build();
EXPECT_TRUE(v.ValidateConstructedTypes(mod->AST().ConstructedTypes()));
EXPECT_TRUE(v.ValidateConstructedTypes(AST().ConstructedTypes()));
}
TEST_F(ValidatorTypeTest, RuntimeArrayIsLastNoBlock_Fail) {
@@ -71,11 +71,11 @@ TEST_F(ValidatorTypeTest, RuntimeArrayIsLastNoBlock_Fail) {
decos);
auto* struct_type = ty.struct_("Foo", st);
mod->AST().AddConstructedType(struct_type);
AST().AddConstructedType(struct_type);
ValidatorImpl& v = Build();
EXPECT_FALSE(v.ValidateConstructedTypes(mod->AST().ConstructedTypes()));
EXPECT_FALSE(v.ValidateConstructedTypes(AST().ConstructedTypes()));
EXPECT_EQ(v.error(),
"v-0031: a struct containing a runtime-sized array must be "
"in the 'storage' storage class: 'Foo'");
@@ -99,11 +99,11 @@ TEST_F(ValidatorTypeTest, RuntimeArrayIsNotLast_Fail) {
auto* struct_type = ty.struct_("Foo", st);
mod->AST().AddConstructedType(struct_type);
AST().AddConstructedType(struct_type);
ValidatorImpl& v = Build();
EXPECT_FALSE(v.ValidateConstructedTypes(mod->AST().ConstructedTypes()));
EXPECT_FALSE(v.ValidateConstructedTypes(AST().ConstructedTypes()));
EXPECT_EQ(v.error(),
"12:34 v-0015: runtime arrays may only appear as the last member "
"of a struct");
@@ -125,11 +125,11 @@ TEST_F(ValidatorTypeTest, AliasRuntimeArrayIsNotLast_Fail) {
ast::StructMemberList{Member("b", alias), Member("a", ty.u32())}, decos);
auto* struct_type = ty.struct_("s", st);
mod->AST().AddConstructedType(struct_type);
AST().AddConstructedType(struct_type);
ValidatorImpl& v = Build();
EXPECT_FALSE(v.ValidateConstructedTypes(mod->AST().ConstructedTypes()));
EXPECT_FALSE(v.ValidateConstructedTypes(AST().ConstructedTypes()));
EXPECT_EQ(v.error(),
"v-0015: runtime arrays may only appear as the last member "
"of a struct");
@@ -151,11 +151,11 @@ TEST_F(ValidatorTypeTest, AliasRuntimeArrayIsLast_Pass) {
ast::StructMemberList{Member("a", ty.u32()), Member("b", alias)}, decos);
auto* struct_type = ty.struct_("s", st);
mod->AST().AddConstructedType(struct_type);
AST().AddConstructedType(struct_type);
ValidatorImpl& v = Build();
EXPECT_TRUE(v.ValidateConstructedTypes(mod->AST().ConstructedTypes()));
EXPECT_TRUE(v.ValidateConstructedTypes(AST().ConstructedTypes()));
}
TEST_F(ValidatorTypeTest, RuntimeArrayInFunction_Fail) {
@@ -172,7 +172,7 @@ TEST_F(ValidatorTypeTest, RuntimeArrayInFunction_Fail) {
ast::FunctionDecorationList{
create<ast::StageDecoration>(ast::PipelineStage::kVertex),
});
mod->AST().Functions().Add(func);
AST().Functions().Add(func);
EXPECT_TRUE(td()->Determine()) << td()->error();
@@ -197,7 +197,7 @@ TEST_F(ValidatorTypeTest, RuntimeArrayAsParameter_Fail) {
create<ast::ReturnStatement>(),
},
ast::FunctionDecorationList{});
mod->AST().Functions().Add(func);
AST().Functions().Add(func);
auto* main =
Func("main", ast::VariableList{}, ty.void_(),
@@ -207,7 +207,7 @@ TEST_F(ValidatorTypeTest, RuntimeArrayAsParameter_Fail) {
ast::FunctionDecorationList{
create<ast::StageDecoration>(ast::PipelineStage::kVertex),
});
mod->AST().Functions().Add(main);
AST().Functions().Add(main);
EXPECT_TRUE(td()->Determine()) << td()->error();