Fix disappearing variable labels

This commit is contained in:
Tamas Vajk
2021-12-02 11:11:55 +01:00
committed by Ian Lynagh
parent 27f58f2929
commit 01f46555b0
3 changed files with 35 additions and 32 deletions

View File

@@ -6,6 +6,7 @@ import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.declarations.IrFunction
import org.jetbrains.kotlin.ir.declarations.IrVariable
import org.jetbrains.kotlin.ir.expressions.IrConst
import org.jetbrains.kotlin.ir.expressions.IrConstructorCall
import org.jetbrains.kotlin.ir.symbols.IrConstructorSymbol
@@ -32,8 +33,16 @@ class KotlinSourceFileExtractor(
}
}
data class SourceFileExtractionState(val anonymousTypeMap: MutableMap<IrClass, TypeResults> = mutableMapOf(),
val generatedLocalFunctionTypeMap: MutableMap<IrFunction, LocalFunctionLabels> = mutableMapOf())
data class SourceFileExtractionState(val anonymousTypeMapping: MutableMap<IrClass, TypeResults> = mutableMapOf(),
val generatedLocalFunctionTypeMapping: MutableMap<IrFunction, LocalFunctionLabels> = mutableMapOf(),
/**
* 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.
*/
val variableLabelMapping: MutableMap<IrVariable, Label<DbLocalvar>> = mutableMapOf())
companion object {
private val stateCache: MutableMap<IrFile, SourceFileExtractionState> = mutableMapOf()
@@ -92,14 +101,29 @@ class KotlinSourceFileExtractor(
return id
}
/**
* This returns the label used for a local variable, creating one
* if none currently exists.
*/
fun <T> getVariableLabelFor(v: IrVariable): Label<DbLocalvar> {
val maybeLabel = fileExtractionState.variableLabelMapping[v]
if (maybeLabel == null) {
val label = tw.getFreshIdLabel<DbLocalvar>()
fileExtractionState.variableLabelMapping[v] = label
return label
} else {
return maybeLabel
}
}
fun useAnonymousClass(c: IrClass): TypeResults {
var res = fileExtractionState.anonymousTypeMap[c]
var res = fileExtractionState.anonymousTypeMapping[c]
if (res == null) {
val javaResult = TypeResult(tw.getFreshIdLabel<DbClass>(), "", "")
val kotlinResult = TypeResult(tw.getFreshIdLabel<DbKt_notnull_type>(), "", "")
tw.writeKt_notnull_types(kotlinResult.id, javaResult.id)
res = TypeResults(javaResult, kotlinResult)
fileExtractionState.anonymousTypeMap[c] = res
fileExtractionState.anonymousTypeMapping[c] = res
}
return res
@@ -112,7 +136,7 @@ class KotlinSourceFileExtractor(
logger.warnElement(Severity.ErrorSevere, "Extracting a non-local function as a local one", f)
}
var res = fileExtractionState.generatedLocalFunctionTypeMap[f]
var res = fileExtractionState.generatedLocalFunctionTypeMapping[f]
if (res == null) {
val javaResult = TypeResult(tw.getFreshIdLabel<DbClass>(), "", "")
val kotlinResult = TypeResult(tw.getFreshIdLabel<DbKt_notnull_type>(), "", "")
@@ -121,7 +145,7 @@ class KotlinSourceFileExtractor(
TypeResults(javaResult, kotlinResult),
tw.getFreshIdLabel(),
tw.getFreshIdLabel())
fileExtractionState.generatedLocalFunctionTypeMap[f] = res
fileExtractionState.generatedLocalFunctionTypeMapping[f] = res
}
return res

View File

@@ -720,8 +720,8 @@ class X {
fun useTypeAlias(ta: IrTypeAlias): Label<out DbKt_type_alias> =
tw.getLabelFor(getTypeAliasLabel(ta))
fun useVariable(v: IrVariable): Label<out DbLocalvar> {
return tw.getVariableLabelFor<DbLocalvar>(v)
fun useVariable(v: IrVariable): Label<DbLocalvar> {
return withSourceFile(v.fileOrNull!!).getVariableLabelFor<DbLocalvar>(v)
}
fun withQuestionMark(t: IrType, hasQuestionMark: Boolean) = if(hasQuestionMark) t.makeNullable() else t.makeNotNull()

View File

@@ -39,7 +39,9 @@ class TrapLabelManager {
* instances will have different additional state, but they must all
* share the same `TrapLabelManager` and `BufferedWriter`.
*/
open class TrapWriter (protected val lm: TrapLabelManager, private val bw: BufferedWriter) {
open class TrapWriter (
protected val lm: TrapLabelManager,
private val bw: BufferedWriter) {
/**
* Returns the label that is defined to be the given key, if such
* 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
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<IrVariable, Label<out DbLocalvar>> = mutableMapOf<IrVariable, Label<out DbLocalvar>>()
/**
* This returns the label used for a local variable, creating one
* if none currently exists.
*/
fun <T> getVariableLabelFor(v: IrVariable): Label<out DbLocalvar> {
val maybeLabel = variableLabelMapping.get(v)
if(maybeLabel == null) {
val label = lm.getFreshLabel<DbLocalvar>()
variableLabelMapping.put(v, label)
writeTrap("$label = *\n")
return label
} else {
return maybeLabel
}
}
/**
* This returns a label for the location described by its arguments.