IR: move defaultParameterDeclarationsCache access up one level

to avoid recomputing overriddenSymbols for fake overrides of default
stubs on repeated calls to generateDefaultsFunction.
This commit is contained in:
pyos
2019-11-14 13:40:03 +01:00
committed by Georgy Bronnikov
parent 70f09a7531
commit a235dd2891
@@ -312,11 +312,13 @@ private fun IrFunction.generateDefaultsFunction(
context: CommonBackendContext, context: CommonBackendContext,
skipInlineMethods: Boolean, skipInlineMethods: Boolean,
skipExternalMethods: Boolean skipExternalMethods: Boolean
): IrFunction? = when { ): IrFunction? = context.ir.defaultParameterDeclarationsCache[this] ?: when {
skipInlineMethods && isInline -> null skipInlineMethods && isInline -> null
skipExternalMethods && isExternalOrInheritedFromExternal() -> null skipExternalMethods && isExternalOrInheritedFromExternal() -> null
valueParameters.any { it.defaultValue != null } -> valueParameters.any { it.defaultValue != null } ->
generateDefaultsFunctionImpl(context, IrDeclarationOrigin.FUNCTION_FOR_DEFAULT_PARAMETER) generateDefaultsFunctionImpl(context, IrDeclarationOrigin.FUNCTION_FOR_DEFAULT_PARAMETER).also {
context.ir.defaultParameterDeclarationsCache[this] = it
}
this is IrSimpleFunction -> { this is IrSimpleFunction -> {
// If this is an override of a function with default arguments, produce a fake override of a default stub. // If this is an override of a function with default arguments, produce a fake override of a default stub.
val overriddenStubs = overriddenSymbols.mapNotNull { val overriddenStubs = overriddenSymbols.mapNotNull {
@@ -325,6 +327,7 @@ private fun IrFunction.generateDefaultsFunction(
if (overriddenStubs.isNotEmpty()) if (overriddenStubs.isNotEmpty())
generateDefaultsFunctionImpl(context, IrDeclarationOrigin.FAKE_OVERRIDE).also { generateDefaultsFunctionImpl(context, IrDeclarationOrigin.FAKE_OVERRIDE).also {
(it as IrSimpleFunction).overriddenSymbols.addAll(overriddenStubs) (it as IrSimpleFunction).overriddenSymbols.addAll(overriddenStubs)
context.ir.defaultParameterDeclarationsCache[this] = it
} }
else else
null null
@@ -332,8 +335,7 @@ private fun IrFunction.generateDefaultsFunction(
else -> null else -> null
} }
private fun IrFunction.generateDefaultsFunctionImpl(context: CommonBackendContext, newOrigin: IrDeclarationOrigin): IrFunction = private fun IrFunction.generateDefaultsFunctionImpl(context: CommonBackendContext, newOrigin: IrDeclarationOrigin): IrFunction {
context.ir.defaultParameterDeclarationsCache.getOrPut(this) {
val newFunction = when (this) { val newFunction = when (this) {
is IrConstructor -> is IrConstructor ->
buildConstructor { buildConstructor {
@@ -379,5 +381,5 @@ private fun IrFunction.generateDefaultsFunctionImpl(context: CommonBackendContex
// TODO some annotations are needed (e.g. @JvmStatic), others need different values (e.g. @JvmName), the rest are redundant. // TODO some annotations are needed (e.g. @JvmStatic), others need different values (e.g. @JvmName), the rest are redundant.
annotations.mapTo(newFunction.annotations) { it.deepCopyWithSymbols() } annotations.mapTo(newFunction.annotations) { it.deepCopyWithSymbols() }
newFunction return newFunction
} }