diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt index b855a67f383..6b97adaa84e 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt @@ -30,6 +30,7 @@ import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol import org.jetbrains.kotlin.fir.symbols.impl.* import org.jetbrains.kotlin.fir.types.* import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET +import org.jetbrains.kotlin.ir.builders.declarations.UNDEFINED_PARAMETER_INDEX import org.jetbrains.kotlin.ir.declarations.* import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin.GeneratedByPlugin import org.jetbrains.kotlin.ir.declarations.impl.IrClassImpl @@ -1113,6 +1114,53 @@ class Fir2IrDeclarationStorage( } } + fun createAndCacheParameter( + valueParameter: FirValueParameter, + index: Int = UNDEFINED_PARAMETER_INDEX, + useStubForDefaultValueStub: Boolean = true, + typeOrigin: ConversionTypeOrigin = ConversionTypeOrigin.DEFAULT, + skipDefaultParameter: Boolean = false, + // Use this parameter if you want to insert the actual default value instead of the stub (overrides useStubForDefaultValueStub parameter). + // This parameter is intended to be used for default values of annotation parameters where they are needed and + // may produce incorrect results for values that may be encountered outside annotations. + // Does not do anything if valueParameter.defaultValue is already FirExpressionStub. + forcedDefaultValueConversion: Boolean = false, + ): IrValueParameter { + return callablesGenerator.createIrParameter( + valueParameter, + index, + useStubForDefaultValueStub, + typeOrigin, + skipDefaultParameter, + forcedDefaultValueConversion + ).also { + localStorage.putParameter(valueParameter, it) + } + } + + fun createAndCacheIrVariable( + variable: FirVariable, + irParent: IrDeclarationParent, + givenOrigin: IrDeclarationOrigin? = null + ): IrVariable { + return callablesGenerator.createIrVariable(variable, irParent, givenOrigin).also { + localStorage.putVariable(variable, it) + } + } + + fun createAndCacheIrLocalDelegatedProperty( + property: FirProperty, + irParent: IrDeclarationParent + ): IrLocalDelegatedProperty { + val irProperty = callablesGenerator.createIrLocalDelegatedProperty(property, irParent) + val symbol = irProperty.symbol + delegateVariableForPropertyCache[symbol] = irProperty.delegate.symbol + getterForPropertyCache[symbol] = irProperty.getter.symbol + irProperty.setter?.let { setterForPropertyCache[symbol] = it.symbol } + localStorage.putDelegatedProperty(property, irProperty) + return irProperty + } + companion object { internal val ENUM_SYNTHETIC_NAMES = mapOf( diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt index c41806bd273..8455aee88f1 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt @@ -220,7 +220,7 @@ class Fir2IrVisitor( declarationStorage.enterScope(irScript.symbol) irScript.explicitCallParameters = script.parameters.map { parameter -> - callablesGenerator.createIrVariable( + declarationStorage.createAndCacheIrVariable( parameter, irScript, givenOrigin = IrDeclarationOrigin.SCRIPT_CALL_PARAMETER @@ -278,7 +278,7 @@ class Fir2IrVisitor( irBuiltIns.unitType, IrStatementOrigin.DESTRUCTURING_DECLARATION ).also { it.statements.add( - callablesGenerator.createIrVariable(statement, conversionScope.parentFromStack()).also { + declarationStorage.createAndCacheIrVariable(statement, conversionScope.parentFromStack()).also { it.initializer = statement.initializer?.toIrStatement() as? IrExpression } ) @@ -442,7 +442,7 @@ class Fir2IrVisitor( assert(variable.isLocal) val delegate = variable.delegate if (delegate != null) { - val irProperty = callablesGenerator.createIrLocalDelegatedProperty(variable, conversionScope.parentFromStack()) + val irProperty = declarationStorage.createAndCacheIrLocalDelegatedProperty(variable, conversionScope.parentFromStack()) irProperty.delegate.initializer = convertToIrExpression(delegate, isDelegate = true) conversionScope.withFunction(irProperty.getter) { memberGenerator.convertFunctionContent(irProperty.getter, variable.getter, null) @@ -458,7 +458,7 @@ class Fir2IrVisitor( val isNextVariable = initializer is FirFunctionCall && initializer.calleeReference.toResolvedNamedFunctionSymbol()?.callableId?.isIteratorNext() == true && variable.source?.isChildOfForLoop == true - val irVariable = callablesGenerator.createIrVariable( + val irVariable = declarationStorage.createAndCacheIrVariable( variable, conversionScope.parentFromStack(), if (isNextVariable) { if (variable.name.isSpecial && variable.name == SpecialNames.DESTRUCT) { @@ -1423,7 +1423,7 @@ class Fir2IrVisitor( override fun visitCatch(catch: FirCatch, data: Any?): IrElement { return catch.convertWithOffsets { startOffset, endOffset -> - val catchParameter = callablesGenerator.createIrVariable( + val catchParameter = declarationStorage.createAndCacheIrVariable( catch.parameter, conversionScope.parentFromStack(), IrDeclarationOrigin.CATCH_PARAMETER ) IrCatchImpl(startOffset, endOffset, catchParameter, catch.block.convertToIrBlock(forceUnitType = false)) diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/Fir2IrCallableDeclarationsGenerator.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/Fir2IrCallableDeclarationsGenerator.kt index 47396195c4e..dfdcaf42cf0 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/Fir2IrCallableDeclarationsGenerator.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/Fir2IrCallableDeclarationsGenerator.kt @@ -197,7 +197,7 @@ class Fir2IrCallableDeclarationsGenerator(val components: Fir2IrComponents) : Fi addContextReceiverParametersTo(contextReceivers, parent, this) function.valueParameters.mapIndexedTo(this) { index, valueParameter -> - createIrParameter( + declarationStorage.createAndCacheParameter( valueParameter, index + contextReceiverParametersCount, useStubForDefaultValueStub = function !is FirConstructor || containingClass?.name != Name.identifier("Enum"), typeOrigin, @@ -830,7 +830,6 @@ class Fir2IrCallableDeclarationsGenerator(val components: Fir2IrComponents) : Fi annotationGenerator.generate(this, valueParameter) } } - localStorage.putParameter(valueParameter, irParameter) return irParameter } @@ -879,7 +878,7 @@ class Fir2IrCallableDeclarationsGenerator(val components: Fir2IrComponents) : Fi fun createIrVariable( variable: FirVariable, irParent: IrDeclarationParent, - givenOrigin: IrDeclarationOrigin? = null + givenOrigin: IrDeclarationOrigin? ): IrVariable = convertCatching(variable) { val type = variable.irTypeForPotentiallyComponentCall() // Some temporary variables are produced in RawFirBuilder, but we consistently use special names for them. @@ -897,7 +896,6 @@ class Fir2IrCallableDeclarationsGenerator(val components: Fir2IrComponents) : Fi ) } irVariable.parent = irParent - localStorage.putVariable(variable, irVariable) return irVariable } @@ -926,28 +924,21 @@ class Fir2IrCallableDeclarationsGenerator(val components: Fir2IrComponents) : Fi startOffset, endOffset, IrDeclarationOrigin.PROPERTY_DELEGATE, NameUtils.propertyDelegateName(property.name), property.delegate!!.resolvedType.toIrType(), isVar = false, isConst = false, isLateinit = false - ).also { - delegateVariableForPropertyCache[symbol] = it.symbol - } + ) delegate.parent = irParent getter = createIrPropertyAccessor( property.getter, property, this, type, irParent, false, IrDeclarationOrigin.DELEGATED_PROPERTY_ACCESSOR, startOffset, endOffset, dontUseSignature = true - ).also { - getterForPropertyCache[symbol] = it.symbol - } + ) if (property.isVar) { setter = createIrPropertyAccessor( property.setter, property, this, type, irParent, true, IrDeclarationOrigin.DELEGATED_PROPERTY_ACCESSOR, startOffset, endOffset, dontUseSignature = true - ).also { - setterForPropertyCache[symbol] = it.symbol - } + ) } annotationGenerator.generate(this, property) } } - localStorage.putDelegatedProperty(property, irProperty) return irProperty }