CloneContext: Drop the first CloneContext* parameter from the ReplaceAll() callback
You have to have the CloneContext in order to call ReplaceAll() in the first place. The overhead of capturing the pointer in the closure is negligible.
Cleans up the callsites.
Change-Id: I3a0fd808517d69f19756f590f3426e5ba226c57e
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/42840
Reviewed-by: James Price <jrprice@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
diff --git a/src/transform/bound_array_accessors.cc b/src/transform/bound_array_accessors.cc
index 4cb3822..ec0a768 100644
--- a/src/transform/bound_array_accessors.cc
+++ b/src/transform/bound_array_accessors.cc
@@ -58,11 +58,11 @@
Transform::Output BoundArrayAccessors::Run(const Program* in) {
ProgramBuilder out;
- CloneContext(&out, in)
- .ReplaceAll([&](CloneContext* ctx, ast::ArrayAccessorExpression* expr) {
- return Transform(expr, ctx);
- })
- .Clone();
+ CloneContext ctx(&out, in);
+ ctx.ReplaceAll([&](ast::ArrayAccessorExpression* expr) {
+ return Transform(expr, &ctx);
+ });
+ ctx.Clone();
return Output(Program(std::move(out)));
}
diff --git a/src/transform/emit_vertex_point_size.cc b/src/transform/emit_vertex_point_size.cc
index 748021c..0dcc50a 100644
--- a/src/transform/emit_vertex_point_size.cc
+++ b/src/transform/emit_vertex_point_size.cc
@@ -64,24 +64,23 @@
out.AST().AddGlobalVariable(pointsize_var);
// Add the pointsize assignment statement to the front of all vertex stages.
- CloneContext(&out, in)
- .ReplaceAll(
- [&](CloneContext* ctx, ast::Function* func) -> ast::Function* {
- if (func->pipeline_stage() != ast::PipelineStage::kVertex) {
- return nullptr; // Just clone func
- }
+ CloneContext ctx(&out, in);
+ ctx.ReplaceAll([&](ast::Function* func) -> ast::Function* {
+ if (func->pipeline_stage() != ast::PipelineStage::kVertex) {
+ return nullptr; // Just clone func
+ }
- // Build the AST expression & statement for assigning pointsize one.
- auto* one = out.create<ast::ScalarConstructorExpression>(
- Source{}, out.create<ast::FloatLiteral>(Source{}, f32, 1.0f));
- auto* pointsize_ident = out.create<ast::IdentifierExpression>(
- Source{}, out.Symbols().Register(kPointSizeVar));
- auto* pointsize_assign = out.create<ast::AssignmentStatement>(
- Source{}, pointsize_ident, one);
+ // Build the AST expression & statement for assigning pointsize one.
+ auto* one = out.create<ast::ScalarConstructorExpression>(
+ Source{}, out.create<ast::FloatLiteral>(Source{}, f32, 1.0f));
+ auto* pointsize_ident = out.create<ast::IdentifierExpression>(
+ Source{}, out.Symbols().Register(kPointSizeVar));
+ auto* pointsize_assign =
+ out.create<ast::AssignmentStatement>(Source{}, pointsize_ident, one);
- return CloneWithStatementsAtStart(ctx, func, {pointsize_assign});
- })
- .Clone();
+ return CloneWithStatementsAtStart(&ctx, func, {pointsize_assign});
+ });
+ ctx.Clone();
return Output(Program(std::move(out)));
}
diff --git a/src/transform/first_index_offset.cc b/src/transform/first_index_offset.cc
index f5f3026..1690d4a 100644
--- a/src/transform/first_index_offset.cc
+++ b/src/transform/first_index_offset.cc
@@ -131,53 +131,52 @@
// add a CreateFirstIndexOffset() statement to each function that uses one of
// these builtins.
- CloneContext(&out, in)
- .ReplaceAll([&](CloneContext* ctx, ast::Variable* var) -> ast::Variable* {
- for (ast::VariableDecoration* dec : var->decorations()) {
- if (auto* blt_dec = dec->As<ast::BuiltinDecoration>()) {
- ast::Builtin blt_type = blt_dec->value();
- if (blt_type == ast::Builtin::kVertexIndex) {
- vertex_index_sym = var->symbol();
- has_vertex_index_ = true;
- return clone_variable_with_new_name(
- ctx, var,
- kIndexOffsetPrefix + in->Symbols().NameFor(var->symbol()));
- } else if (blt_type == ast::Builtin::kInstanceIndex) {
- instance_index_sym = var->symbol();
- has_instance_index_ = true;
- return clone_variable_with_new_name(
- ctx, var,
- kIndexOffsetPrefix + in->Symbols().NameFor(var->symbol()));
- }
+ CloneContext ctx(&out, in);
+ ctx.ReplaceAll([&](ast::Variable* var) -> ast::Variable* {
+ for (ast::VariableDecoration* dec : var->decorations()) {
+ if (auto* blt_dec = dec->As<ast::BuiltinDecoration>()) {
+ ast::Builtin blt_type = blt_dec->value();
+ if (blt_type == ast::Builtin::kVertexIndex) {
+ vertex_index_sym = var->symbol();
+ has_vertex_index_ = true;
+ return clone_variable_with_new_name(
+ &ctx, var,
+ kIndexOffsetPrefix + in->Symbols().NameFor(var->symbol()));
+ } else if (blt_type == ast::Builtin::kInstanceIndex) {
+ instance_index_sym = var->symbol();
+ has_instance_index_ = true;
+ return clone_variable_with_new_name(
+ &ctx, var,
+ kIndexOffsetPrefix + in->Symbols().NameFor(var->symbol()));
+ }
+ }
+ }
+ return nullptr; // Just clone var
+ });
+ ctx.ReplaceAll( // Note: This happens in the same pass as the rename above
+ // which determines the original builtin variable names,
+ // but this should be fine, as variables are cloned first.
+ [&](ast::Function* func) -> ast::Function* {
+ maybe_create_buffer_var(ctx.dst);
+ if (buffer_var == nullptr) {
+ return nullptr; // no transform need, just clone func
+ }
+ auto* func_sem = in->Sem().Get(func);
+ ast::StatementList statements;
+ for (const auto& data : func_sem->LocalReferencedBuiltinVariables()) {
+ if (data.second->value() == ast::Builtin::kVertexIndex) {
+ statements.emplace_back(
+ CreateFirstIndexOffset(in->Symbols().NameFor(vertex_index_sym),
+ kFirstVertexName, buffer_var, ctx.dst));
+ } else if (data.second->value() == ast::Builtin::kInstanceIndex) {
+ statements.emplace_back(CreateFirstIndexOffset(
+ in->Symbols().NameFor(instance_index_sym), kFirstInstanceName,
+ buffer_var, ctx.dst));
}
}
- return nullptr; // Just clone var
- })
- .ReplaceAll( // Note: This happens in the same pass as the rename above
- // which determines the original builtin variable names,
- // but this should be fine, as variables are cloned first.
- [&](CloneContext* ctx, ast::Function* func) -> ast::Function* {
- maybe_create_buffer_var(ctx->dst);
- if (buffer_var == nullptr) {
- return nullptr; // no transform need, just clone func
- }
- auto* func_sem = in->Sem().Get(func);
- ast::StatementList statements;
- for (const auto& data :
- func_sem->LocalReferencedBuiltinVariables()) {
- if (data.second->value() == ast::Builtin::kVertexIndex) {
- statements.emplace_back(CreateFirstIndexOffset(
- in->Symbols().NameFor(vertex_index_sym), kFirstVertexName,
- buffer_var, ctx->dst));
- } else if (data.second->value() == ast::Builtin::kInstanceIndex) {
- statements.emplace_back(CreateFirstIndexOffset(
- in->Symbols().NameFor(instance_index_sym),
- kFirstInstanceName, buffer_var, ctx->dst));
- }
- }
- return CloneWithStatementsAtStart(ctx, func, statements);
- })
- .Clone();
+ return CloneWithStatementsAtStart(&ctx, func, statements);
+ });
+ ctx.Clone();
return Output(
Program(std::move(out)),
diff --git a/src/transform/vertex_pulling.cc b/src/transform/vertex_pulling.cc
index 273806f..3a7adb4 100644
--- a/src/transform/vertex_pulling.cc
+++ b/src/transform/vertex_pulling.cc
@@ -113,7 +113,7 @@
for (auto& replacement : state.location_replacements) {
ctx.Replace(replacement.from, replacement.to);
}
- ctx.ReplaceAll([&](CloneContext*, ast::Function* f) -> ast::Function* {
+ ctx.ReplaceAll([&](ast::Function* f) -> ast::Function* {
if (f == func) {
return CloneWithStatementsAtStart(&ctx, f,
{state.CreateVertexPullingPreamble()});