Merge pull request #8684 from ethereum/yulForSelector

Yul codegen for `.selector`.
This commit is contained in:
chriseth 2020-04-20 16:32:11 +02:00 committed by GitHub
commit 1d8e742296
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 4 deletions

View File

@ -845,11 +845,18 @@ void IRGeneratorForStatements::endVisit(MemberAccess const& _memberAccess)
case Type::Category::Function: case Type::Category::Function:
if (member == "selector") if (member == "selector")
{ {
solUnimplementedAssert( FunctionType const& functionType = dynamic_cast<FunctionType const&>(
dynamic_cast<FunctionType const&>(*_memberAccess.expression().annotation().type).kind() == *_memberAccess.expression().annotation().type
FunctionType::Kind::External, ""
); );
if (functionType.kind() == FunctionType::Kind::External)
define(IRVariable{_memberAccess}, IRVariable(_memberAccess.expression()).part("functionIdentifier")); define(IRVariable{_memberAccess}, IRVariable(_memberAccess.expression()).part("functionIdentifier"));
else if (functionType.kind() == FunctionType::Kind::Declaration)
{
solAssert(functionType.hasDeclaration(), "");
define(IRVariable{_memberAccess}) << formatNumber(functionType.externalIdentifier() << 224) << "\n";
}
else
solAssert(false, "Invalid use of .selector");
} }
else if (member == "address") else if (member == "address")
{ {
@ -976,6 +983,16 @@ void IRGeneratorForStatements::endVisit(MemberAccess const& _memberAccess)
solAssert(false, "Illegal fixed bytes member."); solAssert(false, "Illegal fixed bytes member.");
break; break;
} }
case Type::Category::TypeType:
{
TypeType const& type = dynamic_cast<TypeType const&>(*_memberAccess.expression().annotation().type);
solUnimplementedAssert(type.actualType()->category() == Type::Category::Contract, "");
FunctionType const* funType = dynamic_cast<FunctionType const*>(_memberAccess.annotation().type);
solUnimplementedAssert(funType, "");
solUnimplementedAssert(funType->kind() == FunctionType::Kind::Declaration, "");
// Nothing to do for declaration.
break;
}
default: default:
solAssert(false, "Member access to unknown type."); solAssert(false, "Member access to unknown type.");
} }

View File

@ -19,6 +19,8 @@ contract C {
return this.x.selector; return this.x.selector;
} }
} }
// ====
// compileViaYul: also
// ---- // ----
// f() -> 0x26121ff000000000000000000000000000000000000000000000000000000000 // f() -> 0x26121ff000000000000000000000000000000000000000000000000000000000
// g() -> 0x26121ff000000000000000000000000000000000000000000000000000000000 // g() -> 0x26121ff000000000000000000000000000000000000000000000000000000000

View File

@ -15,6 +15,8 @@ contract C {
return (a.f.selector, a.g.selector, b.f.selector, b.g.selector); return (a.f.selector, a.g.selector, b.f.selector, b.g.selector);
} }
} }
// ====
// compileViaYul: also
// ---- // ----
// test1() -> left(0x26121ff0), left(0xe420264a), left(0x26121ff0), left(0xe420264a) // test1() -> left(0x26121ff0), left(0xe420264a), left(0x26121ff0), left(0xe420264a)
// test2() -> left(0x26121ff0), left(0xe420264a), left(0x26121ff0), left(0xe420264a) // test2() -> left(0x26121ff0), left(0xe420264a), left(0x26121ff0), left(0xe420264a)