Skip to content

Commit e4f66b8

Browse files
committed
Kotlin: Refactor writeUpdateInPlaceExpr
In tryExtractArrayUpdate we need to know if writeUpdateInPlaceExpr will succeed before we start writing any TRAP.
1 parent 31e1230 commit e4f66b8

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2486,18 +2486,21 @@ open class KotlinFileExtractor(
24862486
}
24872487
}
24882488

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)? {
24902490
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
24972497
}
2498-
return true
24992498
}
25002499

2500+
/**
2501+
* This tried to extract a block as an array update.
2502+
* It returns true if it succeeds, and false otherwise.
2503+
*/
25012504
private fun tryExtractArrayUpdate(e: IrContainerExpression, callable: Label<out DbCallable>, parent: StmtExprParent): Boolean {
25022505
/*
25032506
* We're expecting the pattern
@@ -2528,6 +2531,12 @@ open class KotlinFileExtractor(
25282531
},
25292532
arraySetCall.getValueArgument(1)!!
25302533
)?.let { updateRhs ->
2534+
val writeUpdateInPlaceExprFun = writeUpdateInPlaceExpr(e.origin!!)
2535+
if (writeUpdateInPlaceExprFun == null) {
2536+
logger.errorElement("Unexpected origin", e)
2537+
return false
2538+
}
2539+
25312540
// Create an assignment skeleton _ op= _
25322541
val exprParent = parent.expr(e, callable)
25332542
val assignId = tw.getFreshIdLabel<DbAssignexpr>()
@@ -2538,10 +2547,7 @@ open class KotlinFileExtractor(
25382547
tw.writeCallableEnclosingExpr(assignId, callable)
25392548
tw.writeStatementEnclosingExpr(assignId, exprParent.enclosingStmt)
25402549

2541-
if (!writeUpdateInPlaceExpr(e.origin!!, tw, assignId, type, exprParent)) {
2542-
logger.errorElement("Unexpected origin", e)
2543-
return false
2544-
}
2550+
writeUpdateInPlaceExprFun(tw, assignId, type.javaResult.id, exprParent.parent, exprParent.idx)
25452551

25462552
// Extract e1[e2]
25472553
val lhsId = tw.getFreshIdLabel<DbArrayaccess>()
@@ -2897,9 +2903,13 @@ open class KotlinFileExtractor(
28972903
if (origin == null) {
28982904
logger.errorElement("No origin for set-value", e)
28992905
return
2900-
} else if (!writeUpdateInPlaceExpr(origin, tw, id, type, exprParent)) {
2901-
logger.errorElement("Unexpected origin for set-value", e)
2902-
return
2906+
} else {
2907+
val writeUpdateInPlaceExprFun = writeUpdateInPlaceExpr(origin)
2908+
if (writeUpdateInPlaceExprFun == null) {
2909+
logger.errorElement("Unexpected origin for set-value", e)
2910+
return
2911+
}
2912+
writeUpdateInPlaceExprFun(tw, id, type.javaResult.id, exprParent.parent, exprParent.idx)
29032913
}
29042914
} else {
29052915
tw.writeExprs_assignexpr(id, type.javaResult.id, exprParent.parent, exprParent.idx)

0 commit comments

Comments
 (0)