From 7af7273b60235f5c9e9fa63938e55febfc398b57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20=C5=9Aliwak?= Date: Wed, 13 May 2020 19:48:31 +0200 Subject: [PATCH] Move static functions for constructing names from IRGenerationContext into the new `Common` module --- libsolidity/CMakeLists.txt | 2 + libsolidity/codegen/ir/Common.cpp | 46 +++++++++++++++++++ libsolidity/codegen/ir/Common.h | 38 +++++++++++++++ .../codegen/ir/IRGenerationContext.cpp | 25 +--------- libsolidity/codegen/ir/IRGenerationContext.h | 7 +-- libsolidity/codegen/ir/IRGenerator.cpp | 12 ++--- .../codegen/ir/IRGeneratorForStatements.cpp | 4 +- 7 files changed, 97 insertions(+), 37 deletions(-) create mode 100644 libsolidity/codegen/ir/Common.cpp create mode 100644 libsolidity/codegen/ir/Common.h diff --git a/libsolidity/CMakeLists.txt b/libsolidity/CMakeLists.txt index 96ccff9e4..7b66e533e 100644 --- a/libsolidity/CMakeLists.txt +++ b/libsolidity/CMakeLists.txt @@ -79,6 +79,8 @@ set(sources codegen/ReturnInfo.cpp codegen/YulUtilFunctions.h codegen/YulUtilFunctions.cpp + codegen/ir/Common.cpp + codegen/ir/Common.h codegen/ir/IRGenerator.cpp codegen/ir/IRGenerator.h codegen/ir/IRGeneratorForStatements.cpp diff --git a/libsolidity/codegen/ir/Common.cpp b/libsolidity/codegen/ir/Common.cpp new file mode 100644 index 000000000..389b53333 --- /dev/null +++ b/libsolidity/codegen/ir/Common.cpp @@ -0,0 +1,46 @@ +/* + This file is part of solidity. + + solidity is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + solidity is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with solidity. If not, see . +*/ + +#include + +#include + +using namespace std; +using namespace solidity::util; +using namespace solidity::frontend; + +string IRNames::function(FunctionDefinition const& _function) +{ + // @TODO previously, we had to distinguish creation context and runtime context, + // but since we do not work with jump positions anymore, this should not be a problem, right? + return "fun_" + _function.name() + "_" + to_string(_function.id()); +} + +string IRNames::function(VariableDeclaration const& _varDecl) +{ + return "getter_fun_" + _varDecl.name() + "_" + to_string(_varDecl.id()); +} + +string IRNames::creationObject(ContractDefinition const& _contract) +{ + return _contract.name() + "_" + toString(_contract.id()); +} + +string IRNames::runtimeObject(ContractDefinition const& _contract) +{ + return _contract.name() + "_" + toString(_contract.id()) + "_deployed"; +} diff --git a/libsolidity/codegen/ir/Common.h b/libsolidity/codegen/ir/Common.h new file mode 100644 index 000000000..323687618 --- /dev/null +++ b/libsolidity/codegen/ir/Common.h @@ -0,0 +1,38 @@ +/* + This file is part of solidity. + + solidity is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + solidity is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with solidity. If not, see . +*/ +/** + * Miscellaneous utilities for use in IR generator. + */ + +#pragma once + +#include + +#include + +namespace solidity::frontend +{ + +struct IRNames +{ + static std::string function(FunctionDefinition const& _function); + static std::string function(VariableDeclaration const& _varDecl); + static std::string creationObject(ContractDefinition const& _contract); + static std::string runtimeObject(ContractDefinition const& _contract); +}; + +} diff --git a/libsolidity/codegen/ir/IRGenerationContext.cpp b/libsolidity/codegen/ir/IRGenerationContext.cpp index ed3b989d0..8e01b83d5 100644 --- a/libsolidity/codegen/ir/IRGenerationContext.cpp +++ b/libsolidity/codegen/ir/IRGenerationContext.cpp @@ -36,7 +36,7 @@ using namespace solidity::frontend; string IRGenerationContext::enqueueFunctionForCodeGeneration(FunctionDefinition const& _function) { - string name = functionName(_function); + string name = IRNames::function(_function); if (!m_functions.contains(name)) m_functionGenerationQueue.insert(&_function); @@ -116,27 +116,6 @@ void IRGenerationContext::addStateVariable( m_stateVariables[&_declaration] = make_pair(move(_storageOffset), _byteOffset); } -string IRGenerationContext::functionName(FunctionDefinition const& _function) -{ - // @TODO previously, we had to distinguish creation context and runtime context, - // but since we do not work with jump positions anymore, this should not be a problem, right? - return "fun_" + _function.name() + "_" + to_string(_function.id()); -} - -string IRGenerationContext::functionName(VariableDeclaration const& _varDecl) -{ - return "getter_fun_" + _varDecl.name() + "_" + to_string(_varDecl.id()); -} - -string IRGenerationContext::creationObjectName(ContractDefinition const& _contract) const -{ - return _contract.name() + "_" + toString(_contract.id()); -} -string IRGenerationContext::runtimeObjectName(ContractDefinition const& _contract) const -{ - return _contract.name() + "_" + toString(_contract.id()) + "_deployed"; -} - string IRGenerationContext::newYulVariable() { return "_" + to_string(++m_varCounter); @@ -196,7 +175,7 @@ string IRGenerationContext::internalDispatch(size_t _in, size_t _out) functions.emplace_back(map { { "funID", to_string(function->id()) }, - { "name", functionName(*function)} + { "name", IRNames::function(*function)} }); enqueueFunctionForCodeGeneration(*function); diff --git a/libsolidity/codegen/ir/IRGenerationContext.h b/libsolidity/codegen/ir/IRGenerationContext.h index a7eab7ceb..f38f75d86 100644 --- a/libsolidity/codegen/ir/IRGenerationContext.h +++ b/libsolidity/codegen/ir/IRGenerationContext.h @@ -26,6 +26,7 @@ #include #include +#include #include @@ -99,12 +100,6 @@ public: return m_stateVariables.at(&_varDecl); } - std::string functionName(FunctionDefinition const& _function); - std::string functionName(VariableDeclaration const& _varDecl); - - std::string creationObjectName(ContractDefinition const& _contract) const; - std::string runtimeObjectName(ContractDefinition const& _contract) const; - std::string newYulVariable(); std::string internalDispatch(size_t _in, size_t _out); diff --git a/libsolidity/codegen/ir/IRGenerator.cpp b/libsolidity/codegen/ir/IRGenerator.cpp index be878e739..912a93ed0 100644 --- a/libsolidity/codegen/ir/IRGenerator.cpp +++ b/libsolidity/codegen/ir/IRGenerator.cpp @@ -114,7 +114,7 @@ string IRGenerator::generate( for (VariableDeclaration const* var: ContractType(_contract).immutableVariables()) m_context.registerImmutableVariable(*var); - t("CreationObject", m_context.creationObjectName(_contract)); + t("CreationObject", IRNames::creationObject(_contract)); t("memoryInit", memoryInit()); t("notLibrary", !_contract.isLibrary()); @@ -127,7 +127,7 @@ string IRGenerator::generate( constructorParams.emplace_back(m_context.newYulVariable()); t( "copyConstructorArguments", - m_utils.copyConstructorArgumentsToMemoryFunction(_contract, m_context.creationObjectName(_contract)) + m_utils.copyConstructorArgumentsToMemoryFunction(_contract, IRNames::creationObject(_contract)) ); } t("constructorParams", joinHumanReadable(constructorParams)); @@ -142,7 +142,7 @@ string IRGenerator::generate( resetContext(_contract); // Do not register immutables to avoid assignment. - t("RuntimeObject", m_context.runtimeObjectName(_contract)); + t("RuntimeObject", IRNames::runtimeObject(_contract)); t("dispatch", dispatchRoutine(_contract)); generateQueuedFunctions(); t("runtimeFunctions", m_context.functionCollector().requestedFunctions()); @@ -166,7 +166,7 @@ void IRGenerator::generateQueuedFunctions() string IRGenerator::generateFunction(FunctionDefinition const& _function) { - string functionName = m_context.functionName(_function); + string functionName = IRNames::function(_function); return m_context.functionCollector().createFunction(functionName, [&]() { Whiskers t(R"( function () -> { @@ -195,7 +195,7 @@ string IRGenerator::generateFunction(FunctionDefinition const& _function) string IRGenerator::generateGetter(VariableDeclaration const& _varDecl) { - string functionName = m_context.functionName(_varDecl); + string functionName = IRNames::function(_varDecl); Type const* type = _varDecl.annotation().type; @@ -431,7 +431,7 @@ string IRGenerator::deployCode(ContractDefinition const& _contract) return(0, datasize("")) )X"); - t("object", m_context.runtimeObjectName(_contract)); + t("object", IRNames::runtimeObject(_contract)); vector> loadImmutables; vector> storeImmutables; diff --git a/libsolidity/codegen/ir/IRGeneratorForStatements.cpp b/libsolidity/codegen/ir/IRGeneratorForStatements.cpp index 528523227..eaabdd58e 100644 --- a/libsolidity/codegen/ir/IRGeneratorForStatements.cpp +++ b/libsolidity/codegen/ir/IRGeneratorForStatements.cpp @@ -1103,7 +1103,7 @@ void IRGeneratorForStatements::endVisit(FunctionCall const& _functionCall) t("memEnd", m_context.newYulVariable()); t("allocateTemporaryMemory", m_utils.allocationTemporaryMemoryFunction()); t("releaseTemporaryMemory", m_utils.releaseTemporaryMemoryFunction()); - t("object", m_context.creationObjectName(*contract)); + t("object", IRNames::creationObject(*contract)); t("abiEncode", m_context.abiFunctions().tupleEncoder(argumentTypes, functionType->parameterTypes(), false) ); @@ -1371,7 +1371,7 @@ void IRGeneratorForStatements::endVisit(MemberAccess const& _memberAccess) )") ("allocationFunction", m_utils.allocationFunction()) ("size", m_context.newYulVariable()) - ("objectName", m_context.creationObjectName(contract)) + ("objectName", IRNames::creationObject(contract)) ("result", IRVariable(_memberAccess).commaSeparatedList()).render(); } else if (member == "name")