From 16e58449ab08e0eee22c5d061c3770142b55eb57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20=C5=9Aliwak?= Date: Tue, 28 Apr 2020 13:14:45 +0200 Subject: [PATCH] ir/Common: Add YulArity struct --- libsolidity/codegen/ir/Common.cpp | 8 ++++++++ libsolidity/codegen/ir/Common.h | 29 +++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/libsolidity/codegen/ir/Common.cpp b/libsolidity/codegen/ir/Common.cpp index 8b895596a..dba6aee07 100644 --- a/libsolidity/codegen/ir/Common.cpp +++ b/libsolidity/codegen/ir/Common.cpp @@ -16,6 +16,7 @@ */ #include +#include #include @@ -23,6 +24,13 @@ using namespace std; using namespace solidity::util; using namespace solidity::frontend; +YulArity YulArity::fromType(FunctionType const& _functionType) +{ + return YulArity{ + TupleType(_functionType.parameterTypes()).sizeOnStack(), + TupleType(_functionType.returnParameterTypes()).sizeOnStack() + }; +} string IRNames::function(FunctionDefinition const& _function) { // @TODO previously, we had to distinguish creation context and runtime context, diff --git a/libsolidity/codegen/ir/Common.h b/libsolidity/codegen/ir/Common.h index 771dd0ab4..96e2a86ea 100644 --- a/libsolidity/codegen/ir/Common.h +++ b/libsolidity/codegen/ir/Common.h @@ -22,11 +22,28 @@ #include +#include #include namespace solidity::frontend { +/** + * Structure that describes arity and co-arity of a Yul function, i.e. the number of its inputs and outputs. + */ +struct YulArity +{ + explicit YulArity(size_t _in, size_t _out): in(_in), out(_out) {} + + static YulArity fromType(FunctionType const& _functionType); + + bool operator==(YulArity const& _other) const { return in == _other.in && out == _other.out; } + bool operator!=(YulArity const& _other) const { return !(*this == _other); } + + size_t in; /// Number of input parameters + size_t out; /// Number of output parameters +}; + struct IRNames { static std::string function(FunctionDefinition const& _function); @@ -45,3 +62,15 @@ struct IRNames }; } + +// Overloading std::less() makes it possible to use YulArity as a map key. We could define operator< +// instead but such an operator would be a bit ambiguous (e.g. YulArity{2, 2} would be be greater than +// YulArity{1, 10} in lexicographical order but the latter has greater total number of inputs and outputs). +template<> +struct std::less +{ + bool operator() (solidity::frontend::YulArity const& _lhs, solidity::frontend::YulArity const& _rhs) const + { + return _lhs.in < _rhs.in || (_lhs.in == _rhs.in && _lhs.out < _rhs.out); + } +};