JVM IR: support inline classes with private constructors from other modules

#KT-44723 Fixed
This commit is contained in:
Alexander Udalov
2021-03-19 21:01:09 +01:00
parent dac218dc39
commit b5f9b1dfc0
12 changed files with 204 additions and 14 deletions
@@ -58,8 +58,8 @@ class IrLazyClass(
ArrayList<IrDeclaration>().also {
typeTranslator.buildWithScope(this) {
generateChildStubs(descriptor.constructors, it)
generateMemberStubs(descriptor.defaultType.memberScope, it)
generateMemberStubs(descriptor.staticScope, it)
generateChildStubs(descriptor.defaultType.memberScope.getContributedDescriptors(), it)
generateChildStubs(descriptor.staticScope.getContributedDescriptors(), it)
}
}.also {
it.forEach {
@@ -68,6 +68,22 @@ class IrLazyClass(
}
}
private fun generateChildStubs(descriptors: Collection<DeclarationDescriptor>, declarations: MutableList<IrDeclaration>) {
descriptors.mapNotNullTo(declarations) { descriptor ->
if (shouldBuildStub(descriptor)) stubGenerator.generateMemberStub(descriptor) else null
}
}
private fun shouldBuildStub(descriptor: DeclarationDescriptor): Boolean =
descriptor !is DeclarationDescriptorWithVisibility ||
!DescriptorVisibilities.isPrivate(descriptor.visibility) ||
// Always build lazy IR stubs for inline class primary constructors.
// This is needed because primary constructors of inline classes can be private, and prior to 1.5.0, there was no way
// to determine the inline class representation other than by loading the single value parameter of the primary constructor
// (corresponding metadata entry was added in 1.5.0). Backend still tries to load inline class representation in this way
// to support compilation against inline classes compiled with 1.4.30 or earlier.
(descriptor is ConstructorDescriptor && isInline)
override var typeParameters: List<IrTypeParameter> by lazyVar(stubGenerator.lock) {
descriptor.declaredTypeParameters.mapTo(arrayListOf()) {
stubGenerator.generateOrGetTypeParameterStub(it)
@@ -14,7 +14,6 @@ import org.jetbrains.kotlin.ir.symbols.impl.IrValueParameterSymbolImpl
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.util.DeclarationStubGenerator
import org.jetbrains.kotlin.ir.util.TypeTranslator
import org.jetbrains.kotlin.resolve.scopes.MemberScope
import org.jetbrains.kotlin.types.KotlinType
import kotlin.properties.ReadWriteProperty
@@ -36,17 +35,6 @@ interface IrLazyDeclarationBase : IrDeclaration {
isHidden = false, isAssignable = false
)
fun generateMemberStubs(memberScope: MemberScope, container: MutableList<IrDeclaration>) {
generateChildStubs(memberScope.getContributedDescriptors(), container)
}
fun generateChildStubs(descriptors: Collection<DeclarationDescriptor>, declarations: MutableList<IrDeclaration>) {
descriptors.mapNotNullTo(declarations) { descriptor ->
if (descriptor is DeclarationDescriptorWithVisibility && DescriptorVisibilities.isPrivate(descriptor.visibility)) null
else stubGenerator.generateMemberStub(descriptor)
}
}
fun createLazyAnnotations(): ReadWriteProperty<Any?, List<IrConstructorCall>> = lazyVar(stubGenerator.lock) {
descriptor.annotations.mapNotNull(typeTranslator.constantValueGenerator::generateAnnotationConstructorCall).toMutableList()
}