FIR2IR: cache functions and their parents properly

This commit is contained in:
Mikhail Glukhikh
2020-03-03 19:26:15 +03:00
parent 940567b8bd
commit 0fee8a6946
31 changed files with 71 additions and 121 deletions
@@ -62,7 +62,7 @@ class Fir2IrDeclarationStorage(
private val typeParameterCacheForSetter = mutableMapOf<FirTypeParameter, IrTypeParameter>()
private val functionCache = mutableMapOf<FirSimpleFunction, IrSimpleFunction>()
private val functionCache = mutableMapOf<FirFunction<*>, IrSimpleFunction>()
private val constructorCache = mutableMapOf<FirConstructor, IrConstructor>()
@@ -537,57 +537,60 @@ class Fir2IrDeclarationStorage(
return this
}
fun getIrFunction(
function: FirSimpleFunction,
private fun getCachedIrFunction(function: FirFunction<*>): IrSimpleFunction? {
return if (function !is FirSimpleFunction || function.visibility == Visibilities.LOCAL) {
localStorage.getLocalFunction(function)
} else {
functionCache[function]
}
}
private fun createIrFunction(
function: FirFunction<*>,
irParent: IrDeclarationParent?,
shouldLeaveScope: Boolean = true,
origin: IrDeclarationOrigin = IrDeclarationOrigin.DEFINED
): IrSimpleFunction {
fun create(): IrSimpleFunction {
val containerSource = function.containerSource
val descriptor = containerSource?.let { WrappedFunctionDescriptorWithContainerSource(it) } ?: WrappedSimpleFunctionDescriptor()
val updatedOrigin = if (function.symbol.callableId.isKFunctionInvoke()) IrDeclarationOrigin.FAKE_OVERRIDE else origin
preCacheTypeParameters(function)
return function.convertWithOffsets { startOffset, endOffset ->
enterScope(descriptor)
val result = irSymbolTable.declareSimpleFunction(startOffset, endOffset, origin, descriptor) { symbol ->
IrFunctionImpl(
startOffset, endOffset, updatedOrigin, symbol,
function.name, function.visibility, function.modality!!,
function.returnTypeRef.toIrType(),
isInline = function.isInline,
isExternal = function.isExternal,
isTailrec = function.isTailRec,
isSuspend = function.isSuspend,
isExpect = function.isExpect,
isFakeOverride = updatedOrigin == IrDeclarationOrigin.FAKE_OVERRIDE,
isOperator = function.isOperator
)
}
result
}.bindAndDeclareParameters(function, descriptor, irParent, isStatic = function.isStatic)
val simpleFunction = function as? FirSimpleFunction
val containerSource = simpleFunction?.containerSource
val descriptor = containerSource?.let { WrappedFunctionDescriptorWithContainerSource(it) } ?: WrappedSimpleFunctionDescriptor()
val isLambda = function.psi is KtFunctionLiteral
val updatedOrigin = when {
isLambda -> IrDeclarationOrigin.LOCAL_FUNCTION_FOR_LAMBDA
function.symbol.callableId.isKFunctionInvoke() -> IrDeclarationOrigin.FAKE_OVERRIDE
else -> origin
}
preCacheTypeParameters(function)
val name = simpleFunction?.name
?: if (isLambda) Name.special("<anonymous>") else Name.special("<no name provided>")
val visibility = simpleFunction?.visibility ?: Visibilities.LOCAL
val created = function.convertWithOffsets { startOffset, endOffset ->
enterScope(descriptor)
val result = irSymbolTable.declareSimpleFunction(startOffset, endOffset, origin, descriptor) { symbol ->
IrFunctionImpl(
startOffset, endOffset, updatedOrigin, symbol,
name, visibility,
simpleFunction?.modality ?: Modality.FINAL,
function.returnTypeRef.toIrType(),
isInline = simpleFunction?.isInline == true,
isExternal = simpleFunction?.isExternal == true,
isTailrec = simpleFunction?.isTailRec == true,
isSuspend = simpleFunction?.isSuspend == true,
isExpect = simpleFunction?.isExpect == true,
isFakeOverride = updatedOrigin == IrDeclarationOrigin.FAKE_OVERRIDE,
isOperator = simpleFunction?.isOperator == true
)
}
result
}.bindAndDeclareParameters(function, descriptor, irParent, isStatic = simpleFunction?.isStatic == true)
if (function.visibility == Visibilities.LOCAL) {
val cached = localStorage.getLocalFunction(function)
if (cached != null) {
return if (shouldLeaveScope) cached else cached.enterLocalScope(function)
}
val created = create()
if (shouldLeaveScope) {
leaveScope(created.descriptor)
}
localStorage.putLocalFunction(function, created)
return created
}
val cached = functionCache[function]
if (cached != null) {
return if (shouldLeaveScope) cached else cached.enterLocalScope(function)
}
val created = create()
if (shouldLeaveScope) {
leaveScope(created.descriptor)
}
if (visibility == Visibilities.LOCAL) {
localStorage.putLocalFunction(function, created)
return created
}
if (function.symbol.callableId.isKFunctionInvoke()) {
(function.symbol.overriddenSymbol as? FirNamedFunctionSymbol)?.let {
created.overriddenSymbols += getIrFunctionSymbol(it) as IrSimpleFunctionSymbol
@@ -597,38 +600,15 @@ class Fir2IrDeclarationStorage(
return created
}
fun getIrLocalFunction(
function: FirAnonymousFunction,
irParent: IrDeclarationParent? = null,
shouldLeaveScope: Boolean = true
fun getIrFunction(
function: FirFunction<*>,
irParent: IrDeclarationParent?,
shouldLeaveScope: Boolean = true,
origin: IrDeclarationOrigin = IrDeclarationOrigin.DEFINED
): IrSimpleFunction {
val descriptor = WrappedSimpleFunctionDescriptor()
val isLambda = function.psi is KtFunctionLiteral
val origin = if (isLambda) IrDeclarationOrigin.LOCAL_FUNCTION_FOR_LAMBDA else IrDeclarationOrigin.DEFINED
return function.convertWithOffsets { startOffset, endOffset ->
irSymbolTable.declareSimpleFunction(startOffset, endOffset, origin, descriptor) { symbol ->
IrFunctionImpl(
startOffset, endOffset, origin, symbol,
if (isLambda) Name.special("<anonymous>") else Name.special("<no name provided>"),
Visibilities.LOCAL, Modality.FINAL,
function.returnTypeRef.toIrType(),
isInline = false, isExternal = false, isTailrec = false,
// TODO: suspend lambda
isSuspend = false,
isExpect = false,
isFakeOverride = false,
isOperator = false
).apply {
enterScope(descriptor)
}
}.bindAndDeclareParameters(
function, descriptor, irParent = irParent, isStatic = false
).apply {
if (shouldLeaveScope) {
leaveScope(descriptor)
}
}
}
return getCachedIrFunction(function)?.apply {
if (!shouldLeaveScope) enterLocalScope(function)
} ?: createIrFunction(function, irParent, shouldLeaveScope, origin)
}
fun getIrConstructor(
@@ -979,22 +959,17 @@ class Fir2IrDeclarationStorage(
}
fun getIrFunctionSymbol(firFunctionSymbol: FirFunctionSymbol<*>): IrFunctionSymbol {
val firDeclaration = firFunctionSymbol.fir
val irParent = (firDeclaration as? FirCallableDeclaration<*>)?.let { findIrParent(it) }
return when (firDeclaration) {
is FirSimpleFunction -> {
val irDeclaration = getIrFunction(firDeclaration, irParent).apply {
setAndModifyParent(irParent)
}
irSymbolTable.referenceSimpleFunction(irDeclaration.descriptor)
}
is FirAnonymousFunction -> {
val irDeclaration = getIrLocalFunction(firDeclaration, irParent).apply {
return when (val firDeclaration = firFunctionSymbol.fir) {
is FirSimpleFunction, is FirAnonymousFunction -> {
getCachedIrFunction(firDeclaration)?.let { return irSymbolTable.referenceSimpleFunction(it.descriptor) }
val irParent = findIrParent(firDeclaration)
val irDeclaration = createIrFunction(firDeclaration, irParent).apply {
setAndModifyParent(irParent)
}
irSymbolTable.referenceSimpleFunction(irDeclaration.descriptor)
}
is FirConstructor -> {
val irParent = findIrParent(firDeclaration)
val irDeclaration = getIrConstructor(firDeclaration, irParent).apply {
setAndModifyParent(irParent)
}
@@ -300,7 +300,7 @@ class Fir2IrVisitor(
override fun visitAnonymousFunction(anonymousFunction: FirAnonymousFunction, data: Any?): IrElement {
return anonymousFunction.convertWithOffsets { startOffset, endOffset ->
val irFunction = declarationStorage.getIrLocalFunction(anonymousFunction, conversionScope.parent(), shouldLeaveScope = false)
val irFunction = declarationStorage.getIrFunction(anonymousFunction, conversionScope.parent(), shouldLeaveScope = false)
conversionScope.withFunction(irFunction) {
setFunctionContent(irFunction.descriptor, anonymousFunction)
}
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// KJS_WITH_FULL_RUNTIME
// WITH_RUNTIME
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
class Point(val x : Int, val y : Int)
fun box() : String {
-1
View File
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
class A {
fun foo() {}
fun bar(f: A.() -> Unit = {}) {}
@@ -1,5 +1,4 @@
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// IGNORE_BACKEND_FIR: JVM_IR
// IGNORE_BACKEND: NATIVE
import kotlin.contracts.*
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
fun zap(s: String) = s
inline fun tryZap(s1: String, s2: String, fn: String.(String) -> String) =
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
fun zap(s: String) = s
inline fun tryZap(string: String, fn: String.() -> String) =
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
fun test1(f: (Int) -> Int) = f(1)
fun test2(f: Int.() -> Int) = 2.f()
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
fun test(cl: Int.() -> Int):Int = 11.cl()
class Foo {
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// IGNORE_BACKEND: JS_IR
// TODO: muted automatically, investigate should it be ran for JS or not
// IGNORE_BACKEND: JS, NATIVE
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
fun box() : String {
val y = 12
val op = { x:Int -> (x + y).toString() }
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// TARGET_BACKEND: JVM
// FULL_JDK
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
fun box(): String {
val f = fun (s: String): String = s
val g = f as String.() -> String
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
//KT-3450 get and invoke are not parsed in one expression
public class A(val s: String) {
-1
View File
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
class Data
fun newInit(f: Data.() -> Data) = Data().f()
-1
View File
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
class Comment() {
var article = ""
}
-1
View File
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
fun Any.with(operation : Any.() -> Any) = operation().toString()
val f = { a : Int -> }
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// WITH_RUNTIME
object ExtProvider {
-1
View File
@@ -1,6 +1,5 @@
// !LANGUAGE: +NewInference
// !USE_EXPERIMENTAL: kotlin.Experimental
// IGNORE_BACKEND_FIR: JVM_IR
// WITH_RUNTIME
// ISSUE: KT-35684
-1
View File
@@ -1,7 +1,6 @@
// !LANGUAGE: +NewInference
// WITH_RUNTIME
// KJS_WITH_FULL_RUNTIME
// IGNORE_BACKEND_FIR: JVM_IR
// WITH_REFLECT
import kotlin.experimental.ExperimentalTypeInference
-1
View File
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// TARGET_BACKEND: JVM
// WITH_RUNTIME
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
class A(val result: String)
fun a(body: A.() -> String): String {
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// KJS_WITH_FULL_RUNTIME
// WITH_RUNTIME
-1
View File
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// FILE: Outer.kt
package another
-1
View File
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
fun f(b : Int.(Int)->Int) = 1?.b(1)
fun box(): String {
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
fun f(b : Long.(Long)->Long) = 1L?.b(2L)
fun box(): String {
@@ -12,4 +12,4 @@ FILE fqName:<root> fileName:/extensionLambda.kt
BLOCK_BODY
RETURN type=kotlin.Nothing from='local final fun <anonymous> (): kotlin.Int declared in <root>.test1'
CALL 'public open fun <get-length> (): kotlin.Int declared in kotlin.String' type=kotlin.Int origin=GET_PROPERTY
$this: GET_VAR '<this>: kotlin.String declared in special.<anonymous>' type=kotlin.String origin=null
$this: GET_VAR '<this>: kotlin.String declared in <root>.test1.<anonymous>' type=kotlin.String origin=null
@@ -113,7 +113,7 @@ FILE fqName:<root> fileName:/multipleImplicitReceivers.kt
BLOCK_BODY
RETURN type=kotlin.Nothing from='local final fun <anonymous> (): kotlin.Int declared in <root>.test.<anonymous>.<anonymous>'
CALL 'public open fun invoke (): kotlin.Int [operator] declared in <root>.IInvoke' type=kotlin.Int origin=null
$this: GET_VAR '<this>: <root>.IInvoke declared in special.<anonymous>' type=<root>.IInvoke origin=null
$this: GET_VAR '<this>: <root>.IInvoke declared in <root>.test.<anonymous>.<anonymous>.<anonymous>' type=<root>.IInvoke origin=null
$receiver: CALL 'public open fun <get-foo> (): <root>.B declared in <root>.IFoo' type=<root>.B origin=GET_PROPERTY
$this: GET_VAR '<this>: <root>.IFoo declared in special.<anonymous>' type=<root>.IFoo origin=null
$receiver: GET_VAR '<this>: <root>.A declared in special.<anonymous>' type=<root>.A origin=null
$this: GET_VAR '<this>: <root>.IFoo declared in <root>.test.<anonymous>.<anonymous>' type=<root>.IFoo origin=null
$receiver: GET_VAR '<this>: <root>.A declared in <root>.test.<anonymous>' type=<root>.A origin=null
+1 -1
View File
@@ -28,7 +28,7 @@ FILE fqName:<root> fileName:/builtinMap.kt
BLOCK_BODY
RETURN type=kotlin.Nothing from='local final fun <anonymous> (): kotlin.Unit declared in <root>.plus'
CALL 'public open fun put (p0: K of java.util.LinkedHashMap?, p1: V of java.util.LinkedHashMap?): V of java.util.LinkedHashMap? declared in java.util.LinkedHashMap' type=V1 of <root>.plus? origin=null
$this: GET_VAR '<this>: java.util.LinkedHashMap<K1 of <root>.plus?, V1 of <root>.plus?> declared in special.<anonymous>' type=java.util.LinkedHashMap<K1 of <root>.plus?, V1 of <root>.plus?> origin=null
$this: GET_VAR '<this>: java.util.LinkedHashMap<K1 of <root>.plus?, V1 of <root>.plus?> declared in <root>.plus.<anonymous>' type=java.util.LinkedHashMap<K1 of <root>.plus?, V1 of <root>.plus?> origin=null
p0: CALL 'public final fun <get-first> (): A of kotlin.Pair declared in kotlin.Pair' type=K1 of <root>.plus origin=GET_PROPERTY
$this: GET_VAR 'pair: kotlin.Pair<K1 of <root>.plus, V1 of <root>.plus> declared in <root>.plus' type=kotlin.Pair<K1 of <root>.plus, V1 of <root>.plus> origin=null
p1: CALL 'public final fun <get-second> (): B of kotlin.Pair declared in kotlin.Pair' type=V1 of <root>.plus origin=GET_PROPERTY
@@ -11,7 +11,7 @@ FILE fqName:<root> fileName:/castsInsideCoroutineInference.kt
$receiver: VALUE_PARAMETER name:<this> type:<root>.FlowCollector<R of <root>.scopedFlow>
BLOCK_BODY
VAR name:collector type:<root>.FlowCollector<R of <root>.scopedFlow> [val]
GET_VAR '<this>: <root>.FlowCollector<R of <root>.scopedFlow> declared in special.<anonymous>' type=<root>.FlowCollector<R of <root>.scopedFlow> origin=null
GET_VAR '<this>: <root>.FlowCollector<R of <root>.scopedFlow> declared in <root>.scopedFlow.<anonymous>' type=<root>.FlowCollector<R of <root>.scopedFlow> origin=null
CALL 'public final fun flowScope <R> (block: kotlin.Function1<<root>.CoroutineScope, R of <root>.flowScope>): R of <root>.flowScope [suspend] declared in <root>' type=kotlin.Unit origin=null
<R>: kotlin.Unit
block: FUN_EXPR type=kotlin.Function1<<root>.CoroutineScope, kotlin.Unit> origin=LAMBDA
@@ -20,7 +20,7 @@ FILE fqName:<root> fileName:/castsInsideCoroutineInference.kt
BLOCK_BODY
CALL 'public abstract fun invoke (p1: P1 of kotlin.Function2, p2: P2 of kotlin.Function2): R of kotlin.Function2 [operator] declared in kotlin.Function2' type=kotlin.Unit origin=INVOKE
$this: GET_VAR 'block: kotlin.Function2<<root>.CoroutineScope, <root>.FlowCollector<R of <root>.scopedFlow>, kotlin.Unit> declared in <root>.scopedFlow' type=kotlin.Function2<<root>.CoroutineScope, <root>.FlowCollector<R of <root>.scopedFlow>, kotlin.Unit> origin=null
p1: GET_VAR '<this>: <root>.CoroutineScope declared in special.<anonymous>' type=<root>.CoroutineScope origin=null
p1: GET_VAR '<this>: <root>.CoroutineScope declared in <root>.scopedFlow.<anonymous>.<anonymous>' type=<root>.CoroutineScope origin=null
p2: GET_VAR 'val collector: <root>.FlowCollector<R of <root>.scopedFlow> [val] declared in <root>.scopedFlow.<anonymous>' type=<root>.FlowCollector<R of <root>.scopedFlow> origin=null
FUN name:onCompletion visibility:public modality:FINAL <T> ($receiver:<root>.Flow<T of <root>.onCompletion>, action:kotlin.Function2<<root>.FlowCollector<T of <root>.onCompletion>, kotlin.Throwable?, kotlin.Unit>) returnType:<root>.Flow<T of <root>.onCompletion>
TYPE_PARAMETER name:T index:0 variance: superTypes:[kotlin.Any?]
@@ -37,7 +37,7 @@ FILE fqName:<root> fileName:/castsInsideCoroutineInference.kt
VAR name:safeCollector type:<root>.SafeCollector<T of <root>.onCompletion> [val]
CONSTRUCTOR_CALL 'public constructor <init> (collector: <root>.FlowCollector<T of <root>.SafeCollector>) [primary] declared in <root>.SafeCollector' type=<root>.SafeCollector<T of <root>.onCompletion> origin=null
<class: T>: T of <root>.onCompletion
collector: GET_VAR '<this>: <root>.FlowCollector<T of <root>.onCompletion> declared in special.<anonymous>' type=<root>.FlowCollector<T of <root>.onCompletion> origin=null
collector: GET_VAR '<this>: <root>.FlowCollector<T of <root>.onCompletion> declared in <root>.onCompletion.<anonymous>' type=<root>.FlowCollector<T of <root>.onCompletion> origin=null
CALL 'public final fun invokeSafely <T> (action: kotlin.Function2<<root>.FlowCollector<T of <root>.invokeSafely>, kotlin.Throwable?, kotlin.Unit>): kotlin.Unit [suspend] declared in <root>' type=kotlin.Unit origin=null
<T>: T of <root>.onCompletion
$receiver: GET_VAR 'val safeCollector: <root>.SafeCollector<T of <root>.onCompletion> [val] declared in <root>.onCompletion.<anonymous>' type=<root>.SafeCollector<T of <root>.onCompletion> origin=null
@@ -81,7 +81,7 @@ FILE fqName:<root> fileName:/castsInsideCoroutineInference.kt
VAR name:channel type:<root>.ChannelCoroutine<kotlin.Any> [val]
TYPE_OP type=<root>.ChannelCoroutine<kotlin.Any> origin=CAST typeOperand=<root>.ChannelCoroutine<kotlin.Any>
CALL 'public abstract fun <get-channel> (): <root>.SendChannel<E of <root>.ProducerScope> declared in <root>.ProducerScope' type=<root>.SendChannel<kotlin.Any> origin=GET_PROPERTY
$this: GET_VAR '<this>: <root>.ProducerScope<kotlin.Any> declared in special.<anonymous>' type=<root>.ProducerScope<kotlin.Any> origin=null
$this: GET_VAR '<this>: <root>.ProducerScope<kotlin.Any> declared in <root>.asFairChannel.<anonymous>' type=<root>.ProducerScope<kotlin.Any> origin=null
CALL 'public abstract fun collect (collector: <root>.FlowCollector<T of <root>.Flow>): kotlin.Unit [suspend] declared in <root>.Flow' type=kotlin.Unit origin=null
$this: GET_VAR 'flow: <root>.Flow<*> declared in <root>.asFairChannel' type=<root>.Flow<*> origin=null
collector: FUN_EXPR type=kotlin.Function1<kotlin.Any?, kotlin.Unit> origin=LAMBDA
@@ -125,7 +125,7 @@ FILE fqName:<root> fileName:/castsInsideCoroutineInference.kt
RETURN type=kotlin.Nothing from='local final fun <anonymous> (value: kotlin.Any?): kotlin.Unit declared in <root>.asChannel.<anonymous>'
CALL 'public abstract fun send (e: E of <root>.SendChannel): kotlin.Unit [suspend] declared in <root>.SendChannel' type=kotlin.Unit origin=null
$this: CALL 'public abstract fun <get-channel> (): <root>.SendChannel<E of <root>.ProducerScope> declared in <root>.ProducerScope' type=<root>.SendChannel<kotlin.Any> origin=GET_PROPERTY
$this: GET_VAR '<this>: <root>.ProducerScope<kotlin.Any> declared in special.<anonymous>' type=<root>.ProducerScope<kotlin.Any> origin=null
$this: GET_VAR '<this>: <root>.ProducerScope<kotlin.Any> declared in <root>.asChannel.<anonymous>' type=<root>.ProducerScope<kotlin.Any> origin=null
e: BLOCK type=kotlin.Any origin=ELVIS
VAR IR_TEMPORARY_VARIABLE name:tmp_1 type:kotlin.Any? [val]
GET_VAR 'value: kotlin.Any? declared in <root>.asChannel.<anonymous>.<anonymous>' type=kotlin.Any? origin=null