[FIR2IR] Extract logic of IR declarations generation into separate component. Part 2

This commit fixes access of enter/leaveScope in Fir2IrCallableDeclarationsGenerator
This commit is contained in:
Dmitriy Novozhilov
2023-08-28 11:21:32 +03:00
committed by Space Team
parent 08ebe04485
commit 12c02897e1
2 changed files with 129 additions and 125 deletions
@@ -40,7 +40,6 @@ import org.jetbrains.kotlin.ir.expressions.IrSyntheticBodyKind
import org.jetbrains.kotlin.ir.symbols.* import org.jetbrains.kotlin.ir.symbols.*
import org.jetbrains.kotlin.ir.symbols.impl.IrClassSymbolImpl import org.jetbrains.kotlin.ir.symbols.impl.IrClassSymbolImpl
import org.jetbrains.kotlin.ir.types.IrSimpleType import org.jetbrains.kotlin.ir.types.IrSimpleType
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.load.kotlin.FacadeClassSource import org.jetbrains.kotlin.load.kotlin.FacadeClassSource
import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.FqName
@@ -49,7 +48,6 @@ import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.resolve.jvm.JvmClassName import org.jetbrains.kotlin.resolve.jvm.JvmClassName
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedContainerAbiStability import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedContainerAbiStability
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedContainerSource import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedContainerSource
import org.jetbrains.kotlin.types.AbstractTypeChecker
import org.jetbrains.kotlin.utils.threadLocal import org.jetbrains.kotlin.utils.threadLocal
import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ConcurrentHashMap
@@ -254,6 +252,12 @@ class Fir2IrDeclarationStorage(
symbolTable.leaveScope(symbol) symbolTable.leaveScope(symbol)
} }
inline fun withScope(symbol: IrSymbol, crossinline block: () -> Unit) {
enterScope(symbol)
block()
leaveScope(symbol)
}
private fun getIrExternalOrBuiltInsPackageFragment(fqName: FqName, firOrigin: FirDeclarationOrigin): IrExternalPackageFragment { private fun getIrExternalOrBuiltInsPackageFragment(fqName: FqName, firOrigin: FirDeclarationOrigin): IrExternalPackageFragment {
val isBuiltIn = fqName in BUILT_INS_PACKAGE_FQ_NAMES val isBuiltIn = fqName in BUILT_INS_PACKAGE_FQ_NAMES
return if (isBuiltIn) getIrBuiltInsPackageFragment(fqName) else getIrExternalPackageFragment(fqName, firOrigin) return if (isBuiltIn) getIrBuiltInsPackageFragment(fqName) else getIrExternalPackageFragment(fqName, firOrigin)
@@ -329,7 +329,7 @@ class Fir2IrCallableDeclarationsGenerator(val components: Fir2IrComponents) : Fi
containerSource = simpleFunction?.containerSource, containerSource = simpleFunction?.containerSource,
).apply { ).apply {
metadata = FirMetadataSource.Function(function) metadata = FirMetadataSource.Function(function)
enterScope(this.symbol) declarationStorage.withScope(symbol) {
setAndModifyParent(this, irParent) setAndModifyParent(this, irParent)
declareParameters( declareParameters(
function, irParent, function, irParent,
@@ -338,7 +338,7 @@ class Fir2IrCallableDeclarationsGenerator(val components: Fir2IrComponents) : Fi
forSetter = false, forSetter = false,
) )
convertAnnotationsForNonDeclaredMembers(function, origin) convertAnnotationsForNonDeclaredMembers(function, origin)
leaveScope(this.symbol) }
} }
} }
result result
@@ -423,10 +423,10 @@ class Fir2IrCallableDeclarationsGenerator(val components: Fir2IrComponents) : Fi
// Add to cache before generating parameters to prevent an infinite loop when an annotation value parameter is annotated // Add to cache before generating parameters to prevent an infinite loop when an annotation value parameter is annotated
// with the annotation itself. // with the annotation itself.
constructorCache[constructor] = this constructorCache[constructor] = this
enterScope(this.symbol) declarationStorage.withScope(symbol) {
setAndModifyParent(this, irParent) setAndModifyParent(this, irParent)
declareParameters(constructor, irParent, dispatchReceiverType = null, isStatic = false, forSetter = false) declareParameters(constructor, irParent, dispatchReceiverType = null, isStatic = false, forSetter = false)
leaveScope(this.symbol) }
} }
} }
} }
@@ -501,9 +501,10 @@ class Fir2IrCallableDeclarationsGenerator(val components: Fir2IrComponents) : Fi
property, if (isSetter) ConversionTypeOrigin.SETTER else ConversionTypeOrigin.DEFAULT property, if (isSetter) ConversionTypeOrigin.SETTER else ConversionTypeOrigin.DEFAULT
) )
} }
val dispatchReceiverType = computeDispatchReceiverType(this, property, irParent)
// NB: we should enter accessor' scope before declaring its parameters // NB: we should enter accessor' scope before declaring its parameters
// (both setter default and receiver ones, if any) // (both setter default and receiver ones, if any)
enterScope(this.symbol) declarationStorage.withScope(symbol) {
if (propertyAccessor == null && isSetter) { if (propertyAccessor == null && isSetter) {
declareDefaultSetterParameter( declareDefaultSetterParameter(
property.returnTypeRef.toIrType(ConversionTypeOrigin.SETTER), property.returnTypeRef.toIrType(ConversionTypeOrigin.SETTER),
@@ -511,13 +512,12 @@ class Fir2IrCallableDeclarationsGenerator(val components: Fir2IrComponents) : Fi
) )
} }
setAndModifyParent(this, irParent) setAndModifyParent(this, irParent)
val dispatchReceiverType = computeDispatchReceiverType(this, property, irParent)
declareParameters( declareParameters(
propertyAccessor, irParent, dispatchReceiverType, propertyAccessor, irParent, dispatchReceiverType,
isStatic = irParent !is IrClass || propertyAccessor?.isStatic == true, forSetter = isSetter, isStatic = irParent !is IrClass || propertyAccessor?.isStatic == true, forSetter = isSetter,
parentPropertyReceiver = property.receiverParameter, parentPropertyReceiver = property.receiverParameter,
) )
leaveScope(this.symbol) }
if (correspondingProperty is Fir2IrLazyProperty && correspondingProperty.containingClass != null && !isFakeOverride && dispatchReceiverType != null) { if (correspondingProperty is Fir2IrLazyProperty && correspondingProperty.containingClass != null && !isFakeOverride && dispatchReceiverType != null) {
this.overriddenSymbols = correspondingProperty.fir.generateOverriddenAccessorSymbols( this.overriddenSymbols = correspondingProperty.fir.generateOverriddenAccessorSymbols(
correspondingProperty.containingClass, !isSetter correspondingProperty.containingClass, !isSetter
@@ -626,7 +626,7 @@ class Fir2IrCallableDeclarationsGenerator(val components: Fir2IrComponents) : Fi
).apply { ).apply {
metadata = FirMetadataSource.Property(property) metadata = FirMetadataSource.Property(property)
convertAnnotationsForNonDeclaredMembers(property, origin) convertAnnotationsForNonDeclaredMembers(property, origin)
enterScope(this.symbol) declarationStorage.withScope(symbol) {
if (irParent != null) { if (irParent != null) {
parent = irParent parent = irParent
} }
@@ -715,7 +715,7 @@ class Fir2IrCallableDeclarationsGenerator(val components: Fir2IrComponents) : Fi
setterForPropertyCache[symbol] = it.symbol setterForPropertyCache[symbol] = it.symbol
} }
} }
leaveScope(this.symbol) }
} }
} }
if (property.isFakeOverride(fakeOverrideOwnerLookupTag)) { if (property.isFakeOverride(fakeOverrideOwnerLookupTag)) {
@@ -956,7 +956,7 @@ class Fir2IrCallableDeclarationsGenerator(val components: Fir2IrComponents) : Fi
}.apply { }.apply {
parent = irParent parent = irParent
metadata = FirMetadataSource.Property(property) metadata = FirMetadataSource.Property(property)
enterScope(this.symbol) declarationStorage.withScope(symbol) {
delegate = declareIrVariable( delegate = declareIrVariable(
startOffset, endOffset, IrDeclarationOrigin.PROPERTY_DELEGATE, startOffset, endOffset, IrDeclarationOrigin.PROPERTY_DELEGATE,
NameUtils.propertyDelegateName(property.name), property.delegate!!.resolvedType.toIrType(), NameUtils.propertyDelegateName(property.name), property.delegate!!.resolvedType.toIrType(),
@@ -980,7 +980,7 @@ class Fir2IrCallableDeclarationsGenerator(val components: Fir2IrComponents) : Fi
} }
} }
annotationGenerator.generate(this, property) annotationGenerator.generate(this, property)
leaveScope(this.symbol) }
} }
localStorage.putDelegatedProperty(property, irProperty) localStorage.putDelegatedProperty(property, irProperty)
return irProperty return irProperty