2014-10-30 21:52:15 +00:00
|
|
|
/*
|
|
|
|
This file is part of cpp-ethereum.
|
|
|
|
|
|
|
|
cpp-ethereum 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.
|
|
|
|
|
|
|
|
cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @author Christian <c@ethdev.com>
|
|
|
|
* @date 2014
|
|
|
|
* Full-stack compiler that converts a source code string to bytecode.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libsolidity/AST.h>
|
|
|
|
#include <libsolidity/Scanner.h>
|
|
|
|
#include <libsolidity/Parser.h>
|
2014-11-20 17:33:23 +00:00
|
|
|
#include <libsolidity/GlobalContext.h>
|
2014-10-30 21:52:15 +00:00
|
|
|
#include <libsolidity/NameAndTypeResolver.h>
|
|
|
|
#include <libsolidity/Compiler.h>
|
|
|
|
#include <libsolidity/CompilerStack.h>
|
|
|
|
|
2014-12-01 16:03:04 +00:00
|
|
|
#include <jsonrpc/json/json.h>
|
|
|
|
|
2014-10-30 21:52:15 +00:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
namespace dev
|
|
|
|
{
|
|
|
|
namespace solidity
|
|
|
|
{
|
|
|
|
|
2014-11-11 16:41:48 +00:00
|
|
|
void CompilerStack::setSource(string const& _sourceCode)
|
2014-10-30 21:52:15 +00:00
|
|
|
{
|
2014-11-11 16:41:48 +00:00
|
|
|
reset();
|
|
|
|
m_scanner = make_shared<Scanner>(CharStream(_sourceCode));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CompilerStack::parse()
|
|
|
|
{
|
|
|
|
if (!m_scanner)
|
|
|
|
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Source not available."));
|
|
|
|
m_contractASTNode = Parser().parse(m_scanner);
|
2014-11-20 17:33:23 +00:00
|
|
|
m_globalContext = make_shared<GlobalContext>();
|
|
|
|
m_globalContext->setCurrentContract(*m_contractASTNode);
|
|
|
|
NameAndTypeResolver(m_globalContext->getDeclarations()).resolveNamesAndTypes(*m_contractASTNode);
|
2014-11-11 16:41:48 +00:00
|
|
|
m_parseSuccessful = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CompilerStack::parse(string const& _sourceCode)
|
|
|
|
{
|
|
|
|
setSource(_sourceCode);
|
|
|
|
parse();
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes const& CompilerStack::compile(bool _optimize)
|
|
|
|
{
|
|
|
|
if (!m_parseSuccessful)
|
|
|
|
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Parsing was not successful."));
|
|
|
|
m_bytecode.clear();
|
|
|
|
m_compiler = make_shared<Compiler>();
|
2014-11-21 18:14:56 +00:00
|
|
|
m_compiler->compileContract(*m_contractASTNode, m_globalContext->getMagicVariables());
|
2014-11-11 16:41:48 +00:00
|
|
|
return m_bytecode = m_compiler->getAssembledBytecode(_optimize);
|
|
|
|
}
|
2014-10-30 21:52:15 +00:00
|
|
|
|
2014-11-11 16:41:48 +00:00
|
|
|
bytes const& CompilerStack::compile(string const& _sourceCode, bool _optimize)
|
|
|
|
{
|
|
|
|
parse(_sourceCode);
|
|
|
|
return compile(_optimize);
|
2014-10-30 21:52:15 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 16:41:48 +00:00
|
|
|
void CompilerStack::streamAssembly(ostream& _outStream)
|
|
|
|
{
|
|
|
|
if (!m_compiler || m_bytecode.empty())
|
|
|
|
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Compilation was not successful."));
|
|
|
|
m_compiler->streamAssembly(_outStream);
|
|
|
|
}
|
|
|
|
|
|
|
|
string const& CompilerStack::getInterface()
|
|
|
|
{
|
|
|
|
if (!m_parseSuccessful)
|
|
|
|
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Parsing was not successful."));
|
|
|
|
if (m_interface.empty())
|
|
|
|
{
|
|
|
|
stringstream interface;
|
|
|
|
interface << '[';
|
|
|
|
vector<FunctionDefinition const*> exportedFunctions = m_contractASTNode->getInterfaceFunctions();
|
|
|
|
unsigned functionsCount = exportedFunctions.size();
|
|
|
|
for (FunctionDefinition const* f: exportedFunctions)
|
|
|
|
{
|
|
|
|
auto streamVariables = [&](vector<ASTPointer<VariableDeclaration>> const& _vars)
|
|
|
|
{
|
|
|
|
unsigned varCount = _vars.size();
|
|
|
|
for (ASTPointer<VariableDeclaration> const& var: _vars)
|
|
|
|
{
|
|
|
|
interface << "{"
|
|
|
|
<< "\"name\":" << escaped(var->getName(), false) << ","
|
|
|
|
<< "\"type\":" << escaped(var->getType()->toString(), false)
|
|
|
|
<< "}";
|
|
|
|
if (--varCount > 0)
|
|
|
|
interface << ",";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
interface << '{'
|
|
|
|
<< "\"name\":" << escaped(f->getName(), false) << ","
|
|
|
|
<< "\"inputs\":[";
|
|
|
|
streamVariables(f->getParameters());
|
|
|
|
interface << "],"
|
|
|
|
<< "\"outputs\":[";
|
|
|
|
streamVariables(f->getReturnParameters());
|
|
|
|
interface << "]"
|
|
|
|
<< "}";
|
|
|
|
if (--functionsCount > 0)
|
|
|
|
interface << ",";
|
|
|
|
}
|
|
|
|
interface << ']';
|
|
|
|
m_interface = interface.str();
|
|
|
|
}
|
|
|
|
return m_interface;
|
|
|
|
}
|
|
|
|
|
2014-12-01 16:03:04 +00:00
|
|
|
string const& CompilerStack::getDocumentation()
|
|
|
|
{
|
|
|
|
|
|
|
|
Json::StyledWriter writer;
|
|
|
|
if (!m_parseSuccessful)
|
|
|
|
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Parsing was not successful."));
|
|
|
|
if (m_documentation.empty())
|
|
|
|
{
|
|
|
|
Json::Value doc;
|
|
|
|
Json::Value methods;
|
|
|
|
|
|
|
|
vector<FunctionDefinition const*> exportedFunctions = m_contractASTNode->getInterfaceFunctions();
|
|
|
|
for (FunctionDefinition const* f: exportedFunctions)
|
|
|
|
{
|
|
|
|
Json::Value user;
|
|
|
|
user["user"] = Json::Value(*f->getDocumentation());
|
|
|
|
methods[f->getName()] = user;
|
|
|
|
}
|
|
|
|
doc["methods"] = methods;
|
|
|
|
m_documentation = writer.write(doc);
|
|
|
|
}
|
|
|
|
return m_documentation;
|
|
|
|
}
|
|
|
|
|
2014-11-11 16:41:48 +00:00
|
|
|
bytes CompilerStack::staticCompile(std::string const& _sourceCode, bool _optimize)
|
|
|
|
{
|
|
|
|
CompilerStack stack;
|
|
|
|
return stack.compile(_sourceCode, _optimize);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-10-30 21:52:15 +00:00
|
|
|
}
|
|
|
|
}
|