diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/compiler.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/compiler.kt index 66e8def9c05..434c8cf45f9 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/compiler.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/compiler.kt @@ -55,6 +55,8 @@ fun compile( ExternalDependenciesGenerator(psi2IrContext.moduleDescriptor, psi2IrContext.symbolTable, psi2IrContext.irBuiltIns) .generateUnboundSymbolsAsDependencies(moduleFragment) + MoveExternalDeclarationsToSeparatePlace().lower(moduleFragment.files) + context.performInlining(moduleFragment) context.lower(moduleFragment.files) diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/MoveExternalDeclarationsToSeparatePlace.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/MoveExternalDeclarationsToSeparatePlace.kt new file mode 100644 index 00000000000..1f667da83b9 --- /dev/null +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/MoveExternalDeclarationsToSeparatePlace.kt @@ -0,0 +1,45 @@ +/* + * Copyright 2010-2018 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. + */ + +package org.jetbrains.kotlin.ir.backend.js.lower + +import org.jetbrains.kotlin.backend.common.FileLoweringPass +import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor +import org.jetbrains.kotlin.ir.backend.js.lower.inline.addChild +import org.jetbrains.kotlin.ir.backend.js.utils.isEffectivelyExternal +import org.jetbrains.kotlin.ir.declarations.IrExternalPackageFragment +import org.jetbrains.kotlin.ir.declarations.IrFile +import org.jetbrains.kotlin.ir.declarations.impl.IrExternalPackageFragmentImpl +import org.jetbrains.kotlin.ir.symbols.IrExternalPackageFragmentSymbol +import org.jetbrains.kotlin.name.FqName + +class MoveExternalDeclarationsToSeparatePlace : FileLoweringPass { + private val packageFragment = 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 + } + }, FqName.ROOT) + + override fun lower(irFile: IrFile) { + val it = irFile.declarations.iterator() + + while (it.hasNext()) { + val d = it.next() + + if (d.isEffectivelyExternal()) { + it.remove() + packageFragment.addChild(d) + } + } + } +} \ No newline at end of file diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/utils/descriptorBasedUtils.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/utils/descriptorBasedUtils.kt index ce350b07228..aea1e6816d3 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/utils/descriptorBasedUtils.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/utils/descriptorBasedUtils.kt @@ -9,9 +9,11 @@ import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.annotations.Annotations import org.jetbrains.kotlin.descriptors.impl.ValueParameterDescriptorImpl +import org.jetbrains.kotlin.ir.declarations.IrDeclaration import org.jetbrains.kotlin.ir.declarations.IrTypeParameter import org.jetbrains.kotlin.ir.symbols.IrConstructorSymbol import org.jetbrains.kotlin.name.Name +import org.jetbrains.kotlin.resolve.descriptorUtil.isEffectivelyExternal import org.jetbrains.kotlin.types.KotlinType val IrConstructorSymbol.constructedClass get() = descriptor.constructedClass @@ -46,3 +48,5 @@ fun CallableMemberDescriptor.isFakeOverriddenFromAny(): Boolean { } return overriddenDescriptors.all { it.isFakeOverriddenFromAny() } } + +fun IrDeclaration.isEffectivelyExternal() = descriptor.isEffectivelyExternal() diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/DescriptorUtils.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/DescriptorUtils.kt index d32014f445f..b5eb1990371 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/DescriptorUtils.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/DescriptorUtils.kt @@ -422,6 +422,8 @@ fun MemberDescriptor.isEffectivelyExternal(): Boolean { return containingClass != null && containingClass.isEffectivelyExternal() } +fun DeclarationDescriptor.isEffectivelyExternal() = this is MemberDescriptor && this.isEffectivelyExternal() + fun isParameterOfAnnotation(parameterDescriptor: ParameterDescriptor): Boolean = parameterDescriptor.containingDeclaration.isAnnotationConstructor()