2015-01-28 07:50:53 +00:00
|
|
|
/*
|
2016-11-18 23:13:20 +00:00
|
|
|
This file is part of solidity.
|
2015-01-28 07:50:53 +00:00
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is free software: you can redistribute it and/or modify
|
2015-01-28 07:50:53 +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,
|
2015-01-28 07:50:53 +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/>.
|
2015-01-28 07:50:53 +00:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @author Christian <c@ethdev.com>
|
|
|
|
* @date 2014
|
2018-09-27 00:53:34 +00:00
|
|
|
* Public compiler API.
|
2015-01-28 07:50:53 +00:00
|
|
|
*/
|
|
|
|
|
2017-09-16 02:50:16 +00:00
|
|
|
#include <libsolc/libsolc.h>
|
2017-03-16 23:48:51 +00:00
|
|
|
#include <libsolidity/interface/StandardCompiler.h>
|
2015-10-20 22:21:52 +00:00
|
|
|
#include <libsolidity/interface/Version.h>
|
2019-04-24 12:03:09 +00:00
|
|
|
#include <libyul/YulString.h>
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/Common.h>
|
|
|
|
#include <libsolutil/JSON.h>
|
2015-01-28 07:50:53 +00:00
|
|
|
|
2019-12-04 15:36:56 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <list>
|
2017-08-28 23:34:39 +00:00
|
|
|
#include <string>
|
|
|
|
|
2017-06-13 20:47:33 +00:00
|
|
|
#include "license.h"
|
|
|
|
|
2015-01-28 07:50:53 +00:00
|
|
|
using namespace std;
|
|
|
|
using namespace solidity;
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity::util;
|
|
|
|
|
|
|
|
using solidity::frontend::ReadCallback;
|
|
|
|
using solidity::frontend::StandardCompiler;
|
2015-01-28 07:50:53 +00:00
|
|
|
|
2017-08-09 11:44:53 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2019-12-04 15:36:56 +00:00
|
|
|
// The strings in this list must not be resized after they have been added here (via solidity_alloc()), because
|
|
|
|
// this may potentially change the pointer that was passed to the caller from solidity_alloc().
|
|
|
|
static list<string> solidityAllocations;
|
|
|
|
|
|
|
|
/// Find the equivalent to @p _data in the list of allocations of solidity_alloc(),
|
|
|
|
/// removes it from the list and returns its value.
|
|
|
|
///
|
|
|
|
/// If any invalid argument is being passed, it is considered a programming error
|
|
|
|
/// on the caller-side and hence, will call abort() then.
|
|
|
|
string takeOverAllocation(char const* _data)
|
|
|
|
{
|
|
|
|
for (auto iter = begin(solidityAllocations); iter != end(solidityAllocations); ++iter)
|
|
|
|
if (iter->data() == _data)
|
|
|
|
{
|
|
|
|
string chunk = move(*iter);
|
|
|
|
solidityAllocations.erase(iter);
|
|
|
|
return chunk;
|
|
|
|
}
|
|
|
|
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2019-12-10 00:39:20 +00:00
|
|
|
/// Resizes a std::string to the proper length based on the occurrence of a zero terminator.
|
|
|
|
void truncateCString(string& _data)
|
|
|
|
{
|
|
|
|
size_t pos = _data.find('\0');
|
|
|
|
if (pos != string::npos)
|
|
|
|
_data.resize(pos);
|
|
|
|
}
|
|
|
|
|
2019-10-29 19:00:58 +00:00
|
|
|
ReadCallback::Callback wrapReadCallback(CStyleReadFileCallback _readCallback, void* _readContext)
|
2017-03-16 23:48:51 +00:00
|
|
|
{
|
2017-07-13 19:06:04 +00:00
|
|
|
ReadCallback::Callback readCallback;
|
2017-03-16 23:48:51 +00:00
|
|
|
if (_readCallback)
|
|
|
|
{
|
2019-09-17 14:06:43 +00:00
|
|
|
readCallback = [=](string const& _kind, string const& _data)
|
2017-03-16 23:48:51 +00:00
|
|
|
{
|
|
|
|
char* contents_c = nullptr;
|
|
|
|
char* error_c = nullptr;
|
2019-12-04 15:36:56 +00:00
|
|
|
_readCallback(_readContext, _kind.data(), _data.data(), &contents_c, &error_c);
|
2017-07-13 19:06:04 +00:00
|
|
|
ReadCallback::Result result;
|
2017-03-16 23:48:51 +00:00
|
|
|
result.success = true;
|
|
|
|
if (!contents_c && !error_c)
|
|
|
|
{
|
|
|
|
result.success = false;
|
2019-11-21 17:25:51 +00:00
|
|
|
result.responseOrErrorMessage = "Callback not supported.";
|
2017-03-16 23:48:51 +00:00
|
|
|
}
|
|
|
|
if (contents_c)
|
|
|
|
{
|
|
|
|
result.success = true;
|
2019-12-04 15:36:56 +00:00
|
|
|
result.responseOrErrorMessage = takeOverAllocation(contents_c);
|
2017-03-16 23:48:51 +00:00
|
|
|
}
|
|
|
|
if (error_c)
|
|
|
|
{
|
|
|
|
result.success = false;
|
2019-12-04 15:36:56 +00:00
|
|
|
result.responseOrErrorMessage = takeOverAllocation(error_c);
|
2017-03-16 23:48:51 +00:00
|
|
|
}
|
2019-12-10 00:39:20 +00:00
|
|
|
truncateCString(result.responseOrErrorMessage);
|
2017-03-16 23:48:51 +00:00
|
|
|
return result;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return readCallback;
|
|
|
|
}
|
|
|
|
|
2019-10-29 19:00:58 +00:00
|
|
|
string compile(string _input, CStyleReadFileCallback _readCallback, void* _readContext)
|
2017-03-16 23:48:51 +00:00
|
|
|
{
|
2019-10-29 19:00:58 +00:00
|
|
|
StandardCompiler compiler(wrapReadCallback(_readCallback, _readContext));
|
2019-12-04 15:36:56 +00:00
|
|
|
return compiler.compile(move(_input));
|
2017-03-16 23:48:51 +00:00
|
|
|
}
|
|
|
|
|
2017-08-09 11:44:53 +00:00
|
|
|
}
|
|
|
|
|
2015-01-28 07:50:53 +00:00
|
|
|
extern "C"
|
|
|
|
{
|
2018-09-27 00:53:34 +00:00
|
|
|
extern char const* solidity_license() noexcept
|
2017-06-13 20:47:33 +00:00
|
|
|
{
|
2017-06-30 14:59:53 +00:00
|
|
|
static string fullLicenseText = otherLicenses + licenseText;
|
|
|
|
return fullLicenseText.c_str();
|
2017-06-13 20:47:33 +00:00
|
|
|
}
|
2019-12-04 15:36:56 +00:00
|
|
|
|
2018-08-08 12:23:23 +00:00
|
|
|
extern char const* solidity_version() noexcept
|
2018-06-19 23:34:54 +00:00
|
|
|
{
|
2019-12-11 16:31:36 +00:00
|
|
|
return frontend::VersionString.c_str();
|
2018-06-19 23:34:54 +00:00
|
|
|
}
|
2019-12-04 15:36:56 +00:00
|
|
|
|
2019-12-11 23:51:27 +00:00
|
|
|
extern char* solidity_compile(char const* _input, CStyleReadFileCallback _readCallback, void* _readContext) noexcept
|
2018-06-19 23:34:54 +00:00
|
|
|
{
|
2019-12-04 15:36:56 +00:00
|
|
|
return solidityAllocations.emplace_back(compile(_input, _readCallback, _readContext)).data();
|
|
|
|
}
|
|
|
|
|
|
|
|
extern char* solidity_alloc(size_t _size) noexcept
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return solidityAllocations.emplace_back(_size, '\0').data();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
// most likely a std::bad_alloc(), if at all.
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern void solidity_free(char* _data) noexcept
|
|
|
|
{
|
|
|
|
takeOverAllocation(_data);
|
2018-06-19 23:34:54 +00:00
|
|
|
}
|
2019-12-04 15:36:56 +00:00
|
|
|
|
|
|
|
extern void solidity_reset() noexcept
|
2019-01-22 11:41:21 +00:00
|
|
|
{
|
2019-04-24 12:03:09 +00:00
|
|
|
// This is called right before each compilation, but not at the end, so additional memory
|
|
|
|
// can be freed here.
|
|
|
|
yul::YulStringRepository::reset();
|
2019-12-04 15:36:56 +00:00
|
|
|
solidityAllocations.clear();
|
2019-01-22 11:41:21 +00:00
|
|
|
}
|
2015-01-28 07:50:53 +00:00
|
|
|
}
|