@@ -658,6 +658,26 @@ open class KotlinUsesExtractor(
658
658
RETURN , GENERIC_ARGUMENT , OTHER
659
659
}
660
660
661
+ private fun isOnDeclarationStackWithoutTypeParameters (f : IrFunction ) =
662
+ this is KotlinFileExtractor && this .declarationStack.findOverriddenAttributes(f)?.typeParameters?.isEmpty() == true
663
+
664
+ private fun isStaticFunctionOnStackBeforeClass (c : IrClass ) =
665
+ this is KotlinFileExtractor && (this .declarationStack.findFirst { it.first == c || it.second?.isStatic == true })?.second?.isStatic == true
666
+
667
+ private fun isUnavailableTypeParameter (t : IrType ) =
668
+ t is IrSimpleType && t.classifier.owner.let { owner ->
669
+ owner is IrTypeParameter && owner.parent.let { parent ->
670
+ when (parent) {
671
+ is IrFunction -> isOnDeclarationStackWithoutTypeParameters(parent)
672
+ is IrClass -> isStaticFunctionOnStackBeforeClass(parent)
673
+ else -> false
674
+ }
675
+ }
676
+ }
677
+
678
+ private fun argIsUnavailableTypeParameter (t : IrTypeArgument ) =
679
+ t is IrTypeProjection && isUnavailableTypeParameter(t.type)
680
+
661
681
private fun useSimpleType (s : IrSimpleType , context : TypeContext ): TypeResults {
662
682
if (s.abbreviation != null ) {
663
683
// TODO: Extract this information
@@ -729,11 +749,13 @@ open class KotlinUsesExtractor(
729
749
}
730
750
731
751
owner is IrClass -> {
732
- val args = if (s.isRawType()) null else s.arguments
752
+ val args = if (s.isRawType() || s.arguments.any { argIsUnavailableTypeParameter(it) } ) null else s.arguments
733
753
734
754
return useSimpleTypeClass(owner, args, s.isNullable())
735
755
}
736
756
owner is IrTypeParameter -> {
757
+ if (isUnavailableTypeParameter(s))
758
+ return useType(erase(s), context)
737
759
val javaResult = useTypeParameter(owner)
738
760
val aClassId = makeClass(" kotlin" , " TypeParam" ) // TODO: Wrong
739
761
val kotlinResult = if (true ) TypeResult (fakeKotlinType(), " TODO" , " TODO" ) else
@@ -1043,9 +1065,9 @@ open class KotlinUsesExtractor(
1043
1065
f.parent,
1044
1066
maybeParentId,
1045
1067
getFunctionShortName(f).nameInDB,
1046
- maybeParameterList ? : f.valueParameters,
1068
+ ( maybeParameterList ? : f.valueParameters).map { it.type } ,
1047
1069
getAdjustedReturnType(f),
1048
- f.extensionReceiverParameter,
1070
+ f.extensionReceiverParameter?.type ,
1049
1071
getFunctionTypeParameters(f),
1050
1072
classTypeArgsIncludingOuterClasses,
1051
1073
overridesCollectionsMethodWithAlteredParameterTypes(f),
@@ -1067,12 +1089,12 @@ open class KotlinUsesExtractor(
1067
1089
maybeParentId : Label <out DbElement >? ,
1068
1090
// The name of the function; normally f.name.asString().
1069
1091
name : String ,
1070
- // The value parameters that the functions takes; normally f.valueParameters.
1071
- parameters : List <IrValueParameter >,
1092
+ // The types of the value parameters that the functions takes; normally f.valueParameters.map { it.type } .
1093
+ parameterTypes : List <IrType >,
1072
1094
// The return type of the function; normally f.returnType.
1073
1095
returnType : IrType ,
1074
- // The extension receiver of the function, if any; normally f.extensionReceiverParameter.
1075
- extensionReceiverParameter : IrValueParameter ? ,
1096
+ // The extension receiver of the function, if any; normally f.extensionReceiverParameter?.type .
1097
+ extensionParamType : IrType ? ,
1076
1098
// The type parameters of the function. This does not include type parameters of enclosing classes.
1077
1099
functionTypeParameters : List <IrTypeParameter >,
1078
1100
// The type arguments of enclosing classes of the function.
@@ -1089,11 +1111,7 @@ open class KotlinUsesExtractor(
1089
1111
prefix : String = "callable"
1090
1112
): String {
1091
1113
val parentId = maybeParentId ? : useDeclarationParent(parent, false , classTypeArgsIncludingOuterClasses, true )
1092
- val allParams = if (extensionReceiverParameter == null ) {
1093
- parameters
1094
- } else {
1095
- listOf (extensionReceiverParameter) + parameters
1096
- }
1114
+ val allParamTypes = if (extensionParamType == null ) parameterTypes else listOf (extensionParamType) + parameterTypes
1097
1115
1098
1116
val substitutionMap = classTypeArgsIncludingOuterClasses?.let { notNullArgs ->
1099
1117
if (notNullArgs.isEmpty()) {
@@ -1103,11 +1121,11 @@ open class KotlinUsesExtractor(
1103
1121
enclosingClass?.let { notNullClass -> makeTypeGenericSubstitutionMap(notNullClass, notNullArgs) }
1104
1122
}
1105
1123
}
1106
- val getIdForFunctionLabel = { it: IndexedValue <IrValueParameter > ->
1124
+ val getIdForFunctionLabel = { it: IndexedValue <IrType > ->
1107
1125
// Kotlin rewrites certain Java collections types adding additional generic constraints-- for example,
1108
1126
// Collection.remove(Object) because Collection.remove(Collection::E) in the Kotlin universe.
1109
1127
// If this has happened, erase the type again to get the correct Java signature.
1110
- val maybeAmendedForCollections = if (overridesCollectionsMethod) eraseCollectionsMethodParameterType(it.value.type , name, it.index) else it.value.type
1128
+ val maybeAmendedForCollections = if (overridesCollectionsMethod) eraseCollectionsMethodParameterType(it.value, name, it.index) else it.value
1111
1129
// Add any wildcard types that the Kotlin compiler would add in the Java lowering of this function:
1112
1130
val withAddedWildcards = addJavaLoweringWildcards(maybeAmendedForCollections, addParameterWildcardsByDefault, javaSignature?.let { sig -> getJavaValueParameterType(sig, it.index) })
1113
1131
// Now substitute any class type parameters in:
@@ -1117,7 +1135,7 @@ open class KotlinUsesExtractor(
1117
1135
val maybeErased = if (functionTypeParameters.isEmpty()) maybeSubbed else erase(maybeSubbed)
1118
1136
" {${useType(maybeErased).javaResult.id} }"
1119
1137
}
1120
- val paramTypeIds = allParams .withIndex().joinToString(separator = " ," , transform = getIdForFunctionLabel)
1138
+ val paramTypeIds = allParamTypes .withIndex().joinToString(separator = " ," , transform = getIdForFunctionLabel)
1121
1139
val labelReturnType =
1122
1140
if (name == " <init>" )
1123
1141
pluginContext.irBuiltIns.unitType
@@ -1551,7 +1569,7 @@ open class KotlinUsesExtractor(
1551
1569
* Note that `Array<T>` is retained (with `T` itself erased) because these are expected to be lowered to Java
1552
1570
* arrays, which are not generic.
1553
1571
*/
1554
- private fun erase (t : IrType ): IrType {
1572
+ fun erase (t : IrType ): IrType {
1555
1573
if (t is IrSimpleType ) {
1556
1574
val classifier = t.classifier
1557
1575
val owner = classifier.owner
@@ -1578,6 +1596,8 @@ open class KotlinUsesExtractor(
1578
1596
private fun eraseTypeParameter (t : IrTypeParameter ) =
1579
1597
erase(t.superTypes[0 ])
1580
1598
1599
+ fun getValueParameterLabel (parentId : Label <out DbElement >? , idx : Int ) = " @\" params;{$parentId };$idx \" "
1600
+
1581
1601
/* *
1582
1602
* Gets the label for `vp` in the context of function instance `parent`, or in that of its declaring function if
1583
1603
* `parent` is null.
@@ -1607,7 +1627,7 @@ open class KotlinUsesExtractor(
1607
1627
logger.error(" Unexpected negative index for parameter" )
1608
1628
}
1609
1629
1610
- return " @ \" params;{ $ parentId}; $ idx\" "
1630
+ return getValueParameterLabel( parentId, idx)
1611
1631
}
1612
1632
1613
1633
@@ -1669,7 +1689,7 @@ open class KotlinUsesExtractor(
1669
1689
val returnType = getter?.returnType ? : setter?.valueParameters?.singleOrNull()?.type ? : pluginContext.irBuiltIns.unitType
1670
1690
val typeParams = getFunctionTypeParameters(func)
1671
1691
1672
- getFunctionLabel(p.parent, parentId, p.name.asString(), listOf (), returnType, ext, typeParams, classTypeArgsIncludingOuterClasses, overridesCollectionsMethod = false , javaSignature = null , addParameterWildcardsByDefault = false , prefix = " property" )
1692
+ getFunctionLabel(p.parent, parentId, p.name.asString(), listOf (), returnType, ext.type , typeParams, classTypeArgsIncludingOuterClasses, overridesCollectionsMethod = false , javaSignature = null , addParameterWildcardsByDefault = false , prefix = " property" )
1673
1693
}
1674
1694
}
1675
1695
0 commit comments