Skip to content

Commit 643419a

Browse files
authored
Merge pull request #17707 from tamasvajk/ke2-vari
KE2: Extract local variable declarations
2 parents 01c71ba + e82b176 commit 643419a

File tree

4 files changed

+99
-83
lines changed

4 files changed

+99
-83
lines changed

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

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2474,63 +2474,6 @@ OLD: KE1
24742474

24752475
/*
24762476
OLD: KE1
2477-
private fun getVariableLocationProvider(v: IrVariable): IrElement {
2478-
val init = v.initializer
2479-
if (v.startOffset < 0 && init != null) {
2480-
// IR_TEMPORARY_VARIABLEs have no proper location
2481-
return init
2482-
}
2483-
2484-
return v
2485-
}
2486-
2487-
private fun extractVariable(
2488-
v: IrVariable,
2489-
callable: Label<out DbCallable>,
2490-
parent: Label<out DbStmtparent>,
2491-
idx: Int
2492-
) {
2493-
with("variable", v) {
2494-
val stmtId = tw.getFreshIdLabel<DbLocalvariabledeclstmt>()
2495-
val locId = tw.getLocation(getVariableLocationProvider(v))
2496-
tw.writeStmts_localvariabledeclstmt(stmtId, parent, idx, callable)
2497-
tw.writeHasLocation(stmtId, locId)
2498-
extractVariableExpr(v, callable, stmtId, 1, stmtId)
2499-
}
2500-
}
2501-
2502-
private fun extractVariableExpr(
2503-
v: IrVariable,
2504-
callable: Label<out DbCallable>,
2505-
parent: Label<out DbExprparent>,
2506-
idx: Int,
2507-
enclosingStmt: Label<out DbStmt>,
2508-
extractInitializer: Boolean = true
2509-
) {
2510-
with("variable expr", v) {
2511-
val varId = useVariable(v)
2512-
val exprId = tw.getFreshIdLabel<DbLocalvariabledeclexpr>()
2513-
val locId = tw.getLocation(getVariableLocationProvider(v))
2514-
val type = useType(v.type)
2515-
tw.writeLocalvars(varId, v.name.asString(), type.javaResult.id, exprId)
2516-
tw.writeLocalvarsKotlinType(varId, type.kotlinResult.id)
2517-
tw.writeHasLocation(varId, locId)
2518-
tw.writeExprs_localvariabledeclexpr(exprId, type.javaResult.id, parent, idx)
2519-
tw.writeExprsKotlinType(exprId, type.kotlinResult.id)
2520-
extractExprContext(exprId, locId, callable, enclosingStmt)
2521-
val i = v.initializer
2522-
if (i != null && extractInitializer) {
2523-
extractExpressionExpr(i, callable, exprId, 0, enclosingStmt)
2524-
}
2525-
if (!v.isVar) {
2526-
addModifiers(varId, "final")
2527-
}
2528-
if (v.isLateinit) {
2529-
addModifiers(varId, "lateinit")
2530-
}
2531-
}
2532-
}
2533-
25342477
private fun extractIfStmt(
25352478
locId: Label<DbLocation>,
25362479
parent: Label<out DbStmtparent>,

java/kotlin-extractor2/src/main/kotlin/KotlinUsesExtractor.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1699,9 +1699,5 @@ open class KotlinUsesExtractor(
16991699
17001700
fun useTypeAlias(ta: IrTypeAlias): Label<out DbKt_type_alias> =
17011701
tw.getLabelFor(getTypeAliasLabel(ta))
1702-
1703-
fun useVariable(v: IrVariable): Label<out DbLocalvar> {
1704-
return tw.getVariableLabelFor<DbLocalvar>(v)
1705-
}
17061702
*/
17071703
}

java/kotlin-extractor2/src/main/kotlin/TrapWriter.kt

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -147,30 +147,32 @@ abstract class TrapWriter(
147147
return label
148148
}
149149

