/*
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 .
*/
// SPDX-License-Identifier: GPL-3.0
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
using namespace solidity;
using namespace solidity::langutil;
using namespace solidity::yul;
using namespace solidity::util;
using namespace solidity::phaser;
namespace solidity::phaser
{
ostream& operator<<(ostream& _stream, Program const& _program);
}
ostream& std::operator<<(ostream& _outputStream, ErrorList const& _errors)
{
SourceReferenceFormatter formatter(_outputStream);
for (auto const& error: _errors)
formatter.printErrorInformation(*error);
return _outputStream;
}
Program::Program(Program const& program):
m_ast(make_unique(get(ASTCopier{}(*program.m_ast)))),
m_dialect{program.m_dialect},
m_nameDispenser(program.m_nameDispenser)
{
}
variant Program::load(CharStream& _sourceCode)
{
// ASSUMPTION: parseSource() rewinds the stream on its own
Dialect const& dialect = EVMDialect::strictAssemblyForEVMObjects(EVMVersion{});
variant, ErrorList> astOrErrors = parseObject(dialect, _sourceCode);
if (holds_alternative(astOrErrors))
return get(astOrErrors);
variant, ErrorList> analysisInfoOrErrors = analyzeAST(
dialect,
*get>(astOrErrors)
);
if (holds_alternative(analysisInfoOrErrors))
return get(analysisInfoOrErrors);
Program program(
dialect,
disambiguateAST(
dialect,
*get>(astOrErrors),
*get>(analysisInfoOrErrors)
)
);
program.optimise({
FunctionHoister::name,
FunctionGrouper::name,
ForLoopInitRewriter::name,
});
return program;
}
void Program::optimise(vector const& _optimisationSteps)
{
m_ast = applyOptimisationSteps(m_dialect, m_nameDispenser, move(m_ast), _optimisationSteps);
}
ostream& phaser::operator<<(ostream& _stream, Program const& _program)
{
return _stream << AsmPrinter()(*_program.m_ast);
}
string Program::toJson() const
{
Json::Value serializedAst = AsmJsonConverter(0)(*m_ast);
return jsonPrettyPrint(removeNullMembers(std::move(serializedAst)));
}
variant, ErrorList> Program::parseObject(Dialect const& _dialect, CharStream _source)
{
ErrorList errors;
ErrorReporter errorReporter(errors);
auto scanner = make_shared(move(_source));
ObjectParser parser(errorReporter, _dialect);
shared_ptr