New mechanism for mappings between old and produced declarations
This commit is contained in:
+2
@@ -33,4 +33,6 @@ interface CommonBackendContext : BackendContext, LoggingContext {
|
||||
putValueArgument(0, builder.irString(name))
|
||||
}
|
||||
}
|
||||
|
||||
val mapping: Mapping
|
||||
}
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright 2010-2020 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.common
|
||||
|
||||
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
|
||||
import org.jetbrains.kotlin.ir.declarations.IrFunction
|
||||
import kotlin.reflect.KMutableProperty0
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
interface Mapping {
|
||||
val defaultArgumentsDispatchFunction: Delegate<IrFunction, IrFunction>
|
||||
val defaultArgumentsOriginalFunction: Delegate<IrFunction, IrFunction>
|
||||
|
||||
abstract class Delegate<K : IrDeclaration, V> {
|
||||
abstract operator fun get(key: K): V?
|
||||
|
||||
abstract operator fun set(key: K, value: V?)
|
||||
|
||||
operator fun getValue(thisRef: K, desc: KProperty<*>): V? = get(thisRef)
|
||||
|
||||
operator fun setValue(thisRef: K, desc: KProperty<*>, value: V?) {
|
||||
set(thisRef, value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
open class DefaultMapping : Mapping {
|
||||
|
||||
override val defaultArgumentsDispatchFunction: Mapping.Delegate<IrFunction, IrFunction> = newMapping()
|
||||
override val defaultArgumentsOriginalFunction: Mapping.Delegate<IrFunction, IrFunction> = newMapping()
|
||||
|
||||
protected fun <K : IrDeclaration, V> newMapping() = object : Mapping.Delegate<K, V>() {
|
||||
private val map: MutableMap<K, V> = mutableMapOf()
|
||||
|
||||
override operator fun get(key: K): V? {
|
||||
return map[key]
|
||||
}
|
||||
|
||||
override operator fun set(key: K, value: V?) {
|
||||
if (value == null) {
|
||||
map.remove(key)
|
||||
} else {
|
||||
map[key] = value
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun <V : Any> KMutableProperty0<V?>.getOrPut(fn: () -> V) = this.get() ?: fn().also {
|
||||
this.set(it)
|
||||
}
|
||||
|
||||
fun <K : IrDeclaration, V> Mapping.Delegate<K, V>.getOrPut(key: K, fn: () -> V) = this[key] ?: fn().also {
|
||||
this[key] = it
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
/*
|
||||
* Copyright 2010-2020 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.backend.js
|
||||
|
||||
import org.jetbrains.kotlin.backend.common.CommonBackendContext
|
||||
|
||||
interface JsCommonBackendContext : CommonBackendContext {
|
||||
override val mapping: JsMapping
|
||||
}
|
||||
+6
-5
@@ -5,6 +5,7 @@
|
||||
|
||||
package org.jetbrains.kotlin.backend.js
|
||||
|
||||
import org.jetbrains.kotlin.backend.common.getOrPut
|
||||
import org.jetbrains.kotlin.backend.common.ir.DeclarationFactory
|
||||
import org.jetbrains.kotlin.backend.common.ir.copyTo
|
||||
import org.jetbrains.kotlin.backend.common.ir.copyTypeParametersFrom
|
||||
@@ -12,6 +13,7 @@ import org.jetbrains.kotlin.descriptors.ClassKind
|
||||
import org.jetbrains.kotlin.descriptors.Visibilities
|
||||
import org.jetbrains.kotlin.descriptors.Visibility
|
||||
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
|
||||
import org.jetbrains.kotlin.ir.backend.js.JsMapping
|
||||
import org.jetbrains.kotlin.ir.backend.js.ir.JsIrBuilder
|
||||
import org.jetbrains.kotlin.ir.backend.js.utils.Namer
|
||||
import org.jetbrains.kotlin.ir.declarations.*
|
||||
@@ -25,12 +27,11 @@ import org.jetbrains.kotlin.ir.types.IrType
|
||||
import org.jetbrains.kotlin.ir.util.defaultType
|
||||
import org.jetbrains.kotlin.ir.util.dump
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import java.util.*
|
||||
|
||||
class JsDeclarationFactory : DeclarationFactory {
|
||||
private val singletonFieldDescriptors = HashMap<IrClass, IrField>()
|
||||
private val outerThisFieldSymbols = HashMap<IrClass, IrField>()
|
||||
private val innerClassConstructors = HashMap<IrConstructor, IrConstructor>()
|
||||
class JsDeclarationFactory(mapping: JsMapping) : DeclarationFactory {
|
||||
private val singletonFieldDescriptors = mapping.singletonFieldDescriptors
|
||||
private val outerThisFieldSymbols = mapping.outerThisFieldSymbols
|
||||
private val innerClassConstructors = mapping.innerClassConstructors
|
||||
|
||||
override fun getFieldForEnumEntry(enumEntry: IrEnumEntry): IrField = TODO()
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
|
||||
package org.jetbrains.kotlin.ir.backend.js
|
||||
|
||||
import org.jetbrains.kotlin.backend.common.CommonBackendContext
|
||||
import org.jetbrains.kotlin.backend.common.atMostOne
|
||||
import org.jetbrains.kotlin.backend.common.ir.Ir
|
||||
import org.jetbrains.kotlin.backend.common.ir.Symbols
|
||||
@@ -47,7 +46,7 @@ class JsIrBackendContext(
|
||||
val additionalExportedDeclarations: Set<FqName>,
|
||||
override val configuration: CompilerConfiguration, // TODO: remove configuration from backend context
|
||||
override val scriptMode: Boolean = false
|
||||
) : CommonBackendContext {
|
||||
) : JsCommonBackendContext {
|
||||
override val transformedFunction = mutableMapOf<IrFunctionSymbol, IrSimpleFunctionSymbol>()
|
||||
override val lateinitNullableFields = mutableMapOf<IrField, IrField>()
|
||||
|
||||
@@ -102,7 +101,9 @@ class JsIrBackendContext(
|
||||
}
|
||||
|
||||
override val sharedVariablesManager = JsSharedVariablesManager(irBuiltIns, implicitDeclarationFile)
|
||||
override val declarationFactory = JsDeclarationFactory()
|
||||
|
||||
override val mapping = JsMapping()
|
||||
override val declarationFactory = JsDeclarationFactory(mapping)
|
||||
|
||||
companion object {
|
||||
val KOTLIN_PACKAGE_FQN = FqName.fromSegments(listOf("kotlin"))
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* Copyright 2010-2020 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.backend.js
|
||||
|
||||
import org.jetbrains.kotlin.backend.common.DefaultMapping
|
||||
import org.jetbrains.kotlin.ir.declarations.IrClass
|
||||
import org.jetbrains.kotlin.ir.declarations.IrConstructor
|
||||
import org.jetbrains.kotlin.ir.declarations.IrField
|
||||
|
||||
class JsMapping : DefaultMapping() {
|
||||
val singletonFieldDescriptors = newMapping<IrClass, IrField>()
|
||||
val outerThisFieldSymbols = newMapping<IrClass, IrField>()
|
||||
val innerClassConstructors = newMapping<IrConstructor, IrConstructor>()
|
||||
}
|
||||
@@ -6,6 +6,8 @@
|
||||
package org.jetbrains.kotlin.backend.jvm
|
||||
|
||||
import org.jetbrains.kotlin.backend.common.CommonBackendContext
|
||||
import org.jetbrains.kotlin.backend.common.DefaultMapping
|
||||
import org.jetbrains.kotlin.backend.common.Mapping
|
||||
import org.jetbrains.kotlin.backend.common.ir.Ir
|
||||
import org.jetbrains.kotlin.backend.common.lower.irThrow
|
||||
import org.jetbrains.kotlin.backend.common.phaser.PhaseConfig
|
||||
@@ -68,6 +70,8 @@ class JvmBackendContext(
|
||||
override val declarationFactory: JvmDeclarationFactory = JvmDeclarationFactory(methodSignatureMapper)
|
||||
override val sharedVariablesManager = JvmSharedVariablesManager(state.module, builtIns, irBuiltIns)
|
||||
|
||||
override val mapping: Mapping = DefaultMapping()
|
||||
|
||||
val psiErrorBuilder = PsiErrorBuilder(psiSourceManager, state.diagnostics)
|
||||
|
||||
override val ir = JvmIr(irModuleFragment, this.symbolTable)
|
||||
|
||||
+6
-3
@@ -5,7 +5,6 @@
|
||||
|
||||
package org.jetbrains.kotlin.backend.wasm
|
||||
|
||||
import org.jetbrains.kotlin.backend.common.CommonBackendContext
|
||||
import org.jetbrains.kotlin.backend.common.ir.Ir
|
||||
import org.jetbrains.kotlin.backend.common.ir.Symbols
|
||||
import org.jetbrains.kotlin.backend.js.JsDeclarationFactory
|
||||
@@ -14,6 +13,8 @@ import org.jetbrains.kotlin.descriptors.ModuleDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.impl.EmptyPackageFragmentDescriptor
|
||||
import org.jetbrains.kotlin.ir.IrElement
|
||||
import org.jetbrains.kotlin.ir.backend.js.JsCommonBackendContext
|
||||
import org.jetbrains.kotlin.ir.backend.js.JsMapping
|
||||
import org.jetbrains.kotlin.ir.backend.js.JsSharedVariablesManager
|
||||
import org.jetbrains.kotlin.ir.declarations.*
|
||||
import org.jetbrains.kotlin.ir.declarations.impl.IrExternalPackageFragmentImpl
|
||||
@@ -34,7 +35,7 @@ class WasmBackendContext(
|
||||
irModuleFragment: IrModuleFragment,
|
||||
val additionalExportedDeclarations: Set<FqName>,
|
||||
override val configuration: CompilerConfiguration
|
||||
) : CommonBackendContext {
|
||||
) : JsCommonBackendContext {
|
||||
override val builtIns = module.builtIns
|
||||
override var inVerbosePhase: Boolean = false
|
||||
override val scriptMode = false
|
||||
@@ -49,7 +50,9 @@ class WasmBackendContext(
|
||||
)
|
||||
}
|
||||
|
||||
override val declarationFactory = JsDeclarationFactory()
|
||||
override val mapping = JsMapping()
|
||||
|
||||
override val declarationFactory = JsDeclarationFactory(mapping)
|
||||
|
||||
val objectToGetInstanceFunction = mutableMapOf<IrClassSymbol, IrSimpleFunction>()
|
||||
override val internalPackageFqn = FqName("kotlin.wasm")
|
||||
|
||||
Reference in New Issue
Block a user