KE2: Extract local variable declarations

This commit is contained in:
Tamas Vajk
2024-10-09 10:11:10 +02:00
parent 01c71ba8d6
commit a471fa004a
4 changed files with 95 additions and 84 deletions

View File

@@ -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<out DbCallable>,
parent: Label<out DbStmtparent>,
idx: Int
) {
with("variable", v) {
val stmtId = tw.getFreshIdLabel<DbLocalvariabledeclstmt>()
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<out DbCallable>,
parent: Label<out DbExprparent>,
idx: Int,
enclosingStmt: Label<out DbStmt>,
extractInitializer: Boolean = true
) {
with("variable expr", v) {
val varId = useVariable(v)
val exprId = tw.getFreshIdLabel<DbLocalvariabledeclexpr>()
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<DbLocation>,
parent: Label<out DbStmtparent>,

View File

@@ -1699,9 +1699,5 @@ open class KotlinUsesExtractor(
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)
}
*/
}

View File

@@ -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<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 = getFreshIdLabel<DbLocalvar>()
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<KtProperty, Label<out DbLocalvar>> =
mutableMapOf<KtProperty, Label<out DbLocalvar>>()
fun getExistingVariableLabelFor(v: IrVariable): Label<out DbLocalvar>? {
return variableLabelMapping.get(v)
/** This returns the label used for a local variable, creating one if none currently exists. */
fun <T> getVariableLabelFor(v: KtProperty): Label<out DbLocalvar> {
val maybeLabel = variableLabelMapping[v]
if (maybeLabel == null) {
val label = getFreshIdLabel<DbLocalvar>()
variableLabelMapping.put(v, label)
return label
} else {
return maybeLabel
}
*/
}
fun getExistingVariableLabelFor(v: KtProperty): Label<out DbLocalvar>? {
return variableLabelMapping[v]
}
/**
* This returns a label for the location described by its arguments. Typically users will not

View File

@@ -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<out DbCallable>,
parent: Label<out DbStmtparent>,
idx: Int
) {
with("variable", v) {
val stmtId = tw.getFreshIdLabel<DbLocalvariabledeclstmt>()
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<out DbCallable>,
parent: Label<out DbExprparent>,
idx: Int,
enclosingStmt: Label<out DbStmt>,
extractInitializer: Boolean = true
) {
with("variable expr", v) {
val varId = useVariable(v)
val exprId = tw.getFreshIdLabel<DbLocalvariabledeclexpr>()
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<out DbLocalvar> {
return tw.getVariableLabelFor<DbLocalvar>(v)
}