@@ -173,19 +173,7 @@ open class KotlinFileExtractor(
173173
174174 fun extractTypeParameter (tp : IrTypeParameter , apparentIndex : Int ): Label <out DbTypevariable >? {
175175 with (" type parameter" , tp) {
176- val parentId: Label <out DbClassorinterfaceorcallable >? = when (val parent = tp.parent) {
177- is IrFunction -> useFunction(parent)
178- is IrClass -> useClassSource(parent)
179- else -> {
180- logger.errorElement(" Unexpected type parameter parent" , tp)
181- null
182- }
183- }
184-
185- if (parentId == null ) {
186- return null
187- }
188-
176+ val parentId = getTypeParameterParentLabel(tp) ? : return null
189177 val id = tw.getLabelFor<DbTypevariable >(getTypeParameterLabel(tp))
190178
191179 // Note apparentIndex does not necessarily equal `tp.index`, because at least constructor type parameters
@@ -334,17 +322,7 @@ open class KotlinFileExtractor(
334322 val typeParamSubstitution =
335323 when (argsIncludingOuterClasses) {
336324 null -> { x: IrType , _: TypeContext , _: IrPluginContext -> x.toRawType() }
337- else -> {
338- makeTypeGenericSubstitutionMap(c, argsIncludingOuterClasses).let {
339- { x: IrType , useContext: TypeContext , pluginContext: IrPluginContext ->
340- x.substituteTypeAndArguments(
341- it,
342- useContext,
343- pluginContext
344- )
345- }
346- }
347- }
325+ else -> makeGenericSubstitutionFunction(c, argsIncludingOuterClasses)
348326 }
349327
350328 c.declarations.map {
@@ -511,12 +489,12 @@ open class KotlinFileExtractor(
511489 return FieldResult (instanceId, instanceName)
512490 }
513491
514- private fun extractValueParameter (vp : IrValueParameter , parent : Label <out DbCallable >, idx : Int , typeSubstitution : TypeSubstitution ? , parentSourceDeclaration : Label <out DbCallable >, classTypeArgsIncludingOuterClasses : List <IrTypeArgument >? , extractTypeAccess : Boolean ): TypeResults {
492+ private fun extractValueParameter (vp : IrValueParameter , parent : Label <out DbCallable >, idx : Int , typeSubstitution : TypeSubstitution ? , parentSourceDeclaration : Label <out DbCallable >, classTypeArgsIncludingOuterClasses : List <IrTypeArgument >? , extractTypeAccess : Boolean , locOverride : Label < DbLocation > ? = null ): TypeResults {
515493 with (" value parameter" , vp) {
516- val location = getLocation(vp, classTypeArgsIncludingOuterClasses)
494+ val location = locOverride ? : getLocation(vp, classTypeArgsIncludingOuterClasses)
517495 val id = useValueParameter(vp, parent)
518496 if (extractTypeAccess) {
519- extractTypeAccessRecursive(vp.type, location, id, - 1 )
497+ extractTypeAccessRecursive(typeSubstitution?. let { it(vp.type, TypeContext . OTHER , pluginContext) } ? : vp.type, location, id, - 1 )
520498 }
521499 return extractValueParameter(id, vp.type, vp.name.asString(), location, parent, idx, typeSubstitution, useValueParameter(vp, parentSourceDeclaration), vp.isVararg)
522500 }
@@ -676,7 +654,7 @@ open class KotlinFileExtractor(
676654 }
677655 }
678656
679- fun extractFunction (f : IrFunction , parentId : Label <out DbReftype >, extractBody : Boolean , extractMethodAndParameterTypeAccesses : Boolean , typeSubstitution : TypeSubstitution ? , classTypeArgsIncludingOuterClasses : List <IrTypeArgument >? , idOverride : Label <DbMethod >? = null): Label <out DbCallable >? {
657+ fun extractFunction (f : IrFunction , parentId : Label <out DbReftype >, extractBody : Boolean , extractMethodAndParameterTypeAccesses : Boolean , typeSubstitution : TypeSubstitution ? , classTypeArgsIncludingOuterClasses : List <IrTypeArgument >? , idOverride : Label <DbMethod >? = null, locOverride : Label < DbLocation > ? = null ): Label <out DbCallable >? {
680658 if (isFake(f)) return null
681659
682660 with (" function" , f) {
@@ -694,21 +672,21 @@ open class KotlinFileExtractor(
694672 useFunction<DbCallable >(f, parentId, classTypeArgsIncludingOuterClasses, noReplace = true )
695673
696674 val sourceDeclaration =
697- if (typeSubstitution != null )
675+ if (typeSubstitution != null && idOverride == null )
698676 useFunction(f)
699677 else
700678 id
701679
702680 val extReceiver = f.extensionReceiverParameter
703681 val idxOffset = if (extReceiver != null ) 1 else 0
704682 val paramTypes = f.valueParameters.mapIndexed { i, vp ->
705- extractValueParameter(vp, id, i + idxOffset, typeSubstitution, sourceDeclaration, classTypeArgsIncludingOuterClasses, extractTypeAccess = extractMethodAndParameterTypeAccesses)
683+ extractValueParameter(vp, id, i + idxOffset, typeSubstitution, sourceDeclaration, classTypeArgsIncludingOuterClasses, extractTypeAccess = extractMethodAndParameterTypeAccesses, locOverride )
706684 }
707685 val allParamTypes = if (extReceiver != null ) {
708686 val extendedType = useType(extReceiver.type)
709687 tw.writeKtExtensionFunctions(id.cast<DbMethod >(), extendedType.javaResult.id, extendedType.kotlinResult.id)
710688
711- val t = extractValueParameter(extReceiver, id, 0 , null , sourceDeclaration, classTypeArgsIncludingOuterClasses, extractTypeAccess = extractMethodAndParameterTypeAccesses)
689+ val t = extractValueParameter(extReceiver, id, 0 , null , sourceDeclaration, classTypeArgsIncludingOuterClasses, extractTypeAccess = extractMethodAndParameterTypeAccesses, locOverride )
712690 listOf (t) + paramTypes
713691 } else {
714692 paramTypes
@@ -718,7 +696,7 @@ open class KotlinFileExtractor(
718696
719697 val substReturnType = typeSubstitution?.let { it(f.returnType, TypeContext .RETURN , pluginContext) } ? : f.returnType
720698
721- val locId = getLocation(f, classTypeArgsIncludingOuterClasses)
699+ val locId = locOverride ? : getLocation(f, classTypeArgsIncludingOuterClasses)
722700
723701 if (f.symbol is IrConstructorSymbol ) {
724702 val unitType = useType(pluginContext.irBuiltIns.unitType, TypeContext .RETURN )
@@ -738,7 +716,7 @@ open class KotlinFileExtractor(
738716 tw.writeMethodsKotlinType(methodId, returnType.kotlinResult.id)
739717
740718 if (extractMethodAndParameterTypeAccesses) {
741- extractTypeAccessRecursive(f.returnType , locId, id, - 1 )
719+ extractTypeAccessRecursive(substReturnType , locId, id, - 1 )
742720 }
743721
744722 if (shortName.nameInDB != shortName.kotlinName) {
@@ -4014,7 +3992,12 @@ open class KotlinFileExtractor(
40143992 helper.extractParameterToFieldAssignmentInConstructor(" <fn>" , functionType, fieldId, 0 , 1 )
40153993
40163994 // add implementation function
4017- extractFunction(samMember, classId, extractBody = false , extractMethodAndParameterTypeAccesses = true , null , null , ids.function)
3995+ val classTypeArgs = (e.type as ? IrSimpleType )?.arguments
3996+ val typeSub = classTypeArgs?.let { makeGenericSubstitutionFunction(typeOwner, it) }
3997+
3998+ fun trySub (t : IrType , context : TypeContext ) = if (typeSub == null ) t else typeSub(t, context, pluginContext)
3999+
4000+ extractFunction(samMember, classId, extractBody = false , extractMethodAndParameterTypeAccesses = true , typeSub, classTypeArgs, idOverride = ids.function, locOverride = tw.getLocation(e))
40184001
40194002 // body
40204003 val blockId = tw.getFreshIdLabel<DbBlock >()
@@ -4037,7 +4020,7 @@ open class KotlinFileExtractor(
40374020
40384021 // Call to original `invoke`:
40394022 val callId = tw.getFreshIdLabel<DbMethodaccess >()
4040- val callType = useType(samMember.returnType)
4023+ val callType = useType(trySub( samMember.returnType, TypeContext . RETURN ) )
40414024 tw.writeExprs_methodaccess(callId, callType.javaResult.id, returnId, 0 )
40424025 tw.writeExprsKotlinType(callId, callType.kotlinResult.id)
40434026 extractCommonExpr(callId)
@@ -4061,7 +4044,7 @@ open class KotlinFileExtractor(
40614044
40624045 fun extractArgument (p : IrValueParameter , idx : Int , parent : Label <out DbExprparent >) {
40634046 val argsAccessId = tw.getFreshIdLabel<DbVaraccess >()
4064- val paramType = useType(p.type)
4047+ val paramType = useType(trySub( p.type, TypeContext . OTHER ) )
40654048 tw.writeExprs_varaccess(argsAccessId, paramType.javaResult.id, parent, idx)
40664049 tw.writeExprsKotlinType(argsAccessId, paramType.kotlinResult.id)
40674050 extractCommonExpr(argsAccessId)
0 commit comments