Do not use IR_EXTERNAL_JAVA_DECLARATION_STUB in DeclarationStubGenerator
This code is common across backends and it should not depend on JVM-specific behavior. Introduce GeneratorExtensions to reverse the dependency here
This commit is contained in:
@@ -44,8 +44,9 @@ class IrBuiltIns(
|
||||
val irBuiltInsExternalPackageFragment = IrExternalPackageFragmentImpl(IrExternalPackageFragmentSymbolImpl(packageFragment))
|
||||
|
||||
private val symbolTable = outerSymbolTable ?: SymbolTable()
|
||||
private val stubBuilder =
|
||||
DeclarationStubGenerator(builtInsModule, symbolTable, IrDeclarationOrigin.IR_BUILTINS_STUB, languageVersionSettings)
|
||||
private val stubBuilder = DeclarationStubGenerator(
|
||||
builtInsModule, symbolTable, languageVersionSettings, externalDeclarationOrigin = { IrDeclarationOrigin.IR_BUILTINS_STUB }
|
||||
)
|
||||
|
||||
private fun ClassDescriptor.toIrSymbol() = symbolTable.referenceClass(this)
|
||||
private fun KotlinType.toIrType() = typeTranslator.translateType(this)
|
||||
|
||||
@@ -21,30 +21,21 @@ import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
|
||||
import org.jetbrains.kotlin.ir.declarations.*
|
||||
import org.jetbrains.kotlin.ir.declarations.impl.IrFieldImpl
|
||||
import org.jetbrains.kotlin.ir.declarations.impl.IrFunctionImpl
|
||||
import org.jetbrains.kotlin.ir.declarations.impl.IrPropertyImpl
|
||||
import org.jetbrains.kotlin.ir.declarations.impl.IrValueParameterImpl
|
||||
import org.jetbrains.kotlin.ir.declarations.lazy.*
|
||||
import org.jetbrains.kotlin.ir.expressions.impl.IrErrorExpressionImpl
|
||||
import org.jetbrains.kotlin.ir.expressions.impl.IrExpressionBodyImpl
|
||||
import org.jetbrains.kotlin.ir.symbols.impl.IrValueParameterSymbolImpl
|
||||
import org.jetbrains.kotlin.ir.types.impl.IrDynamicTypeImpl
|
||||
import org.jetbrains.kotlin.load.java.descriptors.JavaCallableMemberDescriptor
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||
import org.jetbrains.kotlin.resolve.hasBackingField
|
||||
import org.jetbrains.kotlin.types.KotlinType
|
||||
import org.jetbrains.kotlin.types.Variance
|
||||
import org.jetbrains.kotlin.types.isDynamic
|
||||
|
||||
class DeclarationStubGenerator(
|
||||
moduleDescriptor: ModuleDescriptor,
|
||||
val symbolTable: SymbolTable,
|
||||
val origin: IrDeclarationOrigin,
|
||||
val languageVersionSettings: LanguageVersionSettings,
|
||||
val deserializer: IrDeserializer? = null
|
||||
languageVersionSettings: LanguageVersionSettings,
|
||||
private val externalDeclarationOrigin: ((DeclarationDescriptor) -> IrDeclarationOrigin)? = null,
|
||||
private val deserializer: IrDeserializer? = null
|
||||
) {
|
||||
|
||||
private val lazyTable = symbolTable.lazyWrapper
|
||||
|
||||
internal var unboundSymbolGeneration: Boolean
|
||||
@@ -87,14 +78,15 @@ class DeclarationStubGenerator(
|
||||
throw AssertionError("Unexpected member descriptor: $descriptor")
|
||||
}
|
||||
|
||||
private fun computeOrigin(descriptor: DeclarationDescriptor): IrDeclarationOrigin =
|
||||
externalDeclarationOrigin?.invoke(descriptor) ?: IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB
|
||||
|
||||
internal fun generatePropertyStub(
|
||||
descriptor: PropertyDescriptor,
|
||||
bindingContext: BindingContext? = null
|
||||
): IrProperty = symbolTable.referenceProperty(descriptor) {
|
||||
deserializer?.findDeserializedDeclaration(descriptor) ?:
|
||||
IrLazyProperty(
|
||||
UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, descriptor,
|
||||
this, typeTranslator, bindingContext
|
||||
deserializer?.findDeserializedDeclaration(descriptor) ?: IrLazyProperty(
|
||||
UNDEFINED_OFFSET, UNDEFINED_OFFSET, computeOrigin(descriptor), descriptor, this, typeTranslator, bindingContext
|
||||
)
|
||||
}
|
||||
|
||||
@@ -107,7 +99,7 @@ class DeclarationStubGenerator(
|
||||
val origin =
|
||||
if (descriptor.kind == CallableMemberDescriptor.Kind.FAKE_OVERRIDE)
|
||||
IrDeclarationOrigin.FAKE_OVERRIDE
|
||||
else origin
|
||||
else computeOrigin(descriptor)
|
||||
|
||||
return symbolTable.declareField(
|
||||
UNDEFINED_OFFSET,
|
||||
@@ -116,8 +108,8 @@ class DeclarationStubGenerator(
|
||||
descriptor.original,
|
||||
descriptor.type.toIrType()
|
||||
) {
|
||||
deserializer?.findDeserializedDeclaration(referenced) as? IrField ?:
|
||||
IrFieldImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, it, descriptor.type.toIrType())
|
||||
deserializer?.findDeserializedDeclaration(referenced) as? IrField
|
||||
?: IrFieldImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, it, descriptor.type.toIrType())
|
||||
}.apply {
|
||||
initializer = descriptor.compileTimeInitializer?.let {
|
||||
IrExpressionBodyImpl(
|
||||
@@ -145,17 +137,14 @@ class DeclarationStubGenerator(
|
||||
val origin =
|
||||
if (descriptor.kind == CallableMemberDescriptor.Kind.FAKE_OVERRIDE)
|
||||
IrDeclarationOrigin.FAKE_OVERRIDE
|
||||
else if (origin == IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB && descriptor is JavaCallableMemberDescriptor)
|
||||
IrDeclarationOrigin.IR_EXTERNAL_JAVA_DECLARATION_STUB
|
||||
else
|
||||
origin
|
||||
else computeOrigin(descriptor)
|
||||
return symbolTable.declareSimpleFunction(
|
||||
UNDEFINED_OFFSET, UNDEFINED_OFFSET,
|
||||
origin,
|
||||
descriptor.original
|
||||
) {
|
||||
deserializer?.findDeserializedDeclaration(referenced) as? IrSimpleFunction ?:
|
||||
IrLazyFunction(UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, it, this, typeTranslator)
|
||||
deserializer?.findDeserializedDeclaration(referenced) as? IrSimpleFunction
|
||||
?: IrLazyFunction(UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, it, this, typeTranslator)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -165,11 +154,12 @@ class DeclarationStubGenerator(
|
||||
return referenced.owner
|
||||
}
|
||||
|
||||
val origin = computeOrigin(descriptor)
|
||||
return symbolTable.declareConstructor(
|
||||
UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, descriptor.original
|
||||
) {
|
||||
deserializer?.findDeserializedDeclaration(referenced) as? IrConstructor ?:
|
||||
IrLazyConstructor(UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, it, this, typeTranslator)
|
||||
deserializer?.findDeserializedDeclaration(referenced) as? IrConstructor
|
||||
?: IrLazyConstructor(UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, it, this, typeTranslator)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -177,17 +167,17 @@ class DeclarationStubGenerator(
|
||||
|
||||
internal fun generateValueParameterStub(descriptor: ValueParameterDescriptor): IrValueParameter {
|
||||
return IrValueParameterImpl(
|
||||
UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin,
|
||||
UNDEFINED_OFFSET, UNDEFINED_OFFSET, computeOrigin(descriptor),
|
||||
descriptor, descriptor.type.toIrType(), descriptor.varargElementType?.toIrType()
|
||||
).also { irValueParameter ->
|
||||
if (descriptor.declaresDefaultValue()) {
|
||||
irValueParameter.defaultValue =
|
||||
IrExpressionBodyImpl(
|
||||
IrErrorExpressionImpl(
|
||||
UNDEFINED_OFFSET, UNDEFINED_OFFSET, descriptor.type.toIrType(),
|
||||
"Stub expression for default value of ${descriptor.name}"
|
||||
)
|
||||
IrExpressionBodyImpl(
|
||||
IrErrorExpressionImpl(
|
||||
UNDEFINED_OFFSET, UNDEFINED_OFFSET, descriptor.type.toIrType(),
|
||||
"Stub expression for default value of ${descriptor.name}"
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -197,11 +187,12 @@ class DeclarationStubGenerator(
|
||||
if (referenceClass.isBound) {
|
||||
return referenceClass.owner
|
||||
}
|
||||
val origin = computeOrigin(descriptor)
|
||||
return symbolTable.declareClass(
|
||||
UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, descriptor
|
||||
) {
|
||||
deserializer?.findDeserializedDeclaration(referenceClass) as? IrClass ?:
|
||||
IrLazyClass(UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, it, this, typeTranslator)
|
||||
deserializer?.findDeserializedDeclaration(referenceClass) as? IrClass
|
||||
?: IrLazyClass(UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, it, this, typeTranslator)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -210,9 +201,10 @@ class DeclarationStubGenerator(
|
||||
if (referenced.isBound) {
|
||||
return referenced.owner
|
||||
}
|
||||
val origin = computeOrigin(descriptor)
|
||||
return symbolTable.declareEnumEntry(UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, descriptor) {
|
||||
deserializer?.findDeserializedDeclaration(referenced) as? IrEnumEntry ?:
|
||||
IrLazyEnumEntryImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, it, this, typeTranslator)
|
||||
deserializer?.findDeserializedDeclaration(referenced) as? IrEnumEntry
|
||||
?: IrLazyEnumEntryImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, it, this, typeTranslator)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -221,12 +213,10 @@ class DeclarationStubGenerator(
|
||||
if (referenced.isBound) {
|
||||
return referenced.owner
|
||||
}
|
||||
val origin = computeOrigin(descriptor)
|
||||
return symbolTable.declareGlobalTypeParameter(UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, descriptor) {
|
||||
deserializer?.findDeserializedDeclaration(referenced) as? IrTypeParameter ?:
|
||||
IrLazyTypeParameter(
|
||||
UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin,
|
||||
it, this, typeTranslator
|
||||
)
|
||||
deserializer?.findDeserializedDeclaration(referenced) as? IrTypeParameter
|
||||
?: IrLazyTypeParameter(UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, it, this, typeTranslator)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -235,11 +225,9 @@ class DeclarationStubGenerator(
|
||||
if (referenced.isBound) {
|
||||
return referenced.owner
|
||||
}
|
||||
val origin = computeOrigin(descriptor)
|
||||
return symbolTable.declareScopedTypeParameter(UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, descriptor) {
|
||||
IrLazyTypeParameter(
|
||||
UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin,
|
||||
it, this, typeTranslator
|
||||
)
|
||||
IrLazyTypeParameter(UNDEFINED_OFFSET, UNDEFINED_OFFSET, origin, it, this, typeTranslator)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+4
-2
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.jetbrains.kotlin.ir.util
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
|
||||
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
|
||||
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
|
||||
@@ -26,10 +27,11 @@ class ExternalDependenciesGenerator(
|
||||
moduleDescriptor: ModuleDescriptor,
|
||||
val symbolTable: SymbolTable,
|
||||
val irBuiltIns: IrBuiltIns,
|
||||
externalDeclarationOrigin: ((DeclarationDescriptor) -> IrDeclarationOrigin)? = null,
|
||||
val deserializer: IrDeserializer? = null
|
||||
) {
|
||||
private val stubGenerator = DeclarationStubGenerator(
|
||||
moduleDescriptor, symbolTable, IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB, irBuiltIns.languageVersionSettings, deserializer
|
||||
moduleDescriptor, symbolTable, irBuiltIns.languageVersionSettings, externalDeclarationOrigin, deserializer
|
||||
)
|
||||
|
||||
fun generateUnboundSymbolsAsDependencies(irModule: IrModuleFragment, bindingContext: BindingContext? = null) {
|
||||
@@ -71,4 +73,4 @@ class ExternalDependenciesGenerator(
|
||||
assert(symbolTable.unboundTypeParameters.isEmpty())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user