@@ -658,6 +658,26 @@ open class KotlinUsesExtractor(
658658 RETURN , GENERIC_ARGUMENT , OTHER
659659 }
660660
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+
661681 private fun useSimpleType (s : IrSimpleType , context : TypeContext ): TypeResults {
662682 if (s.abbreviation != null ) {
663683 // TODO: Extract this information
@@ -729,11 +749,13 @@ open class KotlinUsesExtractor(
729749 }
730750
731751 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
733753
734754 return useSimpleTypeClass(owner, args, s.isNullable())
735755 }
736756 owner is IrTypeParameter -> {
757+ if (isUnavailableTypeParameter(s))
758+ return useType(erase(s), context)
737759 val javaResult = useTypeParameter(owner)
738760 val aClassId = makeClass(" kotlin" , " TypeParam" ) // TODO: Wrong
739761 val kotlinResult = if (true ) TypeResult (fakeKotlinType(), " TODO" , " TODO" ) else
@@ -1043,9 +1065,9 @@ open class KotlinUsesExtractor(
10431065 f.parent,
10441066 maybeParentId,
10451067 getFunctionShortName(f).nameInDB,
1046- maybeParameterList ? : f.valueParameters,
1068+ ( maybeParameterList ? : f.valueParameters).map { it.type } ,
10471069 getAdjustedReturnType(f),
1048- f.extensionReceiverParameter,
1070+ f.extensionReceiverParameter?.type ,
10491071 getFunctionTypeParameters(f),
10501072 classTypeArgsIncludingOuterClasses,
10511073 overridesCollectionsMethodWithAlteredParameterTypes(f),
@@ -1067,12 +1089,12 @@ open class KotlinUsesExtractor(
10671089 maybeParentId : Label <out DbElement >? ,
10681090 // The name of the function; normally f.name.asString().
10691091 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 >,
10721094 // The return type of the function; normally f.returnType.
10731095 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 ? ,
10761098 // The type parameters of the function. This does not include type parameters of enclosing classes.
10771099 functionTypeParameters : List <IrTypeParameter >,
10781100 // The type arguments of enclosing classes of the function.
@@ -1089,11 +1111,7 @@ open class KotlinUsesExtractor(
10891111 prefix : String = "callable"
10901112 ): String {
10911113 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
10971115
10981116 val substitutionMap = classTypeArgsIncludingOuterClasses?.let { notNullArgs ->
10991117 if (notNullArgs.isEmpty()) {
@@ -1103,11 +1121,11 @@ open class KotlinUsesExtractor(
11031121 enclosingClass?.let { notNullClass -> makeTypeGenericSubstitutionMap(notNullClass, notNullArgs) }
11041122 }
11051123 }
1106- val getIdForFunctionLabel = { it: IndexedValue <IrValueParameter > ->
1124+ val getIdForFunctionLabel = { it: IndexedValue <IrType > ->
11071125 // Kotlin rewrites certain Java collections types adding additional generic constraints-- for example,
11081126 // Collection.remove(Object) because Collection.remove(Collection::E) in the Kotlin universe.
11091127 // 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
11111129 // Add any wildcard types that the Kotlin compiler would add in the Java lowering of this function:
11121130 val withAddedWildcards = addJavaLoweringWildcards(maybeAmendedForCollections, addParameterWildcardsByDefault, javaSignature?.let { sig -> getJavaValueParameterType(sig, it.index) })
11131131 // Now substitute any class type parameters in:
@@ -1117,7 +1135,7 @@ open class KotlinUsesExtractor(
11171135 val maybeErased = if (functionTypeParameters.isEmpty()) maybeSubbed else erase(maybeSubbed)
11181136 " {${useType(maybeErased).javaResult.id} }"
11191137 }
1120- val paramTypeIds = allParams .withIndex().joinToString(separator = " ," , transform = getIdForFunctionLabel)
1138+ val paramTypeIds = allParamTypes .withIndex().joinToString(separator = " ," , transform = getIdForFunctionLabel)
11211139 val labelReturnType =
11221140 if (name == " <init>" )
11231141 pluginContext.irBuiltIns.unitType
@@ -1551,7 +1569,7 @@ open class KotlinUsesExtractor(
15511569 * Note that `Array<T>` is retained (with `T` itself erased) because these are expected to be lowered to Java
15521570 * arrays, which are not generic.
15531571 */
1554- private fun erase (t : IrType ): IrType {
1572+ fun erase (t : IrType ): IrType {
15551573 if (t is IrSimpleType ) {
15561574 val classifier = t.classifier
15571575 val owner = classifier.owner
@@ -1578,6 +1596,8 @@ open class KotlinUsesExtractor(
15781596 private fun eraseTypeParameter (t : IrTypeParameter ) =
15791597 erase(t.superTypes[0 ])
15801598
1599+ fun getValueParameterLabel (parentId : Label <out DbElement >? , idx : Int ) = " @\" params;{$parentId };$idx \" "
1600+
15811601 /* *
15821602 * Gets the label for `vp` in the context of function instance `parent`, or in that of its declaring function if
15831603 * `parent` is null.
@@ -1607,7 +1627,7 @@ open class KotlinUsesExtractor(
16071627 logger.error(" Unexpected negative index for parameter" )
16081628 }
16091629
1610- return " @ \" params;{ $ parentId}; $ idx\" "
1630+ return getValueParameterLabel( parentId, idx)
16111631 }
16121632
16131633
@@ -1669,7 +1689,7 @@ open class KotlinUsesExtractor(
16691689 val returnType = getter?.returnType ? : setter?.valueParameters?.singleOrNull()?.type ? : pluginContext.irBuiltIns.unitType
16701690 val typeParams = getFunctionTypeParameters(func)
16711691
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" )
16731693 }
16741694 }
16751695
0 commit comments