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.types.expressions;
018
019 import kotlin.Pair;
020 import org.jetbrains.annotations.NotNull;
021 import org.jetbrains.annotations.Nullable;
022 import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
023 import org.jetbrains.kotlin.descriptors.FunctionDescriptor;
024 import org.jetbrains.kotlin.descriptors.ReceiverParameterDescriptor;
025 import org.jetbrains.kotlin.diagnostics.DiagnosticFactory1;
026 import org.jetbrains.kotlin.diagnostics.DiagnosticSink;
027 import org.jetbrains.kotlin.name.Name;
028 import org.jetbrains.kotlin.psi.Call;
029 import org.jetbrains.kotlin.psi.KtExpression;
030 import org.jetbrains.kotlin.resolve.calls.checkers.OperatorCallChecker;
031 import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall;
032 import org.jetbrains.kotlin.resolve.calls.results.OverloadResolutionResults;
033 import org.jetbrains.kotlin.resolve.scopes.receivers.ExpressionReceiver;
034 import org.jetbrains.kotlin.resolve.scopes.receivers.TransientReceiver;
035 import org.jetbrains.kotlin.types.DynamicTypesKt;
036 import org.jetbrains.kotlin.types.ErrorUtils;
037 import org.jetbrains.kotlin.types.KotlinType;
038 import org.jetbrains.kotlin.util.OperatorNameConventions;
039 import org.jetbrains.kotlin.util.slicedMap.WritableSlice;
040
041 import java.util.Collections;
042
043 import static org.jetbrains.kotlin.diagnostics.Errors.*;
044 import static org.jetbrains.kotlin.resolve.BindingContext.*;
045
046 public class ForLoopConventionsChecker {
047 private final KotlinBuiltIns builtIns;
048 private final FakeCallResolver fakeCallResolver;
049
050 public ForLoopConventionsChecker(
051 @NotNull KotlinBuiltIns builtIns,
052 @NotNull FakeCallResolver fakeCallResolver
053 ) {
054 this.builtIns = builtIns;
055 this.fakeCallResolver = fakeCallResolver;
056 }
057
058 @Nullable
059 public KotlinType checkIterableConvention(@NotNull ExpressionReceiver loopRange, ExpressionTypingContext context) {
060 KtExpression loopRangeExpression = loopRange.getExpression();
061
062 // Make a fake call loopRange.iterator(), and try to resolve it
063 Pair<Call, OverloadResolutionResults<FunctionDescriptor>> calls = fakeCallResolver.makeAndResolveFakeCall(
064 loopRange, context, Collections.<KtExpression>emptyList(), OperatorNameConventions.ITERATOR, loopRangeExpression,
065 FakeCallKind.ITERATOR, loopRangeExpression
066 );
067 OverloadResolutionResults<FunctionDescriptor> iteratorResolutionResults = calls.getSecond();
068
069 if (iteratorResolutionResults.isSuccess()) {
070 ResolvedCall<FunctionDescriptor> iteratorResolvedCall = iteratorResolutionResults.getResultingCall();
071 context.trace.record(LOOP_RANGE_ITERATOR_RESOLVED_CALL, loopRangeExpression, iteratorResolvedCall);
072 FunctionDescriptor iteratorFunction = iteratorResolvedCall.getResultingDescriptor();
073
074 checkIfOperatorModifierPresent(loopRangeExpression, iteratorFunction, context.trace);
075
076 KotlinType iteratorType = iteratorFunction.getReturnType();
077 //noinspection ConstantConditions
078 KotlinType hasNextType = checkConventionForIterator(
079 context, loopRangeExpression, iteratorType, OperatorNameConventions.HAS_NEXT,
080 HAS_NEXT_FUNCTION_AMBIGUITY, HAS_NEXT_MISSING, HAS_NEXT_FUNCTION_NONE_APPLICABLE, LOOP_RANGE_HAS_NEXT_RESOLVED_CALL
081 );
082 if (hasNextType != null && !builtIns.isBooleanOrSubtype(hasNextType)) {
083 context.trace.report(HAS_NEXT_FUNCTION_TYPE_MISMATCH.on(loopRangeExpression, hasNextType));
084 }
085 return checkConventionForIterator(
086 context, loopRangeExpression, iteratorType, OperatorNameConventions.NEXT,
087 NEXT_AMBIGUITY, NEXT_MISSING, NEXT_NONE_APPLICABLE, LOOP_RANGE_NEXT_RESOLVED_CALL
088 );
089 }
090 return null;
091 }
092
093 private static void checkIfOperatorModifierPresent(KtExpression expression, FunctionDescriptor descriptor, DiagnosticSink sink) {
094 if (ErrorUtils.isError(descriptor)) return;
095 ReceiverParameterDescriptor extensionReceiverParameter = descriptor.getExtensionReceiverParameter();
096 if ((extensionReceiverParameter != null) && (DynamicTypesKt.isDynamic(extensionReceiverParameter.getType()))) return;
097
098 if (!descriptor.isOperator()) {
099 OperatorCallChecker.Companion.report(expression, descriptor, sink);
100 }
101 }
102
103 @Nullable
104 private KotlinType checkConventionForIterator(
105 @NotNull ExpressionTypingContext context,
106 @NotNull KtExpression loopRangeExpression,
107 @NotNull KotlinType iteratorType,
108 @NotNull Name name,
109 @NotNull DiagnosticFactory1<KtExpression, KotlinType> ambiguity,
110 @NotNull DiagnosticFactory1<KtExpression, KotlinType> missing,
111 @NotNull DiagnosticFactory1<KtExpression, KotlinType> noneApplicable,
112 @NotNull WritableSlice<KtExpression, ResolvedCall<FunctionDescriptor>> resolvedCallKey
113 ) {
114 OverloadResolutionResults<FunctionDescriptor> nextResolutionResults = fakeCallResolver.resolveFakeCall(
115 context, new TransientReceiver(iteratorType), name, loopRangeExpression
116 );
117 if (nextResolutionResults.isAmbiguity()) {
118 context.trace.report(ambiguity.on(loopRangeExpression, iteratorType));
119 }
120 else if (nextResolutionResults.isNothing()) {
121 context.trace.report(missing.on(loopRangeExpression, iteratorType));
122 }
123 else if (!nextResolutionResults.isSuccess()) {
124 context.trace.report(noneApplicable.on(loopRangeExpression, iteratorType));
125 }
126 else {
127 assert nextResolutionResults.isSuccess();
128 ResolvedCall<FunctionDescriptor> resolvedCall = nextResolutionResults.getResultingCall();
129 context.trace.record(resolvedCallKey, loopRangeExpression, resolvedCall);
130
131 FunctionDescriptor functionDescriptor = resolvedCall.getResultingDescriptor();
132 checkIfOperatorModifierPresent(loopRangeExpression, functionDescriptor, context.trace);
133
134 return functionDescriptor.getReturnType();
135 }
136 return null;
137 }
138 }