Coverage Report

Created: 2026-07-13 07:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/solidity/libsolidity/interface/StandardCompiler.h
Line
Count
Source
1
/*
2
  This file is part of solidity.
3
4
  solidity is free software: you can redistribute it and/or modify
5
  it under the terms of the GNU General Public License as published by
6
  the Free Software Foundation, either version 3 of the License, or
7
  (at your option) any later version.
8
9
  solidity is distributed in the hope that it will be useful,
10
  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
  GNU General Public License for more details.
13
14
  You should have received a copy of the GNU General Public License
15
  along with solidity.  If not, see <http://www.gnu.org/licenses/>.
16
*/
17
// SPDX-License-Identifier: GPL-3.0
18
/**
19
 * @author Alex Beregszaszi
20
 * @date 2016
21
 * Standard JSON compiler interface.
22
 */
23
24
#pragma once
25
26
#include <libsolidity/interface/CompilerStack.h>
27
#include <libsolutil/JSON.h>
28
29
#include <liblangutil/DebugInfoSelection.h>
30
31
#include <optional>
32
#include <utility>
33
#include <variant>
34
35
namespace solidity::frontend
36
{
37
38
/**
39
 * Standard JSON compiler interface, which expects a JSON input and returns a JSON output.
40
 * See docs/using-the-compiler#compiler-input-and-output-json-description.
41
 */
42
class StandardCompiler
43
{
44
public:
45
  /// Noncopyable.
46
  StandardCompiler(StandardCompiler const&) = delete;
47
  StandardCompiler& operator=(StandardCompiler const&) = delete;
48
49
  /// Creates a new StandardCompiler.
50
  /// @param _readFile callback used to read files for import statements. Must return
51
  /// and must not emit exceptions.
52
  explicit StandardCompiler(ReadCallback::Callback _readFile = ReadCallback::Callback(),
53
    util::JsonFormat const& _format = {}):
54
0
    m_readFile(std::move(_readFile)),
55
0
    m_jsonPrintingFormat(std::move(_format))
56
0
  {
57
0
  }
58
59
  /// Sets all input parameters according to @a _input which conforms to the standardized input
60
  /// format, performs compilation and returns a standardized output.
61
  Json compile(Json const& _input) noexcept;
62
  /// Parses input as JSON and performs the above processing steps, returning a serialized JSON
63
  /// output. Parsing errors are returned as regular errors.
64
  std::string compile(std::string const& _input) noexcept;
65
66
  static Json formatFunctionDebugData(
67
    std::map<std::string, evmasm::LinkerObject::FunctionDebugData> const& _debugInfo
68
  );
69
70
private:
71
  struct InputsAndSettings
72
  {
73
    std::string language;
74
    Json errors;
75
    CompilerStack::State stopAfter = CompilerStack::State::CompilationSuccessful;
76
    std::map<std::string, std::string> sources;
77
    std::map<std::string, Json> jsonSources;
78
    std::map<util::h256, std::string> smtLib2Responses;
79
    langutil::EVMVersion evmVersion;
80
    std::vector<ImportRemapper::Remapping> remappings;
81
    RevertStrings revertStrings = RevertStrings::Default;
82
    OptimiserSettings optimiserSettings;
83
    std::optional<langutil::DebugInfoSelection> debugInfoSelection;
84
    std::map<std::string, util::h160> libraries;
85
    bool metadataLiteralSources = false;
86
    CompilerStack::MetadataFormat metadataFormat = CompilerStack::defaultMetadataFormat();
87
    CompilerStack::MetadataHash metadataHash = CompilerStack::MetadataHash::IPFS;
88
    Json outputSelection;
89
    ModelCheckerSettings modelCheckerSettings = ModelCheckerSettings{};
90
    bool viaIR = false;
91
    bool viaSSACFG = false;
92
    bool experimental = false;
93
  };
94
95
  /// Parses the input json (and potentially invokes the read callback) and either returns
96
  /// it in condensed form or an error as a json object.
97
  std::variant<InputsAndSettings, Json> parseInput(Json const& _input);
98
99
  std::map<std::string, Json> parseAstFromInput(StringMap const& _sources);
100
  Json importEVMAssembly(InputsAndSettings _inputsAndSettings);
101
  Json compileSolidity(InputsAndSettings _inputsAndSettings);
102
  Json compileYul(InputsAndSettings _inputsAndSettings);
103
104
  ReadCallback::Callback m_readFile;
105
106
  util::JsonFormat m_jsonPrintingFormat;
107
};
108
109
}