mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Code generation for errors.
This commit is contained in:
@@ -103,6 +103,22 @@ void CompilerUtils::revertWithStringData(Type const& _argumentType)
|
||||
m_context << Instruction::REVERT;
|
||||
}
|
||||
|
||||
void CompilerUtils::revertWithError(
|
||||
string const& _signature,
|
||||
vector<Type const*> const& _parameterTypes,
|
||||
vector<Type const*> const& _argumentTypes
|
||||
)
|
||||
{
|
||||
fetchFreeMemoryPointer();
|
||||
m_context << util::selectorFromSignature(_signature);
|
||||
m_context << Instruction::DUP2 << Instruction::MSTORE;
|
||||
m_context << u256(4) << Instruction::ADD;
|
||||
// Stack: <arguments...> <mem pos of encoding start>
|
||||
abiEncode(_argumentTypes, _parameterTypes);
|
||||
toSizeAfterFreeMemoryPointer();
|
||||
m_context << Instruction::REVERT;
|
||||
}
|
||||
|
||||
void CompilerUtils::returnDataToArray()
|
||||
{
|
||||
if (m_context.evmVersion().supportsReturndata())
|
||||
|
||||
@@ -69,6 +69,12 @@ public:
|
||||
/// Stack post:
|
||||
void revertWithStringData(Type const& _argumentType);
|
||||
|
||||
void revertWithError(
|
||||
std::string const& _errorName,
|
||||
std::vector<Type const*> const& _parameterTypes,
|
||||
std::vector<Type const*> const& _argumentTypes
|
||||
);
|
||||
|
||||
/// Allocates a new array and copies the return data to it.
|
||||
/// If the EVM does not support return data, creates an empty array.
|
||||
void returnDataToArray();
|
||||
|
||||
@@ -1281,6 +1281,15 @@ bool ContractCompiler::visit(EmitStatement const& _emit)
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ContractCompiler::visit(RevertStatement const& _revert)
|
||||
{
|
||||
CompilerContext::LocationSetter locationSetter(m_context, _revert);
|
||||
StackHeightChecker checker(m_context);
|
||||
compileExpression(_revert.errorCall());
|
||||
checker.check();
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ContractCompiler::visit(VariableDeclarationStatement const& _variableDeclarationStatement)
|
||||
{
|
||||
CompilerContext::LocationSetter locationSetter(m_context, _variableDeclarationStatement);
|
||||
|
||||
@@ -117,6 +117,7 @@ private:
|
||||
bool visit(Return const& _return) override;
|
||||
bool visit(Throw const& _throw) override;
|
||||
bool visit(EmitStatement const& _emit) override;
|
||||
bool visit(RevertStatement const& _revert) override;
|
||||
bool visit(VariableDeclarationStatement const& _variableDeclarationStatement) override;
|
||||
bool visit(ExpressionStatement const& _expressionStatement) override;
|
||||
bool visit(PlaceholderStatement const&) override;
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
#include <libsolidity/codegen/LValue.h>
|
||||
|
||||
#include <libsolidity/ast/AST.h>
|
||||
#include <libsolidity/ast/ASTUtils.h>
|
||||
#include <libsolidity/ast/TypeProvider.h>
|
||||
|
||||
#include <libevmasm/GasMeter.h>
|
||||
@@ -914,7 +915,20 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
|
||||
}
|
||||
case FunctionType::Kind::Error:
|
||||
{
|
||||
solAssert(false, "");
|
||||
_functionCall.expression().accept(*this);
|
||||
vector<Type const*> argumentTypes;
|
||||
for (ASTPointer<Expression const> const& arg: _functionCall.sortedArguments())
|
||||
{
|
||||
arg->accept(*this);
|
||||
argumentTypes.push_back(arg->annotation().type);
|
||||
}
|
||||
solAssert(dynamic_cast<ErrorDefinition const*>(&function.declaration()), "");
|
||||
utils().revertWithError(
|
||||
function.externalSignature(),
|
||||
function.parameterTypes(),
|
||||
argumentTypes
|
||||
);
|
||||
break;
|
||||
}
|
||||
case FunctionType::Kind::BlockHash:
|
||||
{
|
||||
@@ -1868,6 +1882,7 @@ bool ExpressionCompiler::visit(MemberAccess const& _memberAccess)
|
||||
solAssert(
|
||||
dynamic_cast<VariableDeclaration const*>(_memberAccess.annotation().referencedDeclaration) ||
|
||||
dynamic_cast<FunctionDefinition const*>(_memberAccess.annotation().referencedDeclaration) ||
|
||||
dynamic_cast<ErrorDefinition const*>(_memberAccess.annotation().referencedDeclaration) ||
|
||||
category == Type::Category::TypeType ||
|
||||
category == Type::Category::Module,
|
||||
""
|
||||
|
||||
@@ -1045,7 +1045,14 @@ void IRGeneratorForStatements::endVisit(FunctionCall const& _functionCall)
|
||||
}
|
||||
case FunctionType::Kind::Error:
|
||||
{
|
||||
solAssert(false, "");
|
||||
ErrorDefinition const* error = dynamic_cast<ErrorDefinition const*>(ASTNode::referencedDeclaration(_functionCall.expression()));
|
||||
solAssert(error, "");
|
||||
revertWithError(
|
||||
error->functionType(true)->externalSignature(),
|
||||
error->functionType(true)->parameterTypes(),
|
||||
_functionCall.sortedArguments()
|
||||
);
|
||||
break;
|
||||
}
|
||||
case FunctionType::Kind::Assert:
|
||||
case FunctionType::Kind::Require:
|
||||
@@ -1199,41 +1206,19 @@ void IRGeneratorForStatements::endVisit(FunctionCall const& _functionCall)
|
||||
case FunctionType::Kind::Revert:
|
||||
{
|
||||
solAssert(arguments.size() == parameterTypes.size(), "");
|
||||
if (arguments.empty())
|
||||
solAssert(arguments.size() <= 1, "");
|
||||
solAssert(
|
||||
arguments.empty() ||
|
||||
arguments.front()->annotation().type->isImplicitlyConvertibleTo(*TypeProvider::stringMemory()),
|
||||
"");
|
||||
if (m_context.revertStrings() == RevertStrings::Strip || arguments.empty())
|
||||
m_code << "revert(0, 0)\n";
|
||||
else
|
||||
{
|
||||
solAssert(arguments.size() == 1, "");
|
||||
|
||||
if (m_context.revertStrings() == RevertStrings::Strip)
|
||||
m_code << "revert(0, 0)\n";
|
||||
else
|
||||
{
|
||||
solAssert(type(*arguments.front()).isImplicitlyConvertibleTo(*TypeProvider::stringMemory()),"");
|
||||
|
||||
Whiskers templ(R"({
|
||||
let <pos> := <allocateUnbounded>()
|
||||
mstore(<pos>, <hash>)
|
||||
let <end> := <encode>(add(<pos>, 4) <argumentVars>)
|
||||
revert(<pos>, sub(<end>, <pos>))
|
||||
})");
|
||||
templ("pos", m_context.newYulVariable());
|
||||
templ("end", m_context.newYulVariable());
|
||||
templ("hash", util::selectorFromSignature("Error(string)").str());
|
||||
templ("allocateUnbounded", m_utils.allocateUnboundedFunction());
|
||||
templ(
|
||||
"argumentVars",
|
||||
joinHumanReadablePrefixed(IRVariable{*arguments.front()}.stackSlots())
|
||||
);
|
||||
templ("encode", m_context.abiFunctions().tupleEncoder(
|
||||
{&type(*arguments.front())},
|
||||
{TypeProvider::stringMemory()}
|
||||
));
|
||||
|
||||
m_code << templ.render();
|
||||
}
|
||||
}
|
||||
|
||||
revertWithError(
|
||||
"Error(string)",
|
||||
{TypeProvider::stringMemory()},
|
||||
{arguments.front()}
|
||||
);
|
||||
break;
|
||||
}
|
||||
// Array creation using new
|
||||
@@ -1995,7 +1980,7 @@ void IRGeneratorForStatements::endVisit(MemberAccess const& _memberAccess)
|
||||
dynamic_cast<ErrorDefinition const*>(_memberAccess.annotation().referencedDeclaration),
|
||||
"Error not found"
|
||||
);
|
||||
// the call will do the resolving
|
||||
// The function call will resolve the selector.
|
||||
break;
|
||||
case FunctionType::Kind::DelegateCall:
|
||||
define(IRVariable(_memberAccess).part("address"), _memberAccess.expression());
|
||||
@@ -2043,6 +2028,7 @@ void IRGeneratorForStatements::endVisit(MemberAccess const& _memberAccess)
|
||||
solAssert(
|
||||
dynamic_cast<VariableDeclaration const*>(_memberAccess.annotation().referencedDeclaration) ||
|
||||
dynamic_cast<FunctionDefinition const*>(_memberAccess.annotation().referencedDeclaration) ||
|
||||
dynamic_cast<ErrorDefinition const*>(_memberAccess.annotation().referencedDeclaration) ||
|
||||
category == Type::Category::TypeType ||
|
||||
category == Type::Category::Module,
|
||||
""
|
||||
@@ -3140,6 +3126,38 @@ void IRGeneratorForStatements::rethrow()
|
||||
m_code << "revert(0, 0) // rethrow\n"s;
|
||||
}
|
||||
|
||||
void IRGeneratorForStatements::revertWithError(
|
||||
string const& _signature,
|
||||
vector<Type const*> const& _parameterTypes,
|
||||
vector<ASTPointer<Expression const>> const& _errorArguments
|
||||
)
|
||||
{
|
||||
Whiskers templ(R"({
|
||||
let <pos> := <allocateUnbounded>()
|
||||
mstore(<pos>, <hash>)
|
||||
let <end> := <encode>(add(<pos>, 4) <argumentVars>)
|
||||
revert(<pos>, sub(<end>, <pos>))
|
||||
})");
|
||||
templ("pos", m_context.newYulVariable());
|
||||
templ("end", m_context.newYulVariable());
|
||||
templ("hash", util::selectorFromSignature(_signature).str());
|
||||
templ("allocateUnbounded", m_utils.allocateUnboundedFunction());
|
||||
|
||||
vector<string> errorArgumentVars;
|
||||
vector<Type const*> errorArgumentTypes;
|
||||
for (ASTPointer<Expression const> const& arg: _errorArguments)
|
||||
{
|
||||
errorArgumentVars += IRVariable(*arg).stackSlots();
|
||||
solAssert(arg->annotation().type, "");
|
||||
errorArgumentTypes.push_back(arg->annotation().type);
|
||||
}
|
||||
templ("argumentVars", joinHumanReadablePrefixed(errorArgumentVars));
|
||||
templ("encode", m_context.abiFunctions().tupleEncoder(errorArgumentTypes, _parameterTypes));
|
||||
|
||||
m_code << templ.render();
|
||||
}
|
||||
|
||||
|
||||
bool IRGeneratorForStatements::visit(TryCatchClause const& _clause)
|
||||
{
|
||||
_clause.block().accept(*this);
|
||||
|
||||
@@ -106,6 +106,15 @@ private:
|
||||
/// Generates code to rethrow an exception.
|
||||
void rethrow();
|
||||
|
||||
/// Generates code to revert with an error. The error arguments are assumed to
|
||||
/// be already evaluated and available in local IRVariables, but not yet
|
||||
/// converted.
|
||||
void revertWithError(
|
||||
std::string const& _signature,
|
||||
std::vector<Type const*> const& _parameterTypes,
|
||||
std::vector<ASTPointer<Expression const>> const& _errorArguments
|
||||
);
|
||||
|
||||
void handleVariableReference(
|
||||
VariableDeclaration const& _variable,
|
||||
Expression const& _referencingExpression
|
||||
|
||||
Reference in New Issue
Block a user