Skip to content

Commit 3e823e0

Browse files
cameelaarltr0qs
committed
Introduce AbstractAssemblyStack interface
Co-authored-by: Alexander Arlt <[email protected]> Co-authored-by: r0qs <[email protected]>
1 parent e8b898c commit 3e823e0

File tree

6 files changed

+171
-76
lines changed

6 files changed

+171
-76
lines changed

libevmasm/AbstractAssemblyStack.h

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
#pragma once
20+
21+
#include <libevmasm/LinkerObject.h>
22+
23+
#include <libsolutil/Common.h>
24+
#include <libsolutil/JSON.h>
25+
26+
#include <string>
27+
#include <vector>
28+
29+
namespace solidity::evmasm
30+
{
31+
32+
class AbstractAssemblyStack
33+
{
34+
public:
35+
virtual ~AbstractAssemblyStack() {}
36+
37+
virtual LinkerObject const& object(std::string const& _contractName) const = 0;
38+
virtual LinkerObject const& runtimeObject(std::string const& _contractName) const = 0;
39+
40+
virtual std::string const* sourceMapping(std::string const& _contractName) const = 0;
41+
virtual std::string const* runtimeSourceMapping(std::string const& _contractName) const = 0;
42+
43+
virtual Json::Value assemblyJSON(std::string const& _contractName) const = 0;
44+
virtual std::string assemblyString(std::string const& _contractName, StringMap const& _sourceCodes) const = 0;
45+
46+
virtual std::string const filesystemFriendlyName(std::string const& _contractName) const = 0;
47+
48+
virtual std::vector<std::string> contractNames() const = 0;
49+
virtual std::vector<std::string> sourceNames() const = 0;
50+
51+
virtual bool compilationSuccessful() const = 0;
52+
};
53+
54+
} // namespace solidity::evmasm

