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-03 15:40:37 +00:00
|
|
|
#include <libsolidity/InterfaceHandler.h>
|
2014-12-01 16:03:04 +00:00
|
|
|
|
2014-10-30 21:52:15 +00:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
namespace dev
|
|
|
|
{
|
|
|
|
namespace solidity
|
|
|
|
{
|
|
|
|
|
2014-12-04 22:55:47 +00:00
|
|
|
CompilerStack::CompilerStack(): m_interfaceHandler(make_shared<InterfaceHandler>()) {}
|
2014-12-03 15:40:37 +00:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-12-05 14:50:39 +00:00
|
|
|
std::string const& CompilerStack::getJsonDocumentation(DocumentationType _type)
|
2014-12-01 16:03:04 +00:00
|
|
|
{
|
|
|
|
if (!m_parseSuccessful)
|
|
|
|
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Parsing was not successful."));
|
2014-12-01 17:01:42 +00:00
|
|
|
|
2014-12-04 22:55:47 +00:00
|
|
|
auto createDocIfNotThere = [this, _type](std::unique_ptr<string>& _doc)
|
2014-12-01 16:03:04 +00:00
|
|
|
{
|
2014-12-04 22:55:47 +00:00
|
|
|
if (!_doc)
|
2014-12-03 15:40:37 +00:00
|
|
|
_doc = m_interfaceHandler->getDocumentation(m_contractASTNode, _type);
|
|
|
|
};
|
2014-12-03 12:50:04 +00:00
|
|
|
|
2014-12-03 15:40:37 +00:00
|
|
|
switch (_type)
|
2014-12-03 12:50:04 +00:00
|
|
|
{
|
2014-12-05 14:50:39 +00:00
|
|
|
case DocumentationType::NATSPEC_USER:
|
2014-12-04 22:55:47 +00:00
|
|
|
createDocIfNotThere(m_userDocumentation);
|
2014-12-05 11:41:32 +00:00
|
|
|
return *m_userDocumentation;
|
2014-12-05 14:50:39 +00:00
|
|
|
case DocumentationType::NATSPEC_DEV:
|
2014-12-04 22:55:47 +00:00
|
|
|
createDocIfNotThere(m_devDocumentation);
|
2014-12-05 11:41:32 +00:00
|
|
|
return *m_devDocumentation;
|
2014-12-05 14:50:39 +00:00
|
|
|
case DocumentationType::ABI_INTERFACE:
|
2014-12-04 22:55:47 +00:00
|
|
|
createDocIfNotThere(m_interface);
|
2014-12-05 11:41:32 +00:00
|
|
|
return *m_interface;
|
2014-12-03 12:50:04 +00:00
|
|
|
}
|
2014-12-04 22:55:47 +00:00
|
|
|
|
|
|
|
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Illegal documentation type."));
|
2014-12-01 16:03:04 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|