diff --git a/analysis/analysis-api/testData/scopes/memberScopeByFqName/withTestCompilerPluginEnabled/generatedCompanionWithFoo.pretty.txt b/analysis/analysis-api/testData/scopes/memberScopeByFqName/withTestCompilerPluginEnabled/generatedCompanionWithFoo.pretty.txt index b35acf182ee..30b5c6cb43e 100644 --- a/analysis/analysis-api/testData/scopes/memberScopeByFqName/withTestCompilerPluginEnabled/generatedCompanionWithFoo.pretty.txt +++ b/analysis/analysis-api/testData/scopes/memberScopeByFqName/withTestCompilerPluginEnabled/generatedCompanionWithFoo.pretty.txt @@ -6,4 +6,4 @@ open fun hashCode(): kotlin.Int open fun toString(): kotlin.String -constructor() \ No newline at end of file +private constructor() diff --git a/analysis/analysis-api/testData/scopes/memberScopeByFqName/withTestCompilerPluginEnabled/generatedCompanionWithFoo.txt b/analysis/analysis-api/testData/scopes/memberScopeByFqName/withTestCompilerPluginEnabled/generatedCompanionWithFoo.txt index 7fcf1275967..496c6dc1a02 100644 --- a/analysis/analysis-api/testData/scopes/memberScopeByFqName/withTestCompilerPluginEnabled/generatedCompanionWithFoo.txt +++ b/analysis/analysis-api/testData/scopes/memberScopeByFqName/withTestCompilerPluginEnabled/generatedCompanionWithFoo.txt @@ -144,6 +144,6 @@ KtConstructorSymbol: symbolKind: CLASS_MEMBER typeParameters: [] valueParameters: [] - visibility: Public + visibility: Private getContainingModule: KtSourceModule "Sources of main" - deprecationStatus: null \ No newline at end of file + deprecationStatus: null diff --git a/plugins/fir-plugin-prototype/build.gradle.kts b/plugins/fir-plugin-prototype/build.gradle.kts index 5ae244adf1f..4d4d99a1e0f 100644 --- a/plugins/fir-plugin-prototype/build.gradle.kts +++ b/plugins/fir-plugin-prototype/build.gradle.kts @@ -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")) diff --git a/plugins/fir-plugin-prototype/src/org/jetbrains/kotlin/fir/plugin/AlgebraReceiverInjector.kt b/plugins/fir-plugin-prototype/src/org/jetbrains/kotlin/fir/plugin/AlgebraReceiverInjector.kt index 7f6ed034975..9a0e21efb36 100644 --- a/plugins/fir-plugin-prototype/src/org/jetbrains/kotlin/fir/plugin/AlgebraReceiverInjector.kt +++ b/plugins/fir-plugin-prototype/src/org/jetbrains/kotlin/fir/plugin/AlgebraReceiverInjector.kt @@ -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) } } diff --git a/plugins/fir-plugin-prototype/src/org/jetbrains/kotlin/fir/plugin/generators/AdditionalMembersGenerator.kt b/plugins/fir-plugin-prototype/src/org/jetbrains/kotlin/fir/plugin/generators/AdditionalMembersGenerator.kt index 883772ec651..9f75a733246 100644 --- a/plugins/fir-plugin-prototype/src/org/jetbrains/kotlin/fir/plugin/generators/AdditionalMembersGenerator.kt +++ b/plugins/fir-plugin-prototype/src/org/jetbrains/kotlin/fir/plugin/generators/AdditionalMembersGenerator.kt @@ -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 { 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 { - 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 { diff --git a/plugins/fir-plugin-prototype/src/org/jetbrains/kotlin/fir/plugin/generators/CompanionGenerator.kt b/plugins/fir-plugin-prototype/src/org/jetbrains/kotlin/fir/plugin/generators/CompanionGenerator.kt index d982a029d6a..6f10c46a59c 100644 --- a/plugins/fir-plugin-prototype/src/org/jetbrains/kotlin/fir/plugin/generators/CompanionGenerator.kt +++ b/plugins/fir-plugin-prototype/src/org/jetbrains/kotlin/fir/plugin/generators/CompanionGenerator.kt @@ -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 { @@ -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 { - 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 { - return if (session.predicateBasedProvider.matches(PREDICATE, classSymbol.fir)) { + return if (session.predicateBasedProvider.matches(PREDICATE, classSymbol)) { setOf(SpecialNames.DEFAULT_NAME_FOR_COMPANION_OBJECT) } else { emptySet() diff --git a/plugins/fir-plugin-prototype/src/org/jetbrains/kotlin/fir/plugin/generators/ExternalClassGenerator.kt b/plugins/fir-plugin-prototype/src/org/jetbrains/kotlin/fir/plugin/generators/ExternalClassGenerator.kt index 324e70c9494..334c0a1f33b 100644 --- a/plugins/fir-plugin-prototype/src/org/jetbrains/kotlin/fir/plugin/generators/ExternalClassGenerator.kt +++ b/plugins/fir-plugin-prototype/src/org/jetbrains/kotlin/fir/plugin/generators/ExternalClassGenerator.kt @@ -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 { 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 { 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 { @@ -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) + diff --git a/plugins/fir-plugin-prototype/src/org/jetbrains/kotlin/fir/plugin/generators/MembersOfSerializerGenerator.kt b/plugins/fir-plugin-prototype/src/org/jetbrains/kotlin/fir/plugin/generators/MembersOfSerializerGenerator.kt index 8a7912d090e..0d426306d6e 100644 --- a/plugins/fir-plugin-prototype/src/org/jetbrains/kotlin/fir/plugin/generators/MembersOfSerializerGenerator.kt +++ b/plugins/fir-plugin-prototype/src/org/jetbrains/kotlin/fir/plugin/generators/MembersOfSerializerGenerator.kt @@ -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 { + 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) } diff --git a/plugins/fir-plugin-prototype/src/org/jetbrains/kotlin/fir/plugin/generators/TopLevelDeclarationsGenerator.kt b/plugins/fir-plugin-prototype/src/org/jetbrains/kotlin/fir/plugin/generators/TopLevelDeclarationsGenerator.kt index 361fe5700de..7b1f5d89692 100644 --- a/plugins/fir-plugin-prototype/src/org/jetbrains/kotlin/fir/plugin/generators/TopLevelDeclarationsGenerator.kt +++ b/plugins/fir-plugin-prototype/src/org/jetbrains/kotlin/fir/plugin/generators/TopLevelDeclarationsGenerator.kt @@ -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 { 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) } diff --git a/plugins/fir-plugin-prototype/src/org/jetbrains/kotlin/fir/plugin/generators/generationUtils.kt b/plugins/fir-plugin-prototype/src/org/jetbrains/kotlin/fir/plugin/generators/generationUtils.kt deleted file mode 100644 index e0564ecb537..00000000000 --- a/plugins/fir-plugin-prototype/src/org/jetbrains/kotlin/fir/plugin/generators/generationUtils.kt +++ /dev/null @@ -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 = emptyArray()): ConeClassLikeType { - return ConeClassLikeTypeImpl( - ConeClassLikeLookupTagImpl(this), - typeArguments, - isNullable = false - ) -} diff --git a/plugins/fir-plugin-prototype/src/org/jetbrains/kotlin/ir/plugin/AbstractTransformerForGenerator.kt b/plugins/fir-plugin-prototype/src/org/jetbrains/kotlin/ir/plugin/AbstractTransformerForGenerator.kt index 90cb14b1f2f..2c294f13da3 100644 --- a/plugins/fir-plugin-prototype/src/org/jetbrains/kotlin/ir/plugin/AbstractTransformerForGenerator.kt +++ b/plugins/fir-plugin-prototype/src/org/jetbrains/kotlin/ir/plugin/AbstractTransformerForGenerator.kt @@ -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) } diff --git a/plugins/fir-plugin-prototype/src/org/jetbrains/kotlin/ir/plugin/TransformerForAdditionalMembersGenerator.kt b/plugins/fir-plugin-prototype/src/org/jetbrains/kotlin/ir/plugin/TransformerForAdditionalMembersGenerator.kt index 921d4cad52b..a40c531197b 100644 --- a/plugins/fir-plugin-prototype/src/org/jetbrains/kotlin/ir/plugin/TransformerForAdditionalMembersGenerator.kt +++ b/plugins/fir-plugin-prototype/src/org/jetbrains/kotlin/ir/plugin/TransformerForAdditionalMembersGenerator.kt @@ -22,6 +22,6 @@ class TransformerForAdditionalMembersGenerator(context: IrPluginContext) : Abstr } override fun generateBodyForConstructor(constructor: IrConstructor, key: GeneratedDeclarationKey): IrBody? { - return generateBodyForDefaultConstructor(constructor) + return constructor.body } } diff --git a/plugins/fir-plugin-prototype/testData/box/classWithCompanionObject.fir.txt b/plugins/fir-plugin-prototype/testData/box/classWithCompanionObject.fir.txt index 45bf16ba2d4..37c6a4e8e40 100644 --- a/plugins/fir-plugin-prototype/testData/box/classWithCompanionObject.fir.txt +++ b/plugins/fir-plugin-prototype/testData/box/classWithCompanionObject.fir.txt @@ -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() + } } diff --git a/plugins/fir-plugin-prototype/testData/box/classWithGeneratedMembersAndNestedClass.fir.txt b/plugins/fir-plugin-prototype/testData/box/classWithGeneratedMembersAndNestedClass.fir.txt index feb70cb4451..52746302372 100644 --- a/plugins/fir-plugin-prototype/testData/box/classWithGeneratedMembersAndNestedClass.fir.txt +++ b/plugins/fir-plugin-prototype/testData/box/classWithGeneratedMembersAndNestedClass.fir.txt @@ -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() + } } diff --git a/plugins/fir-plugin-prototype/testData/box/generatedClassWithMembersAndNestedClasses.fir.ir.txt b/plugins/fir-plugin-prototype/testData/box/generatedClassWithMembersAndNestedClasses.fir.ir.txt index bcd37331d39..2d45510e923 100644 --- a/plugins/fir-plugin-prototype/testData/box/generatedClassWithMembersAndNestedClasses.fir.ir.txt +++ b/plugins/fir-plugin-prototype/testData/box/generatedClassWithMembersAndNestedClasses.fir.ir.txt @@ -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 () [primary] declared in foo.AllOpenGenerated' type=foo.AllOpenGenerated origin=null + CONSTRUCTOR_CALL 'public constructor () 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 () [primary] declared in foo.AllOpenGenerated.NestedFoo' type=foo.AllOpenGenerated.NestedFoo origin=null + CONSTRUCTOR_CALL 'public constructor () 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 diff --git a/plugins/fir-plugin-prototype/testData/box/localClassWithCompanionObject.fir.txt b/plugins/fir-plugin-prototype/testData/box/localClassWithCompanionObject.fir.txt index 7cdf354fbd8..669dabf5316 100644 --- a/plugins/fir-plugin-prototype/testData/box/localClassWithCompanionObject.fir.txt +++ b/plugins/fir-plugin-prototype/testData/box/localClassWithCompanionObject.fir.txt @@ -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() + } } diff --git a/plugins/fir-plugin-prototype/testData/diagnostics/memberGen/classWithCompanionObject.fir.txt b/plugins/fir-plugin-prototype/testData/diagnostics/memberGen/classWithCompanionObject.fir.txt index f7b4fbad003..f4e0c6351cf 100644 --- a/plugins/fir-plugin-prototype/testData/diagnostics/memberGen/classWithCompanionObject.fir.txt +++ b/plugins/fir-plugin-prototype/testData/diagnostics/memberGen/classWithCompanionObject.fir.txt @@ -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() + } } diff --git a/plugins/fir-plugin-prototype/testData/diagnostics/memberGen/classWithGeneratedMembersAndNestedClass.fir.txt b/plugins/fir-plugin-prototype/testData/diagnostics/memberGen/classWithGeneratedMembersAndNestedClass.fir.txt index 4aaffe85347..661aa29f824 100644 --- a/plugins/fir-plugin-prototype/testData/diagnostics/memberGen/classWithGeneratedMembersAndNestedClass.fir.txt +++ b/plugins/fir-plugin-prototype/testData/diagnostics/memberGen/classWithGeneratedMembersAndNestedClass.fir.txt @@ -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() + } } diff --git a/plugins/fir-plugin-prototype/testData/diagnostics/memberGen/localClassWithCompanionObject.fir.txt b/plugins/fir-plugin-prototype/testData/diagnostics/memberGen/localClassWithCompanionObject.fir.txt index 2951b34452b..c0afe522a1b 100644 --- a/plugins/fir-plugin-prototype/testData/diagnostics/memberGen/localClassWithCompanionObject.fir.txt +++ b/plugins/fir-plugin-prototype/testData/diagnostics/memberGen/localClassWithCompanionObject.fir.txt @@ -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() + } } @@ -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() + } }