Change generators to take a pointer instead of move.

This CL changes the generators so we don't have to move the module into
the generator. This will then allow calling the demangler at a later
point and still having access to the module.

Change-Id: Icad16ddb2b89921cbb174cf7fec520c410139285
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/36942
Reviewed-by: Ben Clayton <bclayton@google.com>
This commit is contained in:
dan sinclair 2021-01-11 16:24:32 +00:00 committed by dan sinclair
parent 7d152e0936
commit b920e604b2
15 changed files with 68 additions and 19 deletions

View File

@ -550,7 +550,7 @@ int main(int argc, const char** argv) {
#if TINT_BUILD_SPV_WRITER #if TINT_BUILD_SPV_WRITER
if (options.format == Format::kSpirv || options.format == Format::kSpvAsm) { if (options.format == Format::kSpirv || options.format == Format::kSpvAsm) {
writer = std::make_unique<tint::writer::spirv::Generator>(std::move(mod)); writer = std::make_unique<tint::writer::spirv::Generator>(&mod);
} }
#endif // TINT_BUILD_SPV_WRITER #endif // TINT_BUILD_SPV_WRITER
@ -562,13 +562,13 @@ int main(int argc, const char** argv) {
#if TINT_BUILD_MSL_WRITER #if TINT_BUILD_MSL_WRITER
if (options.format == Format::kMsl) { if (options.format == Format::kMsl) {
writer = std::make_unique<tint::writer::msl::Generator>(std::move(mod)); writer = std::make_unique<tint::writer::msl::Generator>(&mod);
} }
#endif // TINT_BUILD_MSL_WRITER #endif // TINT_BUILD_MSL_WRITER
#if TINT_BUILD_HLSL_WRITER #if TINT_BUILD_HLSL_WRITER
if (options.format == Format::kHlsl) { if (options.format == Format::kHlsl) {
writer = std::make_unique<tint::writer::hlsl::Generator>(std::move(mod)); writer = std::make_unique<tint::writer::hlsl::Generator>(&mod);
} }
#endif // TINT_BUILD_HLSL_WRITER #endif // TINT_BUILD_HLSL_WRITER

View File

@ -149,13 +149,13 @@ fn main() -> void {
// reconstruct the WGSL. // reconstruct the WGSL.
std::string src_wgsl; std::string src_wgsl;
{ {
writer::wgsl::Generator src_gen(std::move(src)); writer::wgsl::Generator src_gen(&src);
ASSERT_TRUE(src_gen.Generate()); ASSERT_TRUE(src_gen.Generate());
src_wgsl = src_gen.result(); src_wgsl = src_gen.result();
} }
// Print the dst module, check it matches the original source // Print the dst module, check it matches the original source
writer::wgsl::Generator dst_gen(std::move(dst)); writer::wgsl::Generator dst_gen(&dst);
ASSERT_TRUE(dst_gen.Generate()); ASSERT_TRUE(dst_gen.Generate());
auto dst_wgsl = dst_gen.result(); auto dst_wgsl = dst_gen.result();
ASSERT_EQ(src_wgsl, dst_wgsl); ASSERT_EQ(src_wgsl, dst_wgsl);

View File

@ -66,7 +66,7 @@ class TransformTest : public testing::Test {
// Release the source module to ensure there's no uncloned data in result // Release the source module to ensure there's no uncloned data in result
{ auto tmp = std::move(module); } { auto tmp = std::move(module); }
writer::wgsl::Generator generator(std::move(result.module)); writer::wgsl::Generator generator(&(result.module));
if (!generator.Generate()) { if (!generator.Generate()) {
return "WGSL writer failed:\n" + generator.error(); return "WGSL writer failed:\n" + generator.error();
} }

View File

@ -22,8 +22,13 @@ namespace hlsl {
Generator::Generator(ast::Module module) Generator::Generator(ast::Module module)
: Text(std::move(module)), : Text(std::move(module)),
namer_(std::make_unique<UnsafeNamer>(&module)), namer_(std::make_unique<UnsafeNamer>(module_)),
impl_(std::make_unique<GeneratorImpl>(&module_, namer_.get())) {} impl_(std::make_unique<GeneratorImpl>(module_, namer_.get())) {}
Generator::Generator(ast::Module* module)
: Text(module),
namer_(std::make_unique<UnsafeNamer>(module_)),
impl_(std::make_unique<GeneratorImpl>(module_, namer_.get())) {}
Generator::~Generator() = default; Generator::~Generator() = default;
@ -31,7 +36,7 @@ void Generator::Reset() {
set_error(""); set_error("");
out_ = std::ostringstream(); out_ = std::ostringstream();
namer_->Reset(); namer_->Reset();
impl_ = std::make_unique<GeneratorImpl>(&module_, namer_.get()); impl_ = std::make_unique<GeneratorImpl>(module_, namer_.get());
} }
bool Generator::Generate() { bool Generator::Generate() {

View File

@ -31,8 +31,12 @@ namespace hlsl {
class Generator : public Text { class Generator : public Text {
public: public:
/// Constructor /// Constructor
/// DEPRECATED
/// @param module the module to convert /// @param module the module to convert
explicit Generator(ast::Module module); explicit Generator(ast::Module module);
/// Constructor
/// @param module the module to convert
explicit Generator(ast::Module* module);
~Generator() override; ~Generator() override;
/// Resets the generator /// Resets the generator

View File

@ -22,15 +22,20 @@ namespace msl {
Generator::Generator(ast::Module module) Generator::Generator(ast::Module module)
: Text(std::move(module)), : Text(std::move(module)),
namer_(std::make_unique<UnsafeNamer>(&module_)), namer_(std::make_unique<UnsafeNamer>(module_)),
impl_(std::make_unique<GeneratorImpl>(&module_, namer_.get())) {} impl_(std::make_unique<GeneratorImpl>(module_, namer_.get())) {}
Generator::Generator(ast::Module* module)
: Text(module),
namer_(std::make_unique<UnsafeNamer>(module_)),
impl_(std::make_unique<GeneratorImpl>(module_, namer_.get())) {}
Generator::~Generator() = default; Generator::~Generator() = default;
void Generator::Reset() { void Generator::Reset() {
set_error(""); set_error("");
namer_->Reset(); namer_->Reset();
impl_ = std::make_unique<GeneratorImpl>(&module_, namer_.get()); impl_ = std::make_unique<GeneratorImpl>(module_, namer_.get());
} }
bool Generator::Generate() { bool Generator::Generate() {

View File

@ -30,8 +30,12 @@ namespace msl {
class Generator : public Text { class Generator : public Text {
public: public:
/// Constructor /// Constructor
/// DEPRECATED
/// @param module the module to convert /// @param module the module to convert
explicit Generator(ast::Module module); explicit Generator(ast::Module module);
/// Constructor
/// @param module the module to convert
explicit Generator(ast::Module* module);
~Generator() override; ~Generator() override;
/// Resets the generator /// Resets the generator

View File

@ -22,15 +22,21 @@ namespace spirv {
Generator::Generator(ast::Module module) Generator::Generator(ast::Module module)
: writer::Writer(std::move(module)), : writer::Writer(std::move(module)),
namer_(std::make_unique<UnsafeNamer>(&module_)), namer_(std::make_unique<UnsafeNamer>(module_)),
builder_(std::make_unique<Builder>(&module_, namer_.get())), builder_(std::make_unique<Builder>(module_, namer_.get())),
writer_(std::make_unique<BinaryWriter>()) {}
Generator::Generator(ast::Module* module)
: writer::Writer(module),
namer_(std::make_unique<UnsafeNamer>(module_)),
builder_(std::make_unique<Builder>(module_, namer_.get())),
writer_(std::make_unique<BinaryWriter>()) {} writer_(std::make_unique<BinaryWriter>()) {}
Generator::~Generator() = default; Generator::~Generator() = default;
void Generator::Reset() { void Generator::Reset() {
namer_->Reset(); namer_->Reset();
builder_ = std::make_unique<Builder>(&module_, namer_.get()); builder_ = std::make_unique<Builder>(module_, namer_.get());
writer_ = std::make_unique<BinaryWriter>(); writer_ = std::make_unique<BinaryWriter>();
} }

View File

@ -33,8 +33,12 @@ namespace spirv {
class Generator : public writer::Writer { class Generator : public writer::Writer {
public: public:
/// Constructor /// Constructor
/// DEPRECATED
/// @param module the module to convert /// @param module the module to convert
explicit Generator(ast::Module module); explicit Generator(ast::Module module);
/// Constructor
/// @param module the module to convert
explicit Generator(ast::Module* module);
~Generator() override; ~Generator() override;
/// Resets the generator /// Resets the generator

View File

@ -21,6 +21,8 @@ namespace writer {
Text::Text(ast::Module module) : Writer(std::move(module)) {} Text::Text(ast::Module module) : Writer(std::move(module)) {}
Text::Text(ast::Module* module) : Writer(module) {}
Text::~Text() = default; Text::~Text() = default;
} // namespace writer } // namespace writer

View File

@ -28,6 +28,9 @@ class Text : public Writer {
/// Constructor /// Constructor
/// @param module the module to convert /// @param module the module to convert
explicit Text(ast::Module module); explicit Text(ast::Module module);
/// Constructor
/// @param module the module to convert
explicit Text(ast::Module* module);
~Text() override; ~Text() override;
/// @returns the result data /// @returns the result data

View File

@ -22,13 +22,16 @@ namespace wgsl {
Generator::Generator(ast::Module module) Generator::Generator(ast::Module module)
: Text(std::move(module)), : Text(std::move(module)),
impl_(std::make_unique<GeneratorImpl>(&module_)) {} impl_(std::make_unique<GeneratorImpl>(module_)) {}
Generator::Generator(ast::Module* module)
: Text(module), impl_(std::make_unique<GeneratorImpl>(module_)) {}
Generator::~Generator() = default; Generator::~Generator() = default;
void Generator::Reset() { void Generator::Reset() {
set_error(""); set_error("");
impl_ = std::make_unique<GeneratorImpl>(&module_); impl_ = std::make_unique<GeneratorImpl>(module_);
} }
bool Generator::Generate() { bool Generator::Generate() {

View File

@ -29,8 +29,12 @@ namespace wgsl {
class Generator : public Text { class Generator : public Text {
public: public:
/// Constructor /// Constructor
/// DEPRECATED
/// @param module the module to convert /// @param module the module to convert
explicit Generator(ast::Module module); explicit Generator(ast::Module module);
/// Constructor
/// @param module the module to convert
explicit Generator(ast::Module* module);
~Generator() override; ~Generator() override;
/// Resets the generator /// Resets the generator

View File

@ -19,7 +19,10 @@
namespace tint { namespace tint {
namespace writer { namespace writer {
Writer::Writer(ast::Module module) : module_(std::move(module)) {} Writer::Writer(ast::Module module)
: owned_module_(std::move(module)), module_(&owned_module_) {}
Writer::Writer(ast::Module* module) : module_(module) {}
Writer::~Writer() = default; Writer::~Writer() = default;

View File

@ -51,6 +51,9 @@ class Writer {
/// Constructor /// Constructor
/// @param module the tint module to convert /// @param module the tint module to convert
explicit Writer(ast::Module module); explicit Writer(ast::Module module);
/// Constructor
/// @param module the tint module to convert
explicit Writer(ast::Module* module);
/// Sets the error string /// Sets the error string
/// @param msg the error message /// @param msg the error message
@ -58,8 +61,11 @@ class Writer {
/// An error message, if an error was encountered /// An error message, if an error was encountered
std::string error_; std::string error_;
/// Temporary owned module until we can update the API ...
ast::Module owned_module_;
/// The module being converted /// The module being converted
ast::Module module_; ast::Module* module_;
}; };
} // namespace writer } // namespace writer