From 3e6b74043020d0838f739f144fac4f14828ae429 Mon Sep 17 00:00:00 2001 From: "Emil J. Tywoniak" Date: Sun, 21 Dec 2025 16:56:14 +0100 Subject: [PATCH] rtlil: allow friends to use Cell constructors with a factory token pattern --- kernel/rtlil.cc | 4 ++-- kernel/rtlil.h | 8 +++++--- kernel/unstable/patch.cc | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/kernel/rtlil.cc b/kernel/rtlil.cc index 5a3a5fb60..ac970e2f1 100644 --- a/kernel/rtlil.cc +++ b/kernel/rtlil.cc @@ -3129,7 +3129,7 @@ RTLIL::Wire *RTLIL::Module::addWire(RTLIL::IdString name, const RTLIL::Wire *oth RTLIL::Cell *RTLIL::Module::addCell(RTLIL::IdString name, RTLIL::IdString type) { - RTLIL::Cell *cell = new RTLIL::Cell; + RTLIL::Cell *cell = new RTLIL::Cell(Cell::ConstructToken{}); cell->name = std::move(name); cell->type = type; add(cell); @@ -4243,7 +4243,7 @@ std::string RTLIL::Process::to_rtlil_str() const return f.str(); } -RTLIL::Cell::Cell() : module(nullptr) +RTLIL::Cell::Cell(RTLIL::Cell::ConstructToken) : module(nullptr) { static unsigned int hashidx_count = 123456789; hashidx_count = mkhash_xorshift(hashidx_count); diff --git a/kernel/rtlil.h b/kernel/rtlil.h index cf43df7f0..6c22efcd5 100644 --- a/kernel/rtlil.h +++ b/kernel/rtlil.h @@ -2525,18 +2525,20 @@ struct RTLIL::Memory : public RTLIL::NamedObject struct RTLIL::Cell : public RTLIL::NamedObject { +private: + struct ConstructToken { explicit ConstructToken() = default; }; +public: Hasher::hash_t hashidx_; [[nodiscard]] Hasher hash_into(Hasher h) const { h.eat(hashidx_); return h; } -public: // use module->addCell() and module->remove() to create or destroy cells friend struct RTLIL::Module; friend struct RTLIL::Patch; - Cell(); + Cell(ConstructToken); ~Cell(); // do not simply copy cells - Cell(RTLIL::Cell &other) = delete; + Cell(ConstructToken, RTLIL::Cell &other); void operator=(RTLIL::Cell &other) = delete; RTLIL::Module *module; diff --git a/kernel/unstable/patch.cc b/kernel/unstable/patch.cc index 18d761384..53632114d 100644 --- a/kernel/unstable/patch.cc +++ b/kernel/unstable/patch.cc @@ -4,7 +4,7 @@ YOSYS_NAMESPACE_BEGIN using namespace RTLIL; Cell* Patch::addCell(IdString name, IdString type) { - auto& cell = cells_.emplace_back(); + auto& cell = cells_.emplace_back(Cell::ConstructToken{}); cell.name = std::move(name); cell.type = type; return &cell;