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.intellij.util.SmartList;
020    import kotlin.jvm.functions.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.resolve.BindingTrace;
026    import org.jetbrains.kotlin.resolve.StatementFilter;
027    import org.jetbrains.kotlin.resolve.calls.checkers.CallChecker;
028    import org.jetbrains.kotlin.resolve.calls.context.*;
029    import org.jetbrains.kotlin.resolve.calls.model.MutableDataFlowInfoForArguments;
030    import org.jetbrains.kotlin.resolve.calls.model.MutableResolvedCall;
031    import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo;
032    import org.jetbrains.kotlin.resolve.scopes.LexicalScope;
033    import org.jetbrains.kotlin.types.KotlinType;
034    
035    import java.util.Collection;
036    
037    /**
038     * Stores candidates for call resolution.
039     */
040    public class ResolutionTask<D extends CallableDescriptor, F extends D> extends CallResolutionContext<ResolutionTask<D, F>> {
041        private final Function0<Collection<ResolutionCandidate<D>>> lazyCandidates;
042        private final Collection<MutableResolvedCall<F>> resolvedCalls;
043        public final TracingStrategy tracing;
044    
045        private ResolutionTask(
046                @NotNull Function0<Collection<ResolutionCandidate<D>>> lazyCandidates,
047                @NotNull TracingStrategy tracing,
048                @NotNull BindingTrace trace,
049                @NotNull LexicalScope scope,
050                @NotNull Call call,
051                @NotNull KotlinType expectedType,
052                @NotNull DataFlowInfo dataFlowInfo,
053                @NotNull ContextDependency contextDependency,
054                @NotNull CheckArgumentTypesMode checkArguments,
055                @NotNull ResolutionResultsCache resolutionResultsCache,
056                @Nullable MutableDataFlowInfoForArguments dataFlowInfoForArguments,
057                @NotNull CallChecker callChecker,
058                @NotNull StatementFilter statementFilter,
059                @NotNull Collection<MutableResolvedCall<F>> resolvedCalls,
060                boolean isAnnotationContext,
061                boolean collectAllCandidates
062        ) {
063            super(trace, scope, call, expectedType, dataFlowInfo, contextDependency, checkArguments, resolutionResultsCache,
064                  dataFlowInfoForArguments, callChecker, statementFilter, isAnnotationContext, collectAllCandidates);
065            this.lazyCandidates = lazyCandidates;
066            this.resolvedCalls = resolvedCalls;
067            this.tracing = tracing;
068        }
069    
070        public ResolutionTask(
071                @NotNull BasicCallResolutionContext context,
072                @NotNull TracingStrategy tracing,
073                @NotNull Function0<Collection<ResolutionCandidate<D>>> lazyCandidates
074        ) {
075            this(lazyCandidates, tracing,
076                 context.trace, context.scope, context.call,
077                 context.expectedType, context.dataFlowInfo, context.contextDependency, context.checkArguments,
078                 context.resolutionResultsCache, context.dataFlowInfoForArguments,
079                 context.callChecker,
080                 context.statementFilter, new SmartList<MutableResolvedCall<F>>(),
081                 context.isAnnotationContext, context.collectAllCandidates);
082        }
083    
084        @NotNull
085        public Collection<ResolutionCandidate<D>> getCandidates() {
086            return lazyCandidates.invoke();
087        }
088    
089        public void addResolvedCall(@NotNull MutableResolvedCall<F> resolvedCall) {
090            resolvedCalls.add(resolvedCall);
091        }
092    
093        @NotNull
094        public Collection<MutableResolvedCall<F>> getResolvedCalls() {
095            return resolvedCalls;
096        }
097    
098        @Override
099        protected ResolutionTask<D, F> create(
100                @NotNull BindingTrace trace,
101                @NotNull LexicalScope scope,
102                @NotNull DataFlowInfo dataFlowInfo,
103                @NotNull KotlinType expectedType,
104                @NotNull ContextDependency contextDependency,
105                @NotNull ResolutionResultsCache resolutionResultsCache,
106                @NotNull StatementFilter statementFilter,
107                boolean collectAllCandidates
108        ) {
109            return new ResolutionTask<D, F>(
110                    lazyCandidates, tracing, trace, scope, call, expectedType, dataFlowInfo, contextDependency, checkArguments,
111                    resolutionResultsCache, dataFlowInfoForArguments,
112                    callChecker,
113                    statementFilter, resolvedCalls,
114                    isAnnotationContext, collectAllCandidates);
115        }
116    
117        @Override
118        public String toString() {
119            return lazyCandidates.toString();
120        }
121    }