Added 'getParameterForArgument' util function (using resolved call)

Removed similar util methods (using psi):
getParameterCorrespondingToValueArgumentPassedInCall
getParameterCorrespondingToFunctionLiteralPassedOutsideArgumentList
This commit is contained in:
Svetlana Isakova
2014-07-16 12:03:16 +04:00
parent 2ae87cae4a
commit b3ad725e8d
5 changed files with 77 additions and 129 deletions
@@ -26,7 +26,6 @@ import org.jetbrains.jet.lang.resolve.calls.callUtil.CallUtilPackage;
import org.jetbrains.jet.lang.resolve.calls.model.ArgumentMapping;
import org.jetbrains.jet.lang.resolve.calls.model.ArgumentMatch;
import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall;
import org.jetbrains.jet.lang.resolve.calls.util.CallMaker;
import org.jetbrains.jet.lang.types.lang.InlineUtil;
public class InlineDescriptorUtils {
@@ -54,7 +53,8 @@ public class InlineDescriptorUtils {
CallableDescriptor resultingDescriptor = resolvedCall == null ? null : resolvedCall.getResultingDescriptor();
if (resultingDescriptor instanceof SimpleFunctionDescriptor &&
((SimpleFunctionDescriptor) resultingDescriptor).getInlineStrategy().isInline()) {
ValueArgument argument = getContainingArgument(containingFunction, call);
ValueArgument argument = CallUtilPackage.getValueArgumentForExpression(
resolvedCall.getCall(), (JetFunctionLiteralExpression) containingFunction);
if (argument != null) {
ArgumentMapping mapping = resolvedCall.getArgumentMapping(argument);
if (mapping instanceof ArgumentMatch) {
@@ -77,22 +77,6 @@ public class InlineDescriptorUtils {
return fromFunction == containingFunctionDescriptor;
}
@Nullable
public static ValueArgument getContainingArgument(PsiElement expression, @NotNull JetExpression stopAtCall) {
if (expression instanceof JetValueArgument) {
return (JetValueArgument) expression;
}
while (expression != null && expression.getParent() != stopAtCall) {
PsiElement parent = expression.getParent();
if (parent instanceof JetValueArgument) {
return (JetValueArgument) parent;
}
expression = parent;
}
return expression != null ? CallMaker.makeValueArgument((JetExpression) expression) : null;
}
@Nullable
public static DeclarationDescriptor getContainingClassOrFunctionDescriptor(@NotNull DeclarationDescriptor descriptor, boolean strict) {
DeclarationDescriptor currentDescriptor = strict ? descriptor.getContainingDeclaration() : descriptor;
@@ -24,7 +24,6 @@ import org.jetbrains.jet.lang.resolve.calls.model.ArgumentMatch
import org.jetbrains.jet.lang.resolve.calls.model.ArgumentMatchStatus
import org.jetbrains.jet.lang.psi.Call
import org.jetbrains.jet.lang.psi.ValueArgument
import org.jetbrains.jet.lang.resolve.calls.util.CallMaker
import org.jetbrains.jet.lang.resolve.BindingContext
import org.jetbrains.jet.lang.resolve.calls.ArgumentTypeResolver
import org.jetbrains.jet.lang.psi.JetElement
@@ -48,6 +47,8 @@ import org.jetbrains.jet.lang.psi.psiUtil.getTextWithLocation
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor
import org.jetbrains.jet.lang.psi.JetCallExpression
import org.jetbrains.jet.lang.psi.JetFunctionLiteralArgument
import org.jetbrains.jet.lang.psi.JetFunctionLiteralExpression
import org.jetbrains.jet.lang.psi.JetFunctionLiteral
// resolved call
@@ -74,6 +75,10 @@ public fun <D : CallableDescriptor> ResolvedCall<D>.hasTypeMismatchErrorOnParame
}
}
public fun <D : CallableDescriptor> ResolvedCall<D>.getParameterForArgument(valueArgument: ValueArgument?): ValueParameterDescriptor? {
return (valueArgument?.let { getArgumentMapping(it) } as? ArgumentMatch)?.valueParameter
}
// call
public fun Call.hasUnresolvedArguments(context: BindingContext): Boolean {
@@ -93,6 +98,20 @@ public fun JetCallExpression.getValueArgumentsInParentheses(): List<ValueArgumen
[suppress("UNCHECKED_CAST")]
private fun List<ValueArgument?>.filterArgsInParentheses() = filter { it !is JetFunctionLiteralArgument } as List<ValueArgument>
public fun Call.getValueArgumentForExpression(expression: JetExpression): ValueArgument? {
fun JetElement.deparenthesizeStructurally(): JetElement? {
val deparenthesized = if (this is JetExpression) JetPsiUtil.deparenthesizeOnce(this, false) else this
return when {
deparenthesized != this -> deparenthesized
this is JetFunctionLiteralExpression -> this.getFunctionLiteral()
this is JetFunctionLiteral -> this.getBodyExpression()
else -> null
}
}
fun JetElement.isParenthesizedExpression() = stream(this) { it.deparenthesizeStructurally() }.any { it == expression }
return getValueArguments().firstOrNull { it?.getArgumentExpression()?.isParenthesizedExpression() ?: false }
}
// Get call / resolved call from binding context
public fun JetElement.getCalleeExpressionIfAny(): JetExpression? {