https://kotlinlang.org logo
#compiler
Title
# compiler
t

Tóth István Zoltán

02/25/2024, 6:57 AM
I would like to create a new top level class in FIR whenever I see an interface that extends another interface. Like this:
Copy code
interface Test : Service
And the class I would like to generate:
Copy code
class Test$Impl : Test { }
I tried to figure out how to use
FirDeclarationGenerationExtension
for this but I couldn't.
getTopLevelClassIds
is called very early, and I don't know how to figure out which interfaces should I process. Any pointers? Thanks in advance.
j

Javier

02/25/2024, 9:46 AM
I think you need to use annotations for this.
If nothing has changed, everything around the generation extension needs annotations
thank you color 1
t

Tóth István Zoltán

02/26/2024, 5:37 AM
Thanks, I was afraid that's the case. I'll change to annotations. I really don't like them, they make the code hard to read and move information to the wrong place, but this is how it is.
On the other hand I really don't see what's the difference between an annotation and an interface at this point. I guess I could just figure out how the compiler checks the annotations and do the same for the interfaces. Maybe it's too much work...
d

dmitriy.novozhilov

02/26/2024, 6:27 AM
I recently described the reasoning behind this design
t

Tóth István Zoltán

02/26/2024, 7:15 AM
Thanks, the description of phases in that description is very useful. The problem is that
getTopLevelClassIds
is called very early and I can't figure out how to collect the classes I want to process. In ExternalClassGenerator of the fir plugin prototype a predicate is used but when I tried it I got an empty result. I also checked the cache it uses with debugger and it was empty. From FirDeclarationGenerationExtension: • the
getTopLevelClassIds
method is called AFTER the SUPERTYPES phase • the
generateTopLevelClassLikeDeclaration
method is called ON the SUPERTYPES phase I don't understand how would this work.
d

dmitriy.novozhilov

02/26/2024, 7:19 AM
From `FirDeclarationGenerationExtension`:
• the
getTopLevelClassIds
method is called AFTER the SUPERTYPES phase
• the
generateTopLevelClassLikeDeclaration
method is called ON the SUPERTYPES phase
It's a problem in the documentation. This comment about "after supertypes" is about methods about callables
getTopLevelClassIds
will be called on the supertypes stage
The problem is that
getTopLevelClassIds
is called very early and I can't figure out how to collect the classes I want to process.
Only using predicates
In ExternalClassGenerator of the fir plugin prototype a predicate is used but when I tried it I got an empty result. I also checked the cache it uses with debugger and it was empty.
That's strange Did you register your predicates in the extension itself?
t

Tóth István Zoltán

02/26/2024, 7:22 AM
Yes, I think:
Copy code
override fun FirDeclarationPredicateRegistrar.registerPredicates() {
        register(PREDICATE)
    }
d

dmitriy.novozhilov

02/26/2024, 7:24 AM
Are your annotations listed here during debugging?
t

Tóth István Zoltán

02/26/2024, 7:37 AM
Yes:
As I see it calls FirPredicateBasedProviderImpl.kt but the cache is empty. I'm using the standard plugin test framework btw.