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>
This commit is contained in:
David Neto 2020-03-27 00:47:16 +00:00 committed by dan sinclair
parent 20145170fc
commit a8cd18e9e7
28 changed files with 152 additions and 136 deletions

View File

@ -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<tint::reader::Reader> reader;
#if TINT_BUILD_WGSL_READER
@ -267,7 +264,7 @@ int main(int argc, const char** argv) {
return 1;
}
reader = std::make_unique<tint::reader::wgsl::Parser>(
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<uint32_t>(options.input_filename, &data)) {
return 1;
}
reader = std::make_unique<tint::reader::spirv::Parser>(ctx, data);
reader = std::make_unique<tint::reader::spirv::Parser>(&ctx, data);
}
#endif // TINT_BUILD_SPV_READER

View File

@ -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

31
src/context.cc Normal file
View File

@ -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 <utility>
#include "src/type_manager.h"
namespace tint {
Context::Context() = default;
Context::~Context() = default;
void Context::Reset() {
type_mgr_.Reset();
}
} // namespace tint

View File

@ -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

View File

@ -17,7 +17,7 @@
namespace tint {
namespace reader {
Reader::Reader(const Context& ctx) : ctx_(ctx) {}
Reader::Reader(Context* ctx) : ctx_(*ctx) {}
Reader::~Reader() = default;

View File

@ -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_;

View File

@ -20,7 +20,7 @@ namespace tint {
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;

View File

@ -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<uint32_t>& input);
Parser(Context* ctx, const std::vector<uint32_t>& input);
/// Destructor
~Parser() override;

View File

@ -44,8 +44,7 @@ const spv_target_env kTargetEnv = SPV_ENV_WEBGPU_0;
} // 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(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<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 @@ 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<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 @@ 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::type::VectorType>(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<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.

View File

@ -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<uint32_t>& input);
ParserImpl(Context* ctx, const std::vector<uint32_t>& input);
/// Destructor
~ParserImpl() override;

View File

@ -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<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

View File

@ -30,7 +30,7 @@ using ParserTest = testing::Test;
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?
}

View File

@ -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<ParserImpl>(ctx, input)) {}
Parser::~Parser() = default;

View File

@ -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

View File

