Store break/continue targets

This commit is contained in:
Tamas Vajk
2021-09-21 15:46:45 +02:00
committed by Ian Lynagh
parent ae7aa30bda
commit aa190f9d65
2 changed files with 41 additions and 4 deletions

View File

@@ -634,6 +634,8 @@ class KotlinFileExtractor(val logger: FileLogger, val tw: FileTrapWriter, val fi
}
}
private val loopIdMap: MutableMap<IrLoop, Label<out DbKtloopstmt>> = mutableMapOf()
fun extractExpression(e: IrExpression, callable: Label<out DbCallable>, parent: Label<out DbExprparent>, idx: Int) {
when(e) {
is IrCall -> extractCall(e, callable, parent, idx)
@@ -725,15 +727,13 @@ class KotlinFileExtractor(val logger: FileLogger, val tw: FileTrapWriter, val fi
}
is IrBreak -> {
val id = tw.getFreshIdLabel<DbBreakstmt>()
val locId = tw.getLocation(e)
tw.writeStmts_breakstmt(id, parent, idx, callable)
tw.writeHasLocation(id, locId)
extractBreakContinue(e, id)
}
is IrContinue -> {
val id = tw.getFreshIdLabel<DbContinuestmt>()
val locId = tw.getLocation(e)
tw.writeStmts_continuestmt(id, parent, idx, callable)
tw.writeHasLocation(id, locId)
extractBreakContinue(e, id)
}
is IrReturn -> {
val id = tw.getFreshIdLabel<DbReturnstmt>()
@@ -753,6 +753,7 @@ class KotlinFileExtractor(val logger: FileLogger, val tw: FileTrapWriter, val fi
}
is IrWhileLoop -> {
val id = tw.getFreshIdLabel<DbWhilestmt>()
loopIdMap[e] = id
val locId = tw.getLocation(e)
tw.writeStmts_whilestmt(id, parent, idx, callable)
tw.writeHasLocation(id, locId)
@@ -761,9 +762,11 @@ class KotlinFileExtractor(val logger: FileLogger, val tw: FileTrapWriter, val fi
if(body != null) {
extractExpression(body, callable, id, 1)
}
loopIdMap.remove(e)
}
is IrDoWhileLoop -> {
val id = tw.getFreshIdLabel<DbDostmt>()
loopIdMap[e] = id
val locId = tw.getLocation(e)
tw.writeStmts_dostmt(id, parent, idx, callable)
tw.writeHasLocation(id, locId)
@@ -772,6 +775,7 @@ class KotlinFileExtractor(val logger: FileLogger, val tw: FileTrapWriter, val fi
if(body != null) {
extractExpression(body, callable, id, 1)
}
loopIdMap.remove(e)
}
is IrWhen -> {
val id = tw.getFreshIdLabel<DbWhenexpr>()
@@ -807,5 +811,27 @@ class KotlinFileExtractor(val logger: FileLogger, val tw: FileTrapWriter, val fi
}
}
}
private fun extractBreakContinue(
e: IrBreakContinue,
id: Label<out DbBreakcontinuestmt>
) {
val locId = tw.getLocation(e)
@Suppress("UNCHECKED_CAST")
tw.writeHasLocation(id as Label<out DbLocatable>, locId)
val label = e.label
if (label != null) {
@Suppress("UNCHECKED_CAST")
tw.writeNamestrings(label, "", id as Label<out DbNamedexprorstmt>)
}
val loopId = loopIdMap[e.loop]
if (loopId == null) {
logger.warnElement(Severity.ErrorSevere, "Missing break/continue target", e)
return
}
tw.writeKtBreakContinueTarget(id, loopId)
}
}