Merge pull request #8534 from mijovic/sol2YulConstructorParams

[Sol2Yul] Adding support for constructor with parameters
This commit is contained in:
chriseth 2020-03-30 20:41:00 +02:00 committed by GitHub
commit e0c2b4bb9c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 3 deletions

View File

@ -259,11 +259,28 @@ string IRGenerator::constructorCode(ContractDefinition const& _contract)
if (constructor) if (constructor)
{ {
solUnimplementedAssert(constructor->parameters().empty(), ""); ABIFunctions abiFunctions(m_evmVersion, m_context.revertStrings(), m_context.functionCollector());
unsigned paramVars = make_shared<TupleType>(constructor->functionType(false)->parameterTypes())->sizeOnStack();
// TODO base constructors Whiskers t(R"X(
let programSize := datasize("<object>")
let argSize := sub(codesize(), programSize)
out << m_context.functionName(*constructor) + "()\n"; let memoryDataOffset := <allocate>(argSize)
codecopy(memoryDataOffset, programSize, argSize)
<assignToParams> <abiDecode>(memoryDataOffset, add(memoryDataOffset, argSize))
<constructorName>(<params>)
)X");
t("object", creationObjectName(_contract));
t("allocate", m_utils.allocationFunction());
t("assignToParams", paramVars == 0 ? "" : "let " + suffixedVariableNameList("param_", 0, paramVars) + " := ");
t("params", suffixedVariableNameList("param_", 0, paramVars));
t("abiDecode", abiFunctions.tupleDecoder(constructor->functionType(false)->parameterTypes(), true));
t("constructorName", m_context.functionName(*constructor));
out << t.render();
} }
return out.str(); return out.str();

View File

@ -0,0 +1,15 @@
contract C {
uint public i;
uint public k;
constructor(uint newI, uint newK) public {
i = newI;
k = newK;
}
}
// ====
// compileViaYul: also
// ----
// constructor(): 2, 0 ->
// i() -> 2
// k() -> 0