Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 0 additions & 57 deletions java/kotlin-extractor2/src/main/kotlin/KotlinFileExtractor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
*/
}
46 changes: 24 additions & 22 deletions java/kotlin-extractor2/src/main/kotlin/TrapWriter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -147,30 +147,32 @@ 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.
*/
// TODO: This should be in a subclass so that DiagnosticTrapWriter doesn't include it, as it is not threadsafe
private val variableLabelMapping: MutableMap<KtProperty, Label<out DbLocalvar>> =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add something like // TODO: This should be in a subclass so that DiagnosticTrapWriter doesn't include it, as it is not threadsafe please?

mutableMapOf<KtProperty, Label<out DbLocalvar>>()

/** 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: IrVariable): Label<out DbLocalvar>? {
return variableLabelMapping.get(v)
}
/*
OLD: KE1
fun getExistingVariableLabelFor(v: KtProperty): Label<out DbLocalvar>? {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can remain commented out with OLD: KE1. I hope we'll be able to remove it once we've reimplemented comment extraction.

return variableLabelMapping[v]
}
*/

/**
Expand Down
75 changes: 75 additions & 0 deletions java/kotlin-extractor2/src/main/kotlin/entities/Expression.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 -> {
Expand Down Expand Up @@ -2341,3 +2346,73 @@ 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 // OLD KE1
) {
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
//OLD KE1: if (i != null && extractInitializer) {
if (i != null) {
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)
}
Loading