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.tasks;
018    
019    import com.google.common.collect.Lists;
020    import kotlin.Function0;
021    import org.jetbrains.annotations.NotNull;
022    import org.jetbrains.annotations.Nullable;
023    import org.jetbrains.kotlin.descriptors.CallableDescriptor;
024    import org.jetbrains.kotlin.psi.Call;
025    import org.jetbrains.kotlin.psi.JetReferenceExpression;
026    import org.jetbrains.kotlin.resolve.BindingTrace;
027    import org.jetbrains.kotlin.resolve.StatementFilter;
028    import org.jetbrains.kotlin.resolve.calls.checkers.AdditionalTypeChecker;
029    import org.jetbrains.kotlin.resolve.calls.context.*;
030    import org.jetbrains.kotlin.resolve.calls.checkers.CallChecker;
031    import org.jetbrains.kotlin.resolve.calls.model.MutableDataFlowInfoForArguments;
032    import org.jetbrains.kotlin.resolve.calls.model.MutableResolvedCall;
033    import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo;
034    import org.jetbrains.kotlin.resolve.scopes.JetScope;
035    import org.jetbrains.kotlin.types.JetType;
036    
037    import java.util.Collection;
038    
039    /**
040     * Stores candidates for call resolution.
041     */
042    public class ResolutionTask<D extends CallableDescriptor, F extends D> extends CallResolutionContext<ResolutionTask<D, F>> {
043        private final Function0<Collection<ResolutionCandidate<D>>> lazyCandidates;
044        private final Collection<MutableResolvedCall<F>> resolvedCalls;
045        public final TracingStrategy tracing;
046    
047        private ResolutionTask(
048                @NotNull Function0<Collection<ResolutionCandidate<D>>> lazyCandidates,
049                @NotNull TracingStrategy tracing,
050                @NotNull BindingTrace trace,
051                @NotNull JetScope scope,
052                @NotNull Call call,
053                @NotNull JetType expectedType,
054                @NotNull DataFlowInfo dataFlowInfo,
055                @NotNull ContextDependency contextDependency,
056                @NotNull CheckValueArgumentsMode checkArguments,
057                @NotNull ResolutionResultsCache resolutionResultsCache,
058                @Nullable MutableDataFlowInfoForArguments dataFlowInfoForArguments,
059                @NotNull CallChecker callChecker,
060                @NotNull AdditionalTypeChecker additionalTypeChecker,
061                @NotNull StatementFilter statementFilter,
062                @NotNull Collection<MutableResolvedCall<F>> resolvedCalls,
063                boolean isAnnotationContext,
064                boolean collectAllCandidates
065        ) {
066            super(trace, scope, call, expectedType, dataFlowInfo, contextDependency, checkArguments, resolutionResultsCache,
067                  dataFlowInfoForArguments, callChecker, additionalTypeChecker, statementFilter, isAnnotationContext, collectAllCandidates);
068            this.lazyCandidates = lazyCandidates;
069            this.resolvedCalls = resolvedCalls;
070            this.tracing = tracing;
071        }
072    
073        public ResolutionTask(
074                @NotNull BasicCallResolutionContext context,
075                @NotNull TracingStrategy tracing,
076                @NotNull Function0<Collection<ResolutionCandidate<D>>> lazyCandidates
077        ) {
078            this(lazyCandidates, tracing,
079                 context.trace, context.scope, context.call,
080                 context.expectedType, context.dataFlowInfo, context.contextDependency, context.checkArguments,
081                 context.resolutionResultsCache, context.dataFlowInfoForArguments,
082                 context.callChecker, context.additionalTypeChecker, context.statementFilter, Lists.<MutableResolvedCall<F>>newArrayList(),
083                 context.isAnnotationContext, context.collectAllCandidates);
084        }
085    
086        public ResolutionTask(
087                @NotNull final Collection<ResolutionCandidate<D>> candidates,
088                @NotNull JetReferenceExpression reference,
089                @NotNull BasicCallResolutionContext context
090        ) {
091            this(context, TracingStrategyImpl.create(reference, context.call), new Function0<Collection<ResolutionCandidate<D>>>() {
092                @Override
093                public Collection<ResolutionCandidate<D>> invoke() {
094                    return candidates;
095                }
096            });
097        }
098    
099        @NotNull
100        public Collection<ResolutionCandidate<D>> getCandidates() {
101            return lazyCandidates.invoke();
102        }
103    
104        public void addResolvedCall(@NotNull MutableResolvedCall<F> resolvedCall) {
105            resolvedCalls.add(resolvedCall);
106        }
107    
108        @NotNull
109        public Collection<MutableResolvedCall<F>> getResolvedCalls() {
110            return resolvedCalls;
111        }
112    
113        @Override
114        protected ResolutionTask<D, F> create(
115                @NotNull BindingTrace trace,
116                @NotNull JetScope scope,
117                @NotNull DataFlowInfo dataFlowInfo,
118                @NotNull JetType expectedType,
119                @NotNull ContextDependency contextDependency,
120                @NotNull ResolutionResultsCache resolutionResultsCache,
121                @NotNull StatementFilter statementFilter,
122                boolean collectAllCandidates
123        ) {
124            return new ResolutionTask<D, F>(
125                    lazyCandidates, tracing, trace, scope, call, expectedType, dataFlowInfo, contextDependency, checkArguments,
126                    resolutionResultsCache, dataFlowInfoForArguments, callChecker, additionalTypeChecker, statementFilter, resolvedCalls,
127                    isAnnotationContext, collectAllCandidates);
128        }
129    
130        public ResolutionTask<D, F> replaceContext(@NotNull BasicCallResolutionContext newContext) {
131            return new ResolutionTask<D, F>(newContext, tracing, lazyCandidates);
132        }
133    
134        public ResolutionTask<D, F> replaceCall(@NotNull Call newCall) {
135            return new ResolutionTask<D, F>(
136                    lazyCandidates, tracing, trace, scope, newCall, expectedType, dataFlowInfo, contextDependency, checkArguments,
137                    resolutionResultsCache, dataFlowInfoForArguments, callChecker, additionalTypeChecker, statementFilter, resolvedCalls,
138                    isAnnotationContext, collectAllCandidates);
139        }
140    
141        public interface DescriptorCheckStrategy {
142            <D extends CallableDescriptor> boolean performAdvancedChecks(D descriptor, BindingTrace trace, TracingStrategy tracing);
143        }
144    
145        @Override
146        public String toString() {
147            return lazyCandidates.toString();
148        }
149    }