aboutsummaryrefslogtreecommitdiffstats
path: root/libsolidity/interface/StandardCompiler.cpp
blob: 7f3b7a58ccbd2867bb4bf26fa69ea479312fc4e1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
    This file is part of solidity.

    solidity 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.

    solidity 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 solidity.  If not, see <http://www.gnu.org/licenses/>.
*/
/**
 * @author Alex Beregszaszi
 * @date 2016
 * Standard JSON compiler interface.
 */

#include <libsolidity/interface/StandardCompiler.h>
#include <libsolidity/interface/SourceReferenceFormatter.h>
#include <libevmasm/Instruction.h>
#include <libdevcore/JSON.h>

using namespace std;
using namespace dev;
using namespace dev::solidity;

Json::Value StandardCompiler::compileInternal(Json::Value const& _input)
{
    m_compilerStack.reset(false);

    Json::Value const& sources = _input["sources"];
    if (!sources)
    {
        // @TODO report error
        return Json::Value();
    }

    for (auto const& sourceName: sources.getMemberNames())
        m_compilerStack.addSource(sourceName, sources[sourceName]["content"].asString());

    // @TODO parse settings
    bool optimize = false;
    unsigned optimizeRuns = 200;
    map<string, h160> libraries;

    auto scannerFromSourceName = [&](string const& _sourceName) -> solidity::Scanner const& { return m_compilerStack.scanner(_sourceName); };

    try
    {
        // @TODO check return value and parse errors
        m_compilerStack.compile(optimize, optimizeRuns, libraries);
    }
    catch (Error const& _error)
    {
        if (_error.type() == Error::Type::DocstringParsingError)
            cerr << "Documentation parsing error: " << *boost::get_error_info<errinfo_comment>(_error) << endl;
        else
            SourceReferenceFormatter::printExceptionInformation(cerr, _error, _error.typeName(), scannerFromSourceName);

        return Json::Value();
    }
    catch (CompilerError const& _exception)
    {
        SourceReferenceFormatter::printExceptionInformation(cerr, _exception, "Compiler error", scannerFromSourceName);
        return Json::Value();
    }
    catch (InternalCompilerError const& _exception)
    {
        cerr << "Internal compiler error during compilation:" << endl
            << boost::diagnostic_information(_exception);
        return Json::Value();
    }
    catch (UnimplementedFeatureError const& _exception)
    {
        cerr << "Unimplemented feature:" << endl
            << boost::diagnostic_information(_exception);
        return Json::Value();
    }
    catch (Exception const& _exception)
    {
        cerr << "Exception during compilation: " << boost::diagnostic_information(_exception) << endl;
        return Json::Value();
    }
    catch (...)
    {
        cerr << "Unknown exception during compilation." << endl;
        return Json::Value();
    }

    Json::Value output = Json::objectValue;

    Json::Value contractsOutput = Json::objectValue;
    for (string const& contractName: m_compilerStack.contractNames())
    {
        // ABI, documentation and metadata
        Json::Value contractData(Json::objectValue);
        contractData["abi"] = dev::jsonCompactPrint(m_compilerStack.metadata(contractName, DocumentationType::ABIInterface));
        contractData["metadata"] = m_compilerStack.onChainMetadata(contractName);
        contractData["userdoc"] = dev::jsonCompactPrint(m_compilerStack.metadata(contractName, DocumentationType::NatspecUser));
        contractData["devdoc"] = dev::jsonCompactPrint(m_compilerStack.metadata(contractName, DocumentationType::NatspecDev));

        // EVM
        Json::Value evmData(Json::objectValue);
        // @TODO: add ir
        // @TODO: add assembly
        // @TODO: add legacyAssemblyJSON
        evmData["opcodes"] = solidity::disassemble(m_compilerStack.object(contractName).bytecode);
        // @TODO: add methodIdentifiers
        // @TODO: add gasEstimates

        // EVM bytecode
        Json::Value bytecode(Json::objectValue);
        bytecode["object"] = m_compilerStack.object(contractName).toHex();
        auto sourceMap = m_compilerStack.sourceMapping(contractName);
        bytecode["sourceMap"] = sourceMap ? *sourceMap : "";
        // @TODO: add linkReferences
        evmData["bytecode"] = bytecode;

        // EVM deployed bytecode
        Json::Value deployedBytecode(Json::objectValue);
        deployedBytecode["object"] = m_compilerStack.runtimeObject(contractName).toHex();
        auto runtimeSourceMap = m_compilerStack.runtimeSourceMapping(contractName);
        deployedBytecode["sourceMap"] = runtimeSourceMap ? *runtimeSourceMap : "";
        // @TODO: add linkReferences
        evmData["deployedBytecode"] = deployedBytecode;

        contractData["evm"] = evmData;

        contractsOutput[contractName] = contractData;
    }
    output["contracts"] = Json::objectValue;
    output["contracts"][""] = contractsOutput;

    return output;
}

Json::Value StandardCompiler::compile(Json::Value const& _input)
{
    try
    {
        return compileInternal(_input);
    }
    catch (...)
    {
        return "{\"errors\":\"[{\"type\":\"InternalCompilerError\",\"component\":\"general\",\"severity\":\"error\",\"message\":\"Internal exception in StandardCompiler::compilerInternal\"}]}";
    }
}

string StandardCompiler::compile(string const& _input)
{
    Json::Value input;

    if (!Json::Reader().parse(_input, input, false))
    {
        return "{\"errors\":\"[{\"type\":\"JSONError\",\"component\":\"general\",\"severity\":\"error\",\"message\":\"Error parsing input JSON.\"}]}";
    }

    // cout << "Input: " << input.toStyledString() << endl;
    Json::Value output = compile(input);
    // cout << "Output: " << output.toStyledString() << endl;

    try
    {
        return jsonCompactPrint(output);
    }
    catch(...)
    {
        return "{\"errors\":\"[{\"type\":\"JSONError\",\"component\":\"general\",\"severity\":\"error\",\"message\":\"Error writing output JSON.\"}]}";
    }
}