Merge pull request #2599 from ethereum/compilerstack-cleanup

Cleanup compilerstack (remove old methods and clean compile)
This commit is contained in:
Alex Beregszaszi 2017-07-26 15:37:16 +01:00 committed by GitHub
commit 43002b7bb8
11 changed files with 46 additions and 58 deletions

View File

@ -87,6 +87,7 @@ void CompilerStack::reset(bool _keepSources)
m_stackState = Empty; m_stackState = Empty;
m_sources.clear(); m_sources.clear();
} }
m_libraries.clear();
m_optimize = false; m_optimize = false;
m_optimizeRuns = 200; m_optimizeRuns = 200;
m_globalContext.reset(); m_globalContext.reset();
@ -106,12 +107,6 @@ bool CompilerStack::addSource(string const& _name, string const& _content, bool
return existed; return existed;
} }
void CompilerStack::setSource(string const& _sourceCode)
{
reset();
addSource("", _sourceCode);
}
bool CompilerStack::parse() bool CompilerStack::parse()
{ {
//reset //reset
@ -252,23 +247,11 @@ bool CompilerStack::analyze()
return false; return false;
} }
bool CompilerStack::parse(string const& _sourceCode)
{
setSource(_sourceCode);
return parse();
}
bool CompilerStack::parseAndAnalyze() bool CompilerStack::parseAndAnalyze()
{ {
return parse() && analyze(); return parse() && analyze();
} }
bool CompilerStack::parseAndAnalyze(std::string const& _sourceCode)
{
setSource(_sourceCode);
return parseAndAnalyze();
}
vector<string> CompilerStack::contractNames() const vector<string> CompilerStack::contractNames() const
{ {
if (m_stackState < AnalysisSuccessful) if (m_stackState < AnalysisSuccessful)
@ -279,17 +262,12 @@ vector<string> CompilerStack::contractNames() const
return contractNames; return contractNames;
} }
bool CompilerStack::compile()
bool CompilerStack::compile(bool _optimize, unsigned _runs, map<string, h160> const& _libraries)
{ {
if (m_stackState < AnalysisSuccessful) if (m_stackState < AnalysisSuccessful)
if (!parseAndAnalyze()) if (!parseAndAnalyze())
return false; return false;
m_optimize = _optimize;
m_optimizeRuns = _runs;
m_libraries = _libraries;
map<ContractDefinition const*, eth::Assembly const*> compiledContracts; map<ContractDefinition const*, eth::Assembly const*> compiledContracts;
for (Source const* source: m_sourceOrder) for (Source const* source: m_sourceOrder)
for (ASTPointer<ASTNode> const& node: source->ast->nodes()) for (ASTPointer<ASTNode> const& node: source->ast->nodes())
@ -300,11 +278,6 @@ bool CompilerStack::compile(bool _optimize, unsigned _runs, map<string, h160> co
return true; return true;
} }
bool CompilerStack::compile(string const& _sourceCode, bool _optimize, unsigned _runs)
{
return parseAndAnalyze(_sourceCode) && compile(_optimize, _runs);
}
void CompilerStack::link() void CompilerStack::link()
{ {
for (auto& contract: m_contracts) for (auto& contract: m_contracts)

View File

@ -96,46 +96,45 @@ public:
/// Sets path remappings in the format "context:prefix=target" /// Sets path remappings in the format "context:prefix=target"
void setRemappings(std::vector<std::string> const& _remappings); void setRemappings(std::vector<std::string> const& _remappings);
/// Sets library addresses. Addresses are cleared iff @a _libraries is missing.
/// Will not take effect before running compile.
void setLibraries(std::map<std::string, h160> const& _libraries = std::map<std::string, h160>{})
{
m_libraries = _libraries;
}
/// Changes the optimiser settings.
/// Will not take effect before running compile.
void setOptimiserSettings(bool _optimize, unsigned _runs = 200)
{
m_optimize = _optimize;
m_optimizeRuns = _runs;
}
/// Resets the compiler to a state where the sources are not parsed or even removed. /// Resets the compiler to a state where the sources are not parsed or even removed.
/// Sets the state to SourcesSet if @a _keepSources is true, otherwise to Empty.
/// All settings, with the exception of remappings, are reset.
void reset(bool _keepSources = false); void reset(bool _keepSources = false);
/// Adds a source object (e.g. file) to the parser. After this, parse has to be called again. /// Adds a source object (e.g. file) to the parser. After this, parse has to be called again.
/// @returns true if a source object by the name already existed and was replaced. /// @returns true if a source object by the name already existed and was replaced.
void addSources(StringMap const& _nameContents, bool _isLibrary = false)
{
for (auto const& i: _nameContents) addSource(i.first, i.second, _isLibrary);
}
bool addSource(std::string const& _name, std::string const& _content, bool _isLibrary = false); bool addSource(std::string const& _name, std::string const& _content, bool _isLibrary = false);
void setSource(std::string const& _sourceCode);
/// Parses all source units that were added /// Parses all source units that were added
/// @returns false on error. /// @returns false on error.
bool parse(); bool parse();
/// Sets the given source code as the only source unit apart from standard sources and parses it. /// Performs the analysis steps (imports, scopesetting, syntaxCheck, referenceResolving,
/// @returns false on error.
bool parse(std::string const& _sourceCode);
/// performs the analyisis steps (imports, scopesetting, syntaxCheck, referenceResolving,
/// typechecking, staticAnalysis) on previously set sources /// typechecking, staticAnalysis) on previously set sources
/// @returns false on error. /// @returns false on error.
bool analyze(); bool analyze();
/// Parses and analyzes all source units that were added /// Parses and analyzes all source units that were added
/// @returns false on error. /// @returns false on error.
bool parseAndAnalyze(); bool parseAndAnalyze();
/// Sets the given source code as the only source unit apart from standard sources and parses and analyzes it.
/// @returns false on error.
bool parseAndAnalyze(std::string const& _sourceCode);
/// @returns a list of the contract names in the sources. /// @returns a list of the contract names in the sources.
std::vector<std::string> contractNames() const; std::vector<std::string> contractNames() const;
/// Compiles the source units that were previously added and parsed. /// Compiles the source units that were previously added and parsed.
/// @returns false on error. /// @returns false on error.
bool compile( bool compile();
bool _optimize = false,
unsigned _runs = 200,
std::map<std::string, h160> const& _libraries = std::map<std::string, h160>{}
);
/// Parses and compiles the given source code.
/// @returns false on error.
bool compile(std::string const& _sourceCode, bool _optimize = false, unsigned _runs = 200);
/// @returns the assembled object for a contract. /// @returns the assembled object for a contract.
eth::LinkerObject const& object(std::string const& _contractName = "") const; eth::LinkerObject const& object(std::string const& _contractName = "") const;
@ -202,6 +201,7 @@ public:
/// @returns the list of errors that occured during parsing and type checking. /// @returns the list of errors that occured during parsing and type checking.
ErrorList const& errors() { return m_errorReporter.errors(); } ErrorList const& errors() { return m_errorReporter.errors(); }
/// @returns the current state.
State state() const { return m_stackState; } State state() const { return m_stackState; }
private: private:

View File

@ -249,6 +249,7 @@ Json::Value StandardCompiler::compileInternal(Json::Value const& _input)
Json::Value optimizerSettings = settings.get("optimizer", Json::Value()); Json::Value optimizerSettings = settings.get("optimizer", Json::Value());
bool const optimize = optimizerSettings.get("enabled", Json::Value(false)).asBool(); bool const optimize = optimizerSettings.get("enabled", Json::Value(false)).asBool();
unsigned const optimizeRuns = optimizerSettings.get("runs", Json::Value(200u)).asUInt(); unsigned const optimizeRuns = optimizerSettings.get("runs", Json::Value(200u)).asUInt();
m_compilerStack.setOptimiserSettings(optimize, optimizeRuns);
map<string, h160> libraries; map<string, h160> libraries;
Json::Value jsonLibraries = settings.get("libraries", Json::Value()); Json::Value jsonLibraries = settings.get("libraries", Json::Value());
@ -259,6 +260,7 @@ Json::Value StandardCompiler::compileInternal(Json::Value const& _input)
// @TODO use libraries only for the given source // @TODO use libraries only for the given source
libraries[library] = h160(jsonSourceName[library].asString()); libraries[library] = h160(jsonSourceName[library].asString());
} }
m_compilerStack.setLibraries(libraries);
Json::Value metadataSettings = settings.get("metadata", Json::Value()); Json::Value metadataSettings = settings.get("metadata", Json::Value());
m_compilerStack.useMetadataLiteralSources(metadataSettings.get("useLiteralContent", Json::Value(false)).asBool()); m_compilerStack.useMetadataLiteralSources(metadataSettings.get("useLiteralContent", Json::Value(false)).asBool());
@ -267,7 +269,7 @@ Json::Value StandardCompiler::compileInternal(Json::Value const& _input)
try try
{ {
m_compilerStack.compile(optimize, optimizeRuns, libraries); m_compilerStack.compile();
for (auto const& error: m_compilerStack.errors()) for (auto const& error: m_compilerStack.errors())
{ {

View File

@ -774,10 +774,14 @@ bool CommandLineInterface::processInput()
m_compiler->setRemappings(m_args[g_argInputFile].as<vector<string>>()); m_compiler->setRemappings(m_args[g_argInputFile].as<vector<string>>());
for (auto const& sourceCode: m_sourceCodes) for (auto const& sourceCode: m_sourceCodes)
m_compiler->addSource(sourceCode.first, sourceCode.second); m_compiler->addSource(sourceCode.first, sourceCode.second);
if (m_args.count(g_argLibraries))
m_compiler->setLibraries(m_libraries);
// TODO: Perhaps we should not compile unless requested // TODO: Perhaps we should not compile unless requested
bool optimize = m_args.count(g_argOptimize) > 0; bool optimize = m_args.count(g_argOptimize) > 0;
unsigned runs = m_args[g_argOptimizeRuns].as<unsigned>(); unsigned runs = m_args[g_argOptimizeRuns].as<unsigned>();
bool successful = m_compiler->compile(optimize, runs, m_libraries); m_compiler->setOptimiserSettings(optimize, runs);
bool successful = m_compiler->compile();
for (auto const& error: m_compiler->errors()) for (auto const& error: m_compiler->errors())
SourceReferenceFormatter::printExceptionInformation( SourceReferenceFormatter::printExceptionInformation(

View File

@ -223,7 +223,8 @@ protected:
{ {
m_compiler.reset(false); m_compiler.reset(false);
m_compiler.addSource("", registrarCode); m_compiler.addSource("", registrarCode);
ETH_TEST_REQUIRE_NO_THROW(m_compiler.compile(m_optimize, m_optimizeRuns), "Compiling contract failed"); m_compiler.setOptimiserSettings(m_optimize, m_optimizeRuns);
ETH_TEST_REQUIRE_NO_THROW(m_compiler.compile(), "Compiling contract failed");
s_compiledRegistrar.reset(new bytes(m_compiler.object("GlobalRegistrar").bytecode)); s_compiledRegistrar.reset(new bytes(m_compiler.object("GlobalRegistrar").bytecode));
} }
sendMessage(*s_compiledRegistrar, true); sendMessage(*s_compiledRegistrar, true);

View File

@ -135,7 +135,8 @@ protected:
{ {
m_compiler.reset(false); m_compiler.reset(false);
m_compiler.addSource("", registrarCode); m_compiler.addSource("", registrarCode);
ETH_TEST_REQUIRE_NO_THROW(m_compiler.compile(m_optimize, m_optimizeRuns), "Compiling contract failed"); m_compiler.setOptimiserSettings(m_optimize, m_optimizeRuns);
ETH_TEST_REQUIRE_NO_THROW(m_compiler.compile(), "Compiling contract failed");
s_compiledRegistrar.reset(new bytes(m_compiler.object("FixedFeeRegistrar").bytecode)); s_compiledRegistrar.reset(new bytes(m_compiler.object("FixedFeeRegistrar").bytecode));
} }
sendMessage(*s_compiledRegistrar, true); sendMessage(*s_compiledRegistrar, true);

View File

@ -450,7 +450,8 @@ protected:
{ {
m_compiler.reset(false); m_compiler.reset(false);
m_compiler.addSource("", walletCode); m_compiler.addSource("", walletCode);
ETH_TEST_REQUIRE_NO_THROW(m_compiler.compile(m_optimize, m_optimizeRuns), "Compiling contract failed"); m_compiler.setOptimiserSettings(m_optimize, m_optimizeRuns);
ETH_TEST_REQUIRE_NO_THROW(m_compiler.compile(), "Compiling contract failed");
s_compiledWallet.reset(new bytes(m_compiler.object("Wallet").bytecode)); s_compiledWallet.reset(new bytes(m_compiler.object("Wallet").bytecode));
} }
bytes args = encodeArgs(u256(0x60), _required, _dailyLimit, u256(_owners.size()), _owners); bytes args = encodeArgs(u256(0x60), _required, _dailyLimit, u256(_owners.size()), _owners);

View File

@ -49,7 +49,8 @@ public:
{ {
m_compiler.reset(false); m_compiler.reset(false);
m_compiler.addSource("", "pragma solidity >=0.0;\n" + _sourceCode); m_compiler.addSource("", "pragma solidity >=0.0;\n" + _sourceCode);
ETH_TEST_REQUIRE_NO_THROW(m_compiler.compile(dev::test::Options::get().optimize), "Compiling contract failed"); m_compiler.setOptimiserSettings(dev::test::Options::get().optimize);
ETH_TEST_REQUIRE_NO_THROW(m_compiler.compile(), "Compiling contract failed");
AssemblyItems const* items = m_compiler.runtimeAssemblyItems(""); AssemblyItems const* items = m_compiler.runtimeAssemblyItems("");
ASTNode const& sourceUnit = m_compiler.ast(); ASTNode const& sourceUnit = m_compiler.ast();

View File

@ -44,7 +44,8 @@ BOOST_AUTO_TEST_CASE(metadata_stamp)
)"; )";
CompilerStack compilerStack; CompilerStack compilerStack;
compilerStack.addSource("", std::string(sourceCode)); compilerStack.addSource("", std::string(sourceCode));
ETH_TEST_REQUIRE_NO_THROW(compilerStack.compile(dev::test::Options::get().optimize), "Compiling contract failed"); compilerStack.setOptimiserSettings(dev::test::Options::get().optimize);
ETH_TEST_REQUIRE_NO_THROW(compilerStack.compile(), "Compiling contract failed");
bytes const& bytecode = compilerStack.runtimeObject("test").bytecode; bytes const& bytecode = compilerStack.runtimeObject("test").bytecode;
std::string const& metadata = compilerStack.metadata("test"); std::string const& metadata = compilerStack.metadata("test");
BOOST_CHECK(dev::test::isValidMetadata(metadata)); BOOST_CHECK(dev::test::isValidMetadata(metadata));

View File

@ -56,7 +56,9 @@ public:
std::string sourceCode = "pragma solidity >=0.0;\n" + _sourceCode; std::string sourceCode = "pragma solidity >=0.0;\n" + _sourceCode;
m_compiler.reset(false); m_compiler.reset(false);
m_compiler.addSource("", sourceCode); m_compiler.addSource("", sourceCode);
if (!m_compiler.compile(m_optimize, m_optimizeRuns, _libraryAddresses)) m_compiler.setLibraries(_libraryAddresses);
m_compiler.setOptimiserSettings(m_optimize, m_optimizeRuns);
if (!m_compiler.compile())
{ {
for (auto const& error: m_compiler.errors()) for (auto const& error: m_compiler.errors())
SourceReferenceFormatter::printExceptionInformation( SourceReferenceFormatter::printExceptionInformation(

View File

@ -65,7 +65,9 @@ public:
void expectNatspecError(std::string const& _code) void expectNatspecError(std::string const& _code)
{ {
BOOST_CHECK(!m_compilerStack.parseAndAnalyze(_code)); m_compilerStack.reset(false);
m_compilerStack.addSource("", "pragma solidity >=0.0;\n" + _code);
BOOST_CHECK(!m_compilerStack.parseAndAnalyze());
BOOST_REQUIRE(Error::containsErrorOfType(m_compilerStack.errors(), Error::Type::DocstringParsingError)); BOOST_REQUIRE(Error::containsErrorOfType(m_compilerStack.errors(), Error::Type::DocstringParsingError));
} }