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 (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
@ -562,13 +562,13 @@ int main(int argc, const char** argv) {
#if TINT_BUILD_MSL_WRITER
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
#if TINT_BUILD_HLSL_WRITER
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

View File

@ -149,13 +149,13 @@ fn main() -> void {
// reconstruct the WGSL.
std::string src_wgsl;
{
writer::wgsl::Generator src_gen(std::move(src));
writer::wgsl::Generator src_gen(&src);
ASSERT_TRUE(src_gen.Generate());
src_wgsl = src_gen.result();
}
// 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());
auto dst_wgsl = dst_gen.result();
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
{ auto tmp = std::move(module); }
writer::wgsl::Generator generator(std::move(result.module));
writer::wgsl::Generator generator(&(result.module));
if (!generator.Generate()) {
return "WGSL writer failed:\n" + generator.error();
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -33,8 +33,12 @@ namespace spirv {
class Generator : public writer::Writer {
public:
/// Constructor
/// DEPRECATED
/// @param module the module to convert
explicit Generator(ast::Module module);
/// Constructor
/// @param module the module to convert
explicit Generator(ast::Module* module);
~Generator() override;
/// 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(module) {}
Text::~Text() = default;
} // namespace writer

View File

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

View File

@ -22,13 +22,16 @@ namespace wgsl {
Generator::Generator(ast::Module 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;
void Generator::Reset() {
set_error("");
impl_ = std::make_unique<GeneratorImpl>(&module_);
impl_ = std::make_unique<GeneratorImpl>(module_);
}
bool Generator::Generate() {

View File

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

View File

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

View File

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