I'm working on a compiler plugin (mostly just expe...
# compiler
d
I'm working on a compiler plugin (mostly just experimenting and learning things at the moment). I want to generate some nested classes, which I have managed for one so far. The code looks like this
Copy code
class MyModel : Model() {
    val foo by IntField()

    // this is generated by my plugin:
    interface EntityI {
        val foo: Int
    }
}
Now I also want to generate the following companion object:
Copy code
companion object : ModelCompanion<MyModel, EntityI>
I can create the companion object using
SyntheticResolveExtension.getSyntheticCompanionObjectNameIfNeeded
, however inside
addSyntheticSupertypes
for the companion object I cannot add the supertype, because
EntityI
is not yet resolvable as a descriptor (trying to do so will report a recursion, which does make sense). I understand I need to somehow create a lazy KotlinType (which seems to exist), but I have no idea how I would create it with what I have available in
addSyntheticSupertypes
. Any advice?
s
Hey, I think you theoretically can create class descriptor before it is requested by compiler, so that you will already have a class to get type from. Essentially, I would do something like a hashmap from outer class to entity descriptor, use it in super types and then provide the same descriptor when requested by compiler methods.
d
Thank you, I will try that
m
To anyone struggling with this, there's a method to basically search for a class descriptor in your whole classpath without having to create it from scratch:
Copy code
override fun addSyntheticSupertypes(thisDescriptor: ClassDescriptor, supertypes: MutableList<KotlinType>) {
        val classDescriptor = thisDescriptor.module
                .findClassAcrossModuleDependencies(ClassId(FqName("<package>"), Name.identifier("<className>")))
}