mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
[Sol->Yul] Implementing type name.
This commit is contained in:
parent
1837e0ae7f
commit
ddb05769a5
@ -107,6 +107,38 @@ string YulUtilFunctions::copyToMemoryFunction(bool _fromCalldata)
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
string YulUtilFunctions::copyToMemoryLiteralFunction(string const& _literal)
|
||||||
|
{
|
||||||
|
solAssert(!_literal.empty(), "");
|
||||||
|
string functionName = "copy_literal_to_memory_" + h256(_literal, h256::AlignLeft).hex();
|
||||||
|
|
||||||
|
return m_functionCollector.createFunction(functionName, [&]() {
|
||||||
|
size_t words = (_literal.length() + 31) / 32;
|
||||||
|
vector<map<string, string>> wordParams(words);
|
||||||
|
for (size_t i = 0; i < words; ++i)
|
||||||
|
{
|
||||||
|
wordParams[i]["offset"] = to_string(i * 32);
|
||||||
|
wordParams[i]["wordValue"] = formatAsStringOrNumber(_literal.substr(32 * i, 32));
|
||||||
|
}
|
||||||
|
|
||||||
|
return Whiskers(R"(
|
||||||
|
function <functionName>() -> memPtr {
|
||||||
|
memPtr := <allocationFunction>(add(<size>, 32))
|
||||||
|
mstore(memPtr, <size>)
|
||||||
|
let dataPos := add(memPtr, 32)
|
||||||
|
<#word>
|
||||||
|
mstore(add(dataPos, <offset>), <wordValue>)
|
||||||
|
</word>
|
||||||
|
}
|
||||||
|
)")
|
||||||
|
("functionName", functionName)
|
||||||
|
("allocationFunction", allocationFunction())
|
||||||
|
("size", to_string(_literal.size()))
|
||||||
|
("word", wordParams)
|
||||||
|
.render();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
string YulUtilFunctions::requireOrAssertFunction(bool _assert, Type const* _messageType)
|
string YulUtilFunctions::requireOrAssertFunction(bool _assert, Type const* _messageType)
|
||||||
{
|
{
|
||||||
string functionName =
|
string functionName =
|
||||||
|
@ -72,6 +72,11 @@ public:
|
|||||||
/// Pads with zeros and might write more than exactly length.
|
/// Pads with zeros and might write more than exactly length.
|
||||||
std::string copyToMemoryFunction(bool _fromCalldata);
|
std::string copyToMemoryFunction(bool _fromCalldata);
|
||||||
|
|
||||||
|
/// @returns a function name that copies string literal to memory
|
||||||
|
/// and returns pointer to memory array containing it
|
||||||
|
/// signature: () -> memPtr
|
||||||
|
std::string copyToMemoryLiteralFunction(std::string const& _literal);
|
||||||
|
|
||||||
// @returns the name of a function that has the equivalent logic of an
|
// @returns the name of a function that has the equivalent logic of an
|
||||||
// `assert` or `require` call.
|
// `assert` or `require` call.
|
||||||
std::string requireOrAssertFunction(bool _assert, Type const* _messageType = nullptr);
|
std::string requireOrAssertFunction(bool _assert, Type const* _messageType = nullptr);
|
||||||
|
@ -1737,7 +1737,9 @@ void IRGeneratorForStatements::endVisit(MemberAccess const& _memberAccess)
|
|||||||
}
|
}
|
||||||
else if (member == "name")
|
else if (member == "name")
|
||||||
{
|
{
|
||||||
solUnimplementedAssert(false, "");
|
TypePointer arg = dynamic_cast<MagicType const&>(*_memberAccess.expression().annotation().type).typeArgument();
|
||||||
|
ContractDefinition const& contract = dynamic_cast<ContractType const&>(*arg).contractDefinition();
|
||||||
|
define(IRVariable(_memberAccess)) << m_utils.copyToMemoryLiteralFunction(contract.name()) << "()\n";
|
||||||
}
|
}
|
||||||
else if (member == "interfaceId")
|
else if (member == "interfaceId")
|
||||||
{
|
{
|
||||||
|
@ -22,7 +22,8 @@ contract Test is C {
|
|||||||
return type(I).name;
|
return type(I).name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// ====
|
||||||
|
// compileViaYul: also
|
||||||
// ----
|
// ----
|
||||||
// c() -> 0x20, 1, "C"
|
// c() -> 0x20, 1, "C"
|
||||||
// a() -> 0x20, 1, "A"
|
// a() -> 0x20, 1, "A"
|
||||||
|
Loading…
Reference in New Issue
Block a user