tint/reader: Use ProgramBuilder helpers

Eases the migration to unkeywording types.

Bug: tint:1810
Change-Id: If9ccdbf963550774b85b8cd15df8e37c271cde93
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/118987
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: James Price <jrprice@google.com>
diff --git a/src/tint/reader/spirv/function.cc b/src/tint/reader/spirv/function.cc
index 565b788..224888e 100644
--- a/src/tint/reader/spirv/function.cc
+++ b/src/tint/reader/spirv/function.cc
@@ -955,9 +955,8 @@
             return false;
         }
 
-        builder_.AST().AddFunction(create<ast::Function>(
-            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 @@
         }
     }
 
-    auto* body = create<ast::BlockStatement>(source, stmts, utils::Empty);
     AttributeList fn_attrs;
     fn_attrs.Push(create<ast::StageAttribute>(source, ep_info_->stage));
 
@@ -1409,9 +1407,8 @@
         }
     }
 
-    builder_.AST().AddFunction(create<ast::Function>(
-        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 @@
         }
 
         // 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<transform::SpirvAtomic::Stub>(
-            builder_.ID(), builder_.AllocateNodeID(), builtin);
-        auto* stub =
-            create<ast::Function>(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<transform::SpirvAtomic::Stub>(
+                    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 1b7cd48..a610039 100644
--- a/src/tint/reader/spirv/parser_impl.cc
+++ b/src/tint/reader/spirv/parser_impl.cc
@@ -1167,9 +1167,9 @@
             ++num_non_writable_members;
         }
         const auto member_name = namer_.GetMemberName(type_id, member_index);
-        auto* ast_struct_member = create<ast::StructMember>(
-            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 @@
             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 @@
     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 @@
     }
 
     auto sym = builder_.Symbols().Register(namer_.Name(id));
-    return create<ast::Var>(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<ast::Let>(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<ast::Override>(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<ast::Parameter>(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 55e2250..1e64866 100644
--- a/src/tint/reader/spirv/parser_impl.h
+++ b/src/tint/reader/spirv/parser_impl.h
@@ -429,18 +429,18 @@
     /// @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 @@
     /// @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 @@
     /// @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 @@
     std::unordered_map<const spvtools::opt::Instruction*, const Type*> handle_type_;
 
     /// Maps the SPIR-V ID of a module-scope variable to its AST variable.
-    utils::Hashmap<uint32_t, ast::Var*, 16> module_variable_;
+    utils::Hashmap<uint32_t, const ast::Var*, 16> 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 8d15e45..ec7cfb8 100644
--- a/src/tint/reader/wgsl/parser_impl.cc
+++ b/src/tint/reader/wgsl/parser_impl.cc
@@ -505,8 +505,6 @@
                     return Failure::kErrored;
                 }
             }
-
-            builder_.AST().AddGlobalVariable(gc.value);
             return kSuccess;
         }
 
@@ -565,7 +563,6 @@
         errored = true;
     }
     if (func.matched) {
-        builder_.AST().AddFunction(func.value);
         return kSuccess;
     }
 
@@ -631,13 +628,13 @@
 
     TINT_DEFER(attrs.Clear());
 
-    return create<ast::Var>(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 @@
 // global_const_initializer
 //  : EQUAL const_expr
 Maybe<const ast::Variable*> ParserImpl::global_constant_decl(AttributeList& attrs) {
-    bool is_const = false;
     bool is_overridable = false;
     const char* use = nullptr;
     Source source;
@@ -690,25 +686,18 @@
 
     TINT_DEFER(attrs.Clear());
 
-    if (is_const) {
-        return create<ast::Const>(decl->source,                             // source
-                                  builder_.Symbols().Register(decl->name),  // symbol
-                                  decl->type,                               // type
-                                  initializer,                              // initializer
-                                  std::move(attrs));                        // attributes
-    }
     if (is_overridable) {
-        return create<ast::Override>(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<ast::Const>(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 @@
 
 // struct_member
 //   : attribute* ident_with_type_specifier
-Expect<ast::StructMember*> ParserImpl::expect_struct_member() {
+Expect<const ast::StructMember*> ParserImpl::expect_struct_member() {
     auto attrs = attribute_list();
     if (attrs.errored) {
         return Failure::kErrored;
@@ -1462,8 +1451,7 @@
         return Failure::kErrored;
     }
 
-    return create<ast::StructMember>(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 @@
 
     TINT_DEFER(attrs.Clear());
 
-    return create<ast::Function>(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 @@
 
 // param
 //   : attribute_list* ident COLON type_specifier
-Expect<ast::Parameter*> ParserImpl::expect_param() {
+Expect<const ast::Parameter*> ParserImpl::expect_param() {
     auto attrs = attribute_list();
 
     auto decl = expect_ident_with_type_specifier("parameter");
@@ -1626,10 +1613,10 @@
         return Failure::kErrored;
     }
 
-    return create<ast::Parameter>(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 @@
             return add_error(peek(), "missing initializer for 'const' declaration");
         }
 
-        auto* const_ = create<ast::Const>(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<ast::VariableDeclStatement>(decl_source, const_);
     }
@@ -1972,11 +1958,10 @@
             return add_error(peek(), "missing initializer for 'let' declaration");
         }
 
-        auto* let = create<ast::Let>(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<ast::VariableDeclStatement>(decl_source, let);
     }
@@ -2004,13 +1989,12 @@
         initializer = initializer_expr.value;
     }
 
-    auto* var = create<ast::Var>(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<ast::VariableDeclStatement>(var->source, var);
 }
diff --git a/src/tint/reader/wgsl/parser_impl.h b/src/tint/reader/wgsl/parser_impl.h
index 8707e20..933611b 100644
--- a/src/tint/reader/wgsl/parser_impl.h
+++ b/src/tint/reader/wgsl/parser_impl.h
@@ -474,7 +474,7 @@
     Expect<StructMemberList> expect_struct_body_decl();
     /// Parses a `struct_member` grammar element, erroring on parse failure.
     /// @returns the struct member or nullptr
-    Expect<ast::StructMember*> expect_struct_member();
+    Expect<const ast::StructMember*> 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 @@
     Expect<ParameterList> expect_param_list();
     /// Parses a `param` grammar element, erroring on parse failure.
     /// @returns the parsed variable
-    Expect<ast::Parameter*> expect_param();
+    Expect<const ast::Parameter*> expect_param();
     /// Parses a `pipeline_stage` grammar element, erroring if the next token does
     /// not match a stage name.
     /// @returns the pipeline stage.