Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 286e29c

Browse files
committed
Kotlin: Add exprstmt's where appropriate
1 parent 8704536 commit 286e29c

2 files changed

Lines changed: 112 additions & 98 deletions

File tree

java/kotlin-extractor/src/main/kotlin/KotlinExtractorExtension.kt

Lines changed: 111 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -789,10 +789,13 @@ class X {
789789
continue
790790
}
791791

792+
val declLocId = tw.getLocation(decl)
793+
val stmtId = tw.getFreshIdLabel<DbExprstmt>()
794+
tw.writeStmts_exprstmt(stmtId, blockId, idx++, obinitId)
795+
tw.writeHasLocation(stmtId, declLocId)
792796
val assignmentId = tw.getFreshIdLabel<DbAssignexpr>()
793797
val type = useType(initializer.expression.type)
794-
val declLocId = tw.getLocation(decl)
795-
tw.writeExprs_assignexpr(assignmentId, type.javaResult.id, type.kotlinResult.id, blockId, idx++)
798+
tw.writeExprs_assignexpr(assignmentId, type.javaResult.id, type.kotlinResult.id, stmtId, 0)
796799
tw.writeHasLocation(assignmentId, declLocId)
797800

798801
val lhsId = tw.getFreshIdLabel<DbVaraccess>()
@@ -802,7 +805,7 @@ class X {
802805
val vId = useProperty(decl) // todo: fix this. We should be assigning the field, and not the property
803806
tw.writeVariableBinding(lhsId, vId)
804807

805-
extractExpression(initializer.expression, obinitId, assignmentId, 1)
808+
extractExpressionExpr(initializer.expression, obinitId, assignmentId, 1)
806809
}
807810
is IrAnonymousInitializer -> {
808811
if (decl.isStatic) {
@@ -926,14 +929,14 @@ class X {
926929
tw.writeHasLocation(stmtId, locId)
927930
val i = v.initializer
928931
if(i != null) {
929-
extractExpression(i, callable, exprId, 0)
932+
extractExpressionExpr(i, callable, exprId, 0)
930933
}
931934
}
932935

933936
fun extractStatement(s: IrStatement, callable: Label<out DbCallable>, parent: Label<out DbStmtparent>, idx: Int) {
934937
when(s) {
935938
is IrExpression -> {
936-
extractExpression(s, callable, parent, idx)
939+
extractExpressionStmt(s, callable, parent, idx)
937940
}
938941
is IrVariable -> {
939942
extractVariable(s, callable, parent, idx)
@@ -1047,12 +1050,12 @@ class X {
10471050
}
10481051
val dr = c.dispatchReceiver
10491052
if(dr != null) {
1050-
extractExpression(dr, callable, exprId, -1)
1053+
extractExpressionExpr(dr, callable, exprId, -1)
10511054
}
10521055
for(i in 0 until c.valueArgumentsCount) {
10531056
val arg = c.getValueArgument(i)
10541057
if(arg != null) {
1055-
extractExpression(arg, callable, exprId, i)
1058+
extractExpressionExpr(arg, callable, exprId, i)
10561059
}
10571060
}
10581061
}
@@ -1088,12 +1091,12 @@ class X {
10881091
for (i in 0 until e.valueArgumentsCount) {
10891092
val arg = e.getValueArgument(i)
10901093
if (arg != null) {
1091-
extractExpression(arg, callable, id, i)
1094+
extractExpressionExpr(arg, callable, id, i)
10921095
}
10931096
}
10941097
val dr = e.dispatchReceiver
10951098
if (dr != null) {
1096-
extractExpression(dr, callable, id, -2)
1099+
extractExpressionExpr(dr, callable, id, -2)
10971100
}
10981101

10991102
if (e.typeArgumentsCount > 0) {
@@ -1107,29 +1110,8 @@ class X {
11071110

11081111
private var currentFunction: IrFunction? = null
11091112

1110-
fun extractExpression(e: IrExpression, callable: Label<out DbCallable>, parent: Label<out DbExprparent>, idx: Int) {
1113+
fun extractExpressionStmt(e: IrExpression, callable: Label<out DbCallable>, parent: Label<out DbStmtparent>, idx: Int) {
11111114
when(e) {
1112-
is IrInstanceInitializerCall -> {
1113-
val irCallable = currentFunction
1114-
if (irCallable == null) {
1115-
logger.warnElement(Severity.ErrorSevere, "Current function is not set", e)
1116-
return
1117-
}
1118-
1119-
if (irCallable is IrConstructor && irCallable.isPrimary) {
1120-
// Todo add parameter to field assignments
1121-
}
1122-
1123-
// Add call to <obinit>:
1124-
val id = tw.getFreshIdLabel<DbMethodaccess>()
1125-
val type = useType(e.type)
1126-
val locId = tw.getLocation(e)
1127-
var methodLabel = getFunctionLabel(irCallable.parent, "<obinit>", listOf(), e.type)
1128-
val methodId = tw.getLabelFor<DbMethod>(methodLabel)
1129-
tw.writeExprs_methodaccess(id, type.javaResult.id, type.kotlinResult.id, parent, idx)
1130-
tw.writeHasLocation(id, locId)
1131-
tw.writeCallableBinding(id, methodId)
1132-
}
11331115
is IrDelegatingConstructorCall -> {
11341116
val irCallable = currentFunction
11351117
if (irCallable == null) {
@@ -1158,16 +1140,108 @@ class X {
11581140
for (i in 0 until e.valueArgumentsCount) {
11591141
val arg = e.getValueArgument(i)
11601142
if (arg != null) {
1161-
extractExpression(arg, callable, id, i)
1143+
extractExpressionExpr(arg, callable, id, i)
11621144
}
11631145
}
11641146
val dr = e.dispatchReceiver
11651147
if (dr != null) {
1166-
extractExpression(dr, callable, id, -1)
1148+
extractExpressionExpr(dr, callable, id, -1)
11671149
}
11681150

11691151
// todo: type arguments at index -2, -3, ...
11701152
}
1153+
is IrThrow -> {
1154+
val id = tw.getFreshIdLabel<DbThrowstmt>()
1155+
val locId = tw.getLocation(e)
1156+
tw.writeStmts_throwstmt(id, parent, idx, callable)
1157+
tw.writeHasLocation(id, locId)
1158+
extractExpressionExpr(e.value, callable, id, 0)
1159+
}
1160+
is IrBreak -> {
1161+
val id = tw.getFreshIdLabel<DbBreakstmt>()
1162+
tw.writeStmts_breakstmt(id, parent, idx, callable)
1163+
extractBreakContinue(e, id)
1164+
}
1165+
is IrContinue -> {
1166+
val id = tw.getFreshIdLabel<DbContinuestmt>()
1167+
tw.writeStmts_continuestmt(id, parent, idx, callable)
1168+
extractBreakContinue(e, id)
1169+
}
1170+
is IrReturn -> {
1171+
val id = tw.getFreshIdLabel<DbReturnstmt>()
1172+
val locId = tw.getLocation(e)
1173+
tw.writeStmts_returnstmt(id, parent, idx, callable)
1174+
tw.writeHasLocation(id, locId)
1175+
extractExpressionExpr(e.value, callable, id, 0)
1176+
}
1177+
is IrContainerExpression -> {
1178+
val id = tw.getFreshIdLabel<DbBlock>()
1179+
val locId = tw.getLocation(e)
1180+
tw.writeStmts_block(id, parent, idx, callable)
1181+
tw.writeHasLocation(id, locId)
1182+
e.statements.forEachIndexed { i, s ->
1183+
extractStatement(s, callable, id, i)
1184+
}
1185+
}
1186+
is IrWhileLoop -> {
1187+
val id = tw.getFreshIdLabel<DbWhilestmt>()
1188+
loopIdMap[e] = id
1189+
val locId = tw.getLocation(e)
1190+
tw.writeStmts_whilestmt(id, parent, idx, callable)
1191+
tw.writeHasLocation(id, locId)
1192+
extractExpressionExpr(e.condition, callable, id, 0)
1193+
val body = e.body
1194+
if(body != null) {
1195+
extractExpressionStmt(body, callable, id, 1)
1196+
}
1197+
loopIdMap.remove(e)
1198+
}
1199+
is IrDoWhileLoop -> {
1200+
val id = tw.getFreshIdLabel<DbDostmt>()
1201+
loopIdMap[e] = id
1202+
val locId = tw.getLocation(e)
1203+
tw.writeStmts_dostmt(id, parent, idx, callable)
1204+
tw.writeHasLocation(id, locId)
1205+
extractExpressionExpr(e.condition, callable, id, 0)
1206+
val body = e.body
1207+
if(body != null) {
1208+
extractExpressionStmt(body, callable, id, 1)
1209+
}
1210+
loopIdMap.remove(e)
1211+
}
1212+
else -> {
1213+
val id = tw.getFreshIdLabel<DbExprstmt>()
1214+
val locId = tw.getLocation(e)
1215+
tw.writeStmts_exprstmt(id, parent, idx, callable)
1216+
tw.writeHasLocation(id, locId)
1217+
extractExpressionExpr(e, callable, id, 0)
1218+
}
1219+
}
1220+
}
1221+
1222+
fun extractExpressionExpr(e: IrExpression, callable: Label<out DbCallable>, parent: Label<out DbExprparent>, idx: Int) {
1223+
when(e) {
1224+
is IrInstanceInitializerCall -> {
1225+
val irCallable = currentFunction
1226+
if (irCallable == null) {
1227+
logger.warnElement(Severity.ErrorSevere, "Current function is not set", e)
1228+
return
1229+
}
1230+
1231+
if (irCallable is IrConstructor && irCallable.isPrimary) {
1232+
// Todo add parameter to field assignments
1233+
}
1234+
1235+
// Add call to <obinit>:
1236+
val id = tw.getFreshIdLabel<DbMethodaccess>()
1237+
val type = useType(e.type)
1238+
val locId = tw.getLocation(e)
1239+
var methodLabel = getFunctionLabel(irCallable.parent, "<obinit>", listOf(), e.type)
1240+
val methodId = tw.getLabelFor<DbMethod>(methodLabel)
1241+
tw.writeExprs_methodaccess(id, type.javaResult.id, type.kotlinResult.id, parent, idx)
1242+
tw.writeHasLocation(id, locId)
1243+
tw.writeCallableBinding(id, methodId)
1244+
}
11711245
is IrConstructorCall -> {
11721246
extractConstructorCall(e, parent, idx, callable)
11731247
}
@@ -1277,66 +1351,7 @@ class X {
12771351
val vId = useValueDeclaration(e.symbol.owner)
12781352
tw.writeVariableBinding(lhsId, vId)
12791353

1280-
extractExpression(e.value, callable, id, 1)
1281-
}
1282-
is IrThrow -> {
1283-
val id = tw.getFreshIdLabel<DbThrowstmt>()
1284-
val locId = tw.getLocation(e)
1285-
tw.writeStmts_throwstmt(id, parent, idx, callable)
1286-
tw.writeHasLocation(id, locId)
1287-
extractExpression(e.value, callable, id, 0)
1288-
}
1289-
is IrBreak -> {
1290-
val id = tw.getFreshIdLabel<DbBreakstmt>()
1291-
tw.writeStmts_breakstmt(id, parent, idx, callable)
1292-
extractBreakContinue(e, id)
1293-
}
1294-
is IrContinue -> {
1295-
val id = tw.getFreshIdLabel<DbContinuestmt>()
1296-
tw.writeStmts_continuestmt(id, parent, idx, callable)
1297-
extractBreakContinue(e, id)
1298-
}
1299-
is IrReturn -> {
1300-
val id = tw.getFreshIdLabel<DbReturnstmt>()
1301-
val locId = tw.getLocation(e)
1302-
tw.writeStmts_returnstmt(id, parent, idx, callable)
1303-
tw.writeHasLocation(id, locId)
1304-
extractExpression(e.value, callable, id, 0)
1305-
}
1306-
is IrContainerExpression -> {
1307-
val id = tw.getFreshIdLabel<DbBlock>()
1308-
val locId = tw.getLocation(e)
1309-
tw.writeStmts_block(id, parent, idx, callable)
1310-
tw.writeHasLocation(id, locId)
1311-
e.statements.forEachIndexed { i, s ->
1312-
extractStatement(s, callable, id, i)
1313-
}
1314-
}
1315-
is IrWhileLoop -> {
1316-
val id = tw.getFreshIdLabel<DbWhilestmt>()
1317-
loopIdMap[e] = id
1318-
val locId = tw.getLocation(e)
1319-
tw.writeStmts_whilestmt(id, parent, idx, callable)
1320-
tw.writeHasLocation(id, locId)
1321-
extractExpression(e.condition, callable, id, 0)
1322-
val body = e.body
1323-
if(body != null) {
1324-
extractExpression(body, callable, id, 1)
1325-
}
1326-
loopIdMap.remove(e)
1327-
}
1328-
is IrDoWhileLoop -> {
1329-
val id = tw.getFreshIdLabel<DbDostmt>()
1330-
loopIdMap[e] = id
1331-
val locId = tw.getLocation(e)
1332-
tw.writeStmts_dostmt(id, parent, idx, callable)
1333-
tw.writeHasLocation(id, locId)
1334-
extractExpression(e.condition, callable, id, 0)
1335-
val body = e.body
1336-
if(body != null) {
1337-
extractExpression(body, callable, id, 1)
1338-
}
1339-
loopIdMap.remove(e)
1354+
extractExpressionExpr(e.value, callable, id, 1)
13401355
}
13411356
is IrWhen -> {
13421357
val id = tw.getFreshIdLabel<DbWhenexpr>()
@@ -1352,8 +1367,8 @@ class X {
13521367
val bLocId = tw.getLocation(b)
13531368
tw.writeWhen_branch(bId, id, i)
13541369
tw.writeHasLocation(bId, bLocId)
1355-
extractExpression(b.condition, callable, bId, 0)
1356-
extractExpression(b.result, callable, bId, 1)
1370+
extractExpressionExpr(b.condition, callable, bId, 0)
1371+
extractExpressionStmt(b.result, callable, bId, 1)
13571372
if(b is IrElseBranch) {
13581373
tw.writeWhen_branch_else(bId)
13591374
}
@@ -1365,7 +1380,7 @@ class X {
13651380
val type = useType(e.type)
13661381
tw.writeExprs_getclassexpr(id, type.javaResult.id, type.kotlinResult.id, parent, idx)
13671382
tw.writeHasLocation(id, locId)
1368-
extractExpression(e.argument, callable, id, 0)
1383+
extractExpressionExpr(e.argument, callable, id, 0)
13691384
}
13701385
else -> {
13711386
logger.warnElement(Severity.ErrorSevere, "Unrecognised IrExpression: " + e.javaClass, e)

java/ql/lib/config/semmlecode.dbscheme

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,8 +535,7 @@ stmts(
535535
int bodydecl: @callable ref
536536
);
537537

538-
// @stmtparent = @callable | @stmt | @switchexpr;
539-
@stmtparent = @exprparent;
538+
@stmtparent = @callable | @stmt | @switchexpr | @whenbranch;
540539

541540
case @stmt.kind of
542541
0 = @block

0 commit comments

Comments
 (0)