2017-02-15 13:52:53 +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/>.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Analyzer part of inline assembly.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libsolidity/inlineasm/AsmAnalysis.h>
|
|
|
|
|
|
|
|
#include <libsolidity/inlineasm/AsmData.h>
|
2017-02-17 15:05:22 +00:00
|
|
|
#include <libsolidity/inlineasm/AsmScopeFiller.h>
|
|
|
|
#include <libsolidity/inlineasm/AsmScope.h>
|
2017-04-26 13:41:08 +00:00
|
|
|
#include <libsolidity/inlineasm/AsmAnalysisInfo.h>
|
2017-02-15 13:52:53 +00:00
|
|
|
|
2017-05-24 16:34:19 +00:00
|
|
|
#include <libsolidity/interface/ErrorReporter.h>
|
2017-02-15 13:52:53 +00:00
|
|
|
#include <libsolidity/interface/Utils.h>
|
|
|
|
|
|
|
|
#include <boost/range/adaptor/reversed.hpp>
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace dev;
|
|
|
|
using namespace dev::solidity;
|
|
|
|
using namespace dev::solidity::assembly;
|
|
|
|
|
2017-05-26 19:43:28 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
set<string> const builtinTypes{"bool", "u8", "s8", "u32", "s32", "u64", "s64", "u128", "s128", "u256", "s256"};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-02-17 15:05:22 +00:00
|
|
|
bool AsmAnalyzer::analyze(Block const& _block)
|
2017-02-15 13:52:53 +00:00
|
|
|
{
|
2017-05-11 13:26:35 +00:00
|
|
|
if (!(ScopeFiller(m_info, m_errorReporter))(_block))
|
2017-02-15 13:52:53 +00:00
|
|
|
return false;
|
2017-03-22 12:21:58 +00:00
|
|
|
|
2017-02-17 15:05:22 +00:00
|
|
|
return (*this)(_block);
|
2017-02-15 13:52:53 +00:00
|
|
|
}
|
|
|
|
|
2017-04-26 13:41:08 +00:00
|
|
|
bool AsmAnalyzer::operator()(Label const& _label)
|
2017-04-26 10:34:24 +00:00
|
|
|
{
|
2017-05-25 00:59:57 +00:00
|
|
|
solAssert(!m_julia, "");
|
2017-04-26 13:41:08 +00:00
|
|
|
m_info.stackHeightInfo[&_label] = m_stackHeight;
|
|
|
|
return true;
|
2017-04-26 10:34:24 +00:00
|
|
|
}
|
|
|
|
|
2017-03-22 12:21:58 +00:00
|
|
|
bool AsmAnalyzer::operator()(assembly::Instruction const& _instruction)
|
|
|
|
{
|
2017-05-25 00:59:57 +00:00
|
|
|
solAssert(!m_julia, "");
|
2017-03-22 12:21:58 +00:00
|
|
|
auto const& info = instructionInfo(_instruction.instruction);
|
|
|
|
m_stackHeight += info.ret - info.args;
|
2017-04-26 13:41:08 +00:00
|
|
|
m_info.stackHeightInfo[&_instruction] = m_stackHeight;
|
2017-03-22 12:21:58 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-02-17 15:05:22 +00:00
|
|
|
bool AsmAnalyzer::operator()(assembly::Literal const& _literal)
|
2017-02-16 22:44:22 +00:00
|
|
|
{
|
2017-05-24 23:27:48 +00:00
|
|
|
expectValidType(_literal.type, _literal.location);
|
2017-03-22 12:21:58 +00:00
|
|
|
++m_stackHeight;
|
2017-05-02 17:26:33 +00:00
|
|
|
if (_literal.kind == assembly::LiteralKind::String && _literal.value.size() > 32)
|
2017-02-17 15:05:22 +00:00
|
|
|
{
|
2017-05-11 13:26:35 +00:00
|
|
|
m_errorReporter.typeError(
|
|
|
|
_literal.location,
|
|
|
|
"String literal too long (" + boost::lexical_cast<std::string>(_literal.value.size()) + " > 32)"
|
|
|
|
);
|
2017-02-16 22:44:22 +00:00
|
|
|
return false;
|
2017-02-17 15:05:22 +00:00
|
|
|
}
|
2017-04-26 13:41:08 +00:00
|
|
|
m_info.stackHeightInfo[&_literal] = m_stackHeight;
|
2017-02-16 22:44:22 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-02-17 15:05:22 +00:00
|
|
|
bool AsmAnalyzer::operator()(assembly::Identifier const& _identifier)
|
2017-02-15 13:52:53 +00:00
|
|
|
{
|
2017-05-11 13:26:35 +00:00
|
|
|
size_t numErrorsBefore = m_errorReporter.errors().size();
|
2017-02-17 15:05:22 +00:00
|
|
|
bool success = true;
|
|
|
|
if (m_currentScope->lookup(_identifier.name, Scope::Visitor(
|
|
|
|
[&](Scope::Variable const& _var)
|
2017-02-17 11:03:55 +00:00
|
|
|
{
|
2017-02-17 15:05:22 +00:00
|
|
|
if (!_var.active)
|
|
|
|
{
|
2017-05-11 13:26:35 +00:00
|
|
|
m_errorReporter.declarationError(
|
|
|
|
_identifier.location,
|
|
|
|
"Variable " + _identifier.name + " used before it was declared."
|
|
|
|
);
|
2017-02-17 15:05:22 +00:00
|
|
|
success = false;
|
|
|
|
}
|
2017-03-22 12:21:58 +00:00
|
|
|
++m_stackHeight;
|
|
|
|
},
|
|
|
|
[&](Scope::Label const&)
|
|
|
|
{
|
|
|
|
++m_stackHeight;
|
2017-02-17 15:05:22 +00:00
|
|
|
},
|
|
|
|
[&](Scope::Function const&)
|
|
|
|
{
|
2017-05-11 13:26:35 +00:00
|
|
|
m_errorReporter.typeError(
|
|
|
|
_identifier.location,
|
|
|
|
"Function " + _identifier.name + " used without being called."
|
|
|
|
);
|
2017-02-17 15:05:22 +00:00
|
|
|
success = false;
|
2017-02-17 11:03:55 +00:00
|
|
|
}
|
2017-02-17 15:05:22 +00:00
|
|
|
)))
|
|
|
|
{
|
2017-02-17 11:03:55 +00:00
|
|
|
}
|
2017-03-22 12:21:58 +00:00
|
|
|
else
|
2017-02-15 13:52:53 +00:00
|
|
|
{
|
2017-03-22 12:21:58 +00:00
|
|
|
size_t stackSize(-1);
|
|
|
|
if (m_resolver)
|
2017-05-24 16:34:19 +00:00
|
|
|
{
|
|
|
|
bool insideFunction = m_currentScope->insideFunction();
|
|
|
|
stackSize = m_resolver(_identifier, julia::IdentifierContext::RValue, insideFunction);
|
|
|
|
}
|
2017-03-22 12:21:58 +00:00
|
|
|
if (stackSize == size_t(-1))
|
|
|
|
{
|
2017-04-25 11:15:42 +00:00
|
|
|
// Only add an error message if the callback did not do it.
|
2017-05-11 13:26:35 +00:00
|
|
|
if (numErrorsBefore == m_errorReporter.errors().size())
|
|
|
|
m_errorReporter.declarationError(_identifier.location, "Identifier not found.");
|
2017-03-22 12:21:58 +00:00
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
m_stackHeight += stackSize == size_t(-1) ? 1 : stackSize;
|
2017-02-15 13:52:53 +00:00
|
|
|
}
|
2017-04-26 13:41:08 +00:00
|
|
|
m_info.stackHeightInfo[&_identifier] = m_stackHeight;
|
2017-02-17 15:05:22 +00:00
|
|
|
return success;
|
2017-02-15 13:52:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AsmAnalyzer::operator()(FunctionalInstruction const& _instr)
|
|
|
|
{
|
2017-05-25 00:59:57 +00:00
|
|
|
solAssert(!m_julia, "");
|
2017-02-15 13:52:53 +00:00
|
|
|
bool success = true;
|
|
|
|
for (auto const& arg: _instr.arguments | boost::adaptors::reversed)
|
2017-06-01 12:16:01 +00:00
|
|
|
if (!expectExpression(arg))
|
2017-03-22 12:21:58 +00:00
|
|
|
success = false;
|
|
|
|
// Parser already checks that the number of arguments is correct.
|
2017-04-11 19:12:17 +00:00
|
|
|
solAssert(instructionInfo(_instr.instruction.instruction).args == int(_instr.arguments.size()), "");
|
2017-02-15 13:52:53 +00:00
|
|
|
if (!(*this)(_instr.instruction))
|
|
|
|
success = false;
|
2017-04-26 13:41:08 +00:00
|
|
|
m_info.stackHeightInfo[&_instr] = m_stackHeight;
|
2017-02-15 13:52:53 +00:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2017-05-24 00:02:11 +00:00
|
|
|
bool AsmAnalyzer::operator()(assembly::StackAssignment const& _assignment)
|
2017-02-15 13:52:53 +00:00
|
|
|
{
|
2017-05-25 00:59:57 +00:00
|
|
|
solAssert(!m_julia, "");
|
2017-04-26 10:34:24 +00:00
|
|
|
bool success = checkAssignment(_assignment.variableName, size_t(-1));
|
2017-04-26 13:41:08 +00:00
|
|
|
m_info.stackHeightInfo[&_assignment] = m_stackHeight;
|
2017-04-26 10:34:24 +00:00
|
|
|
return success;
|
2017-02-15 13:52:53 +00:00
|
|
|
}
|
|
|
|
|
2017-05-24 00:07:00 +00:00
|
|
|
bool AsmAnalyzer::operator()(assembly::Assignment const& _assignment)
|
2017-02-15 13:52:53 +00:00
|
|
|
{
|
2017-03-22 12:21:58 +00:00
|
|
|
int const stackHeight = m_stackHeight;
|
2017-02-17 15:05:22 +00:00
|
|
|
bool success = boost::apply_visitor(*this, *_assignment.value);
|
2017-03-22 12:21:58 +00:00
|
|
|
solAssert(m_stackHeight >= stackHeight, "Negative value size.");
|
|
|
|
if (!checkAssignment(_assignment.variableName, m_stackHeight - stackHeight))
|
2017-02-17 15:05:22 +00:00
|
|
|
success = false;
|
2017-04-26 13:41:08 +00:00
|
|
|
m_info.stackHeightInfo[&_assignment] = m_stackHeight;
|
2017-02-17 15:05:22 +00:00
|
|
|
return success;
|
2017-02-15 13:52:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AsmAnalyzer::operator()(assembly::VariableDeclaration const& _varDecl)
|
|
|
|
{
|
2017-05-05 17:35:40 +00:00
|
|
|
int const expectedItems = _varDecl.variables.size();
|
2017-04-21 16:01:41 +00:00
|
|
|
int const stackHeight = m_stackHeight;
|
2017-05-05 17:56:29 +00:00
|
|
|
bool success = boost::apply_visitor(*this, *_varDecl.value);
|
2017-05-05 20:10:05 +00:00
|
|
|
if ((m_stackHeight - stackHeight) != expectedItems)
|
|
|
|
{
|
2017-05-11 13:26:35 +00:00
|
|
|
m_errorReporter.declarationError(_varDecl.location, "Variable count mismatch.");
|
2017-05-05 20:10:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-05-05 17:35:40 +00:00
|
|
|
|
|
|
|
for (auto const& variable: _varDecl.variables)
|
2017-05-24 23:27:48 +00:00
|
|
|
{
|
|
|
|
expectValidType(variable.type, variable.location);
|
2017-05-05 17:35:40 +00:00
|
|
|
boost::get<Scope::Variable>(m_currentScope->identifiers.at(variable.name)).active = true;
|
2017-05-24 23:27:48 +00:00
|
|
|
}
|
2017-04-26 13:41:08 +00:00
|
|
|
m_info.stackHeightInfo[&_varDecl] = m_stackHeight;
|
2017-05-05 17:56:29 +00:00
|
|
|
return success;
|
2017-02-17 11:03:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AsmAnalyzer::operator()(assembly::FunctionDefinition const& _funDef)
|
2017-02-17 15:05:22 +00:00
|
|
|
{
|
2017-05-26 19:46:02 +00:00
|
|
|
Block const* virtualBlock = m_info.virtualBlocks.at(&_funDef).get();
|
|
|
|
solAssert(virtualBlock, "");
|
|
|
|
Scope& varScope = scope(virtualBlock);
|
2017-02-17 15:05:22 +00:00
|
|
|
for (auto const& var: _funDef.arguments + _funDef.returns)
|
2017-05-24 23:27:48 +00:00
|
|
|
{
|
|
|
|
expectValidType(var.type, var.location);
|
2017-05-26 19:46:02 +00:00
|
|
|
boost::get<Scope::Variable>(varScope.identifiers.at(var.name)).active = true;
|
2017-05-24 23:27:48 +00:00
|
|
|
}
|
2017-02-17 15:05:22 +00:00
|
|
|
|
2017-03-22 12:21:58 +00:00
|
|
|
int const stackHeight = m_stackHeight;
|
2017-05-31 10:15:43 +00:00
|
|
|
m_stackHeight = _funDef.arguments.size() + _funDef.returns.size();
|
2017-03-22 12:21:58 +00:00
|
|
|
|
|
|
|
bool success = (*this)(_funDef.body);
|
|
|
|
|
|
|
|
m_stackHeight = stackHeight;
|
2017-04-26 13:41:08 +00:00
|
|
|
m_info.stackHeightInfo[&_funDef] = m_stackHeight;
|
2017-03-22 12:21:58 +00:00
|
|
|
return success;
|
2017-02-17 15:05:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AsmAnalyzer::operator()(assembly::FunctionCall const& _funCall)
|
2017-02-17 11:03:55 +00:00
|
|
|
{
|
|
|
|
bool success = true;
|
2017-02-17 15:05:22 +00:00
|
|
|
size_t arguments = 0;
|
|
|
|
size_t returns = 0;
|
|
|
|
if (!m_currentScope->lookup(_funCall.functionName.name, Scope::Visitor(
|
|
|
|
[&](Scope::Variable const&)
|
|
|
|
{
|
2017-05-11 13:26:35 +00:00
|
|
|
m_errorReporter.typeError(
|
|
|
|
_funCall.functionName.location,
|
|
|
|
"Attempt to call variable instead of function."
|
|
|
|
);
|
2017-02-17 15:05:22 +00:00
|
|
|
success = false;
|
|
|
|
},
|
|
|
|
[&](Scope::Label const&)
|
|
|
|
{
|
2017-05-11 13:26:35 +00:00
|
|
|
m_errorReporter.typeError(
|
|
|
|
_funCall.functionName.location,
|
|
|
|
"Attempt to call label instead of function."
|
|
|
|
);
|
2017-02-17 15:05:22 +00:00
|
|
|
success = false;
|
|
|
|
},
|
|
|
|
[&](Scope::Function const& _fun)
|
|
|
|
{
|
2017-04-26 22:58:34 +00:00
|
|
|
/// TODO: compare types too
|
|
|
|
arguments = _fun.arguments.size();
|
|
|
|
returns = _fun.returns.size();
|
2017-02-17 15:05:22 +00:00
|
|
|
}
|
|
|
|
)))
|
2017-02-15 13:52:53 +00:00
|
|
|
{
|
2017-05-11 13:26:35 +00:00
|
|
|
m_errorReporter.declarationError(_funCall.functionName.location, "Function not found.");
|
2017-02-15 13:52:53 +00:00
|
|
|
success = false;
|
|
|
|
}
|
2017-02-17 15:05:22 +00:00
|
|
|
if (success)
|
|
|
|
{
|
|
|
|
if (_funCall.arguments.size() != arguments)
|
|
|
|
{
|
2017-05-11 13:26:35 +00:00
|
|
|
m_errorReporter.typeError(
|
|
|
|
_funCall.functionName.location,
|
|
|
|
"Expected " + boost::lexical_cast<string>(arguments) + " arguments but got " +
|
|
|
|
boost::lexical_cast<string>(_funCall.arguments.size()) + "."
|
|
|
|
);
|
2017-02-17 11:03:55 +00:00
|
|
|
success = false;
|
2017-02-17 15:05:22 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-17 11:03:55 +00:00
|
|
|
for (auto const& arg: _funCall.arguments | boost::adaptors::reversed)
|
2017-06-01 12:16:01 +00:00
|
|
|
if (!expectExpression(arg))
|
2017-02-17 11:03:55 +00:00
|
|
|
success = false;
|
2017-05-31 10:15:43 +00:00
|
|
|
m_stackHeight += int(returns) - int(arguments);
|
2017-04-26 13:41:08 +00:00
|
|
|
m_info.stackHeightInfo[&_funCall] = m_stackHeight;
|
2017-02-17 11:03:55 +00:00
|
|
|
return success;
|
2017-02-16 22:44:22 +00:00
|
|
|
}
|
|
|
|
|
2017-04-28 13:27:56 +00:00
|
|
|
bool AsmAnalyzer::operator()(Switch const& _switch)
|
|
|
|
{
|
2017-05-19 17:33:25 +00:00
|
|
|
bool success = true;
|
|
|
|
|
2017-06-01 12:16:01 +00:00
|
|
|
if (!expectExpression(*_switch.expression))
|
2017-05-19 17:33:25 +00:00
|
|
|
success = false;
|
2017-04-28 13:27:56 +00:00
|
|
|
|
2017-05-17 10:21:37 +00:00
|
|
|
set<tuple<LiteralKind, string>> cases;
|
2017-04-28 13:27:56 +00:00
|
|
|
for (auto const& _case: _switch.cases)
|
|
|
|
{
|
2017-05-17 10:21:37 +00:00
|
|
|
if (_case.value)
|
2017-04-28 13:27:56 +00:00
|
|
|
{
|
2017-06-07 09:40:47 +00:00
|
|
|
int const initialStackHeight = m_stackHeight;
|
|
|
|
// We cannot use "expectExpression" here because *_case.value is not a
|
|
|
|
// Statement and would be converted to a Statement otherwise.
|
|
|
|
if (!(*this)(*_case.value))
|
2017-05-19 17:33:25 +00:00
|
|
|
success = false;
|
2017-06-07 09:40:47 +00:00
|
|
|
expectDeposit(1, initialStackHeight, _case.value->location);
|
2017-05-17 10:21:37 +00:00
|
|
|
m_stackHeight--;
|
|
|
|
|
|
|
|
/// Note: the parser ensures there is only one default case
|
|
|
|
auto val = make_tuple(_case.value->kind, _case.value->value);
|
|
|
|
if (!cases.insert(val).second)
|
|
|
|
{
|
2017-05-11 13:26:35 +00:00
|
|
|
m_errorReporter.declarationError(
|
|
|
|
_case.location,
|
|
|
|
"Duplicate case defined"
|
|
|
|
);
|
2017-05-19 17:33:25 +00:00
|
|
|
success = false;
|
2017-05-17 10:21:37 +00:00
|
|
|
}
|
2017-04-28 13:27:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!(*this)(_case.body))
|
2017-05-19 17:33:25 +00:00
|
|
|
success = false;
|
2017-04-28 13:27:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_stackHeight--;
|
2017-05-24 16:34:19 +00:00
|
|
|
m_info.stackHeightInfo[&_switch] = m_stackHeight;
|
2017-04-28 13:27:56 +00:00
|
|
|
|
2017-05-19 17:33:25 +00:00
|
|
|
return success;
|
2017-04-28 13:27:56 +00:00
|
|
|
}
|
|
|
|
|
2017-02-15 13:52:53 +00:00
|
|
|
bool AsmAnalyzer::operator()(Block const& _block)
|
|
|
|
{
|
|
|
|
bool success = true;
|
2017-05-26 19:46:02 +00:00
|
|
|
auto previousScope = m_currentScope;
|
2017-02-17 11:03:55 +00:00
|
|
|
m_currentScope = &scope(&_block);
|
2017-02-15 13:52:53 +00:00
|
|
|
|
2017-05-26 19:46:02 +00:00
|
|
|
int const initialStackHeight = m_stackHeight;
|
2017-03-22 12:21:58 +00:00
|
|
|
|
2017-02-15 13:52:53 +00:00
|
|
|
for (auto const& s: _block.statements)
|
|
|
|
if (!boost::apply_visitor(*this, s))
|
|
|
|
success = false;
|
|
|
|
|
2017-03-22 12:21:58 +00:00
|
|
|
for (auto const& identifier: scope(&_block).identifiers)
|
|
|
|
if (identifier.second.type() == typeid(Scope::Variable))
|
|
|
|
--m_stackHeight;
|
|
|
|
|
2017-03-22 18:02:27 +00:00
|
|
|
int const stackDiff = m_stackHeight - initialStackHeight;
|
2017-03-22 12:21:58 +00:00
|
|
|
if (stackDiff != 0)
|
|
|
|
{
|
2017-05-11 13:26:35 +00:00
|
|
|
m_errorReporter.declarationError(
|
|
|
|
_block.location,
|
2017-03-22 12:21:58 +00:00
|
|
|
"Unbalanced stack at the end of a block: " +
|
|
|
|
(
|
|
|
|
stackDiff > 0 ?
|
|
|
|
to_string(stackDiff) + string(" surplus item(s).") :
|
|
|
|
to_string(-stackDiff) + string(" missing item(s).")
|
2017-05-11 13:26:35 +00:00
|
|
|
)
|
|
|
|
);
|
2017-03-22 12:21:58 +00:00
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
|
2017-04-26 13:41:08 +00:00
|
|
|
m_info.stackHeightInfo[&_block] = m_stackHeight;
|
2017-05-26 19:46:02 +00:00
|
|
|
m_currentScope = previousScope;
|
2017-02-15 13:52:53 +00:00
|
|
|
return success;
|
|
|
|
}
|
2017-02-17 11:03:55 +00:00
|
|
|
|
2017-06-01 12:16:01 +00:00
|
|
|
bool AsmAnalyzer::expectExpression(Statement const& _statement)
|
|
|
|
{
|
|
|
|
bool success = true;
|
|
|
|
int const initialHeight = m_stackHeight;
|
|
|
|
if (!boost::apply_visitor(*this, _statement))
|
|
|
|
success = false;
|
2017-06-07 09:40:47 +00:00
|
|
|
if (!expectDeposit(1, initialHeight, locationOf(_statement)))
|
|
|
|
success = false;
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AsmAnalyzer::expectDeposit(int _deposit, int _oldHeight, SourceLocation const& _location)
|
|
|
|
{
|
|
|
|
if (m_stackHeight - _oldHeight != _deposit)
|
2017-06-01 12:16:01 +00:00
|
|
|
{
|
|
|
|
m_errorReporter.typeError(
|
2017-06-07 09:40:47 +00:00
|
|
|
_location,
|
2017-06-01 13:56:49 +00:00
|
|
|
"Expected expression to return one item to the stack, but did return " +
|
2017-06-07 09:40:47 +00:00
|
|
|
boost::lexical_cast<string>(m_stackHeight - _oldHeight) +
|
2017-06-01 12:16:01 +00:00
|
|
|
" items."
|
|
|
|
);
|
2017-06-07 09:40:47 +00:00
|
|
|
return false;
|
2017-06-01 12:16:01 +00:00
|
|
|
}
|
2017-06-07 09:40:47 +00:00
|
|
|
return true;
|
2017-06-01 12:16:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-22 12:21:58 +00:00
|
|
|
bool AsmAnalyzer::checkAssignment(assembly::Identifier const& _variable, size_t _valueSize)
|
2017-02-17 11:03:55 +00:00
|
|
|
{
|
2017-03-22 12:21:58 +00:00
|
|
|
bool success = true;
|
2017-05-11 13:26:35 +00:00
|
|
|
size_t numErrorsBefore = m_errorReporter.errors().size();
|
2017-03-22 12:21:58 +00:00
|
|
|
size_t variableSize(-1);
|
2017-03-21 18:38:37 +00:00
|
|
|
if (Scope::Identifier const* var = m_currentScope->lookup(_variable.name))
|
2017-02-17 15:05:22 +00:00
|
|
|
{
|
|
|
|
// Check that it is a variable
|
2017-03-21 18:38:37 +00:00
|
|
|
if (var->type() != typeid(Scope::Variable))
|
2017-02-17 15:05:22 +00:00
|
|
|
{
|
2017-05-11 13:26:35 +00:00
|
|
|
m_errorReporter.typeError(_variable.location, "Assignment requires variable.");
|
2017-03-22 12:21:58 +00:00
|
|
|
success = false;
|
2017-02-17 15:05:22 +00:00
|
|
|
}
|
2017-03-22 12:21:58 +00:00
|
|
|
else if (!boost::get<Scope::Variable>(*var).active)
|
|
|
|
{
|
2017-05-11 13:26:35 +00:00
|
|
|
m_errorReporter.declarationError(
|
|
|
|
_variable.location,
|
|
|
|
"Variable " + _variable.name + " used before it was declared."
|
|
|
|
);
|
2017-03-22 12:21:58 +00:00
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
variableSize = 1;
|
2017-02-17 11:03:55 +00:00
|
|
|
}
|
2017-03-22 12:21:58 +00:00
|
|
|
else if (m_resolver)
|
2017-05-24 16:34:19 +00:00
|
|
|
{
|
|
|
|
bool insideFunction = m_currentScope->insideFunction();
|
|
|
|
variableSize = m_resolver(_variable, julia::IdentifierContext::LValue, insideFunction);
|
|
|
|
}
|
2017-03-22 12:21:58 +00:00
|
|
|
if (variableSize == size_t(-1))
|
2017-03-21 18:38:37 +00:00
|
|
|
{
|
2017-04-25 11:15:42 +00:00
|
|
|
// Only add message if the callback did not.
|
2017-05-11 13:26:35 +00:00
|
|
|
if (numErrorsBefore == m_errorReporter.errors().size())
|
|
|
|
m_errorReporter.declarationError(_variable.location, "Variable not found or variable not lvalue.");
|
2017-03-22 12:21:58 +00:00
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
if (_valueSize == size_t(-1))
|
|
|
|
_valueSize = variableSize == size_t(-1) ? 1 : variableSize;
|
|
|
|
|
|
|
|
m_stackHeight -= _valueSize;
|
|
|
|
|
|
|
|
if (_valueSize != variableSize && variableSize != size_t(-1))
|
|
|
|
{
|
2017-05-11 13:26:35 +00:00
|
|
|
m_errorReporter.typeError(
|
|
|
|
_variable.location,
|
2017-03-22 12:21:58 +00:00
|
|
|
"Variable size (" +
|
|
|
|
to_string(variableSize) +
|
|
|
|
") and value size (" +
|
|
|
|
to_string(_valueSize) +
|
2017-05-11 13:26:35 +00:00
|
|
|
") do not match."
|
|
|
|
);
|
2017-03-22 12:21:58 +00:00
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2017-02-17 11:03:55 +00:00
|
|
|
Scope& AsmAnalyzer::scope(Block const* _block)
|
|
|
|
{
|
2017-05-23 21:58:37 +00:00
|
|
|
solAssert(m_info.scopes.count(_block) == 1, "Scope requested but not present.");
|
2017-04-26 13:41:08 +00:00
|
|
|
auto scopePtr = m_info.scopes.at(_block);
|
2017-02-17 15:05:22 +00:00
|
|
|
solAssert(scopePtr, "Scope requested but not present.");
|
|
|
|
return *scopePtr;
|
2017-02-17 11:03:55 +00:00
|
|
|
}
|
2017-05-24 23:27:48 +00:00
|
|
|
|
|
|
|
void AsmAnalyzer::expectValidType(string const& type, SourceLocation const& _location)
|
|
|
|
{
|
2017-05-25 00:59:57 +00:00
|
|
|
if (!m_julia)
|
|
|
|
return;
|
2017-05-24 23:27:48 +00:00
|
|
|
|
2017-05-26 19:43:28 +00:00
|
|
|
if (!builtinTypes.count(type))
|
2017-05-11 13:26:35 +00:00
|
|
|
m_errorReporter.typeError(
|
|
|
|
_location,
|
|
|
|
"\"" + type + "\" is not a valid type (user defined types are not yet supported)."
|
|
|
|
);
|
2017-05-24 23:27:48 +00:00
|
|
|
}
|