@ -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<Lexer>(input)) {}
ParserImpl::ParserImpl(Context* ctx, const std::string& input)
: ctx_(*ctx), lexer_(std::make_unique<Lexer>(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<ast::type::AliasType>(name, type));
ctx_.type_mgr().Get(std::make_unique<ast::type::AliasType>(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<ast::type::BoolType>());
return ctx_.type_mgr().Get(std::make_unique<ast::type::BoolType>());
}
if (t.IsF32()) {
next(); // Consume the peek
return ctx_.type_mgr->Get(std::make_unique<ast::type::F32Type>());
return ctx_.type_mgr().Get(std::make_unique<ast::type::F32Type>());
}
if (t.IsI32()) {
next(); // Consume the peek
return ctx_.type_mgr->Get(std::make_unique<ast::type::I32Type>());
return ctx_.type_mgr().Get(std::make_unique<ast::type::I32Type>());
}
if (t.IsU32()) {
next(); // Consume the peek
return ctx_.type_mgr->Get(std::make_unique<ast::type::U32Type>());
return ctx_.type_mgr().Get(std::make_unique<ast::type::U32Type>());
}
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<ast::type::PointerType>(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<ast::type::VectorType>(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<ast::type::ArrayType>(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<ast::type::MatrixType>(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<ast::type::VoidType>());
return ctx_.type_mgr().Get(std::make_unique<ast::type::VoidType>());
}
return type_decl();
}

View File

@ -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> lexer_;
std::deque<Token> token_queue_;

View File

@ -36,22 +36,20 @@ class BuiltinTest : public testing::TestWithParam<BuiltinData> {
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<ParserImpl>(ctx_, str);
impl_ = std::make_unique<ParserImpl>(&ctx_, str);
return impl_.get();
}
private:
std::unique_ptr<ParserImpl> impl_;
Context ctx_;
TypeManager tm_;
};
TEST_P(BuiltinTest, Parses) {

View File

@ -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<ParserImpl>(ctx_, str);
impl_ = std::make_unique<ParserImpl>(&ctx_, str);
return impl_.get();
}
private:
std::unique_ptr<ParserImpl> impl_;
Context ctx_;
TypeManager tm_;
};
TEST_P(DerivativeModifierTest, Parses) {

View File

@ -36,22 +36,20 @@ class PipelineStageTest : public testing::TestWithParam<PipelineStageData> {
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<ParserImpl>(ctx_, str);
impl_ = std::make_unique<ParserImpl>(&ctx_, str);
return impl_.get();
}
private:
std::unique_ptr<ParserImpl> impl_;
Context ctx_;
TypeManager tm_;
};
TEST_P(PipelineStageTest, Parses) {

View File

@ -36,22 +36,20 @@ class StorageClassTest : public testing::TestWithParam<StorageClassData> {
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<ParserImpl>(ctx_, str);
impl_ = std::make_unique<ParserImpl>(&ctx_, str);
return impl_.get();
}
private:
std::unique_ptr<ParserImpl> impl_;
Context ctx_;
TypeManager tm_;
};
TEST_P(StorageClassTest, Parses) {

View File

@ -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<ParserImpl>(ctx_, str);
impl_ = std::make_unique<ParserImpl>(&ctx_, str);
return impl_.get();
}
private:
std::unique_ptr<ParserImpl> impl_;
Context ctx_;
TypeManager tm_;
};
TEST_P(StructDecorationTest, Parses) {

View File

@ -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<ParserImpl>(ctx_, str);
impl_ = std::make_unique<ParserImpl>(&ctx_, str);
return impl_.get();
}
/// @returns the type manager
TypeManager* tm() { return &tm_; }
TypeManager* tm() { return &(ctx_.type_mgr()); }
private:
std::unique_ptr<ParserImpl> impl_;
Context ctx_;
TypeManager tm_;
};
} // namespace wgsl

View File

