diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmBackendContext.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmBackendContext.kt index de06e1e10d3..5d5ae944e31 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmBackendContext.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmBackendContext.kt @@ -15,7 +15,6 @@ import org.jetbrains.kotlin.backend.jvm.codegen.ClassCodegen import org.jetbrains.kotlin.backend.jvm.codegen.IrTypeMapper import org.jetbrains.kotlin.backend.jvm.codegen.MethodSignatureMapper import org.jetbrains.kotlin.backend.jvm.codegen.createFakeContinuation -import org.jetbrains.kotlin.backend.jvm.descriptors.JvmDeclarationFactory import org.jetbrains.kotlin.backend.jvm.descriptors.JvmSharedVariablesManager import org.jetbrains.kotlin.backend.jvm.intrinsics.IrIntrinsicMethods import org.jetbrains.kotlin.backend.jvm.lower.CollectionStubComputer @@ -69,7 +68,7 @@ class JvmBackendContext( val methodSignatureMapper = MethodSignatureMapper(this) internal val innerClassesSupport = JvmInnerClassesSupport() - internal val declarationFactory = JvmDeclarationFactory(this, methodSignatureMapper, state.languageVersionSettings) + internal val cachedDeclarations = JvmCachedDeclarations(this, methodSignatureMapper, state.languageVersionSettings) override val mapping: Mapping = DefaultMapping() diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/descriptors/JvmDeclarationFactory.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmCachedDeclarations.kt similarity index 95% rename from compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/descriptors/JvmDeclarationFactory.kt rename to compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmCachedDeclarations.kt index 8fad050a720..41ed3ef7734 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/descriptors/JvmDeclarationFactory.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmCachedDeclarations.kt @@ -3,14 +3,12 @@ * 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.backend.jvm.descriptors +package org.jetbrains.kotlin.backend.jvm import org.jetbrains.kotlin.backend.common.ir.copyParameterDeclarationsFrom import org.jetbrains.kotlin.backend.common.ir.createImplicitParameterDeclarationWithWrappedDescriptor import org.jetbrains.kotlin.backend.common.ir.createStaticFunctionWithReceivers import org.jetbrains.kotlin.backend.common.lower.createIrBuilder -import org.jetbrains.kotlin.backend.jvm.JvmBackendContext -import org.jetbrains.kotlin.backend.jvm.JvmLoweredDeclarationOrigin import org.jetbrains.kotlin.backend.jvm.codegen.MethodSignatureMapper import org.jetbrains.kotlin.backend.jvm.codegen.isJvmInterface import org.jetbrains.kotlin.backend.jvm.ir.copyCorrespondingPropertyFrom @@ -33,7 +31,7 @@ import org.jetbrains.kotlin.load.java.JvmAbi import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.resolve.deprecation.DeprecationResolver -class JvmDeclarationFactory( +class JvmCachedDeclarations( private val context: JvmBackendContext, private val methodSignatureMapper: MethodSignatureMapper, private val languageVersionSettings: LanguageVersionSettings @@ -125,7 +123,7 @@ class JvmDeclarationFactory( oldParent annotations += oldField.annotations initializer = oldField.initializer - ?.replaceThisByStaticReference(this@JvmDeclarationFactory, oldParent, oldParent.thisReceiver!!) + ?.replaceThisByStaticReference(this@JvmCachedDeclarations, oldParent, oldParent.thisReceiver!!) ?.patchDeclarationParents(this) as IrExpressionBody? origin = if (irProperty.parentAsClass.isCompanion) JvmLoweredDeclarationOrigin.COMPANION_PROPERTY_BACKING_FIELD else origin } @@ -174,8 +172,8 @@ class JvmDeclarationFactory( if (it.origin == JvmLoweredDeclarationOrigin.DEFAULT_IMPLS_BRIDGE_FOR_COMPATIBILITY && !it.annotations.hasAnnotation(DeprecationResolver.JAVA_DEPRECATED) ) { - this@JvmDeclarationFactory.context.createIrBuilder(it.symbol).run { - it.annotations += irCall(this@JvmDeclarationFactory.context.ir.symbols.javaLangDeprecatedConstructor) + this@JvmCachedDeclarations.context.createIrBuilder(it.symbol).run { + it.annotations += irCall(this@JvmCachedDeclarations.context.ir.symbols.javaLangDeprecatedConstructor) } } } diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/ir/IrUtils.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/ir/IrUtils.kt index bf24afb4a31..416301505ec 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/ir/IrUtils.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/ir/IrUtils.kt @@ -8,11 +8,11 @@ package org.jetbrains.kotlin.backend.jvm.ir import org.jetbrains.kotlin.backend.common.ir.ir2string import org.jetbrains.kotlin.backend.common.lower.IrLoweringContext import org.jetbrains.kotlin.backend.jvm.JvmBackendContext +import org.jetbrains.kotlin.backend.jvm.JvmCachedDeclarations import org.jetbrains.kotlin.backend.jvm.JvmLoweredDeclarationOrigin import org.jetbrains.kotlin.backend.jvm.JvmSymbols import org.jetbrains.kotlin.backend.jvm.codegen.isInlineOnly import org.jetbrains.kotlin.backend.jvm.codegen.isJvmInterface -import org.jetbrains.kotlin.backend.jvm.descriptors.JvmDeclarationFactory import org.jetbrains.kotlin.backend.jvm.lower.inlineclasses.unboxInlineClass import org.jetbrains.kotlin.codegen.inline.coroutines.FOR_INLINE_SUFFIX import org.jetbrains.kotlin.config.JvmDefaultMode @@ -228,14 +228,14 @@ fun IrExpression.isSmartcastFromHigherThanNullable(context: JvmBackendContext): } fun IrBody.replaceThisByStaticReference( - declarationFactory: JvmDeclarationFactory, + cachedDeclarations: JvmCachedDeclarations, irClass: IrClass, oldThisReceiverParameter: IrValueParameter ): IrBody = transform(object : IrElementTransformerVoid() { override fun visitGetValue(expression: IrGetValue): IrExpression { if (expression.symbol == oldThisReceiverParameter.symbol) { - val instanceField = declarationFactory.getPrivateFieldForObjectInstance(irClass) + val instanceField = cachedDeclarations.getPrivateFieldForObjectInstance(irClass) return IrGetFieldImpl( expression.startOffset, expression.endOffset, diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/AddContinuationLowering.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/AddContinuationLowering.kt index ec3f0388014..1e5c6da87ee 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/AddContinuationLowering.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/AddContinuationLowering.kt @@ -348,7 +348,7 @@ private class AddContinuationLowering(private val context: JvmBackendContext) : private fun IrBlockBodyBuilder.callInvokeSuspend(invokeSuspend: IrSimpleFunction, lambda: IrExpression): IrExpression { // SingletonReferencesLowering has finished a while ago, so `irUnit()` won't work anymore. val unitClass = context.irBuiltIns.unitClass - val unitField = this@AddContinuationLowering.context.declarationFactory.getFieldForObjectInstance(unitClass.owner) + val unitField = this@AddContinuationLowering.context.cachedDeclarations.getFieldForObjectInstance(unitClass.owner) return irCallOp(invokeSuspend.symbol, invokeSuspend.returnType, lambda, irGetField(null, unitField)) } diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/EnumClassLowering.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/EnumClassLowering.kt index e479b5249e4..916eca678bb 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/EnumClassLowering.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/EnumClassLowering.kt @@ -79,7 +79,7 @@ private class EnumClassLowering(val context: JvmBackendContext) : ClassLoweringP } private fun buildEnumEntryField(enumEntry: IrEnumEntry): IrField = - context.declarationFactory.getFieldForEnumEntry(enumEntry).apply { + context.cachedDeclarations.getFieldForEnumEntry(enumEntry).apply { initializer = IrExpressionBodyImpl(enumEntry.initializerExpression!!.expression.patchDeclarationParents(this)) annotations += enumEntry.annotations } diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/InheritedDefaultMethodsOnClassesLowering.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/InheritedDefaultMethodsOnClassesLowering.kt index ece2c9eaa9f..f64052dd0fd 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/InheritedDefaultMethodsOnClassesLowering.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/InheritedDefaultMethodsOnClassesLowering.kt @@ -74,7 +74,7 @@ private class InheritedDefaultMethodsOnClassesLowering(val context: JvmBackendCo override fun visitSimpleFunction(declaration: IrSimpleFunction) { declaration.overriddenSymbols = declaration.overriddenSymbols.map { symbol -> if (symbol.owner.findInterfaceImplementation(context.state.jvmDefaultMode) != null) - context.declarationFactory.getDefaultImplsRedirection(symbol.owner).symbol + context.cachedDeclarations.getDefaultImplsRedirection(symbol.owner).symbol else symbol } super.visitSimpleFunction(declaration) @@ -84,10 +84,10 @@ private class InheritedDefaultMethodsOnClassesLowering(val context: JvmBackendCo interfaceImplementation: IrSimpleFunction, classOverride: IrSimpleFunction ): IrSimpleFunction { - val irFunction = context.declarationFactory.getDefaultImplsRedirection(classOverride) + val irFunction = context.cachedDeclarations.getDefaultImplsRedirection(classOverride) val superMethod = firstSuperMethodFromKotlin(irFunction, interfaceImplementation).owner - val defaultImplFun = context.declarationFactory.getDefaultImplsFunction(superMethod) + val defaultImplFun = context.cachedDeclarations.getDefaultImplsFunction(superMethod) context.createIrBuilder(irFunction.symbol, UNDEFINED_OFFSET, UNDEFINED_OFFSET).apply { irFunction.body = irBlockBody { +irReturn( @@ -130,7 +130,7 @@ private class InterfaceSuperCallsLowering(val context: JvmBackendContext) : IrEl val superCallee = expression.symbol.owner as IrSimpleFunction if (superCallee.isDefinitelyNotDefaultImplsMethod(context.state.jvmDefaultMode)) return super.visitCall(expression) - val redirectTarget = context.declarationFactory.getDefaultImplsFunction(superCallee) + val redirectTarget = context.cachedDeclarations.getDefaultImplsFunction(superCallee) val newCall = createDelegatingCallWithPlaceholderTypeArguments(expression, redirectTarget, context.irBuiltIns) return super.visitCall(newCall) } @@ -158,7 +158,7 @@ private class InterfaceDefaultCallsLowering(val context: JvmBackendContext) : Ir return super.visitCall(expression) } - val redirectTarget = context.declarationFactory.getDefaultImplsFunction(callee as IrSimpleFunction) + val redirectTarget = context.cachedDeclarations.getDefaultImplsFunction(callee as IrSimpleFunction) // InterfaceLowering bridges from DefaultImpls in compatibility mode -- if that's the case, // this phase will inadvertently cause a recursive loop as the bridge on the DefaultImpls diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/InterfaceLowering.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/InterfaceLowering.kt index 0569b1a3507..81dbf782dc7 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/InterfaceLowering.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/InterfaceLowering.kt @@ -8,7 +8,6 @@ package org.jetbrains.kotlin.backend.jvm.lower import org.jetbrains.kotlin.backend.common.ClassLoweringPass import org.jetbrains.kotlin.backend.common.ir.isMethodOfAny import org.jetbrains.kotlin.backend.common.ir.moveBodyTo -import org.jetbrains.kotlin.backend.common.lower.createIrBuilder import org.jetbrains.kotlin.backend.jvm.JvmBackendContext import org.jetbrains.kotlin.backend.jvm.JvmLoweredDeclarationOrigin import org.jetbrains.kotlin.backend.jvm.codegen.isJvmInterface @@ -16,7 +15,6 @@ import org.jetbrains.kotlin.backend.jvm.ir.* import org.jetbrains.kotlin.descriptors.ClassKind import org.jetbrains.kotlin.descriptors.Modality import org.jetbrains.kotlin.descriptors.Visibilities -import org.jetbrains.kotlin.ir.builders.irCall import org.jetbrains.kotlin.ir.declarations.* import org.jetbrains.kotlin.ir.expressions.IrCall import org.jetbrains.kotlin.ir.expressions.IrExpression @@ -29,7 +27,6 @@ import org.jetbrains.kotlin.ir.types.defaultType import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid -import org.jetbrains.kotlin.resolve.deprecation.DeprecationResolver /** * This phase moves interface members with default implementations to the @@ -54,7 +51,7 @@ internal class InterfaceLowering(val context: JvmBackendContext) : IrElementTran it is IrFunction && removedFunctions.containsKey(it.symbol) } - val defaultImplsIrClass = context.declarationFactory.getDefaultImplsClass(irClass) + val defaultImplsIrClass = context.cachedDeclarations.getDefaultImplsClass(irClass) if (defaultImplsIrClass.declarations.isNotEmpty()) { irClass.declarations.add(defaultImplsIrClass) } @@ -112,7 +109,7 @@ internal class InterfaceLowering(val context: JvmBackendContext) : IrElementTran !function.isDefinitelyNotDefaultImplsMethod(jvmDefaultMode, implementation) -> { val defaultImpl = createDefaultImpl(function) val superImpl = firstSuperMethodFromKotlin(function, implementation) - context.declarationFactory.getDefaultImplsFunction(superImpl.owner).also { + context.cachedDeclarations.getDefaultImplsFunction(superImpl.owner).also { defaultImpl.bridgeToStatic(it) } } @@ -156,7 +153,7 @@ internal class InterfaceLowering(val context: JvmBackendContext) : IrElementTran } } - val defaultImplsIrClass = context.declarationFactory.getDefaultImplsClass(irClass) + val defaultImplsIrClass = context.cachedDeclarations.getDefaultImplsClass(irClass) // Move metadata for local delegated properties from the interface to DefaultImpls, since this is where kotlin-reflect looks for it. val localDelegatedProperties = context.localDelegatedProperties[irClass.attributeOwnerId as IrClass] @@ -196,7 +193,7 @@ internal class InterfaceLowering(val context: JvmBackendContext) : IrElementTran } private fun createDefaultImpl(function: IrSimpleFunction, forCompatibility: Boolean = false): IrSimpleFunction = - context.declarationFactory.getDefaultImplsFunction(function, forCompatibility).also { newFunction -> + context.cachedDeclarations.getDefaultImplsFunction(function, forCompatibility).also { newFunction -> newFunction.parentAsClass.declarations.add(newFunction) } diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/JvmStaticAnnotationLowering.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/JvmStaticAnnotationLowering.kt index ac34bda3f6f..9cf38e481dc 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/JvmStaticAnnotationLowering.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/JvmStaticAnnotationLowering.kt @@ -114,7 +114,7 @@ private class CompanionObjectJvmStaticLowering(val context: JvmBackendContext) : annotations = target.annotations.map { it.deepCopyWithSymbols() } val proxy = this - val companionInstanceField = context.declarationFactory.getFieldForObjectInstance(companion) + val companionInstanceField = context.cachedDeclarations.getFieldForObjectInstance(companion) body = context.createIrBuilder(symbol).run { irExprBody(irCall(target).apply { passTypeArgumentsFrom(proxy) @@ -142,7 +142,7 @@ private class SingletonObjectJvmStaticLowering( jvmStaticFunction.dispatchReceiverParameter?.let { oldDispatchReceiverParameter -> jvmStaticFunction.dispatchReceiverParameter = null jvmStaticFunction.body = jvmStaticFunction.body?.replaceThisByStaticReference( - context.declarationFactory, + context.cachedDeclarations, irClass, oldDispatchReceiverParameter ) diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/MoveCompanionObjectFieldsLowering.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/MoveCompanionObjectFieldsLowering.kt index 79564e3f849..6515ebfc849 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/MoveCompanionObjectFieldsLowering.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/MoveCompanionObjectFieldsLowering.kt @@ -51,7 +51,7 @@ private class MoveOrCopyCompanionObjectFieldsLowering(val context: JvmBackendCon private fun IrClass.handle() { val newDeclarations = declarations.map { when (it) { - is IrProperty -> context.declarationFactory.getStaticBackingField(it)?.also { newField -> + is IrProperty -> context.cachedDeclarations.getStaticBackingField(it)?.also { newField -> it.backingField = newField newField.correspondingPropertySymbol = it.symbol } @@ -101,7 +101,7 @@ private class MoveOrCopyCompanionObjectFieldsLowering(val context: JvmBackendCon IrAnonymousInitializerImpl(startOffset, endOffset, origin, newSymbol, isStatic = true).apply { parent = newParent body = this@with.body - .replaceThisByStaticReference(context.declarationFactory, oldParent, oldParent.thisReceiver!!) + .replaceThisByStaticReference(context.cachedDeclarations, oldParent, oldParent.thisReceiver!!) .patchDeclarationParents(newParent) as IrBlockBody } } @@ -132,7 +132,7 @@ private class RemapObjectFieldAccesses(val context: JvmBackendContext) : FileLow override fun lower(irFile: IrFile) = irFile.transformChildrenVoid() private fun IrField.remap(): IrField? = - correspondingPropertySymbol?.owner?.let(context.declarationFactory::getStaticBackingField) + correspondingPropertySymbol?.owner?.let(context.cachedDeclarations::getStaticBackingField) override fun visitGetField(expression: IrGetField): IrExpression = expression.symbol.owner.remap()?.let { diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/ObjectClassLowering.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/ObjectClassLowering.kt index 068665e6992..9d2a0e0cf83 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/ObjectClassLowering.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/ObjectClassLowering.kt @@ -44,8 +44,8 @@ private class ObjectClassLowering(val context: JvmBackendContext) : IrElementTra private fun process(irClass: IrClass) { if (!irClass.isObject) return - val publicInstanceField = context.declarationFactory.getFieldForObjectInstance(irClass) - val privateInstanceField = context.declarationFactory.getPrivateFieldForObjectInstance(irClass) + val publicInstanceField = context.cachedDeclarations.getFieldForObjectInstance(irClass) + val privateInstanceField = context.cachedDeclarations.getPrivateFieldForObjectInstance(irClass) val constructor = irClass.constructors.find { it.isPrimary } ?: throw AssertionError("Object should have a primary constructor: ${irClass.name}") diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/SingletonReferencesLowering.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/SingletonReferencesLowering.kt index e8d34574ddd..e1a3e5186c4 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/SingletonReferencesLowering.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/SingletonReferencesLowering.kt @@ -46,7 +46,7 @@ private class SingletonReferencesLowering(val context: JvmBackendContext) : File // must be replaced with `SomeEnumEntry.this`. IrGetValueImpl(expression.startOffset, expression.endOffset, expression.type, appropriateThis.symbol) } else { - val entrySymbol = context.declarationFactory.getFieldForEnumEntry(expression.symbol.owner) + val entrySymbol = context.cachedDeclarations.getFieldForEnumEntry(expression.symbol.owner) IrGetFieldImpl(expression.startOffset, expression.endOffset, entrySymbol.symbol, expression.type) } } @@ -60,9 +60,9 @@ private class SingletonReferencesLowering(val context: JvmBackendContext) : File return IrGetValueImpl(expression.startOffset, expression.endOffset, it.symbol) } val instanceField = if (allScopes.any { it.irElement == expression.symbol.owner }) - context.declarationFactory.getPrivateFieldForObjectInstance(expression.symbol.owner) // Constructor or static method. + context.cachedDeclarations.getPrivateFieldForObjectInstance(expression.symbol.owner) // Constructor or static method. else - context.declarationFactory.getFieldForObjectInstance(expression.symbol.owner) // Not in object scope at all. + context.cachedDeclarations.getFieldForObjectInstance(expression.symbol.owner) // Not in object scope at all. return IrGetFieldImpl(expression.startOffset, expression.endOffset, instanceField.symbol, expression.type) }