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 6fb6acbfd65..7deeff03991 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 @@ -140,16 +140,18 @@ class Fir2IrDeclarationStorage( leaveScope(descriptor) } + private fun IrTypeParametersContainer.setTypeParameters(owner: FirTypeParametersOwner) { + typeParameters = owner.typeParameters.mapIndexed { index, typeParameter -> + getIrTypeParameter(typeParameter, index).apply { parent = this@setTypeParameters } + } + } + private fun IrClass.declareSupertypesAndTypeParameters(klass: FirClass<*>): IrClass { - for (superTypeRef in klass.superTypeRefs) { - superTypes += superTypeRef.toIrType(session, this@Fir2IrDeclarationStorage) + superTypes = klass.superTypeRefs.map { superTypeRef -> + superTypeRef.toIrType(session, this@Fir2IrDeclarationStorage) } if (klass is FirRegularClass) { - for ((index, typeParameter) in klass.typeParameters.withIndex()) { - typeParameters += getIrTypeParameter(typeParameter, index).apply { - parent = this@declareSupertypesAndTypeParameters - } - } + setTypeParameters(klass) } return this } @@ -320,19 +322,21 @@ class Fir2IrDeclarationStorage( private fun T.declareDefaultSetterParameter(type: IrType): T { val parent = this val descriptor = WrappedValueParameterDescriptor() - valueParameters += irSymbolTable.declareValueParameter( - startOffset, endOffset, origin, descriptor, type - ) { symbol -> - IrValueParameterImpl( - startOffset, endOffset, IrDeclarationOrigin.DEFINED, symbol, - Name.special(""), 0, type, - varargElementType = null, - isCrossinline = false, isNoinline = false - ).apply { - this.parent = parent - descriptor.bind(this) + valueParameters = listOf( + irSymbolTable.declareValueParameter( + startOffset, endOffset, origin, descriptor, type + ) { symbol -> + IrValueParameterImpl( + startOffset, endOffset, IrDeclarationOrigin.DEFINED, symbol, + Name.special(""), 0, type, + varargElementType = null, + isCrossinline = false, isNoinline = false + ).apply { + this.parent = parent + descriptor.bind(this) + } } - } + ) return this } @@ -342,16 +346,14 @@ class Fir2IrDeclarationStorage( val type = function.valueParameters.first().returnTypeRef.toIrType(session, this@Fir2IrDeclarationStorage) declareDefaultSetterParameter(type) } else if (function != null) { - for ((index, valueParameter) in function.valueParameters.withIndex()) { - valueParameters += createAndSaveIrParameter(valueParameter, index).apply { this.parent = parent } + valueParameters = function.valueParameters.mapIndexed { index, valueParameter -> + createAndSaveIrParameter(valueParameter, index).apply { this.parent = parent } } } if (function !is FirConstructor) { val thisOrigin = IrDeclarationOrigin.DEFINED if (function is FirSimpleFunction) { - for ((index, typeParameter) in function.typeParameters.withIndex()) { - typeParameters += getIrTypeParameter(typeParameter, index).apply { this.parent = parent } - } + setTypeParameters(function) } val receiverTypeRef = function?.receiverTypeRef if (receiverTypeRef != null) { 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 086bb443bfc..557c8806dbe 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 @@ -163,9 +163,8 @@ class Fir2IrVisitor( declarations += irDeclaration } - file.annotations.forEach { - val irCall = it.accept(this@Fir2IrVisitor, data) as? IrConstructorCall ?: return@forEach - annotations += irCall + annotations = file.annotations.mapNotNull { + it.accept(this@Fir2IrVisitor, data) as? IrConstructorCall } } } @@ -305,9 +304,8 @@ class Fir2IrVisitor( } } addFakeOverrides(klass, processedCallableNames) - klass.annotations.forEach { - val irCall = it.accept(this@Fir2IrVisitor, null) as? IrConstructorCall ?: return@forEach - annotations += irCall + annotations = klass.annotations.mapNotNull { + it.accept(this@Fir2IrVisitor, null) as? IrConstructorCall } } if (irPrimaryConstructor != null) { @@ -377,7 +375,7 @@ class Fir2IrVisitor( if (firOverriddenSymbol != null && this is IrSimpleFunction && firFunctionSymbol != null) { val overriddenSymbol = declarationStorage.getIrFunctionSymbol(firOverriddenSymbol) if (overriddenSymbol is IrSimpleFunctionSymbol) { - overriddenSymbols += overriddenSymbol + overriddenSymbols = listOf(overriddenSymbol) } } var body = firFunction?.body?.convertToIrBlockBody() @@ -581,18 +579,18 @@ class Fir2IrVisitor( property.getter, this, propertyType, property.getter is FirDefaultPropertyGetter, property.getter == null ) getter?.apply { - overriddenProperty?.owner?.getter?.symbol?.let { overriddenSymbols += it } + overriddenProperty?.owner?.getter?.symbol?.let { overriddenSymbols = listOf(it) } } if (property.isVar) { setter?.setPropertyAccessorContent( property.setter, this, propertyType, property.setter is FirDefaultPropertySetter, property.setter == null ) setter?.apply { - overriddenProperty?.owner?.setter?.symbol?.let { overriddenSymbols += it } + overriddenProperty?.owner?.setter?.symbol?.let { overriddenSymbols = listOf(it) } } } - property.annotations.forEach { - annotations += it.accept(this@Fir2IrVisitor, null) as IrConstructorCall + annotations = property.annotations.mapNotNull { + it.accept(this@Fir2IrVisitor, null) as? IrConstructorCall } declarationStorage.leaveScope(descriptor) return this