2020-02-06 13:05:25 +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-14 20:44:46 +00:00
|
|
|
#include <libsolidity/codegen/ir/Common.h>
|
2020-02-06 13:05:25 +00:00
|
|
|
#include <libsolidity/codegen/ir/IRVariable.h>
|
|
|
|
#include <libsolidity/ast/AST.h>
|
|
|
|
#include <libsolutil/StringUtils.h>
|
|
|
|
|
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::frontend;
|
|
|
|
using namespace solidity::util;
|
|
|
|
|
|
|
|
IRVariable::IRVariable(std::string _baseName, Type const& _type):
|
|
|
|
m_baseName(std::move(_baseName)), m_type(_type)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
IRVariable::IRVariable(VariableDeclaration const& _declaration):
|
2020-05-14 20:44:46 +00:00
|
|
|
IRVariable(IRNames::localVariable(_declaration), *_declaration.annotation().type)
|
2020-02-06 13:05:25 +00:00
|
|
|
{
|
|
|
|
solAssert(!_declaration.isStateVariable(), "");
|
|
|
|
}
|
|
|
|
|
|
|
|
IRVariable::IRVariable(Expression const& _expression):
|
2020-05-14 20:44:46 +00:00
|
|
|
IRVariable(IRNames::localVariable(_expression), *_expression.annotation().type)
|
2020-02-06 13:05:25 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-08-14 19:37:31 +00:00
|
|
|
IRVariable IRVariable::part(std::string const& _name) const
|
2020-02-06 13:05:25 +00:00
|
|
|
{
|
|
|
|
for (auto const& [itemName, itemType]: m_type.stackItems())
|
|
|
|
if (itemName == _name)
|
|
|
|
{
|
|
|
|
solAssert(itemName.empty() || itemType, "");
|
|
|
|
return IRVariable{suffixedName(itemName), itemType ? *itemType : m_type};
|
|
|
|
}
|
2020-05-04 10:50:31 +00:00
|
|
|
solAssert(false, "Invalid stack item name: " + _name);
|
2020-02-06 13:05:25 +00:00
|
|
|
}
|
|
|
|
|
2020-08-21 12:27:00 +00:00
|
|
|
bool IRVariable::hasPart(std::string const& _name) const
|
|
|
|
{
|
|
|
|
for (auto const& [itemName, itemType]: m_type.stackItems())
|
|
|
|
if (itemName == _name)
|
|
|
|
{
|
|
|
|
solAssert(itemName.empty() || itemType, "");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-08-14 19:37:31 +00:00
|
|
|
std::vector<std::string> IRVariable::stackSlots() const
|
2020-02-06 13:05:25 +00:00
|
|
|
{
|
2023-08-14 19:37:31 +00:00
|
|
|
std::vector<std::string> result;
|
2020-02-06 13:05:25 +00:00
|
|
|
for (auto const& [itemName, itemType]: m_type.stackItems())
|
|
|
|
if (itemType)
|
|
|
|
{
|
|
|
|
solAssert(!itemName.empty(), "");
|
|
|
|
solAssert(m_type != *itemType, "");
|
|
|
|
result += IRVariable{suffixedName(itemName), *itemType}.stackSlots();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
solAssert(itemName.empty(), "");
|
|
|
|
result.emplace_back(m_baseName);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-08-14 19:37:31 +00:00
|
|
|
std::string IRVariable::commaSeparatedList() const
|
2020-02-06 13:05:25 +00:00
|
|
|
{
|
|
|
|
return joinHumanReadable(stackSlots());
|
|
|
|
}
|
|
|
|
|
2023-08-14 19:37:31 +00:00
|
|
|
std::string IRVariable::commaSeparatedListPrefixed() const
|
2020-02-06 13:05:25 +00:00
|
|
|
{
|
|
|
|
return joinHumanReadablePrefixed(stackSlots());
|
|
|
|
}
|
|
|
|
|
2023-08-14 19:37:31 +00:00
|
|
|
std::string IRVariable::name() const
|
2020-02-06 13:05:25 +00:00
|
|
|
{
|
|
|
|
solAssert(m_type.sizeOnStack() == 1, "");
|
|
|
|
auto const& [itemName, type] = m_type.stackItems().front();
|
2020-04-29 09:29:49 +00:00
|
|
|
solAssert(!type, "Expected null type for name " + itemName);
|
2020-02-06 13:05:25 +00:00
|
|
|
return suffixedName(itemName);
|
|
|
|
}
|
|
|
|
|
|
|
|
IRVariable IRVariable::tupleComponent(size_t _i) const
|
|
|
|
{
|
|
|
|
solAssert(
|
|
|
|
m_type.category() == Type::Category::Tuple,
|
|
|
|
"Requested tuple component of non-tuple IR variable."
|
|
|
|
);
|
2020-05-14 20:44:46 +00:00
|
|
|
return part(IRNames::tupleComponent(_i));
|
2020-02-06 13:05:25 +00:00
|
|
|
}
|
|
|
|
|
2023-08-14 19:37:31 +00:00
|
|
|
std::string IRVariable::suffixedName(std::string const& _suffix) const
|
2020-02-06 13:05:25 +00:00
|
|
|
{
|
|
|
|
if (_suffix.empty())
|
|
|
|
return m_baseName;
|
|
|
|
else
|
|
|
|
return m_baseName + '_' + _suffix;
|
|
|
|
}
|