Is there a way to get the compiler to behave like ...
# compiler
m
Is there a way to get the compiler to behave like the IDE? I'm working on a K2 Fir/Ir plugin, but while it works at compile-time, the IDE passes different types of Fir types around which break the code, and I can't simply return an emptySet as it will make generated fields not resolve. Code in thread.
Code:
Copy code
// Goal: Take an exposed dao Table and map its fields onto a dao Entity class
// e.g. 
// object MyTable : IntIdTable() {
//     val name = text("name")
// }
// @DaoCodegen(MyTable::class)
// class MyDao(id: EntityID<Int>) : IntEntity(id) {
//    companion object : IntEntityClass<MyDao>(MyTable)
// }
// Generates as:
// class MyDao(id: EntityID<Int>) : IntEntity(id) {
//    companion object : IntEntityClass<MyDao>(MyTable)
//    val name by MyTable.name
// }

@OptIn(DirectDeclarationsAccess::class, SymbolInternals::class, UnresolvedExpressionTypeAccess::class)
class DaoFirGenerator(session: FirSession) : FirDeclarationGenerationExtension(session) {
    override fun getCallableNamesForClass(classSymbol: FirClassSymbol<*>, context: MemberGenerationContext): Set<Name> {
        if (classSymbol.name.asString() == "DaoCodegen" || !classSymbol.annotations.filterIsInstance<FirAnnotationCall>().any { (it.calleeReference as? FirNamedReference)?.name?.asString() == "DaoCodegen" }) return emptySet()
        val annotation = classSymbol.annotations.filterIsInstance<FirAnnotationCall>().first { (it.calleeReference as? FirNamedReference)?.name?.asString() == "DaoCodegen" }

        val tableClassId = when (val arg = annotation.arguments.first()) {
            is FirLazyExpression -> {
                // TODO: IDE Support
                return emptySet()
            }
            is FirGetClassCall -> {
                val tableRef = (arg.argument as FirPropertyAccessExpression).calleeReference
                val import = classSymbol.getContainingFile()!!.imports.last { it.aliasName == tableRef.name  || it.importedFqName?.shortName() == tableRef.name }
                ClassId(import.importedFqName!!.parent(), tableRef.name)
            }
            else -> error(arg)
        }
        val tableSym = session.symbolProvider.getClassLikeSymbolByClassId(tableClassId) as FirClassSymbol<*>
        val fields = tableSym.declarationSymbols.filterIsInstance<FirPropertySymbol>()
        return fields.map { it.name }.toSet()
    }

    override fun generateProperties(callableId: CallableId, context: MemberGenerationContext?): List<FirPropertySymbol> {
        val classSymbol = context?.owner ?: return emptyList()
        if (classSymbol.name.asString() == "DaoCodegen" || !classSymbol.annotations.filterIsInstance<FirAnnotationCall>().any { (it.calleeReference as? FirNamedReference)?.name?.asString() == "DaoCodegen" }) return emptyList()
        val annotation = classSymbol.annotations.filterIsInstance<FirAnnotationCall>().first { (it.calleeReference as? FirNamedReference)?.name?.asString() == "DaoCodegen" }
        val classCall = annotation.arguments.first() as FirGetClassCall
        val tableSym = (classCall.argument as FirResolvedQualifier).qualifierSymbol as FirClassSymbol<*>
        val fields = tableSym.declarationSymbols.filterIsInstance<FirPropertySymbol>()
        val property = createMemberProperty(
            classSymbol,
            DaoGenerationKey,
            callableId.callableName,
            fields.first { it.name == callableId.callableName }.resolvedReturnType.typeArguments.first().type!!,
            isVal = false,
        )
        return listOf(property.symbol)
    }
}
y
Of course, running IDE tests would easily replicate this behavior. You can also use the Analysis API, which has its own test framework. I haven't explored this much, but the artifacts are published on https://redirector.kotlinlang.org/maven/intellij-dependencies/org/jetbrains/kotlin/ This test framework is similar to the
internal-test-framework
for the compiler. You can find examples of using it in the Kotlin repository.
h
What do you mean with different types? The Kotlin compiler uses multiple phases, and the IDE only uses some of them.
y
I think the problem is in how you access annotations. I think some kind of
lazyResolveToPhase
call is needed or something I'd recommend looking at the code for the Serialization plugin, since it might do similar things
m
Running this as part of a compiler plugin always gives a FirGetClassCall, while the IDE seems to always give a FirLazyExpression with a lot of fields not yet resolved, so I'm not sure how to make the IDE sync up with the compiler. If I just return an emptySet, it never gets called again with a FirGetClassCall so the additional properties are never resolved in the IDE
y
The IDE (or more correctly, Analysis API) implementation indeed has different behavior since it does things lazily.
lazyResolveToPhase
does the trick sometimes.
m
unfortunately all of classSymbol, annotation and arg don't extend FirElementWithResolveState so lazyResolveToPhase won't help.
y
Maybe
classSymbol.fir.lazyResolveToPhase
?
m
resolving that to any stage doesn't seem to resolve the annotation values
And skipping past that step and returning a dummy value, apparently some methods don't even exist in the IDE:
java.lang.NoSuchMethodError: 'org.jetbrains.kotlin.fir.symbols.impl.FirClassLikeSymbol org.jetbrains.kotlin.fir.expressions.FirResolvedQualifier.getQualifierSymbol()'
y
Welp, you were bound to run into that eventually! The IDE uses dev versions of the compiler. Not only that, it uses its own branched-off versions of it. Hence, there's ABI differences between the version you're compiling for and the IDE There's no easy way to deal with this. One approach is to change your behavior at runtime. Metro has a good setup for that.
m
Got everything set up now, except for the fact that
annotation.arguments[0]
still resolves to a FirLazyExpression with seemingly no way to evaluate it to the type. I could maybe parse the result of
getElementTextInContextForDebug
and manually look up the class by the string in the
<ELEMENT>
tag, but surely there has to be a better way.
and now during compile time some fields end up as FirImplicitTypeRefImplWithoutSource instead of FirResolvedTypeRef for some reason?
n
KDoc here explains what's going on in general github.com/JetBrains/kotlin/blob/…/FirResolvePhase.kt . Plus Kdoc here github.com/JetBrains/kotlin/blob/…/FirDeclarationGenerationExtension.kt#… to know at what phase each function can be called. But from looking at the code, i think registering your DaoCodegen as annotation predicate can solve the problem, like done here github.com/JetBrains/kotlin/blob/…/AdditionalMembersGenerator.kt#…. It will make compiler resolve annotation argument at earlier COMPILER_REQUIRED_ANNOTATIONS phase
thank you color 2
y
TIL that
COMPILER_REQUIRED_ANNOTATIONS
includes plugin annotations! I might be wrong, but it doesn't seem like annotation arguments are guaranteed to be resolved in CLI compiler. The relevant code only resolves annotation arguments early for specific compiler ones. However, on IDE (and in AA in general), it intentionally gets resolved in
COMPILER_REQUIRED_ANNOTATIONS
explicitly for compiler plugins. But yes, either way, you must register an annotation predicate for it to get picked up on, as the code path is gated behind
annotationsFromPlugins
, which I've traced back to
registerPredicates
n
Oops, i got confidently wrong after my eye caught KDoc of
COMPILER_REQUIRED_ANNOTATIONS
. It indeed only resolves arguments of a few built in annotations