libevmasm/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
set(sources
2+
AbstractAssemblyStack.h
23
Assembly.cpp
34
Assembly.h
45
AssemblyItem.cpp

libsolidity/interface/CompilerStack.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@
7474
#include <liblangutil/SemVerHandler.h>
7575
#include <liblangutil/SourceReferenceFormatter.h>
7676

77-
#include <libevmasm/Exceptions.h>
7877

7978
#include <libsolutil/SwarmHash.h>
8079
#include <libsolutil/IpfsHash.h>

libsolidity/interface/CompilerStack.h

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include <liblangutil/EVMVersion.h>
4242
#include <liblangutil/SourceLocation.h>
4343

44+
#include <libevmasm/AbstractAssemblyStack.h>
4445
#include <libevmasm/LinkerObject.h>
4546

4647
#include <libsolutil/Common.h>
@@ -91,7 +92,7 @@ class Analysis;
9192
* It holds state and can be used to either step through the compilation stages (and abort e.g.
9293
* before compilation to bytecode) or run the whole compilation in one call.
9394
*/
94-
class CompilerStack: public langutil::CharStreamProvider
95+
class CompilerStack: public langutil::CharStreamProvider, public evmasm::AbstractAssemblyStack
9596
{
9697
public:
9798
/// Noncopyable.
@@ -123,7 +124,7 @@ class CompilerStack: public langutil::CharStreamProvider
123124
/// Regular compilation from Solidity source files.
124125
Solidity,
125126
/// Compilation from an imported Solidity AST.
126-
SolidityAST
127+
SolidityAST,
127128
};
128129

129130
/// Creates a new compiler stack.
@@ -139,6 +140,8 @@ class CompilerStack: public langutil::CharStreamProvider
139140
/// @returns the current state.
140141
State state() const { return m_stackState; }
141142

143+
virtual bool compilationSuccessful() const override { return m_stackState >= CompilationSuccessful; }
144+
142145
/// Resets the compiler to an empty state. Unless @a _keepSettings is set to true,
143146
/// all settings are reset as well.
144147
void reset(bool _keepSettings = false);
@@ -234,7 +237,7 @@ class CompilerStack: public langutil::CharStreamProvider
234237
bool compile(State _stopAfter = State::CompilationSuccessful);
235238

236239
/// @returns the list of sources (paths) used
237-
std::vector<std::string> sourceNames() const;
240+
virtual std::vector<std::string> sourceNames() const override;
238241

239242
/// @returns a mapping assigning each source name its index inside the vector returned
240243
/// by sourceNames().
@@ -255,13 +258,13 @@ class CompilerStack: public langutil::CharStreamProvider
255258
std::vector<std::string> const& unhandledSMTLib2Queries() const { return m_unhandledSMTLib2Queries; }
256259

257260
/// @returns a list of the contract names in the sources.
258-
std::vector<std::string> contractNames() const;
261+
virtual std::vector<std::string> contractNames() const override;
259262

260263
/// @returns the name of the last contract. If _sourceName is defined the last contract of that source will be returned.
261264
std::string const lastContractName(std::optional<std::string> const& _sourceName = std::nullopt) const;
262265

263266
/// @returns either the contract's name or a mixture of its name and source file, sanitized for filesystem use
264-
std::string const filesystemFriendlyName(std::string const& _contractName) const;
267+
virtual std::string const filesystemFriendlyName(std::string const& _contractName) const override;
265268

266269
/// @returns the IR representation of a contract.
267270
std::string const& yulIR(std::string const& _contractName) const;
@@ -276,10 +279,10 @@ class CompilerStack: public langutil::CharStreamProvider
276279
Json::Value const& yulIROptimizedAst(std::string const& _contractName) const;
277280

278281
/// @returns the assembled object for a contract.
279-
evmasm::LinkerObject const& object(std::string const& _contractName) const;
282+
virtual evmasm::LinkerObject const& object(std::string const& _contractName) const override;
280283

281284
/// @returns the runtime object for the contract.
282-
evmasm::LinkerObject const& runtimeObject(std::string const& _contractName) const;
285+
virtual evmasm::LinkerObject const& runtimeObject(std::string const& _contractName) const override;
283286

284287
/// @returns normal contract assembly items
285288
evmasm::AssemblyItems const* assemblyItems(std::string const& _contractName) const;
@@ -293,21 +296,21 @@ class CompilerStack: public langutil::CharStreamProvider
293296

294297
/// @returns the string that provides a mapping between bytecode and sourcecode or a nullptr
295298
/// if the contract does not (yet) have bytecode.
296-
std::string const* sourceMapping(std::string const& _contractName) const;
299+
virtual std::string const* sourceMapping(std::string const& _contractName) const override;
297300

298301
/// @returns the string that provides a mapping between runtime bytecode and sourcecode.
299302
/// if the contract does not (yet) have bytecode.
300-
std::string const* runtimeSourceMapping(std::string const& _contractName) const;
303+
virtual std::string const* runtimeSourceMapping(std::string const& _contractName) const override;
301304

302305
/// @return a verbose text representation of the assembly.
303306
/// @arg _sourceCodes is the map of input files to source code strings
304307
/// Prerequisite: Successful compilation.
305-
std::string assemblyString(std::string const& _contractName, StringMap const& _sourceCodes = StringMap()) const;
308+
virtual std::string assemblyString(std::string const& _contractName, StringMap const& _sourceCodes = StringMap()) const override;
306309

307310
/// @returns a JSON representation of the assembly.
308311
/// @arg _sourceCodes is the map of input files to source code strings
309312
/// Prerequisite: Successful compilation.
310-
Json::Value assemblyJSON(std::string const& _contractName) const;
313+
virtual Json::Value assemblyJSON(std::string const& _contractName) const override;
311314

312315
/// @returns a JSON representing the contract ABI.
313316
/// Prerequisite: Successful call to parse or compile.

0 commit comments

Comments
 (0)