@ -125,22 +125,20 @@ class VecTest : public testing::TestWithParam<VecData> {
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<ParserImpl>(ctx_, str);
impl_ = std::make_unique<ParserImpl>(&ctx_, str);
return impl_.get();
}
private:
std::unique_ptr<ParserImpl> impl_;
Context ctx_;
TypeManager tm_;
};
TEST_P(VecTest, Parse) {
@ -163,22 +161,20 @@ class VecMissingGreaterThanTest : public testing::TestWithParam<VecData> {
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<ParserImpl>(ctx_, str);
impl_ = std::make_unique<ParserImpl>(&ctx_, str);
return impl_.get();
}
private:
std::unique_ptr<ParserImpl> impl_;
Context ctx_;
TypeManager tm_;
};
TEST_P(VecMissingGreaterThanTest, Handles_Missing_GreaterThan) {
@ -200,22 +196,20 @@ class VecMissingLessThanTest : public testing::TestWithParam<VecData> {
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<ParserImpl>(ctx_, str);
impl_ = std::make_unique<ParserImpl>(&ctx_, str);
return impl_.get();
}
private:
std::unique_ptr<ParserImpl> impl_;
Context ctx_;
TypeManager tm_;
};
TEST_P(VecMissingLessThanTest, Handles_Missing_GreaterThan) {
@ -237,22 +231,20 @@ class VecBadType : public testing::TestWithParam<VecData> {
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<ParserImpl>(ctx_, str);
impl_ = std::make_unique<ParserImpl>(&ctx_, str);
return impl_.get();
}
private:
std::unique_ptr<ParserImpl> impl_;
Context ctx_;
TypeManager tm_;
};
TEST_P(VecBadType, Handles_Unknown_Type) {
@ -274,22 +266,20 @@ class VecMissingType : public testing::TestWithParam<VecData> {
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<ParserImpl>(ctx_, str);
impl_ = std::make_unique<ParserImpl>(&ctx_, str);
return impl_.get();
}
private:
std::unique_ptr<ParserImpl> impl_;
Context ctx_;
TypeManager tm_;
};
TEST_P(VecMissingType, Handles_Missing_Type) {
@ -493,22 +483,20 @@ class MatrixTest : public testing::TestWithParam<MatrixData> {
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<ParserImpl>(ctx_, str);
impl_ = std::make_unique<ParserImpl>(&ctx_, str);
return impl_.get();
}
private:
std::unique_ptr<ParserImpl> impl_;
Context ctx_;
TypeManager tm_;
};
TEST_P(MatrixTest, Parse) {
@ -539,22 +527,20 @@ class MatrixMissingGreaterThanTest : public testing::TestWithParam<MatrixData> {
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<ParserImpl>(ctx_, str);
impl_ = std::make_unique<ParserImpl>(&ctx_, str);
return impl_.get();
}
private:
std::unique_ptr<ParserImpl> impl_;
Context ctx_;
TypeManager tm_;
};
TEST_P(MatrixMissingGreaterThanTest, Handles_Missing_GreaterThan) {
auto params = GetParam();
@ -581,22 +567,20 @@ class MatrixMissingLessThanTest : public testing::TestWithParam<MatrixData> {
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<ParserImpl>(ctx_, str);
impl_ = std::make_unique<ParserImpl>(&ctx_, str);
return impl_.get();
}
private:
std::unique_ptr<ParserImpl> impl_;
Context ctx_;
TypeManager tm_;
};
TEST_P(MatrixMissingLessThanTest, Handles_Missing_GreaterThan) {
auto params = GetParam();
@ -623,22 +607,20 @@ class MatrixBadType : public testing::TestWithParam<MatrixData> {
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<ParserImpl>(ctx_, str);
impl_ = std::make_unique<ParserImpl>(&ctx_, str);
return impl_.get();
}
private:
std::unique_ptr<ParserImpl> impl_;
Context ctx_;
TypeManager tm_;
};
TEST_P(MatrixBadType, Handles_Unknown_Type) {
auto params = GetParam();
@ -665,22 +647,20 @@ class MatrixMissingType : public testing::TestWithParam<MatrixData> {
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<ParserImpl>(ctx_, str);
impl_ = std::make_unique<ParserImpl>(&ctx_, str);
return impl_.get();
}
private:
std::unique_ptr<ParserImpl> impl_;
Context ctx_;
TypeManager tm_;
};
TEST_P(MatrixMissingType, Handles_Missing_Type) {
auto params = GetParam();

View File

@ -36,22 +36,20 @@ class VariableStorageTest : public testing::TestWithParam<VariableStorageData> {
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<ParserImpl>(ctx_, str);
impl_ = std::make_unique<ParserImpl>(&ctx_, str);
return impl_.get();
}
private:
std::unique_ptr<ParserImpl> impl_;
Context ctx_;
TypeManager tm_;
};
TEST_P(VariableStorageTest, Parses) {

View File

@ -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<out> gl_FragColor : vec4<f32>;
@ -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

View File

@ -22,6 +22,10 @@ TypeManager::TypeManager() = default;
TypeManager::~TypeManager() = default;
void TypeManager::Reset() {
types_.clear();
}
ast::type::Type* TypeManager::Get(std::unique_ptr<ast::type::Type> type) {
auto name = type->type_name();

View File

@ -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<ast::type::Type> type);
/// Returns the type map, for testing purposes.
/// @returns the mapping from name string to type.
const std::unordered_map<std::string, std::unique_ptr<ast::type::Type>>&
TypesForTesting() {
return types_;
}
private:
std::unordered_map<std::string, std::unique_ptr<ast::type::Type>> types_;
};

View File

@ -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<ast::type::I32Type>());
ASSERT_NE(t, nullptr);
EXPECT_FALSE(tm.TypesForTesting().empty());
tm.Reset();
EXPECT_TRUE(tm.TypesForTesting().empty());
auto t2 = tm.Get(std::make_unique<ast::type::I32Type>());
ASSERT_NE(t2, nullptr);
}
} // namespace
} // namespace tint