ast/builder: Add more builder helpers

Will be used by the transforms

Change-Id: Id4cece30f1ef4cbeb7cf4d7ca8d0e775b4ab4c7c
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/34570
Reviewed-by: dan sinclair <dsinclair@chromium.org>
Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ben Clayton 2020-12-03 20:22:59 +00:00 committed by Commit Bot service account
parent 8e6cd2e680
commit ad0b2cb841
2 changed files with 56 additions and 0 deletions

View File

@ -37,6 +37,15 @@ Variable* Builder::Var(const std::string& name,
return var; return var;
} }
Variable* Builder::Const(const std::string& name,
StorageClass storage,
type::Type* type) {
auto* var = create<Variable>(name, storage, type);
var->set_is_const(true);
OnVariableBuilt(var);
return var;
}
BuilderWithModule::BuilderWithModule() : Builder(new Module()) {} BuilderWithModule::BuilderWithModule() : Builder(new Module()) {}
BuilderWithModule::~BuilderWithModule() { BuilderWithModule::~BuilderWithModule() {

View File

@ -19,6 +19,8 @@
#include <string> #include <string>
#include <utility> #include <utility>
#include "src/ast/array_accessor_expression.h"
#include "src/ast/binary_expression.h"
#include "src/ast/bool_literal.h" #include "src/ast/bool_literal.h"
#include "src/ast/call_expression.h" #include "src/ast/call_expression.h"
#include "src/ast/expression.h" #include "src/ast/expression.h"
@ -32,6 +34,7 @@
#include "src/ast/type/f32_type.h" #include "src/ast/type/f32_type.h"
#include "src/ast/type/i32_type.h" #include "src/ast/type/i32_type.h"
#include "src/ast/type/matrix_type.h" #include "src/ast/type/matrix_type.h"
#include "src/ast/type/pointer_type.h"
#include "src/ast/type/u32_type.h" #include "src/ast/type/u32_type.h"
#include "src/ast/type/vector_type.h" #include "src/ast/type/vector_type.h"
#include "src/ast/type/void_type.h" #include "src/ast/type/void_type.h"
@ -152,6 +155,13 @@ class TypesBuilder {
return array(Of<T>(), N); return array(Of<T>(), N);
} }
/// @return the tint AST pointer to type `T` with the given StorageClass.
/// @param storage_class the storage class of the pointer
template <typename T>
type::Pointer* pointer(StorageClass storage_class) const {
return mod_->create<type::Pointer>(Of<T>(), storage_class);
}
private: private:
/// CToAST<T> is specialized for various `T` types and each specialization /// CToAST<T> is specialized for various `T` types and each specialization
/// contains a single static `get()` method for obtaining the corresponding /// contains a single static `get()` method for obtaining the corresponding
@ -423,6 +433,14 @@ class Builder {
StorageClass storage, StorageClass storage,
type::Type* type); type::Type* type);
/// @param name the variable name
/// @param storage the variable storage class
/// @param type the variable type
/// @returns a constant `Variable` with the given name, storage and type
Variable* Const(const std::string& name,
StorageClass storage,
type::Type* type);
/// @param func the function name /// @param func the function name
/// @param args the function call arguments /// @param args the function call arguments
/// @returns a `CallExpression` to the function `func`, with the /// @returns a `CallExpression` to the function `func`, with the
@ -432,6 +450,35 @@ class Builder {
return CallExpression{Expr(func), ExprList(std::forward<ARGS>(args)...)}; return CallExpression{Expr(func), ExprList(std::forward<ARGS>(args)...)};
} }
/// @param lhs the left hand argument to the addition operation
/// @param rhs the right hand argument to the addition operation
/// @returns a `BinaryExpression` summing the arguments `lhs` and `rhs`
template <typename LHS, typename RHS>
Expression* Add(LHS&& lhs, RHS&& rhs) {
return create<BinaryExpression>(ast::BinaryOp::kAdd,
Expr(std::forward<LHS>(lhs)),
Expr(std::forward<RHS>(rhs)));
}
/// @param lhs the left hand argument to the subtraction operation
/// @param rhs the right hand argument to the subtraction operation
/// @returns a `BinaryExpression` subtracting `rhs` from `lhs`
template <typename LHS, typename RHS>
Expression* Sub(LHS&& lhs, RHS&& rhs) {
return create<BinaryExpression>(ast::BinaryOp::kSubtract,
Expr(std::forward<LHS>(lhs)),
Expr(std::forward<RHS>(rhs)));
}
/// @param arr the array argument for the array accessor expression
/// @param idx the index argument for the array accessor expression
/// @returns a `ArrayAccessorExpression` that indexes `arr` with `idx`
template <typename ARR, typename IDX>
Expression* Index(ARR&& arr, IDX&& idx) {
return create<ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
Expr(std::forward<IDX>(idx)));
}
/// Creates a new `Node` owned by the Module. When the Module is /// Creates a new `Node` owned by the Module. When the Module is
/// destructed, the `Node` will also be destructed. /// destructed, the `Node` will also be destructed.
/// @param args the arguments to pass to the type constructor /// @param args the arguments to pass to the type constructor