Kotlin: Variable initialisers

This commit is contained in:
Ian Lynagh
2021-08-12 12:17:41 +01:00
parent f5e2826b9f
commit 3daec4376f
4 changed files with 50 additions and 16 deletions

View File

@@ -319,15 +319,19 @@ class KotlinFileExtractor(val tw: TrapWriter) {
}
}
fun extractVariable(v: IrVariable, parent: Label<out DbStmtparent>, idx: Int) {
val id = tw.getFreshIdLabel<DbLocalvariabledeclexpr>()
fun extractVariable(v: IrVariable, callable: Label<out DbCallable>) {
val id = tw.getFreshIdLabel<DbLocalvar>()
val locId = tw.getLocation(v.startOffset, v.endOffset)
val typeId = useType(v.type)
tw.writeExprs_localvariabledeclexpr(id, typeId, parent, idx)
val decId = tw.getFreshIdLabel<DbLocalvariabledeclexpr>()
tw.writeLocalvars(id, v.name.asString(), typeId, decId)
tw.writeHasLocation(id, locId)
val varId = tw.getFreshIdLabel<DbLocalvar>()
tw.writeLocalvars(varId, v.name.asString(), typeId, id)
tw.writeHasLocation(varId, locId)
tw.writeExprs_localvariabledeclexpr(decId, typeId, id, 0)
tw.writeHasLocation(id, locId)
val i = v.initializer
if(i != null) {
extractExpression(i, callable, decId, 0)
}
}
fun extractStatement(s: IrStatement, callable: Label<out DbCallable>, parent: Label<out DbStmtparent>, idx: Int) {
@@ -336,7 +340,7 @@ class KotlinFileExtractor(val tw: TrapWriter) {
extractExpression(s, callable, parent, idx)
}
is IrVariable -> {
extractVariable(s, parent, idx)
extractVariable(s, callable)
}
else -> {
extractorBug("Unrecognised IrStatement: " + s.javaClass)

View File

@@ -1,6 +1,6 @@
| variables.kt:0:0:0:0 | other | variables.kt:0:0:0:0 | Any |
| variables.kt:0:0:0:0 | other | variables.kt:0:0:0:0 | Any |
| variables.kt:2:1:8:1 | other | variables.kt:0:0:0:0 | Any |
| variables.kt:3:5:3:21 | prop | file://:0:0:0:0 | int |
| variables.kt:5:20:5:29 | param | file://:0:0:0:0 | int |
| variables.kt:6:9:6:21 | int local | file://:0:0:0:0 | int |
| variables.kt:0:0:0:0 | other | variables.kt:0:0:0:0 | Any | file://:0:0:0:0 | <none> |
| variables.kt:0:0:0:0 | other | variables.kt:0:0:0:0 | Any | file://:0:0:0:0 | <none> |
| variables.kt:2:1:8:1 | other | variables.kt:0:0:0:0 | Any | file://:0:0:0:0 | <none> |
| variables.kt:3:5:3:21 | prop | file://:0:0:0:0 | int | file://:0:0:0:0 | <none> |
| variables.kt:5:20:5:29 | param | file://:0:0:0:0 | int | file://:0:0:0:0 | <none> |
| variables.kt:6:9:6:25 | int local | file://:0:0:0:0 | int | variables.kt:6:21:6:25 | ... + ... |

View File

@@ -3,7 +3,7 @@ class Foo {
val prop: Int = 1
fun myFunction(param: Int) {
val local = 2
val local = 2 + 3
}
}

View File

@@ -1,5 +1,35 @@
import java
from Variable v
select v, v.getType()
newtype TMaybeExpr =
TExpr(Expr c) or
TNoExpr()
class MaybeExpr extends TMaybeExpr {
abstract string toString();
abstract Location getLocation();
}
class YesMaybeExpr extends MaybeExpr {
Expr c;
YesMaybeExpr() { this = TExpr(c) }
override string toString() { result = c.toString() }
override Location getLocation() { result = c.getLocation() }
}
class NoMaybeExpr extends MaybeExpr {
NoMaybeExpr() { this = TNoExpr() }
override string toString() { result = "<none>" }
override Location getLocation() { none() }
}
MaybeExpr initializer(Variable v) {
if exists(v.getInitializer())
then result = TExpr(v.getInitializer())
else result = TNoExpr()
}
from Variable v
select v, v.getType(), initializer(v)