[JS IC] Properly handle type parameters of Functional interfaces
- drop redundant `IrSymbolTable` and `IrIcModuleDeserializerWithBuiltIns`
This commit is contained in:
committed by
teamcityserver
parent
6aed492703
commit
3d3c70141c
@@ -50,7 +50,7 @@ interface ReferenceSymbolTable {
|
||||
fun referenceFieldFromLinker(sig: IdSignature): IrFieldSymbol
|
||||
fun referencePropertyFromLinker(sig: IdSignature): IrPropertySymbol
|
||||
fun referenceSimpleFunctionFromLinker(sig: IdSignature): IrSimpleFunctionSymbol
|
||||
fun referenceTypeParameterFromLinker(sig: IdSignature): IrTypeParameterSymbol
|
||||
fun referenceGlobalTypeParameterFromLinker(sig: IdSignature): IrTypeParameterSymbol
|
||||
fun referenceTypeAliasFromLinker(sig: IdSignature): IrTypeAliasSymbol
|
||||
|
||||
fun enterScope(owner: IrSymbol)
|
||||
@@ -60,7 +60,7 @@ interface ReferenceSymbolTable {
|
||||
fun leaveScope(owner: IrDeclaration)
|
||||
}
|
||||
|
||||
open class SymbolTable(
|
||||
class SymbolTable(
|
||||
val signaturer: IdSignatureComposer,
|
||||
val irFactory: IrFactory,
|
||||
val nameProvider: NameProvider = NameProvider.DEFAULT
|
||||
@@ -212,7 +212,7 @@ open class SymbolTable(
|
||||
}
|
||||
}
|
||||
|
||||
protected open inner class FlatSymbolTable<D : DeclarationDescriptor, B : IrSymbolOwner, S : IrBindableSymbol<D, B>> :
|
||||
private open inner class FlatSymbolTable<D : DeclarationDescriptor, B : IrSymbolOwner, S : IrBindableSymbol<D, B>> :
|
||||
SymbolTableBase<D, B, S>(lock) {
|
||||
val descriptorToSymbol = linkedMapOf<D, S>()
|
||||
val idSigToSymbol = linkedMapOf<IdSignature, S>()
|
||||
@@ -249,11 +249,11 @@ open class SymbolTable(
|
||||
override fun signature(descriptor: ClassDescriptor): IdSignature? = signaturer.composeEnumEntrySignature(descriptor)
|
||||
}
|
||||
|
||||
protected inner class FieldSymbolTable : FlatSymbolTable<PropertyDescriptor, IrField, IrFieldSymbol>() {
|
||||
private inner class FieldSymbolTable : FlatSymbolTable<PropertyDescriptor, IrField, IrFieldSymbol>() {
|
||||
override fun signature(descriptor: PropertyDescriptor): IdSignature? = signaturer.composeFieldSignature(descriptor)
|
||||
}
|
||||
|
||||
protected inner class ScopedSymbolTable<D : DeclarationDescriptor, B : IrSymbolOwner, S : IrBindableSymbol<D, B>>
|
||||
private inner class ScopedSymbolTable<D : DeclarationDescriptor, B : IrSymbolOwner, S : IrBindableSymbol<D, B>>
|
||||
: SymbolTableBase<D, B, S>(lock) {
|
||||
inner class Scope(val owner: IrSymbol, val parent: Scope?) {
|
||||
private val descriptorToSymbol = linkedMapOf<D, S>()
|
||||
@@ -308,7 +308,7 @@ open class SymbolTable(
|
||||
fun dump(): String = dumpTo(StringBuilder()).toString()
|
||||
}
|
||||
|
||||
protected var currentScope: Scope? = null
|
||||
private var currentScope: Scope? = null
|
||||
|
||||
override fun get(d: D): S? {
|
||||
val scope = currentScope ?: return null
|
||||
@@ -372,13 +372,13 @@ open class SymbolTable(
|
||||
private val classSymbolTable = FlatSymbolTable<ClassDescriptor, IrClass, IrClassSymbol>()
|
||||
private val constructorSymbolTable = FlatSymbolTable<ClassConstructorDescriptor, IrConstructor, IrConstructorSymbol>()
|
||||
private val enumEntrySymbolTable = EnumEntrySymbolTable()
|
||||
protected val fieldSymbolTable = FieldSymbolTable()
|
||||
private val fieldSymbolTable = FieldSymbolTable()
|
||||
private val simpleFunctionSymbolTable = FlatSymbolTable<FunctionDescriptor, IrSimpleFunction, IrSimpleFunctionSymbol>()
|
||||
private val propertySymbolTable = FlatSymbolTable<PropertyDescriptor, IrProperty, IrPropertySymbol>()
|
||||
private val typeAliasSymbolTable = FlatSymbolTable<TypeAliasDescriptor, IrTypeAlias, IrTypeAliasSymbol>()
|
||||
|
||||
protected val globalTypeParameterSymbolTable = FlatSymbolTable<TypeParameterDescriptor, IrTypeParameter, IrTypeParameterSymbol>()
|
||||
protected val scopedTypeParameterSymbolTable by threadLocal {
|
||||
private val globalTypeParameterSymbolTable = FlatSymbolTable<TypeParameterDescriptor, IrTypeParameter, IrTypeParameterSymbol>()
|
||||
private val scopedTypeParameterSymbolTable by threadLocal {
|
||||
ScopedSymbolTable<TypeParameterDescriptor, IrTypeParameter, IrTypeParameterSymbol>()
|
||||
}
|
||||
private val valueParameterSymbolTable by threadLocal {
|
||||
@@ -928,7 +928,7 @@ open class SymbolTable(
|
||||
typeParameterFactory
|
||||
)
|
||||
|
||||
open fun declareGlobalTypeParameter(
|
||||
fun declareGlobalTypeParameter(
|
||||
sig: IdSignature,
|
||||
symbolFactory: () -> IrTypeParameterSymbol,
|
||||
typeParameterFactory: (IrTypeParameterSymbol) -> IrTypeParameter
|
||||
@@ -941,7 +941,11 @@ open class SymbolTable(
|
||||
sig: IdSignature,
|
||||
typeParameterFactory: (IrTypeParameterSymbol) -> IrTypeParameter
|
||||
): IrTypeParameter {
|
||||
return globalTypeParameterSymbolTable.declare(descriptor, { if (sig.isPubliclyVisible) IrTypeParameterPublicSymbolImpl(sig, descriptor) else IrTypeParameterSymbolImpl(descriptor) }, typeParameterFactory)
|
||||
return globalTypeParameterSymbolTable.declare(
|
||||
descriptor,
|
||||
{ if (sig.isPubliclyVisible) IrTypeParameterPublicSymbolImpl(sig, descriptor) else IrTypeParameterSymbolImpl(descriptor) },
|
||||
typeParameterFactory
|
||||
)
|
||||
}
|
||||
|
||||
@OptIn(ObsoleteDescriptorBasedAPI::class)
|
||||
@@ -1020,9 +1024,10 @@ open class SymbolTable(
|
||||
createTypeParameterSymbol(classifier)
|
||||
}
|
||||
|
||||
override fun referenceTypeParameterFromLinker(sig: IdSignature): IrTypeParameterSymbol {
|
||||
require(sig.isLocal)
|
||||
return IrTypeParameterSymbolImpl()
|
||||
override fun referenceGlobalTypeParameterFromLinker(sig: IdSignature): IrTypeParameterSymbol {
|
||||
return globalTypeParameterSymbolTable.referenced(sig) {
|
||||
if (sig.isPubliclyVisible) IrTypeParameterPublicSymbolImpl(sig) else IrTypeParameterSymbolImpl()
|
||||
}
|
||||
}
|
||||
|
||||
fun declareVariable(
|
||||
|
||||
+16
-7
@@ -95,7 +95,7 @@ abstract class IrModuleDeserializer(val moduleDescriptor: ModuleDescriptor, val
|
||||
}
|
||||
|
||||
// Used to resolve built in symbols like `kotlin.ir.internal.*` or `kotlin.FunctionN`
|
||||
open class IrModuleDeserializerWithBuiltIns(
|
||||
class IrModuleDeserializerWithBuiltIns(
|
||||
private val builtIns: IrBuiltIns,
|
||||
private val delegate: IrModuleDeserializer
|
||||
) : IrModuleDeserializer(delegate.moduleDescriptor, delegate.libraryAbiVersion) {
|
||||
@@ -108,11 +108,9 @@ open class IrModuleDeserializerWithBuiltIns(
|
||||
private val irBuiltInsMap = builtIns.knownBuiltins.map {
|
||||
val symbol = (it as IrSymbolOwner).symbol
|
||||
symbol.signature to symbol
|
||||
}.toMap() + additionalBuiltIns(builtIns)
|
||||
}.toMap()
|
||||
|
||||
protected open fun additionalBuiltIns(builtIns: IrBuiltIns): Map<IdSignature, IrSymbol> = emptyMap()
|
||||
|
||||
protected open fun checkIsFunctionInterface(idSig: IdSignature): Boolean {
|
||||
private fun checkIsFunctionInterface(idSig: IdSignature): Boolean {
|
||||
val publicSig = idSig.asPublic()
|
||||
return publicSig != null &&
|
||||
publicSig.packageFqName in functionalPackages &&
|
||||
@@ -150,7 +148,16 @@ open class IrModuleDeserializerWithBuiltIns(
|
||||
}
|
||||
}
|
||||
|
||||
protected open fun resolveFunctionalInterface(idSig: IdSignature, symbolKind: BinarySymbolData.SymbolKind): IrSymbol {
|
||||
private fun resolveFunctionalInterface(idSig: IdSignature, symbolKind: BinarySymbolData.SymbolKind): IrSymbol {
|
||||
if (symbolKind == BinarySymbolData.SymbolKind.TYPE_PARAMETER_SYMBOL) {
|
||||
val composite = idSig as IdSignature.CompositeSignature
|
||||
val classSignature = idSig.container
|
||||
val classSymbol = resolveFunctionalInterface(classSignature, BinarySymbolData.SymbolKind.CLASS_SYMBOL) as IrClassSymbol
|
||||
val typeParameterSig = composite.inner as IdSignature.LocalSignature
|
||||
val typeParameterIndex = typeParameterSig.hashSig ?: error("Expected index for $idSig")
|
||||
val typeParameter = classSymbol.owner.typeParameters[typeParameterIndex.toInt()]
|
||||
return typeParameter.symbol
|
||||
}
|
||||
val publicSig = idSig.asPublic() ?: error("$idSig has to be public")
|
||||
|
||||
val fqnParts = publicSig.nameSegments
|
||||
@@ -184,7 +191,9 @@ open class IrModuleDeserializerWithBuiltIns(
|
||||
override fun deserializeIrSymbol(idSig: IdSignature, symbolKind: BinarySymbolData.SymbolKind): IrSymbol {
|
||||
irBuiltInsMap[idSig]?.let { return it }
|
||||
|
||||
if (checkIsFunctionInterface(idSig)) return resolveFunctionalInterface(idSig, symbolKind)
|
||||
val topLevel = idSig.topLevelSignature()
|
||||
|
||||
if (checkIsFunctionInterface(topLevel)) return resolveFunctionalInterface(idSig, symbolKind)
|
||||
|
||||
return delegate.deserializeIrSymbol(idSig, symbolKind)
|
||||
}
|
||||
|
||||
+2
-2
@@ -56,7 +56,7 @@ class IrSymbolDeserializer(
|
||||
BinarySymbolData.SymbolKind.ANONYMOUS_INIT_SYMBOL -> if (useGlobalSignatures) IrAnonymousInitializerPublicSymbolImpl(idSig) else IrAnonymousInitializerSymbolImpl()
|
||||
BinarySymbolData.SymbolKind.CLASS_SYMBOL -> referenceClassFromLinker(idSig)
|
||||
BinarySymbolData.SymbolKind.CONSTRUCTOR_SYMBOL -> referenceConstructorFromLinker(idSig)
|
||||
BinarySymbolData.SymbolKind.TYPE_PARAMETER_SYMBOL -> referenceTypeParameterFromLinker(idSig)
|
||||
BinarySymbolData.SymbolKind.TYPE_PARAMETER_SYMBOL -> referenceGlobalTypeParameterFromLinker(idSig)
|
||||
BinarySymbolData.SymbolKind.ENUM_ENTRY_SYMBOL -> referenceEnumEntryFromLinker(idSig)
|
||||
BinarySymbolData.SymbolKind.STANDALONE_FIELD_SYMBOL -> referenceFieldFromLinker(idSig)
|
||||
BinarySymbolData.SymbolKind.FIELD_SYMBOL -> referenceFieldFromLinker(idSig)
|
||||
@@ -86,7 +86,7 @@ class IrSymbolDeserializer(
|
||||
deserializeIrSymbolData(idSignature, BinarySymbolData.SymbolKind.PROPERTY_SYMBOL) as IrPropertySymbol
|
||||
|
||||
private fun deserializeIrSymbolData(idSignature: IdSignature, symbolKind: BinarySymbolData.SymbolKind): IrSymbol {
|
||||
if (idSignature.isLocal) {
|
||||
if (!idSignature.isPubliclyVisible) {
|
||||
return deserializedSymbols.getOrPut(idSignature) {
|
||||
if (enqueueAllDeclarations) {
|
||||
enqueueLocalTopLevelDeclaration(idSignature)
|
||||
|
||||
-46
@@ -1,46 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2021 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.ic
|
||||
|
||||
import org.jetbrains.kotlin.ir.declarations.IrFactory
|
||||
import org.jetbrains.kotlin.ir.declarations.IrTypeParameter
|
||||
import org.jetbrains.kotlin.ir.symbols.IrFieldSymbol
|
||||
import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
|
||||
import org.jetbrains.kotlin.ir.symbols.impl.IrFieldPublicSymbolImpl
|
||||
import org.jetbrains.kotlin.ir.symbols.impl.IrTypeParameterPublicSymbolImpl
|
||||
import org.jetbrains.kotlin.ir.util.IdSignature
|
||||
import org.jetbrains.kotlin.ir.util.IdSignatureComposer
|
||||
import org.jetbrains.kotlin.ir.util.NameProvider
|
||||
import org.jetbrains.kotlin.ir.util.SymbolTable
|
||||
|
||||
class IcSymbolTable(
|
||||
signaturer: IdSignatureComposer,
|
||||
irFactory: IrFactory,
|
||||
nameProvider: NameProvider = NameProvider.DEFAULT,
|
||||
) : SymbolTable(
|
||||
signaturer,
|
||||
irFactory,
|
||||
nameProvider,
|
||||
) {
|
||||
override fun referenceFieldFromLinker(sig: IdSignature): IrFieldSymbol =
|
||||
fieldSymbolTable.run {
|
||||
fieldSymbolTable.referenced(sig) { IrFieldPublicSymbolImpl(sig) }
|
||||
}
|
||||
|
||||
override fun declareGlobalTypeParameter(
|
||||
sig: IdSignature,
|
||||
symbolFactory: () -> IrTypeParameterSymbol,
|
||||
typeParameterFactory: (IrTypeParameterSymbol) -> IrTypeParameter
|
||||
): IrTypeParameter {
|
||||
return globalTypeParameterSymbolTable.declare(sig, symbolFactory, typeParameterFactory)
|
||||
}
|
||||
|
||||
override fun referenceTypeParameterFromLinker(sig: IdSignature): IrTypeParameterSymbol {
|
||||
return scopedTypeParameterSymbolTable.get(sig) ?: globalTypeParameterSymbolTable.referenced(sig) {
|
||||
IrTypeParameterPublicSymbolImpl(sig)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -30,7 +30,6 @@ import org.jetbrains.kotlin.descriptors.impl.ModuleDescriptorImpl
|
||||
import org.jetbrains.kotlin.incremental.components.LookupTracker
|
||||
import org.jetbrains.kotlin.ir.IrBuiltIns
|
||||
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
|
||||
import org.jetbrains.kotlin.ir.backend.js.ic.IcSymbolTable
|
||||
import org.jetbrains.kotlin.ir.backend.js.ic.SerializedIcData
|
||||
import org.jetbrains.kotlin.ir.backend.js.lower.serialization.ir.JsIrLinker
|
||||
import org.jetbrains.kotlin.ir.backend.js.lower.serialization.ir.JsIrModuleSerializer
|
||||
@@ -273,7 +272,7 @@ fun loadIr(
|
||||
val allDependencies = depsDescriptors.allDependencies
|
||||
|
||||
val signaturer = IdSignatureDescriptor(JsManglerDesc)
|
||||
val symbolTable = if (loweringsCacheProvider == null) SymbolTable(signaturer, irFactory) else IcSymbolTable(signaturer, irFactory)
|
||||
val symbolTable = SymbolTable(signaturer, irFactory)
|
||||
|
||||
when (mainModule) {
|
||||
is MainModule.SourceFiles -> {
|
||||
|
||||
-64
@@ -1,64 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2021 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.lower.serialization.ir
|
||||
|
||||
import org.jetbrains.kotlin.backend.common.serialization.IrModuleDeserializer
|
||||
import org.jetbrains.kotlin.backend.common.serialization.IrModuleDeserializerWithBuiltIns
|
||||
import org.jetbrains.kotlin.backend.common.serialization.encodings.BinarySymbolData
|
||||
import org.jetbrains.kotlin.backend.common.serialization.knownBuiltins
|
||||
import org.jetbrains.kotlin.ir.IrBuiltIns
|
||||
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
|
||||
import org.jetbrains.kotlin.ir.declarations.IrSymbolOwner
|
||||
import org.jetbrains.kotlin.ir.declarations.IrTypeParametersContainer
|
||||
import org.jetbrains.kotlin.ir.symbols.IrSymbol
|
||||
import org.jetbrains.kotlin.ir.util.IdSignature
|
||||
|
||||
class IrIcModuleDeserializerWithBuiltIns(
|
||||
builtIns: IrBuiltIns,
|
||||
delegate: IrModuleDeserializer,
|
||||
) : IrModuleDeserializerWithBuiltIns(builtIns, delegate) {
|
||||
|
||||
override fun additionalBuiltIns(builtIns: IrBuiltIns): Map<IdSignature, IrSymbol> {
|
||||
val result = mutableMapOf<IdSignature, IrSymbol>()
|
||||
|
||||
builtIns.knownBuiltins.forEach {
|
||||
val symbol = (it as IrSymbolOwner).symbol
|
||||
val declaration = symbol.owner
|
||||
if (declaration is IrSimpleFunction) {
|
||||
declaration.typeParameters.forEachIndexed { i, tp ->
|
||||
result[IdSignature.GlobalFileLocalSignature(symbol.signature!!, 1000_000_000_000L + i, "")] = tp.symbol
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
override fun checkIsFunctionInterface(idSig: IdSignature): Boolean {
|
||||
if (idSig is IdSignature.GlobalFileLocalSignature) return checkIsFunctionInterface(idSig.container)
|
||||
return super.checkIsFunctionInterface(idSig)
|
||||
}
|
||||
|
||||
override fun contains(idSig: IdSignature): Boolean {
|
||||
return super.contains(idSig) || idSig is IdSignature.GlobalFileLocalSignature && checkIsFunctionInterface(idSig.container)
|
||||
}
|
||||
|
||||
override fun resolveFunctionalInterface(idSig: IdSignature, symbolKind: BinarySymbolData.SymbolKind): IrSymbol {
|
||||
if (idSig is IdSignature.GlobalFileLocalSignature) {
|
||||
val containerSymbolKind = when (idSig.container.asPublic()!!.nameSegments.size) {
|
||||
1 -> BinarySymbolData.SymbolKind.CLASS_SYMBOL
|
||||
3 -> BinarySymbolData.SymbolKind.FUNCTION_SYMBOL
|
||||
else -> error("Cannot infer symbolKind")
|
||||
}
|
||||
|
||||
val declaration = resolveFunctionalInterface(idSig.container, containerSymbolKind).owner as IrTypeParametersContainer
|
||||
|
||||
return declaration.typeParameters[(idSig.id - 1000_000_000_000L).toInt()].symbol
|
||||
}
|
||||
|
||||
return super.resolveFunctionalInterface(idSig, symbolKind)
|
||||
}
|
||||
}
|
||||
+1
-5
@@ -78,11 +78,7 @@ class JsIrLinker(
|
||||
moduleDeserializer: IrModuleDeserializer
|
||||
): IrModuleDeserializer {
|
||||
return if (isBuiltInModule(moduleDescriptor)) {
|
||||
if (useGlobalSignatures) {
|
||||
IrIcModuleDeserializerWithBuiltIns(builtIns, moduleDeserializer)
|
||||
} else {
|
||||
IrModuleDeserializerWithBuiltIns(builtIns, moduleDeserializer)
|
||||
}
|
||||
IrModuleDeserializerWithBuiltIns(builtIns, moduleDeserializer)
|
||||
} else moduleDeserializer
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user