From 145e2093f3161b4d91b58da7a7b0283b81b8f16a Mon Sep 17 00:00:00 2001 From: Tamas Vajk Date: Wed, 2 Nov 2022 11:27:29 +0100 Subject: [PATCH] Kotlin: Remove code duplication --- .../src/main/kotlin/KotlinFileExtractor.kt | 275 +++++------------- 1 file changed, 71 insertions(+), 204 deletions(-) diff --git a/java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt b/java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt index 5e0d96c9cb0..5de6be4542c 100644 --- a/java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt +++ b/java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt @@ -1505,9 +1505,7 @@ open class KotlinFileExtractor( tw.writeHasLocation(varId, locId) tw.writeExprs_localvariabledeclexpr(exprId, type.javaResult.id, parent, idx) tw.writeExprsKotlinType(exprId, type.kotlinResult.id) - tw.writeHasLocation(exprId, locId) - tw.writeCallableEnclosingExpr(exprId, callable) - tw.writeStatementEnclosingExpr(exprId, enclosingStmt) + extractExprContext(exprId, locId, callable, enclosingStmt) val i = v.initializer if (i != null && extractInitializer) { extractExpressionExpr(i, callable, exprId, 0, enclosingStmt) @@ -1630,9 +1628,7 @@ open class KotlinFileExtractor( private fun unaryOp(id: Label, c: IrCall, callable: Label, enclosingStmt: Label) { val locId = tw.getLocation(c) - tw.writeHasLocation(id, locId) - tw.writeCallableEnclosingExpr(id, callable) - tw.writeStatementEnclosingExpr(id, enclosingStmt) + extractExprContext(id, locId, callable, enclosingStmt) val dr = c.dispatchReceiver if (dr != null) { @@ -1653,9 +1649,7 @@ open class KotlinFileExtractor( private fun binOp(id: Label, c: IrCall, callable: Label, enclosingStmt: Label) { val locId = tw.getLocation(c) - tw.writeHasLocation(id, locId) - tw.writeCallableEnclosingExpr(id, callable) - tw.writeStatementEnclosingExpr(id, enclosingStmt) + extractExprContext(id, locId, callable, enclosingStmt) val dr = c.dispatchReceiver if (dr != null) { @@ -1767,9 +1761,7 @@ open class KotlinFileExtractor( tw.writeExprs_methodaccess(id, type.javaResult.id, callsiteParent, childIdx) tw.writeExprsKotlinType(id, type.kotlinResult.id) - tw.writeHasLocation(id, locId) - tw.writeCallableEnclosingExpr(id, enclosingCallable) - tw.writeStatementEnclosingExpr(id, enclosingStmt) + extractExprContext(id, locId, enclosingCallable, enclosingStmt) // The caller should have warned about this before, so we don't repeat the warning here. if (methodLabel != null) @@ -2370,19 +2362,14 @@ open class KotlinFileExtractor( tw.writeExprs_errorexpr(id, type.javaResult.id, parent, idx) tw.writeExprsKotlinType(id, type.kotlinResult.id) - tw.writeHasLocation(id, tw.getLocation(c)) - tw.writeCallableEnclosingExpr(id, callable) - tw.writeStatementEnclosingExpr(id, enclosingStmt) + extractExprContext(id, tw.getLocation(c), callable, enclosingStmt) } else { logger.errorElement("Unexpected enum type rep ${enumType.javaClass}", c) } } fun binopReceiver(id: Label, receiver: IrExpression?, receiverDescription: String) { - val locId = tw.getLocation(c) - tw.writeHasLocation(id, locId) - tw.writeCallableEnclosingExpr(id, callable) - tw.writeStatementEnclosingExpr(id, enclosingStmt) + extractExprContext(id, tw.getLocation(c), callable, enclosingStmt) if(receiver == null) { logger.errorElement("$receiverDescription not found", c) @@ -2405,10 +2392,7 @@ open class KotlinFileExtractor( } fun unaryopReceiver(id: Label, receiver: IrExpression?, receiverDescription: String) { - val locId = tw.getLocation(c) - tw.writeHasLocation(id, locId) - tw.writeCallableEnclosingExpr(id, callable) - tw.writeStatementEnclosingExpr(id, enclosingStmt) + extractExprContext(id, tw.getLocation(c), callable, enclosingStmt) if(receiver == null) { logger.errorElement("$receiverDescription not found", c) @@ -2702,8 +2686,7 @@ open class KotlinFileExtractor( tw.writeExprs_arraycreationexpr(id, type.javaResult.id, parent, idx) tw.writeExprsKotlinType(id, type.kotlinResult.id) val locId = tw.getLocation(c) - tw.writeHasLocation(id, locId) - tw.writeCallableEnclosingExpr(id, callable) + extractExprContext(id, locId, callable, enclosingStmt) if (c.typeArgumentsCount == 1) { val typeArgument = c.getTypeArgument(0) @@ -2841,9 +2824,7 @@ open class KotlinFileExtractor( val arrayType = useType(array.type) tw.writeExprs_arrayaccess(arrayAccessId, arrayType.javaResult.id, assignId, 0) tw.writeExprsKotlinType(arrayAccessId, arrayType.kotlinResult.id) - tw.writeHasLocation(arrayAccessId, locId) - tw.writeCallableEnclosingExpr(arrayAccessId, callable) - tw.writeStatementEnclosingExpr(arrayAccessId, enclosingStmt) + extractExprContext(arrayAccessId, locId, callable, enclosingStmt) extractExpressionExpr(array, callable, arrayAccessId, 0, enclosingStmt) extractExpressionExpr(arrayIdx, callable, arrayAccessId, 1, enclosingStmt) @@ -2882,9 +2863,7 @@ open class KotlinFileExtractor( val type = useType(c.type) tw.writeExprs_unsafecoerceexpr(id, type.javaResult.id, parent, idx) tw.writeExprsKotlinType(id, type.kotlinResult.id) - tw.writeHasLocation(id, locId) - tw.writeCallableEnclosingExpr(id, callable) - tw.writeStatementEnclosingExpr(id, enclosingStmt) + extractExprContext(id, locId, callable, enclosingStmt) extractTypeAccessRecursive(typeArg, locId, id, 0, callable, enclosingStmt) extractExpressionExpr(valueArg, callable, id, 1, enclosingStmt) } @@ -2974,8 +2953,7 @@ open class KotlinFileExtractor( tw.writeExprs_arraycreationexpr(id, type.javaResult.id, parent, idx) tw.writeExprsKotlinType(id, type.kotlinResult.id) val locId = tw.getLocation(locElement) - tw.writeHasLocation(id, locId) - tw.writeCallableEnclosingExpr(id, enclosingCallable) + extractExprContext(id, locId, enclosingCallable, enclosingStmt) if (elementType != null) { val typeContext = if (allowPrimitiveElementType) TypeContext.OTHER else TypeContext.GENERIC_ARGUMENT @@ -2986,9 +2964,7 @@ open class KotlinFileExtractor( val initId = tw.getFreshIdLabel() tw.writeExprs_arrayinit(initId, type.javaResult.id, id, -2) tw.writeExprsKotlinType(initId, type.kotlinResult.id) - tw.writeHasLocation(initId, locId) - tw.writeCallableEnclosingExpr(initId, enclosingCallable) - tw.writeStatementEnclosingExpr(initId, enclosingStmt) + extractExprContext(initId, locId, enclosingCallable, enclosingStmt) elementList.elements.forEachIndexed { i, arg -> extractVarargElement(arg, enclosingCallable, initId, i, enclosingStmt) } extractConstantInteger(elementList.elements.size, locId, id, 0, enclosingCallable, enclosingStmt) @@ -2999,7 +2975,7 @@ open class KotlinFileExtractor( private fun extractNewExpr( methodId: Label, constructedType: TypeResults, - locId: Label, + locId: Label, parent: Label, idx: Int, callable: Label, @@ -3008,9 +2984,7 @@ open class KotlinFileExtractor( val id = tw.getFreshIdLabel() tw.writeExprs_newexpr(id, constructedType.javaResult.id, parent, idx) tw.writeExprsKotlinType(id, constructedType.kotlinResult.id) - tw.writeHasLocation(id, locId) - tw.writeCallableEnclosingExpr(id, callable) - tw.writeStatementEnclosingExpr(id, enclosingStmt) + extractExprContext(id, locId, callable, enclosingStmt) tw.writeCallableBinding(id, methodId) return id } @@ -3019,7 +2993,7 @@ open class KotlinFileExtractor( calledConstructor: IrFunction, constructorTypeArgs: List?, constructedType: TypeResults, - locId: Label, + locId: Label, parent: Label, idx: Int, callable: Label, @@ -3136,9 +3110,7 @@ open class KotlinFileExtractor( val locId = tw.getLocation(e) tw.writeExprs_stmtexpr(id, type.javaResult.id, parent, idx) tw.writeExprsKotlinType(id, type.kotlinResult.id) - tw.writeHasLocation(id, locId) - tw.writeCallableEnclosingExpr(id, callable) - tw.writeStatementEnclosingExpr(id, enclosingStmt) + extractExprContext(id, locId, callable, enclosingStmt) return StmtParent(id, 0) } override fun expr(e: IrExpression, callable: Label): ExprParent { @@ -3321,9 +3293,7 @@ open class KotlinFileExtractor( val type = useType(arrayVarInitializer.type) val locId = tw.getLocation(e) tw.writeExprsKotlinType(assignId, type.kotlinResult.id) - tw.writeHasLocation(assignId, locId) - tw.writeCallableEnclosingExpr(assignId, callable) - tw.writeStatementEnclosingExpr(assignId, exprParent.enclosingStmt) + extractExprContext(assignId, locId, callable, exprParent.enclosingStmt) writeUpdateInPlaceExprFun(tw, assignId, type.javaResult.id, exprParent.parent, exprParent.idx) @@ -3332,9 +3302,7 @@ open class KotlinFileExtractor( val elementType = useType(updateRhs.type) tw.writeExprs_arrayaccess(lhsId, elementType.javaResult.id, assignId, 0) tw.writeExprsKotlinType(lhsId, elementType.kotlinResult.id) - tw.writeHasLocation(lhsId, locId) - tw.writeCallableEnclosingExpr(lhsId, callable) - tw.writeStatementEnclosingExpr(lhsId, exprParent.enclosingStmt) + extractExprContext(lhsId, locId, callable, exprParent.enclosingStmt) extractExpressionExpr(arrayVarInitializer, callable, lhsId, 0, exprParent.enclosingStmt) extractExpressionExpr(indexVarInitializer, callable, lhsId, 1, exprParent.enclosingStmt) @@ -3389,7 +3357,7 @@ open class KotlinFileExtractor( extractExprContext(it, locId, callable, enclosingStmt) } - private fun extractConstantInteger(v: Int, locId: Label, parent: Label, idx: Int, callable: Label, enclosingStmt: Label) = + private fun extractConstantInteger(v: Number, locId: Label, parent: Label, idx: Int, callable: Label, enclosingStmt: Label) = tw.getFreshIdLabel().also { val type = useType(pluginContext.irBuiltIns.intType) tw.writeExprs_integerliteral(it, type.javaResult.id, parent, idx) @@ -3523,9 +3491,7 @@ open class KotlinFileExtractor( val methodId = tw.getLabelFor(methodLabel) tw.writeExprs_methodaccess(id, type.javaResult.id, exprParent.parent, exprParent.idx) tw.writeExprsKotlinType(id, type.kotlinResult.id) - tw.writeHasLocation(id, locId) - tw.writeCallableEnclosingExpr(id, callable) - tw.writeStatementEnclosingExpr(id, exprParent.enclosingStmt) + extractExprContext(id, locId, callable, exprParent.enclosingStmt) tw.writeCallableBinding(id, methodId) } else { @@ -3551,9 +3517,7 @@ open class KotlinFileExtractor( val locId = tw.getLocation(e) tw.writeExprs_stringtemplateexpr(id, type.javaResult.id, exprParent.parent, exprParent.idx) tw.writeExprsKotlinType(id, type.kotlinResult.id) - tw.writeHasLocation(id, locId) - tw.writeCallableEnclosingExpr(id, callable) - tw.writeStatementEnclosingExpr(id, exprParent.enclosingStmt) + extractExprContext(id, locId, callable, exprParent.enclosingStmt) e.arguments.forEachIndexed { i, a -> extractExpressionExpr(a, callable, id, i, exprParent.enclosingStmt) } @@ -3562,24 +3526,14 @@ open class KotlinFileExtractor( val exprParent = parent.expr(e, callable) when(val v = e.value) { is Int, is Short, is Byte -> { - val id = tw.getFreshIdLabel() - val type = useType(e.type) - val locId = tw.getLocation(e) - tw.writeExprs_integerliteral(id, type.javaResult.id, exprParent.parent, exprParent.idx) - tw.writeExprsKotlinType(id, type.kotlinResult.id) - tw.writeHasLocation(id, locId) - tw.writeCallableEnclosingExpr(id, callable) - tw.writeStatementEnclosingExpr(id, exprParent.enclosingStmt) - tw.writeNamestrings(v.toString(), v.toString(), id) + extractConstantInteger(v as Number, tw.getLocation(e), exprParent.parent, exprParent.idx, callable, exprParent.enclosingStmt) } is Long -> { val id = tw.getFreshIdLabel() val type = useType(e.type) val locId = tw.getLocation(e) tw.writeExprs_longliteral(id, type.javaResult.id, exprParent.parent, exprParent.idx) tw.writeExprsKotlinType(id, type.kotlinResult.id) - tw.writeHasLocation(id, locId) - tw.writeCallableEnclosingExpr(id, callable) - tw.writeStatementEnclosingExpr(id, exprParent.enclosingStmt) + extractExprContext(id, locId, callable, exprParent.enclosingStmt) tw.writeNamestrings(v.toString(), v.toString(), id) } is Float -> { val id = tw.getFreshIdLabel() @@ -3587,9 +3541,7 @@ open class KotlinFileExtractor( val locId = tw.getLocation(e) tw.writeExprs_floatingpointliteral(id, type.javaResult.id, exprParent.parent, exprParent.idx) tw.writeExprsKotlinType(id, type.kotlinResult.id) - tw.writeHasLocation(id, locId) - tw.writeCallableEnclosingExpr(id, callable) - tw.writeStatementEnclosingExpr(id, exprParent.enclosingStmt) + extractExprContext(id, locId, callable, exprParent.enclosingStmt) tw.writeNamestrings(v.toString(), v.toString(), id) } is Double -> { val id = tw.getFreshIdLabel() @@ -3597,9 +3549,7 @@ open class KotlinFileExtractor( val locId = tw.getLocation(e) tw.writeExprs_doubleliteral(id, type.javaResult.id, exprParent.parent, exprParent.idx) tw.writeExprsKotlinType(id, type.kotlinResult.id) - tw.writeHasLocation(id, locId) - tw.writeCallableEnclosingExpr(id, callable) - tw.writeStatementEnclosingExpr(id, exprParent.enclosingStmt) + extractExprContext(id, locId, callable, exprParent.enclosingStmt) tw.writeNamestrings(v.toString(), v.toString(), id) } is Boolean -> { val id = tw.getFreshIdLabel() @@ -3607,9 +3557,7 @@ open class KotlinFileExtractor( val locId = tw.getLocation(e) tw.writeExprs_booleanliteral(id, type.javaResult.id, exprParent.parent, exprParent.idx) tw.writeExprsKotlinType(id, type.kotlinResult.id) - tw.writeHasLocation(id, locId) - tw.writeCallableEnclosingExpr(id, callable) - tw.writeStatementEnclosingExpr(id, exprParent.enclosingStmt) + extractExprContext(id, locId, callable, exprParent.enclosingStmt) tw.writeNamestrings(v.toString(), v.toString(), id) } is Char -> { val id = tw.getFreshIdLabel() @@ -3617,9 +3565,7 @@ open class KotlinFileExtractor( val locId = tw.getLocation(e) tw.writeExprs_characterliteral(id, type.javaResult.id, exprParent.parent, exprParent.idx) tw.writeExprsKotlinType(id, type.kotlinResult.id) - tw.writeHasLocation(id, locId) - tw.writeCallableEnclosingExpr(id, callable) - tw.writeStatementEnclosingExpr(id, exprParent.enclosingStmt) + extractExprContext(id, locId, callable, exprParent.enclosingStmt) tw.writeNamestrings(v.toString(), v.toString(), id) } is String -> { val id = tw.getFreshIdLabel() @@ -3627,9 +3573,7 @@ open class KotlinFileExtractor( val locId = tw.getLocation(e) tw.writeExprs_stringliteral(id, type.javaResult.id, exprParent.parent, exprParent.idx) tw.writeExprsKotlinType(id, type.kotlinResult.id) - tw.writeHasLocation(id, locId) - tw.writeCallableEnclosingExpr(id, callable) - tw.writeStatementEnclosingExpr(id, exprParent.enclosingStmt) + extractExprContext(id, locId, callable, exprParent.enclosingStmt) tw.writeNamestrings(v.toString(), v.toString(), id) } null -> { @@ -3638,9 +3582,7 @@ open class KotlinFileExtractor( val locId = tw.getLocation(e) tw.writeExprs_nullliteral(id, type.javaResult.id, exprParent.parent, exprParent.idx) tw.writeExprsKotlinType(id, type.kotlinResult.id) - tw.writeHasLocation(id, locId) - tw.writeCallableEnclosingExpr(id, callable) - tw.writeStatementEnclosingExpr(id, exprParent.enclosingStmt) + extractExprContext(id, locId, callable, exprParent.enclosingStmt) } else -> { logger.errorElement("Unrecognised IrConst: " + v.javaClass, e) @@ -3679,9 +3621,7 @@ open class KotlinFileExtractor( val locId = tw.getLocation(e) tw.writeExprs_varaccess(id, type.javaResult.id, exprParent.parent, exprParent.idx) tw.writeExprsKotlinType(id, type.kotlinResult.id) - tw.writeHasLocation(id, locId) - tw.writeCallableEnclosingExpr(id, callable) - tw.writeStatementEnclosingExpr(id, exprParent.enclosingStmt) + extractExprContext(id, locId, callable, exprParent.enclosingStmt) val owner = getBoundSymbolOwner(e.symbol, e) ?: return @@ -3704,15 +3644,11 @@ open class KotlinFileExtractor( // set op plus its RHS, while the varAccess takes its location from `e`. val locId = tw.getLocation(e.startOffset, rhsValue.endOffset) tw.writeExprsKotlinType(id, type.kotlinResult.id) - tw.writeHasLocation(id, locId) - tw.writeCallableEnclosingExpr(id, callable) - tw.writeStatementEnclosingExpr(id, exprParent.enclosingStmt) + extractExprContext(id, locId, callable, exprParent.enclosingStmt) val lhsId = tw.getFreshIdLabel() val lhsLocId = tw.getLocation(e) - tw.writeHasLocation(lhsId, lhsLocId) - tw.writeCallableEnclosingExpr(lhsId, callable) - tw.writeStatementEnclosingExpr(lhsId, exprParent.enclosingStmt) + extractExprContext(lhsId, lhsLocId, callable, exprParent.enclosingStmt) when (e) { is IrSetValue -> { @@ -3793,9 +3729,7 @@ open class KotlinFileExtractor( val locId = tw.getLocation(e) tw.writeExprsKotlinType(id, type.kotlinResult.id) - tw.writeHasLocation(id, locId) - tw.writeCallableEnclosingExpr(id, callable) - tw.writeStatementEnclosingExpr(id, exprParent.enclosingStmt) + extractExprContext(id, locId, callable, exprParent.enclosingStmt) extractExpressionExpr(e.branches[0].condition, callable, id, 0, exprParent.enclosingStmt) @@ -3811,9 +3745,7 @@ open class KotlinFileExtractor( val locId = tw.getLocation(e) tw.writeExprs_whenexpr(id, type.javaResult.id, exprParent.parent, exprParent.idx) tw.writeExprsKotlinType(id, type.kotlinResult.id) - tw.writeHasLocation(id, locId) - tw.writeCallableEnclosingExpr(id, callable) - tw.writeStatementEnclosingExpr(id, exprParent.enclosingStmt) + extractExprContext(id, locId, callable, exprParent.enclosingStmt) if(e.origin == IrStatementOrigin.IF) { tw.writeWhen_if(id) } @@ -3836,9 +3768,7 @@ open class KotlinFileExtractor( val type = useType(e.type) tw.writeExprs_getclassexpr(id, type.javaResult.id, exprParent.parent, exprParent.idx) tw.writeExprsKotlinType(id, type.kotlinResult.id) - tw.writeHasLocation(id, locId) - tw.writeCallableEnclosingExpr(id, callable) - tw.writeStatementEnclosingExpr(id, exprParent.enclosingStmt) + extractExprContext(id, locId, callable, exprParent.enclosingStmt) extractExpressionExpr(e.argument, callable, id, 0, exprParent.enclosingStmt) } is IrTypeOperatorCall -> { @@ -3868,9 +3798,7 @@ open class KotlinFileExtractor( val locId = tw.getLocation(e) tw.writeExprs_varaccess(id, type.javaResult.id, exprParent.parent, exprParent.idx) tw.writeExprsKotlinType(id, type.kotlinResult.id) - tw.writeHasLocation(id, locId) - tw.writeCallableEnclosingExpr(id, callable) - tw.writeStatementEnclosingExpr(id, exprParent.enclosingStmt) + extractExprContext(id, locId, callable, exprParent.enclosingStmt) tw.writeVariableBinding(id, instance.id) } @@ -3923,9 +3851,7 @@ open class KotlinFileExtractor( val idLambdaExpr = tw.getFreshIdLabel() tw.writeExprs_lambdaexpr(idLambdaExpr, ids.type.javaResult.id, exprParent.parent, exprParent.idx) tw.writeExprsKotlinType(idLambdaExpr, ids.type.kotlinResult.id) - tw.writeHasLocation(idLambdaExpr, locId) - tw.writeCallableEnclosingExpr(idLambdaExpr, callable) - tw.writeStatementEnclosingExpr(idLambdaExpr, exprParent.enclosingStmt) + extractExprContext(idLambdaExpr, locId, callable, exprParent.enclosingStmt) tw.writeCallableBinding(idLambdaExpr, ids.constructor) // todo: fix hard coded block body of lambda @@ -3951,9 +3877,7 @@ open class KotlinFileExtractor( val type = useType(e.type) tw.writeExprs_typeliteral(id, type.javaResult.id, exprParent.parent, exprParent.idx) tw.writeExprsKotlinType(id, type.kotlinResult.id) - tw.writeHasLocation(id, locId) - tw.writeCallableEnclosingExpr(id, callable) - tw.writeStatementEnclosingExpr(id, exprParent.enclosingStmt) + extractExprContext(id, locId, callable, exprParent.enclosingStmt) extractTypeAccessRecursive(e.classType, locId, id, 0, callable, exprParent.enclosingStmt) } @@ -4006,27 +3930,23 @@ open class KotlinFileExtractor( return null } - private fun extractSuperAccess(irType: IrType, callable: Label, parent: Label, idx: Int, enclosingStmt: Label, locId: Label) = + private fun extractSuperAccess(irType: IrType, callable: Label, parent: Label, idx: Int, enclosingStmt: Label, locId: Label) = tw.getFreshIdLabel().also { val type = useType(irType) tw.writeExprs_superaccess(it, type.javaResult.id, parent, idx) tw.writeExprsKotlinType(it, type.kotlinResult.id) - tw.writeHasLocation(it, locId) - tw.writeCallableEnclosingExpr(it, callable) - tw.writeStatementEnclosingExpr(it, enclosingStmt) + extractExprContext(it, locId, callable, enclosingStmt) extractTypeAccessRecursive(irType, locId, it, 0) } - private fun extractThisAccess(type: TypeResults, callable: Label, parent: Label, idx: Int, enclosingStmt: Label, locId: Label) = + private fun extractThisAccess(type: TypeResults, callable: Label, parent: Label, idx: Int, enclosingStmt: Label, locId: Label) = tw.getFreshIdLabel().also { tw.writeExprs_thisaccess(it, type.javaResult.id, parent, idx) tw.writeExprsKotlinType(it, type.kotlinResult.id) - tw.writeHasLocation(it, locId) - tw.writeCallableEnclosingExpr(it, callable) - tw.writeStatementEnclosingExpr(it, enclosingStmt) + extractExprContext(it, locId, callable, enclosingStmt) } - private fun extractThisAccess(irType: IrType, callable: Label, parent: Label, idx: Int, enclosingStmt: Label, locId: Label) = + private fun extractThisAccess(irType: IrType, callable: Label, parent: Label, idx: Int, enclosingStmt: Label, locId: Label) = extractThisAccess(useType(irType), callable, parent, idx, enclosingStmt, locId) private fun extractThisAccess(e: IrGetValue, thisParamParent: IrDeclarationParent, exprParent: ExprParent, callable: Label) { @@ -4087,20 +4007,18 @@ open class KotlinFileExtractor( } } - private fun extractVariableAccess(variable: Label?, type: TypeResults, locId: Label, parent: Label, idx: Int, callable: Label, enclosingStmt: Label) = + private fun extractVariableAccess(variable: Label?, type: TypeResults, locId: Label, parent: Label, idx: Int, callable: Label, enclosingStmt: Label) = tw.getFreshIdLabel().also { tw.writeExprs_varaccess(it, type.javaResult.id, parent, idx) tw.writeExprsKotlinType(it, type.kotlinResult.id) - tw.writeHasLocation(it, locId) - tw.writeCallableEnclosingExpr(it, callable) - tw.writeStatementEnclosingExpr(it, enclosingStmt) + extractExprContext(it, locId, callable, enclosingStmt) if (variable != null) { tw.writeVariableBinding(it, variable) } } - private fun extractVariableAccess(variable: Label?, irType: IrType, locId: Label, parent: Label, idx: Int, callable: Label, enclosingStmt: Label) = + private fun extractVariableAccess(variable: Label?, irType: IrType, locId: Label, parent: Label, idx: Int, callable: Label, enclosingStmt: Label) = extractVariableAccess(variable, useType(irType), locId, parent, idx, callable, enclosingStmt) private fun extractLoop( @@ -4168,12 +4086,6 @@ open class KotlinFileExtractor( private open inner class GeneratedClassHelper(protected val locId: Label, protected val ids: GeneratedClassLabels) { protected val classId = ids.type.javaResult.id.cast() - fun writeExpressionMetadataToTrapFile(id: Label, callable: Label, stmt: Label) { - tw.writeHasLocation(id, locId) - tw.writeCallableEnclosingExpr(id, callable) - tw.writeStatementEnclosingExpr(id, stmt) - } - /** * Extract a parameter to field assignment, such as `this.field = paramName` below: * ``` @@ -4246,7 +4158,7 @@ open class KotlinFileExtractor( tw.writeExprs_varaccess(pId, pType.javaResult.id, parent, idx) tw.writeExprsKotlinType(pId, pType.kotlinResult.id) tw.writeVariableBinding(pId, variable) - writeExpressionMetadataToTrapFile(pId, callable, stmt) + extractExprContext(pId, locId, callable, stmt) return pId } @@ -4298,7 +4210,7 @@ open class KotlinFileExtractor( tw.writeExprs_varaccess(accessId, fieldType.javaResult.id, parent, 0) tw.writeExprsKotlinType(accessId, fieldType.kotlinResult.id) - writeExpressionMetadataToTrapFile(accessId, labels.methodId, stmt) + extractExprContext(accessId, locId, labels.methodId, stmt) val fieldId = useField(target.owner) tw.writeVariableBinding(accessId, fieldId) @@ -4357,7 +4269,7 @@ open class KotlinFileExtractor( callId } - writeExpressionMetadataToTrapFile(callId, labels.methodId, retId) + extractExprContext(callId, locId, labels.methodId, retId) val callableId = useFunction(target.owner.realOverrideTarget, classTypeArgsIncludingOuterClasses) tw.writeCallableBinding(callId.cast(), callableId) @@ -4394,7 +4306,6 @@ open class KotlinFileExtractor( // In case we're extracting a big arity function reference: addArgumentsToInvocationInInvokeNBody( bigArityParameterTypes, labels, retId, callId, locId, - { exp -> writeExpressionMetadataToTrapFile(exp, labels.methodId, retId) }, extensionIdxOffset, useFirstArgAsDispatch, dispatchReceiverIdx) } else { val dispatchIdxOffset = if (useFirstArgAsDispatch) 1 else 0 @@ -4448,7 +4359,7 @@ open class KotlinFileExtractor( val callId = tw.getFreshIdLabel() tw.writeExprs_methodaccess(callId, callType.javaResult.id, retId, 0) tw.writeExprsKotlinType(callId, callType.kotlinResult.id) - this.writeExpressionMetadataToTrapFile(callId, invokeLabels.methodId, retId) + extractExprContext(callId, locId, invokeLabels.methodId, retId) tw.writeCallableBinding(callId, getId) @@ -4606,9 +4517,7 @@ open class KotlinFileExtractor( val exprParent = parent.expr(propertyReferenceExpr, callable) tw.writeExprs_propertyref(idPropertyRef, ids.type.javaResult.id, exprParent.parent, exprParent.idx) tw.writeExprsKotlinType(idPropertyRef, ids.type.kotlinResult.id) - tw.writeHasLocation(idPropertyRef, locId) - tw.writeCallableEnclosingExpr(idPropertyRef, callable) - tw.writeStatementEnclosingExpr(idPropertyRef, exprParent.enclosingStmt) + extractExprContext(idPropertyRef, locId, callable, exprParent.enclosingStmt) tw.writeCallableBinding(idPropertyRef, ids.constructor) extractTypeAccessRecursive(kPropertyType, locId, idPropertyRef, -3, callable, exprParent.enclosingStmt) @@ -4719,9 +4628,7 @@ open class KotlinFileExtractor( val idMemberRef = tw.getFreshIdLabel() tw.writeExprs_memberref(idMemberRef, ids.type.javaResult.id, exprParent.parent, exprParent.idx) tw.writeExprsKotlinType(idMemberRef, ids.type.kotlinResult.id) - tw.writeHasLocation(idMemberRef, locId) - tw.writeCallableEnclosingExpr(idMemberRef, callable) - tw.writeStatementEnclosingExpr(idMemberRef, exprParent.enclosingStmt) + extractExprContext(idMemberRef, locId, callable, exprParent.enclosingStmt) tw.writeCallableBinding(idMemberRef, ids.constructor) val targetCallableId = useFunction(target.owner.realOverrideTarget, classTypeArguments) @@ -4885,25 +4792,19 @@ open class KotlinFileExtractor( tw.writeStmts_returnstmt(retId, funLabels.blockId, 0, funLabels.methodId) tw.writeHasLocation(retId, locId) - fun extractCommonExpr(id: Label) { - tw.writeHasLocation(id, locId) - tw.writeCallableEnclosingExpr(id, funLabels.methodId) - tw.writeStatementEnclosingExpr(id, retId) - } - // Call to original `invoke`: val callId = tw.getFreshIdLabel() val callType = useType(lambda.returnType) tw.writeExprs_methodaccess(callId, callType.javaResult.id, retId, 0) tw.writeExprsKotlinType(callId, callType.kotlinResult.id) - extractCommonExpr(callId) + extractExprContext(callId, locId, funLabels.methodId, retId) val calledMethodId = useFunction(lambda) tw.writeCallableBinding(callId, calledMethodId) // this access extractThisAccess(ids.type, funLabels.methodId, callId, -1, retId, locId) - addArgumentsToInvocationInInvokeNBody(parameters.map { it.type }, funLabels, retId, callId, locId, ::extractCommonExpr) + addArgumentsToInvocationInInvokeNBody(parameters.map { it.type }, funLabels, retId, callId, locId) } /** @@ -4921,12 +4822,10 @@ open class KotlinFileExtractor( enclosingStmtId: Label, // label for the enclosing statement (return) exprParentId: Label, // label for the expression parent (call) locId: Label, // label for the location of all generated items - extractCommonExpr: (Label) -> Unit, // lambda used for extracting location, enclosing stmt and expr for all new expressions firstArgumentOffset: Int = 0, // 0 or 1, the index used for the first argument. 1 in case an extension parameter is already accessed at index 0 useFirstArgAsDispatch: Boolean = false, // true if the first argument should be used as the dispatch receiver dispatchReceiverIdx: Int = -1 // index of the dispatch receiver. -1 in case of functions, -2 in case of constructors ) { - val intType = useType(pluginContext.irBuiltIns.intType) val argsParamType = pluginContext.irBuiltIns.arrayClass.typeWith(pluginContext.irBuiltIns.anyNType) val argsType = useType(argsParamType) val anyNType = useType(pluginContext.irBuiltIns.anyNType) @@ -4948,7 +4847,7 @@ open class KotlinFileExtractor( val type = useType(pType) tw.writeExprs_castexpr(castId, type.javaResult.id, exprParentId, childIdx) tw.writeExprsKotlinType(castId, type.kotlinResult.id) - extractCommonExpr(castId) + extractExprContext(castId, locId, funLabels.methodId, enclosingStmtId) // type access `Ti` extractTypeAccessRecursive(pType, locId, castId, 0, funLabels.methodId, enclosingStmtId) @@ -4957,21 +4856,17 @@ open class KotlinFileExtractor( val arrayAccessId = tw.getFreshIdLabel() tw.writeExprs_arrayaccess(arrayAccessId, anyNType.javaResult.id, castId, 1) tw.writeExprsKotlinType(arrayAccessId, anyNType.kotlinResult.id) - extractCommonExpr(arrayAccessId) + extractExprContext(arrayAccessId, locId, funLabels.methodId, enclosingStmtId) // parameter access: `a0` val argsAccessId = tw.getFreshIdLabel() tw.writeExprs_varaccess(argsAccessId, argsType.javaResult.id, arrayAccessId, 0) tw.writeExprsKotlinType(argsAccessId, argsType.kotlinResult.id) - extractCommonExpr(argsAccessId) + extractExprContext(argsAccessId, locId, funLabels.methodId, enclosingStmtId) tw.writeVariableBinding(argsAccessId, funLabels.parameters.first().first) // index access: `i` - val indexId = tw.getFreshIdLabel() - tw.writeExprs_integerliteral(indexId, intType.javaResult.id, arrayAccessId, 1) - tw.writeExprsKotlinType(indexId, intType.kotlinResult.id) - extractCommonExpr(indexId) - tw.writeNamestrings(pIdx.toString(), pIdx.toString(), indexId) + extractConstantInteger(pIdx, locId, arrayAccessId, 1, funLabels.methodId, enclosingStmtId) } } @@ -5143,25 +5038,19 @@ open class KotlinFileExtractor( enclosingStmt: Label ) : Label { - fun extractCommonExpr(id: Label) { - tw.writeHasLocation(id, locId) - tw.writeCallableEnclosingExpr(id, enclosingCallable) - tw.writeStatementEnclosingExpr(id, enclosingStmt) - } - val arrayCreationId = tw.getFreshIdLabel() val arrayType = pluginContext.irBuiltIns.arrayClass.typeWith(pluginContext.irBuiltIns.anyNType) val at = useType(arrayType) tw.writeExprs_arraycreationexpr(arrayCreationId, at.javaResult.id, parent, 0) tw.writeExprsKotlinType(arrayCreationId, at.kotlinResult.id) - extractCommonExpr(arrayCreationId) + extractExprContext(arrayCreationId, locId, enclosingCallable, enclosingStmt) extractTypeAccessRecursive(pluginContext.irBuiltIns.anyNType, locId, arrayCreationId, -1, enclosingCallable, enclosingStmt) val initId = tw.getFreshIdLabel() tw.writeExprs_arrayinit(initId, at.javaResult.id, arrayCreationId, -2) tw.writeExprsKotlinType(initId, at.kotlinResult.id) - extractCommonExpr(initId) + extractExprContext(initId, locId, enclosingCallable, enclosingStmt) extractConstantInteger(arraySize, locId, arrayCreationId, 0, enclosingCallable, enclosingStmt) @@ -5177,9 +5066,7 @@ open class KotlinFileExtractor( val type = useType(e.type) tw.writeExprs_castexpr(id, type.javaResult.id, parent, idx) tw.writeExprsKotlinType(id, type.kotlinResult.id) - tw.writeHasLocation(id, locId) - tw.writeCallableEnclosingExpr(id, callable) - tw.writeStatementEnclosingExpr(id, enclosingStmt) + extractExprContext(id, locId, callable, enclosingStmt) extractTypeAccessRecursive(e.typeOperand, locId, id, 0, callable, enclosingStmt) extractExpressionExpr(e.argument, callable, id, 1, enclosingStmt) } @@ -5189,9 +5076,7 @@ open class KotlinFileExtractor( val type = useType(e.type) tw.writeExprs_implicitcastexpr(id, type.javaResult.id, parent, idx) tw.writeExprsKotlinType(id, type.kotlinResult.id) - tw.writeHasLocation(id, locId) - tw.writeCallableEnclosingExpr(id, callable) - tw.writeStatementEnclosingExpr(id, enclosingStmt) + extractExprContext(id, locId, callable, enclosingStmt) extractTypeAccessRecursive(e.typeOperand, locId, id, 0, callable, enclosingStmt) extractExpressionExpr(e.argument, callable, id, 1, enclosingStmt) } @@ -5201,9 +5086,7 @@ open class KotlinFileExtractor( val type = useType(e.type) tw.writeExprs_implicitnotnullexpr(id, type.javaResult.id, parent, idx) tw.writeExprsKotlinType(id, type.kotlinResult.id) - tw.writeHasLocation(id, locId) - tw.writeCallableEnclosingExpr(id, callable) - tw.writeStatementEnclosingExpr(id, enclosingStmt) + extractExprContext(id, locId, callable, enclosingStmt) extractTypeAccessRecursive(e.typeOperand, locId, id, 0, callable, enclosingStmt) extractExpressionExpr(e.argument, callable, id, 1, enclosingStmt) } @@ -5213,9 +5096,7 @@ open class KotlinFileExtractor( val type = useType(e.type) tw.writeExprs_implicitcoerciontounitexpr(id, type.javaResult.id, parent, idx) tw.writeExprsKotlinType(id, type.kotlinResult.id) - tw.writeHasLocation(id, locId) - tw.writeCallableEnclosingExpr(id, callable) - tw.writeStatementEnclosingExpr(id, enclosingStmt) + extractExprContext(id, locId, callable, enclosingStmt) extractTypeAccessRecursive(e.typeOperand, locId, id, 0, callable, enclosingStmt) extractExpressionExpr(e.argument, callable, id, 1, enclosingStmt) } @@ -5225,9 +5106,7 @@ open class KotlinFileExtractor( val type = useType(e.type) tw.writeExprs_safecastexpr(id, type.javaResult.id, parent, idx) tw.writeExprsKotlinType(id, type.kotlinResult.id) - tw.writeHasLocation(id, locId) - tw.writeCallableEnclosingExpr(id, callable) - tw.writeStatementEnclosingExpr(id, enclosingStmt) + extractExprContext(id, locId, callable, enclosingStmt) extractTypeAccessRecursive(e.typeOperand, locId, id, 0, callable, enclosingStmt) extractExpressionExpr(e.argument, callable, id, 1, enclosingStmt) } @@ -5237,9 +5116,7 @@ open class KotlinFileExtractor( val type = useType(e.type) tw.writeExprs_instanceofexpr(id, type.javaResult.id, parent, idx) tw.writeExprsKotlinType(id, type.kotlinResult.id) - tw.writeHasLocation(id, locId) - tw.writeCallableEnclosingExpr(id, callable) - tw.writeStatementEnclosingExpr(id, enclosingStmt) + extractExprContext(id, locId, callable, enclosingStmt) extractExpressionExpr(e.argument, callable, id, 0, enclosingStmt) extractTypeAccessRecursive(e.typeOperand, locId, id, 1, callable, enclosingStmt) } @@ -5249,9 +5126,7 @@ open class KotlinFileExtractor( val type = useType(e.type) tw.writeExprs_notinstanceofexpr(id, type.javaResult.id, parent, idx) tw.writeExprsKotlinType(id, type.kotlinResult.id) - tw.writeHasLocation(id, locId) - tw.writeCallableEnclosingExpr(id, callable) - tw.writeStatementEnclosingExpr(id, enclosingStmt) + extractExprContext(id, locId, callable, enclosingStmt) extractExpressionExpr(e.argument, callable, id, 0, enclosingStmt) extractTypeAccessRecursive(e.typeOperand, locId, id, 1, callable, enclosingStmt) } @@ -5376,18 +5251,12 @@ open class KotlinFileExtractor( //.invoke(vp0, cp1, vp2, vp3, ...) or //.invoke(new Object[x]{vp0, vp1, vp2, ...}) - fun extractCommonExpr(id: Label) { - tw.writeHasLocation(id, locId) - tw.writeCallableEnclosingExpr(id, ids.function) - tw.writeStatementEnclosingExpr(id, returnId) - } - // Call to original `invoke`: val callId = tw.getFreshIdLabel() val callType = useType(trySub(samMember.returnType, TypeContext.RETURN)) tw.writeExprs_methodaccess(callId, callType.javaResult.id, returnId, 0) tw.writeExprsKotlinType(callId, callType.kotlinResult.id) - extractCommonExpr(callId) + extractExprContext(callId, locId, ids.function, returnId) val calledMethodId = useFunction(invokeMethod, functionType.arguments) tw.writeCallableBinding(callId, calledMethodId) @@ -5396,7 +5265,7 @@ open class KotlinFileExtractor( val lhsType = useType(functionType) tw.writeExprs_varaccess(lhsId, lhsType.javaResult.id, callId, -1) tw.writeExprsKotlinType(lhsId, lhsType.kotlinResult.id) - extractCommonExpr(lhsId) + extractExprContext(lhsId, locId, ids.function, returnId) tw.writeVariableBinding(lhsId, fieldId) val parameters = mutableListOf() @@ -5411,7 +5280,7 @@ open class KotlinFileExtractor( val paramType = useType(trySub(p.type, TypeContext.OTHER)) tw.writeExprs_varaccess(argsAccessId, paramType.javaResult.id, parent, idx) tw.writeExprsKotlinType(argsAccessId, paramType.kotlinResult.id) - extractCommonExpr(argsAccessId) + extractExprContext(argsAccessId, locId, ids.function, returnId) tw.writeVariableBinding(argsAccessId, useValueParameter(p, ids.function)) } @@ -5432,9 +5301,7 @@ open class KotlinFileExtractor( val type = useType(e.typeOperand) tw.writeExprs_castexpr(id, type.javaResult.id, parent, idx) tw.writeExprsKotlinType(id, type.kotlinResult.id) - tw.writeHasLocation(id, locId) - tw.writeCallableEnclosingExpr(id, callable) - tw.writeStatementEnclosingExpr(id, enclosingStmt) + extractExprContext(id, locId, callable, enclosingStmt) extractTypeAccessRecursive(e.typeOperand, locId, id, 0, callable, enclosingStmt) val idNewexpr = extractNewExpr(ids.constructor, ids.type, locId, id, 1, callable, enclosingStmt)