JVM_IR KT-49203 generate stubs for not found classes
This commit is contained in:
committed by
TeamCityServer
parent
4cca78f8d8
commit
af18b10da9
+4
-2
@@ -34,6 +34,7 @@ import org.jetbrains.kotlin.ir.util.*
|
|||||||
import org.jetbrains.kotlin.psi.KtFile
|
import org.jetbrains.kotlin.psi.KtFile
|
||||||
import org.jetbrains.kotlin.psi2ir.Psi2IrConfiguration
|
import org.jetbrains.kotlin.psi2ir.Psi2IrConfiguration
|
||||||
import org.jetbrains.kotlin.psi2ir.Psi2IrTranslator
|
import org.jetbrains.kotlin.psi2ir.Psi2IrTranslator
|
||||||
|
import org.jetbrains.kotlin.psi2ir.generators.DeclarationStubGeneratorForNotFoundClasses
|
||||||
import org.jetbrains.kotlin.psi2ir.generators.DeclarationStubGeneratorImpl
|
import org.jetbrains.kotlin.psi2ir.generators.DeclarationStubGeneratorImpl
|
||||||
import org.jetbrains.kotlin.psi2ir.generators.fragments.EvaluatorFragmentInfo
|
import org.jetbrains.kotlin.psi2ir.generators.fragments.EvaluatorFragmentInfo
|
||||||
import org.jetbrains.kotlin.psi2ir.generators.fragments.FragmentContext
|
import org.jetbrains.kotlin.psi2ir.generators.fragments.FragmentContext
|
||||||
@@ -145,10 +146,11 @@ open class JvmIrCodegenFactory(
|
|||||||
irLinker.deserializeIrModuleHeader(it, kotlinLibrary, _moduleName = it.name.asString())
|
irLinker.deserializeIrModuleHeader(it, kotlinLibrary, _moduleName = it.name.asString())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val stubGeneratorForMissingClasses = DeclarationStubGeneratorForNotFoundClasses(stubGenerator)
|
||||||
val irProviders = if (evaluatorFragmentInfoForPsi2Ir != null) {
|
val irProviders = if (evaluatorFragmentInfoForPsi2Ir != null) {
|
||||||
listOf(stubGenerator, irLinker)
|
listOf(stubGenerator, irLinker, stubGeneratorForMissingClasses)
|
||||||
} else {
|
} else {
|
||||||
listOf(irLinker)
|
listOf(irLinker, stubGeneratorForMissingClasses)
|
||||||
}
|
}
|
||||||
|
|
||||||
val irModuleFragment =
|
val irModuleFragment =
|
||||||
|
|||||||
+20
-4
@@ -6,14 +6,15 @@
|
|||||||
package org.jetbrains.kotlin.psi2ir.generators
|
package org.jetbrains.kotlin.psi2ir.generators
|
||||||
|
|
||||||
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
|
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
|
||||||
|
import org.jetbrains.kotlin.descriptors.NotFoundClasses
|
||||||
import org.jetbrains.kotlin.ir.IrBuiltIns
|
import org.jetbrains.kotlin.ir.IrBuiltIns
|
||||||
|
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
|
||||||
|
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
|
||||||
import org.jetbrains.kotlin.ir.declarations.lazy.LazyScopedTypeParametersResolver
|
import org.jetbrains.kotlin.ir.declarations.lazy.LazyScopedTypeParametersResolver
|
||||||
import org.jetbrains.kotlin.ir.linkage.IrDeserializer
|
import org.jetbrains.kotlin.ir.linkage.IrDeserializer
|
||||||
import org.jetbrains.kotlin.ir.linkage.IrProvider
|
import org.jetbrains.kotlin.ir.linkage.IrProvider
|
||||||
import org.jetbrains.kotlin.ir.util.DeclarationStubGenerator
|
import org.jetbrains.kotlin.ir.symbols.IrSymbol
|
||||||
import org.jetbrains.kotlin.ir.util.StubGeneratorExtensions
|
import org.jetbrains.kotlin.ir.util.*
|
||||||
import org.jetbrains.kotlin.ir.util.SymbolTable
|
|
||||||
import org.jetbrains.kotlin.ir.util.TypeTranslator
|
|
||||||
|
|
||||||
class DeclarationStubGeneratorImpl(
|
class DeclarationStubGeneratorImpl(
|
||||||
moduleDescriptor: ModuleDescriptor,
|
moduleDescriptor: ModuleDescriptor,
|
||||||
@@ -45,3 +46,18 @@ fun generateTypicalIrProviderList(
|
|||||||
)
|
)
|
||||||
return listOfNotNull(deserializer, stubGenerator)
|
return listOfNotNull(deserializer, stubGenerator)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@OptIn(ObsoleteDescriptorBasedAPI::class)
|
||||||
|
class DeclarationStubGeneratorForNotFoundClasses(
|
||||||
|
private val stubGenerator: DeclarationStubGeneratorImpl
|
||||||
|
) : IrProvider {
|
||||||
|
|
||||||
|
override fun getDeclaration(symbol: IrSymbol): IrDeclaration? {
|
||||||
|
if (symbol.isBound) return null
|
||||||
|
|
||||||
|
val classDescriptor = symbol.descriptor as? NotFoundClasses.MockClassDescriptor
|
||||||
|
?: return null
|
||||||
|
return stubGenerator.generateClassStub(classDescriptor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+3
-29
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.psi2ir.generators
|
|||||||
import org.jetbrains.kotlin.config.LanguageFeature
|
import org.jetbrains.kotlin.config.LanguageFeature
|
||||||
import org.jetbrains.kotlin.descriptors.*
|
import org.jetbrains.kotlin.descriptors.*
|
||||||
import org.jetbrains.kotlin.ir.IrElement
|
import org.jetbrains.kotlin.ir.IrElement
|
||||||
|
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
|
||||||
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
|
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
|
||||||
import org.jetbrains.kotlin.ir.declarations.*
|
import org.jetbrains.kotlin.ir.declarations.*
|
||||||
import org.jetbrains.kotlin.ir.expressions.IrBlockBody
|
import org.jetbrains.kotlin.ir.expressions.IrBlockBody
|
||||||
@@ -18,10 +19,6 @@ import org.jetbrains.kotlin.ir.expressions.impl.IrGetFieldImpl
|
|||||||
import org.jetbrains.kotlin.ir.expressions.impl.IrGetValueImpl
|
import org.jetbrains.kotlin.ir.expressions.impl.IrGetValueImpl
|
||||||
import org.jetbrains.kotlin.ir.expressions.impl.IrReturnImpl
|
import org.jetbrains.kotlin.ir.expressions.impl.IrReturnImpl
|
||||||
import org.jetbrains.kotlin.ir.expressions.impl.IrSetFieldImpl
|
import org.jetbrains.kotlin.ir.expressions.impl.IrSetFieldImpl
|
||||||
import org.jetbrains.kotlin.ir.types.IrErrorType
|
|
||||||
import org.jetbrains.kotlin.ir.types.IrSimpleType
|
|
||||||
import org.jetbrains.kotlin.ir.types.IrType
|
|
||||||
import org.jetbrains.kotlin.ir.types.IrTypeProjection
|
|
||||||
import org.jetbrains.kotlin.ir.types.impl.IrUninitializedType
|
import org.jetbrains.kotlin.ir.types.impl.IrUninitializedType
|
||||||
import org.jetbrains.kotlin.ir.util.declareSimpleFunctionWithOverrides
|
import org.jetbrains.kotlin.ir.util.declareSimpleFunctionWithOverrides
|
||||||
import org.jetbrains.kotlin.name.Name
|
import org.jetbrains.kotlin.name.Name
|
||||||
@@ -39,6 +36,7 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.isEffectivelyExternal
|
|||||||
import org.jetbrains.kotlin.resolve.descriptorUtil.propertyIfAccessor
|
import org.jetbrains.kotlin.resolve.descriptorUtil.propertyIfAccessor
|
||||||
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
|
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
|
||||||
|
|
||||||
|
@ObsoleteDescriptorBasedAPI
|
||||||
class FunctionGenerator(declarationGenerator: DeclarationGenerator) : DeclarationGeneratorExtension(declarationGenerator) {
|
class FunctionGenerator(declarationGenerator: DeclarationGenerator) : DeclarationGeneratorExtension(declarationGenerator) {
|
||||||
|
|
||||||
constructor(context: GeneratorContext) : this(DeclarationGenerator(context))
|
constructor(context: GeneratorContext) : this(DeclarationGenerator(context))
|
||||||
@@ -74,31 +72,8 @@ class FunctionGenerator(declarationGenerator: DeclarationGenerator) : Declaratio
|
|||||||
.buildWithScope { irFunction ->
|
.buildWithScope { irFunction ->
|
||||||
generateFunctionParameterDeclarationsAndReturnType(irFunction, ktElement, null)
|
generateFunctionParameterDeclarationsAndReturnType(irFunction, ktElement, null)
|
||||||
}
|
}
|
||||||
.takeUnless { irFunction ->
|
|
||||||
irFunction.containsErrorTypesInSignature()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun IrSimpleFunction.containsErrorTypesInSignature(): Boolean {
|
|
||||||
if (this.typeParameters.any { it.superTypes.any { it.containsErrorType() } }) return true
|
|
||||||
this.dispatchReceiverParameter?.let {
|
|
||||||
if (it.type.containsErrorType()) return true
|
|
||||||
}
|
|
||||||
this.extensionReceiverParameter?.let {
|
|
||||||
if (it.type.containsErrorType()) return true
|
|
||||||
}
|
|
||||||
if (this.valueParameters.any { it.type.containsErrorType() }) return true
|
|
||||||
if (this.returnType.containsErrorType()) return true
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun IrType.containsErrorType(): Boolean =
|
|
||||||
when (this) {
|
|
||||||
is IrErrorType -> true
|
|
||||||
is IrSimpleType -> arguments.any { it is IrTypeProjection && it.type.containsErrorType() }
|
|
||||||
else -> false
|
|
||||||
}
|
|
||||||
|
|
||||||
private inline fun declareSimpleFunction(
|
private inline fun declareSimpleFunction(
|
||||||
ktFunction: KtFunction,
|
ktFunction: KtFunction,
|
||||||
ktReceiver: KtElement?,
|
ktReceiver: KtElement?,
|
||||||
@@ -234,8 +209,7 @@ class FunctionGenerator(declarationGenerator: DeclarationGenerator) : Declaratio
|
|||||||
property: PropertyDescriptor,
|
property: PropertyDescriptor,
|
||||||
irAccessor: IrSimpleFunction
|
irAccessor: IrSimpleFunction
|
||||||
): IrExpression? {
|
): IrExpression? {
|
||||||
val containingDeclaration = property.containingDeclaration
|
return when (val containingDeclaration = property.containingDeclaration) {
|
||||||
return when (containingDeclaration) {
|
|
||||||
is ClassDescriptor -> {
|
is ClassDescriptor -> {
|
||||||
val thisAsReceiverParameter = containingDeclaration.thisAsReceiverParameter
|
val thisAsReceiverParameter = containingDeclaration.thisAsReceiverParameter
|
||||||
IrGetValueImpl(
|
IrGetValueImpl(
|
||||||
|
|||||||
+7
-10
@@ -13,7 +13,6 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.jetbrains.kotlin.ir.util
|
package org.jetbrains.kotlin.ir.util
|
||||||
|
|
||||||
import org.jetbrains.kotlin.analyzer.CompilationErrorException
|
import org.jetbrains.kotlin.analyzer.CompilationErrorException
|
||||||
@@ -26,21 +25,19 @@ class ExternalDependenciesGenerator(
|
|||||||
val symbolTable: SymbolTable,
|
val symbolTable: SymbolTable,
|
||||||
private val irProviders: List<IrProvider>
|
private val irProviders: List<IrProvider>
|
||||||
) {
|
) {
|
||||||
|
|
||||||
fun generateUnboundSymbolsAsDependencies() {
|
fun generateUnboundSymbolsAsDependencies() {
|
||||||
// There should be at most one DeclarationStubGenerator (none in closed world?)
|
// There should be at most one DeclarationStubGenerator (none in closed world?)
|
||||||
irProviders.singleOrNull { it is DeclarationStubGenerator }?.let {
|
irProviders.singleOrNull { it is DeclarationStubGenerator }?.let {
|
||||||
(it as DeclarationStubGenerator).unboundSymbolGeneration = true
|
(it as DeclarationStubGenerator).unboundSymbolGeneration = true
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
Deserializing a reference may lead to new unbound references, so we loop until none are left.
|
|
||||||
*/
|
|
||||||
var unbound = setOf<IrSymbol>()
|
|
||||||
lateinit var prevUnbound: Set<IrSymbol>
|
|
||||||
try {
|
|
||||||
do {
|
|
||||||
prevUnbound = unbound
|
|
||||||
unbound = symbolTable.allUnbound
|
|
||||||
|
|
||||||
|
// Deserializing a reference may lead to new unbound references, so we loop until none are left.
|
||||||
|
try {
|
||||||
|
var unbound = setOf<IrSymbol>()
|
||||||
|
do {
|
||||||
|
val prevUnbound = unbound
|
||||||
|
unbound = symbolTable.allUnbound
|
||||||
for (symbol in unbound) {
|
for (symbol in unbound) {
|
||||||
// Symbol could get bound as a side effect of deserializing other symbols.
|
// Symbol could get bound as a side effect of deserializing other symbols.
|
||||||
if (!symbol.isBound) {
|
if (!symbol.isBound) {
|
||||||
|
|||||||
@@ -145,9 +145,6 @@ abstract class TypeTranslator(
|
|||||||
lowerType.constructor.declarationDescriptor as? ClassDescriptor
|
lowerType.constructor.declarationDescriptor as? ClassDescriptor
|
||||||
?: throw AssertionError("No class descriptor for lower type $lowerType of $approximatedType")
|
?: throw AssertionError("No class descriptor for lower type $lowerType of $approximatedType")
|
||||||
annotations = translateTypeAnnotations(upperType, approximatedType)
|
annotations = translateTypeAnnotations(upperType, approximatedType)
|
||||||
if (lowerTypeDescriptor is NotFoundClasses.MockClassDescriptor) {
|
|
||||||
return IrErrorTypeImpl(approximatedType, annotations, variance)
|
|
||||||
}
|
|
||||||
classifier = symbolTable.referenceClass(lowerTypeDescriptor)
|
classifier = symbolTable.referenceClass(lowerTypeDescriptor)
|
||||||
arguments = when {
|
arguments = when {
|
||||||
approximatedType is RawType ->
|
approximatedType is RawType ->
|
||||||
|
|||||||
+3
-2
@@ -130,7 +130,6 @@ abstract class KotlinIrLinker(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun getDeclaration(symbol: IrSymbol): IrDeclaration? {
|
override fun getDeclaration(symbol: IrSymbol): IrDeclaration? {
|
||||||
|
|
||||||
if (!symbol.isPublicApi) {
|
if (!symbol.isPublicApi) {
|
||||||
if (symbol.hasDescriptor) {
|
if (symbol.hasDescriptor) {
|
||||||
val descriptor = symbol.descriptor
|
val descriptor = symbol.descriptor
|
||||||
@@ -142,7 +141,9 @@ abstract class KotlinIrLinker(
|
|||||||
|
|
||||||
if (!symbol.isBound) {
|
if (!symbol.isBound) {
|
||||||
try {
|
try {
|
||||||
findDeserializedDeclarationForSymbol(symbol) ?: tryResolveCustomDeclaration(symbol) ?: return null
|
findDeserializedDeclarationForSymbol(symbol)
|
||||||
|
?: tryResolveCustomDeclaration(symbol)
|
||||||
|
?: return null
|
||||||
} catch (e: IrSymbolTypeMismatchException) {
|
} catch (e: IrSymbolTypeMismatchException) {
|
||||||
throw SymbolTypeMismatch(e, deserializersForModules.values, userVisibleIrModulesSupport).raiseIssue(messageLogger)
|
throw SymbolTypeMismatch(e, deserializersForModules.values, userVisibleIrModulesSupport).raiseIssue(messageLogger)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
// WITH_RUNTIME
|
// WITH_RUNTIME
|
||||||
// IGNORE_BACKEND: JVM_IR
|
|
||||||
// TARGET_BACKEND: JVM
|
// TARGET_BACKEND: JVM
|
||||||
|
|
||||||
// MODULE: lib1
|
// MODULE: lib1
|
||||||
|
|||||||
Reference in New Issue
Block a user