# How to get declaring KtFile/KtClass using KtCallExpression?

**URL:** <https://discuss.kotlinlang.org/t/how-to-get-declaring-ktfile-ktclass-using-ktcallexpression/26271>\
**Category:** Libraries\
**Created:** [January 25, 2023, 4:53am UTC](https://discuss.kotlinlang.org/t/how-to-get-declaring-ktfile-ktclass-using-ktcallexpression/26271 "2023-01-25T04:53:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tmvkrpxl0](https://avatars.discourse-cdn.com/v4/letter/t/9de0a6/32.png) [@tmvkrpxl0](https://discuss.kotlinlang.org/u/tmvkrpxl0)\
**Post date:** [January 25, 2023, 4:53am UTC](https://discuss.kotlinlang.org/t/how-to-get-declaring-ktfile-ktclass-using-ktcallexpression/26271/1 "2023-01-25T04:53:38Z")

</div>

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`:

```plaintext
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()`?

---

<div class="post-metadata">

**Author:** ![tmvkrpxl0](https://avatars.discourse-cdn.com/v4/letter/t/9de0a6/32.png) [@tmvkrpxl0](https://discuss.kotlinlang.org/u/tmvkrpxl0)\
**Post date:** [January 25, 2023, 5:45am UTC](https://discuss.kotlinlang.org/t/how-to-get-declaring-ktfile-ktclass-using-ktcallexpression/26271/3 "2023-01-25T05:45:20Z")

</div>

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.
