FIR2IR: add local delegated property generation

This commit is contained in:
pyos
2020-09-07 14:11:17 +02:00
committed by Mikhail Glukhikh
parent 4792be2522
commit f198a19ab0
43 changed files with 211 additions and 217 deletions
@@ -149,11 +149,9 @@ private fun FirCallableSymbol<*>.toSymbol(declarationStorage: Fir2IrDeclarationS
} else {
syntheticProperty.setter!!.delegate.symbol.toSymbol(declarationStorage, preferGetter)
}
} ?: if (fir.isLocal) declarationStorage.getIrValueSymbol(this) else declarationStorage.getIrPropertyOrFieldSymbol(this)
}
is FirPropertySymbol -> {
if (fir.isLocal) declarationStorage.getIrValueSymbol(this) else declarationStorage.getIrPropertyOrFieldSymbol(this)
} ?: fir.toSymbol(declarationStorage)
}
is FirPropertySymbol -> fir.toSymbol(declarationStorage)
is FirFieldSymbol -> declarationStorage.getIrPropertyOrFieldSymbol(this)
is FirBackingFieldSymbol -> declarationStorage.getIrBackingFieldSymbol(this)
is FirDelegateFieldSymbol<*> -> declarationStorage.getIrBackingFieldSymbol(this)
@@ -161,6 +159,12 @@ private fun FirCallableSymbol<*>.toSymbol(declarationStorage: Fir2IrDeclarationS
else -> null
}
private fun FirProperty.toSymbol(declarationStorage: Fir2IrDeclarationStorage): IrSymbol? = when {
!isLocal -> declarationStorage.getIrPropertyOrFieldSymbol(symbol)
delegate != null -> declarationStorage.getIrLocalDelegatedPropertySymbol(symbol)
else -> declarationStorage.getIrValueSymbol(symbol)
}
fun FirConstExpression<*>.getIrConstKind(): IrConstKind<*> = when (kind) {
FirConstKind.IntegerLiteral -> {
val type = typeRef.coneTypeUnsafe<ConeIntegerLiteralType>()
@@ -525,7 +525,7 @@ class Fir2IrDeclarationStorage(
internal fun createIrPropertyAccessor(
propertyAccessor: FirPropertyAccessor?,
property: FirProperty,
correspondingProperty: IrProperty,
correspondingProperty: IrDeclarationWithName,
propertyType: IrType,
irParent: IrDeclarationParent?,
thisReceiverOwner: IrClass? = irParent as? IrClass,
@@ -537,7 +537,7 @@ class Fir2IrDeclarationStorage(
): IrSimpleFunction {
val prefix = if (isSetter) "set" else "get"
val signature = if (isLocal) null else signatureComposer.composeAccessorSignature(property, isSetter)
val containerSource = correspondingProperty.containerSource
val containerSource = (correspondingProperty as? IrProperty)?.containerSource
return declareIrAccessor(
signature,
containerSource,
@@ -550,8 +550,8 @@ class Fir2IrDeclarationStorage(
irFactory.createFunction(
startOffset, endOffset, origin, symbol,
Name.special("<$prefix-${correspondingProperty.name}>"),
visibility ?: correspondingProperty.visibility,
correspondingProperty.modality, accessorReturnType,
visibility ?: (correspondingProperty as IrDeclarationWithVisibility).visibility,
(correspondingProperty as? IrOverridableMember)?.modality ?: Modality.FINAL, accessorReturnType,
isInline = propertyAccessor?.isInline == true,
isExternal = propertyAccessor?.isExternal == true,
isTailrec = false, isSuspend = false, isOperator = false,
@@ -559,7 +559,7 @@ class Fir2IrDeclarationStorage(
isExpect = false, isFakeOverride = origin == IrDeclarationOrigin.FAKE_OVERRIDE,
containerSource = containerSource,
).apply {
correspondingPropertySymbol = correspondingProperty.symbol
correspondingPropertySymbol = (correspondingProperty as? IrProperty)?.symbol
if (propertyAccessor != null) {
metadata = FirMetadataSource.Function(propertyAccessor)
// Note that deserialized annotations are stored in the accessor, not the property.
@@ -883,6 +883,41 @@ class Fir2IrDeclarationStorage(
return irVariable
}
fun createIrLocalDelegatedProperty(property: FirProperty, irParent: IrDeclarationParent): IrLocalDelegatedProperty {
val type = property.returnTypeRef.toIrType()
val origin = IrDeclarationOrigin.DEFINED
val irProperty = property.convertWithOffsets { startOffset, endOffset ->
val descriptor = WrappedVariableDescriptorWithAccessor()
symbolTable.declareLocalDelegatedProperty(startOffset, endOffset, origin, descriptor, type) {
irFactory.createLocalDelegatedProperty(startOffset, endOffset, origin, it, property.name, type, property.isVar).apply {
descriptor.bind(this)
}
}
}.apply {
parent = irParent
enterScope(this)
delegate = declareIrVariable(
startOffset, endOffset, IrDeclarationOrigin.PROPERTY_DELEGATE,
Name.identifier("${property.name}\$delegate"), property.delegate!!.typeRef.toIrType(),
isVar = false, isConst = false, isLateinit = false
)
delegate.parent = irParent
getter = createIrPropertyAccessor(
property.getter, property, this, type, irParent, null, false,
IrDeclarationOrigin.DELEGATED_PROPERTY_ACCESSOR, startOffset, endOffset, isLocal
)
if (property.isVar) {
setter = createIrPropertyAccessor(
property.setter, property, this, type, irParent, null, true,
IrDeclarationOrigin.DELEGATED_PROPERTY_ACCESSOR, startOffset, endOffset, isLocal
)
}
leaveScope(this)
}
localStorage.putDelegatedProperty(property, irProperty)
return irProperty
}
fun declareTemporaryVariable(base: IrExpression, nameHint: String? = null): IrVariable {
return declareIrVariable(
base.startOffset, base.endOffset, IrDeclarationOrigin.IR_TEMPORARY_VARIABLE,
@@ -1029,6 +1064,9 @@ class Fir2IrDeclarationStorage(
fun getIrBackingFieldSymbol(firVariableSymbol: FirVariableSymbol<*>): IrSymbol {
return when (val fir = firVariableSymbol.fir) {
is FirProperty -> {
if (fir.isLocal) {
return localStorage.getDelegatedProperty(fir)?.delegate?.symbol ?: getIrVariableSymbol(fir)
}
propertyCache[fir]?.let { return it.backingField!!.symbol }
val irParent = findIrParent(fir)
val parentOrigin = (irParent as? IrDeclaration)?.origin ?: IrDeclarationOrigin.DEFINED
@@ -1047,6 +1085,11 @@ class Fir2IrDeclarationStorage(
?: throw IllegalArgumentException("Cannot find variable ${firVariable.render()} in local storage")
}
fun getIrLocalDelegatedPropertySymbol(firPropertySymbol: FirPropertySymbol): IrSymbol {
return localStorage.getDelegatedProperty(firPropertySymbol.fir)?.symbol
?: throw IllegalArgumentException("Cannot find delegated property ${firPropertySymbol.fir.render()} in local storage")
}
fun getIrValueSymbol(firVariableSymbol: FirVariableSymbol<*>): IrSymbol {
return when (val firDeclaration = firVariableSymbol.fir) {
is FirEnumEntry -> {
@@ -6,10 +6,7 @@
package org.jetbrains.kotlin.fir.backend
import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
import org.jetbrains.kotlin.ir.declarations.IrValueParameter
import org.jetbrains.kotlin.ir.declarations.IrVariable
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.name.ClassId
class Fir2IrLocalStorage {
@@ -35,13 +32,8 @@ class Fir2IrLocalStorage {
return null
}
fun getVariable(variable: FirVariable<*>): IrVariable? {
for (cache in cacheStack.asReversed()) {
val local = cache.getVariable(variable)
if (local != null) return local
}
return null
}
fun getVariable(variable: FirVariable<*>): IrVariable? =
last { getVariable(variable) }
fun getLocalClass(localClass: FirClass<*>): IrClass? {
return localClassCache[localClass]
@@ -51,10 +43,15 @@ class Fir2IrLocalStorage {
return localClassCache.entries.find { (firClass, _) -> firClass.classId == classId }?.value
}
fun getLocalFunction(localFunction: FirFunction<*>): IrSimpleFunction? {
fun getLocalFunction(localFunction: FirFunction<*>): IrSimpleFunction? =
last { getLocalFunction(localFunction) }
fun getDelegatedProperty(property: FirProperty): IrLocalDelegatedProperty? =
last { getDelegatedProperty(property) }
private inline fun <T> last(getter: Fir2IrScopeCache.() -> T?): T? {
for (cache in cacheStack.asReversed()) {
val local = cache.getLocalFunction(localFunction)
if (local != null) return local
cache.getter()?.let { return it }
}
return null
}
@@ -74,4 +71,8 @@ class Fir2IrLocalStorage {
fun putLocalFunction(firFunction: FirFunction<*>, irFunction: IrSimpleFunction) {
cacheStack.last().putLocalFunction(firFunction, irFunction)
}
fun putDelegatedProperty(firProperty: FirProperty, irProperty: IrLocalDelegatedProperty) {
cacheStack.last().putDelegatedProperty(firProperty, irProperty)
}
}
@@ -7,6 +7,7 @@ package org.jetbrains.kotlin.fir.backend
import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.ir.declarations.IrLocalDelegatedProperty
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
import org.jetbrains.kotlin.ir.declarations.IrValueParameter
import org.jetbrains.kotlin.ir.declarations.IrVariable
@@ -18,6 +19,8 @@ class Fir2IrScopeCache {
private val localFunctionCache = mutableMapOf<FirFunction<*>, IrSimpleFunction>()
private val delegatedPropertyCache = mutableMapOf<FirProperty, IrLocalDelegatedProperty>()
fun getParameter(parameter: FirValueParameter): IrValueParameter? = parameterCache[parameter]
fun putParameter(firParameter: FirValueParameter, irParameter: IrValueParameter) {
@@ -37,9 +40,16 @@ class Fir2IrScopeCache {
localFunctionCache[localFunction] = irFunction
}
fun getDelegatedProperty(property: FirProperty): IrLocalDelegatedProperty? = delegatedPropertyCache[property]
fun putDelegatedProperty(firProperty: FirProperty, irProperty: IrLocalDelegatedProperty) {
delegatedPropertyCache[firProperty] = irProperty
}
fun clear() {
parameterCache.clear()
variableCache.clear()
localFunctionCache.clear()
delegatedPropertyCache.clear()
}
}
@@ -254,6 +254,20 @@ class Fir2IrVisitor(
private fun visitLocalVariable(variable: FirProperty): IrElement {
assert(variable.isLocal)
val delegate = variable.delegate
if (delegate != null) {
val irProperty = declarationStorage.createIrLocalDelegatedProperty(variable, conversionScope.parentFromStack())
irProperty.delegate.initializer = convertToIrExpression(delegate)
conversionScope.withFunction(irProperty.getter) {
memberGenerator.convertFunctionContent(irProperty.getter, variable.getter, null)
}
irProperty.setter?.let {
conversionScope.withFunction(it) {
memberGenerator.convertFunctionContent(it, variable.setter, null)
}
}
return irProperty
}
val initializer = variable.initializer
val isNextVariable = initializer is FirFunctionCall &&
initializer.resolvedNamedFunctionSymbol()?.callableId?.isIteratorNext() == true &&
@@ -79,6 +79,15 @@ class CallAndReferenceGenerator(
origin
)
}
is IrLocalDelegatedPropertySymbol -> {
IrLocalDelegatedPropertyReferenceImpl(
startOffset, endOffset, type, symbol,
symbol.owner.delegate.symbol,
symbol.owner.getter.symbol as IrSimpleFunctionSymbol,
symbol.owner.setter?.symbol as IrSimpleFunctionSymbol?,
IrStatementOrigin.PROPERTY_REFERENCE_FOR_DELEGATE
)
}
is IrConstructorSymbol -> {
val constructor = symbol.owner
val klass = constructor.parent as? IrClass
@@ -489,6 +498,15 @@ class CallAndReferenceGenerator(
superQualifierSymbol = dispatchReceiver.superQualifierSymbol(symbol)
)
}
is IrLocalDelegatedPropertySymbol -> {
IrCallImpl(
startOffset, endOffset, type, symbol.owner.getter.symbol as IrSimpleFunctionSymbol,
typeArgumentsCount = symbol.owner.getter.typeParameters.size,
valueArgumentsCount = 0,
origin = IrStatementOrigin.GET_LOCAL_PROPERTY,
superQualifierSymbol = dispatchReceiver.superQualifierSymbol(symbol)
)
}
is IrPropertySymbol -> {
val getter = symbol.owner.getter
val backingField = symbol.owner.backingField
@@ -541,6 +559,21 @@ class CallAndReferenceGenerator(
is IrFieldSymbol -> IrSetFieldImpl(startOffset, endOffset, symbol, type, origin).apply {
value = assignedValue
}
is IrLocalDelegatedPropertySymbol -> {
val setter = symbol.owner.setter
when {
setter != null -> IrCallImpl(
startOffset, endOffset, type, setter.symbol as IrSimpleFunctionSymbol,
typeArgumentsCount = setter.typeParameters.size,
valueArgumentsCount = 1,
origin = origin,
superQualifierSymbol = variableAssignment.dispatchReceiver.superQualifierSymbol(symbol)
).apply {
putValueArgument(0, assignedValue)
}
else -> generateErrorCallExpression(startOffset, endOffset, calleeReference)
}
}
is IrPropertySymbol -> {
val irProperty = symbol.owner
val setter = irProperty.setter