diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt index 8c12b8e6ba8..61a822d4b73 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt @@ -611,16 +611,17 @@ class Fir2IrDeclarationStorage( } ?: createIrFunction(function, irParent, shouldLeaveScope, origin) } - fun getIrConstructor( + private fun getCachedIrConstructor( + constructor: FirConstructor + ): IrConstructor? { + return constructorCache[constructor] + } + + private fun createIrConstructor( constructor: FirConstructor, - irParent: IrDeclarationParent? = null, + irParent: IrDeclarationParent, shouldLeaveScope: Boolean = true ): IrConstructor { - val cached = constructorCache[constructor] - if (cached != null) { - return if (shouldLeaveScope) cached else cached.enterLocalScope(constructor) - } - val descriptor = WrappedClassConstructorDescriptor() val origin = IrDeclarationOrigin.DEFINED val isPrimary = constructor.isPrimary @@ -648,6 +649,18 @@ class Fir2IrDeclarationStorage( return created } + fun getIrConstructor( + constructor: FirConstructor, + irParent: IrDeclarationParent, + shouldLeaveScope: Boolean = true + ): IrConstructor { + val cached = getCachedIrConstructor(constructor) + if (cached != null) { + return if (shouldLeaveScope) cached else cached.enterLocalScope(constructor) + } + return createIrConstructor(constructor, irParent, shouldLeaveScope) + } + private fun createIrPropertyAccessor( propertyAccessor: FirPropertyAccessor?, property: FirProperty, @@ -969,8 +982,9 @@ class Fir2IrDeclarationStorage( irSymbolTable.referenceSimpleFunction(irDeclaration.descriptor) } is FirConstructor -> { - val irParent = findIrParent(firDeclaration) - val irDeclaration = getIrConstructor(firDeclaration, irParent).apply { + getCachedIrConstructor(firDeclaration)?.let { return irSymbolTable.referenceConstructor(it.descriptor) } + val irParent = findIrParent(firDeclaration)!! + val irDeclaration = createIrConstructor(firDeclaration, irParent).apply { setAndModifyParent(irParent) } irSymbolTable.referenceConstructor(irDeclaration.descriptor) diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt index cddd6b4b18f..0f89368c7df 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt @@ -234,7 +234,7 @@ class Fir2IrVisitor( override fun visitConstructor(constructor: FirConstructor, data: Any?): IrElement { val irConstructor = declarationStorage.getIrConstructor( - constructor, irParent = conversionScope.lastClass(), shouldLeaveScope = false + constructor, irParent = conversionScope.lastClass()!!, shouldLeaveScope = false ) return conversionScope.withFunction(irConstructor) { setFunctionContent(irConstructor.descriptor, constructor)