Skip to content

Commit b0ae128

Browse files
authored
Merge pull request #10160 from igfoo/igfoo/more-not-null-exprs
Kotlin: Remove the last not-null-expressions
2 parents bf6d9f8 + b485706 commit b0ae128

File tree

1 file changed

+39
-15
lines changed

1 file changed

+39
-15
lines changed

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

Lines changed: 39 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
@@ -2517,6 +2520,11 @@ open class KotlinFileExtractor(
25172520
indexVarDecl.initializer?.let { indexVarInitializer ->
25182521
(e.statements[2] as? IrCall)?.let { arraySetCall ->
25192522
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+
}
25202528
getUpdateInPlaceRHS(
25212529
e.origin, // Using e.origin not arraySetCall.origin here distinguishes a compiler-generated block from a user manually code that looks the same.
25222530
{ oldValue ->
@@ -2526,8 +2534,19 @@ open class KotlinFileExtractor(
25262534
receiverVal -> receiverVal.symbol.owner == arrayVarDecl.symbol.owner
25272535
} ?: false
25282536
},
2529-
arraySetCall.getValueArgument(1)!!
2537+
updateRhs
25302538
)?.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+
25312550
// Create an assignment skeleton _ op= _
25322551
val exprParent = parent.expr(e, callable)
25332552
val assignId = tw.getFreshIdLabel<DbAssignexpr>()
@@ -2538,10 +2557,7 @@ open class KotlinFileExtractor(
25382557
tw.writeCallableEnclosingExpr(assignId, callable)
25392558
tw.writeStatementEnclosingExpr(assignId, exprParent.enclosingStmt)
25402559

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)
25452561

25462562
// Extract e1[e2]
25472563
val lhsId = tw.getFreshIdLabel<DbArrayaccess>()
@@ -2893,9 +2909,17 @@ open class KotlinFileExtractor(
28932909
// Check for a desugared in-place update operator, such as "v += e":
28942910
val inPlaceUpdateRhs = getUpdateInPlaceRHS(e.origin, { it is IrGetValue && it.symbol.owner == e.symbol.owner }, rhsValue)
28952911
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)
28982915
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)
28992923
}
29002924
} else {
29012925
tw.writeExprs_assignexpr(id, type.javaResult.id, exprParent.parent, exprParent.idx)

0 commit comments

Comments
 (0)