2016-02-22 01:13:41 +00:00
|
|
|
/*
|
2016-11-18 23:13:20 +00:00
|
|
|
This file is part of solidity.
|
2016-02-22 01:13:41 +00:00
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is free software: you can redistribute it and/or modify
|
2016-02-22 01:13:41 +00:00
|
|
|
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.
|
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is distributed in the hope that it will be useful,
|
2016-02-22 01:13:41 +00:00
|
|
|
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
|
2016-11-18 23:13:20 +00:00
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
2016-02-22 01:13:41 +00:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @author Christian <c@ethdev.com>
|
|
|
|
* @date 2016
|
|
|
|
* Solidity inline assembly parser.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libsolidity/inlineasm/AsmParser.h>
|
2017-05-11 13:26:35 +00:00
|
|
|
#include <libsolidity/parsing/Scanner.h>
|
|
|
|
#include <libsolidity/interface/ErrorReporter.h>
|
2016-02-22 01:13:41 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace dev;
|
|
|
|
using namespace dev::solidity;
|
2016-03-01 21:56:39 +00:00
|
|
|
using namespace dev::solidity::assembly;
|
2016-02-22 01:13:41 +00:00
|
|
|
|
2016-03-01 21:56:39 +00:00
|
|
|
shared_ptr<assembly::Block> Parser::parse(std::shared_ptr<Scanner> const& _scanner)
|
2016-02-22 01:13:41 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
m_scanner = _scanner;
|
2016-04-18 11:47:40 +00:00
|
|
|
return make_shared<Block>(parseBlock());
|
2016-02-22 01:13:41 +00:00
|
|
|
}
|
|
|
|
catch (FatalError const&)
|
|
|
|
{
|
2017-05-11 13:26:35 +00:00
|
|
|
if (m_errorReporter.errors().empty())
|
2016-02-22 01:13:41 +00:00
|
|
|
throw; // Something is weird here, rather throw again.
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-03-01 21:56:39 +00:00
|
|
|
assembly::Block Parser::parseBlock()
|
2016-02-22 01:13:41 +00:00
|
|
|
{
|
2016-04-18 11:47:40 +00:00
|
|
|
assembly::Block block = createWithLocation<Block>();
|
2016-02-22 01:13:41 +00:00
|
|
|
expectToken(Token::LBrace);
|
2017-05-24 23:13:51 +00:00
|
|
|
while (currentToken() != Token::RBrace)
|
2016-02-22 01:13:41 +00:00
|
|
|
block.statements.emplace_back(parseStatement());
|
2016-04-18 11:47:40 +00:00
|
|
|
block.location.end = endPosition();
|
2017-05-24 23:13:51 +00:00
|
|
|
advance();
|
2016-02-22 01:13:41 +00:00
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
2016-03-01 21:56:39 +00:00
|
|
|
assembly::Statement Parser::parseStatement()
|
2016-02-22 01:13:41 +00:00
|
|
|
{
|
2017-05-24 23:13:51 +00:00
|
|
|
switch (currentToken())
|
2016-02-22 01:13:41 +00:00
|
|
|
{
|
|
|
|
case Token::Let:
|
|
|
|
return parseVariableDeclaration();
|
2017-01-31 22:59:41 +00:00
|
|
|
case Token::Function:
|
|
|
|
return parseFunctionDefinition();
|
2016-02-22 01:13:41 +00:00
|
|
|
case Token::LBrace:
|
|
|
|
return parseBlock();
|
2017-04-28 13:27:56 +00:00
|
|
|
case Token::Switch:
|
|
|
|
{
|
|
|
|
assembly::Switch _switch = createWithLocation<assembly::Switch>();
|
|
|
|
m_scanner->next();
|
|
|
|
_switch.expression = make_shared<Statement>(parseExpression());
|
2017-05-24 13:33:10 +00:00
|
|
|
if (_switch.expression->type() == typeid(assembly::Instruction))
|
|
|
|
fatalParserError("Instructions are not supported as expressions for switch.");
|
2017-04-28 13:27:56 +00:00
|
|
|
while (m_scanner->currentToken() == Token::Case)
|
|
|
|
_switch.cases.emplace_back(parseCase());
|
|
|
|
if (m_scanner->currentToken() == Token::Default)
|
2017-05-19 17:04:40 +00:00
|
|
|
_switch.cases.emplace_back(parseCase());
|
2017-05-26 00:49:32 +00:00
|
|
|
if (m_scanner->currentToken() == Token::Default)
|
|
|
|
fatalParserError("Only one default case allowed.");
|
|
|
|
else if (m_scanner->currentToken() == Token::Case)
|
|
|
|
fatalParserError("Case not allowed after default case.");
|
2017-04-28 13:27:56 +00:00
|
|
|
if (_switch.cases.size() == 0)
|
|
|
|
fatalParserError("Switch statement without any cases.");
|
|
|
|
_switch.location.end = _switch.cases.back().body.location.end;
|
|
|
|
return _switch;
|
|
|
|
}
|
2017-04-28 13:33:52 +00:00
|
|
|
case Token::For:
|
|
|
|
return parseForLoop();
|
2016-02-22 01:13:41 +00:00
|
|
|
case Token::Assign:
|
|
|
|
{
|
2017-04-27 18:20:53 +00:00
|
|
|
if (m_julia)
|
|
|
|
break;
|
2017-05-24 00:02:11 +00:00
|
|
|
assembly::StackAssignment assignment = createWithLocation<assembly::StackAssignment>();
|
2017-05-24 23:13:51 +00:00
|
|
|
advance();
|
2016-02-22 01:13:41 +00:00
|
|
|
expectToken(Token::Colon);
|
2016-04-18 11:47:40 +00:00
|
|
|
assignment.variableName.location = location();
|
2017-05-24 23:13:51 +00:00
|
|
|
assignment.variableName.name = currentLiteral();
|
2017-04-27 08:58:52 +00:00
|
|
|
if (!m_julia && instructions().count(assignment.variableName.name))
|
2017-01-26 12:52:02 +00:00
|
|
|
fatalParserError("Identifier expected, got instruction name.");
|
2016-04-18 11:47:40 +00:00
|
|
|
assignment.location.end = endPosition();
|
2016-02-22 01:13:41 +00:00
|
|
|
expectToken(Token::Identifier);
|
2016-04-18 11:47:40 +00:00
|
|
|
return assignment;
|
2016-02-22 01:13:41 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Options left:
|
|
|
|
// Simple instruction (might turn into functional),
|
|
|
|
// literal,
|
|
|
|
// identifier (might turn into label or functional assignment)
|
2017-05-02 08:25:12 +00:00
|
|
|
Statement statement(parseElementaryOperation(false));
|
2017-05-24 23:13:51 +00:00
|
|
|
switch (currentToken())
|
2016-02-22 01:13:41 +00:00
|
|
|
{
|
|
|
|
case Token::LParen:
|
2017-05-24 00:26:17 +00:00
|
|
|
return parseCall(std::move(statement));
|
2016-02-22 01:13:41 +00:00
|
|
|
case Token::Colon:
|
|
|
|
{
|
2016-03-01 21:56:39 +00:00
|
|
|
if (statement.type() != typeid(assembly::Identifier))
|
2016-02-22 01:13:41 +00:00
|
|
|
fatalParserError("Label name / variable name must precede \":\".");
|
2016-04-18 11:47:40 +00:00
|
|
|
assembly::Identifier const& identifier = boost::get<assembly::Identifier>(statement);
|
2017-05-24 23:13:51 +00:00
|
|
|
advance();
|
2016-10-06 13:32:11 +00:00
|
|
|
// identifier:=: should be parsed as identifier: =: (i.e. a label),
|
|
|
|
// while identifier:= (being followed by a non-colon) as identifier := (assignment).
|
2017-05-24 23:13:51 +00:00
|
|
|
if (currentToken() == Token::Assign && peekNextToken() != Token::Colon)
|
2016-02-22 01:13:41 +00:00
|
|
|
{
|
2017-05-24 00:07:00 +00:00
|
|
|
assembly::Assignment assignment = createWithLocation<assembly::Assignment>(identifier.location);
|
2017-04-27 08:58:52 +00:00
|
|
|
if (!m_julia && instructions().count(identifier.name))
|
2017-01-25 16:29:06 +00:00
|
|
|
fatalParserError("Cannot use instruction names for identifier names.");
|
2017-05-24 23:13:51 +00:00
|
|
|
advance();
|
2017-05-24 00:07:00 +00:00
|
|
|
assignment.variableName = identifier;
|
|
|
|
assignment.value.reset(new Statement(parseExpression()));
|
|
|
|
assignment.location.end = locationOf(*assignment.value).end;
|
|
|
|
return assignment;
|
2016-02-22 01:13:41 +00:00
|
|
|
}
|
|
|
|
else
|
2016-04-18 11:47:40 +00:00
|
|
|
{
|
2016-02-22 01:13:41 +00:00
|
|
|
// label
|
2017-05-01 16:40:37 +00:00
|
|
|
if (m_julia)
|
|
|
|
fatalParserError("Labels are not supported.");
|
2016-04-18 11:47:40 +00:00
|
|
|
Label label = createWithLocation<Label>(identifier.location);
|
|
|
|
label.name = identifier.name;
|
|
|
|
return label;
|
|
|
|
}
|
2016-02-22 01:13:41 +00:00
|
|
|
}
|
|
|
|
default:
|
2017-05-02 08:24:35 +00:00
|
|
|
if (m_julia)
|
|
|
|
fatalParserError("Call or assignment expected.");
|
2016-02-22 01:13:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return statement;
|
|
|
|
}
|
|
|
|
|
2017-05-19 17:04:40 +00:00
|
|
|
assembly::Case Parser::parseCase()
|
2017-04-28 13:27:56 +00:00
|
|
|
{
|
|
|
|
assembly::Case _case = createWithLocation<assembly::Case>();
|
2017-05-19 17:04:40 +00:00
|
|
|
if (m_scanner->currentToken() == Token::Default)
|
|
|
|
m_scanner->next();
|
|
|
|
else if (m_scanner->currentToken() == Token::Case)
|
2017-04-28 13:27:56 +00:00
|
|
|
{
|
2017-05-19 17:04:40 +00:00
|
|
|
m_scanner->next();
|
2017-05-17 10:21:37 +00:00
|
|
|
assembly::Statement statement = parseElementaryOperation();
|
|
|
|
if (statement.type() != typeid(assembly::Literal))
|
|
|
|
fatalParserError("Literal expected.");
|
|
|
|
_case.value = make_shared<Literal>(std::move(boost::get<assembly::Literal>(statement)));
|
2017-04-28 13:27:56 +00:00
|
|
|
}
|
2017-05-19 17:04:40 +00:00
|
|
|
else
|
|
|
|
fatalParserError("Case or default case expected.");
|
2017-04-28 13:27:56 +00:00
|
|
|
_case.body = parseBlock();
|
|
|
|
_case.location.end = _case.body.location.end;
|
|
|
|
return _case;
|
|
|
|
}
|
|
|
|
|
2017-04-28 13:33:52 +00:00
|
|
|
assembly::ForLoop Parser::parseForLoop()
|
|
|
|
{
|
|
|
|
ForLoop forLoop = createWithLocation<ForLoop>();
|
|
|
|
expectToken(Token::For);
|
|
|
|
forLoop.pre = parseBlock();
|
|
|
|
forLoop.condition = make_shared<Statement>(parseExpression());
|
|
|
|
if (forLoop.condition->type() == typeid(assembly::Instruction))
|
|
|
|
fatalParserError("Instructions are not supported as conditions for the for statement.");
|
|
|
|
forLoop.post = parseBlock();
|
|
|
|
forLoop.body = parseBlock();
|
|
|
|
forLoop.location.end = forLoop.body.location.end;
|
|
|
|
return forLoop;
|
|
|
|
}
|
|
|
|
|
2016-03-01 21:56:39 +00:00
|
|
|
assembly::Statement Parser::parseExpression()
|
2016-02-22 01:13:41 +00:00
|
|
|
{
|
2016-03-01 21:56:39 +00:00
|
|
|
Statement operation = parseElementaryOperation(true);
|
2017-06-13 16:38:55 +00:00
|
|
|
if (operation.type() == typeid(Instruction))
|
|
|
|
{
|
|
|
|
Instruction const& instr = boost::get<Instruction>(operation);
|
|
|
|
int args = instructionInfo(instr.instruction).args;
|
|
|
|
if (args > 0 && currentToken() != Token::LParen)
|
|
|
|
fatalParserError(string(
|
|
|
|
"Expected token \"(\" (\"" +
|
|
|
|
instructionNames().at(instr.instruction) +
|
|
|
|
"\" expects " +
|
|
|
|
boost::lexical_cast<string>(args) +
|
|
|
|
" arguments)"
|
|
|
|
));
|
|
|
|
}
|
2017-05-24 23:13:51 +00:00
|
|
|
if (currentToken() == Token::LParen)
|
2017-05-24 00:26:17 +00:00
|
|
|
return parseCall(std::move(operation));
|
2016-02-22 01:13:41 +00:00
|
|
|
else
|
|
|
|
return operation;
|
|
|
|
}
|
|
|
|
|
2017-01-25 16:24:50 +00:00
|
|
|
std::map<string, dev::solidity::Instruction> const& Parser::instructions()
|
2016-02-22 01:13:41 +00:00
|
|
|
{
|
|
|
|
// Allowed instructions, lowercase names.
|
2016-04-02 12:56:43 +00:00
|
|
|
static map<string, dev::solidity::Instruction> s_instructions;
|
2016-02-22 01:13:41 +00:00
|
|
|
if (s_instructions.empty())
|
2016-10-05 10:59:16 +00:00
|
|
|
{
|
2016-04-02 12:56:43 +00:00
|
|
|
for (auto const& instruction: solidity::c_instructions)
|
2016-02-22 01:13:41 +00:00
|
|
|
{
|
|
|
|
if (
|
2016-04-02 12:56:43 +00:00
|
|
|
instruction.second == solidity::Instruction::JUMPDEST ||
|
|
|
|
(solidity::Instruction::PUSH1 <= instruction.second && instruction.second <= solidity::Instruction::PUSH32)
|
2016-02-22 01:13:41 +00:00
|
|
|
)
|
|
|
|
continue;
|
|
|
|
string name = instruction.first;
|
|
|
|
transform(name.begin(), name.end(), name.begin(), [](unsigned char _c) { return tolower(_c); });
|
|
|
|
s_instructions[name] = instruction.second;
|
|
|
|
}
|
|
|
|
|
2017-02-06 18:35:18 +00:00
|
|
|
// add alias for suicide
|
|
|
|
s_instructions["suicide"] = solidity::Instruction::SELFDESTRUCT;
|
2017-05-11 08:58:45 +00:00
|
|
|
// add alis for sha3
|
|
|
|
s_instructions["sha3"] = solidity::Instruction::KECCAK256;
|
2016-10-05 10:59:16 +00:00
|
|
|
}
|
2017-01-25 10:33:09 +00:00
|
|
|
return s_instructions;
|
|
|
|
}
|
|
|
|
|
2017-06-09 12:44:38 +00:00
|
|
|
std::map<dev::solidity::Instruction, string> const& Parser::instructionNames()
|
|
|
|
{
|
|
|
|
static map<dev::solidity::Instruction, string> s_instructionNames;
|
|
|
|
if (s_instructionNames.empty())
|
|
|
|
{
|
|
|
|
for (auto const& instr: instructions())
|
|
|
|
s_instructionNames[instr.second] = instr.first;
|
|
|
|
// set the ambiguous instructions to a clear default
|
|
|
|
s_instructionNames[solidity::Instruction::SELFDESTRUCT] = "selfdestruct";
|
|
|
|
s_instructionNames[solidity::Instruction::KECCAK256] = "keccak256";
|
|
|
|
}
|
|
|
|
return s_instructionNames;
|
|
|
|
}
|
|
|
|
|
2017-01-25 10:33:09 +00:00
|
|
|
assembly::Statement Parser::parseElementaryOperation(bool _onlySinglePusher)
|
|
|
|
{
|
2016-04-18 11:47:40 +00:00
|
|
|
Statement ret;
|
2017-05-24 23:13:51 +00:00
|
|
|
switch (currentToken())
|
2016-02-22 01:13:41 +00:00
|
|
|
{
|
|
|
|
case Token::Identifier:
|
2016-03-01 21:56:39 +00:00
|
|
|
case Token::Return:
|
2016-04-05 12:57:40 +00:00
|
|
|
case Token::Byte:
|
2016-10-05 10:47:56 +00:00
|
|
|
case Token::Address:
|
2016-02-22 01:13:41 +00:00
|
|
|
{
|
2016-03-01 21:56:39 +00:00
|
|
|
string literal;
|
2017-05-24 23:13:51 +00:00
|
|
|
if (currentToken() == Token::Return)
|
2016-03-01 21:56:39 +00:00
|
|
|
literal = "return";
|
2017-05-24 23:13:51 +00:00
|
|
|
else if (currentToken() == Token::Byte)
|
2016-04-05 12:57:40 +00:00
|
|
|
literal = "byte";
|
2017-05-24 23:13:51 +00:00
|
|
|
else if (currentToken() == Token::Address)
|
2016-10-05 10:47:56 +00:00
|
|
|
literal = "address";
|
2016-03-01 21:56:39 +00:00
|
|
|
else
|
2017-05-24 23:13:51 +00:00
|
|
|
literal = currentLiteral();
|
2016-02-22 01:13:41 +00:00
|
|
|
// first search the set of instructions.
|
2017-04-27 08:58:52 +00:00
|
|
|
if (!m_julia && instructions().count(literal))
|
2016-02-22 01:13:41 +00:00
|
|
|
{
|
2017-01-25 16:29:06 +00:00
|
|
|
dev::solidity::Instruction const& instr = instructions().at(literal);
|
2016-02-22 01:13:41 +00:00
|
|
|
if (_onlySinglePusher)
|
|
|
|
{
|
2016-04-04 11:27:09 +00:00
|
|
|
InstructionInfo info = dev::solidity::instructionInfo(instr);
|
2016-02-22 01:13:41 +00:00
|
|
|
if (info.ret != 1)
|
2017-06-09 12:44:38 +00:00
|
|
|
fatalParserError("Instruction \"" + literal + "\" not allowed in this context.");
|
2016-02-22 01:13:41 +00:00
|
|
|
}
|
2016-04-18 11:47:40 +00:00
|
|
|
ret = Instruction{location(), instr};
|
2016-02-22 01:13:41 +00:00
|
|
|
}
|
|
|
|
else
|
2016-04-18 11:47:40 +00:00
|
|
|
ret = Identifier{location(), literal};
|
2017-05-24 23:13:51 +00:00
|
|
|
advance();
|
2016-02-22 01:13:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Token::StringLiteral:
|
|
|
|
case Token::Number:
|
2017-05-02 17:26:33 +00:00
|
|
|
case Token::TrueLiteral:
|
|
|
|
case Token::FalseLiteral:
|
2016-02-22 01:13:41 +00:00
|
|
|
{
|
2017-05-02 17:26:33 +00:00
|
|
|
LiteralKind kind = LiteralKind::Number;
|
2017-05-24 23:13:51 +00:00
|
|
|
switch (currentToken())
|
2017-05-02 17:26:33 +00:00
|
|
|
{
|
|
|
|
case Token::StringLiteral:
|
|
|
|
kind = LiteralKind::String;
|
|
|
|
break;
|
|
|
|
case Token::Number:
|
|
|
|
kind = LiteralKind::Number;
|
|
|
|
break;
|
|
|
|
case Token::TrueLiteral:
|
|
|
|
case Token::FalseLiteral:
|
|
|
|
kind = LiteralKind::Boolean;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-04-26 22:58:34 +00:00
|
|
|
Literal literal{
|
2016-04-18 11:47:40 +00:00
|
|
|
location(),
|
2017-05-02 17:26:33 +00:00
|
|
|
kind,
|
2017-05-24 23:13:51 +00:00
|
|
|
currentLiteral(),
|
2017-04-26 22:58:34 +00:00
|
|
|
""
|
2016-02-22 01:13:41 +00:00
|
|
|
};
|
2017-05-24 23:13:51 +00:00
|
|
|
advance();
|
2017-04-26 22:58:34 +00:00
|
|
|
if (m_julia)
|
|
|
|
{
|
|
|
|
expectToken(Token::Colon);
|
|
|
|
literal.location.end = endPosition();
|
|
|
|
literal.type = expectAsmIdentifier();
|
|
|
|
}
|
2017-05-02 17:26:33 +00:00
|
|
|
else if (kind == LiteralKind::Boolean)
|
|
|
|
fatalParserError("True and false are not valid literals.");
|
2017-04-26 22:58:34 +00:00
|
|
|
ret = std::move(literal);
|
2016-04-18 11:47:40 +00:00
|
|
|
break;
|
2016-02-22 01:13:41 +00:00
|
|
|
}
|
|
|
|
default:
|
2017-05-02 08:25:12 +00:00
|
|
|
fatalParserError(
|
|
|
|
m_julia ?
|
|
|
|
"Literal or identifier expected." :
|
2017-05-26 08:41:53 +00:00
|
|
|
"Literal, identifier or instruction expected."
|
2017-05-02 08:25:12 +00:00
|
|
|
);
|
2016-02-22 01:13:41 +00:00
|
|
|
}
|
2016-04-18 11:47:40 +00:00
|
|
|
return ret;
|
2016-02-22 01:13:41 +00:00
|
|
|
}
|
|
|
|
|
2016-03-01 21:56:39 +00:00
|
|
|
assembly::VariableDeclaration Parser::parseVariableDeclaration()
|
2016-02-22 01:13:41 +00:00
|
|
|
{
|
2016-04-18 11:47:40 +00:00
|
|
|
VariableDeclaration varDecl = createWithLocation<VariableDeclaration>();
|
2016-02-22 01:13:41 +00:00
|
|
|
expectToken(Token::Let);
|
2017-05-05 17:35:40 +00:00
|
|
|
while (true)
|
|
|
|
{
|
2017-05-26 08:41:53 +00:00
|
|
|
varDecl.variables.emplace_back(parseTypedName());
|
2017-05-24 23:13:51 +00:00
|
|
|
if (currentToken() == Token::Comma)
|
2017-05-05 17:35:40 +00:00
|
|
|
expectToken(Token::Comma);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2017-05-05 15:46:26 +00:00
|
|
|
if (currentToken() == Token::Colon)
|
|
|
|
{
|
|
|
|
expectToken(Token::Colon);
|
|
|
|
expectToken(Token::Assign);
|
|
|
|
varDecl.value.reset(new Statement(parseExpression()));
|
|
|
|
varDecl.location.end = locationOf(*varDecl.value).end;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
varDecl.location.end = varDecl.variables.back().location.end;
|
2016-04-18 11:47:40 +00:00
|
|
|
return varDecl;
|
2016-02-22 01:13:41 +00:00
|
|
|
}
|
|
|
|
|
2017-01-31 22:59:41 +00:00
|
|
|
assembly::FunctionDefinition Parser::parseFunctionDefinition()
|
|
|
|
{
|
|
|
|
FunctionDefinition funDef = createWithLocation<FunctionDefinition>();
|
|
|
|
expectToken(Token::Function);
|
|
|
|
funDef.name = expectAsmIdentifier();
|
|
|
|
expectToken(Token::LParen);
|
2017-05-24 23:13:51 +00:00
|
|
|
while (currentToken() != Token::RParen)
|
2017-01-31 22:59:41 +00:00
|
|
|
{
|
2017-04-26 22:58:34 +00:00
|
|
|
funDef.arguments.emplace_back(parseTypedName());
|
2017-05-24 23:13:51 +00:00
|
|
|
if (currentToken() == Token::RParen)
|
2017-01-31 22:59:41 +00:00
|
|
|
break;
|
|
|
|
expectToken(Token::Comma);
|
|
|
|
}
|
|
|
|
expectToken(Token::RParen);
|
2017-05-24 23:13:51 +00:00
|
|
|
if (currentToken() == Token::Sub)
|
2017-01-31 22:59:41 +00:00
|
|
|
{
|
|
|
|
expectToken(Token::Sub);
|
|
|
|
expectToken(Token::GreaterThan);
|
|
|
|
while (true)
|
|
|
|
{
|
2017-04-26 22:58:34 +00:00
|
|
|
funDef.returns.emplace_back(parseTypedName());
|
2017-05-24 23:13:51 +00:00
|
|
|
if (currentToken() == Token::LBrace)
|
2017-01-31 22:59:41 +00:00
|
|
|
break;
|
|
|
|
expectToken(Token::Comma);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
funDef.body = parseBlock();
|
|
|
|
funDef.location.end = funDef.body.location.end;
|
|
|
|
return funDef;
|
|
|
|
}
|
|
|
|
|
2017-05-24 00:26:17 +00:00
|
|
|
assembly::Statement Parser::parseCall(assembly::Statement&& _instruction)
|
2016-02-22 01:13:41 +00:00
|
|
|
{
|
2017-02-01 20:20:21 +00:00
|
|
|
if (_instruction.type() == typeid(Instruction))
|
2016-02-22 01:13:41 +00:00
|
|
|
{
|
2017-04-27 08:58:52 +00:00
|
|
|
solAssert(!m_julia, "Instructions are invalid in JULIA");
|
2017-02-01 20:20:21 +00:00
|
|
|
FunctionalInstruction ret;
|
|
|
|
ret.instruction = std::move(boost::get<Instruction>(_instruction));
|
|
|
|
ret.location = ret.instruction.location;
|
|
|
|
solidity::Instruction instr = ret.instruction.instruction;
|
|
|
|
InstructionInfo instrInfo = instructionInfo(instr);
|
|
|
|
if (solidity::Instruction::DUP1 <= instr && instr <= solidity::Instruction::DUP16)
|
|
|
|
fatalParserError("DUPi instructions not allowed for functional notation");
|
|
|
|
if (solidity::Instruction::SWAP1 <= instr && instr <= solidity::Instruction::SWAP16)
|
|
|
|
fatalParserError("SWAPi instructions not allowed for functional notation");
|
|
|
|
expectToken(Token::LParen);
|
|
|
|
unsigned args = unsigned(instrInfo.args);
|
|
|
|
for (unsigned i = 0; i < args; ++i)
|
2016-04-18 11:47:40 +00:00
|
|
|
{
|
2017-05-24 12:13:05 +00:00
|
|
|
/// check for premature closing parentheses
|
2017-05-24 23:13:51 +00:00
|
|
|
if (currentToken() == Token::RParen)
|
2017-05-24 12:13:05 +00:00
|
|
|
fatalParserError(string(
|
2017-06-09 12:44:38 +00:00
|
|
|
"Expected expression (\"" +
|
|
|
|
instructionNames().at(instr) +
|
|
|
|
"\" expects " +
|
2017-05-24 12:13:05 +00:00
|
|
|
boost::lexical_cast<string>(args) +
|
2017-05-24 12:27:58 +00:00
|
|
|
" arguments)"
|
2017-05-24 12:13:05 +00:00
|
|
|
));
|
|
|
|
|
2017-02-01 20:20:21 +00:00
|
|
|
ret.arguments.emplace_back(parseExpression());
|
|
|
|
if (i != args - 1)
|
|
|
|
{
|
2017-05-24 23:13:51 +00:00
|
|
|
if (currentToken() != Token::Comma)
|
2017-02-01 20:20:21 +00:00
|
|
|
fatalParserError(string(
|
2017-06-09 12:44:38 +00:00
|
|
|
"Expected comma (\"" +
|
|
|
|
instructionNames().at(instr) +
|
|
|
|
"\" expects " +
|
2017-02-01 20:20:21 +00:00
|
|
|
boost::lexical_cast<string>(args) +
|
|
|
|
" arguments)"
|
|
|
|
));
|
|
|
|
else
|
2017-05-24 23:13:51 +00:00
|
|
|
advance();
|
2017-02-01 20:20:21 +00:00
|
|
|
}
|
2016-04-18 11:47:40 +00:00
|
|
|
}
|
2017-02-01 20:20:21 +00:00
|
|
|
ret.location.end = endPosition();
|
2017-05-24 23:13:51 +00:00
|
|
|
if (currentToken() == Token::Comma)
|
2017-06-09 12:44:38 +00:00
|
|
|
fatalParserError(string(
|
|
|
|
"Expected ')' (\"" +
|
|
|
|
instructionNames().at(instr) +
|
|
|
|
"\" expects " +
|
|
|
|
boost::lexical_cast<string>(args) +
|
|
|
|
" arguments)"
|
|
|
|
));
|
2017-02-01 20:20:21 +00:00
|
|
|
expectToken(Token::RParen);
|
|
|
|
return ret;
|
2016-02-22 01:13:41 +00:00
|
|
|
}
|
2017-02-01 20:20:21 +00:00
|
|
|
else if (_instruction.type() == typeid(Identifier))
|
|
|
|
{
|
|
|
|
FunctionCall ret;
|
|
|
|
ret.functionName = std::move(boost::get<Identifier>(_instruction));
|
|
|
|
ret.location = ret.functionName.location;
|
|
|
|
expectToken(Token::LParen);
|
2017-05-24 23:13:51 +00:00
|
|
|
while (currentToken() != Token::RParen)
|
2017-02-01 20:20:21 +00:00
|
|
|
{
|
|
|
|
ret.arguments.emplace_back(parseExpression());
|
2017-05-24 23:13:51 +00:00
|
|
|
if (currentToken() == Token::RParen)
|
2017-02-01 20:20:21 +00:00
|
|
|
break;
|
|
|
|
expectToken(Token::Comma);
|
|
|
|
}
|
|
|
|
ret.location.end = endPosition();
|
|
|
|
expectToken(Token::RParen);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else
|
2017-05-02 08:25:12 +00:00
|
|
|
fatalParserError(
|
|
|
|
m_julia ?
|
|
|
|
"Function name expected." :
|
|
|
|
"Assembly instruction or function name required in front of \"(\")"
|
|
|
|
);
|
2017-02-01 20:20:21 +00:00
|
|
|
|
|
|
|
return {};
|
2016-02-22 01:13:41 +00:00
|
|
|
}
|
2017-01-31 22:59:41 +00:00
|
|
|
|
2017-04-26 22:58:34 +00:00
|
|
|
TypedName Parser::parseTypedName()
|
|
|
|
{
|
|
|
|
TypedName typedName = createWithLocation<TypedName>();
|
|
|
|
typedName.name = expectAsmIdentifier();
|
|
|
|
if (m_julia)
|
|
|
|
{
|
|
|
|
expectToken(Token::Colon);
|
|
|
|
typedName.location.end = endPosition();
|
|
|
|
typedName.type = expectAsmIdentifier();
|
|
|
|
}
|
|
|
|
return typedName;
|
|
|
|
}
|
|
|
|
|
2017-01-31 22:59:41 +00:00
|
|
|
string Parser::expectAsmIdentifier()
|
|
|
|
{
|
2017-05-24 23:13:51 +00:00
|
|
|
string name = currentLiteral();
|
2017-05-17 12:33:05 +00:00
|
|
|
if (m_julia)
|
|
|
|
{
|
2017-05-24 23:13:51 +00:00
|
|
|
if (currentToken() == Token::Bool)
|
2017-05-17 12:33:05 +00:00
|
|
|
{
|
2017-05-24 23:13:51 +00:00
|
|
|
advance();
|
2017-05-17 12:33:05 +00:00
|
|
|
return name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (instructions().count(name))
|
2017-01-31 22:59:41 +00:00
|
|
|
fatalParserError("Cannot use instruction names for identifier names.");
|
|
|
|
expectToken(Token::Identifier);
|
|
|
|
return name;
|
|
|
|
}
|