mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Print @src and @use-src locations in AsmPrinter
This commit is contained in:
+90
-35
@@ -41,48 +41,55 @@ using namespace solidity;
|
||||
using namespace solidity::util;
|
||||
using namespace solidity::yul;
|
||||
|
||||
//@TODO source locations
|
||||
|
||||
string AsmPrinter::operator()(Literal const& _literal) const
|
||||
string AsmPrinter::operator()(Literal const& _literal)
|
||||
{
|
||||
string const locationComment = formatSourceLocationComment(_literal);
|
||||
|
||||
switch (_literal.kind)
|
||||
{
|
||||
case LiteralKind::Number:
|
||||
yulAssert(isValidDecimal(_literal.value.str()) || isValidHex(_literal.value.str()), "Invalid number literal");
|
||||
return _literal.value.str() + appendTypeName(_literal.type);
|
||||
return locationComment + _literal.value.str() + appendTypeName(_literal.type);
|
||||
case LiteralKind::Boolean:
|
||||
yulAssert(_literal.value == "true"_yulstring || _literal.value == "false"_yulstring, "Invalid bool literal.");
|
||||
return ((_literal.value == "true"_yulstring) ? "true" : "false") + appendTypeName(_literal.type, true);
|
||||
return locationComment + ((_literal.value == "true"_yulstring) ? "true" : "false") + appendTypeName(_literal.type, true);
|
||||
case LiteralKind::String:
|
||||
break;
|
||||
}
|
||||
|
||||
return escapeAndQuoteString(_literal.value.str()) + appendTypeName(_literal.type);
|
||||
return locationComment + escapeAndQuoteString(_literal.value.str()) + appendTypeName(_literal.type);
|
||||
}
|
||||
|
||||
string AsmPrinter::operator()(Identifier const& _identifier) const
|
||||
string AsmPrinter::operator()(Identifier const& _identifier)
|
||||
{
|
||||
yulAssert(!_identifier.name.empty(), "Invalid identifier.");
|
||||
return _identifier.name.str();
|
||||
return formatSourceLocationComment(_identifier) + _identifier.name.str();
|
||||
}
|
||||
|
||||
string AsmPrinter::operator()(ExpressionStatement const& _statement) const
|
||||
string AsmPrinter::operator()(ExpressionStatement const& _statement)
|
||||
{
|
||||
return std::visit(*this, _statement.expression);
|
||||
string const locationComment = formatSourceLocationComment(_statement);
|
||||
|
||||
return locationComment + std::visit(*this, _statement.expression);
|
||||
}
|
||||
|
||||
string AsmPrinter::operator()(Assignment const& _assignment) const
|
||||
string AsmPrinter::operator()(Assignment const& _assignment)
|
||||
{
|
||||
string const locationComment = formatSourceLocationComment(_assignment);
|
||||
|
||||
yulAssert(_assignment.variableNames.size() >= 1, "");
|
||||
string variables = (*this)(_assignment.variableNames.front());
|
||||
for (size_t i = 1; i < _assignment.variableNames.size(); ++i)
|
||||
variables += ", " + (*this)(_assignment.variableNames[i]);
|
||||
return variables + " := " + std::visit(*this, *_assignment.value);
|
||||
|
||||
return locationComment + variables + " := " + std::visit(*this, *_assignment.value);
|
||||
}
|
||||
|
||||
string AsmPrinter::operator()(VariableDeclaration const& _variableDeclaration) const
|
||||
string AsmPrinter::operator()(VariableDeclaration const& _variableDeclaration)
|
||||
{
|
||||
string out = "let ";
|
||||
string out = formatSourceLocationComment(_variableDeclaration);
|
||||
|
||||
out += "let ";
|
||||
out += boost::algorithm::join(
|
||||
_variableDeclaration.variables | ranges::views::transform(
|
||||
[this](TypedName argument) { return formatTypedName(argument); }
|
||||
@@ -97,10 +104,12 @@ string AsmPrinter::operator()(VariableDeclaration const& _variableDeclaration) c
|
||||
return out;
|
||||
}
|
||||
|
||||
string AsmPrinter::operator()(FunctionDefinition const& _functionDefinition) const
|
||||
string AsmPrinter::operator()(FunctionDefinition const& _functionDefinition)
|
||||
{
|
||||
yulAssert(!_functionDefinition.name.empty(), "Invalid function name.");
|
||||
string out = "function " + _functionDefinition.name.str() + "(";
|
||||
|
||||
string out = formatSourceLocationComment(_functionDefinition);
|
||||
out += "function " + _functionDefinition.name.str() + "(";
|
||||
out += boost::algorithm::join(
|
||||
_functionDefinition.parameters | ranges::views::transform(
|
||||
[this](TypedName argument) { return formatTypedName(argument); }
|
||||
@@ -122,30 +131,41 @@ string AsmPrinter::operator()(FunctionDefinition const& _functionDefinition) con
|
||||
return out + "\n" + (*this)(_functionDefinition.body);
|
||||
}
|
||||
|
||||
string AsmPrinter::operator()(FunctionCall const& _functionCall) const
|
||||
string AsmPrinter::operator()(FunctionCall const& _functionCall)
|
||||
{
|
||||
string const locationComment = formatSourceLocationComment(_functionCall);
|
||||
string const functionName = (*this)(_functionCall.functionName);
|
||||
return
|
||||
(*this)(_functionCall.functionName) + "(" +
|
||||
locationComment +
|
||||
functionName + "(" +
|
||||
boost::algorithm::join(
|
||||
_functionCall.arguments | ranges::views::transform([&](auto&& _node) { return std::visit(*this, _node); }),
|
||||
", " ) +
|
||||
")";
|
||||
}
|
||||
|
||||
string AsmPrinter::operator()(If const& _if) const
|
||||
string AsmPrinter::operator()(If const& _if)
|
||||
{
|
||||
yulAssert(_if.condition, "Invalid if condition.");
|
||||
|
||||
string out = formatSourceLocationComment(_if);
|
||||
out += "if " + std::visit(*this, *_if.condition);
|
||||
|
||||
string body = (*this)(_if.body);
|
||||
char delim = '\n';
|
||||
if (body.find('\n') == string::npos)
|
||||
delim = ' ';
|
||||
return "if " + std::visit(*this, *_if.condition) + delim + (*this)(_if.body);
|
||||
|
||||
return out + delim + body;
|
||||
}
|
||||
|
||||
string AsmPrinter::operator()(Switch const& _switch) const
|
||||
string AsmPrinter::operator()(Switch const& _switch)
|
||||
{
|
||||
yulAssert(_switch.expression, "Invalid expression pointer.");
|
||||
string out = "switch " + std::visit(*this, *_switch.expression);
|
||||
|
||||
string out = formatSourceLocationComment(_switch);
|
||||
out += "switch " + std::visit(*this, *_switch.expression);
|
||||
|
||||
for (auto const& _case: _switch.cases)
|
||||
{
|
||||
if (!_case.value)
|
||||
@@ -157,12 +177,15 @@ string AsmPrinter::operator()(Switch const& _switch) const
|
||||
return out;
|
||||
}
|
||||
|
||||
string AsmPrinter::operator()(ForLoop const& _forLoop) const
|
||||
string AsmPrinter::operator()(ForLoop const& _forLoop)
|
||||
{
|
||||
yulAssert(_forLoop.condition, "Invalid for loop condition.");
|
||||
string const locationComment = formatSourceLocationComment(_forLoop);
|
||||
|
||||
string pre = (*this)(_forLoop.pre);
|
||||
string condition = std::visit(*this, *_forLoop.condition);
|
||||
string post = (*this)(_forLoop.post);
|
||||
|
||||
char delim = '\n';
|
||||
if (
|
||||
pre.size() + condition.size() + post.size() < 60 &&
|
||||
@@ -171,46 +194,50 @@ string AsmPrinter::operator()(ForLoop const& _forLoop) const
|
||||
)
|
||||
delim = ' ';
|
||||
return
|
||||
locationComment +
|
||||
("for " + move(pre) + delim + move(condition) + delim + move(post) + "\n") +
|
||||
(*this)(_forLoop.body);
|
||||
}
|
||||
|
||||
string AsmPrinter::operator()(Break const&) const
|
||||
string AsmPrinter::operator()(Break const& _break)
|
||||
{
|
||||
return "break";
|
||||
return formatSourceLocationComment(_break) + "break";
|
||||
}
|
||||
|
||||
string AsmPrinter::operator()(Continue const&) const
|
||||
string AsmPrinter::operator()(Continue const& _continue)
|
||||
{
|
||||
return "continue";
|
||||
return formatSourceLocationComment(_continue) + "continue";
|
||||
}
|
||||
|
||||
string AsmPrinter::operator()(Leave const&) const
|
||||
// '_leave' and '__leave' is reserved in VisualStudio
|
||||
string AsmPrinter::operator()(Leave const& leave_)
|
||||
{
|
||||
return "leave";
|
||||
return formatSourceLocationComment(leave_) + "leave";
|
||||
}
|
||||
|
||||
string AsmPrinter::operator()(Block const& _block) const
|
||||
string AsmPrinter::operator()(Block const& _block)
|
||||
{
|
||||
string const locationComment = formatSourceLocationComment(_block);
|
||||
|
||||
if (_block.statements.empty())
|
||||
return "{ }";
|
||||
return locationComment + "{ }";
|
||||
string body = boost::algorithm::join(
|
||||
_block.statements | ranges::views::transform([&](auto&& _node) { return std::visit(*this, _node); }),
|
||||
"\n"
|
||||
);
|
||||
if (body.size() < 30 && body.find('\n') == string::npos)
|
||||
return "{ " + body + " }";
|
||||
return locationComment + "{ " + body + " }";
|
||||
else
|
||||
{
|
||||
boost::replace_all(body, "\n", "\n ");
|
||||
return "{\n " + body + "\n}";
|
||||
return locationComment + "{\n " + body + "\n}";
|
||||
}
|
||||
}
|
||||
|
||||
string AsmPrinter::formatTypedName(TypedName _variable) const
|
||||
string AsmPrinter::formatTypedName(TypedName _variable)
|
||||
{
|
||||
yulAssert(!_variable.name.empty(), "Invalid variable name.");
|
||||
return _variable.name.str() + appendTypeName(_variable.type);
|
||||
return formatSourceLocationComment(_variable) + _variable.name.str() + appendTypeName(_variable.type);
|
||||
}
|
||||
|
||||
string AsmPrinter::appendTypeName(YulString _type, bool _isBoolLiteral) const
|
||||
@@ -228,3 +255,31 @@ string AsmPrinter::appendTypeName(YulString _type, bool _isBoolLiteral) const
|
||||
else
|
||||
return ":" + _type.str();
|
||||
}
|
||||
|
||||
string AsmPrinter::formatSourceLocationComment(shared_ptr<DebugData const> const& _debugData, bool _statement)
|
||||
{
|
||||
if (
|
||||
!_debugData ||
|
||||
m_lastLocation == _debugData->location ||
|
||||
m_nameToSourceIndex.empty()
|
||||
)
|
||||
return "";
|
||||
|
||||
m_lastLocation = _debugData->location;
|
||||
|
||||
string sourceIndex = "-1";
|
||||
if (_debugData->location.sourceName)
|
||||
sourceIndex = to_string(m_nameToSourceIndex.at(*_debugData->location.sourceName));
|
||||
|
||||
string sourceLocation =
|
||||
"@src " +
|
||||
sourceIndex +
|
||||
":" +
|
||||
to_string(_debugData->location.start) +
|
||||
":" +
|
||||
to_string(_debugData->location.end);
|
||||
return
|
||||
_statement ?
|
||||
"/// " + sourceLocation + "\n" :
|
||||
"/** " + sourceLocation + " */ ";
|
||||
}
|
||||
|
||||
+47
-19
@@ -24,9 +24,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <libyul/ASTForward.h>
|
||||
|
||||
#include <libyul/YulString.h>
|
||||
|
||||
#include <libsolutil/CommonData.h>
|
||||
|
||||
#include <liblangutil/SourceLocation.h>
|
||||
|
||||
#include <map>
|
||||
|
||||
namespace solidity::yul
|
||||
{
|
||||
struct Dialect;
|
||||
@@ -39,29 +44,52 @@ struct Dialect;
|
||||
class AsmPrinter
|
||||
{
|
||||
public:
|
||||
AsmPrinter() {}
|
||||
explicit AsmPrinter(Dialect const& _dialect): m_dialect(&_dialect) {}
|
||||
explicit AsmPrinter(
|
||||
Dialect const* _dialect = nullptr,
|
||||
std::optional<std::map<unsigned, std::shared_ptr<std::string const>>> _sourceIndexToName = {}
|
||||
):
|
||||
m_dialect(_dialect)
|
||||
{
|
||||
if (_sourceIndexToName)
|
||||
for (auto&& [index, name]: *_sourceIndexToName)
|
||||
m_nameToSourceIndex[*name] = index;
|
||||
}
|
||||
|
||||
std::string operator()(Literal const& _literal) const;
|
||||
std::string operator()(Identifier const& _identifier) const;
|
||||
std::string operator()(ExpressionStatement const& _expr) const;
|
||||
std::string operator()(Assignment const& _assignment) const;
|
||||
std::string operator()(VariableDeclaration const& _variableDeclaration) const;
|
||||
std::string operator()(FunctionDefinition const& _functionDefinition) const;
|
||||
std::string operator()(FunctionCall const& _functionCall) const;
|
||||
std::string operator()(If const& _if) const;
|
||||
std::string operator()(Switch const& _switch) const;
|
||||
std::string operator()(ForLoop const& _forLoop) const;
|
||||
std::string operator()(Break const& _break) const;
|
||||
std::string operator()(Continue const& _continue) const;
|
||||
std::string operator()(Leave const& _continue) const;
|
||||
std::string operator()(Block const& _block) const;
|
||||
|
||||
explicit AsmPrinter(
|
||||
Dialect const& _dialect,
|
||||
std::optional<std::map<unsigned, std::shared_ptr<std::string const>>> _sourceIndexToName = {}
|
||||
): AsmPrinter(&_dialect, _sourceIndexToName) {}
|
||||
|
||||
std::string operator()(Literal const& _literal);
|
||||
std::string operator()(Identifier const& _identifier);
|
||||
std::string operator()(ExpressionStatement const& _expr);
|
||||
std::string operator()(Assignment const& _assignment);
|
||||
std::string operator()(VariableDeclaration const& _variableDeclaration);
|
||||
std::string operator()(FunctionDefinition const& _functionDefinition);
|
||||
std::string operator()(FunctionCall const& _functionCall);
|
||||
std::string operator()(If const& _if);
|
||||
std::string operator()(Switch const& _switch);
|
||||
std::string operator()(ForLoop const& _forLoop);
|
||||
std::string operator()(Break const& _break);
|
||||
std::string operator()(Continue const& _continue);
|
||||
std::string operator()(Leave const& _continue);
|
||||
std::string operator()(Block const& _block);
|
||||
|
||||
private:
|
||||
std::string formatTypedName(TypedName _variable) const;
|
||||
std::string formatTypedName(TypedName _variable);
|
||||
std::string appendTypeName(YulString _type, bool _isBoolLiteral = false) const;
|
||||
std::string formatSourceLocationComment(std::shared_ptr<DebugData const> const& _debugData, bool _statement);
|
||||
template <class T>
|
||||
std::string formatSourceLocationComment(T const& _node)
|
||||
{
|
||||
bool isExpression = std::is_constructible<Expression, T>::value;
|
||||
return formatSourceLocationComment(_node.debugData, !isExpression);
|
||||
}
|
||||
|
||||
Dialect const* m_dialect = nullptr;
|
||||
Dialect const* const m_dialect = nullptr;
|
||||
std::map<std::string const, unsigned> m_nameToSourceIndex;
|
||||
langutil::SourceLocation m_lastLocation = {};
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
+24
-3
@@ -25,11 +25,14 @@
|
||||
#include <libyul/Exceptions.h>
|
||||
|
||||
#include <libsolutil/CommonData.h>
|
||||
#include <libsolutil/StringUtils.h>
|
||||
|
||||
#include <boost/algorithm/string.hpp>
|
||||
#include <boost/algorithm/string/split.hpp>
|
||||
#include <boost/algorithm/string/replace.hpp>
|
||||
|
||||
#include <range/v3/view/transform.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace solidity;
|
||||
using namespace solidity::yul;
|
||||
@@ -47,18 +50,36 @@ string indent(std::string const& _input)
|
||||
|
||||
}
|
||||
|
||||
string Data::toString(Dialect const*) const
|
||||
string Data::toString(Dialect const*, optional<SourceNameMap>) const
|
||||
{
|
||||
return "data \"" + name.str() + "\" hex\"" + util::toHex(data) + "\"";
|
||||
}
|
||||
|
||||
string Object::toString(Dialect const* _dialect) const
|
||||
{
|
||||
string useSrcComment;
|
||||
|
||||
if (debugData && debugData->sourceNames)
|
||||
useSrcComment =
|
||||
"/// @use-src " +
|
||||
joinHumanReadable(ranges::views::transform(*debugData->sourceNames, [](auto&& _pair) {
|
||||
return to_string(_pair.first) + ":" + util::escapeAndQuoteString(*_pair.second);
|
||||
})) +
|
||||
"\n";
|
||||
return useSrcComment + toString(_dialect, debugData ? debugData->sourceNames : optional<SourceNameMap>{});
|
||||
}
|
||||
|
||||
string Object::toString(Dialect const* _dialect, std::optional<SourceNameMap> _sourceNames) const
|
||||
{
|
||||
yulAssert(code, "No code");
|
||||
string inner = "code " + (_dialect ? AsmPrinter{*_dialect} : AsmPrinter{})(*code);
|
||||
string inner = "code " + AsmPrinter{_dialect, _sourceNames}(*code);
|
||||
|
||||
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}";
|
||||
}
|
||||
|
||||
+27
-7
@@ -35,39 +35,55 @@ struct Dialect;
|
||||
struct AsmAnalysisInfo;
|
||||
|
||||
|
||||
using SourceNameMap = std::map<unsigned, std::shared_ptr<std::string const>>;
|
||||
|
||||
struct Object;
|
||||
|
||||
/**
|
||||
* Generic base class for both Yul objects and Yul data.
|
||||
*/
|
||||
struct ObjectNode
|
||||
{
|
||||
virtual ~ObjectNode() = default;
|
||||
virtual std::string toString(Dialect const* _dialect) const = 0;
|
||||
std::string toString() { return toString(nullptr); }
|
||||
|
||||
/// Name of the object.
|
||||
/// Can be empty since .yul files can also just contain code, without explicitly placing it in an object.
|
||||
YulString name;
|
||||
protected:
|
||||
virtual std::string toString(Dialect const* _dialect, std::optional<SourceNameMap> _sourceNames) const = 0;
|
||||
|
||||
/// Object should have access to toString
|
||||
friend struct Object;
|
||||
};
|
||||
|
||||
/**
|
||||
* Named data in Yul objects.
|
||||
*/
|
||||
struct Data: ObjectNode
|
||||
struct Data: public ObjectNode
|
||||
{
|
||||
Data(YulString _name, bytes _data): data(std::move(_data)) { name = _name; }
|
||||
std::string toString(Dialect const* _dialect) const override;
|
||||
|
||||
bytes data;
|
||||
|
||||
protected:
|
||||
std::string toString(Dialect const* _dialect, std::optional<SourceNameMap> _sourceNames) const override;
|
||||
};
|
||||
|
||||
|
||||
struct ObjectDebugData
|
||||
{
|
||||
std::optional<SourceNameMap> sourceNames = {};
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Yul code and data object container.
|
||||
*/
|
||||
struct Object: ObjectNode
|
||||
struct Object: public ObjectNode
|
||||
{
|
||||
public:
|
||||
/// @returns a (parseable) string representation. Includes types if @a _yul is set.
|
||||
std::string toString(Dialect const* _dialect) const override;
|
||||
/// @returns a (parseable) string representation.
|
||||
std::string toString(Dialect const* _dialect) const;
|
||||
|
||||
/// @returns the set of names of data objects accessible from within the code of
|
||||
/// this object, including the name of object itself
|
||||
@@ -94,8 +110,12 @@ public:
|
||||
std::map<YulString, size_t> subIndexByName;
|
||||
std::shared_ptr<yul::AsmAnalysisInfo> analysisInfo;
|
||||
|
||||
std::shared_ptr<ObjectDebugData const> debugData;
|
||||
|
||||
/// @returns the name of the special metadata data object.
|
||||
static std::string metadataName() { return ".metadata"; }
|
||||
protected:
|
||||
std::string toString(Dialect const* _dialect, std::optional<SourceNameMap> _sourceNames) const override;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -58,8 +58,9 @@ shared_ptr<Object> ObjectParser::parse(shared_ptr<Scanner> const& _scanner, bool
|
||||
}
|
||||
else
|
||||
object = parseObject();
|
||||
if (object && !_reuseScanner)
|
||||
if (!_reuseScanner)
|
||||
expectToken(Token::EOS);
|
||||
object->debugData = make_shared<ObjectDebugData>(ObjectDebugData{m_sourceNameMapping});
|
||||
return object;
|
||||
}
|
||||
catch (FatalError const&)
|
||||
@@ -111,7 +112,7 @@ shared_ptr<Block> ObjectParser::parseCode()
|
||||
return parseBlock();
|
||||
}
|
||||
|
||||
optional<ObjectParser::SourceNameMap> ObjectParser::tryParseSourceNameMapping() const
|
||||
optional<SourceNameMap> ObjectParser::tryParseSourceNameMapping() const
|
||||
{
|
||||
// @use-src 0:"abc.sol", 1:"foo.sol", 2:"bar.sol"
|
||||
//
|
||||
|
||||
@@ -55,7 +55,6 @@ public:
|
||||
/// @returns an empty shared pointer on error.
|
||||
std::shared_ptr<Object> parse(std::shared_ptr<langutil::Scanner> const& _scanner, bool _reuseScanner);
|
||||
|
||||
using SourceNameMap = std::map<unsigned, std::shared_ptr<std::string const>>;
|
||||
std::optional<SourceNameMap> const& sourceNameMapping() const noexcept { return m_sourceNameMapping; }
|
||||
|
||||
private:
|
||||
|
||||
Reference in New Issue
Block a user