summaryrefslogtreecommitdiff
path: root/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/codeInliner/CodeToInlineBuilder.kt
blob: 98a828a5821537003233f239425fb682e662833b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.

package org.jetbrains.kotlin.idea.codeInliner

import com.intellij.psi.util.elementType
import org.jetbrains.kotlin.builtins.isExtensionFunctionType
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
import org.jetbrains.kotlin.idea.codeInliner.CommentHolder.CommentNode.Companion.mergeComments
import org.jetbrains.kotlin.idea.core.asExpression
import org.jetbrains.kotlin.idea.core.copied
import org.jetbrains.kotlin.idea.core.replaced
import org.jetbrains.kotlin.idea.imports.importableFqName
import org.jetbrains.kotlin.idea.intentions.InsertExplicitTypeArgumentsIntention
import org.jetbrains.kotlin.idea.intentions.SpecifyExplicitLambdaSignatureIntention
import org.jetbrains.kotlin.idea.references.canBeResolvedViaImport
import org.jetbrains.kotlin.idea.resolve.ResolutionFacade
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers
import org.jetbrains.kotlin.idea.util.getResolutionScope
import org.jetbrains.kotlin.idea.util.isAnonymousFunction
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.*
import org.jetbrains.kotlin.renderer.render
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.ImportedFromObjectCallableDescriptor
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
import org.jetbrains.kotlin.resolve.calls.model.isReallySuccess
import org.jetbrains.kotlin.resolve.calls.util.getResolvedCall
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameOrNull
import org.jetbrains.kotlin.resolve.descriptorUtil.isExtension
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.resolve.scopes.receivers.ImplicitReceiver
import org.jetbrains.kotlin.resolve.scopes.utils.findClassifier
import org.jetbrains.kotlin.types.error.ErrorUtils
import org.jetbrains.kotlin.types.typeUtil.unCapture
import org.jetbrains.kotlin.utils.addToStdlib.cast
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
import org.jetbrains.kotlin.utils.sure

