Context object owns a TypeManager

Add a Context::Reset method to clear state.

Hide the member behind an accessor.

Change-Id: Iafb7c39249f66f70c5a99a8ed1c69f2c0238834f
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/17742
Reviewed-by: dan sinclair <dsinclair@google.com>
diff --git a/src/reader/spirv/parser.cc b/src/reader/spirv/parser.cc
index 8b3bd44..c283cae 100644
--- a/src/reader/spirv/parser.cc
+++ b/src/reader/spirv/parser.cc
@@ -20,7 +20,7 @@
 namespace reader {
 namespace spirv {
 
-Parser::Parser(const Context& ctx, const std::vector<uint32_t>& spv_binary)
+Parser::Parser(Context* ctx, const std::vector<uint32_t>& spv_binary)
     : Reader(ctx), impl_(std::make_unique<ParserImpl>(ctx, spv_binary)) {}
 
 Parser::~Parser() = default;
diff --git a/src/reader/spirv/parser.h b/src/reader/spirv/parser.h
index d5c41c2..d4d0974 100644
--- a/src/reader/spirv/parser.h
+++ b/src/reader/spirv/parser.h
@@ -31,9 +31,9 @@
 class Parser : public Reader {
  public:
   /// Creates a new parser
-  /// @param ctx the context object
+  /// @param ctx the non-null context object
   /// @param input the input data to parse
-  Parser(const Context& ctx, const std::vector<uint32_t>& input);
+  Parser(Context* ctx, const std::vector<uint32_t>& input);
   /// Destructor
   ~Parser() override;
 
diff --git a/src/reader/spirv/parser_impl.cc b/src/reader/spirv/parser_impl.cc
index 97ce5f2..1f6b6d4 100644
--- a/src/reader/spirv/parser_impl.cc
+++ b/src/reader/spirv/parser_impl.cc
@@ -44,8 +44,7 @@
 
 }  // namespace
 
-ParserImpl::ParserImpl(const Context& ctx,
-                       const std::vector<uint32_t>& spv_binary)
+ParserImpl::ParserImpl(Context* ctx, const std::vector<uint32_t>& spv_binary)
     : Reader(ctx),
       spv_binary_(spv_binary),
       fail_stream_(&success_, &errors_),
@@ -76,11 +75,6 @@
 ParserImpl::~ParserImpl() = default;
 
 bool ParserImpl::Parse() {
-  if (ctx_.type_mgr == nullptr) {
-    Fail() << "Missing type manager";
-    return false;
-  }
-
   if (!success_) {
     return false;
   }
@@ -134,18 +128,18 @@
 
   switch (spirv_type->kind()) {
     case spvtools::opt::analysis::Type::kVoid:
-      result = ctx_.type_mgr->Get(std::make_unique<ast::type::VoidType>());
+      result = ctx_.type_mgr().Get(std::make_unique<ast::type::VoidType>());
       break;
     case spvtools::opt::analysis::Type::kBool:
-      result = ctx_.type_mgr->Get(std::make_unique<ast::type::BoolType>());
+      result = ctx_.type_mgr().Get(std::make_unique<ast::type::BoolType>());
       break;
     case spvtools::opt::analysis::Type::kInteger: {
       const auto* int_ty = spirv_type->AsInteger();
       if (int_ty->width() == 32) {
         if (int_ty->IsSigned()) {
-          result = ctx_.type_mgr->Get(std::make_unique<ast::type::I32Type>());
+          result = ctx_.type_mgr().Get(std::make_unique<ast::type::I32Type>());
         } else {
-          result = ctx_.type_mgr->Get(std::make_unique<ast::type::U32Type>());
+          result = ctx_.type_mgr().Get(std::make_unique<ast::type::U32Type>());
         }
       } else {
         Fail() << "unhandled integer width: " << int_ty->width();
@@ -155,7 +149,7 @@
     case spvtools::opt::analysis::Type::kFloat: {
       const auto* float_ty = spirv_type->AsFloat();
       if (float_ty->width() == 32) {
-        result = ctx_.type_mgr->Get(std::make_unique<ast::type::F32Type>());
+        result = ctx_.type_mgr().Get(std::make_unique<ast::type::F32Type>());
       } else {
         Fail() << "unhandled float width: " << float_ty->width();
       }
@@ -166,7 +160,7 @@
       const auto num_elem = vec_ty->element_count();
       auto* ast_elem_ty = ConvertType(type_mgr_->GetId(vec_ty->element_type()));
       if (ast_elem_ty != nullptr) {
-        result = ctx_.type_mgr->Get(
+        result = ctx_.type_mgr().Get(
             std::make_unique<ast::type::VectorType>(ast_elem_ty, num_elem));
       }
       // In the error case, we'll already have emitted a diagnostic.
@@ -180,7 +174,7 @@
       const auto num_columns = mat_ty->element_count();
       auto* ast_scalar_ty = ConvertType(type_mgr_->GetId(scalar_ty));
       if (ast_scalar_ty != nullptr) {
-        result = ctx_.type_mgr->Get(std::make_unique<ast::type::MatrixType>(
+        result = ctx_.type_mgr().Get(std::make_unique<ast::type::MatrixType>(
             ast_scalar_ty, num_rows, num_columns));
       }
       // In the error case, we'll already have emitted a diagnostic.
diff --git a/src/reader/spirv/parser_impl.h b/src/reader/spirv/parser_impl.h
index d7487c5..a422cb6 100644
--- a/src/reader/spirv/parser_impl.h
+++ b/src/reader/spirv/parser_impl.h
@@ -45,9 +45,9 @@
 class ParserImpl : Reader {
  public:
   /// Creates a new parser
-  /// @param ctx the context object
+  /// @param ctx the non-null context object
   /// @param input the input data to parse
-  ParserImpl(const Context& ctx, const std::vector<uint32_t>& input);
+  ParserImpl(Context* ctx, const std::vector<uint32_t>& input);
   /// Destructor
   ~ParserImpl() override;
 
diff --git a/src/reader/spirv/parser_impl_test_helper.h b/src/reader/spirv/parser_impl_test_helper.h
index 0838b00..97ac096 100644
--- a/src/reader/spirv/parser_impl_test_helper.h
+++ b/src/reader/spirv/parser_impl_test_helper.h
@@ -34,26 +34,24 @@
   ~SpvParserTest() = default;
 
   /// Sets up the test helper
-  void SetUp() { ctx_.type_mgr = &tm_; }
+  void SetUp() { ctx_.Reset(); }
 
   /// Tears down the test helper
   void TearDown() {
     impl_ = nullptr;
-    ctx_.type_mgr = nullptr;
   }
 
   /// Retrieves the parser from the helper
   /// @param input the string to parse
   /// @returns the parser implementation
   ParserImpl* parser(const std::vector<uint32_t>& input) {
-    impl_ = std::make_unique<ParserImpl>(ctx_, input);
+    impl_ = std::make_unique<ParserImpl>(&ctx_, input);
     return impl_.get();
   }
 
  private:
   std::unique_ptr<ParserImpl> impl_;
   Context ctx_;
-  TypeManager tm_;
 };
 
 }  // namespace spirv
diff --git a/src/reader/spirv/parser_test.cc b/src/reader/spirv/parser_test.cc
index a893905..bef23df 100644
--- a/src/reader/spirv/parser_test.cc
+++ b/src/reader/spirv/parser_test.cc
@@ -30,7 +30,7 @@
 TEST_F(ParserTest, Uint32VecEmpty) {
   std::vector<uint32_t> data;
   Context ctx;
-  Parser p(ctx, data);
+  Parser p(&ctx, data);
   EXPECT_FALSE(p.Parse());
   // TODO(dneto): What message?
 }