Split Program into Program and ProgramBuilder

Program is now immutable*, and remains part of the public Tint
interface.

ProgramBuilder is the mutable builder for Programs, and is not part of
the public Tint interface. ast::Builder has been folded into
ProgramBuilder.

Immutable Programs can be cloned into a mutable ProgramBuilder with
Program::CloneAsBuilder().

Mutable ProgramBuilders can be moved into immutable Programs.

* - mostly immutable. It still has a move constructor and move
  assignment operator - required for practical usage - and the
  semantic information on AST nodes is still mutable.

Bug: tint:390
Change-Id: Ia856c50b1880c2f95c91467a9eef5024cbc380c6
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/38240
Reviewed-by: dan sinclair <dsinclair@chromium.org>
diff --git a/src/transform/first_index_offset.cc b/src/transform/first_index_offset.cc
index d3137f4..d9790b0 100644
--- a/src/transform/first_index_offset.cc
+++ b/src/transform/first_index_offset.cc
@@ -47,6 +47,8 @@
 #include "src/ast/variable_decl_statement.h"
 #include "src/ast/variable_decoration.h"
 #include "src/clone_context.h"
+#include "src/program.h"
+#include "src/program_builder.h"
 #include "src/type/struct_type.h"
 #include "src/type/u32_type.h"
 #include "src/type_determiner.h"
@@ -99,15 +101,20 @@
   // Running TypeDeterminer as we require local_referenced_builtin_variables()
   // to be populated. TODO(bclayton) - it should not be necessary to re-run the
   // type determiner if semantic information is already generated. Remove.
-  // TODO(https://crbug.com/tint/390): Remove this const_cast hack!
-  TypeDeterminer td(const_cast<Program*>(in));
-  if (!td.Determine()) {
-    diag::Diagnostic err;
-    err.severity = diag::Severity::Error;
-    err.message = td.error();
-    Output out;
-    out.diagnostics.add(std::move(err));
-    return out;
+  Program program;
+  {
+    ProgramBuilder builder = in->CloneAsBuilder();
+    TypeDeterminer td(&builder);
+    if (!td.Determine()) {
+      diag::Diagnostic err;
+      err.severity = diag::Severity::Error;
+      err.message = td.error();
+      Output out;
+      out.diagnostics.add(std::move(err));
+      return out;
+    }
+    program = Program(std::move(builder));
+    in = &program;
   }
 
   Symbol vertex_index_sym;
@@ -116,9 +123,9 @@
   // Lazilly construct the UniformBuffer on first call to
   // maybe_create_buffer_var()
   ast::Variable* buffer_var = nullptr;
-  auto maybe_create_buffer_var = [&](Program* program) {
+  auto maybe_create_buffer_var = [&](ProgramBuilder* dst) {
     if (buffer_var == nullptr) {
-      buffer_var = AddUniformBuffer(program);
+      buffer_var = AddUniformBuffer(dst);
     }
   };
 
@@ -126,8 +133,8 @@
   // add a CreateFirstIndexOffset() statement to each function that uses one of
   // these builtins.
 
-  Output out;
-  CloneContext(&out.program, in)
+  ProgramBuilder out;
+  CloneContext(&out, in)
       .ReplaceAll([&](CloneContext* ctx, ast::Variable* var) -> ast::Variable* {
         for (ast::VariableDecoration* dec : var->decorations()) {
           if (auto* blt_dec = dec->As<ast::BuiltinDecoration>()) {
@@ -174,7 +181,7 @@
           })
       .Clone();
 
-  return out;
+  return Output(Program(std::move(out)));
 }
 
 bool FirstIndexOffset::HasVertexIndex() {
@@ -195,7 +202,7 @@
   return instance_index_offset_;
 }
 
-ast::Variable* FirstIndexOffset::AddUniformBuffer(Program* dst) {
+ast::Variable* FirstIndexOffset::AddUniformBuffer(ProgramBuilder* dst) {
   auto* u32_type = dst->create<type::U32>();
   ast::StructMemberList members;
   uint32_t offset = 0;
@@ -251,7 +258,7 @@
     const std::string& original_name,
     const std::string& field_name,
     ast::Variable* buffer_var,
-    Program* dst) {
+    ProgramBuilder* dst) {
   auto* buffer =
       dst->create<ast::IdentifierExpression>(Source{}, buffer_var->symbol());