| | 1464 | if .hasKeywordArg and not .isForAttribute # uu |
| | 1465 | _makeHelperMethod |
| | 1466 | |
| | 1467 | def _makeHelperMethod |
| | 1468 | """ |
| | 1469 | Add a private helper method to the current box to support extended initializer. |
| | 1470 | |
| | 1471 | Foo(expr0, expr1, bar=expr2) --> |
| | 1472 | call: |
| | 1473 | _ch_ext_init_1207(expr0, expr1, expr2) |
| | 1474 | def: |
| | 1475 | def _ch_ext_init_1207(arg0 as int, arg1 as int, /#bar=#/arg2 as int) |
| | 1476 | obj = Foo(arg0, arg1) |
| | 1477 | obj.bar = arg2 |
| | 1478 | return obj |
| | 1479 | """ |
| | 1480 | box = .compiler.curBox |
| | 1481 | paramsForDecl = List<of Param>() |
| | 1482 | argsForInitCall = List<of Expr>() # args to pass to `Foo(arg0, arg1)` |
| | 1483 | propsToSet = List<of AssignExpr>() # props to set as `obj.bar = arg2` etc. |
| | 1484 | firstPropArg = -1 |
| | 1485 | i = 0 |
| | 1486 | for arg in .args |
| | 1487 | if arg inherits AssignExpr |
| | 1488 | propsToSet.add(arg) |
| | 1489 | if firstPropArg == -1, firstPropArg = i |
| | 1490 | arg = arg.right |
| | 1491 | else |
| | 1492 | argsForInitCall.add(arg) |
| | 1493 | paramsForDecl.add(Param(box.token.copy('ID', 'arg[i]'), arg.type)) |
| | 1494 | i += 1 |
| | 1495 | name = '_ch_ext_init_[.serialNum]' # ch = class helper, ext = extended, init = initializer |
| | 1496 | token = box.token.copy |
| | 1497 | m = Method(token.copy('ID', name), box, name, paramsForDecl, _type, nil, ['shared'], AttributeList(), '') |
| | 1498 | m.locals.add(LocalVar(token.copy('ID', 'obj'), .type)) |
| | 1499 | |
| | 1500 | objId = IdentifierExpr(token.copy('ID', 'obj'), 'obj') |
| | 1501 | callExpr = PostCallExpr(token.copy('ID', .type.name), IdentifierExpr(token.copy('ID', .type.name), .type), argsForInitCall) |
| | 1502 | assign = AssignExpr(token.copy('ASSIGN', '='), 'ASSIGN', objId, callExpr) |
| | 1503 | m.addStmt(assign) |
| | 1504 | |
| | 1505 | i = firstPropArg |
| | 1506 | for propSetExpr in propsToSet |
| | 1507 | propName = (propSetExpr.left to IdentifierExpr).name |
| | 1508 | memberExpr = DotExpr(token.copy('DOT', '.'), 'DOT', IdentifierExpr(token.copy('ID', 'obj')), MemberExpr(token.copy('ID', propName))) |
| | 1509 | assign = AssignExpr(token.copy('ASSIGN', '='), 'ASSIGN', memberExpr, IdentifierExpr(token.copy('ID', 'arg[i]'))) |
| | 1510 | m.addStmt(assign) |
| | 1511 | i += 1 |
| | 1512 | |
| | 1513 | retStmt = ReturnStmt(token.copy('RETURN', 'return'), IdentifierExpr(token.copy('ID', 'obj'), 'obj')) |
| | 1514 | m.addStmt(retStmt) |
| | 1515 | |
| | 1516 | m.bindAll |
| | 1517 | box.addDecl(m) |
| | 1518 | _helperMethod = m |
| | 1519 | |