2020-05-13 17:48:31 +00:00
|
|
|
/*
|
|
|
|
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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2020-05-13 17:48:31 +00:00
|
|
|
|
2020-04-28 11:14:45 +00:00
|
|
|
#include <libsolidity/ast/TypeProvider.h>
|
2021-05-27 20:05:21 +00:00
|
|
|
#include <libsolidity/codegen/ir/Common.h>
|
|
|
|
#include <libsolidity/codegen/ir/IRGenerationContext.h>
|
2020-05-13 17:48:31 +00:00
|
|
|
|
|
|
|
#include <libsolutil/CommonIO.h>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace solidity::util;
|
|
|
|
using namespace solidity::frontend;
|
|
|
|
|
2021-05-27 20:05:21 +00:00
|
|
|
namespace solidity::frontend
|
|
|
|
{
|
|
|
|
|
2020-04-28 11:14:45 +00:00
|
|
|
YulArity YulArity::fromType(FunctionType const& _functionType)
|
|
|
|
{
|
|
|
|
return YulArity{
|
2020-11-19 09:53:07 +00:00
|
|
|
TupleType(_functionType.parameterTypesIncludingSelf()).sizeOnStack(),
|
2020-04-28 11:14:45 +00:00
|
|
|
TupleType(_functionType.returnParameterTypes()).sizeOnStack()
|
|
|
|
};
|
|
|
|
}
|
2020-11-30 17:59:49 +00:00
|
|
|
|
2020-05-13 17:48:31 +00:00
|
|
|
string IRNames::function(FunctionDefinition const& _function)
|
|
|
|
{
|
2020-11-18 17:24:33 +00:00
|
|
|
if (_function.isConstructor())
|
2021-06-14 10:35:31 +00:00
|
|
|
return constructor(*_function.annotation().contract);
|
2020-11-18 17:24:33 +00:00
|
|
|
|
2020-05-13 17:48:31 +00:00
|
|
|
return "fun_" + _function.name() + "_" + to_string(_function.id());
|
|
|
|
}
|
|
|
|
|
|
|
|
string IRNames::function(VariableDeclaration const& _varDecl)
|
|
|
|
{
|
|
|
|
return "getter_fun_" + _varDecl.name() + "_" + to_string(_varDecl.id());
|
|
|
|
}
|
|
|
|
|
2020-11-30 17:59:49 +00:00
|
|
|
string IRNames::modifierInvocation(ModifierInvocation const& _modifierInvocation)
|
|
|
|
{
|
|
|
|
// This uses the ID of the modifier invocation because it has to be unique
|
|
|
|
// for each invocation.
|
|
|
|
solAssert(!_modifierInvocation.name().path().empty(), "");
|
|
|
|
string const& modifierName = _modifierInvocation.name().path().back();
|
|
|
|
solAssert(!modifierName.empty(), "");
|
|
|
|
return "modifier_" + modifierName + "_" + to_string(_modifierInvocation.id());
|
|
|
|
}
|
|
|
|
|
|
|
|
string IRNames::functionWithModifierInner(FunctionDefinition const& _function)
|
|
|
|
{
|
|
|
|
return "fun_" + _function.name() + "_" + to_string(_function.id()) + "_inner";
|
|
|
|
}
|
|
|
|
|
2020-05-13 17:48:31 +00:00
|
|
|
string IRNames::creationObject(ContractDefinition const& _contract)
|
|
|
|
{
|
|
|
|
return _contract.name() + "_" + toString(_contract.id());
|
|
|
|
}
|
|
|
|
|
2021-02-09 23:55:12 +00:00
|
|
|
string IRNames::deployedObject(ContractDefinition const& _contract)
|
2020-05-13 17:48:31 +00:00
|
|
|
{
|
|
|
|
return _contract.name() + "_" + toString(_contract.id()) + "_deployed";
|
|
|
|
}
|
2020-05-14 20:44:46 +00:00
|
|
|
|
2020-04-28 11:20:39 +00:00
|
|
|
string IRNames::internalDispatch(YulArity const& _arity)
|
|
|
|
{
|
|
|
|
return "dispatch_internal"
|
|
|
|
"_in_" + to_string(_arity.in) +
|
|
|
|
"_out_" + to_string(_arity.out);
|
|
|
|
}
|
|
|
|
|
2021-06-14 10:35:31 +00:00
|
|
|
string IRNames::constructor(ContractDefinition const& _contract)
|
2020-05-14 20:44:46 +00:00
|
|
|
{
|
|
|
|
return "constructor_" + _contract.name() + "_" + to_string(_contract.id());
|
|
|
|
}
|
|
|
|
|
2020-12-22 16:45:55 +00:00
|
|
|
string IRNames::libraryAddressImmutable()
|
|
|
|
{
|
|
|
|
return "library_deploy_address";
|
|
|
|
}
|
|
|
|
|
2020-05-14 20:44:46 +00:00
|
|
|
string IRNames::constantValueFunction(VariableDeclaration const& _constant)
|
|
|
|
{
|
|
|
|
solAssert(_constant.isConstant(), "");
|
|
|
|
return "constant_" + _constant.name() + "_" + to_string(_constant.id());
|
|
|
|
}
|
|
|
|
|
|
|
|
string IRNames::localVariable(VariableDeclaration const& _declaration)
|
|
|
|
{
|
2020-12-08 20:42:59 +00:00
|
|
|
return "var_" + _declaration.name() + '_' + std::to_string(_declaration.id());
|
2020-05-14 20:44:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
string IRNames::localVariable(Expression const& _expression)
|
|
|
|
{
|
|
|
|
return "expr_" + to_string(_expression.id());
|
|
|
|
}
|
|
|
|
|
|
|
|
string IRNames::trySuccessConditionVariable(Expression const& _expression)
|
|
|
|
{
|
|
|
|
auto annotation = dynamic_cast<FunctionCallAnnotation const*>(&_expression.annotation());
|
|
|
|
solAssert(annotation, "");
|
|
|
|
solAssert(annotation->tryCall, "Parameter must be a FunctionCall with tryCall-annotation set.");
|
|
|
|
|
|
|
|
return "trySuccessCondition_" + to_string(_expression.id());
|
|
|
|
}
|
|
|
|
|
|
|
|
string IRNames::tupleComponent(size_t _i)
|
|
|
|
{
|
|
|
|
return "component_" + to_string(_i + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
string IRNames::zeroValue(Type const& _type, string const& _variableName)
|
|
|
|
{
|
2020-12-08 20:42:59 +00:00
|
|
|
return "zero_" + _type.identifier() + _variableName;
|
2020-05-14 20:44:46 +00:00
|
|
|
}
|
2021-05-27 20:05:21 +00:00
|
|
|
|
|
|
|
string sourceLocationComment(langutil::SourceLocation const& _location, IRGenerationContext const& _context)
|
|
|
|
{
|
2021-06-29 12:38:59 +00:00
|
|
|
solAssert(_location.sourceName, "");
|
2021-05-27 20:05:21 +00:00
|
|
|
return "/// @src "
|
2021-06-29 12:38:59 +00:00
|
|
|
+ to_string(_context.sourceIndices().at(*_location.sourceName))
|
2021-05-27 20:05:21 +00:00
|
|
|
+ ":"
|
|
|
|
+ to_string(_location.start)
|
2021-07-15 10:31:48 +00:00
|
|
|
+ ":"
|
2021-05-27 20:05:21 +00:00
|
|
|
+ to_string(_location.end);
|
|
|
|
}
|
|
|
|
|
|
|
|
string sourceLocationComment(ASTNode const& _node, IRGenerationContext const& _context)
|
|
|
|
{
|
|
|
|
return sourceLocationComment(_node.location(), _context);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|