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()
|
|
|
|
{
|
2014-12-01 17:01:42 +00:00
|
|
|
Json::StyledWriter writer;
|
2014-11-11 16:41:48 +00:00
|
|
|
if (!m_parseSuccessful)
|
|
|
|
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Parsing was not successful."));
|
2014-12-01 17:01:42 +00:00
|
|
|
|
2014-11-11 16:41:48 +00:00
|
|
|
if (m_interface.empty())
|
|
|
|
{
|
2014-12-01 17:01:42 +00:00
|
|
|
Json::Value methods(Json::arrayValue);
|
|
|
|
|
2014-11-11 16:41:48 +00:00
|
|
|
vector<FunctionDefinition const*> exportedFunctions = m_contractASTNode->getInterfaceFunctions();
|
|
|
|
for (FunctionDefinition const* f: exportedFunctions)
|
|
|
|
{
|
2014-12-01 17:01:42 +00:00
|
|
|
Json::Value method;
|
|
|
|
Json::Value inputs(Json::arrayValue);
|
|
|
|
Json::Value outputs(Json::arrayValue);
|
|
|
|
|
2014-12-02 16:18:09 +00:00
|
|
|
auto streamVariables = [](vector<ASTPointer<VariableDeclaration>> const& _vars)
|
2014-11-11 16:41:48 +00:00
|
|
|
{
|
2014-12-02 16:18:09 +00:00
|
|
|
Json::Value params(Json::arrayValue);
|
2014-11-11 16:41:48 +00:00
|
|
|
for (ASTPointer<VariableDeclaration> const& var: _vars)
|
|
|
|
{
|
2014-12-01 17:01:42 +00:00
|
|
|
Json::Value input;
|
|
|
|
input["name"] = var->getName();
|
|
|
|
input["type"] = var->getType()->toString();
|
2014-12-02 16:18:09 +00:00
|
|
|
params.append(input);
|
2014-11-11 16:41:48 +00:00
|
|
|
}
|
2014-12-02 16:18:09 +00:00
|
|
|
return params;
|
2014-11-11 16:41:48 +00:00
|
|
|
};
|
|
|
|
|
2014-12-01 17:01:42 +00:00
|
|
|
method["name"] = f->getName();
|
2014-12-02 16:18:09 +00:00
|
|
|
method["inputs"] = streamVariables(f->getParameters());
|
|
|
|
method["outputs"] = streamVariables(f->getReturnParameters());
|
2014-12-01 17:01:42 +00:00
|
|
|
methods.append(method);
|
2014-11-11 16:41:48 +00:00
|
|
|
}
|
2014-12-01 17:01:42 +00:00
|
|
|
m_interface = writer.write(methods);
|
2014-11-11 16:41:48 +00:00
|
|
|
}
|
|
|
|
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."));
|
2014-12-01 17:01:42 +00:00
|
|
|
|
2014-12-01 16:03:04 +00:00
|
|
|
if (m_documentation.empty())
|
|
|
|
{
|
|
|
|
Json::Value doc;
|
2014-12-02 09:41:18 +00:00
|
|
|
Json::Value methods(Json::objectValue);
|
2014-12-01 16:03:04 +00:00
|
|
|
|
2014-12-02 11:14:24 +00:00
|
|
|
for (FunctionDefinition const* f: m_contractASTNode->getInterfaceFunctions())
|
2014-12-01 16:03:04 +00:00
|
|
|
{
|
|
|
|
Json::Value user;
|
2014-12-02 10:03:34 +00:00
|
|
|
auto strPtr = f->getDocumentation();
|
|
|
|
if (strPtr)
|
|
|
|
{
|
|
|
|
user["user"] = Json::Value(*strPtr);
|
|
|
|
methods[f->getName()] = user;
|
|
|
|
}
|
2014-12-01 16:03:04 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|