From e0448d3d643f827509b13cc7dba192fd0b4b0a8e Mon Sep 17 00:00:00 2001 From: Antonio Maiorano Date: Mon, 10 May 2021 20:42:56 +0000 Subject: [PATCH] ProgramBuilder: make Const helpers require a constructor arg This was originally used to find and fix tests that were not initializing constants, but these were independently fixed by jrprice@ recently (see https://dawn-review.googlesource.com/c/tint/+/50042) recently. Still, this change is useful to avoid this happening again. Bug: tint:792 Change-Id: I0119a1a6ade7a70a0d110ef0aac80f4eaf37a7b4 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/50560 Commit-Queue: Antonio Maiorano Reviewed-by: Ben Clayton Reviewed-by: James Price --- src/program_builder.h | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/src/program_builder.h b/src/program_builder.h index 53ad9cb796..acefa0840c 100644 --- a/src/program_builder.h +++ b/src/program_builder.h @@ -1218,13 +1218,13 @@ class ProgramBuilder { /// @param name the variable name /// @param type the variable type - /// @param constructor optional constructor expression + /// @param constructor constructor expression /// @param decorations optional variable decorations /// @returns a constant `ast::Variable` with the given name and type template ast::Variable* Const(NAME&& name, ast::Type* type, - ast::Expression* constructor = nullptr, + ast::Expression* constructor, ast::DecorationList decorations = {}) { type = ty.MaybeCreateTypename(type); return create(Sym(std::forward(name)), @@ -1235,14 +1235,14 @@ class ProgramBuilder { /// @param source the variable source /// @param name the variable name /// @param type the variable type - /// @param constructor optional constructor expression + /// @param constructor constructor expression /// @param decorations optional variable decorations /// @returns a constant `ast::Variable` with the given name and type template ast::Variable* Const(const Source& source, NAME&& name, ast::Type* type, - ast::Expression* constructor = nullptr, + ast::Expression* constructor, ast::DecorationList decorations = {}) { type = ty.MaybeCreateTypename(type); return create(source, Sym(std::forward(name)), @@ -1320,13 +1320,40 @@ class ProgramBuilder { return var; } - /// @param args the arguments to pass to Const() + /// @param name the variable name + /// @param type the variable type + /// @param constructor constructor expression + /// @param decorations optional variable decorations /// @returns a const `ast::Variable` constructed by calling Var() with the /// arguments of `args`, which is automatically registered as a global /// variable with the ast::Module. - template - ast::Variable* GlobalConst(ARGS&&... args) { - auto* var = Const(std::forward(args)...); + template + ast::Variable* GlobalConst(NAME&& name, + typ::Type type, + ast::Expression* constructor, + ast::DecorationList decorations = {}) { + auto* var = Const(std::forward(name), type, constructor, + std::move(decorations)); + AST().AddGlobalVariable(var); + return var; + } + + /// @param source the variable source + /// @param name the variable name + /// @param type the variable type + /// @param constructor constructor expression + /// @param decorations optional variable decorations + /// @returns a const `ast::Variable` constructed by calling Var() with the + /// arguments of `args`, which is automatically registered as a global + /// variable with the ast::Module. + template + ast::Variable* GlobalConst(const Source& source, + NAME&& name, + typ::Type type, + ast::Expression* constructor, + ast::DecorationList decorations = {}) { + auto* var = Const(source, std::forward(name), type, constructor, + std::move(decorations)); AST().AddGlobalVariable(var); return var; }