Is there a way to get the scope of a function/variable in a compiler plugin?

Hi, in my compiler plugin, I need to get the scope of functions and variables. This should also say if they are defined inside a function.

e.g. for code like:
`
package compilerPlugin

object executeMain {
def main(args: Array[String]) {
def dosthg(): Unit ={
val cat = new Cat()
}
}
}`

I would like to get compilerPlugin.executeMain.main.dosthg.cat for the cat instance
and compilerPlugin.executeMain.main.dosthg for the function
At the moment, I found that using .symbol.fullNameString() gives compilerPlugin.executeMain.cat but is there a way to get the functions in the path too?

Give .owner a try. See https://docs.scala-lang.org/overviews/reflection/symbols-trees-types.html#the-symbol-owner-hierarchy

1 Like

It isn’t recognising .owner sadly. It gives "value owner is not a member of MyComponent.this.global.Tree
"

@Aliceravier owner belongs to Symbol, not Tree.

2 Likes

Thanks that worked. I ended up using symbol.owner.ownerChain to get the entire scope.

1 Like

Incidentally, is there a way to get the scope of a variable on the line it is initialised? At that point, it doesn’t have an owner yet it seems.

e.g. on line val kat = new Cat when matching it with
q"$mods val $tname: $tpt = new $classNm(...$exprss)"