[JS IR BE] Fix fqNames in moveBodilessDeclarationsToSeparatePlace
This commit is contained in:
+51
-45
@@ -13,40 +13,60 @@ import org.jetbrains.kotlin.ir.backend.js.utils.getJsQualifier
|
|||||||
import org.jetbrains.kotlin.ir.declarations.*
|
import org.jetbrains.kotlin.ir.declarations.*
|
||||||
import org.jetbrains.kotlin.ir.declarations.impl.IrExternalPackageFragmentImpl
|
import org.jetbrains.kotlin.ir.declarations.impl.IrExternalPackageFragmentImpl
|
||||||
import org.jetbrains.kotlin.ir.symbols.IrExternalPackageFragmentSymbol
|
import org.jetbrains.kotlin.ir.symbols.IrExternalPackageFragmentSymbol
|
||||||
|
import org.jetbrains.kotlin.ir.util.getFqName
|
||||||
import org.jetbrains.kotlin.ir.util.isEffectivelyExternal
|
import org.jetbrains.kotlin.ir.util.isEffectivelyExternal
|
||||||
import org.jetbrains.kotlin.ir.util.transformFlat
|
import org.jetbrains.kotlin.ir.util.transformFlat
|
||||||
import org.jetbrains.kotlin.ir.util.kotlinPackageFqn
|
|
||||||
import org.jetbrains.kotlin.name.FqName
|
import org.jetbrains.kotlin.name.FqName
|
||||||
import org.jetbrains.kotlin.name.Name
|
|
||||||
|
private val BODILESS_BUILTIN_CLASSES = listOf(
|
||||||
|
"kotlin.String",
|
||||||
|
"kotlin.Nothing",
|
||||||
|
"kotlin.Array",
|
||||||
|
"kotlin.Any",
|
||||||
|
"kotlin.ByteArray",
|
||||||
|
"kotlin.CharArray",
|
||||||
|
"kotlin.ShortArray",
|
||||||
|
"kotlin.IntArray",
|
||||||
|
"kotlin.LongArray",
|
||||||
|
"kotlin.FloatArray",
|
||||||
|
"kotlin.DoubleArray",
|
||||||
|
"kotlin.BooleanArray",
|
||||||
|
"kotlin.Boolean",
|
||||||
|
"kotlin.Byte",
|
||||||
|
"kotlin.Short",
|
||||||
|
"kotlin.Int",
|
||||||
|
"kotlin.Float",
|
||||||
|
"kotlin.Double",
|
||||||
|
"kotlin.Function"
|
||||||
|
).map { FqName(it) }.toSet()
|
||||||
|
|
||||||
|
private class DescriptorlessExternalPackageFragmentSymbol : IrExternalPackageFragmentSymbol {
|
||||||
|
override val descriptor: PackageFragmentDescriptor
|
||||||
|
get() = error("Operation is unsupported")
|
||||||
|
|
||||||
|
private var _owner: IrExternalPackageFragment? = null
|
||||||
|
override val owner get() = _owner!!
|
||||||
|
|
||||||
|
override val isBound get() = _owner != null
|
||||||
|
|
||||||
|
override fun bind(owner: IrExternalPackageFragment) {
|
||||||
|
_owner = owner
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun moveBodilessDeclarationsToSeparatePlace(context: JsIrBackendContext, module: IrModuleFragment) {
|
fun moveBodilessDeclarationsToSeparatePlace(context: JsIrBackendContext, module: IrModuleFragment) {
|
||||||
|
val externalPackageFragment = IrExternalPackageFragmentImpl(
|
||||||
|
DescriptorlessExternalPackageFragmentSymbol(),
|
||||||
|
FqName.ROOT
|
||||||
|
)
|
||||||
|
|
||||||
val builtInClasses = listOf(
|
val bodilessBuiltInsPackageFragment = IrExternalPackageFragmentImpl(
|
||||||
"String",
|
DescriptorlessExternalPackageFragmentSymbol(),
|
||||||
"Nothing",
|
FqName("kotlin")
|
||||||
"Array",
|
)
|
||||||
"Any",
|
|
||||||
"ByteArray",
|
|
||||||
"CharArray",
|
|
||||||
"ShortArray",
|
|
||||||
"IntArray",
|
|
||||||
"LongArray",
|
|
||||||
"FloatArray",
|
|
||||||
"DoubleArray",
|
|
||||||
"BooleanArray",
|
|
||||||
"Boolean",
|
|
||||||
"Byte",
|
|
||||||
"Short",
|
|
||||||
"Int",
|
|
||||||
"Float",
|
|
||||||
"Double",
|
|
||||||
"Function"
|
|
||||||
).map { Name.identifier(it) }.toSet()
|
|
||||||
|
|
||||||
fun isBuiltInClass(declaration: IrDeclaration): Boolean =
|
fun isBuiltInClass(declaration: IrDeclaration): Boolean =
|
||||||
declaration is IrClass && declaration.name in builtInClasses
|
declaration is IrClass && declaration.getFqName() in BODILESS_BUILTIN_CLASSES
|
||||||
|
|
||||||
val packageFragmentMap = mutableMapOf<FqName, IrExternalPackageFragment>()
|
|
||||||
|
|
||||||
fun collectExternalClasses(container: IrDeclarationContainer, includeCurrentLevel: Boolean): List<IrClass> {
|
fun collectExternalClasses(container: IrDeclarationContainer, includeCurrentLevel: Boolean): List<IrClass> {
|
||||||
val externalClasses =
|
val externalClasses =
|
||||||
@@ -71,32 +91,18 @@ fun moveBodilessDeclarationsToSeparatePlace(context: JsIrBackendContext, module:
|
|||||||
|
|
||||||
val it = irFile.declarations.iterator()
|
val it = irFile.declarations.iterator()
|
||||||
|
|
||||||
val packageFragment = packageFragmentMap.getOrPut(irFile.fqName) {
|
|
||||||
IrExternalPackageFragmentImpl(object : IrExternalPackageFragmentSymbol {
|
|
||||||
override val descriptor: PackageFragmentDescriptor
|
|
||||||
get() = error("Operation is unsupported")
|
|
||||||
|
|
||||||
private var _owner: IrExternalPackageFragment? = null
|
|
||||||
override val owner get() = _owner!!
|
|
||||||
|
|
||||||
override val isBound get() = _owner != null
|
|
||||||
|
|
||||||
override fun bind(owner: IrExternalPackageFragment) {
|
|
||||||
_owner = owner
|
|
||||||
}
|
|
||||||
}, irFile.fqName)
|
|
||||||
}
|
|
||||||
|
|
||||||
while (it.hasNext()) {
|
while (it.hasNext()) {
|
||||||
val d = it.next()
|
val d = it.next()
|
||||||
|
|
||||||
if (d.isEffectivelyExternal() || isBuiltInClass(d)) {
|
if (isBuiltInClass(d)) {
|
||||||
|
it.remove()
|
||||||
|
bodilessBuiltInsPackageFragment.addChild(d)
|
||||||
|
} else if (d.isEffectivelyExternal()) {
|
||||||
if (d.getJsModule() != null)
|
if (d.getJsModule() != null)
|
||||||
context.declarationLevelJsModules.add(d)
|
context.declarationLevelJsModules.add(d)
|
||||||
|
|
||||||
it.remove()
|
it.remove()
|
||||||
packageFragment.addChild(d)
|
externalPackageFragment.addChild(d)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return irFile
|
return irFile
|
||||||
|
|||||||
@@ -575,4 +575,13 @@ val IrDeclaration.file: IrFile get() = parent.let {
|
|||||||
is IrDeclaration -> it.file
|
is IrDeclaration -> it.file
|
||||||
else -> TODO("Unexpected declaration parent")
|
else -> TODO("Unexpected declaration parent")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun IrDeclarationWithName.getFqName(): FqName? {
|
||||||
|
val parentFqName = when (val parent = parent) {
|
||||||
|
is IrPackageFragment -> parent.fqName
|
||||||
|
is IrDeclarationWithName -> parent.getFqName()
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
|
return parentFqName?.child(name)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user