Parse default dialect and omit when printing.

This commit is contained in:
chriseth 2020-01-16 12:03:19 +01:00
parent 90c98a3289
commit fbe5bb0cce
10 changed files with 27 additions and 10 deletions

View File

@ -387,7 +387,7 @@ void CompilerContext::appendInlineAssembly(
yul::EVMDialect const& dialect = yul::EVMDialect::strictAssemblyForEVM(m_evmVersion);
auto parserResult = yul::Parser(errorReporter, dialect).parse(scanner, false);
#ifdef SOL_OUTPUT_ASM
cout << yul::AsmPrinter()(*parserResult) << endl;
cout << yul::AsmPrinter(&dialect)(*parserResult) << endl;
#endif
auto reportError = [&](string const& _context)
@ -438,7 +438,7 @@ void CompilerContext::appendInlineAssembly(
#ifdef SOL_OUTPUT_ASM
cout << "After optimizer:" << endl;
cout << yul::AsmPrinter()(*parserResult) << endl;
cout << yul::AsmPrinter(&dialect)(*parserResult) << endl;
#endif
}

View File

@ -892,6 +892,7 @@ bool IRGeneratorForStatements::visit(InlineAssembly const& _inlineAsm)
solAssert(holds_alternative<yul::Block>(modified), "");
// Do not provide dialect so that we get the full type information.
m_code << yul::AsmPrinter()(std::get<yul::Block>(std::move(modified))) << "\n";
return false;
}

View File

@ -608,7 +608,7 @@ Scope& AsmAnalyzer::scope(Block const* _block)
}
void AsmAnalyzer::expectValidType(YulString _type, SourceLocation const& _location)
{
if (!_type.empty() && !contains(m_dialect.types, _type))
if (!_type.empty() && !m_dialect.types.count(_type))
m_errorReporter.typeError(
_location,
"\"" + _type.str() + "\" is not a valid type (user defined types are not yet supported)."

View File

@ -366,7 +366,7 @@ Parser::ElementaryOperation Parser::parseElementaryOperation()
location(),
kind,
YulString{currentLiteral()},
{}
kind == LiteralKind::Boolean ? m_dialect.boolType : m_dialect.defaultType
};
advance();
if (currentToken() == Token::Colon)
@ -497,6 +497,9 @@ TypedName Parser::parseTypedName()
typedName.location.end = endPosition();
typedName.type = expectAsmIdentifier();
}
else
typedName.type = m_dialect.defaultType;
return typedName;
}

View File

@ -23,6 +23,7 @@
#include <libyul/AsmPrinter.h>
#include <libyul/AsmData.h>
#include <libyul/Exceptions.h>
#include <libyul/Dialect.h>
#include <libsolutil/CommonData.h>
@ -238,7 +239,8 @@ string AsmPrinter::formatTypedName(TypedName _variable) const
string AsmPrinter::appendTypeName(YulString _type) const
{
if (!_type.empty())
if (_type.empty() || (m_dialect && _type == m_dialect->defaultType))
return {};
else
return ":" + _type.str();
return "";
}

View File

@ -30,10 +30,16 @@ namespace solidity::yul
{
struct Dialect;
/**
* Converts a parsed Yul AST into readable string representation.
* Ignores source locations.
* If a dialect is provided, the dialect's default type is omitted.
*/
class AsmPrinter
{
public:
explicit AsmPrinter() {}
AsmPrinter() {}
explicit AsmPrinter(Dialect const& _dialect): m_dialect(&_dialect) {}
std::string operator()(Literal const& _literal) const;
std::string operator()(Identifier const& _identifier) const;
@ -53,6 +59,8 @@ public:
private:
std::string formatTypedName(TypedName _variable) const;
std::string appendTypeName(YulString _type) const;
Dialect const* m_dialect = nullptr;
};
}

View File

@ -51,7 +51,7 @@ struct Dialect: boost::noncopyable
YulString defaultType;
/// Type used for the literals "true" and "false".
YulString boolType;
std::vector<YulString> types;
std::set<YulString> types;
/// @returns the builtin function of the given name or a nullptr if it is not a builtin function.
virtual BuiltinFunction const* builtin(YulString /*_name*/) const { return nullptr; }

View File

@ -218,6 +218,9 @@ EVMDialectTyped::EVMDialectTyped(langutil::EVMVersion _evmVersion, bool _objectA
EVMDialect(_evmVersion, _objectAccess)
{
defaultType = "u256"_yulstring;
boolType = "bool"_yulstring;
types = {defaultType, boolType};
m_functions["lt"_yulstring].returns = {"bool"_yulstring};
m_functions["gt"_yulstring].returns = {"bool"_yulstring};
m_functions["slt"_yulstring].returns = {"bool"_yulstring};

View File

@ -365,7 +365,7 @@ TestCase::TestResult YulOptimizerTest::run(ostream& _stream, string const& _line
return TestResult::FatalError;
}
m_obtainedResult = AsmPrinter{}(*m_ast) + "\n";
m_obtainedResult = AsmPrinter{*m_dialect}(*m_ast) + "\n";
if (m_optimizerStep != m_validatedSettings["step"])
{

View File

@ -248,7 +248,7 @@ public:
default:
cout << "Unknown option." << endl;
}
source = AsmPrinter{}(*m_ast);
source = AsmPrinter{m_dialect}(*m_ast);
}
}