FIR2IR: cache constructors and their parents properly

This commit is contained in:
Mikhail Glukhikh
2020-03-03 20:15:36 +03:00
parent 0fee8a6946
commit 9788a7cfbc
2 changed files with 24 additions and 10 deletions
@@ -611,16 +611,17 @@ class Fir2IrDeclarationStorage(
} ?: createIrFunction(function, irParent, shouldLeaveScope, origin) } ?: createIrFunction(function, irParent, shouldLeaveScope, origin)
} }
fun getIrConstructor( private fun getCachedIrConstructor(
constructor: FirConstructor
): IrConstructor? {
return constructorCache[constructor]
}
private fun createIrConstructor(
constructor: FirConstructor, constructor: FirConstructor,
irParent: IrDeclarationParent? = null, irParent: IrDeclarationParent,
shouldLeaveScope: Boolean = true shouldLeaveScope: Boolean = true
): IrConstructor { ): IrConstructor {
val cached = constructorCache[constructor]
if (cached != null) {
return if (shouldLeaveScope) cached else cached.enterLocalScope(constructor)
}
val descriptor = WrappedClassConstructorDescriptor() val descriptor = WrappedClassConstructorDescriptor()
val origin = IrDeclarationOrigin.DEFINED val origin = IrDeclarationOrigin.DEFINED
val isPrimary = constructor.isPrimary val isPrimary = constructor.isPrimary
@@ -648,6 +649,18 @@ class Fir2IrDeclarationStorage(
return created return created
} }
fun getIrConstructor(
constructor: FirConstructor,
irParent: IrDeclarationParent,
shouldLeaveScope: Boolean = true
): IrConstructor {
val cached = getCachedIrConstructor(constructor)
if (cached != null) {
return if (shouldLeaveScope) cached else cached.enterLocalScope(constructor)
}
return createIrConstructor(constructor, irParent, shouldLeaveScope)
}
private fun createIrPropertyAccessor( private fun createIrPropertyAccessor(
propertyAccessor: FirPropertyAccessor?, propertyAccessor: FirPropertyAccessor?,
property: FirProperty, property: FirProperty,
@@ -969,8 +982,9 @@ class Fir2IrDeclarationStorage(
irSymbolTable.referenceSimpleFunction(irDeclaration.descriptor) irSymbolTable.referenceSimpleFunction(irDeclaration.descriptor)
} }
is FirConstructor -> { is FirConstructor -> {
val irParent = findIrParent(firDeclaration) getCachedIrConstructor(firDeclaration)?.let { return irSymbolTable.referenceConstructor(it.descriptor) }
val irDeclaration = getIrConstructor(firDeclaration, irParent).apply { val irParent = findIrParent(firDeclaration)!!
val irDeclaration = createIrConstructor(firDeclaration, irParent).apply {
setAndModifyParent(irParent) setAndModifyParent(irParent)
} }
irSymbolTable.referenceConstructor(irDeclaration.descriptor) irSymbolTable.referenceConstructor(irDeclaration.descriptor)
@@ -234,7 +234,7 @@ class Fir2IrVisitor(
override fun visitConstructor(constructor: FirConstructor, data: Any?): IrElement { override fun visitConstructor(constructor: FirConstructor, data: Any?): IrElement {
val irConstructor = declarationStorage.getIrConstructor( val irConstructor = declarationStorage.getIrConstructor(
constructor, irParent = conversionScope.lastClass(), shouldLeaveScope = false constructor, irParent = conversionScope.lastClass()!!, shouldLeaveScope = false
) )
return conversionScope.withFunction(irConstructor) { return conversionScope.withFunction(irConstructor) {
setFunctionContent(irConstructor.descriptor, constructor) setFunctionContent(irConstructor.descriptor, constructor)