150-
/*
151-
OLD: KE1
152-
/**
153-
* It is not easy to assign keys to local variables, so they get given `*` IDs. However, the
154-
* same variable may be referred to from distant places in the IR, so we need a way to find out
155-
* which label is used for a given local variable. This information is stored in this mapping.
156-
*/
157-
private val variableLabelMapping: MutableMap<IrVariable, Label<out DbLocalvar>> =
158-
mutableMapOf<IrVariable, Label<out DbLocalvar>>()
159-
/** This returns the label used for a local variable, creating one if none currently exists. */
160-
fun <T> getVariableLabelFor(v: IrVariable): Label<out DbLocalvar> {
161-
val maybeLabel = variableLabelMapping.get(v)
162-
if (maybeLabel == null) {
163-
val label = getFreshIdLabel<DbLocalvar>()
164-
variableLabelMapping.put(v, label)
165-
return label
166-
} else {
167-
return maybeLabel
168-
}
150+
/**
151+
* It is not easy to assign keys to local variables, so they get given `*` IDs. However, the
152+
* same variable may be referred to from distant places in the IR, so we need a way to find out
153+
* which label is used for a given local variable. This information is stored in this mapping.
154+
*/
155+
// TODO: This should be in a subclass so that DiagnosticTrapWriter doesn't include it, as it is not threadsafe
156+
private val variableLabelMapping: MutableMap<KtProperty, Label<out DbLocalvar>> =
157+
mutableMapOf<KtProperty, Label<out DbLocalvar>>()
158+
159+
/** This returns the label used for a local variable, creating one if none currently exists. */
160+
fun <T> getVariableLabelFor(v: KtProperty): Label<out DbLocalvar> {
161+
val maybeLabel = variableLabelMapping[v]
162+
if (maybeLabel == null) {
163+
val label = getFreshIdLabel<DbLocalvar>()
164+
variableLabelMapping.put(v, label)
165+
return label
166+
} else {
167+
return maybeLabel
169168
}
169+
}
170170

171-
fun getExistingVariableLabelFor(v: IrVariable): Label<out DbLocalvar>? {
172-
return variableLabelMapping.get(v)
173-
}
171+
/*
172+
OLD: KE1
173+
fun getExistingVariableLabelFor(v: KtProperty): Label<out DbLocalvar>? {
174+
return variableLabelMapping[v]
175+
}
174176
*/
175177

176178
/**

java/kotlin-extractor2/src/main/kotlin/entities/Expression.kt

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,11 @@ private fun KotlinFileExtractor.extractExpression(
279279
extractExpressionExpr(e.left, callable, id, 1, exprParent.enclosingStmt)
280280
}
281281

282+
is KtProperty -> {
283+
val stmtParent = parent.stmt(e, callable)
284+
extractVariable(e, callable, stmtParent.parent, stmtParent.idx)
285+
}
286+
282287
/*
283288
OLD: KE1
284289
is IrDelegatingConstructorCall -> {
@@ -2341,3 +2346,73 @@ private fun KotlinFileExtractor.extractBreakContinue(e: KtExpressionWithLabel, i
23412346
}
23422347
}
23432348
}
2349+
2350+
/*
2351+
OLD KE1:
2352+
private fun KotlinFileExtractor.getVariableLocationProvider(v: KtProperty): IrElement {
2353+
val init = v.initializer
2354+
if (v.startOffset < 0 && init != null) {
2355+
// IR_TEMPORARY_VARIABLEs have no proper location
2356+
return init
2357+
}
2358+
2359+
return v
2360+
}
2361+
*/
2362+
2363+
context(KaSession)
2364+
private fun KotlinFileExtractor.extractVariable(
2365+
v: KtProperty,
2366+
callable: Label<out DbCallable>,
2367+
parent: Label<out DbStmtparent>,
2368+
idx: Int
2369+
) {
2370+
with("variable", v) {
2371+
val stmtId = tw.getFreshIdLabel<DbLocalvariabledeclstmt>()
2372+
val locId = tw.getLocation(v) // OLD KE1: getVariableLocationProvider(v))
2373+
tw.writeStmts_localvariabledeclstmt(stmtId, parent, idx, callable)
2374+
tw.writeHasLocation(stmtId, locId)
2375+
extractVariableExpr(v, callable, stmtId, 1, stmtId)
2376+
}
2377+
}
2378+
2379+
context(KaSession)
2380+
private fun KotlinFileExtractor.extractVariableExpr(
2381+
v: KtProperty,
2382+
callable: Label<out DbCallable>,
2383+
parent: Label<out DbExprparent>,
2384+
idx: Int,
2385+
enclosingStmt: Label<out DbStmt>,
2386+
//extractInitializer: Boolean = true // OLD KE1
2387+
) {
2388+
with("variable expr", v) {
2389+
val varId = useVariable(v)
2390+
val exprId = tw.getFreshIdLabel<DbLocalvariabledeclexpr>()
2391+
val locId = tw.getLocation(v) // OLD KE1: getVariableLocationProvider(v))
2392+
val type = useType(v.returnType)
2393+
tw.writeLocalvars(varId, v.name!!, type.javaResult.id, exprId)
2394+
tw.writeLocalvarsKotlinType(varId, type.kotlinResult.id)
2395+
tw.writeHasLocation(varId, locId)
2396+
tw.writeExprs_localvariabledeclexpr(exprId, type.javaResult.id, parent, idx)
2397+
tw.writeExprsKotlinType(exprId, type.kotlinResult.id)
2398+
extractExprContext(exprId, locId, callable, enclosingStmt)
2399+
val i = v.initializer
2400+
//OLD KE1: if (i != null && extractInitializer) {
2401+
if (i != null) {
2402+
extractExpressionExpr(i, callable, exprId, 0, enclosingStmt)
2403+
}
2404+
if (!v.isVar) {
2405+
addModifiers(varId, "final")
2406+
}
2407+
/*
2408+
OLD KE1:
2409+
if (v.isLateinit) {
2410+
addModifiers(varId, "lateinit")
2411+
}
2412+
*/
2413+
}
2414+
}
2415+
2416+
private fun KotlinFileExtractor.useVariable(v: KtProperty): Label<out DbLocalvar> {
2417+
return tw.getVariableLabelFor<DbLocalvar>(v)
2418+
}

0 commit comments

Comments
 (0)