From 28a56346154f9d55a4331fc13e2cb6b556282a0c Mon Sep 17 00:00:00 2001 From: Tamas Vajk Date: Tue, 19 Nov 2024 13:55:57 +0100 Subject: [PATCH 1/2] KE2: Extract `if` expressions/statements --- .../src/main/kotlin/entities/Expression.kt | 52 +++++++++++++++++-- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/java/kotlin-extractor2/src/main/kotlin/entities/Expression.kt b/java/kotlin-extractor2/src/main/kotlin/entities/Expression.kt index c6317286d7a..fcd845e3440 100644 --- a/java/kotlin-extractor2/src/main/kotlin/entities/Expression.kt +++ b/java/kotlin-extractor2/src/main/kotlin/entities/Expression.kt @@ -802,6 +802,10 @@ private fun KotlinFileExtractor.extractExpression( extractBlock(e, e.statements, parent, callable) } + is KtIfExpression -> { + return extractIf(e, parent, callable) + } + is KtWhileExpression -> { extractLoopWithCondition(e, parent, callable) } @@ -1410,6 +1414,7 @@ private fun KotlinFileExtractor.extractBlock( statements.forEachIndexed { i, s -> extractStatement(s, callable, id, i) } } +// TODO: Can this function be inlined? context(KaSession) private fun KotlinFileExtractor.extractStatement( s: KtExpression, @@ -1419,9 +1424,6 @@ private fun KotlinFileExtractor.extractStatement( ) { with("statement", s) { when (s) { - is KtStatementExpression -> { - extractExpressionStmt(s, callable, parent, idx) - } /* OLD: KE1 is IrVariable -> { @@ -1478,7 +1480,7 @@ private fun KotlinFileExtractor.extractStatement( */ else -> { - logger.errorElement("Unhandled statement: " + s.javaClass, s) + extractExpressionStmt(s, callable, parent, idx) } } } @@ -1733,6 +1735,48 @@ private fun KotlinFileExtractor.extractLoop( return id } +context(KaSession) +private fun KotlinFileExtractor.extractIf( + ifStmt: KtIfExpression, + stmtExprParent: StmtExprParent, + callable: Label +): Label? { + val expressionType = ifStmt.expressionType + + if (expressionType?.isNothingType == true) { + // We're extracting this `if` as a statement + val stmtParent = stmtExprParent.stmt(ifStmt, callable) + val id = tw.getFreshIdLabel() + val locId = tw.getLocation(ifStmt) + tw.writeStmts_ifstmt(id, stmtParent.parent, stmtParent.idx, callable) + tw.writeHasLocation(id, locId) + + extractExpressionExpr(ifStmt.condition!!, callable, id, 0, id) + extractExpressionStmt(ifStmt.then!!, callable, id, 1) + val elseBranch = ifStmt.`else` + if (elseBranch != null) { + extractExpressionStmt(elseBranch, callable, id, 2) + } + + return null + } + + // We're extracting this `if` as a conditional expression + val exprParent = stmtExprParent.expr(ifStmt, callable) + val id = tw.getFreshIdLabel() + val type = useType(expressionType) + tw.writeExprs_conditionalexpr(id, type.javaResult.id, exprParent.parent, exprParent.idx) + tw.writeExprsKotlinType(id, type.kotlinResult.id) + + extractExprContext(id, tw.getLocation(ifStmt), callable, exprParent.enclosingStmt) + + extractExpressionExpr(ifStmt.condition!!, callable, id, 0, exprParent.enclosingStmt) + extractExpressionExpr(ifStmt.then!!, callable, id, 1, exprParent.enclosingStmt) + extractExpressionExpr(ifStmt.`else`!!, callable, id, 2, exprParent.enclosingStmt) + + return id +} + context(KaSession) private fun KotlinFileExtractor.extractLoopWithCondition( loop: KtWhileExpressionBase, From 39aefb8d176b4a2d9e224f86b91068fe56189739 Mon Sep 17 00:00:00 2001 From: Tamas Vajk Date: Tue, 19 Nov 2024 18:06:35 +0100 Subject: [PATCH 2/2] Fix code review finding --- .../src/main/kotlin/entities/Expression.kt | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/java/kotlin-extractor2/src/main/kotlin/entities/Expression.kt b/java/kotlin-extractor2/src/main/kotlin/entities/Expression.kt index fcd845e3440..5d353f86dd5 100644 --- a/java/kotlin-extractor2/src/main/kotlin/entities/Expression.kt +++ b/java/kotlin-extractor2/src/main/kotlin/entities/Expression.kt @@ -1741,9 +1741,7 @@ private fun KotlinFileExtractor.extractIf( stmtExprParent: StmtExprParent, callable: Label ): Label? { - val expressionType = ifStmt.expressionType - - if (expressionType?.isNothingType == true) { + if (!ifStmt.isUsedAsExpression) { // We're extracting this `if` as a statement val stmtParent = stmtExprParent.stmt(ifStmt, callable) val id = tw.getFreshIdLabel() @@ -1764,7 +1762,7 @@ private fun KotlinFileExtractor.extractIf( // We're extracting this `if` as a conditional expression val exprParent = stmtExprParent.expr(ifStmt, callable) val id = tw.getFreshIdLabel() - val type = useType(expressionType) + val type = useType(ifStmt.expressionType) tw.writeExprs_conditionalexpr(id, type.javaResult.id, exprParent.parent, exprParent.idx) tw.writeExprsKotlinType(id, type.kotlinResult.id)