From a8cd18e9e709bda4e34c36b07dbf928d95e36b1f Mon Sep 17 00:00:00 2001 From: David Neto Date: Fri, 27 Mar 2020 00:47:16 +0000 Subject: [PATCH] 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 --- samples/main.cc | 7 +-- src/CMakeLists.txt | 2 + src/context.cc | 31 ++++++++++ src/context.h | 16 ++++- src/reader/reader.cc | 2 +- src/reader/reader.h | 6 +- src/reader/spirv/parser.cc | 2 +- src/reader/spirv/parser.h | 4 +- src/reader/spirv/parser_impl.cc | 22 +++---- src/reader/spirv/parser_impl.h | 4 +- src/reader/spirv/parser_impl_test_helper.h | 6 +- src/reader/spirv/parser_test.cc | 2 +- src/reader/wgsl/parser.cc | 2 +- src/reader/wgsl/parser.h | 4 +- src/reader/wgsl/parser_impl.cc | 31 ++++------ src/reader/wgsl/parser_impl.h | 6 +- .../parser_impl_builtin_decoration_test.cc | 6 +- .../parser_impl_derivative_modifier_test.cc | 6 +- .../wgsl/parser_impl_pipeline_stage_test.cc | 6 +- .../wgsl/parser_impl_storage_class_test.cc | 6 +- .../parser_impl_struct_decoration_test.cc | 6 +- src/reader/wgsl/parser_impl_test_helper.h | 8 +-- src/reader/wgsl/parser_impl_type_decl_test.cc | 60 +++++++------------ ...r_impl_variable_storage_decoration_test.cc | 6 +- src/reader/wgsl/parser_test.cc | 10 +--- src/type_manager.cc | 4 ++ src/type_manager.h | 10 ++++ src/type_manager_test.cc | 13 ++++ 28 files changed, 152 insertions(+), 136 deletions(-) create mode 100644 src/context.cc diff --git a/samples/main.cc b/samples/main.cc index 5866646ecc..2937852dec 100644 --- a/samples/main.cc +++ b/samples/main.cc @@ -252,10 +252,7 @@ int main(int argc, const char** argv) { return 1; } - tint::TypeManager type_manager; - tint::Context ctx; - ctx.type_mgr = &type_manager; std::unique_ptr reader; #if TINT_BUILD_WGSL_READER @@ -267,7 +264,7 @@ int main(int argc, const char** argv) { return 1; } reader = std::make_unique( - ctx, std::string(data.begin(), data.end())); + &ctx, std::string(data.begin(), data.end())); } #endif // TINT_BUILD_WGSL_READER @@ -279,7 +276,7 @@ int main(int argc, const char** argv) { if (!ReadFile(options.input_filename, &data)) { return 1; } - reader = std::make_unique(ctx, data); + reader = std::make_unique(&ctx, data); } #endif // TINT_BUILD_SPV_READER diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index dc37b6eb5e..8f4523696f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -181,6 +181,8 @@ set(TINT_LIB_SRCS ast/variable_decoration.h ast/variable_statement.cc ast/variable_statement.h + context.h + context.cc reader/reader.cc reader/reader.h source.h diff --git a/src/context.cc b/src/context.cc new file mode 100644 index 0000000000..4285eb4307 --- /dev/null +++ b/src/context.cc @@ -0,0 +1,31 @@ +// Copyright 2020 The Tint Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "src/context.h" + +#include + +#include "src/type_manager.h" + +namespace tint { + +Context::Context() = default; + +Context::~Context() = default; + +void Context::Reset() { + type_mgr_.Reset(); +} + +} // namespace tint diff --git a/src/context.h b/src/context.h index d6d094f8e8..f0495f2599 100644 --- a/src/context.h +++ b/src/context.h @@ -21,9 +21,19 @@ namespace tint { /// Context object for Tint. Holds various global resources used through /// the system. -struct Context { - /// Manager to hold all of the various type objects - TypeManager* type_mgr = nullptr; +class Context { + public: + /// Constructs a context with an empty type manager. + Context(); + /// Destructor + ~Context(); + /// Resets the state of this context. + void Reset(); + + TypeManager& type_mgr() { return type_mgr_; } + + private: + TypeManager type_mgr_; }; } // namespace tint diff --git a/src/reader/reader.cc b/src/reader/reader.cc index 064482eceb..c94f8999bb 100644 --- a/src/reader/reader.cc +++ b/src/reader/reader.cc @@ -17,7 +17,7 @@ namespace tint { namespace reader { -Reader::Reader(const Context& ctx) : ctx_(ctx) {} +Reader::Reader(Context* ctx) : ctx_(*ctx) {} Reader::~Reader() = default; diff --git a/src/reader/reader.h b/src/reader/reader.h index a3dbbf5dda..6b1fd01f94 100644 --- a/src/reader/reader.h +++ b/src/reader/reader.h @@ -42,15 +42,15 @@ class Reader { protected: /// Constructor - /// @param ctx the context object - explicit Reader(const Context& ctx); + /// @param ctx the context object, must be non-null + explicit Reader(Context* ctx); /// Sets the error string /// @param msg the error message void set_error(const std::string& msg) { error_ = msg; } /// The Tint context object - const Context& ctx_; + Context& ctx_; /// An error message, if an error was encountered std::string error_; diff --git a/src/reader/spirv/parser.cc b/src/reader/spirv/parser.cc index 8b3bd44c1d..c283caeaf3 100644 --- a/src/reader/spirv/parser.cc +++ b/src/reader/spirv/parser.cc @@ -20,7 +20,7 @@ namespace tint { namespace reader { namespace spirv { -Parser::Parser(const Context& ctx, const std::vector& spv_binary) +Parser::Parser(Context* ctx, const std::vector& spv_binary) : Reader(ctx), impl_(std::make_unique(ctx, spv_binary)) {} Parser::~Parser() = default; diff --git a/src/reader/spirv/parser.h b/src/reader/spirv/parser.h index d5c41c26b9..d4d0974b15 100644 --- a/src/reader/spirv/parser.h +++ b/src/reader/spirv/parser.h @@ -31,9 +31,9 @@ class ParserImpl; 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& input); + Parser(Context* ctx, const std::vector& input); /// Destructor ~Parser() override; diff --git a/src/reader/spirv/parser_impl.cc b/src/reader/spirv/parser_impl.cc index 97ce5f2fc9..1f6b6d49a9 100644 --- a/src/reader/spirv/parser_impl.cc +++ b/src/reader/spirv/parser_impl.cc @@ -44,8 +44,7 @@ const spv_target_env kTargetEnv = SPV_ENV_WEBGPU_0; } // namespace -ParserImpl::ParserImpl(const Context& ctx, - const std::vector& spv_binary) +ParserImpl::ParserImpl(Context* ctx, const std::vector& spv_binary) : Reader(ctx), spv_binary_(spv_binary), fail_stream_(&success_, &errors_), @@ -76,11 +75,6 @@ ParserImpl::ParserImpl(const Context& ctx, 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 @@ ast::type::Type* ParserImpl::ConvertType(uint32_t type_id) { switch (spirv_type->kind()) { case spvtools::opt::analysis::Type::kVoid: - result = ctx_.type_mgr->Get(std::make_unique()); + result = ctx_.type_mgr().Get(std::make_unique()); break; case spvtools::opt::analysis::Type::kBool: - result = ctx_.type_mgr->Get(std::make_unique()); + result = ctx_.type_mgr().Get(std::make_unique()); 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()); + result = ctx_.type_mgr().Get(std::make_unique()); } else { - result = ctx_.type_mgr->Get(std::make_unique()); + result = ctx_.type_mgr().Get(std::make_unique()); } } else { Fail() << "unhandled integer width: " << int_ty->width(); @@ -155,7 +149,7 @@ ast::type::Type* ParserImpl::ConvertType(uint32_t type_id) { 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()); + result = ctx_.type_mgr().Get(std::make_unique()); } else { Fail() << "unhandled float width: " << float_ty->width(); } @@ -166,7 +160,7 @@ ast::type::Type* ParserImpl::ConvertType(uint32_t type_id) { 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_elem_ty, num_elem)); } // In the error case, we'll already have emitted a diagnostic. @@ -180,7 +174,7 @@ ast::type::Type* ParserImpl::ConvertType(uint32_t type_id) { 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( + result = ctx_.type_mgr().Get(std::make_unique( 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 d7487c55ad..a422cb6f7a 100644 --- a/src/reader/spirv/parser_impl.h +++ b/src/reader/spirv/parser_impl.h @@ -45,9 +45,9 @@ namespace spirv { 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& input); + ParserImpl(Context* ctx, const std::vector& 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 0838b008a1..97ac096521 100644 --- a/src/reader/spirv/parser_impl_test_helper.h +++ b/src/reader/spirv/parser_impl_test_helper.h @@ -34,26 +34,24 @@ class SpvParserTest : public testing::Test { ~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& input) { - impl_ = std::make_unique(ctx_, input); + impl_ = std::make_unique(&ctx_, input); return impl_.get(); } private: std::unique_ptr impl_; Context ctx_; - TypeManager tm_; }; } // namespace spirv diff --git a/src/reader/spirv/parser_test.cc b/src/reader/spirv/parser_test.cc index a89390546b..bef23dfa9f 100644 --- a/src/reader/spirv/parser_test.cc +++ b/src/reader/spirv/parser_test.cc @@ -30,7 +30,7 @@ using ParserTest = testing::Test; TEST_F(ParserTest, Uint32VecEmpty) { std::vector data; Context ctx; - Parser p(ctx, data); + Parser p(&ctx, data); EXPECT_FALSE(p.Parse()); // TODO(dneto): What message? } diff --git a/src/reader/wgsl/parser.cc b/src/reader/wgsl/parser.cc index 597d5781bf..2b2adc7156 100644 --- a/src/reader/wgsl/parser.cc +++ b/src/reader/wgsl/parser.cc @@ -20,7 +20,7 @@ namespace tint { namespace reader { namespace wgsl { -Parser::Parser(const Context& ctx, const std::string& input) +Parser::Parser(Context* ctx, const std::string& input) : Reader(ctx), impl_(std::make_unique(ctx, input)) {} Parser::~Parser() = default; diff --git a/src/reader/wgsl/parser.h b/src/reader/wgsl/parser.h index aa511e8b32..6fe3390647 100644 --- a/src/reader/wgsl/parser.h +++ b/src/reader/wgsl/parser.h @@ -30,9 +30,9 @@ class ParserImpl; 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 string to parse - Parser(const Context& ctx, const std::string& input); + Parser(Context* ctx, const std::string& input); ~Parser() override; /// Run the parser diff --git a/src/reader/wgsl/parser_impl.cc b/src/reader/wgsl/parser_impl.cc index e2db44d8ac..f2131f5977 100644 --- a/src/reader/wgsl/parser_impl.cc +++ b/src/reader/wgsl/parser_impl.cc @@ -71,8 +71,8 @@ namespace tint { namespace reader { namespace wgsl { -ParserImpl::ParserImpl(const Context& ctx, const std::string& input) - : ctx_(ctx), lexer_(std::make_unique(input)) {} +ParserImpl::ParserImpl(Context* ctx, const std::string& input) + : ctx_(*ctx), lexer_(std::make_unique(input)) {} ParserImpl::~ParserImpl() = default; @@ -134,11 +134,6 @@ ast::type::Type* ParserImpl::get_alias(const std::string& name) { } bool ParserImpl::Parse() { - if (ctx_.type_mgr == nullptr) { - set_error(peek(), "missing type manager"); - return false; - } - translation_unit(); return !has_error(); } @@ -690,7 +685,7 @@ ast::type::AliasType* ParserImpl::type_alias() { } str->set_name(name); - type = ctx_.type_mgr->Get(std::move(str)); + type = ctx_.type_mgr().Get(std::move(str)); } if (type == nullptr) { set_error(peek(), "invalid type for alias"); @@ -698,7 +693,7 @@ ast::type::AliasType* ParserImpl::type_alias() { } auto alias = - ctx_.type_mgr->Get(std::make_unique(name, type)); + ctx_.type_mgr().Get(std::make_unique(name, type)); register_alias(name, alias); return alias->AsAlias(); @@ -738,19 +733,19 @@ ast::type::Type* ParserImpl::type_decl() { } if (t.IsBool()) { next(); // Consume the peek - return ctx_.type_mgr->Get(std::make_unique()); + return ctx_.type_mgr().Get(std::make_unique()); } if (t.IsF32()) { next(); // Consume the peek - return ctx_.type_mgr->Get(std::make_unique()); + return ctx_.type_mgr().Get(std::make_unique()); } if (t.IsI32()) { next(); // Consume the peek - return ctx_.type_mgr->Get(std::make_unique()); + return ctx_.type_mgr().Get(std::make_unique()); } if (t.IsU32()) { next(); // Consume the peek - return ctx_.type_mgr->Get(std::make_unique()); + return ctx_.type_mgr().Get(std::make_unique()); } if (t.IsVec2() || t.IsVec3() || t.IsVec4()) { return type_decl_vector(t); @@ -806,7 +801,7 @@ ast::type::Type* ParserImpl::type_decl_pointer(Token t) { return nullptr; } - return ctx_.type_mgr->Get( + return ctx_.type_mgr().Get( std::make_unique(subtype, sc)); } @@ -839,7 +834,7 @@ ast::type::Type* ParserImpl::type_decl_vector(Token t) { return nullptr; } - return ctx_.type_mgr->Get( + return ctx_.type_mgr().Get( std::make_unique(subtype, count)); } @@ -880,7 +875,7 @@ ast::type::Type* ParserImpl::type_decl_array(Token t) { return nullptr; } - return ctx_.type_mgr->Get( + return ctx_.type_mgr().Get( std::make_unique(subtype, size)); } @@ -920,7 +915,7 @@ ast::type::Type* ParserImpl::type_decl_matrix(Token t) { return nullptr; } - return ctx_.type_mgr->Get( + return ctx_.type_mgr().Get( std::make_unique(subtype, rows, columns)); } @@ -1214,7 +1209,7 @@ ast::type::Type* ParserImpl::function_type_decl() { auto t = peek(); if (t.IsVoid()) { next(); // Consume the peek - return ctx_.type_mgr->Get(std::make_unique()); + return ctx_.type_mgr().Get(std::make_unique()); } return type_decl(); } diff --git a/src/reader/wgsl/parser_impl.h b/src/reader/wgsl/parser_impl.h index 5413d855ad..6c8e813ee0 100644 --- a/src/reader/wgsl/parser_impl.h +++ b/src/reader/wgsl/parser_impl.h @@ -57,9 +57,9 @@ class Lexer; class ParserImpl { public: /// Creates a new parser - /// @param ctx the context object + /// @param ctx the non-null context object /// @param input the input string to parse - ParserImpl(const Context& ctx, const std::string& input); + ParserImpl(Context* ctx, const std::string& input); ~ParserImpl(); /// Run the parser @@ -351,7 +351,7 @@ class ParserImpl { ast::type::Type* type_decl_array(Token t); ast::type::Type* type_decl_matrix(Token t); - const Context& ctx_; + Context& ctx_; std::string error_; std::unique_ptr lexer_; std::deque token_queue_; diff --git a/src/reader/wgsl/parser_impl_builtin_decoration_test.cc b/src/reader/wgsl/parser_impl_builtin_decoration_test.cc index d33b6d6a37..09755b6737 100644 --- a/src/reader/wgsl/parser_impl_builtin_decoration_test.cc +++ b/src/reader/wgsl/parser_impl_builtin_decoration_test.cc @@ -36,22 +36,20 @@ class BuiltinTest : public testing::TestWithParam { BuiltinTest() = default; ~BuiltinTest() = default; - void SetUp() { ctx_.type_mgr = &tm_; } + void SetUp() { ctx_.Reset(); } void TearDown() { impl_ = nullptr; - ctx_.type_mgr = nullptr; } ParserImpl* parser(const std::string& str) { - impl_ = std::make_unique(ctx_, str); + impl_ = std::make_unique(&ctx_, str); return impl_.get(); } private: std::unique_ptr impl_; Context ctx_; - TypeManager tm_; }; TEST_P(BuiltinTest, Parses) { diff --git a/src/reader/wgsl/parser_impl_derivative_modifier_test.cc b/src/reader/wgsl/parser_impl_derivative_modifier_test.cc index 6c84dd59f1..b73469a532 100644 --- a/src/reader/wgsl/parser_impl_derivative_modifier_test.cc +++ b/src/reader/wgsl/parser_impl_derivative_modifier_test.cc @@ -38,22 +38,20 @@ class DerivativeModifierTest DerivativeModifierTest() = default; ~DerivativeModifierTest() = default; - void SetUp() { ctx_.type_mgr = &tm_; } + void SetUp() { ctx_.Reset(); } void TearDown() { impl_ = nullptr; - ctx_.type_mgr = nullptr; } ParserImpl* parser(const std::string& str) { - impl_ = std::make_unique(ctx_, str); + impl_ = std::make_unique(&ctx_, str); return impl_.get(); } private: std::unique_ptr impl_; Context ctx_; - TypeManager tm_; }; TEST_P(DerivativeModifierTest, Parses) { diff --git a/src/reader/wgsl/parser_impl_pipeline_stage_test.cc b/src/reader/wgsl/parser_impl_pipeline_stage_test.cc index d36cb0ad84..f280575868 100644 --- a/src/reader/wgsl/parser_impl_pipeline_stage_test.cc +++ b/src/reader/wgsl/parser_impl_pipeline_stage_test.cc @@ -36,22 +36,20 @@ class PipelineStageTest : public testing::TestWithParam { PipelineStageTest() = default; ~PipelineStageTest() = default; - void SetUp() { ctx_.type_mgr = &tm_; } + void SetUp() { ctx_.Reset(); } void TearDown() { impl_ = nullptr; - ctx_.type_mgr = nullptr; } ParserImpl* parser(const std::string& str) { - impl_ = std::make_unique(ctx_, str); + impl_ = std::make_unique(&ctx_, str); return impl_.get(); } private: std::unique_ptr impl_; Context ctx_; - TypeManager tm_; }; TEST_P(PipelineStageTest, Parses) { diff --git a/src/reader/wgsl/parser_impl_storage_class_test.cc b/src/reader/wgsl/parser_impl_storage_class_test.cc index 986b8ab860..7c27682120 100644 --- a/src/reader/wgsl/parser_impl_storage_class_test.cc +++ b/src/reader/wgsl/parser_impl_storage_class_test.cc @@ -36,22 +36,20 @@ class StorageClassTest : public testing::TestWithParam { StorageClassTest() = default; ~StorageClassTest() = default; - void SetUp() { ctx_.type_mgr = &tm_; } + void SetUp() { ctx_.Reset(); } void TearDown() { impl_ = nullptr; - ctx_.type_mgr = nullptr; } ParserImpl* parser(const std::string& str) { - impl_ = std::make_unique(ctx_, str); + impl_ = std::make_unique(&ctx_, str); return impl_.get(); } private: std::unique_ptr impl_; Context ctx_; - TypeManager tm_; }; TEST_P(StorageClassTest, Parses) { diff --git a/src/reader/wgsl/parser_impl_struct_decoration_test.cc b/src/reader/wgsl/parser_impl_struct_decoration_test.cc index ae2ff1c40c..d32e2ab84c 100644 --- a/src/reader/wgsl/parser_impl_struct_decoration_test.cc +++ b/src/reader/wgsl/parser_impl_struct_decoration_test.cc @@ -37,22 +37,20 @@ class StructDecorationTest StructDecorationTest() = default; ~StructDecorationTest() = default; - void SetUp() { ctx_.type_mgr = &tm_; } + void SetUp() { ctx_.Reset(); } void TearDown() { impl_ = nullptr; - ctx_.type_mgr = nullptr; } ParserImpl* parser(const std::string& str) { - impl_ = std::make_unique(ctx_, str); + impl_ = std::make_unique(&ctx_, str); return impl_.get(); } private: std::unique_ptr impl_; Context ctx_; - TypeManager tm_; }; TEST_P(StructDecorationTest, Parses) { diff --git a/src/reader/wgsl/parser_impl_test_helper.h b/src/reader/wgsl/parser_impl_test_helper.h index 63f50cbbab..a58b32f9a1 100644 --- a/src/reader/wgsl/parser_impl_test_helper.h +++ b/src/reader/wgsl/parser_impl_test_helper.h @@ -34,29 +34,27 @@ class ParserImplTest : public testing::Test { ~ParserImplTest() = 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 str the string to parse /// @returns the parser implementation ParserImpl* parser(const std::string& str) { - impl_ = std::make_unique(ctx_, str); + impl_ = std::make_unique(&ctx_, str); return impl_.get(); } /// @returns the type manager - TypeManager* tm() { return &tm_; } + TypeManager* tm() { return &(ctx_.type_mgr()); } private: std::unique_ptr impl_; Context ctx_; - TypeManager tm_; }; } // namespace wgsl diff --git a/src/reader/wgsl/parser_impl_type_decl_test.cc b/src/reader/wgsl/parser_impl_type_decl_test.cc index 13a465714b..df8b65f4e3 100644 --- a/src/reader/wgsl/parser_impl_type_decl_test.cc +++ b/src/reader/wgsl/parser_impl_type_decl_test.cc @@ -125,22 +125,20 @@ class VecTest : public testing::TestWithParam { VecTest() = default; ~VecTest() = default; - void SetUp() { ctx_.type_mgr = &tm_; } + void SetUp() { ctx_.Reset(); } void TearDown() { impl_ = nullptr; - ctx_.type_mgr = nullptr; } ParserImpl* parser(const std::string& str) { - impl_ = std::make_unique(ctx_, str); + impl_ = std::make_unique(&ctx_, str); return impl_.get(); } private: std::unique_ptr impl_; Context ctx_; - TypeManager tm_; }; TEST_P(VecTest, Parse) { @@ -163,22 +161,20 @@ class VecMissingGreaterThanTest : public testing::TestWithParam { VecMissingGreaterThanTest() = default; ~VecMissingGreaterThanTest() = default; - void SetUp() { ctx_.type_mgr = &tm_; } + void SetUp() { ctx_.Reset(); } void TearDown() { impl_ = nullptr; - ctx_.type_mgr = nullptr; } ParserImpl* parser(const std::string& str) { - impl_ = std::make_unique(ctx_, str); + impl_ = std::make_unique(&ctx_, str); return impl_.get(); } private: std::unique_ptr impl_; Context ctx_; - TypeManager tm_; }; TEST_P(VecMissingGreaterThanTest, Handles_Missing_GreaterThan) { @@ -200,22 +196,20 @@ class VecMissingLessThanTest : public testing::TestWithParam { VecMissingLessThanTest() = default; ~VecMissingLessThanTest() = default; - void SetUp() { ctx_.type_mgr = &tm_; } + void SetUp() { ctx_.Reset(); } void TearDown() { impl_ = nullptr; - ctx_.type_mgr = nullptr; } ParserImpl* parser(const std::string& str) { - impl_ = std::make_unique(ctx_, str); + impl_ = std::make_unique(&ctx_, str); return impl_.get(); } private: std::unique_ptr impl_; Context ctx_; - TypeManager tm_; }; TEST_P(VecMissingLessThanTest, Handles_Missing_GreaterThan) { @@ -237,22 +231,20 @@ class VecBadType : public testing::TestWithParam { VecBadType() = default; ~VecBadType() = default; - void SetUp() { ctx_.type_mgr = &tm_; } + void SetUp() { ctx_.Reset(); } void TearDown() { impl_ = nullptr; - ctx_.type_mgr = nullptr; } ParserImpl* parser(const std::string& str) { - impl_ = std::make_unique(ctx_, str); + impl_ = std::make_unique(&ctx_, str); return impl_.get(); } private: std::unique_ptr impl_; Context ctx_; - TypeManager tm_; }; TEST_P(VecBadType, Handles_Unknown_Type) { @@ -274,22 +266,20 @@ class VecMissingType : public testing::TestWithParam { VecMissingType() = default; ~VecMissingType() = default; - void SetUp() { ctx_.type_mgr = &tm_; } + void SetUp() { ctx_.Reset(); } void TearDown() { impl_ = nullptr; - ctx_.type_mgr = nullptr; } ParserImpl* parser(const std::string& str) { - impl_ = std::make_unique(ctx_, str); + impl_ = std::make_unique(&ctx_, str); return impl_.get(); } private: std::unique_ptr impl_; Context ctx_; - TypeManager tm_; }; TEST_P(VecMissingType, Handles_Missing_Type) { @@ -493,22 +483,20 @@ class MatrixTest : public testing::TestWithParam { MatrixTest() = default; ~MatrixTest() = default; - void SetUp() { ctx_.type_mgr = &tm_; } + void SetUp() { ctx_.Reset(); } void TearDown() { impl_ = nullptr; - ctx_.type_mgr = nullptr; } ParserImpl* parser(const std::string& str) { - impl_ = std::make_unique(ctx_, str); + impl_ = std::make_unique(&ctx_, str); return impl_.get(); } private: std::unique_ptr impl_; Context ctx_; - TypeManager tm_; }; TEST_P(MatrixTest, Parse) { @@ -539,22 +527,20 @@ class MatrixMissingGreaterThanTest : public testing::TestWithParam { MatrixMissingGreaterThanTest() = default; ~MatrixMissingGreaterThanTest() = default; - void SetUp() { ctx_.type_mgr = &tm_; } + void SetUp() { ctx_.Reset(); } void TearDown() { impl_ = nullptr; - ctx_.type_mgr = nullptr; } ParserImpl* parser(const std::string& str) { - impl_ = std::make_unique(ctx_, str); + impl_ = std::make_unique(&ctx_, str); return impl_.get(); } private: std::unique_ptr impl_; Context ctx_; - TypeManager tm_; }; TEST_P(MatrixMissingGreaterThanTest, Handles_Missing_GreaterThan) { auto params = GetParam(); @@ -581,22 +567,20 @@ class MatrixMissingLessThanTest : public testing::TestWithParam { MatrixMissingLessThanTest() = default; ~MatrixMissingLessThanTest() = default; - void SetUp() { ctx_.type_mgr = &tm_; } + void SetUp() { ctx_.Reset(); } void TearDown() { impl_ = nullptr; - ctx_.type_mgr = nullptr; } ParserImpl* parser(const std::string& str) { - impl_ = std::make_unique(ctx_, str); + impl_ = std::make_unique(&ctx_, str); return impl_.get(); } private: std::unique_ptr impl_; Context ctx_; - TypeManager tm_; }; TEST_P(MatrixMissingLessThanTest, Handles_Missing_GreaterThan) { auto params = GetParam(); @@ -623,22 +607,20 @@ class MatrixBadType : public testing::TestWithParam { MatrixBadType() = default; ~MatrixBadType() = default; - void SetUp() { ctx_.type_mgr = &tm_; } + void SetUp() { ctx_.Reset(); } void TearDown() { impl_ = nullptr; - ctx_.type_mgr = nullptr; } ParserImpl* parser(const std::string& str) { - impl_ = std::make_unique(ctx_, str); + impl_ = std::make_unique(&ctx_, str); return impl_.get(); } private: std::unique_ptr impl_; Context ctx_; - TypeManager tm_; }; TEST_P(MatrixBadType, Handles_Unknown_Type) { auto params = GetParam(); @@ -665,22 +647,20 @@ class MatrixMissingType : public testing::TestWithParam { MatrixMissingType() = default; ~MatrixMissingType() = default; - void SetUp() { ctx_.type_mgr = &tm_; } + void SetUp() { ctx_.Reset(); } void TearDown() { impl_ = nullptr; - ctx_.type_mgr = nullptr; } ParserImpl* parser(const std::string& str) { - impl_ = std::make_unique(ctx_, str); + impl_ = std::make_unique(&ctx_, str); return impl_.get(); } private: std::unique_ptr impl_; Context ctx_; - TypeManager tm_; }; TEST_P(MatrixMissingType, Handles_Missing_Type) { auto params = GetParam(); diff --git a/src/reader/wgsl/parser_impl_variable_storage_decoration_test.cc b/src/reader/wgsl/parser_impl_variable_storage_decoration_test.cc index e82a44d7a5..9bf38aa80a 100644 --- a/src/reader/wgsl/parser_impl_variable_storage_decoration_test.cc +++ b/src/reader/wgsl/parser_impl_variable_storage_decoration_test.cc @@ -36,22 +36,20 @@ class VariableStorageTest : public testing::TestWithParam { VariableStorageTest() = default; ~VariableStorageTest() = default; - void SetUp() { ctx_.type_mgr = &tm_; } + void SetUp() { ctx_.Reset(); } void TearDown() { impl_ = nullptr; - ctx_.type_mgr = nullptr; } ParserImpl* parser(const std::string& str) { - impl_ = std::make_unique(ctx_, str); + impl_ = std::make_unique(&ctx_, str); return impl_.get(); } private: std::unique_ptr impl_; Context ctx_; - TypeManager tm_; }; TEST_P(VariableStorageTest, Parses) { diff --git a/src/reader/wgsl/parser_test.cc b/src/reader/wgsl/parser_test.cc index 1d937209ec..fa06290819 100644 --- a/src/reader/wgsl/parser_test.cc +++ b/src/reader/wgsl/parser_test.cc @@ -25,19 +25,15 @@ namespace { using ParserTest = testing::Test; TEST_F(ParserTest, Empty) { - TypeManager tm; Context ctx; - ctx.type_mgr = &tm; - Parser p(ctx, ""); + Parser p(&ctx, ""); ASSERT_TRUE(p.Parse()) << p.error(); } TEST_F(ParserTest, DISABLED_Parses) { - TypeManager tm; Context ctx; - ctx.type_mgr = &tm; - Parser p(ctx, R"( + Parser p(&ctx, R"( import "GLSL.std.430" as glsl; [[location 0]] var gl_FragColor : vec4; @@ -56,7 +52,7 @@ fn main() -> void { TEST_F(ParserTest, DISABLED_HandlesError) { Context ctx; - Parser p(ctx, R"( + Parser p(&ctx, R"( import "GLSL.std.430" as glsl; fn main() -> { # missing return type diff --git a/src/type_manager.cc b/src/type_manager.cc index e52c3ff3b6..88569ce26f 100644 --- a/src/type_manager.cc +++ b/src/type_manager.cc @@ -22,6 +22,10 @@ TypeManager::TypeManager() = default; TypeManager::~TypeManager() = default; +void TypeManager::Reset() { + types_.clear(); +} + ast::type::Type* TypeManager::Get(std::unique_ptr type) { auto name = type->type_name(); diff --git a/src/type_manager.h b/src/type_manager.h index 61ca864f8f..eb26a25d9d 100644 --- a/src/type_manager.h +++ b/src/type_manager.h @@ -29,11 +29,21 @@ class TypeManager { TypeManager(); ~TypeManager(); + /// Clears all registered types. + void Reset(); + /// Get the given type from the type manager /// @param type The type to register /// @return the pointer to the registered type ast::type::Type* Get(std::unique_ptr type); + /// Returns the type map, for testing purposes. + /// @returns the mapping from name string to type. + const std::unordered_map>& + TypesForTesting() { + return types_; + } + private: std::unordered_map> types_; }; diff --git a/src/type_manager_test.cc b/src/type_manager_test.cc index 004450fd8e..a0526d1220 100644 --- a/src/type_manager_test.cc +++ b/src/type_manager_test.cc @@ -52,5 +52,18 @@ TEST_F(TypeManagerTest, GetDifferentTypeReturnsDifferentPtr) { EXPECT_TRUE(t2->IsU32()); } +TEST_F(TypeManagerTest, ResetClearsPreviousData) { + TypeManager tm; + auto t = tm.Get(std::make_unique()); + ASSERT_NE(t, nullptr); + + EXPECT_FALSE(tm.TypesForTesting().empty()); + tm.Reset(); + EXPECT_TRUE(tm.TypesForTesting().empty()); + + auto t2 = tm.Get(std::make_unique()); + ASSERT_NE(t2, nullptr); +} + } // namespace } // namespace tint