diff --git a/fuzzers/tint_ast_fuzzer/mutator.cc b/fuzzers/tint_ast_fuzzer/mutator.cc index d1b5efede8..d1637db05c 100644 --- a/fuzzers/tint_ast_fuzzer/mutator.cc +++ b/fuzzers/tint_ast_fuzzer/mutator.cc @@ -71,7 +71,7 @@ bool MaybeApplyMutation(const tint::Program& program, tint::CloneContext clone_context(&mutated, &program); NodeIdMap new_node_id_map; clone_context.ReplaceAll( - [&node_id_map, &new_node_id_map, &clone_context](ast::Node* node) { + [&node_id_map, &new_node_id_map, &clone_context](const ast::Node* node) { // Make sure all `tint::ast::` nodes' ids are preserved. auto* cloned = tint::As(node->Clone(&clone_context)); new_node_id_map.Add(cloned, node_id_map.GetId(node)); diff --git a/fuzzers/tint_ast_fuzzer/node_id_map.cc b/fuzzers/tint_ast_fuzzer/node_id_map.cc index bca123787f..f86b4f4dc9 100644 --- a/fuzzers/tint_ast_fuzzer/node_id_map.cc +++ b/fuzzers/tint_ast_fuzzer/node_id_map.cc @@ -22,33 +22,29 @@ namespace ast_fuzzer { NodeIdMap::NodeIdMap() = default; -NodeIdMap::NodeIdMap(const tint::Program& program) : NodeIdMap() { +NodeIdMap::NodeIdMap(const Program& program) : NodeIdMap() { for (const auto* node : program.ASTNodes().Objects()) { Add(node, TakeFreshId()); } } NodeIdMap::IdType NodeIdMap::GetId(const ast::Node* node) const { - // Since node is immutable by default, const_cast won't - // modify the node structure. - auto it = node_to_id_.find(const_cast(node)); + auto it = node_to_id_.find(node); return it == node_to_id_.end() ? 0 : it->second; } -ast::Node* NodeIdMap::GetNode(IdType id) const { +const ast::Node* NodeIdMap::GetNode(IdType id) const { auto it = id_to_node_.find(id); return it == id_to_node_.end() ? nullptr : it->second; } void NodeIdMap::Add(const ast::Node* node, IdType id) { - auto* casted_node = const_cast(node); - assert(!node_to_id_.count(casted_node) && - "The node already exists in the map"); + assert(!node_to_id_.count(node) && "The node already exists in the map"); assert(IdIsFreshAndValid(id) && "Id already exists in the map or Id is zero"); assert(node && "`node` can't be a nullptr"); - node_to_id_[casted_node] = id; - id_to_node_[id] = casted_node; + node_to_id_[node] = id; + id_to_node_[id] = node; if (id >= fresh_id_) { fresh_id_ = id + 1; diff --git a/fuzzers/tint_ast_fuzzer/node_id_map.h b/fuzzers/tint_ast_fuzzer/node_id_map.h index 78f1590277..2e0131d846 100644 --- a/fuzzers/tint_ast_fuzzer/node_id_map.h +++ b/fuzzers/tint_ast_fuzzer/node_id_map.h @@ -48,13 +48,13 @@ class NodeIdMap { /// @brief Initializes this instance with all the nodes in the `program`. /// @param program - must be valid. - explicit NodeIdMap(const tint::Program& program); + explicit NodeIdMap(const Program& program); /// @brief Returns a node for the given `id`. /// @param id - any value is accepted. /// @return a pointer to some node if `id` exists in this map. /// @return `nullptr` otherwise. - ast::Node* GetNode(IdType id) const; + const ast::Node* GetNode(IdType id) const; /// @brief Returns an id of the given `node`. /// @param node - can be a `nullptr`. @@ -85,8 +85,8 @@ class NodeIdMap { private: IdType fresh_id_ = 1; - std::unordered_map node_to_id_; - std::unordered_map id_to_node_; + std::unordered_map node_to_id_; + std::unordered_map id_to_node_; }; } // namespace ast_fuzzer diff --git a/src/ast/alias.cc b/src/ast/alias.cc index a6983e1b40..8a6a568d12 100644 --- a/src/ast/alias.cc +++ b/src/ast/alias.cc @@ -21,7 +21,10 @@ TINT_INSTANTIATE_TYPEINFO(tint::ast::Alias); namespace tint { namespace ast { -Alias::Alias(ProgramID pid, const Source& src, const Symbol& n, Type* subtype) +Alias::Alias(ProgramID pid, + const Source& src, + const Symbol& n, + const Type* subtype) : Base(pid, src, n), type(subtype) { TINT_ASSERT(AST, type); } @@ -30,7 +33,7 @@ Alias::Alias(Alias&&) = default; Alias::~Alias() = default; -Alias* Alias::Clone(CloneContext* ctx) const { +const Alias* Alias::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); auto sym = ctx->Clone(name); diff --git a/src/ast/alias.h b/src/ast/alias.h index 7f8acdb9aa..495e470707 100644 --- a/src/ast/alias.h +++ b/src/ast/alias.h @@ -30,7 +30,10 @@ class Alias : public Castable { /// @param src the source of this node /// @param name the symbol for the alias /// @param subtype the alias'd type - Alias(ProgramID pid, const Source& src, const Symbol& name, Type* subtype); + Alias(ProgramID pid, + const Source& src, + const Symbol& name, + const Type* subtype); /// Move constructor Alias(Alias&&); /// Destructor @@ -39,10 +42,10 @@ class Alias : public Castable { /// Clones this type and all transitive types using the `CloneContext` `ctx`. /// @param ctx the clone context /// @return the newly cloned type - Alias* Clone(CloneContext* ctx) const override; + const Alias* Clone(CloneContext* ctx) const override; /// the alias type - Type* const type; + const Type* const type; }; } // namespace ast diff --git a/src/ast/array.cc b/src/ast/array.cc index 826efd2950..e37de045c1 100644 --- a/src/ast/array.cc +++ b/src/ast/array.cc @@ -25,13 +25,13 @@ namespace ast { namespace { // Returns the string representation of an array size expression. -std::string SizeExprToString(const ast::Expression* size, +std::string SizeExprToString(const Expression* size, const SymbolTable& symbols) { - if (auto* ident = size->As()) { + if (auto* ident = size->As()) { return symbols.NameFor(ident->symbol); } - if (auto* scalar = size->As()) { - auto* literal = scalar->literal->As(); + if (auto* scalar = size->As()) { + auto* literal = scalar->literal->As(); if (literal) { return std::to_string(literal->ValueAsU32()); } @@ -44,9 +44,9 @@ std::string SizeExprToString(const ast::Expression* size, Array::Array(ProgramID pid, const Source& src, - Type* subtype, - ast::Expression* cnt, - ast::DecorationList decos) + const Type* subtype, + const Expression* cnt, + DecorationList decos) : Base(pid, src), type(subtype), count(cnt), decorations(decos) {} Array::Array(Array&&) = default; @@ -68,7 +68,7 @@ std::string Array::FriendlyName(const SymbolTable& symbols) const { return out.str(); } -Array* Array::Clone(CloneContext* ctx) const { +const Array* Array::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); auto* ty = ctx->Clone(type); diff --git a/src/ast/array.h b/src/ast/array.h index 28d8682ba4..7c06f13ee2 100644 --- a/src/ast/array.h +++ b/src/ast/array.h @@ -38,9 +38,9 @@ class Array : public Castable { /// @param decorations the array decorations Array(ProgramID pid, const Source& src, - Type* subtype, - ast::Expression* count, - ast::DecorationList decorations); + const Type* subtype, + const Expression* count, + DecorationList decorations); /// Move constructor Array(Array&&); ~Array() override; @@ -57,16 +57,16 @@ class Array : public Castable { /// Clones this type and all transitive types using the `CloneContext` `ctx`. /// @param ctx the clone context /// @return the newly cloned type - Array* Clone(CloneContext* ctx) const override; + const Array* Clone(CloneContext* ctx) const override; /// the array element type - Type* const type; + const Type* const type; /// the array size in elements, or nullptr for a runtime array - ast::Expression* const count; + const Expression* const count; /// the array decorations - ast::DecorationList const decorations; + const DecorationList decorations; }; } // namespace ast diff --git a/src/ast/array_accessor_expression.cc b/src/ast/array_accessor_expression.cc index 1f40234b8a..cf2fb7b625 100644 --- a/src/ast/array_accessor_expression.cc +++ b/src/ast/array_accessor_expression.cc @@ -23,8 +23,8 @@ namespace ast { ArrayAccessorExpression::ArrayAccessorExpression(ProgramID pid, const Source& src, - Expression* arr, - Expression* idx) + const Expression* arr, + const Expression* idx) : Base(pid, src), array(arr), index(idx) { TINT_ASSERT(AST, array); TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, array, program_id); @@ -37,7 +37,7 @@ ArrayAccessorExpression::ArrayAccessorExpression(ArrayAccessorExpression&&) = ArrayAccessorExpression::~ArrayAccessorExpression() = default; -ArrayAccessorExpression* ArrayAccessorExpression::Clone( +const ArrayAccessorExpression* ArrayAccessorExpression::Clone( CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); diff --git a/src/ast/array_accessor_expression.h b/src/ast/array_accessor_expression.h index 543ec1d81e..0cb1e47766 100644 --- a/src/ast/array_accessor_expression.h +++ b/src/ast/array_accessor_expression.h @@ -31,8 +31,8 @@ class ArrayAccessorExpression /// @param idx the index expression ArrayAccessorExpression(ProgramID program_id, const Source& source, - Expression* arr, - Expression* idx); + const Expression* arr, + const Expression* idx); /// Move constructor ArrayAccessorExpression(ArrayAccessorExpression&&); ~ArrayAccessorExpression() override; @@ -41,13 +41,13 @@ class ArrayAccessorExpression /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - ArrayAccessorExpression* Clone(CloneContext* ctx) const override; + const ArrayAccessorExpression* Clone(CloneContext* ctx) const override; /// the array - Expression* const array; + const Expression* const array; /// the index expression - Expression* const index; + const Expression* const index; }; } // namespace ast diff --git a/src/ast/assignment_statement.cc b/src/ast/assignment_statement.cc index 0031bf606d..b6a2907177 100644 --- a/src/ast/assignment_statement.cc +++ b/src/ast/assignment_statement.cc @@ -23,8 +23,8 @@ namespace ast { AssignmentStatement::AssignmentStatement(ProgramID pid, const Source& src, - Expression* l, - Expression* r) + const Expression* l, + const Expression* r) : Base(pid, src), lhs(l), rhs(r) { TINT_ASSERT(AST, lhs); TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, lhs, program_id); @@ -36,7 +36,7 @@ AssignmentStatement::AssignmentStatement(AssignmentStatement&&) = default; AssignmentStatement::~AssignmentStatement() = default; -AssignmentStatement* AssignmentStatement::Clone(CloneContext* ctx) const { +const AssignmentStatement* AssignmentStatement::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); auto* l = ctx->Clone(lhs); diff --git a/src/ast/assignment_statement.h b/src/ast/assignment_statement.h index c6fdc5a065..ee8fd3d96d 100644 --- a/src/ast/assignment_statement.h +++ b/src/ast/assignment_statement.h @@ -31,8 +31,8 @@ class AssignmentStatement : public Castable { /// @param rhs the right side of the expression AssignmentStatement(ProgramID program_id, const Source& source, - Expression* lhs, - Expression* rhs); + const Expression* lhs, + const Expression* rhs); /// Move constructor AssignmentStatement(AssignmentStatement&&); ~AssignmentStatement() override; @@ -41,13 +41,13 @@ class AssignmentStatement : public Castable { /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - AssignmentStatement* Clone(CloneContext* ctx) const override; + const AssignmentStatement* Clone(CloneContext* ctx) const override; /// left side expression - Expression* const lhs; + const Expression* const lhs; /// right side expression - Expression* const rhs; + const Expression* const rhs; }; } // namespace ast diff --git a/src/ast/atomic.cc b/src/ast/atomic.cc index 635e758d36..af35e4f03f 100644 --- a/src/ast/atomic.cc +++ b/src/ast/atomic.cc @@ -21,7 +21,7 @@ TINT_INSTANTIATE_TYPEINFO(tint::ast::Atomic); namespace tint { namespace ast { -Atomic::Atomic(ProgramID pid, const Source& src, Type* const subtype) +Atomic::Atomic(ProgramID pid, const Source& src, const Type* const subtype) : Base(pid, src), type(subtype) {} std::string Atomic::FriendlyName(const SymbolTable& symbols) const { @@ -34,10 +34,10 @@ Atomic::Atomic(Atomic&&) = default; Atomic::~Atomic() = default; -Atomic* Atomic::Clone(CloneContext* ctx) const { +const Atomic* Atomic::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); - auto* ty = ctx->Clone(const_cast(type)); + auto* ty = ctx->Clone(type); return ctx->dst->create(src, ty); } diff --git a/src/ast/atomic.h b/src/ast/atomic.h index f00d128a1f..4dcc7a0021 100644 --- a/src/ast/atomic.h +++ b/src/ast/atomic.h @@ -29,7 +29,7 @@ class Atomic : public Castable { /// @param pid the identifier of the program that owns this node /// @param src the source of this node /// @param subtype the pointee type - Atomic(ProgramID pid, const Source& src, Type* const subtype); + Atomic(ProgramID pid, const Source& src, const Type* const subtype); /// Move constructor Atomic(Atomic&&); ~Atomic() override; @@ -42,10 +42,10 @@ class Atomic : public Castable { /// Clones this type and all transitive types using the `CloneContext` `ctx`. /// @param ctx the clone context /// @return the newly cloned type - Atomic* Clone(CloneContext* ctx) const override; + const Atomic* Clone(CloneContext* ctx) const override; /// the pointee type - Type const* const type; + const Type* const type; }; } // namespace ast diff --git a/src/ast/binary_expression.cc b/src/ast/binary_expression.cc index f757bfef25..f025f44070 100644 --- a/src/ast/binary_expression.cc +++ b/src/ast/binary_expression.cc @@ -24,8 +24,8 @@ namespace ast { BinaryExpression::BinaryExpression(ProgramID pid, const Source& src, BinaryOp o, - Expression* l, - Expression* r) + const Expression* l, + const Expression* r) : Base(pid, src), op(o), lhs(l), rhs(r) { TINT_ASSERT(AST, lhs); TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, lhs, program_id); @@ -38,7 +38,7 @@ BinaryExpression::BinaryExpression(BinaryExpression&&) = default; BinaryExpression::~BinaryExpression() = default; -BinaryExpression* BinaryExpression::Clone(CloneContext* ctx) const { +const BinaryExpression* BinaryExpression::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); auto* l = ctx->Clone(lhs); diff --git a/src/ast/binary_expression.h b/src/ast/binary_expression.h index bca7aa4c05..73b4337317 100644 --- a/src/ast/binary_expression.h +++ b/src/ast/binary_expression.h @@ -55,8 +55,8 @@ class BinaryExpression : public Castable { BinaryExpression(ProgramID program_id, const Source& source, BinaryOp op, - Expression* lhs, - Expression* rhs); + const Expression* lhs, + const Expression* rhs); /// Move constructor BinaryExpression(BinaryExpression&&); ~BinaryExpression() override; @@ -110,14 +110,14 @@ class BinaryExpression : public Castable { /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - BinaryExpression* Clone(CloneContext* ctx) const override; + const BinaryExpression* Clone(CloneContext* ctx) const override; /// the binary op type - BinaryOp const op; + const BinaryOp op; /// the left side expression - Expression* const lhs; + const Expression* const lhs; /// the right side expression - Expression* const rhs; + const Expression* const rhs; }; inline bool BinaryExpression::IsArithmetic() const { diff --git a/src/ast/binding_decoration.cc b/src/ast/binding_decoration.cc index 1cdc219a8c..6924dfa885 100644 --- a/src/ast/binding_decoration.cc +++ b/src/ast/binding_decoration.cc @@ -34,7 +34,7 @@ std::string BindingDecoration::Name() const { return "binding"; } -BindingDecoration* BindingDecoration::Clone(CloneContext* ctx) const { +const BindingDecoration* BindingDecoration::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); return ctx->dst->create(src, value); diff --git a/src/ast/binding_decoration.h b/src/ast/binding_decoration.h index d9c2c14b28..1ada708abd 100644 --- a/src/ast/binding_decoration.h +++ b/src/ast/binding_decoration.h @@ -39,10 +39,10 @@ class BindingDecoration : public Castable { /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - BindingDecoration* Clone(CloneContext* ctx) const override; + const BindingDecoration* Clone(CloneContext* ctx) const override; /// the binding value - uint32_t const value; + const uint32_t value; }; } // namespace ast diff --git a/src/ast/bitcast_expression.cc b/src/ast/bitcast_expression.cc index e58b5e5061..5a764928e5 100644 --- a/src/ast/bitcast_expression.cc +++ b/src/ast/bitcast_expression.cc @@ -23,8 +23,8 @@ namespace ast { BitcastExpression::BitcastExpression(ProgramID pid, const Source& src, - ast::Type* t, - Expression* e) + const Type* t, + const Expression* e) : Base(pid, src), type(t), expr(e) { TINT_ASSERT(AST, type); TINT_ASSERT(AST, expr); @@ -34,7 +34,7 @@ BitcastExpression::BitcastExpression(ProgramID pid, BitcastExpression::BitcastExpression(BitcastExpression&&) = default; BitcastExpression::~BitcastExpression() = default; -BitcastExpression* BitcastExpression::Clone(CloneContext* ctx) const { +const BitcastExpression* BitcastExpression::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); auto* t = ctx->Clone(type); diff --git a/src/ast/bitcast_expression.h b/src/ast/bitcast_expression.h index a599679939..fe2c134cca 100644 --- a/src/ast/bitcast_expression.h +++ b/src/ast/bitcast_expression.h @@ -33,8 +33,8 @@ class BitcastExpression : public Castable { /// @param expr the expr BitcastExpression(ProgramID program_id, const Source& source, - ast::Type* type, - Expression* expr); + const Type* type, + const Expression* expr); /// Move constructor BitcastExpression(BitcastExpression&&); ~BitcastExpression() override; @@ -43,12 +43,12 @@ class BitcastExpression : public Castable { /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - BitcastExpression* Clone(CloneContext* ctx) const override; + const BitcastExpression* Clone(CloneContext* ctx) const override; /// the target cast type - ast::Type* const type; + const Type* const type; /// the expression - Expression* const expr; + const Expression* const expr; }; } // namespace ast diff --git a/src/ast/block_statement.cc b/src/ast/block_statement.cc index b0642f4f7b..395cc5bfba 100644 --- a/src/ast/block_statement.cc +++ b/src/ast/block_statement.cc @@ -35,7 +35,7 @@ BlockStatement::BlockStatement(BlockStatement&&) = default; BlockStatement::~BlockStatement() = default; -BlockStatement* BlockStatement::Clone(CloneContext* ctx) const { +const BlockStatement* BlockStatement::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); auto stmts = ctx->Clone(statements); diff --git a/src/ast/block_statement.h b/src/ast/block_statement.h index f8f39b1a6b..56bdc43c65 100644 --- a/src/ast/block_statement.h +++ b/src/ast/block_statement.h @@ -40,7 +40,7 @@ class BlockStatement : public Castable { bool Empty() const { return statements.empty(); } /// @returns the last statement in the block or nullptr if block empty - Statement* Last() const { + const Statement* Last() const { return statements.empty() ? nullptr : statements.back(); } @@ -48,10 +48,10 @@ class BlockStatement : public Castable { /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - BlockStatement* Clone(CloneContext* ctx) const override; + const BlockStatement* Clone(CloneContext* ctx) const override; /// the statement list - StatementList const statements; + const StatementList statements; }; } // namespace ast diff --git a/src/ast/bool.cc b/src/ast/bool.cc index f7caf9b67d..1148107833 100644 --- a/src/ast/bool.cc +++ b/src/ast/bool.cc @@ -31,7 +31,7 @@ std::string Bool::FriendlyName(const SymbolTable&) const { return "bool"; } -Bool* Bool::Clone(CloneContext* ctx) const { +const Bool* Bool::Clone(CloneContext* ctx) const { auto src = ctx->Clone(source); return ctx->dst->create(src); } diff --git a/src/ast/bool.h b/src/ast/bool.h index d3d87021cf..41625d4f06 100644 --- a/src/ast/bool.h +++ b/src/ast/bool.h @@ -47,7 +47,7 @@ class Bool : public Castable { /// Clones this type and all transitive types using the `CloneContext` `ctx`. /// @param ctx the clone context /// @return the newly cloned type - Bool* Clone(CloneContext* ctx) const override; + const Bool* Clone(CloneContext* ctx) const override; }; } // namespace ast diff --git a/src/ast/bool_literal.cc b/src/ast/bool_literal.cc index e6f256e18d..77e7e28e0f 100644 --- a/src/ast/bool_literal.cc +++ b/src/ast/bool_literal.cc @@ -26,7 +26,7 @@ BoolLiteral::BoolLiteral(ProgramID pid, const Source& src, bool val) BoolLiteral::~BoolLiteral() = default; -BoolLiteral* BoolLiteral::Clone(CloneContext* ctx) const { +const BoolLiteral* BoolLiteral::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); return ctx->dst->create(src, value); diff --git a/src/ast/bool_literal.h b/src/ast/bool_literal.h index da5f571c89..e385f1dc44 100644 --- a/src/ast/bool_literal.h +++ b/src/ast/bool_literal.h @@ -32,15 +32,14 @@ class BoolLiteral : public Castable { BoolLiteral(ProgramID pid, const Source& src, bool value); ~BoolLiteral() override; - /// Clones this node and all transitive child nodes using the `CloneContext` /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - BoolLiteral* Clone(CloneContext* ctx) const override; + const BoolLiteral* Clone(CloneContext* ctx) const override; /// The boolean literal value - bool const value; + const bool value; }; } // namespace ast diff --git a/src/ast/break_statement.cc b/src/ast/break_statement.cc index 8bdde65279..1a515cc287 100644 --- a/src/ast/break_statement.cc +++ b/src/ast/break_statement.cc @@ -28,7 +28,7 @@ BreakStatement::BreakStatement(BreakStatement&&) = default; BreakStatement::~BreakStatement() = default; -BreakStatement* BreakStatement::Clone(CloneContext* ctx) const { +const BreakStatement* BreakStatement::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); return ctx->dst->create(src); diff --git a/src/ast/break_statement.h b/src/ast/break_statement.h index 15aeebed28..f88212cb0c 100644 --- a/src/ast/break_statement.h +++ b/src/ast/break_statement.h @@ -35,7 +35,7 @@ class BreakStatement : public Castable { /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - BreakStatement* Clone(CloneContext* ctx) const override; + const BreakStatement* Clone(CloneContext* ctx) const override; }; } // namespace ast diff --git a/src/ast/builtin_decoration.cc b/src/ast/builtin_decoration.cc index db3dfe4e81..f7e44b613b 100644 --- a/src/ast/builtin_decoration.cc +++ b/src/ast/builtin_decoration.cc @@ -34,7 +34,7 @@ std::string BuiltinDecoration::Name() const { return "builtin"; } -BuiltinDecoration* BuiltinDecoration::Clone(CloneContext* ctx) const { +const BuiltinDecoration* BuiltinDecoration::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); return ctx->dst->create(src, builtin); diff --git a/src/ast/builtin_decoration.h b/src/ast/builtin_decoration.h index d31210337a..f5a2da4eb7 100644 --- a/src/ast/builtin_decoration.h +++ b/src/ast/builtin_decoration.h @@ -40,10 +40,10 @@ class BuiltinDecoration : public Castable { /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - BuiltinDecoration* Clone(CloneContext* ctx) const override; + const BuiltinDecoration* Clone(CloneContext* ctx) const override; /// The builtin value - Builtin const builtin; + const Builtin builtin; }; } // namespace ast diff --git a/src/ast/call_expression.cc b/src/ast/call_expression.cc index 43e7a44c60..d4ffa6cd54 100644 --- a/src/ast/call_expression.cc +++ b/src/ast/call_expression.cc @@ -23,7 +23,7 @@ namespace ast { CallExpression::CallExpression(ProgramID pid, const Source& src, - IdentifierExpression* fn, + const IdentifierExpression* fn, ExpressionList a) : Base(pid, src), func(fn), args(a) { TINT_ASSERT(AST, func); @@ -38,7 +38,7 @@ CallExpression::CallExpression(CallExpression&&) = default; CallExpression::~CallExpression() = default; -CallExpression* CallExpression::Clone(CloneContext* ctx) const { +const CallExpression* CallExpression::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); auto* fn = ctx->Clone(func); diff --git a/src/ast/call_expression.h b/src/ast/call_expression.h index 6693e2f2d7..4bd6fc94e0 100644 --- a/src/ast/call_expression.h +++ b/src/ast/call_expression.h @@ -33,7 +33,7 @@ class CallExpression : public Castable { /// @param args the arguments CallExpression(ProgramID program_id, const Source& source, - IdentifierExpression* func, + const IdentifierExpression* func, ExpressionList args); /// Move constructor CallExpression(CallExpression&&); @@ -43,12 +43,12 @@ class CallExpression : public Castable { /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - CallExpression* Clone(CloneContext* ctx) const override; + const CallExpression* Clone(CloneContext* ctx) const override; /// The target function - IdentifierExpression* const func; + const IdentifierExpression* const func; /// The arguments - ExpressionList const args; + const ExpressionList args; }; } // namespace ast diff --git a/src/ast/call_statement.cc b/src/ast/call_statement.cc index 540f2addd1..f8a24b89e6 100644 --- a/src/ast/call_statement.cc +++ b/src/ast/call_statement.cc @@ -23,7 +23,7 @@ namespace ast { CallStatement::CallStatement(ProgramID pid, const Source& src, - CallExpression* call) + const CallExpression* call) : Base(pid, src), expr(call) { TINT_ASSERT(AST, expr); TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, expr, program_id); @@ -33,7 +33,7 @@ CallStatement::CallStatement(CallStatement&&) = default; CallStatement::~CallStatement() = default; -CallStatement* CallStatement::Clone(CloneContext* ctx) const { +const CallStatement* CallStatement::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); auto* call = ctx->Clone(expr); diff --git a/src/ast/call_statement.h b/src/ast/call_statement.h index 130e7f5657..946e7ea9ae 100644 --- a/src/ast/call_statement.h +++ b/src/ast/call_statement.h @@ -28,7 +28,7 @@ class CallStatement : public Castable { /// @param pid the identifier of the program that owns this node /// @param src the source of this node for the statement /// @param call the function - CallStatement(ProgramID pid, const Source& src, CallExpression* call); + CallStatement(ProgramID pid, const Source& src, const CallExpression* call); /// Move constructor CallStatement(CallStatement&&); ~CallStatement() override; @@ -37,10 +37,10 @@ class CallStatement : public Castable { /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - CallStatement* Clone(CloneContext* ctx) const override; + const CallStatement* Clone(CloneContext* ctx) const override; /// The call expression - CallExpression* const expr; + const CallExpression* const expr; }; } // namespace ast diff --git a/src/ast/case_statement.cc b/src/ast/case_statement.cc index 71fe7f98c6..580f477228 100644 --- a/src/ast/case_statement.cc +++ b/src/ast/case_statement.cc @@ -24,7 +24,7 @@ namespace ast { CaseStatement::CaseStatement(ProgramID pid, const Source& src, CaseSelectorList s, - BlockStatement* b) + const BlockStatement* b) : Base(pid, src), selectors(s), body(b) { TINT_ASSERT(AST, body); TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, body, program_id); @@ -38,7 +38,7 @@ CaseStatement::CaseStatement(CaseStatement&&) = default; CaseStatement::~CaseStatement() = default; -CaseStatement* CaseStatement::Clone(CloneContext* ctx) const { +const CaseStatement* CaseStatement::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); auto sel = ctx->Clone(selectors); diff --git a/src/ast/case_statement.h b/src/ast/case_statement.h index ed96a11d74..7b7af0ca78 100644 --- a/src/ast/case_statement.h +++ b/src/ast/case_statement.h @@ -24,7 +24,7 @@ namespace tint { namespace ast { /// A list of case literals -using CaseSelectorList = std::vector; +using CaseSelectorList = std::vector; /// A case statement class CaseStatement : public Castable { @@ -37,7 +37,7 @@ class CaseStatement : public Castable { CaseStatement(ProgramID pid, const Source& src, CaseSelectorList selectors, - BlockStatement* body); + const BlockStatement* body); /// Move constructor CaseStatement(CaseStatement&&); ~CaseStatement() override; @@ -49,17 +49,17 @@ class CaseStatement : public Castable { /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - CaseStatement* Clone(CloneContext* ctx) const override; + const CaseStatement* Clone(CloneContext* ctx) const override; /// The case selectors, empty if none set - CaseSelectorList const selectors; + const CaseSelectorList selectors; /// The case body - BlockStatement* const body; + const BlockStatement* const body; }; /// A list of case statements -using CaseStatementList = std::vector; +using CaseStatementList = std::vector; } // namespace ast } // namespace tint diff --git a/src/ast/continue_statement.cc b/src/ast/continue_statement.cc index 222f7f5ff8..9337f7bed0 100644 --- a/src/ast/continue_statement.cc +++ b/src/ast/continue_statement.cc @@ -28,7 +28,7 @@ ContinueStatement::ContinueStatement(ContinueStatement&&) = default; ContinueStatement::~ContinueStatement() = default; -ContinueStatement* ContinueStatement::Clone(CloneContext* ctx) const { +const ContinueStatement* ContinueStatement::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); return ctx->dst->create(src); diff --git a/src/ast/continue_statement.h b/src/ast/continue_statement.h index 73445a3a06..580c630daf 100644 --- a/src/ast/continue_statement.h +++ b/src/ast/continue_statement.h @@ -35,7 +35,7 @@ class ContinueStatement : public Castable { /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - ContinueStatement* Clone(CloneContext* ctx) const override; + const ContinueStatement* Clone(CloneContext* ctx) const override; }; } // namespace ast diff --git a/src/ast/decoration.h b/src/ast/decoration.h index d915de3fad..c3c9258897 100644 --- a/src/ast/decoration.h +++ b/src/ast/decoration.h @@ -39,7 +39,7 @@ class Decoration : public Castable { }; /// A list of decorations -using DecorationList = std::vector; +using DecorationList = std::vector; /// @param decorations the list of decorations to search /// @returns true if `decorations` includes a decoration of type `T` @@ -56,7 +56,7 @@ bool HasDecoration(const DecorationList& decorations) { /// @param decorations the list of decorations to search /// @returns a pointer to `T` from `decorations` if found, otherwise nullptr. template -T* GetDecoration(const DecorationList& decorations) { +const T* GetDecoration(const DecorationList& decorations) { for (auto* deco : decorations) { if (deco->Is()) { return deco->As(); diff --git a/src/ast/depth_multisampled_texture.cc b/src/ast/depth_multisampled_texture.cc index 5985fee1ba..c870585040 100644 --- a/src/ast/depth_multisampled_texture.cc +++ b/src/ast/depth_multisampled_texture.cc @@ -46,7 +46,7 @@ std::string DepthMultisampledTexture::FriendlyName(const SymbolTable&) const { return out.str(); } -DepthMultisampledTexture* DepthMultisampledTexture::Clone( +const DepthMultisampledTexture* DepthMultisampledTexture::Clone( CloneContext* ctx) const { auto src = ctx->Clone(source); return ctx->dst->create(src, dim); diff --git a/src/ast/depth_multisampled_texture.h b/src/ast/depth_multisampled_texture.h index c274d937ac..4797e03819 100644 --- a/src/ast/depth_multisampled_texture.h +++ b/src/ast/depth_multisampled_texture.h @@ -45,7 +45,7 @@ class DepthMultisampledTexture /// Clones this type and all transitive types using the `CloneContext` `ctx`. /// @param ctx the clone context /// @return the newly cloned type - DepthMultisampledTexture* Clone(CloneContext* ctx) const override; + const DepthMultisampledTexture* Clone(CloneContext* ctx) const override; }; } // namespace ast diff --git a/src/ast/depth_texture.cc b/src/ast/depth_texture.cc index 39f576c6c5..56f222aefc 100644 --- a/src/ast/depth_texture.cc +++ b/src/ast/depth_texture.cc @@ -44,7 +44,7 @@ std::string DepthTexture::FriendlyName(const SymbolTable&) const { return out.str(); } -DepthTexture* DepthTexture::Clone(CloneContext* ctx) const { +const DepthTexture* DepthTexture::Clone(CloneContext* ctx) const { auto src = ctx->Clone(source); return ctx->dst->create(src, dim); } diff --git a/src/ast/depth_texture.h b/src/ast/depth_texture.h index f2e1e10271..530dea2447 100644 --- a/src/ast/depth_texture.h +++ b/src/ast/depth_texture.h @@ -42,7 +42,7 @@ class DepthTexture : public Castable { /// Clones this type and all transitive types using the `CloneContext` `ctx`. /// @param ctx the clone context /// @return the newly cloned type - DepthTexture* Clone(CloneContext* ctx) const override; + const DepthTexture* Clone(CloneContext* ctx) const override; }; } // namespace ast diff --git a/src/ast/disable_validation_decoration.cc b/src/ast/disable_validation_decoration.cc index 69c4605d39..96f28a2520 100644 --- a/src/ast/disable_validation_decoration.cc +++ b/src/ast/disable_validation_decoration.cc @@ -47,7 +47,7 @@ std::string DisableValidationDecoration::InternalName() const { return ""; } -DisableValidationDecoration* DisableValidationDecoration::Clone( +const DisableValidationDecoration* DisableValidationDecoration::Clone( CloneContext* ctx) const { return ctx->dst->ASTNodes().Create( ctx->dst->ID(), validation); diff --git a/src/ast/disable_validation_decoration.h b/src/ast/disable_validation_decoration.h index 2d0fd8927e..099a669cfe 100644 --- a/src/ast/disable_validation_decoration.h +++ b/src/ast/disable_validation_decoration.h @@ -71,10 +71,10 @@ class DisableValidationDecoration /// Performs a deep clone of this object using the CloneContext `ctx`. /// @param ctx the clone context /// @return the newly cloned object - DisableValidationDecoration* Clone(CloneContext* ctx) const override; + const DisableValidationDecoration* Clone(CloneContext* ctx) const override; /// The validation that this decoration disables - DisabledValidation const validation; + const DisabledValidation validation; }; } // namespace ast diff --git a/src/ast/discard_statement.cc b/src/ast/discard_statement.cc index fbc5d28960..c50b989171 100644 --- a/src/ast/discard_statement.cc +++ b/src/ast/discard_statement.cc @@ -28,7 +28,7 @@ DiscardStatement::DiscardStatement(DiscardStatement&&) = default; DiscardStatement::~DiscardStatement() = default; -DiscardStatement* DiscardStatement::Clone(CloneContext* ctx) const { +const DiscardStatement* DiscardStatement::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); return ctx->dst->create(src); diff --git a/src/ast/discard_statement.h b/src/ast/discard_statement.h index 1284b755ab..c670656f33 100644 --- a/src/ast/discard_statement.h +++ b/src/ast/discard_statement.h @@ -35,7 +35,7 @@ class DiscardStatement : public Castable { /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - DiscardStatement* Clone(CloneContext* ctx) const override; + const DiscardStatement* Clone(CloneContext* ctx) const override; }; } // namespace ast diff --git a/src/ast/else_statement.cc b/src/ast/else_statement.cc index 8c3bd59ac7..02ed656f03 100644 --- a/src/ast/else_statement.cc +++ b/src/ast/else_statement.cc @@ -23,8 +23,8 @@ namespace ast { ElseStatement::ElseStatement(ProgramID pid, const Source& src, - Expression* cond, - BlockStatement* b) + const Expression* cond, + const BlockStatement* b) : Base(pid, src), condition(cond), body(b) { TINT_ASSERT(AST, body); TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, body, program_id); @@ -35,7 +35,7 @@ ElseStatement::ElseStatement(ElseStatement&&) = default; ElseStatement::~ElseStatement() = default; -ElseStatement* ElseStatement::Clone(CloneContext* ctx) const { +const ElseStatement* ElseStatement::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); auto* cond = ctx->Clone(condition); diff --git a/src/ast/else_statement.h b/src/ast/else_statement.h index 6dcc2c9bd5..53a4ba4086 100644 --- a/src/ast/else_statement.h +++ b/src/ast/else_statement.h @@ -33,8 +33,8 @@ class ElseStatement : public Castable { /// @param body the else body ElseStatement(ProgramID pid, const Source& src, - Expression* condition, - BlockStatement* body); + const Expression* condition, + const BlockStatement* body); /// Move constructor ElseStatement(ElseStatement&&); ~ElseStatement() override; @@ -43,17 +43,17 @@ class ElseStatement : public Castable { /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - ElseStatement* Clone(CloneContext* ctx) const override; + const ElseStatement* Clone(CloneContext* ctx) const override; /// The else condition or nullptr if none set - Expression* const condition; + const Expression* const condition; /// The else body - BlockStatement* const body; + const BlockStatement* const body; }; /// A list of else statements -using ElseStatementList = std::vector; +using ElseStatementList = std::vector; } // namespace ast } // namespace tint diff --git a/src/ast/expression.h b/src/ast/expression.h index b5a51f24cf..408c55aea8 100644 --- a/src/ast/expression.h +++ b/src/ast/expression.h @@ -39,7 +39,7 @@ class Expression : public Castable { }; /// A list of expressions -using ExpressionList = std::vector; +using ExpressionList = std::vector; } // namespace ast } // namespace tint diff --git a/src/ast/external_texture.cc b/src/ast/external_texture.cc index cdd120c0cd..ded95802a9 100644 --- a/src/ast/external_texture.cc +++ b/src/ast/external_texture.cc @@ -33,7 +33,7 @@ std::string ExternalTexture::FriendlyName(const SymbolTable&) const { return "texture_external"; } -ExternalTexture* ExternalTexture::Clone(CloneContext* ctx) const { +const ExternalTexture* ExternalTexture::Clone(CloneContext* ctx) const { return ctx->dst->create(); } diff --git a/src/ast/external_texture.h b/src/ast/external_texture.h index 250f466d4d..734ba8ef1e 100644 --- a/src/ast/external_texture.h +++ b/src/ast/external_texture.h @@ -42,7 +42,7 @@ class ExternalTexture : public Castable { /// Clones this type and all transitive types using the `CloneContext` `ctx`. /// @param ctx the clone context /// @return the newly cloned type - ExternalTexture* Clone(CloneContext* ctx) const override; + const ExternalTexture* Clone(CloneContext* ctx) const override; }; } // namespace ast diff --git a/src/ast/f32.cc b/src/ast/f32.cc index cc5f0d1663..b455036550 100644 --- a/src/ast/f32.cc +++ b/src/ast/f32.cc @@ -31,7 +31,7 @@ std::string F32::FriendlyName(const SymbolTable&) const { return "f32"; } -F32* F32::Clone(CloneContext* ctx) const { +const F32* F32::Clone(CloneContext* ctx) const { auto src = ctx->Clone(source); return ctx->dst->create(src); } diff --git a/src/ast/f32.h b/src/ast/f32.h index 804ea55001..4643edc717 100644 --- a/src/ast/f32.h +++ b/src/ast/f32.h @@ -41,7 +41,7 @@ class F32 : public Castable { /// Clones this type and all transitive types using the `CloneContext` `ctx`. /// @param ctx the clone context /// @return the newly cloned type - F32* Clone(CloneContext* ctx) const override; + const F32* Clone(CloneContext* ctx) const override; }; } // namespace ast diff --git a/src/ast/fallthrough_statement.cc b/src/ast/fallthrough_statement.cc index cac4d86080..316405ced8 100644 --- a/src/ast/fallthrough_statement.cc +++ b/src/ast/fallthrough_statement.cc @@ -28,7 +28,8 @@ FallthroughStatement::FallthroughStatement(FallthroughStatement&&) = default; FallthroughStatement::~FallthroughStatement() = default; -FallthroughStatement* FallthroughStatement::Clone(CloneContext* ctx) const { +const FallthroughStatement* FallthroughStatement::Clone( + CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); return ctx->dst->create(src); diff --git a/src/ast/fallthrough_statement.h b/src/ast/fallthrough_statement.h index 282869f99a..e715710ff8 100644 --- a/src/ast/fallthrough_statement.h +++ b/src/ast/fallthrough_statement.h @@ -35,7 +35,7 @@ class FallthroughStatement : public Castable { /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - FallthroughStatement* Clone(CloneContext* ctx) const override; + const FallthroughStatement* Clone(CloneContext* ctx) const override; }; } // namespace ast diff --git a/src/ast/float_literal.cc b/src/ast/float_literal.cc index 94bc68ecbd..c895983f50 100644 --- a/src/ast/float_literal.cc +++ b/src/ast/float_literal.cc @@ -28,7 +28,7 @@ FloatLiteral::FloatLiteral(ProgramID pid, const Source& src, float val) FloatLiteral::~FloatLiteral() = default; -FloatLiteral* FloatLiteral::Clone(CloneContext* ctx) const { +const FloatLiteral* FloatLiteral::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); return ctx->dst->create(src, value); diff --git a/src/ast/float_literal.h b/src/ast/float_literal.h index 990b7e4246..469e7a820e 100644 --- a/src/ast/float_literal.h +++ b/src/ast/float_literal.h @@ -36,10 +36,10 @@ class FloatLiteral : public Castable { /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - FloatLiteral* Clone(CloneContext* ctx) const override; + const FloatLiteral* Clone(CloneContext* ctx) const override; /// The float literal value - float const value; + const float value; }; } // namespace ast diff --git a/src/ast/for_loop_statement.cc b/src/ast/for_loop_statement.cc index a1eab181d5..ec54edfe3f 100644 --- a/src/ast/for_loop_statement.cc +++ b/src/ast/for_loop_statement.cc @@ -23,10 +23,10 @@ namespace ast { ForLoopStatement::ForLoopStatement(ProgramID pid, const Source& src, - Statement* init, - Expression* cond, - Statement* cont, - BlockStatement* b) + const Statement* init, + const Expression* cond, + const Statement* cont, + const BlockStatement* b) : Base(pid, src), initializer(init), condition(cond), @@ -44,7 +44,7 @@ ForLoopStatement::ForLoopStatement(ForLoopStatement&&) = default; ForLoopStatement::~ForLoopStatement() = default; -ForLoopStatement* ForLoopStatement::Clone(CloneContext* ctx) const { +const ForLoopStatement* ForLoopStatement::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); diff --git a/src/ast/for_loop_statement.h b/src/ast/for_loop_statement.h index 72b502603b..5684c12a93 100644 --- a/src/ast/for_loop_statement.h +++ b/src/ast/for_loop_statement.h @@ -33,11 +33,11 @@ class ForLoopStatement : public Castable { /// @param continuing the optional continuing statement /// @param body the loop body ForLoopStatement(ProgramID program_id, - const Source& source, - Statement* initializer, - Expression* condition, - Statement* continuing, - BlockStatement* body); + Source const& source, + const Statement* initializer, + const Expression* condition, + const Statement* continuing, + const BlockStatement* body); /// Move constructor ForLoopStatement(ForLoopStatement&&); ~ForLoopStatement() override; @@ -46,19 +46,19 @@ class ForLoopStatement : public Castable { /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - ForLoopStatement* Clone(CloneContext* ctx) const override; + const ForLoopStatement* Clone(CloneContext* ctx) const override; /// The initializer statement - Statement* const initializer; + const Statement* const initializer; /// The condition expression - Expression* const condition; + const Expression* const condition; /// The continuing statement - Statement* const continuing; + const Statement* const continuing; /// The loop body block - BlockStatement* const body; + const BlockStatement* const body; }; } // namespace ast diff --git a/src/ast/function.cc b/src/ast/function.cc index fa44f3ea4a..ddbf34fd6d 100644 --- a/src/ast/function.cc +++ b/src/ast/function.cc @@ -27,8 +27,8 @@ Function::Function(ProgramID pid, const Source& src, Symbol sym, VariableList parameters, - ast::Type* return_ty, - BlockStatement* b, + const Type* return_ty, + const BlockStatement* b, DecorationList decos, DecorationList return_type_decos) : Base(pid, src), @@ -65,7 +65,7 @@ PipelineStage Function::PipelineStage() const { return PipelineStage::kNone; } -Function* Function::Clone(CloneContext* ctx) const { +const Function* Function::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); auto sym = ctx->Clone(symbol); @@ -77,7 +77,7 @@ Function* Function::Clone(CloneContext* ctx) const { return ctx->dst->create(src, sym, p, ret, b, decos, ret_decos); } -Function* FunctionList::Find(Symbol sym) const { +const Function* FunctionList::Find(Symbol sym) const { for (auto* func : *this) { if (func->symbol == sym) { return func; @@ -86,7 +86,7 @@ Function* FunctionList::Find(Symbol sym) const { return nullptr; } -Function* FunctionList::Find(Symbol sym, PipelineStage stage) const { +const Function* FunctionList::Find(Symbol sym, PipelineStage stage) const { for (auto* func : *this) { if (func->symbol == sym && func->PipelineStage() == stage) { return func; diff --git a/src/ast/function.h b/src/ast/function.h index a76c6e1232..b546c096c4 100644 --- a/src/ast/function.h +++ b/src/ast/function.h @@ -48,8 +48,8 @@ class Function : public Castable { const Source& source, Symbol symbol, VariableList params, - ast::Type* return_type, - BlockStatement* body, + const Type* return_type, + const BlockStatement* body, DecorationList decorations, DecorationList return_type_decorations); /// Move constructor @@ -67,44 +67,44 @@ class Function : public Castable { /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - Function* Clone(CloneContext* ctx) const override; + const Function* Clone(CloneContext* ctx) const override; /// The function symbol - Symbol const symbol; + const Symbol symbol; /// The function params - VariableList const params; + const VariableList params; /// The function return type - ast::Type* const return_type; + const Type* const return_type; /// The function body - BlockStatement* const body; + const BlockStatement* const body; /// The decorations attached to this function - DecorationList const decorations; + const DecorationList decorations; /// The decorations attached to the function return type. - DecorationList const return_type_decorations; + const DecorationList return_type_decorations; }; /// A list of functions -class FunctionList : public std::vector { +class FunctionList : public std::vector { public: /// Appends f to the end of the list /// @param f the function to append to this list - void Add(Function* f) { this->emplace_back(f); } + void Add(const Function* f) { this->emplace_back(f); } /// Returns the function with the given name /// @param sym the function symbol to search for /// @returns the associated function or nullptr if none exists - Function* Find(Symbol sym) const; + const Function* Find(Symbol sym) const; /// Returns the function with the given name /// @param sym the function symbol to search for /// @param stage the pipeline stage /// @returns the associated function or nullptr if none exists - Function* Find(Symbol sym, PipelineStage stage) const; + const Function* Find(Symbol sym, PipelineStage stage) const; /// @param stage the pipeline stage /// @returns true if the Builder contains an entrypoint function with diff --git a/src/ast/group_decoration.cc b/src/ast/group_decoration.cc index 571794e8ec..66f2fabe36 100644 --- a/src/ast/group_decoration.cc +++ b/src/ast/group_decoration.cc @@ -32,7 +32,7 @@ std::string GroupDecoration::Name() const { return "group"; } -GroupDecoration* GroupDecoration::Clone(CloneContext* ctx) const { +const GroupDecoration* GroupDecoration::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); return ctx->dst->create(src, value); diff --git a/src/ast/group_decoration.h b/src/ast/group_decoration.h index de14126b81..e00488fcb7 100644 --- a/src/ast/group_decoration.h +++ b/src/ast/group_decoration.h @@ -39,10 +39,10 @@ class GroupDecoration : public Castable { /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - GroupDecoration* Clone(CloneContext* ctx) const override; + const GroupDecoration* Clone(CloneContext* ctx) const override; /// The group value - uint32_t const value; + const uint32_t value; }; } // namespace ast diff --git a/src/ast/i32.cc b/src/ast/i32.cc index b9145d3a7e..50207c3a6e 100644 --- a/src/ast/i32.cc +++ b/src/ast/i32.cc @@ -31,7 +31,7 @@ std::string I32::FriendlyName(const SymbolTable&) const { return "i32"; } -I32* I32::Clone(CloneContext* ctx) const { +const I32* I32::Clone(CloneContext* ctx) const { auto src = ctx->Clone(source); return ctx->dst->create(src); } diff --git a/src/ast/i32.h b/src/ast/i32.h index 63e66e452e..8a76cf2e3e 100644 --- a/src/ast/i32.h +++ b/src/ast/i32.h @@ -41,7 +41,7 @@ class I32 : public Castable { /// Clones this type and all transitive types using the `CloneContext` `ctx`. /// @param ctx the clone context /// @return the newly cloned type - I32* Clone(CloneContext* ctx) const override; + const I32* Clone(CloneContext* ctx) const override; }; } // namespace ast diff --git a/src/ast/identifier_expression.cc b/src/ast/identifier_expression.cc index da80ff03db..f0c50c4412 100644 --- a/src/ast/identifier_expression.cc +++ b/src/ast/identifier_expression.cc @@ -33,7 +33,8 @@ IdentifierExpression::IdentifierExpression(IdentifierExpression&&) = default; IdentifierExpression::~IdentifierExpression() = default; -IdentifierExpression* IdentifierExpression::Clone(CloneContext* ctx) const { +const IdentifierExpression* IdentifierExpression::Clone( + CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); auto sym = ctx->Clone(symbol); diff --git a/src/ast/identifier_expression.h b/src/ast/identifier_expression.h index 7977b293f9..e238cde83d 100644 --- a/src/ast/identifier_expression.h +++ b/src/ast/identifier_expression.h @@ -36,10 +36,10 @@ class IdentifierExpression : public Castable { /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - IdentifierExpression* Clone(CloneContext* ctx) const override; + const IdentifierExpression* Clone(CloneContext* ctx) const override; /// The symbol for the identifier - Symbol const symbol; + const Symbol symbol; }; } // namespace ast diff --git a/src/ast/if_statement.cc b/src/ast/if_statement.cc index f4120d7227..31708d0b62 100644 --- a/src/ast/if_statement.cc +++ b/src/ast/if_statement.cc @@ -23,8 +23,8 @@ namespace ast { IfStatement::IfStatement(ProgramID pid, const Source& src, - Expression* cond, - BlockStatement* b, + const Expression* cond, + const BlockStatement* b, ElseStatementList else_stmts) : Base(pid, src), condition(cond), @@ -44,7 +44,7 @@ IfStatement::IfStatement(IfStatement&&) = default; IfStatement::~IfStatement() = default; -IfStatement* IfStatement::Clone(CloneContext* ctx) const { +const IfStatement* IfStatement::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); auto* cond = ctx->Clone(condition); diff --git a/src/ast/if_statement.h b/src/ast/if_statement.h index cd37ba0cfb..d4c54e1f03 100644 --- a/src/ast/if_statement.h +++ b/src/ast/if_statement.h @@ -33,8 +33,8 @@ class IfStatement : public Castable { /// @param else_stmts the else statements IfStatement(ProgramID pid, const Source& src, - Expression* condition, - BlockStatement* body, + const Expression* condition, + const BlockStatement* body, ElseStatementList else_stmts); /// Move constructor IfStatement(IfStatement&&); @@ -44,16 +44,16 @@ class IfStatement : public Castable { /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - IfStatement* Clone(CloneContext* ctx) const override; + const IfStatement* Clone(CloneContext* ctx) const override; /// The if condition or nullptr if none set - Expression* const condition; + const Expression* const condition; /// The if body - BlockStatement* const body; + const BlockStatement* const body; /// The else statements - ElseStatementList const else_statements; + const ElseStatementList else_statements; }; } // namespace ast diff --git a/src/ast/int_literal.cc b/src/ast/int_literal.cc index f9659d503a..ff164c0543 100644 --- a/src/ast/int_literal.cc +++ b/src/ast/int_literal.cc @@ -19,8 +19,7 @@ TINT_INSTANTIATE_TYPEINFO(tint::ast::IntLiteral); namespace tint { namespace ast { -IntLiteral::IntLiteral(ProgramID pid, const Source& src) - : Base(pid, src) {} +IntLiteral::IntLiteral(ProgramID pid, const Source& src) : Base(pid, src) {} IntLiteral::~IntLiteral() = default; diff --git a/src/ast/interpolate_decoration.cc b/src/ast/interpolate_decoration.cc index e3f7fae8f6..44a679d582 100644 --- a/src/ast/interpolate_decoration.cc +++ b/src/ast/interpolate_decoration.cc @@ -35,7 +35,8 @@ std::string InterpolateDecoration::Name() const { return "interpolate"; } -InterpolateDecoration* InterpolateDecoration::Clone(CloneContext* ctx) const { +const InterpolateDecoration* InterpolateDecoration::Clone( + CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); return ctx->dst->create(src, type, sampling); diff --git a/src/ast/interpolate_decoration.h b/src/ast/interpolate_decoration.h index 54883fab34..30d173b5c6 100644 --- a/src/ast/interpolate_decoration.h +++ b/src/ast/interpolate_decoration.h @@ -51,13 +51,13 @@ class InterpolateDecoration /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - InterpolateDecoration* Clone(CloneContext* ctx) const override; + const InterpolateDecoration* Clone(CloneContext* ctx) const override; /// The interpolation type - InterpolationType const type; + const InterpolationType type; /// The interpolation sampling - InterpolationSampling const sampling; + const InterpolationSampling sampling; }; /// @param out the std::ostream to write to diff --git a/src/ast/intrinsic_texture_helper_test.cc b/src/ast/intrinsic_texture_helper_test.cc index c926f2d2db..04c3ee0246 100644 --- a/src/ast/intrinsic_texture_helper_test.cc +++ b/src/ast/intrinsic_texture_helper_test.cc @@ -134,7 +134,7 @@ std::ostream& operator<<(std::ostream& out, const TextureOverloadCase& data) { return out; } -ast::Type* TextureOverloadCase::BuildResultVectorComponentType( +const ast::Type* TextureOverloadCase::BuildResultVectorComponentType( ProgramBuilder* b) const { switch (texture_data_type) { case ast::intrinsic::test::TextureDataType::kF32: @@ -149,7 +149,7 @@ ast::Type* TextureOverloadCase::BuildResultVectorComponentType( return {}; } -ast::Variable* TextureOverloadCase::BuildTextureVariable( +const ast::Variable* TextureOverloadCase::BuildTextureVariable( ProgramBuilder* b) const { DecorationList decos = { b->create(0), @@ -188,7 +188,7 @@ ast::Variable* TextureOverloadCase::BuildTextureVariable( return nullptr; } -ast::Variable* TextureOverloadCase::BuildSamplerVariable( +const ast::Variable* TextureOverloadCase::BuildSamplerVariable( ProgramBuilder* b) const { DecorationList decos = { b->create(0), diff --git a/src/ast/intrinsic_texture_helper_test.h b/src/ast/intrinsic_texture_helper_test.h index 5f842f1225..ee517a1743 100644 --- a/src/ast/intrinsic_texture_helper_test.h +++ b/src/ast/intrinsic_texture_helper_test.h @@ -205,22 +205,23 @@ struct TextureOverloadCase { /// @param builder the AST builder used for the test /// @returns the vector component type of the texture function return value - ast::Type* BuildResultVectorComponentType(ProgramBuilder* builder) const; + const ast::Type* BuildResultVectorComponentType( + ProgramBuilder* builder) const; /// @param builder the AST builder used for the test /// @returns a variable holding the test texture, automatically registered as /// a global variable. - ast::Variable* BuildTextureVariable(ProgramBuilder* builder) const; + const ast::Variable* BuildTextureVariable(ProgramBuilder* builder) const; /// @param builder the AST builder used for the test /// @returns a Variable holding the test sampler, automatically registered as /// a global variable. - ast::Variable* BuildSamplerVariable(ProgramBuilder* builder) const; + const ast::Variable* BuildSamplerVariable(ProgramBuilder* builder) const; /// The enumerator for this overload - ValidTextureOverload const overload; + const ValidTextureOverload overload; /// A human readable description of the overload const char* const description; /// The texture kind for the texture parameter - TextureKind const texture_kind; + const TextureKind texture_kind; /// The sampler kind for the sampler parameter /// Used only when texture_kind is not kStorage ast::SamplerKind const sampler_kind = ast::SamplerKind::kSampler; @@ -233,7 +234,7 @@ struct TextureOverloadCase { /// The dimensions of the texture parameter ast::TextureDimension const texture_dimension; /// The data type of the texture parameter - TextureDataType const texture_data_type; + const TextureDataType texture_data_type; /// Name of the function. e.g. `textureSample`, `textureSampleGrad`, etc const char* const function; /// A function that builds the AST arguments for the overload diff --git a/src/ast/invariant_decoration.cc b/src/ast/invariant_decoration.cc index cf00a5b4af..6ef6dbd93e 100644 --- a/src/ast/invariant_decoration.cc +++ b/src/ast/invariant_decoration.cc @@ -30,7 +30,7 @@ std::string InvariantDecoration::Name() const { return "invariant"; } -InvariantDecoration* InvariantDecoration::Clone(CloneContext* ctx) const { +const InvariantDecoration* InvariantDecoration::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); return ctx->dst->create(src); diff --git a/src/ast/invariant_decoration.h b/src/ast/invariant_decoration.h index 5f3216de53..77154d1990 100644 --- a/src/ast/invariant_decoration.h +++ b/src/ast/invariant_decoration.h @@ -38,7 +38,7 @@ class InvariantDecoration : public Castable { /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - InvariantDecoration* Clone(CloneContext* ctx) const override; + const InvariantDecoration* Clone(CloneContext* ctx) const override; }; } // namespace ast diff --git a/src/ast/location_decoration.cc b/src/ast/location_decoration.cc index 5db7e36644..3c386fae7a 100644 --- a/src/ast/location_decoration.cc +++ b/src/ast/location_decoration.cc @@ -34,7 +34,7 @@ std::string LocationDecoration::Name() const { return "location"; } -LocationDecoration* LocationDecoration::Clone(CloneContext* ctx) const { +const LocationDecoration* LocationDecoration::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); return ctx->dst->create(src, value); diff --git a/src/ast/location_decoration.h b/src/ast/location_decoration.h index d5478745b3..e498ca5e08 100644 --- a/src/ast/location_decoration.h +++ b/src/ast/location_decoration.h @@ -39,10 +39,10 @@ class LocationDecoration : public Castable { /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - LocationDecoration* Clone(CloneContext* ctx) const override; + const LocationDecoration* Clone(CloneContext* ctx) const override; /// The location value - uint32_t const value; + const uint32_t value; }; } // namespace ast diff --git a/src/ast/loop_statement.cc b/src/ast/loop_statement.cc index e01392ed1a..84f1249f04 100644 --- a/src/ast/loop_statement.cc +++ b/src/ast/loop_statement.cc @@ -23,8 +23,8 @@ namespace ast { LoopStatement::LoopStatement(ProgramID pid, const Source& src, - BlockStatement* b, - BlockStatement* cont) + const BlockStatement* b, + const BlockStatement* cont) : Base(pid, src), body(b), continuing(cont) { TINT_ASSERT(AST, body); TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, body, program_id); @@ -35,7 +35,7 @@ LoopStatement::LoopStatement(LoopStatement&&) = default; LoopStatement::~LoopStatement() = default; -LoopStatement* LoopStatement::Clone(CloneContext* ctx) const { +const LoopStatement* LoopStatement::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); auto* b = ctx->Clone(body); diff --git a/src/ast/loop_statement.h b/src/ast/loop_statement.h index 5f0ba50944..90a6ddf894 100644 --- a/src/ast/loop_statement.h +++ b/src/ast/loop_statement.h @@ -30,8 +30,8 @@ class LoopStatement : public Castable { /// @param continuing the continuing statements LoopStatement(ProgramID program_id, const Source& source, - BlockStatement* body, - BlockStatement* continuing); + const BlockStatement* body, + const BlockStatement* continuing); /// Move constructor LoopStatement(LoopStatement&&); ~LoopStatement() override; @@ -40,13 +40,13 @@ class LoopStatement : public Castable { /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - LoopStatement* Clone(CloneContext* ctx) const override; + const LoopStatement* Clone(CloneContext* ctx) const override; /// The loop body - BlockStatement* const body; + const BlockStatement* const body; /// The continuing statements - BlockStatement* const continuing; + const BlockStatement* const continuing; }; } // namespace ast diff --git a/src/ast/matrix.cc b/src/ast/matrix.cc index 2a5801a99f..2530e800cc 100644 --- a/src/ast/matrix.cc +++ b/src/ast/matrix.cc @@ -23,7 +23,7 @@ namespace ast { Matrix::Matrix(ProgramID pid, const Source& src, - Type* subtype, + const Type* subtype, uint32_t r, uint32_t c) : Base(pid, src), type(subtype), rows(r), columns(c) { @@ -45,7 +45,7 @@ std::string Matrix::FriendlyName(const SymbolTable& symbols) const { return out.str(); } -Matrix* Matrix::Clone(CloneContext* ctx) const { +const Matrix* Matrix::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); auto* ty = ctx->Clone(type); diff --git a/src/ast/matrix.h b/src/ast/matrix.h index 434ee3f098..023adc6dd9 100644 --- a/src/ast/matrix.h +++ b/src/ast/matrix.h @@ -33,7 +33,7 @@ class Matrix : public Castable { /// @param columns the number of columns in the matrix Matrix(ProgramID pid, const Source& src, - Type* subtype, + const Type* subtype, uint32_t rows, uint32_t columns); /// Move constructor @@ -48,16 +48,16 @@ class Matrix : public Castable { /// Clones this type and all transitive types using the `CloneContext` `ctx`. /// @param ctx the clone context /// @return the newly cloned type - Matrix* Clone(CloneContext* ctx) const override; + const Matrix* Clone(CloneContext* ctx) const override; /// The type of the matrix - Type* const type; + const Type* const type; /// The number of rows in the matrix - uint32_t const rows; + const uint32_t rows; /// The number of columns in the matrix - uint32_t const columns; + const uint32_t columns; }; } // namespace ast diff --git a/src/ast/member_accessor_expression.cc b/src/ast/member_accessor_expression.cc index 4dc3bf896f..12b2a3d109 100644 --- a/src/ast/member_accessor_expression.cc +++ b/src/ast/member_accessor_expression.cc @@ -21,10 +21,11 @@ TINT_INSTANTIATE_TYPEINFO(tint::ast::MemberAccessorExpression); namespace tint { namespace ast { -MemberAccessorExpression::MemberAccessorExpression(ProgramID pid, - const Source& src, - Expression* str, - IdentifierExpression* mem) +MemberAccessorExpression::MemberAccessorExpression( + ProgramID pid, + const Source& src, + const Expression* str, + const IdentifierExpression* mem) : Base(pid, src), structure(str), member(mem) { TINT_ASSERT(AST, structure); TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, structure, program_id); @@ -37,7 +38,7 @@ MemberAccessorExpression::MemberAccessorExpression(MemberAccessorExpression&&) = MemberAccessorExpression::~MemberAccessorExpression() = default; -MemberAccessorExpression* MemberAccessorExpression::Clone( +const MemberAccessorExpression* MemberAccessorExpression::Clone( CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); diff --git a/src/ast/member_accessor_expression.h b/src/ast/member_accessor_expression.h index 9716e2ed91..58f93d0bd3 100644 --- a/src/ast/member_accessor_expression.h +++ b/src/ast/member_accessor_expression.h @@ -31,8 +31,8 @@ class MemberAccessorExpression /// @param member the member MemberAccessorExpression(ProgramID program_id, const Source& source, - Expression* structure, - IdentifierExpression* member); + const Expression* structure, + const IdentifierExpression* member); /// Move constructor MemberAccessorExpression(MemberAccessorExpression&&); ~MemberAccessorExpression() override; @@ -41,13 +41,13 @@ class MemberAccessorExpression /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - MemberAccessorExpression* Clone(CloneContext* ctx) const override; + const MemberAccessorExpression* Clone(CloneContext* ctx) const override; /// The structure - Expression* const structure; + const Expression* const structure; /// The member expression - IdentifierExpression* const member; + const IdentifierExpression* const member; }; } // namespace ast diff --git a/src/ast/module.cc b/src/ast/module.cc index 33ccd055ae..24999d2f1d 100644 --- a/src/ast/module.cc +++ b/src/ast/module.cc @@ -28,7 +28,7 @@ Module::Module(ProgramID pid, const Source& src) : Base(pid, src) {} Module::Module(ProgramID pid, const Source& src, - std::vector global_decls) + std::vector global_decls) : Base(pid, src), global_declarations_(std::move(global_decls)) { for (auto* decl : global_declarations_) { if (decl == nullptr) { @@ -59,28 +59,28 @@ const ast::TypeDecl* Module::LookupType(Symbol name) const { return nullptr; } -void Module::AddGlobalVariable(ast::Variable* var) { +void Module::AddGlobalVariable(const ast::Variable* var) { TINT_ASSERT(AST, var); TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, var, program_id); global_variables_.push_back(var); global_declarations_.push_back(var); } -void Module::AddTypeDecl(ast::TypeDecl* type) { +void Module::AddTypeDecl(const ast::TypeDecl* type) { TINT_ASSERT(AST, type); TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, type, program_id); type_decls_.push_back(type); global_declarations_.push_back(type); } -void Module::AddFunction(ast::Function* func) { +void Module::AddFunction(const ast::Function* func) { TINT_ASSERT(AST, func); TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, func, program_id); functions_.push_back(func); global_declarations_.push_back(func); } -Module* Module::Clone(CloneContext* ctx) const { +const Module* Module::Clone(CloneContext* ctx) const { auto* out = ctx->dst->create(); out->Copy(ctx, this); return out; diff --git a/src/ast/module.h b/src/ast/module.h index a4e392c9e2..012a73d644 100644 --- a/src/ast/module.h +++ b/src/ast/module.h @@ -42,23 +42,23 @@ class Module : public Castable { /// the order they were declared in the source program Module(ProgramID pid, const Source& src, - std::vector global_decls); + std::vector global_decls); /// Destructor ~Module() override; /// @returns the ordered global declarations for the translation unit - const std::vector& GlobalDeclarations() const { + const std::vector& GlobalDeclarations() const { return global_declarations_; } /// Add a global variable to the Builder /// @param var the variable to add - void AddGlobalVariable(ast::Variable* var); + void AddGlobalVariable(const Variable* var); /// @returns true if the module has the global declaration `decl` /// @param decl the declaration to check - bool HasGlobalDeclaration(ast::Node* decl) const { + bool HasGlobalDeclaration(Node* decl) const { for (auto* d : global_declarations_) { if (d == decl) { return true; @@ -75,18 +75,18 @@ class Module : public Castable { /// Adds a type declaration to the Builder. /// @param decl the type declaration to add - void AddTypeDecl(ast::TypeDecl* decl); + void AddTypeDecl(const TypeDecl* decl); /// @returns the TypeDecl registered as a TypeDecl() /// @param name the name of the type to search for - const ast::TypeDecl* LookupType(Symbol name) const; + const TypeDecl* LookupType(Symbol name) const; /// @returns the declared types in the translation unit - const std::vector& TypeDecls() const { return type_decls_; } + const std::vector& TypeDecls() const { return type_decls_; } /// Add a function to the Builder /// @param func the function to add - void AddFunction(ast::Function* func); + void AddFunction(const Function* func); /// @returns the functions declared in the translation unit const FunctionList& Functions() const { return functions_; } @@ -95,7 +95,7 @@ class Module : public Castable { /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - Module* Clone(CloneContext* ctx) const override; + const Module* Clone(CloneContext* ctx) const override; /// Copy copies the content of the Module src into this module. /// @param ctx the clone context @@ -103,8 +103,8 @@ class Module : public Castable { void Copy(CloneContext* ctx, const Module* src); private: - std::vector global_declarations_; - std::vector type_decls_; + std::vector global_declarations_; + std::vector type_decls_; FunctionList functions_; VariableList global_variables_; }; diff --git a/src/ast/module_test.cc b/src/ast/module_test.cc index e36a2b8ab8..795988f7e4 100644 --- a/src/ast/module_test.cc +++ b/src/ast/module_test.cc @@ -106,15 +106,15 @@ TEST_F(ModuleTest, CloneOrder) { // declaration that triggered the ReplaceAll(). ProgramBuilder cloned; CloneContext ctx(&cloned, &p); - ctx.ReplaceAll([&](ast::Function*) -> ast::Function* { + ctx.ReplaceAll([&](const ast::Function*) -> const ast::Function* { ctx.dst->Alias("inserted_before_F", cloned.ty.u32()); return nullptr; }); - ctx.ReplaceAll([&](ast::Alias*) -> ast::Alias* { + ctx.ReplaceAll([&](const ast::Alias*) -> const ast::Alias* { ctx.dst->Alias("inserted_before_A", cloned.ty.u32()); return nullptr; }); - ctx.ReplaceAll([&](ast::Variable*) -> ast::Variable* { + ctx.ReplaceAll([&](const ast::Variable*) -> const ast::Variable* { ctx.dst->Alias("inserted_before_V", cloned.ty.u32()); return nullptr; }); diff --git a/src/ast/multisampled_texture.cc b/src/ast/multisampled_texture.cc index bd8127e970..b1aa36c43c 100644 --- a/src/ast/multisampled_texture.cc +++ b/src/ast/multisampled_texture.cc @@ -24,7 +24,7 @@ namespace ast { MultisampledTexture::MultisampledTexture(ProgramID pid, const Source& src, TextureDimension d, - Type* ty) + const Type* ty) : Base(pid, src, d), type(ty) { TINT_ASSERT(AST, type); } @@ -41,7 +41,7 @@ std::string MultisampledTexture::FriendlyName( return out.str(); } -MultisampledTexture* MultisampledTexture::Clone(CloneContext* ctx) const { +const MultisampledTexture* MultisampledTexture::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); auto* ty = ctx->Clone(type); diff --git a/src/ast/multisampled_texture.h b/src/ast/multisampled_texture.h index c1cef57a16..8cf65abb02 100644 --- a/src/ast/multisampled_texture.h +++ b/src/ast/multisampled_texture.h @@ -33,7 +33,7 @@ class MultisampledTexture : public Castable { MultisampledTexture(ProgramID pid, const Source& src, TextureDimension dim, - Type* type); + const Type* type); /// Move constructor MultisampledTexture(MultisampledTexture&&); ~MultisampledTexture() override; @@ -46,10 +46,10 @@ class MultisampledTexture : public Castable { /// Clones this type and all transitive types using the `CloneContext` `ctx`. /// @param ctx the clone context /// @return the newly cloned type - MultisampledTexture* Clone(CloneContext* ctx) const override; + const MultisampledTexture* Clone(CloneContext* ctx) const override; /// The subtype of the multisampled texture - Type* const type; + const Type* const type; }; } // namespace ast diff --git a/src/ast/node.h b/src/ast/node.h index 242f7a38df..fc0bc7f03d 100644 --- a/src/ast/node.h +++ b/src/ast/node.h @@ -38,10 +38,10 @@ class Node : public Castable { ~Node() override; /// The identifier of the program that owns this node - ProgramID const program_id; + const ProgramID program_id; /// The node source data - Source const source; + const Source source; protected: /// Create a new node diff --git a/src/ast/override_decoration.cc b/src/ast/override_decoration.cc index 6227d9ce7f..6df009f9c8 100644 --- a/src/ast/override_decoration.cc +++ b/src/ast/override_decoration.cc @@ -37,7 +37,7 @@ std::string OverrideDecoration::Name() const { return "override"; } -OverrideDecoration* OverrideDecoration::Clone(CloneContext* ctx) const { +const OverrideDecoration* OverrideDecoration::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); if (has_value) { diff --git a/src/ast/override_decoration.h b/src/ast/override_decoration.h index 714498aa3b..be1719874c 100644 --- a/src/ast/override_decoration.h +++ b/src/ast/override_decoration.h @@ -43,13 +43,13 @@ class OverrideDecoration : public Castable { /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - OverrideDecoration* Clone(CloneContext* ctx) const override; + const OverrideDecoration* Clone(CloneContext* ctx) const override; /// True if an override id was specified - bool const has_value; + const bool has_value; /// The override id value - uint32_t const value; + const uint32_t value; }; } // namespace ast diff --git a/src/ast/pointer.cc b/src/ast/pointer.cc index 4ae782dde1..2ba6d1b5c7 100644 --- a/src/ast/pointer.cc +++ b/src/ast/pointer.cc @@ -23,7 +23,7 @@ namespace ast { Pointer::Pointer(ProgramID pid, const Source& src, - Type* const subtype, + const Type* const subtype, ast::StorageClass sc, ast::Access ac) : Base(pid, src), type(subtype), storage_class(sc), access(ac) {} @@ -46,7 +46,7 @@ Pointer::Pointer(Pointer&&) = default; Pointer::~Pointer() = default; -Pointer* Pointer::Clone(CloneContext* ctx) const { +const Pointer* Pointer::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); auto* ty = ctx->Clone(type); diff --git a/src/ast/pointer.h b/src/ast/pointer.h index b621c954f4..1cba57baa8 100644 --- a/src/ast/pointer.h +++ b/src/ast/pointer.h @@ -35,7 +35,7 @@ class Pointer : public Castable { /// @param access the access control of the pointer Pointer(ProgramID pid, const Source& src, - Type* const subtype, + const Type* const subtype, ast::StorageClass storage_class, ast::Access access); /// Move constructor @@ -50,10 +50,10 @@ class Pointer : public Castable { /// Clones this type and all transitive types using the `CloneContext` `ctx`. /// @param ctx the clone context /// @return the newly cloned type - Pointer* Clone(CloneContext* ctx) const override; + const Pointer* Clone(CloneContext* ctx) const override; /// The pointee type - Type* const type; + const Type* const type; /// The storage class of the pointer ast::StorageClass const storage_class; diff --git a/src/ast/return_statement.cc b/src/ast/return_statement.cc index e48be37362..fe0c9460d0 100644 --- a/src/ast/return_statement.cc +++ b/src/ast/return_statement.cc @@ -26,7 +26,7 @@ ReturnStatement::ReturnStatement(ProgramID pid, const Source& src) ReturnStatement::ReturnStatement(ProgramID pid, const Source& src, - Expression* val) + const Expression* val) : Base(pid, src), value(val) { TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, value, program_id); } @@ -35,7 +35,7 @@ ReturnStatement::ReturnStatement(ReturnStatement&&) = default; ReturnStatement::~ReturnStatement() = default; -ReturnStatement* ReturnStatement::Clone(CloneContext* ctx) const { +const ReturnStatement* ReturnStatement::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); auto* ret = ctx->Clone(value); diff --git a/src/ast/return_statement.h b/src/ast/return_statement.h index 79e1b35e97..e00f8ba557 100644 --- a/src/ast/return_statement.h +++ b/src/ast/return_statement.h @@ -33,7 +33,7 @@ class ReturnStatement : public Castable { /// @param pid the identifier of the program that owns this node /// @param src the source of this node /// @param value the return value - ReturnStatement(ProgramID pid, const Source& src, Expression* value); + ReturnStatement(ProgramID pid, const Source& src, const Expression* value); /// Move constructor ReturnStatement(ReturnStatement&&); ~ReturnStatement() override; @@ -42,10 +42,10 @@ class ReturnStatement : public Castable { /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - ReturnStatement* Clone(CloneContext* ctx) const override; + const ReturnStatement* Clone(CloneContext* ctx) const override; /// The value returned. May be null. - Expression* const value; + const Expression* const value; }; } // namespace ast diff --git a/src/ast/sampled_texture.cc b/src/ast/sampled_texture.cc index 11e0d17d56..da010a0d79 100644 --- a/src/ast/sampled_texture.cc +++ b/src/ast/sampled_texture.cc @@ -24,8 +24,8 @@ namespace ast { SampledTexture::SampledTexture(ProgramID pid, const Source& src, TextureDimension d, - Type const* ty) - : Base(pid, src, d), type(const_cast(ty)) { + const Type* ty) + : Base(pid, src, d), type(ty) { TINT_ASSERT(AST, type); } @@ -39,7 +39,7 @@ std::string SampledTexture::FriendlyName(const SymbolTable& symbols) const { return out.str(); } -SampledTexture* SampledTexture::Clone(CloneContext* ctx) const { +const SampledTexture* SampledTexture::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); auto* ty = ctx->Clone(type); diff --git a/src/ast/sampled_texture.h b/src/ast/sampled_texture.h index 05307673f7..4cae22542c 100644 --- a/src/ast/sampled_texture.h +++ b/src/ast/sampled_texture.h @@ -33,7 +33,7 @@ class SampledTexture : public Castable { SampledTexture(ProgramID pid, const Source& src, TextureDimension dim, - Type const* type); + const Type* type); /// Move constructor SampledTexture(SampledTexture&&); ~SampledTexture() override; @@ -46,10 +46,10 @@ class SampledTexture : public Castable { /// Clones this type and all transitive types using the `CloneContext` `ctx`. /// @param ctx the clone context /// @return the newly cloned type - SampledTexture* Clone(CloneContext* ctx) const override; + const SampledTexture* Clone(CloneContext* ctx) const override; /// The subtype of the sampled texture - Type* const type; + const Type* const type; }; } // namespace ast diff --git a/src/ast/sampler.cc b/src/ast/sampler.cc index e2387bbab9..a0b926137e 100644 --- a/src/ast/sampler.cc +++ b/src/ast/sampler.cc @@ -44,7 +44,7 @@ std::string Sampler::FriendlyName(const SymbolTable&) const { return kind == SamplerKind::kSampler ? "sampler" : "sampler_comparison"; } -Sampler* Sampler::Clone(CloneContext* ctx) const { +const Sampler* Sampler::Clone(CloneContext* ctx) const { auto src = ctx->Clone(source); return ctx->dst->create(src, kind); } diff --git a/src/ast/sampler.h b/src/ast/sampler.h index 64bf4ea2f2..a2eacc47a0 100644 --- a/src/ast/sampler.h +++ b/src/ast/sampler.h @@ -58,10 +58,10 @@ class Sampler : public Castable { /// Clones this type and all transitive types using the `CloneContext` `ctx`. /// @param ctx the clone context /// @return the newly cloned type - Sampler* Clone(CloneContext* ctx) const override; + const Sampler* Clone(CloneContext* ctx) const override; /// The sampler type - SamplerKind const kind; + const SamplerKind kind; }; } // namespace ast diff --git a/src/ast/scalar_constructor_expression.cc b/src/ast/scalar_constructor_expression.cc index 74e6e78c07..8085188c91 100644 --- a/src/ast/scalar_constructor_expression.cc +++ b/src/ast/scalar_constructor_expression.cc @@ -23,7 +23,7 @@ namespace ast { ScalarConstructorExpression::ScalarConstructorExpression(ProgramID pid, const Source& src, - Literal* lit) + const Literal* lit) : Base(pid, src), literal(lit) { TINT_ASSERT(AST, literal); TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, literal, program_id); @@ -34,7 +34,7 @@ ScalarConstructorExpression::ScalarConstructorExpression( ScalarConstructorExpression::~ScalarConstructorExpression() = default; -ScalarConstructorExpression* ScalarConstructorExpression::Clone( +const ScalarConstructorExpression* ScalarConstructorExpression::Clone( CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); diff --git a/src/ast/scalar_constructor_expression.h b/src/ast/scalar_constructor_expression.h index 47412ac493..b80650e2bb 100644 --- a/src/ast/scalar_constructor_expression.h +++ b/src/ast/scalar_constructor_expression.h @@ -31,7 +31,7 @@ class ScalarConstructorExpression /// @param literal the const literal ScalarConstructorExpression(ProgramID pid, const Source& src, - Literal* literal); + const Literal* literal); /// Move constructor ScalarConstructorExpression(ScalarConstructorExpression&&); ~ScalarConstructorExpression() override; @@ -40,10 +40,10 @@ class ScalarConstructorExpression /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - ScalarConstructorExpression* Clone(CloneContext* ctx) const override; + const ScalarConstructorExpression* Clone(CloneContext* ctx) const override; /// The literal value - Literal* const literal; + const Literal* const literal; }; } // namespace ast diff --git a/src/ast/sint_literal.cc b/src/ast/sint_literal.cc index 2e6f23220f..99f291dc22 100644 --- a/src/ast/sint_literal.cc +++ b/src/ast/sint_literal.cc @@ -30,7 +30,7 @@ uint32_t SintLiteral::ValueAsU32() const { return static_cast(value); } -SintLiteral* SintLiteral::Clone(CloneContext* ctx) const { +const SintLiteral* SintLiteral::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); return ctx->dst->create(src, value); diff --git a/src/ast/sint_literal.h b/src/ast/sint_literal.h index 75f9aa365f..ec807d6860 100644 --- a/src/ast/sint_literal.h +++ b/src/ast/sint_literal.h @@ -39,10 +39,10 @@ class SintLiteral : public Castable { /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - SintLiteral* Clone(CloneContext* ctx) const override; + const SintLiteral* Clone(CloneContext* ctx) const override; /// The int literal value - int32_t const value; + const int32_t value; }; } // namespace ast diff --git a/src/ast/stage_decoration.cc b/src/ast/stage_decoration.cc index 3fd66ee646..eafc1bc940 100644 --- a/src/ast/stage_decoration.cc +++ b/src/ast/stage_decoration.cc @@ -34,7 +34,7 @@ std::string StageDecoration::Name() const { return "stage"; } -StageDecoration* StageDecoration::Clone(CloneContext* ctx) const { +const StageDecoration* StageDecoration::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); return ctx->dst->create(src, stage); diff --git a/src/ast/stage_decoration.h b/src/ast/stage_decoration.h index acd035923f..ac9069019f 100644 --- a/src/ast/stage_decoration.h +++ b/src/ast/stage_decoration.h @@ -42,10 +42,10 @@ class StageDecoration : public Castable { /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - StageDecoration* Clone(CloneContext* ctx) const override; + const StageDecoration* Clone(CloneContext* ctx) const override; /// The pipeline stage - PipelineStage const stage; + const PipelineStage stage; }; } // namespace ast diff --git a/src/ast/statement.h b/src/ast/statement.h index bc31e37ee6..567545bd1d 100644 --- a/src/ast/statement.h +++ b/src/ast/statement.h @@ -40,7 +40,7 @@ class Statement : public Castable { }; /// A list of statements -using StatementList = std::vector; +using StatementList = std::vector; } // namespace ast } // namespace tint diff --git a/src/ast/storage_texture.cc b/src/ast/storage_texture.cc index 6c8783f040..5b8fdcda72 100644 --- a/src/ast/storage_texture.cc +++ b/src/ast/storage_texture.cc @@ -144,7 +144,7 @@ StorageTexture::StorageTexture(ProgramID pid, const Source& src, TextureDimension d, ImageFormat fmt, - Type* subtype, + const Type* subtype, Access ac) : Base(pid, src, d), format(fmt), type(subtype), access(ac) {} @@ -158,7 +158,7 @@ std::string StorageTexture::FriendlyName(const SymbolTable&) const { return out.str(); } -StorageTexture* StorageTexture::Clone(CloneContext* ctx) const { +const StorageTexture* StorageTexture::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); auto* ty = ctx->Clone(type); diff --git a/src/ast/storage_texture.h b/src/ast/storage_texture.h index d09bb1a7a6..5e56abafa6 100644 --- a/src/ast/storage_texture.h +++ b/src/ast/storage_texture.h @@ -84,7 +84,7 @@ class StorageTexture : public Castable { const Source& src, TextureDimension dim, ImageFormat format, - Type* subtype, + const Type* subtype, Access access_control); /// Move constructor @@ -99,7 +99,7 @@ class StorageTexture : public Castable { /// Clones this type and all transitive types using the `CloneContext` `ctx`. /// @param ctx the clone context /// @return the newly cloned type - StorageTexture* Clone(CloneContext* ctx) const override; + const StorageTexture* Clone(CloneContext* ctx) const override; /// @param format the storage texture image format /// @param builder the ProgramBuilder used to build the returned type @@ -107,13 +107,13 @@ class StorageTexture : public Castable { static Type* SubtypeFor(ImageFormat format, ProgramBuilder& builder); /// The image format - ImageFormat const format; + const ImageFormat format; /// The storage subtype - Type* const type; + const Type* const type; /// The access control - Access const access; + const Access access; }; } // namespace ast diff --git a/src/ast/stride_decoration.cc b/src/ast/stride_decoration.cc index 08d7ee9d39..9aa8bb1bec 100644 --- a/src/ast/stride_decoration.cc +++ b/src/ast/stride_decoration.cc @@ -32,7 +32,7 @@ std::string StrideDecoration::Name() const { return "stride"; } -StrideDecoration* StrideDecoration::Clone(CloneContext* ctx) const { +const StrideDecoration* StrideDecoration::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); return ctx->dst->create(src, stride); diff --git a/src/ast/stride_decoration.h b/src/ast/stride_decoration.h index c9cf3e35e7..5053c3ae2d 100644 --- a/src/ast/stride_decoration.h +++ b/src/ast/stride_decoration.h @@ -39,10 +39,10 @@ class StrideDecoration : public Castable { /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - StrideDecoration* Clone(CloneContext* ctx) const override; + const StrideDecoration* Clone(CloneContext* ctx) const override; /// The stride value - uint32_t const stride; + const uint32_t stride; }; } // namespace ast diff --git a/src/ast/struct.cc b/src/ast/struct.cc index cb9106785f..7868caa487 100644 --- a/src/ast/struct.cc +++ b/src/ast/struct.cc @@ -48,7 +48,7 @@ bool Struct::IsBlockDecorated() const { return HasDecoration(decorations); } -Struct* Struct::Clone(CloneContext* ctx) const { +const Struct* Struct::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); auto n = ctx->Clone(name); diff --git a/src/ast/struct.h b/src/ast/struct.h index 6235b2e01e..f3a08c9307 100644 --- a/src/ast/struct.h +++ b/src/ast/struct.h @@ -51,13 +51,13 @@ class Struct : public Castable { /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - Struct* Clone(CloneContext* ctx) const override; + const Struct* Clone(CloneContext* ctx) const override; /// The members - StructMemberList const members; + const StructMemberList members; /// The struct decorations - DecorationList const decorations; + const DecorationList decorations; }; } // namespace ast diff --git a/src/ast/struct_block_decoration.cc b/src/ast/struct_block_decoration.cc index e41b34e42a..73f2530d09 100644 --- a/src/ast/struct_block_decoration.cc +++ b/src/ast/struct_block_decoration.cc @@ -32,7 +32,8 @@ std::string StructBlockDecoration::Name() const { return "block"; } -StructBlockDecoration* StructBlockDecoration::Clone(CloneContext* ctx) const { +const StructBlockDecoration* StructBlockDecoration::Clone( + CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); return ctx->dst->create(src); diff --git a/src/ast/struct_block_decoration.h b/src/ast/struct_block_decoration.h index 8a3cbffe40..481591f47b 100644 --- a/src/ast/struct_block_decoration.h +++ b/src/ast/struct_block_decoration.h @@ -40,7 +40,7 @@ class StructBlockDecoration /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - StructBlockDecoration* Clone(CloneContext* ctx) const override; + const StructBlockDecoration* Clone(CloneContext* ctx) const override; }; } // namespace ast diff --git a/src/ast/struct_member.cc b/src/ast/struct_member.cc index 73a34600ff..c33240b659 100644 --- a/src/ast/struct_member.cc +++ b/src/ast/struct_member.cc @@ -24,7 +24,7 @@ namespace ast { StructMember::StructMember(ProgramID pid, const Source& src, const Symbol& sym, - ast::Type* ty, + const ast::Type* ty, DecorationList decos) : Base(pid, src), symbol(sym), type(ty), decorations(std::move(decos)) { TINT_ASSERT(AST, type); @@ -40,7 +40,7 @@ StructMember::StructMember(StructMember&&) = default; StructMember::~StructMember() = default; -StructMember* StructMember::Clone(CloneContext* ctx) const { +const StructMember* StructMember::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); auto sym = ctx->Clone(symbol); diff --git a/src/ast/struct_member.h b/src/ast/struct_member.h index ac3725bde5..eaad5485f8 100644 --- a/src/ast/struct_member.h +++ b/src/ast/struct_member.h @@ -38,7 +38,7 @@ class StructMember : public Castable { StructMember(ProgramID pid, const Source& src, const Symbol& sym, - ast::Type* type, + const ast::Type* type, DecorationList decorations); /// Move constructor StructMember(StructMember&&); @@ -49,20 +49,20 @@ class StructMember : public Castable { /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - StructMember* Clone(CloneContext* ctx) const override; + const StructMember* Clone(CloneContext* ctx) const override; /// The symbol - Symbol const symbol; + const Symbol symbol; /// The type - ast::Type* const type; + const ast::Type* const type; /// The decorations - DecorationList const decorations; + const DecorationList decorations; }; /// A list of struct members -using StructMemberList = std::vector; +using StructMemberList = std::vector; } // namespace ast } // namespace tint diff --git a/src/ast/struct_member_align_decoration.cc b/src/ast/struct_member_align_decoration.cc index e2fb028b30..fc84a45088 100644 --- a/src/ast/struct_member_align_decoration.cc +++ b/src/ast/struct_member_align_decoration.cc @@ -35,7 +35,7 @@ std::string StructMemberAlignDecoration::Name() const { return "align"; } -StructMemberAlignDecoration* StructMemberAlignDecoration::Clone( +const StructMemberAlignDecoration* StructMemberAlignDecoration::Clone( CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); diff --git a/src/ast/struct_member_align_decoration.h b/src/ast/struct_member_align_decoration.h index 64c4dfa634..13febe9337 100644 --- a/src/ast/struct_member_align_decoration.h +++ b/src/ast/struct_member_align_decoration.h @@ -41,10 +41,10 @@ class StructMemberAlignDecoration /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - StructMemberAlignDecoration* Clone(CloneContext* ctx) const override; + const StructMemberAlignDecoration* Clone(CloneContext* ctx) const override; /// The align value - uint32_t const align; + const uint32_t align; }; } // namespace ast diff --git a/src/ast/struct_member_offset_decoration.cc b/src/ast/struct_member_offset_decoration.cc index a482fbd39e..ac73b24d85 100644 --- a/src/ast/struct_member_offset_decoration.cc +++ b/src/ast/struct_member_offset_decoration.cc @@ -34,7 +34,7 @@ std::string StructMemberOffsetDecoration::Name() const { return "offset"; } -StructMemberOffsetDecoration* StructMemberOffsetDecoration::Clone( +const StructMemberOffsetDecoration* StructMemberOffsetDecoration::Clone( CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); diff --git a/src/ast/struct_member_offset_decoration.h b/src/ast/struct_member_offset_decoration.h index 753c647b13..8c6f3ac695 100644 --- a/src/ast/struct_member_offset_decoration.h +++ b/src/ast/struct_member_offset_decoration.h @@ -51,10 +51,10 @@ class StructMemberOffsetDecoration /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - StructMemberOffsetDecoration* Clone(CloneContext* ctx) const override; + const StructMemberOffsetDecoration* Clone(CloneContext* ctx) const override; /// The offset value - uint32_t const offset; + const uint32_t offset; }; } // namespace ast diff --git a/src/ast/struct_member_size_decoration.cc b/src/ast/struct_member_size_decoration.cc index 0155c41f0f..b54c8dec28 100644 --- a/src/ast/struct_member_size_decoration.cc +++ b/src/ast/struct_member_size_decoration.cc @@ -35,7 +35,7 @@ std::string StructMemberSizeDecoration::Name() const { return "size"; } -StructMemberSizeDecoration* StructMemberSizeDecoration::Clone( +const StructMemberSizeDecoration* StructMemberSizeDecoration::Clone( CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); diff --git a/src/ast/struct_member_size_decoration.h b/src/ast/struct_member_size_decoration.h index 50bbed0fcc..1ca7d773a5 100644 --- a/src/ast/struct_member_size_decoration.h +++ b/src/ast/struct_member_size_decoration.h @@ -41,10 +41,10 @@ class StructMemberSizeDecoration /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - StructMemberSizeDecoration* Clone(CloneContext* ctx) const override; + const StructMemberSizeDecoration* Clone(CloneContext* ctx) const override; /// The size value - uint32_t const size; + const uint32_t size; }; } // namespace ast diff --git a/src/ast/switch_statement.cc b/src/ast/switch_statement.cc index 2013fccdc8..eeb74bf3d9 100644 --- a/src/ast/switch_statement.cc +++ b/src/ast/switch_statement.cc @@ -23,7 +23,7 @@ namespace ast { SwitchStatement::SwitchStatement(ProgramID pid, const Source& src, - Expression* cond, + const Expression* cond, CaseStatementList b) : Base(pid, src), condition(cond), body(b) { TINT_ASSERT(AST, condition); @@ -38,7 +38,7 @@ SwitchStatement::SwitchStatement(SwitchStatement&&) = default; SwitchStatement::~SwitchStatement() = default; -SwitchStatement* SwitchStatement::Clone(CloneContext* ctx) const { +const SwitchStatement* SwitchStatement::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); auto* cond = ctx->Clone(condition); diff --git a/src/ast/switch_statement.h b/src/ast/switch_statement.h index 8f9a8e7946..711e55303b 100644 --- a/src/ast/switch_statement.h +++ b/src/ast/switch_statement.h @@ -31,7 +31,7 @@ class SwitchStatement : public Castable { /// @param body the switch body SwitchStatement(ProgramID pid, const Source& src, - Expression* condition, + const Expression* condition, CaseStatementList body); /// Move constructor SwitchStatement(SwitchStatement&&); @@ -44,13 +44,13 @@ class SwitchStatement : public Castable { /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - SwitchStatement* Clone(CloneContext* ctx) const override; + const SwitchStatement* Clone(CloneContext* ctx) const override; /// The switch condition or nullptr if none set - Expression* const condition; + const Expression* const condition; /// The Switch body - CaseStatementList const body; + const CaseStatementList body; SwitchStatement(const SwitchStatement&) = delete; }; diff --git a/src/ast/texture.h b/src/ast/texture.h index 769d1ebea9..e3383a734f 100644 --- a/src/ast/texture.h +++ b/src/ast/texture.h @@ -74,7 +74,7 @@ class Texture : public Castable { ~Texture() override; /// The texture dimension - TextureDimension const dim; + const TextureDimension dim; }; } // namespace ast diff --git a/src/ast/type_constructor_expression.cc b/src/ast/type_constructor_expression.cc index bfae88bcf7..2745f2e12a 100644 --- a/src/ast/type_constructor_expression.cc +++ b/src/ast/type_constructor_expression.cc @@ -23,7 +23,7 @@ namespace ast { TypeConstructorExpression::TypeConstructorExpression(ProgramID pid, const Source& src, - ast::Type* ty, + const ast::Type* ty, ExpressionList vals) : Base(pid, src), type(ty), values(std::move(vals)) { TINT_ASSERT(AST, type); @@ -38,7 +38,7 @@ TypeConstructorExpression::TypeConstructorExpression( TypeConstructorExpression::~TypeConstructorExpression() = default; -TypeConstructorExpression* TypeConstructorExpression::Clone( +const TypeConstructorExpression* TypeConstructorExpression::Clone( CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); diff --git a/src/ast/type_constructor_expression.h b/src/ast/type_constructor_expression.h index b61d8ddb4b..94095e1ca5 100644 --- a/src/ast/type_constructor_expression.h +++ b/src/ast/type_constructor_expression.h @@ -36,7 +36,7 @@ class TypeConstructorExpression /// @param values the constructor values TypeConstructorExpression(ProgramID pid, const Source& src, - ast::Type* type, + const ast::Type* type, ExpressionList values); /// Move constructor TypeConstructorExpression(TypeConstructorExpression&&); @@ -46,13 +46,13 @@ class TypeConstructorExpression /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - TypeConstructorExpression* Clone(CloneContext* ctx) const override; + const TypeConstructorExpression* Clone(CloneContext* ctx) const override; /// The type - ast::Type* const type; + const ast::Type* const type; /// The values - ExpressionList const values; + const ExpressionList values; }; } // namespace ast diff --git a/src/ast/type_decl.h b/src/ast/type_decl.h index 2ad75c394e..d0d49e4fbc 100644 --- a/src/ast/type_decl.h +++ b/src/ast/type_decl.h @@ -36,7 +36,7 @@ class TypeDecl : public Castable { ~TypeDecl() override; /// The name of the type declaration - Symbol const name; + const Symbol name; }; } // namespace ast diff --git a/src/ast/type_name.cc b/src/ast/type_name.cc index 3ad3236dbb..66da08444d 100644 --- a/src/ast/type_name.cc +++ b/src/ast/type_name.cc @@ -32,7 +32,7 @@ std::string TypeName::FriendlyName(const SymbolTable& symbols) const { return symbols.NameFor(name); } -TypeName* TypeName::Clone(CloneContext* ctx) const { +const TypeName* TypeName::Clone(CloneContext* ctx) const { auto src = ctx->Clone(source); auto n = ctx->Clone(name); return ctx->dst->create(src, n); diff --git a/src/ast/type_name.h b/src/ast/type_name.h index 867bc96815..5e177ab233 100644 --- a/src/ast/type_name.h +++ b/src/ast/type_name.h @@ -35,7 +35,6 @@ class TypeName : public Castable { /// Destructor ~TypeName() override; - /// @param symbols the program's symbol table /// @returns the name for this type that closely resembles how it would be /// declared in WGSL. @@ -44,7 +43,7 @@ class TypeName : public Castable { /// Clones this type and all transitive types using the `CloneContext` `ctx`. /// @param ctx the clone context /// @return the newly cloned type - TypeName* Clone(CloneContext* ctx) const override; + const TypeName* Clone(CloneContext* ctx) const override; /// The type name Symbol name; diff --git a/src/ast/u32.cc b/src/ast/u32.cc index 9337861d86..efe362439c 100644 --- a/src/ast/u32.cc +++ b/src/ast/u32.cc @@ -31,7 +31,7 @@ std::string U32::FriendlyName(const SymbolTable&) const { return "u32"; } -U32* U32::Clone(CloneContext* ctx) const { +const U32* U32::Clone(CloneContext* ctx) const { auto src = ctx->Clone(source); return ctx->dst->create(src); } diff --git a/src/ast/u32.h b/src/ast/u32.h index 66714298af..5d7ba4488a 100644 --- a/src/ast/u32.h +++ b/src/ast/u32.h @@ -41,7 +41,7 @@ class U32 : public Castable { /// Clones this type and all transitive types using the `CloneContext` `ctx`. /// @param ctx the clone context /// @return the newly cloned type - U32* Clone(CloneContext* ctx) const override; + const U32* Clone(CloneContext* ctx) const override; }; } // namespace ast diff --git a/src/ast/uint_literal.cc b/src/ast/uint_literal.cc index b25db81960..27ebda044c 100644 --- a/src/ast/uint_literal.cc +++ b/src/ast/uint_literal.cc @@ -30,7 +30,7 @@ uint32_t UintLiteral::ValueAsU32() const { return value; } -UintLiteral* UintLiteral::Clone(CloneContext* ctx) const { +const UintLiteral* UintLiteral::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); return ctx->dst->create(src, value); diff --git a/src/ast/uint_literal.h b/src/ast/uint_literal.h index 3a2df6299c..3429758b53 100644 --- a/src/ast/uint_literal.h +++ b/src/ast/uint_literal.h @@ -39,10 +39,10 @@ class UintLiteral : public Castable { /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - UintLiteral* Clone(CloneContext* ctx) const override; + const UintLiteral* Clone(CloneContext* ctx) const override; /// The int literal value - uint32_t const value; + const uint32_t value; }; } // namespace ast diff --git a/src/ast/unary_op_expression.cc b/src/ast/unary_op_expression.cc index 21ebfec0a9..c80241a9cf 100644 --- a/src/ast/unary_op_expression.cc +++ b/src/ast/unary_op_expression.cc @@ -24,7 +24,7 @@ namespace ast { UnaryOpExpression::UnaryOpExpression(ProgramID pid, const Source& src, UnaryOp o, - Expression* e) + const Expression* e) : Base(pid, src), op(o), expr(e) { TINT_ASSERT(AST, expr); TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, expr, program_id); @@ -34,7 +34,7 @@ UnaryOpExpression::UnaryOpExpression(UnaryOpExpression&&) = default; UnaryOpExpression::~UnaryOpExpression() = default; -UnaryOpExpression* UnaryOpExpression::Clone(CloneContext* ctx) const { +const UnaryOpExpression* UnaryOpExpression::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); auto* e = ctx->Clone(expr); diff --git a/src/ast/unary_op_expression.h b/src/ast/unary_op_expression.h index baecf4a832..1d3e25a7a6 100644 --- a/src/ast/unary_op_expression.h +++ b/src/ast/unary_op_expression.h @@ -32,7 +32,7 @@ class UnaryOpExpression : public Castable { UnaryOpExpression(ProgramID program_id, const Source& source, UnaryOp op, - Expression* expr); + const Expression* expr); /// Move constructor UnaryOpExpression(UnaryOpExpression&&); ~UnaryOpExpression() override; @@ -41,13 +41,13 @@ class UnaryOpExpression : public Castable { /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - UnaryOpExpression* Clone(CloneContext* ctx) const override; + const UnaryOpExpression* Clone(CloneContext* ctx) const override; /// The op - UnaryOp const op; + const UnaryOp op; /// The expression - Expression* const expr; + const Expression* const expr; }; } // namespace ast diff --git a/src/ast/variable.cc b/src/ast/variable.cc index f5da167d78..5f9baf89b2 100644 --- a/src/ast/variable.cc +++ b/src/ast/variable.cc @@ -30,11 +30,11 @@ Variable::Variable(ProgramID pid, Access da, const ast::Type* ty, bool constant, - Expression* ctor, + const Expression* ctor, DecorationList decos) : Base(pid, src), symbol(sym), - type(const_cast(ty)), + type(ty), is_const(constant), constructor(ctor), decorations(std::move(decos)), @@ -50,8 +50,8 @@ Variable::Variable(Variable&&) = default; Variable::~Variable() = default; VariableBindingPoint Variable::BindingPoint() const { - GroupDecoration* group = nullptr; - BindingDecoration* binding = nullptr; + const GroupDecoration* group = nullptr; + const BindingDecoration* binding = nullptr; for (auto* deco : decorations) { if (auto* g = deco->As()) { group = g; @@ -62,7 +62,7 @@ VariableBindingPoint Variable::BindingPoint() const { return VariableBindingPoint{group, binding}; } -Variable* Variable::Clone(CloneContext* ctx) const { +const Variable* Variable::Clone(CloneContext* ctx) const { auto src = ctx->Clone(source); auto sym = ctx->Clone(symbol); auto* ty = ctx->Clone(type); diff --git a/src/ast/variable.h b/src/ast/variable.h index 66d48113f8..3ef3b8aaed 100644 --- a/src/ast/variable.h +++ b/src/ast/variable.h @@ -35,9 +35,9 @@ class Type; /// VariableBindingPoint holds a group and binding decoration. struct VariableBindingPoint { /// The `[[group]]` part of the binding point - GroupDecoration* group = nullptr; + const GroupDecoration* group = nullptr; /// The `[[binding]]` part of the binding point - BindingDecoration* binding = nullptr; + const BindingDecoration* binding = nullptr; /// @returns true if the BindingPoint has a valid group and binding /// decoration. @@ -116,7 +116,7 @@ class Variable : public Castable { Access declared_access, const ast::Type* type, bool is_const, - Expression* constructor, + const Expression* constructor, DecorationList decorations); /// Move constructor Variable(Variable&&); @@ -130,32 +130,32 @@ class Variable : public Castable { /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - Variable* Clone(CloneContext* ctx) const override; + const Variable* Clone(CloneContext* ctx) const override; /// The variable symbol - Symbol const symbol; + const Symbol symbol; /// The variable type - ast::Type* const type; + const ast::Type* const type; /// True if this is a constant, false otherwise - bool const is_const; + const bool is_const; /// The constructor expression or nullptr if none set - Expression* const constructor; + const Expression* const constructor; /// The decorations attached to this variable - DecorationList const decorations; + const DecorationList decorations; /// The declared storage class - StorageClass const declared_storage_class; + const StorageClass declared_storage_class; /// The declared access control - Access const declared_access; + const Access declared_access; }; /// A list of variables -using VariableList = std::vector; +using VariableList = std::vector; } // namespace ast } // namespace tint diff --git a/src/ast/variable_decl_statement.cc b/src/ast/variable_decl_statement.cc index d4ffaca9d9..8e65919b81 100644 --- a/src/ast/variable_decl_statement.cc +++ b/src/ast/variable_decl_statement.cc @@ -23,7 +23,7 @@ namespace ast { VariableDeclStatement::VariableDeclStatement(ProgramID pid, const Source& src, - Variable* var) + const Variable* var) : Base(pid, src), variable(var) { TINT_ASSERT(AST, variable); TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, variable, program_id); @@ -33,7 +33,8 @@ VariableDeclStatement::VariableDeclStatement(VariableDeclStatement&&) = default; VariableDeclStatement::~VariableDeclStatement() = default; -VariableDeclStatement* VariableDeclStatement::Clone(CloneContext* ctx) const { +const VariableDeclStatement* VariableDeclStatement::Clone( + CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); auto* var = ctx->Clone(variable); diff --git a/src/ast/variable_decl_statement.h b/src/ast/variable_decl_statement.h index 33351795d0..8a148b2809 100644 --- a/src/ast/variable_decl_statement.h +++ b/src/ast/variable_decl_statement.h @@ -31,7 +31,7 @@ class VariableDeclStatement /// @param variable the variable VariableDeclStatement(ProgramID program_id, const Source& source, - Variable* variable); + const Variable* variable); /// Move constructor VariableDeclStatement(VariableDeclStatement&&); ~VariableDeclStatement() override; @@ -40,10 +40,10 @@ class VariableDeclStatement /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - VariableDeclStatement* Clone(CloneContext* ctx) const override; + const VariableDeclStatement* Clone(CloneContext* ctx) const override; /// The variable - Variable* const variable; + const Variable* const variable; }; } // namespace ast diff --git a/src/ast/vector.cc b/src/ast/vector.cc index 4fdd60eb4b..bea0dc2ded 100644 --- a/src/ast/vector.cc +++ b/src/ast/vector.cc @@ -22,10 +22,10 @@ namespace tint { namespace ast { Vector::Vector(ProgramID pid, - const Source& src, - Type const* subtype, + Source const& src, + const Type* subtype, uint32_t w) - : Base(pid, src), type(const_cast(subtype)), width(w) { + : Base(pid, src), type(subtype), width(w) { TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, subtype, program_id); TINT_ASSERT(AST, width > 1); TINT_ASSERT(AST, width < 5); @@ -41,7 +41,7 @@ std::string Vector::FriendlyName(const SymbolTable& symbols) const { return out.str(); } -Vector* Vector::Clone(CloneContext* ctx) const { +const Vector* Vector::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); auto* ty = ctx->Clone(type); diff --git a/src/ast/vector.h b/src/ast/vector.h index 91a53c5fbd..956594d680 100644 --- a/src/ast/vector.h +++ b/src/ast/vector.h @@ -30,7 +30,7 @@ class Vector : public Castable { /// @param src the source of this node /// @param subtype the vector element type /// @param width the number of elements in the vector - Vector(ProgramID pid, const Source& src, Type const* subtype, uint32_t width); + Vector(ProgramID pid, Source const& src, const Type* subtype, uint32_t width); /// Move constructor Vector(Vector&&); ~Vector() override; @@ -43,13 +43,13 @@ class Vector : public Castable { /// Clones this type and all transitive types using the `CloneContext` `ctx`. /// @param ctx the clone context /// @return the newly cloned type - Vector* Clone(CloneContext* ctx) const override; + const Vector* Clone(CloneContext* ctx) const override; /// The type of the vector elements - Type* const type; + const Type* const type; /// The number of elements in the vector - uint32_t const width; + const uint32_t width; }; } // namespace ast diff --git a/src/ast/void.cc b/src/ast/void.cc index 427d82755a..59c4670ba3 100644 --- a/src/ast/void.cc +++ b/src/ast/void.cc @@ -31,7 +31,7 @@ std::string Void::FriendlyName(const SymbolTable&) const { return "void"; } -Void* Void::Clone(CloneContext* ctx) const { +const Void* Void::Clone(CloneContext* ctx) const { auto src = ctx->Clone(source); return ctx->dst->create(src); } diff --git a/src/ast/void.h b/src/ast/void.h index 46cb3a23d5..a2b7af45a8 100644 --- a/src/ast/void.h +++ b/src/ast/void.h @@ -41,7 +41,7 @@ class Void : public Castable { /// Clones this type and all transitive types using the `CloneContext` `ctx`. /// @param ctx the clone context /// @return the newly cloned type - Void* Clone(CloneContext* ctx) const override; + const Void* Clone(CloneContext* ctx) const override; }; } // namespace ast diff --git a/src/ast/workgroup_decoration.cc b/src/ast/workgroup_decoration.cc index 271bb85506..cca599d2e0 100644 --- a/src/ast/workgroup_decoration.cc +++ b/src/ast/workgroup_decoration.cc @@ -25,9 +25,9 @@ namespace ast { WorkgroupDecoration::WorkgroupDecoration(ProgramID pid, const Source& src, - ast::Expression* x_, - ast::Expression* y_, - ast::Expression* z_) + const ast::Expression* x_, + const ast::Expression* y_, + const ast::Expression* z_) : Base(pid, src), x(x_), y(y_), z(z_) {} WorkgroupDecoration::~WorkgroupDecoration() = default; @@ -36,7 +36,7 @@ std::string WorkgroupDecoration::Name() const { return "workgroup_size"; } -WorkgroupDecoration* WorkgroupDecoration::Clone(CloneContext* ctx) const { +const WorkgroupDecoration* WorkgroupDecoration::Clone(CloneContext* ctx) const { // Clone arguments outside of create() call to have deterministic ordering auto src = ctx->Clone(source); auto* x_ = ctx->Clone(x); diff --git a/src/ast/workgroup_decoration.h b/src/ast/workgroup_decoration.h index daae3f0ece..119e3c4e50 100644 --- a/src/ast/workgroup_decoration.h +++ b/src/ast/workgroup_decoration.h @@ -37,14 +37,14 @@ class WorkgroupDecoration : public Castable { /// @param z the optional workgroup z dimension expression WorkgroupDecoration(ProgramID pid, const Source& src, - ast::Expression* x, - ast::Expression* y = nullptr, - ast::Expression* z = nullptr); + const ast::Expression* x, + const ast::Expression* y = nullptr, + const ast::Expression* z = nullptr); ~WorkgroupDecoration() override; /// @returns the workgroup dimensions - std::array Values() const { return {x, y, z}; } + std::array Values() const { return {x, y, z}; } /// @returns the WGSL name for the decoration std::string Name() const override; @@ -53,14 +53,14 @@ class WorkgroupDecoration : public Castable { /// `ctx`. /// @param ctx the clone context /// @return the newly cloned node - WorkgroupDecoration* Clone(CloneContext* ctx) const override; + const WorkgroupDecoration* Clone(CloneContext* ctx) const override; /// The workgroup x dimension. - ast::Expression* const x; + const ast::Expression* const x; /// The optional workgroup y dimension. May be null. - ast::Expression* const y = nullptr; + const ast::Expression* const y = nullptr; /// The optional workgroup z dimension. May be null. - ast::Expression* const z = nullptr; + const ast::Expression* const z = nullptr; }; } // namespace ast diff --git a/src/castable.h b/src/castable.h index ed075b0043..67f1dc22de 100644 --- a/src/castable.h +++ b/src/castable.h @@ -74,7 +74,8 @@ struct TypeInfo { /// @returns the static TypeInfo for the type T template static const TypeInfo& Of() { - return detail::TypeInfoOf::info; + using NO_CV = typename std::remove_cv::type; + return detail::TypeInfoOf::info; } }; diff --git a/src/clone_context.cc b/src/clone_context.cc index ed85d49453..e56f754190 100644 --- a/src/clone_context.cc +++ b/src/clone_context.cc @@ -63,13 +63,13 @@ void CloneContext::Clone() { ast::FunctionList CloneContext::Clone(const ast::FunctionList& v) { ast::FunctionList out; out.reserve(v.size()); - for (ast::Function* el : v) { + for (const ast::Function* el : v) { out.Add(Clone(el)); } return out; } -tint::Cloneable* CloneContext::CloneCloneable(Cloneable* object) { +const tint::Cloneable* CloneContext::CloneCloneable(const Cloneable* object) { // If the input is nullptr, there's nothing to clone - just return nullptr. if (object == nullptr) { return nullptr; @@ -97,7 +97,7 @@ tint::Cloneable* CloneContext::CloneCloneable(Cloneable* object) { return object->Clone(this); } -void CloneContext::CheckedCastFailure(Cloneable* got, +void CloneContext::CheckedCastFailure(const Cloneable* got, const TypeInfo& expected) { TINT_ICE(Clone, Diagnostics()) << "Cloned object was not of the expected type\n" diff --git a/src/clone_context.h b/src/clone_context.h index 04f5dac0e4..9d765a06eb 100644 --- a/src/clone_context.h +++ b/src/clone_context.h @@ -48,7 +48,7 @@ class Cloneable : public Castable { /// Performs a deep clone of this object using the CloneContext `ctx`. /// @param ctx the clone context /// @return the newly cloned object - virtual Cloneable* Clone(CloneContext* ctx) const = 0; + virtual const Cloneable* Clone(CloneContext* ctx) const = 0; }; /// @returns an invalid ProgramID @@ -97,7 +97,7 @@ class CloneContext { /// @param object the type deriving from Cloneable to clone /// @return the cloned node template - T* Clone(T* object) { + const T* Clone(const T* object) { if (src) { TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(Clone, src, object); } @@ -121,7 +121,7 @@ class CloneContext { /// @param a the type deriving from Cloneable to clone /// @return the cloned node template - T* CloneWithoutTransform(T* a) { + const T* CloneWithoutTransform(const T* a) { // If the input is nullptr, there's nothing to clone - just return nullptr. if (a == nullptr) { return nullptr; @@ -289,7 +289,9 @@ class CloneContext { } CloneableTransform transform; transform.typeinfo = &TypeInfo::Of(); - transform.function = [=](Cloneable* in) { return replacer(in->As()); }; + transform.function = [=](const Cloneable* in) { + return replacer(in->As()); + }; transforms_.emplace_back(std::move(transform)); return *this; } @@ -329,10 +331,10 @@ class CloneContext { template > - CloneContext& Replace(WHAT* what, WITH* with) { + CloneContext& Replace(const WHAT* what, const WITH* with) { TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(Clone, src, what); TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(Clone, dst, with); - replacements_[what] = [with]() -> Cloneable* { return with; }; + replacements_[what] = [with]() -> const Cloneable* { return with; }; return *this; } @@ -350,7 +352,7 @@ class CloneContext { /// assertion in debug builds, and undefined behavior in release builds. /// @returns this CloneContext so calls can be chained template > - CloneContext& Replace(WHAT* what, WITH&& with) { + CloneContext& Replace(const WHAT* what, WITH&& with) { TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(Clone, src, what); replacements_[what] = with; return *this; @@ -411,7 +413,7 @@ class CloneContext { template CloneContext& InsertBefore(const std::vector& vector, const BEFORE* before, - OBJECT* object) { + const OBJECT* object) { TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(Clone, src, before); TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(Clone, dst, object); if (std::find(vector.begin(), vector.end(), before) == vector.end()) { @@ -435,7 +437,7 @@ class CloneContext { template CloneContext& InsertAfter(const std::vector& vector, const AFTER* after, - OBJECT* object) { + const OBJECT* object) { TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(Clone, src, after); TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(Clone, dst, object); if (std::find(vector.begin(), vector.end(), after) == vector.end()) { @@ -473,7 +475,7 @@ class CloneContext { // TypeInfo of the Cloneable that the transform operates on const TypeInfo* typeinfo; - std::function function; + std::function function; }; CloneContext(const CloneContext&) = delete; @@ -482,11 +484,11 @@ class CloneContext { /// Cast `obj` from type `FROM` to type `TO`, returning the cast object. /// Reports an internal compiler error if the cast failed. template - TO* CheckedCast(FROM* obj) { + const TO* CheckedCast(const FROM* obj) { if (obj == nullptr) { return nullptr; } - if (TO* cast = obj->template As()) { + if (const TO* cast = obj->template As()) { return cast; } CheckedCastFailure(obj, TypeInfo::Of()); @@ -495,17 +497,17 @@ class CloneContext { /// Clones a Cloneable object, using any replacements or transforms that have /// been configured. - tint::Cloneable* CloneCloneable(Cloneable* object); + const Cloneable* CloneCloneable(const Cloneable* object); /// Adds an error diagnostic to Diagnostics() that the cloned object was not /// of the expected type. - void CheckedCastFailure(Cloneable* got, const TypeInfo& expected); + void CheckedCastFailure(const Cloneable* got, const TypeInfo& expected); /// @returns the diagnostic list of #dst diag::List& Diagnostics() const; - /// A vector of Cloneable* - using CloneableList = std::vector; + /// A vector of const Cloneable* + using CloneableList = std::vector; /// Transformations to be applied to a list (vector) struct ListTransforms { @@ -538,7 +540,7 @@ class CloneContext { /// A map of object in #src to functions that create their replacement in /// #dst - std::unordered_map> + std::unordered_map> replacements_; /// A map of symbol in #src to their cloned equivalent in #dst diff --git a/src/clone_context_test.cc b/src/clone_context_test.cc index e3720cb6d9..d51d4ae70a 100644 --- a/src/clone_context_test.cc +++ b/src/clone_context_test.cc @@ -31,13 +31,18 @@ struct Allocator { }; struct Node : public Castable { - Node(Allocator* alloc, Symbol n) : allocator(alloc), name(n) {} + Node(Allocator* alloc, + Symbol n, + const Node* node_a = nullptr, + const Node* node_b = nullptr, + const Node* node_c = nullptr) + : allocator(alloc), name(n), a(node_a), b(node_b), c(node_c) {} Allocator* const allocator; Symbol name; - Node* a = nullptr; - Node* b = nullptr; - Node* c = nullptr; - std::vector vec; + const Node* a = nullptr; + const Node* b = nullptr; + const Node* c = nullptr; + std::vector vec; Node* Clone(CloneContext* ctx) const override { auto* out = allocator->Create(ctx->Clone(name)); @@ -50,7 +55,12 @@ struct Node : public Castable { }; struct Replaceable : public Castable { - Replaceable(Allocator* alloc, Symbol n) : Base(alloc, n) {} + Replaceable(Allocator* alloc, + Symbol n, + const Node* node_a = nullptr, + const Node* node_b = nullptr, + const Node* node_c = nullptr) + : Base(alloc, n, node_a, node_b, node_c) {} }; struct Replacement : public Castable { @@ -71,8 +81,8 @@ struct ProgramNode : public Castable { : allocator(alloc), program_id(id), cloned_program_id(cloned_id) {} Allocator* const allocator; - ProgramID const program_id; - ProgramID const cloned_program_id; + const ProgramID program_id; + const ProgramID cloned_program_id; ProgramNode* Clone(CloneContext*) const override { return allocator->Create(cloned_program_id, cloned_program_id); @@ -86,16 +96,19 @@ ProgramID ProgramIDOf(const ProgramNode* node) { using CloneContextNodeTest = ::testing::Test; TEST_F(CloneContextNodeTest, Clone) { - Allocator a; + Allocator alloc; ProgramBuilder builder; - auto* original_root = a.Create(builder.Symbols().New("root")); - original_root->a = a.Create(builder.Symbols().New("a")); - original_root->a->b = a.Create(builder.Symbols().New("a->b")); - original_root->b = a.Create(builder.Symbols().New("b")); - original_root->b->a = original_root->a; // Aliased - original_root->b->b = a.Create(builder.Symbols().New("b->b")); - original_root->c = original_root->b; // Aliased + Node* original_root; + { + auto* a_b = alloc.Create(builder.Symbols().New("a->b")); + auto* a = alloc.Create(builder.Symbols().New("a"), nullptr, a_b); + auto* b_a = a; // Aliased + auto* b_b = alloc.Create(builder.Symbols().New("b->b")); + auto* b = alloc.Create(builder.Symbols().New("b"), b_a, b_b); + auto* c = b; // Aliased + original_root = alloc.Create(builder.Symbols().New("root"), a, b, c); + } Program original(std::move(builder)); // root @@ -142,15 +155,19 @@ TEST_F(CloneContextNodeTest, Clone) { } TEST_F(CloneContextNodeTest, CloneWithReplaceAll_Cloneable) { - Allocator a; + Allocator alloc; ProgramBuilder builder; - auto* original_root = a.Create(builder.Symbols().New("root")); - original_root->a = a.Create(builder.Symbols().New("a")); - original_root->a->b = a.Create(builder.Symbols().New("a->b")); - original_root->b = a.Create(builder.Symbols().New("b")); - original_root->b->a = original_root->a; // Aliased - original_root->c = original_root->b; // Aliased + Node* original_root; + { + auto* a_b = alloc.Create(builder.Symbols().New("a->b")); + auto* a = alloc.Create(builder.Symbols().New("a"), nullptr, a_b); + auto* b_a = a; // Aliased + auto* b = + alloc.Create(builder.Symbols().New("b"), b_a, nullptr); + auto* c = b; // Aliased + original_root = alloc.Create(builder.Symbols().New("root"), a, b, c); + } Program original(std::move(builder)); // root @@ -167,13 +184,13 @@ TEST_F(CloneContextNodeTest, CloneWithReplaceAll_Cloneable) { ProgramBuilder cloned; CloneContext ctx(&cloned, &original); - ctx.ReplaceAll([&](Replaceable* in) { + ctx.ReplaceAll([&](const Replaceable* in) { auto out_name = cloned.Symbols().Register( "replacement:" + original.Symbols().NameFor(in->name)); auto b_name = cloned.Symbols().Register( "replacement-child:" + original.Symbols().NameFor(in->name)); - auto* out = a.Create(out_name); - out->b = a.Create(b_name); + auto* out = alloc.Create(out_name); + out->b = alloc.Create(b_name); out->c = ctx.Clone(in->a); return out; }); @@ -235,16 +252,19 @@ TEST_F(CloneContextNodeTest, CloneWithReplaceAll_Cloneable) { } TEST_F(CloneContextNodeTest, CloneWithReplaceAll_Symbols) { - Allocator a; + Allocator alloc; ProgramBuilder builder; - auto* original_root = a.Create(builder.Symbols().New("root")); - original_root->a = a.Create(builder.Symbols().New("a")); - original_root->a->b = a.Create(builder.Symbols().New("a->b")); - original_root->b = a.Create(builder.Symbols().New("b")); - original_root->b->a = original_root->a; // Aliased - original_root->b->b = a.Create(builder.Symbols().New("b->b")); - original_root->c = original_root->b; // Aliased + Node* original_root; + { + auto* a_b = alloc.Create(builder.Symbols().New("a->b")); + auto* a = alloc.Create(builder.Symbols().New("a"), nullptr, a_b); + auto* b_a = a; // Aliased + auto* b_b = alloc.Create(builder.Symbols().New("b->b")); + auto* b = alloc.Create(builder.Symbols().New("b"), b_a, b_b); + auto* c = b; // Aliased + original_root = alloc.Create(builder.Symbols().New("root"), a, b, c); + } Program original(std::move(builder)); // root @@ -282,7 +302,7 @@ TEST_F(CloneContextNodeTest, CloneWithoutTransform) { ProgramBuilder cloned; CloneContext ctx(&cloned, &original); - ctx.ReplaceAll([&](Node*) { + ctx.ReplaceAll([&](const Node*) { return a.Create(builder.Symbols().New("")); }); @@ -629,8 +649,8 @@ TEST_F(CloneContextNodeTest, CloneWithReplaceAll_SameTypeTwice) { ProgramBuilder cloned; Program original; CloneContext ctx(&cloned, &original); - ctx.ReplaceAll([](Node*) { return nullptr; }); - ctx.ReplaceAll([](Node*) { return nullptr; }); + ctx.ReplaceAll([](const Node*) { return nullptr; }); + ctx.ReplaceAll([](const Node*) { return nullptr; }); }, "internal compiler error: ReplaceAll() called with a handler for type " + node_name + " that is already handled by a handler for type " + @@ -646,8 +666,8 @@ TEST_F(CloneContextNodeTest, CloneWithReplaceAll_BaseThenDerived) { ProgramBuilder cloned; Program original; CloneContext ctx(&cloned, &original); - ctx.ReplaceAll([](Node*) { return nullptr; }); - ctx.ReplaceAll([](Replaceable*) { return nullptr; }); + ctx.ReplaceAll([](const Node*) { return nullptr; }); + ctx.ReplaceAll([](const Replaceable*) { return nullptr; }); }, "internal compiler error: ReplaceAll() called with a handler for type " + replaceable_name + " that is already handled by a handler for type " + @@ -663,8 +683,8 @@ TEST_F(CloneContextNodeTest, CloneWithReplaceAll_DerivedThenBase) { ProgramBuilder cloned; Program original; CloneContext ctx(&cloned, &original); - ctx.ReplaceAll([](Replaceable*) { return nullptr; }); - ctx.ReplaceAll([](Node*) { return nullptr; }); + ctx.ReplaceAll([](const Replaceable*) { return nullptr; }); + ctx.ReplaceAll([](const Node*) { return nullptr; }); }, "internal compiler error: ReplaceAll() called with a handler for type " + node_name + " that is already handled by a handler for type " + @@ -735,8 +755,8 @@ TEST_F(CloneContextTest, CloneWithReplaceAll_SymbolsTwice) { ProgramBuilder cloned; Program original; CloneContext ctx(&cloned, &original); - ctx.ReplaceAll([](Symbol s) { return s; }); - ctx.ReplaceAll([](Symbol s) { return s; }); + ctx.ReplaceAll([](const Symbol s) { return s; }); + ctx.ReplaceAll([](const Symbol s) { return s; }); }, "internal compiler error: ReplaceAll(const SymbolTransform&) called " "multiple times on the same CloneContext"); diff --git a/src/debug.h b/src/debug.h index d6086d782c..f17afafa95 100644 --- a/src/debug.h +++ b/src/debug.h @@ -67,7 +67,7 @@ class InternalCompilerError { private: char const* const file_; - size_t const line_; + const size_t line_; diag::System system_; diag::List& diagnostics_; std::stringstream msg_; diff --git a/src/diagnostic/formatter.h b/src/diagnostic/formatter.h index dbb28564a2..91afd81515 100644 --- a/src/diagnostic/formatter.h +++ b/src/diagnostic/formatter.h @@ -63,7 +63,7 @@ class Formatter { void format(const Diagnostic& diag, State& state) const; - Style const style_; + const Style style_; }; } // namespace diag diff --git a/src/diagnostic/printer_linux.cc b/src/diagnostic/printer_linux.cc index 99b0da49a8..42c52643c1 100644 --- a/src/diagnostic/printer_linux.cc +++ b/src/diagnostic/printer_linux.cc @@ -87,7 +87,7 @@ class PrinterLinux : public Printer { } FILE* const file; - bool const use_colors; + const bool use_colors; }; } // namespace diff --git a/src/diagnostic/printer_windows.cc b/src/diagnostic/printer_windows.cc index 504ca9b60e..eba1dda35f 100644 --- a/src/diagnostic/printer_windows.cc +++ b/src/diagnostic/printer_windows.cc @@ -100,7 +100,7 @@ class PrinterWindows : public Printer { } FILE* const file; - ConsoleInfo const console; + const ConsoleInfo console; }; } // namespace diff --git a/src/inspector/inspector.cc b/src/inspector/inspector.cc index fe64df1876..ca2a968abf 100644 --- a/src/inspector/inspector.cc +++ b/src/inspector/inspector.cc @@ -579,7 +579,7 @@ uint32_t Inspector::GetWorkgroupStorageSize(const std::string& entry_point) { return total_size; } -ast::Function* Inspector::FindEntryPointByName(const std::string& name) { +const ast::Function* Inspector::FindEntryPointByName(const std::string& name) { auto* func = program_->AST().Functions().Find(program_->Symbols().Get(name)); if (!func) { diagnostics_.add_error(diag::System::Inspector, name + " was not found!"); @@ -597,7 +597,7 @@ ast::Function* Inspector::FindEntryPointByName(const std::string& name) { void Inspector::AddEntryPointInOutVariables( std::string name, - sem::Type* type, + const sem::Type* type, const ast::DecorationList& decorations, std::vector& variables) const { // Skip builtins. @@ -638,7 +638,7 @@ void Inspector::AddEntryPointInOutVariables( } bool Inspector::ContainsBuiltin(ast::Builtin builtin, - sem::Type* type, + const sem::Type* type, const ast::DecorationList& decorations) const { auto* unwrapped_type = type->UnwrapRef(); diff --git a/src/inspector/inspector.h b/src/inspector/inspector.h index 4a9938d3ca..8a8f582ee8 100644 --- a/src/inspector/inspector.h +++ b/src/inspector/inspector.h @@ -147,7 +147,7 @@ class Inspector { /// @param name name of the entry point to find /// @returns a pointer to the entry point if it exists, otherwise returns /// nullptr and sets the error string. - ast::Function* FindEntryPointByName(const std::string& name); + const ast::Function* FindEntryPointByName(const std::string& name); /// Recursively add entry point IO variables. /// If `type` is a struct, recurse into members, appending the member name. @@ -157,7 +157,7 @@ class Inspector { /// @param decorations the variable decorations /// @param variables the list to add the variables to void AddEntryPointInOutVariables(std::string name, - sem::Type* type, + const sem::Type* type, const ast::DecorationList& decorations, std::vector& variables) const; @@ -165,7 +165,7 @@ class Inspector { /// If `type` is a struct, recurse into members to check for the decoration. /// Otherwise, check `decorations` for the decoration. bool ContainsBuiltin(ast::Builtin builtin, - sem::Type* type, + const sem::Type* type, const ast::DecorationList& decorations) const; /// Gathers all the texture resource bindings of the given type for the given diff --git a/src/inspector/inspector_test.cc b/src/inspector/inspector_test.cc index bda54c0001..743449bc1e 100644 --- a/src/inspector/inspector_test.cc +++ b/src/inspector/inspector_test.cc @@ -300,7 +300,7 @@ TEST_P(InspectorGetEntryPointComponentAndCompositionTest, Test) { ComponentType component; CompositionType composition; std::tie(component, composition) = GetParam(); - std::function tint_type = + std::function tint_type = GetTypeFunction(component, composition); auto* in_var = Param("in_var", tint_type(), {Location(0u)}); @@ -683,7 +683,7 @@ TEST_F(InspectorGetEntryPointTest, OverridableConstantUninitialized) { } TEST_F(InspectorGetEntryPointTest, NonOverridableConstantSkipped) { - ast::Struct* foo_struct_type = MakeUniformBufferType("foo_type", {ty.i32()}); + auto* foo_struct_type = MakeUniformBufferType("foo_type", {ty.i32()}); AddUniformBuffer("foo_ub", ty.Of(foo_struct_type), 0, 0); MakeStructVariableReferenceBodyFunction("ub_func", "foo_ub", {{0, ty.i32()}}); MakeCallerBodyFunction("ep_func", {"ub_func"}, @@ -1193,8 +1193,7 @@ TEST_F(InspectorGetStorageSizeTest, Empty) { } TEST_F(InspectorGetStorageSizeTest, Simple) { - ast::Struct* ub_struct_type = - MakeUniformBufferType("ub_type", {ty.i32(), ty.i32()}); + auto* ub_struct_type = MakeUniformBufferType("ub_type", {ty.i32(), ty.i32()}); AddUniformBuffer("ub_var", ty.Of(ub_struct_type), 0, 0); MakeStructVariableReferenceBodyFunction("ub_func", "ub_var", {{0, ty.i32()}}); @@ -1232,7 +1231,7 @@ TEST_F(InspectorGetResourceBindingsTest, Empty) { } TEST_F(InspectorGetResourceBindingsTest, Simple) { - ast::Struct* ub_struct_type = MakeUniformBufferType("ub_type", {ty.i32()}); + auto* ub_struct_type = MakeUniformBufferType("ub_type", {ty.i32()}); AddUniformBuffer("ub_var", ty.Of(ub_struct_type), 0, 0); MakeStructVariableReferenceBodyFunction("ub_func", "ub_var", {{0, ty.i32()}}); @@ -1339,7 +1338,7 @@ TEST_F(InspectorGetUniformBufferResourceBindingsTest, MissingEntryPoint) { } TEST_F(InspectorGetUniformBufferResourceBindingsTest, NonEntryPointFunc) { - ast::Struct* foo_struct_type = MakeUniformBufferType("foo_type", {ty.i32()}); + auto* foo_struct_type = MakeUniformBufferType("foo_type", {ty.i32()}); AddUniformBuffer("foo_ub", ty.Of(foo_struct_type), 0, 0); MakeStructVariableReferenceBodyFunction("ub_func", "foo_ub", {{0, ty.i32()}}); @@ -1357,7 +1356,7 @@ TEST_F(InspectorGetUniformBufferResourceBindingsTest, NonEntryPointFunc) { } TEST_F(InspectorGetUniformBufferResourceBindingsTest, Simple) { - ast::Struct* foo_struct_type = MakeUniformBufferType("foo_type", {ty.i32()}); + auto* foo_struct_type = MakeUniformBufferType("foo_type", {ty.i32()}); AddUniformBuffer("foo_ub", ty.Of(foo_struct_type), 0, 0); MakeStructVariableReferenceBodyFunction("ub_func", "foo_ub", {{0, ty.i32()}}); @@ -1382,7 +1381,7 @@ TEST_F(InspectorGetUniformBufferResourceBindingsTest, Simple) { } TEST_F(InspectorGetUniformBufferResourceBindingsTest, MultipleMembers) { - ast::Struct* foo_struct_type = + auto* foo_struct_type = MakeUniformBufferType("foo_type", {ty.i32(), ty.u32(), ty.f32()}); AddUniformBuffer("foo_ub", ty.Of(foo_struct_type), 0, 0); @@ -1409,8 +1408,7 @@ TEST_F(InspectorGetUniformBufferResourceBindingsTest, MultipleMembers) { } TEST_F(InspectorGetUniformBufferResourceBindingsTest, ContainingPadding) { - ast::Struct* foo_struct_type = - MakeUniformBufferType("foo_type", {ty.vec3()}); + auto* foo_struct_type = MakeUniformBufferType("foo_type", {ty.vec3()}); AddUniformBuffer("foo_ub", ty.Of(foo_struct_type), 0, 0); MakeStructVariableReferenceBodyFunction("ub_func", "foo_ub", @@ -1436,7 +1434,7 @@ TEST_F(InspectorGetUniformBufferResourceBindingsTest, ContainingPadding) { } TEST_F(InspectorGetUniformBufferResourceBindingsTest, MultipleUniformBuffers) { - ast::Struct* ub_struct_type = + auto* ub_struct_type = MakeUniformBufferType("ub_type", {ty.i32(), ty.u32(), ty.f32()}); AddUniformBuffer("ub_foo", ty.Of(ub_struct_type), 0, 0); AddUniformBuffer("ub_bar", ty.Of(ub_struct_type), 0, 1); @@ -1493,7 +1491,7 @@ TEST_F(InspectorGetUniformBufferResourceBindingsTest, MultipleUniformBuffers) { TEST_F(InspectorGetUniformBufferResourceBindingsTest, ContainingArray) { // Manually create uniform buffer to make sure it had a valid layout (array // with elem stride of 16, and that is 16-byte aligned within the struct) - ast::Struct* foo_struct_type = Structure( + auto* foo_struct_type = Structure( "foo_type", {Member("0i32", ty.i32()), Member("b", ty.array(ty.u32(), 4, /*stride*/ 16), {MemberAlign(16)})}, @@ -2370,7 +2368,7 @@ TEST_P(InspectorGetStorageTextureResourceBindingsTestWithParam, Simple) { auto* st_type = MakeStorageTextureTypes(dim, format); AddStorageTexture("st_var", st_type, 0, 0); - ast::Type* dim_type = nullptr; + const ast::Type* dim_type = nullptr; switch (dim) { case ast::TextureDimension::k1d: dim_type = ty.i32(); @@ -2849,7 +2847,7 @@ TEST_F(InspectorGetWorkgroupStorageSizeTest, Simple) { TEST_F(InspectorGetWorkgroupStorageSizeTest, CompoundTypes) { // This struct should occupy 68 bytes. 4 from the i32 field, and another 64 // from the 4-element array with 16-byte stride. - ast::Struct* wg_struct_type = MakeStructType( + auto* wg_struct_type = MakeStructType( "WgStruct", {ty.i32(), ty.array(ty.i32(), 4, /*stride=*/16)}, /*is_block=*/false); AddWorkgroupStorage("wg_struct_var", ty.Of(wg_struct_type)); @@ -2891,7 +2889,7 @@ TEST_F(InspectorGetWorkgroupStorageSizeTest, StructAlignment) { // Per WGSL spec, a struct's size is the offset its last member plus the size // of its last member, rounded up to the alignment of its largest member. So // here the struct is expected to occupy 1024 bytes of workgroup storage. - ast::Struct* wg_struct_type = MakeStructTypeFromMembers( + const auto* wg_struct_type = MakeStructTypeFromMembers( "WgStruct", {MakeStructMember(0, ty.f32(), {create(1024)})}, diff --git a/src/inspector/resource_binding.cc b/src/inspector/resource_binding.cc index 8519140308..e9f6e8a204 100644 --- a/src/inspector/resource_binding.cc +++ b/src/inspector/resource_binding.cc @@ -53,7 +53,7 @@ ResourceBinding::SampledKind BaseTypeToSampledKind(const sem::Type* base_type) { } if (auto* at = base_type->As()) { - base_type = const_cast(at->ElemType()); + base_type = at->ElemType(); } else if (auto* mt = base_type->As()) { base_type = mt->type(); } else if (auto* vt = base_type->As()) { diff --git a/src/inspector/test_inspector_builder.cc b/src/inspector/test_inspector_builder.cc index 1d42d58222..649004a95f 100644 --- a/src/inspector/test_inspector_builder.cc +++ b/src/inspector/test_inspector_builder.cc @@ -47,7 +47,7 @@ void InspectorBuilder::MakeCallerBodyFunction(std::string caller, Func(caller, ast::VariableList(), ty.void_(), body, decorations); } -ast::Struct* InspectorBuilder::MakeInOutStruct( +const ast::Struct* InspectorBuilder::MakeInOutStruct( std::string name, std::vector> inout_vars) { ast::StructMemberList members; @@ -60,10 +60,10 @@ ast::Struct* InspectorBuilder::MakeInOutStruct( return Structure(name, members); } -ast::Function* InspectorBuilder::MakePlainGlobalReferenceBodyFunction( +const ast::Function* InspectorBuilder::MakePlainGlobalReferenceBodyFunction( std::string func, std::string var, - ast::Type* type, + const ast::Type* type, ast::DecorationList decorations) { ast::StatementList stmts; stmts.emplace_back(Decl(Var("local_" + var, type))); @@ -83,13 +83,14 @@ bool InspectorBuilder::ContainsName(const std::vector& vec, return false; } -std::string InspectorBuilder::StructMemberName(size_t idx, ast::Type* type) { +std::string InspectorBuilder::StructMemberName(size_t idx, + const ast::Type* type) { return std::to_string(idx) + type->FriendlyName(Symbols()); } -ast::Struct* InspectorBuilder::MakeStructType( +const ast::Struct* InspectorBuilder::MakeStructType( const std::string& name, - std::vector member_types, + std::vector member_types, bool is_block) { ast::StructMemberList members; for (auto* type : member_types) { @@ -98,7 +99,7 @@ ast::Struct* InspectorBuilder::MakeStructType( return MakeStructTypeFromMembers(name, std::move(members), is_block); } -ast::Struct* InspectorBuilder::MakeStructTypeFromMembers( +const ast::Struct* InspectorBuilder::MakeStructTypeFromMembers( const std::string& name, ast::StructMemberList members, bool is_block) { @@ -109,28 +110,28 @@ ast::Struct* InspectorBuilder::MakeStructTypeFromMembers( return Structure(name, std::move(members), decos); } -ast::StructMember* InspectorBuilder::MakeStructMember( +const ast::StructMember* InspectorBuilder::MakeStructMember( size_t index, - ast::Type* type, + const ast::Type* type, ast::DecorationList decorations) { return Member(StructMemberName(index, type), type, std::move(decorations)); } -ast::Struct* InspectorBuilder::MakeUniformBufferType( +const ast::Struct* InspectorBuilder::MakeUniformBufferType( const std::string& name, - std::vector member_types) { + std::vector member_types) { return MakeStructType(name, member_types, true); } -std::function InspectorBuilder::MakeStorageBufferTypes( +std::function InspectorBuilder::MakeStorageBufferTypes( const std::string& name, - std::vector member_types) { + std::vector member_types) { MakeStructType(name, member_types, true); return [this, name] { return ty.type_name(name); }; } void InspectorBuilder::AddUniformBuffer(const std::string& name, - ast::Type* type, + const ast::Type* type, uint32_t group, uint32_t binding) { Global(name, type, ast::StorageClass::kUniform, @@ -141,12 +142,12 @@ void InspectorBuilder::AddUniformBuffer(const std::string& name, } void InspectorBuilder::AddWorkgroupStorage(const std::string& name, - ast::Type* type) { + const ast::Type* type) { Global(name, type, ast::StorageClass::kWorkgroup); } void InspectorBuilder::AddStorageBuffer(const std::string& name, - ast::Type* type, + const ast::Type* type, ast::Access access, uint32_t group, uint32_t binding) { @@ -160,11 +161,11 @@ void InspectorBuilder::AddStorageBuffer(const std::string& name, void InspectorBuilder::MakeStructVariableReferenceBodyFunction( std::string func_name, std::string struct_name, - std::vector> members) { + std::vector> members) { ast::StatementList stmts; for (auto member : members) { size_t member_idx; - ast::Type* member_type; + const ast::Type* member_type; std::tie(member_idx, member_type) = member; std::string member_name = StructMemberName(member_idx, member_type); @@ -173,7 +174,7 @@ void InspectorBuilder::MakeStructVariableReferenceBodyFunction( for (auto member : members) { size_t member_idx; - ast::Type* member_type; + const ast::Type* member_type; std::tie(member_idx, member_type) = member; std::string member_name = StructMemberName(member_idx, member_type); @@ -208,7 +209,7 @@ void InspectorBuilder::AddComparisonSampler(const std::string& name, } void InspectorBuilder::AddResource(const std::string& name, - ast::Type* type, + const ast::Type* type, uint32_t group, uint32_t binding) { Global(name, type, @@ -219,16 +220,16 @@ void InspectorBuilder::AddResource(const std::string& name, } void InspectorBuilder::AddGlobalVariable(const std::string& name, - ast::Type* type) { + const ast::Type* type) { Global(name, type, ast::StorageClass::kPrivate); } -ast::Function* InspectorBuilder::MakeSamplerReferenceBodyFunction( +const ast::Function* InspectorBuilder::MakeSamplerReferenceBodyFunction( const std::string& func_name, const std::string& texture_name, const std::string& sampler_name, const std::string& coords_name, - ast::Type* base_type, + const ast::Type* base_type, ast::DecorationList decorations) { std::string result_name = "sampler_result"; @@ -242,13 +243,13 @@ ast::Function* InspectorBuilder::MakeSamplerReferenceBodyFunction( return Func(func_name, ast::VariableList(), ty.void_(), stmts, decorations); } -ast::Function* InspectorBuilder::MakeSamplerReferenceBodyFunction( +const ast::Function* InspectorBuilder::MakeSamplerReferenceBodyFunction( const std::string& func_name, const std::string& texture_name, const std::string& sampler_name, const std::string& coords_name, const std::string& array_index, - ast::Type* base_type, + const ast::Type* base_type, ast::DecorationList decorations) { std::string result_name = "sampler_result"; @@ -264,13 +265,14 @@ ast::Function* InspectorBuilder::MakeSamplerReferenceBodyFunction( return Func(func_name, ast::VariableList(), ty.void_(), stmts, decorations); } -ast::Function* InspectorBuilder::MakeComparisonSamplerReferenceBodyFunction( +const ast::Function* +InspectorBuilder::MakeComparisonSamplerReferenceBodyFunction( const std::string& func_name, const std::string& texture_name, const std::string& sampler_name, const std::string& coords_name, const std::string& depth_name, - ast::Type* base_type, + const ast::Type* base_type, ast::DecorationList decorations) { std::string result_name = "sampler_result"; @@ -285,7 +287,7 @@ ast::Function* InspectorBuilder::MakeComparisonSamplerReferenceBodyFunction( return Func(func_name, ast::VariableList(), ty.void_(), stmts, decorations); } -ast::Type* InspectorBuilder::GetBaseType( +const ast::Type* InspectorBuilder::GetBaseType( ResourceBinding::SampledKind sampled_kind) { switch (sampled_kind) { case ResourceBinding::SampledKind::kFloat: @@ -299,8 +301,8 @@ ast::Type* InspectorBuilder::GetBaseType( } } -ast::Type* InspectorBuilder::GetCoordsType(ast::TextureDimension dim, - ast::Type* scalar) { +const ast::Type* InspectorBuilder::GetCoordsType(ast::TextureDimension dim, + const ast::Type* scalar) { switch (dim) { case ast::TextureDimension::k1d: return scalar; @@ -317,13 +319,14 @@ ast::Type* InspectorBuilder::GetCoordsType(ast::TextureDimension dim, return nullptr; } -ast::Type* InspectorBuilder::MakeStorageTextureTypes(ast::TextureDimension dim, - ast::ImageFormat format) { +const ast::Type* InspectorBuilder::MakeStorageTextureTypes( + ast::TextureDimension dim, + ast::ImageFormat format) { return ty.storage_texture(dim, format, ast::Access::kWrite); } void InspectorBuilder::AddStorageTexture(const std::string& name, - ast::Type* type, + const ast::Type* type, uint32_t group, uint32_t binding) { Global(name, type, @@ -333,10 +336,10 @@ void InspectorBuilder::AddStorageTexture(const std::string& name, }); } -ast::Function* InspectorBuilder::MakeStorageTextureBodyFunction( +const ast::Function* InspectorBuilder::MakeStorageTextureBodyFunction( const std::string& func_name, const std::string& st_name, - ast::Type* dim_type, + const ast::Type* dim_type, ast::DecorationList decorations) { ast::StatementList stmts; @@ -347,22 +350,22 @@ ast::Function* InspectorBuilder::MakeStorageTextureBodyFunction( return Func(func_name, ast::VariableList(), ty.void_(), stmts, decorations); } -std::function InspectorBuilder::GetTypeFunction( +std::function InspectorBuilder::GetTypeFunction( ComponentType component, CompositionType composition) { - std::function func; + std::function func; switch (component) { case ComponentType::kFloat: - func = [this]() -> ast::Type* { return ty.f32(); }; + func = [this]() -> const ast::Type* { return ty.f32(); }; break; case ComponentType::kSInt: - func = [this]() -> ast::Type* { return ty.i32(); }; + func = [this]() -> const ast::Type* { return ty.i32(); }; break; case ComponentType::kUInt: - func = [this]() -> ast::Type* { return ty.u32(); }; + func = [this]() -> const ast::Type* { return ty.u32(); }; break; case ComponentType::kUnknown: - return []() -> ast::Type* { return nullptr; }; + return []() -> const ast::Type* { return nullptr; }; } uint32_t n; @@ -382,7 +385,7 @@ std::function InspectorBuilder::GetTypeFunction( return []() -> ast::Type* { return nullptr; }; } - return [this, func, n]() -> ast::Type* { return ty.vec(func(), n); }; + return [this, func, n]() -> const ast::Type* { return ty.vec(func(), n); }; } Inspector& InspectorBuilder::Build() { diff --git a/src/inspector/test_inspector_builder.h b/src/inspector/test_inspector_builder.h index faa69a4505..e3783ab573 100644 --- a/src/inspector/test_inspector_builder.h +++ b/src/inspector/test_inspector_builder.h @@ -60,7 +60,7 @@ class InspectorBuilder : public ProgramBuilder { /// @param name the name of the generated struct /// @param inout_vars tuples of {name, loc} that will be the struct members /// @returns a structure object - ast::Struct* MakeInOutStruct( + const ast::Struct* MakeInOutStruct( std::string name, std::vector> inout_vars); @@ -91,7 +91,7 @@ class InspectorBuilder : public ProgramBuilder { /// calls in the function body /// @param decorations the function decorations /// @returns a function object - ast::Function* MakeInOutVariableCallerBodyFunction( + const ast::Function* MakeInOutVariableCallerBodyFunction( std::string caller, std::string callee, std::vector> inout_vars, @@ -104,10 +104,11 @@ class InspectorBuilder : public ProgramBuilder { /// @param constructor val to initialize the constant with, if NULL no /// constructor will be added. /// @returns the constant that was created - ast::Variable* AddOverridableConstantWithID(std::string name, - uint32_t id, - ast::Type* type, - ast::Expression* constructor) { + const ast::Variable* AddOverridableConstantWithID( + std::string name, + uint32_t id, + const ast::Type* type, + const ast::Expression* constructor) { return GlobalConst(name, type, constructor, ast::DecorationList{ Override(id), @@ -120,9 +121,10 @@ class InspectorBuilder : public ProgramBuilder { /// @param constructor val to initialize the constant with, if NULL no /// constructor will be added. /// @returns the constant that was created - ast::Variable* AddOverridableConstantWithoutID(std::string name, - ast::Type* type, - ast::Expression* constructor) { + const ast::Variable* AddOverridableConstantWithoutID( + std::string name, + const ast::Type* type, + const ast::Expression* constructor) { return GlobalConst(name, type, constructor, ast::DecorationList{ Override(), @@ -136,10 +138,10 @@ class InspectorBuilder : public ProgramBuilder { /// @param type type of the const being referenced /// @param decorations the function decorations /// @returns a function object - ast::Function* MakePlainGlobalReferenceBodyFunction( + const ast::Function* MakePlainGlobalReferenceBodyFunction( std::string func, std::string var, - ast::Type* type, + const ast::Type* type, ast::DecorationList decorations); /// @param vec Vector of StageVariable to be searched @@ -152,49 +154,50 @@ class InspectorBuilder : public ProgramBuilder { /// @param idx index of member /// @param type type of member /// @returns a string for the member - std::string StructMemberName(size_t idx, ast::Type* type); + std::string StructMemberName(size_t idx, const ast::Type* type); /// Generates a struct type /// @param name name for the type /// @param member_types a vector of member types /// @param is_block whether or not to decorate as a Block /// @returns a struct type - ast::Struct* MakeStructType(const std::string& name, - std::vector member_types, - bool is_block); + const ast::Struct* MakeStructType(const std::string& name, + std::vector member_types, + bool is_block); /// Generates a struct type from a list of member nodes. /// @param name name for the struct type /// @param members a vector of members /// @param is_block whether or not to decorate as a Block /// @returns a struct type - ast::Struct* MakeStructTypeFromMembers(const std::string& name, - ast::StructMemberList members, - bool is_block); + const ast::Struct* MakeStructTypeFromMembers(const std::string& name, + ast::StructMemberList members, + bool is_block); /// Generates a struct member with a specified index and type. /// @param index index of the field within the struct /// @param type the type of the member field /// @param decorations a list of decorations to apply to the member field /// @returns a struct member - ast::StructMember* MakeStructMember(size_t index, - ast::Type* type, - ast::DecorationList decorations); + const ast::StructMember* MakeStructMember(size_t index, + const ast::Type* type, + ast::DecorationList decorations); /// Generates types appropriate for using in an uniform buffer /// @param name name for the type /// @param member_types a vector of member types /// @returns a struct type that has the layout for an uniform buffer. - ast::Struct* MakeUniformBufferType(const std::string& name, - std::vector member_types); + const ast::Struct* MakeUniformBufferType( + const std::string& name, + std::vector member_types); /// Generates types appropriate for using in a storage buffer /// @param name name for the type /// @param member_types a vector of member types /// @returns a function that returns the created structure. - std::function MakeStorageBufferTypes( + std::function MakeStorageBufferTypes( const std::string& name, - std::vector member_types); + std::vector member_types); /// Adds an uniform buffer variable to the program /// @param name the name of the variable @@ -202,14 +205,14 @@ class InspectorBuilder : public ProgramBuilder { /// @param group the binding/group/ to use for the uniform buffer /// @param binding the binding number to use for the uniform buffer void AddUniformBuffer(const std::string& name, - ast::Type* type, + const ast::Type* type, uint32_t group, uint32_t binding); /// Adds a workgroup storage variable to the program /// @param name the name of the variable /// @param type the type of the variable - void AddWorkgroupStorage(const std::string& name, ast::Type* type); + void AddWorkgroupStorage(const std::string& name, const ast::Type* type); /// Adds a storage buffer variable to the program /// @param name the name of the variable @@ -218,7 +221,7 @@ class InspectorBuilder : public ProgramBuilder { /// @param group the binding/group to use for the storage buffer /// @param binding the binding number to use for the storage buffer void AddStorageBuffer(const std::string& name, - ast::Type* type, + const ast::Type* type, ast::Access access, uint32_t group, uint32_t binding); @@ -230,7 +233,7 @@ class InspectorBuilder : public ProgramBuilder { void MakeStructVariableReferenceBodyFunction( std::string func_name, std::string struct_name, - std::vector> members); + std::vector> members); /// Adds a regular sampler variable to the program /// @param name the name of the variable @@ -252,14 +255,14 @@ class InspectorBuilder : public ProgramBuilder { /// @param group the binding/group to use for the resource /// @param binding the binding number to use for the resource void AddResource(const std::string& name, - ast::Type* type, + const ast::Type* type, uint32_t group, uint32_t binding); /// Add a module scope private variable to the progames /// @param name the name of the variable /// @param type the type to use - void AddGlobalVariable(const std::string& name, ast::Type* type); + void AddGlobalVariable(const std::string& name, const ast::Type* type); /// Generates a function that references a specific sampler variable /// @param func_name name of the function created @@ -269,12 +272,12 @@ class InspectorBuilder : public ProgramBuilder { /// @param base_type sampler base type /// @param decorations the function decorations /// @returns a function that references all of the values specified - ast::Function* MakeSamplerReferenceBodyFunction( + const ast::Function* MakeSamplerReferenceBodyFunction( const std::string& func_name, const std::string& texture_name, const std::string& sampler_name, const std::string& coords_name, - ast::Type* base_type, + const ast::Type* base_type, ast::DecorationList decorations); /// Generates a function that references a specific sampler variable @@ -286,13 +289,13 @@ class InspectorBuilder : public ProgramBuilder { /// @param base_type sampler base type /// @param decorations the function decorations /// @returns a function that references all of the values specified - ast::Function* MakeSamplerReferenceBodyFunction( + const ast::Function* MakeSamplerReferenceBodyFunction( const std::string& func_name, const std::string& texture_name, const std::string& sampler_name, const std::string& coords_name, const std::string& array_index, - ast::Type* base_type, + const ast::Type* base_type, ast::DecorationList decorations); /// Generates a function that references a specific comparison sampler @@ -305,33 +308,34 @@ class InspectorBuilder : public ProgramBuilder { /// @param base_type sampler base type /// @param decorations the function decorations /// @returns a function that references all of the values specified - ast::Function* MakeComparisonSamplerReferenceBodyFunction( + const ast::Function* MakeComparisonSamplerReferenceBodyFunction( const std::string& func_name, const std::string& texture_name, const std::string& sampler_name, const std::string& coords_name, const std::string& depth_name, - ast::Type* base_type, + const ast::Type* base_type, ast::DecorationList decorations); /// Gets an appropriate type for the data in a given texture type. /// @param sampled_kind type of in the texture /// @returns a pointer to a type appropriate for the coord param - ast::Type* GetBaseType(ResourceBinding::SampledKind sampled_kind); + const ast::Type* GetBaseType(ResourceBinding::SampledKind sampled_kind); /// Gets an appropriate type for the coords parameter depending the the /// dimensionality of the texture being sampled. /// @param dim dimensionality of the texture being sampled /// @param scalar the scalar type /// @returns a pointer to a type appropriate for the coord param - ast::Type* GetCoordsType(ast::TextureDimension dim, ast::Type* scalar); + const ast::Type* GetCoordsType(ast::TextureDimension dim, + const ast::Type* scalar); /// Generates appropriate types for a Read-Only StorageTexture /// @param dim the texture dimension of the storage texture /// @param format the image format of the storage texture /// @returns the storage texture type - ast::Type* MakeStorageTextureTypes(ast::TextureDimension dim, - ast::ImageFormat format); + const ast::Type* MakeStorageTextureTypes(ast::TextureDimension dim, + ast::ImageFormat format); /// Adds a storage texture variable to the program /// @param name the name of the variable @@ -339,7 +343,7 @@ class InspectorBuilder : public ProgramBuilder { /// @param group the binding/group to use for the sampled texture /// @param binding the binding57 number to use for the sampled texture void AddStorageTexture(const std::string& name, - ast::Type* type, + const ast::Type* type, uint32_t group, uint32_t binding); @@ -349,10 +353,10 @@ class InspectorBuilder : public ProgramBuilder { /// @param dim_type type expected by textureDimensons to return /// @param decorations the function decorations /// @returns a function that references all of the values specified - ast::Function* MakeStorageTextureBodyFunction( + const ast::Function* MakeStorageTextureBodyFunction( const std::string& func_name, const std::string& st_name, - ast::Type* dim_type, + const ast::Type* dim_type, ast::DecorationList decorations); /// Get a generator function that returns a type appropriate for a stage @@ -360,8 +364,9 @@ class InspectorBuilder : public ProgramBuilder { /// @param component component type of the stage variable /// @param composition composition type of the stage variable /// @returns a generator function for the stage variable's type. - std::function GetTypeFunction(ComponentType component, - CompositionType composition); + std::function GetTypeFunction( + ComponentType component, + CompositionType composition); /// Build the Program given all of the previous methods called and return an /// Inspector for it. @@ -370,12 +375,12 @@ class InspectorBuilder : public ProgramBuilder { Inspector& Build(); /// @returns the type for a SamplerKind::kSampler - ast::Sampler* sampler_type() { + const ast::Sampler* sampler_type() { return ty.sampler(ast::SamplerKind::kSampler); } /// @returns the type for a SamplerKind::kComparison - ast::Sampler* comparison_sampler_type() { + const ast::Sampler* comparison_sampler_type() { return ty.sampler(ast::SamplerKind::kComparisonSampler); } diff --git a/src/intrinsic_table.cc b/src/intrinsic_table.cc index 0f497ef666..d74bb8db83 100644 --- a/src/intrinsic_table.cc +++ b/src/intrinsic_table.cc @@ -742,7 +742,7 @@ const sem::Struct* build_frexp_result_vec(MatchState& state, Number& n) { /// ParameterInfo describes a parameter struct ParameterInfo { /// The parameter usage (parameter name in definition file) - ParameterUsage const usage; + const ParameterUsage usage; /// Pointer to a list of indices that are used to match the parameter type. /// The matcher indices index on Matchers::type and / or Matchers::number. @@ -757,7 +757,7 @@ struct OpenTypeInfo { const char* name; /// Optional type matcher constraint. /// Either an index in Matchers::type, or kNoMatcher - MatcherIndex const matcher_index; + const MatcherIndex matcher_index; }; /// OpenNumberInfo describes an open number @@ -766,17 +766,17 @@ struct OpenNumberInfo { const char* name; /// Optional number matcher constraint. /// Either an index in Matchers::number, or kNoMatcher - MatcherIndex const matcher_index; + const MatcherIndex matcher_index; }; /// OverloadInfo describes a single function overload struct OverloadInfo { /// Total number of parameters for the overload - uint8_t const num_parameters; + const uint8_t num_parameters; /// Total number of open types for the overload - uint8_t const num_open_types; + const uint8_t num_open_types; /// Total number of open numbers for the overload - uint8_t const num_open_numbers; + const uint8_t num_open_numbers; /// Pointer to the first open type OpenTypeInfo const* const open_types; /// Pointer to the first open number @@ -796,7 +796,7 @@ struct OverloadInfo { /// IntrinsicInfo describes an intrinsic function struct IntrinsicInfo { /// Number of overloads of the intrinsic function - uint8_t const num_overloads; + const uint8_t num_overloads; /// Pointer to the start of the overloads for the function OverloadInfo const* const overloads; }; @@ -809,7 +809,7 @@ struct IntrinsicPrototype { /// Parameter describes a single parameter struct Parameter { /// Parameter type - sem::Type* const type; + const sem::Type* const type; /// Parameter usage ParameterUsage const usage = ParameterUsage::kNone; }; @@ -995,8 +995,8 @@ const sem::Intrinsic* Impl::Match(sem::IntrinsicType intrinsic_type, auto* indices = parameter.matcher_indices; auto* type = Match(closed, overload, indices).Type(args[p]->UnwrapRef()); if (type) { - parameters.emplace_back(IntrinsicPrototype::Parameter{ - const_cast(type), parameter.usage}); + parameters.emplace_back( + IntrinsicPrototype::Parameter{type, parameter.usage}); match_score += kScorePerMatchedParam; } else { overload_matched = false; @@ -1070,7 +1070,7 @@ const sem::Intrinsic* Impl::Match(sem::IntrinsicType intrinsic_type, ast::StorageClass::kNone, ast::Access::kUndefined, p.usage)); } return builder.create( - intrinsic.type, const_cast(intrinsic.return_type), + intrinsic.type, intrinsic.return_type, std::move(params), intrinsic.supported_stages, intrinsic.is_deprecated); }); } diff --git a/src/program.cc b/src/program.cc index c3cd9c1cbd..b838c7d6d5 100644 --- a/src/program.cc +++ b/src/program.cc @@ -114,7 +114,7 @@ bool Program::IsValid() const { return is_valid_; } -sem::Type* Program::TypeOf(const ast::Expression* expr) const { +const sem::Type* Program::TypeOf(const ast::Expression* expr) const { auto* sem = Sem().Get(expr); return sem ? sem->Type() : nullptr; } diff --git a/src/program.h b/src/program.h index ad4270468a..f5ceca7252 100644 --- a/src/program.h +++ b/src/program.h @@ -149,7 +149,7 @@ class Program { /// @param expr the AST expression /// @return the resolved semantic type for the expression, or nullptr if the /// expression has no resolved type. - sem::Type* TypeOf(const ast::Expression* expr) const; + const sem::Type* TypeOf(const ast::Expression* expr) const; /// Helper for returning the resolved semantic type of the AST type `type`. /// @param type the AST type diff --git a/src/program_builder.cc b/src/program_builder.cc index f9b6bc6baa..abcd6f212b 100644 --- a/src/program_builder.cc +++ b/src/program_builder.cc @@ -89,12 +89,12 @@ void ProgramBuilder::AssertNotMoved() const { } } -sem::Type* ProgramBuilder::TypeOf(const ast::Expression* expr) const { +const sem::Type* ProgramBuilder::TypeOf(const ast::Expression* expr) const { auto* sem = Sem().Get(expr); return sem ? sem->Type() : nullptr; } -sem::Type* ProgramBuilder::TypeOf(const ast::Variable* var) const { +const sem::Type* ProgramBuilder::TypeOf(const ast::Variable* var) const { auto* sem = Sem().Get(var); return sem ? sem->Type() : nullptr; } @@ -107,10 +107,6 @@ const sem::Type* ProgramBuilder::TypeOf(const ast::TypeDecl* type_decl) const { return Sem().Get(type_decl); } -ast::TypeName* ProgramBuilder::TypesBuilder::Of(ast::TypeDecl* decl) const { - return type_name(decl->name); -} - const ast::TypeName* ProgramBuilder::TypesBuilder::Of( const ast::TypeDecl* decl) const { return type_name(decl->name); @@ -118,11 +114,12 @@ const ast::TypeName* ProgramBuilder::TypesBuilder::Of( ProgramBuilder::TypesBuilder::TypesBuilder(ProgramBuilder* pb) : builder(pb) {} -ast::Statement* ProgramBuilder::WrapInStatement(ast::Literal* lit) { +const ast::Statement* ProgramBuilder::WrapInStatement(const ast::Literal* lit) { return WrapInStatement(create(lit)); } -ast::Statement* ProgramBuilder::WrapInStatement(ast::Expression* expr) { +const ast::Statement* ProgramBuilder::WrapInStatement( + const ast::Expression* expr) { if (auto* ce = expr->As()) { return Ignore(ce); } @@ -130,15 +127,18 @@ ast::Statement* ProgramBuilder::WrapInStatement(ast::Expression* expr) { return Decl(Const(symbols_.New(), nullptr, expr)); } -ast::VariableDeclStatement* ProgramBuilder::WrapInStatement(ast::Variable* v) { +const ast::VariableDeclStatement* ProgramBuilder::WrapInStatement( + const ast::Variable* v) { return create(v); } -ast::Statement* ProgramBuilder::WrapInStatement(ast::Statement* stmt) { +const ast::Statement* ProgramBuilder::WrapInStatement( + const ast::Statement* stmt) { return stmt; } -ast::Function* ProgramBuilder::WrapInFunction(ast::StatementList stmts) { +const ast::Function* ProgramBuilder::WrapInFunction( + const ast::StatementList stmts) { return Func("test_function", {}, ty.void_(), std::move(stmts), {create(ast::PipelineStage::kCompute), WorkgroupSize(1, 1, 1)}); diff --git a/src/program_builder.h b/src/program_builder.h index 39a1fa3501..0928b8ec21 100644 --- a/src/program_builder.h +++ b/src/program_builder.h @@ -127,13 +127,13 @@ class ProgramBuilder { ast::StorageClass storage = ast::StorageClass::kNone; ast::Access access = ast::Access::kUndefined; - ast::Expression* constructor = nullptr; + const ast::Expression* constructor = nullptr; ast::DecorationList decorations = {}; private: void Set(ast::StorageClass sc) { storage = sc; } void Set(ast::Access ac) { access = ac; } - void Set(ast::Expression* c) { constructor = c; } + void Set(const ast::Expression* c) { constructor = c; } void Set(const ast::DecorationList& l) { decorations = l; } template @@ -388,59 +388,59 @@ class ProgramBuilder { /// @return the tint AST type for the C type `T`. template - ast::Type* Of() const { + const ast::Type* Of() const { return CToAST::get(this); } /// @returns a boolean type - ast::Bool* bool_() const { return builder->create(); } + const ast::Bool* bool_() const { return builder->create(); } /// @param source the Source of the node /// @returns a boolean type - ast::Bool* bool_(const Source& source) const { + const ast::Bool* bool_(const Source& source) const { return builder->create(source); } /// @returns a f32 type - ast::F32* f32() const { return builder->create(); } + const ast::F32* f32() const { return builder->create(); } /// @param source the Source of the node /// @returns a f32 type - ast::F32* f32(const Source& source) const { + const ast::F32* f32(const Source& source) const { return builder->create(source); } /// @returns a i32 type - ast::I32* i32() const { return builder->create(); } + const ast::I32* i32() const { return builder->create(); } /// @param source the Source of the node /// @returns a i32 type - ast::I32* i32(const Source& source) const { + const ast::I32* i32(const Source& source) const { return builder->create(source); } /// @returns a u32 type - ast::U32* u32() const { return builder->create(); } + const ast::U32* u32() const { return builder->create(); } /// @param source the Source of the node /// @returns a u32 type - ast::U32* u32(const Source& source) const { + const ast::U32* u32(const Source& source) const { return builder->create(source); } /// @returns a void type - ast::Void* void_() const { return builder->create(); } + const ast::Void* void_() const { return builder->create(); } /// @param source the Source of the node /// @returns a void type - ast::Void* void_(const Source& source) const { + const ast::Void* void_(const Source& source) const { return builder->create(source); } /// @param type vector subtype /// @param n vector width in elements /// @return the tint AST type for a `n`-element vector of `type`. - ast::Vector* vec(ast::Type* type, uint32_t n) const { + const ast::Vector* vec(const ast::Type* type, uint32_t n) const { return builder->create(type, n); } @@ -448,44 +448,52 @@ class ProgramBuilder { /// @param type vector subtype /// @param n vector width in elements /// @return the tint AST type for a `n`-element vector of `type`. - ast::Vector* vec(const Source& source, ast::Type* type, uint32_t n) const { + const ast::Vector* vec(const Source& source, + const ast::Type* type, + uint32_t n) const { return builder->create(source, type, n); } /// @param type vector subtype /// @return the tint AST type for a 2-element vector of `type`. - ast::Vector* vec2(ast::Type* type) const { return vec(type, 2u); } + const ast::Vector* vec2(const ast::Type* type) const { + return vec(type, 2u); + } /// @param type vector subtype /// @return the tint AST type for a 3-element vector of `type`. - ast::Vector* vec3(ast::Type* type) const { return vec(type, 3u); } + const ast::Vector* vec3(const ast::Type* type) const { + return vec(type, 3u); + } /// @param type vector subtype /// @return the tint AST type for a 4-element vector of `type`. - ast::Vector* vec4(ast::Type* type) const { return vec(type, 4u); } + const ast::Vector* vec4(const ast::Type* type) const { + return vec(type, 4u); + } /// @param n vector width in elements /// @return the tint AST type for a `n`-element vector of `type`. template - ast::Vector* vec(uint32_t n) const { + const ast::Vector* vec(uint32_t n) const { return vec(Of(), n); } /// @return the tint AST type for a 2-element vector of the C type `T`. template - ast::Vector* vec2() const { + const ast::Vector* vec2() const { return vec2(Of()); } /// @return the tint AST type for a 3-element vector of the C type `T`. template - ast::Vector* vec3() const { + const ast::Vector* vec3() const { return vec3(Of()); } /// @return the tint AST type for a 4-element vector of the C type `T`. template - ast::Vector* vec4() const { + const ast::Vector* vec4() const { return vec4(Of()); } @@ -493,7 +501,9 @@ class ProgramBuilder { /// @param columns number of columns for the matrix /// @param rows number of rows for the matrix /// @return the tint AST type for a matrix of `type` - ast::Matrix* mat(ast::Type* type, uint32_t columns, uint32_t rows) const { + const ast::Matrix* mat(const ast::Type* type, + uint32_t columns, + uint32_t rows) const { return builder->create(type, rows, columns); } @@ -502,108 +512,126 @@ class ProgramBuilder { /// @param columns number of columns for the matrix /// @param rows number of rows for the matrix /// @return the tint AST type for a matrix of `type` - ast::Matrix* mat(const Source& source, - ast::Type* type, - uint32_t columns, - uint32_t rows) const { + const ast::Matrix* mat(const Source& source, + const ast::Type* type, + uint32_t columns, + uint32_t rows) const { return builder->create(source, type, rows, columns); } /// @param type matrix subtype /// @return the tint AST type for a 2x3 matrix of `type`. - ast::Matrix* mat2x2(ast::Type* type) const { return mat(type, 2u, 2u); } + const ast::Matrix* mat2x2(const ast::Type* type) const { + return mat(type, 2u, 2u); + } /// @param type matrix subtype /// @return the tint AST type for a 2x3 matrix of `type`. - ast::Matrix* mat2x3(ast::Type* type) const { return mat(type, 2u, 3u); } + const ast::Matrix* mat2x3(const ast::Type* type) const { + return mat(type, 2u, 3u); + } /// @param type matrix subtype /// @return the tint AST type for a 2x4 matrix of `type`. - ast::Matrix* mat2x4(ast::Type* type) const { return mat(type, 2u, 4u); } + const ast::Matrix* mat2x4(const ast::Type* type) const { + return mat(type, 2u, 4u); + } /// @param type matrix subtype /// @return the tint AST type for a 3x2 matrix of `type`. - ast::Matrix* mat3x2(ast::Type* type) const { return mat(type, 3u, 2u); } + const ast::Matrix* mat3x2(const ast::Type* type) const { + return mat(type, 3u, 2u); + } /// @param type matrix subtype /// @return the tint AST type for a 3x3 matrix of `type`. - ast::Matrix* mat3x3(ast::Type* type) const { return mat(type, 3u, 3u); } + const ast::Matrix* mat3x3(const ast::Type* type) const { + return mat(type, 3u, 3u); + } /// @param type matrix subtype /// @return the tint AST type for a 3x4 matrix of `type`. - ast::Matrix* mat3x4(ast::Type* type) const { return mat(type, 3u, 4u); } + const ast::Matrix* mat3x4(const ast::Type* type) const { + return mat(type, 3u, 4u); + } /// @param type matrix subtype /// @return the tint AST type for a 4x2 matrix of `type`. - ast::Matrix* mat4x2(ast::Type* type) const { return mat(type, 4u, 2u); } + const ast::Matrix* mat4x2(const ast::Type* type) const { + return mat(type, 4u, 2u); + } /// @param type matrix subtype /// @return the tint AST type for a 4x3 matrix of `type`. - ast::Matrix* mat4x3(ast::Type* type) const { return mat(type, 4u, 3u); } + const ast::Matrix* mat4x3(const ast::Type* type) const { + return mat(type, 4u, 3u); + } /// @param type matrix subtype /// @return the tint AST type for a 4x4 matrix of `type`. - ast::Matrix* mat4x4(ast::Type* type) const { return mat(type, 4u, 4u); } + const ast::Matrix* mat4x4(const ast::Type* type) const { + return mat(type, 4u, 4u); + } /// @param columns number of columns for the matrix /// @param rows number of rows for the matrix /// @return the tint AST type for a matrix of `type` template - ast::Matrix* mat(uint32_t columns, uint32_t rows) const { + const ast::Matrix* mat(uint32_t columns, uint32_t rows) const { return mat(Of(), columns, rows); } /// @return the tint AST type for a 2x3 matrix of the C type `T`. template - ast::Matrix* mat2x2() const { + const ast::Matrix* mat2x2() const { return mat2x2(Of()); } /// @return the tint AST type for a 2x3 matrix of the C type `T`. template - ast::Matrix* mat2x3() const { + const ast::Matrix* mat2x3() const { return mat2x3(Of()); } /// @return the tint AST type for a 2x4 matrix of the C type `T`. template - ast::Matrix* mat2x4() const { + const ast::Matrix* mat2x4() const { return mat2x4(Of()); } /// @return the tint AST type for a 3x2 matrix of the C type `T`. template - ast::Matrix* mat3x2() const { + const ast::Matrix* mat3x2() const { return mat3x2(Of()); } /// @return the tint AST type for a 3x3 matrix of the C type `T`. template - ast::Matrix* mat3x3() const { + const ast::Matrix* mat3x3() const { return mat3x3(Of()); } /// @return the tint AST type for a 3x4 matrix of the C type `T`. template - ast::Matrix* mat3x4() const { + const ast::Matrix* mat3x4() const { return mat3x4(Of()); } /// @return the tint AST type for a 4x2 matrix of the C type `T`. template - ast::Matrix* mat4x2() const { + const ast::Matrix* mat4x2() const { return mat4x2(Of()); } /// @return the tint AST type for a 4x3 matrix of the C type `T`. template - ast::Matrix* mat4x3() const { + const ast::Matrix* mat4x3() const { return mat4x3(Of()); } /// @return the tint AST type for a 4x4 matrix of the C type `T`. template - ast::Matrix* mat4x4() const { + const ast::Matrix* mat4x4() const { return mat4x4(Of()); } @@ -612,9 +640,9 @@ class ProgramBuilder { /// @param decos the optional decorations for the array /// @return the tint AST type for a array of size `n` of type `T` template - ast::Array* array(ast::Type* subtype, - EXPR&& n = nullptr, - ast::DecorationList decos = {}) const { + const ast::Array* array(const ast::Type* subtype, + EXPR&& n = nullptr, + ast::DecorationList decos = {}) const { return builder->create( subtype, builder->Expr(std::forward(n)), decos); } @@ -625,10 +653,10 @@ class ProgramBuilder { /// @param decos the optional decorations for the array /// @return the tint AST type for a array of size `n` of type `T` template - ast::Array* array(const Source& source, - ast::Type* subtype, - EXPR&& n = nullptr, - ast::DecorationList decos = {}) const { + const ast::Array* array(const Source& source, + const ast::Type* subtype, + EXPR&& n = nullptr, + ast::DecorationList decos = {}) const { return builder->create( source, subtype, builder->Expr(std::forward(n)), decos); } @@ -638,7 +666,9 @@ class ProgramBuilder { /// @param stride the array stride. 0 represents implicit stride /// @return the tint AST type for a array of size `n` of type `T` template - ast::Array* array(ast::Type* subtype, EXPR&& n, uint32_t stride) const { + const ast::Array* array(const ast::Type* subtype, + EXPR&& n, + uint32_t stride) const { ast::DecorationList decos; if (stride) { decos.emplace_back(builder->create(stride)); @@ -652,10 +682,10 @@ class ProgramBuilder { /// @param stride the array stride. 0 represents implicit stride /// @return the tint AST type for a array of size `n` of type `T` template - ast::Array* array(const Source& source, - ast::Type* subtype, - EXPR&& n, - uint32_t stride) const { + const ast::Array* array(const Source& source, + const ast::Type* subtype, + EXPR&& n, + uint32_t stride) const { ast::DecorationList decos; if (stride) { decos.emplace_back(builder->create(stride)); @@ -665,27 +695,27 @@ class ProgramBuilder { /// @return the tint AST type for a runtime-sized array of type `T` template - ast::Array* array() const { + const ast::Array* array() const { return array(Of(), nullptr); } /// @return the tint AST type for an array of size `N` of type `T` template - ast::Array* array() const { + const ast::Array* array() const { return array(Of(), builder->Expr(N)); } /// @param stride the array stride /// @return the tint AST type for a runtime-sized array of type `T` template - ast::Array* array(uint32_t stride) const { + const ast::Array* array(uint32_t stride) const { return array(Of(), nullptr, stride); } /// @param stride the array stride /// @return the tint AST type for an array of size `N` of type `T` template - ast::Array* array(uint32_t stride) const { + const ast::Array* array(uint32_t stride) const { return array(Of(), builder->Expr(N), stride); } @@ -693,7 +723,7 @@ class ProgramBuilder { /// @param name the name /// @returns the type name template - ast::TypeName* type_name(NAME&& name) const { + const ast::TypeName* type_name(NAME&& name) const { return builder->create( builder->Sym(std::forward(name))); } @@ -703,7 +733,7 @@ class ProgramBuilder { /// @param name the name /// @returns the type name template - ast::TypeName* type_name(const Source& source, NAME&& name) const { + const ast::TypeName* type_name(const Source& source, NAME&& name) const { return builder->create( source, builder->Sym(std::forward(name))); } @@ -713,7 +743,7 @@ class ProgramBuilder { /// @param type the alias type /// @returns the alias pointer template - ast::Alias* alias(NAME&& name, ast::Type* type) const { + const ast::Alias* alias(NAME&& name, const ast::Type* type) const { auto sym = builder->Sym(std::forward(name)); return builder->create(sym, type); } @@ -724,9 +754,9 @@ class ProgramBuilder { /// @param type the alias type /// @returns the alias pointer template - ast::Alias* alias(const Source& source, - NAME&& name, - ast::Type* type) const { + const ast::Alias* alias(const Source& source, + NAME&& name, + const ast::Type* type) const { auto sym = builder->Sym(std::forward(name)); return builder->create(source, sym, type); } @@ -735,9 +765,10 @@ class ProgramBuilder { /// @param storage_class the storage class of the pointer /// @param access the optional access control of the pointer /// @return the pointer to `type` with the given ast::StorageClass - ast::Pointer* pointer(ast::Type* type, - ast::StorageClass storage_class, - ast::Access access = ast::Access::kUndefined) const { + const ast::Pointer* pointer( + const ast::Type* type, + ast::StorageClass storage_class, + ast::Access access = ast::Access::kUndefined) const { return builder->create(type, storage_class, access); } @@ -746,10 +777,11 @@ class ProgramBuilder { /// @param storage_class the storage class of the pointer /// @param access the optional access control of the pointer /// @return the pointer to `type` with the given ast::StorageClass - ast::Pointer* pointer(const Source& source, - ast::Type* type, - ast::StorageClass storage_class, - ast::Access access = ast::Access::kUndefined) const { + const ast::Pointer* pointer( + const Source& source, + const ast::Type* type, + ast::StorageClass storage_class, + ast::Access access = ast::Access::kUndefined) const { return builder->create(source, type, storage_class, access); } @@ -757,60 +789,63 @@ class ProgramBuilder { /// @param access the optional access control of the pointer /// @return the pointer to type `T` with the given ast::StorageClass. template - ast::Pointer* pointer(ast::StorageClass storage_class, - ast::Access access = ast::Access::kUndefined) const { + const ast::Pointer* pointer( + ast::StorageClass storage_class, + ast::Access access = ast::Access::kUndefined) const { return pointer(Of(), storage_class, access); } /// @param source the Source of the node /// @param type the type of the atomic /// @return the atomic to `type` - ast::Atomic* atomic(const Source& source, ast::Type* type) const { + const ast::Atomic* atomic(const Source& source, + const ast::Type* type) const { return builder->create(source, type); } /// @param type the type of the atomic /// @return the atomic to `type` - ast::Atomic* atomic(ast::Type* type) const { + const ast::Atomic* atomic(const ast::Type* type) const { return builder->create(type); } /// @return the atomic to type `T` template - ast::Atomic* atomic() const { + const ast::Atomic* atomic() const { return atomic(Of()); } /// @param kind the kind of sampler /// @returns the sampler - ast::Sampler* sampler(ast::SamplerKind kind) const { + const ast::Sampler* sampler(ast::SamplerKind kind) const { return builder->create(kind); } /// @param source the Source of the node /// @param kind the kind of sampler /// @returns the sampler - ast::Sampler* sampler(const Source& source, ast::SamplerKind kind) const { + const ast::Sampler* sampler(const Source& source, + ast::SamplerKind kind) const { return builder->create(source, kind); } /// @param dims the dimensionality of the texture /// @returns the depth texture - ast::DepthTexture* depth_texture(ast::TextureDimension dims) const { + const ast::DepthTexture* depth_texture(ast::TextureDimension dims) const { return builder->create(dims); } /// @param source the Source of the node /// @param dims the dimensionality of the texture /// @returns the depth texture - ast::DepthTexture* depth_texture(const Source& source, - ast::TextureDimension dims) const { + const ast::DepthTexture* depth_texture(const Source& source, + ast::TextureDimension dims) const { return builder->create(source, dims); } /// @param dims the dimensionality of the texture /// @returns the multisampled depth texture - ast::DepthMultisampledTexture* depth_multisampled_texture( + const ast::DepthMultisampledTexture* depth_multisampled_texture( ast::TextureDimension dims) const { return builder->create(dims); } @@ -818,7 +853,7 @@ class ProgramBuilder { /// @param source the Source of the node /// @param dims the dimensionality of the texture /// @returns the multisampled depth texture - ast::DepthMultisampledTexture* depth_multisampled_texture( + const ast::DepthMultisampledTexture* depth_multisampled_texture( const Source& source, ast::TextureDimension dims) const { return builder->create(source, dims); @@ -827,8 +862,8 @@ class ProgramBuilder { /// @param dims the dimensionality of the texture /// @param subtype the texture subtype. /// @returns the sampled texture - ast::SampledTexture* sampled_texture(ast::TextureDimension dims, - ast::Type* subtype) const { + const ast::SampledTexture* sampled_texture(ast::TextureDimension dims, + const ast::Type* subtype) const { return builder->create(dims, subtype); } @@ -836,17 +871,18 @@ class ProgramBuilder { /// @param dims the dimensionality of the texture /// @param subtype the texture subtype. /// @returns the sampled texture - ast::SampledTexture* sampled_texture(const Source& source, - ast::TextureDimension dims, - ast::Type* subtype) const { + const ast::SampledTexture* sampled_texture(const Source& source, + ast::TextureDimension dims, + const ast::Type* subtype) const { return builder->create(source, dims, subtype); } /// @param dims the dimensionality of the texture /// @param subtype the texture subtype. /// @returns the multisampled texture - ast::MultisampledTexture* multisampled_texture(ast::TextureDimension dims, - ast::Type* subtype) const { + const ast::MultisampledTexture* multisampled_texture( + ast::TextureDimension dims, + const ast::Type* subtype) const { return builder->create(dims, subtype); } @@ -854,9 +890,10 @@ class ProgramBuilder { /// @param dims the dimensionality of the texture /// @param subtype the texture subtype. /// @returns the multisampled texture - ast::MultisampledTexture* multisampled_texture(const Source& source, - ast::TextureDimension dims, - ast::Type* subtype) const { + const ast::MultisampledTexture* multisampled_texture( + const Source& source, + ast::TextureDimension dims, + const ast::Type* subtype) const { return builder->create(source, dims, subtype); } @@ -864,9 +901,9 @@ class ProgramBuilder { /// @param format the image format of the texture /// @param access the access control of the texture /// @returns the storage texture - ast::StorageTexture* storage_texture(ast::TextureDimension dims, - ast::ImageFormat format, - ast::Access access) const { + const ast::StorageTexture* storage_texture(ast::TextureDimension dims, + ast::ImageFormat format, + ast::Access access) const { auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder); return builder->create(dims, format, subtype, access); @@ -877,32 +914,26 @@ class ProgramBuilder { /// @param format the image format of the texture /// @param access the access control of the texture /// @returns the storage texture - ast::StorageTexture* storage_texture(const Source& source, - ast::TextureDimension dims, - ast::ImageFormat format, - ast::Access access) const { + const ast::StorageTexture* storage_texture(const Source& source, + ast::TextureDimension dims, + ast::ImageFormat format, + ast::Access access) const { auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder); return builder->create(source, dims, format, subtype, access); } /// @returns the external texture - ast::ExternalTexture* external_texture() const { + const ast::ExternalTexture* external_texture() const { return builder->create(); } /// @param source the Source of the node /// @returns the external texture - ast::ExternalTexture* external_texture(const Source& source) const { + const ast::ExternalTexture* external_texture(const Source& source) const { return builder->create(source); } - /// [DEPRECATED]: TODO(crbug.com/tint/745): Migrate to const AST pointers. - /// Constructs a TypeName for the type declaration. - /// @param type the type - /// @return either type or a pointer to a new ast::TypeName - ast::TypeName* Of(ast::TypeDecl* type) const; - /// Constructs a TypeName for the type declaration. /// @param type the type /// @return either type or a pointer to a new ast::TypeName @@ -916,7 +947,7 @@ class ProgramBuilder { /// contains a single static `get()` method for obtaining the corresponding /// AST type for the C type `T`. /// `get()` has the signature: - /// `static ast::Type* get(Types* t)` + /// `static const ast::Type* get(Types* t)` template struct CToAST {}; }; @@ -945,111 +976,116 @@ class ProgramBuilder { /// Passthrough for nullptr /// @return nullptr - ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; } + const ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; } /// @param source the source information /// @param symbol the identifier symbol /// @return an ast::IdentifierExpression with the given symbol - ast::IdentifierExpression* Expr(const Source& source, Symbol symbol) { + const ast::IdentifierExpression* Expr(const Source& source, Symbol symbol) { return create(source, symbol); } /// @param symbol the identifier symbol /// @return an ast::IdentifierExpression with the given symbol - ast::IdentifierExpression* Expr(Symbol symbol) { + const ast::IdentifierExpression* Expr(Symbol symbol) { return create(symbol); } /// @param source the source information /// @param variable the AST variable /// @return an ast::IdentifierExpression with the variable's symbol - ast::IdentifierExpression* Expr(const Source& source, - ast::Variable* variable) { + const ast::IdentifierExpression* Expr(const Source& source, + const ast::Variable* variable) { return create(source, variable->symbol); } /// @param variable the AST variable /// @return an ast::IdentifierExpression with the variable's symbol - ast::IdentifierExpression* Expr(ast::Variable* variable) { + const ast::IdentifierExpression* Expr(const ast::Variable* variable) { return create(variable->symbol); } /// @param source the source information /// @param name the identifier name /// @return an ast::IdentifierExpression with the given name - ast::IdentifierExpression* Expr(const Source& source, const char* name) { + const ast::IdentifierExpression* Expr(const Source& source, + const char* name) { return create(source, Symbols().Register(name)); } /// @param name the identifier name /// @return an ast::IdentifierExpression with the given name - ast::IdentifierExpression* Expr(const char* name) { + const ast::IdentifierExpression* Expr(const char* name) { return create(Symbols().Register(name)); } /// @param source the source information /// @param name the identifier name /// @return an ast::IdentifierExpression with the given name - ast::IdentifierExpression* Expr(const Source& source, - const std::string& name) { + const ast::IdentifierExpression* Expr(const Source& source, + const std::string& name) { return create(source, Symbols().Register(name)); } /// @param name the identifier name /// @return an ast::IdentifierExpression with the given name - ast::IdentifierExpression* Expr(const std::string& name) { + const ast::IdentifierExpression* Expr(const std::string& name) { return create(Symbols().Register(name)); } /// @param source the source information /// @param value the boolean value /// @return a Scalar constructor for the given value - ast::ScalarConstructorExpression* Expr(const Source& source, bool value) { + const ast::ScalarConstructorExpression* Expr(const Source& source, + bool value) { return create(source, Literal(value)); } /// @param value the boolean value /// @return a Scalar constructor for the given value - ast::ScalarConstructorExpression* Expr(bool value) { + const ast::ScalarConstructorExpression* Expr(bool value) { return create(Literal(value)); } /// @param source the source information /// @param value the float value /// @return a Scalar constructor for the given value - ast::ScalarConstructorExpression* Expr(const Source& source, f32 value) { + const ast::ScalarConstructorExpression* Expr(const Source& source, + f32 value) { return create(source, Literal(value)); } /// @param value the float value /// @return a Scalar constructor for the given value - ast::ScalarConstructorExpression* Expr(f32 value) { + const ast::ScalarConstructorExpression* Expr(f32 value) { return create(Literal(value)); } /// @param source the source information /// @param value the integer value /// @return a Scalar constructor for the given value - ast::ScalarConstructorExpression* Expr(const Source& source, i32 value) { + const ast::ScalarConstructorExpression* Expr(const Source& source, + i32 value) { return create(source, Literal(value)); } /// @param value the integer value /// @return a Scalar constructor for the given value - ast::ScalarConstructorExpression* Expr(i32 value) { + const ast::ScalarConstructorExpression* Expr(i32 value) { return create(Literal(value)); } /// @param source the source information /// @param value the unsigned int value /// @return a Scalar constructor for the given value - ast::ScalarConstructorExpression* Expr(const Source& source, u32 value) { + const ast::ScalarConstructorExpression* Expr(const Source& source, + u32 value) { return create(source, Literal(value)); } /// @param value the unsigned int value /// @return a Scalar constructor for the given value - ast::ScalarConstructorExpression* Expr(u32 value) { + const ast::ScalarConstructorExpression* Expr(u32 value) { return create(Literal(value)); } @@ -1094,52 +1130,60 @@ class ProgramBuilder { /// @param source the source location for the literal /// @param val the boolan value /// @return a boolean literal with the given value - ast::BoolLiteral* Literal(const Source& source, bool val) { + const ast::BoolLiteral* Literal(const Source& source, bool val) { return create(source, val); } /// @param val the boolan value /// @return a boolean literal with the given value - ast::BoolLiteral* Literal(bool val) { return create(val); } + const ast::BoolLiteral* Literal(bool val) { + return create(val); + } /// @param source the source location for the literal /// @param val the float value /// @return a float literal with the given value - ast::FloatLiteral* Literal(const Source& source, f32 val) { + const ast::FloatLiteral* Literal(const Source& source, f32 val) { return create(source, val); } /// @param val the float value /// @return a float literal with the given value - ast::FloatLiteral* Literal(f32 val) { return create(val); } + const ast::FloatLiteral* Literal(f32 val) { + return create(val); + } /// @param source the source location for the literal /// @param val the unsigned int value /// @return a ast::UintLiteral with the given value - ast::UintLiteral* Literal(const Source& source, u32 val) { + const ast::UintLiteral* Literal(const Source& source, u32 val) { return create(source, val); } /// @param val the unsigned int value /// @return a ast::UintLiteral with the given value - ast::UintLiteral* Literal(u32 val) { return create(val); } + const ast::UintLiteral* Literal(u32 val) { + return create(val); + } /// @param source the source location for the literal /// @param val the integer value /// @return the ast::SintLiteral with the given value - ast::SintLiteral* Literal(const Source& source, i32 val) { + const ast::SintLiteral* Literal(const Source& source, i32 val) { return create(source, val); } /// @param val the integer value /// @return the ast::SintLiteral with the given value - ast::SintLiteral* Literal(i32 val) { return create(val); } + const ast::SintLiteral* Literal(i32 val) { + return create(val); + } /// @param args the arguments for the type constructor /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values /// of `args` converted to `ast::Expression`s using `Expr()` template - ast::TypeConstructorExpression* Construct(ARGS&&... args) { + const ast::TypeConstructorExpression* Construct(ARGS&&... args) { return Construct(ty.Of(), std::forward(args)...); } @@ -1148,7 +1192,8 @@ class ProgramBuilder { /// @return an `ast::TypeConstructorExpression` of `type` constructed with the /// values `args`. template - ast::TypeConstructorExpression* Construct(ast::Type* type, ARGS&&... args) { + const ast::TypeConstructorExpression* Construct(const ast::Type* type, + ARGS&&... args) { return create( type, ExprList(std::forward(args)...)); } @@ -1159,9 +1204,9 @@ class ProgramBuilder { /// @return an `ast::TypeConstructorExpression` of `type` constructed with the /// values `args`. template - ast::TypeConstructorExpression* Construct(const Source& source, - ast::Type* type, - ARGS&&... args) { + const ast::TypeConstructorExpression* Construct(const Source& source, + const ast::Type* type, + ARGS&&... args) { return create( source, type, ExprList(std::forward(args)...)); } @@ -1170,7 +1215,7 @@ class ProgramBuilder { /// @return an `ast::BitcastExpression` of type `ty`, with the values of /// `expr` converted to `ast::Expression`s using `Expr()` template - ast::BitcastExpression* Bitcast(EXPR&& expr) { + const ast::BitcastExpression* Bitcast(EXPR&& expr) { return Bitcast(ty.Of(), std::forward(expr)); } @@ -1179,7 +1224,7 @@ class ProgramBuilder { /// @return an `ast::BitcastExpression` of `type` constructed with the values /// `expr`. template - ast::BitcastExpression* Bitcast(ast::Type* type, EXPR&& expr) { + const ast::BitcastExpression* Bitcast(const ast::Type* type, EXPR&& expr) { return create(type, Expr(std::forward(expr))); } @@ -1189,9 +1234,9 @@ class ProgramBuilder { /// @return an `ast::BitcastExpression` of `type` constructed with the values /// `expr`. template - ast::BitcastExpression* Bitcast(const Source& source, - ast::Type* type, - EXPR&& expr) { + const ast::BitcastExpression* Bitcast(const Source& source, + const ast::Type* type, + EXPR&& expr) { return create(source, type, Expr(std::forward(expr))); } @@ -1202,9 +1247,9 @@ class ProgramBuilder { /// @return an `ast::TypeConstructorExpression` of a `size`-element vector of /// type `type`, constructed with the values `args`. template - ast::TypeConstructorExpression* vec(ast::Type* type, - uint32_t size, - ARGS&&... args) { + const ast::TypeConstructorExpression* vec(const ast::Type* type, + uint32_t size, + ARGS&&... args) { return Construct(ty.vec(type, size), std::forward(args)...); } @@ -1212,7 +1257,7 @@ class ProgramBuilder { /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type /// `T`, constructed with the values `args`. template - ast::TypeConstructorExpression* vec2(ARGS&&... args) { + const ast::TypeConstructorExpression* vec2(ARGS&&... args) { return Construct(ty.vec2(), std::forward(args)...); } @@ -1220,7 +1265,7 @@ class ProgramBuilder { /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type /// `T`, constructed with the values `args`. template - ast::TypeConstructorExpression* vec3(ARGS&&... args) { + const ast::TypeConstructorExpression* vec3(ARGS&&... args) { return Construct(ty.vec3(), std::forward(args)...); } @@ -1228,7 +1273,7 @@ class ProgramBuilder { /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type /// `T`, constructed with the values `args`. template - ast::TypeConstructorExpression* vec4(ARGS&&... args) { + const ast::TypeConstructorExpression* vec4(ARGS&&... args) { return Construct(ty.vec4(), std::forward(args)...); } @@ -1236,7 +1281,7 @@ class ProgramBuilder { /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type /// `T`, constructed with the values `args`. template - ast::TypeConstructorExpression* mat2x2(ARGS&&... args) { + const ast::TypeConstructorExpression* mat2x2(ARGS&&... args) { return Construct(ty.mat2x2(), std::forward(args)...); } @@ -1244,7 +1289,7 @@ class ProgramBuilder { /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type /// `T`, constructed with the values `args`. template - ast::TypeConstructorExpression* mat2x3(ARGS&&... args) { + const ast::TypeConstructorExpression* mat2x3(ARGS&&... args) { return Construct(ty.mat2x3(), std::forward(args)...); } @@ -1252,7 +1297,7 @@ class ProgramBuilder { /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type /// `T`, constructed with the values `args`. template - ast::TypeConstructorExpression* mat2x4(ARGS&&... args) { + const ast::TypeConstructorExpression* mat2x4(ARGS&&... args) { return Construct(ty.mat2x4(), std::forward(args)...); } @@ -1260,7 +1305,7 @@ class ProgramBuilder { /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type /// `T`, constructed with the values `args`. template - ast::TypeConstructorExpression* mat3x2(ARGS&&... args) { + const ast::TypeConstructorExpression* mat3x2(ARGS&&... args) { return Construct(ty.mat3x2(), std::forward(args)...); } @@ -1268,7 +1313,7 @@ class ProgramBuilder { /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type /// `T`, constructed with the values `args`. template - ast::TypeConstructorExpression* mat3x3(ARGS&&... args) { + const ast::TypeConstructorExpression* mat3x3(ARGS&&... args) { return Construct(ty.mat3x3(), std::forward(args)...); } @@ -1276,7 +1321,7 @@ class ProgramBuilder { /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type /// `T`, constructed with the values `args`. template - ast::TypeConstructorExpression* mat3x4(ARGS&&... args) { + const ast::TypeConstructorExpression* mat3x4(ARGS&&... args) { return Construct(ty.mat3x4(), std::forward(args)...); } @@ -1284,7 +1329,7 @@ class ProgramBuilder { /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type /// `T`, constructed with the values `args`. template - ast::TypeConstructorExpression* mat4x2(ARGS&&... args) { + const ast::TypeConstructorExpression* mat4x2(ARGS&&... args) { return Construct(ty.mat4x2(), std::forward(args)...); } @@ -1292,7 +1337,7 @@ class ProgramBuilder { /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type /// `T`, constructed with the values `args`. template - ast::TypeConstructorExpression* mat4x3(ARGS&&... args) { + const ast::TypeConstructorExpression* mat4x3(ARGS&&... args) { return Construct(ty.mat4x3(), std::forward(args)...); } @@ -1300,7 +1345,7 @@ class ProgramBuilder { /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type /// `T`, constructed with the values `args`. template - ast::TypeConstructorExpression* mat4x4(ARGS&&... args) { + const ast::TypeConstructorExpression* mat4x4(ARGS&&... args) { return Construct(ty.mat4x4(), std::forward(args)...); } @@ -1308,7 +1353,7 @@ class ProgramBuilder { /// @return an `ast::TypeConstructorExpression` of an array with element type /// `T` and size `N`, constructed with the values `args`. template - ast::TypeConstructorExpression* array(ARGS&&... args) { + const ast::TypeConstructorExpression* array(ARGS&&... args) { return Construct(ty.array(), std::forward(args)...); } @@ -1318,9 +1363,9 @@ class ProgramBuilder { /// @return an `ast::TypeConstructorExpression` of an array with element type /// `subtype`, constructed with the values `args`. template - ast::TypeConstructorExpression* array(ast::Type* subtype, - EXPR&& n, - ARGS&&... args) { + const ast::TypeConstructorExpression* array(const ast::Type* subtype, + EXPR&& n, + ARGS&&... args) { return Construct(ty.array(subtype, std::forward(n)), std::forward(args)...); } @@ -1338,9 +1383,9 @@ class ProgramBuilder { /// @returns a `ast::Variable` with the given name, type and additional /// options template - ast::Variable* Var(NAME&& name, - const ast::Type* type, - OPTIONAL&&... optional) { + const ast::Variable* Var(NAME&& name, + const ast::Type* type, + OPTIONAL&&... optional) { VarOptionals opts(std::forward(optional)...); return create(Sym(std::forward(name)), opts.storage, opts.access, type, false, opts.constructor, @@ -1360,10 +1405,10 @@ class ProgramBuilder { /// value. /// @returns a `ast::Variable` with the given name, storage and type template - ast::Variable* Var(const Source& source, - NAME&& name, - const ast::Type* type, - OPTIONAL&&... optional) { + const ast::Variable* Var(const Source& source, + NAME&& name, + const ast::Type* type, + OPTIONAL&&... optional) { VarOptionals opts(std::forward(optional)...); return create(source, Sym(std::forward(name)), opts.storage, opts.access, type, false, @@ -1376,10 +1421,10 @@ class ProgramBuilder { /// @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, - ast::DecorationList decorations = {}) { + const ast::Variable* Const(NAME&& name, + const ast::Type* type, + const ast::Expression* constructor, + ast::DecorationList decorations = {}) { return create( Sym(std::forward(name)), ast::StorageClass::kNone, ast::Access::kUndefined, type, true, constructor, decorations); @@ -1392,11 +1437,11 @@ class ProgramBuilder { /// @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, - ast::DecorationList decorations = {}) { + const ast::Variable* Const(const Source& source, + NAME&& name, + const ast::Type* type, + const ast::Expression* constructor, + ast::DecorationList decorations = {}) { return create( source, Sym(std::forward(name)), ast::StorageClass::kNone, ast::Access::kUndefined, type, true, constructor, decorations); @@ -1407,9 +1452,9 @@ class ProgramBuilder { /// @param decorations optional parameter decorations /// @returns a constant `ast::Variable` with the given name and type template - ast::Variable* Param(NAME&& name, - ast::Type* type, - ast::DecorationList decorations = {}) { + const ast::Variable* Param(NAME&& name, + const ast::Type* type, + ast::DecorationList decorations = {}) { return create( Sym(std::forward(name)), ast::StorageClass::kNone, ast::Access::kUndefined, type, true, nullptr, decorations); @@ -1421,10 +1466,10 @@ class ProgramBuilder { /// @param decorations optional parameter decorations /// @returns a constant `ast::Variable` with the given name and type template - ast::Variable* Param(const Source& source, - NAME&& name, - ast::Type* type, - ast::DecorationList decorations = {}) { + const ast::Variable* Param(const Source& source, + NAME&& name, + const ast::Type* type, + ast::DecorationList decorations = {}) { return create( source, Sym(std::forward(name)), ast::StorageClass::kNone, ast::Access::kUndefined, type, true, nullptr, decorations); @@ -1445,9 +1490,9 @@ class ProgramBuilder { template > - ast::Variable* Global(NAME&& name, - const ast::Type* type, - OPTIONAL&&... optional) { + const ast::Variable* Global(NAME&& name, + const ast::Type* type, + OPTIONAL&&... optional) { auto* var = Var(std::forward(name), type, std::forward(optional)...); AST().AddGlobalVariable(var); @@ -1468,10 +1513,10 @@ class ProgramBuilder { /// @returns a new `ast::Variable`, which is automatically registered as a /// global variable with the ast::Module. template - ast::Variable* Global(const Source& source, - NAME&& name, - ast::Type* type, - OPTIONAL&&... optional) { + const ast::Variable* Global(const Source& source, + NAME&& name, + const ast::Type* type, + OPTIONAL&&... optional) { auto* var = Var(source, std::forward(name), type, std::forward(optional)...); AST().AddGlobalVariable(var); @@ -1486,10 +1531,10 @@ class ProgramBuilder { /// arguments of `args`, which is automatically registered as a global /// variable with the ast::Module. template - ast::Variable* GlobalConst(NAME&& name, - ast::Type* type, - ast::Expression* constructor, - ast::DecorationList decorations = {}) { + const ast::Variable* GlobalConst(NAME&& name, + const ast::Type* type, + const ast::Expression* constructor, + ast::DecorationList decorations = {}) { auto* var = Const(std::forward(name), type, constructor, std::move(decorations)); AST().AddGlobalVariable(var); @@ -1505,11 +1550,11 @@ class ProgramBuilder { /// arguments of `args`, which is automatically registered as a global /// variable with the ast::Module. template - ast::Variable* GlobalConst(const Source& source, - NAME&& name, - ast::Type* type, - ast::Expression* constructor, - ast::DecorationList decorations = {}) { + const ast::Variable* GlobalConst(const Source& source, + NAME&& name, + const ast::Type* type, + const ast::Expression* constructor, + ast::DecorationList decorations = {}) { auto* var = Const(source, std::forward(name), type, constructor, std::move(decorations)); AST().AddGlobalVariable(var); @@ -1520,7 +1565,7 @@ class ProgramBuilder { /// @param expr the expression to take the address of /// @return an ast::UnaryOpExpression that takes the address of `expr` template - ast::UnaryOpExpression* AddressOf(const Source& source, EXPR&& expr) { + const ast::UnaryOpExpression* AddressOf(const Source& source, EXPR&& expr) { return create(source, ast::UnaryOp::kAddressOf, Expr(std::forward(expr))); } @@ -1528,7 +1573,7 @@ class ProgramBuilder { /// @param expr the expression to take the address of /// @return an ast::UnaryOpExpression that takes the address of `expr` template - ast::UnaryOpExpression* AddressOf(EXPR&& expr) { + const ast::UnaryOpExpression* AddressOf(EXPR&& expr) { return create(ast::UnaryOp::kAddressOf, Expr(std::forward(expr))); } @@ -1537,7 +1582,7 @@ class ProgramBuilder { /// @param expr the expression to perform an indirection on /// @return an ast::UnaryOpExpression that dereferences the pointer `expr` template - ast::UnaryOpExpression* Deref(const Source& source, EXPR&& expr) { + const ast::UnaryOpExpression* Deref(const Source& source, EXPR&& expr) { return create(source, ast::UnaryOp::kIndirection, Expr(std::forward(expr))); } @@ -1545,7 +1590,7 @@ class ProgramBuilder { /// @param expr the expression to perform an indirection on /// @return an ast::UnaryOpExpression that dereferences the pointer `expr` template - ast::UnaryOpExpression* Deref(EXPR&& expr) { + const ast::UnaryOpExpression* Deref(EXPR&& expr) { return create(ast::UnaryOp::kIndirection, Expr(std::forward(expr))); } @@ -1556,7 +1601,9 @@ class ProgramBuilder { /// @returns a `ast::CallExpression` to the function `func`, with the /// arguments of `args` converted to `ast::Expression`s using `Expr()`. template - ast::CallExpression* Call(const Source& source, NAME&& func, ARGS&&... args) { + const ast::CallExpression* Call(const Source& source, + NAME&& func, + ARGS&&... args) { return create(source, Expr(func), ExprList(std::forward(args)...)); } @@ -1566,7 +1613,7 @@ class ProgramBuilder { /// @returns a `ast::CallExpression` to the function `func`, with the /// arguments of `args` converted to `ast::Expression`s using `Expr()`. template > - ast::CallExpression* Call(NAME&& func, ARGS&&... args) { + const ast::CallExpression* Call(NAME&& func, ARGS&&... args) { return create(Expr(func), ExprList(std::forward(args)...)); } @@ -1575,7 +1622,7 @@ class ProgramBuilder { /// @returns a `ast::CallStatement` that calls the `ignore` intrinsic which is /// passed the single `expr` argument template - ast::CallStatement* Ignore(EXPR&& expr) { + const ast::CallStatement* Ignore(EXPR&& expr) { return create(Call("ignore", Expr(expr))); } @@ -1583,7 +1630,7 @@ class ProgramBuilder { /// @param rhs the right hand argument to the addition operation /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs` template - ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) { + const ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) { return create(ast::BinaryOp::kAdd, Expr(std::forward(lhs)), Expr(std::forward(rhs))); @@ -1593,7 +1640,7 @@ class ProgramBuilder { /// @param rhs the right hand argument to the and operation /// @returns a `ast::BinaryExpression` bitwise anding `lhs` and `rhs` template - ast::BinaryExpression* And(LHS&& lhs, RHS&& rhs) { + const ast::BinaryExpression* And(LHS&& lhs, RHS&& rhs) { return create(ast::BinaryOp::kAnd, Expr(std::forward(lhs)), Expr(std::forward(rhs))); @@ -1603,7 +1650,7 @@ class ProgramBuilder { /// @param rhs the right hand argument to the or operation /// @returns a `ast::BinaryExpression` bitwise or-ing `lhs` and `rhs` template - ast::BinaryExpression* Or(LHS&& lhs, RHS&& rhs) { + const ast::BinaryExpression* Or(LHS&& lhs, RHS&& rhs) { return create(ast::BinaryOp::kOr, Expr(std::forward(lhs)), Expr(std::forward(rhs))); @@ -1613,7 +1660,7 @@ class ProgramBuilder { /// @param rhs the right hand argument to the subtraction operation /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs` template - ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) { + const ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) { return create(ast::BinaryOp::kSubtract, Expr(std::forward(lhs)), Expr(std::forward(rhs))); @@ -1623,7 +1670,7 @@ class ProgramBuilder { /// @param rhs the right hand argument to the multiplication operation /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs` template - ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) { + const ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) { return create(ast::BinaryOp::kMultiply, Expr(std::forward(lhs)), Expr(std::forward(rhs))); @@ -1634,7 +1681,7 @@ class ProgramBuilder { /// @param rhs the right hand argument to the multiplication operation /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs` template - ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) { + const ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) { return create(source, ast::BinaryOp::kMultiply, Expr(std::forward(lhs)), Expr(std::forward(rhs))); @@ -1644,7 +1691,7 @@ class ProgramBuilder { /// @param rhs the right hand argument to the division operation /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs` template - ast::Expression* Div(LHS&& lhs, RHS&& rhs) { + const ast::Expression* Div(LHS&& lhs, RHS&& rhs) { return create(ast::BinaryOp::kDivide, Expr(std::forward(lhs)), Expr(std::forward(rhs))); @@ -1654,7 +1701,7 @@ class ProgramBuilder { /// @param rhs the right hand argument to the bit shift right operation /// @returns a `ast::BinaryExpression` bit shifting right `lhs` by `rhs` template - ast::BinaryExpression* Shr(LHS&& lhs, RHS&& rhs) { + const ast::BinaryExpression* Shr(LHS&& lhs, RHS&& rhs) { return create(ast::BinaryOp::kShiftRight, Expr(std::forward(lhs)), Expr(std::forward(rhs))); @@ -1664,7 +1711,7 @@ class ProgramBuilder { /// @param rhs the right hand argument to the bit shift left operation /// @returns a `ast::BinaryExpression` bit shifting left `lhs` by `rhs` template - ast::BinaryExpression* Shl(LHS&& lhs, RHS&& rhs) { + const ast::BinaryExpression* Shl(LHS&& lhs, RHS&& rhs) { return create(ast::BinaryOp::kShiftLeft, Expr(std::forward(lhs)), Expr(std::forward(rhs))); @@ -1675,9 +1722,9 @@ class ProgramBuilder { /// @param idx the index argument for the array accessor expression /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx` template - ast::ArrayAccessorExpression* IndexAccessor(const Source& source, - ARR&& arr, - IDX&& idx) { + const ast::ArrayAccessorExpression* IndexAccessor(const Source& source, + ARR&& arr, + IDX&& idx) { return create( source, Expr(std::forward(arr)), Expr(std::forward(idx))); } @@ -1686,7 +1733,7 @@ class ProgramBuilder { /// @param idx the index argument for the array accessor expression /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx` template - ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) { + const ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) { return create(Expr(std::forward(arr)), Expr(std::forward(idx))); } @@ -1695,7 +1742,7 @@ class ProgramBuilder { /// @param idx the index argument for the array accessor expression /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx` template - ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) { + const ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) { return create(Expr(std::forward(obj)), Expr(std::forward(idx))); } @@ -1703,7 +1750,7 @@ class ProgramBuilder { /// Creates a ast::StructMemberOffsetDecoration /// @param val the offset value /// @returns the offset decoration pointer - ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) { + const ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) { return create(source_, val); } @@ -1711,15 +1758,15 @@ class ProgramBuilder { /// @param source the source information /// @param val the size value /// @returns the size decoration pointer - ast::StructMemberSizeDecoration* MemberSize(const Source& source, - uint32_t val) { + const ast::StructMemberSizeDecoration* MemberSize(const Source& source, + uint32_t val) { return create(source, val); } /// Creates a ast::StructMemberSizeDecoration /// @param val the size value /// @returns the size decoration pointer - ast::StructMemberSizeDecoration* MemberSize(uint32_t val) { + const ast::StructMemberSizeDecoration* MemberSize(uint32_t val) { return create(source_, val); } @@ -1727,35 +1774,35 @@ class ProgramBuilder { /// @param source the source information /// @param val the align value /// @returns the align decoration pointer - ast::StructMemberAlignDecoration* MemberAlign(const Source& source, - uint32_t val) { + const ast::StructMemberAlignDecoration* MemberAlign(const Source& source, + uint32_t val) { return create(source, val); } /// Creates a ast::StructMemberAlignDecoration /// @param val the align value /// @returns the align decoration pointer - ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) { + const ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) { return create(source_, val); } /// Creates a ast::StructBlockDecoration /// @returns the struct block decoration pointer - ast::StructBlockDecoration* StructBlock() { + const ast::StructBlockDecoration* StructBlock() { return create(); } /// Creates the ast::GroupDecoration /// @param value group decoration index /// @returns the group decoration pointer - ast::GroupDecoration* Group(uint32_t value) { + const ast::GroupDecoration* Group(uint32_t value) { return create(value); } /// Creates the ast::BindingDecoration /// @param value the binding index /// @returns the binding deocration pointer - ast::BindingDecoration* Binding(uint32_t value) { + const ast::BindingDecoration* Binding(uint32_t value) { return create(value); } @@ -1779,13 +1826,13 @@ class ProgramBuilder { /// decorations /// @returns the function pointer template - ast::Function* Func(const Source& source, - NAME&& name, - ast::VariableList params, - ast::Type* type, - ast::StatementList body, - ast::DecorationList decorations = {}, - ast::DecorationList return_type_decorations = {}) { + const ast::Function* Func(const Source& source, + NAME&& name, + ast::VariableList params, + const ast::Type* type, + ast::StatementList body, + ast::DecorationList decorations = {}, + ast::DecorationList return_type_decorations = {}) { auto* func = create(source, Sym(std::forward(name)), params, type, create(body), @@ -1804,12 +1851,12 @@ class ProgramBuilder { /// decorations /// @returns the function pointer template - ast::Function* Func(NAME&& name, - ast::VariableList params, - ast::Type* type, - ast::StatementList body, - ast::DecorationList decorations = {}, - ast::DecorationList return_type_decorations = {}) { + const ast::Function* Func(NAME&& name, + ast::VariableList params, + const ast::Type* type, + ast::StatementList body, + ast::DecorationList decorations = {}, + ast::DecorationList return_type_decorations = {}) { auto* func = create(Sym(std::forward(name)), params, type, create(body), decorations, return_type_decorations); @@ -1820,31 +1867,33 @@ class ProgramBuilder { /// Creates an ast::BreakStatement /// @param source the source information /// @returns the break statement pointer - ast::BreakStatement* Break(const Source& source) { + const ast::BreakStatement* Break(const Source& source) { return create(source); } /// Creates an ast::BreakStatement /// @returns the break statement pointer - ast::BreakStatement* Break() { return create(); } + const ast::BreakStatement* Break() { return create(); } /// Creates an ast::ReturnStatement with no return value /// @param source the source information /// @returns the return statement pointer - ast::ReturnStatement* Return(const Source& source) { + const ast::ReturnStatement* Return(const Source& source) { return create(source); } /// Creates an ast::ReturnStatement with no return value /// @returns the return statement pointer - ast::ReturnStatement* Return() { return create(); } + const ast::ReturnStatement* Return() { + return create(); + } /// Creates an ast::ReturnStatement with the given return value /// @param source the source information /// @param val the return value /// @returns the return statement pointer template - ast::ReturnStatement* Return(const Source& source, EXPR&& val) { + const ast::ReturnStatement* Return(const Source& source, EXPR&& val) { return create(source, Expr(std::forward(val))); } @@ -1852,7 +1901,7 @@ class ProgramBuilder { /// @param val the return value /// @returns the return statement pointer template > - ast::ReturnStatement* Return(EXPR&& val) { + const ast::ReturnStatement* Return(EXPR&& val) { return create(Expr(std::forward(val))); } @@ -1862,7 +1911,9 @@ class ProgramBuilder { /// @param type the alias target type /// @returns the alias type template - ast::Alias* Alias(const Source& source, NAME&& name, ast::Type* type) { + const ast::Alias* Alias(const Source& source, + NAME&& name, + const ast::Type* type) { auto* out = ty.alias(source, std::forward(name), type); AST().AddTypeDecl(out); return out; @@ -1873,7 +1924,7 @@ class ProgramBuilder { /// @param type the alias target type /// @returns the alias type template - ast::Alias* Alias(NAME&& name, ast::Type* type) { + const ast::Alias* Alias(NAME&& name, const ast::Type* type) { auto* out = ty.alias(std::forward(name), type); AST().AddTypeDecl(out); return out; @@ -1886,10 +1937,10 @@ class ProgramBuilder { /// @param decorations the optional struct decorations /// @returns the struct type template - ast::Struct* Structure(const Source& source, - NAME&& name, - ast::StructMemberList members, - ast::DecorationList decorations = {}) { + const ast::Struct* Structure(const Source& source, + NAME&& name, + ast::StructMemberList members, + ast::DecorationList decorations = {}) { auto sym = Sym(std::forward(name)); auto* type = create(source, sym, std::move(members), std::move(decorations)); @@ -1903,9 +1954,9 @@ class ProgramBuilder { /// @param decorations the optional struct decorations /// @returns the struct type template - ast::Struct* Structure(NAME&& name, - ast::StructMemberList members, - ast::DecorationList decorations = {}) { + const ast::Struct* Structure(NAME&& name, + ast::StructMemberList members, + ast::DecorationList decorations = {}) { auto sym = Sym(std::forward(name)); auto* type = create(sym, std::move(members), std::move(decorations)); @@ -1920,10 +1971,10 @@ class ProgramBuilder { /// @param decorations the optional struct member decorations /// @returns the struct member pointer template - ast::StructMember* Member(const Source& source, - NAME&& name, - ast::Type* type, - ast::DecorationList decorations = {}) { + const ast::StructMember* Member(const Source& source, + NAME&& name, + const ast::Type* type, + ast::DecorationList decorations = {}) { return create(source, Sym(std::forward(name)), type, std::move(decorations)); } @@ -1934,9 +1985,9 @@ class ProgramBuilder { /// @param decorations the optional struct member decorations /// @returns the struct member pointer template - ast::StructMember* Member(NAME&& name, - ast::Type* type, - ast::DecorationList decorations = {}) { + const ast::StructMember* Member(NAME&& name, + const ast::Type* type, + ast::DecorationList decorations = {}) { return create(source_, Sym(std::forward(name)), type, std::move(decorations)); } @@ -1947,7 +1998,9 @@ class ProgramBuilder { /// @param type the struct member type /// @returns the struct member pointer template - ast::StructMember* Member(uint32_t offset, NAME&& name, ast::Type* type) { + const ast::StructMember* Member(uint32_t offset, + NAME&& name, + const ast::Type* type) { return create( source_, Sym(std::forward(name)), type, ast::DecorationList{ @@ -1960,7 +2013,8 @@ class ProgramBuilder { /// @param statements statements of block /// @returns the block statement pointer template - ast::BlockStatement* Block(const Source& source, Statements&&... statements) { + const ast::BlockStatement* Block(const Source& source, + Statements&&... statements) { return create( source, ast::StatementList{std::forward(statements)...}); } @@ -1969,7 +2023,7 @@ class ProgramBuilder { /// @param statements statements of block /// @returns the block statement pointer template > - ast::BlockStatement* Block(STATEMENTS&&... statements) { + const ast::BlockStatement* Block(STATEMENTS&&... statements) { return create( ast::StatementList{std::forward(statements)...}); } @@ -1979,7 +2033,8 @@ class ProgramBuilder { /// @param body the else body /// @returns the else statement pointer template - ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) { + const ast::ElseStatement* Else(CONDITION&& condition, + const ast::BlockStatement* body) { return create(Expr(std::forward(condition)), body); } @@ -1991,9 +2046,9 @@ class ProgramBuilder { /// @param elseStatements optional variadic else statements /// @returns the if statement pointer template - ast::IfStatement* If(CONDITION&& condition, - ast::BlockStatement* body, - ELSE_STATEMENTS&&... elseStatements) { + const ast::IfStatement* If(CONDITION&& condition, + const ast::BlockStatement* body, + ELSE_STATEMENTS&&... elseStatements) { return create( Expr(std::forward(condition)), body, ast::ElseStatementList{ @@ -2006,9 +2061,9 @@ class ProgramBuilder { /// @param rhs the right hand side expression initializer /// @returns the assignment statement pointer template - ast::AssignmentStatement* Assign(const Source& source, - LhsExpressionInit&& lhs, - RhsExpressionInit&& rhs) { + const ast::AssignmentStatement* Assign(const Source& source, + LhsExpressionInit&& lhs, + RhsExpressionInit&& rhs) { return create( source, Expr(std::forward(lhs)), Expr(std::forward(rhs))); @@ -2019,8 +2074,8 @@ class ProgramBuilder { /// @param rhs the right hand side expression initializer /// @returns the assignment statement pointer template - ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs, - RhsExpressionInit&& rhs) { + const ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs, + RhsExpressionInit&& rhs) { return create( Expr(std::forward(lhs)), Expr(std::forward(rhs))); @@ -2030,8 +2085,9 @@ class ProgramBuilder { /// @param body the loop body /// @param continuing the optional continuing block /// @returns the loop statement pointer - ast::LoopStatement* Loop(ast::BlockStatement* body, - ast::BlockStatement* continuing = nullptr) { + const ast::LoopStatement* Loop( + const ast::BlockStatement* body, + const ast::BlockStatement* continuing = nullptr) { return create(body, continuing); } @@ -2044,11 +2100,11 @@ class ProgramBuilder { /// @param body the loop body /// @returns the for loop statement pointer template - ast::ForLoopStatement* For(const Source& source, - ast::Statement* init, - COND&& cond, - ast::Statement* cont, - ast::BlockStatement* body) { + const ast::ForLoopStatement* For(const Source& source, + const ast::Statement* init, + COND&& cond, + const ast::Statement* cont, + const ast::BlockStatement* body) { return create( source, init, Expr(std::forward(cond)), cont, body); } @@ -2061,10 +2117,10 @@ class ProgramBuilder { /// @param body the loop body /// @returns the for loop statement pointer template - ast::ForLoopStatement* For(ast::Statement* init, - COND&& cond, - ast::Statement* cont, - ast::BlockStatement* body) { + const ast::ForLoopStatement* For(const ast::Statement* init, + COND&& cond, + const ast::Statement* cont, + const ast::BlockStatement* body) { return create(init, Expr(std::forward(cond)), cont, body); } @@ -2073,14 +2129,15 @@ class ProgramBuilder { /// @param source the source information /// @param var the variable to wrap in a decl statement /// @returns the variable decl statement pointer - ast::VariableDeclStatement* Decl(const Source& source, ast::Variable* var) { + const ast::VariableDeclStatement* Decl(const Source& source, + const ast::Variable* var) { return create(source, var); } /// Creates a ast::VariableDeclStatement for the input variable /// @param var the variable to wrap in a decl statement /// @returns the variable decl statement pointer - ast::VariableDeclStatement* Decl(ast::Variable* var) { + const ast::VariableDeclStatement* Decl(const ast::Variable* var) { return create(var); } @@ -2090,9 +2147,9 @@ class ProgramBuilder { /// @param cases case statements /// @returns the switch statement pointer template - ast::SwitchStatement* Switch(const Source& source, - ExpressionInit&& condition, - Cases&&... cases) { + const ast::SwitchStatement* Switch(const Source& source, + ExpressionInit&& condition, + Cases&&... cases) { return create( source, Expr(std::forward(condition)), ast::CaseStatementList{std::forward(cases)...}); @@ -2105,7 +2162,8 @@ class ProgramBuilder { template > - ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) { + const ast::SwitchStatement* Switch(ExpressionInit&& condition, + Cases&&... cases) { return create( Expr(std::forward(condition)), ast::CaseStatementList{std::forward(cases)...}); @@ -2116,9 +2174,9 @@ class ProgramBuilder { /// @param selectors list of selectors /// @param body the case body /// @returns the case statement pointer - ast::CaseStatement* Case(const Source& source, - ast::CaseSelectorList selectors, - ast::BlockStatement* body = nullptr) { + const ast::CaseStatement* Case(const Source& source, + ast::CaseSelectorList selectors, + const ast::BlockStatement* body = nullptr) { return create(source, std::move(selectors), body ? body : Block()); } @@ -2127,8 +2185,8 @@ class ProgramBuilder { /// @param selectors list of selectors /// @param body the case body /// @returns the case statement pointer - ast::CaseStatement* Case(ast::CaseSelectorList selectors, - ast::BlockStatement* body = nullptr) { + const ast::CaseStatement* Case(ast::CaseSelectorList selectors, + const ast::BlockStatement* body = nullptr) { return create(std::move(selectors), body ? body : Block()); } @@ -2137,8 +2195,8 @@ class ProgramBuilder { /// @param selector a single case selector /// @param body the case body /// @returns the case statement pointer - ast::CaseStatement* Case(ast::IntLiteral* selector, - ast::BlockStatement* body = nullptr) { + const ast::CaseStatement* Case(const ast::IntLiteral* selector, + const ast::BlockStatement* body = nullptr) { return Case(ast::CaseSelectorList{selector}, body); } @@ -2146,15 +2204,17 @@ class ProgramBuilder { /// @param source the source information /// @param body the case body /// @returns the case statement pointer - ast::CaseStatement* DefaultCase(const Source& source, - ast::BlockStatement* body = nullptr) { + const ast::CaseStatement* DefaultCase( + const Source& source, + const ast::BlockStatement* body = nullptr) { return Case(source, ast::CaseSelectorList{}, body); } /// Convenience function that creates a 'default' ast::CaseStatement /// @param body the case body /// @returns the case statement pointer - ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) { + const ast::CaseStatement* DefaultCase( + const ast::BlockStatement* body = nullptr) { return Case(ast::CaseSelectorList{}, body); } @@ -2162,14 +2222,15 @@ class ProgramBuilder { /// @param source the source information /// @param builtin the builtin value /// @returns the builtin decoration pointer - ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) { + const ast::BuiltinDecoration* Builtin(const Source& source, + ast::Builtin builtin) { return create(source, builtin); } /// Creates an ast::BuiltinDecoration /// @param builtin the builtin value /// @returns the builtin decoration pointer - ast::BuiltinDecoration* Builtin(ast::Builtin builtin) { + const ast::BuiltinDecoration* Builtin(ast::Builtin builtin) { return create(source_, builtin); } @@ -2178,9 +2239,10 @@ class ProgramBuilder { /// @param type the interpolation type /// @param sampling the interpolation sampling /// @returns the interpolate decoration pointer - ast::InterpolateDecoration* Interpolate(const Source& source, - ast::InterpolationType type, - ast::InterpolationSampling sampling) { + const ast::InterpolateDecoration* Interpolate( + const Source& source, + ast::InterpolationType type, + ast::InterpolationSampling sampling) { return create(source, type, sampling); } @@ -2188,21 +2250,22 @@ class ProgramBuilder { /// @param type the interpolation type /// @param sampling the interpolation sampling /// @returns the interpolate decoration pointer - ast::InterpolateDecoration* Interpolate(ast::InterpolationType type, - ast::InterpolationSampling sampling) { + const ast::InterpolateDecoration* Interpolate( + ast::InterpolationType type, + ast::InterpolationSampling sampling) { return create(source_, type, sampling); } /// Creates an ast::InvariantDecoration /// @param source the source information /// @returns the invariant decoration pointer - ast::InvariantDecoration* Invariant(const Source& source) { + const ast::InvariantDecoration* Invariant(const Source& source) { return create(source); } /// Creates an ast::InvariantDecoration /// @returns the invariant decoration pointer - ast::InvariantDecoration* Invariant() { + const ast::InvariantDecoration* Invariant() { return create(source_); } @@ -2210,14 +2273,15 @@ class ProgramBuilder { /// @param source the source information /// @param location the location value /// @returns the location decoration pointer - ast::LocationDecoration* Location(const Source& source, uint32_t location) { + const ast::LocationDecoration* Location(const Source& source, + uint32_t location) { return create(source, location); } /// Creates an ast::LocationDecoration /// @param location the location value /// @returns the location decoration pointer - ast::LocationDecoration* Location(uint32_t location) { + const ast::LocationDecoration* Location(uint32_t location) { return create(source_, location); } @@ -2225,40 +2289,41 @@ class ProgramBuilder { /// @param source the source information /// @param id the id value /// @returns the override decoration pointer - ast::OverrideDecoration* Override(const Source& source, uint32_t id) { + const ast::OverrideDecoration* Override(const Source& source, uint32_t id) { return create(source, id); } /// Creates an ast::OverrideDecoration with a specific constant ID /// @param id the optional id value /// @returns the override decoration pointer - ast::OverrideDecoration* Override(uint32_t id) { + const ast::OverrideDecoration* Override(uint32_t id) { return Override(source_, id); } /// Creates an ast::OverrideDecoration without a constant ID /// @param source the source information /// @returns the override decoration pointer - ast::OverrideDecoration* Override(const Source& source) { + const ast::OverrideDecoration* Override(const Source& source) { return create(source); } /// Creates an ast::OverrideDecoration without a constant ID /// @returns the override decoration pointer - ast::OverrideDecoration* Override() { return Override(source_); } + const ast::OverrideDecoration* Override() { return Override(source_); } /// Creates an ast::StageDecoration /// @param source the source information /// @param stage the pipeline stage /// @returns the stage decoration pointer - ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) { + const ast::StageDecoration* Stage(const Source& source, + ast::PipelineStage stage) { return create(source, stage); } /// Creates an ast::StageDecoration /// @param stage the pipeline stage /// @returns the stage decoration pointer - ast::StageDecoration* Stage(ast::PipelineStage stage) { + const ast::StageDecoration* Stage(ast::PipelineStage stage) { return create(source_, stage); } @@ -2266,7 +2331,7 @@ class ProgramBuilder { /// @param x the x dimension expression /// @returns the workgroup decoration pointer template - ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x) { + const ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x) { return WorkgroupSize(std::forward(x), nullptr, nullptr); } @@ -2275,7 +2340,7 @@ class ProgramBuilder { /// @param y the y dimension expression /// @returns the workgroup decoration pointer template - ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y) { + const ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y) { return WorkgroupSize(std::forward(x), std::forward(y), nullptr); } @@ -2287,10 +2352,10 @@ class ProgramBuilder { /// @param z the z dimension expression /// @returns the workgroup decoration pointer template - ast::WorkgroupDecoration* WorkgroupSize(const Source& source, - EXPR_X&& x, - EXPR_Y&& y, - EXPR_Z&& z) { + const ast::WorkgroupDecoration* WorkgroupSize(const Source& source, + EXPR_X&& x, + EXPR_Y&& y, + EXPR_Z&& z) { return create( source, Expr(std::forward(x)), Expr(std::forward(y)), Expr(std::forward(z))); @@ -2302,7 +2367,9 @@ class ProgramBuilder { /// @param z the z dimension expression /// @returns the workgroup decoration pointer template - ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y, EXPR_Z&& z) { + const ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, + EXPR_Y&& y, + EXPR_Z&& z) { return create( source_, Expr(std::forward(x)), Expr(std::forward(y)), Expr(std::forward(z))); @@ -2362,7 +2429,7 @@ class ProgramBuilder { /// @param expr the AST expression /// @return the resolved semantic type for the expression, or nullptr if the /// expression has no resolved type. - sem::Type* TypeOf(const ast::Expression* expr) const; + const sem::Type* TypeOf(const ast::Expression* expr) const; /// Helper for returning the resolved semantic type of the variable `var`. /// @note As the Resolver is run when the Program is built, this will only be @@ -2370,7 +2437,7 @@ class ProgramBuilder { /// @param var the AST variable /// @return the resolved semantic type for the variable, or nullptr if the /// variable has no resolved type. - sem::Type* TypeOf(const ast::Variable* var) const; + const sem::Type* TypeOf(const ast::Variable* var) const; /// Helper for returning the resolved semantic type of the AST type `type`. /// @note As the Resolver is run when the Program is built, this will only be @@ -2394,37 +2461,37 @@ class ProgramBuilder { /// nodes. /// @param lit the ast::Literal to be wrapped by an ast::Statement /// @return the ast::Statement that wraps the ast::Statement - ast::Statement* WrapInStatement(ast::Literal* lit); + const ast::Statement* WrapInStatement(const ast::Literal* lit); /// Wraps the ast::Expression in a statement. This is used by tests that /// construct a partial AST and require the Resolver to reach these /// nodes. /// @param expr the ast::Expression to be wrapped by an ast::Statement /// @return the ast::Statement that wraps the ast::Expression - ast::Statement* WrapInStatement(ast::Expression* expr); + const ast::Statement* WrapInStatement(const ast::Expression* expr); /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by /// tests that construct a partial AST and require the Resolver to reach /// these nodes. /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement /// @return the ast::VariableDeclStatement that wraps the ast::Variable - ast::VariableDeclStatement* WrapInStatement(ast::Variable* v); + const ast::VariableDeclStatement* WrapInStatement(const ast::Variable* v); /// Returns the statement argument. Used as a passthrough-overload by /// WrapInFunction(). /// @param stmt the ast::Statement /// @return `stmt` - ast::Statement* WrapInStatement(ast::Statement* stmt); + const ast::Statement* WrapInStatement(const ast::Statement* stmt); /// Wraps the list of arguments in a simple function so that each is reachable /// by the Resolver. /// @param args a mix of ast::Expression, ast::Statement, ast::Variables. /// @returns the function template - ast::Function* WrapInFunction(ARGS&&... args) { + const ast::Function* WrapInFunction(ARGS&&... args) { ast::StatementList stmts{WrapInStatement(std::forward(args))...}; return WrapInFunction(std::move(stmts)); } /// @param stmts a list of ast::Statement that will be wrapped by a function, /// so that each statement is reachable by the Resolver. /// @returns the function - ast::Function* WrapInFunction(ast::StatementList stmts); + const ast::Function* WrapInFunction(ast::StatementList stmts); /// The builder types TypesBuilder const ty{this}; @@ -2460,31 +2527,31 @@ class ProgramBuilder { // Various template specializations for ProgramBuilder::TypesBuilder::CToAST. template <> struct ProgramBuilder::TypesBuilder::CToAST { - static ast::Type* get(const ProgramBuilder::TypesBuilder* t) { + static const ast::Type* get(const ProgramBuilder::TypesBuilder* t) { return t->i32(); } }; template <> struct ProgramBuilder::TypesBuilder::CToAST { - static ast::Type* get(const ProgramBuilder::TypesBuilder* t) { + static const ast::Type* get(const ProgramBuilder::TypesBuilder* t) { return t->u32(); } }; template <> struct ProgramBuilder::TypesBuilder::CToAST { - static ast::Type* get(const ProgramBuilder::TypesBuilder* t) { + static const ast::Type* get(const ProgramBuilder::TypesBuilder* t) { return t->f32(); } }; template <> struct ProgramBuilder::TypesBuilder::CToAST { - static ast::Type* get(const ProgramBuilder::TypesBuilder* t) { + static const ast::Type* get(const ProgramBuilder::TypesBuilder* t) { return t->bool_(); } }; template <> struct ProgramBuilder::TypesBuilder::CToAST { - static ast::Type* get(const ProgramBuilder::TypesBuilder* t) { + static const ast::Type* get(const ProgramBuilder::TypesBuilder* t) { return t->void_(); } }; diff --git a/src/reader/spirv/function.cc b/src/reader/spirv/function.cc index b9ef0c93c8..b02f4670d1 100644 --- a/src/reader/spirv/function.cc +++ b/src/reader/spirv/function.cc @@ -641,11 +641,12 @@ struct SwitchStatementBuilder : public Castable { /// Constructor /// @param cond the switch statement condition - explicit SwitchStatementBuilder(ast::Expression* cond) : condition(cond) {} + explicit SwitchStatementBuilder(const ast::Expression* cond) + : condition(cond) {} /// @param builder the program builder /// @returns the built ast::SwitchStatement - ast::SwitchStatement* Build(ProgramBuilder* builder) const override { + const ast::SwitchStatement* Build(ProgramBuilder* builder) const override { // We've listed cases in reverse order in the switch statement. // Reorder them to match the presentation order in WGSL. auto reversed_cases = cases; @@ -656,7 +657,7 @@ struct SwitchStatementBuilder } /// Switch statement condition - ast::Expression* const condition; + const ast::Expression* const condition; /// Switch statement cases ast::CaseStatementList cases; }; @@ -667,18 +668,18 @@ struct IfStatementBuilder : public Castable { /// Constructor /// @param c the if-statement condition - explicit IfStatementBuilder(ast::Expression* c) : cond(c) {} + explicit IfStatementBuilder(const ast::Expression* c) : cond(c) {} /// @param builder the program builder /// @returns the built ast::IfStatement - ast::IfStatement* Build(ProgramBuilder* builder) const override { + const ast::IfStatement* Build(ProgramBuilder* builder) const override { return builder->create(Source{}, cond, body, else_stmts); } /// If-statement condition - ast::Expression* const cond; + const ast::Expression* const cond; /// If-statement block body - ast::BlockStatement* body = nullptr; + const ast::BlockStatement* body = nullptr; /// Optional if-statement else statements ast::ElseStatementList else_stmts; }; @@ -694,9 +695,14 @@ struct LoopStatementBuilder } /// Loop-statement block body - ast::BlockStatement* body = nullptr; + const ast::BlockStatement* body = nullptr; /// Loop-statement continuing body - ast::BlockStatement* continuing = nullptr; + /// @note the mutable keyword here is required as all non-StatementBuilders + /// `ast::Node`s are immutable and are referenced with `const` pointers. + /// StatementBuilders however exist to provide mutable state while the + /// FunctionEmitter is building the function. All StatementBuilders are + /// replaced with immutable AST nodes when Finalize() is called. + mutable const ast::BlockStatement* continuing = nullptr; }; /// @param decos a list of parsed decorations @@ -798,7 +804,7 @@ void FunctionEmitter::StatementBlock::Finalize(ProgramBuilder* pb) { finalized_ = true; } -void FunctionEmitter::StatementBlock::Add(ast::Statement* statement) { +void FunctionEmitter::StatementBlock::Add(const ast::Statement* statement) { TINT_ASSERT(Reader, !finalized_ /* Add() must not be called after Finalize() */); statements_.emplace_back(statement); @@ -849,7 +855,8 @@ const ast::StatementList FunctionEmitter::ast_body() { return entry.GetStatements(); } -ast::Statement* FunctionEmitter::AddStatement(ast::Statement* statement) { +const ast::Statement* FunctionEmitter::AddStatement( + const ast::Statement* statement) { TINT_ASSERT(Reader, !statements_stack_.empty()); if (statement != nullptr) { statements_stack_.back().Add(statement); @@ -857,7 +864,7 @@ ast::Statement* FunctionEmitter::AddStatement(ast::Statement* statement) { return statement; } -ast::Statement* FunctionEmitter::LastStatement() { +const ast::Statement* FunctionEmitter::LastStatement() { TINT_ASSERT(Reader, !statements_stack_.empty()); auto& statement_list = statements_stack_.back().GetStatements(); TINT_ASSERT(Reader, !statement_list.empty()); @@ -913,7 +920,7 @@ bool FunctionEmitter::Emit() { return success(); } -ast::BlockStatement* FunctionEmitter::MakeFunctionBody() { +const ast::BlockStatement* FunctionEmitter::MakeFunctionBody() { TINT_ASSERT(Reader, statements_stack_.size() == 1); if (!EmitBody()) { @@ -1020,8 +1027,8 @@ bool FunctionEmitter::EmitPipelineInput(std::string var_name, // Add a body statement to copy the parameter to the corresponding private // variable. - ast::Expression* param_value = builder_.Expr(param_name); - ast::Expression* store_dest = builder_.Expr(var_name); + const ast::Expression* param_value = builder_.Expr(param_name); + const ast::Expression* store_dest = builder_.Expr(var_name); // Index into the LHS as needed. auto* current_type = var_type->UnwrapAlias()->UnwrapRef()->UnwrapAlias(); @@ -1066,7 +1073,7 @@ void FunctionEmitter::IncrementLocation(ast::DecorationList* decos) { } } -ast::Decoration* FunctionEmitter::GetLocation( +const ast::Decoration* FunctionEmitter::GetLocation( const ast::DecorationList& decos) { for (auto* const& deco : decos) { if (deco->Is()) { @@ -1159,7 +1166,7 @@ bool FunctionEmitter::EmitPipelineOutput(std::string var_name, // Create an expression to evaluate the part of the variable indexed by // the index_prefix. - ast::Expression* load_source = builder_.Expr(var_name); + const ast::Expression* load_source = builder_.Expr(var_name); // Index into the variable as needed to pick out the flattened member. auto* current_type = var_type->UnwrapAlias()->UnwrapRef()->UnwrapAlias(); @@ -1201,7 +1208,7 @@ bool FunctionEmitter::EmitEntryPointAsWrapper() { FunctionDeclaration decl; decl.source = source; decl.name = ep_info_->name; - ast::Type* return_type = nullptr; // Populated below. + const ast::Type* return_type = nullptr; // Populated below. // Pipeline inputs become parameters to the wrapper function, and // their values are saved into the corresponding private variables that @@ -1271,7 +1278,7 @@ bool FunctionEmitter::EmitEntryPointAsWrapper() { builder_.Symbols().Register(return_struct_name); // Define the structure. - std::vector return_members; + std::vector return_members; ast::ExpressionList return_exprs; const auto& builtin_position_info = parser_impl_.GetBuiltInPositionInfo(); @@ -1355,10 +1362,10 @@ bool FunctionEmitter::EmitEntryPointAsWrapper() { if (ep_info_->stage == ast::PipelineStage::kCompute) { auto& size = ep_info_->workgroup_size; if (size.x != 0 && size.y != 0 && size.z != 0) { - ast::Expression* x = builder_.Expr(static_cast(size.x)); - ast::Expression* y = + const ast::Expression* x = builder_.Expr(static_cast(size.x)); + const ast::Expression* y = size.y ? builder_.Expr(static_cast(size.y)) : nullptr; - ast::Expression* z = + const ast::Expression* z = size.z ? builder_.Expr(static_cast(size.z)) : nullptr; fn_decos.emplace_back( create(Source{}, x, y, z)); @@ -2487,7 +2494,7 @@ bool FunctionEmitter::EmitFunctionVariables() { if (failed()) { return false; } - ast::Expression* constructor = nullptr; + const ast::Expression* constructor = nullptr; if (inst.NumInOperands() > 1) { // SPIR-V initializers are always constants. // (OpenCL also allows the ID of an OpVariable, but we don't handle that @@ -3228,7 +3235,7 @@ bool FunctionEmitter::EmitNormalTerminator(const BlockInfo& block_info) { return success(); } -ast::Statement* FunctionEmitter::MakeBranchDetailed( +const ast::Statement* FunctionEmitter::MakeBranchDetailed( const BlockInfo& src_info, const BlockInfo& dest_info, bool forced, @@ -3306,9 +3313,10 @@ ast::Statement* FunctionEmitter::MakeBranchDetailed( return nullptr; } -ast::Statement* FunctionEmitter::MakeSimpleIf(ast::Expression* condition, - ast::Statement* then_stmt, - ast::Statement* else_stmt) const { +const ast::Statement* FunctionEmitter::MakeSimpleIf( + const ast::Expression* condition, + const ast::Statement* then_stmt, + const ast::Statement* else_stmt) const { if ((then_stmt == nullptr) && (else_stmt == nullptr)) { return nullptr; } @@ -3331,7 +3339,7 @@ ast::Statement* FunctionEmitter::MakeSimpleIf(ast::Expression* condition, bool FunctionEmitter::EmitConditionalCaseFallThrough( const BlockInfo& src_info, - ast::Expression* cond, + const ast::Expression* cond, EdgeKind other_edge_kind, const BlockInfo& other_dest, bool fall_through_is_true_branch) { @@ -3650,7 +3658,7 @@ bool FunctionEmitter::EmitStatement(const spvtools::opt::Instruction& inst) { return true; case SkipReason::kSampleMaskInBuiltinPointer: { auto name = namer_.Name(sample_mask_in_id); - ast::Expression* id_expr = create( + const ast::Expression* id_expr = create( Source{}, builder_.Symbols().Register(name)); // SampleMask is an array in Vulkan SPIR-V. Always access the first // element. @@ -4310,7 +4318,7 @@ TypedExpression FunctionEmitter::MakeAccessChain( constants[index] ? constants[index]->AsIntConstant() : nullptr; const int64_t index_const_val = index_const ? index_const->GetSignExtendedValue() : 0; - ast::Expression* next_expr = nullptr; + const ast::Expression* next_expr = nullptr; const auto* pointee_type_inst = def_use_mgr_->GetDef(pointee_type_id); if (!pointee_type_inst) { @@ -4473,7 +4481,7 @@ TypedExpression FunctionEmitter::MakeCompositeValueDecomposition( Fail() << "internal error: unhandled " << inst.PrettyPrint(); return {}; } - ast::Expression* next_expr = nullptr; + const ast::Expression* next_expr = nullptr; switch (current_type_inst->opcode()) { case SpvOpTypeVector: { // Try generating a MemberAccessor expression. That result in something @@ -4559,12 +4567,12 @@ TypedExpression FunctionEmitter::MakeCompositeValueDecomposition( return current_expr; } -ast::Expression* FunctionEmitter::MakeTrue(const Source& source) const { +const ast::Expression* FunctionEmitter::MakeTrue(const Source& source) const { return create( source, create(source, true)); } -ast::Expression* FunctionEmitter::MakeFalse(const Source& source) const { +const ast::Expression* FunctionEmitter::MakeFalse(const Source& source) const { return create( source, create(source, false)); } @@ -5179,7 +5187,7 @@ const Texture* FunctionEmitter::GetImageType( return result; } -ast::Expression* FunctionEmitter::GetImageExpression( +const ast::Expression* FunctionEmitter::GetImageExpression( const spvtools::opt::Instruction& inst) { auto* image = GetImage(inst); if (!image) { @@ -5190,7 +5198,7 @@ ast::Expression* FunctionEmitter::GetImageExpression( builder_.Symbols().Register(name)); } -ast::Expression* FunctionEmitter::GetSamplerExpression( +const ast::Expression* FunctionEmitter::GetSamplerExpression( const spvtools::opt::Instruction& inst) { // The sampled image operand is always the first operand. const auto image_or_sampled_image_operand_id = inst.GetSingleWordInOperand(0); @@ -5415,7 +5423,7 @@ bool FunctionEmitter::EmitImageAccess(const spvtools::opt::Instruction& inst) { if (inst.type_id() != 0) { // It returns a value. - ast::Expression* value = call_expr; + const ast::Expression* value = call_expr; // The result type, derived from the SPIR-V instruction. auto* result_type = parser_impl_.ConvertType(inst.type_id()); @@ -5505,7 +5513,7 @@ bool FunctionEmitter::EmitImageQuery(const spvtools::opt::Instruction& inst) { if (opcode == SpvOpImageQuerySizeLod) { dims_args.push_back(ToI32(MakeOperand(inst, 1)).expr); } - ast::Expression* dims_call = + const ast::Expression* dims_call = create(Source{}, dims_ident, dims_args); auto dims = texture_type->dims; if ((dims == ast::TextureDimension::kCube) || @@ -5539,7 +5547,7 @@ bool FunctionEmitter::EmitImageQuery(const spvtools::opt::Instruction& inst) { : "textureNumSamples"; auto* levels_ident = create( Source{}, builder_.Symbols().Register(name)); - ast::Expression* ast_expr = create( + const ast::Expression* ast_expr = create( Source{}, levels_ident, ast::ExpressionList{GetImageExpression(inst)}); auto* result_type = parser_impl_.ConvertType(inst.type_id()); @@ -5646,7 +5654,7 @@ ast::ExpressionList FunctionEmitter::MakeCoordinateOperandsForImageAccess( // Use a lambda to make it easy to only generate the expressions when we // will actually use them. auto prefix_swizzle_expr = [this, num_axes, component_type, is_proj, - raw_coords]() -> ast::Expression* { + raw_coords]() -> const ast::Expression* { auto* swizzle_type = (num_axes == 1) ? component_type : ty_.Vector(component_type, num_axes); auto* swizzle = create( @@ -5668,7 +5676,7 @@ ast::ExpressionList FunctionEmitter::MakeCoordinateOperandsForImageAccess( result.push_back(prefix_swizzle_expr()); // Now get the array index. - ast::Expression* array_index = create( + const ast::Expression* array_index = create( Source{}, raw_coords.expr, Swizzle(num_axes)); // Convert it to a signed integer type, if needed result.push_back(ToI32({component_type, array_index}).expr); @@ -5686,7 +5694,7 @@ ast::ExpressionList FunctionEmitter::MakeCoordinateOperandsForImageAccess( return result; } -ast::Expression* FunctionEmitter::ConvertTexelForStorage( +const ast::Expression* FunctionEmitter::ConvertTexelForStorage( const spvtools::opt::Instruction& inst, TypedExpression texel, const Texture* texture_type) { diff --git a/src/reader/spirv/function.h b/src/reader/spirv/function.h index edf5d483f5..38b76a9a7a 100644 --- a/src/reader/spirv/function.h +++ b/src/reader/spirv/function.h @@ -352,7 +352,7 @@ class StatementBuilder : public Castable { /// @param builder the program builder /// @returns the build AST node - virtual ast::Statement* Build(ProgramBuilder* builder) const = 0; + virtual const ast::Statement* Build(ProgramBuilder* builder) const = 0; private: Node* Clone(CloneContext*) const override; @@ -473,13 +473,13 @@ class FunctionEmitter { /// Returns the Location dcoration, if it exists. /// @param decos the list of decorations to search /// @returns the Location decoration, or nullptr if it doesn't exist - ast::Decoration* GetLocation(const ast::DecorationList& decos); + const ast::Decoration* GetLocation(const ast::DecorationList& decos); /// Create an ast::BlockStatement representing the body of the function. /// This creates the statement stack, which is non-empty for the lifetime /// of the function. /// @returns the body of the function, or null on error - ast::BlockStatement* MakeFunctionBody(); + const ast::BlockStatement* MakeFunctionBody(); /// Emits the function body, populating the bottom entry of the statements /// stack. @@ -660,8 +660,8 @@ class FunctionEmitter { /// @param src_info the source block /// @param dest_info the destination block /// @returns the new statement, or a null statement - ast::Statement* MakeBranch(const BlockInfo& src_info, - const BlockInfo& dest_info) const { + const ast::Statement* MakeBranch(const BlockInfo& src_info, + const BlockInfo& dest_info) const { return MakeBranchDetailed(src_info, dest_info, false, nullptr); } @@ -671,8 +671,8 @@ class FunctionEmitter { /// @param src_info the source block /// @param dest_info the destination block /// @returns the new statement, or a null statement - ast::Statement* MakeForcedBranch(const BlockInfo& src_info, - const BlockInfo& dest_info) const { + const ast::Statement* MakeForcedBranch(const BlockInfo& src_info, + const BlockInfo& dest_info) const { return MakeBranchDetailed(src_info, dest_info, true, nullptr); } @@ -690,10 +690,11 @@ class FunctionEmitter { /// @param forced if true, always emit the branch (if it exists in WGSL) /// @param flow_guard_name_ptr return parameter for control flow guard name /// @returns the new statement, or a null statement - ast::Statement* MakeBranchDetailed(const BlockInfo& src_info, - const BlockInfo& dest_info, - bool forced, - std::string* flow_guard_name_ptr) const; + const ast::Statement* MakeBranchDetailed( + const BlockInfo& src_info, + const BlockInfo& dest_info, + bool forced, + std::string* flow_guard_name_ptr) const; /// Returns a new if statement with the given statements as the then-clause /// and the else-clause. Either or both clauses might be nullptr. If both @@ -702,9 +703,9 @@ class FunctionEmitter { /// @param then_stmt the statement for the then clause of the if, or nullptr /// @param else_stmt the statement for the else clause of the if, or nullptr /// @returns the new statement, or nullptr - ast::Statement* MakeSimpleIf(ast::Expression* condition, - ast::Statement* then_stmt, - ast::Statement* else_stmt) const; + const ast::Statement* MakeSimpleIf(const ast::Expression* condition, + const ast::Statement* then_stmt, + const ast::Statement* else_stmt) const; /// Emits the statements for an normal-terminator OpBranchConditional /// where one branch is a case fall through (the true branch if and only @@ -719,7 +720,7 @@ class FunctionEmitter { /// branch /// @returns the false if emission fails bool EmitConditionalCaseFallThrough(const BlockInfo& src_info, - ast::Expression* cond, + const ast::Expression* cond, EdgeKind other_edge_kind, const BlockInfo& other_dest, bool fall_through_is_true_branch); @@ -1031,13 +1032,15 @@ class FunctionEmitter { /// given instruction. /// @param inst the SPIR-V instruction /// @returns an identifier expression, or null on error - ast::Expression* GetImageExpression(const spvtools::opt::Instruction& inst); + const ast::Expression* GetImageExpression( + const spvtools::opt::Instruction& inst); /// Get the expression for the sampler operand from the first operand to the /// given instruction. /// @param inst the SPIR-V instruction /// @returns an identifier expression, or null on error - ast::Expression* GetSamplerExpression(const spvtools::opt::Instruction& inst); + const ast::Expression* GetSamplerExpression( + const spvtools::opt::Instruction& inst); /// Emits a texture builtin function call for a SPIR-V instruction that /// accesses an image or sampled image. @@ -1059,7 +1062,7 @@ class FunctionEmitter { /// @param texel the texel /// @param texture_type the type of the storage texture /// @returns the texel, after necessary conversion. - ast::Expression* ConvertTexelForStorage( + const ast::Expression* ConvertTexelForStorage( const spvtools::opt::Instruction& inst, TypedExpression texel, const Texture* texture_type); @@ -1087,7 +1090,7 @@ class FunctionEmitter { /// Does nothing if the statement is null. /// @param statement the new statement /// @returns a pointer to the statement. - ast::Statement* AddStatement(ast::Statement* statement); + const ast::Statement* AddStatement(const ast::Statement* statement); /// AddStatementBuilder() constructs and adds the StatementBuilder of type /// `T` to the top of the statement stack. @@ -1106,7 +1109,7 @@ class FunctionEmitter { Source GetSourceForInst(const spvtools::opt::Instruction& inst) const; /// @returns the last statetment in the top of the statement stack. - ast::Statement* LastStatement(); + const ast::Statement* LastStatement(); using CompletionAction = std::function; @@ -1131,7 +1134,7 @@ class FunctionEmitter { /// Add() adds `statement` to the block. /// Add() must not be called after calling Finalize(). - void Add(ast::Statement* statement); + void Add(const ast::Statement* statement); /// AddStatementBuilder() constructs and adds the StatementBuilder of type /// `T` to the block. @@ -1168,7 +1171,7 @@ class FunctionEmitter { /// The ID of the block at which the completion action should be triggered /// and this statement block discarded. This is often the `end_id` of /// `construct` itself. - uint32_t const end_id_; + const uint32_t end_id_; /// The completion action finishes processing this statement block. FunctionEmitter::CompletionAction const completion_action_; /// The list of statements being built, if this construct is not a switch. @@ -1200,10 +1203,10 @@ class FunctionEmitter { void PushTrueGuard(uint32_t end_id); /// @returns a boolean true expression. - ast::Expression* MakeTrue(const Source&) const; + const ast::Expression* MakeTrue(const Source&) const; /// @returns a boolean false expression. - ast::Expression* MakeFalse(const Source&) const; + const ast::Expression* MakeFalse(const Source&) const; /// @param expr the expression to take the address of /// @returns a TypedExpression that is the address-of `expr` (`&expr`) diff --git a/src/reader/spirv/parser_impl.cc b/src/reader/spirv/parser_impl.cc index 89617566dd..b5ff8fca6f 100644 --- a/src/reader/spirv/parser_impl.cc +++ b/src/reader/spirv/parser_impl.cc @@ -262,7 +262,8 @@ TypedExpression::TypedExpression(const TypedExpression&) = default; TypedExpression& TypedExpression::operator=(const TypedExpression&) = default; -TypedExpression::TypedExpression(const Type* type_in, ast::Expression* expr_in) +TypedExpression::TypedExpression(const Type* type_in, + const ast::Expression* expr_in) : type(type_in), expr(expr_in) {} ParserImpl::ParserImpl(const std::vector& spv_binary) @@ -1208,7 +1209,7 @@ const Type* ParserImpl::ConvertType( return result; } -void ParserImpl::AddTypeDecl(Symbol name, ast::TypeDecl* decl) { +void ParserImpl::AddTypeDecl(Symbol name, const ast::TypeDecl* decl) { auto iter = declared_types_.insert(name); if (iter.second) { builder_.AST().AddTypeDecl(decl); @@ -1521,7 +1522,7 @@ bool ParserImpl::EmitModuleScopeVariables() { auto* ast_store_type = ast_type->As()->type; auto ast_storage_class = ast_type->As()->storage_class; - ast::Expression* ast_constructor = nullptr; + const ast::Expression* ast_constructor = nullptr; if (var.NumInOperands() > 1) { // SPIR-V initializers are always constants. // (OpenCL also allows the ID of an OpVariable, but we don't handle that @@ -1543,7 +1544,7 @@ bool ParserImpl::EmitModuleScopeVariables() { // Make sure the variable has a name. namer_.SuggestSanitizedName(builtin_position_.per_vertex_var_id, "gl_Position"); - ast::Expression* ast_constructor = nullptr; + const ast::Expression* ast_constructor = nullptr; if (builtin_position_.per_vertex_var_init_id) { // The initializer is complex. const auto* init = @@ -1603,7 +1604,7 @@ ast::Variable* ParserImpl::MakeVariable(uint32_t id, ast::StorageClass sc, const Type* storage_type, bool is_const, - ast::Expression* constructor, + const ast::Expression* constructor, ast::DecorationList decorations) { if (storage_type == nullptr) { Fail() << "internal error: can't make ast::Variable for null type"; @@ -1755,8 +1756,9 @@ DecorationList ParserImpl::GetMemberPipelineDecorations( return result; } -ast::Decoration* ParserImpl::SetLocation(ast::DecorationList* decos, - ast::Decoration* replacement) { +const ast::Decoration* ParserImpl::SetLocation( + ast::DecorationList* decos, + const ast::Decoration* replacement) { if (!replacement) { return nullptr; } @@ -1765,7 +1767,7 @@ ast::Decoration* ParserImpl::SetLocation(ast::DecorationList* decos, // Replace this location decoration with the replacement. // The old one doesn't leak because it's kept in the builder's AST node // list. - ast::Decoration* result = nullptr; + const ast::Decoration* result = nullptr; result = deco; deco = replacement; return result; // Assume there is only one such decoration. @@ -2004,7 +2006,7 @@ TypedExpression ParserImpl::MakeConstantExpressionForScalarSpirvConstant( return {}; } -ast::Expression* ParserImpl::MakeNullValue(const Type* type) { +const ast::Expression* ParserImpl::MakeNullValue(const Type* type) { // TODO(dneto): Use the no-operands constructor syntax when it becomes // available in Tint. // https://github.com/gpuweb/gpuweb/issues/685 diff --git a/src/reader/spirv/parser_impl.h b/src/reader/spirv/parser_impl.h index d07bb7779f..9895439dd8 100644 --- a/src/reader/spirv/parser_impl.h +++ b/src/reader/spirv/parser_impl.h @@ -73,7 +73,7 @@ struct TypedExpression { /// Constructor /// @param type_in the type of the expression /// @param expr_in the expression - TypedExpression(const Type* type_in, ast::Expression* expr_in); + TypedExpression(const Type* type_in, const ast::Expression* expr_in); /// Assignment operator /// @returns this TypedExpression @@ -83,9 +83,9 @@ struct TypedExpression { operator bool() const { return type && expr; } /// The type - Type const* type = nullptr; + const Type* type = nullptr; /// The expression - ast::Expression* expr = nullptr; + const ast::Expression* expr = nullptr; }; /// Info about the WorkgroupSize builtin. @@ -208,7 +208,7 @@ class ParserImpl : Reader { /// Adds `decl` as a declared type if it hasn't been added yet. /// @param name the type's unique name /// @param decl the type declaration to add - void AddTypeDecl(Symbol name, ast::TypeDecl* decl); + void AddTypeDecl(Symbol name, const ast::TypeDecl* decl); /// @returns the fail stream object FailStream& fail_stream() { return fail_stream_; } @@ -269,8 +269,8 @@ class ParserImpl : Reader { /// @param replacement the location decoration to place into the list /// @returns the location decoration that was replaced, if one was replaced, /// or null otherwise. - ast::Decoration* SetLocation(ast::DecorationList* decos, - ast::Decoration* replacement); + const ast::Decoration* SetLocation(ast::DecorationList* decos, + const ast::Decoration* replacement); /// Converts a SPIR-V struct member decoration into a number of AST /// decorations. If the decoration is recognized but deliberately dropped, @@ -429,7 +429,7 @@ class ParserImpl : Reader { ast::StorageClass sc, const Type* storage_type, bool is_const, - ast::Expression* constructor, + const ast::Expression* constructor, ast::DecorationList decorations); /// Returns true if a constant expression can be generated. @@ -456,7 +456,7 @@ class ParserImpl : Reader { /// Creates an AST expression node for the null value for the given type. /// @param type the AST type /// @returns a new expression - ast::Expression* MakeNullValue(const Type* type); + const ast::Expression* MakeNullValue(const Type* type); /// Make a typed expression for the null value for the given type. /// @param type the AST type diff --git a/src/reader/spirv/parser_impl_test_helper.cc b/src/reader/spirv/parser_impl_test_helper.cc index 79ea5d012d..7a1dd99fb8 100644 --- a/src/reader/spirv/parser_impl_test_helper.cc +++ b/src/reader/spirv/parser_impl_test_helper.cc @@ -47,7 +47,7 @@ std::string ToString(const Program& program) { std::string ToString(const Program& program, const ast::StatementList& stmts) { writer::wgsl::GeneratorImpl writer(&program); for (const auto* stmt : stmts) { - if (!writer.EmitStatement(const_cast(stmt))) { + if (!writer.EmitStatement(stmt)) { return "WGSL writer error: " + writer.error(); } } @@ -58,17 +58,17 @@ std::string ToString(const Program& program, const ast::Node* node) { writer::wgsl::GeneratorImpl writer(&program); if (auto* expr = node->As()) { std::stringstream out; - if (!writer.EmitExpression(out, const_cast(expr))) { + if (!writer.EmitExpression(out, expr)) { return "WGSL writer error: " + writer.error(); } return out.str(); } else if (auto* stmt = node->As()) { - if (!writer.EmitStatement(const_cast(stmt))) { + if (!writer.EmitStatement(stmt)) { return "WGSL writer error: " + writer.error(); } } else if (auto* ty = node->As()) { std::stringstream out; - if (!writer.EmitType(out, const_cast(ty))) { + if (!writer.EmitType(out, ty)) { return "WGSL writer error: " + writer.error(); } return out.str(); diff --git a/src/reader/spirv/parser_type.cc b/src/reader/spirv/parser_type.cc index a8e03c6683..90f250bb87 100644 --- a/src/reader/spirv/parser_type.cc +++ b/src/reader/spirv/parser_type.cc @@ -131,23 +131,23 @@ static bool operator==(const StorageTexture& a, const StorageTexture& b) { return a.dims == b.dims && a.format == b.format; } -ast::Type* Void::Build(ProgramBuilder& b) const { +const ast::Type* Void::Build(ProgramBuilder& b) const { return b.ty.void_(); } -ast::Type* Bool::Build(ProgramBuilder& b) const { +const ast::Type* Bool::Build(ProgramBuilder& b) const { return b.ty.bool_(); } -ast::Type* U32::Build(ProgramBuilder& b) const { +const ast::Type* U32::Build(ProgramBuilder& b) const { return b.ty.u32(); } -ast::Type* F32::Build(ProgramBuilder& b) const { +const ast::Type* F32::Build(ProgramBuilder& b) const { return b.ty.f32(); } -ast::Type* I32::Build(ProgramBuilder& b) const { +const ast::Type* I32::Build(ProgramBuilder& b) const { return b.ty.i32(); } @@ -155,7 +155,7 @@ Pointer::Pointer(const Type* t, ast::StorageClass s) : type(t), storage_class(s) {} Pointer::Pointer(const Pointer&) = default; -ast::Type* Pointer::Build(ProgramBuilder& b) const { +const ast::Type* Pointer::Build(ProgramBuilder& b) const { return b.ty.pointer(type->Build(b), storage_class); } @@ -163,14 +163,14 @@ Reference::Reference(const Type* t, ast::StorageClass s) : type(t), storage_class(s) {} Reference::Reference(const Reference&) = default; -ast::Type* Reference::Build(ProgramBuilder& b) const { +const ast::Type* Reference::Build(ProgramBuilder& b) const { return type->Build(b); } Vector::Vector(const Type* t, uint32_t s) : type(t), size(s) {} Vector::Vector(const Vector&) = default; -ast::Type* Vector::Build(ProgramBuilder& b) const { +const ast::Type* Vector::Build(ProgramBuilder& b) const { return b.ty.vec(type->Build(b), size); } @@ -178,7 +178,7 @@ Matrix::Matrix(const Type* t, uint32_t c, uint32_t r) : type(t), columns(c), rows(r) {} Matrix::Matrix(const Matrix&) = default; -ast::Type* Matrix::Build(ProgramBuilder& b) const { +const ast::Type* Matrix::Build(ProgramBuilder& b) const { return b.ty.mat(type->Build(b), columns, rows); } @@ -186,7 +186,7 @@ Array::Array(const Type* t, uint32_t sz, uint32_t st) : type(t), size(sz), stride(st) {} Array::Array(const Array&) = default; -ast::Type* Array::Build(ProgramBuilder& b) const { +const ast::Type* Array::Build(ProgramBuilder& b) const { if (size > 0) { return b.ty.array(type->Build(b), size, stride); } else { @@ -197,7 +197,7 @@ ast::Type* Array::Build(ProgramBuilder& b) const { Sampler::Sampler(ast::SamplerKind k) : kind(k) {} Sampler::Sampler(const Sampler&) = default; -ast::Type* Sampler::Build(ProgramBuilder& b) const { +const ast::Type* Sampler::Build(ProgramBuilder& b) const { return b.ty.sampler(kind); } @@ -207,7 +207,7 @@ Texture::Texture(const Texture&) = default; DepthTexture::DepthTexture(ast::TextureDimension d) : Base(d) {} DepthTexture::DepthTexture(const DepthTexture&) = default; -ast::Type* DepthTexture::Build(ProgramBuilder& b) const { +const ast::Type* DepthTexture::Build(ProgramBuilder& b) const { return b.ty.depth_texture(dims); } @@ -216,7 +216,7 @@ DepthMultisampledTexture::DepthMultisampledTexture(ast::TextureDimension d) DepthMultisampledTexture::DepthMultisampledTexture( const DepthMultisampledTexture&) = default; -ast::Type* DepthMultisampledTexture::Build(ProgramBuilder& b) const { +const ast::Type* DepthMultisampledTexture::Build(ProgramBuilder& b) const { return b.ty.depth_multisampled_texture(dims); } @@ -224,7 +224,7 @@ MultisampledTexture::MultisampledTexture(ast::TextureDimension d, const Type* t) : Base(d), type(t) {} MultisampledTexture::MultisampledTexture(const MultisampledTexture&) = default; -ast::Type* MultisampledTexture::Build(ProgramBuilder& b) const { +const ast::Type* MultisampledTexture::Build(ProgramBuilder& b) const { return b.ty.multisampled_texture(dims, type->Build(b)); } @@ -232,7 +232,7 @@ SampledTexture::SampledTexture(ast::TextureDimension d, const Type* t) : Base(d), type(t) {} SampledTexture::SampledTexture(const SampledTexture&) = default; -ast::Type* SampledTexture::Build(ProgramBuilder& b) const { +const ast::Type* SampledTexture::Build(ProgramBuilder& b) const { return b.ty.sampled_texture(dims, type->Build(b)); } @@ -242,7 +242,7 @@ StorageTexture::StorageTexture(ast::TextureDimension d, : Base(d), format(f), access(a) {} StorageTexture::StorageTexture(const StorageTexture&) = default; -ast::Type* StorageTexture::Build(ProgramBuilder& b) const { +const ast::Type* StorageTexture::Build(ProgramBuilder& b) const { return b.ty.storage_texture(dims, format, access); } @@ -253,7 +253,7 @@ Named::~Named() = default; Alias::Alias(Symbol n, const Type* ty) : Base(n), type(ty) {} Alias::Alias(const Alias&) = default; -ast::Type* Alias::Build(ProgramBuilder& b) const { +const ast::Type* Alias::Build(ProgramBuilder& b) const { return b.ty.type_name(name); } @@ -261,7 +261,7 @@ Struct::Struct(Symbol n, TypeList m) : Base(n), members(std::move(m)) {} Struct::Struct(const Struct&) = default; Struct::~Struct() = default; -ast::Type* Struct::Build(ProgramBuilder& b) const { +const ast::Type* Struct::Build(ProgramBuilder& b) const { return b.ty.type_name(name); } diff --git a/src/reader/spirv/parser_type.h b/src/reader/spirv/parser_type.h index fad71610a2..4466a63d1e 100644 --- a/src/reader/spirv/parser_type.h +++ b/src/reader/spirv/parser_type.h @@ -44,7 +44,7 @@ class Type : public Castable { public: /// @param b the ProgramBuilder used to construct the AST types /// @returns the constructed ast::Type node for the given type - virtual ast::Type* Build(ProgramBuilder& b) const = 0; + virtual const ast::Type* Build(ProgramBuilder& b) const = 0; /// @returns the inner most store type if this is a pointer, `this` otherwise const Type* UnwrapPtr() const; @@ -95,7 +95,7 @@ using TypeList = std::vector; struct Void : public Castable { /// @param b the ProgramBuilder used to construct the AST types /// @returns the constructed ast::Type node for the given type - ast::Type* Build(ProgramBuilder& b) const override; + const ast::Type* Build(ProgramBuilder& b) const override; #ifndef NDEBUG /// @returns a string representation of the type, for debug purposes only @@ -107,7 +107,7 @@ struct Void : public Castable { struct Bool : public Castable { /// @param b the ProgramBuilder used to construct the AST types /// @returns the constructed ast::Type node for the given type - ast::Type* Build(ProgramBuilder& b) const override; + const ast::Type* Build(ProgramBuilder& b) const override; #ifndef NDEBUG /// @returns a string representation of the type, for debug purposes only @@ -119,7 +119,7 @@ struct Bool : public Castable { struct U32 : public Castable { /// @param b the ProgramBuilder used to construct the AST types /// @returns the constructed ast::Type node for the given type - ast::Type* Build(ProgramBuilder& b) const override; + const ast::Type* Build(ProgramBuilder& b) const override; #ifndef NDEBUG /// @returns a string representation of the type, for debug purposes only @@ -131,7 +131,7 @@ struct U32 : public Castable { struct F32 : public Castable { /// @param b the ProgramBuilder used to construct the AST types /// @returns the constructed ast::Type node for the given type - ast::Type* Build(ProgramBuilder& b) const override; + const ast::Type* Build(ProgramBuilder& b) const override; #ifndef NDEBUG /// @returns a string representation of the type, for debug purposes only @@ -143,7 +143,7 @@ struct F32 : public Castable { struct I32 : public Castable { /// @param b the ProgramBuilder used to construct the AST types /// @returns the constructed ast::Type node for the given type - ast::Type* Build(ProgramBuilder& b) const override; + const ast::Type* Build(ProgramBuilder& b) const override; #ifndef NDEBUG /// @returns a string representation of the type, for debug purposes only @@ -164,7 +164,7 @@ struct Pointer : public Castable { /// @param b the ProgramBuilder used to construct the AST types /// @returns the constructed ast::Type node for the given type - ast::Type* Build(ProgramBuilder& b) const override; + const ast::Type* Build(ProgramBuilder& b) const override; #ifndef NDEBUG /// @returns a string representation of the type, for debug purposes only @@ -192,7 +192,7 @@ struct Reference : public Castable { /// @param b the ProgramBuilder used to construct the AST types /// @returns the constructed ast::Type node for the given type - ast::Type* Build(ProgramBuilder& b) const override; + const ast::Type* Build(ProgramBuilder& b) const override; #ifndef NDEBUG /// @returns a string representation of the type, for debug purposes only @@ -218,7 +218,7 @@ struct Vector : public Castable { /// @param b the ProgramBuilder used to construct the AST types /// @returns the constructed ast::Type node for the given type - ast::Type* Build(ProgramBuilder& b) const override; + const ast::Type* Build(ProgramBuilder& b) const override; #ifndef NDEBUG /// @returns a string representation of the type, for debug purposes only @@ -228,7 +228,7 @@ struct Vector : public Castable { /// the element type Type const* const type; /// the number of elements in the vector - uint32_t const size; + const uint32_t size; }; /// `matNxM` type @@ -245,7 +245,7 @@ struct Matrix : public Castable { /// @param b the ProgramBuilder used to construct the AST types /// @returns the constructed ast::Type node for the given type - ast::Type* Build(ProgramBuilder& b) const override; + const ast::Type* Build(ProgramBuilder& b) const override; #ifndef NDEBUG /// @returns a string representation of the type, for debug purposes only @@ -255,9 +255,9 @@ struct Matrix : public Castable { /// the matrix element type Type const* const type; /// the number of columns in the matrix - uint32_t const columns; + const uint32_t columns; /// the number of rows in the matrix - uint32_t const rows; + const uint32_t rows; }; /// `array` type @@ -275,7 +275,7 @@ struct Array : public Castable { /// @param b the ProgramBuilder used to construct the AST types /// @returns the constructed ast::Type node for the given type - ast::Type* Build(ProgramBuilder& b) const override; + const ast::Type* Build(ProgramBuilder& b) const override; #ifndef NDEBUG /// @returns a string representation of the type, for debug purposes only @@ -285,9 +285,9 @@ struct Array : public Castable { /// the element type Type const* const type; /// the number of elements in the array. 0 represents runtime-sized array. - uint32_t const size; + const uint32_t size; /// the byte stride of the array - uint32_t const stride; + const uint32_t stride; }; /// `sampler` type @@ -302,7 +302,7 @@ struct Sampler : public Castable { /// @param b the ProgramBuilder used to construct the AST types /// @returns the constructed ast::Type node for the given type - ast::Type* Build(ProgramBuilder& b) const override; + const ast::Type* Build(ProgramBuilder& b) const override; #ifndef NDEBUG /// @returns a string representation of the type, for debug purposes only @@ -339,7 +339,7 @@ struct DepthTexture : public Castable { /// @param b the ProgramBuilder used to construct the AST types /// @returns the constructed ast::Type node for the given type - ast::Type* Build(ProgramBuilder& b) const override; + const ast::Type* Build(ProgramBuilder& b) const override; #ifndef NDEBUG /// @returns a string representation of the type, for debug purposes only @@ -360,7 +360,7 @@ struct DepthMultisampledTexture /// @param b the ProgramBuilder used to construct the AST types /// @returns the constructed ast::Type node for the given type - ast::Type* Build(ProgramBuilder& b) const override; + const ast::Type* Build(ProgramBuilder& b) const override; #ifndef NDEBUG /// @returns a string representation of the type, for debug purposes only @@ -381,7 +381,7 @@ struct MultisampledTexture : public Castable { /// @param b the ProgramBuilder used to construct the AST types /// @returns the constructed ast::Type node for the given type - ast::Type* Build(ProgramBuilder& b) const override; + const ast::Type* Build(ProgramBuilder& b) const override; #ifndef NDEBUG /// @returns a string representation of the type, for debug purposes only @@ -405,7 +405,7 @@ struct SampledTexture : public Castable { /// @param b the ProgramBuilder used to construct the AST types /// @returns the constructed ast::Type node for the given type - ast::Type* Build(ProgramBuilder& b) const override; + const ast::Type* Build(ProgramBuilder& b) const override; #ifndef NDEBUG /// @returns a string representation of the type, for debug purposes only @@ -430,7 +430,7 @@ struct StorageTexture : public Castable { /// @param b the ProgramBuilder used to construct the AST types /// @returns the constructed ast::Type node for the given type - ast::Type* Build(ProgramBuilder& b) const override; + const ast::Type* Build(ProgramBuilder& b) const override; #ifndef NDEBUG /// @returns a string representation of the type, for debug purposes only @@ -463,7 +463,7 @@ struct Named : public Castable { #endif // NDEBUG /// the type name - Symbol const name; + const Symbol name; }; /// `type T = N` type @@ -479,7 +479,7 @@ struct Alias : public Castable { /// @param b the ProgramBuilder used to construct the AST types /// @returns the constructed ast::Type node for the given type - ast::Type* Build(ProgramBuilder& b) const override; + const ast::Type* Build(ProgramBuilder& b) const override; /// the aliased type Type const* const type; @@ -501,10 +501,10 @@ struct Struct : public Castable { /// @param b the ProgramBuilder used to construct the AST types /// @returns the constructed ast::Type node for the given type - ast::Type* Build(ProgramBuilder& b) const override; + const ast::Type* Build(ProgramBuilder& b) const override; /// the member types - TypeList const members; + const TypeList members; }; /// A manager of types diff --git a/src/reader/wgsl/parser_impl.cc b/src/reader/wgsl/parser_impl.cc index 57c20ac7cd..6ab154a03e 100644 --- a/src/reader/wgsl/parser_impl.cc +++ b/src/reader/wgsl/parser_impl.cc @@ -216,7 +216,7 @@ ParserImpl::TypedIdentifier::TypedIdentifier() = default; ParserImpl::TypedIdentifier::TypedIdentifier(const TypedIdentifier&) = default; -ParserImpl::TypedIdentifier::TypedIdentifier(ast::Type* type_in, +ParserImpl::TypedIdentifier::TypedIdentifier(const ast::Type* type_in, std::string name_in, Source source_in) : type(type_in), name(std::move(name_in)), source(std::move(source_in)) {} @@ -230,7 +230,7 @@ ParserImpl::FunctionHeader::FunctionHeader(const FunctionHeader&) = default; ParserImpl::FunctionHeader::FunctionHeader(Source src, std::string n, ast::VariableList p, - ast::Type* ret_ty, + const ast::Type* ret_ty, ast::DecorationList ret_decos) : source(src), name(n), @@ -251,7 +251,7 @@ ParserImpl::VarDeclInfo::VarDeclInfo(Source source_in, std::string name_in, ast::StorageClass storage_class_in, ast::Access access_in, - ast::Type* type_in) + const ast::Type* type_in) : source(std::move(source_in)), name(std::move(name_in)), storage_class(storage_class_in), @@ -407,7 +407,7 @@ Expect ParserImpl::expect_global_decl() { if (!expect("type alias", Token::Type::kSemicolon)) return Failure::kErrored; - builder_.AST().AddTypeDecl(const_cast(ta.value)); + builder_.AST().AddTypeDecl(ta.value); return true; } @@ -478,7 +478,7 @@ Expect ParserImpl::expect_global_decl() { // global_variable_decl // : variable_decoration_list* variable_decl // | variable_decoration_list* variable_decl EQUAL const_expr -Maybe ParserImpl::global_variable_decl( +Maybe ParserImpl::global_variable_decl( ast::DecorationList& decos) { auto decl = variable_decl(); if (decl.errored) @@ -486,7 +486,7 @@ Maybe ParserImpl::global_variable_decl( if (!decl.matched) return Failure::kNoMatch; - ast::Expression* constructor = nullptr; + const ast::Expression* constructor = nullptr; if (match(Token::Type::kEqual)) { auto expr = expect_const_expr(); if (expr.errored) @@ -509,7 +509,7 @@ Maybe ParserImpl::global_variable_decl( // : attribute_list* LET variable_ident_decl global_const_initializer? // global_const_initializer // : EQUAL const_expr -Maybe ParserImpl::global_constant_decl( +Maybe ParserImpl::global_constant_decl( ast::DecorationList& decos) { if (!match(Token::Type::kLet)) { return Failure::kNoMatch; @@ -571,7 +571,7 @@ Maybe ParserImpl::variable_decl(bool allow_inferred) { // | multisampled_texture_type LESS_THAN type_decl GREATER_THAN // | storage_texture_type LESS_THAN image_storage_type // COMMA access GREATER_THAN -Maybe ParserImpl::texture_sampler_types() { +Maybe ParserImpl::texture_sampler_types() { auto type = sampler_type(); if (type.matched) return type; @@ -646,7 +646,7 @@ Maybe ParserImpl::texture_sampler_types() { // sampler_type // : SAMPLER // | SAMPLER_COMPARISON -Maybe ParserImpl::sampler_type() { +Maybe ParserImpl::sampler_type() { Source source; if (match(Token::Type::kSampler, &source)) return builder_.ty.sampler(source, ast::SamplerKind::kSampler); @@ -664,7 +664,7 @@ Maybe ParserImpl::sampler_type() { // | TEXTURE_SAMPLED_3D // | TEXTURE_SAMPLED_CUBE // | TEXTURE_SAMPLED_CUBE_ARRAY -Maybe ParserImpl::sampled_texture_type() { +Maybe ParserImpl::sampled_texture_type() { if (match(Token::Type::kTextureSampled1d)) return ast::TextureDimension::k1d; @@ -688,7 +688,7 @@ Maybe ParserImpl::sampled_texture_type() { // external_texture_type // : TEXTURE_EXTERNAL -Maybe ParserImpl::external_texture_type() { +Maybe ParserImpl::external_texture_type() { Source source; if (match(Token::Type::kTextureExternal, &source)) { return builder_.ty.external_texture(source); @@ -699,7 +699,7 @@ Maybe ParserImpl::external_texture_type() { // multisampled_texture_type // : TEXTURE_MULTISAMPLED_2D -Maybe ParserImpl::multisampled_texture_type() { +Maybe ParserImpl::multisampled_texture_type() { if (match(Token::Type::kTextureMultisampled2d)) return ast::TextureDimension::k2d; @@ -711,7 +711,7 @@ Maybe ParserImpl::multisampled_texture_type() { // | TEXTURE_STORAGE_2D // | TEXTURE_STORAGE_2D_ARRAY // | TEXTURE_STORAGE_3D -Maybe ParserImpl::storage_texture_type() { +Maybe ParserImpl::storage_texture_type() { if (match(Token::Type::kTextureStorage1d)) return ast::TextureDimension::k1d; if (match(Token::Type::kTextureStorage2d)) @@ -730,7 +730,7 @@ Maybe ParserImpl::storage_texture_type() { // | TEXTURE_DEPTH_CUBE // | TEXTURE_DEPTH_CUBE_ARRAY // | TEXTURE_DEPTH_MULTISAMPLED_2D -Maybe ParserImpl::depth_texture_type() { +Maybe ParserImpl::depth_texture_type() { Source source; if (match(Token::Type::kTextureDepth2d, &source)) { return builder_.ty.depth_texture(source, ast::TextureDimension::k2d); @@ -979,7 +979,7 @@ Maybe ParserImpl::variable_qualifier() { // type_alias // : TYPE IDENT EQUAL type_decl -Maybe ParserImpl::type_alias() { +Maybe ParserImpl::type_alias() { if (!peek_is(Token::Type::kType)) return Failure::kNoMatch; @@ -1029,7 +1029,7 @@ Maybe ParserImpl::type_alias() { // | MAT4x3 LESS_THAN type_decl GREATER_THAN // | MAT4x4 LESS_THAN type_decl GREATER_THAN // | texture_sampler_types -Maybe ParserImpl::type_decl() { +Maybe ParserImpl::type_decl() { auto decos = decoration_list(); if (decos.errored) return Failure::kErrored; @@ -1048,7 +1048,7 @@ Maybe ParserImpl::type_decl() { return type; } -Maybe ParserImpl::type_decl(ast::DecorationList& decos) { +Maybe ParserImpl::type_decl(ast::DecorationList& decos) { auto t = peek(); Source source; if (match(Token::Type::kIdentifier, &source)) { @@ -1104,7 +1104,7 @@ Maybe ParserImpl::type_decl(ast::DecorationList& decos) { return Failure::kNoMatch; } -Expect ParserImpl::expect_type(const std::string& use) { +Expect ParserImpl::expect_type(const std::string& use) { auto type = type_decl(); if (type.errored) return Failure::kErrored; @@ -1113,13 +1113,13 @@ Expect ParserImpl::expect_type(const std::string& use) { return type.value; } -Expect ParserImpl::expect_type_decl_pointer(Token t) { +Expect ParserImpl::expect_type_decl_pointer(Token t) { const char* use = "ptr declaration"; auto storage_class = ast::StorageClass::kNone; auto access = ast::Access::kUndefined; - auto subtype = expect_lt_gt_block(use, [&]() -> Expect { + auto subtype = expect_lt_gt_block(use, [&]() -> Expect { auto sc = expect_storage_class(use); if (sc.errored) { return Failure::kErrored; @@ -1154,7 +1154,7 @@ Expect ParserImpl::expect_type_decl_pointer(Token t) { storage_class, access); } -Expect ParserImpl::expect_type_decl_atomic(Token t) { +Expect ParserImpl::expect_type_decl_atomic(Token t) { const char* use = "atomic declaration"; auto subtype = expect_lt_gt_block(use, [&] { return expect_type(use); }); @@ -1165,7 +1165,7 @@ Expect ParserImpl::expect_type_decl_atomic(Token t) { return builder_.ty.atomic(make_source_range_from(t.source()), subtype.value); } -Expect ParserImpl::expect_type_decl_vector(Token t) { +Expect ParserImpl::expect_type_decl_vector(Token t) { uint32_t count = 2; if (t.Is(Token::Type::kVec3)) count = 3; @@ -1182,14 +1182,14 @@ Expect ParserImpl::expect_type_decl_vector(Token t) { count); } -Expect ParserImpl::expect_type_decl_array( +Expect ParserImpl::expect_type_decl_array( Token t, ast::DecorationList decos) { const char* use = "array declaration"; - ast::Expression* size = nullptr; + const ast::Expression* size = nullptr; - auto subtype = expect_lt_gt_block(use, [&]() -> Expect { + auto subtype = expect_lt_gt_block(use, [&]() -> Expect { auto type = expect_type(use); if (type.errored) return Failure::kErrored; @@ -1216,7 +1216,7 @@ Expect ParserImpl::expect_type_decl_array( size, std::move(decos)); } -Expect ParserImpl::expect_type_decl_matrix(Token t) { +Expect ParserImpl::expect_type_decl_matrix(Token t) { uint32_t rows = 2; uint32_t columns = 2; if (t.IsMat3xN()) { @@ -1276,7 +1276,7 @@ Expect ParserImpl::expect_storage_class( // struct_decl // : struct_decoration_decl* STRUCT IDENT struct_body_decl -Maybe ParserImpl::struct_decl(ast::DecorationList& decos) { +Maybe ParserImpl::struct_decl(ast::DecorationList& decos) { auto t = peek(); auto source = t.source(); @@ -1351,7 +1351,8 @@ Expect ParserImpl::expect_struct_member( // function_decl // : function_header body_stmt -Maybe ParserImpl::function_decl(ast::DecorationList& decos) { +Maybe ParserImpl::function_decl( + ast::DecorationList& decos) { auto header = function_header(); if (header.errored) { if (sync_to(Token::Type::kBraceLeft, /* consume: */ false)) { @@ -1412,7 +1413,7 @@ Maybe ParserImpl::function_header() { } } - ast::Type* return_type = nullptr; + const ast::Type* return_type = nullptr; ast::DecorationList return_decorations; if (match(Token::Type::kArrow)) { @@ -1556,8 +1557,8 @@ Expect ParserImpl::expect_body_stmt() { // paren_rhs_stmt // : PAREN_LEFT logical_or_expression PAREN_RIGHT -Expect ParserImpl::expect_paren_rhs_stmt() { - return expect_paren_block("", [&]() -> Expect { +Expect ParserImpl::expect_paren_rhs_stmt() { + return expect_paren_block("", [&]() -> Expect { auto expr = logical_or_expression(); if (expr.errored) return Failure::kErrored; @@ -1606,7 +1607,7 @@ Expect ParserImpl::expect_statements() { // | continue_stmt SEMICOLON // | DISCARD SEMICOLON // | assignment_stmt SEMICOLON -Maybe ParserImpl::statement() { +Maybe ParserImpl::statement() { while (match(Token::Type::kSemicolon)) { // Skip empty statements } @@ -1662,8 +1663,8 @@ Maybe ParserImpl::statement() { // | continue_stmt SEMICOLON // | DISCARD SEMICOLON // | assignment_stmt SEMICOLON -Maybe ParserImpl::non_block_statement() { - auto stmt = [&]() -> Maybe { +Maybe ParserImpl::non_block_statement() { + auto stmt = [&]() -> Maybe { auto ret_stmt = return_stmt(); if (ret_stmt.errored) return Failure::kErrored; @@ -1715,7 +1716,7 @@ Maybe ParserImpl::non_block_statement() { // return_stmt // : RETURN logical_or_expression? -Maybe ParserImpl::return_stmt() { +Maybe ParserImpl::return_stmt() { Source source; if (!match(Token::Type::kReturn, &source)) return Failure::kNoMatch; @@ -1735,7 +1736,7 @@ Maybe ParserImpl::return_stmt() { // : variable_decl // | variable_decl EQUAL logical_or_expression // | CONST variable_ident_decl EQUAL logical_or_expression -Maybe ParserImpl::variable_stmt() { +Maybe ParserImpl::variable_stmt() { if (match(Token::Type::kLet)) { auto decl = expect_variable_ident_decl("let declaration", /*allow_inferred = */ true); @@ -1770,7 +1771,7 @@ Maybe ParserImpl::variable_stmt() { if (!decl.matched) return Failure::kNoMatch; - ast::Expression* constructor = nullptr; + const ast::Expression* constructor = nullptr; if (match(Token::Type::kEqual)) { auto constructor_expr = logical_or_expression(); if (constructor_expr.errored) @@ -1796,7 +1797,7 @@ Maybe ParserImpl::variable_stmt() { // if_stmt // : IF paren_rhs_stmt body_stmt elseif_stmt? else_stmt? -Maybe ParserImpl::if_stmt() { +Maybe ParserImpl::if_stmt() { Source source; if (!match(Token::Type::kIf, &source)) return Failure::kNoMatch; @@ -1852,7 +1853,7 @@ Maybe ParserImpl::elseif_stmt() { // else_stmt // : ELSE body_stmt -Maybe ParserImpl::else_stmt() { +Maybe ParserImpl::else_stmt() { Source source; if (!match(Token::Type::kElse, &source)) return Failure::kNoMatch; @@ -1866,7 +1867,7 @@ Maybe ParserImpl::else_stmt() { // switch_stmt // : SWITCH paren_rhs_stmt BRACKET_LEFT switch_body+ BRACKET_RIGHT -Maybe ParserImpl::switch_stmt() { +Maybe ParserImpl::switch_stmt() { Source source; if (!match(Token::Type::kSwitch, &source)) return Failure::kNoMatch; @@ -1903,7 +1904,7 @@ Maybe ParserImpl::switch_stmt() { // switch_body // : CASE case_selectors COLON BRACKET_LEFT case_body BRACKET_RIGHT // | DEFAULT COLON BRACKET_LEFT case_body BRACKET_RIGHT -Maybe ParserImpl::switch_body() { +Maybe ParserImpl::switch_body() { if (!peek_is(Token::Type::kCase) && !peek_is(Token::Type::kDefault)) return Failure::kNoMatch; @@ -1967,7 +1968,7 @@ Expect ParserImpl::expect_case_selectors() { // : // | statement case_body // | FALLTHROUGH SEMICOLON -Maybe ParserImpl::case_body() { +Maybe ParserImpl::case_body() { ast::StatementList stmts; while (continue_parsing()) { Source source; @@ -1993,12 +1994,12 @@ Maybe ParserImpl::case_body() { // loop_stmt // : LOOP BRACKET_LEFT statements continuing_stmt? BRACKET_RIGHT -Maybe ParserImpl::loop_stmt() { +Maybe ParserImpl::loop_stmt() { Source source; if (!match(Token::Type::kLoop, &source)) return Failure::kNoMatch; - return expect_brace_block("loop", [&]() -> Maybe { + return expect_brace_block("loop", [&]() -> Maybe { auto stmts = expect_statements(); if (stmts.errored) return Failure::kErrored; @@ -2012,15 +2013,15 @@ Maybe ParserImpl::loop_stmt() { }); } -ForHeader::ForHeader(ast::Statement* init, - ast::Expression* cond, - ast::Statement* cont) +ForHeader::ForHeader(const ast::Statement* init, + const ast::Expression* cond, + const ast::Statement* cont) : initializer(init), condition(cond), continuing(cont) {} ForHeader::~ForHeader() = default; // (variable_stmt | assignment_stmt | func_call_stmt)? -Maybe ParserImpl::for_header_initializer() { +Maybe ParserImpl::for_header_initializer() { auto call = func_call_stmt(); if (call.errored) return Failure::kErrored; @@ -2043,7 +2044,7 @@ Maybe ParserImpl::for_header_initializer() { } // (assignment_stmt | func_call_stmt)? -Maybe ParserImpl::for_header_continuing() { +Maybe ParserImpl::for_header_continuing() { auto call_stmt = func_call_stmt(); if (call_stmt.errored) return Failure::kErrored; @@ -2089,7 +2090,7 @@ Expect> ParserImpl::expect_for_header() { // for_statement // : FOR PAREN_LEFT for_header PAREN_RIGHT BRACE_LEFT statements BRACE_RIGHT -Maybe ParserImpl::for_stmt() { +Maybe ParserImpl::for_stmt() { Source source; if (!match(Token::Type::kFor, &source)) return Failure::kNoMatch; @@ -2111,7 +2112,7 @@ Maybe ParserImpl::for_stmt() { // func_call_stmt // : IDENT argument_expression_list -Maybe ParserImpl::func_call_stmt() { +Maybe ParserImpl::func_call_stmt() { auto t = peek(); auto t2 = peek(1); if (!t.IsIdentifier() || !t2.Is(Token::Type::kParenLeft)) @@ -2136,7 +2137,7 @@ Maybe ParserImpl::func_call_stmt() { // break_stmt // : BREAK -Maybe ParserImpl::break_stmt() { +Maybe ParserImpl::break_stmt() { Source source; if (!match(Token::Type::kBreak, &source)) return Failure::kNoMatch; @@ -2146,7 +2147,7 @@ Maybe ParserImpl::break_stmt() { // continue_stmt // : CONTINUE -Maybe ParserImpl::continue_stmt() { +Maybe ParserImpl::continue_stmt() { Source source; if (!match(Token::Type::kContinue, &source)) return Failure::kNoMatch; @@ -2156,7 +2157,7 @@ Maybe ParserImpl::continue_stmt() { // continuing_stmt // : CONTINUING body_stmt -Maybe ParserImpl::continuing_stmt() { +Maybe ParserImpl::continuing_stmt() { if (!match(Token::Type::kContinuing)) return create(Source{}, ast::StatementList{}); @@ -2169,7 +2170,7 @@ Maybe ParserImpl::continuing_stmt() { // | const_literal // | paren_rhs_stmt // | BITCAST LESS_THAN type_decl GREATER_THAN paren_rhs_stmt -Maybe ParserImpl::primary_expression() { +Maybe ParserImpl::primary_expression() { auto t = peek(); auto source = t.source(); @@ -2238,8 +2239,8 @@ Maybe ParserImpl::primary_expression() { // : // | BRACE_LEFT logical_or_expression BRACE_RIGHT postfix_expr // | PERIOD IDENTIFIER postfix_expr -Maybe ParserImpl::postfix_expression( - ast::Expression* prefix) { +Maybe ParserImpl::postfix_expression( + const ast::Expression* prefix) { Source source; while (continue_parsing()) { @@ -2252,8 +2253,8 @@ Maybe ParserImpl::postfix_expression( } if (match(Token::Type::kBracketLeft, &source)) { - auto res = - sync(Token::Type::kBracketRight, [&]() -> Maybe { + auto res = sync( + Token::Type::kBracketRight, [&]() -> Maybe { auto param = logical_or_expression(); if (param.errored) return Failure::kErrored; @@ -2297,7 +2298,7 @@ Maybe ParserImpl::postfix_expression( // singular_expression // : primary_expression postfix_expr -Maybe ParserImpl::singular_expression() { +Maybe ParserImpl::singular_expression() { auto prefix = primary_expression(); if (prefix.errored) return Failure::kErrored; @@ -2338,7 +2339,7 @@ Expect ParserImpl::expect_argument_expression_list( // | TILDE unary_expression // | STAR unary_expression // | AND unary_expression -Maybe ParserImpl::unary_expression() { +Maybe ParserImpl::unary_expression() { auto t = peek(); if (match(Token::Type::kPlusPlus) || match(Token::Type::kMinusMinus)) { @@ -2391,8 +2392,8 @@ Maybe ParserImpl::unary_expression() { // | STAR unary_expression multiplicative_expr // | FORWARD_SLASH unary_expression multiplicative_expr // | MODULO unary_expression multiplicative_expr -Expect ParserImpl::expect_multiplicative_expr( - ast::Expression* lhs) { +Expect ParserImpl::expect_multiplicative_expr( + const ast::Expression* lhs) { while (continue_parsing()) { ast::BinaryOp op = ast::BinaryOp::kNone; if (peek_is(Token::Type::kStar)) @@ -2423,7 +2424,7 @@ Expect ParserImpl::expect_multiplicative_expr( // multiplicative_expression // : unary_expression multiplicative_expr -Maybe ParserImpl::multiplicative_expression() { +Maybe ParserImpl::multiplicative_expression() { auto lhs = unary_expression(); if (lhs.errored) return Failure::kErrored; @@ -2437,8 +2438,8 @@ Maybe ParserImpl::multiplicative_expression() { // : // | PLUS multiplicative_expression additive_expr // | MINUS multiplicative_expression additive_expr -Expect ParserImpl::expect_additive_expr( - ast::Expression* lhs) { +Expect ParserImpl::expect_additive_expr( + const ast::Expression* lhs) { while (continue_parsing()) { ast::BinaryOp op = ast::BinaryOp::kNone; if (peek_is(Token::Type::kPlus)) @@ -2464,7 +2465,7 @@ Expect ParserImpl::expect_additive_expr( // additive_expression // : multiplicative_expression additive_expr -Maybe ParserImpl::additive_expression() { +Maybe ParserImpl::additive_expression() { auto lhs = multiplicative_expression(); if (lhs.errored) return Failure::kErrored; @@ -2478,7 +2479,8 @@ Maybe ParserImpl::additive_expression() { // : // | SHIFT_LEFT additive_expression shift_expr // | SHIFT_RIGHT additive_expression shift_expr -Expect ParserImpl::expect_shift_expr(ast::Expression* lhs) { +Expect ParserImpl::expect_shift_expr( + const ast::Expression* lhs) { while (continue_parsing()) { auto* name = ""; ast::BinaryOp op = ast::BinaryOp::kNone; @@ -2509,7 +2511,7 @@ Expect ParserImpl::expect_shift_expr(ast::Expression* lhs) { // shift_expression // : additive_expression shift_expr -Maybe ParserImpl::shift_expression() { +Maybe ParserImpl::shift_expression() { auto lhs = additive_expression(); if (lhs.errored) return Failure::kErrored; @@ -2525,8 +2527,8 @@ Maybe ParserImpl::shift_expression() { // | GREATER_THAN shift_expression relational_expr // | LESS_THAN_EQUAL shift_expression relational_expr // | GREATER_THAN_EQUAL shift_expression relational_expr -Expect ParserImpl::expect_relational_expr( - ast::Expression* lhs) { +Expect ParserImpl::expect_relational_expr( + const ast::Expression* lhs) { while (continue_parsing()) { ast::BinaryOp op = ast::BinaryOp::kNone; if (peek_is(Token::Type::kLessThan)) @@ -2559,7 +2561,7 @@ Expect ParserImpl::expect_relational_expr( // relational_expression // : shift_expression relational_expr -Maybe ParserImpl::relational_expression() { +Maybe ParserImpl::relational_expression() { auto lhs = shift_expression(); if (lhs.errored) return Failure::kErrored; @@ -2573,8 +2575,8 @@ Maybe ParserImpl::relational_expression() { // : // | EQUAL_EQUAL relational_expression equality_expr // | NOT_EQUAL relational_expression equality_expr -Expect ParserImpl::expect_equality_expr( - ast::Expression* lhs) { +Expect ParserImpl::expect_equality_expr( + const ast::Expression* lhs) { while (continue_parsing()) { ast::BinaryOp op = ast::BinaryOp::kNone; if (peek_is(Token::Type::kEqualEqual)) @@ -2603,7 +2605,7 @@ Expect ParserImpl::expect_equality_expr( // equality_expression // : relational_expression equality_expr -Maybe ParserImpl::equality_expression() { +Maybe ParserImpl::equality_expression() { auto lhs = relational_expression(); if (lhs.errored) return Failure::kErrored; @@ -2616,7 +2618,8 @@ Maybe ParserImpl::equality_expression() { // and_expr // : // | AND equality_expression and_expr -Expect ParserImpl::expect_and_expr(ast::Expression* lhs) { +Expect ParserImpl::expect_and_expr( + const ast::Expression* lhs) { while (continue_parsing()) { if (!peek_is(Token::Type::kAnd)) { return lhs; @@ -2639,7 +2642,7 @@ Expect ParserImpl::expect_and_expr(ast::Expression* lhs) { // and_expression // : equality_expression and_expr -Maybe ParserImpl::and_expression() { +Maybe ParserImpl::and_expression() { auto lhs = equality_expression(); if (lhs.errored) return Failure::kErrored; @@ -2652,8 +2655,8 @@ Maybe ParserImpl::and_expression() { // exclusive_or_expr // : // | XOR and_expression exclusive_or_expr -Expect ParserImpl::expect_exclusive_or_expr( - ast::Expression* lhs) { +Expect ParserImpl::expect_exclusive_or_expr( + const ast::Expression* lhs) { while (continue_parsing()) { Source source; if (!match(Token::Type::kXor, &source)) @@ -2673,7 +2676,7 @@ Expect ParserImpl::expect_exclusive_or_expr( // exclusive_or_expression // : and_expression exclusive_or_expr -Maybe ParserImpl::exclusive_or_expression() { +Maybe ParserImpl::exclusive_or_expression() { auto lhs = and_expression(); if (lhs.errored) return Failure::kErrored; @@ -2686,8 +2689,8 @@ Maybe ParserImpl::exclusive_or_expression() { // inclusive_or_expr // : // | OR exclusive_or_expression inclusive_or_expr -Expect ParserImpl::expect_inclusive_or_expr( - ast::Expression* lhs) { +Expect ParserImpl::expect_inclusive_or_expr( + const ast::Expression* lhs) { while (continue_parsing()) { Source source; if (!match(Token::Type::kOr)) @@ -2707,7 +2710,7 @@ Expect ParserImpl::expect_inclusive_or_expr( // inclusive_or_expression // : exclusive_or_expression inclusive_or_expr -Maybe ParserImpl::inclusive_or_expression() { +Maybe ParserImpl::inclusive_or_expression() { auto lhs = exclusive_or_expression(); if (lhs.errored) return Failure::kErrored; @@ -2720,8 +2723,8 @@ Maybe ParserImpl::inclusive_or_expression() { // logical_and_expr // : // | AND_AND inclusive_or_expression logical_and_expr -Expect ParserImpl::expect_logical_and_expr( - ast::Expression* lhs) { +Expect ParserImpl::expect_logical_and_expr( + const ast::Expression* lhs) { while (continue_parsing()) { if (!peek_is(Token::Type::kAndAnd)) { return lhs; @@ -2744,7 +2747,7 @@ Expect ParserImpl::expect_logical_and_expr( // logical_and_expression // : inclusive_or_expression logical_and_expr -Maybe ParserImpl::logical_and_expression() { +Maybe ParserImpl::logical_and_expression() { auto lhs = inclusive_or_expression(); if (lhs.errored) return Failure::kErrored; @@ -2757,8 +2760,8 @@ Maybe ParserImpl::logical_and_expression() { // logical_or_expr // : // | OR_OR logical_and_expression logical_or_expr -Expect ParserImpl::expect_logical_or_expr( - ast::Expression* lhs) { +Expect ParserImpl::expect_logical_or_expr( + const ast::Expression* lhs) { while (continue_parsing()) { Source source; if (!match(Token::Type::kOrOr)) @@ -2778,7 +2781,7 @@ Expect ParserImpl::expect_logical_or_expr( // logical_or_expression // : logical_and_expression logical_or_expr -Maybe ParserImpl::logical_or_expression() { +Maybe ParserImpl::logical_or_expression() { auto lhs = logical_and_expression(); if (lhs.errored) return Failure::kErrored; @@ -2790,7 +2793,7 @@ Maybe ParserImpl::logical_or_expression() { // assignment_stmt // : unary_expression EQUAL logical_or_expression -Maybe ParserImpl::assignment_stmt() { +Maybe ParserImpl::assignment_stmt() { auto t = peek(); auto source = t.source(); @@ -2826,7 +2829,7 @@ Maybe ParserImpl::assignment_stmt() { // | FLOAT_LITERAL // | TRUE // | FALSE -Maybe ParserImpl::const_literal() { +Maybe ParserImpl::const_literal() { auto t = peek(); if (t.IsError()) { return add_error(t.source(), t.to_str()); @@ -2979,7 +2982,7 @@ Maybe ParserImpl::decoration_bracketed_list(ast::DecorationList& decos) { }); } -Expect ParserImpl::expect_decoration() { +Expect ParserImpl::expect_decoration() { auto t = peek(); auto deco = decoration(); if (deco.errored) @@ -2989,8 +2992,8 @@ Expect ParserImpl::expect_decoration() { return add_error(t, "expected decoration"); } -Maybe ParserImpl::decoration() { - using Result = Maybe; +Maybe ParserImpl::decoration() { + using Result = Maybe; auto t = next(); if (!t.IsIdentifier()) { @@ -3083,9 +3086,9 @@ Maybe ParserImpl::decoration() { if (s == kWorkgroupSizeDecoration) { return expect_paren_block("workgroup_size decoration", [&]() -> Result { - ast::Expression* x = nullptr; - ast::Expression* y = nullptr; - ast::Expression* z = nullptr; + const ast::Expression* x = nullptr; + const ast::Expression* y = nullptr; + const ast::Expression* z = nullptr; auto expr = primary_expression(); if (expr.errored) { @@ -3187,7 +3190,7 @@ Maybe ParserImpl::decoration() { return Failure::kNoMatch; } -bool ParserImpl::expect_decorations_consumed(const ast::DecorationList& in) { +bool ParserImpl::expect_decorations_consumed(ast::DecorationList& in) { if (in.empty()) { return true; } diff --git a/src/reader/wgsl/parser_impl.h b/src/reader/wgsl/parser_impl.h index 33a6d64b17..40335aa0fc 100644 --- a/src/reader/wgsl/parser_impl.h +++ b/src/reader/wgsl/parser_impl.h @@ -52,16 +52,18 @@ struct ForHeader { /// @param init the initializer statement /// @param cond the condition statement /// @param cont the continuing statement - ForHeader(ast::Statement* init, ast::Expression* cond, ast::Statement* cont); + ForHeader(const ast::Statement* init, + const ast::Expression* cond, + const ast::Statement* cont); ~ForHeader(); /// The for loop initializer - ast::Statement* initializer = nullptr; + const ast::Statement* initializer = nullptr; /// The for loop condition - ast::Expression* condition = nullptr; + const ast::Expression* condition = nullptr; /// The for loop continuing statement - ast::Statement* continuing = nullptr; + const ast::Statement* continuing = nullptr; }; /// ParserImpl for WGSL source data @@ -210,12 +212,14 @@ class ParserImpl { /// @param type_in parsed type /// @param name_in parsed identifier /// @param source_in source to the identifier - TypedIdentifier(ast::Type* type_in, std::string name_in, Source source_in); + TypedIdentifier(const ast::Type* type_in, + std::string name_in, + Source source_in); /// Destructor ~TypedIdentifier(); /// Parsed type. May be nullptr for inferred types. - ast::Type* type = nullptr; + const ast::Type* type = nullptr; /// Parsed identifier. std::string name; /// Source to the identifier. @@ -238,7 +242,7 @@ class ParserImpl { FunctionHeader(Source src, std::string n, ast::VariableList p, - ast::Type* ret_ty, + const ast::Type* ret_ty, ast::DecorationList ret_decos); /// Destructor ~FunctionHeader(); @@ -254,7 +258,7 @@ class ParserImpl { /// Function parameters ast::VariableList params; /// Function return type - ast::Type* return_type; + const ast::Type* return_type = nullptr; /// Function return type decorations ast::DecorationList return_type_decorations; }; @@ -276,7 +280,7 @@ class ParserImpl { std::string name_in, ast::StorageClass storage_class_in, ast::Access access_in, - ast::Type* type_in); + const ast::Type* type_in); /// Destructor ~VarDeclInfo(); @@ -289,7 +293,7 @@ class ParserImpl { /// Variable access control ast::Access access = ast::Access::kUndefined; /// Variable type - ast::Type* type = nullptr; + const ast::Type* type = nullptr; }; /// VariableQualifier contains the parsed information for a variable qualifier @@ -396,12 +400,12 @@ class ParserImpl { /// `variable_decoration_list*` provided as `decos` /// @returns the variable parsed or nullptr /// @param decos the list of decorations for the variable declaration. - Maybe global_variable_decl(ast::DecorationList& decos); + Maybe global_variable_decl(ast::DecorationList& decos); /// Parses a `global_constant_decl` grammar element with the initial /// `variable_decoration_list*` provided as `decos` /// @returns the const object or nullptr /// @param decos the list of decorations for the constant declaration. - Maybe global_constant_decl(ast::DecorationList& decos); + Maybe global_constant_decl(ast::DecorationList& decos); /// Parses a `variable_decl` grammar element /// @param allow_inferred if true, do not fail if variable decl does not /// specify type @@ -421,15 +425,15 @@ class ParserImpl { Maybe variable_qualifier(); /// Parses a `type_alias` grammar element /// @returns the type alias or nullptr on error - Maybe type_alias(); + Maybe type_alias(); /// Parses a `type_decl` grammar element /// @returns the parsed Type or nullptr if none matched. - Maybe type_decl(); + Maybe type_decl(); /// Parses a `type_decl` grammar element with the given pre-parsed /// decorations. /// @param decos the list of decorations for the type. /// @returns the parsed Type or nullptr if none matched. - Maybe type_decl(ast::DecorationList& decos); + Maybe type_decl(ast::DecorationList& decos); /// Parses a `storage_class` grammar element, erroring on parse failure. /// @param use a description of what was being parsed if an error was raised. /// @returns the storage class or StorageClass::kNone if none matched @@ -438,7 +442,7 @@ class ParserImpl { /// `struct_decoration_decl*` provided as `decos`. /// @returns the struct type or nullptr on error /// @param decos the list of decorations for the struct declaration. - Maybe struct_decl(ast::DecorationList& decos); + Maybe struct_decl(ast::DecorationList& decos); /// Parses a `struct_body_decl` grammar element, erroring on parse failure. /// @returns the struct members Expect expect_struct_body_decl(); @@ -452,30 +456,30 @@ class ParserImpl { /// `function_decoration_decl*` provided as `decos`. /// @param decos the list of decorations for the function declaration. /// @returns the parsed function, nullptr otherwise - Maybe function_decl(ast::DecorationList& decos); + Maybe function_decl(ast::DecorationList& decos); /// Parses a `texture_sampler_types` grammar element /// @returns the parsed Type or nullptr if none matched. - Maybe texture_sampler_types(); + Maybe texture_sampler_types(); /// Parses a `sampler_type` grammar element /// @returns the parsed Type or nullptr if none matched. - Maybe sampler_type(); + Maybe sampler_type(); /// Parses a `multisampled_texture_type` grammar element /// @returns returns the multisample texture dimension or kNone if none /// matched. - Maybe multisampled_texture_type(); + Maybe multisampled_texture_type(); /// Parses a `sampled_texture_type` grammar element /// @returns returns the sample texture dimension or kNone if none matched. - Maybe sampled_texture_type(); + Maybe sampled_texture_type(); /// Parses a `storage_texture_type` grammar element /// @returns returns the storage texture dimension. /// Returns kNone if none matched. - Maybe storage_texture_type(); + Maybe storage_texture_type(); /// Parses a `depth_texture_type` grammar element /// @returns the parsed Type or nullptr if none matched. - Maybe depth_texture_type(); + Maybe depth_texture_type(); /// Parses a 'texture_external_type' grammar element /// @returns the parsed Type or nullptr if none matched - Maybe external_texture_type(); + Maybe external_texture_type(); /// Parses a `image_storage_type` grammar element /// @param use a description of what was being parsed if an error was raised /// @returns returns the image format or kNone if none matched. @@ -507,70 +511,70 @@ class ParserImpl { Expect expect_body_stmt(); /// Parses a `paren_rhs_stmt` grammar element, erroring on parse failure. /// @returns the parsed element or nullptr - Expect expect_paren_rhs_stmt(); + Expect expect_paren_rhs_stmt(); /// Parses a `statements` grammar element /// @returns the statements parsed Expect expect_statements(); /// Parses a `statement` grammar element /// @returns the parsed statement or nullptr - Maybe statement(); + Maybe statement(); /// Parses a `break_stmt` grammar element /// @returns the parsed statement or nullptr - Maybe break_stmt(); + Maybe break_stmt(); /// Parses a `return_stmt` grammar element /// @returns the parsed statement or nullptr - Maybe return_stmt(); + Maybe return_stmt(); /// Parses a `continue_stmt` grammar element /// @returns the parsed statement or nullptr - Maybe continue_stmt(); + Maybe continue_stmt(); /// Parses a `variable_stmt` grammar element /// @returns the parsed variable or nullptr - Maybe variable_stmt(); + Maybe variable_stmt(); /// Parses a `if_stmt` grammar element /// @returns the parsed statement or nullptr - Maybe if_stmt(); + Maybe if_stmt(); /// Parses a `elseif_stmt` grammar element /// @returns the parsed elements Maybe elseif_stmt(); /// Parses a `else_stmt` grammar element /// @returns the parsed statement or nullptr - Maybe else_stmt(); + Maybe else_stmt(); /// Parses a `switch_stmt` grammar element /// @returns the parsed statement or nullptr - Maybe switch_stmt(); + Maybe switch_stmt(); /// Parses a `switch_body` grammar element /// @returns the parsed statement or nullptr - Maybe switch_body(); + Maybe switch_body(); /// Parses a `case_selectors` grammar element /// @returns the list of literals Expect expect_case_selectors(); /// Parses a `case_body` grammar element /// @returns the parsed statements - Maybe case_body(); + Maybe case_body(); /// Parses a `func_call_stmt` grammar element /// @returns the parsed function call or nullptr - Maybe func_call_stmt(); + Maybe func_call_stmt(); /// Parses a `loop_stmt` grammar element /// @returns the parsed loop or nullptr - Maybe loop_stmt(); + Maybe loop_stmt(); /// Parses a `for_header` grammar element, erroring on parse failure. /// @returns the parsed for header or nullptr Expect> expect_for_header(); /// Parses a `for_stmt` grammar element /// @returns the parsed for loop or nullptr - Maybe for_stmt(); + Maybe for_stmt(); /// Parses a `continuing_stmt` grammar element /// @returns the parsed statements - Maybe continuing_stmt(); + Maybe continuing_stmt(); /// Parses a `const_literal` grammar element /// @returns the const literal parsed or nullptr if none found - Maybe const_literal(); + Maybe const_literal(); /// Parses a `const_expr` grammar element, erroring on parse failure. /// @returns the parsed constructor expression or nullptr on error Expect expect_const_expr(); /// Parses a `primary_expression` grammar element /// @returns the parsed expression or nullptr - Maybe primary_expression(); + Maybe primary_expression(); /// Parses a `argument_expression_list` grammar element, erroring on parse /// failure. /// @param use a description of what was being parsed if an error was raised @@ -580,96 +584,105 @@ class ParserImpl { /// Parses the recursive portion of the postfix_expression /// @param prefix the left side of the expression /// @returns the parsed expression or nullptr - Maybe postfix_expression(ast::Expression* prefix); + Maybe postfix_expression( + const ast::Expression* prefix); /// Parses a `singular_expression` grammar elment /// @returns the parsed expression or nullptr - Maybe singular_expression(); + Maybe singular_expression(); /// Parses a `unary_expression` grammar element /// @returns the parsed expression or nullptr - Maybe unary_expression(); + Maybe unary_expression(); /// Parses the recursive part of the `multiplicative_expression`, erroring on /// parse failure. /// @param lhs the left side of the expression /// @returns the parsed expression or nullptr - Expect expect_multiplicative_expr(ast::Expression* lhs); + Expect expect_multiplicative_expr( + const ast::Expression* lhs); /// Parses the `multiplicative_expression` grammar element /// @returns the parsed expression or nullptr - Maybe multiplicative_expression(); + Maybe multiplicative_expression(); /// Parses the recursive part of the `additive_expression`, erroring on parse /// failure. /// @param lhs the left side of the expression /// @returns the parsed expression or nullptr - Expect expect_additive_expr(ast::Expression* lhs); + Expect expect_additive_expr( + const ast::Expression* lhs); /// Parses the `additive_expression` grammar element /// @returns the parsed expression or nullptr - Maybe additive_expression(); + Maybe additive_expression(); /// Parses the recursive part of the `shift_expression`, erroring on parse /// failure. /// @param lhs the left side of the expression /// @returns the parsed expression or nullptr - Expect expect_shift_expr(ast::Expression* lhs); + Expect expect_shift_expr(const ast::Expression* lhs); /// Parses the `shift_expression` grammar element /// @returns the parsed expression or nullptr - Maybe shift_expression(); + Maybe shift_expression(); /// Parses the recursive part of the `relational_expression`, erroring on /// parse failure. /// @param lhs the left side of the expression /// @returns the parsed expression or nullptr - Expect expect_relational_expr(ast::Expression* lhs); + Expect expect_relational_expr( + const ast::Expression* lhs); /// Parses the `relational_expression` grammar element /// @returns the parsed expression or nullptr - Maybe relational_expression(); + Maybe relational_expression(); /// Parses the recursive part of the `equality_expression`, erroring on parse /// failure. /// @param lhs the left side of the expression /// @returns the parsed expression or nullptr - Expect expect_equality_expr(ast::Expression* lhs); + Expect expect_equality_expr( + const ast::Expression* lhs); /// Parses the `equality_expression` grammar element /// @returns the parsed expression or nullptr - Maybe equality_expression(); + Maybe equality_expression(); /// Parses the recursive part of the `and_expression`, erroring on parse /// failure. /// @param lhs the left side of the expression /// @returns the parsed expression or nullptr - Expect expect_and_expr(ast::Expression* lhs); + Expect expect_and_expr(const ast::Expression* lhs); /// Parses the `and_expression` grammar element /// @returns the parsed expression or nullptr - Maybe and_expression(); + Maybe and_expression(); /// Parses the recursive part of the `exclusive_or_expression`, erroring on /// parse failure. /// @param lhs the left side of the expression /// @returns the parsed expression or nullptr - Expect expect_exclusive_or_expr(ast::Expression* lhs); + Expect expect_exclusive_or_expr( + const ast::Expression* lhs); /// Parses the `exclusive_or_expression` grammar elememnt /// @returns the parsed expression or nullptr - Maybe exclusive_or_expression(); + Maybe exclusive_or_expression(); /// Parses the recursive part of the `inclusive_or_expression`, erroring on /// parse failure. /// @param lhs the left side of the expression /// @returns the parsed expression or nullptr - Expect expect_inclusive_or_expr(ast::Expression* lhs); + Expect expect_inclusive_or_expr( + const ast::Expression* lhs); /// Parses the `inclusive_or_expression` grammar element /// @returns the parsed expression or nullptr - Maybe inclusive_or_expression(); + Maybe inclusive_or_expression(); /// Parses the recursive part of the `logical_and_expression`, erroring on /// parse failure. /// @param lhs the left side of the expression /// @returns the parsed expression or nullptr - Expect expect_logical_and_expr(ast::Expression* lhs); + Expect expect_logical_and_expr( + const ast::Expression* lhs); /// Parses a `logical_and_expression` grammar element /// @returns the parsed expression or nullptr - Maybe logical_and_expression(); + Maybe logical_and_expression(); /// Parses the recursive part of the `logical_or_expression`, erroring on /// parse failure. /// @param lhs the left side of the expression /// @returns the parsed expression or nullptr - Expect expect_logical_or_expr(ast::Expression* lhs); + Expect expect_logical_or_expr( + const ast::Expression* lhs); /// Parses a `logical_or_expression` grammar element /// @returns the parsed expression or nullptr - Maybe logical_or_expression(); + Maybe logical_or_expression(); /// Parses a `assignment_stmt` grammar element /// @returns the parsed assignment or nullptr - Maybe assignment_stmt(); + Maybe assignment_stmt(); /// Parses one or more bracketed decoration lists. /// @return the parsed decoration list, or an empty list on error. Maybe decoration_list(); @@ -686,12 +699,12 @@ class ParserImpl { /// * `global_const_decoration` /// * `function_decoration` /// @return the parsed decoration, or nullptr. - Maybe decoration(); + Maybe decoration(); /// Parses a single decoration, reporting an error if the next token does not /// represent a decoration. /// @see #decoration for the full list of decorations this method parses. /// @return the parsed decoration, or nullptr on error. - Expect expect_decoration(); + Expect expect_decoration(); private: /// ReturnType resolves to the return type for the function or lambda F. @@ -845,19 +858,20 @@ class ParserImpl { /// Reports an error if the decoration list `list` is not empty. /// Used to ensure that all decorations are consumed. - bool expect_decorations_consumed(const ast::DecorationList& list); + bool expect_decorations_consumed(ast::DecorationList& list); - Expect expect_type_decl_pointer(Token t); - Expect expect_type_decl_atomic(Token t); - Expect expect_type_decl_vector(Token t); - Expect expect_type_decl_array(Token t, ast::DecorationList decos); - Expect expect_type_decl_matrix(Token t); + Expect expect_type_decl_pointer(Token t); + Expect expect_type_decl_atomic(Token t); + Expect expect_type_decl_vector(Token t); + Expect expect_type_decl_array(Token t, + ast::DecorationList decos); + Expect expect_type_decl_matrix(Token t); - Expect expect_type(const std::string& use); + Expect expect_type(const std::string& use); - Maybe non_block_statement(); - Maybe for_header_initializer(); - Maybe for_header_continuing(); + Maybe non_block_statement(); + Maybe for_header_initializer(); + Maybe for_header_continuing(); class MultiTokenSource; MultiTokenSource make_source_range(); diff --git a/src/reader/wgsl/parser_impl_function_decoration_list_test.cc b/src/reader/wgsl/parser_impl_function_decoration_list_test.cc index dae3f9aaf8..1a5924a2cb 100644 --- a/src/reader/wgsl/parser_impl_function_decoration_list_test.cc +++ b/src/reader/wgsl/parser_impl_function_decoration_list_test.cc @@ -34,7 +34,7 @@ TEST_F(ParserImplTest, DecorationList_Parses) { ASSERT_NE(deco_1, nullptr); ASSERT_TRUE(deco_0->Is()); - ast::Expression* x = deco_0->As()->x; + const ast::Expression* x = deco_0->As()->x; ASSERT_NE(x, nullptr); auto* x_scalar = x->As(); ASSERT_NE(x_scalar, nullptr); diff --git a/src/resolver/intrinsic_test.cc b/src/resolver/intrinsic_test.cc index 677b64a36a..0b13c3206a 100644 --- a/src/resolver/intrinsic_test.cc +++ b/src/resolver/intrinsic_test.cc @@ -224,7 +224,8 @@ class ResolverIntrinsicTest_TextureOperation /// @param dim dimensionality of the texture being sampled /// @param scalar the scalar type /// @returns a pointer to a type appropriate for the coord param - ast::Type* GetCoordsType(ast::TextureDimension dim, ast::Type* scalar) { + const ast::Type* GetCoordsType(ast::TextureDimension dim, + const ast::Type* scalar) { switch (dim) { case ast::TextureDimension::k1d: return scalar; @@ -257,7 +258,7 @@ class ResolverIntrinsicTest_TextureOperation call_params->push_back(Expr(name)); } - ast::Type* subtype(Texture type) { + const ast::Type* subtype(Texture type) { if (type == Texture::kF32) { return ty.f32(); } diff --git a/src/resolver/resolver.cc b/src/resolver/resolver.cc index 19f461ae7f..369a4ce96a 100644 --- a/src/resolver/resolver.cc +++ b/src/resolver/resolver.cc @@ -304,8 +304,7 @@ sem::Type* Resolver::Type(const ast::Type* ty) { } if (auto* t = ty->As()) { if (auto* el = Type(t->type)) { - if (auto* vector = builder_->create( - const_cast(el), t->width)) { + if (auto* vector = builder_->create(el, t->width)) { if (ValidateVector(vector, t->source)) { return vector; } @@ -315,8 +314,7 @@ sem::Type* Resolver::Type(const ast::Type* ty) { } if (auto* t = ty->As()) { if (auto* el = Type(t->type)) { - if (auto* column_type = builder_->create( - const_cast(el), t->rows)) { + if (auto* column_type = builder_->create(el, t->rows)) { if (auto* matrix = builder_->create(column_type, t->columns)) { if (ValidateMatrix(matrix, t->source)) { @@ -332,7 +330,7 @@ sem::Type* Resolver::Type(const ast::Type* ty) { } if (auto* t = ty->As()) { if (auto* el = Type(t->type)) { - auto* a = builder_->create(const_cast(el)); + auto* a = builder_->create(el); if (!ValidateAtomic(t, a)) { return nullptr; } @@ -346,8 +344,7 @@ sem::Type* Resolver::Type(const ast::Type* ty) { if (access == ast::kUndefined) { access = DefaultAccessForStorageClass(t->storage_class); } - return builder_->create(const_cast(el), - t->storage_class, access); + return builder_->create(el, t->storage_class, access); } return nullptr; } @@ -356,15 +353,13 @@ sem::Type* Resolver::Type(const ast::Type* ty) { } if (auto* t = ty->As()) { if (auto* el = Type(t->type)) { - return builder_->create( - t->dim, const_cast(el)); + return builder_->create(t->dim, el); } return nullptr; } if (auto* t = ty->As()) { if (auto* el = Type(t->type)) { - return builder_->create( - t->dim, const_cast(el)); + return builder_->create(t->dim, el); } return nullptr; } @@ -379,8 +374,8 @@ sem::Type* Resolver::Type(const ast::Type* ty) { if (!ValidateStorageTexture(t)) { return nullptr; } - return builder_->create( - t->dim, t->format, t->access, const_cast(el)); + return builder_->create(t->dim, t->format, + t->access, el); } return nullptr; } @@ -447,7 +442,7 @@ bool Resolver::ValidateStorageTexture(const ast::StorageTexture* t) { return true; } -Resolver::VariableInfo* Resolver::Variable(ast::Variable* var, +Resolver::VariableInfo* Resolver::Variable(const ast::Variable* var, VariableKind kind, uint32_t index /* = 0 */) { if (variable_to_info_.count(var)) { @@ -651,7 +646,7 @@ bool Resolver::ValidateVariableConstructor(const ast::Variable* var, return true; } -bool Resolver::GlobalVariable(ast::Variable* var) { +bool Resolver::GlobalVariable(const ast::Variable* var) { if (!ValidateNoDuplicateDefinition(var->symbol, var->source, /* check_global_scope_only */ true)) { return false; @@ -1515,118 +1510,115 @@ bool Resolver::ValidateEntryPoint(const ast::Function* func, }; // Inner lambda that is applied to a type and all of its members. - auto validate_entry_point_decorations_inner = - [&](const ast::DecorationList& decos, sem::Type* ty, Source source, - ParamOrRetType param_or_ret, bool is_struct_member) { - // Scan decorations for pipeline IO attributes. - // Check for overlap with attributes that have been seen previously. - ast::Decoration* pipeline_io_attribute = nullptr; - ast::InvariantDecoration* invariant_attribute = nullptr; - for (auto* deco : decos) { - auto is_invalid_compute_shader_decoration = false; - if (auto* builtin = deco->As()) { - if (pipeline_io_attribute) { - AddError("multiple entry point IO attributes", deco->source); - AddNote( - "previously consumed " + deco_to_str(pipeline_io_attribute), + auto validate_entry_point_decorations_inner = [&](const ast::DecorationList& + decos, + sem::Type* ty, + Source source, + ParamOrRetType param_or_ret, + bool is_struct_member) { + // Scan decorations for pipeline IO attributes. + // Check for overlap with attributes that have been seen previously. + const ast::Decoration* pipeline_io_attribute = nullptr; + const ast::InvariantDecoration* invariant_attribute = nullptr; + for (auto* deco : decos) { + auto is_invalid_compute_shader_decoration = false; + if (auto* builtin = deco->As()) { + if (pipeline_io_attribute) { + AddError("multiple entry point IO attributes", deco->source); + AddNote("previously consumed " + deco_to_str(pipeline_io_attribute), pipeline_io_attribute->source); - return false; - } - pipeline_io_attribute = deco; + return false; + } + pipeline_io_attribute = deco; - if (builtins.count(builtin->builtin)) { - AddError( - deco_to_str(builtin) + - " attribute appears multiple times as pipeline " + - (param_or_ret == ParamOrRetType::kParameter ? "input" - : "output"), - func->source); - return false; - } - - if (!ValidateBuiltinDecoration(builtin, ty, - /* is_input */ param_or_ret == - ParamOrRetType::kParameter)) { - return false; - } - builtins.emplace(builtin->builtin); - } else if (auto* location = deco->As()) { - if (pipeline_io_attribute) { - AddError("multiple entry point IO attributes", deco->source); - AddNote( - "previously consumed " + deco_to_str(pipeline_io_attribute), - pipeline_io_attribute->source); - return false; - } - pipeline_io_attribute = deco; - - bool is_input = param_or_ret == ParamOrRetType::kParameter; - if (!ValidateLocationDecoration(location, ty, locations, source, - is_input)) { - return false; - } - } else if (auto* interpolate = - deco->As()) { - if (func->PipelineStage() == ast::PipelineStage::kCompute) { - is_invalid_compute_shader_decoration = true; - } else if (!ValidateInterpolateDecoration(interpolate, ty)) { - return false; - } - } else if (auto* invariant = deco->As()) { - if (func->PipelineStage() == ast::PipelineStage::kCompute) { - is_invalid_compute_shader_decoration = true; - } - invariant_attribute = invariant; - } - if (is_invalid_compute_shader_decoration) { - std::string input_or_output = - param_or_ret == ParamOrRetType::kParameter ? "inputs" - : "output"; - AddError( - "decoration is not valid for compute shader " + input_or_output, - deco->source); - return false; - } + if (builtins.count(builtin->builtin)) { + AddError(deco_to_str(builtin) + + " attribute appears multiple times as pipeline " + + (param_or_ret == ParamOrRetType::kParameter ? "input" + : "output"), + func->source); + return false; } - if (IsValidationEnabled( - decos, ast::DisabledValidation::kEntryPointParameter)) { - if (is_struct_member && ty->Is()) { - AddError("nested structures cannot be used for entry point IO", - source); - return false; - } + if (!ValidateBuiltinDecoration( + builtin, ty, + /* is_input */ param_or_ret == ParamOrRetType::kParameter)) { + return false; + } + builtins.emplace(builtin->builtin); + } else if (auto* location = deco->As()) { + if (pipeline_io_attribute) { + AddError("multiple entry point IO attributes", deco->source); + AddNote("previously consumed " + deco_to_str(pipeline_io_attribute), + pipeline_io_attribute->source); + return false; + } + pipeline_io_attribute = deco; - if (!ty->Is() && !pipeline_io_attribute) { - std::string err = "missing entry point IO attribute"; - if (!is_struct_member) { - err += (param_or_ret == ParamOrRetType::kParameter - ? " on parameter" - : " on return type"); - } - AddError(err, source); - return false; - } + bool is_input = param_or_ret == ParamOrRetType::kParameter; + if (!ValidateLocationDecoration(location, ty, locations, source, + is_input)) { + return false; + } + } else if (auto* interpolate = deco->As()) { + if (func->PipelineStage() == ast::PipelineStage::kCompute) { + is_invalid_compute_shader_decoration = true; + } else if (!ValidateInterpolateDecoration(interpolate, ty)) { + return false; + } + } else if (auto* invariant = deco->As()) { + if (func->PipelineStage() == ast::PipelineStage::kCompute) { + is_invalid_compute_shader_decoration = true; + } + invariant_attribute = invariant; + } + if (is_invalid_compute_shader_decoration) { + std::string input_or_output = + param_or_ret == ParamOrRetType::kParameter ? "inputs" : "output"; + AddError( + "decoration is not valid for compute shader " + input_or_output, + deco->source); + return false; + } + } - if (invariant_attribute) { - bool has_position = false; - if (pipeline_io_attribute) { - if (auto* builtin = - pipeline_io_attribute->As()) { - has_position = (builtin->builtin == ast::Builtin::kPosition); - } - } - if (!has_position) { - AddError( - "invariant attribute must only be applied to a position " - "builtin", - invariant_attribute->source); - return false; - } + if (IsValidationEnabled(decos, + ast::DisabledValidation::kEntryPointParameter)) { + if (is_struct_member && ty->Is()) { + AddError("nested structures cannot be used for entry point IO", source); + return false; + } + + if (!ty->Is() && !pipeline_io_attribute) { + std::string err = "missing entry point IO attribute"; + if (!is_struct_member) { + err += + (param_or_ret == ParamOrRetType::kParameter ? " on parameter" + : " on return type"); + } + AddError(err, source); + return false; + } + + if (invariant_attribute) { + bool has_position = false; + if (pipeline_io_attribute) { + if (auto* builtin = + pipeline_io_attribute->As()) { + has_position = (builtin->builtin == ast::Builtin::kPosition); } } - return true; - }; + if (!has_position) { + AddError( + "invariant attribute must only be applied to a position " + "builtin", + invariant_attribute->source); + return false; + } + } + } + return true; + }; // Outer lambda for validating the entry point decorations for a type. auto validate_entry_point_decorations = [&](const ast::DecorationList& decos, @@ -1742,7 +1734,7 @@ bool Resolver::ValidateEntryPoint(const ast::Function* func, return true; } -bool Resolver::Function(ast::Function* func) { +bool Resolver::Function(const ast::Function* func) { auto* info = function_infos_.Create(func); if (func->IsEntryPoint()) { @@ -2019,7 +2011,7 @@ bool Resolver::ValidateStatements(const ast::StatementList& stmts) { return true; } -bool Resolver::Statement(ast::Statement* stmt) { +bool Resolver::Statement(const ast::Statement* stmt) { if (stmt->Is()) { AddError("case statement can only be used inside a switch statement", stmt->source); @@ -2129,7 +2121,7 @@ bool Resolver::Statement(ast::Statement* stmt) { return false; } -bool Resolver::CaseStatement(ast::CaseStatement* stmt) { +bool Resolver::CaseStatement(const ast::CaseStatement* stmt) { auto* sem = builder_->create( stmt->body, current_compound_statement_); builder_->Sem().Add(stmt, sem); @@ -2141,7 +2133,7 @@ bool Resolver::CaseStatement(ast::CaseStatement* stmt) { return Scope(sem, [&] { return Statements(stmt->body->statements); }); } -bool Resolver::IfStatement(ast::IfStatement* stmt) { +bool Resolver::IfStatement(const ast::IfStatement* stmt) { auto* sem = builder_->create(stmt, current_compound_statement_); builder_->Sem().Add(stmt, sem); @@ -2177,7 +2169,7 @@ bool Resolver::IfStatement(ast::IfStatement* stmt) { }); } -bool Resolver::ElseStatement(ast::ElseStatement* stmt) { +bool Resolver::ElseStatement(const ast::ElseStatement* stmt) { auto* sem = builder_->create(stmt, current_compound_statement_); builder_->Sem().Add(stmt, sem); @@ -2205,14 +2197,14 @@ bool Resolver::ElseStatement(ast::ElseStatement* stmt) { }); } -bool Resolver::BlockStatement(ast::BlockStatement* stmt) { +bool Resolver::BlockStatement(const ast::BlockStatement* stmt) { auto* sem = builder_->create( stmt->As(), current_compound_statement_); builder_->Sem().Add(stmt, sem); return Scope(sem, [&] { return Statements(stmt->statements); }); } -bool Resolver::LoopStatement(ast::LoopStatement* stmt) { +bool Resolver::LoopStatement(const ast::LoopStatement* stmt) { auto* sem = builder_->create(stmt, current_compound_statement_); builder_->Sem().Add(stmt, sem); @@ -2245,7 +2237,7 @@ bool Resolver::LoopStatement(ast::LoopStatement* stmt) { }); } -bool Resolver::ForLoopStatement(ast::ForLoopStatement* stmt) { +bool Resolver::ForLoopStatement(const ast::ForLoopStatement* stmt) { auto* sem = builder_->create( stmt, current_compound_statement_); builder_->Sem().Add(stmt, sem); @@ -2287,12 +2279,12 @@ bool Resolver::ForLoopStatement(ast::ForLoopStatement* stmt) { }); } -bool Resolver::TraverseExpressions(ast::Expression* root, - std::vector& out) { - std::vector to_visit; +bool Resolver::TraverseExpressions(const ast::Expression* root, + std::vector& out) { + std::vector to_visit; to_visit.emplace_back(root); - auto add = [&](ast::Expression* e) { + auto add = [&](const ast::Expression* e) { Mark(e); to_visit.emplace_back(e); }; @@ -2336,8 +2328,8 @@ bool Resolver::TraverseExpressions(ast::Expression* root, return true; } -bool Resolver::Expression(ast::Expression* root) { - std::vector sorted; +bool Resolver::Expression(const ast::Expression* root) { + std::vector sorted; if (!TraverseExpressions(root, sorted)) { return false; } @@ -2373,7 +2365,7 @@ bool Resolver::Expression(ast::Expression* root) { return true; } -bool Resolver::ArrayAccessor(ast::ArrayAccessorExpression* expr) { +bool Resolver::ArrayAccessor(const ast::ArrayAccessorExpression* expr) { auto* idx = expr->index; auto* res = TypeOf(expr->array); auto* parent_type = res->UnwrapRef(); @@ -2420,7 +2412,7 @@ bool Resolver::ArrayAccessor(ast::ArrayAccessorExpression* expr) { return true; } -bool Resolver::Bitcast(ast::BitcastExpression* expr) { +bool Resolver::Bitcast(const ast::BitcastExpression* expr) { auto* ty = Type(expr->type); if (!ty) { return false; @@ -2433,7 +2425,7 @@ bool Resolver::Bitcast(ast::BitcastExpression* expr) { return true; } -bool Resolver::Call(ast::CallExpression* call) { +bool Resolver::Call(const ast::CallExpression* call) { Mark(call->func); auto* ident = call->func; auto name = builder_->Symbols().NameFor(ident->symbol); @@ -2452,7 +2444,7 @@ bool Resolver::Call(ast::CallExpression* call) { return ValidateCall(call); } -bool Resolver::ValidateCall(ast::CallExpression* call) { +bool Resolver::ValidateCall(const ast::CallExpression* call) { if (TypeOf(call)->Is()) { bool is_call_statement = false; if (current_statement_) { @@ -2483,7 +2475,7 @@ bool Resolver::ValidateCall(ast::CallExpression* call) { return true; } -bool Resolver::ValidateCallStatement(ast::CallStatement* stmt) { +bool Resolver::ValidateCallStatement(const ast::CallStatement* stmt) { const sem::Type* return_type = TypeOf(stmt->expr); if (!return_type->Is()) { // https://gpuweb.github.io/gpuweb/wgsl/#function-call-statement @@ -2501,7 +2493,7 @@ bool Resolver::ValidateCallStatement(ast::CallStatement* stmt) { return true; } -bool Resolver::IntrinsicCall(ast::CallExpression* call, +bool Resolver::IntrinsicCall(const ast::CallExpression* call, sem::IntrinsicType intrinsic_type) { std::vector arg_tys; arg_tys.reserve(call->args.size()); @@ -2710,7 +2702,7 @@ bool Resolver::ValidateFunctionCall(const ast::CallExpression* call, return true; } -bool Resolver::Constructor(ast::ConstructorExpression* expr) { +bool Resolver::Constructor(const ast::ConstructorExpression* expr) { if (auto* type_ctor = expr->As()) { auto* type = Type(type_ctor->type); if (!type) { @@ -2997,7 +2989,7 @@ bool Resolver::ValidateScalarConstructor( return true; } -bool Resolver::Identifier(ast::IdentifierExpression* expr) { +bool Resolver::Identifier(const ast::IdentifierExpression* expr) { auto symbol = expr->symbol; VariableInfo* var; if (variable_stack_.get(symbol, &var)) { @@ -3061,11 +3053,11 @@ bool Resolver::Identifier(ast::IdentifierExpression* expr) { return false; } -bool Resolver::MemberAccessor(ast::MemberAccessorExpression* expr) { +bool Resolver::MemberAccessor(const ast::MemberAccessorExpression* expr) { auto* structure = TypeOf(expr->structure); auto* storage_type = structure->UnwrapRef(); - sem::Type* ret = nullptr; + const sem::Type* ret = nullptr; std::vector swizzle; if (auto* str = storage_type->As()) { @@ -3181,7 +3173,7 @@ bool Resolver::MemberAccessor(ast::MemberAccessorExpression* expr) { return true; } -bool Resolver::Binary(ast::BinaryExpression* expr) { +bool Resolver::Binary(const ast::BinaryExpression* expr) { using Bool = sem::Bool; using F32 = sem::F32; using I32 = sem::I32; @@ -3189,8 +3181,8 @@ bool Resolver::Binary(ast::BinaryExpression* expr) { using Matrix = sem::Matrix; using Vector = sem::Vector; - auto* lhs_type = const_cast(TypeOf(expr->lhs)->UnwrapRef()); - auto* rhs_type = const_cast(TypeOf(expr->rhs)->UnwrapRef()); + auto* lhs_type = TypeOf(expr->lhs)->UnwrapRef(); + auto* rhs_type = TypeOf(expr->rhs)->UnwrapRef(); auto* lhs_vec = lhs_type->As(); auto* lhs_vec_elem_type = lhs_vec ? lhs_vec->type() : nullptr; @@ -3386,7 +3378,7 @@ bool Resolver::Binary(ast::BinaryExpression* expr) { return false; } -bool Resolver::UnaryOp(ast::UnaryOpExpression* unary) { +bool Resolver::UnaryOp(const ast::UnaryOpExpression* unary) { auto* expr_type = TypeOf(unary->expr); if (!expr_type) { return false; @@ -3466,7 +3458,7 @@ bool Resolver::UnaryOp(ast::UnaryOpExpression* unary) { } bool Resolver::VariableDeclStatement(const ast::VariableDeclStatement* stmt) { - ast::Variable* var = stmt->variable; + const ast::Variable* var = stmt->variable; Mark(var); if (!ValidateNoDuplicateDefinition(var->symbol, var->source)) { @@ -3819,8 +3811,8 @@ void Resolver::CreateSemanticNodes() const { } auto* sem_func = builder_->create( - info->declaration, const_cast(info->return_type), - parameters, remap_vars(info->referenced_module_vars), + info->declaration, info->return_type, parameters, + remap_vars(info->referenced_module_vars), remap_vars(info->local_referenced_module_vars), info->return_statements, info->callsites, ancestor_entry_points[func->symbol], info->workgroup_size); @@ -3845,8 +3837,7 @@ void Resolver::CreateSemanticNodes() const { continue; } sem.Add(expr, builder_->create( - const_cast(expr), info.type, - info.statement, info.constant_value)); + expr, info.type, info.statement, info.constant_value)); } } @@ -4057,7 +4048,7 @@ bool Resolver::ValidateStructure(const sem::Struct* str) { } auto has_position = false; - ast::InvariantDecoration* invariant_attribute = nullptr; + const ast::InvariantDecoration* invariant_attribute = nullptr; for (auto* deco : member->Declaration()->decorations) { if (!deco->IsAnyOf member_map; + std::unordered_map member_map; for (auto* member : str->members) { Mark(member); @@ -4275,8 +4266,7 @@ sem::Struct* Resolver::Structure(const ast::Struct* str) { } auto* sem_member = builder_->create( - member, member->symbol, const_cast(type), - static_cast(sem_members.size()), + member, member->symbol, type, static_cast(sem_members.size()), static_cast(offset), static_cast(align), static_cast(size)); builder_->Sem().Add(member, sem_member); @@ -4360,7 +4350,7 @@ bool Resolver::ValidateReturn(const ast::ReturnStatement* ret) { return true; } -bool Resolver::Return(ast::ReturnStatement* ret) { +bool Resolver::Return(const ast::ReturnStatement* ret) { current_function_->return_statements.push_back(ret); if (auto* value = ret->value) { @@ -4443,7 +4433,7 @@ bool Resolver::ValidateSwitch(const ast::SwitchStatement* s) { return true; } -bool Resolver::SwitchStatement(ast::SwitchStatement* stmt) { +bool Resolver::SwitchStatement(const ast::SwitchStatement* stmt) { auto* sem = builder_->create(stmt, current_compound_statement_); builder_->Sem().Add(stmt, sem); @@ -4465,7 +4455,7 @@ bool Resolver::SwitchStatement(ast::SwitchStatement* stmt) { }); } -bool Resolver::Assignment(ast::AssignmentStatement* a) { +bool Resolver::Assignment(const ast::AssignmentStatement* a) { Mark(a->lhs); Mark(a->rhs); @@ -4688,7 +4678,8 @@ Resolver::VariableInfo::VariableInfo(const ast::Variable* decl, Resolver::VariableInfo::~VariableInfo() = default; -Resolver::FunctionInfo::FunctionInfo(ast::Function* decl) : declaration(decl) {} +Resolver::FunctionInfo::FunctionInfo(const ast::Function* decl) + : declaration(decl) {} Resolver::FunctionInfo::~FunctionInfo() = default; } // namespace resolver diff --git a/src/resolver/resolver.h b/src/resolver/resolver.h index 3b05f2c619..b7f839bf29 100644 --- a/src/resolver/resolver.h +++ b/src/resolver/resolver.h @@ -112,7 +112,7 @@ class Resolver { std::string const type_name; ast::StorageClass storage_class; ast::Access const access; - std::vector users; + std::vector users; sem::BindingPoint binding_point; VariableKind kind; uint32_t index = 0; // Parameter index, if kind == kParameter @@ -130,10 +130,10 @@ class Resolver { /// Structure holding semantic information about a function. /// Used to build the sem::Function nodes at the end of resolving. struct FunctionInfo { - explicit FunctionInfo(ast::Function* decl); + explicit FunctionInfo(const ast::Function* decl); ~FunctionInfo(); - ast::Function* const declaration; + const ast::Function* const declaration; std::vector parameters; UniqueVector referenced_module_vars; UniqueVector local_referenced_module_vars; @@ -192,7 +192,7 @@ class Resolver { } ast::BlockStatement const* const block; - Type const type; + const Type type; BlockInfo* const parent; std::vector decls; @@ -235,31 +235,31 @@ class Resolver { // AST and Type traversal methods // Each return true on success, false on failure. - bool ArrayAccessor(ast::ArrayAccessorExpression*); - bool Assignment(ast::AssignmentStatement* a); - bool Binary(ast::BinaryExpression*); - bool Bitcast(ast::BitcastExpression*); - bool BlockStatement(ast::BlockStatement*); - bool Call(ast::CallExpression*); - bool CaseStatement(ast::CaseStatement*); - bool Constructor(ast::ConstructorExpression*); - bool ElseStatement(ast::ElseStatement*); - bool Expression(ast::Expression*); - bool ForLoopStatement(ast::ForLoopStatement*); - bool Function(ast::Function*); + bool ArrayAccessor(const ast::ArrayAccessorExpression*); + bool Assignment(const ast::AssignmentStatement* a); + bool Binary(const ast::BinaryExpression*); + bool Bitcast(const ast::BitcastExpression*); + bool BlockStatement(const ast::BlockStatement*); + bool Call(const ast::CallExpression*); + bool CaseStatement(const ast::CaseStatement*); + bool Constructor(const ast::ConstructorExpression*); + bool ElseStatement(const ast::ElseStatement*); + bool Expression(const ast::Expression*); + bool ForLoopStatement(const ast::ForLoopStatement*); + bool Function(const ast::Function*); bool FunctionCall(const ast::CallExpression* call); - bool GlobalVariable(ast::Variable* var); - bool Identifier(ast::IdentifierExpression*); - bool IfStatement(ast::IfStatement*); - bool IntrinsicCall(ast::CallExpression*, sem::IntrinsicType); - bool LoopStatement(ast::LoopStatement*); - bool MemberAccessor(ast::MemberAccessorExpression*); - bool Parameter(ast::Variable* param); - bool Return(ast::ReturnStatement* ret); - bool Statement(ast::Statement*); + bool GlobalVariable(const ast::Variable* var); + bool Identifier(const ast::IdentifierExpression*); + bool IfStatement(const ast::IfStatement*); + bool IntrinsicCall(const ast::CallExpression*, sem::IntrinsicType); + bool LoopStatement(const ast::LoopStatement*); + bool MemberAccessor(const ast::MemberAccessorExpression*); + bool Parameter(const ast::Variable* param); + bool Return(const ast::ReturnStatement* ret); + bool Statement(const ast::Statement*); bool Statements(const ast::StatementList&); - bool SwitchStatement(ast::SwitchStatement* s); - bool UnaryOp(ast::UnaryOpExpression*); + bool SwitchStatement(const ast::SwitchStatement* s); + bool UnaryOp(const ast::UnaryOpExpression*); bool VariableDeclStatement(const ast::VariableDeclStatement*); /// Performs a depth-first traversal of the expression nodes from `root`, @@ -268,8 +268,8 @@ class Resolver { /// @param out the ordered list of visited expression nodes, starting with the /// root node, and ending with leaf nodes /// @return true on success, false on error - bool TraverseExpressions(ast::Expression* root, - std::vector& out); + bool TraverseExpressions(const ast::Expression* root, + std::vector& out); // AST and Type validation methods // Each return true on success, false on failure. @@ -284,8 +284,8 @@ class Resolver { bool ValidateBuiltinDecoration(const ast::BuiltinDecoration* deco, const sem::Type* storage_type, const bool is_input); - bool ValidateCall(ast::CallExpression* call); - bool ValidateCallStatement(ast::CallStatement* stmt); + bool ValidateCall(const ast::CallExpression* call); + bool ValidateCallStatement(const ast::CallStatement* stmt); bool ValidateEntryPoint(const ast::Function* func, const FunctionInfo* info); bool ValidateFunction(const ast::Function* func, const FunctionInfo* info); bool ValidateFunctionCall(const ast::CallExpression* call, @@ -371,7 +371,7 @@ class Resolver { /// @param var the variable to create or return the `VariableInfo` for /// @param kind what kind of variable we are declaring /// @param index the index of the parameter, if this variable is a parameter - VariableInfo* Variable(ast::Variable* var, + VariableInfo* Variable(const ast::Variable* var, VariableKind kind, uint32_t index = 0); diff --git a/src/resolver/resolver_test.cc b/src/resolver/resolver_test.cc index 776cf4c15a..94bc94a175 100644 --- a/src/resolver/resolver_test.cc +++ b/src/resolver/resolver_test.cc @@ -1641,9 +1641,9 @@ TEST_P(Expr_Binary_Test_Invalid_VectorMatrixMultiply, All) { uint32_t mat_rows = std::get<2>(GetParam()); uint32_t mat_cols = std::get<3>(GetParam()); - ast::Type* lhs_type; - ast::Type* rhs_type; - sem::Type* result_type; + const ast::Type* lhs_type = nullptr; + const ast::Type* rhs_type = nullptr; + const sem::Type* result_type = nullptr; bool is_valid_expr; if (vec_by_mat) { diff --git a/src/resolver/resolver_test_helper.h b/src/resolver/resolver_test_helper.h index a0a17c2f2f..9f5ff52801 100644 --- a/src/resolver/resolver_test_helper.h +++ b/src/resolver/resolver_test_helper.h @@ -45,16 +45,16 @@ class TestHelper : public ProgramBuilder { /// @param expr the ast::Expression /// @return the ast::Statement of the ast::Expression, or nullptr if the /// expression is not owned by a statement. - const ast::Statement* StmtOf(ast::Expression* expr) { + const ast::Statement* StmtOf(const ast::Expression* expr) { auto* sem_stmt = Sem().Get(expr)->Stmt(); return sem_stmt ? sem_stmt->Declaration() : nullptr; } /// Returns the BlockStatement that holds the given statement. - /// @param stmt the ast::Statment + /// @param stmt the ast::Statement /// @return the ast::BlockStatement that holds the ast::Statement, or nullptr /// if the statement is not owned by a BlockStatement. - const ast::BlockStatement* BlockOf(ast::Statement* stmt) { + const ast::BlockStatement* BlockOf(const ast::Statement* stmt) { auto* sem_stmt = Sem().Get(stmt); return sem_stmt ? sem_stmt->Block()->Declaration() : nullptr; } @@ -63,7 +63,7 @@ class TestHelper : public ProgramBuilder { /// @param expr the ast::Expression /// @return the ast::Statement of the ast::Expression, or nullptr if the /// expression is not indirectly owned by a BlockStatement. - const ast::BlockStatement* BlockOf(ast::Expression* expr) { + const ast::BlockStatement* BlockOf(const ast::Expression* expr) { auto* sem_stmt = Sem().Get(expr)->Stmt(); return sem_stmt ? sem_stmt->Block()->Declaration() : nullptr; } @@ -72,7 +72,7 @@ class TestHelper : public ProgramBuilder { /// @param expr the identifier expression /// @return the resolved sem::Variable of the identifier, or nullptr if /// the expression did not resolve to a variable. - const sem::Variable* VarOf(ast::Expression* expr) { + const sem::Variable* VarOf(const ast::Expression* expr) { auto* sem_ident = Sem().Get(expr); auto* var_user = sem_ident ? sem_ident->As() : nullptr; return var_user ? var_user->Variable() : nullptr; @@ -82,8 +82,8 @@ class TestHelper : public ProgramBuilder { /// @param var the variable to check /// @param expected_users the expected users of the variable /// @return true if all users are as expected - bool CheckVarUsers(ast::Variable* var, - std::vector&& expected_users) { + bool CheckVarUsers(const ast::Variable* var, + std::vector&& expected_users) { auto& var_users = Sem().Get(var)->Users(); if (var_users.size() != expected_users.size()) { return false; @@ -171,10 +171,10 @@ using alias2 = alias; template using alias3 = alias; -using ast_type_func_ptr = ast::Type* (*)(ProgramBuilder& b); -using ast_expr_func_ptr = ast::Expression* (*)(ProgramBuilder& b, - int elem_value); -using sem_type_func_ptr = sem::Type* (*)(ProgramBuilder& b); +using ast_type_func_ptr = const ast::Type* (*)(ProgramBuilder& b); +using ast_expr_func_ptr = const ast::Expression* (*)(ProgramBuilder& b, + int elem_value); +using sem_type_func_ptr = const sem::Type* (*)(ProgramBuilder& b); template struct DataType {}; @@ -187,16 +187,16 @@ struct DataType { /// @param b the ProgramBuilder /// @return a new AST bool type - static inline ast::Type* AST(ProgramBuilder& b) { return b.ty.bool_(); } + static inline const ast::Type* AST(ProgramBuilder& b) { return b.ty.bool_(); } /// @param b the ProgramBuilder /// @return the semantic bool type - static inline sem::Type* Sem(ProgramBuilder& b) { + static inline const sem::Type* Sem(ProgramBuilder& b) { return b.create(); } /// @param b the ProgramBuilder /// @param elem_value the b /// @return a new AST expression of the bool type - static inline ast::Expression* Expr(ProgramBuilder& b, int elem_value) { + static inline const ast::Expression* Expr(ProgramBuilder& b, int elem_value) { return b.Expr(elem_value == 0); } }; @@ -209,16 +209,16 @@ struct DataType { /// @param b the ProgramBuilder /// @return a new AST i32 type - static inline ast::Type* AST(ProgramBuilder& b) { return b.ty.i32(); } + static inline const ast::Type* AST(ProgramBuilder& b) { return b.ty.i32(); } /// @param b the ProgramBuilder /// @return the semantic i32 type - static inline sem::Type* Sem(ProgramBuilder& b) { + static inline const sem::Type* Sem(ProgramBuilder& b) { return b.create(); } /// @param b the ProgramBuilder /// @param elem_value the value i32 will be initialized with /// @return a new AST i32 literal value expression - static inline ast::Expression* Expr(ProgramBuilder& b, int elem_value) { + static inline const ast::Expression* Expr(ProgramBuilder& b, int elem_value) { return b.Expr(static_cast(elem_value)); } }; @@ -231,16 +231,16 @@ struct DataType { /// @param b the ProgramBuilder /// @return a new AST u32 type - static inline ast::Type* AST(ProgramBuilder& b) { return b.ty.u32(); } + static inline const ast::Type* AST(ProgramBuilder& b) { return b.ty.u32(); } /// @param b the ProgramBuilder /// @return the semantic u32 type - static inline sem::Type* Sem(ProgramBuilder& b) { + static inline const sem::Type* Sem(ProgramBuilder& b) { return b.create(); } /// @param b the ProgramBuilder /// @param elem_value the value u32 will be initialized with /// @return a new AST u32 literal value expression - static inline ast::Expression* Expr(ProgramBuilder& b, int elem_value) { + static inline const ast::Expression* Expr(ProgramBuilder& b, int elem_value) { return b.Expr(static_cast(elem_value)); } }; @@ -253,16 +253,16 @@ struct DataType { /// @param b the ProgramBuilder /// @return a new AST f32 type - static inline ast::Type* AST(ProgramBuilder& b) { return b.ty.f32(); } + static inline const ast::Type* AST(ProgramBuilder& b) { return b.ty.f32(); } /// @param b the ProgramBuilder /// @return the semantic f32 type - static inline sem::Type* Sem(ProgramBuilder& b) { + static inline const sem::Type* Sem(ProgramBuilder& b) { return b.create(); } /// @param b the ProgramBuilder /// @param elem_value the value f32 will be initialized with /// @return a new AST f32 literal value expression - static inline ast::Expression* Expr(ProgramBuilder& b, int elem_value) { + static inline const ast::Expression* Expr(ProgramBuilder& b, int elem_value) { return b.Expr(static_cast(elem_value)); } }; @@ -275,19 +275,19 @@ struct DataType> { /// @param b the ProgramBuilder /// @return a new AST vector type - static inline ast::Type* AST(ProgramBuilder& b) { + static inline const ast::Type* AST(ProgramBuilder& b) { return b.ty.vec(DataType::AST(b), N); } /// @param b the ProgramBuilder /// @return the semantic vector type - static inline sem::Type* Sem(ProgramBuilder& b) { + static inline const sem::Type* Sem(ProgramBuilder& b) { return b.create(DataType::Sem(b), N); } /// @param b the ProgramBuilder /// @param elem_value the value each element in the vector will be initialized /// with /// @return a new AST vector value expression - static inline ast::Expression* Expr(ProgramBuilder& b, int elem_value) { + static inline const ast::Expression* Expr(ProgramBuilder& b, int elem_value) { return b.Construct(AST(b), ExprArgs(b, elem_value)); } @@ -312,12 +312,12 @@ struct DataType> { /// @param b the ProgramBuilder /// @return a new AST matrix type - static inline ast::Type* AST(ProgramBuilder& b) { + static inline const ast::Type* AST(ProgramBuilder& b) { return b.ty.mat(DataType::AST(b), N, M); } /// @param b the ProgramBuilder /// @return the semantic matrix type - static inline sem::Type* Sem(ProgramBuilder& b) { + static inline const sem::Type* Sem(ProgramBuilder& b) { auto* column_type = b.create(DataType::Sem(b), M); return b.create(column_type, N); } @@ -325,7 +325,7 @@ struct DataType> { /// @param elem_value the value each element in the matrix will be initialized /// with /// @return a new AST matrix value expression - static inline ast::Expression* Expr(ProgramBuilder& b, int elem_value) { + static inline const ast::Expression* Expr(ProgramBuilder& b, int elem_value) { return b.Construct(AST(b), ExprArgs(b, elem_value)); } @@ -350,7 +350,7 @@ struct DataType> { /// @param b the ProgramBuilder /// @return a new AST alias type - static inline ast::Type* AST(ProgramBuilder& b) { + static inline const ast::Type* AST(ProgramBuilder& b) { auto name = b.Symbols().Register("alias_" + std::to_string(ID)); if (!b.AST().LookupType(name)) { auto* type = DataType::AST(b); @@ -360,7 +360,7 @@ struct DataType> { } /// @param b the ProgramBuilder /// @return the semantic aliased type - static inline sem::Type* Sem(ProgramBuilder& b) { + static inline const sem::Type* Sem(ProgramBuilder& b) { return DataType::Sem(b); } @@ -368,7 +368,7 @@ struct DataType> { /// @param elem_value the value nested elements will be initialized with /// @return a new AST expression of the alias type template - static inline traits::EnableIf Expr( + static inline traits::EnableIf Expr( ProgramBuilder& b, int elem_value) { // Cast @@ -379,7 +379,7 @@ struct DataType> { /// @param elem_value the value nested elements will be initialized with /// @return a new AST expression of the alias type template - static inline traits::EnableIf Expr( + static inline traits::EnableIf Expr( ProgramBuilder& b, int elem_value) { // Construct @@ -395,19 +395,19 @@ struct DataType> { /// @param b the ProgramBuilder /// @return a new AST array type - static inline ast::Type* AST(ProgramBuilder& b) { + static inline const ast::Type* AST(ProgramBuilder& b) { return b.ty.array(DataType::AST(b), N); } /// @param b the ProgramBuilder /// @return the semantic array type - static inline sem::Type* Sem(ProgramBuilder& b) { + static inline const sem::Type* Sem(ProgramBuilder& b) { return b.create(DataType::Sem(b), N); } /// @param b the ProgramBuilder /// @param elem_value the value each element in the array will be initialized /// with /// @return a new AST array value expression - static inline ast::Expression* Expr(ProgramBuilder& b, int elem_value) { + static inline const ast::Expression* Expr(ProgramBuilder& b, int elem_value) { return b.Construct(AST(b), ExprArgs(b, elem_value)); } diff --git a/src/scope_stack_test.cc b/src/scope_stack_test.cc index 9c6c0a54c4..0006347933 100644 --- a/src/scope_stack_test.cc +++ b/src/scope_stack_test.cc @@ -33,10 +33,10 @@ TEST_F(ScopeStackTest, Global) { TEST_F(ScopeStackTest, Global_SetWithPointer) { auto* v = Var("my_var", ty.f32(), ast::StorageClass::kNone); - ScopeStack s; + ScopeStack s; s.set_global(v->symbol, v); - ast::Variable* v2 = nullptr; + const ast::Variable* v2 = nullptr; EXPECT_TRUE(s.get(v->symbol, &v2)); EXPECT_EQ(v2->symbol, v->symbol); } diff --git a/src/sem/array.h b/src/sem/array.h index 8ae7a97550..b1992ffb35 100644 --- a/src/sem/array.h +++ b/src/sem/array.h @@ -99,12 +99,12 @@ class Array : public Castable { private: Type const* const element_; - uint32_t const count_; - uint32_t const align_; - uint32_t const size_; - uint32_t const stride_; - uint32_t const implicit_stride_; - bool const constructible_; + const uint32_t count_; + const uint32_t align_; + const uint32_t size_; + const uint32_t stride_; + const uint32_t implicit_stride_; + const bool constructible_; }; } // namespace sem diff --git a/src/sem/block_statement.cc b/src/sem/block_statement.cc index 8022cff2f1..c0dbd68e8a 100644 --- a/src/sem/block_statement.cc +++ b/src/sem/block_statement.cc @@ -35,7 +35,7 @@ const ast::BlockStatement* BlockStatement::Declaration() const { return Base::Declaration()->As(); } -void BlockStatement::AddDecl(ast::Variable* var) { +void BlockStatement::AddDecl(const ast::Variable* var) { decls_.push_back(var); } diff --git a/src/sem/block_statement.h b/src/sem/block_statement.h index 7a99feee5c..6ab6403c0f 100644 --- a/src/sem/block_statement.h +++ b/src/sem/block_statement.h @@ -55,7 +55,7 @@ class BlockStatement : public Castable { /// Associates a declaration with this block. /// @param var a variable declaration to be added to the block - void AddDecl(ast::Variable* var); + void AddDecl(const ast::Variable* var); private: std::vector decls_; diff --git a/src/sem/call_target.cc b/src/sem/call_target.cc index 9ea38bf992..b2eb89efec 100644 --- a/src/sem/call_target.cc +++ b/src/sem/call_target.cc @@ -22,7 +22,8 @@ TINT_INSTANTIATE_TYPEINFO(tint::sem::CallTarget); namespace tint { namespace sem { -CallTarget::CallTarget(sem::Type* return_type, const ParameterList& parameters) +CallTarget::CallTarget(const sem::Type* return_type, + const ParameterList& parameters) : signature_{return_type, parameters} { TINT_ASSERT(Semantic, return_type); } @@ -30,7 +31,7 @@ CallTarget::CallTarget(sem::Type* return_type, const ParameterList& parameters) CallTarget::CallTarget(const CallTarget&) = default; CallTarget::~CallTarget() = default; -CallTargetSignature::CallTargetSignature(sem::Type* ret_ty, +CallTargetSignature::CallTargetSignature(const sem::Type* ret_ty, const ParameterList& params) : return_type(ret_ty), parameters(params) {} CallTargetSignature::CallTargetSignature(const CallTargetSignature&) = default; diff --git a/src/sem/call_target.h b/src/sem/call_target.h index c0e7b8ec8f..3ed05abc81 100644 --- a/src/sem/call_target.h +++ b/src/sem/call_target.h @@ -32,7 +32,7 @@ struct CallTargetSignature { /// Constructor /// @param ret_ty the call target return type /// @param params the call target parameters - CallTargetSignature(sem::Type* ret_ty, const ParameterList& params); + CallTargetSignature(const sem::Type* ret_ty, const ParameterList& params); /// Copy constructor CallTargetSignature(const CallTargetSignature&); @@ -41,9 +41,9 @@ struct CallTargetSignature { ~CallTargetSignature(); /// The type of the call target return value - sem::Type* const return_type = nullptr; + const sem::Type* const return_type = nullptr; /// The parameters of the call target - ParameterList const parameters; + const ParameterList parameters; /// Equality operator /// @param other the signature to compare this to @@ -62,7 +62,7 @@ class CallTarget : public Castable { /// Constructor /// @param return_type the return type of the call target /// @param parameters the parameters for the call target - CallTarget(sem::Type* return_type, const ParameterList& parameters); + CallTarget(const sem::Type* return_type, const ParameterList& parameters); /// Copy constructor CallTarget(const CallTarget&); @@ -71,7 +71,7 @@ class CallTarget : public Castable { ~CallTarget() override; /// @return the return type of the call target - sem::Type* ReturnType() const { return signature_.return_type; } + const sem::Type* ReturnType() const { return signature_.return_type; } /// @return the parameters of the call target const ParameterList& Parameters() const { return signature_.parameters; } diff --git a/src/sem/expression.cc b/src/sem/expression.cc index f6ce44be1f..6cb318a69d 100644 --- a/src/sem/expression.cc +++ b/src/sem/expression.cc @@ -23,7 +23,7 @@ namespace sem { Expression::Expression(const ast::Expression* declaration, const sem::Type* type, - Statement* statement, + const Statement* statement, Constant constant) : declaration_(declaration), type_(type), diff --git a/src/sem/expression.h b/src/sem/expression.h index ccb7dd8ef0..ba001cb15b 100644 --- a/src/sem/expression.h +++ b/src/sem/expression.h @@ -35,31 +35,29 @@ class Expression : public Castable { /// @param constant the constant value of the expression. May be invalid Expression(const ast::Expression* declaration, const sem::Type* type, - Statement* statement, + const Statement* statement, Constant constant); /// Destructor ~Expression() override; /// @return the resolved type of the expression - sem::Type* Type() const { return const_cast(type_); } + const sem::Type* Type() const { return type_; } /// @return the statement that owns this expression - Statement* Stmt() const { return statement_; } + const Statement* Stmt() const { return statement_; } /// @return the constant value of this expression const Constant& ConstantValue() const { return constant_; } /// @returns the AST node - ast::Expression* Declaration() const { - return const_cast(declaration_); - } + const ast::Expression* Declaration() const { return declaration_; } private: - const ast::Expression* declaration_; + const ast::Expression* const declaration_; const sem::Type* const type_; - Statement* const statement_; - Constant const constant_; + const Statement* const statement_; + const Constant constant_; }; } // namespace sem diff --git a/src/sem/function.cc b/src/sem/function.cc index 0743cb4ea7..ac81698b7b 100644 --- a/src/sem/function.cc +++ b/src/sem/function.cc @@ -28,7 +28,7 @@ TINT_INSTANTIATE_TYPEINFO(tint::sem::Function); namespace tint { namespace sem { -Function::Function(ast::Function* declaration, +Function::Function(const ast::Function* declaration, Type* return_type, std::vector parameters, std::vector referenced_module_vars, @@ -52,9 +52,9 @@ Function::Function(ast::Function* declaration, Function::~Function() = default; -std::vector> +std::vector> Function::ReferencedLocationVariables() const { - std::vector> ret; + std::vector> ret; for (auto* var : ReferencedModuleVariables()) { for (auto* deco : var->Declaration()->decorations) { @@ -97,9 +97,9 @@ Function::VariableBindings Function::ReferencedStorageBufferVariables() const { return ret; } -std::vector> +std::vector> Function::ReferencedBuiltinVariables() const { - std::vector> ret; + std::vector> ret; for (auto* var : ReferencedModuleVariables()) { for (auto* deco : var->Declaration()->decorations) { diff --git a/src/sem/function.h b/src/sem/function.h index cd0c014f51..18e46177e7 100644 --- a/src/sem/function.h +++ b/src/sem/function.h @@ -66,7 +66,7 @@ class Function : public Castable { /// @param callsites the callsites of the function /// @param ancestor_entry_points the ancestor entry points /// @param workgroup_size the workgroup size - Function(ast::Function* declaration, + Function(const ast::Function* declaration, Type* return_type, std::vector parameters, std::vector referenced_module_vars, @@ -80,7 +80,7 @@ class Function : public Castable { ~Function() override; /// @returns the ast::Function declaration - ast::Function* Declaration() const { return declaration_; } + const ast::Function* Declaration() const { return declaration_; } /// Note: If this function calls other functions, the return will also include /// all of the referenced variables from the callees. @@ -106,12 +106,12 @@ class Function : public Castable { } /// Retrieves any referenced location variables /// @returns the pair. - std::vector> + std::vector> ReferencedLocationVariables() const; /// Retrieves any referenced builtin variables /// @returns the pair. - std::vector> + std::vector> ReferencedBuiltinVariables() const; /// Retrieves any referenced uniform variables. Note, the variables must be @@ -174,7 +174,7 @@ class Function : public Castable { VariableBindings ReferencedSampledTextureVariablesImpl( bool multisampled) const; - ast::Function* const declaration_; + const ast::Function* const declaration_; std::vector const referenced_module_vars_; std::vector const local_referenced_module_vars_; std::vector const return_statements_; diff --git a/src/sem/intrinsic.cc b/src/sem/intrinsic.cc index 0b1988edf1..13ccd21a79 100644 --- a/src/sem/intrinsic.cc +++ b/src/sem/intrinsic.cc @@ -102,7 +102,7 @@ bool IsAtomicIntrinsic(IntrinsicType i) { } Intrinsic::Intrinsic(IntrinsicType type, - sem::Type* return_type, + const sem::Type* return_type, std::vector parameters, PipelineStageSet supported_stages, bool is_deprecated) diff --git a/src/sem/intrinsic.h b/src/sem/intrinsic.h index 27f3136334..fb55b48586 100644 --- a/src/sem/intrinsic.h +++ b/src/sem/intrinsic.h @@ -88,7 +88,7 @@ class Intrinsic : public Castable { /// @param is_deprecated true if the particular overload is considered /// deprecated Intrinsic(IntrinsicType type, - sem::Type* return_type, + const sem::Type* return_type, std::vector parameters, PipelineStageSet supported_stages, bool is_deprecated); @@ -140,9 +140,9 @@ class Intrinsic : public Castable { bool IsAtomic() const; private: - IntrinsicType const type_; - PipelineStageSet const supported_stages_; - bool const is_deprecated_; + const IntrinsicType type_; + const PipelineStageSet supported_stages_; + const bool is_deprecated_; }; } // namespace sem diff --git a/src/sem/matrix_type.cc b/src/sem/matrix_type.cc index 34de689216..14bd0ba2b1 100644 --- a/src/sem/matrix_type.cc +++ b/src/sem/matrix_type.cc @@ -22,7 +22,7 @@ TINT_INSTANTIATE_TYPEINFO(tint::sem::Matrix); namespace tint { namespace sem { -Matrix::Matrix(Vector* column_type, uint32_t columns) +Matrix::Matrix(const Vector* column_type, uint32_t columns) : subtype_(column_type->type()), column_type_(column_type), rows_(column_type->Width()), diff --git a/src/sem/matrix_type.h b/src/sem/matrix_type.h index 9c69b798e1..896eb3bc91 100644 --- a/src/sem/matrix_type.h +++ b/src/sem/matrix_type.h @@ -31,20 +31,20 @@ class Matrix : public Castable { /// Constructor /// @param column_type the type of a column of the matrix /// @param columns the number of columns in the matrix - Matrix(Vector* column_type, uint32_t columns); + Matrix(const Vector* column_type, uint32_t columns); /// Move constructor Matrix(Matrix&&); ~Matrix() override; /// @returns the type of the matrix - Type* type() const { return subtype_; } + const Type* type() const { return subtype_; } /// @returns the number of rows in the matrix uint32_t rows() const { return rows_; } /// @returns the number of columns in the matrix uint32_t columns() const { return columns_; } /// @returns the column-vector type of the matrix - Vector* ColumnType() const { return column_type_; } + const Vector* ColumnType() const { return column_type_; } /// @returns the name for this type std::string type_name() const override; @@ -69,10 +69,10 @@ class Matrix : public Castable { uint32_t ColumnStride() const; private: - Type* const subtype_; - Vector* const column_type_; - uint32_t const rows_; - uint32_t const columns_; + const Type* const subtype_; + const Vector* const column_type_; + const uint32_t rows_; + const uint32_t columns_; }; } // namespace sem diff --git a/src/sem/member_accessor_expression.cc b/src/sem/member_accessor_expression.cc index 309785bba3..d8dcd69b86 100644 --- a/src/sem/member_accessor_expression.cc +++ b/src/sem/member_accessor_expression.cc @@ -25,25 +25,25 @@ namespace tint { namespace sem { MemberAccessorExpression::MemberAccessorExpression( - ast::MemberAccessorExpression* declaration, + const ast::MemberAccessorExpression* declaration, const sem::Type* type, - Statement* statement) + const Statement* statement) : Base(declaration, type, statement, Constant{}) {} MemberAccessorExpression::~MemberAccessorExpression() = default; StructMemberAccess::StructMemberAccess( - ast::MemberAccessorExpression* declaration, + const ast::MemberAccessorExpression* declaration, const sem::Type* type, - Statement* statement, + const Statement* statement, const StructMember* member) : Base(declaration, type, statement), member_(member) {} StructMemberAccess::~StructMemberAccess() = default; -Swizzle::Swizzle(ast::MemberAccessorExpression* declaration, +Swizzle::Swizzle(const ast::MemberAccessorExpression* declaration, const sem::Type* type, - Statement* statement, + const Statement* statement, std::vector indices) : Base(declaration, type, statement), indices_(std::move(indices)) {} diff --git a/src/sem/member_accessor_expression.h b/src/sem/member_accessor_expression.h index e298024ad1..fcc6c6fbc3 100644 --- a/src/sem/member_accessor_expression.h +++ b/src/sem/member_accessor_expression.h @@ -41,9 +41,9 @@ class MemberAccessorExpression /// @param declaration the AST node /// @param type the resolved type of the expression /// @param statement the statement that owns this expression - MemberAccessorExpression(ast::MemberAccessorExpression* declaration, + MemberAccessorExpression(const ast::MemberAccessorExpression* declaration, const sem::Type* type, - Statement* statement); + const Statement* statement); /// Destructor ~MemberAccessorExpression() override; @@ -60,9 +60,9 @@ class StructMemberAccess /// @param type the resolved type of the expression /// @param statement the statement that owns this expression /// @param member the structure member - StructMemberAccess(ast::MemberAccessorExpression* declaration, + StructMemberAccess(const ast::MemberAccessorExpression* declaration, const sem::Type* type, - Statement* statement, + const Statement* statement, const StructMember* member); /// Destructor @@ -84,9 +84,9 @@ class Swizzle : public Castable { /// @param type the resolved type of the expression /// @param statement the statement that /// @param indices the swizzle indices - Swizzle(ast::MemberAccessorExpression* declaration, + Swizzle(const ast::MemberAccessorExpression* declaration, const sem::Type* type, - Statement* statement, + const Statement* statement, std::vector indices); /// Destructor diff --git a/src/sem/multisampled_texture_type.h b/src/sem/multisampled_texture_type.h index 715d6aa808..f67ee1ab5d 100644 --- a/src/sem/multisampled_texture_type.h +++ b/src/sem/multisampled_texture_type.h @@ -34,7 +34,7 @@ class MultisampledTexture : public Castable { ~MultisampledTexture() override; /// @returns the subtype of the sampled texture - Type* type() const { return const_cast(type_); } + const Type* type() const { return type_; } /// @returns the name for this type std::string type_name() const override; diff --git a/src/sem/struct.cc b/src/sem/struct.cc index 9238ec9eaa..d764115409 100644 --- a/src/sem/struct.cc +++ b/src/sem/struct.cc @@ -78,7 +78,7 @@ bool Struct::IsConstructible() const { return constructible_; } -StructMember::StructMember(ast::StructMember* declaration, +StructMember::StructMember(const ast::StructMember* declaration, Symbol name, sem::Type* type, uint32_t index, diff --git a/src/sem/struct.h b/src/sem/struct.h index f6014aa276..3e3b62d502 100644 --- a/src/sem/struct.h +++ b/src/sem/struct.h @@ -161,11 +161,11 @@ class Struct : public Castable { uint64_t LargestMemberBaseAlignment(MemoryLayout mem_layout) const; ast::Struct const* const declaration_; - Symbol const name_; - StructMemberList const members_; - uint32_t const align_; - uint32_t const size_; - uint32_t const size_no_padding_; + const Symbol name_; + const StructMemberList members_; + const uint32_t align_; + const uint32_t size_; + const uint32_t size_no_padding_; std::unordered_set storage_class_usage_; std::unordered_set pipeline_stage_uses_; bool constructible_; @@ -182,7 +182,7 @@ class StructMember : public Castable { /// @param offset the byte offset from the base of the structure /// @param align the byte alignment of the member /// @param size the byte size of the member - StructMember(ast::StructMember* declaration, + StructMember(const ast::StructMember* declaration, Symbol name, sem::Type* type, uint32_t index, @@ -194,7 +194,7 @@ class StructMember : public Castable { ~StructMember() override; /// @returns the AST declaration node - ast::StructMember* Declaration() const { return declaration_; } + const ast::StructMember* Declaration() const { return declaration_; } /// @returns the name of the structure Symbol Name() const { return name_; } @@ -215,13 +215,13 @@ class StructMember : public Castable { uint32_t Size() const { return size_; } private: - ast::StructMember* const declaration_; - Symbol const name_; + const ast::StructMember* const declaration_; + const Symbol name_; sem::Type* const type_; - uint32_t const index_; - uint32_t const offset_; - uint32_t const align_; - uint32_t const size_; + const uint32_t index_; + const uint32_t offset_; + const uint32_t align_; + const uint32_t size_; }; } // namespace sem diff --git a/src/sem/variable.cc b/src/sem/variable.cc index bc783a1c59..a1e0eb8e2a 100644 --- a/src/sem/variable.cc +++ b/src/sem/variable.cc @@ -80,7 +80,7 @@ Parameter::Parameter(const ast::Variable* declaration, Parameter::~Parameter() = default; -VariableUser::VariableUser(ast::IdentifierExpression* declaration, +VariableUser::VariableUser(const ast::IdentifierExpression* declaration, const sem::Type* type, Statement* statement, sem::Variable* variable, diff --git a/src/sem/variable.h b/src/sem/variable.h index 5bbe3e1ab5..782fda926f 100644 --- a/src/sem/variable.h +++ b/src/sem/variable.h @@ -59,7 +59,7 @@ class Variable : public Castable { const ast::Variable* Declaration() const { return declaration_; } /// @returns the canonical type for the variable - sem::Type* Type() const { return const_cast(type_); } + const sem::Type* Type() const { return type_; } /// @returns the storage class for the variable ast::StorageClass StorageClass() const { return storage_class_; } @@ -135,7 +135,7 @@ class GlobalVariable : public Castable { private: sem::BindingPoint binding_point_; - bool const is_pipeline_constant_; + const bool is_pipeline_constant_; uint16_t const constant_id_ = 0; }; @@ -172,8 +172,8 @@ class Parameter : public Castable { void SetOwner(CallTarget const* owner) { owner_ = owner; } private: - uint32_t const index_; - ParameterUsage const usage_; + const uint32_t index_; + const ParameterUsage usage_; CallTarget const* owner_; }; @@ -190,7 +190,7 @@ class VariableUser : public Castable { /// @param statement the statement that owns this expression /// @param variable the semantic variable /// @param constant_value the constant value for the variable. May be invalid - VariableUser(ast::IdentifierExpression* declaration, + VariableUser(const ast::IdentifierExpression* declaration, const sem::Type* type, Statement* statement, sem::Variable* variable, diff --git a/src/sem/vector_type.h b/src/sem/vector_type.h index 91633a76c3..3f3b714d29 100644 --- a/src/sem/vector_type.h +++ b/src/sem/vector_type.h @@ -34,7 +34,7 @@ class Vector : public Castable { ~Vector() override; /// @returns the type of the vector elements - Type* type() const { return const_cast(subtype_); } + const Type* type() const { return subtype_; } /// @returns the name for th type std::string type_name() const override; @@ -68,7 +68,7 @@ class Vector : public Castable { private: Type const* const subtype_; - uint32_t const width_; + const uint32_t width_; }; } // namespace sem diff --git a/src/transform/array_length_from_uniform.cc b/src/transform/array_length_from_uniform.cc index 70ba5fc31f..65f8f0ad3e 100644 --- a/src/transform/array_length_from_uniform.cc +++ b/src/transform/array_length_from_uniform.cc @@ -64,7 +64,7 @@ void ArrayLengthFromUniform::Run(CloneContext& ctx, // Get (or create, on first call) the uniform buffer that will receive the // size of each storage buffer in the module. - ast::Variable* buffer_size_ubo = nullptr; + const ast::Variable* buffer_size_ubo = nullptr; auto get_ubo = [&]() { if (!buffer_size_ubo) { // Emit an array, N>, where N is 1/4 number of elements. diff --git a/src/transform/array_length_from_uniform.h b/src/transform/array_length_from_uniform.h index a310616759..306ed8a231 100644 --- a/src/transform/array_length_from_uniform.h +++ b/src/transform/array_length_from_uniform.h @@ -89,7 +89,7 @@ class ArrayLengthFromUniform ~Result() override; /// True if the transform generated the buffer sizes UBO. - bool const needs_buffer_sizes; + const bool needs_buffer_sizes; }; protected: diff --git a/src/transform/binding_remapper.cc b/src/transform/binding_remapper.cc index 3fe4e6ef0c..ef77fed65f 100644 --- a/src/transform/binding_remapper.cc +++ b/src/transform/binding_remapper.cc @@ -129,7 +129,7 @@ void BindingRemapper::Run(CloneContext& ctx, const DataMap& inputs, DataMap&) { return; } auto* ty = sem->Type()->UnwrapRef(); - ast::Type* inner_ty = CreateASTTypeFor(ctx, ty); + const ast::Type* inner_ty = CreateASTTypeFor(ctx, ty); auto* new_var = ctx.dst->create( ctx.Clone(var->source), ctx.Clone(var->symbol), var->declared_storage_class, ac, inner_ty, var->is_const, diff --git a/src/transform/binding_remapper.h b/src/transform/binding_remapper.h index 5fd2fc8d59..8e0d8fd5bd 100644 --- a/src/transform/binding_remapper.h +++ b/src/transform/binding_remapper.h @@ -54,14 +54,14 @@ class BindingRemapper : public Castable { ~Remappings() override; /// A map of old binding point to new binding point - BindingPoints const binding_points; + const BindingPoints binding_points; /// A map of old binding point to new access controls - AccessControls const access_controls; + const AccessControls access_controls; /// If true, then validation will be disabled for binding point collisions /// generated by this transform - bool const allow_collisions; + const bool allow_collisions; }; /// Constructor diff --git a/src/transform/calculate_array_length.cc b/src/transform/calculate_array_length.cc index 37394a0ac5..849d062620 100644 --- a/src/transform/calculate_array_length.cc +++ b/src/transform/calculate_array_length.cc @@ -61,7 +61,7 @@ std::string CalculateArrayLength::BufferSizeIntrinsic::InternalName() const { return "intrinsic_buffer_size"; } -CalculateArrayLength::BufferSizeIntrinsic* +const CalculateArrayLength::BufferSizeIntrinsic* CalculateArrayLength::BufferSizeIntrinsic::Clone(CloneContext* ctx) const { return ctx->dst->ASTNodes().Create( ctx->dst->ID()); diff --git a/src/transform/calculate_array_length.h b/src/transform/calculate_array_length.h index 20030587fe..55aabe41be 100644 --- a/src/transform/calculate_array_length.h +++ b/src/transform/calculate_array_length.h @@ -48,7 +48,7 @@ class CalculateArrayLength : public Castable { /// Performs a deep clone of this object using the CloneContext `ctx`. /// @param ctx the clone context /// @return the newly cloned object - BufferSizeIntrinsic* Clone(CloneContext* ctx) const override; + const BufferSizeIntrinsic* Clone(CloneContext* ctx) const override; }; /// Constructor diff --git a/src/transform/canonicalize_entry_point_io.cc b/src/transform/canonicalize_entry_point_io.cc index 28378b1856..51b5e46bf9 100644 --- a/src/transform/canonicalize_entry_point_io.cc +++ b/src/transform/canonicalize_entry_point_io.cc @@ -82,11 +82,11 @@ struct CanonicalizeEntryPointIO::State { /// The name of the output value. std::string name; /// The type of the output value. - ast::Type* type; + const ast::Type* type; /// The shader IO attributes. ast::DecorationList attributes; /// The value itself. - ast::Expression* value; + const ast::Expression* value; }; /// The clone context. @@ -94,9 +94,9 @@ struct CanonicalizeEntryPointIO::State { /// The transform config. CanonicalizeEntryPointIO::Config const cfg; /// The entry point function (AST). - ast::Function* func_ast; + const ast::Function* func_ast; /// The entry point function (SEM). - sem::Function const* func_sem; + const sem::Function* func_sem; /// The new entry point wrapper function's parameters. ast::VariableList wrapper_ep_parameters; @@ -121,7 +121,7 @@ struct CanonicalizeEntryPointIO::State { /// @param function the entry point function State(CloneContext& context, const CanonicalizeEntryPointIO::Config& config, - ast::Function* function) + const ast::Function* function) : ctx(context), cfg(config), func_ast(function), @@ -154,9 +154,9 @@ struct CanonicalizeEntryPointIO::State { /// @param type the type of the shader input /// @param attributes the attributes to apply to the shader input /// @returns an expression which evaluates to the value of the shader input - ast::Expression* AddInput(std::string name, - sem::Type* type, - ast::DecorationList attributes) { + const ast::Expression* AddInput(std::string name, + const sem::Type* type, + ast::DecorationList attributes) { auto* ast_type = CreateASTTypeFor(ctx, type); if (cfg.shader_style == ShaderStyle::kSpirv) { // Vulkan requires that integer user-defined fragment inputs are @@ -175,7 +175,7 @@ struct CanonicalizeEntryPointIO::State { // Create the global variable and use its value for the shader input. auto symbol = ctx.dst->Symbols().New(name); - ast::Expression* value = ctx.dst->Expr(symbol); + const ast::Expression* value = ctx.dst->Expr(symbol); if (HasSampleMask(attributes)) { // Vulkan requires the type of a SampleMask builtin to be an array. // Declare it as array and then load the first element. @@ -212,9 +212,9 @@ struct CanonicalizeEntryPointIO::State { /// @param attributes the attributes to apply to the shader output /// @param value the value of the shader output void AddOutput(std::string name, - sem::Type* type, + const sem::Type* type, ast::DecorationList attributes, - ast::Expression* value) { + const ast::Expression* value) { // Vulkan requires that integer user-defined vertex outputs are // always decorated with `Flat`. if (cfg.shader_style == ShaderStyle::kSpirv && @@ -417,7 +417,7 @@ struct CanonicalizeEntryPointIO::State { // Create the global variable and assign it the output value. auto name = ctx.dst->Symbols().New(outval.name); auto* type = outval.type; - ast::Expression* lhs = ctx.dst->Expr(name); + const ast::Expression* lhs = ctx.dst->Expr(name); if (HasSampleMask(attributes)) { // Vulkan requires the type of a SampleMask builtin to be an array. // Declare it as array and then store to the first element. @@ -432,7 +432,7 @@ struct CanonicalizeEntryPointIO::State { // Recreate the original function without entry point attributes and call it. /// @returns the inner function call expression - ast::CallExpression* CallInnerFunction() { + const ast::CallExpression* CallInnerFunction() { // Add a suffix to the function name, as the wrapper function will take the // original entry point name. auto ep_name = ctx.src->Symbols().NameFor(func_ast->symbol); @@ -492,7 +492,7 @@ struct CanonicalizeEntryPointIO::State { auto* call_inner = CallInnerFunction(); // Process the return type, and start building the wrapper function body. - std::function wrapper_ret_type = [&] { + std::function wrapper_ret_type = [&] { return ctx.dst->ty.void_(); }; if (func_sem->ReturnType()->Is()) { diff --git a/src/transform/canonicalize_entry_point_io.h b/src/transform/canonicalize_entry_point_io.h index 87c3e104cf..baaa65a454 100644 --- a/src/transform/canonicalize_entry_point_io.h +++ b/src/transform/canonicalize_entry_point_io.h @@ -110,14 +110,14 @@ class CanonicalizeEntryPointIO ~Config() override; /// The approach to use for emitting shader IO. - ShaderStyle const shader_style; + const ShaderStyle shader_style; /// A fixed sample mask to combine into masks produced by fragment shaders. - uint32_t const fixed_sample_mask; + const uint32_t fixed_sample_mask; /// Set to `true` to generate a pointsize builtin and have it set to 1.0 /// from all vertex shaders in the module. - bool const emit_vertex_point_size; + const bool emit_vertex_point_size; }; /// Constructor diff --git a/src/transform/decompose_memory_access.cc b/src/transform/decompose_memory_access.cc index d6b5a40ab0..5d32b02e13 100644 --- a/src/transform/decompose_memory_access.cc +++ b/src/transform/decompose_memory_access.cc @@ -51,17 +51,17 @@ namespace { /// offsets for storage and uniform buffer accesses. struct Offset : Castable { /// @returns builds and returns the ast::Expression in `ctx.dst` - virtual ast::Expression* Build(CloneContext& ctx) const = 0; + virtual const ast::Expression* Build(CloneContext& ctx) const = 0; }; /// OffsetExpr is an implementation of Offset that clones and casts the given /// expression to `u32`. struct OffsetExpr : Offset { - ast::Expression* const expr = nullptr; + const ast::Expression* const expr = nullptr; - explicit OffsetExpr(ast::Expression* e) : expr(e) {} + explicit OffsetExpr(const ast::Expression* e) : expr(e) {} - ast::Expression* Build(CloneContext& ctx) const override { + const ast::Expression* Build(CloneContext& ctx) const override { auto* type = ctx.src->Sem().Get(expr)->Type()->UnwrapRef(); auto* res = ctx.Clone(expr); if (!type->Is()) { @@ -78,7 +78,7 @@ struct OffsetLiteral : Castable { explicit OffsetLiteral(uint32_t lit) : literal(lit) {} - ast::Expression* Build(CloneContext& ctx) const override { + const ast::Expression* Build(CloneContext& ctx) const override { return ctx.dst->Expr(literal); } }; @@ -90,7 +90,7 @@ struct OffsetBinOp : Offset { Offset const* lhs = nullptr; Offset const* rhs = nullptr; - ast::Expression* Build(CloneContext& ctx) const override { + const ast::Expression* Build(CloneContext& ctx) const override { return ctx.dst->create(op, lhs->Build(ctx), rhs->Build(ctx)); } @@ -304,9 +304,9 @@ struct DecomposeMemoryAccess::State { /// expressions chain the access. /// Subset of #expression_order, as expressions are not removed from /// #expression_order. - std::unordered_map accesses; + std::unordered_map accesses; /// The visited order of AST expressions (superset of #accesses) - std::vector expression_order; + std::vector expression_order; /// [buffer-type, element-type] -> load function name std::unordered_map load_funcs; /// [buffer-type, element-type] -> store function name @@ -330,7 +330,7 @@ struct DecomposeMemoryAccess::State { /// @param expr the expression to convert to an Offset /// @returns an Offset for the given ast::Expression - const Offset* ToOffset(ast::Expression* expr) { + const Offset* ToOffset(const ast::Expression* expr) { if (auto* scalar = expr->As()) { if (auto* u32 = scalar->literal->As()) { return offsets_.Create(u32->value); @@ -415,7 +415,7 @@ struct DecomposeMemoryAccess::State { /// to #expression_order. /// @param expr the expression that performs the access /// @param access the access - void AddAccess(ast::Expression* expr, const BufferAccess& access) { + void AddAccess(const ast::Expression* expr, const BufferAccess& access) { TINT_ASSERT(Transform, access.type); accesses.emplace(expr, access); expression_order.emplace_back(expr); @@ -426,7 +426,7 @@ struct DecomposeMemoryAccess::State { /// `node`, an invalid BufferAccess is returned. /// @param node the expression that performed an access /// @return the BufferAccess for the given expression - BufferAccess TakeAccess(ast::Expression* node) { + BufferAccess TakeAccess(const ast::Expression* node) { auto lhs_it = accesses.find(node); if (lhs_it == accesses.end()) { return {}; @@ -793,7 +793,7 @@ std::string DecomposeMemoryAccess::Intrinsic::InternalName() const { return ss.str(); } -DecomposeMemoryAccess::Intrinsic* DecomposeMemoryAccess::Intrinsic::Clone( +const DecomposeMemoryAccess::Intrinsic* DecomposeMemoryAccess::Intrinsic::Clone( CloneContext* ctx) const { return ctx->dst->ASTNodes().Create( ctx->dst->ID(), op, storage_class, type); diff --git a/src/transform/decompose_memory_access.h b/src/transform/decompose_memory_access.h index 66cdf37d4d..67f3eb96a5 100644 --- a/src/transform/decompose_memory_access.h +++ b/src/transform/decompose_memory_access.h @@ -88,16 +88,16 @@ class DecomposeMemoryAccess /// Performs a deep clone of this object using the CloneContext `ctx`. /// @param ctx the clone context /// @return the newly cloned object - Intrinsic* Clone(CloneContext* ctx) const override; + const Intrinsic* Clone(CloneContext* ctx) const override; /// The op of the intrinsic - Op const op; + const Op op; /// The storage class of the buffer this intrinsic operates on ast::StorageClass const storage_class; /// The type of the intrinsic - DataType const type; + const DataType type; }; /// Constructor diff --git a/src/transform/decompose_strided_matrix.cc b/src/transform/decompose_strided_matrix.cc index 497a89151f..2d21a5bb28 100644 --- a/src/transform/decompose_strided_matrix.cc +++ b/src/transform/decompose_strided_matrix.cc @@ -37,11 +37,11 @@ struct MatrixInfo { /// The stride in bytes between columns of the matrix uint32_t stride = 0; /// The type of the matrix - sem::Matrix const* matrix = nullptr; + const sem::Matrix* matrix = nullptr; /// @returns a new ast::Array that holds an vector column for each row of the /// matrix. - ast::Array* array(ProgramBuilder* b) const { + const ast::Array* array(ProgramBuilder* b) const { return b->ty.array(b->ty.vec(matrix->rows()), matrix->columns(), stride); } @@ -126,7 +126,7 @@ void DecomposeStridedMatrix::Run(CloneContext& ctx, const DataMap&, DataMap&) { // Scan the program for all storage and uniform structure matrix members with // a custom stride attribute. Replace these matrices with an equivalent array, // and populate the `decomposed` map with the members that have been replaced. - std::unordered_map decomposed; + std::unordered_map decomposed; GatherCustomStrideMatrixMembers( ctx.src, [&](const sem::StructMember* member, sem::Matrix* matrix, uint32_t stride) { @@ -144,19 +144,19 @@ void DecomposeStridedMatrix::Run(CloneContext& ctx, const DataMap&, DataMap&) { // preserve these without calling conversion functions. // Example: // ssbo.mat[2] -> ssbo.mat[2] - ctx.ReplaceAll( - [&](ast::ArrayAccessorExpression* expr) -> ast::ArrayAccessorExpression* { - if (auto* access = - ctx.src->Sem().Get(expr->array)) { - auto it = decomposed.find(access->Member()->Declaration()); - if (it != decomposed.end()) { - auto* obj = ctx.CloneWithoutTransform(expr->array); - auto* idx = ctx.Clone(expr->index); - return ctx.dst->IndexAccessor(obj, idx); - } - } - return nullptr; - }); + ctx.ReplaceAll([&](const ast::ArrayAccessorExpression* expr) + -> const ast::ArrayAccessorExpression* { + if (auto* access = + ctx.src->Sem().Get(expr->array)) { + auto it = decomposed.find(access->Member()->Declaration()); + if (it != decomposed.end()) { + auto* obj = ctx.CloneWithoutTransform(expr->array); + auto* idx = ctx.Clone(expr->index); + return ctx.dst->IndexAccessor(obj, idx); + } + } + return nullptr; + }); // For all struct member accesses to the matrix on the LHS of an assignment, // we need to convert the matrix to the array before assigning to the @@ -164,7 +164,8 @@ void DecomposeStridedMatrix::Run(CloneContext& ctx, const DataMap&, DataMap&) { // Example: // ssbo.mat = mat_to_arr(m) std::unordered_map mat_to_arr; - ctx.ReplaceAll([&](ast::AssignmentStatement* stmt) -> ast::Statement* { + ctx.ReplaceAll([&](const ast::AssignmentStatement* stmt) + -> const ast::Statement* { if (auto* access = ctx.src->Sem().Get(stmt->lhs)) { auto it = decomposed.find(access->Member()->Declaration()); if (it == decomposed.end()) { @@ -206,42 +207,44 @@ void DecomposeStridedMatrix::Run(CloneContext& ctx, const DataMap&, DataMap&) { // matrix type. Example: // m = arr_to_mat(ssbo.mat) std::unordered_map arr_to_mat; - ctx.ReplaceAll([&](ast::MemberAccessorExpression* expr) -> ast::Expression* { - if (auto* access = ctx.src->Sem().Get(expr)) { - auto it = decomposed.find(access->Member()->Declaration()); - if (it == decomposed.end()) { - return nullptr; - } - MatrixInfo info = it->second; - auto fn = utils::GetOrCreate(arr_to_mat, info, [&] { - auto name = ctx.dst->Symbols().New( - "arr_to_mat" + std::to_string(info.matrix->columns()) + "x" + - std::to_string(info.matrix->rows()) + "_stride_" + - std::to_string(info.stride)); + ctx.ReplaceAll( + [&](const ast::MemberAccessorExpression* expr) -> const ast::Expression* { + if (auto* access = ctx.src->Sem().Get(expr)) { + auto it = decomposed.find(access->Member()->Declaration()); + if (it == decomposed.end()) { + return nullptr; + } + MatrixInfo info = it->second; + auto fn = utils::GetOrCreate(arr_to_mat, info, [&] { + auto name = ctx.dst->Symbols().New( + "arr_to_mat" + std::to_string(info.matrix->columns()) + "x" + + std::to_string(info.matrix->rows()) + "_stride_" + + std::to_string(info.stride)); - auto matrix = [&] { return CreateASTTypeFor(ctx, info.matrix); }; - auto array = [&] { return info.array(ctx.dst); }; + auto matrix = [&] { return CreateASTTypeFor(ctx, info.matrix); }; + auto array = [&] { return info.array(ctx.dst); }; - auto arr = ctx.dst->Sym("arr"); - ast::ExpressionList columns(info.matrix->columns()); - for (uint32_t i = 0; i < static_cast(columns.size()); i++) { - columns[i] = ctx.dst->IndexAccessor(arr, i); + auto arr = ctx.dst->Sym("arr"); + ast::ExpressionList columns(info.matrix->columns()); + for (uint32_t i = 0; i < static_cast(columns.size()); + i++) { + columns[i] = ctx.dst->IndexAccessor(arr, i); + } + ctx.dst->Func( + name, + { + ctx.dst->Param(arr, array()), + }, + matrix(), + { + ctx.dst->Return(ctx.dst->Construct(matrix(), columns)), + }); + return name; + }); + return ctx.dst->Call(fn, ctx.CloneWithoutTransform(expr)); } - ctx.dst->Func( - name, - { - ctx.dst->Param(arr, array()), - }, - matrix(), - { - ctx.dst->Return(ctx.dst->Construct(matrix(), columns)), - }); - return name; + return nullptr; }); - return ctx.dst->Call(fn, ctx.CloneWithoutTransform(expr)); - } - return nullptr; - }); ctx.Clone(); } diff --git a/src/transform/first_index_offset.cc b/src/transform/first_index_offset.cc index 0ae7ffa49f..4bc21c29ce 100644 --- a/src/transform/first_index_offset.cc +++ b/src/transform/first_index_offset.cc @@ -80,7 +80,7 @@ void FirstIndexOffset::Run(CloneContext& ctx, // parameters) or structure member accesses. for (auto* node : ctx.src->ASTNodes().Objects()) { if (auto* var = node->As()) { - for (ast::Decoration* dec : var->decorations) { + for (auto* dec : var->decorations) { if (auto* builtin_dec = dec->As()) { ast::Builtin builtin = builtin_dec->builtin; if (builtin == ast::Builtin::kVertexIndex) { @@ -97,7 +97,7 @@ void FirstIndexOffset::Run(CloneContext& ctx, } } if (auto* member = node->As()) { - for (ast::Decoration* dec : member->decorations) { + for (auto* dec : member->decorations) { if (auto* builtin_dec = dec->As()) { ast::Builtin builtin = builtin_dec->builtin; if (builtin == ast::Builtin::kVertexIndex) { @@ -147,28 +147,29 @@ void FirstIndexOffset::Run(CloneContext& ctx, }); // Fix up all references to the builtins with the offsets - ctx.ReplaceAll([=, &ctx](ast::Expression* expr) -> ast::Expression* { - if (auto* sem = ctx.src->Sem().Get(expr)) { - if (auto* user = sem->As()) { - auto it = builtin_vars.find(user->Variable()); - if (it != builtin_vars.end()) { - return ctx.dst->Add( - ctx.CloneWithoutTransform(expr), - ctx.dst->MemberAccessor(buffer_name, it->second)); + ctx.ReplaceAll( + [=, &ctx](const ast::Expression* expr) -> const ast::Expression* { + if (auto* sem = ctx.src->Sem().Get(expr)) { + if (auto* user = sem->As()) { + auto it = builtin_vars.find(user->Variable()); + if (it != builtin_vars.end()) { + return ctx.dst->Add( + ctx.CloneWithoutTransform(expr), + ctx.dst->MemberAccessor(buffer_name, it->second)); + } + } + if (auto* access = sem->As()) { + auto it = builtin_members.find(access->Member()); + if (it != builtin_members.end()) { + return ctx.dst->Add( + ctx.CloneWithoutTransform(expr), + ctx.dst->MemberAccessor(buffer_name, it->second)); + } + } } - } - if (auto* access = sem->As()) { - auto it = builtin_members.find(access->Member()); - if (it != builtin_members.end()) { - return ctx.dst->Add( - ctx.CloneWithoutTransform(expr), - ctx.dst->MemberAccessor(buffer_name, it->second)); - } - } - } - // Not interested in this experssion. Just clone. - return nullptr; - }); + // Not interested in this experssion. Just clone. + return nullptr; + }); } ctx.Clone(); diff --git a/src/transform/first_index_offset.h b/src/transform/first_index_offset.h index 2ba27b5c24..21a618a194 100644 --- a/src/transform/first_index_offset.h +++ b/src/transform/first_index_offset.h @@ -98,13 +98,13 @@ class FirstIndexOffset : public Castable { ~Data() override; /// True if the shader uses vertex_index - bool const has_vertex_index; + const bool has_vertex_index; /// True if the shader uses instance_index - bool const has_instance_index; + const bool has_instance_index; /// Offset of first vertex into constant buffer - uint32_t const first_vertex_offset; + const uint32_t first_vertex_offset; /// Offset of first instance into constant buffer - uint32_t const first_instance_offset; + const uint32_t first_instance_offset; }; /// Constructor diff --git a/src/transform/fold_constants.cc b/src/transform/fold_constants.cc index 54f7f15f0d..271a930d64 100644 --- a/src/transform/fold_constants.cc +++ b/src/transform/fold_constants.cc @@ -31,7 +31,7 @@ FoldConstants::FoldConstants() = default; FoldConstants::~FoldConstants() = default; void FoldConstants::Run(CloneContext& ctx, const DataMap&, DataMap&) { - ctx.ReplaceAll([&](ast::Expression* expr) -> ast::Expression* { + ctx.ReplaceAll([&](const ast::Expression* expr) -> const ast::Expression* { auto* sem = ctx.src->Sem().Get(expr); if (!sem) { return nullptr; diff --git a/src/transform/fold_trivial_single_use_lets.cc b/src/transform/fold_trivial_single_use_lets.cc index 78d77e3eae..0427075b37 100644 --- a/src/transform/fold_trivial_single_use_lets.cc +++ b/src/transform/fold_trivial_single_use_lets.cc @@ -27,7 +27,7 @@ namespace tint { namespace transform { namespace { -ast::VariableDeclStatement* AsTrivialLetDecl(ast::Statement* stmt) { +const ast::VariableDeclStatement* AsTrivialLetDecl(const ast::Statement* stmt) { auto* var_decl = stmt->As(); if (!var_decl) { return nullptr; diff --git a/src/transform/for_loop_to_loop.cc b/src/transform/for_loop_to_loop.cc index 6b43064cad..1eab804828 100644 --- a/src/transform/for_loop_to_loop.cc +++ b/src/transform/for_loop_to_loop.cc @@ -26,37 +26,39 @@ ForLoopToLoop::ForLoopToLoop() = default; ForLoopToLoop::~ForLoopToLoop() = default; void ForLoopToLoop::Run(CloneContext& ctx, const DataMap&, DataMap&) { - ctx.ReplaceAll([&](ast::ForLoopStatement* for_loop) -> ast::Statement* { - ast::StatementList stmts; - if (auto* cond = for_loop->condition) { - // !condition - auto* not_cond = ctx.dst->create( - ast::UnaryOp::kNot, ctx.Clone(cond)); + ctx.ReplaceAll( + [&](const ast::ForLoopStatement* for_loop) -> const ast::Statement* { + ast::StatementList stmts; + if (auto* cond = for_loop->condition) { + // !condition + auto* not_cond = ctx.dst->create( + ast::UnaryOp::kNot, ctx.Clone(cond)); - // { break; } - auto* break_body = ctx.dst->Block(ctx.dst->create()); + // { break; } + auto* break_body = + ctx.dst->Block(ctx.dst->create()); - // if (!condition) { break; } - stmts.emplace_back(ctx.dst->If(not_cond, break_body)); - } - for (auto* stmt : for_loop->body->statements) { - stmts.emplace_back(ctx.Clone(stmt)); - } + // if (!condition) { break; } + stmts.emplace_back(ctx.dst->If(not_cond, break_body)); + } + for (auto* stmt : for_loop->body->statements) { + stmts.emplace_back(ctx.Clone(stmt)); + } - ast::BlockStatement* continuing = nullptr; - if (auto* cont = for_loop->continuing) { - continuing = ctx.dst->Block(ctx.Clone(cont)); - } + const ast::BlockStatement* continuing = nullptr; + if (auto* cont = for_loop->continuing) { + continuing = ctx.dst->Block(ctx.Clone(cont)); + } - auto* body = ctx.dst->Block(stmts); - auto* loop = ctx.dst->create(body, continuing); + auto* body = ctx.dst->Block(stmts); + auto* loop = ctx.dst->create(body, continuing); - if (auto* init = for_loop->initializer) { - return ctx.dst->Block(ctx.Clone(init), loop); - } + if (auto* init = for_loop->initializer) { + return ctx.dst->Block(ctx.Clone(init), loop); + } - return loop; - }); + return loop; + }); ctx.Clone(); } diff --git a/src/transform/inline_pointer_lets.cc b/src/transform/inline_pointer_lets.cc index 85287a2c50..196c0d564d 100644 --- a/src/transform/inline_pointer_lets.cc +++ b/src/transform/inline_pointer_lets.cc @@ -41,7 +41,7 @@ namespace { /// expression template void CollectSavedArrayIndices(const Program* program, - ast::Expression* expr, + const ast::Expression* expr, F&& cb) { if (auto* a = expr->As()) { CollectSavedArrayIndices(program, a->array, cb); @@ -95,7 +95,7 @@ void InlinePointerLets::Run(CloneContext& ctx, const DataMap&, DataMap&) { // * Sub-expressions inside the pointer-typed `let` initializer expression // that have been hoisted to a saved variable are replaced with the saved // variable identifier. - ctx.ReplaceAll([&](ast::Expression* expr) -> ast::Expression* { + ctx.ReplaceAll([&](const ast::Expression* expr) -> const ast::Expression* { if (current_ptr_let) { // We're currently processing the initializer expression of a // pointer-typed `let` declaration. Look to see if we need to swap this @@ -150,7 +150,7 @@ void InlinePointerLets::Run(CloneContext& ctx, const DataMap&, DataMap&) { // to be hoist to temporary "saved" variables. CollectSavedArrayIndices( ctx.src, var->Declaration()->constructor, - [&](ast::Expression* idx_expr) { + [&](const ast::Expression* idx_expr) { // We have a sub-expression that needs to be saved. // Create a new variable auto saved_name = ctx.dst->Symbols().New( diff --git a/src/transform/loop_to_for_loop.cc b/src/transform/loop_to_for_loop.cc index 34d452477c..d2da837973 100644 --- a/src/transform/loop_to_for_loop.cc +++ b/src/transform/loop_to_for_loop.cc @@ -29,7 +29,7 @@ namespace tint { namespace transform { namespace { -bool IsBlockWithSingleBreak(ast::BlockStatement* block) { +bool IsBlockWithSingleBreak(const ast::BlockStatement* block) { if (block->statements.size() != 1) { return false; } @@ -37,8 +37,8 @@ bool IsBlockWithSingleBreak(ast::BlockStatement* block) { } bool IsVarUsedByStmt(const sem::Info& sem, - ast::Variable* var, - ast::Statement* stmt) { + const ast::Variable* var, + const ast::Statement* stmt) { auto* var_sem = sem.Get(var); for (auto* user : var_sem->Users()) { if (auto* s = user->Stmt()) { @@ -57,7 +57,7 @@ LoopToForLoop::LoopToForLoop() = default; LoopToForLoop::~LoopToForLoop() = default; void LoopToForLoop::Run(CloneContext& ctx, const DataMap&, DataMap&) { - ctx.ReplaceAll([&](ast::LoopStatement* loop) -> ast::Statement* { + ctx.ReplaceAll([&](const ast::LoopStatement* loop) -> const ast::Statement* { // For loop condition is taken from the first statement in the loop. // This requires an if-statement with either: // * A true block with no else statements, and the true block contains a @@ -90,7 +90,7 @@ void LoopToForLoop::Run(CloneContext& ctx, const DataMap&, DataMap&) { // The continuing block must be empty or contain a single, assignment or // function call statement. - ast::Statement* continuing = nullptr; + const ast::Statement* continuing = nullptr; if (auto* loop_cont = loop->continuing) { if (loop_cont->statements.size() != 1) { return nullptr; diff --git a/src/transform/module_scope_var_to_entry_point_param.cc b/src/transform/module_scope_var_to_entry_point_param.cc index f71a0105dc..0eeaa53d59 100644 --- a/src/transform/module_scope_var_to_entry_point_param.cc +++ b/src/transform/module_scope_var_to_entry_point_param.cc @@ -77,7 +77,7 @@ struct ModuleScopeVarToEntryPointParam::State { // Clone the struct and add it to the global declaration list. // Remove the old declaration. auto* ast_str = str->Declaration(); - ctx.dst->AST().AddTypeDecl(ctx.Clone(const_cast(ast_str))); + ctx.dst->AST().AddTypeDecl(ctx.Clone(ast_str)); ctx.Remove(ctx.src->AST().GlobalDeclarations(), ast_str); } else if (auto* arr = ty->As()) { CloneStructTypes(arr->ElemType()); @@ -90,7 +90,7 @@ struct ModuleScopeVarToEntryPointParam::State { using CallList = std::vector; std::unordered_map calls_to_replace; - std::vector functions_to_process; + std::vector functions_to_process; // Build a list of functions that transitively reference any private or // workgroup variables, or texture/sampler variables. @@ -123,7 +123,8 @@ struct ModuleScopeVarToEntryPointParam::State { // rules when this expression is passed to a function. // TODO(jrprice): We should add support for bidirectional SEM tree traversal // so that we can do this on the fly instead. - std::unordered_map + std::unordered_map ident_to_address_of; for (auto* node : ctx.src->ASTNodes().Objects()) { auto* address_of = node->As(); @@ -248,7 +249,7 @@ struct ModuleScopeVarToEntryPointParam::State { // For non-entry points, dereference non-handle pointer parameters. for (auto* user : var->Users()) { if (user->Stmt()->Function() == func_ast) { - ast::Expression* expr = ctx.dst->Expr(new_var_symbol); + const ast::Expression* expr = ctx.dst->Expr(new_var_symbol); if (is_pointer) { // If this identifier is used by an address-of operator, just // remove the address-of instead of adding a deref, since we @@ -301,7 +302,8 @@ struct ModuleScopeVarToEntryPointParam::State { target_var->StorageClass() == ast::StorageClass::kWorkgroup || target_var->StorageClass() == ast::StorageClass::kUniformConstant) { - ast::Expression* arg = ctx.dst->Expr(var_to_symbol[target_var]); + const ast::Expression* arg = + ctx.dst->Expr(var_to_symbol[target_var]); if (is_entry_point && !is_handle && !is_workgroup_matrix) { arg = ctx.dst->AddressOf(arg); } diff --git a/src/transform/num_workgroups_from_uniform.cc b/src/transform/num_workgroups_from_uniform.cc index 4b2815c59f..62faf9a204 100644 --- a/src/transform/num_workgroups_from_uniform.cc +++ b/src/transform/num_workgroups_from_uniform.cc @@ -116,7 +116,7 @@ void NumWorkgroupsFromUniform::Run(CloneContext& ctx, // Get (or create, on first call) the uniform buffer that will receive the // number of workgroups. - ast::Variable* num_workgroups_ubo = nullptr; + const ast::Variable* num_workgroups_ubo = nullptr; auto get_ubo = [&]() { if (!num_workgroups_ubo) { auto* num_workgroups_struct = ctx.dst->Structure( diff --git a/src/transform/pad_array_elements.cc b/src/transform/pad_array_elements.cc index ff3f8df2f8..58f6836313 100644 --- a/src/transform/pad_array_elements.cc +++ b/src/transform/pad_array_elements.cc @@ -28,7 +28,7 @@ namespace tint { namespace transform { namespace { -using ArrayBuilder = std::function; +using ArrayBuilder = std::function; /// PadArray returns a function that constructs a new array in `ctx.dst` with /// the element type padded to account for the explicit stride. PadArray will @@ -55,7 +55,7 @@ ArrayBuilder PadArray( auto name = ctx.dst->Symbols().New("tint_padded_array_element"); // Examine the element type. Is it also an array? - ast::Type* el_ty = nullptr; + const ast::Type* el_ty = nullptr; if (auto* el_array = array->ElemType()->As()) { // Array of array - call PadArray() on the element type if (auto p = @@ -104,7 +104,7 @@ void PadArrayElements::Run(CloneContext& ctx, const DataMap&, DataMap&) { }; // Replace all array types with their corresponding padded array type - ctx.ReplaceAll([&](ast::Type* ast_type) -> ast::Type* { + ctx.ReplaceAll([&](const ast::Type* ast_type) -> const ast::Type* { auto* type = ctx.src->TypeOf(ast_type); if (auto* array = type->UnwrapRef()->As()) { if (auto p = pad(array)) { @@ -115,23 +115,24 @@ void PadArrayElements::Run(CloneContext& ctx, const DataMap&, DataMap&) { }); // Fix up array accessors so `a[1]` becomes `a[1].el` - ctx.ReplaceAll( - [&](ast::ArrayAccessorExpression* accessor) -> ast::Expression* { - if (auto* array = tint::As( - sem.Get(accessor->array)->Type()->UnwrapRef())) { - if (pad(array)) { - // Array element is wrapped in a structure. Emit a member accessor - // to get to the actual array element. - auto* idx = ctx.CloneWithoutTransform(accessor); - return ctx.dst->MemberAccessor(idx, "el"); - } - } - return nullptr; - }); + ctx.ReplaceAll([&](const ast::ArrayAccessorExpression* accessor) + -> const ast::Expression* { + if (auto* array = tint::As( + sem.Get(accessor->array)->Type()->UnwrapRef())) { + if (pad(array)) { + // Array element is wrapped in a structure. Emit a member accessor + // to get to the actual array element. + auto* idx = ctx.CloneWithoutTransform(accessor); + return ctx.dst->MemberAccessor(idx, "el"); + } + } + return nullptr; + }); // Fix up array constructors so `A(1,2)` becomes // `A(padded(1), padded(2))` - ctx.ReplaceAll([&](ast::TypeConstructorExpression* ctor) -> ast::Expression* { + ctx.ReplaceAll([&](const ast::TypeConstructorExpression* ctor) + -> const ast::Expression* { if (auto* array = tint::As(sem.Get(ctor)->Type()->UnwrapRef())) { if (auto p = pad(array)) { diff --git a/src/transform/renamer.cc b/src/transform/renamer.cc index 38fbc35de0..647db82a91 100644 --- a/src/transform/renamer.cc +++ b/src/transform/renamer.cc @@ -1131,7 +1131,7 @@ Output Renamer::Run(const Program* in, const DataMap& inputs) { // Swizzles, intrinsic calls and builtin structure members need to keep their // symbols preserved. - std::unordered_set preserve; + std::unordered_set preserve; for (auto* node : in->ASTNodes().Objects()) { if (auto* member = node->As()) { auto* sem = in->Sem().Get(member); @@ -1213,17 +1213,17 @@ Output Renamer::Run(const Program* in, const DataMap& inputs) { return sym_out; }); - ctx.ReplaceAll( - [&](ast::IdentifierExpression* ident) -> ast::IdentifierExpression* { - if (preserve.count(ident)) { - auto sym_in = ident->symbol; - auto str = in->Symbols().NameFor(sym_in); - auto sym_out = out.Symbols().Register(str); - return ctx.dst->create( - ctx.Clone(ident->source), sym_out); - } - return nullptr; // Clone ident. Uses the symbol remapping above. - }); + ctx.ReplaceAll([&](const ast::IdentifierExpression* ident) + -> const ast::IdentifierExpression* { + if (preserve.count(ident)) { + auto sym_in = ident->symbol; + auto str = in->Symbols().NameFor(sym_in); + auto sym_out = out.Symbols().Register(str); + return ctx.dst->create( + ctx.Clone(ident->source), sym_out); + } + return nullptr; // Clone ident. Uses the symbol remapping above. + }); ctx.Clone(); return Output(Program(std::move(out)), diff --git a/src/transform/renamer.h b/src/transform/renamer.h index 2e0586e85f..2c2ea0aeb8 100644 --- a/src/transform/renamer.h +++ b/src/transform/renamer.h @@ -43,7 +43,7 @@ class Renamer : public Castable { ~Data() override; /// A map of old symbol name to new symbol name - Remappings const remappings; + const Remappings remappings; }; /// Target is an enumerator of rename targets that can be used diff --git a/src/transform/robustness.cc b/src/transform/robustness.cc index 6bd7678242..28cd0f9031 100644 --- a/src/transform/robustness.cc +++ b/src/transform/robustness.cc @@ -41,16 +41,19 @@ struct Robustness::State { /// Applies the transformation state to `ctx`. void Transform() { + ctx.ReplaceAll([&](const ast::ArrayAccessorExpression* expr) { + return Transform(expr); + }); ctx.ReplaceAll( - [&](ast::ArrayAccessorExpression* expr) { return Transform(expr); }); - ctx.ReplaceAll([&](ast::CallExpression* expr) { return Transform(expr); }); + [&](const ast::CallExpression* expr) { return Transform(expr); }); } /// Apply bounds clamping to array, vector and matrix indexing /// @param expr the array, vector or matrix index expression /// @return the clamped replacement expression, or nullptr if `expr` should be /// cloned without changes. - ast::ArrayAccessorExpression* Transform(ast::ArrayAccessorExpression* expr) { + const ast::ArrayAccessorExpression* Transform( + const ast::ArrayAccessorExpression* expr) { auto* ret_type = ctx.src->Sem().Get(expr->array)->Type(); auto* ref = ret_type->As(); @@ -64,7 +67,7 @@ struct Robustness::State { using u32 = ProgramBuilder::u32; struct Value { - ast::Expression* expr = nullptr; // If null, then is a constant + const ast::Expression* expr = nullptr; // If null, then is a constant union { uint32_t u32 = 0; // use if is_signed == false int32_t i32; // use if is_signed == true @@ -208,7 +211,7 @@ struct Robustness::State { /// @param expr the intrinsic call expression /// @return the clamped replacement call expression, or nullptr if `expr` /// should be cloned without changes. - ast::CallExpression* Transform(ast::CallExpression* expr) { + const ast::CallExpression* Transform(const ast::CallExpression* expr) { auto* call = ctx.src->Sem().Get(expr); auto* call_target = call->Target(); auto* intrinsic = call_target->As(); @@ -235,7 +238,7 @@ struct Robustness::State { // to clamp both usages. // TODO(bclayton): We probably want to place this into a let so that the // calculation can be reused. This is fiddly to get right. - std::function level_arg; + std::function level_arg; if (level_idx >= 0) { level_arg = [&] { auto* arg = expr->args[level_idx]; diff --git a/src/transform/simplify.cc b/src/transform/simplify.cc index 2d414d762d..7d26ee2004 100644 --- a/src/transform/simplify.cc +++ b/src/transform/simplify.cc @@ -35,7 +35,7 @@ Simplify::Simplify() = default; Simplify::~Simplify() = default; void Simplify::Run(CloneContext& ctx, const DataMap&, DataMap&) { - ctx.ReplaceAll([&](ast::Expression* expr) -> ast::Expression* { + ctx.ReplaceAll([&](const ast::Expression* expr) -> const ast::Expression* { if (auto* outer = expr->As()) { if (auto* inner = outer->expr->As()) { if (outer->op == ast::UnaryOp::kAddressOf && diff --git a/src/transform/single_entry_point.cc b/src/transform/single_entry_point.cc index 9a3135a044..531a1def3d 100644 --- a/src/transform/single_entry_point.cc +++ b/src/transform/single_entry_point.cc @@ -42,7 +42,7 @@ void SingleEntryPoint::Run(CloneContext& ctx, const DataMap& inputs, DataMap&) { } // Find the target entry point. - ast::Function* entry_point = nullptr; + const ast::Function* entry_point = nullptr; for (auto* f : ctx.src->AST().Functions()) { if (!f->IsEntryPoint()) { continue; diff --git a/src/transform/transform.cc b/src/transform/transform.cc index 32ca39d39b..34f5ec6af8 100644 --- a/src/transform/transform.cc +++ b/src/transform/transform.cc @@ -90,7 +90,8 @@ void Transform::RemoveStatement(CloneContext& ctx, ast::Statement* stmt) { << sem->TypeInfo().name; } -ast::Type* Transform::CreateASTTypeFor(CloneContext& ctx, const sem::Type* ty) { +const ast::Type* Transform::CreateASTTypeFor(CloneContext& ctx, + const sem::Type* ty) { if (ty->Is()) { return ctx.dst->create(); } diff --git a/src/transform/transform.h b/src/transform/transform.h index f270e277c3..6d3499724c 100644 --- a/src/transform/transform.h +++ b/src/transform/transform.h @@ -198,7 +198,8 @@ class Transform : public Castable { /// @param ty the semantic type to reconstruct /// @returns a ast::Type that when resolved, will produce the semantic type /// `ty`. - static ast::Type* CreateASTTypeFor(CloneContext& ctx, const sem::Type* ty); + static const ast::Type* CreateASTTypeFor(CloneContext& ctx, + const sem::Type* ty); }; } // namespace transform diff --git a/src/transform/transform_test.cc b/src/transform/transform_test.cc index 6b9d352a08..001e54ab73 100644 --- a/src/transform/transform_test.cc +++ b/src/transform/transform_test.cc @@ -26,7 +26,7 @@ namespace { struct CreateASTTypeForTest : public testing::Test, public Transform { Output Run(const Program*, const DataMap&) override { return {}; } - ast::Type* create( + const ast::Type* create( std::function create_sem_type) { ProgramBuilder sem_type_builder; auto* sem_type = create_sem_type(sem_type_builder); diff --git a/src/transform/vertex_pulling.cc b/src/transform/vertex_pulling.cc index ae89e3dd21..1fc01fb904 100644 --- a/src/transform/vertex_pulling.cc +++ b/src/transform/vertex_pulling.cc @@ -137,7 +137,7 @@ struct DataType { uint32_t width; // 1 for scalar, 2+ for a vector }; -DataType DataTypeOf(sem::Type* ty) { +DataType DataTypeOf(const sem::Type* ty) { if (ty->Is()) { return {BaseType::kI32, 1}; } @@ -217,15 +217,15 @@ struct State { }; struct LocationInfo { - std::function expr; - sem::Type* type; + std::function expr; + const sem::Type* type; }; CloneContext& ctx; VertexPulling::Config const cfg; std::unordered_map location_info; - std::function vertex_index_expr = nullptr; - std::function instance_index_expr = nullptr; + std::function vertex_index_expr = nullptr; + std::function instance_index_expr = nullptr; Symbol pulling_position_name; Symbol struct_buffer_name; std::unordered_map vertex_buffer_names; @@ -369,7 +369,7 @@ struct State { } } else if (var_dt.width > fmt_dt.width) { // WGSL variable vector width is wider than the loaded vector width - ast::Type* ty = nullptr; + const ast::Type* ty = nullptr; ast::ExpressionList values{fetch}; switch (var_dt.base_type) { case BaseType::kI32: @@ -416,10 +416,10 @@ struct State { /// @param offset the byte offset of the data from `buffer_base` /// @param buffer the index of the vertex buffer /// @param format the format to read - ast::Expression* Fetch(Symbol array_base, - uint32_t offset, - uint32_t buffer, - VertexFormat format) { + const ast::Expression* Fetch(Symbol array_base, + uint32_t offset, + uint32_t buffer, + VertexFormat format) { using u32 = ProgramBuilder::u32; using i32 = ProgramBuilder::i32; using f32 = ProgramBuilder::f32; @@ -642,15 +642,15 @@ struct State { /// @param buffer the index of the vertex buffer /// @param format VertexFormat::kUint32, VertexFormat::kSint32 or /// VertexFormat::kFloat32 - ast::Expression* LoadPrimitive(Symbol array_base, - uint32_t offset, - uint32_t buffer, - VertexFormat format) { - ast::Expression* u32 = nullptr; + const ast::Expression* LoadPrimitive(Symbol array_base, + uint32_t offset, + uint32_t buffer, + VertexFormat format) { + const ast::Expression* u32 = nullptr; if ((offset & 3) == 0) { // Aligned load. - ast ::Expression* index = nullptr; + const ast ::Expression* index = nullptr; if (offset > 0) { index = ctx.dst->Add(array_base, offset / 4); } else { @@ -700,13 +700,13 @@ struct State { /// @param base_type underlying AST type /// @param base_format underlying vertex format /// @param count how many elements the vector has - ast::Expression* LoadVec(Symbol array_base, - uint32_t offset, - uint32_t buffer, - uint32_t element_stride, - ast::Type* base_type, - VertexFormat base_format, - uint32_t count) { + const ast::Expression* LoadVec(Symbol array_base, + uint32_t offset, + uint32_t buffer, + uint32_t element_stride, + const ast::Type* base_type, + VertexFormat base_format, + uint32_t count) { ast::ExpressionList expr_list; for (uint32_t i = 0; i < count; ++i) { // Offset read position by element_stride for each component @@ -724,7 +724,8 @@ struct State { /// vertex_index and instance_index builtins if present. /// @param func the entry point function /// @param param the parameter to process - void ProcessNonStructParameter(ast::Function* func, ast::Variable* param) { + void ProcessNonStructParameter(const ast::Function* func, + const ast::Variable* param) { if (auto* location = ast::GetDecoration(param->decorations)) { // Create a function-scope variable to replace the parameter. @@ -764,8 +765,8 @@ struct State { /// @param func the entry point function /// @param param the parameter to process /// @param struct_ty the structure type - void ProcessStructParameter(ast::Function* func, - ast::Variable* param, + void ProcessStructParameter(const ast::Function* func, + const ast::Variable* param, const ast::Struct* struct_ty) { auto param_sym = ctx.Clone(param->symbol); @@ -774,8 +775,8 @@ struct State { ast::StructMemberList members_to_clone; for (auto* member : struct_ty->members) { auto member_sym = ctx.Clone(member->symbol); - std::function member_expr = [this, param_sym, - member_sym]() { + std::function member_expr = [this, param_sym, + member_sym]() { return ctx.dst->MemberAccessor(param_sym, member_sym); }; @@ -842,7 +843,7 @@ struct State { /// Process an entry point function. /// @param func the entry point function - void Process(ast::Function* func) { + void Process(const ast::Function* func) { if (func->body->Empty()) { return; } diff --git a/src/transform/wrap_arrays_in_structs.cc b/src/transform/wrap_arrays_in_structs.cc index 6d6f90cb0c..782004f8d4 100644 --- a/src/transform/wrap_arrays_in_structs.cc +++ b/src/transform/wrap_arrays_in_structs.cc @@ -48,7 +48,7 @@ void WrapArraysInStructs::Run(CloneContext& ctx, const DataMap&, DataMap&) { }; // Replace all array types with their corresponding wrapper - ctx.ReplaceAll([&](ast::Type* ast_type) -> ast::Type* { + ctx.ReplaceAll([&](const ast::Type* ast_type) -> const ast::Type* { auto* type = ctx.src->TypeOf(ast_type); if (auto* array = type->UnwrapRef()->As()) { return wrapper_typename(array); @@ -57,8 +57,8 @@ void WrapArraysInStructs::Run(CloneContext& ctx, const DataMap&, DataMap&) { }); // Fix up array accessors so `a[1]` becomes `a.arr[1]` - ctx.ReplaceAll([&](ast::ArrayAccessorExpression* accessor) - -> ast::ArrayAccessorExpression* { + ctx.ReplaceAll([&](const ast::ArrayAccessorExpression* accessor) + -> const ast::ArrayAccessorExpression* { if (auto* array = ::tint::As( sem.Get(accessor->array)->Type()->UnwrapRef())) { if (wrapper(array)) { @@ -74,7 +74,8 @@ void WrapArraysInStructs::Run(CloneContext& ctx, const DataMap&, DataMap&) { }); // Fix up array constructors so `A(1,2)` becomes `tint_array_wrapper(A(1,2))` - ctx.ReplaceAll([&](ast::TypeConstructorExpression* ctor) -> ast::Expression* { + ctx.ReplaceAll([&](const ast::TypeConstructorExpression* ctor) + -> const ast::Expression* { if (auto* array = ::tint::As(sem.Get(ctor)->Type()->UnwrapRef())) { if (auto w = wrapper(array)) { @@ -107,7 +108,7 @@ WrapArraysInStructs::WrappedArrayInfo WrapArraysInStructs::WrapArray( info.wrapper_name = ctx.dst->Symbols().New("tint_array_wrapper"); // Examine the element type. Is it also an array? - std::function el_type; + std::function el_type; if (auto* el_array = array->ElemType()->As()) { // Array of array - call WrapArray() on the element type if (auto el = WrapArray(ctx, wrapped_arrays, el_array)) { diff --git a/src/transform/wrap_arrays_in_structs.h b/src/transform/wrap_arrays_in_structs.h index 6eb4bc164b..64dc047838 100644 --- a/src/transform/wrap_arrays_in_structs.h +++ b/src/transform/wrap_arrays_in_structs.h @@ -60,7 +60,7 @@ class WrapArraysInStructs : public Castable { ~WrappedArrayInfo(); Symbol wrapper_name; - std::function array_type; + std::function array_type; operator bool() { return wrapper_name.IsValid(); } }; diff --git a/src/transform/zero_init_workgroup_memory.cc b/src/transform/zero_init_workgroup_memory.cc index edd51c33a6..e5f3f8e6a7 100644 --- a/src/transform/zero_init_workgroup_memory.cc +++ b/src/transform/zero_init_workgroup_memory.cc @@ -46,7 +46,7 @@ struct ZeroInitWorkgroupMemory::State { uint32_t workgroup_size_const = 0; /// The size of the workgroup as an expression generator. Use if /// #workgroup_size_const is 0. - std::function workgroup_size_expr; + std::function workgroup_size_expr; /// ArrayIndex represents a function on the local invocation index, of /// the form: `array_index = (local_invocation_index % modulo) / division` @@ -80,7 +80,7 @@ struct ZeroInitWorkgroupMemory::State { /// statement will zero workgroup values. struct Expression { /// The AST expression node - ast::Expression* expr = nullptr; + const ast::Expression* expr = nullptr; /// The number of iterations required to zero the value uint32_t num_iterations = 0; /// All array indices used by this expression @@ -91,7 +91,7 @@ struct ZeroInitWorkgroupMemory::State { /// values. struct Statement { /// The AST statement node - ast::Statement* stmt; + const ast::Statement* stmt; /// The number of iterations required to zero the value uint32_t num_iterations; /// All array indices used by this statement @@ -112,7 +112,7 @@ struct ZeroInitWorkgroupMemory::State { /// Run inserts the workgroup memory zero-initialization logic at the top of /// the given function /// @param fn a compute shader entry point function - void Run(ast::Function* fn) { + void Run(const ast::Function* fn) { auto& sem = ctx.src->Sem(); CalculateWorkgroupSize( @@ -137,7 +137,7 @@ struct ZeroInitWorkgroupMemory::State { // Scan the entry point for an existing local_invocation_index builtin // parameter - std::function local_index; + std::function local_index; for (auto* param : fn->params) { if (auto* builtin = ast::GetDecoration(param->decorations)) { @@ -341,7 +341,7 @@ struct ZeroInitWorkgroupMemory::State { ast::StatementList DeclareArrayIndices( uint32_t num_iterations, const ArrayIndices& array_indices, - const std::function& iteration) { + const std::function& iteration) { ast::StatementList stmts; std::map indices_by_name; for (auto index : array_indices) { diff --git a/src/writer/append_vector.cc b/src/writer/append_vector.cc index 0da52c0497..18ea6147d8 100644 --- a/src/writer/append_vector.cc +++ b/src/writer/append_vector.cc @@ -23,8 +23,9 @@ namespace writer { namespace { -ast::TypeConstructorExpression* AsVectorConstructor(ProgramBuilder* b, - ast::Expression* expr) { +const ast::TypeConstructorExpression* AsVectorConstructor( + ProgramBuilder* b, + const ast::Expression* expr) { if (auto* constructor = expr->As()) { if (b->TypeOf(constructor)->Is()) { return constructor; @@ -35,9 +36,10 @@ ast::TypeConstructorExpression* AsVectorConstructor(ProgramBuilder* b, } // namespace -ast::TypeConstructorExpression* AppendVector(ProgramBuilder* b, - ast::Expression* vector, - ast::Expression* scalar) { +const ast::TypeConstructorExpression* AppendVector( + ProgramBuilder* b, + const ast::Expression* vector, + const ast::Expression* scalar) { uint32_t packed_size; const sem::Type* packed_el_sem_ty; auto* vector_sem = b->Sem().Get(vector); @@ -50,7 +52,7 @@ ast::TypeConstructorExpression* AppendVector(ProgramBuilder* b, packed_el_sem_ty = vector_ty; } - ast::Type* packed_el_ty = nullptr; + const ast::Type* packed_el_ty = nullptr; if (packed_el_sem_ty->Is()) { packed_el_ty = b->create(); } else if (packed_el_sem_ty->Is()) { @@ -83,7 +85,7 @@ ast::TypeConstructorExpression* AppendVector(ProgramBuilder* b, const auto num_supplied = vc->values.size(); if (num_supplied == 0) { // Zero-value vector constructor. Populate with zeros - auto buildZero = [&]() -> ast::ScalarConstructorExpression* { + auto buildZero = [&]() -> const ast::ScalarConstructorExpression* { if (packed_el_sem_ty->Is()) { return b->Expr(0); } else if (packed_el_sem_ty->Is()) { diff --git a/src/writer/append_vector.h b/src/writer/append_vector.h index 3955a31700..e95d4b4763 100644 --- a/src/writer/append_vector.h +++ b/src/writer/append_vector.h @@ -36,9 +36,10 @@ namespace writer { /// @param scalar the scalar to append to the vector. Must be a scalar. /// @returns a vector expression containing the elements of `vector` followed by /// the single element of `scalar` cast to the `vector` element type. -ast::TypeConstructorExpression* AppendVector(ProgramBuilder* builder, - ast::Expression* vector, - ast::Expression* scalar); +const ast::TypeConstructorExpression* AppendVector( + ProgramBuilder* builder, + const ast::Expression* vector, + const ast::Expression* scalar); } // namespace writer } // namespace tint diff --git a/src/writer/glsl/generator_impl.cc b/src/writer/glsl/generator_impl.cc index b7a79c4064..2bbef36770 100644 --- a/src/writer/glsl/generator_impl.cc +++ b/src/writer/glsl/generator_impl.cc @@ -157,8 +157,9 @@ bool GeneratorImpl::Generate() { return true; } -bool GeneratorImpl::EmitArrayAccessor(std::ostream& out, - ast::ArrayAccessorExpression* expr) { +bool GeneratorImpl::EmitArrayAccessor( + std::ostream& out, + const ast::ArrayAccessorExpression* expr) { if (!EmitExpression(out, expr->array)) { return false; } @@ -173,7 +174,7 @@ bool GeneratorImpl::EmitArrayAccessor(std::ostream& out, } bool GeneratorImpl::EmitBitcast(std::ostream& out, - ast::BitcastExpression* expr) { + const ast::BitcastExpression* expr) { auto* type = TypeOf(expr); if (auto* vec = type->UnwrapRef()->As()) { type = vec->type(); @@ -198,7 +199,7 @@ bool GeneratorImpl::EmitBitcast(std::ostream& out, return true; } -bool GeneratorImpl::EmitAssign(ast::AssignmentStatement* stmt) { +bool GeneratorImpl::EmitAssign(const ast::AssignmentStatement* stmt) { auto out = line(); if (!EmitExpression(out, stmt->lhs)) { return false; @@ -211,7 +212,8 @@ bool GeneratorImpl::EmitAssign(ast::AssignmentStatement* stmt) { return true; } -bool GeneratorImpl::EmitBinary(std::ostream& out, ast::BinaryExpression* expr) { +bool GeneratorImpl::EmitBinary(std::ostream& out, + const ast::BinaryExpression* expr) { if (expr->op == ast::BinaryOp::kLogicalAnd || expr->op == ast::BinaryOp::kLogicalOr) { auto name = UniqueIdentifier(kTempNamePrefix); @@ -351,12 +353,13 @@ bool GeneratorImpl::EmitBlock(const ast::BlockStatement* stmt) { return true; } -bool GeneratorImpl::EmitBreak(ast::BreakStatement*) { +bool GeneratorImpl::EmitBreak(const ast::BreakStatement*) { line() << "break;"; return true; } -bool GeneratorImpl::EmitCall(std::ostream& out, ast::CallExpression* expr) { +bool GeneratorImpl::EmitCall(std::ostream& out, + const ast::CallExpression* expr) { const auto& args = expr->args; auto* ident = expr->func; auto* call = builder_.Sem().Get(expr); @@ -456,7 +459,7 @@ bool GeneratorImpl::EmitCall(std::ostream& out, ast::CallExpression* expr) { } bool GeneratorImpl::EmitWorkgroupAtomicCall(std::ostream& out, - ast::CallExpression* expr, + const ast::CallExpression* expr, const sem::Intrinsic* intrinsic) { std::string result = UniqueIdentifier("atomic_result"); @@ -625,7 +628,7 @@ bool GeneratorImpl::EmitWorkgroupAtomicCall(std::ostream& out, } bool GeneratorImpl::EmitSelectCall(std::ostream& out, - ast::CallExpression* expr) { + const ast::CallExpression* expr) { auto* expr_false = expr->args[0]; auto* expr_true = expr->args[1]; auto* expr_cond = expr->args[2]; @@ -650,7 +653,7 @@ bool GeneratorImpl::EmitSelectCall(std::ostream& out, } bool GeneratorImpl::EmitModfCall(std::ostream& out, - ast::CallExpression* expr, + const ast::CallExpression* expr, const sem::Intrinsic* intrinsic) { if (expr->args.size() == 1) { return CallIntrinsicHelper( @@ -700,7 +703,7 @@ bool GeneratorImpl::EmitModfCall(std::ostream& out, } bool GeneratorImpl::EmitFrexpCall(std::ostream& out, - ast::CallExpression* expr, + const ast::CallExpression* expr, const sem::Intrinsic* intrinsic) { if (expr->args.size() == 1) { return CallIntrinsicHelper( @@ -771,7 +774,7 @@ bool GeneratorImpl::EmitFrexpCall(std::ostream& out, } bool GeneratorImpl::EmitIsNormalCall(std::ostream& out, - ast::CallExpression* expr, + const ast::CallExpression* expr, const sem::Intrinsic* intrinsic) { // GLSL doesn't have a isNormal intrinsic, we need to emulate return CallIntrinsicHelper( @@ -799,7 +802,7 @@ bool GeneratorImpl::EmitIsNormalCall(std::ostream& out, } bool GeneratorImpl::EmitDataPackingCall(std::ostream& out, - ast::CallExpression* expr, + const ast::CallExpression* expr, const sem::Intrinsic* intrinsic) { return CallIntrinsicHelper( out, expr, intrinsic, @@ -865,7 +868,7 @@ bool GeneratorImpl::EmitDataPackingCall(std::ostream& out, } bool GeneratorImpl::EmitDataUnpackingCall(std::ostream& out, - ast::CallExpression* expr, + const ast::CallExpression* expr, const sem::Intrinsic* intrinsic) { return CallIntrinsicHelper( out, expr, intrinsic, @@ -951,7 +954,7 @@ bool GeneratorImpl::EmitBarrierCall(std::ostream& out, } bool GeneratorImpl::EmitTextureCall(std::ostream& out, - ast::CallExpression* expr, + const ast::CallExpression* expr, const sem::Intrinsic* intrinsic) { using Usage = sem::ParameterUsage; @@ -1072,7 +1075,7 @@ bool GeneratorImpl::EmitTextureCall(std::ostream& out, return false; } - auto emit_vector_appended_with_i32_zero = [&](tint::ast::Expression* vector) { + auto emit_vector_appended_with_i32_zero = [&](const ast::Expression* vector) { auto* i32 = builder_.create(); auto* zero = builder_.Expr(0); auto* stmt = builder_.Sem().Get(vector)->Stmt(); @@ -1082,7 +1085,7 @@ bool GeneratorImpl::EmitTextureCall(std::ostream& out, return EmitExpression(out, packed); }; - auto emit_vector_appended_with_level = [&](tint::ast::Expression* vector) { + auto emit_vector_appended_with_level = [&](const ast::Expression* vector) { if (auto* level = arg(Usage::kLevel)) { auto* packed = AppendVector(&builder_, vector, level); return EmitExpression(out, packed); @@ -1252,7 +1255,7 @@ std::string GeneratorImpl::generate_builtin_name( return ""; } -bool GeneratorImpl::EmitCase(ast::CaseStatement* stmt) { +bool GeneratorImpl::EmitCase(const ast::CaseStatement* stmt) { if (stmt->IsDefault()) { line() << "default: {"; } else { @@ -1285,7 +1288,7 @@ bool GeneratorImpl::EmitCase(ast::CaseStatement* stmt) { } bool GeneratorImpl::EmitConstructor(std::ostream& out, - ast::ConstructorExpression* expr) { + const ast::ConstructorExpression* expr) { if (auto* scalar = expr->As()) { return EmitScalarConstructor(out, scalar); } @@ -1294,12 +1297,13 @@ bool GeneratorImpl::EmitConstructor(std::ostream& out, bool GeneratorImpl::EmitScalarConstructor( std::ostream& out, - ast::ScalarConstructorExpression* expr) { + const ast::ScalarConstructorExpression* expr) { return EmitLiteral(out, expr->literal); } -bool GeneratorImpl::EmitTypeConstructor(std::ostream& out, - ast::TypeConstructorExpression* expr) { +bool GeneratorImpl::EmitTypeConstructor( + std::ostream& out, + const ast::TypeConstructorExpression* expr) { auto* type = TypeOf(expr)->UnwrapRef(); // If the type constructor is empty then we need to construct with the zero @@ -1349,7 +1353,7 @@ bool GeneratorImpl::EmitTypeConstructor(std::ostream& out, return true; } -bool GeneratorImpl::EmitContinue(ast::ContinueStatement*) { +bool GeneratorImpl::EmitContinue(const ast::ContinueStatement*) { if (!emit_continuing_()) { return false; } @@ -1357,14 +1361,15 @@ bool GeneratorImpl::EmitContinue(ast::ContinueStatement*) { return true; } -bool GeneratorImpl::EmitDiscard(ast::DiscardStatement*) { +bool GeneratorImpl::EmitDiscard(const ast::DiscardStatement*) { // TODO(dsinclair): Verify this is correct when the discard semantics are // defined for WGSL (https://github.com/gpuweb/gpuweb/issues/361) line() << "discard;"; return true; } -bool GeneratorImpl::EmitExpression(std::ostream& out, ast::Expression* expr) { +bool GeneratorImpl::EmitExpression(std::ostream& out, + const ast::Expression* expr) { if (auto* a = expr->As()) { return EmitArrayAccessor(out, a); } @@ -1397,12 +1402,12 @@ bool GeneratorImpl::EmitExpression(std::ostream& out, ast::Expression* expr) { } bool GeneratorImpl::EmitIdentifier(std::ostream& out, - ast::IdentifierExpression* expr) { + const ast::IdentifierExpression* expr) { out << builder_.Symbols().NameFor(expr->symbol); return true; } -bool GeneratorImpl::EmitIf(ast::IfStatement* stmt) { +bool GeneratorImpl::EmitIf(const ast::IfStatement* stmt) { { auto out = line(); out << "if ("; @@ -1449,7 +1454,7 @@ bool GeneratorImpl::EmitIf(ast::IfStatement* stmt) { return true; } -bool GeneratorImpl::EmitFunction(ast::Function* func) { +bool GeneratorImpl::EmitFunction(const ast::Function* func) { auto* sem = builder_.Sem().Get(func); if (ast::HasDecoration(func->decorations)) { @@ -1524,7 +1529,7 @@ bool GeneratorImpl::EmitFunction(ast::Function* func) { return true; } -bool GeneratorImpl::EmitGlobalVariable(ast::Variable* global) { +bool GeneratorImpl::EmitGlobalVariable(const ast::Variable* global) { if (global->is_const) { return EmitProgramConstVariable(global); } @@ -1729,7 +1734,7 @@ std::string GeneratorImpl::interpolation_to_modifiers( return modifiers; } -bool GeneratorImpl::EmitEntryPointFunction(ast::Function* func) { +bool GeneratorImpl::EmitEntryPointFunction(const ast::Function* func) { auto* func_sem = builder_.Sem().Get(func); { @@ -1917,7 +1922,7 @@ bool GeneratorImpl::EmitEntryPointFunction(ast::Function* func) { return true; } -bool GeneratorImpl::EmitLiteral(std::ostream& out, ast::Literal* lit) { +bool GeneratorImpl::EmitLiteral(std::ostream& out, const ast::Literal* lit) { if (auto* l = lit->As()) { out << (l->value ? "true" : "false"); } else if (auto* fl = lit->As()) { @@ -2014,7 +2019,7 @@ bool GeneratorImpl::EmitZeroValue(std::ostream& out, const sem::Type* type) { return true; } -bool GeneratorImpl::EmitLoop(ast::LoopStatement* stmt) { +bool GeneratorImpl::EmitLoop(const ast::LoopStatement* stmt) { auto emit_continuing = [this, stmt]() { if (stmt->continuing && !stmt->continuing->Empty()) { if (!EmitBlock(stmt->continuing)) { @@ -2040,7 +2045,7 @@ bool GeneratorImpl::EmitLoop(ast::LoopStatement* stmt) { return true; } -bool GeneratorImpl::EmitForLoop(ast::ForLoopStatement* stmt) { +bool GeneratorImpl::EmitForLoop(const ast::ForLoopStatement* stmt) { // Nest a for loop with a new block. In HLSL the initializer scope is not // nested by the for-loop, so we may get variable redefinitions. line() << "{"; @@ -2149,8 +2154,9 @@ bool GeneratorImpl::EmitForLoop(ast::ForLoopStatement* stmt) { return true; } -bool GeneratorImpl::EmitMemberAccessor(std::ostream& out, - ast::MemberAccessorExpression* expr) { +bool GeneratorImpl::EmitMemberAccessor( + std::ostream& out, + const ast::MemberAccessorExpression* expr) { if (!EmitExpression(out, expr->structure)) { return false; } @@ -2166,7 +2172,7 @@ bool GeneratorImpl::EmitMemberAccessor(std::ostream& out, return true; } -bool GeneratorImpl::EmitReturn(ast::ReturnStatement* stmt) { +bool GeneratorImpl::EmitReturn(const ast::ReturnStatement* stmt) { if (stmt->value) { auto out = line(); out << "return "; @@ -2180,7 +2186,7 @@ bool GeneratorImpl::EmitReturn(ast::ReturnStatement* stmt) { return true; } -bool GeneratorImpl::EmitStatement(ast::Statement* stmt) { +bool GeneratorImpl::EmitStatement(const ast::Statement* stmt) { if (auto* a = stmt->As()) { return EmitAssign(a); } @@ -2236,7 +2242,7 @@ bool GeneratorImpl::EmitStatement(ast::Statement* stmt) { return false; } -bool GeneratorImpl::EmitSwitch(ast::SwitchStatement* stmt) { +bool GeneratorImpl::EmitSwitch(const ast::SwitchStatement* stmt) { { // switch(expr) { auto out = line(); out << "switch("; @@ -2484,7 +2490,7 @@ bool GeneratorImpl::EmitStructType(TextBuffer* b, const sem::Struct* str) { } bool GeneratorImpl::EmitUnaryOp(std::ostream& out, - ast::UnaryOpExpression* expr) { + const ast::UnaryOpExpression* expr) { switch (expr->op) { case ast::UnaryOp::kIndirection: case ast::UnaryOp::kAddressOf: @@ -2510,7 +2516,7 @@ bool GeneratorImpl::EmitUnaryOp(std::ostream& out, return true; } -bool GeneratorImpl::EmitVariable(ast::Variable* var) { +bool GeneratorImpl::EmitVariable(const ast::Variable* var) { auto* sem = builder_.Sem().Get(var); auto* type = sem->Type()->UnwrapRef(); @@ -2604,7 +2610,7 @@ bool GeneratorImpl::EmitProgramConstVariable(const ast::Variable* var) { template bool GeneratorImpl::CallIntrinsicHelper(std::ostream& out, - ast::CallExpression* call, + const ast::CallExpression* call, const sem::Intrinsic* intrinsic, F&& build) { // Generate the helper function if it hasn't been created already diff --git a/src/writer/glsl/generator_impl.h b/src/writer/glsl/generator_impl.h index c689ba1d38..4f9e8910ba 100644 --- a/src/writer/glsl/generator_impl.h +++ b/src/writer/glsl/generator_impl.h @@ -63,21 +63,22 @@ class GeneratorImpl : public TextGenerator { /// @param out the output of the expression stream /// @param expr the expression to emit /// @returns true if the array accessor was emitted - bool EmitArrayAccessor(std::ostream& out, ast::ArrayAccessorExpression* expr); + bool EmitArrayAccessor(std::ostream& out, + const ast::ArrayAccessorExpression* expr); /// Handles an assignment statement /// @param stmt the statement to emit /// @returns true if the statement was emitted successfully - bool EmitAssign(ast::AssignmentStatement* stmt); + bool EmitAssign(const ast::AssignmentStatement* stmt); /// Handles generating a binary expression /// @param out the output of the expression stream /// @param expr the binary expression /// @returns true if the expression was emitted, false otherwise - bool EmitBinary(std::ostream& out, ast::BinaryExpression* expr); + bool EmitBinary(std::ostream& out, const ast::BinaryExpression* expr); /// Handles generating a bitcast expression /// @param out the output of the expression stream /// @param expr the as expression /// @returns true if the bitcast was emitted - bool EmitBitcast(std::ostream& out, ast::BitcastExpression* expr); + bool EmitBitcast(std::ostream& out, const ast::BitcastExpression* expr); /// Emits a list of statements /// @param stmts the statement list /// @returns true if the statements were emitted successfully @@ -93,12 +94,12 @@ class GeneratorImpl : public TextGenerator { /// Handles a break statement /// @param stmt the statement to emit /// @returns true if the statement was emitted successfully - bool EmitBreak(ast::BreakStatement* stmt); + bool EmitBreak(const ast::BreakStatement* stmt); /// Handles generating a call expression /// @param out the output of the expression stream /// @param expr the call expression /// @returns true if the call expression is emitted - bool EmitCall(std::ostream& out, ast::CallExpression* expr); + bool EmitCall(std::ostream& out, const ast::CallExpression* expr); /// Handles generating a barrier intrinsic call /// @param out the output of the expression stream /// @param intrinsic the semantic information for the barrier intrinsic @@ -111,7 +112,7 @@ class GeneratorImpl : public TextGenerator { /// @returns true if the call expression is emitted bool EmitStorageAtomicCall( std::ostream& out, - ast::CallExpression* expr, + const ast::CallExpression* expr, const transform::DecomposeMemoryAccess::Intrinsic* intrinsic); /// Handles generating an atomic intrinsic call for a workgroup variable /// @param out the output of the expression stream @@ -119,7 +120,7 @@ class GeneratorImpl : public TextGenerator { /// @param intrinsic the semantic information for the atomic intrinsic /// @returns true if the call expression is emitted bool EmitWorkgroupAtomicCall(std::ostream& out, - ast::CallExpression* expr, + const ast::CallExpression* expr, const sem::Intrinsic* intrinsic); /// Handles generating a call to a texture function (`textureSample`, /// `textureSampleGrad`, etc) @@ -128,20 +129,20 @@ class GeneratorImpl : public TextGenerator { /// @param intrinsic the semantic information for the texture intrinsic /// @returns true if the call expression is emitted bool EmitTextureCall(std::ostream& out, - ast::CallExpression* expr, + const ast::CallExpression* expr, const sem::Intrinsic* intrinsic); /// Handles generating a call to the `select()` intrinsic /// @param out the output of the expression stream /// @param expr the call expression /// @returns true if the call expression is emitted - bool EmitSelectCall(std::ostream& out, ast::CallExpression* expr); + bool EmitSelectCall(std::ostream& out, const ast::CallExpression* expr); /// Handles generating a call to the `modf()` intrinsic /// @param out the output of the expression stream /// @param expr the call expression /// @param intrinsic the semantic information for the intrinsic /// @returns true if the call expression is emitted bool EmitModfCall(std::ostream& out, - ast::CallExpression* expr, + const ast::CallExpression* expr, const sem::Intrinsic* intrinsic); /// Handles generating a call to the `frexp()` intrinsic /// @param out the output of the expression stream @@ -149,7 +150,7 @@ class GeneratorImpl : public TextGenerator { /// @param intrinsic the semantic information for the intrinsic /// @returns true if the call expression is emitted bool EmitFrexpCall(std::ostream& out, - ast::CallExpression* expr, + const ast::CallExpression* expr, const sem::Intrinsic* intrinsic); /// Handles generating a call to the `isNormal()` intrinsic /// @param out the output of the expression stream @@ -157,7 +158,7 @@ class GeneratorImpl : public TextGenerator { /// @param intrinsic the semantic information for the intrinsic /// @returns true if the call expression is emitted bool EmitIsNormalCall(std::ostream& out, - ast::CallExpression* expr, + const ast::CallExpression* expr, const sem::Intrinsic* intrinsic); /// Handles generating a call to data packing intrinsic /// @param out the output of the expression stream @@ -165,7 +166,7 @@ class GeneratorImpl : public TextGenerator { /// @param intrinsic the semantic information for the texture intrinsic /// @returns true if the call expression is emitted bool EmitDataPackingCall(std::ostream& out, - ast::CallExpression* expr, + const ast::CallExpression* expr, const sem::Intrinsic* intrinsic); /// Handles generating a call to data unpacking intrinsic /// @param out the output of the expression stream @@ -173,51 +174,52 @@ class GeneratorImpl : public TextGenerator { /// @param intrinsic the semantic information for the texture intrinsic /// @returns true if the call expression is emitted bool EmitDataUnpackingCall(std::ostream& out, - ast::CallExpression* expr, + const ast::CallExpression* expr, const sem::Intrinsic* intrinsic); /// Handles a case statement /// @param stmt the statement /// @returns true if the statement was emitted successfully - bool EmitCase(ast::CaseStatement* stmt); + bool EmitCase(const ast::CaseStatement* stmt); /// Handles generating constructor expressions /// @param out the output of the expression stream /// @param expr the constructor expression /// @returns true if the expression was emitted - bool EmitConstructor(std::ostream& out, ast::ConstructorExpression* expr); + bool EmitConstructor(std::ostream& out, + const ast::ConstructorExpression* expr); /// Handles generating a discard statement /// @param stmt the discard statement /// @returns true if the statement was successfully emitted - bool EmitDiscard(ast::DiscardStatement* stmt); + bool EmitDiscard(const ast::DiscardStatement* stmt); /// Handles generating a scalar constructor /// @param out the output of the expression stream /// @param expr the scalar constructor expression /// @returns true if the scalar constructor is emitted bool EmitScalarConstructor(std::ostream& out, - ast::ScalarConstructorExpression* expr); + const ast::ScalarConstructorExpression* expr); /// Handles emitting a type constructor /// @param out the output of the expression stream /// @param expr the type constructor expression /// @returns true if the constructor is emitted bool EmitTypeConstructor(std::ostream& out, - ast::TypeConstructorExpression* expr); + const ast::TypeConstructorExpression* expr); /// Handles a continue statement /// @param stmt the statement to emit /// @returns true if the statement was emitted successfully - bool EmitContinue(ast::ContinueStatement* stmt); + bool EmitContinue(const ast::ContinueStatement* stmt); /// Handles generate an Expression /// @param out the output of the expression stream /// @param expr the expression /// @returns true if the expression was emitted - bool EmitExpression(std::ostream& out, ast::Expression* expr); + bool EmitExpression(std::ostream& out, const ast::Expression* expr); /// Handles generating a function /// @param func the function to generate /// @returns true if the function was emitted - bool EmitFunction(ast::Function* func); + bool EmitFunction(const ast::Function* func); /// Handles emitting a global variable /// @param global the global variable /// @returns true on success - bool EmitGlobalVariable(ast::Variable* global); + bool EmitGlobalVariable(const ast::Variable* global); /// Handles emitting a global variable with the uniform storage class /// @param var the global variable @@ -247,47 +249,47 @@ class GeneratorImpl : public TextGenerator { /// Handles emitting the entry point function /// @param func the entry point /// @returns true if the entry point function was emitted - bool EmitEntryPointFunction(ast::Function* func); + bool EmitEntryPointFunction(const ast::Function* func); /// Handles an if statement /// @param stmt the statement to emit /// @returns true if the statement was successfully emitted - bool EmitIf(ast::IfStatement* stmt); + bool EmitIf(const ast::IfStatement* stmt); /// Handles a literal /// @param out the output stream /// @param lit the literal to emit /// @returns true if the literal was successfully emitted - bool EmitLiteral(std::ostream& out, ast::Literal* lit); + bool EmitLiteral(std::ostream& out, const ast::Literal* lit); /// Handles a loop statement /// @param stmt the statement to emit /// @returns true if the statement was emitted - bool EmitLoop(ast::LoopStatement* stmt); + bool EmitLoop(const ast::LoopStatement* stmt); /// Handles a for loop statement /// @param stmt the statement to emit /// @returns true if the statement was emitted - bool EmitForLoop(ast::ForLoopStatement* stmt); + bool EmitForLoop(const ast::ForLoopStatement* stmt); /// Handles generating an identifier expression /// @param out the output of the expression stream /// @param expr the identifier expression /// @returns true if the identifeir was emitted - bool EmitIdentifier(std::ostream& out, ast::IdentifierExpression* expr); + bool EmitIdentifier(std::ostream& out, const ast::IdentifierExpression* expr); /// Handles a member accessor expression /// @param out the output of the expression stream /// @param expr the member accessor expression /// @returns true if the member accessor was emitted bool EmitMemberAccessor(std::ostream& out, - ast::MemberAccessorExpression* expr); + const ast::MemberAccessorExpression* expr); /// Handles return statements /// @param stmt the statement to emit /// @returns true if the statement was successfully emitted - bool EmitReturn(ast::ReturnStatement* stmt); + bool EmitReturn(const ast::ReturnStatement* stmt); /// Handles statement /// @param stmt the statement to emit /// @returns true if the statement was emitted - bool EmitStatement(ast::Statement* stmt); + bool EmitStatement(const ast::Statement* stmt); /// Handles generating a switch statement /// @param stmt the statement to emit /// @returns true if the statement was emitted - bool EmitSwitch(ast::SwitchStatement* stmt); + bool EmitSwitch(const ast::SwitchStatement* stmt); /// Handles generating type /// @param out the output stream /// @param type the type to generate @@ -324,7 +326,7 @@ class GeneratorImpl : public TextGenerator { /// @param out the output of the expression stream /// @param expr the expression to emit /// @returns true if the expression was emitted - bool EmitUnaryOp(std::ostream& out, ast::UnaryOpExpression* expr); + bool EmitUnaryOp(std::ostream& out, const ast::UnaryOpExpression* expr); /// Emits the zero value for the given type /// @param out the output stream /// @param type the type to emit the value for @@ -333,7 +335,7 @@ class GeneratorImpl : public TextGenerator { /// Handles generating a variable /// @param var the variable to generate /// @returns true if the variable was emitted - bool EmitVariable(ast::Variable* var); + bool EmitVariable(const ast::Variable* var); /// Handles generating a program scope constant variable /// @param var the variable to emit /// @returns true if the variable was emitted @@ -398,7 +400,7 @@ class GeneratorImpl : public TextGenerator { /// @returns true if the call expression is emitted template bool CallIntrinsicHelper(std::ostream& out, - ast::CallExpression* call, + const ast::CallExpression* call, const sem::Intrinsic* intrinsic, F&& build); diff --git a/src/writer/glsl/generator_impl_intrinsic_test.cc b/src/writer/glsl/generator_impl_intrinsic_test.cc index 35333de19a..a499b4b7d5 100644 --- a/src/writer/glsl/generator_impl_intrinsic_test.cc +++ b/src/writer/glsl/generator_impl_intrinsic_test.cc @@ -57,9 +57,9 @@ inline std::ostream& operator<<(std::ostream& out, IntrinsicData data) { return out; } -ast::CallExpression* GenerateCall(IntrinsicType intrinsic, - ParamType type, - ProgramBuilder* builder) { +const ast::CallExpression* GenerateCall(IntrinsicType intrinsic, + ParamType type, + ProgramBuilder* builder) { std::string name; std::ostringstream str(name); str << intrinsic; diff --git a/src/writer/glsl/generator_impl_member_accessor_test.cc b/src/writer/glsl/generator_impl_member_accessor_test.cc index a7c09255a9..b297bf3386 100644 --- a/src/writer/glsl/generator_impl_member_accessor_test.cc +++ b/src/writer/glsl/generator_impl_member_accessor_test.cc @@ -25,63 +25,63 @@ namespace { using ::testing::HasSubstr; using create_type_func_ptr = - ast::Type* (*)(const ProgramBuilder::TypesBuilder& ty); + const ast::Type* (*)(const ProgramBuilder::TypesBuilder& ty); -inline ast::Type* ty_i32(const ProgramBuilder::TypesBuilder& ty) { +inline const ast::Type* ty_i32(const ProgramBuilder::TypesBuilder& ty) { return ty.i32(); } -inline ast::Type* ty_u32(const ProgramBuilder::TypesBuilder& ty) { +inline const ast::Type* ty_u32(const ProgramBuilder::TypesBuilder& ty) { return ty.u32(); } -inline ast::Type* ty_f32(const ProgramBuilder::TypesBuilder& ty) { +inline const ast::Type* ty_f32(const ProgramBuilder::TypesBuilder& ty) { return ty.f32(); } template -inline ast::Type* ty_vec2(const ProgramBuilder::TypesBuilder& ty) { +inline const ast::Type* ty_vec2(const ProgramBuilder::TypesBuilder& ty) { return ty.vec2(); } template -inline ast::Type* ty_vec3(const ProgramBuilder::TypesBuilder& ty) { +inline const ast::Type* ty_vec3(const ProgramBuilder::TypesBuilder& ty) { return ty.vec3(); } template -inline ast::Type* ty_vec4(const ProgramBuilder::TypesBuilder& ty) { +inline const ast::Type* ty_vec4(const ProgramBuilder::TypesBuilder& ty) { return ty.vec4(); } template -inline ast::Type* ty_mat2x2(const ProgramBuilder::TypesBuilder& ty) { +inline const ast::Type* ty_mat2x2(const ProgramBuilder::TypesBuilder& ty) { return ty.mat2x2(); } template -inline ast::Type* ty_mat2x3(const ProgramBuilder::TypesBuilder& ty) { +inline const ast::Type* ty_mat2x3(const ProgramBuilder::TypesBuilder& ty) { return ty.mat2x3(); } template -inline ast::Type* ty_mat2x4(const ProgramBuilder::TypesBuilder& ty) { +inline const ast::Type* ty_mat2x4(const ProgramBuilder::TypesBuilder& ty) { return ty.mat2x4(); } template -inline ast::Type* ty_mat3x2(const ProgramBuilder::TypesBuilder& ty) { +inline const ast::Type* ty_mat3x2(const ProgramBuilder::TypesBuilder& ty) { return ty.mat3x2(); } template -inline ast::Type* ty_mat3x3(const ProgramBuilder::TypesBuilder& ty) { +inline const ast::Type* ty_mat3x3(const ProgramBuilder::TypesBuilder& ty) { return ty.mat3x3(); } template -inline ast::Type* ty_mat3x4(const ProgramBuilder::TypesBuilder& ty) { +inline const ast::Type* ty_mat3x4(const ProgramBuilder::TypesBuilder& ty) { return ty.mat3x4(); } template -inline ast::Type* ty_mat4x2(const ProgramBuilder::TypesBuilder& ty) { +inline const ast::Type* ty_mat4x2(const ProgramBuilder::TypesBuilder& ty) { return ty.mat4x2(); } template -inline ast::Type* ty_mat4x3(const ProgramBuilder::TypesBuilder& ty) { +inline const ast::Type* ty_mat4x3(const ProgramBuilder::TypesBuilder& ty) { return ty.mat4x3(); } template -inline ast::Type* ty_mat4x4(const ProgramBuilder::TypesBuilder& ty) { +inline const ast::Type* ty_mat4x4(const ProgramBuilder::TypesBuilder& ty) { return ty.mat4x4(); } diff --git a/src/writer/glsl/generator_impl_type_test.cc b/src/writer/glsl/generator_impl_type_test.cc index 2481f6ff6d..74a32c27c8 100644 --- a/src/writer/glsl/generator_impl_type_test.cc +++ b/src/writer/glsl/generator_impl_type_test.cc @@ -372,7 +372,7 @@ using GlslSampledTexturesTest = TestParamHelper; TEST_P(GlslSampledTexturesTest, Emit) { auto params = GetParam(); - ast::Type* datatype = nullptr; + const ast::Type* datatype = nullptr; switch (params.datatype) { case TextureDataType::F32: datatype = ty.f32(); diff --git a/src/writer/hlsl/generator_impl.cc b/src/writer/hlsl/generator_impl.cc index b690e89bed..f0dee544d3 100644 --- a/src/writer/hlsl/generator_impl.cc +++ b/src/writer/hlsl/generator_impl.cc @@ -102,7 +102,7 @@ struct RegisterAndSpace { RegisterAndSpace(char r, ast::VariableBindingPoint bp) : reg(r), binding_point(bp) {} - char const reg; + const char reg; ast::VariableBindingPoint const binding_point; }; @@ -306,8 +306,9 @@ bool GeneratorImpl::EmitDynamicVectorAssignment( return true; } -bool GeneratorImpl::EmitArrayAccessor(std::ostream& out, - ast::ArrayAccessorExpression* expr) { +bool GeneratorImpl::EmitArrayAccessor( + std::ostream& out, + const ast::ArrayAccessorExpression* expr) { if (!EmitExpression(out, expr->array)) { return false; } @@ -322,7 +323,7 @@ bool GeneratorImpl::EmitArrayAccessor(std::ostream& out, } bool GeneratorImpl::EmitBitcast(std::ostream& out, - ast::BitcastExpression* expr) { + const ast::BitcastExpression* expr) { auto* type = TypeOf(expr); if (auto* vec = type->UnwrapRef()->As()) { type = vec->type(); @@ -347,7 +348,7 @@ bool GeneratorImpl::EmitBitcast(std::ostream& out, return true; } -bool GeneratorImpl::EmitAssign(ast::AssignmentStatement* stmt) { +bool GeneratorImpl::EmitAssign(const ast::AssignmentStatement* stmt) { if (auto* idx = stmt->lhs->As()) { if (auto* vec = TypeOf(idx->array)->UnwrapRef()->As()) { auto* rhs_sem = builder_.Sem().Get(idx->index); @@ -369,7 +370,8 @@ bool GeneratorImpl::EmitAssign(ast::AssignmentStatement* stmt) { return true; } -bool GeneratorImpl::EmitBinary(std::ostream& out, ast::BinaryExpression* expr) { +bool GeneratorImpl::EmitBinary(std::ostream& out, + const ast::BinaryExpression* expr) { if (expr->op == ast::BinaryOp::kLogicalAnd || expr->op == ast::BinaryOp::kLogicalOr) { auto name = UniqueIdentifier(kTempNamePrefix); @@ -545,12 +547,13 @@ bool GeneratorImpl::EmitBlock(const ast::BlockStatement* stmt) { return true; } -bool GeneratorImpl::EmitBreak(ast::BreakStatement*) { +bool GeneratorImpl::EmitBreak(const ast::BreakStatement*) { line() << "break;"; return true; } -bool GeneratorImpl::EmitCall(std::ostream& out, ast::CallExpression* expr) { +bool GeneratorImpl::EmitCall(std::ostream& out, + const ast::CallExpression* expr) { const auto& args = expr->args; auto* ident = expr->func; auto* call = builder_.Sem().Get(expr); @@ -667,7 +670,7 @@ bool GeneratorImpl::EmitCall(std::ostream& out, ast::CallExpression* expr) { bool GeneratorImpl::EmitUniformBufferAccess( std::ostream& out, - ast::CallExpression* expr, + const ast::CallExpression* expr, const transform::DecomposeMemoryAccess::Intrinsic* intrinsic) { const auto& args = expr->args; auto* offset_arg = builder_.Sem().Get(args[1]); @@ -806,7 +809,7 @@ bool GeneratorImpl::EmitUniformBufferAccess( bool GeneratorImpl::EmitStorageBufferAccess( std::ostream& out, - ast::CallExpression* expr, + const ast::CallExpression* expr, const transform::DecomposeMemoryAccess::Intrinsic* intrinsic) { const auto& args = expr->args; @@ -940,7 +943,7 @@ bool GeneratorImpl::EmitStorageBufferAccess( bool GeneratorImpl::EmitStorageAtomicCall( std::ostream& out, - ast::CallExpression* expr, + const ast::CallExpression* expr, const transform::DecomposeMemoryAccess::Intrinsic* intrinsic) { using Op = transform::DecomposeMemoryAccess::Intrinsic::Op; @@ -1166,7 +1169,7 @@ bool GeneratorImpl::EmitStorageAtomicCall( } bool GeneratorImpl::EmitWorkgroupAtomicCall(std::ostream& out, - ast::CallExpression* expr, + const ast::CallExpression* expr, const sem::Intrinsic* intrinsic) { std::string result = UniqueIdentifier("atomic_result"); @@ -1339,7 +1342,7 @@ bool GeneratorImpl::EmitWorkgroupAtomicCall(std::ostream& out, } bool GeneratorImpl::EmitSelectCall(std::ostream& out, - ast::CallExpression* expr) { + const ast::CallExpression* expr) { auto* expr_false = expr->args[0]; auto* expr_true = expr->args[1]; auto* expr_cond = expr->args[2]; @@ -1364,7 +1367,7 @@ bool GeneratorImpl::EmitSelectCall(std::ostream& out, } bool GeneratorImpl::EmitModfCall(std::ostream& out, - ast::CallExpression* expr, + const ast::CallExpression* expr, const sem::Intrinsic* intrinsic) { return CallIntrinsicHelper( out, expr, intrinsic, @@ -1400,7 +1403,7 @@ bool GeneratorImpl::EmitModfCall(std::ostream& out, } bool GeneratorImpl::EmitFrexpCall(std::ostream& out, - ast::CallExpression* expr, + const ast::CallExpression* expr, const sem::Intrinsic* intrinsic) { return CallIntrinsicHelper( out, expr, intrinsic, @@ -1436,7 +1439,7 @@ bool GeneratorImpl::EmitFrexpCall(std::ostream& out, } bool GeneratorImpl::EmitIsNormalCall(std::ostream& out, - ast::CallExpression* expr, + const ast::CallExpression* expr, const sem::Intrinsic* intrinsic) { // HLSL doesn't have a isNormal intrinsic, we need to emulate return CallIntrinsicHelper( @@ -1464,7 +1467,7 @@ bool GeneratorImpl::EmitIsNormalCall(std::ostream& out, } bool GeneratorImpl::EmitDataPackingCall(std::ostream& out, - ast::CallExpression* expr, + const ast::CallExpression* expr, const sem::Intrinsic* intrinsic) { return CallIntrinsicHelper( out, expr, intrinsic, @@ -1530,7 +1533,7 @@ bool GeneratorImpl::EmitDataPackingCall(std::ostream& out, } bool GeneratorImpl::EmitDataUnpackingCall(std::ostream& out, - ast::CallExpression* expr, + const ast::CallExpression* expr, const sem::Intrinsic* intrinsic) { return CallIntrinsicHelper( out, expr, intrinsic, @@ -1616,7 +1619,7 @@ bool GeneratorImpl::EmitBarrierCall(std::ostream& out, } bool GeneratorImpl::EmitTextureCall(std::ostream& out, - ast::CallExpression* expr, + const ast::CallExpression* expr, const sem::Intrinsic* intrinsic) { using Usage = sem::ParameterUsage; @@ -1877,7 +1880,7 @@ bool GeneratorImpl::EmitTextureCall(std::ostream& out, return false; } - auto emit_vector_appended_with_i32_zero = [&](tint::ast::Expression* vector) { + auto emit_vector_appended_with_i32_zero = [&](const ast::Expression* vector) { auto* i32 = builder_.create(); auto* zero = builder_.Expr(0); auto* stmt = builder_.Sem().Get(vector)->Stmt(); @@ -1887,7 +1890,7 @@ bool GeneratorImpl::EmitTextureCall(std::ostream& out, return EmitExpression(out, packed); }; - auto emit_vector_appended_with_level = [&](tint::ast::Expression* vector) { + auto emit_vector_appended_with_level = [&](const ast::Expression* vector) { if (auto* level = arg(Usage::kLevel)) { auto* packed = AppendVector(&builder_, vector, level); return EmitExpression(out, packed); @@ -2056,7 +2059,7 @@ std::string GeneratorImpl::generate_builtin_name( return ""; } -bool GeneratorImpl::EmitCase(ast::SwitchStatement* s, size_t case_idx) { +bool GeneratorImpl::EmitCase(const ast::SwitchStatement* s, size_t case_idx) { auto* stmt = s->body[case_idx]; if (stmt->IsDefault()) { line() << "default: {"; @@ -2105,7 +2108,7 @@ bool GeneratorImpl::EmitCase(ast::SwitchStatement* s, size_t case_idx) { } bool GeneratorImpl::EmitConstructor(std::ostream& out, - ast::ConstructorExpression* expr) { + const ast::ConstructorExpression* expr) { if (auto* scalar = expr->As()) { return EmitScalarConstructor(out, scalar); } @@ -2114,12 +2117,13 @@ bool GeneratorImpl::EmitConstructor(std::ostream& out, bool GeneratorImpl::EmitScalarConstructor( std::ostream& out, - ast::ScalarConstructorExpression* expr) { + const ast::ScalarConstructorExpression* expr) { return EmitLiteral(out, expr->literal); } -bool GeneratorImpl::EmitTypeConstructor(std::ostream& out, - ast::TypeConstructorExpression* expr) { +bool GeneratorImpl::EmitTypeConstructor( + std::ostream& out, + const ast::TypeConstructorExpression* expr) { auto* type = TypeOf(expr)->UnwrapRef(); // If the type constructor is empty then we need to construct with the zero @@ -2174,7 +2178,7 @@ bool GeneratorImpl::EmitTypeConstructor(std::ostream& out, return true; } -bool GeneratorImpl::EmitContinue(ast::ContinueStatement*) { +bool GeneratorImpl::EmitContinue(const ast::ContinueStatement*) { if (!emit_continuing_()) { return false; } @@ -2182,14 +2186,15 @@ bool GeneratorImpl::EmitContinue(ast::ContinueStatement*) { return true; } -bool GeneratorImpl::EmitDiscard(ast::DiscardStatement*) { +bool GeneratorImpl::EmitDiscard(const ast::DiscardStatement*) { // TODO(dsinclair): Verify this is correct when the discard semantics are // defined for WGSL (https://github.com/gpuweb/gpuweb/issues/361) line() << "discard;"; return true; } -bool GeneratorImpl::EmitExpression(std::ostream& out, ast::Expression* expr) { +bool GeneratorImpl::EmitExpression(std::ostream& out, + const ast::Expression* expr) { if (auto* a = expr->As()) { return EmitArrayAccessor(out, a); } @@ -2222,12 +2227,12 @@ bool GeneratorImpl::EmitExpression(std::ostream& out, ast::Expression* expr) { } bool GeneratorImpl::EmitIdentifier(std::ostream& out, - ast::IdentifierExpression* expr) { + const ast::IdentifierExpression* expr) { out << builder_.Symbols().NameFor(expr->symbol); return true; } -bool GeneratorImpl::EmitIf(ast::IfStatement* stmt) { +bool GeneratorImpl::EmitIf(const ast::IfStatement* stmt) { { auto out = line(); out << "if ("; @@ -2274,7 +2279,7 @@ bool GeneratorImpl::EmitIf(ast::IfStatement* stmt) { return true; } -bool GeneratorImpl::EmitFunction(ast::Function* func) { +bool GeneratorImpl::EmitFunction(const ast::Function* func) { auto* sem = builder_.Sem().Get(func); if (ast::HasDecoration(func->decorations)) { @@ -2349,7 +2354,7 @@ bool GeneratorImpl::EmitFunction(ast::Function* func) { return true; } -bool GeneratorImpl::EmitGlobalVariable(ast::Variable* global) { +bool GeneratorImpl::EmitGlobalVariable(const ast::Variable* global) { if (global->is_const) { return EmitProgramConstVariable(global); } @@ -2563,7 +2568,7 @@ std::string GeneratorImpl::interpolation_to_modifiers( return modifiers; } -bool GeneratorImpl::EmitEntryPointFunction(ast::Function* func) { +bool GeneratorImpl::EmitEntryPointFunction(const ast::Function* func) { auto* func_sem = builder_.Sem().Get(func); { @@ -2643,7 +2648,7 @@ bool GeneratorImpl::EmitEntryPointFunction(ast::Function* func) { return true; } -bool GeneratorImpl::EmitLiteral(std::ostream& out, ast::Literal* lit) { +bool GeneratorImpl::EmitLiteral(std::ostream& out, const ast::Literal* lit) { if (auto* l = lit->As()) { out << (l->value ? "true" : "false"); } else if (auto* fl = lit->As()) { @@ -2718,7 +2723,7 @@ bool GeneratorImpl::EmitZeroValue(std::ostream& out, const sem::Type* type) { return true; } -bool GeneratorImpl::EmitLoop(ast::LoopStatement* stmt) { +bool GeneratorImpl::EmitLoop(const ast::LoopStatement* stmt) { auto emit_continuing = [this, stmt]() { if (stmt->continuing && !stmt->continuing->Empty()) { if (!EmitBlock(stmt->continuing)) { @@ -2744,7 +2749,7 @@ bool GeneratorImpl::EmitLoop(ast::LoopStatement* stmt) { return true; } -bool GeneratorImpl::EmitForLoop(ast::ForLoopStatement* stmt) { +bool GeneratorImpl::EmitForLoop(const ast::ForLoopStatement* stmt) { // Nest a for loop with a new block. In HLSL the initializer scope is not // nested by the for-loop, so we may get variable redefinitions. line() << "{"; @@ -2853,8 +2858,9 @@ bool GeneratorImpl::EmitForLoop(ast::ForLoopStatement* stmt) { return true; } -bool GeneratorImpl::EmitMemberAccessor(std::ostream& out, - ast::MemberAccessorExpression* expr) { +bool GeneratorImpl::EmitMemberAccessor( + std::ostream& out, + const ast::MemberAccessorExpression* expr) { if (!EmitExpression(out, expr->structure)) { return false; } @@ -2870,7 +2876,7 @@ bool GeneratorImpl::EmitMemberAccessor(std::ostream& out, return true; } -bool GeneratorImpl::EmitReturn(ast::ReturnStatement* stmt) { +bool GeneratorImpl::EmitReturn(const ast::ReturnStatement* stmt) { if (stmt->value) { auto out = line(); out << "return "; @@ -2884,7 +2890,7 @@ bool GeneratorImpl::EmitReturn(ast::ReturnStatement* stmt) { return true; } -bool GeneratorImpl::EmitStatement(ast::Statement* stmt) { +bool GeneratorImpl::EmitStatement(const ast::Statement* stmt) { if (auto* a = stmt->As()) { return EmitAssign(a); } @@ -2940,7 +2946,7 @@ bool GeneratorImpl::EmitStatement(ast::Statement* stmt) { return false; } -bool GeneratorImpl::EmitSwitch(ast::SwitchStatement* stmt) { +bool GeneratorImpl::EmitSwitch(const ast::SwitchStatement* stmt) { { // switch(expr) { auto out = line(); out << "switch("; @@ -3252,7 +3258,7 @@ bool GeneratorImpl::EmitStructType(TextBuffer* b, const sem::Struct* str) { } bool GeneratorImpl::EmitUnaryOp(std::ostream& out, - ast::UnaryOpExpression* expr) { + const ast::UnaryOpExpression* expr) { switch (expr->op) { case ast::UnaryOp::kIndirection: case ast::UnaryOp::kAddressOf: @@ -3278,7 +3284,7 @@ bool GeneratorImpl::EmitUnaryOp(std::ostream& out, return true; } -bool GeneratorImpl::EmitVariable(ast::Variable* var) { +bool GeneratorImpl::EmitVariable(const ast::Variable* var) { auto* sem = builder_.Sem().Get(var); auto* type = sem->Type()->UnwrapRef(); @@ -3374,7 +3380,7 @@ bool GeneratorImpl::EmitProgramConstVariable(const ast::Variable* var) { template bool GeneratorImpl::CallIntrinsicHelper(std::ostream& out, - ast::CallExpression* call, + const ast::CallExpression* call, const sem::Intrinsic* intrinsic, F&& build) { // Generate the helper function if it hasn't been created already diff --git a/src/writer/hlsl/generator_impl.h b/src/writer/hlsl/generator_impl.h index 5717d71d13..57d9cb848a 100644 --- a/src/writer/hlsl/generator_impl.h +++ b/src/writer/hlsl/generator_impl.h @@ -79,21 +79,22 @@ class GeneratorImpl : public TextGenerator { /// @param out the output of the expression stream /// @param expr the expression to emit /// @returns true if the array accessor was emitted - bool EmitArrayAccessor(std::ostream& out, ast::ArrayAccessorExpression* expr); + bool EmitArrayAccessor(std::ostream& out, + const ast::ArrayAccessorExpression* expr); /// Handles an assignment statement /// @param stmt the statement to emit /// @returns true if the statement was emitted successfully - bool EmitAssign(ast::AssignmentStatement* stmt); + bool EmitAssign(const ast::AssignmentStatement* stmt); /// Handles generating a binary expression /// @param out the output of the expression stream /// @param expr the binary expression /// @returns true if the expression was emitted, false otherwise - bool EmitBinary(std::ostream& out, ast::BinaryExpression* expr); + bool EmitBinary(std::ostream& out, const ast::BinaryExpression* expr); /// Handles generating a bitcast expression /// @param out the output of the expression stream /// @param expr the as expression /// @returns true if the bitcast was emitted - bool EmitBitcast(std::ostream& out, ast::BitcastExpression* expr); + bool EmitBitcast(std::ostream& out, const ast::BitcastExpression* expr); /// Emits a list of statements /// @param stmts the statement list /// @returns true if the statements were emitted successfully @@ -109,12 +110,12 @@ class GeneratorImpl : public TextGenerator { /// Handles a break statement /// @param stmt the statement to emit /// @returns true if the statement was emitted successfully - bool EmitBreak(ast::BreakStatement* stmt); + bool EmitBreak(const ast::BreakStatement* stmt); /// Handles generating a call expression /// @param out the output of the expression stream /// @param expr the call expression /// @returns true if the call expression is emitted - bool EmitCall(std::ostream& out, ast::CallExpression* expr); + bool EmitCall(std::ostream& out, const ast::CallExpression* expr); /// Handles generating a call expression to a /// transform::DecomposeMemoryAccess::Intrinsic for a uniform buffer /// @param out the output of the expression stream @@ -123,7 +124,7 @@ class GeneratorImpl : public TextGenerator { /// @returns true if the call expression is emitted bool EmitUniformBufferAccess( std::ostream& out, - ast::CallExpression* expr, + const ast::CallExpression* expr, const transform::DecomposeMemoryAccess::Intrinsic* intrinsic); /// Handles generating a call expression to a /// transform::DecomposeMemoryAccess::Intrinsic for a storage buffer @@ -133,7 +134,7 @@ class GeneratorImpl : public TextGenerator { /// @returns true if the call expression is emitted bool EmitStorageBufferAccess( std::ostream& out, - ast::CallExpression* expr, + const ast::CallExpression* expr, const transform::DecomposeMemoryAccess::Intrinsic* intrinsic); /// Handles generating a barrier intrinsic call /// @param out the output of the expression stream @@ -147,7 +148,7 @@ class GeneratorImpl : public TextGenerator { /// @returns true if the call expression is emitted bool EmitStorageAtomicCall( std::ostream& out, - ast::CallExpression* expr, + const ast::CallExpression* expr, const transform::DecomposeMemoryAccess::Intrinsic* intrinsic); /// Handles generating an atomic intrinsic call for a workgroup variable /// @param out the output of the expression stream @@ -155,7 +156,7 @@ class GeneratorImpl : public TextGenerator { /// @param intrinsic the semantic information for the atomic intrinsic /// @returns true if the call expression is emitted bool EmitWorkgroupAtomicCall(std::ostream& out, - ast::CallExpression* expr, + const ast::CallExpression* expr, const sem::Intrinsic* intrinsic); /// Handles generating a call to a texture function (`textureSample`, /// `textureSampleGrad`, etc) @@ -164,20 +165,20 @@ class GeneratorImpl : public TextGenerator { /// @param intrinsic the semantic information for the texture intrinsic /// @returns true if the call expression is emitted bool EmitTextureCall(std::ostream& out, - ast::CallExpression* expr, + const ast::CallExpression* expr, const sem::Intrinsic* intrinsic); /// Handles generating a call to the `select()` intrinsic /// @param out the output of the expression stream /// @param expr the call expression /// @returns true if the call expression is emitted - bool EmitSelectCall(std::ostream& out, ast::CallExpression* expr); + bool EmitSelectCall(std::ostream& out, const ast::CallExpression* expr); /// Handles generating a call to the `modf()` intrinsic /// @param out the output of the expression stream /// @param expr the call expression /// @param intrinsic the semantic information for the intrinsic /// @returns true if the call expression is emitted bool EmitModfCall(std::ostream& out, - ast::CallExpression* expr, + const ast::CallExpression* expr, const sem::Intrinsic* intrinsic); /// Handles generating a call to the `frexp()` intrinsic /// @param out the output of the expression stream @@ -185,7 +186,7 @@ class GeneratorImpl : public TextGenerator { /// @param intrinsic the semantic information for the intrinsic /// @returns true if the call expression is emitted bool EmitFrexpCall(std::ostream& out, - ast::CallExpression* expr, + const ast::CallExpression* expr, const sem::Intrinsic* intrinsic); /// Handles generating a call to the `isNormal()` intrinsic /// @param out the output of the expression stream @@ -193,7 +194,7 @@ class GeneratorImpl : public TextGenerator { /// @param intrinsic the semantic information for the intrinsic /// @returns true if the call expression is emitted bool EmitIsNormalCall(std::ostream& out, - ast::CallExpression* expr, + const ast::CallExpression* expr, const sem::Intrinsic* intrinsic); /// Handles generating a call to data packing intrinsic /// @param out the output of the expression stream @@ -201,7 +202,7 @@ class GeneratorImpl : public TextGenerator { /// @param intrinsic the semantic information for the texture intrinsic /// @returns true if the call expression is emitted bool EmitDataPackingCall(std::ostream& out, - ast::CallExpression* expr, + const ast::CallExpression* expr, const sem::Intrinsic* intrinsic); /// Handles generating a call to data unpacking intrinsic /// @param out the output of the expression stream @@ -209,52 +210,53 @@ class GeneratorImpl : public TextGenerator { /// @param intrinsic the semantic information for the texture intrinsic /// @returns true if the call expression is emitted bool EmitDataUnpackingCall(std::ostream& out, - ast::CallExpression* expr, + const ast::CallExpression* expr, const sem::Intrinsic* intrinsic); /// Handles a case statement /// @param s the switch statement /// @param case_idx the index of the switch case in the switch statement /// @returns true if the statement was emitted successfully - bool EmitCase(ast::SwitchStatement* s, size_t case_idx); + bool EmitCase(const ast::SwitchStatement* s, size_t case_idx); /// Handles generating constructor expressions /// @param out the output of the expression stream /// @param expr the constructor expression /// @returns true if the expression was emitted - bool EmitConstructor(std::ostream& out, ast::ConstructorExpression* expr); + bool EmitConstructor(std::ostream& out, + const ast::ConstructorExpression* expr); /// Handles generating a discard statement /// @param stmt the discard statement /// @returns true if the statement was successfully emitted - bool EmitDiscard(ast::DiscardStatement* stmt); + bool EmitDiscard(const ast::DiscardStatement* stmt); /// Handles generating a scalar constructor /// @param out the output of the expression stream /// @param expr the scalar constructor expression /// @returns true if the scalar constructor is emitted bool EmitScalarConstructor(std::ostream& out, - ast::ScalarConstructorExpression* expr); + const ast::ScalarConstructorExpression* expr); /// Handles emitting a type constructor /// @param out the output of the expression stream /// @param expr the type constructor expression /// @returns true if the constructor is emitted bool EmitTypeConstructor(std::ostream& out, - ast::TypeConstructorExpression* expr); + const ast::TypeConstructorExpression* expr); /// Handles a continue statement /// @param stmt the statement to emit /// @returns true if the statement was emitted successfully - bool EmitContinue(ast::ContinueStatement* stmt); + bool EmitContinue(const ast::ContinueStatement* stmt); /// Handles generate an Expression /// @param out the output of the expression stream /// @param expr the expression /// @returns true if the expression was emitted - bool EmitExpression(std::ostream& out, ast::Expression* expr); + bool EmitExpression(std::ostream& out, const ast::Expression* expr); /// Handles generating a function /// @param func the function to generate /// @returns true if the function was emitted - bool EmitFunction(ast::Function* func); + bool EmitFunction(const ast::Function* func); /// Handles emitting a global variable /// @param global the global variable /// @returns true on success - bool EmitGlobalVariable(ast::Variable* global); + bool EmitGlobalVariable(const ast::Variable* global); /// Handles emitting a global variable with the uniform storage class /// @param var the global variable @@ -284,47 +286,47 @@ class GeneratorImpl : public TextGenerator { /// Handles emitting the entry point function /// @param func the entry point /// @returns true if the entry point function was emitted - bool EmitEntryPointFunction(ast::Function* func); + bool EmitEntryPointFunction(const ast::Function* func); /// Handles an if statement /// @param stmt the statement to emit /// @returns true if the statement was successfully emitted - bool EmitIf(ast::IfStatement* stmt); + bool EmitIf(const ast::IfStatement* stmt); /// Handles a literal /// @param out the output stream /// @param lit the literal to emit /// @returns true if the literal was successfully emitted - bool EmitLiteral(std::ostream& out, ast::Literal* lit); + bool EmitLiteral(std::ostream& out, const ast::Literal* lit); /// Handles a loop statement /// @param stmt the statement to emit /// @returns true if the statement was emitted - bool EmitLoop(ast::LoopStatement* stmt); + bool EmitLoop(const ast::LoopStatement* stmt); /// Handles a for loop statement /// @param stmt the statement to emit /// @returns true if the statement was emitted - bool EmitForLoop(ast::ForLoopStatement* stmt); + bool EmitForLoop(const ast::ForLoopStatement* stmt); /// Handles generating an identifier expression /// @param out the output of the expression stream /// @param expr the identifier expression /// @returns true if the identifeir was emitted - bool EmitIdentifier(std::ostream& out, ast::IdentifierExpression* expr); + bool EmitIdentifier(std::ostream& out, const ast::IdentifierExpression* expr); /// Handles a member accessor expression /// @param out the output of the expression stream /// @param expr the member accessor expression /// @returns true if the member accessor was emitted bool EmitMemberAccessor(std::ostream& out, - ast::MemberAccessorExpression* expr); + const ast::MemberAccessorExpression* expr); /// Handles return statements /// @param stmt the statement to emit /// @returns true if the statement was successfully emitted - bool EmitReturn(ast::ReturnStatement* stmt); + bool EmitReturn(const ast::ReturnStatement* stmt); /// Handles statement /// @param stmt the statement to emit /// @returns true if the statement was emitted - bool EmitStatement(ast::Statement* stmt); + bool EmitStatement(const ast::Statement* stmt); /// Handles generating a switch statement /// @param stmt the statement to emit /// @returns true if the statement was emitted - bool EmitSwitch(ast::SwitchStatement* stmt); + bool EmitSwitch(const ast::SwitchStatement* stmt); /// Handles generating type /// @param out the output stream /// @param type the type to generate @@ -361,7 +363,7 @@ class GeneratorImpl : public TextGenerator { /// @param out the output of the expression stream /// @param expr the expression to emit /// @returns true if the expression was emitted - bool EmitUnaryOp(std::ostream& out, ast::UnaryOpExpression* expr); + bool EmitUnaryOp(std::ostream& out, const ast::UnaryOpExpression* expr); /// Emits the zero value for the given type /// @param out the output stream /// @param type the type to emit the value for @@ -370,7 +372,7 @@ class GeneratorImpl : public TextGenerator { /// Handles generating a variable /// @param var the variable to generate /// @returns true if the variable was emitted - bool EmitVariable(ast::Variable* var); + bool EmitVariable(const ast::Variable* var); /// Handles generating a program scope constant variable /// @param var the variable to emit /// @returns true if the variable was emitted @@ -441,7 +443,7 @@ class GeneratorImpl : public TextGenerator { /// @returns true if the call expression is emitted template bool CallIntrinsicHelper(std::ostream& out, - ast::CallExpression* call, + const ast::CallExpression* call, const sem::Intrinsic* intrinsic, F&& build); diff --git a/src/writer/hlsl/generator_impl_intrinsic_test.cc b/src/writer/hlsl/generator_impl_intrinsic_test.cc index 1603561f4d..2771a92d76 100644 --- a/src/writer/hlsl/generator_impl_intrinsic_test.cc +++ b/src/writer/hlsl/generator_impl_intrinsic_test.cc @@ -57,9 +57,9 @@ inline std::ostream& operator<<(std::ostream& out, IntrinsicData data) { return out; } -ast::CallExpression* GenerateCall(IntrinsicType intrinsic, - ParamType type, - ProgramBuilder* builder) { +const ast::CallExpression* GenerateCall(IntrinsicType intrinsic, + ParamType type, + ProgramBuilder* builder) { std::string name; std::ostringstream str(name); str << intrinsic; diff --git a/src/writer/hlsl/generator_impl_member_accessor_test.cc b/src/writer/hlsl/generator_impl_member_accessor_test.cc index 40b73d4259..ccefe547e2 100644 --- a/src/writer/hlsl/generator_impl_member_accessor_test.cc +++ b/src/writer/hlsl/generator_impl_member_accessor_test.cc @@ -25,63 +25,63 @@ namespace { using ::testing::HasSubstr; using create_type_func_ptr = - ast::Type* (*)(const ProgramBuilder::TypesBuilder& ty); + const ast::Type* (*)(const ProgramBuilder::TypesBuilder& ty); -inline ast::Type* ty_i32(const ProgramBuilder::TypesBuilder& ty) { +inline const ast::Type* ty_i32(const ProgramBuilder::TypesBuilder& ty) { return ty.i32(); } -inline ast::Type* ty_u32(const ProgramBuilder::TypesBuilder& ty) { +inline const ast::Type* ty_u32(const ProgramBuilder::TypesBuilder& ty) { return ty.u32(); } -inline ast::Type* ty_f32(const ProgramBuilder::TypesBuilder& ty) { +inline const ast::Type* ty_f32(const ProgramBuilder::TypesBuilder& ty) { return ty.f32(); } template -inline ast::Type* ty_vec2(const ProgramBuilder::TypesBuilder& ty) { +inline const ast::Type* ty_vec2(const ProgramBuilder::TypesBuilder& ty) { return ty.vec2(); } template -inline ast::Type* ty_vec3(const ProgramBuilder::TypesBuilder& ty) { +inline const ast::Type* ty_vec3(const ProgramBuilder::TypesBuilder& ty) { return ty.vec3(); } template -inline ast::Type* ty_vec4(const ProgramBuilder::TypesBuilder& ty) { +inline const ast::Type* ty_vec4(const ProgramBuilder::TypesBuilder& ty) { return ty.vec4(); } template -inline ast::Type* ty_mat2x2(const ProgramBuilder::TypesBuilder& ty) { +inline const ast::Type* ty_mat2x2(const ProgramBuilder::TypesBuilder& ty) { return ty.mat2x2(); } template -inline ast::Type* ty_mat2x3(const ProgramBuilder::TypesBuilder& ty) { +inline const ast::Type* ty_mat2x3(const ProgramBuilder::TypesBuilder& ty) { return ty.mat2x3(); } template -inline ast::Type* ty_mat2x4(const ProgramBuilder::TypesBuilder& ty) { +inline const ast::Type* ty_mat2x4(const ProgramBuilder::TypesBuilder& ty) { return ty.mat2x4(); } template -inline ast::Type* ty_mat3x2(const ProgramBuilder::TypesBuilder& ty) { +inline const ast::Type* ty_mat3x2(const ProgramBuilder::TypesBuilder& ty) { return ty.mat3x2(); } template -inline ast::Type* ty_mat3x3(const ProgramBuilder::TypesBuilder& ty) { +inline const ast::Type* ty_mat3x3(const ProgramBuilder::TypesBuilder& ty) { return ty.mat3x3(); } template -inline ast::Type* ty_mat3x4(const ProgramBuilder::TypesBuilder& ty) { +inline const ast::Type* ty_mat3x4(const ProgramBuilder::TypesBuilder& ty) { return ty.mat3x4(); } template -inline ast::Type* ty_mat4x2(const ProgramBuilder::TypesBuilder& ty) { +inline const ast::Type* ty_mat4x2(const ProgramBuilder::TypesBuilder& ty) { return ty.mat4x2(); } template -inline ast::Type* ty_mat4x3(const ProgramBuilder::TypesBuilder& ty) { +inline const ast::Type* ty_mat4x3(const ProgramBuilder::TypesBuilder& ty) { return ty.mat4x3(); } template -inline ast::Type* ty_mat4x4(const ProgramBuilder::TypesBuilder& ty) { +inline const ast::Type* ty_mat4x4(const ProgramBuilder::TypesBuilder& ty) { return ty.mat4x4(); } diff --git a/src/writer/hlsl/generator_impl_type_test.cc b/src/writer/hlsl/generator_impl_type_test.cc index fe4965ba3a..6eb6a1956c 100644 --- a/src/writer/hlsl/generator_impl_type_test.cc +++ b/src/writer/hlsl/generator_impl_type_test.cc @@ -394,7 +394,7 @@ using HlslSampledTexturesTest = TestParamHelper; TEST_P(HlslSampledTexturesTest, Emit) { auto params = GetParam(); - ast::Type* datatype = nullptr; + const ast::Type* datatype = nullptr; switch (params.datatype) { case TextureDataType::F32: datatype = ty.f32(); diff --git a/src/writer/msl/generator_impl.cc b/src/writer/msl/generator_impl.cc index 510b649229..2c90761e4b 100644 --- a/src/writer/msl/generator_impl.cc +++ b/src/writer/msl/generator_impl.cc @@ -242,8 +242,9 @@ bool GeneratorImpl::EmitTypeDecl(const sem::Type* ty) { return true; } -bool GeneratorImpl::EmitArrayAccessor(std::ostream& out, - ast::ArrayAccessorExpression* expr) { +bool GeneratorImpl::EmitArrayAccessor( + std::ostream& out, + const ast::ArrayAccessorExpression* expr) { bool paren_lhs = !expr->array ->IsAnyOfUnwrapRef(), "")) { return false; @@ -286,7 +287,7 @@ bool GeneratorImpl::EmitBitcast(std::ostream& out, return true; } -bool GeneratorImpl::EmitAssign(ast::AssignmentStatement* stmt) { +bool GeneratorImpl::EmitAssign(const ast::AssignmentStatement* stmt) { auto out = line(); if (!EmitExpression(out, stmt->lhs)) { @@ -304,7 +305,8 @@ bool GeneratorImpl::EmitAssign(ast::AssignmentStatement* stmt) { return true; } -bool GeneratorImpl::EmitBinary(std::ostream& out, ast::BinaryExpression* expr) { +bool GeneratorImpl::EmitBinary(std::ostream& out, + const ast::BinaryExpression* expr) { auto emit_op = [&] { out << " "; @@ -491,12 +493,13 @@ bool GeneratorImpl::EmitBinary(std::ostream& out, ast::BinaryExpression* expr) { return true; } -bool GeneratorImpl::EmitBreak(ast::BreakStatement*) { +bool GeneratorImpl::EmitBreak(const ast::BreakStatement*) { line() << "break;"; return true; } -bool GeneratorImpl::EmitCall(std::ostream& out, ast::CallExpression* expr) { +bool GeneratorImpl::EmitCall(std::ostream& out, + const ast::CallExpression* expr) { auto* ident = expr->func; auto* call = program_->Sem().Get(expr); if (auto* intrinsic = call->Target()->As()) { @@ -551,7 +554,7 @@ bool GeneratorImpl::EmitCall(std::ostream& out, ast::CallExpression* expr) { } bool GeneratorImpl::EmitIntrinsicCall(std::ostream& out, - ast::CallExpression* expr, + const ast::CallExpression* expr, const sem::Intrinsic* intrinsic) { if (intrinsic->IsAtomic()) { return EmitAtomicCall(out, expr, intrinsic); @@ -653,7 +656,7 @@ bool GeneratorImpl::EmitIntrinsicCall(std::ostream& out, } bool GeneratorImpl::EmitAtomicCall(std::ostream& out, - ast::CallExpression* expr, + const ast::CallExpression* expr, const sem::Intrinsic* intrinsic) { auto call = [&](const std::string& name, bool append_memory_order_relaxed) { out << name; @@ -754,7 +757,7 @@ bool GeneratorImpl::EmitAtomicCall(std::ostream& out, } bool GeneratorImpl::EmitTextureCall(std::ostream& out, - ast::CallExpression* expr, + const ast::CallExpression* expr, const sem::Intrinsic* intrinsic) { using Usage = sem::ParameterUsage; @@ -1026,7 +1029,7 @@ bool GeneratorImpl::EmitTextureCall(std::ostream& out, } bool GeneratorImpl::EmitModfCall(std::ostream& out, - ast::CallExpression* expr, + const ast::CallExpression* expr, const sem::Intrinsic* intrinsic) { return CallIntrinsicHelper( out, expr, intrinsic, @@ -1054,7 +1057,7 @@ bool GeneratorImpl::EmitModfCall(std::ostream& out, } bool GeneratorImpl::EmitFrexpCall(std::ostream& out, - ast::CallExpression* expr, + const ast::CallExpression* expr, const sem::Intrinsic* intrinsic) { return CallIntrinsicHelper( out, expr, intrinsic, @@ -1226,7 +1229,7 @@ std::string GeneratorImpl::generate_builtin_name( return out; } -bool GeneratorImpl::EmitCase(ast::CaseStatement* stmt) { +bool GeneratorImpl::EmitCase(const ast::CaseStatement* stmt) { if (stmt->IsDefault()) { line() << "default: {"; } else { @@ -1263,14 +1266,14 @@ bool GeneratorImpl::EmitCase(ast::CaseStatement* stmt) { } bool GeneratorImpl::EmitConstructor(std::ostream& out, - ast::ConstructorExpression* expr) { + const ast::ConstructorExpression* expr) { if (auto* scalar = expr->As()) { return EmitScalarConstructor(out, scalar); } return EmitTypeConstructor(out, expr->As()); } -bool GeneratorImpl::EmitContinue(ast::ContinueStatement*) { +bool GeneratorImpl::EmitContinue(const ast::ContinueStatement*) { if (!emit_continuing_()) { return false; } @@ -1279,8 +1282,9 @@ bool GeneratorImpl::EmitContinue(ast::ContinueStatement*) { return true; } -bool GeneratorImpl::EmitTypeConstructor(std::ostream& out, - ast::TypeConstructorExpression* expr) { +bool GeneratorImpl::EmitTypeConstructor( + std::ostream& out, + const ast::TypeConstructorExpression* expr) { auto* type = TypeOf(expr)->UnwrapRef(); if (type->IsAnyOf()) { @@ -1359,11 +1363,11 @@ bool GeneratorImpl::EmitZeroValue(std::ostream& out, const sem::Type* type) { bool GeneratorImpl::EmitScalarConstructor( std::ostream& out, - ast::ScalarConstructorExpression* expr) { + const ast::ScalarConstructorExpression* expr) { return EmitLiteral(out, expr->literal); } -bool GeneratorImpl::EmitLiteral(std::ostream& out, ast::Literal* lit) { +bool GeneratorImpl::EmitLiteral(std::ostream& out, const ast::Literal* lit) { if (auto* l = lit->As()) { out << (l->value ? "true" : "false"); } else if (auto* fl = lit->As()) { @@ -1395,7 +1399,8 @@ bool GeneratorImpl::EmitLiteral(std::ostream& out, ast::Literal* lit) { return true; } -bool GeneratorImpl::EmitExpression(std::ostream& out, ast::Expression* expr) { +bool GeneratorImpl::EmitExpression(std::ostream& out, + const ast::Expression* expr) { if (auto* a = expr->As()) { return EmitArrayAccessor(out, a); } @@ -1444,7 +1449,7 @@ void GeneratorImpl::EmitStage(std::ostream& out, ast::PipelineStage stage) { return; } -bool GeneratorImpl::EmitFunction(ast::Function* func) { +bool GeneratorImpl::EmitFunction(const ast::Function* func) { auto* func_sem = program_->Sem().Get(func); { @@ -1584,7 +1589,7 @@ std::string GeneratorImpl::interpolation_to_attribute( return attr; } -bool GeneratorImpl::EmitEntryPointFunction(ast::Function* func) { +bool GeneratorImpl::EmitEntryPointFunction(const ast::Function* func) { auto* func_sem = program_->Sem().Get(func); auto func_name = program_->Symbols().NameFor(func->symbol); @@ -1750,12 +1755,12 @@ bool GeneratorImpl::EmitEntryPointFunction(ast::Function* func) { } bool GeneratorImpl::EmitIdentifier(std::ostream& out, - ast::IdentifierExpression* expr) { + const ast::IdentifierExpression* expr) { out << program_->Symbols().NameFor(expr->symbol); return true; } -bool GeneratorImpl::EmitLoop(ast::LoopStatement* stmt) { +bool GeneratorImpl::EmitLoop(const ast::LoopStatement* stmt) { auto emit_continuing = [this, stmt]() { if (stmt->continuing && !stmt->continuing->Empty()) { if (!EmitBlock(stmt->continuing)) { @@ -1781,7 +1786,7 @@ bool GeneratorImpl::EmitLoop(ast::LoopStatement* stmt) { return true; } -bool GeneratorImpl::EmitForLoop(ast::ForLoopStatement* stmt) { +bool GeneratorImpl::EmitForLoop(const ast::ForLoopStatement* stmt) { TextBuffer init_buf; if (auto* init = stmt->initializer) { TINT_SCOPED_ASSIGNMENT(current_buffer_, &init_buf); @@ -1891,14 +1896,14 @@ bool GeneratorImpl::EmitForLoop(ast::ForLoopStatement* stmt) { return true; } -bool GeneratorImpl::EmitDiscard(ast::DiscardStatement*) { +bool GeneratorImpl::EmitDiscard(const ast::DiscardStatement*) { // TODO(dsinclair): Verify this is correct when the discard semantics are // defined for WGSL (https://github.com/gpuweb/gpuweb/issues/361) line() << "discard_fragment();"; return true; } -bool GeneratorImpl::EmitIf(ast::IfStatement* stmt) { +bool GeneratorImpl::EmitIf(const ast::IfStatement* stmt) { { auto out = line(); out << "if ("; @@ -1945,8 +1950,9 @@ bool GeneratorImpl::EmitIf(ast::IfStatement* stmt) { return true; } -bool GeneratorImpl::EmitMemberAccessor(std::ostream& out, - ast::MemberAccessorExpression* expr) { +bool GeneratorImpl::EmitMemberAccessor( + std::ostream& out, + const ast::MemberAccessorExpression* expr) { bool paren_lhs = !expr->structure ->IsAnyOfvalue) { @@ -1999,7 +2005,7 @@ bool GeneratorImpl::EmitBlock(const ast::BlockStatement* stmt) { return true; } -bool GeneratorImpl::EmitStatement(ast::Statement* stmt) { +bool GeneratorImpl::EmitStatement(const ast::Statement* stmt) { if (auto* a = stmt->As()) { return EmitAssign(a); } @@ -2067,7 +2073,7 @@ bool GeneratorImpl::EmitStatementsWithIndent(const ast::StatementList& stmts) { return EmitStatements(stmts); } -bool GeneratorImpl::EmitSwitch(ast::SwitchStatement* stmt) { +bool GeneratorImpl::EmitSwitch(const ast::SwitchStatement* stmt) { { auto out = line(); out << "switch("; @@ -2510,7 +2516,7 @@ bool GeneratorImpl::EmitStructType(TextBuffer* b, const sem::Struct* str) { } bool GeneratorImpl::EmitUnaryOp(std::ostream& out, - ast::UnaryOpExpression* expr) { + const ast::UnaryOpExpression* expr) { // Handle `-e` when `e` is signed, so that we ensure that if `e` is the // largest negative value, it returns `e`. auto* expr_type = TypeOf(expr->expr)->UnwrapRef(); @@ -2760,7 +2766,7 @@ GeneratorImpl::SizeAndAlign GeneratorImpl::MslPackedTypeSizeAndAlign( template bool GeneratorImpl::CallIntrinsicHelper(std::ostream& out, - ast::CallExpression* call, + const ast::CallExpression* call, const sem::Intrinsic* intrinsic, F&& build) { // Generate the helper function if it hasn't been created already diff --git a/src/writer/msl/generator_impl.h b/src/writer/msl/generator_impl.h index ee5b2bf843..6009c922b8 100644 --- a/src/writer/msl/generator_impl.h +++ b/src/writer/msl/generator_impl.h @@ -99,21 +99,22 @@ class GeneratorImpl : public TextGenerator { /// @param out the output of the expression stream /// @param expr the expression to emit /// @returns true if the array accessor was emitted - bool EmitArrayAccessor(std::ostream& out, ast::ArrayAccessorExpression* expr); + bool EmitArrayAccessor(std::ostream& out, + const ast::ArrayAccessorExpression* expr); /// Handles an assignment statement /// @param stmt the statement to emit /// @returns true if the statement was emitted successfully - bool EmitAssign(ast::AssignmentStatement* stmt); + bool EmitAssign(const ast::AssignmentStatement* stmt); /// Handles generating a binary expression /// @param out the output of the expression stream /// @param expr the binary expression /// @returns true if the expression was emitted, false otherwise - bool EmitBinary(std::ostream& out, ast::BinaryExpression* expr); + bool EmitBinary(std::ostream& out, const ast::BinaryExpression* expr); /// Handles generating a bitcast expression /// @param out the output of the expression stream /// @param expr the bitcast expression /// @returns true if the bitcast was emitted - bool EmitBitcast(std::ostream& out, ast::BitcastExpression* expr); + bool EmitBitcast(std::ostream& out, const ast::BitcastExpression* expr); /// Handles a block statement /// @param stmt the statement to emit /// @returns true if the statement was emitted successfully @@ -121,19 +122,19 @@ class GeneratorImpl : public TextGenerator { /// Handles a break statement /// @param stmt the statement to emit /// @returns true if the statement was emitted successfully - bool EmitBreak(ast::BreakStatement* stmt); + bool EmitBreak(const ast::BreakStatement* stmt); /// Handles generating a call expression /// @param out the output of the expression stream /// @param expr the call expression /// @returns true if the call expression is emitted - bool EmitCall(std::ostream& out, ast::CallExpression* expr); + bool EmitCall(std::ostream& out, const ast::CallExpression* expr); /// Handles generating an intrinsic call expression /// @param out the output of the expression stream /// @param expr the call expression /// @param intrinsic the intrinsic being called /// @returns true if the call expression is emitted bool EmitIntrinsicCall(std::ostream& out, - ast::CallExpression* expr, + const ast::CallExpression* expr, const sem::Intrinsic* intrinsic); /// Handles generating a call to an atomic function (`atomicAdd`, /// `atomicMax`, etc) @@ -142,7 +143,7 @@ class GeneratorImpl : public TextGenerator { /// @param intrinsic the semantic information for the atomic intrinsic /// @returns true if the call expression is emitted bool EmitAtomicCall(std::ostream& out, - ast::CallExpression* expr, + const ast::CallExpression* expr, const sem::Intrinsic* intrinsic); /// Handles generating a call to a texture function (`textureSample`, /// `textureSampleGrad`, etc) @@ -151,7 +152,7 @@ class GeneratorImpl : public TextGenerator { /// @param intrinsic the semantic information for the texture intrinsic /// @returns true if the call expression is emitted bool EmitTextureCall(std::ostream& out, - ast::CallExpression* expr, + const ast::CallExpression* expr, const sem::Intrinsic* intrinsic); /// Handles generating a call to the `modf()` intrinsic /// @param out the output of the expression stream @@ -159,7 +160,7 @@ class GeneratorImpl : public TextGenerator { /// @param intrinsic the semantic information for the intrinsic /// @returns true if the call expression is emitted bool EmitModfCall(std::ostream& out, - ast::CallExpression* expr, + const ast::CallExpression* expr, const sem::Intrinsic* intrinsic); /// Handles generating a call to the `frexp()` intrinsic /// @param out the output of the expression stream @@ -167,76 +168,77 @@ class GeneratorImpl : public TextGenerator { /// @param intrinsic the semantic information for the intrinsic /// @returns true if the call expression is emitted bool EmitFrexpCall(std::ostream& out, - ast::CallExpression* expr, + const ast::CallExpression* expr, const sem::Intrinsic* intrinsic); /// Handles a case statement /// @param stmt the statement /// @returns true if the statement was emitted successfully - bool EmitCase(ast::CaseStatement* stmt); + bool EmitCase(const ast::CaseStatement* stmt); /// Handles generating constructor expressions /// @param out the output of the expression stream /// @param expr the constructor expression /// @returns true if the expression was emitted - bool EmitConstructor(std::ostream& out, ast::ConstructorExpression* expr); + bool EmitConstructor(std::ostream& out, + const ast::ConstructorExpression* expr); /// Handles a continue statement /// @param stmt the statement to emit /// @returns true if the statement was emitted successfully - bool EmitContinue(ast::ContinueStatement* stmt); + bool EmitContinue(const ast::ContinueStatement* stmt); /// Handles generating a discard statement /// @param stmt the discard statement /// @returns true if the statement was successfully emitted - bool EmitDiscard(ast::DiscardStatement* stmt); + bool EmitDiscard(const ast::DiscardStatement* stmt); /// Handles emitting the entry point function /// @param func the entry point function /// @returns true if the entry point function was emitted - bool EmitEntryPointFunction(ast::Function* func); + bool EmitEntryPointFunction(const ast::Function* func); /// Handles generate an Expression /// @param out the output of the expression stream /// @param expr the expression /// @returns true if the expression was emitted - bool EmitExpression(std::ostream& out, ast::Expression* expr); + bool EmitExpression(std::ostream& out, const ast::Expression* expr); /// Handles generating a function /// @param func the function to generate /// @returns true if the function was emitted - bool EmitFunction(ast::Function* func); + bool EmitFunction(const ast::Function* func); /// Handles generating an identifier expression /// @param out the output of the expression stream /// @param expr the identifier expression /// @returns true if the identifier was emitted - bool EmitIdentifier(std::ostream& out, ast::IdentifierExpression* expr); + bool EmitIdentifier(std::ostream& out, const ast::IdentifierExpression* expr); /// Handles an if statement /// @param stmt the statement to emit /// @returns true if the statement was successfully emitted - bool EmitIf(ast::IfStatement* stmt); + bool EmitIf(const ast::IfStatement* stmt); /// Handles a literal /// @param out the output of the expression stream /// @param lit the literal to emit /// @returns true if the literal was successfully emitted - bool EmitLiteral(std::ostream& out, ast::Literal* lit); + bool EmitLiteral(std::ostream& out, const ast::Literal* lit); /// Handles a loop statement /// @param stmt the statement to emit /// @returns true if the statement was emitted - bool EmitLoop(ast::LoopStatement* stmt); + bool EmitLoop(const ast::LoopStatement* stmt); /// Handles a for loop statement /// @param stmt the statement to emit /// @returns true if the statement was emitted - bool EmitForLoop(ast::ForLoopStatement* stmt); + bool EmitForLoop(const ast::ForLoopStatement* stmt); /// Handles a member accessor expression /// @param out the output of the expression stream /// @param expr the member accessor expression /// @returns true if the member accessor was emitted bool EmitMemberAccessor(std::ostream& out, - ast::MemberAccessorExpression* expr); + const ast::MemberAccessorExpression* expr); /// Handles return statements /// @param stmt the statement to emit /// @returns true if the statement was successfully emitted - bool EmitReturn(ast::ReturnStatement* stmt); + bool EmitReturn(const ast::ReturnStatement* stmt); /// Handles generating a scalar constructor /// @param out the output of the expression stream /// @param expr the scalar constructor expression /// @returns true if the scalar constructor is emitted bool EmitScalarConstructor(std::ostream& out, - ast::ScalarConstructorExpression* expr); + const ast::ScalarConstructorExpression* expr); /// Handles emitting a pipeline stage name /// @param out the output of the expression stream /// @param stage the stage to emit @@ -244,7 +246,7 @@ class GeneratorImpl : public TextGenerator { /// Handles statement /// @param stmt the statement to emit /// @returns true if the statement was emitted - bool EmitStatement(ast::Statement* stmt); + bool EmitStatement(const ast::Statement* stmt); /// Emits a list of statements /// @param stmts the statement list /// @returns true if the statements were emitted successfully @@ -256,7 +258,7 @@ class GeneratorImpl : public TextGenerator { /// Handles generating a switch statement /// @param stmt the statement to emit /// @returns true if the statement was emitted - bool EmitSwitch(ast::SwitchStatement* stmt); + bool EmitSwitch(const ast::SwitchStatement* stmt); /// Handles generating a type /// @param out the output of the type stream /// @param type the type to generate @@ -300,12 +302,12 @@ class GeneratorImpl : public TextGenerator { /// @param expr the type constructor expression /// @returns true if the constructor is emitted bool EmitTypeConstructor(std::ostream& out, - ast::TypeConstructorExpression* expr); + const ast::TypeConstructorExpression* expr); /// Handles a unary op expression /// @param out the output of the expression stream /// @param expr the expression to emit /// @returns true if the expression was emitted - bool EmitUnaryOp(std::ostream& out, ast::UnaryOpExpression* expr); + bool EmitUnaryOp(std::ostream& out, const ast::UnaryOpExpression* expr); /// Handles generating a variable /// @param var the variable to generate /// @returns true if the variable was emitted @@ -360,7 +362,7 @@ class GeneratorImpl : public TextGenerator { /// @returns true if the call expression is emitted template bool CallIntrinsicHelper(std::ostream& out, - ast::CallExpression* call, + const ast::CallExpression* call, const sem::Intrinsic* intrinsic, F&& build); diff --git a/src/writer/msl/generator_impl_binary_test.cc b/src/writer/msl/generator_impl_binary_test.cc index 4b460dccbe..330d641854 100644 --- a/src/writer/msl/generator_impl_binary_test.cc +++ b/src/writer/msl/generator_impl_binary_test.cc @@ -34,8 +34,8 @@ TEST_P(MslBinaryTest, Emit) { auto type = [&] { return ((params.op == ast::BinaryOp::kLogicalAnd) || (params.op == ast::BinaryOp::kLogicalOr)) - ? static_cast(ty.bool_()) - : static_cast(ty.u32()); + ? static_cast(ty.bool_()) + : static_cast(ty.u32()); }; auto* left = Var("left", type()); @@ -82,7 +82,7 @@ TEST_P(MslBinaryTest_SignedOverflowDefinedBehaviour, Emit) { auto* a_type = ty.i32(); auto* b_type = (params.op == ast::BinaryOp::kShiftLeft || params.op == ast::BinaryOp::kShiftRight) - ? static_cast(ty.u32()) + ? static_cast(ty.u32()) : ty.i32(); auto* a = Var("a", a_type); @@ -117,7 +117,7 @@ TEST_P(MslBinaryTest_SignedOverflowDefinedBehaviour_Chained, Emit) { auto* a_type = ty.i32(); auto* b_type = (params.op == ast::BinaryOp::kShiftLeft || params.op == ast::BinaryOp::kShiftRight) - ? static_cast(ty.u32()) + ? static_cast(ty.u32()) : ty.i32(); auto* a = Var("a", a_type); diff --git a/src/writer/msl/generator_impl_intrinsic_test.cc b/src/writer/msl/generator_impl_intrinsic_test.cc index eba80cc255..5b681b3989 100644 --- a/src/writer/msl/generator_impl_intrinsic_test.cc +++ b/src/writer/msl/generator_impl_intrinsic_test.cc @@ -53,9 +53,9 @@ inline std::ostream& operator<<(std::ostream& out, IntrinsicData data) { return out; } -ast::CallExpression* GenerateCall(IntrinsicType intrinsic, - ParamType type, - ProgramBuilder* builder) { +const ast::CallExpression* GenerateCall(IntrinsicType intrinsic, + ParamType type, + ProgramBuilder* builder) { std::string name; std::ostringstream str(name); str << intrinsic; diff --git a/src/writer/spirv/builder.cc b/src/writer/spirv/builder.cc index f37ffd10dd..da3f887182 100644 --- a/src/writer/spirv/builder.cc +++ b/src/writer/spirv/builder.cc @@ -387,7 +387,7 @@ bool Builder::GenerateLabel(uint32_t id) { return true; } -bool Builder::GenerateAssignStatement(ast::AssignmentStatement* assign) { +bool Builder::GenerateAssignStatement(const ast::AssignmentStatement* assign) { auto lhs_id = GenerateExpression(assign->lhs); if (lhs_id == 0) { return false; @@ -404,7 +404,7 @@ bool Builder::GenerateAssignStatement(ast::AssignmentStatement* assign) { return GenerateStore(lhs_id, rhs_id); } -bool Builder::GenerateBreakStatement(ast::BreakStatement*) { +bool Builder::GenerateBreakStatement(const ast::BreakStatement*) { if (merge_stack_.empty()) { error_ = "Attempted to break without a merge block"; return false; @@ -416,7 +416,7 @@ bool Builder::GenerateBreakStatement(ast::BreakStatement*) { return true; } -bool Builder::GenerateContinueStatement(ast::ContinueStatement*) { +bool Builder::GenerateContinueStatement(const ast::ContinueStatement*) { if (continue_stack_.empty()) { error_ = "Attempted to continue without a continue block"; return false; @@ -431,14 +431,14 @@ bool Builder::GenerateContinueStatement(ast::ContinueStatement*) { // TODO(dsinclair): This is generating an OpKill but the semantics of kill // haven't been defined for WGSL yet. So, this may need to change. // https://github.com/gpuweb/gpuweb/issues/676 -bool Builder::GenerateDiscardStatement(ast::DiscardStatement*) { +bool Builder::GenerateDiscardStatement(const ast::DiscardStatement*) { if (!push_function_inst(spv::Op::OpKill, {})) { return false; } return true; } -bool Builder::GenerateEntryPoint(ast::Function* func, uint32_t id) { +bool Builder::GenerateEntryPoint(const ast::Function* func, uint32_t id) { auto stage = pipeline_stage_to_execution_model(func->PipelineStage()); if (stage == SpvExecutionModelMax) { error_ = "Unknown pipeline stage provided"; @@ -472,7 +472,7 @@ bool Builder::GenerateEntryPoint(ast::Function* func, uint32_t id) { return true; } -bool Builder::GenerateExecutionModes(ast::Function* func, uint32_t id) { +bool Builder::GenerateExecutionModes(const ast::Function* func, uint32_t id) { auto* func_sem = builder_.Sem().Get(func); // WGSL fragment shader origin is upper left @@ -553,7 +553,7 @@ bool Builder::GenerateExecutionModes(ast::Function* func, uint32_t id) { return true; } -uint32_t Builder::GenerateExpression(ast::Expression* expr) { +uint32_t Builder::GenerateExpression(const ast::Expression* expr) { if (auto* a = expr->As()) { return GenerateAccessorExpression(a); } @@ -583,7 +583,7 @@ uint32_t Builder::GenerateExpression(ast::Expression* expr) { return 0; } -bool Builder::GenerateFunction(ast::Function* func_ast) { +bool Builder::GenerateFunction(const ast::Function* func_ast) { auto* func = builder_.Sem().Get(func_ast); uint32_t func_type_id = GenerateFunctionTypeIfNeeded(func); @@ -678,7 +678,7 @@ uint32_t Builder::GenerateFunctionTypeIfNeeded(const sem::Function* func) { }); } -bool Builder::GenerateFunctionVariable(ast::Variable* var) { +bool Builder::GenerateFunctionVariable(const ast::Variable* var) { uint32_t init_id = 0; if (var->constructor) { init_id = GenerateExpression(var->constructor); @@ -741,7 +741,7 @@ bool Builder::GenerateStore(uint32_t to, uint32_t from) { {Operand::Int(to), Operand::Int(from)}); } -bool Builder::GenerateGlobalVariable(ast::Variable* var) { +bool Builder::GenerateGlobalVariable(const ast::Variable* var) { auto* sem = builder_.Sem().Get(var); auto* type = sem->Type()->UnwrapRef(); @@ -893,7 +893,7 @@ bool Builder::GenerateGlobalVariable(ast::Variable* var) { return true; } -bool Builder::GenerateArrayAccessor(ast::ArrayAccessorExpression* expr, +bool Builder::GenerateArrayAccessor(const ast::ArrayAccessorExpression* expr, AccessorInfo* info) { auto idx_id = GenerateExpression(expr->index); if (idx_id == 0) { @@ -962,7 +962,7 @@ bool Builder::GenerateArrayAccessor(ast::ArrayAccessorExpression* expr, return false; } -bool Builder::GenerateMemberAccessor(ast::MemberAccessorExpression* expr, +bool Builder::GenerateMemberAccessor(const ast::MemberAccessorExpression* expr, AccessorInfo* info) { auto* expr_sem = builder_.Sem().Get(expr); auto* expr_type = expr_sem->Type(); @@ -1088,7 +1088,7 @@ bool Builder::GenerateMemberAccessor(ast::MemberAccessorExpression* expr, return false; } -uint32_t Builder::GenerateAccessorExpression(ast::Expression* expr) { +uint32_t Builder::GenerateAccessorExpression(const ast::Expression* expr) { if (!expr->IsAnyOf()) { TINT_ICE(Writer, builder_.Diagnostics()) << "expression is not an accessor"; @@ -1098,8 +1098,8 @@ uint32_t Builder::GenerateAccessorExpression(ast::Expression* expr) { // Gather a list of all the member and array accessors that are in this chain. // The list is built in reverse order as that's the order we need to access // the chain. - std::vector accessors; - ast::Expression* source = expr; + std::vector accessors; + const ast::Expression* source = expr; while (true) { if (auto* array = source->As()) { accessors.insert(accessors.begin(), source); @@ -1162,7 +1162,7 @@ uint32_t Builder::GenerateAccessorExpression(ast::Expression* expr) { } uint32_t Builder::GenerateIdentifierExpression( - ast::IdentifierExpression* expr) { + const ast::IdentifierExpression* expr) { uint32_t val = 0; if (scope_stack_.get(expr->symbol, &val)) { return val; @@ -1190,7 +1190,8 @@ uint32_t Builder::GenerateLoadIfNeeded(const sem::Type* type, uint32_t id) { return result_id; } -uint32_t Builder::GenerateUnaryOpExpression(ast::UnaryOpExpression* expr) { +uint32_t Builder::GenerateUnaryOpExpression( + const ast::UnaryOpExpression* expr) { auto result = result_op(); auto result_id = result.to_i(); @@ -1256,8 +1257,8 @@ uint32_t Builder::GetGLSLstd450Import() { } uint32_t Builder::GenerateConstructorExpression( - ast::Variable* var, - ast::ConstructorExpression* expr, + const ast::Variable* var, + const ast::ConstructorExpression* expr, bool is_global_init) { if (auto* scalar = expr->As()) { return GenerateLiteralIfNeeded(var, scalar->literal); @@ -1270,7 +1271,8 @@ uint32_t Builder::GenerateConstructorExpression( return 0; } -bool Builder::is_constructor_const(ast::Expression* expr, bool is_global_init) { +bool Builder::is_constructor_const(const ast::Expression* expr, + bool is_global_init) { auto* constructor = expr->As(); if (constructor == nullptr) { return false; @@ -1326,7 +1328,7 @@ bool Builder::is_constructor_const(ast::Expression* expr, bool is_global_init) { } uint32_t Builder::GenerateTypeConstructorExpression( - ast::TypeConstructorExpression* init, + const ast::TypeConstructorExpression* init, bool is_global_init) { auto& values = init->values; @@ -1502,9 +1504,10 @@ uint32_t Builder::GenerateTypeConstructorExpression( return result.to_i(); } -uint32_t Builder::GenerateCastOrCopyOrPassthrough(const sem::Type* to_type, - ast::Expression* from_expr, - bool is_global_init) { +uint32_t Builder::GenerateCastOrCopyOrPassthrough( + const sem::Type* to_type, + const ast::Expression* from_expr, + bool is_global_init) { // This should not happen as we rely on constant folding to obviate // casts/conversions for module-scope variables if (is_global_init) { @@ -1648,8 +1651,8 @@ uint32_t Builder::GenerateCastOrCopyOrPassthrough(const sem::Type* to_type, return result_id; } -uint32_t Builder::GenerateLiteralIfNeeded(ast::Variable* var, - ast::Literal* lit) { +uint32_t Builder::GenerateLiteralIfNeeded(const ast::Variable* var, + const ast::Literal* lit) { ScalarConstant constant; auto* global = builder_.Sem().Get(var); @@ -1803,7 +1806,7 @@ uint32_t Builder::GenerateConstantVectorSplatIfNeeded(const sem::Vector* type, } uint32_t Builder::GenerateShortCircuitBinaryExpression( - ast::BinaryExpression* expr) { + const ast::BinaryExpression* expr) { auto lhs_id = GenerateExpression(expr->lhs); if (lhs_id == 0) { return false; @@ -1965,7 +1968,7 @@ uint32_t Builder::GenerateMatrixAddOrSub(uint32_t lhs_id, return result_mat_id.to_i(); } -uint32_t Builder::GenerateBinaryExpression(ast::BinaryExpression* expr) { +uint32_t Builder::GenerateBinaryExpression(const ast::BinaryExpression* expr) { // There is special logic for short circuiting operators. if (expr->IsLogicalAnd() || expr->IsLogicalOr()) { return GenerateShortCircuitBinaryExpression(expr); @@ -2216,7 +2219,7 @@ bool Builder::GenerateBlockStatementWithoutScoping( return true; } -uint32_t Builder::GenerateCallExpression(ast::CallExpression* expr) { +uint32_t Builder::GenerateCallExpression(const ast::CallExpression* expr) { auto* ident = expr->func; auto* call = builder_.Sem().Get(expr); auto* target = call->Target(); @@ -2263,7 +2266,7 @@ uint32_t Builder::GenerateCallExpression(ast::CallExpression* expr) { return result_id; } -uint32_t Builder::GenerateIntrinsic(ast::CallExpression* call, +uint32_t Builder::GenerateIntrinsic(const ast::CallExpression* call, const sem::Intrinsic* intrinsic) { auto result = result_op(); auto result_id = result.to_i(); @@ -2648,7 +2651,7 @@ uint32_t Builder::GenerateIntrinsic(ast::CallExpression* call, return result_id; } -bool Builder::GenerateTextureIntrinsic(ast::CallExpression* call, +bool Builder::GenerateTextureIntrinsic(const ast::CallExpression* call, const sem::Intrinsic* intrinsic, Operand result_type, Operand result_id) { @@ -2658,7 +2661,7 @@ bool Builder::GenerateTextureIntrinsic(ast::CallExpression* call, auto arguments = call->args; // Generates the given expression, returning the operand ID - auto gen = [&](ast::Expression* expr) { + auto gen = [&](const ast::Expression* expr) { auto val_id = GenerateExpression(expr); if (val_id == 0) { return Operand::Int(0); @@ -3094,7 +3097,7 @@ bool Builder::GenerateControlBarrierIntrinsic(const sem::Intrinsic* intrinsic) { }); } -bool Builder::GenerateAtomicIntrinsic(ast::CallExpression* call, +bool Builder::GenerateAtomicIntrinsic(const ast::CallExpression* call, const sem::Intrinsic* intrinsic, Operand result_type, Operand result_id) { @@ -3368,7 +3371,8 @@ uint32_t Builder::GenerateSampledImage(const sem::Type* texture_type, return sampled_image.to_i(); } -uint32_t Builder::GenerateBitcastExpression(ast::BitcastExpression* expr) { +uint32_t Builder::GenerateBitcastExpression( + const ast::BitcastExpression* expr) { auto result = result_op(); auto result_id = result.to_i(); @@ -3404,7 +3408,7 @@ uint32_t Builder::GenerateBitcastExpression(ast::BitcastExpression* expr) { } bool Builder::GenerateConditionalBlock( - ast::Expression* cond, + const ast::Expression* cond, const ast::BlockStatement* true_body, size_t cur_else_idx, const ast::ElseStatementList& else_stmts) { @@ -3482,7 +3486,7 @@ bool Builder::GenerateConditionalBlock( return GenerateLabel(merge_block_id); } -bool Builder::GenerateIfStatement(ast::IfStatement* stmt) { +bool Builder::GenerateIfStatement(const ast::IfStatement* stmt) { if (!continuing_stack_.empty() && stmt == continuing_stack_.back().last_statement->As()) { const ContinuingInfo& ci = continuing_stack_.back(); @@ -3497,7 +3501,7 @@ bool Builder::GenerateIfStatement(ast::IfStatement* stmt) { // continuing { ... // if (cond) {} else {break;} // } - auto is_just_a_break = [](ast::BlockStatement* block) { + auto is_just_a_break = [](const ast::BlockStatement* block) { return block && (block->statements.size() == 1) && block->Last()->Is(); }; @@ -3533,7 +3537,7 @@ bool Builder::GenerateIfStatement(ast::IfStatement* stmt) { return true; } -bool Builder::GenerateSwitchStatement(ast::SwitchStatement* stmt) { +bool Builder::GenerateSwitchStatement(const ast::SwitchStatement* stmt) { auto merge_block = result_op(); auto merge_block_id = merge_block.to_i(); @@ -3635,7 +3639,7 @@ bool Builder::GenerateSwitchStatement(ast::SwitchStatement* stmt) { return GenerateLabel(merge_block_id); } -bool Builder::GenerateReturnStatement(ast::ReturnStatement* stmt) { +bool Builder::GenerateReturnStatement(const ast::ReturnStatement* stmt) { if (stmt->value) { auto val_id = GenerateExpression(stmt->value); if (val_id == 0) { @@ -3654,7 +3658,7 @@ bool Builder::GenerateReturnStatement(ast::ReturnStatement* stmt) { return true; } -bool Builder::GenerateLoopStatement(ast::LoopStatement* stmt) { +bool Builder::GenerateLoopStatement(const ast::LoopStatement* stmt) { auto loop_header = result_op(); auto loop_header_id = loop_header.to_i(); if (!push_function_inst(spv::Op::OpBranch, {Operand::Int(loop_header_id)})) { @@ -3738,7 +3742,7 @@ bool Builder::GenerateLoopStatement(ast::LoopStatement* stmt) { return GenerateLabel(merge_block_id); } -bool Builder::GenerateStatement(ast::Statement* stmt) { +bool Builder::GenerateStatement(const ast::Statement* stmt) { if (auto* a = stmt->As()) { return GenerateAssignStatement(a); } @@ -3781,7 +3785,8 @@ bool Builder::GenerateStatement(ast::Statement* stmt) { return false; } -bool Builder::GenerateVariableDeclStatement(ast::VariableDeclStatement* stmt) { +bool Builder::GenerateVariableDeclStatement( + const ast::VariableDeclStatement* stmt) { return GenerateFunctionVariable(stmt->variable); } diff --git a/src/writer/spirv/builder.h b/src/writer/spirv/builder.h index baa4d7f4ba..0aad2ddc9a 100644 --- a/src/writer/spirv/builder.h +++ b/src/writer/spirv/builder.h @@ -241,7 +241,7 @@ class Builder { /// Generates an assignment statement /// @param assign the statement to generate /// @returns true if the statement was successfully generated - bool GenerateAssignStatement(ast::AssignmentStatement* assign); + bool GenerateAssignStatement(const ast::AssignmentStatement* assign); /// Generates a block statement, wrapped in a push/pop scope /// @param stmt the statement to generate /// @returns true if the statement was successfully generated @@ -253,33 +253,33 @@ class Builder { /// Generates a break statement /// @param stmt the statement to generate /// @returns true if the statement was successfully generated - bool GenerateBreakStatement(ast::BreakStatement* stmt); + bool GenerateBreakStatement(const ast::BreakStatement* stmt); /// Generates a continue statement /// @param stmt the statement to generate /// @returns true if the statement was successfully generated - bool GenerateContinueStatement(ast::ContinueStatement* stmt); + bool GenerateContinueStatement(const ast::ContinueStatement* stmt); /// Generates a discard statement /// @param stmt the statement to generate /// @returns true if the statement was successfully generated - bool GenerateDiscardStatement(ast::DiscardStatement* stmt); + bool GenerateDiscardStatement(const ast::DiscardStatement* stmt); /// Generates an entry point instruction /// @param func the function /// @param id the id of the function /// @returns true if the instruction was generated, false otherwise - bool GenerateEntryPoint(ast::Function* func, uint32_t id); + bool GenerateEntryPoint(const ast::Function* func, uint32_t id); /// Generates execution modes for an entry point /// @param func the function /// @param id the id of the function /// @returns false on failure - bool GenerateExecutionModes(ast::Function* func, uint32_t id); + bool GenerateExecutionModes(const ast::Function* func, uint32_t id); /// Generates an expression /// @param expr the expression to generate /// @returns the resulting ID of the expression or 0 on error - uint32_t GenerateExpression(ast::Expression* expr); + uint32_t GenerateExpression(const ast::Expression* expr); /// Generates the instructions for a function /// @param func the function to generate /// @returns true if the instructions were generated - bool GenerateFunction(ast::Function* func); + bool GenerateFunction(const ast::Function* func); /// Generates a function type if not already created /// @param func the function to generate for /// @returns the ID to use for the function type. Returns 0 on failure. @@ -294,11 +294,11 @@ class Builder { /// Generates a function variable /// @param var the variable /// @returns true if the variable was generated - bool GenerateFunctionVariable(ast::Variable* var); + bool GenerateFunctionVariable(const ast::Variable* var); /// Generates a global variable /// @param var the variable to generate /// @returns true if the variable is emited. - bool GenerateGlobalVariable(ast::Variable* var); + bool GenerateGlobalVariable(const ast::Variable* var); /// Generates an array accessor expression. /// /// For more information on accessors see the "Pointer evaluation" section of @@ -306,31 +306,31 @@ class Builder { /// /// @param expr the expresssion to generate /// @returns the id of the expression or 0 on failure - uint32_t GenerateAccessorExpression(ast::Expression* expr); + uint32_t GenerateAccessorExpression(const ast::Expression* expr); /// Generates an array accessor /// @param expr the accessor to generate /// @param info the current accessor information /// @returns true if the accessor was generated successfully - bool GenerateArrayAccessor(ast::ArrayAccessorExpression* expr, + bool GenerateArrayAccessor(const ast::ArrayAccessorExpression* expr, AccessorInfo* info); /// Generates a member accessor /// @param expr the accessor to generate /// @param info the current accessor information /// @returns true if the accessor was generated successfully - bool GenerateMemberAccessor(ast::MemberAccessorExpression* expr, + bool GenerateMemberAccessor(const ast::MemberAccessorExpression* expr, AccessorInfo* info); /// Generates an identifier expression /// @param expr the expresssion to generate /// @returns the id of the expression or 0 on failure - uint32_t GenerateIdentifierExpression(ast::IdentifierExpression* expr); + uint32_t GenerateIdentifierExpression(const ast::IdentifierExpression* expr); /// Generates a unary op expression /// @param expr the expression to generate /// @returns the id of the expression or 0 on failure - uint32_t GenerateUnaryOpExpression(ast::UnaryOpExpression* expr); + uint32_t GenerateUnaryOpExpression(const ast::UnaryOpExpression* expr); /// Generates an if statement /// @param stmt the statement to generate /// @returns true on success - bool GenerateIfStatement(ast::IfStatement* stmt); + bool GenerateIfStatement(const ast::IfStatement* stmt); /// Generates an import instruction for the "GLSL.std.450" extended /// instruction set, if one doesn't exist yet, and returns the import ID. /// @returns the import ID, or 0 on error. @@ -340,42 +340,44 @@ class Builder { /// @param expr the expression to generate /// @param is_global_init set true if this is a global variable constructor /// @returns the ID of the expression or 0 on failure. - uint32_t GenerateConstructorExpression(ast::Variable* var, - ast::ConstructorExpression* expr, + uint32_t GenerateConstructorExpression(const ast::Variable* var, + const ast::ConstructorExpression* expr, bool is_global_init); /// Generates a type constructor expression /// @param init the expression to generate /// @param is_global_init set true if this is a global variable constructor /// @returns the ID of the expression or 0 on failure. uint32_t GenerateTypeConstructorExpression( - ast::TypeConstructorExpression* init, + const ast::TypeConstructorExpression* init, bool is_global_init); /// Generates a literal constant if needed /// @param var the variable generated for, nullptr if no variable associated. /// @param lit the literal to generate /// @returns the ID on success or 0 on failure - uint32_t GenerateLiteralIfNeeded(ast::Variable* var, ast::Literal* lit); + uint32_t GenerateLiteralIfNeeded(const ast::Variable* var, + const ast::Literal* lit); /// Generates a binary expression /// @param expr the expression to generate /// @returns the expression ID on success or 0 otherwise - uint32_t GenerateBinaryExpression(ast::BinaryExpression* expr); + uint32_t GenerateBinaryExpression(const ast::BinaryExpression* expr); /// Generates a bitcast expression /// @param expr the expression to generate /// @returns the expression ID on success or 0 otherwise - uint32_t GenerateBitcastExpression(ast::BitcastExpression* expr); + uint32_t GenerateBitcastExpression(const ast::BitcastExpression* expr); /// Generates a short circuting binary expression /// @param expr the expression to generate /// @returns teh expression ID on success or 0 otherwise - uint32_t GenerateShortCircuitBinaryExpression(ast::BinaryExpression* expr); + uint32_t GenerateShortCircuitBinaryExpression( + const ast::BinaryExpression* expr); /// Generates a call expression /// @param expr the expression to generate /// @returns the expression ID on success or 0 otherwise - uint32_t GenerateCallExpression(ast::CallExpression* expr); + uint32_t GenerateCallExpression(const ast::CallExpression* expr); /// Generates an intrinsic call /// @param call the call expression /// @param intrinsic the semantic information for the intrinsic /// @returns the expression ID on success or 0 otherwise - uint32_t GenerateIntrinsic(ast::CallExpression* call, + uint32_t GenerateIntrinsic(const ast::CallExpression* call, const sem::Intrinsic* intrinsic); /// Generates a texture intrinsic call. Emits an error and returns false if /// we're currently outside a function. @@ -385,7 +387,7 @@ class Builder { /// @param result_id result identifier operand of the texture instruction /// parameters /// @returns true on success - bool GenerateTextureIntrinsic(ast::CallExpression* call, + bool GenerateTextureIntrinsic(const ast::CallExpression* call, const sem::Intrinsic* intrinsic, spirv::Operand result_type, spirv::Operand result_id); @@ -399,7 +401,7 @@ class Builder { /// @param result_type result type operand of the texture instruction /// @param result_id result identifier operand of the texture instruction /// @returns true on success - bool GenerateAtomicIntrinsic(ast::CallExpression* call, + bool GenerateAtomicIntrinsic(const ast::CallExpression* call, const sem::Intrinsic* intrinsic, Operand result_type, Operand result_id); @@ -419,34 +421,34 @@ class Builder { /// @param is_global_init if this is a global initializer /// @returns the expression ID on success or 0 otherwise uint32_t GenerateCastOrCopyOrPassthrough(const sem::Type* to_type, - ast::Expression* from_expr, + const ast::Expression* from_expr, bool is_global_init); /// Generates a loop statement /// @param stmt the statement to generate /// @returns true on successful generation - bool GenerateLoopStatement(ast::LoopStatement* stmt); + bool GenerateLoopStatement(const ast::LoopStatement* stmt); /// Generates a return statement /// @param stmt the statement to generate /// @returns true on success, false otherwise - bool GenerateReturnStatement(ast::ReturnStatement* stmt); + bool GenerateReturnStatement(const ast::ReturnStatement* stmt); /// Generates a switch statement /// @param stmt the statement to generate /// @returns ture on success, false otherwise - bool GenerateSwitchStatement(ast::SwitchStatement* stmt); + bool GenerateSwitchStatement(const ast::SwitchStatement* stmt); /// Generates a conditional section merge block /// @param cond the condition /// @param true_body the statements making up the true block /// @param cur_else_idx the index of the current else statement to process /// @param else_stmts the list of all else statements /// @returns true on success, false on failure - bool GenerateConditionalBlock(ast::Expression* cond, + bool GenerateConditionalBlock(const ast::Expression* cond, const ast::BlockStatement* true_body, size_t cur_else_idx, const ast::ElseStatementList& else_stmts); /// Generates a statement /// @param stmt the statement to generate /// @returns true if the statement was generated - bool GenerateStatement(ast::Statement* stmt); + bool GenerateStatement(const ast::Statement* stmt); /// Geneates an OpLoad /// @param type the type to load /// @param id the variable id to load @@ -504,7 +506,7 @@ class Builder { /// Generates a variable declaration statement /// @param stmt the statement to generate /// @returns true on successfull generation - bool GenerateVariableDeclStatement(ast::VariableDeclStatement* stmt); + bool GenerateVariableDeclStatement(const ast::VariableDeclStatement* stmt); /// Generates a vector type declaration /// @param vec the vector to generate /// @param result the result operand @@ -538,7 +540,7 @@ class Builder { /// @param expr the expression to check /// @param is_global_init if this is a global initializer /// @returns true if the constructor is constant - bool is_constructor_const(ast::Expression* expr, bool is_global_init); + bool is_constructor_const(const ast::Expression* expr, bool is_global_init); private: /// @returns an Operand with a new result ID in it. Increments the next_id_ @@ -547,7 +549,7 @@ class Builder { /// @returns the resolved type of the ast::Expression `expr` /// @param expr the expression - const sem::Type* TypeOf(ast::Expression* expr) const { + const sem::Type* TypeOf(const ast::Expression* expr) const { return builder_.TypeOf(expr); } @@ -594,7 +596,7 @@ class Builder { std::unordered_map texture_type_name_to_sampled_image_type_id_; ScopeStack scope_stack_; - std::unordered_map spirv_id_to_variable_; + std::unordered_map spirv_id_to_variable_; std::vector merge_stack_; std::vector continue_stack_; std::unordered_set capability_set_; diff --git a/src/writer/spirv/builder_binary_expression_test.cc b/src/writer/spirv/builder_binary_expression_test.cc index e384387175..a2388457df 100644 --- a/src/writer/spirv/builder_binary_expression_test.cc +++ b/src/writer/spirv/builder_binary_expression_test.cc @@ -923,7 +923,8 @@ OpBranch %9 namespace BinaryArithVectorScalar { enum class Type { f32, i32, u32 }; -static ast::Expression* MakeVectorExpr(ProgramBuilder* builder, Type type) { +static const ast::Expression* MakeVectorExpr(ProgramBuilder* builder, + Type type) { switch (type) { case Type::f32: return builder->vec3(1.f, 1.f, 1.f); @@ -934,7 +935,8 @@ static ast::Expression* MakeVectorExpr(ProgramBuilder* builder, Type type) { } return nullptr; } -static ast::Expression* MakeScalarExpr(ProgramBuilder* builder, Type type) { +static const ast::Expression* MakeScalarExpr(ProgramBuilder* builder, + Type type) { switch (type) { case Type::f32: return builder->Expr(1.f); @@ -967,8 +969,8 @@ using BinaryArithVectorScalarTest = TestParamHelper; TEST_P(BinaryArithVectorScalarTest, VectorScalar) { auto& param = GetParam(); - ast::Expression* lhs = MakeVectorExpr(this, param.type); - ast::Expression* rhs = MakeScalarExpr(this, param.type); + const ast::Expression* lhs = MakeVectorExpr(this, param.type); + const ast::Expression* rhs = MakeScalarExpr(this, param.type); std::string op_type_decl = OpTypeDecl(param.type); auto* expr = create(param.op, lhs, rhs); @@ -1005,8 +1007,8 @@ OpFunctionEnd TEST_P(BinaryArithVectorScalarTest, ScalarVector) { auto& param = GetParam(); - ast::Expression* lhs = MakeScalarExpr(this, param.type); - ast::Expression* rhs = MakeVectorExpr(this, param.type); + const ast::Expression* lhs = MakeScalarExpr(this, param.type); + const ast::Expression* rhs = MakeVectorExpr(this, param.type); std::string op_type_decl = OpTypeDecl(param.type); auto* expr = create(param.op, lhs, rhs); @@ -1068,8 +1070,8 @@ using BinaryArithVectorScalarMultiplyTest = TestParamHelper; TEST_P(BinaryArithVectorScalarMultiplyTest, VectorScalar) { auto& param = GetParam(); - ast::Expression* lhs = MakeVectorExpr(this, param.type); - ast::Expression* rhs = MakeScalarExpr(this, param.type); + const ast::Expression* lhs = MakeVectorExpr(this, param.type); + const ast::Expression* rhs = MakeScalarExpr(this, param.type); std::string op_type_decl = OpTypeDecl(param.type); auto* expr = create(param.op, lhs, rhs); @@ -1102,8 +1104,8 @@ OpFunctionEnd TEST_P(BinaryArithVectorScalarMultiplyTest, ScalarVector) { auto& param = GetParam(); - ast::Expression* lhs = MakeScalarExpr(this, param.type); - ast::Expression* rhs = MakeVectorExpr(this, param.type); + const ast::Expression* lhs = MakeScalarExpr(this, param.type); + const ast::Expression* rhs = MakeVectorExpr(this, param.type); std::string op_type_decl = OpTypeDecl(param.type); auto* expr = create(param.op, lhs, rhs); @@ -1151,8 +1153,8 @@ using BinaryArithMatrixMatrix = TestParamHelper; TEST_P(BinaryArithMatrixMatrix, AddOrSubtract) { auto& param = GetParam(); - ast::Expression* lhs = mat3x4(); - ast::Expression* rhs = mat3x4(); + const ast::Expression* lhs = mat3x4(); + const ast::Expression* rhs = mat3x4(); auto* expr = create(param.op, lhs, rhs); @@ -1200,8 +1202,8 @@ using BinaryArithMatrixMatrixMultiply = TestParamHelper; TEST_P(BinaryArithMatrixMatrixMultiply, Multiply) { auto& param = GetParam(); - ast::Expression* lhs = mat3x4(); - ast::Expression* rhs = mat4x3(); + const ast::Expression* lhs = mat3x4(); + const ast::Expression* rhs = mat4x3(); auto* expr = create(param.op, lhs, rhs); diff --git a/src/writer/spirv/builder_function_decoration_test.cc b/src/writer/spirv/builder_function_decoration_test.cc index 0aa57ea34a..309a6fb312 100644 --- a/src/writer/spirv/builder_function_decoration_test.cc +++ b/src/writer/spirv/builder_function_decoration_test.cc @@ -50,8 +50,8 @@ using Decoration_StageTest = TestParamHelper; TEST_P(Decoration_StageTest, Emit) { auto params = GetParam(); - ast::Variable* var = nullptr; - ast::Type* ret_type = nullptr; + const ast::Variable* var = nullptr; + const ast::Type* ret_type = nullptr; ast::DecorationList ret_type_decos; ast::StatementList body; if (params.stage == ast::PipelineStage::kVertex) { diff --git a/src/writer/text_generator.h b/src/writer/text_generator.h index 8b8aeafdfe..3bd204961a 100644 --- a/src/writer/text_generator.h +++ b/src/writer/text_generator.h @@ -195,7 +195,7 @@ class TextGenerator { /// @returns the resolved type of the ast::Expression `expr` /// @param expr the expression - sem::Type* TypeOf(ast::Expression* expr) const { + const sem::Type* TypeOf(const ast::Expression* expr) const { return builder_.TypeOf(expr); } diff --git a/src/writer/wgsl/generator_impl.cc b/src/writer/wgsl/generator_impl.cc index 68133bb862..3c3a06d2bf 100644 --- a/src/writer/wgsl/generator_impl.cc +++ b/src/writer/wgsl/generator_impl.cc @@ -114,7 +114,8 @@ bool GeneratorImpl::EmitTypeDecl(const ast::TypeDecl* ty) { return true; } -bool GeneratorImpl::EmitExpression(std::ostream& out, ast::Expression* expr) { +bool GeneratorImpl::EmitExpression(std::ostream& out, + const ast::Expression* expr) { if (auto* a = expr->As()) { return EmitArrayAccessor(out, a); } @@ -144,8 +145,9 @@ bool GeneratorImpl::EmitExpression(std::ostream& out, ast::Expression* expr) { return false; } -bool GeneratorImpl::EmitArrayAccessor(std::ostream& out, - ast::ArrayAccessorExpression* expr) { +bool GeneratorImpl::EmitArrayAccessor( + std::ostream& out, + const ast::ArrayAccessorExpression* expr) { bool paren_lhs = !expr->array ->IsAnyOfstructure ->IsAnyOftype)) { return false; @@ -208,7 +211,8 @@ bool GeneratorImpl::EmitBitcast(std::ostream& out, return true; } -bool GeneratorImpl::EmitCall(std::ostream& out, ast::CallExpression* expr) { +bool GeneratorImpl::EmitCall(std::ostream& out, + const ast::CallExpression* expr) { if (!EmitExpression(out, expr->func)) { return false; } @@ -233,15 +237,16 @@ bool GeneratorImpl::EmitCall(std::ostream& out, ast::CallExpression* expr) { } bool GeneratorImpl::EmitConstructor(std::ostream& out, - ast::ConstructorExpression* expr) { + const ast::ConstructorExpression* expr) { if (auto* scalar = expr->As()) { return EmitScalarConstructor(out, scalar); } return EmitTypeConstructor(out, expr->As()); } -bool GeneratorImpl::EmitTypeConstructor(std::ostream& out, - ast::TypeConstructorExpression* expr) { +bool GeneratorImpl::EmitTypeConstructor( + std::ostream& out, + const ast::TypeConstructorExpression* expr) { if (!EmitType(out, expr->type)) { return false; } @@ -266,11 +271,11 @@ bool GeneratorImpl::EmitTypeConstructor(std::ostream& out, bool GeneratorImpl::EmitScalarConstructor( std::ostream& out, - ast::ScalarConstructorExpression* expr) { + const ast::ScalarConstructorExpression* expr) { return EmitLiteral(out, expr->literal); } -bool GeneratorImpl::EmitLiteral(std::ostream& out, ast::Literal* lit) { +bool GeneratorImpl::EmitLiteral(std::ostream& out, const ast::Literal* lit) { if (auto* bl = lit->As()) { out << (bl->value ? "true" : "false"); } else if (auto* fl = lit->As()) { @@ -287,12 +292,12 @@ bool GeneratorImpl::EmitLiteral(std::ostream& out, ast::Literal* lit) { } bool GeneratorImpl::EmitIdentifier(std::ostream& out, - ast::IdentifierExpression* expr) { + const ast::IdentifierExpression* expr) { out << program_->Symbols().NameFor(expr->symbol); return true; } -bool GeneratorImpl::EmitFunction(ast::Function* func) { +bool GeneratorImpl::EmitFunction(const ast::Function* func) { if (func->decorations.size()) { if (!EmitDecorations(line(), func->decorations)) { return false; @@ -592,7 +597,7 @@ bool GeneratorImpl::EmitStructType(const ast::Struct* str) { return true; } -bool GeneratorImpl::EmitVariable(std::ostream& out, ast::Variable* var) { +bool GeneratorImpl::EmitVariable(std::ostream& out, const ast::Variable* var) { if (!var->decorations.empty()) { if (!EmitDecorations(out, var->decorations)) { return false; @@ -706,7 +711,8 @@ bool GeneratorImpl::EmitDecorations(std::ostream& out, return true; } -bool GeneratorImpl::EmitBinary(std::ostream& out, ast::BinaryExpression* expr) { +bool GeneratorImpl::EmitBinary(std::ostream& out, + const ast::BinaryExpression* expr) { out << "("; if (!EmitExpression(out, expr->lhs)) { @@ -785,7 +791,7 @@ bool GeneratorImpl::EmitBinary(std::ostream& out, ast::BinaryExpression* expr) { } bool GeneratorImpl::EmitUnaryOp(std::ostream& out, - ast::UnaryOpExpression* expr) { + const ast::UnaryOpExpression* expr) { switch (expr->op) { case ast::UnaryOp::kAddressOf: out << "&"; @@ -824,7 +830,7 @@ bool GeneratorImpl::EmitBlock(const ast::BlockStatement* stmt) { return true; } -bool GeneratorImpl::EmitStatement(ast::Statement* stmt) { +bool GeneratorImpl::EmitStatement(const ast::Statement* stmt) { if (auto* a = stmt->As()) { return EmitAssign(a); } @@ -890,7 +896,7 @@ bool GeneratorImpl::EmitStatementsWithIndent(const ast::StatementList& stmts) { return EmitStatements(stmts); } -bool GeneratorImpl::EmitAssign(ast::AssignmentStatement* stmt) { +bool GeneratorImpl::EmitAssign(const ast::AssignmentStatement* stmt) { auto out = line(); if (!EmitExpression(out, stmt->lhs)) { @@ -908,12 +914,12 @@ bool GeneratorImpl::EmitAssign(ast::AssignmentStatement* stmt) { return true; } -bool GeneratorImpl::EmitBreak(ast::BreakStatement*) { +bool GeneratorImpl::EmitBreak(const ast::BreakStatement*) { line() << "break;"; return true; } -bool GeneratorImpl::EmitCase(ast::CaseStatement* stmt) { +bool GeneratorImpl::EmitCase(const ast::CaseStatement* stmt) { if (stmt->IsDefault()) { line() << "default: {"; } else { @@ -942,17 +948,17 @@ bool GeneratorImpl::EmitCase(ast::CaseStatement* stmt) { return true; } -bool GeneratorImpl::EmitContinue(ast::ContinueStatement*) { +bool GeneratorImpl::EmitContinue(const ast::ContinueStatement*) { line() << "continue;"; return true; } -bool GeneratorImpl::EmitFallthrough(ast::FallthroughStatement*) { +bool GeneratorImpl::EmitFallthrough(const ast::FallthroughStatement*) { line() << "fallthrough;"; return true; } -bool GeneratorImpl::EmitIf(ast::IfStatement* stmt) { +bool GeneratorImpl::EmitIf(const ast::IfStatement* stmt) { { auto out = line(); out << "if ("; @@ -988,12 +994,12 @@ bool GeneratorImpl::EmitIf(ast::IfStatement* stmt) { return true; } -bool GeneratorImpl::EmitDiscard(ast::DiscardStatement*) { +bool GeneratorImpl::EmitDiscard(const ast::DiscardStatement*) { line() << "discard;"; return true; } -bool GeneratorImpl::EmitLoop(ast::LoopStatement* stmt) { +bool GeneratorImpl::EmitLoop(const ast::LoopStatement* stmt) { line() << "loop {"; increment_indent(); @@ -1016,7 +1022,7 @@ bool GeneratorImpl::EmitLoop(ast::LoopStatement* stmt) { return true; } -bool GeneratorImpl::EmitForLoop(ast::ForLoopStatement* stmt) { +bool GeneratorImpl::EmitForLoop(const ast::ForLoopStatement* stmt) { TextBuffer init_buf; if (auto* init = stmt->initializer) { TINT_SCOPED_ASSIGNMENT(current_buffer_, &init_buf); @@ -1090,7 +1096,7 @@ bool GeneratorImpl::EmitForLoop(ast::ForLoopStatement* stmt) { return true; } -bool GeneratorImpl::EmitReturn(ast::ReturnStatement* stmt) { +bool GeneratorImpl::EmitReturn(const ast::ReturnStatement* stmt) { auto out = line(); out << "return"; if (stmt->value) { @@ -1103,7 +1109,7 @@ bool GeneratorImpl::EmitReturn(ast::ReturnStatement* stmt) { return true; } -bool GeneratorImpl::EmitSwitch(ast::SwitchStatement* stmt) { +bool GeneratorImpl::EmitSwitch(const ast::SwitchStatement* stmt) { { auto out = line(); out << "switch("; diff --git a/src/writer/wgsl/generator_impl.h b/src/writer/wgsl/generator_impl.h index c413ee2081..c626d76193 100644 --- a/src/writer/wgsl/generator_impl.h +++ b/src/writer/wgsl/generator_impl.h @@ -63,21 +63,22 @@ class GeneratorImpl : public TextGenerator { /// @param out the output of the expression stream /// @param expr the expression to emit /// @returns true if the array accessor was emitted - bool EmitArrayAccessor(std::ostream& out, ast::ArrayAccessorExpression* expr); + bool EmitArrayAccessor(std::ostream& out, + const ast::ArrayAccessorExpression* expr); /// Handles an assignment statement /// @param stmt the statement to emit /// @returns true if the statement was emitted successfully - bool EmitAssign(ast::AssignmentStatement* stmt); + bool EmitAssign(const ast::AssignmentStatement* stmt); /// Handles generating a binary expression /// @param out the output of the expression stream /// @param expr the binary expression /// @returns true if the expression was emitted, false otherwise - bool EmitBinary(std::ostream& out, ast::BinaryExpression* expr); + bool EmitBinary(std::ostream& out, const ast::BinaryExpression* expr); /// Handles generating a bitcast expression /// @param out the output of the expression stream /// @param expr the bitcast expression /// @returns true if the bitcast was emitted - bool EmitBitcast(std::ostream& out, ast::BitcastExpression* expr); + bool EmitBitcast(std::ostream& out, const ast::BitcastExpression* expr); /// Handles a block statement /// @param stmt the statement to emit /// @returns true if the statement was emitted successfully @@ -85,84 +86,85 @@ class GeneratorImpl : public TextGenerator { /// Handles a break statement /// @param stmt the statement to emit /// @returns true if the statement was emitted successfully - bool EmitBreak(ast::BreakStatement* stmt); + bool EmitBreak(const ast::BreakStatement* stmt); /// Handles generating a call expression /// @param out the output of the expression stream /// @param expr the call expression /// @returns true if the call expression is emitted - bool EmitCall(std::ostream& out, ast::CallExpression* expr); + bool EmitCall(std::ostream& out, const ast::CallExpression* expr); /// Handles a case statement /// @param stmt the statement /// @returns true if the statment was emitted successfully - bool EmitCase(ast::CaseStatement* stmt); + bool EmitCase(const ast::CaseStatement* stmt); /// Handles generating a scalar constructor /// @param out the output of the expression stream /// @param expr the scalar constructor expression /// @returns true if the scalar constructor is emitted bool EmitScalarConstructor(std::ostream& out, - ast::ScalarConstructorExpression* expr); + const ast::ScalarConstructorExpression* expr); /// Handles a continue statement /// @param stmt the statement to emit /// @returns true if the statement was emitted successfully - bool EmitContinue(ast::ContinueStatement* stmt); + bool EmitContinue(const ast::ContinueStatement* stmt); /// Handles generate an Expression /// @param out the output of the expression stream /// @param expr the expression /// @returns true if the expression was emitted - bool EmitExpression(std::ostream& out, ast::Expression* expr); + bool EmitExpression(std::ostream& out, const ast::Expression* expr); /// Handles generating a fallthrough statement /// @param stmt the fallthrough statement /// @returns true if the statement was successfully emitted - bool EmitFallthrough(ast::FallthroughStatement* stmt); + bool EmitFallthrough(const ast::FallthroughStatement* stmt); /// Handles generating a function /// @param func the function to generate /// @returns true if the function was emitted - bool EmitFunction(ast::Function* func); + bool EmitFunction(const ast::Function* func); /// Handles generating an identifier expression /// @param out the output of the expression stream /// @param expr the identifier expression /// @returns true if the identifeir was emitted - bool EmitIdentifier(std::ostream& out, ast::IdentifierExpression* expr); + bool EmitIdentifier(std::ostream& out, const ast::IdentifierExpression* expr); /// Handles an if statement /// @param stmt the statement to emit /// @returns true if the statement was successfully emitted - bool EmitIf(ast::IfStatement* stmt); + bool EmitIf(const ast::IfStatement* stmt); /// Handles generating constructor expressions /// @param out the output of the expression stream /// @param expr the constructor expression /// @returns true if the expression was emitted - bool EmitConstructor(std::ostream& out, ast::ConstructorExpression* expr); + bool EmitConstructor(std::ostream& out, + const ast::ConstructorExpression* expr); /// Handles generating a discard statement /// @param stmt the discard statement /// @returns true if the statement was successfully emitted - bool EmitDiscard(ast::DiscardStatement* stmt); + bool EmitDiscard(const ast::DiscardStatement* stmt); /// Handles a literal /// @param out the output of the expression stream /// @param lit the literal to emit /// @returns true if the literal was successfully emitted - bool EmitLiteral(std::ostream& out, ast::Literal* lit); + bool EmitLiteral(std::ostream& out, const ast::Literal* lit); /// Handles a loop statement /// @param stmt the statement to emit /// @returns true if the statement was emtited - bool EmitLoop(ast::LoopStatement* stmt); + bool EmitLoop(const ast::LoopStatement* stmt); /// Handles a for-loop statement /// @param stmt the statement to emit /// @returns true if the statement was emtited - bool EmitForLoop(ast::ForLoopStatement* stmt); + bool EmitForLoop(const ast::ForLoopStatement* stmt); /// Handles a member accessor expression /// @param out the output of the expression stream /// @param expr the member accessor expression /// @returns true if the member accessor was emitted bool EmitMemberAccessor(std::ostream& out, - ast::MemberAccessorExpression* expr); + const ast::MemberAccessorExpression* expr); /// Handles return statements /// @param stmt the statement to emit /// @returns true if the statement was successfully emitted - bool EmitReturn(ast::ReturnStatement* stmt); + bool EmitReturn(const ast::ReturnStatement* stmt); /// Handles statement /// @param stmt the statement to emit /// @returns true if the statement was emitted - bool EmitStatement(ast::Statement* stmt); + bool EmitStatement(const ast::Statement* stmt); /// Handles a statement list /// @param stmts the statements to emit /// @returns true if the statements were emitted @@ -174,7 +176,7 @@ class GeneratorImpl : public TextGenerator { /// Handles generating a switch statement /// @param stmt the statement to emit /// @returns true if the statement was emitted - bool EmitSwitch(ast::SwitchStatement* stmt); + bool EmitSwitch(const ast::SwitchStatement* stmt); /// Handles generating type /// @param out the output of the expression stream /// @param type the type to generate @@ -199,17 +201,17 @@ class GeneratorImpl : public TextGenerator { /// @param expr the type constructor expression /// @returns true if the constructor is emitted bool EmitTypeConstructor(std::ostream& out, - ast::TypeConstructorExpression* expr); + const ast::TypeConstructorExpression* expr); /// Handles a unary op expression /// @param out the output of the expression stream /// @param expr the expression to emit /// @returns true if the expression was emitted - bool EmitUnaryOp(std::ostream& out, ast::UnaryOpExpression* expr); + bool EmitUnaryOp(std::ostream& out, const ast::UnaryOpExpression* expr); /// Handles generating a variable /// @param out the output of the expression stream /// @param var the variable to generate /// @returns true if the variable was emitted - bool EmitVariable(std::ostream& out, ast::Variable* var); + bool EmitVariable(std::ostream& out, const ast::Variable* var); /// Handles generating a decoration list /// @param out the output of the expression stream /// @param decos the decoration list diff --git a/src/writer/wgsl/generator_impl_binary_test.cc b/src/writer/wgsl/generator_impl_binary_test.cc index 3432f83cff..aeaf894c77 100644 --- a/src/writer/wgsl/generator_impl_binary_test.cc +++ b/src/writer/wgsl/generator_impl_binary_test.cc @@ -31,7 +31,7 @@ using WgslBinaryTest = TestParamHelper; TEST_P(WgslBinaryTest, Emit) { auto params = GetParam(); - auto op_ty = [&]() -> ast::Type* { + auto op_ty = [&]() -> const ast::Type* { if (params.op == ast::BinaryOp::kLogicalAnd || params.op == ast::BinaryOp::kLogicalOr) { return ty.bool_(); diff --git a/tools/src/cmd/remote-compile/main.cc b/tools/src/cmd/remote-compile/main.cc index df541c6ca9..d6f62e7671 100644 --- a/tools/src/cmd/remote-compile/main.cc +++ b/tools/src/cmd/remote-compile/main.cc @@ -168,7 +168,7 @@ struct Message { explicit Message(Type ty) : type(ty) {} - Type const type; + const Type type; }; struct ConnectionResponse : Message { // Server -> Client