From 46d78d731b1faf4b26ce7b91f9b892a2f4d53a87 Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Wed, 10 Feb 2021 21:34:26 +0000 Subject: [PATCH] ProgramBuilder: Add Symbol overloads of Var(), Const(), Global(), GlobalConst() Useful if you have a symbol already Change-Id: Ib9e15ea761f58ee67dc3cc722d9129cd5369d92b Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/41481 Commit-Queue: Ben Clayton Reviewed-by: dan sinclair --- src/program_builder.cc | 50 ------------------- src/program_builder.h | 111 ++++++++++++++++++++++++++++++++--------- 2 files changed, 88 insertions(+), 73 deletions(-) diff --git a/src/program_builder.cc b/src/program_builder.cc index 780da734d3..11b960d5b7 100644 --- a/src/program_builder.cc +++ b/src/program_builder.cc @@ -91,56 +91,6 @@ type::Type* ProgramBuilder::TypeOf(ast::Expression* expr) const { ProgramBuilder::TypesBuilder::TypesBuilder(ProgramBuilder* pb) : builder(pb) {} -ast::Variable* ProgramBuilder::Var(const std::string& name, - ast::StorageClass storage, - type::Type* type) { - return Var(name, storage, type, nullptr, {}); -} - -ast::Variable* ProgramBuilder::Var(const std::string& name, - ast::StorageClass storage, - type::Type* type, - ast::Expression* constructor, - ast::VariableDecorationList decorations) { - return create(Symbols().Register(name), storage, type, false, - constructor, decorations); -} - -ast::Variable* ProgramBuilder::Var(const Source& source, - const std::string& name, - ast::StorageClass storage, - type::Type* type, - ast::Expression* constructor, - ast::VariableDecorationList decorations) { - return create(source, Symbols().Register(name), storage, type, - false, constructor, decorations); -} - -ast::Variable* ProgramBuilder::Const(const std::string& name, - ast::StorageClass storage, - type::Type* type) { - return Const(name, storage, type, nullptr, {}); -} - -ast::Variable* ProgramBuilder::Const(const std::string& name, - ast::StorageClass storage, - type::Type* type, - ast::Expression* constructor, - ast::VariableDecorationList decorations) { - return create(Symbols().Register(name), storage, type, true, - constructor, decorations); -} - -ast::Variable* ProgramBuilder::Const(const Source& source, - const std::string& name, - ast::StorageClass storage, - type::Type* type, - ast::Expression* constructor, - ast::VariableDecorationList decorations) { - return create(source, Symbols().Register(name), storage, type, - true, constructor, decorations); -} - ast::VariableDeclStatement* ProgramBuilder::WrapInStatement(ast::Variable* v) { return create(v); } diff --git a/src/program_builder.h b/src/program_builder.h index c7c8bb5df0..22ccfe23f9 100644 --- a/src/program_builder.h +++ b/src/program_builder.h @@ -494,6 +494,12 @@ class ProgramBuilder { return create(Symbols().Register(name)); } + /// @param symbol the identifier symbol + /// @return an ast::IdentifierExpression with the given symbol + ast::IdentifierExpression* Expr(Symbol symbol) { + return create(symbol); + } + /// @param source the source information /// @param name the identifier name /// @return an ast::IdentifierExpression with the given name @@ -743,15 +749,6 @@ class ProgramBuilder { ty.array(subtype, n), ExprList(std::forward(args)...)); } - /// @param name the variable name - /// @param storage the variable storage class - /// @param type the variable type - /// @returns a `ast::Variable` with the given name, storage and type. The - /// variable will be built with a nullptr constructor and no decorations. - ast::Variable* Var(const std::string& name, - ast::StorageClass storage, - type::Type* type); - /// @param name the variable name /// @param storage the variable storage class /// @param type the variable type @@ -761,8 +758,11 @@ class ProgramBuilder { ast::Variable* Var(const std::string& name, ast::StorageClass storage, type::Type* type, - ast::Expression* constructor, - ast::VariableDecorationList decorations); + ast::Expression* constructor = nullptr, + ast::VariableDecorationList decorations = {}) { + return create(Symbols().Register(name), storage, type, false, + constructor, decorations); + } /// @param source the variable source /// @param name the variable name @@ -775,17 +775,43 @@ class ProgramBuilder { const std::string& name, ast::StorageClass storage, type::Type* type, - ast::Expression* constructor, - ast::VariableDecorationList decorations); + ast::Expression* constructor = nullptr, + ast::VariableDecorationList decorations = {}) { + return create(source, Symbols().Register(name), storage, + type, false, constructor, decorations); + } - /// @param name the variable name + /// @param symbol the variable symbol /// @param storage the variable storage class /// @param type the variable type - /// @returns a constant `ast::Variable` with the given name, storage and type. - /// The variable will be built with a nullptr constructor and no decorations. - ast::Variable* Const(const std::string& name, - ast::StorageClass storage, - type::Type* type); + /// @param constructor constructor expression + /// @param decorations variable decorations + /// @returns a `ast::Variable` with the given symbol, storage and type + ast::Variable* Var(Symbol symbol, + ast::StorageClass storage, + type::Type* type, + ast::Expression* constructor = nullptr, + ast::VariableDecorationList decorations = {}) { + return create(symbol, storage, type, false, constructor, + decorations); + } + + /// @param source the variable source + /// @param symbol the variable symbol + /// @param storage the variable storage class + /// @param type the variable type + /// @param constructor constructor expression + /// @param decorations variable decorations + /// @returns a `ast::Variable` with the given symbol, storage and type + ast::Variable* Var(const Source& source, + Symbol symbol, + ast::StorageClass storage, + type::Type* type, + ast::Expression* constructor = nullptr, + ast::VariableDecorationList decorations = {}) { + return create(source, symbol, storage, type, false, + constructor, decorations); + } /// @param name the variable name /// @param storage the variable storage class @@ -796,8 +822,11 @@ class ProgramBuilder { ast::Variable* Const(const std::string& name, ast::StorageClass storage, type::Type* type, - ast::Expression* constructor, - ast::VariableDecorationList decorations); + ast::Expression* constructor = nullptr, + ast::VariableDecorationList decorations = {}) { + return create(Symbols().Register(name), storage, type, true, + constructor, decorations); + } /// @param source the variable source /// @param name the variable name @@ -810,9 +839,45 @@ class ProgramBuilder { const std::string& name, ast::StorageClass storage, type::Type* type, - ast::Expression* constructor, - ast::VariableDecorationList decorations); + ast::Expression* constructor = nullptr, + ast::VariableDecorationList decorations = {}) { + return create(source, Symbols().Register(name), storage, + type, true, constructor, decorations); + } + /// @param symbol the variable symbol + /// @param storage the variable storage class + /// @param type the variable type + /// @param constructor optional constructor expression + /// @param decorations optional variable decorations + /// @returns a constant `ast::Variable` with the given symbol, storage and + /// type + ast::Variable* Const(Symbol symbol, + ast::StorageClass storage, + type::Type* type, + ast::Expression* constructor = nullptr, + ast::VariableDecorationList decorations = {}) { + return create(symbol, storage, type, true, constructor, + decorations); + } + + /// @param source the variable source + /// @param symbol the variable symbol + /// @param storage the variable storage class + /// @param type the variable type + /// @param constructor optional constructor expression + /// @param decorations optional variable decorations + /// @returns a constant `ast::Variable` with the given symbol, storage and + /// type + ast::Variable* Const(const Source& source, + Symbol symbol, + ast::StorageClass storage, + type::Type* type, + ast::Expression* constructor = nullptr, + ast::VariableDecorationList decorations = {}) { + return create(source, symbol, storage, type, true, + constructor, decorations); + } /// @param args the arguments to pass to Var() /// @returns a `ast::Variable` constructed by calling Var() with the arguments /// of `args`, which is automatically registered as a global variable with the