From a471fa004a6a6145adaf973ced6eafef01209da6 Mon Sep 17 00:00:00 2001 From: Tamas Vajk Date: Wed, 9 Oct 2024 10:11:10 +0200 Subject: [PATCH 1/2] KE2: Extract local variable declarations --- .../src/main/kotlin/KotlinFileExtractor.kt | 57 -------------- .../src/main/kotlin/KotlinUsesExtractor.kt | 4 - .../src/main/kotlin/TrapWriter.kt | 44 ++++++----- .../src/main/kotlin/entities/Expression.kt | 74 +++++++++++++++++++ 4 files changed, 95 insertions(+), 84 deletions(-) diff --git a/java/kotlin-extractor2/src/main/kotlin/KotlinFileExtractor.kt b/java/kotlin-extractor2/src/main/kotlin/KotlinFileExtractor.kt index 968289a6696a..8234f9e1a87a 100644 --- a/java/kotlin-extractor2/src/main/kotlin/KotlinFileExtractor.kt +++ b/java/kotlin-extractor2/src/main/kotlin/KotlinFileExtractor.kt @@ -2474,63 +2474,6 @@ OLD: KE1 /* OLD: KE1 - private fun getVariableLocationProvider(v: IrVariable): IrElement { - val init = v.initializer - if (v.startOffset < 0 && init != null) { - // IR_TEMPORARY_VARIABLEs have no proper location - return init - } - - return v - } - - private fun extractVariable( - v: IrVariable, - callable: Label, - parent: Label, - idx: Int - ) { - with("variable", v) { - val stmtId = tw.getFreshIdLabel() - val locId = tw.getLocation(getVariableLocationProvider(v)) - tw.writeStmts_localvariabledeclstmt(stmtId, parent, idx, callable) - tw.writeHasLocation(stmtId, locId) - extractVariableExpr(v, callable, stmtId, 1, stmtId) - } - } - - private fun extractVariableExpr( - v: IrVariable, - callable: Label, - parent: Label, - idx: Int, - enclosingStmt: Label, - extractInitializer: Boolean = true - ) { - with("variable expr", v) { - val varId = useVariable(v) - val exprId = tw.getFreshIdLabel() - val locId = tw.getLocation(getVariableLocationProvider(v)) - val type = useType(v.type) - tw.writeLocalvars(varId, v.name.asString(), type.javaResult.id, exprId) - tw.writeLocalvarsKotlinType(varId, type.kotlinResult.id) - tw.writeHasLocation(varId, locId) - tw.writeExprs_localvariabledeclexpr(exprId, type.javaResult.id, parent, idx) - tw.writeExprsKotlinType(exprId, type.kotlinResult.id) - extractExprContext(exprId, locId, callable, enclosingStmt) - val i = v.initializer - if (i != null && extractInitializer) { - extractExpressionExpr(i, callable, exprId, 0, enclosingStmt) - } - if (!v.isVar) { - addModifiers(varId, "final") - } - if (v.isLateinit) { - addModifiers(varId, "lateinit") - } - } - } - private fun extractIfStmt( locId: Label, parent: Label, diff --git a/java/kotlin-extractor2/src/main/kotlin/KotlinUsesExtractor.kt b/java/kotlin-extractor2/src/main/kotlin/KotlinUsesExtractor.kt index 35caf2fb01a1..9860f21620ba 100644 --- a/java/kotlin-extractor2/src/main/kotlin/KotlinUsesExtractor.kt +++ b/java/kotlin-extractor2/src/main/kotlin/KotlinUsesExtractor.kt @@ -1699,9 +1699,5 @@ open class KotlinUsesExtractor( fun useTypeAlias(ta: IrTypeAlias): Label = tw.getLabelFor(getTypeAliasLabel(ta)) - - fun useVariable(v: IrVariable): Label { - return tw.getVariableLabelFor(v) - } */ } diff --git a/java/kotlin-extractor2/src/main/kotlin/TrapWriter.kt b/java/kotlin-extractor2/src/main/kotlin/TrapWriter.kt index 595e7ba3d0f2..e6dd43be80d4 100644 --- a/java/kotlin-extractor2/src/main/kotlin/TrapWriter.kt +++ b/java/kotlin-extractor2/src/main/kotlin/TrapWriter.kt @@ -147,31 +147,29 @@ abstract class TrapWriter( return label } - /* - OLD: KE1 - /** - * It is not easy to assign keys to local variables, so they get given `*` IDs. However, the - * same variable may be referred to from distant places in the IR, so we need a way to find out - * which label is used for a given local variable. This information is stored in this mapping. - */ - private val variableLabelMapping: MutableMap> = - mutableMapOf>() - /** This returns the label used for a local variable, creating one if none currently exists. */ - fun getVariableLabelFor(v: IrVariable): Label { - val maybeLabel = variableLabelMapping.get(v) - if (maybeLabel == null) { - val label = getFreshIdLabel() - variableLabelMapping.put(v, label) - return label - } else { - return maybeLabel - } + /** + * It is not easy to assign keys to local variables, so they get given `*` IDs. However, the + * same variable may be referred to from distant places in the IR, so we need a way to find out + * which label is used for a given local variable. This information is stored in this mapping. + */ + private val variableLabelMapping: MutableMap> = + mutableMapOf>() + + /** This returns the label used for a local variable, creating one if none currently exists. */ + fun getVariableLabelFor(v: KtProperty): Label { + val maybeLabel = variableLabelMapping[v] + if (maybeLabel == null) { + val label = getFreshIdLabel() + variableLabelMapping.put(v, label) + return label + } else { + return maybeLabel } + } - fun getExistingVariableLabelFor(v: IrVariable): Label? { - return variableLabelMapping.get(v) - } - */ + fun getExistingVariableLabelFor(v: KtProperty): Label? { + return variableLabelMapping[v] + } /** * This returns a label for the location described by its arguments. Typically users will not diff --git a/java/kotlin-extractor2/src/main/kotlin/entities/Expression.kt b/java/kotlin-extractor2/src/main/kotlin/entities/Expression.kt index c6702d99a4ab..c563d23dd437 100644 --- a/java/kotlin-extractor2/src/main/kotlin/entities/Expression.kt +++ b/java/kotlin-extractor2/src/main/kotlin/entities/Expression.kt @@ -279,6 +279,11 @@ private fun KotlinFileExtractor.extractExpression( extractExpressionExpr(e.left, callable, id, 1, exprParent.enclosingStmt) } + is KtProperty -> { + val stmtParent = parent.stmt(e, callable) + extractVariable(e, callable, stmtParent.parent, stmtParent.idx) + } + /* OLD: KE1 is IrDelegatingConstructorCall -> { @@ -2341,3 +2346,72 @@ private fun KotlinFileExtractor.extractBreakContinue(e: KtExpressionWithLabel, i } } } + +/* +OLD KE1: +private fun KotlinFileExtractor.getVariableLocationProvider(v: KtProperty): IrElement { + val init = v.initializer + if (v.startOffset < 0 && init != null) { + // IR_TEMPORARY_VARIABLEs have no proper location + return init + } + + return v +} +*/ + +context(KaSession) +private fun KotlinFileExtractor.extractVariable( + v: KtProperty, + callable: Label, + parent: Label, + idx: Int +) { + with("variable", v) { + val stmtId = tw.getFreshIdLabel() + val locId = tw.getLocation(v) // OLD KE1: getVariableLocationProvider(v)) + tw.writeStmts_localvariabledeclstmt(stmtId, parent, idx, callable) + tw.writeHasLocation(stmtId, locId) + extractVariableExpr(v, callable, stmtId, 1, stmtId) + } +} + +context(KaSession) +private fun KotlinFileExtractor.extractVariableExpr( + v: KtProperty, + callable: Label, + parent: Label, + idx: Int, + enclosingStmt: Label, + extractInitializer: Boolean = true +) { + with("variable expr", v) { + val varId = useVariable(v) + val exprId = tw.getFreshIdLabel() + val locId = tw.getLocation(v) // OLD KE1: getVariableLocationProvider(v)) + val type = useType(v.returnType) + tw.writeLocalvars(varId, v.name!!, type.javaResult.id, exprId) + tw.writeLocalvarsKotlinType(varId, type.kotlinResult.id) + tw.writeHasLocation(varId, locId) + tw.writeExprs_localvariabledeclexpr(exprId, type.javaResult.id, parent, idx) + tw.writeExprsKotlinType(exprId, type.kotlinResult.id) + extractExprContext(exprId, locId, callable, enclosingStmt) + val i = v.initializer + if (i != null && extractInitializer) { + extractExpressionExpr(i, callable, exprId, 0, enclosingStmt) + } + if (!v.isVar) { + addModifiers(varId, "final") + } + /* + OLD KE1: + if (v.isLateinit) { + addModifiers(varId, "lateinit") + } + */ + } +} + +private fun KotlinFileExtractor.useVariable(v: KtProperty): Label { + return tw.getVariableLabelFor(v) +} From e82b1762c096abee2424da0d584c14af6ea0eca9 Mon Sep 17 00:00:00 2001 From: Tamas Vajk Date: Wed, 9 Oct 2024 16:02:54 +0200 Subject: [PATCH 2/2] Apply code review findings --- java/kotlin-extractor2/src/main/kotlin/TrapWriter.kt | 4 ++++ .../kotlin-extractor2/src/main/kotlin/entities/Expression.kt | 5 +++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/java/kotlin-extractor2/src/main/kotlin/TrapWriter.kt b/java/kotlin-extractor2/src/main/kotlin/TrapWriter.kt index e6dd43be80d4..2a2610c6a097 100644 --- a/java/kotlin-extractor2/src/main/kotlin/TrapWriter.kt +++ b/java/kotlin-extractor2/src/main/kotlin/TrapWriter.kt @@ -152,6 +152,7 @@ abstract class TrapWriter( * same variable may be referred to from distant places in the IR, so we need a way to find out * which label is used for a given local variable. This information is stored in this mapping. */ + // TODO: This should be in a subclass so that DiagnosticTrapWriter doesn't include it, as it is not threadsafe private val variableLabelMapping: MutableMap> = mutableMapOf>() @@ -167,9 +168,12 @@ abstract class TrapWriter( } } + /* + OLD: KE1 fun getExistingVariableLabelFor(v: KtProperty): Label? { return variableLabelMapping[v] } + */ /** * This returns a label for the location described by its arguments. Typically users will not diff --git a/java/kotlin-extractor2/src/main/kotlin/entities/Expression.kt b/java/kotlin-extractor2/src/main/kotlin/entities/Expression.kt index c563d23dd437..4592059b1776 100644 --- a/java/kotlin-extractor2/src/main/kotlin/entities/Expression.kt +++ b/java/kotlin-extractor2/src/main/kotlin/entities/Expression.kt @@ -2383,7 +2383,7 @@ private fun KotlinFileExtractor.extractVariableExpr( parent: Label, idx: Int, enclosingStmt: Label, - extractInitializer: Boolean = true + //extractInitializer: Boolean = true // OLD KE1 ) { with("variable expr", v) { val varId = useVariable(v) @@ -2397,7 +2397,8 @@ private fun KotlinFileExtractor.extractVariableExpr( tw.writeExprsKotlinType(exprId, type.kotlinResult.id) extractExprContext(exprId, locId, callable, enclosingStmt) val i = v.initializer - if (i != null && extractInitializer) { + //OLD KE1: if (i != null && extractInitializer) { + if (i != null) { extractExpressionExpr(i, callable, exprId, 0, enclosingStmt) } if (!v.isVar) {