Kotlin: Add support for try statements

This commit is contained in:
Ian Lynagh
2021-10-29 19:57:45 +01:00
parent 6b5663df46
commit cbd265ab7a
4 changed files with 57 additions and 4 deletions

View File

@@ -1000,17 +1000,22 @@ class X {
}
fun extractVariable(v: IrVariable, callable: Label<out DbCallable>, parent: Label<out DbStmtparent>, idx: Int) {
val stmtId = tw.getFreshIdLabel<DbLocalvariabledeclstmt>()
val locId = tw.getLocation(v)
tw.writeStmts_localvariabledeclstmt(stmtId, parent, idx, callable)
tw.writeHasLocation(stmtId, locId)
extractVariableExpr(v, callable, stmtId, 1)
}
fun extractVariableExpr(v: IrVariable, callable: Label<out DbCallable>, parent: Label<out DbExprparent>, idx: Int) {
val varId = useVariable(v)
val exprId = tw.getFreshIdLabel<DbLocalvariabledeclexpr>()
val stmtId = tw.getFreshIdLabel<DbLocalvariabledeclstmt>()
val locId = tw.getLocation(v)
val type = useType(v.type)
tw.writeLocalvars(varId, v.name.asString(), type.javaResult.id, exprId) // TODO: KT type
tw.writeHasLocation(varId, locId)
tw.writeExprs_localvariabledeclexpr(exprId, type.javaResult.id, type.kotlinResult.id, stmtId, 1)
tw.writeExprs_localvariabledeclexpr(exprId, type.javaResult.id, type.kotlinResult.id, parent, idx)
tw.writeHasLocation(exprId, locId)
tw.writeStmts_localvariabledeclstmt(stmtId, parent, idx, callable)
tw.writeHasLocation(stmtId, locId)
val i = v.initializer
if(i != null) {
extractExpressionExpr(i, callable, exprId, 0)
@@ -1303,6 +1308,25 @@ class X {
tw.writeHasLocation(id, locId)
extractExpressionExpr(e.value, callable, id, 0)
}
is IrTry -> {
val stmtParent = parent.stmt(e, callable)
val id = tw.getFreshIdLabel<DbTrystmt>()
val locId = tw.getLocation(e)
tw.writeStmts_trystmt(id, stmtParent.parent, stmtParent.idx, callable)
tw.writeHasLocation(id, locId)
extractExpressionExpr(e.tryResult, callable, id, -1)
val finallyStmt = e.finallyExpression
if(finallyStmt != null) {
extractExpressionExpr(finallyStmt, callable, id, -2)
}
for((catchIdx, catchClause) in e.catches.withIndex()) {
val catchId = tw.getFreshIdLabel<DbCatchclause>()
tw.writeStmts_catchclause(catchId, id, catchIdx, callable)
// TODO: Index -1: unannotatedtypeaccess
extractVariableExpr(catchClause.catchParameter, callable, catchId, 0)
extractExpressionExpr(catchClause.result, callable, catchId, 1)
}
}
is IrContainerExpression -> {
val stmtParent = parent.stmt(e, callable)
val id = tw.getFreshIdLabel<DbBlock>()

View File

@@ -56,3 +56,11 @@
| stmts.kt:28:11:28:11 | x | VarAccess |
| stmts.kt:28:11:28:15 | ... > ... | GTExpr |
| stmts.kt:28:15:28:15 | y | VarAccess |
| stmts.kt:33:9:35:5 | <Stmt> | StmtExpr |
| stmts.kt:34:15:34:30 | new Exception(...) | ClassInstanceExpr |
| stmts.kt:34:26:34:28 | Foo | StringLiteral |
| stmts.kt:36:12:36:23 | e | LocalVariableDeclExpr |
| stmts.kt:36:26:38:5 | <Stmt> | StmtExpr |
| stmts.kt:37:16:37:16 | 1 | IntegerLiteral |
| stmts.kt:39:13:41:5 | <Stmt> | StmtExpr |
| stmts.kt:40:16:40:16 | 2 | IntegerLiteral |

View File

@@ -1,3 +1,4 @@
| file://:0:0:0:0 | catch (...) | CatchClause |
| stmts.kt:2:41:20:1 | { ... } | BlockStmt |
| stmts.kt:3:5:6:5 | <Expr>; | ExprStmt |
| stmts.kt:3:15:4:5 | { ... } | BlockStmt |
@@ -32,3 +33,11 @@
| stmts.kt:25:24:25:33 | break | BreakStmt |
| stmts.kt:28:5:29:16 | while (...) | WhileStmt |
| stmts.kt:29:9:29:16 | continue | ContinueStmt |
| stmts.kt:32:23:42:1 | { ... } | BlockStmt |
| stmts.kt:33:5:41:5 | try ... | TryStmt |
| stmts.kt:33:9:35:5 | { ... } | BlockStmt |
| stmts.kt:34:9:34:30 | throw ... | ThrowStmt |
| stmts.kt:36:26:38:5 | { ... } | BlockStmt |
| stmts.kt:37:9:37:16 | return ... | ReturnStmt |
| stmts.kt:39:13:41:5 | { ... } | BlockStmt |
| stmts.kt:40:9:40:16 | return ... | ReturnStmt |

View File

@@ -28,3 +28,15 @@ fun loops(x: Int, y: Int) {
while(x > y)
continue
}
fun exceptions(): Int {
try {
throw Exception("Foo")
}
catch (e: Exception) {
return 1
}
finally {
return 2
}
}