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                boolean insideSafeCallChain
063        ) {
064            super(trace, scope, call, expectedType, dataFlowInfo, contextDependency, checkArguments, resolutionResultsCache,
065                  dataFlowInfoForArguments, callChecker, statementFilter, isAnnotationContext, collectAllCandidates, insideSafeCallChain);
066            this.lazyCandidates = lazyCandidates;
067            this.resolvedCalls = resolvedCalls;
068            this.tracing = tracing;
069        }
070    
071        public ResolutionTask(
072                @NotNull BasicCallResolutionContext context,
073                @NotNull TracingStrategy tracing,
074                @NotNull Function0<Collection<ResolutionCandidate<D>>> lazyCandidates
075        ) {
076            this(lazyCandidates, tracing,
077                 context.trace, context.scope, context.call,
078                 context.expectedType, context.dataFlowInfo, context.contextDependency, context.checkArguments,
079                 context.resolutionResultsCache, context.dataFlowInfoForArguments,
080                 context.callChecker,
081                 context.statementFilter, new SmartList<MutableResolvedCall<F>>(),
082                 context.isAnnotationContext, context.collectAllCandidates, context.insideCallChain);
083        }
084    
085        @NotNull
086        public Collection<ResolutionCandidate<D>> getCandidates() {
087            return lazyCandidates.invoke();
088        }
089    
090        public void addResolvedCall(@NotNull MutableResolvedCall<F> resolvedCall) {
091            resolvedCalls.add(resolvedCall);
092        }
093    
094        @NotNull
095        public Collection<MutableResolvedCall<F>> getResolvedCalls() {
096            return resolvedCalls;
097        }
098    
099        @Override
100        protected ResolutionTask<D, F> create(
101                @NotNull BindingTrace trace,
102                @NotNull LexicalScope scope,
103                @NotNull DataFlowInfo dataFlowInfo,
104                @NotNull KotlinType expectedType,
105                @NotNull ContextDependency contextDependency,
106                @NotNull ResolutionResultsCache resolutionResultsCache,
107                @NotNull StatementFilter statementFilter,
108                boolean collectAllCandidates,
109                boolean insideSafeCallChain
110        ) {
111            return new ResolutionTask<D, F>(
112                    lazyCandidates, tracing, trace, scope, call, expectedType, dataFlowInfo, contextDependency, checkArguments,
113                    resolutionResultsCache, dataFlowInfoForArguments,
114                    callChecker,
115                    statementFilter, resolvedCalls,
116                    isAnnotationContext, collectAllCandidates, insideSafeCallChain);
117        }
118    
119        @Override
120        public String toString() {
121            return lazyCandidates.toString();
122        }
123    }