diff --git a/src/tint/reader/spirv/function.cc b/src/tint/reader/spirv/function.cc index 565b788483..224888e678 100644 --- a/src/tint/reader/spirv/function.cc +++ b/src/tint/reader/spirv/function.cc @@ -955,9 +955,8 @@ bool FunctionEmitter::Emit() { return false; } - builder_.AST().AddFunction(create( - decl.source, builder_.Symbols().Register(decl.name), std::move(decl.params), - decl.return_type->Build(builder_), body, std::move(decl.attributes), utils::Empty)); + builder_.Func(decl.source, decl.name, std::move(decl.params), + decl.return_type->Build(builder_), body, std::move(decl.attributes)); } if (ep_info_ && !ep_info_->inner_name.empty()) { @@ -1395,7 +1394,6 @@ bool FunctionEmitter::EmitEntryPointAsWrapper() { } } - auto* body = create(source, stmts, utils::Empty); AttributeList fn_attrs; fn_attrs.Push(create(source, ep_info_->stage)); @@ -1409,9 +1407,8 @@ bool FunctionEmitter::EmitEntryPointAsWrapper() { } } - builder_.AST().AddFunction(create( - source, builder_.Symbols().Register(ep_info_->name), std::move(decl.params), return_type, - body, std::move(fn_attrs), AttributeList{})); + builder_.Func(source, ep_info_->name, std::move(decl.params), return_type, std::move(stmts), + std::move(fn_attrs)); return true; } @@ -5779,21 +5776,18 @@ bool FunctionEmitter::EmitAtomicOp(const spvtools::opt::Instruction& inst) { } // Emit stub, will be removed by transform::SpirvAtomic - auto sym = builder_.Symbols().New(std::string("stub_") + sem::str(builtin)); - auto* stub_deco = builder_.ASTNodes().Create( - builder_.ID(), builder_.AllocateNodeID(), builtin); - auto* stub = - create(Source{}, sym, std::move(params), ret_type, - /* body */ nullptr, - AttributeList{ - stub_deco, - builder_.Disable(ast::DisabledValidation::kFunctionHasNoBody), - }, - AttributeList{}); - builder_.AST().AddFunction(stub); + auto* stub = builder_.Func( + Source{}, builder_.Symbols().New(std::string("stub_") + sem::str(builtin)), + std::move(params), ret_type, + /* body */ nullptr, + utils::Vector{ + builder_.ASTNodes().Create( + builder_.ID(), builder_.AllocateNodeID(), builtin), + builder_.Disable(ast::DisabledValidation::kFunctionHasNoBody), + }); // Emit call to stub, will be replaced with call to atomic builtin by transform::SpirvAtomic - auto* call = builder_.Call(Source{}, sym, std::move(exprs)); + auto* call = builder_.Call(Source{}, stub->symbol, std::move(exprs)); if (inst.type_id() != 0) { auto* result_type = parser_impl_.ConvertType(inst.type_id()); TypedExpression expr{result_type, call}; diff --git a/src/tint/reader/spirv/parser_impl.cc b/src/tint/reader/spirv/parser_impl.cc index 1b7cd48d1d..a61003925d 100644 --- a/src/tint/reader/spirv/parser_impl.cc +++ b/src/tint/reader/spirv/parser_impl.cc @@ -1167,9 +1167,9 @@ const Type* ParserImpl::ConvertType(uint32_t type_id, ++num_non_writable_members; } const auto member_name = namer_.GetMemberName(type_id, member_index); - auto* ast_struct_member = create( - Source{}, builder_.Symbols().Register(member_name), ast_member_ty->Build(builder_), - std::move(ast_member_decorations)); + auto* ast_struct_member = + builder_.Member(Source{}, member_name, ast_member_ty->Build(builder_), + std::move(ast_member_decorations)); ast_members.Push(ast_struct_member); } @@ -1390,7 +1390,6 @@ bool ParserImpl::EmitScalarSpecConstants() { auto* ast_var = MakeOverride(inst.result_id(), ast_type, ast_expr, std::move(spec_id_decos)); if (ast_var) { - builder_.AST().AddGlobalVariable(ast_var); scalar_spec_constants_.insert(inst.result_id()); } } @@ -1577,11 +1576,11 @@ const spvtools::opt::analysis::IntConstant* ParserImpl::GetArraySize(uint32_t va return size->AsIntConstant(); } -ast::Var* ParserImpl::MakeVar(uint32_t id, - type::AddressSpace address_space, - const Type* storage_type, - const ast::Expression* initializer, - AttributeList decorations) { +const ast::Var* ParserImpl::MakeVar(uint32_t id, + type::AddressSpace address_space, + const Type* storage_type, + const ast::Expression* initializer, + AttributeList decorations) { if (storage_type == nullptr) { Fail() << "internal error: can't make ast::Variable for null type"; return nullptr; @@ -1610,35 +1609,37 @@ ast::Var* ParserImpl::MakeVar(uint32_t id, } auto sym = builder_.Symbols().Register(namer_.Name(id)); - return create(Source{}, sym, storage_type->Build(builder_), address_space, access, - initializer, decorations); + return builder_.Var(Source{}, sym, storage_type->Build(builder_), address_space, access, + initializer, decorations); } -ast::Let* ParserImpl::MakeLet(uint32_t id, const Type* type, const ast::Expression* initializer) { +const ast::Let* ParserImpl::MakeLet(uint32_t id, + const Type* type, + const ast::Expression* initializer) { auto sym = builder_.Symbols().Register(namer_.Name(id)); - return create(Source{}, sym, type->Build(builder_), initializer, utils::Empty); + return builder_.Let(Source{}, sym, type->Build(builder_), initializer, utils::Empty); } -ast::Override* ParserImpl::MakeOverride(uint32_t id, - const Type* type, - const ast::Expression* initializer, - AttributeList decorations) { +const ast::Override* ParserImpl::MakeOverride(uint32_t id, + const Type* type, + const ast::Expression* initializer, + AttributeList decorations) { if (!ConvertDecorationsForVariable(id, &type, &decorations, false)) { return nullptr; } auto sym = builder_.Symbols().Register(namer_.Name(id)); - return create(Source{}, sym, type->Build(builder_), initializer, decorations); + return builder_.Override(Source{}, sym, type->Build(builder_), initializer, decorations); } -ast::Parameter* ParserImpl::MakeParameter(uint32_t id, - const Type* type, - AttributeList decorations) { +const ast::Parameter* ParserImpl::MakeParameter(uint32_t id, + const Type* type, + AttributeList decorations) { if (!ConvertDecorationsForVariable(id, &type, &decorations, false)) { return nullptr; } auto sym = builder_.Symbols().Register(namer_.Name(id)); - return create(Source{}, sym, type->Build(builder_), decorations); + return builder_.Param(Source{}, sym, type->Build(builder_), decorations); } bool ParserImpl::ConvertDecorationsForVariable(uint32_t id, diff --git a/src/tint/reader/spirv/parser_impl.h b/src/tint/reader/spirv/parser_impl.h index 55e2250af9..1e64866b21 100644 --- a/src/tint/reader/spirv/parser_impl.h +++ b/src/tint/reader/spirv/parser_impl.h @@ -429,18 +429,18 @@ class ParserImpl : Reader { /// @param decorations the variable decorations /// @returns a new Variable node, or null in the ignorable variable case and /// in the error case - ast::Var* MakeVar(uint32_t id, - type::AddressSpace address_space, - const Type* storage_type, - const ast::Expression* initializer, - AttributeList decorations); + const ast::Var* MakeVar(uint32_t id, + type::AddressSpace address_space, + const Type* storage_type, + const ast::Expression* initializer, + AttributeList decorations); /// Creates an AST 'let' node for a SPIR-V ID, including any attached decorations,. /// @param id the SPIR-V result ID /// @param type the type of the variable /// @param initializer the variable initializer /// @returns the AST 'let' node - ast::Let* MakeLet(uint32_t id, const Type* type, const ast::Expression* initializer); + const ast::Let* MakeLet(uint32_t id, const Type* type, const ast::Expression* initializer); /// Creates an AST 'override' node for a SPIR-V ID, including any attached decorations. /// @param id the SPIR-V result ID @@ -448,10 +448,10 @@ class ParserImpl : Reader { /// @param initializer the variable initializer /// @param decorations the variable decorations /// @returns the AST 'override' node - ast::Override* MakeOverride(uint32_t id, - const Type* type, - const ast::Expression* initializer, - AttributeList decorations); + const ast::Override* MakeOverride(uint32_t id, + const Type* type, + const ast::Expression* initializer, + AttributeList decorations); /// Creates an AST parameter node for a SPIR-V ID, including any attached decorations, unless /// it's an ignorable builtin variable. @@ -459,7 +459,7 @@ class ParserImpl : Reader { /// @param type the type of the parameter /// @param decorations the parameter decorations /// @returns the AST parameter node - ast::Parameter* MakeParameter(uint32_t id, const Type* type, AttributeList decorations); + const ast::Parameter* MakeParameter(uint32_t id, const Type* type, AttributeList decorations); /// Returns true if a constant expression can be generated. /// @param id the SPIR-V ID of the value @@ -885,7 +885,7 @@ class ParserImpl : Reader { std::unordered_map handle_type_; /// Maps the SPIR-V ID of a module-scope variable to its AST variable. - utils::Hashmap module_variable_; + utils::Hashmap module_variable_; // Set of symbols of declared type that have been added, used to avoid // adding duplicates. diff --git a/src/tint/reader/wgsl/parser_impl.cc b/src/tint/reader/wgsl/parser_impl.cc index 8d15e4510d..ec7cfb8823 100644 --- a/src/tint/reader/wgsl/parser_impl.cc +++ b/src/tint/reader/wgsl/parser_impl.cc @@ -505,8 +505,6 @@ Maybe ParserImpl::global_decl() { return Failure::kErrored; } } - - builder_.AST().AddGlobalVariable(gc.value); return kSuccess; } @@ -565,7 +563,6 @@ Maybe ParserImpl::global_decl() { errored = true; } if (func.matched) { - builder_.AST().AddFunction(func.value); return kSuccess; } @@ -631,13 +628,13 @@ Maybe ParserImpl::global_variable_decl(AttributeList& attr TINT_DEFER(attrs.Clear()); - return create(decl->source, // source - builder_.Symbols().Register(decl->name), // symbol - decl->type, // type - decl->address_space, // address space - decl->access, // access control - initializer, // initializer - std::move(attrs)); // attributes + return builder_.Var(decl->source, // source + decl->name, // symbol + decl->type, // type + decl->address_space, // address space + decl->access, // access control + initializer, // initializer + std::move(attrs)); // attributes } // global_constant_decl : @@ -646,7 +643,6 @@ Maybe ParserImpl::global_variable_decl(AttributeList& attr // global_const_initializer // : EQUAL const_expr Maybe ParserImpl::global_constant_decl(AttributeList& attrs) { - bool is_const = false; bool is_overridable = false; const char* use = nullptr; Source source; @@ -690,25 +686,18 @@ Maybe ParserImpl::global_constant_decl(AttributeList& attr TINT_DEFER(attrs.Clear()); - if (is_const) { - return create(decl->source, // source - builder_.Symbols().Register(decl->name), // symbol - decl->type, // type - initializer, // initializer - std::move(attrs)); // attributes - } if (is_overridable) { - return create(decl->source, // source - builder_.Symbols().Register(decl->name), // symbol - decl->type, // type - initializer, // initializer - std::move(attrs)); // attributes + return builder_.Override(decl->source, // source + decl->name, // symbol + decl->type, // type + initializer, // initializer + std::move(attrs)); // attributes } - return create(decl->source, // source - builder_.Symbols().Register(decl->name), // symbol - decl->type, // type - initializer, // initializer - std::move(attrs)); // attributes + return builder_.GlobalConst(decl->source, // source + decl->name, // symbol + decl->type, // type + initializer, // initializer + std::move(attrs)); // attributes } // variable_decl @@ -1451,7 +1440,7 @@ Expect ParserImpl::expect_struct_body_decl() { // struct_member // : attribute* ident_with_type_specifier -Expect ParserImpl::expect_struct_member() { +Expect ParserImpl::expect_struct_member() { auto attrs = attribute_list(); if (attrs.errored) { return Failure::kErrored; @@ -1462,8 +1451,7 @@ Expect ParserImpl::expect_struct_member() { return Failure::kErrored; } - return create(decl->source, builder_.Symbols().Register(decl->name), - decl->type, std::move(attrs.value)); + return builder_.Member(decl->source, decl->name, decl->type, std::move(attrs.value)); } // const_assert_statement @@ -1523,9 +1511,8 @@ Maybe ParserImpl::function_decl(AttributeList& attrs) { TINT_DEFER(attrs.Clear()); - return create(header->source, builder_.Symbols().Register(header->name), - header->params, header->return_type, body.value, std::move(attrs), - header->return_type_attributes); + return builder_.Func(header->source, header->name, header->params, header->return_type, + body.value, std::move(attrs), header->return_type_attributes); } // function_header @@ -1618,7 +1605,7 @@ Expect ParserImpl::expect_param_list() { // param // : attribute_list* ident COLON type_specifier -Expect ParserImpl::expect_param() { +Expect ParserImpl::expect_param() { auto attrs = attribute_list(); auto decl = expect_ident_with_type_specifier("parameter"); @@ -1626,10 +1613,10 @@ Expect ParserImpl::expect_param() { return Failure::kErrored; } - return create(decl->source, // source - builder_.Symbols().Register(decl->name), // symbol - decl->type, // type - std::move(attrs.value)); // attributes + return builder_.Param(decl->source, // source + decl->name, // symbol + decl->type, // type + std::move(attrs.value)); // attributes } // interpolation_sample_name @@ -1943,11 +1930,10 @@ Maybe ParserImpl::variable_statement() { return add_error(peek(), "missing initializer for 'const' declaration"); } - auto* const_ = create(typed_ident->source, // source - builder_.Symbols().Register(typed_ident->name), // symbol - typed_ident->type, // type - initializer.value, // initializer - utils::Empty); // attributes + auto* const_ = builder_.Const(typed_ident->source, // source + typed_ident->name, // symbol + typed_ident->type, // type + initializer.value); // initializer return create(decl_source, const_); } @@ -1972,11 +1958,10 @@ Maybe ParserImpl::variable_statement() { return add_error(peek(), "missing initializer for 'let' declaration"); } - auto* let = create(typed_ident->source, // source - builder_.Symbols().Register(typed_ident->name), // symbol - typed_ident->type, // type - initializer.value, // initializer - utils::Empty); // attributes + auto* let = builder_.Let(typed_ident->source, // source + typed_ident->name, // symbol + typed_ident->type, // type + initializer.value); // initializer return create(decl_source, let); } @@ -2004,13 +1989,12 @@ Maybe ParserImpl::variable_statement() { initializer = initializer_expr.value; } - auto* var = create(decl_source, // source - builder_.Symbols().Register(decl->name), // symbol - decl->type, // type - decl->address_space, // address space - decl->access, // access control - initializer, // initializer - utils::Empty); // attributes + auto* var = builder_.Var(decl_source, // source + decl->name, // symbol + decl->type, // type + decl->address_space, // address space + decl->access, // access control + initializer); // initializer return create(var->source, var); } diff --git a/src/tint/reader/wgsl/parser_impl.h b/src/tint/reader/wgsl/parser_impl.h index 8707e209f4..933611bf78 100644 --- a/src/tint/reader/wgsl/parser_impl.h +++ b/src/tint/reader/wgsl/parser_impl.h @@ -474,7 +474,7 @@ class ParserImpl { Expect expect_struct_body_decl(); /// Parses a `struct_member` grammar element, erroring on parse failure. /// @returns the struct member or nullptr - Expect expect_struct_member(); + Expect expect_struct_member(); /// Parses a `function_decl` grammar element with the initial /// `function_attribute_decl*` provided as `attrs`. /// @param attrs the list of attributes for the function declaration. If attributes are consumed @@ -519,7 +519,7 @@ class ParserImpl { Expect expect_param_list(); /// Parses a `param` grammar element, erroring on parse failure. /// @returns the parsed variable - Expect expect_param(); + Expect expect_param(); /// Parses a `pipeline_stage` grammar element, erroring if the next token does /// not match a stage name. /// @returns the pipeline stage.