JVM_IR: Add attributes to named suspend functions

This is a prerequisite to moving continuation classes inside said
functions, which is needed to support crossinline suspend lambdas.
The logic of whether to copy attributes on lowerings or not is simple:
  - if the lowering moves function body to a new place, it should copy
  the attributes.
  - if the lowering just generates new declarations (i.e. bridges), it
  should leave attributes as is.
This commit is contained in:
Ilmir Usmanov
2019-11-19 21:57:14 +03:00
parent 0f1592b023
commit 3b37f6bd32
12 changed files with 36 additions and 36 deletions
@@ -493,6 +493,7 @@ fun IrClass.addFakeOverrides() {
parent = this@addFakeOverrides
overriddenSymbols += overriddenFunctions.map { it.symbol }
copyParameterDeclarationsFrom(irFunction)
copyAttributes(irFunction)
}
}
@@ -6,7 +6,7 @@
package org.jetbrains.kotlin.backend.common.lower
import org.jetbrains.kotlin.backend.common.*
import org.jetbrains.kotlin.backend.common.descriptors.*
import org.jetbrains.kotlin.backend.common.descriptors.synthesizedString
import org.jetbrains.kotlin.backend.common.ir.*
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.Visibilities
@@ -22,7 +22,10 @@ import org.jetbrains.kotlin.ir.expressions.impl.*
import org.jetbrains.kotlin.ir.symbols.IrConstructorSymbol
import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.defaultType
import org.jetbrains.kotlin.ir.types.isNullable
import org.jetbrains.kotlin.ir.types.makeNullable
import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@@ -543,6 +543,7 @@ class LocalDeclarationsLowering(
newParameterToOld.putAbsentOrSame(it, this)
}
}
newDeclaration.copyAttributes(oldDeclaration)
newDeclaration.valueParameters += createTransformedValueParameters(capturedValues, oldDeclaration, newDeclaration)
newDeclaration.recordTransformedValueParameters(localFunctionContext)
@@ -211,7 +211,7 @@ class JvmDeclarationFactory(
},
// Old backend doesn't generate ACC_FINAL on DefaultImpls methods.
modality = Modality.OPEN
)
).also { it.copyAttributes(interfaceFun) }
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@@ -1,6 +1,6 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.backend.jvm.lower
@@ -161,6 +161,12 @@ class InventNamesForLocalClasses(private val context: JvmBackendContext) : FileL
declaration.acceptChildren(this, data.makeLocal())
return
}
if (declaration.isSuspend && declaration.body != null && declaration.origin != IrDeclarationOrigin.LOCAL_FUNCTION_FOR_LAMBDA) {
// Suspend functions have a continuation, which is essentially a local class
val newData = data.withName(inventName(declaration.name, data))
val internalName = inventName(null, newData)
context.putLocalClassType(declaration, Type.getObjectType(internalName))
}
super.visitSimpleFunction(declaration, data)
}
@@ -111,6 +111,7 @@ private class JvmInlineClassLowering(private val context: JvmBackendContext) : F
valueMap.putAll(replacement.valueParameterMap)
worker.valueParameters.forEach { it.transformChildrenVoid() }
worker.body = function.body?.transform(this, null)?.patchDeclarationParents(worker)
(worker as? IrAttributeContainer)?.copyAttributes(function)
// Don't create a wrapper for functions which are only used in an unboxed context
if (function.overriddenSymbols.isEmpty() || worker.dispatchReceiverParameter != null)
@@ -1,17 +1,6 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.psi2ir.generators
@@ -67,6 +56,10 @@ class FunctionGenerator(declarationGenerator: DeclarationGenerator) : Declaratio
?.let {
declareSimpleFunctionInner(it, ktElement, IrDeclarationOrigin.FAKE_OVERRIDE).buildWithScope { irFunction ->
generateFunctionParameterDeclarationsAndReturnType(irFunction, ktElement, null)
val overridenSymbol = irFunction.overriddenSymbols.first()
if (overridenSymbol.isBound) {
irFunction.copyAttributes(overridenSymbol.owner)
}
}
}
@@ -1,17 +1,6 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.ir.declarations
@@ -23,7 +12,8 @@ import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
interface IrSimpleFunction :
IrFunction,
IrSymbolDeclaration<IrSimpleFunctionSymbol>,
IrOverridableDeclaration<IrSimpleFunctionSymbol> {
IrOverridableDeclaration<IrSimpleFunctionSymbol>,
IrAttributeContainer {
val modality: Modality
val isTailrec: Boolean
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.ir.declarations.impl
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.Visibility
import org.jetbrains.kotlin.ir.declarations.IrAttributeContainer
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
import org.jetbrains.kotlin.ir.symbols.IrPropertySymbol
@@ -64,6 +65,7 @@ class IrFunctionImpl(
override val descriptor: FunctionDescriptor = symbol.descriptor
override val overriddenSymbols: MutableList<IrSimpleFunctionSymbol> = SmartList()
override var attributeOwnerId: IrAttributeContainer = this
override var correspondingPropertySymbol: IrPropertySymbol? = null
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.ir.declarations.lazy
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.Visibility
import org.jetbrains.kotlin.ir.declarations.IrAttributeContainer
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
import org.jetbrains.kotlin.ir.declarations.IrTypeParameter
@@ -89,6 +90,7 @@ class IrLazyFunction(
stubGenerator.generateFunctionStub(it.original).symbol
}
}
override var attributeOwnerId: IrAttributeContainer = this
override var correspondingPropertySymbol: IrPropertySymbol? = null
@@ -55,6 +55,7 @@ class IrBuiltInOperator(
}
override val overriddenSymbols: MutableList<IrSimpleFunctionSymbol> = SmartList()
override var attributeOwnerId: IrAttributeContainer = this
override val mangle: String get() = "operator#$name@$suffix"
init {