MCPcopy Create free account
hub / github.com/argotorg/solidity / generateConstructors

Method generateConstructors

libsolidity/codegen/ir/IRGenerator.cpp:879–982  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

877
878
879void IRGenerator::generateConstructors(ContractDefinition const& _contract)
880{
881 auto listAllParams =
882 [&](std::map<ContractDefinition const*, std::vector<std::string>> const& baseParams) -> std::vector<std::string>
883 {
884 std::vector<std::string> params;
885 for (ContractDefinition const* contract: _contract.annotation().linearizedBaseContracts)
886 if (baseParams.count(contract))
887 params += baseParams.at(contract);
888 return params;
889 };
890
891 std::map<ContractDefinition const*, std::vector<std::string>> baseConstructorParams;
892 for (size_t i = 0; i < _contract.annotation().linearizedBaseContracts.size(); ++i)
893 {
894 ContractDefinition const* contract = _contract.annotation().linearizedBaseContracts[i];
895 baseConstructorParams.erase(contract);
896
897 m_context.resetLocalVariables();
898 m_context.functionCollector().createFunction(IRNames::constructor(*contract), [&]() {
899 Whiskers t(R"(
900 <astIDComment><sourceLocationComment>
901 function <functionName>(<params><comma><baseParams>) {
902 <evalBaseArguments>
903 <sourceLocationComment>
904 <?hasNextConstructor> <nextConstructor>(<nextParams>) </hasNextConstructor>
905 <initStateVariables>
906 <userDefinedConstructorBody>
907 }
908 <contractSourceLocationComment>
909 )");
910 std::vector<std::string> params;
911 if (contract->constructor())
912 for (ASTPointer<VariableDeclaration> const& varDecl: contract->constructor()->parameters())
913 params += m_context.addLocalVariable(*varDecl).stackSlots();
914
915 if (m_context.debugInfoSelection().astID && contract->constructor())
916 t("astIDComment", "/// @ast-id " + std::to_string(contract->constructor()->id()) + "\n");
917 else
918 t("astIDComment", "");
919 t("sourceLocationComment", dispenseLocationComment(
920 contract->constructor() ?
921 dynamic_cast<ASTNode const&>(*contract->constructor()) :
922 dynamic_cast<ASTNode const&>(*contract)
923 ));
924 t(
925 "contractSourceLocationComment",
926 dispenseLocationComment(m_context.mostDerivedContract())
927 );
928
929 t("params", joinHumanReadable(params));
930 std::vector<std::string> baseParams = listAllParams(baseConstructorParams);
931 t("baseParams", joinHumanReadable(baseParams));
932 t("comma", !params.empty() && !baseParams.empty() ? ", " : "");
933 t("functionName", IRNames::constructor(*contract));
934 std::pair<std::string, std::map<ContractDefinition const*, std::vector<std::string>>> evaluatedArgs = evaluateConstructorArguments(*contract);
935 baseConstructorParams.insert(evaluatedArgs.second.begin(), evaluatedArgs.second.end());
936 t("evalBaseArguments", evaluatedArgs.first);

Callers

nothing calls this directly

Calls 15

constructorFunction · 0.85
dispenseLocationCommentFunction · 0.85
joinHumanReadableFunction · 0.85
atMethod · 0.80
eraseMethod · 0.80
resetLocalVariablesMethod · 0.80
parametersMethod · 0.80
stackSlotsMethod · 0.80
addLocalVariableMethod · 0.80
insertMethod · 0.80
renderMethod · 0.80
to_stringFunction · 0.50

Tested by

no test coverage detected