JVM_IR KT-49203 generate stubs for not found classes

This commit is contained in:
Dmitry Petrov
2021-10-14 08:17:07 +03:00
committed by TeamCityServer
parent 4cca78f8d8
commit af18b10da9
7 changed files with 37 additions and 51 deletions
@@ -34,6 +34,7 @@ import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi2ir.Psi2IrConfiguration
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.fragments.EvaluatorFragmentInfo
import org.jetbrains.kotlin.psi2ir.generators.fragments.FragmentContext
@@ -145,10 +146,11 @@ open class JvmIrCodegenFactory(
irLinker.deserializeIrModuleHeader(it, kotlinLibrary, _moduleName = it.name.asString())
}
val stubGeneratorForMissingClasses = DeclarationStubGeneratorForNotFoundClasses(stubGenerator)
val irProviders = if (evaluatorFragmentInfoForPsi2Ir != null) {
listOf(stubGenerator, irLinker)
listOf(stubGenerator, irLinker, stubGeneratorForMissingClasses)
} else {
listOf(irLinker)
listOf(irLinker, stubGeneratorForMissingClasses)
}
val irModuleFragment =
@@ -6,14 +6,15 @@
package org.jetbrains.kotlin.psi2ir.generators
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.NotFoundClasses
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.linkage.IrDeserializer
import org.jetbrains.kotlin.ir.linkage.IrProvider
import org.jetbrains.kotlin.ir.util.DeclarationStubGenerator
import org.jetbrains.kotlin.ir.util.StubGeneratorExtensions
import org.jetbrains.kotlin.ir.util.SymbolTable
import org.jetbrains.kotlin.ir.util.TypeTranslator
import org.jetbrains.kotlin.ir.symbols.IrSymbol
import org.jetbrains.kotlin.ir.util.*
class DeclarationStubGeneratorImpl(
moduleDescriptor: ModuleDescriptor,
@@ -45,3 +46,18 @@ fun generateTypicalIrProviderList(
)
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)
}
}
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.psi2ir.generators
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.declarations.*
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.IrReturnImpl
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.util.declareSimpleFunctionWithOverrides
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.utils.addToStdlib.safeAs
@ObsoleteDescriptorBasedAPI
class FunctionGenerator(declarationGenerator: DeclarationGenerator) : DeclarationGeneratorExtension(declarationGenerator) {
constructor(context: GeneratorContext) : this(DeclarationGenerator(context))
@@ -74,31 +72,8 @@ class FunctionGenerator(declarationGenerator: DeclarationGenerator) : Declaratio
.buildWithScope { irFunction ->
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(
ktFunction: KtFunction,
ktReceiver: KtElement?,
@@ -234,8 +209,7 @@ class FunctionGenerator(declarationGenerator: DeclarationGenerator) : Declaratio
property: PropertyDescriptor,
irAccessor: IrSimpleFunction
): IrExpression? {
val containingDeclaration = property.containingDeclaration
return when (containingDeclaration) {
return when (val containingDeclaration = property.containingDeclaration) {
is ClassDescriptor -> {
val thisAsReceiverParameter = containingDeclaration.thisAsReceiverParameter
IrGetValueImpl(
@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.ir.util
import org.jetbrains.kotlin.analyzer.CompilationErrorException
@@ -26,21 +25,19 @@ class ExternalDependenciesGenerator(
val symbolTable: SymbolTable,
private val irProviders: List<IrProvider>
) {
fun generateUnboundSymbolsAsDependencies() {
// There should be at most one DeclarationStubGenerator (none in closed world?)
irProviders.singleOrNull { it is DeclarationStubGenerator }?.let {
(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) {
// Symbol could get bound as a side effect of deserializing other symbols.
if (!symbol.isBound) {
@@ -145,9 +145,6 @@ abstract class TypeTranslator(
lowerType.constructor.declarationDescriptor as? ClassDescriptor
?: throw AssertionError("No class descriptor for lower type $lowerType of $approximatedType")
annotations = translateTypeAnnotations(upperType, approximatedType)
if (lowerTypeDescriptor is NotFoundClasses.MockClassDescriptor) {
return IrErrorTypeImpl(approximatedType, annotations, variance)
}
classifier = symbolTable.referenceClass(lowerTypeDescriptor)
arguments = when {
approximatedType is RawType ->
@@ -130,7 +130,6 @@ abstract class KotlinIrLinker(
}
override fun getDeclaration(symbol: IrSymbol): IrDeclaration? {
if (!symbol.isPublicApi) {
if (symbol.hasDescriptor) {
val descriptor = symbol.descriptor
@@ -142,7 +141,9 @@ abstract class KotlinIrLinker(
if (!symbol.isBound) {
try {
findDeserializedDeclarationForSymbol(symbol) ?: tryResolveCustomDeclaration(symbol) ?: return null
findDeserializedDeclarationForSymbol(symbol)
?: tryResolveCustomDeclaration(symbol)
?: return null
} catch (e: IrSymbolTypeMismatchException) {
throw SymbolTypeMismatch(e, deserializersForModules.values, userVisibleIrModulesSupport).raiseIssue(messageLogger)
}
-1
View File
@@ -1,5 +1,4 @@
// WITH_RUNTIME
// IGNORE_BACKEND: JVM_IR
// TARGET_BACKEND: JVM
// MODULE: lib1