@@ -190,7 +190,7 @@ class FunctionCallTransformer(
190
190
val tokenFir = token.toClassSymbol(session)!! .fir
191
191
tokenFir.callShapeData = CallShapeData .RefinedType (dataSchemaApis.map { it.scope.symbol })
192
192
193
- return buildLetCall (call, originalSymbol, dataSchemaApis, listOf (tokenFir))
193
+ return buildScopeFunctionCall (call, originalSymbol, dataSchemaApis, listOf (tokenFir))
194
194
}
195
195
}
196
196
@@ -253,7 +253,7 @@ class FunctionCallTransformer(
253
253
val keyToken = groupMarker.toClassSymbol(session)!! .fir
254
254
keyToken.callShapeData = CallShapeData .RefinedType (groupApis.map { it.scope.symbol })
255
255
256
- return buildLetCall (call, originalSymbol, keyApis + groupApis, additionalDeclarations = listOf (groupToken, keyToken))
256
+ return buildScopeFunctionCall (call, originalSymbol, keyApis + groupApis, additionalDeclarations = listOf (groupToken, keyToken))
257
257
}
258
258
}
259
259
@@ -305,18 +305,17 @@ class FunctionCallTransformer(
305
305
private fun Name.asTokenName () = identifierOrNullIfSpecial?.titleCase() ? : DEFAULT_NAME
306
306
307
307
@OptIn(SymbolInternals ::class )
308
- private fun buildLetCall (
308
+ private fun buildScopeFunctionCall (
309
309
call : FirFunctionCall ,
310
310
originalSymbol : FirNamedFunctionSymbol ,
311
311
dataSchemaApis : List <DataSchemaApi >,
312
312
additionalDeclarations : List <FirClass >
313
313
): FirFunctionCall {
314
314
315
- val explicitReceiver = call.explicitReceiver ? : return call
316
- val receiverType = explicitReceiver.resolvedType
315
+ val explicitReceiver = call.explicitReceiver
316
+ val receiverType = explicitReceiver? .resolvedType
317
317
val returnType = call.resolvedType
318
- val resolvedLet = findLet()
319
- val parameter = resolvedLet.valueParameterSymbols[0 ]
318
+ val scopeFunction = if (explicitReceiver != null ) findLet() else findRun()
320
319
321
320
// original call is inserted later
322
321
call.transformCalleeReference(object : FirTransformer <Nothing ?>() {
@@ -350,20 +349,23 @@ class FunctionCallTransformer(
350
349
returnTypeRef = buildResolvedTypeRef {
351
350
type = returnType
352
351
}
353
- val itName = Name .identifier(" it" )
354
- val parameterSymbol = FirValueParameterSymbol (itName)
355
- valueParameters + = buildValueParameter {
356
- moduleData = session.moduleData
357
- origin = FirDeclarationOrigin .Source
358
- returnTypeRef = buildResolvedTypeRef {
359
- type = receiverType
352
+ val parameterSymbol = receiverType?.let {
353
+ val itName = Name .identifier(" it" )
354
+ val parameterSymbol = FirValueParameterSymbol (itName)
355
+ valueParameters + = buildValueParameter {
356
+ moduleData = session.moduleData
357
+ origin = FirDeclarationOrigin .Source
358
+ returnTypeRef = buildResolvedTypeRef {
359
+ type = receiverType
360
+ }
361
+ this .name = itName
362
+ this .symbol = parameterSymbol
363
+ containingFunctionSymbol = fSymbol
364
+ isCrossinline = false
365
+ isNoinline = false
366
+ isVararg = false
360
367
}
361
- this .name = itName
362
- this .symbol = parameterSymbol
363
- containingFunctionSymbol = fSymbol
364
- isCrossinline = false
365
- isNoinline = false
366
- isVararg = false
368
+ parameterSymbol
367
369
}
368
370
body = buildBlock {
369
371
this .coneTypeOrNull = returnType
@@ -375,20 +377,23 @@ class FunctionCallTransformer(
375
377
statements + = additionalDeclarations
376
378
377
379
statements + = buildReturnExpression {
378
- val itPropertyAccess = buildPropertyAccessExpression {
379
- coneTypeOrNull = receiverType
380
- calleeReference = buildResolvedNamedReference {
381
- name = itName
382
- resolvedSymbol = parameterSymbol
380
+ if (parameterSymbol != null ) {
381
+ val itPropertyAccess = buildPropertyAccessExpression {
382
+ coneTypeOrNull = receiverType
383
+ calleeReference = buildResolvedNamedReference {
384
+ name = parameterSymbol.name
385
+ resolvedSymbol = parameterSymbol
386
+ }
387
+ }
388
+ if (callDispatchReceiver != null ) {
389
+ call.replaceDispatchReceiver(itPropertyAccess)
390
+ }
391
+ call.replaceExplicitReceiver(itPropertyAccess)
392
+ if (callExtensionReceiver != null ) {
393
+ call.replaceExtensionReceiver(itPropertyAccess)
383
394
}
384
395
}
385
- if (callDispatchReceiver != null ) {
386
- call.replaceDispatchReceiver(itPropertyAccess)
387
- }
388
- call.replaceExplicitReceiver(itPropertyAccess)
389
- if (callExtensionReceiver != null ) {
390
- call.replaceExtensionReceiver(itPropertyAccess)
391
- }
396
+
392
397
result = call
393
398
this .target = target
394
399
}
@@ -397,11 +402,19 @@ class FunctionCallTransformer(
397
402
isLambda = true
398
403
hasExplicitParameterList = false
399
404
typeRef = buildResolvedTypeRef {
400
- type = ConeClassLikeTypeImpl (
401
- ConeClassLikeLookupTagImpl (ClassId (FqName (" kotlin" ), Name .identifier(" Function1" ))),
402
- typeArguments = arrayOf(receiverType, returnType),
403
- isNullable = false
404
- )
405
+ type = if (receiverType != null ) {
406
+ ConeClassLikeTypeImpl (
407
+ ConeClassLikeLookupTagImpl (ClassId (FqName (" kotlin" ), Name .identifier(" Function1" ))),
408
+ typeArguments = arrayOf(receiverType, returnType),
409
+ isNullable = false
410
+ )
411
+ } else {
412
+ ConeClassLikeTypeImpl (
413
+ ConeClassLikeLookupTagImpl (ClassId (FqName (" kotlin" ), Name .identifier(" Function0" ))),
414
+ typeArguments = arrayOf(returnType),
415
+ isNullable = false
416
+ )
417
+ }
405
418
}
406
419
invocationKind = EventOccurrencesRange .EXACTLY_ONCE
407
420
inlineStatus = InlineStatus .Inline
@@ -413,11 +426,13 @@ class FunctionCallTransformer(
413
426
val newCall1 = buildFunctionCall {
414
427
source = call.source
415
428
this .coneTypeOrNull = returnType
416
- typeArguments + = buildTypeProjectionWithVariance {
417
- typeRef = buildResolvedTypeRef {
418
- type = receiverType
429
+ if (receiverType != null ) {
430
+ typeArguments + = buildTypeProjectionWithVariance {
431
+ typeRef = buildResolvedTypeRef {
432
+ type = receiverType
433
+ }
434
+ variance = Variance .INVARIANT
419
435
}
420
- variance = Variance .INVARIANT
421
436
}
422
437
423
438
typeArguments + = buildTypeProjectionWithVariance {
@@ -429,11 +444,14 @@ class FunctionCallTransformer(
429
444
dispatchReceiver = null
430
445
this .explicitReceiver = callExplicitReceiver
431
446
extensionReceiver = callExtensionReceiver ? : callDispatchReceiver
432
- argumentList = buildResolvedArgumentList(original = null , linkedMapOf(argument to parameter.fir))
447
+ argumentList = buildResolvedArgumentList(
448
+ original = null ,
449
+ linkedMapOf(argument to scopeFunction.valueParameterSymbols[0 ].fir)
450
+ )
433
451
calleeReference = buildResolvedNamedReference {
434
452
source = call.calleeReference.source
435
- this .name = Name .identifier( " let " )
436
- resolvedSymbol = resolvedLet
453
+ this .name = scopeFunction.name
454
+ resolvedSymbol = scopeFunction
437
455
}
438
456
}
439
457
return newCall1
@@ -565,5 +583,9 @@ class FunctionCallTransformer(
565
583
return session.symbolProvider.getTopLevelFunctionSymbols(FqName (" kotlin" ), Name .identifier(" let" )).single()
566
584
}
567
585
586
+ private fun findRun (): FirFunctionSymbol <* > {
587
+ return session.symbolProvider.getTopLevelFunctionSymbols(FqName (" kotlin" ), Name .identifier(" run" )).single { it.typeParameterSymbols.size == 1 }
588
+ }
589
+
568
590
private fun String.titleCase () = replaceFirstChar { it.uppercaseChar() }
569
591
}
0 commit comments