Move the ast node ownership from Context to Module

Fixes: tint:335
Change-Id: I128e229daa56d43e7227ecab72269be33b3ee012
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/33240
Commit-Queue: Ben Clayton <bclayton@google.com>
Auto-Submit: Ben Clayton <bclayton@google.com>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
Ben Clayton
2020-11-18 20:58:20 +00:00
committed by Commit Bot service account
parent 627732c408
commit 2f4096b0d7
23 changed files with 85 additions and 82 deletions

View File

@@ -25,7 +25,8 @@ TypesBuilder::TypesBuilder(TypeManager* tm)
void_(tm->Get<ast::type::VoidType>()),
tm_(tm) {}
Builder::Builder(tint::Context* c) : ctx(c), ty(&c->type_mgr()) {}
Builder::Builder(tint::Context* c, tint::ast::Module* m)
: ctx(c), mod(m), ty(&c->type_mgr()) {}
Builder::~Builder() = default;
ast::Variable* Builder::Var(const std::string& name,
@@ -36,9 +37,11 @@ ast::Variable* Builder::Var(const std::string& name,
return var;
}
BuilderWithContext::BuilderWithContext() : Builder(new Context()) {}
BuilderWithContext::~BuilderWithContext() {
BuilderWithContextAndModule::BuilderWithContextAndModule()
: Builder(new Context(), new ast::Module()) {}
BuilderWithContextAndModule::~BuilderWithContextAndModule() {
delete ctx;
delete mod;
}
} // namespace ast

View File

@@ -24,6 +24,7 @@
#include "src/ast/expression.h"
#include "src/ast/float_literal.h"
#include "src/ast/identifier_expression.h"
#include "src/ast/module.h"
#include "src/ast/scalar_constructor_expression.h"
#include "src/ast/sint_literal.h"
#include "src/ast/type/array_type.h"
@@ -185,7 +186,8 @@ class Builder {
/// Constructor
/// @param ctx the context to use in the builder
explicit Builder(tint::Context* ctx);
/// @param mod the module to use in the builder
explicit Builder(tint::Context* ctx, tint::ast::Module* mod);
virtual ~Builder();
/// @param expr the expression
@@ -441,17 +443,19 @@ class Builder {
ExprList(std::forward<ARGS>(args)...)};
}
/// Creates a new `ast::Node` owned by the Context. When the Context is
/// Creates a new `ast::Node` owned by the Module. When the Module is
/// destructed, the `ast::Node` will also be destructed.
/// @param args the arguments to pass to the type constructor
/// @returns the node pointer
template <typename T, typename... ARGS>
T* create(ARGS&&... args) {
return ctx->create<T>(std::forward<ARGS>(args)...);
return mod->create<T>(std::forward<ARGS>(args)...);
}
/// The builder context
/// The builder module
tint::Context* const ctx;
/// The builder module
tint::ast::Module* const mod;
/// The builder types
const TypesBuilder ty;
@@ -460,11 +464,12 @@ class Builder {
virtual void OnVariableBuilt(ast::Variable*) {}
};
/// BuilderWithContext is a `Builder` that constructs and owns its `Context`.
class BuilderWithContext : public Builder {
/// BuilderWithContextAndModule is a `Builder` that constructs and owns its
/// `Context` and `Module`.
class BuilderWithContextAndModule : public Builder {
public:
BuilderWithContext();
~BuilderWithContext() override;
BuilderWithContextAndModule();
~BuilderWithContextAndModule() override;
};
//! @cond Doxygen_Suppress

View File

@@ -77,6 +77,20 @@ class Module {
/// @returns a string representation of the module
std::string to_str() const;
/// Creates a new `ast::Node` owned by the Module. When the Module is
/// destructed, the `ast::Node` will also be destructed.
/// @param args the arguments to pass to the type constructor
/// @returns the node pointer
template <typename T, typename... ARGS>
T* create(ARGS&&... args) {
static_assert(std::is_base_of<ast::Node, T>::value,
"T does not derive from ast::Node");
auto uptr = std::make_unique<T>(std::forward<ARGS>(args)...);
auto ptr = uptr.get();
ast_nodes_.emplace_back(std::move(uptr));
return ptr;
}
private:
Module(const Module&) = delete;
@@ -84,6 +98,7 @@ class Module {
// The constructed types are owned by the type manager
std::vector<type::Type*> constructed_types_;
FunctionList functions_;
std::vector<std::unique_ptr<ast::Node>> ast_nodes_;
};
} // namespace ast

View File

@@ -19,7 +19,7 @@
#include <utility>
#include "gtest/gtest.h"
#include "src/context.h"
#include "src/ast/module.h"
namespace tint {
namespace ast {
@@ -31,17 +31,17 @@ class TestHelperBase : public BASE {
TestHelperBase() {}
~TestHelperBase() = default;
/// Creates a new `ast::Node` owned by the Context. When the Context is
/// Creates a new `ast::Node` owned by the Module. When the Module is
/// destructed, the `ast::Node` will also be destructed.
/// @param args the arguments to pass to the type constructor
/// @returns the node pointer
template <typename T, typename... ARGS>
T* create(ARGS&&... args) {
return ctx.create<T>(std::forward<ARGS>(args)...);
return mod.create<T>(std::forward<ARGS>(args)...);
}
/// The context
Context ctx;
/// The module
Module mod;
};
using TestHelper = TestHelperBase<testing::Test>;