FIR2IR: split getIrPropertyOrFieldSymbol, handle locals there

This commit is contained in:
pyos
2020-09-07 14:40:57 +02:00
committed by Mikhail Glukhikh
parent f198a19ab0
commit 75891e860b
4 changed files with 50 additions and 62 deletions
@@ -130,7 +130,7 @@ fun FirReference.toSymbol(
is FirClassSymbol<*> -> classifierStorage.getIrClassSymbol(boundSymbol).owner.thisReceiver?.symbol is FirClassSymbol<*> -> classifierStorage.getIrClassSymbol(boundSymbol).owner.thisReceiver?.symbol
is FirFunctionSymbol -> declarationStorage.getIrFunctionSymbol(boundSymbol).owner.extensionReceiverParameter?.symbol is FirFunctionSymbol -> declarationStorage.getIrFunctionSymbol(boundSymbol).owner.extensionReceiverParameter?.symbol
is FirPropertySymbol -> { is FirPropertySymbol -> {
val property = declarationStorage.getIrPropertyOrFieldSymbol(boundSymbol).owner as? IrProperty val property = declarationStorage.getIrPropertySymbol(boundSymbol).owner as? IrProperty
property?.let { conversionScope.parentAccessorOfPropertyFromStack(it) }?.symbol property?.let { conversionScope.parentAccessorOfPropertyFromStack(it) }?.symbol
} }
else -> null else -> null
@@ -149,22 +149,16 @@ private fun FirCallableSymbol<*>.toSymbol(declarationStorage: Fir2IrDeclarationS
} else { } else {
syntheticProperty.setter!!.delegate.symbol.toSymbol(declarationStorage, preferGetter) syntheticProperty.setter!!.delegate.symbol.toSymbol(declarationStorage, preferGetter)
} }
} ?: fir.toSymbol(declarationStorage) } ?: declarationStorage.getIrPropertySymbol(this)
} }
is FirPropertySymbol -> fir.toSymbol(declarationStorage) is FirPropertySymbol -> declarationStorage.getIrPropertySymbol(this)
is FirFieldSymbol -> declarationStorage.getIrPropertyOrFieldSymbol(this) is FirFieldSymbol -> declarationStorage.getIrFieldSymbol(this)
is FirBackingFieldSymbol -> declarationStorage.getIrBackingFieldSymbol(this) is FirBackingFieldSymbol -> declarationStorage.getIrBackingFieldSymbol(this)
is FirDelegateFieldSymbol<*> -> declarationStorage.getIrBackingFieldSymbol(this) is FirDelegateFieldSymbol<*> -> declarationStorage.getIrBackingFieldSymbol(this)
is FirVariableSymbol<*> -> declarationStorage.getIrValueSymbol(this) is FirVariableSymbol<*> -> declarationStorage.getIrValueSymbol(this)
else -> null 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) { fun FirConstExpression<*>.getIrConstKind(): IrConstKind<*> = when (kind) {
FirConstKind.IntegerLiteral -> { FirConstKind.IntegerLiteral -> {
val type = typeRef.coneTypeUnsafe<ConeIntegerLiteralType>() val type = typeRef.coneTypeUnsafe<ConeIntegerLiteralType>()
@@ -327,7 +321,7 @@ internal fun FirProperty.generateOverriddenAccessorSymbols(
if (it.fir.visibility == Visibilities.Private) { if (it.fir.visibility == Visibilities.Private) {
return@processDirectlyOverriddenProperties ProcessorAction.NEXT return@processDirectlyOverriddenProperties ProcessorAction.NEXT
} }
val overriddenProperty = declarationStorage.getIrPropertyOrFieldSymbol(it.unwrapSubstitutionOverrides()) as IrPropertySymbol val overriddenProperty = declarationStorage.getIrPropertySymbol(it.unwrapSubstitutionOverrides()) as IrPropertySymbol
val overriddenAccessor = if (isGetter) overriddenProperty.owner.getter?.symbol else overriddenProperty.owner.setter?.symbol val overriddenAccessor = if (isGetter) overriddenProperty.owner.getter?.symbol else overriddenProperty.owner.setter?.symbol
if (overriddenAccessor != null) { if (overriddenAccessor != null) {
overriddenSet += overriddenAccessor overriddenSet += overriddenAccessor
@@ -9,8 +9,6 @@ import org.jetbrains.kotlin.KtNodeTypes
import org.jetbrains.kotlin.builtins.StandardNames.BUILT_INS_PACKAGE_FQ_NAMES import org.jetbrains.kotlin.builtins.StandardNames.BUILT_INS_PACKAGE_FQ_NAMES
import org.jetbrains.kotlin.descriptors.Modality import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.PropertyDescriptor import org.jetbrains.kotlin.descriptors.PropertyDescriptor
import org.jetbrains.kotlin.descriptors.SourceElement
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.fir.FirAnnotationContainer import org.jetbrains.kotlin.fir.FirAnnotationContainer
import org.jetbrains.kotlin.descriptors.Visibilities import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.descriptors.Visibility import org.jetbrains.kotlin.descriptors.Visibility
@@ -1012,53 +1010,54 @@ class Fir2IrDeclarationStorage(
} }
} }
fun getIrPropertyOrFieldSymbol(firVariableSymbol: FirVariableSymbol<*>): IrSymbol { fun getIrPropertySymbol(firPropertySymbol: FirPropertySymbol): IrSymbol {
return when (val fir = firVariableSymbol.fir) { val fir = firPropertySymbol.fir
is FirProperty -> { if (fir.isLocal) {
propertyCache[fir]?.let { return it.symbol } return localStorage.getDelegatedProperty(fir)?.symbol ?: getIrVariableSymbol(fir)
val signature = signatureComposer.composeSignature(fir) }
val irParent = findIrParent(fir) propertyCache[fir]?.let { return it.symbol }
val parentOrigin = (irParent as? IrDeclaration)?.origin ?: IrDeclarationOrigin.DEFINED val signature = signatureComposer.composeSignature(fir)
if (signature != null) { val irParent = findIrParent(fir)
symbolTable.referencePropertyIfAny(signature)?.let { irPropertySymbol -> val parentOrigin = (irParent as? IrDeclaration)?.origin ?: IrDeclarationOrigin.DEFINED
val irProperty = irPropertySymbol.owner if (signature != null) {
propertyCache[fir] = irProperty symbolTable.referencePropertyIfAny(signature)?.let { irPropertySymbol ->
return irPropertySymbol val irProperty = irPropertySymbol.owner
} propertyCache[fir] = irProperty
// TODO: package fragment members (?) return irPropertySymbol
if (irParent is Fir2IrLazyClass) { }
assert(parentOrigin != IrDeclarationOrigin.DEFINED) { // TODO: package fragment members (?)
"Should not have reference to public API uncached property from source code" if (irParent is Fir2IrLazyClass) {
assert(parentOrigin != IrDeclarationOrigin.DEFINED) {
"Should not have reference to public API uncached property from source code"
}
val symbol = Fir2IrPropertySymbol(signature, fir.containerSource)
val irProperty = fir.convertWithOffsets { startOffset, endOffset ->
symbolTable.declareProperty(signature, { symbol }) {
val isFakeOverride =
firPropertySymbol.isFakeOverride &&
firPropertySymbol.callableId != firPropertySymbol.overriddenSymbol?.callableId
Fir2IrLazyProperty(
components, startOffset, endOffset, parentOrigin, fir, irParent.fir, symbol, isFakeOverride
).apply {
parent = irParent
} }
val symbol = Fir2IrPropertySymbol(signature, fir.containerSource)
val irProperty = fir.convertWithOffsets { startOffset, endOffset ->
symbolTable.declareProperty(signature, { symbol }) {
val isFakeOverride =
firVariableSymbol is FirPropertySymbol && firVariableSymbol.isFakeOverride &&
firVariableSymbol.callableId != firVariableSymbol.overriddenSymbol?.callableId
Fir2IrLazyProperty(
components, startOffset, endOffset, parentOrigin, fir, irParent.fir, symbol, isFakeOverride
).apply {
parent = irParent
}
}
}
propertyCache[fir] = irProperty
return symbol
} }
} }
createIrProperty(fir, irParent, origin = parentOrigin).apply { propertyCache[fir] = irProperty
setAndModifyParent(irParent) return symbol
}.symbol
} }
is FirField -> {
fieldCache[fir]?.let { return it.symbol }
createIrField(fir).apply {
setAndModifyParent(findIrParent(fir))
}.symbol
}
else -> throw IllegalArgumentException("Unexpected fir in property symbol: ${fir.render()}")
} }
return createIrProperty(fir, irParent, origin = parentOrigin).apply {
setAndModifyParent(irParent)
}.symbol
}
fun getIrFieldSymbol(firFieldSymbol: FirFieldSymbol): IrSymbol {
val fir = firFieldSymbol.fir
val irProperty = fieldCache[fir] ?: createIrField(fir).apply {
setAndModifyParent(findIrParent(fir))
}
return irProperty.symbol
} }
fun getIrBackingFieldSymbol(firVariableSymbol: FirVariableSymbol<*>): IrSymbol { fun getIrBackingFieldSymbol(firVariableSymbol: FirVariableSymbol<*>): IrSymbol {
@@ -1085,11 +1084,6 @@ class Fir2IrDeclarationStorage(
?: throw IllegalArgumentException("Cannot find variable ${firVariable.render()} in local storage") ?: 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 { fun getIrValueSymbol(firVariableSymbol: FirVariableSymbol<*>): IrSymbol {
return when (val firDeclaration = firVariableSymbol.fir) { return when (val firDeclaration = firVariableSymbol.fir) {
is FirEnumEntry -> { is FirEnumEntry -> {
@@ -219,7 +219,7 @@ class FakeOverrideGenerator(
isVar: Boolean, isVar: Boolean,
firOverriddenSymbol: FirPropertySymbol firOverriddenSymbol: FirPropertySymbol
): IrProperty { ): IrProperty {
val irSymbol = declarationStorage.getIrPropertyOrFieldSymbol(firOverriddenSymbol) as? IrPropertySymbol ?: return this val irSymbol = declarationStorage.getIrPropertySymbol(firOverriddenSymbol) as? IrPropertySymbol ?: return this
val overriddenProperty = irSymbol.owner val overriddenProperty = irSymbol.owner
getter?.apply { getter?.apply {
overriddenProperty.getter?.symbol?.let { overriddenSymbols = listOf(it) } overriddenProperty.getter?.symbol?.let { overriddenSymbols = listOf(it) }
@@ -155,7 +155,7 @@ class Fir2IrLazyClass(
processedNames += declaration.name processedNames += declaration.name
scope.processPropertiesByName(declaration.name) { scope.processPropertiesByName(declaration.name) {
if (it is FirPropertySymbol) { if (it is FirPropertySymbol) {
result += declarationStorage.getIrPropertyOrFieldSymbol(it).owner as IrProperty result += declarationStorage.getIrPropertySymbol(it).owner as IrProperty
} }
} }
} }