@@ -957,7 +957,7 @@ open class KotlinFileExtractor(
957
957
val locId = getLocation(f, null )
958
958
val extReceiver = f.extensionReceiverParameter
959
959
val dispatchReceiver = if (f.shouldExtractAsStatic) null else f.dispatchReceiverParameter
960
- val parameterTypes = listOfNotNull(extReceiver?. let { erase(it.type) }) + getDefaultsMethodArgTypes(f)
960
+ val parameterTypes = getDefaultsMethodArgTypes(f)
961
961
val allParamTypeResults = parameterTypes.mapIndexed { i, paramType ->
962
962
val paramId = tw.getLabelFor<DbParam >(getValueParameterLabel(id, i))
963
963
extractValueParameter(paramId, paramType, " p$i " , locId, id, i, paramId, isVararg = false , syntheticParameterNames = true , isCrossinline = false , isNoinline = false ).also {
@@ -976,7 +976,8 @@ open class KotlinFileExtractor(
976
976
extractMethod(methodId, locId, shortName, erase(f.returnType), paramsSignature, parentId, methodId, origin = null , extractTypeAccess = extractMethodAndParameterTypeAccesses)
977
977
addModifiers(id, " static" )
978
978
if (extReceiver != null ) {
979
- val extendedType = allParamTypeResults[0 ] // TODO: this is not correct for member extension methods, where the dispatch receiver is the first parameter
979
+ val idx = if (dispatchReceiver != null ) 1 else 0
980
+ val extendedType = allParamTypeResults[idx]
980
981
tw.writeKtExtensionFunctions(methodId, extendedType.javaResult.id, extendedType.kotlinResult.id)
981
982
}
982
983
}
@@ -1044,8 +1045,8 @@ open class KotlinFileExtractor(
1044
1045
val realFnIdxOffset = if (f.extensionReceiverParameter != null ) 1 else 0
1045
1046
val paramMappings = f.valueParameters.mapIndexed { idx, param -> Triple (param.type, idx + paramIdxOffset, idx + realFnIdxOffset) } +
1046
1047
listOfNotNull(
1047
- dispatchReceiver?.let { Triple (it.type, realFnIdxOffset , - 1 ) },
1048
- extReceiver?.let { Triple (it.type, 0 , 0 ) }
1048
+ dispatchReceiver?.let { Triple (it.type, 0 , - 1 ) },
1049
+ extReceiver?.let { Triple (it.type, if (dispatchReceiver != null ) 1 else 0 , 0 ) }
1049
1050
)
1050
1051
paramMappings.forEach { (type, fromIdx, toIdx) ->
1051
1052
extractVariableAccess(tw.getLabelFor<DbParam >(getValueParameterLabel(id, fromIdx)), type, locId, thisCallId, toIdx, id, returnId)
@@ -1188,6 +1189,7 @@ open class KotlinFileExtractor(
1188
1189
id
1189
1190
1190
1191
val extReceiver = f.extensionReceiverParameter
1192
+ // The following parameter order is correct, because member $default methods (where the order would be [dispatchParam], [extensionParam], normalParams) are not extracted here
1191
1193
val fParameters = listOfNotNull(extReceiver) + (overriddenAttributes?.valueParameters ? : f.valueParameters)
1192
1194
val paramTypes = fParameters.mapIndexed { i, vp ->
1193
1195
extractValueParameter(vp, id, i, typeSubstitution, sourceDeclaration, classTypeArgsIncludingOuterClasses, extractTypeAccess = extractMethodAndParameterTypeAccesses, overriddenAttributes?.sourceLoc)
@@ -1797,11 +1799,12 @@ open class KotlinFileExtractor(
1797
1799
) ? : pluginContext.irBuiltIns.anyType
1798
1800
1799
1801
private fun getDefaultsMethodArgTypes (f : IrFunction ) =
1800
- // The $default method has type ([extensionReceiver ], [dispatchReceiver ], paramTypes..., int, Object)
1802
+ // The $default method has type ([dispatchReceiver ], [extensionReceiver ], paramTypes..., int, Object)
1801
1803
// All parameter types are erased. The trailing int is a mask indicating which parameter values are real
1802
1804
// and which should be replaced by defaults. The final Object parameter is apparently always null.
1803
1805
(
1804
1806
listOfNotNull(if (f.shouldExtractAsStatic) null else f.dispatchReceiverParameter?.type) +
1807
+ listOfNotNull(f.extensionReceiverParameter?.type) +
1805
1808
f.valueParameters.map { it.type } +
1806
1809
listOf (pluginContext.irBuiltIns.intType, getDefaultsMethodLastArgType(f))
1807
1810
).map { erase(it) }
@@ -1820,17 +1823,16 @@ open class KotlinFileExtractor(
1820
1823
1821
1824
private fun getDefaultsMethodLabel (f : IrFunction ): Label <out DbCallable > {
1822
1825
val defaultsMethodName = if (f is IrConstructor ) " <init>" else getDefaultsMethodName(f)
1823
- val normalArgTypes = getDefaultsMethodArgTypes(f)
1824
- val extensionParamType = f.extensionReceiverParameter?.let { erase(it.type) }
1826
+ val argTypes = getDefaultsMethodArgTypes(f)
1825
1827
1826
1828
val defaultMethodLabelStr = getFunctionLabel(
1827
1829
f.parent,
1828
1830
maybeParentId = null ,
1829
1831
defaultsMethodName,
1830
- normalArgTypes ,
1832
+ argTypes ,
1831
1833
erase(f.returnType),
1832
- extensionParamType,
1833
- listOf (),
1834
+ extensionParamType = null , // if there's any, that's included already in argTypes
1835
+ functionTypeParameters = listOf (),
1834
1836
classTypeArgsIncludingOuterClasses = null ,
1835
1837
overridesCollectionsMethod = false ,
1836
1838
javaSignature = null ,
@@ -1890,13 +1892,14 @@ open class KotlinFileExtractor(
1890
1892
extensionReceiver : IrExpression ?
1891
1893
) {
1892
1894
var nextIdx = 0
1893
- if (extensionReceiver != null ) {
1894
- extractExpressionExpr(extensionReceiver, enclosingCallable, id, nextIdx++ , enclosingStmt)
1895
- }
1896
1895
if (dispatchReceiver != null && ! callTarget.shouldExtractAsStatic) {
1897
1896
extractExpressionExpr(dispatchReceiver, enclosingCallable, id, nextIdx++ , enclosingStmt)
1898
1897
}
1899
1898
1899
+ if (extensionReceiver != null ) {
1900
+ extractExpressionExpr(extensionReceiver, enclosingCallable, id, nextIdx++ , enclosingStmt)
1901
+ }
1902
+
1900
1903
val valueArgsWithDummies = valueArguments.zip(callTarget.valueParameters).map {
1901
1904
(expr, param) -> expr ? : IrConstImpl .defaultValueForType(0 , 0 , param.type)
1902
1905
}
@@ -4050,8 +4053,7 @@ open class KotlinFileExtractor(
4050
4053
// Use of 'this' in a function where the dispatch receiver is passed like an ordinary parameter,
4051
4054
// such as a `$default` static function that substitutes in default arguments as needed.
4052
4055
val paramDeclarerId = overriddenAttributes.id ? : useDeclarationParent(thisParamParent, false )
4053
- val extensionParamOffset = if (thisParamParent.extensionReceiverParameter != null ) 1 else 0
4054
- val replacementParamId = tw.getLabelFor<DbParam >(getValueParameterLabel(paramDeclarerId, replaceWithParamIdx + extensionParamOffset))
4056
+ val replacementParamId = tw.getLabelFor<DbParam >(getValueParameterLabel(paramDeclarerId, replaceWithParamIdx))
4055
4057
extractVariableAccess(replacementParamId, e.type, locId, exprParent.parent, exprParent.idx, callable, exprParent.enclosingStmt)
4056
4058
return
4057
4059
}
0 commit comments