Skip to content

Commit a3a93d8

Browse files
committed
KE2: Extract String.plus and String?.plus calls
1 parent bc15f40 commit a3a93d8

File tree

2 files changed

+61
-37
lines changed

2 files changed

+61
-37
lines changed

java/kotlin-extractor2/src/main/kotlin/KotlinFileExtractor.kt

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3695,29 +3695,6 @@ OLD: KE1
36953695
36963696
val dr = c.dispatchReceiver
36973697
when {
3698-
isFunction(target, "kotlin", "String", "plus", false) -> {
3699-
val id = tw.getFreshIdLabel<DbAddexpr>()
3700-
val type = useType(c.type)
3701-
tw.writeExprs_addexpr(id, type.javaResult.id, parent, idx)
3702-
tw.writeExprsKotlinType(id, type.kotlinResult.id)
3703-
binopDisp(id)
3704-
}
3705-
isFunction(target, "kotlin", "String", "plus", true) -> {
3706-
findJdkIntrinsicOrWarn("stringPlus", c)?.let { stringPlusFn ->
3707-
extractRawMethodAccess(
3708-
stringPlusFn,
3709-
c,
3710-
c.type,
3711-
callable,
3712-
parent,
3713-
idx,
3714-
enclosingStmt,
3715-
listOf(c.extensionReceiver, c.getValueArgument(0)),
3716-
null,
3717-
null
3718-
)
3719-
}
3720-
}
37213698
isNumericFunction(
37223699
target,
37233700
"plus",

java/kotlin-extractor2/src/main/kotlin/entities/Expression.kt

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ import org.jetbrains.kotlin.analysis.api.resolution.KaSuccessCallInfo
88
import org.jetbrains.kotlin.analysis.api.resolution.symbol
99
import org.jetbrains.kotlin.analysis.api.symbols.KaFunctionSymbol
1010
import org.jetbrains.kotlin.analysis.api.types.KaType
11+
import org.jetbrains.kotlin.analysis.api.types.KaTypeNullability
12+
import org.jetbrains.kotlin.analysis.api.types.symbol
1113
import org.jetbrains.kotlin.lexer.KtTokens
14+
import org.jetbrains.kotlin.name.CallableId
15+
import org.jetbrains.kotlin.name.ClassId
16+
import org.jetbrains.kotlin.name.FqName
17+
import org.jetbrains.kotlin.name.Name
1218
import org.jetbrains.kotlin.parsing.parseNumericLiteral
1319
import org.jetbrains.kotlin.psi.*
1420

@@ -215,25 +221,57 @@ OLD: KE1
215221
}
216222
*/
217223

218-
private fun isFunction(
224+
private fun isFunctionMatchingNames(
225+
symbol: KaFunctionSymbol,
226+
name: CallableId,
227+
isExtension: Boolean = false,
228+
nullability: KaTypeNullability = KaTypeNullability.UNKNOWN,
229+
extensionReceiverClassName: ClassId? = null
230+
): Boolean {
231+
232+
if (symbol.callableId != name)
233+
return false
234+
235+
if (!isExtension)
236+
return true
237+
238+
val receiverType = symbol.receiverParameter?.type
239+
if (receiverType == null)
240+
return false
241+
242+
if (receiverType.nullability != nullability)
243+
return false
244+
245+
if (receiverType.symbol?.classId != extensionReceiverClassName)
246+
return false
247+
248+
return true
249+
}
250+
251+
private fun isFunctionMatchingName(
219252
symbol: KaFunctionSymbol,
220253
packageName: String,
221-
className: String,
254+
className: String?,
222255
functionName: String
223256
): Boolean {
224257

225-
return symbol.callableId?.packageName?.asString() == packageName &&
226-
symbol.callableId?.className?.asString() == className &&
227-
symbol.callableId?.callableName?.asString() == functionName
258+
return isFunctionMatchingNames(
259+
symbol,
260+
CallableId(
261+
FqName(packageName),
262+
if (className == null) null else FqName(className),
263+
Name.identifier(functionName)
264+
)
265+
)
228266
}
229267

230-
private fun isNumericFunction(target: KaFunctionSymbol, fName: String): Boolean {
231-
return isFunction(target, "kotlin", "Int", fName) ||
232-
isFunction(target, "kotlin", "Byte", fName) ||
233-
isFunction(target, "kotlin", "Short", fName) ||
234-
isFunction(target, "kotlin", "Long", fName) ||
235-
isFunction(target, "kotlin", "Float", fName) ||
236-
isFunction(target, "kotlin", "Double", fName)
268+
private fun isNumericFunction(target: KaFunctionSymbol, functionName: String): Boolean {
269+
return isFunctionMatchingName(target, "kotlin", "Int", functionName) ||
270+
isFunctionMatchingName(target, "kotlin", "Byte", functionName) ||
271+
isFunctionMatchingName(target, "kotlin", "Short", functionName) ||
272+
isFunctionMatchingName(target, "kotlin", "Long", functionName) ||
273+
isFunctionMatchingName(target, "kotlin", "Float", functionName) ||
274+
isFunctionMatchingName(target, "kotlin", "Double", functionName)
237275
}
238276

239277
/**
@@ -267,7 +305,16 @@ private fun KotlinFileExtractor.extractBinaryExpression(
267305
TODO()
268306
}
269307

270-
if (isNumericFunction(target, "plus")) {
308+
if (isNumericFunction(target, "plus") ||
309+
isFunctionMatchingName(target, "kotlin", "String", "plus") ||
310+
isFunctionMatchingNames(
311+
target,
312+
CallableId(FqName("kotlin"), null, Name.identifier("plus")),
313+
isExtension = true,
314+
nullability = KaTypeNullability.NULLABLE,
315+
ClassId(FqName("kotlin"), Name.identifier("String"))
316+
)
317+
) {
271318
val id = tw.getFreshIdLabel<DbAddexpr>()
272319
val type = useType(expression.expressionType)
273320
val exprParent = parent.expr(expression, callable)
@@ -278,7 +325,7 @@ private fun KotlinFileExtractor.extractBinaryExpression(
278325
extractExpressionExpr(expression.left!!, callable, id, 0, exprParent.enclosingStmt)
279326
extractExpressionExpr(expression.right!!, callable, id, 1, exprParent.enclosingStmt)
280327
} else {
281-
TODO()
328+
TODO("Extract as method call")
282329
}
283330
}
284331

0 commit comments

Comments
 (0)