From ba1172b3ad076cc7bbc58cf1797cbc4e7b0295e9 Mon Sep 17 00:00:00 2001 From: Jinseong Jeon Date: Tue, 2 Jun 2020 09:34:03 +0300 Subject: [PATCH] FIR2IR: distinguish substitution case when adding external fake overrides --- .../fir/backend/Fir2IrDeclarationStorage.kt | 55 +++++++++++++++---- .../generators/FakeOverrideGenerator.kt | 41 +++++++------- .../bound/companionObjectPropertyAccessors.kt | 1 - .../call/bound/extensionPropertyAccessors.kt | 1 - ...mStaticCompanionObjectPropertyAccessors.kt | 1 - .../bound/jvmStaticObjectPropertyAccessors.kt | 1 - .../call/bound/memberPropertyAccessors.kt | 1 - .../call/bound/objectPropertyAccessors.kt | 1 - .../call/disallowNullValueForNotNullField.kt | 1 - .../call/inlineClasses/fieldAccessors.kt | 1 - .../inlineClasses/jvmStaticFieldInObject.kt | 1 - .../nonOverridingVarOfInlineClass.kt | 1 - .../overridingVarOfInlineClass.kt | 1 - .../call/inlineClasses/properties.kt | 1 - .../box/reflection/call/privateProperty.kt | 1 - .../box/reflection/call/propertyAccessors.kt | 1 - .../box/reflection/call/protectedMembers.kt | 1 - .../reflection/callBy/inlineClassMembers.kt | 1 - .../callPropertiesInMultifileClass.kt | 1 - .../accessors/extensionPropertyAccessors.kt | 1 - .../properties/accessors/memberExtensions.kt | 1 - .../accessors/memberPropertyAccessors.kt | 1 - .../accessors/topLevelPropertyAccessors.kt | 1 - ...vatePropertyCallIsAccessibleOnAccessors.kt | 1 - 24 files changed, 64 insertions(+), 54 deletions(-) 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 e13b66a8cb6..cbffb72f0fd 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 @@ -9,6 +9,7 @@ import org.jetbrains.kotlin.KtNodeTypes import org.jetbrains.kotlin.builtins.KotlinBuiltIns.* import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.annotations.Annotations +import org.jetbrains.kotlin.fir.backend.generators.setOverriddenSymbolsForAccessors import org.jetbrains.kotlin.fir.declarations.* import org.jetbrains.kotlin.fir.declarations.impl.FirDefaultPropertyGetter import org.jetbrains.kotlin.fir.declarations.impl.FirDefaultPropertySetter @@ -239,31 +240,63 @@ class Fir2IrDeclarationStorage( } } is FirRegularClass -> { - irClass.declarations += classifierStorage.createIrClass(declaration, irClass) + val nestedExternalClass = classifierStorage.createIrClass(declaration, irClass) + addDeclarationsToExternalClass(declaration, nestedExternalClass) + irClass.declarations += nestedExternalClass } else -> continue } } + // TODO: better to refactor somehow so as to entirely reuse FakeOverrideGenerator's logic. val allNames = regularClass.collectCallableNamesFromSupertypes(session) for (name in allNames) { if (name in processedNames) continue processedNames += name scope.processFunctionsByName(name) { functionSymbol -> if (functionSymbol is FirNamedFunctionSymbol) { - val fakeOverrideSymbol = FirClassSubstitutionScope.createFakeOverrideFunction( - session, functionSymbol.fir, functionSymbol, derivedClassId = regularClass.symbol.classId - ) - classifierStorage.preCacheTypeParameters(functionSymbol.fir) - irClass.declarations += createIrFunction(fakeOverrideSymbol.fir, irClass) + val originalFunction = functionSymbol.fir + if (functionSymbol.isFakeOverride) { + // Substitution case + val baseSymbol = functionSymbol.deepestOverriddenSymbol() as FirNamedFunctionSymbol + val irFunction = createIrFunction( + originalFunction, irParent = irClass, + thisReceiverOwner = findIrParent(baseSymbol.fir) as? IrClass, + origin = IrDeclarationOrigin.FAKE_OVERRIDE + ) + val overriddenSymbol = getIrFunctionSymbol(baseSymbol) as IrSimpleFunctionSymbol + irClass.declarations += irFunction.apply { + overriddenSymbols = listOf(overriddenSymbol) + } + } else { + val fakeOverrideSymbol = FirClassSubstitutionScope.createFakeOverrideFunction( + session, originalFunction, functionSymbol, derivedClassId = regularClass.symbol.classId + ) + classifierStorage.preCacheTypeParameters(functionSymbol.fir) + irClass.declarations += createIrFunction(fakeOverrideSymbol.fir, irClass) + } } } scope.processPropertiesByName(name) { propertySymbol -> if (propertySymbol is FirPropertySymbol) { - val fakeOverrideSymbol = FirClassSubstitutionScope.createFakeOverrideProperty( - session, propertySymbol.fir, propertySymbol, derivedClassId = regularClass.symbol.classId - ) - classifierStorage.preCacheTypeParameters(propertySymbol.fir) - irClass.declarations += createIrProperty(fakeOverrideSymbol.fir, irClass) + val originalProperty = propertySymbol.fir + if (propertySymbol.isFakeOverride) { + // Substitution case + val baseSymbol = propertySymbol.deepestOverriddenSymbol() as FirPropertySymbol + val irProperty = createIrProperty( + originalProperty, irParent = irClass, + thisReceiverOwner = findIrParent(baseSymbol.fir) as? IrClass, + origin = IrDeclarationOrigin.FAKE_OVERRIDE + ) + irClass.declarations += irProperty.apply { + setOverriddenSymbolsForAccessors(declarationStorage, originalProperty, baseSymbol) + } + } else { + val fakeOverrideSymbol = FirClassSubstitutionScope.createFakeOverrideProperty( + session, originalProperty, propertySymbol, derivedClassId = regularClass.symbol.classId + ) + classifierStorage.preCacheTypeParameters(propertySymbol.fir) + irClass.declarations += createIrProperty(fakeOverrideSymbol.fir, irClass) + } } } } diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/FakeOverrideGenerator.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/FakeOverrideGenerator.kt index b03ea425243..39db26ee248 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/FakeOverrideGenerator.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/FakeOverrideGenerator.kt @@ -41,24 +41,6 @@ internal class FakeOverrideGenerator( return conversionScope.withProperty(conversionScope.applyParentFromStackTo(this), f) } - private fun IrProperty.setOverriddenSymbolsForAccessors( - property: FirProperty, - firOverriddenSymbol: FirPropertySymbol - ): IrProperty { - - val irSymbol = declarationStorage.getIrPropertyOrFieldSymbol(firOverriddenSymbol) as? IrPropertySymbol ?: return this - val overriddenProperty = irSymbol.owner - getter?.apply { - overriddenProperty.getter?.symbol?.let { overriddenSymbols = listOf(it) } - } - if (property.isVar) { - setter?.apply { - overriddenProperty.setter?.symbol?.let { overriddenSymbols = listOf(it) } - } - } - return this - } - private fun FirCallableMemberDeclaration<*>.allowsToHaveFakeOverrideIn(klass: FirClass<*>): Boolean { if (!allowsToHaveFakeOverride) return false if (this.visibility != JavaVisibilities.PACKAGE_VISIBILITY) return true @@ -138,7 +120,7 @@ internal class FakeOverrideGenerator( origin = origin ) declarations += irProperty.withProperty { - setOverriddenSymbolsForAccessors(originalProperty, firOverriddenSymbol = baseSymbol) + setOverriddenSymbolsForAccessors(declarationStorage, originalProperty, firOverriddenSymbol = baseSymbol) } } else if (fakeOverrideMode != FakeOverrideMode.SUBSTITUTION && originalProperty.allowsToHaveFakeOverrideIn(klass)) { // Trivial fake override case @@ -167,12 +149,29 @@ internal class FakeOverrideGenerator( return@processPropertiesByName } declarations += irProperty.withProperty { - setOverriddenSymbolsForAccessors(fakeOverrideProperty, firOverriddenSymbol = propertySymbol) + setOverriddenSymbolsForAccessors(declarationStorage, fakeOverrideProperty, firOverriddenSymbol = propertySymbol) } } } } } } - +} + +internal fun IrProperty.setOverriddenSymbolsForAccessors( + declarationStorage: Fir2IrDeclarationStorage, + property: FirProperty, + firOverriddenSymbol: FirPropertySymbol +): IrProperty { + val irSymbol = declarationStorage.getIrPropertyOrFieldSymbol(firOverriddenSymbol) as? IrPropertySymbol ?: return this + val overriddenProperty = irSymbol.owner + getter?.apply { + overriddenProperty.getter?.symbol?.let { overriddenSymbols = listOf(it) } + } + if (property.isVar) { + setter?.apply { + overriddenProperty.setter?.symbol?.let { overriddenSymbols = listOf(it) } + } + } + return this } diff --git a/compiler/testData/codegen/box/reflection/call/bound/companionObjectPropertyAccessors.kt b/compiler/testData/codegen/box/reflection/call/bound/companionObjectPropertyAccessors.kt index d534cafa8dd..c12ebc3081d 100644 --- a/compiler/testData/codegen/box/reflection/call/bound/companionObjectPropertyAccessors.kt +++ b/compiler/testData/codegen/box/reflection/call/bound/companionObjectPropertyAccessors.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // IGNORE_BACKEND: JS_IR // IGNORE_BACKEND: JS_IR_ES6 // TODO: investigate should it be ran for JS or not diff --git a/compiler/testData/codegen/box/reflection/call/bound/extensionPropertyAccessors.kt b/compiler/testData/codegen/box/reflection/call/bound/extensionPropertyAccessors.kt index aed0b5d0e90..ebb068590aa 100644 --- a/compiler/testData/codegen/box/reflection/call/bound/extensionPropertyAccessors.kt +++ b/compiler/testData/codegen/box/reflection/call/bound/extensionPropertyAccessors.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // IGNORE_BACKEND: JS_IR // IGNORE_BACKEND: JS_IR_ES6 // TODO: investigate should it be ran for JS or not diff --git a/compiler/testData/codegen/box/reflection/call/bound/jvmStaticCompanionObjectPropertyAccessors.kt b/compiler/testData/codegen/box/reflection/call/bound/jvmStaticCompanionObjectPropertyAccessors.kt index 990cdca0cce..17e720ede7b 100644 --- a/compiler/testData/codegen/box/reflection/call/bound/jvmStaticCompanionObjectPropertyAccessors.kt +++ b/compiler/testData/codegen/box/reflection/call/bound/jvmStaticCompanionObjectPropertyAccessors.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // TARGET_BACKEND: JVM // WITH_REFLECT diff --git a/compiler/testData/codegen/box/reflection/call/bound/jvmStaticObjectPropertyAccessors.kt b/compiler/testData/codegen/box/reflection/call/bound/jvmStaticObjectPropertyAccessors.kt index 0ebf828973c..cadc829f879 100644 --- a/compiler/testData/codegen/box/reflection/call/bound/jvmStaticObjectPropertyAccessors.kt +++ b/compiler/testData/codegen/box/reflection/call/bound/jvmStaticObjectPropertyAccessors.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // TARGET_BACKEND: JVM // WITH_REFLECT diff --git a/compiler/testData/codegen/box/reflection/call/bound/memberPropertyAccessors.kt b/compiler/testData/codegen/box/reflection/call/bound/memberPropertyAccessors.kt index f8ad21fe8f9..33a3318975e 100644 --- a/compiler/testData/codegen/box/reflection/call/bound/memberPropertyAccessors.kt +++ b/compiler/testData/codegen/box/reflection/call/bound/memberPropertyAccessors.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // IGNORE_BACKEND: JS_IR // IGNORE_BACKEND: JS_IR_ES6 // TODO: investigate should it be ran for JS or not diff --git a/compiler/testData/codegen/box/reflection/call/bound/objectPropertyAccessors.kt b/compiler/testData/codegen/box/reflection/call/bound/objectPropertyAccessors.kt index 0aabb2a94e7..5e96d1ec340 100644 --- a/compiler/testData/codegen/box/reflection/call/bound/objectPropertyAccessors.kt +++ b/compiler/testData/codegen/box/reflection/call/bound/objectPropertyAccessors.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // IGNORE_BACKEND: JS_IR // IGNORE_BACKEND: JS_IR_ES6 // TODO: investigate should it be ran for JS or not diff --git a/compiler/testData/codegen/box/reflection/call/disallowNullValueForNotNullField.kt b/compiler/testData/codegen/box/reflection/call/disallowNullValueForNotNullField.kt index 10a0d9338b7..6c3cbb1d2ad 100644 --- a/compiler/testData/codegen/box/reflection/call/disallowNullValueForNotNullField.kt +++ b/compiler/testData/codegen/box/reflection/call/disallowNullValueForNotNullField.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // TARGET_BACKEND: JVM // WITH_REFLECT diff --git a/compiler/testData/codegen/box/reflection/call/inlineClasses/fieldAccessors.kt b/compiler/testData/codegen/box/reflection/call/inlineClasses/fieldAccessors.kt index babe1a20145..eee6b146996 100644 --- a/compiler/testData/codegen/box/reflection/call/inlineClasses/fieldAccessors.kt +++ b/compiler/testData/codegen/box/reflection/call/inlineClasses/fieldAccessors.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // TARGET_BACKEND: JVM // WITH_REFLECT diff --git a/compiler/testData/codegen/box/reflection/call/inlineClasses/jvmStaticFieldInObject.kt b/compiler/testData/codegen/box/reflection/call/inlineClasses/jvmStaticFieldInObject.kt index 2bacf68b0e1..626eabedccf 100644 --- a/compiler/testData/codegen/box/reflection/call/inlineClasses/jvmStaticFieldInObject.kt +++ b/compiler/testData/codegen/box/reflection/call/inlineClasses/jvmStaticFieldInObject.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // TARGET_BACKEND: JVM // JVM_TARGET: 1.8 // WITH_REFLECT diff --git a/compiler/testData/codegen/box/reflection/call/inlineClasses/nonOverridingVarOfInlineClass.kt b/compiler/testData/codegen/box/reflection/call/inlineClasses/nonOverridingVarOfInlineClass.kt index d3bc4de1af9..e365dff5fc0 100644 --- a/compiler/testData/codegen/box/reflection/call/inlineClasses/nonOverridingVarOfInlineClass.kt +++ b/compiler/testData/codegen/box/reflection/call/inlineClasses/nonOverridingVarOfInlineClass.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // IGNORE_BACKEND: JS_IR, JS, NATIVE // IGNORE_BACKEND: JS_IR_ES6 // WITH_REFLECT diff --git a/compiler/testData/codegen/box/reflection/call/inlineClasses/overridingVarOfInlineClass.kt b/compiler/testData/codegen/box/reflection/call/inlineClasses/overridingVarOfInlineClass.kt index d512976a440..fff7c09b21d 100644 --- a/compiler/testData/codegen/box/reflection/call/inlineClasses/overridingVarOfInlineClass.kt +++ b/compiler/testData/codegen/box/reflection/call/inlineClasses/overridingVarOfInlineClass.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // IGNORE_BACKEND: JS_IR, JS, NATIVE // IGNORE_BACKEND: JS_IR_ES6 // WITH_REFLECT diff --git a/compiler/testData/codegen/box/reflection/call/inlineClasses/properties.kt b/compiler/testData/codegen/box/reflection/call/inlineClasses/properties.kt index 9b6d59bf181..919589dc174 100644 --- a/compiler/testData/codegen/box/reflection/call/inlineClasses/properties.kt +++ b/compiler/testData/codegen/box/reflection/call/inlineClasses/properties.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // IGNORE_BACKEND: JS_IR, JS, NATIVE // IGNORE_BACKEND: JS_IR_ES6 // WITH_REFLECT diff --git a/compiler/testData/codegen/box/reflection/call/privateProperty.kt b/compiler/testData/codegen/box/reflection/call/privateProperty.kt index a4b86f3d940..a9a4f82a02d 100644 --- a/compiler/testData/codegen/box/reflection/call/privateProperty.kt +++ b/compiler/testData/codegen/box/reflection/call/privateProperty.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // TARGET_BACKEND: JVM // WITH_REFLECT diff --git a/compiler/testData/codegen/box/reflection/call/propertyAccessors.kt b/compiler/testData/codegen/box/reflection/call/propertyAccessors.kt index 7c0888839e5..3dbaa5f91d7 100644 --- a/compiler/testData/codegen/box/reflection/call/propertyAccessors.kt +++ b/compiler/testData/codegen/box/reflection/call/propertyAccessors.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // IGNORE_BACKEND: JS_IR // IGNORE_BACKEND: JS_IR_ES6 // TODO: muted automatically, investigate should it be ran for JS or not diff --git a/compiler/testData/codegen/box/reflection/call/protectedMembers.kt b/compiler/testData/codegen/box/reflection/call/protectedMembers.kt index a9c48a232c6..6cc6f489d92 100644 --- a/compiler/testData/codegen/box/reflection/call/protectedMembers.kt +++ b/compiler/testData/codegen/box/reflection/call/protectedMembers.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // TARGET_BACKEND: JVM // WITH_REFLECT diff --git a/compiler/testData/codegen/box/reflection/callBy/inlineClassMembers.kt b/compiler/testData/codegen/box/reflection/callBy/inlineClassMembers.kt index df843379b4a..c6c099e1bf6 100644 --- a/compiler/testData/codegen/box/reflection/callBy/inlineClassMembers.kt +++ b/compiler/testData/codegen/box/reflection/callBy/inlineClassMembers.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // IGNORE_BACKEND: JS_IR, JS, NATIVE // IGNORE_BACKEND: JS_IR_ES6 // WITH_REFLECT diff --git a/compiler/testData/codegen/box/reflection/multifileClasses/callPropertiesInMultifileClass.kt b/compiler/testData/codegen/box/reflection/multifileClasses/callPropertiesInMultifileClass.kt index ec9a39c7072..406c12f0ab4 100644 --- a/compiler/testData/codegen/box/reflection/multifileClasses/callPropertiesInMultifileClass.kt +++ b/compiler/testData/codegen/box/reflection/multifileClasses/callPropertiesInMultifileClass.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // TARGET_BACKEND: JVM // KT-11447 Multifile declaration causes IAE: Method can not access a member of class diff --git a/compiler/testData/codegen/box/reflection/properties/accessors/extensionPropertyAccessors.kt b/compiler/testData/codegen/box/reflection/properties/accessors/extensionPropertyAccessors.kt index b8cefa7347e..e1787bfe3a1 100644 --- a/compiler/testData/codegen/box/reflection/properties/accessors/extensionPropertyAccessors.kt +++ b/compiler/testData/codegen/box/reflection/properties/accessors/extensionPropertyAccessors.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // IGNORE_BACKEND: JS_IR // IGNORE_BACKEND: JS_IR_ES6 // TODO: muted automatically, investigate should it be ran for JS or not diff --git a/compiler/testData/codegen/box/reflection/properties/accessors/memberExtensions.kt b/compiler/testData/codegen/box/reflection/properties/accessors/memberExtensions.kt index 104df67ed11..ca47181fc02 100644 --- a/compiler/testData/codegen/box/reflection/properties/accessors/memberExtensions.kt +++ b/compiler/testData/codegen/box/reflection/properties/accessors/memberExtensions.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // IGNORE_BACKEND: JS_IR // IGNORE_BACKEND: JS_IR_ES6 // TODO: muted automatically, investigate should it be ran for JS or not diff --git a/compiler/testData/codegen/box/reflection/properties/accessors/memberPropertyAccessors.kt b/compiler/testData/codegen/box/reflection/properties/accessors/memberPropertyAccessors.kt index a10c5de8c44..7ead01f0255 100644 --- a/compiler/testData/codegen/box/reflection/properties/accessors/memberPropertyAccessors.kt +++ b/compiler/testData/codegen/box/reflection/properties/accessors/memberPropertyAccessors.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // IGNORE_BACKEND: JS_IR // IGNORE_BACKEND: JS_IR_ES6 // TODO: muted automatically, investigate should it be ran for JS or not diff --git a/compiler/testData/codegen/box/reflection/properties/accessors/topLevelPropertyAccessors.kt b/compiler/testData/codegen/box/reflection/properties/accessors/topLevelPropertyAccessors.kt index 374d1425ac6..47305ad34f9 100644 --- a/compiler/testData/codegen/box/reflection/properties/accessors/topLevelPropertyAccessors.kt +++ b/compiler/testData/codegen/box/reflection/properties/accessors/topLevelPropertyAccessors.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // IGNORE_BACKEND: JS_IR // IGNORE_BACKEND: JS_IR_ES6 // TODO: muted automatically, investigate should it be ran for JS or not diff --git a/compiler/testData/codegen/box/reflection/properties/privatePropertyCallIsAccessibleOnAccessors.kt b/compiler/testData/codegen/box/reflection/properties/privatePropertyCallIsAccessibleOnAccessors.kt index 6d8659e025e..7536cc3e2c0 100644 --- a/compiler/testData/codegen/box/reflection/properties/privatePropertyCallIsAccessibleOnAccessors.kt +++ b/compiler/testData/codegen/box/reflection/properties/privatePropertyCallIsAccessibleOnAccessors.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // TARGET_BACKEND: JVM // WITH_REFLECT