[Sol->Yul] Implementing type name.

This commit is contained in:
Djordje Mijovic 2020-12-11 16:33:10 +01:00
parent 1837e0ae7f
commit ddb05769a5
4 changed files with 42 additions and 2 deletions

View File

@ -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 functionName =

View File

@ -72,6 +72,11 @@ public:
/// Pads with zeros and might write more than exactly length.
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
// `assert` or `require` call.
std::string requireOrAssertFunction(bool _assert, Type const* _messageType = nullptr);

View File

@ -1737,7 +1737,9 @@ void IRGeneratorForStatements::endVisit(MemberAccess const& _memberAccess)
}
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")
{

View File

@ -22,7 +22,8 @@ contract Test is C {
return type(I).name;
}
}
// ====
// compileViaYul: also
// ----
// c() -> 0x20, 1, "C"
// a() -> 0x20, 1, "A"