Merge pull request #11951 from ethereum/scopeFillerNullptr

Use locationOf helper instead of accessing debugData directly.
This commit is contained in:
chriseth 2021-09-14 15:43:01 +02:00 committed by GitHub
commit a66e6f08c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 86 additions and 96 deletions

View File

@ -96,22 +96,22 @@ AsmAnalysisInfo AsmAnalyzer::analyzeStrictAssertCorrect(Dialect const& _dialect,
vector<YulString> AsmAnalyzer::operator()(Literal const& _literal) vector<YulString> AsmAnalyzer::operator()(Literal const& _literal)
{ {
expectValidType(_literal.type, _literal.debugData->location); expectValidType(_literal.type, locationOf(_literal));
if (_literal.kind == LiteralKind::String && _literal.value.str().size() > 32) if (_literal.kind == LiteralKind::String && _literal.value.str().size() > 32)
m_errorReporter.typeError( m_errorReporter.typeError(
3069_error, 3069_error,
_literal.debugData->location, locationOf(_literal),
"String literal too long (" + to_string(_literal.value.str().size()) + " > 32)" "String literal too long (" + to_string(_literal.value.str().size()) + " > 32)"
); );
else if (_literal.kind == LiteralKind::Number && bigint(_literal.value.str()) > u256(-1)) else if (_literal.kind == LiteralKind::Number && bigint(_literal.value.str()) > u256(-1))
m_errorReporter.typeError(6708_error, _literal.debugData->location, "Number literal too large (> 256 bits)"); m_errorReporter.typeError(6708_error, locationOf(_literal), "Number literal too large (> 256 bits)");
else if (_literal.kind == LiteralKind::Boolean) else if (_literal.kind == LiteralKind::Boolean)
yulAssert(_literal.value == "true"_yulstring || _literal.value == "false"_yulstring, ""); yulAssert(_literal.value == "true"_yulstring || _literal.value == "false"_yulstring, "");
if (!m_dialect.validTypeForLiteral(_literal.kind, _literal.value, _literal.type)) if (!m_dialect.validTypeForLiteral(_literal.kind, _literal.value, _literal.type))
m_errorReporter.typeError( m_errorReporter.typeError(
5170_error, 5170_error,
_literal.debugData->location, locationOf(_literal),
"Invalid type \"" + _literal.type.str() + "\" for literal \"" + _literal.value.str() + "\"." "Invalid type \"" + _literal.type.str() + "\" for literal \"" + _literal.value.str() + "\"."
); );
@ -131,7 +131,7 @@ vector<YulString> AsmAnalyzer::operator()(Identifier const& _identifier)
if (!m_activeVariables.count(&_var)) if (!m_activeVariables.count(&_var))
m_errorReporter.declarationError( m_errorReporter.declarationError(
4990_error, 4990_error,
_identifier.debugData->location, locationOf(_identifier),
"Variable " + _identifier.name.str() + " used before it was declared." "Variable " + _identifier.name.str() + " used before it was declared."
); );
type = _var.type; type = _var.type;
@ -140,7 +140,7 @@ vector<YulString> AsmAnalyzer::operator()(Identifier const& _identifier)
{ {
m_errorReporter.typeError( m_errorReporter.typeError(
6041_error, 6041_error,
_identifier.debugData->location, locationOf(_identifier),
"Function " + _identifier.name.str() + " used without being called." "Function " + _identifier.name.str() + " used without being called."
); );
} }
@ -165,7 +165,7 @@ vector<YulString> AsmAnalyzer::operator()(Identifier const& _identifier)
// Only add an error message if the callback did not do it. // Only add an error message if the callback did not do it.
m_errorReporter.declarationError( m_errorReporter.declarationError(
8198_error, 8198_error,
_identifier.debugData->location, locationOf(_identifier),
"Identifier \"" + _identifier.name.str() + "\" not found." "Identifier \"" + _identifier.name.str() + "\" not found."
); );
@ -181,7 +181,7 @@ void AsmAnalyzer::operator()(ExpressionStatement const& _statement)
if (watcher.ok() && !types.empty()) if (watcher.ok() && !types.empty())
m_errorReporter.typeError( m_errorReporter.typeError(
3083_error, 3083_error,
_statement.debugData->location, locationOf(_statement),
"Top-level expressions are not supposed to return values (this expression returns " + "Top-level expressions are not supposed to return values (this expression returns " +
to_string(types.size()) + to_string(types.size()) +
" value" + " value" +
@ -201,7 +201,7 @@ void AsmAnalyzer::operator()(Assignment const& _assignment)
if (!variables.insert(_variableName.name).second) if (!variables.insert(_variableName.name).second)
m_errorReporter.declarationError( m_errorReporter.declarationError(
9005_error, 9005_error,
_assignment.debugData->location, locationOf(_assignment),
"Variable " + "Variable " +
_variableName.name.str() + _variableName.name.str() +
" occurs multiple times on the left-hand side of the assignment." " occurs multiple times on the left-hand side of the assignment."
@ -212,7 +212,7 @@ void AsmAnalyzer::operator()(Assignment const& _assignment)
if (types.size() != numVariables) if (types.size() != numVariables)
m_errorReporter.declarationError( m_errorReporter.declarationError(
8678_error, 8678_error,
_assignment.debugData->location, locationOf(_assignment),
"Variable count for assignment to \"" + "Variable count for assignment to \"" +
joinHumanReadable(applyMap(_assignment.variableNames, [](auto const& _identifier){ return _identifier.name.str(); })) + joinHumanReadable(applyMap(_assignment.variableNames, [](auto const& _identifier){ return _identifier.name.str(); })) +
"\" does not match number of values (" + "\" does not match number of values (" +
@ -240,8 +240,8 @@ void AsmAnalyzer::operator()(VariableDeclaration const& _varDecl)
); );
for (auto const& variable: _varDecl.variables) for (auto const& variable: _varDecl.variables)
{ {
expectValidIdentifier(variable.name, variable.debugData->location); expectValidIdentifier(variable.name, locationOf(variable));
expectValidType(variable.type, variable.debugData->location); expectValidType(variable.type, locationOf(variable));
} }
if (_varDecl.value) if (_varDecl.value)
@ -250,7 +250,7 @@ void AsmAnalyzer::operator()(VariableDeclaration const& _varDecl)
if (types.size() != numVariables) if (types.size() != numVariables)
m_errorReporter.declarationError( m_errorReporter.declarationError(
3812_error, 3812_error,
_varDecl.debugData->location, locationOf(_varDecl),
"Variable count mismatch for declaration of \"" + "Variable count mismatch for declaration of \"" +
joinHumanReadable(applyMap(_varDecl.variables, [](auto const& _identifier){ return _identifier.name.str(); })) + joinHumanReadable(applyMap(_varDecl.variables, [](auto const& _identifier){ return _identifier.name.str(); })) +
+ "\": " + + "\": " +
@ -269,7 +269,7 @@ void AsmAnalyzer::operator()(VariableDeclaration const& _varDecl)
if (variable.type != givenType) if (variable.type != givenType)
m_errorReporter.typeError( m_errorReporter.typeError(
3947_error, 3947_error,
variable.debugData->location, locationOf(variable),
"Assigning value of type \"" + givenType.str() + "\" to variable of type \"" + variable.type.str() + "\"." "Assigning value of type \"" + givenType.str() + "\" to variable of type \"" + variable.type.str() + "\"."
); );
} }
@ -284,14 +284,14 @@ void AsmAnalyzer::operator()(VariableDeclaration const& _varDecl)
void AsmAnalyzer::operator()(FunctionDefinition const& _funDef) void AsmAnalyzer::operator()(FunctionDefinition const& _funDef)
{ {
yulAssert(!_funDef.name.empty(), ""); yulAssert(!_funDef.name.empty(), "");
expectValidIdentifier(_funDef.name, _funDef.debugData->location); expectValidIdentifier(_funDef.name, locationOf(_funDef));
Block const* virtualBlock = m_info.virtualBlocks.at(&_funDef).get(); Block const* virtualBlock = m_info.virtualBlocks.at(&_funDef).get();
yulAssert(virtualBlock, ""); yulAssert(virtualBlock, "");
Scope& varScope = scope(virtualBlock); Scope& varScope = scope(virtualBlock);
for (auto const& var: _funDef.parameters + _funDef.returnVariables) for (auto const& var: _funDef.parameters + _funDef.returnVariables)
{ {
expectValidIdentifier(var.name, var.debugData->location); expectValidIdentifier(var.name, locationOf(var));
expectValidType(var.type, var.debugData->location); expectValidType(var.type, locationOf(var));
m_activeVariables.insert(&std::get<Scope::Variable>(varScope.identifiers.at(var.name))); m_activeVariables.insert(&std::get<Scope::Variable>(varScope.identifiers.at(var.name)));
} }
@ -320,7 +320,7 @@ vector<YulString> AsmAnalyzer::operator()(FunctionCall const& _funCall)
{ {
m_errorReporter.typeError( m_errorReporter.typeError(
4202_error, 4202_error,
_funCall.functionName.debugData->location, locationOf(_funCall.functionName),
"Attempt to call variable instead of function." "Attempt to call variable instead of function."
); );
}, },
@ -344,7 +344,7 @@ vector<YulString> AsmAnalyzer::operator()(FunctionCall const& _funCall)
if (!validateInstructions(_funCall)) if (!validateInstructions(_funCall))
m_errorReporter.declarationError( m_errorReporter.declarationError(
4619_error, 4619_error,
_funCall.functionName.debugData->location, locationOf(_funCall.functionName),
"Function \"" + _funCall.functionName.name.str() + "\" not found." "Function \"" + _funCall.functionName.name.str() + "\" not found."
); );
yulAssert(!watcher.ok(), "Expected a reported error."); yulAssert(!watcher.ok(), "Expected a reported error.");
@ -353,7 +353,7 @@ vector<YulString> AsmAnalyzer::operator()(FunctionCall const& _funCall)
if (parameterTypes && _funCall.arguments.size() != parameterTypes->size()) if (parameterTypes && _funCall.arguments.size() != parameterTypes->size())
m_errorReporter.typeError( m_errorReporter.typeError(
7000_error, 7000_error,
_funCall.functionName.debugData->location, locationOf(_funCall.functionName),
"Function \"" + _funCall.functionName.name.str() + "\" expects " + "Function \"" + _funCall.functionName.name.str() + "\" expects " +
to_string(parameterTypes->size()) + to_string(parameterTypes->size()) +
" arguments but got " + " arguments but got " +
@ -373,13 +373,13 @@ vector<YulString> AsmAnalyzer::operator()(FunctionCall const& _funCall)
if (!holds_alternative<Literal>(arg)) if (!holds_alternative<Literal>(arg))
m_errorReporter.typeError( m_errorReporter.typeError(
9114_error, 9114_error,
_funCall.functionName.debugData->location, locationOf(_funCall.functionName),
"Function expects direct literals as arguments." "Function expects direct literals as arguments."
); );
else if (*literalArgumentKind != get<Literal>(arg).kind) else if (*literalArgumentKind != get<Literal>(arg).kind)
m_errorReporter.typeError( m_errorReporter.typeError(
5859_error, 5859_error,
get<Literal>(arg).debugData->location, locationOf(arg),
"Function expects " + to_string(*literalArgumentKind) + " literal." "Function expects " + to_string(*literalArgumentKind) + " literal."
); );
else if (*literalArgumentKind == LiteralKind::String) else if (*literalArgumentKind == LiteralKind::String)
@ -390,7 +390,7 @@ vector<YulString> AsmAnalyzer::operator()(FunctionCall const& _funCall)
if (!m_dataNames.count(get<Literal>(arg).value)) if (!m_dataNames.count(get<Literal>(arg).value))
m_errorReporter.typeError( m_errorReporter.typeError(
3517_error, 3517_error,
get<Literal>(arg).debugData->location, locationOf(arg),
"Unknown data object \"" + std::get<Literal>(arg).value.str() + "\"." "Unknown data object \"" + std::get<Literal>(arg).value.str() + "\"."
); );
} }
@ -399,7 +399,7 @@ vector<YulString> AsmAnalyzer::operator()(FunctionCall const& _funCall)
if (get<Literal>(arg).value.empty()) if (get<Literal>(arg).value.empty())
m_errorReporter.typeError( m_errorReporter.typeError(
1844_error, 1844_error,
get<Literal>(arg).debugData->location, locationOf(arg),
"The \"verbatim_*\" builtins cannot be used with empty bytecode." "The \"verbatim_*\" builtins cannot be used with empty bytecode."
); );
} }
@ -442,7 +442,7 @@ void AsmAnalyzer::operator()(Switch const& _switch)
if (_switch.cases.size() == 1 && !_switch.cases[0].value) if (_switch.cases.size() == 1 && !_switch.cases[0].value)
m_errorReporter.warning( m_errorReporter.warning(
9592_error, 9592_error,
_switch.debugData->location, locationOf(_switch),
"\"switch\" statement with only a default case." "\"switch\" statement with only a default case."
); );
@ -455,7 +455,7 @@ void AsmAnalyzer::operator()(Switch const& _switch)
{ {
auto watcher = m_errorReporter.errorWatcher(); auto watcher = m_errorReporter.errorWatcher();
expectType(valueType, _case.value->type, _case.value->debugData->location); expectType(valueType, _case.value->type, locationOf(*_case.value));
// We cannot use "expectExpression" here because *_case.value is not an // We cannot use "expectExpression" here because *_case.value is not an
// Expression and would be converted to an Expression otherwise. // Expression and would be converted to an Expression otherwise.
@ -465,7 +465,7 @@ void AsmAnalyzer::operator()(Switch const& _switch)
if (watcher.ok() && !cases.insert(valueOfLiteral(*_case.value)).second) if (watcher.ok() && !cases.insert(valueOfLiteral(*_case.value)).second)
m_errorReporter.declarationError( m_errorReporter.declarationError(
6792_error, 6792_error,
_case.debugData->location, locationOf(_case),
"Duplicate case \"" + "Duplicate case \"" +
valueOfLiteral(*_case.value).str() + valueOfLiteral(*_case.value).str() +
"\" defined." "\" defined."
@ -565,11 +565,11 @@ void AsmAnalyzer::checkAssignment(Identifier const& _variable, YulString _valueT
); );
if (!holds_alternative<Scope::Variable>(*var)) if (!holds_alternative<Scope::Variable>(*var))
m_errorReporter.typeError(2657_error, _variable.debugData->location, "Assignment requires variable."); m_errorReporter.typeError(2657_error, locationOf(_variable), "Assignment requires variable.");
else if (!m_activeVariables.count(&std::get<Scope::Variable>(*var))) else if (!m_activeVariables.count(&std::get<Scope::Variable>(*var)))
m_errorReporter.declarationError( m_errorReporter.declarationError(
1133_error, 1133_error,
_variable.debugData->location, locationOf(_variable),
"Variable " + _variable.name.str() + " used before it was declared." "Variable " + _variable.name.str() + " used before it was declared."
); );
else else
@ -588,11 +588,11 @@ void AsmAnalyzer::checkAssignment(Identifier const& _variable, YulString _valueT
if (!found && watcher.ok()) if (!found && watcher.ok())
// Only add message if the callback did not. // Only add message if the callback did not.
m_errorReporter.declarationError(4634_error, _variable.debugData->location, "Variable not found or variable not lvalue."); m_errorReporter.declarationError(4634_error, locationOf(_variable), "Variable not found or variable not lvalue.");
if (variableType && *variableType != _valueType) if (variableType && *variableType != _valueType)
m_errorReporter.typeError( m_errorReporter.typeError(
9547_error, 9547_error,
_variable.debugData->location, locationOf(_variable),
"Assigning a value of type \"" + "Assigning a value of type \"" +
_valueType.str() + _valueType.str() +
"\" to a variable of type \"" + "\" to a variable of type \"" +
@ -738,5 +738,5 @@ bool AsmAnalyzer::validateInstructions(evmasm::Instruction _instr, SourceLocatio
bool AsmAnalyzer::validateInstructions(FunctionCall const& _functionCall) bool AsmAnalyzer::validateInstructions(FunctionCall const& _functionCall)
{ {
return validateInstructions(_functionCall.functionName.name.str(), _functionCall.functionName.debugData->location); return validateInstructions(_functionCall.functionName.name.str(), locationOf(_functionCall.functionName));
} }

View File

@ -33,7 +33,7 @@ namespace solidity::yul
Json::Value AsmJsonConverter::operator()(Block const& _node) const Json::Value AsmJsonConverter::operator()(Block const& _node) const
{ {
Json::Value ret = createAstNode(_node.debugData->location, "YulBlock"); Json::Value ret = createAstNode(locationOf(_node), "YulBlock");
ret["statements"] = vectorOfVariantsToJson(_node.statements); ret["statements"] = vectorOfVariantsToJson(_node.statements);
return ret; return ret;
} }
@ -41,7 +41,7 @@ Json::Value AsmJsonConverter::operator()(Block const& _node) const
Json::Value AsmJsonConverter::operator()(TypedName const& _node) const Json::Value AsmJsonConverter::operator()(TypedName const& _node) const
{ {
yulAssert(!_node.name.empty(), "Invalid variable name."); yulAssert(!_node.name.empty(), "Invalid variable name.");
Json::Value ret = createAstNode(_node.debugData->location, "YulTypedName"); Json::Value ret = createAstNode(locationOf(_node), "YulTypedName");
ret["name"] = _node.name.str(); ret["name"] = _node.name.str();
ret["type"] = _node.type.str(); ret["type"] = _node.type.str();
return ret; return ret;
@ -49,7 +49,7 @@ Json::Value AsmJsonConverter::operator()(TypedName const& _node) const
Json::Value AsmJsonConverter::operator()(Literal const& _node) const Json::Value AsmJsonConverter::operator()(Literal const& _node) const
{ {
Json::Value ret = createAstNode(_node.debugData->location, "YulLiteral"); Json::Value ret = createAstNode(locationOf(_node), "YulLiteral");
switch (_node.kind) switch (_node.kind)
{ {
case LiteralKind::Number: case LiteralKind::Number:
@ -76,7 +76,7 @@ Json::Value AsmJsonConverter::operator()(Literal const& _node) const
Json::Value AsmJsonConverter::operator()(Identifier const& _node) const Json::Value AsmJsonConverter::operator()(Identifier const& _node) const
{ {
yulAssert(!_node.name.empty(), "Invalid identifier"); yulAssert(!_node.name.empty(), "Invalid identifier");
Json::Value ret = createAstNode(_node.debugData->location, "YulIdentifier"); Json::Value ret = createAstNode(locationOf(_node), "YulIdentifier");
ret["name"] = _node.name.str(); ret["name"] = _node.name.str();
return ret; return ret;
} }
@ -84,7 +84,7 @@ Json::Value AsmJsonConverter::operator()(Identifier const& _node) const
Json::Value AsmJsonConverter::operator()(Assignment const& _node) const Json::Value AsmJsonConverter::operator()(Assignment const& _node) const
{ {
yulAssert(_node.variableNames.size() >= 1, "Invalid assignment syntax"); yulAssert(_node.variableNames.size() >= 1, "Invalid assignment syntax");
Json::Value ret = createAstNode(_node.debugData->location, "YulAssignment"); Json::Value ret = createAstNode(locationOf(_node), "YulAssignment");
for (auto const& var: _node.variableNames) for (auto const& var: _node.variableNames)
ret["variableNames"].append((*this)(var)); ret["variableNames"].append((*this)(var));
ret["value"] = _node.value ? std::visit(*this, *_node.value) : Json::nullValue; ret["value"] = _node.value ? std::visit(*this, *_node.value) : Json::nullValue;
@ -93,7 +93,7 @@ Json::Value AsmJsonConverter::operator()(Assignment const& _node) const
Json::Value AsmJsonConverter::operator()(FunctionCall const& _node) const Json::Value AsmJsonConverter::operator()(FunctionCall const& _node) const
{ {
Json::Value ret = createAstNode(_node.debugData->location, "YulFunctionCall"); Json::Value ret = createAstNode(locationOf(_node), "YulFunctionCall");
ret["functionName"] = (*this)(_node.functionName); ret["functionName"] = (*this)(_node.functionName);
ret["arguments"] = vectorOfVariantsToJson(_node.arguments); ret["arguments"] = vectorOfVariantsToJson(_node.arguments);
return ret; return ret;
@ -101,14 +101,14 @@ Json::Value AsmJsonConverter::operator()(FunctionCall const& _node) const
Json::Value AsmJsonConverter::operator()(ExpressionStatement const& _node) const Json::Value AsmJsonConverter::operator()(ExpressionStatement const& _node) const
{ {
Json::Value ret = createAstNode(_node.debugData->location, "YulExpressionStatement"); Json::Value ret = createAstNode(locationOf(_node), "YulExpressionStatement");
ret["expression"] = std::visit(*this, _node.expression); ret["expression"] = std::visit(*this, _node.expression);
return ret; return ret;
} }
Json::Value AsmJsonConverter::operator()(VariableDeclaration const& _node) const Json::Value AsmJsonConverter::operator()(VariableDeclaration const& _node) const
{ {
Json::Value ret = createAstNode(_node.debugData->location, "YulVariableDeclaration"); Json::Value ret = createAstNode(locationOf(_node), "YulVariableDeclaration");
for (auto const& var: _node.variables) for (auto const& var: _node.variables)
ret["variables"].append((*this)(var)); ret["variables"].append((*this)(var));
@ -120,7 +120,7 @@ Json::Value AsmJsonConverter::operator()(VariableDeclaration const& _node) const
Json::Value AsmJsonConverter::operator()(FunctionDefinition const& _node) const Json::Value AsmJsonConverter::operator()(FunctionDefinition const& _node) const
{ {
yulAssert(!_node.name.empty(), "Invalid function name."); yulAssert(!_node.name.empty(), "Invalid function name.");
Json::Value ret = createAstNode(_node.debugData->location, "YulFunctionDefinition"); Json::Value ret = createAstNode(locationOf(_node), "YulFunctionDefinition");
ret["name"] = _node.name.str(); ret["name"] = _node.name.str();
for (auto const& var: _node.parameters) for (auto const& var: _node.parameters)
ret["parameters"].append((*this)(var)); ret["parameters"].append((*this)(var));
@ -133,7 +133,7 @@ Json::Value AsmJsonConverter::operator()(FunctionDefinition const& _node) const
Json::Value AsmJsonConverter::operator()(If const& _node) const Json::Value AsmJsonConverter::operator()(If const& _node) const
{ {
yulAssert(_node.condition, "Invalid if condition."); yulAssert(_node.condition, "Invalid if condition.");
Json::Value ret = createAstNode(_node.debugData->location, "YulIf"); Json::Value ret = createAstNode(locationOf(_node), "YulIf");
ret["condition"] = std::visit(*this, *_node.condition); ret["condition"] = std::visit(*this, *_node.condition);
ret["body"] = (*this)(_node.body); ret["body"] = (*this)(_node.body);
return ret; return ret;
@ -142,7 +142,7 @@ Json::Value AsmJsonConverter::operator()(If const& _node) const
Json::Value AsmJsonConverter::operator()(Switch const& _node) const Json::Value AsmJsonConverter::operator()(Switch const& _node) const
{ {
yulAssert(_node.expression, "Invalid expression pointer."); yulAssert(_node.expression, "Invalid expression pointer.");
Json::Value ret = createAstNode(_node.debugData->location, "YulSwitch"); Json::Value ret = createAstNode(locationOf(_node), "YulSwitch");
ret["expression"] = std::visit(*this, *_node.expression); ret["expression"] = std::visit(*this, *_node.expression);
for (auto const& var: _node.cases) for (auto const& var: _node.cases)
ret["cases"].append((*this)(var)); ret["cases"].append((*this)(var));
@ -151,7 +151,7 @@ Json::Value AsmJsonConverter::operator()(Switch const& _node) const
Json::Value AsmJsonConverter::operator()(Case const& _node) const Json::Value AsmJsonConverter::operator()(Case const& _node) const
{ {
Json::Value ret = createAstNode(_node.debugData->location, "YulCase"); Json::Value ret = createAstNode(locationOf(_node), "YulCase");
ret["value"] = _node.value ? (*this)(*_node.value) : "default"; ret["value"] = _node.value ? (*this)(*_node.value) : "default";
ret["body"] = (*this)(_node.body); ret["body"] = (*this)(_node.body);
return ret; return ret;
@ -160,7 +160,7 @@ Json::Value AsmJsonConverter::operator()(Case const& _node) const
Json::Value AsmJsonConverter::operator()(ForLoop const& _node) const Json::Value AsmJsonConverter::operator()(ForLoop const& _node) const
{ {
yulAssert(_node.condition, "Invalid for loop condition."); yulAssert(_node.condition, "Invalid for loop condition.");
Json::Value ret = createAstNode(_node.debugData->location, "YulForLoop"); Json::Value ret = createAstNode(locationOf(_node), "YulForLoop");
ret["pre"] = (*this)(_node.pre); ret["pre"] = (*this)(_node.pre);
ret["condition"] = std::visit(*this, *_node.condition); ret["condition"] = std::visit(*this, *_node.condition);
ret["post"] = (*this)(_node.post); ret["post"] = (*this)(_node.post);
@ -170,17 +170,17 @@ Json::Value AsmJsonConverter::operator()(ForLoop const& _node) const
Json::Value AsmJsonConverter::operator()(Break const& _node) const Json::Value AsmJsonConverter::operator()(Break const& _node) const
{ {
return createAstNode(_node.debugData->location, "YulBreak"); return createAstNode(locationOf(_node), "YulBreak");
} }
Json::Value AsmJsonConverter::operator()(Continue const& _node) const Json::Value AsmJsonConverter::operator()(Continue const& _node) const
{ {
return createAstNode(_node.debugData->location, "YulContinue"); return createAstNode(locationOf(_node), "YulContinue");
} }
Json::Value AsmJsonConverter::operator()(Leave const& _node) const Json::Value AsmJsonConverter::operator()(Leave const& _node) const
{ {
return createAstNode(_node.debugData->location, "YulLeave"); return createAstNode(locationOf(_node), "YulLeave");
} }
Json::Value AsmJsonConverter::createAstNode(langutil::SourceLocation const& _location, string _nodeType) const Json::Value AsmJsonConverter::createAstNode(langutil::SourceLocation const& _location, string _nodeType) const

View File

@ -52,7 +52,7 @@ shared_ptr<DebugData const> updateLocationEndFrom(
langutil::SourceLocation const& _location langutil::SourceLocation const& _location
) )
{ {
SourceLocation updatedLocation = _debugData->location; SourceLocation updatedLocation = _debugData ? _debugData->location : langutil::SourceLocation{};
updatedLocation.end = _location.end; updatedLocation.end = _location.end;
return make_shared<DebugData const>(updatedLocation); return make_shared<DebugData const>(updatedLocation);
} }
@ -245,7 +245,7 @@ Statement Parser::parseStatement()
_if.condition = make_unique<Expression>(parseExpression()); _if.condition = make_unique<Expression>(parseExpression());
_if.body = parseBlock(); _if.body = parseBlock();
if (m_useSourceLocationFrom == UseSourceLocationFrom::Scanner) if (m_useSourceLocationFrom == UseSourceLocationFrom::Scanner)
_if.debugData = updateLocationEndFrom(_if.debugData, _if.body.debugData->location); _if.debugData = updateLocationEndFrom(_if.debugData, locationOf(_if.body));
return Statement{move(_if)}; return Statement{move(_if)};
} }
case Token::Switch: case Token::Switch:
@ -264,7 +264,7 @@ Statement Parser::parseStatement()
if (_switch.cases.empty()) if (_switch.cases.empty())
fatalParserError(2418_error, "Switch statement without any cases."); fatalParserError(2418_error, "Switch statement without any cases.");
if (m_useSourceLocationFrom == UseSourceLocationFrom::Scanner) if (m_useSourceLocationFrom == UseSourceLocationFrom::Scanner)
_switch.debugData = updateLocationEndFrom(_switch.debugData, _switch.cases.back().body.debugData->location); _switch.debugData = updateLocationEndFrom(_switch.debugData, locationOf(_switch.cases.back().body));
return Statement{move(_switch)}; return Statement{move(_switch)};
} }
case Token::For: case Token::For:
@ -378,7 +378,7 @@ Case Parser::parseCase()
yulAssert(false, "Case or default case expected."); yulAssert(false, "Case or default case expected.");
_case.body = parseBlock(); _case.body = parseBlock();
if (m_useSourceLocationFrom == UseSourceLocationFrom::Scanner) if (m_useSourceLocationFrom == UseSourceLocationFrom::Scanner)
_case.debugData = updateLocationEndFrom(_case.debugData, _case.body.debugData->location); _case.debugData = updateLocationEndFrom(_case.debugData, locationOf(_case.body));
return _case; return _case;
} }
@ -399,7 +399,7 @@ ForLoop Parser::parseForLoop()
m_currentForLoopComponent = ForLoopComponent::ForLoopBody; m_currentForLoopComponent = ForLoopComponent::ForLoopBody;
forLoop.body = parseBlock(); forLoop.body = parseBlock();
if (m_useSourceLocationFrom == UseSourceLocationFrom::Scanner) if (m_useSourceLocationFrom == UseSourceLocationFrom::Scanner)
forLoop.debugData = updateLocationEndFrom(forLoop.debugData, forLoop.body.debugData->location); forLoop.debugData = updateLocationEndFrom(forLoop.debugData, locationOf(forLoop.body));
m_currentForLoopComponent = outerForLoopComponent; m_currentForLoopComponent = outerForLoopComponent;
@ -419,7 +419,7 @@ Expression Parser::parseExpression()
if (m_dialect.builtin(_identifier.name)) if (m_dialect.builtin(_identifier.name))
fatalParserError( fatalParserError(
7104_error, 7104_error,
_identifier.debugData->location, locationOf(_identifier),
"Builtin function \"" + _identifier.name.str() + "\" must be called." "Builtin function \"" + _identifier.name.str() + "\" must be called."
); );
return move(_identifier); return move(_identifier);
@ -515,7 +515,7 @@ VariableDeclaration Parser::parseVariableDeclaration()
varDecl.debugData = updateLocationEndFrom(varDecl.debugData, locationOf(*varDecl.value)); varDecl.debugData = updateLocationEndFrom(varDecl.debugData, locationOf(*varDecl.value));
} }
else if (m_useSourceLocationFrom == UseSourceLocationFrom::Scanner) else if (m_useSourceLocationFrom == UseSourceLocationFrom::Scanner)
varDecl.debugData = updateLocationEndFrom(varDecl.debugData, varDecl.variables.back().debugData->location); varDecl.debugData = updateLocationEndFrom(varDecl.debugData, locationOf(varDecl.variables.back()));
return varDecl; return varDecl;
} }
@ -562,7 +562,7 @@ FunctionDefinition Parser::parseFunctionDefinition()
funDef.body = parseBlock(); funDef.body = parseBlock();
m_insideFunction = preInsideFunction; m_insideFunction = preInsideFunction;
if (m_useSourceLocationFrom == UseSourceLocationFrom::Scanner) if (m_useSourceLocationFrom == UseSourceLocationFrom::Scanner)
funDef.debugData = updateLocationEndFrom(funDef.debugData, funDef.body.debugData->location); funDef.debugData = updateLocationEndFrom(funDef.debugData, locationOf(funDef.body));
m_currentForLoopComponent = outerForLoopComponent; m_currentForLoopComponent = outerForLoopComponent;
return funDef; return funDef;

View File

@ -53,7 +53,7 @@ bool ScopeFiller::operator()(ExpressionStatement const& _expr)
bool ScopeFiller::operator()(VariableDeclaration const& _varDecl) bool ScopeFiller::operator()(VariableDeclaration const& _varDecl)
{ {
for (auto const& variable: _varDecl.variables) for (auto const& variable: _varDecl.variables)
if (!registerVariable(variable, _varDecl.debugData->location, *m_currentScope)) if (!registerVariable(variable, locationOf(_varDecl), *m_currentScope))
return false; return false;
return true; return true;
} }
@ -68,7 +68,7 @@ bool ScopeFiller::operator()(FunctionDefinition const& _funDef)
bool success = true; bool success = true;
for (auto const& var: _funDef.parameters + _funDef.returnVariables) for (auto const& var: _funDef.parameters + _funDef.returnVariables)
if (!registerVariable(var, _funDef.debugData->location, varScope)) if (!registerVariable(var, locationOf(_funDef), varScope))
success = false; success = false;
if (!(*this)(_funDef.body)) if (!(*this)(_funDef.body))
@ -162,7 +162,7 @@ bool ScopeFiller::registerFunction(FunctionDefinition const& _funDef)
//@TODO secondary location //@TODO secondary location
m_errorReporter.declarationError( m_errorReporter.declarationError(
6052_error, 6052_error,
_funDef.debugData->location, locationOf(_funDef),
"Function name " + _funDef.name.str() + " already taken in this scope." "Function name " + _funDef.name.str() + " already taken in this scope."
); );
return false; return false;

View File

@ -44,16 +44,6 @@ using namespace solidity;
using namespace solidity::yul; using namespace solidity::yul;
using namespace solidity::util; using namespace solidity::util;
namespace
{
langutil::SourceLocation extractSourceLocationFromDebugData(shared_ptr<DebugData const> const& _debugData)
{
return _debugData ? _debugData->location : langutil::SourceLocation{};
}
}
CodeTransform::CodeTransform( CodeTransform::CodeTransform(
AbstractAssembly& _assembly, AbstractAssembly& _assembly,
AsmAnalysisInfo& _analysisInfo, AsmAnalysisInfo& _analysisInfo,
@ -155,13 +145,13 @@ void CodeTransform::operator()(VariableDeclaration const& _varDecl)
} }
else else
{ {
m_assembly.setSourceLocation(extractSourceLocationFromDebugData(_varDecl.debugData)); m_assembly.setSourceLocation(locationOf(_varDecl));
size_t variablesLeft = numVariables; size_t variablesLeft = numVariables;
while (variablesLeft--) while (variablesLeft--)
m_assembly.appendConstant(u256(0)); m_assembly.appendConstant(u256(0));
} }
m_assembly.setSourceLocation(extractSourceLocationFromDebugData(_varDecl.debugData)); m_assembly.setSourceLocation(locationOf(_varDecl));
bool atTopOfStack = true; bool atTopOfStack = true;
for (size_t varIndex = 0; varIndex < numVariables; ++varIndex) for (size_t varIndex = 0; varIndex < numVariables; ++varIndex)
{ {
@ -223,13 +213,13 @@ void CodeTransform::operator()(Assignment const& _assignment)
std::visit(*this, *_assignment.value); std::visit(*this, *_assignment.value);
expectDeposit(static_cast<int>(_assignment.variableNames.size()), height); expectDeposit(static_cast<int>(_assignment.variableNames.size()), height);
m_assembly.setSourceLocation(extractSourceLocationFromDebugData(_assignment.debugData)); m_assembly.setSourceLocation(locationOf(_assignment));
generateMultiAssignment(_assignment.variableNames); generateMultiAssignment(_assignment.variableNames);
} }
void CodeTransform::operator()(ExpressionStatement const& _statement) void CodeTransform::operator()(ExpressionStatement const& _statement)
{ {
m_assembly.setSourceLocation(extractSourceLocationFromDebugData(_statement.debugData)); m_assembly.setSourceLocation(locationOf(_statement));
std::visit(*this, _statement.expression); std::visit(*this, _statement.expression);
} }
@ -237,13 +227,13 @@ void CodeTransform::operator()(FunctionCall const& _call)
{ {
yulAssert(m_scope, ""); yulAssert(m_scope, "");
m_assembly.setSourceLocation(extractSourceLocationFromDebugData(_call.debugData)); m_assembly.setSourceLocation(locationOf(_call));
if (BuiltinFunctionForEVM const* builtin = m_dialect.builtin(_call.functionName.name)) if (BuiltinFunctionForEVM const* builtin = m_dialect.builtin(_call.functionName.name))
{ {
for (auto&& [i, arg]: _call.arguments | ranges::views::enumerate | ranges::views::reverse) for (auto&& [i, arg]: _call.arguments | ranges::views::enumerate | ranges::views::reverse)
if (!builtin->literalArgument(i)) if (!builtin->literalArgument(i))
visitExpression(arg); visitExpression(arg);
m_assembly.setSourceLocation(extractSourceLocationFromDebugData(_call.debugData)); m_assembly.setSourceLocation(locationOf(_call));
builtin->generateCode(_call, m_assembly, m_builtinContext); builtin->generateCode(_call, m_assembly, m_builtinContext);
} }
else else
@ -260,7 +250,7 @@ void CodeTransform::operator()(FunctionCall const& _call)
yulAssert(function->arguments.size() == _call.arguments.size(), ""); yulAssert(function->arguments.size() == _call.arguments.size(), "");
for (auto const& arg: _call.arguments | ranges::views::reverse) for (auto const& arg: _call.arguments | ranges::views::reverse)
visitExpression(arg); visitExpression(arg);
m_assembly.setSourceLocation(extractSourceLocationFromDebugData(_call.debugData)); m_assembly.setSourceLocation(locationOf(_call));
m_assembly.appendJumpTo( m_assembly.appendJumpTo(
functionEntryID(_call.functionName.name, *function), functionEntryID(_call.functionName.name, *function),
static_cast<int>(function->returns.size() - function->arguments.size()) - 1, static_cast<int>(function->returns.size() - function->arguments.size()) - 1,
@ -272,7 +262,7 @@ void CodeTransform::operator()(FunctionCall const& _call)
void CodeTransform::operator()(Identifier const& _identifier) void CodeTransform::operator()(Identifier const& _identifier)
{ {
m_assembly.setSourceLocation(extractSourceLocationFromDebugData(_identifier.debugData)); m_assembly.setSourceLocation(locationOf(_identifier));
// First search internals, then externals. // First search internals, then externals.
yulAssert(m_scope, ""); yulAssert(m_scope, "");
if (m_scope->lookup(_identifier.name, GenericVisitor{ if (m_scope->lookup(_identifier.name, GenericVisitor{
@ -304,19 +294,19 @@ void CodeTransform::operator()(Identifier const& _identifier)
void CodeTransform::operator()(Literal const& _literal) void CodeTransform::operator()(Literal const& _literal)
{ {
m_assembly.setSourceLocation(extractSourceLocationFromDebugData(_literal.debugData)); m_assembly.setSourceLocation(locationOf(_literal));
m_assembly.appendConstant(valueOfLiteral(_literal)); m_assembly.appendConstant(valueOfLiteral(_literal));
} }
void CodeTransform::operator()(If const& _if) void CodeTransform::operator()(If const& _if)
{ {
visitExpression(*_if.condition); visitExpression(*_if.condition);
m_assembly.setSourceLocation(extractSourceLocationFromDebugData(_if.debugData)); m_assembly.setSourceLocation(locationOf(_if));
m_assembly.appendInstruction(evmasm::Instruction::ISZERO); m_assembly.appendInstruction(evmasm::Instruction::ISZERO);
AbstractAssembly::LabelID end = m_assembly.newLabelId(); AbstractAssembly::LabelID end = m_assembly.newLabelId();
m_assembly.appendJumpToIf(end); m_assembly.appendJumpToIf(end);
(*this)(_if.body); (*this)(_if.body);
m_assembly.setSourceLocation(extractSourceLocationFromDebugData(_if.debugData)); m_assembly.setSourceLocation(locationOf(_if));
m_assembly.appendLabel(end); m_assembly.appendLabel(end);
} }
@ -331,7 +321,7 @@ void CodeTransform::operator()(Switch const& _switch)
if (c.value) if (c.value)
{ {
(*this)(*c.value); (*this)(*c.value);
m_assembly.setSourceLocation(extractSourceLocationFromDebugData(c.debugData)); m_assembly.setSourceLocation(locationOf(c));
AbstractAssembly::LabelID bodyLabel = m_assembly.newLabelId(); AbstractAssembly::LabelID bodyLabel = m_assembly.newLabelId();
caseBodies[&c] = bodyLabel; caseBodies[&c] = bodyLabel;
yulAssert(m_assembly.stackHeight() == expressionHeight + 1, ""); yulAssert(m_assembly.stackHeight() == expressionHeight + 1, "");
@ -343,24 +333,24 @@ void CodeTransform::operator()(Switch const& _switch)
// default case // default case
(*this)(c.body); (*this)(c.body);
} }
m_assembly.setSourceLocation(extractSourceLocationFromDebugData(_switch.debugData)); m_assembly.setSourceLocation(locationOf(_switch));
m_assembly.appendJumpTo(end); m_assembly.appendJumpTo(end);
size_t numCases = caseBodies.size(); size_t numCases = caseBodies.size();
for (auto const& c: caseBodies) for (auto const& c: caseBodies)
{ {
m_assembly.setSourceLocation(extractSourceLocationFromDebugData(c.first->debugData)); m_assembly.setSourceLocation(locationOf(*c.first));
m_assembly.appendLabel(c.second); m_assembly.appendLabel(c.second);
(*this)(c.first->body); (*this)(c.first->body);
// Avoid useless "jump to next" for the last case. // Avoid useless "jump to next" for the last case.
if (--numCases > 0) if (--numCases > 0)
{ {
m_assembly.setSourceLocation(extractSourceLocationFromDebugData(c.first->debugData)); m_assembly.setSourceLocation(locationOf(*c.first));
m_assembly.appendJumpTo(end); m_assembly.appendJumpTo(end);
} }
} }
m_assembly.setSourceLocation(extractSourceLocationFromDebugData(_switch.debugData)); m_assembly.setSourceLocation(locationOf(_switch));
m_assembly.appendLabel(end); m_assembly.appendLabel(end);
m_assembly.appendInstruction(evmasm::Instruction::POP); m_assembly.appendInstruction(evmasm::Instruction::POP);
} }
@ -381,7 +371,7 @@ void CodeTransform::operator()(FunctionDefinition const& _function)
m_context->variableStackHeights[&var] = height++; m_context->variableStackHeights[&var] = height++;
} }
m_assembly.setSourceLocation(extractSourceLocationFromDebugData(_function.debugData)); m_assembly.setSourceLocation(locationOf(_function));
int const stackHeightBefore = m_assembly.stackHeight(); int const stackHeightBefore = m_assembly.stackHeight();
m_assembly.appendLabel(functionEntryID(_function.name, function)); m_assembly.appendLabel(functionEntryID(_function.name, function));
@ -417,7 +407,7 @@ void CodeTransform::operator()(FunctionDefinition const& _function)
subTransform(_function.body); subTransform(_function.body);
m_assembly.setSourceLocation(extractSourceLocationFromDebugData(_function.debugData)); m_assembly.setSourceLocation(locationOf(_function));
if (!subTransform.m_stackErrors.empty()) if (!subTransform.m_stackErrors.empty())
{ {
m_assembly.markAsInvalid(); m_assembly.markAsInvalid();
@ -508,11 +498,11 @@ void CodeTransform::operator()(ForLoop const& _forLoop)
AbstractAssembly::LabelID postPart = m_assembly.newLabelId(); AbstractAssembly::LabelID postPart = m_assembly.newLabelId();
AbstractAssembly::LabelID loopEnd = m_assembly.newLabelId(); AbstractAssembly::LabelID loopEnd = m_assembly.newLabelId();
m_assembly.setSourceLocation(extractSourceLocationFromDebugData(_forLoop.debugData)); m_assembly.setSourceLocation(locationOf(_forLoop));
m_assembly.appendLabel(loopStart); m_assembly.appendLabel(loopStart);
visitExpression(*_forLoop.condition); visitExpression(*_forLoop.condition);
m_assembly.setSourceLocation(extractSourceLocationFromDebugData(_forLoop.debugData)); m_assembly.setSourceLocation(locationOf(_forLoop));
m_assembly.appendInstruction(evmasm::Instruction::ISZERO); m_assembly.appendInstruction(evmasm::Instruction::ISZERO);
m_assembly.appendJumpToIf(loopEnd); m_assembly.appendJumpToIf(loopEnd);
@ -520,12 +510,12 @@ void CodeTransform::operator()(ForLoop const& _forLoop)
m_context->forLoopStack.emplace(Context::ForLoopLabels{ {postPart, stackHeightBody}, {loopEnd, stackHeightBody} }); m_context->forLoopStack.emplace(Context::ForLoopLabels{ {postPart, stackHeightBody}, {loopEnd, stackHeightBody} });
(*this)(_forLoop.body); (*this)(_forLoop.body);
m_assembly.setSourceLocation(extractSourceLocationFromDebugData(_forLoop.debugData)); m_assembly.setSourceLocation(locationOf(_forLoop));
m_assembly.appendLabel(postPart); m_assembly.appendLabel(postPart);
(*this)(_forLoop.post); (*this)(_forLoop.post);
m_assembly.setSourceLocation(extractSourceLocationFromDebugData(_forLoop.debugData)); m_assembly.setSourceLocation(locationOf(_forLoop));
m_assembly.appendJumpTo(loopStart); m_assembly.appendJumpTo(loopStart);
m_assembly.appendLabel(loopEnd); m_assembly.appendLabel(loopEnd);
@ -545,7 +535,7 @@ int CodeTransform::appendPopUntil(int _targetDepth)
void CodeTransform::operator()(Break const& _break) void CodeTransform::operator()(Break const& _break)
{ {
yulAssert(!m_context->forLoopStack.empty(), "Invalid break-statement. Requires surrounding for-loop in code generation."); yulAssert(!m_context->forLoopStack.empty(), "Invalid break-statement. Requires surrounding for-loop in code generation.");
m_assembly.setSourceLocation(extractSourceLocationFromDebugData(_break.debugData)); m_assembly.setSourceLocation(locationOf(_break));
Context::JumpInfo const& jump = m_context->forLoopStack.top().done; Context::JumpInfo const& jump = m_context->forLoopStack.top().done;
m_assembly.appendJumpTo(jump.label, appendPopUntil(jump.targetStackHeight)); m_assembly.appendJumpTo(jump.label, appendPopUntil(jump.targetStackHeight));
@ -554,7 +544,7 @@ void CodeTransform::operator()(Break const& _break)
void CodeTransform::operator()(Continue const& _continue) void CodeTransform::operator()(Continue const& _continue)
{ {
yulAssert(!m_context->forLoopStack.empty(), "Invalid continue-statement. Requires surrounding for-loop in code generation."); yulAssert(!m_context->forLoopStack.empty(), "Invalid continue-statement. Requires surrounding for-loop in code generation.");
m_assembly.setSourceLocation(extractSourceLocationFromDebugData(_continue.debugData)); m_assembly.setSourceLocation(locationOf(_continue));
Context::JumpInfo const& jump = m_context->forLoopStack.top().post; Context::JumpInfo const& jump = m_context->forLoopStack.top().post;
m_assembly.appendJumpTo(jump.label, appendPopUntil(jump.targetStackHeight)); m_assembly.appendJumpTo(jump.label, appendPopUntil(jump.targetStackHeight));
@ -564,7 +554,7 @@ void CodeTransform::operator()(Leave const& _leaveStatement)
{ {
yulAssert(m_functionExitLabel, "Invalid leave-statement. Requires surrounding function in code generation."); yulAssert(m_functionExitLabel, "Invalid leave-statement. Requires surrounding function in code generation.");
yulAssert(m_functionExitStackHeight, ""); yulAssert(m_functionExitStackHeight, "");
m_assembly.setSourceLocation(extractSourceLocationFromDebugData(_leaveStatement.debugData)); m_assembly.setSourceLocation(locationOf(_leaveStatement));
m_assembly.appendJumpTo(*m_functionExitLabel, appendPopUntil(*m_functionExitStackHeight)); m_assembly.appendJumpTo(*m_functionExitLabel, appendPopUntil(*m_functionExitStackHeight));
} }
@ -676,7 +666,7 @@ void CodeTransform::visitStatements(vector<Statement> const& _statements)
auto const* functionDefinition = std::get_if<FunctionDefinition>(&statement); auto const* functionDefinition = std::get_if<FunctionDefinition>(&statement);
if (functionDefinition && !jumpTarget) if (functionDefinition && !jumpTarget)
{ {
m_assembly.setSourceLocation(extractSourceLocationFromDebugData(functionDefinition->debugData)); m_assembly.setSourceLocation(locationOf(*functionDefinition));
jumpTarget = m_assembly.newLabelId(); jumpTarget = m_assembly.newLabelId();
m_assembly.appendJumpTo(*jumpTarget, 0); m_assembly.appendJumpTo(*jumpTarget, 0);
} }
@ -697,7 +687,7 @@ void CodeTransform::visitStatements(vector<Statement> const& _statements)
void CodeTransform::finalizeBlock(Block const& _block, optional<int> blockStartStackHeight) void CodeTransform::finalizeBlock(Block const& _block, optional<int> blockStartStackHeight)
{ {
m_assembly.setSourceLocation(extractSourceLocationFromDebugData(_block.debugData)); m_assembly.setSourceLocation(locationOf(_block));
freeUnusedVariables(); freeUnusedVariables();