001 /*
002 * Copyright 2010-2015 JetBrains s.r.o.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package org.jetbrains.kotlin.resolve.calls;
018
019 import com.google.common.collect.Lists;
020 import org.jetbrains.annotations.NotNull;
021 import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
022 import org.jetbrains.kotlin.descriptors.CallableDescriptor;
023 import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor;
024 import org.jetbrains.kotlin.descriptors.ReceiverParameterDescriptor;
025 import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor;
026 import org.jetbrains.kotlin.psi.Call;
027 import org.jetbrains.kotlin.psi.JetExpression;
028 import org.jetbrains.kotlin.psi.JetSimpleNameExpression;
029 import org.jetbrains.kotlin.psi.JetSuperExpression;
030 import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystem;
031 import org.jetbrains.kotlin.resolve.scopes.receivers.ExpressionReceiver;
032 import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue;
033 import org.jetbrains.kotlin.types.*;
034
035 import java.util.Collections;
036 import java.util.List;
037
038 import static org.jetbrains.kotlin.resolve.calls.inference.constraintPosition.ConstraintPositionKind.EXPECTED_TYPE_POSITION;
039 import static org.jetbrains.kotlin.types.TypeUtils.DONT_CARE;
040
041 public class CallResolverUtil {
042 public static enum ResolveArgumentsMode {
043 RESOLVE_FUNCTION_ARGUMENTS,
044 SHAPE_FUNCTION_ARGUMENTS
045 }
046
047 private CallResolverUtil() {}
048
049
050 public static boolean hasUnknownFunctionParameter(@NotNull JetType type) {
051 assert KotlinBuiltIns.isFunctionOrExtensionFunctionType(type);
052 List<TypeProjection> arguments = type.getArguments();
053 // last argument is return type of function type
054 List<TypeProjection> functionParameters = arguments.subList(0, arguments.size() - 1);
055 for (TypeProjection functionParameter : functionParameters) {
056 if (TypeUtils.containsSpecialType(functionParameter.getType(), DONT_CARE)
057 || ErrorUtils.containsUninferredParameter(functionParameter.getType())) {
058 return true;
059 }
060 }
061 return false;
062 }
063
064 public static boolean hasUnknownReturnType(@NotNull JetType type) {
065 assert KotlinBuiltIns.isFunctionOrExtensionFunctionType(type);
066 JetType returnTypeFromFunctionType = KotlinBuiltIns.getReturnTypeFromFunctionType(type);
067 return ErrorUtils.containsErrorType(returnTypeFromFunctionType);
068 }
069
070 public static JetType replaceReturnTypeByUnknown(@NotNull JetType type) {
071 assert KotlinBuiltIns.isFunctionOrExtensionFunctionType(type);
072 List<TypeProjection> arguments = type.getArguments();
073 List<TypeProjection> newArguments = Lists.newArrayList();
074 newArguments.addAll(arguments.subList(0, arguments.size() - 1));
075 newArguments.add(new TypeProjectionImpl(Variance.INVARIANT, DONT_CARE));
076 return new JetTypeImpl(type.getAnnotations(), type.getConstructor(), type.isMarkedNullable(), newArguments, type.getMemberScope());
077 }
078
079 private static boolean hasReturnTypeDependentOnUninferredParams(
080 @NotNull CallableDescriptor candidateDescriptor,
081 @NotNull ConstraintSystem constraintSystem
082 ) {
083 JetType returnType = candidateDescriptor.getReturnType();
084 if (returnType == null) return false;
085
086 for (TypeParameterDescriptor typeVariable : constraintSystem.getTypeVariables()) {
087 JetType inferredValueForTypeVariable = constraintSystem.getTypeBounds(typeVariable).getValue();
088 if (inferredValueForTypeVariable == null) {
089 if (TypeUtils.dependsOnTypeParameters(returnType, Collections.singleton(typeVariable))) {
090 return true;
091 }
092 }
093 }
094 return false;
095 }
096
097 public static boolean hasInferredReturnType(
098 @NotNull CallableDescriptor candidateDescriptor,
099 @NotNull ConstraintSystem constraintSystem
100 ) {
101 if (hasReturnTypeDependentOnUninferredParams(candidateDescriptor, constraintSystem)) return false;
102
103 // Expected type mismatch was reported before as 'TYPE_INFERENCE_EXPECTED_TYPE_MISMATCH'
104 if (constraintSystem.getStatus().hasOnlyErrorsFromPosition(EXPECTED_TYPE_POSITION.position())) return false;
105 return true;
106 }
107
108 @NotNull
109 public static JetType getErasedReceiverType(
110 @NotNull ReceiverParameterDescriptor receiverParameterDescriptor,
111 @NotNull CallableDescriptor descriptor
112 ) {
113 JetType receiverType = receiverParameterDescriptor.getType();
114 for (TypeParameterDescriptor typeParameter : descriptor.getTypeParameters()) {
115 if (typeParameter.getTypeConstructor().equals(receiverType.getConstructor())) {
116 receiverType = typeParameter.getUpperBoundsAsType();
117 }
118 }
119 List<TypeProjection> fakeTypeArguments = Lists.newArrayList();
120 for (TypeProjection typeProjection : receiverType.getArguments()) {
121 fakeTypeArguments.add(new TypeProjectionImpl(typeProjection.getProjectionKind(), DONT_CARE));
122 }
123 return new JetTypeImpl(
124 receiverType.getAnnotations(), receiverType.getConstructor(), receiverType.isMarkedNullable(),
125 fakeTypeArguments, ErrorUtils.createErrorScope("Error scope for erased receiver type", /*throwExceptions=*/true));
126 }
127
128 public static boolean isOrOverridesSynthesized(@NotNull CallableMemberDescriptor descriptor) {
129 if (descriptor.getKind() == CallableMemberDescriptor.Kind.SYNTHESIZED) {
130 return true;
131 }
132 if (descriptor.getKind() == CallableMemberDescriptor.Kind.FAKE_OVERRIDE) {
133 for (CallableMemberDescriptor overridden : descriptor.getOverriddenDescriptors()) {
134 if (!isOrOverridesSynthesized(overridden)) {
135 return false;
136 }
137 }
138 return true;
139 }
140 return false;
141 }
142
143 public static boolean isInvokeCallOnVariable(@NotNull Call call) {
144 if (call.getCallType() != Call.CallType.INVOKE) return false;
145 ReceiverValue dispatchReceiver = call.getDispatchReceiver();
146 //calleeExpressionAsDispatchReceiver for invoke is always ExpressionReceiver, see CallForImplicitInvoke
147 JetExpression expression = ((ExpressionReceiver) dispatchReceiver).getExpression();
148 return expression instanceof JetSimpleNameExpression;
149 }
150
151 public static boolean isInvokeCallOnExpressionWithBothReceivers(@NotNull Call call) {
152 if (call.getCallType() != Call.CallType.INVOKE || isInvokeCallOnVariable(call)) return false;
153 return call.getExplicitReceiver().exists() && call.getDispatchReceiver().exists();
154 }
155
156 public static JetSuperExpression getSuperCallExpression(@NotNull Call call) {
157 ReceiverValue explicitReceiver = call.getExplicitReceiver();
158 if (explicitReceiver instanceof ExpressionReceiver) {
159 JetExpression receiverExpression = ((ExpressionReceiver) explicitReceiver).getExpression();
160 if (receiverExpression instanceof JetSuperExpression) {
161 return (JetSuperExpression) receiverExpression;
162 }
163 }
164 return null;
165 }
166 }