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:
parent
3353107779
commit
d5669696d5
@ -25,10 +25,6 @@ class VariableDeclaration;
|
|||||||
class Declaration;
|
class Declaration;
|
||||||
class Expression;
|
class Expression;
|
||||||
|
|
||||||
/// @returns the declaration referenced from the expression which has to be MemberAccess
|
|
||||||
/// or Identifier. Returns nullptr otherwise.
|
|
||||||
Declaration const* referencedDeclaration(Expression const& _expression);
|
|
||||||
|
|
||||||
/// Find the topmost referenced constant variable declaration when the given variable
|
/// Find the topmost referenced constant variable declaration when the given variable
|
||||||
/// declaration value is an identifier. Works only for constant variable declarations.
|
/// declaration value is an identifier. Works only for constant variable declarations.
|
||||||
/// Returns nullptr if an identifier in the chain is not referencing a constant variable declaration.
|
/// Returns nullptr if an identifier in the chain is not referencing a constant variable declaration.
|
||||||
|
@ -103,6 +103,22 @@ void CompilerUtils::revertWithStringData(Type const& _argumentType)
|
|||||||
m_context << Instruction::REVERT;
|
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()
|
void CompilerUtils::returnDataToArray()
|
||||||
{
|
{
|
||||||
if (m_context.evmVersion().supportsReturndata())
|
if (m_context.evmVersion().supportsReturndata())
|
||||||
|
@ -69,6 +69,12 @@ public:
|
|||||||
/// Stack post:
|
/// Stack post:
|
||||||
void revertWithStringData(Type const& _argumentType);
|
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.
|
/// Allocates a new array and copies the return data to it.
|
||||||
/// If the EVM does not support return data, creates an empty array.
|
/// If the EVM does not support return data, creates an empty array.
|
||||||
void returnDataToArray();
|
void returnDataToArray();
|
||||||
|
@ -1281,6 +1281,15 @@ bool ContractCompiler::visit(EmitStatement const& _emit)
|
|||||||
return false;
|
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)
|
bool ContractCompiler::visit(VariableDeclarationStatement const& _variableDeclarationStatement)
|
||||||
{
|
{
|
||||||
CompilerContext::LocationSetter locationSetter(m_context, _variableDeclarationStatement);
|
CompilerContext::LocationSetter locationSetter(m_context, _variableDeclarationStatement);
|
||||||
|
@ -117,6 +117,7 @@ private:
|
|||||||
bool visit(Return const& _return) override;
|
bool visit(Return const& _return) override;
|
||||||
bool visit(Throw const& _throw) override;
|
bool visit(Throw const& _throw) override;
|
||||||
bool visit(EmitStatement const& _emit) override;
|
bool visit(EmitStatement const& _emit) override;
|
||||||
|
bool visit(RevertStatement const& _revert) override;
|
||||||
bool visit(VariableDeclarationStatement const& _variableDeclarationStatement) override;
|
bool visit(VariableDeclarationStatement const& _variableDeclarationStatement) override;
|
||||||
bool visit(ExpressionStatement const& _expressionStatement) override;
|
bool visit(ExpressionStatement const& _expressionStatement) override;
|
||||||
bool visit(PlaceholderStatement const&) override;
|
bool visit(PlaceholderStatement const&) override;
|
||||||
|
@ -29,6 +29,7 @@
|
|||||||
#include <libsolidity/codegen/LValue.h>
|
#include <libsolidity/codegen/LValue.h>
|
||||||
|
|
||||||
#include <libsolidity/ast/AST.h>
|
#include <libsolidity/ast/AST.h>
|
||||||
|
#include <libsolidity/ast/ASTUtils.h>
|
||||||
#include <libsolidity/ast/TypeProvider.h>
|
#include <libsolidity/ast/TypeProvider.h>
|
||||||
|
|
||||||
#include <libevmasm/GasMeter.h>
|
#include <libevmasm/GasMeter.h>
|
||||||
@ -914,7 +915,20 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
|
|||||||
}
|
}
|
||||||
case FunctionType::Kind::Error:
|
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:
|
case FunctionType::Kind::BlockHash:
|
||||||
{
|
{
|
||||||
@ -1868,6 +1882,7 @@ bool ExpressionCompiler::visit(MemberAccess const& _memberAccess)
|
|||||||
solAssert(
|
solAssert(
|
||||||
dynamic_cast<VariableDeclaration const*>(_memberAccess.annotation().referencedDeclaration) ||
|
dynamic_cast<VariableDeclaration const*>(_memberAccess.annotation().referencedDeclaration) ||
|
||||||
dynamic_cast<FunctionDefinition 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::TypeType ||
|
||||||
category == Type::Category::Module,
|
category == Type::Category::Module,
|
||||||
""
|
""
|
||||||
|
@ -1045,7 +1045,14 @@ void IRGeneratorForStatements::endVisit(FunctionCall const& _functionCall)
|
|||||||
}
|
}
|
||||||
case FunctionType::Kind::Error:
|
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::Assert:
|
||||||
case FunctionType::Kind::Require:
|
case FunctionType::Kind::Require:
|
||||||
@ -1199,41 +1206,19 @@ void IRGeneratorForStatements::endVisit(FunctionCall const& _functionCall)
|
|||||||
case FunctionType::Kind::Revert:
|
case FunctionType::Kind::Revert:
|
||||||
{
|
{
|
||||||
solAssert(arguments.size() == parameterTypes.size(), "");
|
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";
|
m_code << "revert(0, 0)\n";
|
||||||
else
|
else
|
||||||
{
|
revertWithError(
|
||||||
solAssert(arguments.size() == 1, "");
|
"Error(string)",
|
||||||
|
{TypeProvider::stringMemory()},
|
||||||
if (m_context.revertStrings() == RevertStrings::Strip)
|
{arguments.front()}
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// Array creation using new
|
// Array creation using new
|
||||||
@ -1995,7 +1980,7 @@ void IRGeneratorForStatements::endVisit(MemberAccess const& _memberAccess)
|
|||||||
dynamic_cast<ErrorDefinition const*>(_memberAccess.annotation().referencedDeclaration),
|
dynamic_cast<ErrorDefinition const*>(_memberAccess.annotation().referencedDeclaration),
|
||||||
"Error not found"
|
"Error not found"
|
||||||
);
|
);
|
||||||
// the call will do the resolving
|
// The function call will resolve the selector.
|
||||||
break;
|
break;
|
||||||
case FunctionType::Kind::DelegateCall:
|
case FunctionType::Kind::DelegateCall:
|
||||||
define(IRVariable(_memberAccess).part("address"), _memberAccess.expression());
|
define(IRVariable(_memberAccess).part("address"), _memberAccess.expression());
|
||||||
@ -2043,6 +2028,7 @@ void IRGeneratorForStatements::endVisit(MemberAccess const& _memberAccess)
|
|||||||
solAssert(
|
solAssert(
|
||||||
dynamic_cast<VariableDeclaration const*>(_memberAccess.annotation().referencedDeclaration) ||
|
dynamic_cast<VariableDeclaration const*>(_memberAccess.annotation().referencedDeclaration) ||
|
||||||
dynamic_cast<FunctionDefinition 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::TypeType ||
|
||||||
category == Type::Category::Module,
|
category == Type::Category::Module,
|
||||||
""
|
""
|
||||||
@ -3140,6 +3126,38 @@ void IRGeneratorForStatements::rethrow()
|
|||||||
m_code << "revert(0, 0) // rethrow\n"s;
|
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)
|
bool IRGeneratorForStatements::visit(TryCatchClause const& _clause)
|
||||||
{
|
{
|
||||||
_clause.block().accept(*this);
|
_clause.block().accept(*this);
|
||||||
|
@ -106,6 +106,15 @@ private:
|
|||||||
/// Generates code to rethrow an exception.
|
/// Generates code to rethrow an exception.
|
||||||
void rethrow();
|
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(
|
void handleVariableReference(
|
||||||
VariableDeclaration const& _variable,
|
VariableDeclaration const& _variable,
|
||||||
Expression const& _referencingExpression
|
Expression const& _referencingExpression
|
||||||
|
@ -0,0 +1,24 @@
|
|||||||
|
error E(uint a);
|
||||||
|
library L {
|
||||||
|
error E(uint a, uint b);
|
||||||
|
}
|
||||||
|
interface I {
|
||||||
|
error E(uint a, uint b, uint c);
|
||||||
|
}
|
||||||
|
contract C {
|
||||||
|
function f() public pure {
|
||||||
|
revert E(1);
|
||||||
|
}
|
||||||
|
function g() public pure {
|
||||||
|
revert L.E(1, 2);
|
||||||
|
}
|
||||||
|
function h() public pure {
|
||||||
|
revert I.E(1, 2, 3);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// ====
|
||||||
|
// compileViaYul: also
|
||||||
|
// ----
|
||||||
|
// f() -> FAILURE, hex"002ff067", hex"0000000000000000000000000000000000000000000000000000000000000001"
|
||||||
|
// g() -> FAILURE, hex"85208890", hex"0000000000000000000000000000000000000000000000000000000000000001", hex"0000000000000000000000000000000000000000000000000000000000000002"
|
||||||
|
// h() -> FAILURE, hex"7924ea7c", hex"0000000000000000000000000000000000000000000000000000000000000001", hex"0000000000000000000000000000000000000000000000000000000000000002", hex"0000000000000000000000000000000000000000000000000000000000000003"
|
10
test/libsolidity/semanticTests/errors/named_error_args.sol
Normal file
10
test/libsolidity/semanticTests/errors/named_error_args.sol
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
error E(uint a, uint b);
|
||||||
|
contract C {
|
||||||
|
function f() public pure {
|
||||||
|
revert E({b: 7, a: 2});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// ====
|
||||||
|
// compileViaYul: also
|
||||||
|
// ----
|
||||||
|
// f() -> FAILURE, hex"85208890", hex"0000000000000000000000000000000000000000000000000000000000000002", hex"0000000000000000000000000000000000000000000000000000000000000007"
|
12
test/libsolidity/semanticTests/errors/revert_conversion.sol
Normal file
12
test/libsolidity/semanticTests/errors/revert_conversion.sol
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
error E(string a, uint[] b);
|
||||||
|
contract C {
|
||||||
|
uint[] x;
|
||||||
|
function f() public {
|
||||||
|
x.push(7);
|
||||||
|
revert E("abc", x);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// ====
|
||||||
|
// compileViaYul: also
|
||||||
|
// ----
|
||||||
|
// f() -> FAILURE, hex"59e4d4df", 0x40, 0x80, 3, "abc", 1, 7
|
10
test/libsolidity/semanticTests/errors/simple.sol
Normal file
10
test/libsolidity/semanticTests/errors/simple.sol
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
error E(uint a, uint b);
|
||||||
|
contract C {
|
||||||
|
function f() public pure {
|
||||||
|
revert E(2, 7);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// ====
|
||||||
|
// compileViaYul: also
|
||||||
|
// ----
|
||||||
|
// f() -> FAILURE, hex"85208890", 2, 7
|
18
test/libsolidity/semanticTests/errors/via_contract_type.sol
Normal file
18
test/libsolidity/semanticTests/errors/via_contract_type.sol
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
contract A {
|
||||||
|
error E(uint);
|
||||||
|
}
|
||||||
|
contract X {
|
||||||
|
error E(string);
|
||||||
|
}
|
||||||
|
contract B is A {
|
||||||
|
function f() public pure { revert E(1); }
|
||||||
|
function g() public pure { revert A.E(1); }
|
||||||
|
function h() public pure { revert X.E("abc"); }
|
||||||
|
|
||||||
|
}
|
||||||
|
// ====
|
||||||
|
// compileViaYul: also
|
||||||
|
// ----
|
||||||
|
// f() -> FAILURE, hex"002ff067", hex"0000000000000000000000000000000000000000000000000000000000000001"
|
||||||
|
// g() -> FAILURE, hex"002ff067", hex"0000000000000000000000000000000000000000000000000000000000000001"
|
||||||
|
// h() -> FAILURE, hex"3e9992c9", hex"0000000000000000000000000000000000000000000000000000000000000020", hex"0000000000000000000000000000000000000000000000000000000000000003", hex"6162630000000000000000000000000000000000000000000000000000000000"
|
25
test/libsolidity/semanticTests/errors/via_import.sol
Normal file
25
test/libsolidity/semanticTests/errors/via_import.sol
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
==== Source: s1.sol ====
|
||||||
|
error E(uint);
|
||||||
|
==== Source: s2.sol ====
|
||||||
|
import "s1.sol" as S;
|
||||||
|
==== Source: s3.sol ====
|
||||||
|
import "s1.sol" as S;
|
||||||
|
import "s2.sol" as T;
|
||||||
|
import "s1.sol";
|
||||||
|
contract C {
|
||||||
|
function x() public pure {
|
||||||
|
revert E(1);
|
||||||
|
}
|
||||||
|
function y() public pure {
|
||||||
|
revert S.E(2);
|
||||||
|
}
|
||||||
|
function z() public pure {
|
||||||
|
revert T.S.E(3);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// ====
|
||||||
|
// compileViaYul: also
|
||||||
|
// ----
|
||||||
|
// x() -> FAILURE, hex"002ff067", hex"0000000000000000000000000000000000000000000000000000000000000001"
|
||||||
|
// y() -> FAILURE, hex"002ff067", hex"0000000000000000000000000000000000000000000000000000000000000002"
|
||||||
|
// z() -> FAILURE, hex"002ff067", hex"0000000000000000000000000000000000000000000000000000000000000003"
|
10
test/libsolidity/semanticTests/errors/weird_name.sol
Normal file
10
test/libsolidity/semanticTests/errors/weird_name.sol
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
error error(uint a);
|
||||||
|
contract C {
|
||||||
|
function f() public pure {
|
||||||
|
revert error(2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// ====
|
||||||
|
// compileViaYul: also
|
||||||
|
// ----
|
||||||
|
// f() -> FAILURE, hex"b48fb6cf", hex"0000000000000000000000000000000000000000000000000000000000000002"
|
17
test/libsolidity/semanticTests/reverts/error_struct.sol
Normal file
17
test/libsolidity/semanticTests/reverts/error_struct.sol
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
struct error { uint error; }
|
||||||
|
contract C {
|
||||||
|
error test();
|
||||||
|
error _struct;
|
||||||
|
function f() public {
|
||||||
|
revert test();
|
||||||
|
}
|
||||||
|
function g(uint x) public returns (uint) {
|
||||||
|
_struct.error = x;
|
||||||
|
return _struct.error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// ====
|
||||||
|
// compileViaYul: also
|
||||||
|
// ----
|
||||||
|
// f() -> FAILURE, hex"f8a8fd6d"
|
||||||
|
// g(uint256): 7 -> 7
|
Loading…
Reference in New Issue
Block a user