class CodeToInlineBuilder(
    private val targetCallable: CallableDescriptor,
    private val resolutionFacade: ResolutionFacade,
    private val originalDeclaration: KtDeclaration?,
    private val fallbackToSuperCall: Boolean = false,
) {
    private val psiFactory = KtPsiFactory(resolutionFacade.project)

    fun prepareCodeToInlineWithAdvancedResolution(
        bodyOrExpression: KtExpression,
        expressionMapper: (bodyOrExpression: KtExpression) -> Pair<KtExpression?, List<KtExpression>>?,
    ): CodeToInline? {
        val (mainExpression, statementsBefore) = expressionMapper(bodyOrExpression) ?: return null
        val codeToInline = prepareMutableCodeToInline(
            mainExpression = mainExpression,
            statementsBefore = statementsBefore,
            analyze = { it.analyze(BodyResolveMode.PARTIAL) },
            reformat = true,
        )

        val copyOfBodyOrExpression = bodyOrExpression.copied()

        // Body's expressions to be inlined contain related comments as a user data (see CommentHolder.CommentNode.Companion.mergeComments).
        // When inlining (with untouched declaration!) is reverted and called again expressions become polluted with duplicates (^ merge!).
        // Now that we copied required data it's time to clear the storage.
        codeToInline.expressions.forEach { it.putCopyableUserData(CommentHolder.COMMENTS_TO_RESTORE_KEY, null) }

        val (resultMainExpression, resultStatementsBefore) = expressionMapper(copyOfBodyOrExpression) ?: return null
        codeToInline.mainExpression = resultMainExpression
        codeToInline.statementsBefore.clear()
        codeToInline.statementsBefore.addAll(resultStatementsBefore)

        return codeToInline.toNonMutable()
    }

    private fun prepareMutableCodeToInline(
        mainExpression: KtExpression?,
        statementsBefore: List<KtExpression>,
        analyze: (KtExpression) -> BindingContext,
        reformat: Boolean,
    ): MutableCodeToInline {
        val alwaysKeepMainExpression =
            when (val descriptor = mainExpression?.getResolvedCall(analyze(mainExpression))?.resultingDescriptor) {
                is PropertyDescriptor -> descriptor.getter?.isDefault == false
                else -> false
            }

        val codeToInline = MutableCodeToInline(
            mainExpression,
            statementsBefore.toMutableList(),
            mutableSetOf(),
            alwaysKeepMainExpression,
            extraComments = null,
        )

        if (originalDeclaration != null) {
            saveComments(codeToInline, originalDeclaration)
        }

        insertExplicitTypeArguments(codeToInline, analyze)
        processReferences(codeToInline, analyze, reformat)
        removeContracts(codeToInline, analyze)

        when {
            mainExpression == null -> Unit
            mainExpression.isNull() -> targetCallable.returnType?.let { returnType ->
                codeToInline.addPreCommitAction(mainExpression) {
                    codeToInline.replaceExpression(
                        it,
                        psiFactory.createExpression(
                            "null as ${
                                IdeDescriptorRenderers.FQ_NAMES_IN_TYPES_WITH_NORMALIZER.renderType(returnType)
                            }"
                        ),
                    )
                }
            }

            else -> {
                val functionLiteralExpression = mainExpression.unpackFunctionLiteral(true)
                if (functionLiteralExpression != null) {
                    val functionLiteralParameterTypes = getParametersForFunctionLiteral(functionLiteralExpression, analyze)
                    if (functionLiteralParameterTypes != null) {
                        codeToInline.addPostInsertionAction(mainExpression) { inlinedExpression ->
                            addFunctionLiteralParameterTypes(functionLiteralParameterTypes, inlinedExpression)
                        }
                    }
                }
            }
        }

        return codeToInline
    }

    private fun removeContracts(codeToInline: MutableCodeToInline, analyze: (KtExpression) -> BindingContext) {
        for (statement in codeToInline.statementsBefore) {
            val context = analyze(statement)
            if (statement.getResolvedCall(context)?.resultingDescriptor?.fqNameOrNull()?.asString() == "kotlin.contracts.contract") {
                codeToInline.addPreCommitAction(statement) {
                    codeToInline.statementsBefore.remove(it)
                }
            }
        }
    }

    fun prepareCodeToInline(
        mainExpression: KtExpression?,
        statementsBefore: List<KtExpression>,
        analyze: (KtExpression) -> BindingContext,
        reformat: Boolean,
    ): CodeToInline = prepareMutableCodeToInline(mainExpression, statementsBefore, analyze, reformat).toNonMutable()

    private fun saveComments(codeToInline: MutableCodeToInline, contextDeclaration: KtDeclaration) {
        val bodyBlockExpression = contextDeclaration.safeAs<KtDeclarationWithBody>()?.bodyBlockExpression
        if (bodyBlockExpression != null) addCommentHoldersForStatements(codeToInline, bodyBlockExpression)
    }

    private fun addCommentHoldersForStatements(mutableCodeToInline: MutableCodeToInline, blockExpression: KtBlockExpression) {
        val expressions = mutableCodeToInline.expressions

        for ((indexOfIteration, commentHolder) in CommentHolder.extract(blockExpression).withIndex()) {
            if (commentHolder.isEmpty) continue

            if (expressions.isEmpty()) {
                mutableCodeToInline.addExtraComments(commentHolder)
            } else {
                val expression = expressions.elementAtOrNull(indexOfIteration)
                if (expression != null) {
                    expression.mergeComments(commentHolder)
                } else {
                    expressions.last().mergeComments(
                        CommentHolder(emptyList(), trailingComments = commentHolder.leadingComments + commentHolder.trailingComments)
                    )
                }
            }
        }
    }

    private fun getParametersForFunctionLiteral(
        functionLiteralExpression: KtLambdaExpression,
        analyze: (KtExpression) -> BindingContext
    ): String? {
        val context = analyze(functionLiteralExpression)
        val lambdaDescriptor = context.get(BindingContext.FUNCTION, functionLiteralExpression.functionLiteral)
        if (lambdaDescriptor == null ||
            ErrorUtils.containsErrorTypeInParameters(lambdaDescriptor) ||
            ErrorUtils.containsErrorType(lambdaDescriptor.returnType)
        ) return null

        return lambdaDescriptor.valueParameters.joinToString {
            it.name.render() + ": " + IdeDescriptorRenderers.SOURCE_CODE.renderType(it.type)
        }
    }

    private fun addFunctionLiteralParameterTypes(parameters: String, inlinedExpression: KtExpression) {
        val containingFile = inlinedExpression.containingKtFile
        val resolutionFacade = containingFile.getResolutionFacade()
        val lambdaExpr = inlinedExpression.unpackFunctionLiteral(true).sure {
            "can't find function literal expression for " + inlinedExpression.text
        }

        if (!needToAddParameterTypes(lambdaExpr, resolutionFacade)) return
        SpecifyExplicitLambdaSignatureIntention.applyWithParameters(lambdaExpr, parameters)
    }

    private fun needToAddParameterTypes(
        lambdaExpression: KtLambdaExpression,
        resolutionFacade: ResolutionFacade
    ): Boolean {
        val functionLiteral = lambdaExpression.functionLiteral
        val context = resolutionFacade.analyze(lambdaExpression, BodyResolveMode.PARTIAL_WITH_DIAGNOSTICS)
        return context.diagnostics.any { diagnostic ->
            val factory = diagnostic.factory
            val element = diagnostic.psiElement
            val hasCantInferParameter = factory == Errors.CANNOT_INFER_PARAMETER_TYPE && element.parent.parent == functionLiteral
            val hasUnresolvedItOrThis = factory == Errors.UNRESOLVED_REFERENCE &&
                    element.text == "it" &&
                    element.getStrictParentOfType<KtFunctionLiteral>() == functionLiteral

            hasCantInferParameter || hasUnresolvedItOrThis
        }
    }

    private fun insertExplicitTypeArguments(
        codeToInline: MutableCodeToInline,
        analyze: (KtExpression) -> BindingContext,
    ) = codeToInline.forEachDescendantOfType<KtCallExpression> {
        val bindingContext = analyze(it)
        if (InsertExplicitTypeArgumentsIntention.isApplicableTo(it, bindingContext)) {
            val typeArguments = InsertExplicitTypeArgumentsIntention.createTypeArguments(it, bindingContext)!!
            codeToInline.addPreCommitAction(it) { callExpression ->
                callExpression.addAfter(typeArguments, callExpression.calleeExpression)
                callExpression.typeArguments.forEach { typeArgument ->
                    val reference = typeArgument.typeReference?.typeElement?.safeAs<KtUserType>()?.referenceExpression
                    reference?.putCopyableUserData(CodeToInline.TYPE_PARAMETER_USAGE_KEY, Name.identifier(reference.text))
                }
            }
        }
    }

    private fun findDescriptorAndContext(
        expression: KtSimpleNameExpression,
        analyzer: (KtExpression) -> BindingContext
    ): Pair<BindingContext, DeclarationDescriptor>? {
        val currentContext = analyzer(expression)
        val descriptor = currentContext[BindingContext.SHORT_REFERENCE_TO_COMPANION_OBJECT, expression]
            ?: currentContext[BindingContext.REFERENCE_TARGET, expression]

        if (descriptor != null) return currentContext to descriptor
        return findCallableDescriptorAndContext(expression, analyzer)
    }

    private fun findCallableDescriptorAndContext(
        expression: KtSimpleNameExpression,
        analyzer: (KtExpression) -> BindingContext
    ): Pair<BindingContext, CallableDescriptor>? {
        val callExpression = expression.parent as? KtCallExpression ?: return null
        val context = analyzer(callExpression)
        return callExpression.getResolvedCall(context)?.resultingDescriptor?.let { context to it }
    }

    private fun findResolvedCall(
        expression: KtSimpleNameExpression,
        bindingContext: BindingContext,
        analyzer: (KtExpression) -> BindingContext,
    ): Pair<KtExpression, ResolvedCall<out CallableDescriptor>>? {
        return getResolvedCallIfReallySuccess(expression, bindingContext)?.let { expression to it }
            ?: expression.parent?.safeAs<KtCallExpression>()?.let { callExpression ->
                getResolvedCallIfReallySuccess(callExpression, analyzer(callExpression))?.let { callExpression to it }
            }
    }

    private fun getResolvedCallIfReallySuccess(
        expression: KtExpression,
        bindingContext: BindingContext
    ): ResolvedCall<out CallableDescriptor>? {
        return expression.getResolvedCall(bindingContext)?.takeIf { it.isReallySuccess() }
    }

    private fun processReferences(codeToInline: MutableCodeToInline, analyze: (KtExpression) -> BindingContext, reformat: Boolean) {
        val targetDispatchReceiverType = targetCallable.dispatchReceiverParameter?.value?.type?.unCapture()
        val targetExtensionReceiverType = targetCallable.extensionReceiverParameter?.value?.type?.unCapture()
        val isAnonymousFunction = originalDeclaration?.isAnonymousFunction == true
        val isAnonymousFunctionWithReceiver = isAnonymousFunction &&
                originalDeclaration.cast<KtNamedFunction>().receiverTypeReference != null

        fun getParameterName(parameter: ValueParameterDescriptor): Name = if (isAnonymousFunction) {
            val shift = if (isAnonymousFunctionWithReceiver) 2 else 1
            Name.identifier("p${parameter.index + shift}")
        } else {
            parameter.name
        }

        codeToInline.forEachDescendantOfType<KtSimpleNameExpression> { expression ->
            val parent = expression.parent
            if (parent is KtValueArgumentName || parent is KtCallableReferenceExpression) return@forEachDescendantOfType
            val (bindingContext, target) = findDescriptorAndContext(expression, analyze)
                ?: return@forEachDescendantOfType addFakeSuperReceiver(codeToInline, expression)

            //TODO: other types of references ('[]' etc)
            if (expression.canBeResolvedViaImport(target, bindingContext)) {
                val importableFqName = target.importableFqName
                if (importableFqName != null) {
                    val lexicalScope = (expression.containingFile as? KtFile)?.getResolutionScope(bindingContext, resolutionFacade)
                    val lookupName = lexicalScope?.findClassifier(importableFqName.shortName(), NoLookupLocation.FROM_IDE)
                        ?.typeConstructor
                        ?.declarationDescriptor
                        ?.fqNameOrNull()

                    codeToInline.fqNamesToImport.add(lookupName ?: importableFqName)
                }
            }

            val callableDescriptor = targetCallable.safeAs<ImportedFromObjectCallableDescriptor<*>>()?.callableFromObject ?: targetCallable
            val receiverExpression = expression.getReceiverExpression()
            if (receiverExpression != null &&
                parent is KtCallExpression &&
                target is ValueParameterDescriptor &&
                target.type.isExtensionFunctionType &&
                target.containingDeclaration == callableDescriptor
            ) {
                codeToInline.addPreCommitAction(parent) { callExpression ->
                    val qualifiedExpression = callExpression.parent as KtDotQualifiedExpression
                    val valueArgumentList = callExpression.getOrCreateValueArgumentList()
                    val newArgument = psiFactory.createArgument(qualifiedExpression.receiverExpression)
                    valueArgumentList.addArgumentBefore(newArgument, valueArgumentList.arguments.firstOrNull())
                    val newExpression = qualifiedExpression.replaced(callExpression)
                    if (qualifiedExpression in codeToInline) {
                        codeToInline.replaceExpression(qualifiedExpression, newExpression)
                    }
                }

                expression.putCopyableUserData(CodeToInline.PARAMETER_USAGE_KEY, getParameterName(target))
            } else if (receiverExpression == null) {
                if (isAnonymousFunctionWithReceiver && target == callableDescriptor) {
                    // parent is [KtThisExpression]
                    parent.putCopyableUserData(CodeToInline.PARAMETER_USAGE_KEY, getFirstParameterName())
                } else if (target is ValueParameterDescriptor && target.containingDeclaration == callableDescriptor) {
                    expression.putCopyableUserData(CodeToInline.PARAMETER_USAGE_KEY, getParameterName(target))
                } else if (target is TypeParameterDescriptor && target.containingDeclaration == callableDescriptor) {
                    expression.putCopyableUserData(CodeToInline.TYPE_PARAMETER_USAGE_KEY, target.name)
                }

                if (targetCallable !is ImportedFromObjectCallableDescriptor<*>) {
                    val (expressionToResolve, resolvedCall) = findResolvedCall(expression, bindingContext, analyze)
                        ?: return@forEachDescendantOfType

                    val receiver = if (resolvedCall.resultingDescriptor.isExtension)
                        resolvedCall.extensionReceiver
                    else
                        resolvedCall.dispatchReceiver

                    if (receiver is ImplicitReceiver) {
                        val resolutionScope = expression.getResolutionScope(bindingContext, resolutionFacade)
                        val receiverExpressionToInline = receiver.asExpression(resolutionScope, psiFactory)
                        val receiverType = receiver.type.unCapture()
                        val isSameReceiverType = receiverType == targetDispatchReceiverType || receiverType == targetExtensionReceiverType
                        val receiverIsUnnecessary =
                            (receiverExpressionToInline as? KtThisExpression)?.labelQualifier != null && isSameReceiverType
                        if (receiverExpressionToInline != null && !receiverIsUnnecessary) {
                            codeToInline.addPreCommitAction(expressionToResolve) { expr ->
                                val expressionToReplace = expr.parent as? KtCallExpression ?: expr
                                val replaced = codeToInline.replaceExpression(
                                    expressionToReplace,
                                    psiFactory.createExpressionByPattern(
                                        "$0.$1", receiverExpressionToInline, expressionToReplace,
                                        reformat = reformat
                                    )
                                ) as? KtQualifiedExpression

                                val thisExpression = replaced?.receiverExpression ?: return@addPreCommitAction
                                if (isAnonymousFunctionWithReceiver && receiverType == targetExtensionReceiverType) {
                                    thisExpression.putCopyableUserData(CodeToInline.PARAMETER_USAGE_KEY, getFirstParameterName())
                                } else if (!isSameReceiverType) {
                                    thisExpression.putCopyableUserData(CodeToInline.SIDE_RECEIVER_USAGE_KEY, Unit)
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    private fun addFakeSuperReceiver(codeToInline: MutableCodeToInline, expression: KtExpression) {
        if (!fallbackToSuperCall || expression !is KtNameReferenceExpression) return

        val parent = expression.parent
        val prevSiblingElementType = expression.getPrevSiblingIgnoringWhitespaceAndComments()?.elementType
        if (prevSiblingElementType != KtTokens.SAFE_ACCESS && prevSiblingElementType != KtTokens.DOT) {
            val expressionToReplace = if (parent is KtCallExpression) parent else expression
            codeToInline.addPreCommitAction(expressionToReplace) { referenceExpression ->
                val qualifierExpression = codeToInline.replaceExpression(
                    referenceExpression,
                    psiFactory.createExpressionByPattern("super.$0", referenceExpression),
                ) as? KtDotQualifiedExpression

                qualifierExpression?.receiverExpression?.putCopyableUserData(CodeToInline.FAKE_SUPER_CALL_KEY, Unit)
            }
        }
    }
}

private fun getFirstParameterName(): Name = Name.identifier("p1")