[FIR2IR] Change receiver of field of base class in derived class in property reference

This commit is contained in:
Dmitriy Novozhilov
2021-03-29 10:25:33 +03:00
parent 38ab37d7eb
commit a9399535fb
7 changed files with 250 additions and 6 deletions
@@ -95,7 +95,7 @@ class Fir2IrDeclarationStorage(
private val fakeOverridesInClass = mutableMapOf<IrClass, MutableMap<FirCallableDeclaration<*>, FirCallableDeclaration<*>>>()
// For pure fields (from Java) only
private val fieldToPropertyCache = ConcurrentHashMap<FirField, IrProperty>()
private val fieldToPropertyCache = ConcurrentHashMap<Pair<FirField, IrDeclarationParent>, IrProperty>()
private val delegatedReverseCache = ConcurrentHashMap<IrDeclaration, FirDeclaration>()
@@ -719,9 +719,13 @@ class Fir2IrDeclarationStorage(
field: FirField,
irParent: IrDeclarationParent
): IrProperty {
fieldToPropertyCache[field]?.let { return it }
return createIrProperty(field.toStubProperty(), irParent).apply {
fieldToPropertyCache[field] = this
return fieldToPropertyCache.getOrPut(field to irParent) {
val containingClassId = (irParent as? IrClass)?.classId
createIrProperty(
field.toStubProperty(),
irParent,
containingClass = containingClassId?.let { ConeClassLikeLookupTagImpl(it) }
)
}
}
@@ -39,6 +39,7 @@ import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.*
import org.jetbrains.kotlin.ir.symbols.*
import org.jetbrains.kotlin.ir.symbols.impl.IrFieldSymbolImpl
import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.psi2ir.generators.hasNoSideEffects
@@ -103,7 +104,8 @@ class CallAndReferenceGenerator(
)
}
is IrFieldSymbol -> {
val referencedField = symbol.owner
val fieldSymbol = computeFieldSymbolForCallableReference(callableReferenceAccess, symbol)
val referencedField = fieldSymbol.owner
val propertySymbol = referencedField.correspondingPropertySymbol
?: run {
// In case of [IrField] without the corresponding property, we've created it directly from [FirField].
@@ -116,7 +118,7 @@ class CallAndReferenceGenerator(
startOffset, endOffset, type,
propertySymbol,
typeArgumentsCount = (type as? IrSimpleType)?.arguments?.size ?: 0,
field = symbol,
field = fieldSymbol,
getter = if (referencedField.isStatic) null else propertySymbol.owner.getter?.symbol,
setter = if (referencedField.isStatic) null else propertySymbol.owner.setter?.symbol,
origin
@@ -154,6 +156,33 @@ class CallAndReferenceGenerator(
}.applyTypeArguments(callableReferenceAccess).applyReceivers(callableReferenceAccess, explicitReceiverExpression)
}
private fun computeFieldSymbolForCallableReference(
callableReferenceAccess: FirCallableReferenceAccess,
symbol: IrFieldSymbol
): IrFieldSymbol {
val receiverClass = when (val receiverExpression = callableReferenceAccess.explicitReceiver) {
is FirResolvedQualifier -> receiverExpression.symbol
null -> null
else -> receiverExpression.typeRef.coneType.toSymbol(session)
} as? FirClassSymbol<*>
val newParent = receiverClass?.let { classifierStorage.getIrClassSymbol(it) }?.owner ?: return symbol
return IrFieldSymbolImpl().also { newSymbol ->
val field = symbol.owner
irFactory.createField(
startOffset = -1,
endOffset = -1,
field.origin,
newSymbol,
field.name,
field.type,
field.visibility,
field.isFinal,
field.isExternal,
field.isStatic,
).also { it.parent = newParent }
}
}
private fun FirQualifiedAccess.tryConvertToSamConstructorCall(type: IrType): IrTypeOperatorCall? {
val calleeReference = calleeReference as? FirResolvedNamedReference ?: return null
val fir = calleeReference.resolvedSymbol.fir
@@ -15098,6 +15098,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/fir"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@Test
@TestMetadata("callableReferenceToJavaField.kt")
public void testCallableReferenceToJavaField() throws Exception {
runTest("compiler/testData/codegen/box/fir/callableReferenceToJavaField.kt");
}
@Test
@TestMetadata("callableReferenceToStaticFunction.kt")
public void testCallableReferenceToStaticFunction() throws Exception {