How to get declaring KtFile/KtClass using KtCallExpression?

I’m using Kotlin compiler to analyze kotlin source file. I need to get enclosing element that defines function of KtCallExpression
For example, If I have this in a file called Main.kt:

fun main() {
    f1()
    Test.f2()
}

fun f1() {
}

class Test {
    companion object {
        fun f2() {
        }
    }
}

There would be 2 KtCallExpression in main(), one for f1() and other for f2(). How do I get File Main.kt from f1()? and How do I get Class Test from f2()?

1 Like

Plus, if the method is extension method, it should point to where the extension method is defined, and not to where the object being called is defined.

1 Like