2019-04-30 16:32:56 +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/>.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Generator for code that handles LValues.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libsolidity/codegen/ir/IRLValue.h>
|
|
|
|
|
|
|
|
#include <libsolidity/codegen/ir/IRGenerationContext.h>
|
|
|
|
#include <libsolidity/codegen/YulUtilFunctions.h>
|
2019-05-22 10:25:00 +00:00
|
|
|
#include <libsolidity/codegen/CompilerUtils.h>
|
2019-04-30 16:32:56 +00:00
|
|
|
#include <libsolidity/ast/AST.h>
|
|
|
|
|
|
|
|
#include <libdevcore/Whiskers.h>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace dev;
|
|
|
|
using namespace dev::solidity;
|
|
|
|
|
|
|
|
IRLocalVariable::IRLocalVariable(
|
|
|
|
IRGenerationContext& _context,
|
|
|
|
VariableDeclaration const& _varDecl
|
|
|
|
):
|
2019-05-13 17:04:22 +00:00
|
|
|
IRLValue(_context, _varDecl.annotation().type),
|
2019-04-30 16:32:56 +00:00
|
|
|
m_variableName(_context.localVariableName(_varDecl))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-05-13 17:04:22 +00:00
|
|
|
string IRLocalVariable::storeValue(string const& _value, Type const& _type) const
|
2019-04-30 16:32:56 +00:00
|
|
|
{
|
|
|
|
solAssert(_type == *m_type, "Storing different types - not necessarily a problem.");
|
2019-05-13 17:04:22 +00:00
|
|
|
return m_variableName + " := " + _value + "\n";
|
2019-04-30 16:32:56 +00:00
|
|
|
}
|
|
|
|
|
2019-05-22 10:25:00 +00:00
|
|
|
string IRLocalVariable::setToZero() const
|
|
|
|
{
|
|
|
|
return storeValue(m_context.utils().zeroValueFunction(*m_type) + "()", *m_type);
|
|
|
|
}
|
|
|
|
|
2019-04-30 16:32:56 +00:00
|
|
|
IRStorageItem::IRStorageItem(
|
|
|
|
IRGenerationContext& _context,
|
|
|
|
VariableDeclaration const& _varDecl
|
2019-06-17 13:26:40 +00:00
|
|
|
):
|
|
|
|
IRStorageItem(
|
2019-06-05 17:32:30 +00:00
|
|
|
_context,
|
|
|
|
*_varDecl.annotation().type,
|
|
|
|
_context.storageLocationOfVariable(_varDecl)
|
|
|
|
)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
IRStorageItem::IRStorageItem(
|
|
|
|
IRGenerationContext& _context,
|
|
|
|
Type const& _type,
|
|
|
|
std::pair<u256, unsigned> slot_offset
|
2019-06-17 13:26:40 +00:00
|
|
|
):
|
|
|
|
IRLValue(_context, &_type),
|
2019-06-05 17:32:30 +00:00
|
|
|
m_slot(toCompactHexWithPrefix(slot_offset.first)),
|
|
|
|
m_offset(slot_offset.second)
|
2019-04-30 16:32:56 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-05-02 10:48:34 +00:00
|
|
|
IRStorageItem::IRStorageItem(
|
|
|
|
IRGenerationContext& _context,
|
|
|
|
string _slot,
|
2019-06-05 17:32:30 +00:00
|
|
|
boost::variant<string, unsigned> _offset,
|
2019-05-02 10:48:34 +00:00
|
|
|
Type const& _type
|
|
|
|
):
|
2019-05-13 17:04:22 +00:00
|
|
|
IRLValue(_context, &_type),
|
2019-05-02 10:48:34 +00:00
|
|
|
m_slot(move(_slot)),
|
2019-06-05 17:32:30 +00:00
|
|
|
m_offset(std::move(_offset))
|
2019-05-02 10:48:34 +00:00
|
|
|
{
|
2019-06-05 17:32:30 +00:00
|
|
|
solAssert(!m_offset.empty(), "");
|
|
|
|
solAssert(!m_slot.empty(), "");
|
2019-05-02 10:48:34 +00:00
|
|
|
}
|
|
|
|
|
2019-04-30 16:32:56 +00:00
|
|
|
string IRStorageItem::retrieveValue() const
|
|
|
|
{
|
|
|
|
if (!m_type->isValueType())
|
|
|
|
return m_slot;
|
|
|
|
solUnimplementedAssert(m_type->category() != Type::Category::Function, "");
|
2019-06-05 17:32:30 +00:00
|
|
|
if (m_offset.type() == typeid(string))
|
|
|
|
return
|
2019-06-17 13:26:40 +00:00
|
|
|
m_context.utils().readFromStorageDynamic(*m_type, false) +
|
2019-06-05 17:32:30 +00:00
|
|
|
"(" +
|
|
|
|
m_slot +
|
|
|
|
", " +
|
|
|
|
boost::get<string>(m_offset) +
|
|
|
|
")";
|
|
|
|
else if (m_offset.type() == typeid(unsigned))
|
|
|
|
return
|
|
|
|
m_context.utils().readFromStorage(*m_type, boost::get<unsigned>(m_offset), false) +
|
|
|
|
"(" +
|
|
|
|
m_slot +
|
|
|
|
")";
|
|
|
|
|
|
|
|
solAssert(false, "");
|
2019-04-30 16:32:56 +00:00
|
|
|
}
|
|
|
|
|
2019-05-13 17:04:22 +00:00
|
|
|
string IRStorageItem::storeValue(string const& _value, Type const& _sourceType) const
|
2019-04-30 16:32:56 +00:00
|
|
|
{
|
|
|
|
if (m_type->isValueType())
|
|
|
|
solAssert(_sourceType == *m_type, "Different type, but might not be an error.");
|
|
|
|
|
2019-06-05 17:32:30 +00:00
|
|
|
boost::optional<unsigned> offset;
|
|
|
|
|
|
|
|
if (m_offset.type() == typeid(unsigned))
|
|
|
|
offset = get<unsigned>(m_offset);
|
|
|
|
|
|
|
|
return
|
|
|
|
m_context.utils().updateStorageValueFunction(*m_type, offset) +
|
|
|
|
"(" +
|
|
|
|
m_slot +
|
|
|
|
(m_offset.type() == typeid(string) ?
|
|
|
|
(", " + get<string>(m_offset)) :
|
|
|
|
""
|
|
|
|
) +
|
|
|
|
", " +
|
|
|
|
_value +
|
|
|
|
")\n";
|
2019-04-30 16:32:56 +00:00
|
|
|
}
|
|
|
|
|
2019-05-22 10:25:00 +00:00
|
|
|
string IRStorageItem::setToZero() const
|
|
|
|
{
|
2019-06-13 15:22:24 +00:00
|
|
|
solUnimplementedAssert(m_type->isValueType(), "");
|
|
|
|
return storeValue(m_context.utils().zeroValueFunction(*m_type) + "()", *m_type);
|
2019-05-22 10:25:00 +00:00
|
|
|
}
|
2019-06-06 12:07:40 +00:00
|
|
|
|
|
|
|
IRStorageArrayLength::IRStorageArrayLength(IRGenerationContext& _context, string _slot, Type const& _type, ArrayType const& _arrayType):
|
|
|
|
IRLValue(_context, &_type), m_arrayType(_arrayType), m_slot(move(_slot))
|
|
|
|
{
|
|
|
|
solAssert(*m_type == *TypeProvider::uint256(), "Must be uint256!");
|
|
|
|
}
|
|
|
|
|
|
|
|
string IRStorageArrayLength::retrieveValue() const
|
|
|
|
{
|
|
|
|
return m_context.utils().arrayLengthFunction(m_arrayType) + "(" + m_slot + ")\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
string IRStorageArrayLength::storeValue(std::string const& _value, Type const& _type) const
|
|
|
|
{
|
|
|
|
solAssert(_type == *m_type, "Different type, but might not be an error.");
|
|
|
|
|
|
|
|
return m_context.utils().resizeDynamicArrayFunction(m_arrayType) +
|
|
|
|
"(" +
|
|
|
|
m_slot +
|
|
|
|
", " +
|
|
|
|
_value +
|
|
|
|
")\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
string IRStorageArrayLength::setToZero() const
|
|
|
|
{
|
|
|
|
return storeValue("0", *TypeProvider::uint256());
|
|
|
|
}
|