[FIR Plugin prototype] Use new DSL for generating declarations

This commit is contained in:
Dmitriy Novozhilov
2022-12-22 14:29:55 +02:00
committed by Space Team
parent 42db0b14f0
commit 3744878b6d
19 changed files with 82 additions and 300 deletions
@@ -6,4 +6,4 @@ open fun hashCode(): kotlin.Int
open fun toString(): kotlin.String
constructor()
private constructor()
@@ -144,6 +144,6 @@ KtConstructorSymbol:
symbolKind: CLASS_MEMBER
typeParameters: []
valueParameters: []
visibility: Public
visibility: Private
getContainingModule: KtSourceModule "Sources of main"
deprecationStatus: null
deprecationStatus: null
@@ -9,6 +9,7 @@ dependencies {
compileOnly(project(":compiler:fir:cones"))
compileOnly(project(":compiler:fir:tree"))
compileOnly(project(":compiler:fir:resolve"))
compileOnly(project(":compiler:fir:plugin-utils"))
compileOnly(project(":compiler:fir:checkers"))
compileOnly(project(":compiler:fir:fir2ir"))
compileOnly(project(":compiler:ir.backend.common"))
@@ -24,6 +25,7 @@ dependencies {
testApi(project(":compiler:fir:checkers"))
testApi(project(":compiler:fir:checkers:checkers.jvm"))
testApi(project(":compiler:fir:checkers:checkers.js"))
testApi(project(":compiler:fir:plugin-utils"))
testRuntimeOnly(project(":core:descriptors.runtime"))
testRuntimeOnly(project(":compiler:fir:fir-serialization"))
@@ -8,7 +8,6 @@ package org.jetbrains.kotlin.fir.plugin
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.expressions.FirFunctionCall
import org.jetbrains.kotlin.fir.extensions.FirExpressionResolutionExtension
import org.jetbrains.kotlin.fir.plugin.generators.toSimpleConeType
import org.jetbrains.kotlin.fir.types.ConeKotlinType
import org.jetbrains.kotlin.fir.types.FirTypeProjectionWithVariance
import org.jetbrains.kotlin.fir.types.coneType
@@ -29,7 +28,7 @@ class AlgebraReceiverInjector(session: FirSession) : FirExpressionResolutionExte
if (functionCall.calleeReference.name != INJECT_ALGEBRA_NAME) return emptyList()
val typeProjection = functionCall.typeArguments.firstOrNull() as? FirTypeProjectionWithVariance ?: return emptyList()
val argumentType = typeProjection.typeRef.coneType
val algebraType = ALGEBRA_CLASS_ID.toSimpleConeType(arrayOf(argumentType))
val algebraType = ALGEBRA_CLASS_ID.createConeType(session, arrayOf(argumentType))
return listOf(algebraType)
}
}
@@ -6,23 +6,17 @@
package org.jetbrains.kotlin.fir.plugin.generators
import org.jetbrains.kotlin.GeneratedDeclarationKey
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.descriptors.EffectiveVisibility
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.declarations.FirResolvePhase
import org.jetbrains.kotlin.fir.declarations.builder.buildRegularClass
import org.jetbrains.kotlin.fir.declarations.impl.FirResolvedDeclarationStatusImpl
import org.jetbrains.kotlin.fir.declarations.origin
import org.jetbrains.kotlin.fir.extensions.FirDeclarationGenerationExtension
import org.jetbrains.kotlin.fir.extensions.FirDeclarationPredicateRegistrar
import org.jetbrains.kotlin.fir.extensions.MemberGenerationContext
import org.jetbrains.kotlin.fir.extensions.predicate.LookupPredicate
import org.jetbrains.kotlin.fir.extensions.predicateBasedProvider
import org.jetbrains.kotlin.fir.moduleData
import org.jetbrains.kotlin.fir.plugin.createConstructor
import org.jetbrains.kotlin.fir.plugin.createMemberFunction
import org.jetbrains.kotlin.fir.plugin.createNestedClass
import org.jetbrains.kotlin.fir.plugin.fqn
import org.jetbrains.kotlin.fir.scopes.kotlinScopeProvider
import org.jetbrains.kotlin.fir.types.constructStarProjectedType
import org.jetbrains.kotlin.fir.symbols.impl.*
import org.jetbrains.kotlin.name.CallableId
import org.jetbrains.kotlin.name.Name
@@ -52,28 +46,20 @@ class AdditionalMembersGenerator(session: FirSession) : FirDeclarationGeneration
override fun generateFunctions(callableId: CallableId, context: MemberGenerationContext?): List<FirNamedFunctionSymbol> {
if (callableId.callableName != MATERIALIZE_NAME) return emptyList()
val classId = callableId.classId ?: return emptyList()
val matchedClassSymbol = matchedClasses.firstOrNull { it.classId == classId } ?: return emptyList()
return listOf(buildMaterializeFunction(matchedClassSymbol, callableId, Key).symbol)
if (context == null) return emptyList()
val matchedClassSymbol = matchedClasses.firstOrNull { it == context.owner } ?: return emptyList()
val function = createMemberFunction(context.owner, Key, callableId.callableName, matchedClassSymbol.constructStarProjectedType())
return listOf(function.symbol)
}
override fun generateNestedClassLikeDeclaration(owner: FirClassSymbol<*>, name: Name): FirClassLikeSymbol<*>? {
if (matchedClasses.none { it == owner }) return null
return buildRegularClass {
resolvePhase = FirResolvePhase.BODY_RESOLVE
moduleData = session.moduleData
origin = Key.origin
classKind = ClassKind.CLASS
scopeProvider = session.kotlinScopeProvider
status = FirResolvedDeclarationStatusImpl(Visibilities.Public, Modality.FINAL, EffectiveVisibility.Public)
this.name = name
symbol = FirRegularClassSymbol(owner.classId.createNestedClassId(name))
superTypeRefs += session.builtinTypes.anyType
}.symbol
return createNestedClass(owner, name, Key).symbol
}
override fun generateConstructors(context: MemberGenerationContext): List<FirConstructorSymbol> {
return listOf(buildConstructor(context.owner, isInner = false, Key).symbol)
val createConstructor = createConstructor(context.owner, Key, generateDelegatedNoArgConstructorCall = true)
return listOf(createConstructor.symbol)
}
override fun getCallableNamesForClass(classSymbol: FirClassSymbol<*>): Set<Name> {
@@ -5,25 +5,19 @@
package org.jetbrains.kotlin.fir.plugin.generators
import org.jetbrains.kotlin.GeneratedDeclarationKey
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.descriptors.EffectiveVisibility
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.declarations.FirDeclarationOrigin
import org.jetbrains.kotlin.GeneratedDeclarationKey
import org.jetbrains.kotlin.fir.declarations.FirResolvePhase
import org.jetbrains.kotlin.fir.declarations.builder.buildRegularClass
import org.jetbrains.kotlin.fir.declarations.builder.buildSimpleFunction
import org.jetbrains.kotlin.fir.declarations.impl.FirResolvedDeclarationStatusImpl
import org.jetbrains.kotlin.fir.declarations.origin
import org.jetbrains.kotlin.fir.extensions.*
import org.jetbrains.kotlin.fir.extensions.FirDeclarationGenerationExtension
import org.jetbrains.kotlin.fir.extensions.FirDeclarationPredicateRegistrar
import org.jetbrains.kotlin.fir.extensions.MemberGenerationContext
import org.jetbrains.kotlin.fir.extensions.predicate.LookupPredicate
import org.jetbrains.kotlin.fir.moduleData
import org.jetbrains.kotlin.fir.extensions.predicateBasedProvider
import org.jetbrains.kotlin.fir.plugin.createCompanionObject
import org.jetbrains.kotlin.fir.plugin.createDefaultPrivateConstructor
import org.jetbrains.kotlin.fir.plugin.createMemberFunction
import org.jetbrains.kotlin.fir.plugin.fqn
import org.jetbrains.kotlin.fir.resolve.defaultType
import org.jetbrains.kotlin.fir.scopes.kotlinScopeProvider
import org.jetbrains.kotlin.fir.symbols.SymbolInternals
import org.jetbrains.kotlin.fir.symbols.impl.*
import org.jetbrains.kotlin.name.CallableId
import org.jetbrains.kotlin.name.Name
@@ -40,24 +34,7 @@ class CompanionGenerator(session: FirSession) : FirDeclarationGenerationExtensio
override fun generateNestedClassLikeDeclaration(owner: FirClassSymbol<*>, name: Name): FirClassLikeSymbol<*>? {
if (name != SpecialNames.DEFAULT_NAME_FOR_COMPANION_OBJECT) return null
val regularClass = buildRegularClass {
resolvePhase = FirResolvePhase.BODY_RESOLVE
moduleData = session.moduleData
origin = Key.origin
classKind = ClassKind.OBJECT
scopeProvider = session.kotlinScopeProvider
status = FirResolvedDeclarationStatusImpl(
Visibilities.Public,
Modality.FINAL,
EffectiveVisibility.Public
).apply {
isCompanion = true
}
this.name = name
symbol = FirRegularClassSymbol(owner.classId.createNestedClassId(name))
superTypeRefs += session.builtinTypes.anyType
}
return regularClass.symbol
return createCompanionObject(owner, Key).symbol
}
override fun generateFunctions(callableId: CallableId, context: MemberGenerationContext?): List<FirNamedFunctionSymbol> {
@@ -65,25 +42,12 @@ class CompanionGenerator(session: FirSession) : FirDeclarationGenerationExtensio
val ownerKey = (owner.origin as? FirDeclarationOrigin.Plugin)?.key ?: return emptyList()
if (ownerKey != Key) return emptyList()
if (callableId.callableName != FOO_NAME) return emptyList()
val function = buildSimpleFunction {
resolvePhase = FirResolvePhase.BODY_RESOLVE
moduleData = session.moduleData
origin = Key.origin
status = FirResolvedDeclarationStatusImpl(
Visibilities.Public,
Modality.FINAL,
EffectiveVisibility.Public
)
name = FOO_NAME
symbol = FirNamedFunctionSymbol(callableId)
returnTypeRef = session.builtinTypes.intType
dispatchReceiverType = owner.defaultType()
}
val function = createMemberFunction(owner, Key, callableId.callableName, session.builtinTypes.intType.type)
return listOf(function.symbol)
}
override fun generateConstructors(context: MemberGenerationContext): List<FirConstructorSymbol> {
val constructor = buildConstructor(context.owner, isInner = false, Key)
val constructor = createDefaultPrivateConstructor(context.owner, Key)
return listOf(constructor.symbol)
}
@@ -100,9 +64,8 @@ class CompanionGenerator(session: FirSession) : FirDeclarationGenerationExtensio
}
}
@OptIn(SymbolInternals::class)
override fun getNestedClassifiersNames(classSymbol: FirClassSymbol<*>): Set<Name> {
return if (session.predicateBasedProvider.matches(PREDICATE, classSymbol.fir)) {
return if (session.predicateBasedProvider.matches(PREDICATE, classSymbol)) {
setOf(SpecialNames.DEFAULT_NAME_FOR_COMPANION_OBJECT)
} else {
emptySet()
@@ -6,24 +6,17 @@
package org.jetbrains.kotlin.fir.plugin.generators
import org.jetbrains.kotlin.GeneratedDeclarationKey
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.descriptors.EffectiveVisibility
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.declarations.builder.buildRegularClass
import org.jetbrains.kotlin.fir.declarations.impl.FirResolvedDeclarationStatusImpl
import org.jetbrains.kotlin.fir.extensions.FirDeclarationGenerationExtension
import org.jetbrains.kotlin.fir.extensions.FirDeclarationPredicateRegistrar
import org.jetbrains.kotlin.fir.extensions.MemberGenerationContext
import org.jetbrains.kotlin.fir.extensions.predicate.LookupPredicate
import org.jetbrains.kotlin.fir.extensions.predicateBasedProvider
import org.jetbrains.kotlin.fir.moduleData
import org.jetbrains.kotlin.fir.plugin.fqn
import org.jetbrains.kotlin.fir.plugin.*
import org.jetbrains.kotlin.fir.resolve.providers.getRegularClassSymbolByClassId
import org.jetbrains.kotlin.fir.resolve.providers.symbolProvider
import org.jetbrains.kotlin.fir.scopes.kotlinScopeProvider
import org.jetbrains.kotlin.fir.symbols.SymbolInternals
import org.jetbrains.kotlin.fir.types.constructStarProjectedType
import org.jetbrains.kotlin.fir.symbols.impl.*
import org.jetbrains.kotlin.name.*
@@ -64,7 +57,7 @@ class ExternalClassGenerator(session: FirSession) : FirDeclarationGenerationExte
override fun generateTopLevelClassLikeDeclaration(classId: ClassId): FirClassLikeSymbol<*>? {
if (classId != GENERATED_CLASS_ID) return null
if (matchedClasses.isEmpty()) return null
return buildClass(classId).symbol
return createTopLevelClass(classId, Key).symbol
}
override fun generateNestedClassLikeDeclaration(owner: FirClassSymbol<*>, name: Name): FirClassLikeSymbol<*>? {
@@ -80,40 +73,26 @@ class ExternalClassGenerator(session: FirSession) : FirDeclarationGenerationExte
override fun generateConstructors(context: MemberGenerationContext): List<FirConstructorSymbol> {
val classId = context.owner.classId
if (classId != GENERATED_CLASS_ID && classId !in classIdsForMatchedClasses) return emptyList()
return listOf(buildConstructor(context.owner, isInner = false, Key).symbol)
return listOf(createConstructor(context.owner, Key).symbol)
}
private fun generateNestedClass(classId: ClassId, owner: FirClassSymbol<*>): FirClassLikeSymbol<*>? {
if (owner.classId != GENERATED_CLASS_ID) return null
val matchedClass = classIdsForMatchedClasses[classId] ?: return null
return buildClass(classId).also {
return createNestedClass(owner, classId.shortClassName, Key).also {
it.matchedClass = matchedClass.classId
}.symbol
}
@OptIn(SymbolInternals::class)
override fun generateFunctions(callableId: CallableId, context: MemberGenerationContext?): List<FirNamedFunctionSymbol> {
if (callableId.classId !in classIdsForMatchedClasses || callableId.callableName != MATERIALIZE_NAME) return emptyList()
val owner = context?.owner
require(owner is FirRegularClassSymbol)
val matchedClassId = owner.fir.matchedClass ?: return emptyList()
val matchedClassSymbol = session.symbolProvider.getClassLikeSymbolByClassId(matchedClassId) ?: return emptyList()
return listOf(buildMaterializeFunction(matchedClassSymbol, callableId, Key).symbol)
}
private fun buildClass(classId: ClassId): FirRegularClass {
return buildRegularClass {
resolvePhase = FirResolvePhase.BODY_RESOLVE
moduleData = session.moduleData
origin = Key.origin
classKind = ClassKind.CLASS
scopeProvider = session.kotlinScopeProvider
status = FirResolvedDeclarationStatusImpl(Visibilities.Public, Modality.FINAL, EffectiveVisibility.Public)
name = classId.shortClassName
symbol = FirRegularClassSymbol(classId)
superTypeRefs += session.builtinTypes.anyType
}
val matchedClassId = owner.matchedClass ?: return emptyList()
val matchedClassSymbol = session.symbolProvider.getRegularClassSymbolByClassId(matchedClassId) ?: return emptyList()
val function = createMemberFunction(owner, Key, callableId.callableName, matchedClassSymbol.constructStarProjectedType())
return listOf(function.symbol)
}
override fun getCallableNamesForClass(classSymbol: FirClassSymbol<*>): Set<Name> {
@@ -148,3 +127,5 @@ class ExternalClassGenerator(session: FirSession) : FirDeclarationGenerationExte
private object MatchedClassAttributeKey : FirDeclarationDataKey()
private var FirRegularClass.matchedClass: ClassId? by FirDeclarationDataRegistry.data(MatchedClassAttributeKey)
private val FirRegularClassSymbol.matchedClass: ClassId? by FirDeclarationDataRegistry.symbolAccessor(MatchedClassAttributeKey)
@@ -5,29 +5,20 @@
package org.jetbrains.kotlin.fir.plugin.generators
import org.jetbrains.kotlin.descriptors.EffectiveVisibility
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.GeneratedDeclarationKey
import org.jetbrains.kotlin.fir.declarations.FirResolvePhase
import org.jetbrains.kotlin.fir.declarations.builder.buildSimpleFunction
import org.jetbrains.kotlin.fir.declarations.builder.buildValueParameter
import org.jetbrains.kotlin.fir.declarations.impl.FirResolvedDeclarationStatusImpl
import org.jetbrains.kotlin.fir.declarations.origin
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.expressions.builder.buildBlock
import org.jetbrains.kotlin.fir.extensions.FirDeclarationGenerationExtension
import org.jetbrains.kotlin.fir.extensions.FirDeclarationPredicateRegistrar
import org.jetbrains.kotlin.fir.extensions.MemberGenerationContext
import org.jetbrains.kotlin.fir.extensions.predicate.LookupPredicate
import org.jetbrains.kotlin.fir.extensions.predicateBasedProvider
import org.jetbrains.kotlin.fir.moduleData
import org.jetbrains.kotlin.fir.plugin.createConeType
import org.jetbrains.kotlin.fir.plugin.createMemberFunction
import org.jetbrains.kotlin.fir.plugin.fqn
import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirNamedFunctionSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirRegularClassSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirValueParameterSymbol
import org.jetbrains.kotlin.fir.types.builder.buildResolvedTypeRef
import org.jetbrains.kotlin.name.CallableId
import org.jetbrains.kotlin.name.Name
@@ -60,33 +51,13 @@ class MembersOfSerializerGenerator(session: FirSession) : FirDeclarationGenerati
}
override fun generateFunctions(callableId: CallableId, context: MemberGenerationContext?): List<FirNamedFunctionSymbol> {
val owner = context?.owner ?: return emptyList()
val argumentClassId = serializeMethodNames[callableId.callableName] ?: return emptyList()
val dispatchReceiverClassId = callableId.classId ?: return emptyList()
val function = buildSimpleFunction {
moduleData = session.moduleData
resolvePhase = FirResolvePhase.BODY_RESOLVE
origin = Key.origin
status = FirResolvedDeclarationStatusImpl(Visibilities.Public, Modality.FINAL, EffectiveVisibility.Public)
returnTypeRef = session.builtinTypes.unitType
dispatchReceiverType = dispatchReceiverClassId.toSimpleConeType()
symbol = FirNamedFunctionSymbol(callableId)
valueParameters += buildValueParameter {
moduleData = session.moduleData
containingFunctionSymbol = this@buildSimpleFunction.symbol
resolvePhase = FirResolvePhase.BODY_RESOLVE
origin = Key.origin
returnTypeRef = buildResolvedTypeRef {
type = argumentClassId.toSimpleConeType()
}
name = X_NAME
symbol = FirValueParameterSymbol(name)
isCrossinline = false
isNoinline = false
isVararg = false
}
body = buildBlock {}
.apply { replaceTypeRef(session.builtinTypes.unitType) }
name = callableId.callableName
val function = createMemberFunction(owner, Key, callableId.callableName, session.builtinTypes.unitType.type) {
valueParameter(X_NAME, argumentClassId.createConeType(session))
}.apply {
replaceBody(buildBlock {}.apply { replaceTypeRef(session.builtinTypes.unitType) })
}
return listOf(function.symbol)
}
@@ -5,26 +5,18 @@
package org.jetbrains.kotlin.fir.plugin.generators
import org.jetbrains.kotlin.descriptors.EffectiveVisibility
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.GeneratedDeclarationKey
import org.jetbrains.kotlin.fir.declarations.FirResolvePhase
import org.jetbrains.kotlin.fir.declarations.builder.buildSimpleFunction
import org.jetbrains.kotlin.fir.declarations.builder.buildValueParameter
import org.jetbrains.kotlin.fir.declarations.impl.FirResolvedDeclarationStatusImpl
import org.jetbrains.kotlin.fir.declarations.origin
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.extensions.FirDeclarationGenerationExtension
import org.jetbrains.kotlin.fir.extensions.FirDeclarationPredicateRegistrar
import org.jetbrains.kotlin.fir.extensions.MemberGenerationContext
import org.jetbrains.kotlin.fir.extensions.predicate.LookupPredicate
import org.jetbrains.kotlin.fir.extensions.predicateBasedProvider
import org.jetbrains.kotlin.fir.moduleData
import org.jetbrains.kotlin.fir.plugin.createTopLevelFunction
import org.jetbrains.kotlin.fir.plugin.fqn
import org.jetbrains.kotlin.fir.symbols.impl.*
import org.jetbrains.kotlin.fir.types.builder.buildResolvedTypeRef
import org.jetbrains.kotlin.fir.types.impl.ConeClassLikeTypeImpl
import org.jetbrains.kotlin.fir.types.constructStarProjectedType
import org.jetbrains.kotlin.fir.symbols.impl.FirNamedFunctionSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirRegularClassSymbol
import org.jetbrains.kotlin.name.CallableId
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.Name
@@ -45,32 +37,8 @@ class TopLevelDeclarationsGenerator(session: FirSession) : FirDeclarationGenerat
override fun generateFunctions(callableId: CallableId, context: MemberGenerationContext?): List<FirNamedFunctionSymbol> {
if (context != null) return emptyList()
val matchedClassSymbol = findMatchedClassForFunction(callableId) ?: return emptyList()
val function = buildSimpleFunction {
resolvePhase = FirResolvePhase.BODY_RESOLVE
moduleData = session.moduleData
origin = Key.origin
status = FirResolvedDeclarationStatusImpl(
Visibilities.Public,
Modality.FINAL,
EffectiveVisibility.Public
)
returnTypeRef = session.builtinTypes.stringType
symbol = FirNamedFunctionSymbol(callableId)
valueParameters += buildValueParameter {
containingFunctionSymbol = this@buildSimpleFunction.symbol
resolvePhase = FirResolvePhase.BODY_RESOLVE
moduleData = session.moduleData
origin = Key.origin
returnTypeRef = buildResolvedTypeRef {
type = ConeClassLikeTypeImpl(ConeClassLikeLookupTagImpl(matchedClassSymbol.classId), emptyArray(), isNullable = false)
}
name = Name.identifier("value")
symbol = FirValueParameterSymbol(name)
isCrossinline = false
isNoinline = false
isVararg = false
}
name = callableId.callableName
val function = createTopLevelFunction(Key, callableId, session.builtinTypes.stringType.type) {
valueParameter(Name.identifier("value"), matchedClassSymbol.constructStarProjectedType())
}
return listOf(function.symbol)
}
@@ -1,101 +0,0 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.fir.plugin.generators
import org.jetbrains.kotlin.GeneratedDeclarationKey
import org.jetbrains.kotlin.descriptors.EffectiveVisibility
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.fir.containingClassForStaticMemberAttr
import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.declarations.builder.buildPrimaryConstructor
import org.jetbrains.kotlin.fir.declarations.builder.buildSimpleFunction
import org.jetbrains.kotlin.fir.declarations.impl.FirResolvedDeclarationStatusImpl
import org.jetbrains.kotlin.fir.extensions.FirDeclarationGenerationExtension
import org.jetbrains.kotlin.fir.moduleData
import org.jetbrains.kotlin.fir.resolve.defaultType
import org.jetbrains.kotlin.fir.resolve.providers.symbolProvider
import org.jetbrains.kotlin.fir.symbols.SymbolInternals
import org.jetbrains.kotlin.fir.symbols.impl.*
import org.jetbrains.kotlin.fir.types.ConeClassLikeType
import org.jetbrains.kotlin.fir.types.ConeKotlinTypeProjection
import org.jetbrains.kotlin.fir.types.builder.buildResolvedTypeRef
import org.jetbrains.kotlin.fir.types.impl.ConeClassLikeTypeImpl
import org.jetbrains.kotlin.name.CallableId
import org.jetbrains.kotlin.name.ClassId
@OptIn(SymbolInternals::class)
fun FirDeclarationGenerationExtension.buildMaterializeFunction(
matchedClassSymbol: FirClassLikeSymbol<*>,
callableId: CallableId,
key: GeneratedDeclarationKey
): FirSimpleFunction {
return buildSimpleFunction {
resolvePhase = FirResolvePhase.BODY_RESOLVE
moduleData = session.moduleData
origin = key.origin
status = FirResolvedDeclarationStatusImpl(
Visibilities.Public,
Modality.FINAL,
EffectiveVisibility.Public
)
returnTypeRef = buildResolvedTypeRef {
type = ConeClassLikeTypeImpl(
matchedClassSymbol.toLookupTag(),
emptyArray(),
isNullable = false
)
}
name = callableId.callableName
symbol = FirNamedFunctionSymbol(callableId)
dispatchReceiverType = callableId.classId?.let {
val firClass = session.symbolProvider.getClassLikeSymbolByClassId(it)?.fir as? FirClass
firClass?.defaultType()
}
}
}
// FIXME: this has to be shared
@OptIn(SymbolInternals::class)
fun FirDeclarationGenerationExtension.buildConstructor(owner: FirClassSymbol<*>, isInner: Boolean, key: GeneratedDeclarationKey): FirConstructor {
val classId = owner.classId
val lookupTag = owner.toLookupTag()
return buildPrimaryConstructor {
resolvePhase = FirResolvePhase.BODY_RESOLVE
moduleData = session.moduleData
origin = key.origin
returnTypeRef = buildResolvedTypeRef {
type = ConeClassLikeTypeImpl(
lookupTag,
emptyArray(),
isNullable = false
)
}
status = FirResolvedDeclarationStatusImpl(
Visibilities.Public,
Modality.FINAL,
EffectiveVisibility.Public
)
symbol = FirConstructorSymbol(classId)
if (isInner && classId.isNestedClass) {
dispatchReceiverType = classId.parentClassId?.let {
val firClass = session.symbolProvider.getClassLikeSymbolByClassId(it)?.fir as? FirClass
firClass?.defaultType()
}
}
}.also {
it.containingClassForStaticMemberAttr = lookupTag
}
}
// FIXME: this has to be shared
fun ClassId.toSimpleConeType(typeArguments: Array<ConeKotlinTypeProjection> = emptyArray()): ConeClassLikeType {
return ConeClassLikeTypeImpl(
ConeClassLikeLookupTagImpl(this),
typeArguments,
isNullable = false
)
}
@@ -58,13 +58,12 @@ abstract class AbstractTransformerForGenerator(
final override fun visitConstructor(declaration: IrConstructor) {
val origin = declaration.origin
if (origin !is GeneratedByPlugin || !interestedIn(origin.pluginKey)) {
if (origin !is GeneratedByPlugin || !interestedIn(origin.pluginKey) || declaration.body != null) {
if (visitBodies) {
visitElement(declaration)
}
return
}
require(declaration.body == null)
declaration.body = generateBodyForConstructor(declaration, origin.pluginKey)
}
@@ -22,6 +22,6 @@ class TransformerForAdditionalMembersGenerator(context: IrPluginContext) : Abstr
}
override fun generateBodyForConstructor(constructor: IrConstructor, key: GeneratedDeclarationKey): IrBody? {
return generateBodyForDefaultConstructor(constructor)
return constructor.body
}
}
@@ -7,7 +7,9 @@ FILE: classWithCompanionObject.kt
public final companion object Companion : R|kotlin/Any| {
public final fun foo(): R|kotlin/Int|
public constructor(): R|SomeClass.Companion|
private constructor(): R|SomeClass.Companion| {
super<R|kotlin/Any|>()
}
}
@@ -17,7 +17,9 @@ FILE: classWithGeneratedMembersAndNestedClass.kt
public final fun materialize(): R|Foo|
public final class Nested : R|kotlin/Any| {
public constructor(): R|Foo.Nested|
public constructor(): R|Foo.Nested| {
super<R|kotlin/Any|>()
}
}
@@ -28,11 +28,11 @@ FILE fqName:bar fileName:/generatedClassWithMembersAndNestedClasses.kt
FUN name:testConstructor visibility:public modality:FINAL <> () returnType:kotlin.Unit
BLOCK_BODY
VAR name:generatedClass type:foo.AllOpenGenerated [val]
CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in foo.AllOpenGenerated' type=foo.AllOpenGenerated origin=null
CONSTRUCTOR_CALL 'public constructor <init> () declared in foo.AllOpenGenerated' type=foo.AllOpenGenerated origin=null
FUN name:testNestedClasses visibility:public modality:FINAL <> () returnType:kotlin.String
BLOCK_BODY
VAR name:nestedFoo type:foo.AllOpenGenerated.NestedFoo [val]
CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in foo.AllOpenGenerated.NestedFoo' type=foo.AllOpenGenerated.NestedFoo origin=null
CONSTRUCTOR_CALL 'public constructor <init> () declared in foo.AllOpenGenerated.NestedFoo' type=foo.AllOpenGenerated.NestedFoo origin=null
RETURN type=kotlin.Nothing from='public final fun testNestedClasses (): kotlin.String declared in bar'
CALL 'public final fun box (): kotlin.String declared in bar.Foo' type=kotlin.String origin=null
$this: CALL 'public final fun materialize (): bar.Foo declared in foo.AllOpenGenerated.NestedFoo' type=bar.Foo origin=null
@@ -8,7 +8,9 @@ FILE: localClassWithCompanionObject.kt
public final companion object Companion : R|kotlin/Any| {
public final fun foo(): R|kotlin/Int|
public constructor(): R|SomeClass.Companion|
private constructor(): R|SomeClass.Companion| {
super<R|kotlin/Any|>()
}
}
@@ -7,7 +7,9 @@ FILE: classWithCompanionObject.kt
public final companion object Companion : R|kotlin/Any| {
public final fun foo(): R|kotlin/Int|
public constructor(): R|SomeClass.Companion|
private constructor(): R|SomeClass.Companion| {
super<R|kotlin/Any|>()
}
}
@@ -14,7 +14,9 @@ FILE: classWithGeneratedMembersAndNestedClass.kt
public final fun materialize(): R|Foo|
public final class Nested : R|kotlin/Any| {
public constructor(): R|Foo.Nested|
public constructor(): R|Foo.Nested| {
super<R|kotlin/Any|>()
}
}
@@ -13,7 +13,9 @@ FILE: localClassWithCompanionObject.kt
public final companion object Companion : R|kotlin/Any| {
public final fun foo(): R|kotlin/Int|
public constructor(): R|SomeClass.Nested.Companion|
private constructor(): R|SomeClass.Nested.Companion| {
super<R|kotlin/Any|>()
}
}
@@ -22,7 +24,9 @@ FILE: localClassWithCompanionObject.kt
public final companion object Companion : R|kotlin/Any| {
public final fun foo(): R|kotlin/Int|
public constructor(): R|SomeClass.Companion|
private constructor(): R|SomeClass.Companion| {
super<R|kotlin/Any|>()
}
}