IR: introduce StubGeneratorExtensions for ExternalDependenciesGenerator
This is a refactoring to simplify the API of ExternalDependenciesGenerator and to constrain future additions of platform-specific stub generator extensions.
This commit is contained in:
@@ -36,9 +36,8 @@ class DeclarationStubGenerator(
|
||||
moduleDescriptor: ModuleDescriptor,
|
||||
val symbolTable: SymbolTable,
|
||||
languageVersionSettings: LanguageVersionSettings,
|
||||
private val externalDeclarationOrigin: ((DeclarationDescriptor) -> IrDeclarationOrigin)? = null,
|
||||
private val irProviders: List<IrProvider> = emptyList(),
|
||||
private val facadeClassGenerator: (DeserializedContainerSource) -> IrClass? = { null }
|
||||
private val extensions: StubGeneratorExtensions = StubGeneratorExtensions.EMPTY
|
||||
) {
|
||||
private val lazyTable = symbolTable.lazyWrapper
|
||||
|
||||
@@ -49,7 +48,8 @@ class DeclarationStubGenerator(
|
||||
}
|
||||
|
||||
|
||||
val typeTranslator = TypeTranslator(lazyTable, languageVersionSettings, moduleDescriptor.builtIns, LazyScopedTypeParametersResolver(lazyTable), true)
|
||||
val typeTranslator =
|
||||
TypeTranslator(lazyTable, languageVersionSettings, moduleDescriptor.builtIns, LazyScopedTypeParametersResolver(lazyTable), true)
|
||||
private val constantValueGenerator = ConstantValueGenerator(moduleDescriptor, lazyTable)
|
||||
|
||||
private val facadeClassMap = mutableMapOf<DeserializedContainerSource, IrClass?>()
|
||||
@@ -79,7 +79,7 @@ class DeclarationStubGenerator(
|
||||
val packageFragment = directMember.containingDeclaration as? PackageFragmentDescriptor ?: return null
|
||||
val containerSource = directMember.safeAs<DescriptorWithContainerSource>()?.containerSource ?: return null
|
||||
return facadeClassMap.getOrPut(containerSource) {
|
||||
facadeClassGenerator(containerSource)?.also { facade ->
|
||||
extensions.generateFacadeClass(containerSource)?.also { facade ->
|
||||
val packageStub = generateOrGetEmptyExternalPackageFragmentStub(packageFragment)
|
||||
facade.parent = packageStub
|
||||
packageStub.declarations.add(facade)
|
||||
@@ -106,7 +106,7 @@ class DeclarationStubGenerator(
|
||||
}
|
||||
|
||||
private fun computeOrigin(descriptor: DeclarationDescriptor): IrDeclarationOrigin =
|
||||
externalDeclarationOrigin?.invoke(descriptor) ?: IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB
|
||||
extensions.computeExternalDeclarationOrigin(descriptor) ?: IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB
|
||||
|
||||
internal fun generatePropertyStub(
|
||||
descriptor: PropertyDescriptor,
|
||||
|
||||
+4
-10
@@ -16,34 +16,28 @@
|
||||
|
||||
package org.jetbrains.kotlin.ir.util
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
|
||||
import org.jetbrains.kotlin.ir.declarations.IrClass
|
||||
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
|
||||
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
|
||||
import org.jetbrains.kotlin.ir.symbols.IrSymbol
|
||||
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedContainerSource
|
||||
import kotlin.math.min
|
||||
|
||||
class ExternalDependenciesGenerator(
|
||||
moduleDescriptor: ModuleDescriptor,
|
||||
val symbolTable: SymbolTable,
|
||||
val irBuiltIns: IrBuiltIns,
|
||||
externalDeclarationOrigin: ((DeclarationDescriptor) -> IrDeclarationOrigin)? = null,
|
||||
private val deserializer: IrDeserializer? = null,
|
||||
irProviders: List<IrProvider> = emptyList(),
|
||||
facadeClassGenerator: (DeserializedContainerSource) -> IrClass? = { null }
|
||||
extensions: StubGeneratorExtensions = StubGeneratorExtensions.EMPTY
|
||||
) {
|
||||
private val stubGenerator = DeclarationStubGenerator(
|
||||
moduleDescriptor, symbolTable, irBuiltIns.languageVersionSettings, externalDeclarationOrigin,
|
||||
listOfNotNull(deserializer) + irProviders, facadeClassGenerator
|
||||
moduleDescriptor, symbolTable, irBuiltIns.languageVersionSettings, listOfNotNull(deserializer) + irProviders, extensions
|
||||
)
|
||||
|
||||
fun generateUnboundSymbolsAsDependencies() {
|
||||
stubGenerator.unboundSymbolGeneration = true
|
||||
do {
|
||||
fun <T> haveNotStabilized(prev: ArrayList<T>, cur: Set<T>) =
|
||||
cur.isNotEmpty() && (prev.size != cur.size || prev.any { !cur.contains(it) })
|
||||
cur.isNotEmpty() && (prev.size != cur.size || prev.any { !cur.contains(it) })
|
||||
|
||||
val unboundClasses = ArrayList(symbolTable.unboundClasses)
|
||||
val unboundConstructors = ArrayList(symbolTable.unboundConstructors)
|
||||
@@ -89,4 +83,4 @@ class ExternalDependenciesGenerator(
|
||||
s.toList().subList(0, min(10, s.size)).joinToString(separator = "\n") { it.descriptor.toString() }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* Copyright 2010-2019 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.util
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||
import org.jetbrains.kotlin.ir.declarations.IrClass
|
||||
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
|
||||
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedContainerSource
|
||||
|
||||
open class StubGeneratorExtensions {
|
||||
open fun computeExternalDeclarationOrigin(descriptor: DeclarationDescriptor): IrDeclarationOrigin? = null
|
||||
|
||||
open fun generateFacadeClass(source: DeserializedContainerSource): IrClass? = null
|
||||
|
||||
companion object {
|
||||
@JvmField
|
||||
val EMPTY = StubGeneratorExtensions()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user