From 8564d0822864d298f8b75f6fa348644126964be3 Mon Sep 17 00:00:00 2001 From: hrkrshnn Date: Fri, 11 Dec 2020 16:44:38 +0100 Subject: [PATCH] Added a helper class FunctionCopier in ASTCopier Helper class that creates a copy of the function definition, replacing the names of the variable declaration with a new name. --- libyul/optimiser/ASTCopier.cpp | 6 ++++++ libyul/optimiser/ASTCopier.h | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/libyul/optimiser/ASTCopier.cpp b/libyul/optimiser/ASTCopier.cpp index 358811358..8c0b3acfa 100644 --- a/libyul/optimiser/ASTCopier.cpp +++ b/libyul/optimiser/ASTCopier.cpp @@ -171,3 +171,9 @@ TypedName ASTCopier::translate(TypedName const& _typedName) return TypedName{_typedName.location, translateIdentifier(_typedName.name), _typedName.type}; } +YulString FunctionCopier::translateIdentifier(YulString _name) +{ + if (m_translations.count(_name)) + return m_translations.at(_name); + return _name; +} diff --git a/libyul/optimiser/ASTCopier.h b/libyul/optimiser/ASTCopier.h index 4cf69b0b7..db9d72af3 100644 --- a/libyul/optimiser/ASTCopier.h +++ b/libyul/optimiser/ASTCopier.h @@ -29,6 +29,7 @@ #include #include #include +#include namespace solidity::yul { @@ -117,5 +118,22 @@ std::vector ASTCopier::translateVector(std::vector const& _values) return translated; } +/// Helper class that creates a copy of the function definition, replacing the names of the variable +/// declarations with new names. +class FunctionCopier: public ASTCopier +{ +public: + FunctionCopier( + std::map const& _translations + ): + m_translations(_translations) + {} + using ASTCopier::operator(); + YulString translateIdentifier(YulString _name) override; +private: + /// A mapping between old and new names. We replace the names of variable declarations contained + /// in the mapping with their new names. + std::map const& m_translations; +}; }