@@ -2486,18 +2486,21 @@ open class KotlinFileExtractor(
2486
2486
}
2487
2487
}
2488
2488
2489
- private fun writeUpdateInPlaceExpr (origin : IrStatementOrigin , tw : TrapWriter , id : Label <DbAssignexpr >, type : TypeResults , exprParent : ExprParent ): Boolean {
2489
+ private fun writeUpdateInPlaceExpr (origin : IrStatementOrigin ): (( tw: TrapWriter , id: Label <out DbAssignexpr >, type: Label < out DbType > , exprParent: Label < out DbExprparent >, index: Int ) -> Unit ) ? {
2490
2490
when (origin) {
2491
- IrStatementOrigin .PLUSEQ -> tw .writeExprs_assignaddexpr(id.cast<DbAssignaddexpr >(), type.javaResult.id , exprParent.parent, exprParent.idx)
2492
- IrStatementOrigin .MINUSEQ -> tw .writeExprs_assignsubexpr(id.cast<DbAssignsubexpr >(), type.javaResult.id , exprParent.parent, exprParent.idx)
2493
- IrStatementOrigin .MULTEQ -> tw .writeExprs_assignmulexpr(id.cast<DbAssignmulexpr >(), type.javaResult.id , exprParent.parent, exprParent.idx)
2494
- IrStatementOrigin .DIVEQ -> tw .writeExprs_assigndivexpr(id.cast<DbAssigndivexpr >(), type.javaResult.id , exprParent.parent, exprParent.idx)
2495
- IrStatementOrigin .PERCEQ -> tw .writeExprs_assignremexpr(id.cast<DbAssignremexpr >(), type.javaResult.id , exprParent.parent, exprParent.idx)
2496
- else -> return false
2491
+ IrStatementOrigin .PLUSEQ -> return { tw : TrapWriter , id : Label < out DbAssignexpr >, type : Label < out DbType >, exprParent : Label < out DbExprparent >, index : Int -> tw .writeExprs_assignaddexpr(id.cast<DbAssignaddexpr >(), type, exprParent, index) }
2492
+ IrStatementOrigin .MINUSEQ -> return { tw : TrapWriter , id : Label < out DbAssignexpr >, type : Label < out DbType >, exprParent : Label < out DbExprparent >, index : Int -> tw .writeExprs_assignsubexpr(id.cast<DbAssignsubexpr >(), type, exprParent, index) }
2493
+ IrStatementOrigin .MULTEQ -> return { tw : TrapWriter , id : Label < out DbAssignexpr >, type : Label < out DbType >, exprParent : Label < out DbExprparent >, index : Int -> tw .writeExprs_assignmulexpr(id.cast<DbAssignmulexpr >(), type, exprParent, index) }
2494
+ IrStatementOrigin .DIVEQ -> return { tw : TrapWriter , id : Label < out DbAssignexpr >, type : Label < out DbType >, exprParent : Label < out DbExprparent >, index : Int -> tw .writeExprs_assigndivexpr(id.cast<DbAssigndivexpr >(), type, exprParent, index) }
2495
+ IrStatementOrigin .PERCEQ -> return { tw : TrapWriter , id : Label < out DbAssignexpr >, type : Label < out DbType >, exprParent : Label < out DbExprparent >, index : Int -> tw .writeExprs_assignremexpr(id.cast<DbAssignremexpr >(), type, exprParent, index) }
2496
+ else -> return null
2497
2497
}
2498
- return true
2499
2498
}
2500
2499
2500
+ /* *
2501
+ * This tried to extract a block as an array update.
2502
+ * It returns true if it succeeds, and false otherwise.
2503
+ */
2501
2504
private fun tryExtractArrayUpdate (e : IrContainerExpression , callable : Label <out DbCallable >, parent : StmtExprParent ): Boolean {
2502
2505
/*
2503
2506
* We're expecting the pattern
@@ -2517,6 +2520,11 @@ open class KotlinFileExtractor(
2517
2520
indexVarDecl.initializer?.let { indexVarInitializer ->
2518
2521
(e.statements[2 ] as ? IrCall )?.let { arraySetCall ->
2519
2522
if (isFunction(arraySetCall.symbol.owner, " kotlin" , " (some array type)" , { isArrayType(it) }, " set" )) {
2523
+ val updateRhs = arraySetCall.getValueArgument(1 )
2524
+ if (updateRhs == null ) {
2525
+ logger.errorElement(" Update RHS not found" , e)
2526
+ return false
2527
+ }
2520
2528
getUpdateInPlaceRHS(
2521
2529
e.origin, // Using e.origin not arraySetCall.origin here distinguishes a compiler-generated block from a user manually code that looks the same.
2522
2530
{ oldValue ->
@@ -2526,8 +2534,19 @@ open class KotlinFileExtractor(
2526
2534
receiverVal -> receiverVal.symbol.owner == arrayVarDecl.symbol.owner
2527
2535
} ? : false
2528
2536
},
2529
- arraySetCall.getValueArgument( 1 ) !!
2537
+ updateRhs
2530
2538
)?.let { updateRhs ->
2539
+ val origin = e.origin
2540
+ if (origin == null ) {
2541
+ logger.errorElement(" No origin found" , e)
2542
+ return false
2543
+ }
2544
+ val writeUpdateInPlaceExprFun = writeUpdateInPlaceExpr(origin)
2545
+ if (writeUpdateInPlaceExprFun == null ) {
2546
+ logger.errorElement(" Unexpected origin" , e)
2547
+ return false
2548
+ }
2549
+
2531
2550
// Create an assignment skeleton _ op= _
2532
2551
val exprParent = parent.expr(e, callable)
2533
2552
val assignId = tw.getFreshIdLabel<DbAssignexpr >()
@@ -2538,10 +2557,7 @@ open class KotlinFileExtractor(
2538
2557
tw.writeCallableEnclosingExpr(assignId, callable)
2539
2558
tw.writeStatementEnclosingExpr(assignId, exprParent.enclosingStmt)
2540
2559
2541
- if (! writeUpdateInPlaceExpr(e.origin!! , tw, assignId, type, exprParent)) {
2542
- logger.errorElement(" Unexpected origin" , e)
2543
- return false
2544
- }
2560
+ writeUpdateInPlaceExprFun(tw, assignId, type.javaResult.id, exprParent.parent, exprParent.idx)
2545
2561
2546
2562
// Extract e1[e2]
2547
2563
val lhsId = tw.getFreshIdLabel<DbArrayaccess >()
@@ -2893,9 +2909,17 @@ open class KotlinFileExtractor(
2893
2909
// Check for a desugared in-place update operator, such as "v += e":
2894
2910
val inPlaceUpdateRhs = getUpdateInPlaceRHS(e.origin, { it is IrGetValue && it.symbol.owner == e.symbol.owner }, rhsValue)
2895
2911
if (inPlaceUpdateRhs != null ) {
2896
- if (! writeUpdateInPlaceExpr(e.origin!! , tw, id, type, exprParent)) {
2897
- logger.errorElement(" Unexpected origin" , e)
2912
+ val origin = e.origin
2913
+ if (origin == null ) {
2914
+ logger.errorElement(" No origin for set-value" , e)
2898
2915
return
2916
+ } else {
2917
+ val writeUpdateInPlaceExprFun = writeUpdateInPlaceExpr(origin)
2918
+ if (writeUpdateInPlaceExprFun == null ) {
2919
+ logger.errorElement(" Unexpected origin for set-value" , e)
2920
+ return
2921
+ }
2922
+ writeUpdateInPlaceExprFun(tw, id, type.javaResult.id, exprParent.parent, exprParent.idx)
2899
2923
}
2900
2924
} else {
2901
2925
tw.writeExprs_assignexpr(id, type.javaResult.id, exprParent.parent, exprParent.idx)
0 commit comments