Kotlin: Fix extraction of dispatch and extension receivers

It is possible for a call to have both, e.g. the `arg.ext()` call in:

class Class1 {
    val y = 4
}
class Class2 (val arg:Class1) {
    val x = 3
    fun someFun() {
        arg.ext();
    }
    fun Class1.ext() {
        val z = x + y
    }
}
This commit is contained in:
Ian Lynagh
2021-11-29 12:58:03 +00:00
parent 76d7ac9898
commit 75e22da096
2 changed files with 11 additions and 11 deletions

View File

@@ -683,20 +683,17 @@ open class KotlinFileExtractor(
tw.writeStatementEnclosingExpr(id, enclosingStmt)
if (extractTypeArguments) {
// type arguments at index -2, -3, ...
extractTypeArguments(c, id, callable, enclosingStmt, -2, true)
// type arguments at index -3, -4, ...
extractTypeArguments(c, id, callable, enclosingStmt, -3, true)
}
val dr = c.dispatchReceiver
val er = c.extensionReceiver
if (dr != null) {
extractExpressionExpr(dr, callable, id, -1, enclosingStmt)
if (er != null && er != dr) {
logger.warnElement(Severity.ErrorSevere, "Expected to only find extension receiver or dispatch receiver. Found both. Extracting dispatch receiver only", c)
}
} else if (er != null) {
extractExpressionExpr(er, callable, id, -1, enclosingStmt)
}
if (er != null) {
extractExpressionExpr(er, callable, id, -2, enclosingStmt)
}
for(i in 0 until c.valueArgumentsCount) {
@@ -967,7 +964,7 @@ open class KotlinFileExtractor(
if (isBuiltinCallKotlin(c, "arrayOf")) {
if (c.typeArgumentsCount == 1) {
extractTypeArguments(c, id, callable, enclosingStmt,-1)
extractTypeArguments(c, id, callable, enclosingStmt, -1)
} else {
logger.warnElement( Severity.ErrorSevere, "Expected to find one type argument in arrayOf call", c )
}

View File

@@ -1741,6 +1741,9 @@ class MethodAccess extends Expr, Call, @methodaccess {
/** Holds if this method access has a qualifier. */
predicate hasQualifier() { exists(this.getQualifier()) }
/** Gets the extension receiver expression of this method access, if any. */
Expr getExtensionReceiver() { result.isNthChildOf(this, -2) }
/** Gets an argument supplied to the method that is invoked using this method access. */
override Expr getAnArgument() { result.getIndex() >= 0 and result.getParent() = this }
@@ -1748,12 +1751,12 @@ class MethodAccess extends Expr, Call, @methodaccess {
override Expr getArgument(int index) { exprs(result, _, _, _, this, index) and index >= 0 }
/** Gets a type argument supplied as part of this method access, if any. */
Expr getATypeArgument() { result.getIndex() <= -2 and result.getParent() = this }
Expr getATypeArgument() { result.getIndex() <= -3 and result.getParent() = this }
/** Gets the type argument at the specified (zero-based) position in this method access, if any. */
Expr getTypeArgument(int index) {
result = this.getATypeArgument() and
(-2 - result.getIndex()) = index
(-3 - result.getIndex()) = index
}
/** Gets the method accessed by this method access. */