Allow and require use-src to be repeated for each object.

This commit is contained in:
chriseth 2021-08-31 17:22:13 +02:00
parent 13691dfbaa
commit 5caa15879b
31 changed files with 67 additions and 41 deletions

View File

@ -133,7 +133,7 @@ string IRGenerator::generate(
}; };
Whiskers t(R"( Whiskers t(R"(
/// @use-src <useSrcMap> /// @use-src <useSrcMapCreation>
object "<CreationObject>" { object "<CreationObject>" {
code { code {
<sourceLocationComment> <sourceLocationComment>
@ -147,6 +147,7 @@ string IRGenerator::generate(
<deploy> <deploy>
<functions> <functions>
} }
/// @use-src <useSrcMapDeployed>
object "<DeployedObject>" { object "<DeployedObject>" {
code { code {
<sourceLocationComment> <sourceLocationComment>
@ -177,7 +178,7 @@ string IRGenerator::generate(
", " ", "
); );
t("useSrcMap", useSrcMap); t("useSrcMapCreation", useSrcMap);
t("sourceLocationComment", sourceLocationComment(_contract, m_context)); t("sourceLocationComment", sourceLocationComment(_contract, m_context));
t("CreationObject", IRNames::creationObject(_contract)); t("CreationObject", IRNames::creationObject(_contract));
@ -219,6 +220,7 @@ string IRGenerator::generate(
m_context.initializeInternalDispatch(move(internalDispatchMap)); m_context.initializeInternalDispatch(move(internalDispatchMap));
// Do not register immutables to avoid assignment. // Do not register immutables to avoid assignment.
t("useSrcMapDeployed", useSrcMap);
t("DeployedObject", IRNames::deployedObject(_contract)); t("DeployedObject", IRNames::deployedObject(_contract));
t("library_address", IRNames::libraryAddressImmutable()); t("library_address", IRNames::libraryAddressImmutable());
t("dispatch", dispatchRoutine(_contract)); t("dispatch", dispatchRoutine(_contract));

View File

@ -50,38 +50,32 @@ string indent(std::string const& _input)
} }
string Data::toString(Dialect const*, optional<SourceNameMap>) const string Data::toString(Dialect const*) const
{ {
return "data \"" + name.str() + "\" hex\"" + util::toHex(data) + "\""; return "data \"" + name.str() + "\" hex\"" + util::toHex(data) + "\"";
} }
string Object::toString(Dialect const* _dialect) const string Object::toString(Dialect const* _dialect) const
{ {
yulAssert(code, "No code");
yulAssert(debugData, "No debug data");
string useSrcComment; string useSrcComment;
if (debugData && debugData->sourceNames) if (debugData->sourceNames)
useSrcComment = useSrcComment =
"/// @use-src " + "/// @use-src " +
joinHumanReadable(ranges::views::transform(*debugData->sourceNames, [](auto&& _pair) { joinHumanReadable(ranges::views::transform(*debugData->sourceNames, [](auto&& _pair) {
return to_string(_pair.first) + ":" + util::escapeAndQuoteString(*_pair.second); return to_string(_pair.first) + ":" + util::escapeAndQuoteString(*_pair.second);
})) + })) +
"\n"; "\n";
return useSrcComment + toString(_dialect, debugData ? debugData->sourceNames : optional<SourceNameMap>{});
}
string Object::toString(Dialect const* _dialect, std::optional<SourceNameMap> _sourceNames) const string inner = "code " + AsmPrinter{_dialect, debugData->sourceNames}(*code);
{
yulAssert(code, "No code");
string inner = "code " + AsmPrinter{_dialect, _sourceNames}(*code);
for (auto const& obj: subObjects) for (auto const& obj: subObjects)
{ inner += "\n" + obj->toString(_dialect);
if (auto const* o = dynamic_cast<Object const*>(obj.get()))
yulAssert(!o->debugData || !o->debugData->sourceNames, "");
inner += "\n" + obj->toString(_dialect, _sourceNames);
}
return "object \"" + name.str() + "\" {\n" + indent(inner) + "\n}"; return useSrcComment + "object \"" + name.str() + "\" {\n" + indent(inner) + "\n}";
} }
set<YulString> Object::qualifiedDataNames() const set<YulString> Object::qualifiedDataNames() const

View File

@ -49,11 +49,8 @@ struct ObjectNode
/// Name of the object. /// Name of the object.
/// Can be empty since .yul files can also just contain code, without explicitly placing it in an object. /// Can be empty since .yul files can also just contain code, without explicitly placing it in an object.
YulString name; YulString name;
protected:
virtual std::string toString(Dialect const* _dialect, std::optional<SourceNameMap> _sourceNames) const = 0;
/// Object should have access to toString virtual std::string toString(Dialect const* _dialect) const = 0;
friend struct Object;
}; };
/** /**
@ -65,8 +62,7 @@ struct Data: public ObjectNode
bytes data; bytes data;
protected: std::string toString(Dialect const* _dialect) const override;
std::string toString(Dialect const* _dialect, std::optional<SourceNameMap> _sourceNames) const override;
}; };
@ -114,8 +110,6 @@ public:
/// @returns the name of the special metadata data object. /// @returns the name of the special metadata data object.
static std::string metadataName() { return ".metadata"; } static std::string metadataName() { return ".metadata"; }
protected:
std::string toString(Dialect const* _dialect, std::optional<SourceNameMap> _sourceNames) const override;
}; };
} }

View File

@ -45,14 +45,15 @@ shared_ptr<Object> ObjectParser::parse(shared_ptr<Scanner> const& _scanner, bool
{ {
shared_ptr<Object> object; shared_ptr<Object> object;
m_scanner = _scanner; m_scanner = _scanner;
m_sourceNameMapping = tryParseSourceNameMapping();
if (currentToken() == Token::LBrace) if (currentToken() == Token::LBrace)
{ {
// Special case: Code-only form. // Special case: Code-only form.
object = make_shared<Object>(); object = make_shared<Object>();
object->name = "object"_yulstring; object->name = "object"_yulstring;
object->code = parseBlock(); auto sourceNameMapping = tryParseSourceNameMapping();
object->debugData = make_shared<ObjectDebugData>(ObjectDebugData{sourceNameMapping});
object->code = parseBlock(sourceNameMapping);
if (!object->code) if (!object->code)
return nullptr; return nullptr;
} }
@ -60,7 +61,6 @@ shared_ptr<Object> ObjectParser::parse(shared_ptr<Scanner> const& _scanner, bool
object = parseObject(); object = parseObject();
if (!_reuseScanner) if (!_reuseScanner)
expectToken(Token::EOS); expectToken(Token::EOS);
object->debugData = make_shared<ObjectDebugData>(ObjectDebugData{m_sourceNameMapping});
return object; return object;
} }
catch (FatalError const&) catch (FatalError const&)
@ -75,16 +75,20 @@ shared_ptr<Object> ObjectParser::parseObject(Object* _containingObject)
{ {
RecursionGuard guard(*this); RecursionGuard guard(*this);
shared_ptr<Object> ret = make_shared<Object>();
auto sourceNameMapping = tryParseSourceNameMapping();
ret->debugData = make_shared<ObjectDebugData>(ObjectDebugData{sourceNameMapping});
if (currentToken() != Token::Identifier || currentLiteral() != "object") if (currentToken() != Token::Identifier || currentLiteral() != "object")
fatalParserError(4294_error, "Expected keyword \"object\"."); fatalParserError(4294_error, "Expected keyword \"object\".");
advance(); advance();
shared_ptr<Object> ret = make_shared<Object>();
ret->name = parseUniqueName(_containingObject); ret->name = parseUniqueName(_containingObject);
expectToken(Token::LBrace); expectToken(Token::LBrace);
ret->code = parseCode(); ret->code = parseCode(move(sourceNameMapping));
while (currentToken() != Token::RBrace) while (currentToken() != Token::RBrace)
{ {
@ -103,13 +107,13 @@ shared_ptr<Object> ObjectParser::parseObject(Object* _containingObject)
return ret; return ret;
} }
shared_ptr<Block> ObjectParser::parseCode() shared_ptr<Block> ObjectParser::parseCode(optional<SourceNameMap> _sourceNames)
{ {
if (currentToken() != Token::Identifier || currentLiteral() != "code") if (currentToken() != Token::Identifier || currentLiteral() != "code")
fatalParserError(4846_error, "Expected keyword \"code\"."); fatalParserError(4846_error, "Expected keyword \"code\".");
advance(); advance();
return parseBlock(); return parseBlock(move(_sourceNames));
} }
optional<SourceNameMap> ObjectParser::tryParseSourceNameMapping() const optional<SourceNameMap> ObjectParser::tryParseSourceNameMapping() const
@ -166,9 +170,9 @@ optional<SourceNameMap> ObjectParser::tryParseSourceNameMapping() const
return nullopt; return nullopt;
} }
shared_ptr<Block> ObjectParser::parseBlock() shared_ptr<Block> ObjectParser::parseBlock(optional<SourceNameMap> _sourceNames)
{ {
Parser parser(m_errorReporter, m_dialect, m_sourceNameMapping); Parser parser(m_errorReporter, m_dialect, move(_sourceNames));
shared_ptr<Block> block = parser.parseInline(m_scanner); shared_ptr<Block> block = parser.parseInline(m_scanner);
yulAssert(block || m_errorReporter.hasErrors(), "Invalid block but no error!"); yulAssert(block || m_errorReporter.hasErrors(), "Invalid block but no error!");
return block; return block;

View File

@ -55,13 +55,11 @@ public:
/// @returns an empty shared pointer on error. /// @returns an empty shared pointer on error.
std::shared_ptr<Object> parse(std::shared_ptr<langutil::Scanner> const& _scanner, bool _reuseScanner); std::shared_ptr<Object> parse(std::shared_ptr<langutil::Scanner> const& _scanner, bool _reuseScanner);
std::optional<SourceNameMap> const& sourceNameMapping() const noexcept { return m_sourceNameMapping; }
private: private:
std::optional<SourceNameMap> tryParseSourceNameMapping() const; std::optional<SourceNameMap> tryParseSourceNameMapping() const;
std::shared_ptr<Object> parseObject(Object* _containingObject = nullptr); std::shared_ptr<Object> parseObject(Object* _containingObject = nullptr);
std::shared_ptr<Block> parseCode(); std::shared_ptr<Block> parseCode(std::optional<SourceNameMap> _sourceNames);
std::shared_ptr<Block> parseBlock(); std::shared_ptr<Block> parseBlock(std::optional<SourceNameMap> _sourceNames);
void parseData(Object& _containingObject); void parseData(Object& _containingObject);
/// Tries to parse a name that is non-empty and unique inside the containing object. /// Tries to parse a name that is non-empty and unique inside the containing object.
@ -69,8 +67,6 @@ private:
void addNamedSubObject(Object& _container, YulString _name, std::shared_ptr<ObjectNode> _subObject); void addNamedSubObject(Object& _container, YulString _name, std::shared_ptr<ObjectNode> _subObject);
Dialect const& m_dialect; Dialect const& m_dialect;
std::optional<SourceNameMap> m_sourceNameMapping;
}; };
} }

View File

@ -93,6 +93,7 @@ Object EVMToEwasmTranslator::run(Object const& _object)
Object ret; Object ret;
ret.name = _object.name; ret.name = _object.name;
ret.code = make_shared<Block>(move(ast)); ret.code = make_shared<Block>(move(ast));
ret.debugData = _object.debugData;
ret.analysisInfo = make_shared<AsmAnalysisInfo>(); ret.analysisInfo = make_shared<AsmAnalysisInfo>();
ErrorList errors; ErrorList errors;

View File

@ -21,6 +21,7 @@ object "C_12" {
return(128, _1) return(128, _1)
} }
} }
/// @use-src 0:"constant_optimizer_yul/input.sol", 1:"#utility.yul"
object "C_12_deployed" { object "C_12_deployed" {
code { code {
{ {

View File

@ -38,6 +38,7 @@ object "C_81" {
} }
} }
/// @use-src 0:"exp_base_literal/input.sol", 1:"#utility.yul"
object "C_81_deployed" { object "C_81_deployed" {
code { code {
/// @src 0:82:370 /// @src 0:82:370

View File

@ -18,6 +18,7 @@ object "C_7" {
return(128, _1) return(128, _1)
} }
} }
/// @use-src 0:"ir_compiler_inheritance_nosubobjects/input.sol", 1:"#utility.yul"
object "C_7_deployed" { object "C_7_deployed" {
code { code {
{ {
@ -50,6 +51,7 @@ object "D_10" {
return(128, _1) return(128, _1)
} }
} }
/// @use-src 0:"ir_compiler_inheritance_nosubobjects/input.sol", 1:"#utility.yul"
object "D_10_deployed" { object "D_10_deployed" {
code { code {
{ {

View File

@ -18,6 +18,7 @@ object "C_3" {
return(128, _1) return(128, _1)
} }
} }
/// @use-src 0:"ir_compiler_subobjects/input.sol", 1:"#utility.yul"
object "C_3_deployed" { object "C_3_deployed" {
code { code {
{ {
@ -50,6 +51,7 @@ object "D_16" {
return(128, _1) return(128, _1)
} }
} }
/// @use-src 0:"ir_compiler_subobjects/input.sol", 1:"#utility.yul"
object "D_16_deployed" { object "D_16_deployed" {
code { code {
{ {
@ -88,6 +90,7 @@ object "D_16" {
revert(0, 0) revert(0, 0)
} }
} }
/// @use-src 0:"ir_compiler_subobjects/input.sol", 1:"#utility.yul"
object "C_3" { object "C_3" {
code { code {
{ {
@ -99,6 +102,7 @@ object "D_16" {
return(128, _1) return(128, _1)
} }
} }
/// @use-src 0:"ir_compiler_subobjects/input.sol", 1:"#utility.yul"
object "C_3_deployed" { object "C_3_deployed" {
code { code {
{ {

View File

@ -18,6 +18,7 @@ object "D_12" {
return(128, _1) return(128, _1)
} }
} }
/// @use-src 0:"ir_with_assembly_no_memoryguard_creation/input.sol", 1:"#utility.yul"
object "D_12_deployed" { object "D_12_deployed" {
code { code {
{ {

View File

@ -18,6 +18,7 @@ object "D_8" {
return(128, _1) return(128, _1)
} }
} }
/// @use-src 0:"ir_with_assembly_no_memoryguard_runtime/input.sol", 1:"#utility.yul"
object "D_8_deployed" { object "D_8_deployed" {
code { code {
{ {

View File

@ -22,6 +22,7 @@ object "C_12" {
return(128, _1) return(128, _1)
} }
} }
/// @use-src 0:"keccak_optimization_deploy_code/input.sol", 1:"#utility.yul"
object "C_12_deployed" { object "C_12_deployed" {
code { code {
{ {

View File

@ -18,6 +18,7 @@ object "C_7" {
return(128, _1) return(128, _1)
} }
} }
/// @use-src 0:"keccak_optimization_low_runs/input.sol", 1:"#utility.yul"
object "C_7_deployed" { object "C_7_deployed" {
code { code {
{ {

View File

@ -18,6 +18,7 @@ object "C_59" {
return(128, _1) return(128, _1)
} }
} }
/// @use-src 0:"name_simplifier/input.sol", 1:"#utility.yul"
object "C_59_deployed" { object "C_59_deployed" {
code { code {
{ {

View File

@ -18,6 +18,7 @@ object "Arraysum_34" {
return(128, _1) return(128, _1)
} }
} }
/// @use-src 0:"optimizer_array_sload/input.sol", 1:"#utility.yul"
object "Arraysum_34_deployed" { object "Arraysum_34_deployed" {
code { code {
{ {

View File

@ -53,6 +53,7 @@ object "C_15" {
} }
} }
/// @use-src 0:"revert_strings/input.sol", 1:"#utility.yul"
object "C_15_deployed" { object "C_15_deployed" {
code { code {
/// @src 0:59:147 /// @src 0:59:147

View File

@ -25,6 +25,7 @@ object \"C_7\" {
function revert_error_ca66f745a3ce8ff40e2ccaf1ad45db7774001b90d25810abd9040049be7bf4bb() function revert_error_ca66f745a3ce8ff40e2ccaf1ad45db7774001b90d25810abd9040049be7bf4bb()
{ revert(0, 0) } { revert(0, 0) }
} }
/// @use-src 0:\"A\", 1:\"#utility.yul\"
object \"C_7_deployed\" { object \"C_7_deployed\" {
code { code {
/// @src 0:79:121 /// @src 0:79:121

View File

@ -37,6 +37,7 @@ object \"C_7\" {
} }
} }
/// @use-src 0:\"A\", 1:\"#utility.yul\"
object \"C_7_deployed\" { object \"C_7_deployed\" {
code { code {
/// @src 0:79:121 /// @src 0:79:121

View File

@ -37,6 +37,7 @@ object \"C_3\" {
} }
} }
/// @use-src 0:\"A\", 1:\"#utility.yul\"
object \"C_3_deployed\" { object \"C_3_deployed\" {
code { code {
/// @src 0:79:92 /// @src 0:79:92
@ -113,6 +114,7 @@ object \"D_16\" {
} }
} }
/// @use-src 0:\"A\", 1:\"#utility.yul\"
object \"D_16_deployed\" { object \"D_16_deployed\" {
code { code {
/// @src 0:93:146 /// @src 0:93:146
@ -244,6 +246,7 @@ object \"D_16\" {
} }
} }
/// @use-src 0:\"A\", 1:\"#utility.yul\"
object \"C_3_deployed\" { object \"C_3_deployed\" {
code { code {
/// @src 0:79:92 /// @src 0:79:92

View File

@ -38,6 +38,7 @@ object "test_11" {
} }
} }
/// @use-src 0:"viair_abicoder_v1/input.sol", 1:"#utility.yul"
object "test_11_deployed" { object "test_11_deployed" {
code { code {
/// @src 0:79:169 /// @src 0:79:169

View File

@ -24,6 +24,7 @@ object "C_3" {
return(128, _1) return(128, _1)
} }
} }
/// @use-src 0:"viair_subobjects/input.sol", 1:"#utility.yul"
object "C_3_deployed" { object "C_3_deployed" {
code { code {
{ {
@ -62,6 +63,7 @@ object "D_16" {
return(128, _1) return(128, _1)
} }
} }
/// @use-src 0:"viair_subobjects/input.sol", 1:"#utility.yul"
object "D_16_deployed" { object "D_16_deployed" {
code { code {
{ {
@ -100,6 +102,7 @@ object "D_16" {
revert(0, 0) revert(0, 0)
} }
} }
/// @use-src 0:"viair_subobjects/input.sol", 1:"#utility.yul"
object "C_3" { object "C_3" {
code { code {
{ {
@ -111,6 +114,7 @@ object "D_16" {
return(128, _1) return(128, _1)
} }
} }
/// @use-src 0:"viair_subobjects/input.sol", 1:"#utility.yul"
object "C_3_deployed" { object "C_3_deployed" {
code { code {
{ {

View File

@ -25,6 +25,7 @@ object "C_7" {
function revert_error_ca66f745a3ce8ff40e2ccaf1ad45db7774001b90d25810abd9040049be7bf4bb() function revert_error_ca66f745a3ce8ff40e2ccaf1ad45db7774001b90d25810abd9040049be7bf4bb()
{ revert(0, 0) } { revert(0, 0) }
} }
/// @use-src 0:"yul_optimizer_steps/input.sol", 1:"#utility.yul"
object "C_7_deployed" { object "C_7_deployed" {
code { code {
{ {

View File

@ -156,6 +156,7 @@ object \"C_54\" {
} }
} }
/// @use-src 0:\"C\", 1:\"D\", 2:\"#utility.yul\"
object \"C_54_deployed\" { object \"C_54_deployed\" {
code { code {
/// @src 0:79:428 /// @src 0:79:428
@ -780,6 +781,7 @@ object \"D_72\" {
} }
} }
/// @use-src 0:\"C\", 1:\"D\", 2:\"#utility.yul\"
object \"D_72_deployed\" { object \"D_72_deployed\" {
code { code {
/// @src 1:91:166 /// @src 1:91:166

View File

@ -37,6 +37,7 @@ object \"C_11\" {
} }
} }
/// @use-src 0:\"A\", 1:\"#utility.yul\"
object \"C_11_deployed\" { object \"C_11_deployed\" {
code { code {
/// @src 0:78:164 /// @src 0:78:164

View File

@ -37,6 +37,7 @@ object \"C_11\" {
} }
} }
/// @use-src 0:\"A\", 1:\"#utility.yul\"
object \"C_11_deployed\" { object \"C_11_deployed\" {
code { code {
/// @src 0:78:158 /// @src 0:78:158

View File

@ -37,6 +37,7 @@ object \"C_11\" {
} }
} }
/// @use-src 0:\"A\", 1:\"#utility.yul\"
object \"C_11_deployed\" { object \"C_11_deployed\" {
code { code {
/// @src 0:78:159 /// @src 0:78:159

View File

@ -37,6 +37,7 @@ object \"C_11\" {
} }
} }
/// @use-src 0:\"A\", 1:\"#utility.yul\"
object \"C_11_deployed\" { object \"C_11_deployed\" {
code { code {
/// @src 0:78:243 /// @src 0:78:243

View File

@ -37,6 +37,7 @@ object \"C_11\" {
} }
} }
/// @use-src 0:\"A\", 1:\"#utility.yul\"
object \"C_11_deployed\" { object \"C_11_deployed\" {
code { code {
/// @src 0:78:159 /// @src 0:78:159

View File

@ -121,8 +121,9 @@ tuple<optional<SourceNameMap>, ErrorList> tryGetSourceLocationMapping(string _so
Dialect const& dialect = yul::EVMDialect::strictAssemblyForEVM(EVMVersion::berlin()); Dialect const& dialect = yul::EVMDialect::strictAssemblyForEVM(EVMVersion::berlin());
ObjectParser objectParser{reporter, dialect}; ObjectParser objectParser{reporter, dialect};
CharStream stream(move(source), ""); CharStream stream(move(source), "");
objectParser.parse(make_shared<Scanner>(stream), false); auto object = objectParser.parse(make_shared<Scanner>(stream), false);
return {objectParser.sourceNameMapping(), std::move(errors)}; BOOST_REQUIRE(object && object->debugData);
return {object->debugData->sourceNames, std::move(errors)};
} }
} }

View File

@ -10,6 +10,7 @@ object "a" {
datasize("sub") datasize("sub")
) )
} }
/// @use-src 3: "abc.sol" , 2: "def.sol"
object "sub" { object "sub" {
code { code {
/// @src 2:70:72 /// @src 2:70:72