martmists
10/25/2026, 3:38 PMmartmists
10/25/2026, 3:52 PM// 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)
}
}Youssef Shoaib [MOD]
10/25/2026, 4:25 PMinternal-test-framework for the compiler. You can find examples of using it in the Kotlin repository.hfhbd
10/25/2026, 4:47 PMYoussef Shoaib [MOD]
10/25/2026, 4:50 PMlazyResolveToPhase call is needed or something
I'd recommend looking at the code for the Serialization plugin, since it might do similar thingsmartmists
10/25/2026, 4:50 PMYoussef Shoaib [MOD]
10/25/2026, 4:53 PMlazyResolveToPhase does the trick sometimes.martmists
10/25/2026, 4:54 PMYoussef Shoaib [MOD]
10/25/2026, 7:07 PMclassSymbol.fir.lazyResolveToPhase?martmists
10/25/2026, 7:50 PMmartmists
10/25/2026, 7:57 PMjava.lang.NoSuchMethodError: 'org.jetbrains.kotlin.fir.symbols.impl.FirClassLikeSymbol org.jetbrains.kotlin.fir.expressions.FirResolvedQualifier.getQualifierSymbol()'Youssef Shoaib [MOD]
10/25/2026, 8:19 PMmartmists
10/25/2026, 11:27 PMannotation.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.martmists
10/26/2026, 1:46 PMNikita Klimenko [JB]
10/27/2026, 12:00 AMYoussef Shoaib [MOD]
10/27/2026, 3:08 PMCOMPILER_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 registerPredicatesNikita Klimenko [JB]
10/27/2026, 4:13 PMCOMPILER_REQUIRED_ANNOTATIONS . It indeed only resolves arguments of a few built in annotations