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

Federico Tomassetti

04/01/2024, 8:48 AM
I would like to generate a class per package, when at least a class extending a certain base class is present in my package. Currently I am doing this in a
FirDeclarationGenerationExtension
. When I see a class extending the base class of interest in
getNestedClassifiersNames
then I wrote that down and the next time that
getTopLevelClassIds
is invoked a return my new class id.
Copy code
override fun getTopLevelClassIds(): Set<ClassId> {
        val packages =  mpNodeClassesEncountered.map { it.packageFqName() }.distinct()
        val topLevelValues = packages.map { ClassId(it, Name.identifier("Language${it.asString().replace(".", "_").capitalize()}")) }.toMutableSet()
        topLevelValues.removeAll(alreadyAddedClasses)
        topLevelValues.map { sli ->
            createTopLevelClass(sli, Key, classKind = ClassKind.OBJECT) {
                superType(StarLasuLanguage::class.classId.toConeType())
            }
        }
        alreadyAddedClasses.addAll(topLevelValues)
        return alreadyAddedClasses
    }
The problem is that reference to the object I generate are not resolved. The compiler behave as if the object does not exist. Any idea about what I am doing wrong?
f

Federico Tomassetti

04/02/2024, 9:35 AM
Thank you! Based on this discussion I changed my approach