Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 01f4655

Browse files
tamasvajkigfoo
authored andcommitted
Fix disappearing variable labels
1 parent 27f58f2 commit 01f4655

3 files changed

Lines changed: 35 additions & 32 deletions

File tree

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

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import org.jetbrains.kotlin.ir.IrElement
66
import org.jetbrains.kotlin.ir.declarations.IrClass
77
import org.jetbrains.kotlin.ir.declarations.IrFile
88
import org.jetbrains.kotlin.ir.declarations.IrFunction
9+
import org.jetbrains.kotlin.ir.declarations.IrVariable
910
import org.jetbrains.kotlin.ir.expressions.IrConst
1011
import org.jetbrains.kotlin.ir.expressions.IrConstructorCall
1112
import org.jetbrains.kotlin.ir.symbols.IrConstructorSymbol
@@ -32,8 +33,16 @@ class KotlinSourceFileExtractor(
3233
}
3334
}
3435

35-
data class SourceFileExtractionState(val anonymousTypeMap: MutableMap<IrClass, TypeResults> = mutableMapOf(),
36-
val generatedLocalFunctionTypeMap: MutableMap<IrFunction, LocalFunctionLabels> = mutableMapOf())
36+
data class SourceFileExtractionState(val anonymousTypeMapping: MutableMap<IrClass, TypeResults> = mutableMapOf(),
37+
val generatedLocalFunctionTypeMapping: MutableMap<IrFunction, LocalFunctionLabels> = mutableMapOf(),
38+
/**
39+
* It is not easy to assign keys to local variables, so they get
40+
* given `*` IDs. However, the same variable may be referred to
41+
* from distant places in the IR, so we need a way to find out
42+
* which label is used for a given local variable. This information
43+
* is stored in this mapping.
44+
*/
45+
val variableLabelMapping: MutableMap<IrVariable, Label<DbLocalvar>> = mutableMapOf())
3746

3847
companion object {
3948
private val stateCache: MutableMap<IrFile, SourceFileExtractionState> = mutableMapOf()
@@ -92,14 +101,29 @@ class KotlinSourceFileExtractor(
92101
return id
93102
}
94103

104+
/**
105+
* This returns the label used for a local variable, creating one
106+
* if none currently exists.
107+
*/
108+
fun <T> getVariableLabelFor(v: IrVariable): Label<DbLocalvar> {
109+
val maybeLabel = fileExtractionState.variableLabelMapping[v]
110+
if (maybeLabel == null) {
111+
val label = tw.getFreshIdLabel<DbLocalvar>()
112+
fileExtractionState.variableLabelMapping[v] = label
113+
return label
114+
} else {
115+
return maybeLabel
116+
}
117+
}
118+
95119
fun useAnonymousClass(c: IrClass): TypeResults {
96-
var res = fileExtractionState.anonymousTypeMap[c]
120+
var res = fileExtractionState.anonymousTypeMapping[c]
97121
if (res == null) {
98122
val javaResult = TypeResult(tw.getFreshIdLabel<DbClass>(), "", "")
99123
val kotlinResult = TypeResult(tw.getFreshIdLabel<DbKt_notnull_type>(), "", "")
100124
tw.writeKt_notnull_types(kotlinResult.id, javaResult.id)
101125
res = TypeResults(javaResult, kotlinResult)
102-
fileExtractionState.anonymousTypeMap[c] = res
126+
fileExtractionState.anonymousTypeMapping[c] = res
103127
}
104128

105129
return res
@@ -112,7 +136,7 @@ class KotlinSourceFileExtractor(
112136
logger.warnElement(Severity.ErrorSevere, "Extracting a non-local function as a local one", f)
113137
}
114138

115-
var res = fileExtractionState.generatedLocalFunctionTypeMap[f]
139+
var res = fileExtractionState.generatedLocalFunctionTypeMapping[f]
116140
if (res == null) {
117141
val javaResult = TypeResult(tw.getFreshIdLabel<DbClass>(), "", "")
118142
val kotlinResult = TypeResult(tw.getFreshIdLabel<DbKt_notnull_type>(), "", "")
@@ -121,7 +145,7 @@ class KotlinSourceFileExtractor(
121145
TypeResults(javaResult, kotlinResult),
122146
tw.getFreshIdLabel(),
123147
tw.getFreshIdLabel())
124-
fileExtractionState.generatedLocalFunctionTypeMap[f] = res
148+
fileExtractionState.generatedLocalFunctionTypeMapping[f] = res
125149
}
126150

127151
return res

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -720,8 +720,8 @@ class X {
720720
fun useTypeAlias(ta: IrTypeAlias): Label<out DbKt_type_alias> =
721721
tw.getLabelFor(getTypeAliasLabel(ta))
722722

723-
fun useVariable(v: IrVariable): Label<out DbLocalvar> {
724-
return tw.getVariableLabelFor<DbLocalvar>(v)
723+
fun useVariable(v: IrVariable): Label<DbLocalvar> {
724+
return withSourceFile(v.fileOrNull!!).getVariableLabelFor<DbLocalvar>(v)
725725
}
726726

727727
fun withQuestionMark(t: IrType, hasQuestionMark: Boolean) = if(hasQuestionMark) t.makeNullable() else t.makeNotNull()

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

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ class TrapLabelManager {
3939
* instances will have different additional state, but they must all
4040
* share the same `TrapLabelManager` and `BufferedWriter`.
4141
*/
42-
open class TrapWriter (protected val lm: TrapLabelManager, private val bw: BufferedWriter) {
42+
open class TrapWriter (
43+
protected val lm: TrapLabelManager,
44+
private val bw: BufferedWriter) {
4345
/**
4446
* Returns the label that is defined to be the given key, if such
4547
* a label exists, and `null` otherwise. Most users will want to use
@@ -68,29 +70,6 @@ open class TrapWriter (protected val lm: TrapLabelManager, private val bw: Buffe
6870
return maybeLabel
6971
}
7072
}
71-
/**
72-
* It is not easy to assign keys to local variables, so they get
73-
* given `*` IDs. However, the same variable may be referred to
74-
* from distant places in the IR, so we need a way to find out
75-
* which label is used for a given local variable. This information
76-
* is stored in this mapping.
77-
*/
78-
private val variableLabelMapping: MutableMap<IrVariable, Label<out DbLocalvar>> = mutableMapOf<IrVariable, Label<out DbLocalvar>>()
79-
/**
80-
* This returns the label used for a local variable, creating one
81-
* if none currently exists.
82-
*/
83-
fun <T> getVariableLabelFor(v: IrVariable): Label<out DbLocalvar> {
84-
val maybeLabel = variableLabelMapping.get(v)
85-
if(maybeLabel == null) {
86-
val label = lm.getFreshLabel<DbLocalvar>()
87-
variableLabelMapping.put(v, label)
88-
writeTrap("$label = *\n")
89-
return label
90-
} else {
91-
return maybeLabel
92-
}
93-
}
9473

9574
/**
9675
* This returns a label for the location described by its arguments.

0 commit comments

Comments
 (0)