org.jetbrains.kotlin.resolve.calls.util.callUtil.kt Maven / Gradle / Ivy
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.resolve.calls.callUtil
import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
import org.jetbrains.kotlin.incremental.KotlinLookupLocation
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getTextWithLocation
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.BindingContext.CALL
import org.jetbrains.kotlin.resolve.BindingContext.RESOLVED_CALL
import org.jetbrains.kotlin.resolve.calls.ArgumentTypeResolver
import org.jetbrains.kotlin.resolve.calls.CallTransformer
import org.jetbrains.kotlin.resolve.calls.context.ResolutionContext
import org.jetbrains.kotlin.resolve.calls.model.*
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
import org.jetbrains.kotlin.utils.sure
// resolved call
fun ResolvedCall.noErrorsInValueArguments(): Boolean {
return call.valueArguments.all { argument -> !getArgumentMapping(argument!!).isError() }
}
fun ResolvedCall.hasUnmappedArguments(): Boolean {
return call.valueArguments.any { argument -> getArgumentMapping(argument!!) == ArgumentUnmapped }
}
fun ResolvedCall.hasUnmappedParameters(): Boolean {
val parameterToArgumentMap = valueArguments
return !parameterToArgumentMap.keys.containsAll(resultingDescriptor.valueParameters)
}
fun ResolvedCall.allArgumentsMapped()
= call.valueArguments.all { argument -> getArgumentMapping(argument) is ArgumentMatch }
fun ResolvedCall.hasTypeMismatchErrorOnParameter(parameter: ValueParameterDescriptor): Boolean {
val resolvedValueArgument = valueArguments[parameter]
if (resolvedValueArgument == null) return true
return resolvedValueArgument.arguments.any { argument ->
val argumentMapping = getArgumentMapping(argument)
argumentMapping is ArgumentMatch && argumentMapping.status == ArgumentMatchStatus.TYPE_MISMATCH
}
}
fun ResolvedCall.getParameterForArgument(valueArgument: ValueArgument?): ValueParameterDescriptor? {
return (valueArgument?.let { getArgumentMapping(it) } as? ArgumentMatch)?.valueParameter
}
fun ResolvedCall.usesDefaultArguments(): Boolean {
return valueArgumentsByIndex?.any { it is DefaultValueArgument } ?: false
}
// call
fun > Call.hasUnresolvedArguments(context: ResolutionContext): Boolean {
val arguments = valueArguments.map { it.getArgumentExpression() }
return arguments.any (fun (argument: KtExpression?): Boolean {
if (argument == null || ArgumentTypeResolver.isFunctionLiteralArgument(argument, context)) return false
val resolvedCall = argument.getResolvedCall(context.trace.bindingContext) as MutableResolvedCall<*>?
if (resolvedCall != null && !resolvedCall.hasInferredReturnType()) return false
val expressionType = context.trace.bindingContext.getType(argument)
return expressionType == null || expressionType.isError
})
}
fun Call.getValueArgumentsInParentheses(): List = valueArguments.filterArgsInParentheses()
fun KtCallElement.getValueArgumentsInParentheses(): List = valueArguments.filterArgsInParentheses()
fun Call.getValueArgumentListOrElement(): KtElement =
if (this is CallTransformer.CallForImplicitInvoke) {
outerCall.getValueArgumentListOrElement()
}
else {
valueArgumentList ?: calleeExpression ?: callElement
}
@Suppress("UNCHECKED_CAST")
private fun List.filterArgsInParentheses() = filter { it !is KtLambdaArgument } as List
fun Call.getValueArgumentForExpression(expression: KtExpression): ValueArgument? {
fun KtElement.deparenthesizeStructurally(): KtElement? {
val deparenthesized = if (this is KtExpression) KtPsiUtil.deparenthesizeOnce(this) else this
return when {
deparenthesized != this -> deparenthesized
this is KtLambdaExpression -> this.functionLiteral
this is KtFunctionLiteral -> this.bodyExpression
else -> null
}
}
fun KtElement.isParenthesizedExpression() = generateSequence(this) { it.deparenthesizeStructurally() }.any { it == expression }
return valueArguments.firstOrNull { it?.getArgumentExpression()?.isParenthesizedExpression() ?: false }
}
// Get call / resolved call from binding context
fun KtElement?.getCalleeExpressionIfAny(): KtExpression? {
val element = if (this is KtExpression) KtPsiUtil.deparenthesize(this) else this
return when (element) {
is KtSimpleNameExpression -> element
is KtCallElement -> element.calleeExpression
is KtQualifiedExpression -> element.selectorExpression.getCalleeExpressionIfAny()
is KtOperationExpression -> element.operationReference
else -> null
}
}
/**
* For expressions like a(), a[i], a.b.c(), +a, a + b, (a()), a(): Int, @label a()
* returns a corresponding call.
*
* Note: special construction like a!!, a ?: b, if (c) a else b
are resolved as calls,
* so there is a corresponding call for them.
*/
fun KtElement.getCall(context: BindingContext): Call? {
val element = if (this is KtExpression) KtPsiUtil.deparenthesize(this) else this
if (element == null) return null
// Do not use Call bound to outer call expression (if any) to prevent stack overflow during analysis
if (element is KtCallElement && element.calleeExpression == null) return null
val parent = element.parent
val reference: KtExpression? = when {
parent is KtInstanceExpressionWithLabel -> parent
parent is KtUserType -> parent.getParent()?.getParent() as? KtConstructorCalleeExpression
else -> element.getCalleeExpressionIfAny()
}
if (reference != null) {
return context[CALL, reference]
}
return context[CALL, element]
}
fun KtElement.getParentCall(context: BindingContext, strict: Boolean = true): Call? {
val callExpressionTypes = arrayOf?>(
KtSimpleNameExpression::class.java, KtCallElement::class.java, KtBinaryExpression::class.java,
KtUnaryExpression::class.java, KtArrayAccessExpression::class.java)
val parent = if (strict) {
PsiTreeUtil.getParentOfType(this, *callExpressionTypes)
} else {
PsiTreeUtil.getNonStrictParentOfType(this, *callExpressionTypes)
}
return parent?.getCall(context)
}
fun Call?.getResolvedCall(context: BindingContext): ResolvedCall? {
return context[RESOLVED_CALL, this]
}
fun KtElement?.getResolvedCall(context: BindingContext): ResolvedCall? {
return this?.getCall(context)?.getResolvedCall(context)
}
fun KtElement?.getParentResolvedCall(context: BindingContext, strict: Boolean = true): ResolvedCall? {
return this?.getParentCall(context, strict)?.getResolvedCall(context)
}
fun KtElement.getCallWithAssert(context: BindingContext): Call {
return getCall(context).sure { "No call for ${this.getTextWithLocation()}" }
}
fun KtElement.getResolvedCallWithAssert(context: BindingContext): ResolvedCall {
return getResolvedCall(context).sure { "No resolved call for ${this.getTextWithLocation()}" }
}
fun Call.getResolvedCallWithAssert(context: BindingContext): ResolvedCall {
return getResolvedCall(context).sure { "No resolved call for ${this.callElement.getTextWithLocation()}" }
}
fun KtExpression.getFunctionResolvedCallWithAssert(context: BindingContext): ResolvedCall {
val resolvedCall = getResolvedCallWithAssert(context)
assert(resolvedCall.resultingDescriptor is FunctionDescriptor) {
"ResolvedCall for this expression must be ResolvedCall extends FunctionDescriptor>: ${this.getTextWithLocation()}"
}
@Suppress("UNCHECKED_CAST")
return resolvedCall as ResolvedCall
}
fun Call.isSafeCall(): Boolean {
if (this is CallTransformer.CallForImplicitInvoke) {
//implicit safe 'invoke'
if (outerCall.isExplicitSafeCall()) {
return true
}
}
return isExplicitSafeCall()
}
fun Call.isExplicitSafeCall(): Boolean = callOperationNode?.elementType == KtTokens.SAFE_ACCESS
fun Call.createLookupLocation() = KotlinLookupLocation(run {
calleeExpression?.let {
// Can't use getContainingJetFile() because we can get from IDE an element with JavaDummyHolder as containing file
if ((it.containingFile as? KtFile)?.doNotAnalyze == null) it else null
}
?: callElement
})