[IR] Use common expect/actual matching algorithm in IR actualizer

^KT-58578 Fixed
This commit is contained in:
Dmitriy Novozhilov
2023-06-06 17:27:03 +03:00
committed by Space Team
parent 62534f43c9
commit ba41e8ec38
52 changed files with 1130 additions and 440 deletions
@@ -25,6 +25,7 @@ import org.jetbrains.kotlin.ir.types.IrSimpleType
import org.jetbrains.kotlin.ir.types.IrTypeAbbreviation
import org.jetbrains.kotlin.ir.util.IdSignature
import org.jetbrains.kotlin.ir.util.SymbolTable
import org.jetbrains.kotlin.mpp.*
import org.jetbrains.kotlin.types.model.TypeConstructorMarker
import org.jetbrains.kotlin.types.model.TypeParameterMarker
@@ -46,7 +47,7 @@ import org.jetbrains.kotlin.types.model.TypeParameterMarker
* @see IdSignature
* @see SymbolTable
*/
interface IrSymbol {
interface IrSymbol : DeclarationSymbolMarker {
/**
* The declaration that this symbol refers to if it's bound.
@@ -167,7 +168,7 @@ interface IrAnonymousInitializerSymbol : IrBindableSymbol<ClassDescriptor, IrAno
*
* @see IrGetEnumValue
*/
interface IrEnumEntrySymbol : IrBindableSymbol<ClassDescriptor, IrEnumEntry>
interface IrEnumEntrySymbol : IrBindableSymbol<ClassDescriptor, IrEnumEntry>, EnumEntrySymbolMarker
/**
* A symbol whose [owner] is [IrField].
@@ -175,7 +176,7 @@ interface IrEnumEntrySymbol : IrBindableSymbol<ClassDescriptor, IrEnumEntry>
* @see IrGetField
* @see IrSetField
*/
interface IrFieldSymbol : IrBindableSymbol<PropertyDescriptor, IrField>
interface IrFieldSymbol : IrBindableSymbol<PropertyDescriptor, IrField>, FieldSymbolMarker
/**
* A symbol whose [owner] is [IrClass], [IrScript] or [IrTypeParameter].
@@ -195,7 +196,7 @@ sealed interface IrClassifierSymbol : IrSymbol, TypeConstructorMarker {
* @see IrCall.superQualifierSymbol
* @see IrFieldAccessExpression.superQualifierSymbol
*/
interface IrClassSymbol : IrClassifierSymbol, IrBindableSymbol<ClassDescriptor, IrClass>
interface IrClassSymbol : IrClassifierSymbol, IrBindableSymbol<ClassDescriptor, IrClass>, RegularClassSymbolMarker
/**
* A symbol whose [owner] is [IrScript].
@@ -205,7 +206,10 @@ interface IrScriptSymbol : IrClassifierSymbol, IrBindableSymbol<ScriptDescriptor
/**
* A symbol whose [owner] is [IrTypeParameter].
*/
interface IrTypeParameterSymbol : IrClassifierSymbol, IrBindableSymbol<TypeParameterDescriptor, IrTypeParameter>, TypeParameterMarker
interface IrTypeParameterSymbol : IrClassifierSymbol,
IrBindableSymbol<TypeParameterDescriptor, IrTypeParameter>,
TypeParameterMarker,
TypeParameterSymbolMarker
/**
* A symbol whose [owner] is [IrValueParameter] or [IrVariable].
@@ -223,7 +227,7 @@ sealed interface IrValueSymbol : IrSymbol {
/**
* A symbol whose [owner] is [IrValueParameter].
*/
interface IrValueParameterSymbol : IrValueSymbol, IrBindableSymbol<ParameterDescriptor, IrValueParameter>
interface IrValueParameterSymbol : IrValueSymbol, IrBindableSymbol<ParameterDescriptor, IrValueParameter>, ValueParameterSymbolMarker
/**
* A symbol whose [owner] is [IrVariable].
@@ -247,7 +251,7 @@ sealed interface IrReturnTargetSymbol : IrSymbol {
*
* @see IrFunctionReference
*/
sealed interface IrFunctionSymbol : IrReturnTargetSymbol {
sealed interface IrFunctionSymbol : IrReturnTargetSymbol, FunctionSymbolMarker {
override val owner: IrFunction
}
@@ -256,14 +260,14 @@ sealed interface IrFunctionSymbol : IrReturnTargetSymbol {
*
* @see IrConstructorCall
*/
interface IrConstructorSymbol : IrFunctionSymbol, IrBindableSymbol<ClassConstructorDescriptor, IrConstructor>
interface IrConstructorSymbol : IrFunctionSymbol, IrBindableSymbol<ClassConstructorDescriptor, IrConstructor>, ConstructorSymbolMarker
/**
* A symbol whose [owner] is [IrSimpleFunction].
*
* @see IrCall
*/
interface IrSimpleFunctionSymbol : IrFunctionSymbol, IrBindableSymbol<FunctionDescriptor, IrSimpleFunction>
interface IrSimpleFunctionSymbol : IrFunctionSymbol, IrBindableSymbol<FunctionDescriptor, IrSimpleFunction>, SimpleFunctionSymbolMarker
/**
* A symbol whose [owner] is [IrReturnableBlock].
@@ -273,7 +277,7 @@ interface IrReturnableBlockSymbol : IrReturnTargetSymbol, IrBindableSymbol<Funct
/**
* A symbol whose [owner] is [IrProperty].
*/
interface IrPropertySymbol : IrBindableSymbol<PropertyDescriptor, IrProperty>
interface IrPropertySymbol : IrBindableSymbol<PropertyDescriptor, IrProperty>, PropertySymbolMarker
/**
* A symbol whose [owner] is [IrLocalDelegatedProperty].
@@ -285,4 +289,4 @@ interface IrLocalDelegatedPropertySymbol : IrBindableSymbol<VariableDescriptorWi
*
* @see IrTypeAbbreviation
*/
interface IrTypeAliasSymbol : IrBindableSymbol<TypeAliasDescriptor, IrTypeAlias>
interface IrTypeAliasSymbol : IrBindableSymbol<TypeAliasDescriptor, IrTypeAlias>, TypeAliasSymbolMarker
@@ -13,8 +13,11 @@ import org.jetbrains.kotlin.types.Variance
import org.jetbrains.kotlin.types.model.TypeSubstitutorMarker
import org.jetbrains.kotlin.utils.memoryOptimizedMap
abstract class AbstractIrTypeSubstitutor : TypeSubstitutorMarker {
abstract fun substitute(type: IrType): IrType
}
abstract class AbstractIrTypeSubstitutor(private val irBuiltIns: IrBuiltIns) : TypeSubstitutorMarker {
abstract class BaseIrTypeSubstitutor(private val irBuiltIns: IrBuiltIns) : AbstractIrTypeSubstitutor() {
private fun IrType.typeParameterConstructor(): IrTypeParameterSymbol? {
return if (this is IrSimpleType) classifier as? IrTypeParameterSymbol
@@ -25,7 +28,7 @@ abstract class AbstractIrTypeSubstitutor(private val irBuiltIns: IrBuiltIns) : T
abstract fun isEmptySubstitution(): Boolean
fun substitute(type: IrType): IrType {
final override fun substitute(type: IrType): IrType {
if (isEmptySubstitution()) return type
return substituteType(type)
}
@@ -63,8 +66,9 @@ abstract class AbstractIrTypeSubstitutor(private val irBuiltIns: IrBuiltIns) : T
class IrTypeSubstitutor(
typeParameters: List<IrTypeParameterSymbol>,
typeArguments: List<IrTypeArgument>,
irBuiltIns: IrBuiltIns
) : AbstractIrTypeSubstitutor(irBuiltIns) {
irBuiltIns: IrBuiltIns,
private val allowEmptySubstitution: Boolean = false
) : BaseIrTypeSubstitutor(irBuiltIns) {
init {
assert(typeParameters.size == typeArguments.size) {
@@ -80,7 +84,8 @@ class IrTypeSubstitutor(
override fun getSubstitutionArgument(typeParameter: IrTypeParameterSymbol): IrTypeArgument =
substitution[typeParameter]
?: throw AssertionError("Unsubstituted type parameter: ${typeParameter.owner.render()}")
?: typeParameter.takeIf { allowEmptySubstitution }?.owner?.defaultType
?: error("Unsubstituted type parameter: ${typeParameter.owner.render()}")
override fun isEmptySubstitution(): Boolean = substitution.isEmpty()
}
@@ -90,7 +95,7 @@ class IrCapturedTypeSubstitutor(
typeArguments: List<IrTypeArgument>,
capturedTypes: List<IrCapturedType?>,
irBuiltIns: IrBuiltIns
) : AbstractIrTypeSubstitutor(irBuiltIns) {
) : BaseIrTypeSubstitutor(irBuiltIns) {
init {
assert(typeArguments.size == typeParameters.size)
@@ -108,3 +113,10 @@ class IrCapturedTypeSubstitutor(
override fun isEmptySubstitution(): Boolean = oldSubstitution.isEmpty()
}
class IrChainedSubstitutor(val first: AbstractIrTypeSubstitutor, val second: AbstractIrTypeSubstitutor) : AbstractIrTypeSubstitutor() {
override fun substitute(type: IrType): IrType {
val firstResult = first.substitute(type)
return second.substitute(firstResult)
}
}
@@ -169,7 +169,7 @@ val IrClassifierSymbol.defaultType: IrType
is IrScriptSymbol -> unexpectedSymbolKind<IrClassifierSymbol>()
}
val IrTypeParameter.defaultType: IrType
val IrTypeParameter.defaultType: IrSimpleType
get() = IrSimpleTypeImpl(
symbol,
SimpleTypeNullability.NOT_SPECIFIED,
@@ -15,10 +15,7 @@ import org.jetbrains.kotlin.ir.symbols.IrClassifierSymbol
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.IrSymbol
import org.jetbrains.kotlin.ir.symbols.impl.IrClassPublicSymbolImpl
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.name.SpecialNames
import org.jetbrains.kotlin.name.*
import org.jetbrains.kotlin.resolve.descriptorUtil.module
import org.jetbrains.kotlin.utils.filterIsInstanceAnd
import java.io.File
@@ -57,12 +54,49 @@ val IrDeclarationParent.kotlinFqName: FqName
}
val IrClass.classId: ClassId?
get() = classIdImpl
val IrTypeAlias.classId: ClassId?
get() = classIdImpl
private val IrDeclarationWithName.classIdImpl: ClassId?
get() = when (val parent = this.parent) {
is IrClass -> parent.classId?.createNestedClassId(this.name)
is IrPackageFragment -> ClassId.topLevel(parent.packageFqName.child(this.name))
else -> null
}
val IrClass.classIdOrFail: ClassId
get() = classIdOrFailImpl
val IrTypeAlias.classIdOrFail: ClassId
get() = classIdOrFailImpl
private val IrDeclarationWithName.classIdOrFailImpl: ClassId
get() = classIdImpl ?: error("No classId for $this")
val IrFunction.callableId: CallableId
get() = callableIdImpl
val IrProperty.callableId: CallableId
get() = callableIdImpl
val IrField.callableId: CallableId
get() = callableIdImpl
val IrEnumEntry.callableId: CallableId
get() = callableIdImpl
private val IrDeclarationWithName.callableIdImpl: CallableId
get() {
if (this.symbol is IrClassifierSymbol) error("Classifiers can not have callableId. Got $this")
return when (val parent = this.parent) {
is IrClass -> parent.classId?.let { CallableId(it, name) }
is IrPackageFragment -> CallableId(parent.packageFqName, name)
else -> null
} ?: error("$this has no callableId")
}
@Suppress("unused")
@Deprecated(
"This function is deprecated because it has confusing name and behavior. " +
@@ -802,7 +802,7 @@ open class DeepCopyIrTreeWithSymbols(
expression.value.transform()
).copyAttributes(expression)
private fun SymbolRemapper.getReferencedReturnTarget(returnTarget: IrReturnTargetSymbol) =
private fun SymbolRemapper.getReferencedReturnTarget(returnTarget: IrReturnTargetSymbol): IrReturnTargetSymbol =
when (returnTarget) {
is IrFunctionSymbol -> getReferencedFunction(returnTarget)
is IrReturnableBlockSymbol -> getReferencedReturnableBlock(returnTarget)