[libsolidity] interface/CompilerStack.h: Remove m_evmAssemblyJson.

This commit is contained in:
Alexander Arlt 2022-08-17 20:12:40 +02:00
parent 4f76877753
commit 5c9c88ccc9
2 changed files with 40 additions and 26 deletions

View File

@ -410,7 +410,7 @@ void CompilerStack::importASTs(map<string, Json::Value> const& _sources)
m_sources[path] = std::move(source);
}
m_stackState = ParsedAndImported;
m_importedSources = true;
m_importedSourceType = ImportedSourceType::SolidityAST;
storeContractDefinitions();
}
@ -418,11 +418,12 @@ void CompilerStack::importASTs(map<string, Json::Value> const& _sources)
void CompilerStack::importEvmAssemblyJson(std::map<std::string, Json::Value> const& _sources)
{
solAssert(_sources.size() == 1, "");
solAssert(m_sources.empty(), "");
solAssert(m_sourceJsons.empty(), "");
solAssert(m_sourceOrder.empty(), "");
if (m_stackState != Empty)
solThrow(CompilerError, "Must call importEvmAssemblyJson only before the SourcesSet state.");
m_sourceJsons = _sources;
Json::Value jsonValue = _sources.begin()->second;
if (jsonValue.isMember("sourceList"))
for (auto const& item: jsonValue["sourceList"])
@ -432,8 +433,8 @@ void CompilerStack::importEvmAssemblyJson(std::map<std::string, Json::Value> con
m_sources.emplace(std::make_pair(item.asString(), source));
m_sourceOrder.push_back(&m_sources[item.asString()]);
}
m_evmAssemblyJson[_sources.begin()->first] = std::move(jsonValue);
m_importedSources = true;
m_sourceJsons[_sources.begin()->first] = std::move(jsonValue);
m_importedSourceType = ImportedSourceType::EvmAssemblyJson;
m_stackState = SourcesSet;
}
@ -626,7 +627,7 @@ bool CompilerStack::parseAndAnalyze(State _stopAfter)
{
m_stopAfter = _stopAfter;
if (!m_evmAssemblyJson.empty())
if (m_importedSourceType == ImportedSourceType::EvmAssemblyJson)
return true;
bool success = parse();
@ -678,13 +679,12 @@ bool CompilerStack::compile(State _stopAfter)
// Only compile contracts individually which have been requested.
map<ContractDefinition const*, shared_ptr<Compiler const>> otherCompilers;
if (!m_evmAssemblyJson.empty())
if (m_importedSourceType == ImportedSourceType::EvmAssemblyJson)
{
solAssert(m_importedSources, "");
solAssert(m_evmAssemblyJson.size() == 1, "");
solAssert(m_sourceJsons.size() == 1, "");
string const evmSourceName = m_evmAssemblyJson.begin()->first;
Json::Value const evmJson = m_evmAssemblyJson.begin()->second;
string const evmSourceName = m_sourceJsons.begin()->first;
Json::Value const evmJson = m_sourceJsons.begin()->second;
// todo: remove code duplication.
evmasm::Assembly::OptimiserSettings optimiserSettings{false, false, false, false, false, false, m_evmVersion, 0};
@ -697,7 +697,7 @@ bool CompilerStack::compile(State _stopAfter)
optimiserSettings.expectedExecutionsPerDeployment = m_optimiserSettings.expectedExecutionsPerDeployment;
optimiserSettings.evmVersion = m_evmVersion;
m_contracts[evmSourceName].evmAssembly = evmasm::Assembly::loadFromAssemblyJSON(m_evmAssemblyJson[evmSourceName]);
m_contracts[evmSourceName].evmAssembly = evmasm::Assembly::loadFromAssemblyJSON(m_sourceJsons[evmSourceName]);
if (m_optimiserSettings.enabled)
m_contracts[evmSourceName].evmAssembly->optimise(optimiserSettings);
m_contracts[evmSourceName].object = m_contracts[evmSourceName].evmAssembly->assemble();
@ -991,21 +991,15 @@ vector<string> CompilerStack::sourceNames() const
return names;
}
map<string, unsigned> CompilerStack::sourceIndices(bool _includeInternalSources /* = true */) const
map<string, unsigned> CompilerStack::sourceIndices() const
{
map<string, unsigned> indices;
unsigned index = 0;
if (m_evmAssemblyJson.empty())
{
for (auto const& s: m_sources)
for (auto const& s: m_sources)
if (s.first != CompilerContext::yulUtilityFileName())
indices[s.first] = index++;
solAssert(!indices.count(CompilerContext::yulUtilityFileName()), "");
if (_includeInternalSources)
indices[CompilerContext::yulUtilityFileName()] = index++;
}
else
for (auto const& s: m_sourceOrder)
indices[s->charStream->source()] = index++;
if (indices.find(CompilerContext::yulUtilityFileName()) == indices.end())
indices[CompilerContext::yulUtilityFileName()] = index++;
return indices;
}
@ -1543,7 +1537,22 @@ string CompilerStack::createMetadata(Contract const& _contract, bool _forIR) con
{
Json::Value meta{Json::objectValue};
meta["version"] = 1;
meta["language"] = m_importedSources ? "SolidityAST" : "Solidity";
string sourceType;
switch (m_importedSourceType)
{
case ImportedSourceType::None:
sourceType = "Solidity";
break;
case ImportedSourceType::SolidityAST:
sourceType = "SolidityAST";
break;
case ImportedSourceType::EvmAssemblyJson:
sourceType = "EvmAssemblyJson";
break;
default:
solAssert(false);
}
meta["language"] = sourceType;
meta["compiler"]["version"] = VersionStringStrict;
/// All the source files (including self), which should be included in the metadata.

View File

@ -117,6 +117,12 @@ public:
None
};
enum class ImportedSourceType {
None,
SolidityAST,
EvmAssemblyJson
};
/// Creates a new compiler stack.
/// @param _readFile callback used to read files for import statements. Must return
/// and must not emit exceptions.
@ -244,7 +250,7 @@ public:
/// @returns a mapping assigning each source name its index inside the vector returned
/// by sourceNames().
std::map<std::string, unsigned> sourceIndices(bool _includeInternalSources = true) const;
std::map<std::string, unsigned> sourceIndices() const;
/// @returns the previously used character stream, useful for counting lines during error reporting.
langutil::CharStream const& charStream(std::string const& _sourceName) const override;
@ -502,7 +508,6 @@ private:
std::map<std::string const, Source> m_sources;
// if imported, store AST-JSONS for each filename
std::map<std::string, Json::Value> m_sourceJsons;
std::map<std::string, Json::Value> m_evmAssemblyJson;
std::vector<std::string> m_unhandledSMTLib2Queries;
std::map<util::h256, std::string> m_smtlib2Responses;
std::shared_ptr<GlobalContext> m_globalContext;
@ -516,7 +521,7 @@ private:
langutil::DebugInfoSelection m_debugInfoSelection = langutil::DebugInfoSelection::Default();
bool m_parserErrorRecovery = false;
State m_stackState = Empty;
bool m_importedSources = false;
ImportedSourceType m_importedSourceType = ImportedSourceType::None;
/// Whether or not there has been an error during processing.
/// If this is true, the stack will refuse to generate code.
bool m_hasError = false;