| 348 | } |
| 349 | |
| 350 | std::string IRGenerator::generateFunction(FunctionDefinition const& _function) |
| 351 | { |
| 352 | std::string functionName = IRNames::function(_function); |
| 353 | return m_context.functionCollector().createFunction(functionName, [&]() { |
| 354 | m_context.resetLocalVariables(); |
| 355 | Whiskers t(R"( |
| 356 | <astIDComment><sourceLocationComment> |
| 357 | function <functionName>(<params>)<?+retParams> -> <retParams></+retParams> { |
| 358 | <retInit> |
| 359 | <body> |
| 360 | } |
| 361 | <contractSourceLocationComment> |
| 362 | )"); |
| 363 | |
| 364 | if (m_context.debugInfoSelection().astID) |
| 365 | t("astIDComment", "/// @ast-id " + std::to_string(_function.id()) + "\n"); |
| 366 | else |
| 367 | t("astIDComment", ""); |
| 368 | t("sourceLocationComment", dispenseLocationComment(_function)); |
| 369 | t( |
| 370 | "contractSourceLocationComment", |
| 371 | dispenseLocationComment(m_context.mostDerivedContract()) |
| 372 | ); |
| 373 | |
| 374 | t("functionName", functionName); |
| 375 | std::vector<std::string> params; |
| 376 | for (auto const& varDecl: _function.parameters()) |
| 377 | params += m_context.addLocalVariable(*varDecl).stackSlots(); |
| 378 | t("params", joinHumanReadable(params)); |
| 379 | std::vector<std::string> retParams; |
| 380 | std::string retInit; |
| 381 | for (auto const& varDecl: _function.returnParameters()) |
| 382 | { |
| 383 | retParams += m_context.addLocalVariable(*varDecl).stackSlots(); |
| 384 | retInit += generateInitialAssignment(*varDecl); |
| 385 | } |
| 386 | |
| 387 | t("retParams", joinHumanReadable(retParams)); |
| 388 | t("retInit", retInit); |
| 389 | |
| 390 | if (_function.modifiers().empty()) |
| 391 | t("body", generate(_function.body())); |
| 392 | else |
| 393 | { |
| 394 | for (size_t i = 0; i < _function.modifiers().size(); ++i) |
| 395 | { |
| 396 | ModifierInvocation const& modifier = *_function.modifiers().at(i); |
| 397 | std::string next = |
| 398 | i + 1 < _function.modifiers().size() ? |
| 399 | IRNames::modifierInvocation(*_function.modifiers().at(i + 1)) : |
| 400 | IRNames::functionWithModifierInner(_function); |
| 401 | generateModifier(modifier, _function, next); |
| 402 | } |
| 403 | t("body", |
| 404 | (retParams.empty() ? std::string{} : joinHumanReadable(retParams) + " := ") + |
| 405 | IRNames::modifierInvocation(*_function.modifiers().at(0)) + |
| 406 | "(" + |
| 407 | joinHumanReadable(retParams + params) + |
nothing calls this directly
no test coverage detected