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

Skip to content

Commit 3a0435b

Browse files
committed
Kotlin: Towards 'this' qualifiers
This probably handles most cases well enough
1 parent 9158f01 commit 3a0435b

2 files changed

Lines changed: 27 additions & 28 deletions

File tree

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

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,26 +1174,6 @@ open class KotlinFileExtractor(
11741174
return FieldResult(instanceId, instanceName)
11751175
}
11761176

1177-
private fun isQualifiedThis(vp: IrValueParameter): Boolean {
1178-
return isQualifiedThisFunction(vp) ||
1179-
isQualifiedThisClass(vp)
1180-
}
1181-
1182-
private fun isQualifiedThisFunction(vp: IrValueParameter): Boolean {
1183-
val parent = vp.parent
1184-
return vp.index == -1 &&
1185-
parent is IrFunction &&
1186-
parent.dispatchReceiverParameter == vp &&
1187-
parent.extensionReceiverParameter != null
1188-
}
1189-
1190-
private fun isQualifiedThisClass(vp: IrValueParameter): Boolean {
1191-
val parent = vp.parent
1192-
return vp.index == -1 &&
1193-
parent is IrClass &&
1194-
parent.thisReceiver == vp
1195-
}
1196-
11971177
fun extractValueParameter(vp: IrValueParameter, parent: Label<out DbCallable>, idx: Int) {
11981178
val id = useValueParameter(vp)
11991179
val type = useType(vp.type)
@@ -1895,12 +1875,31 @@ open class KotlinFileExtractor(
18951875
val type = useType(e.type)
18961876
val locId = tw.getLocation(e)
18971877
tw.writeExprs_thisaccess(id, type.javaResult.id, type.kotlinResult.id, exprParent.parent, exprParent.idx)
1898-
if (isQualifiedThis(owner)) {
1899-
// todo: add type access as child of 'id' at index 0
1900-
logger.warnElement(Severity.ErrorSevere, "TODO: Qualified this access found.", e)
1901-
}
19021878
tw.writeHasLocation(id, locId)
19031879
tw.writeCallableEnclosingExpr(id, callable)
1880+
1881+
val ownerParent = owner.parent
1882+
when(ownerParent) {
1883+
is IrFunction -> {
1884+
if (ownerParent.dispatchReceiverParameter == owner &&
1885+
ownerParent.extensionReceiverParameter != null) {
1886+
logger.warnElement(Severity.ErrorSevere, "Function-qualifier for this", e)
1887+
}
1888+
}
1889+
is IrClass -> {
1890+
if (ownerParent.thisReceiver == owner) {
1891+
val qualId = tw.getFreshIdLabel<DbUnannotatedtypeaccess>()
1892+
// TODO: Type arguments
1893+
val qualType = useSimpleTypeClass(ownerParent, listOf(), false)
1894+
tw.writeExprs_unannotatedtypeaccess(qualId, qualType.javaResult.id, qualType.kotlinResult.id, id, 0)
1895+
tw.writeHasLocation(qualId, locId)
1896+
tw.writeCallableEnclosingExpr(qualId, callable)
1897+
}
1898+
}
1899+
else -> {
1900+
logger.warnElement(Severity.ErrorSevere, "Unexpected owner parent for this access: " + ownerParent.javaClass, e)
1901+
}
1902+
}
19041903
} else {
19051904
val id = tw.getFreshIdLabel<DbVaraccess>()
19061905
val type = useType(e.type)

java/ql/test/kotlin/library-tests/this/this.expected

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
| this.kt:5:29:5:32 | this | file://:0:0:0:0 | <none> |
22
| this.kt:6:29:6:38 | this | file://:0:0:0:0 | <none> |
3-
| this.kt:7:29:7:38 | this | file://:0:0:0:0 | <none> |
3+
| this.kt:7:29:7:38 | Outer.this | this.kt:7:29:7:38 | Outer |
44
| this.kt:29:38:29:41 | this | file://:0:0:0:0 | <none> |
55
| this.kt:30:29:30:38 | this | file://:0:0:0:0 | <none> |
6-
| this.kt:31:29:31:38 | this | file://:0:0:0:0 | <none> |
6+
| this.kt:31:29:31:38 | Outer.this | this.kt:31:29:31:38 | Outer |
77
| this.kt:32:38:32:54 | this | file://:0:0:0:0 | <none> |
8-
| this.kt:37:13:37:22 | this | file://:0:0:0:0 | <none> |
8+
| this.kt:37:13:37:22 | Outer.this | this.kt:37:13:37:22 | Outer |
99
| this.kt:38:13:38:22 | this | file://:0:0:0:0 | <none> |
10-
| this.kt:39:13:39:30 | this | file://:0:0:0:0 | <none> |
10+
| this.kt:39:13:39:30 | Outer.this | this.kt:39:13:39:30 | Outer |
1111
| this.kt:40:13:40:30 | this | file://:0:0:0:0 | <none> |
1212
| this.kt:41:13:41:27 | this | file://:0:0:0:0 | <none> |
1313
| this.kt:42:13:42:35 | this | file://:0:0:0:0 | <none> |

0 commit comments

Comments
 (0)