FIR: Refine FirDelegatedMemberScope

- Use an attribute instead of overriddenSymbol
- Use createCopyForFir because it copies type parameters
This commit is contained in:
Denis Zharkov
2020-09-24 14:30:50 +03:00
parent 28c536e511
commit e311a60055
@@ -8,20 +8,17 @@ package org.jetbrains.kotlin.fir.scopes.impl
import org.jetbrains.kotlin.descriptors.Modality import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.fir.FirSession import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.declarations.* import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.declarations.builder.buildProperty
import org.jetbrains.kotlin.fir.declarations.builder.buildSimpleFunction
import org.jetbrains.kotlin.fir.declarations.impl.FirDeclarationStatusImpl
import org.jetbrains.kotlin.fir.scopes.FirTypeScope import org.jetbrains.kotlin.fir.scopes.FirTypeScope
import org.jetbrains.kotlin.fir.scopes.ProcessorAction import org.jetbrains.kotlin.fir.scopes.ProcessorAction
import org.jetbrains.kotlin.fir.symbols.impl.FirFunctionSymbol import org.jetbrains.kotlin.fir.symbols.ConeClassLikeLookupTag
import org.jetbrains.kotlin.fir.symbols.impl.FirNamedFunctionSymbol import org.jetbrains.kotlin.fir.symbols.impl.*
import org.jetbrains.kotlin.fir.symbols.impl.FirPropertySymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirVariableSymbol
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
class FirDelegatedMemberScope( class FirDelegatedMemberScope(
private val useSiteScope: FirTypeScope, private val useSiteScope: FirTypeScope,
private val session: FirSession private val session: FirSession,
private val containingClass: ConeClassLikeLookupTag,
private val delegateField: FirField,
) : FirTypeScope() { ) : FirTypeScope() {
private val delegatedFunctionCache = mutableMapOf<FirNamedFunctionSymbol, FirNamedFunctionSymbol>() private val delegatedFunctionCache = mutableMapOf<FirNamedFunctionSymbol, FirNamedFunctionSymbol>()
private val delegatedPropertyCache = mutableMapOf<FirPropertySymbol, FirPropertySymbol>() private val delegatedPropertyCache = mutableMapOf<FirPropertySymbol, FirPropertySymbol>()
@@ -38,25 +35,18 @@ class FirDelegatedMemberScope(
return@processor return@processor
} }
val delegatedSymbol = delegatedFunctionCache.getOrPut(functionSymbol) { val delegatedSymbol = delegatedFunctionCache.getOrPut(functionSymbol) {
val delegatedFunction = buildSimpleFunction { val newSymbol = FirNamedFunctionSymbol(
origin = FirDeclarationOrigin.Delegated functionSymbol.callableId,
session = this@FirDelegatedMemberScope.session )
this.name = name FirClassSubstitutionScope.createCopyForFirFunction(
symbol = FirNamedFunctionSymbol( newSymbol,
functionSymbol.callableId, original,
overriddenSymbol = functionSymbol session,
) FirDeclarationOrigin.Delegated,
status = FirDeclarationStatusImpl(original.visibility, Modality.OPEN).apply { newModality = Modality.OPEN,
isOperator = original.isOperator ).apply {
} delegatedWrapperData = DelegatedWrapperData(functionSymbol.fir, containingClass, delegateField)
resolvePhase = FirResolvePhase.BODY_RESOLVE }.symbol as FirNamedFunctionSymbol
returnTypeRef = original.returnTypeRef
receiverTypeRef = original.receiverTypeRef
valueParameters.addAll(original.valueParameters)
typeParameters.addAll(original.typeParameters)
annotations.addAll(original.annotations)
}
delegatedFunction.symbol as FirNamedFunctionSymbol
} }
processor(delegatedSymbol) processor(delegatedSymbol)
} }
@@ -74,24 +64,17 @@ class FirDelegatedMemberScope(
return@processor return@processor
} }
val delegatedSymbol = delegatedPropertyCache.getOrPut(propertySymbol) { val delegatedSymbol = delegatedPropertyCache.getOrPut(propertySymbol) {
val delegatedProperty = buildProperty { FirClassSubstitutionScope.createCopyForFirProperty(
origin = FirDeclarationOrigin.Delegated FirPropertySymbol(
session = this@FirDelegatedMemberScope.session
this.name = name
symbol = FirPropertySymbol(
propertySymbol.callableId, propertySymbol.callableId,
overriddenSymbol = propertySymbol overriddenSymbol = propertySymbol
) ),
isVar = original.isVar original,
isLocal = false session,
status = FirDeclarationStatusImpl(original.visibility, Modality.OPEN) newModality = Modality.OPEN,
resolvePhase = FirResolvePhase.BODY_RESOLVE ).apply {
returnTypeRef = original.returnTypeRef delegatedWrapperData = DelegatedWrapperData(propertySymbol.fir, containingClass, delegateField)
receiverTypeRef = original.receiverTypeRef }.symbol
typeParameters.addAll(original.typeParameters)
annotations.addAll(original.annotations)
}
delegatedProperty.symbol
} }
processor(delegatedSymbol) processor(delegatedSymbol)
} }
@@ -101,14 +84,32 @@ class FirDelegatedMemberScope(
functionSymbol: FirFunctionSymbol<*>, functionSymbol: FirFunctionSymbol<*>,
processor: (FirFunctionSymbol<*>, FirTypeScope) -> ProcessorAction processor: (FirFunctionSymbol<*>, FirTypeScope) -> ProcessorAction
): ProcessorAction { ): ProcessorAction {
return useSiteScope.processDirectOverriddenFunctionsWithBaseScope(functionSymbol, processor) return processDirectOverriddenWithBaseScope(
functionSymbol, processor, FirTypeScope::processDirectOverriddenFunctionsWithBaseScope
)
} }
override fun processDirectOverriddenPropertiesWithBaseScope( override fun processDirectOverriddenPropertiesWithBaseScope(
propertySymbol: FirPropertySymbol, propertySymbol: FirPropertySymbol,
processor: (FirPropertySymbol, FirTypeScope) -> ProcessorAction processor: (FirPropertySymbol, FirTypeScope) -> ProcessorAction
): ProcessorAction { ): ProcessorAction {
return useSiteScope.processDirectOverriddenPropertiesWithBaseScope(propertySymbol, processor) return processDirectOverriddenWithBaseScope(
propertySymbol, processor, FirTypeScope::processDirectOverriddenPropertiesWithBaseScope
)
}
private inline fun <reified D : FirCallableSymbol<*>> processDirectOverriddenWithBaseScope(
symbol: D,
noinline processor: (D, FirTypeScope) -> ProcessorAction,
processDirectOverriddenCallablesWithBaseScope: FirTypeScope.(D, ((D, FirTypeScope) -> ProcessorAction)) -> ProcessorAction,
): ProcessorAction {
val wrappedData = (symbol.fir as? FirCallableMemberDeclaration<*>)?.delegatedWrapperData
return when {
wrappedData == null || wrappedData.containingClass != containingClass -> {
useSiteScope.processDirectOverriddenCallablesWithBaseScope(symbol, processor)
}
else -> processor(wrappedData.wrapped.symbol as D, useSiteScope)
}
} }
override fun getCallableNames(): Set<Name> { override fun getCallableNames(): Set<Name> {
@@ -118,4 +119,13 @@ class FirDelegatedMemberScope(
override fun getClassifierNames(): Set<Name> { override fun getClassifierNames(): Set<Name> {
return useSiteScope.getClassifierNames() return useSiteScope.getClassifierNames()
} }
} }
private object DelegatedWrapperDataKey : FirDeclarationDataKey()
class DelegatedWrapperData<D : FirCallableDeclaration<*>>(
val wrapped: D,
val containingClass: ConeClassLikeLookupTag,
val delegateField: FirField,
)
var <D : FirCallableDeclaration<*>>
D.delegatedWrapperData: DelegatedWrapperData<D>? by FirDeclarationDataRegistry.data(DelegatedWrapperDataKey)