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.context;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.annotations.Nullable;
021    import org.jetbrains.kotlin.descriptors.CallableDescriptor;
022    import org.jetbrains.kotlin.psi.Call;
023    import org.jetbrains.kotlin.resolve.BindingTrace;
024    import org.jetbrains.kotlin.resolve.StatementFilter;
025    import org.jetbrains.kotlin.resolve.calls.checkers.CallChecker;
026    import org.jetbrains.kotlin.resolve.calls.model.MutableDataFlowInfoForArguments;
027    import org.jetbrains.kotlin.resolve.calls.model.MutableResolvedCall;
028    import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo;
029    import org.jetbrains.kotlin.resolve.calls.tasks.TracingStrategy;
030    import org.jetbrains.kotlin.resolve.scopes.JetScope;
031    import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue;
032    import org.jetbrains.kotlin.types.JetType;
033    
034    public final class CallCandidateResolutionContext<D extends CallableDescriptor> extends CallResolutionContext<CallCandidateResolutionContext<D>> {
035        @NotNull
036        public final MutableResolvedCall<D> candidateCall;
037        @NotNull
038        public final TracingStrategy tracing;
039        @NotNull
040        public final ReceiverValue explicitExtensionReceiverForInvoke;
041    
042        private CallCandidateResolutionContext(
043                @NotNull MutableResolvedCall<D> candidateCall,
044                @NotNull TracingStrategy tracing,
045                @NotNull BindingTrace trace,
046                @NotNull JetScope scope,
047                @NotNull Call call,
048                @NotNull JetType expectedType,
049                @NotNull DataFlowInfo dataFlowInfo,
050                @NotNull ContextDependency contextDependency,
051                @NotNull CheckValueArgumentsMode checkArguments,
052                @NotNull ResolutionResultsCache resolutionResultsCache,
053                @Nullable MutableDataFlowInfoForArguments dataFlowInfoForArguments,
054                @NotNull CallChecker callChecker,
055                @NotNull StatementFilter statementFilter,
056                @NotNull ReceiverValue explicitExtensionReceiverForInvoke,
057                boolean isAnnotationContext,
058                boolean collectAllCandidates
059        ) {
060            super(trace, scope, call, expectedType, dataFlowInfo, contextDependency, checkArguments, resolutionResultsCache,
061                  dataFlowInfoForArguments, callChecker, statementFilter, isAnnotationContext, collectAllCandidates);
062            this.candidateCall = candidateCall;
063            this.tracing = tracing;
064            this.explicitExtensionReceiverForInvoke = explicitExtensionReceiverForInvoke;
065        }
066    
067        public static <D extends CallableDescriptor> CallCandidateResolutionContext<D> create(
068                @NotNull MutableResolvedCall<D> candidateCall, @NotNull CallResolutionContext<?> context, @NotNull BindingTrace trace,
069                @NotNull TracingStrategy tracing, @NotNull Call call, @NotNull ReceiverValue explicitExtensionReceiverForInvoke
070        ) {
071            candidateCall.getDataFlowInfoForArguments().setInitialDataFlowInfo(context.dataFlowInfo);
072            return new CallCandidateResolutionContext<D>(
073                    candidateCall, tracing, trace, context.scope, call, context.expectedType,
074                    context.dataFlowInfo, context.contextDependency, context.checkArguments,
075                    context.resolutionResultsCache, context.dataFlowInfoForArguments,
076                    context.callChecker, context.statementFilter, explicitExtensionReceiverForInvoke,
077                    context.isAnnotationContext, context.collectAllCandidates);
078        }
079    
080        public static <D extends CallableDescriptor> CallCandidateResolutionContext<D> create(
081                @NotNull MutableResolvedCall<D> candidateCall, @NotNull CallResolutionContext<?> context, @NotNull BindingTrace trace,
082                @NotNull TracingStrategy tracing, @NotNull Call call
083        ) {
084            return create(candidateCall, context, trace, tracing, call, ReceiverValue.NO_RECEIVER);
085        }
086    
087        public static <D extends CallableDescriptor> CallCandidateResolutionContext<D> create(
088                @NotNull MutableResolvedCall<D> candidateCall, @NotNull CallResolutionContext<?> context, @NotNull BindingTrace trace,
089                @NotNull TracingStrategy tracing) {
090            return create(candidateCall, context, trace, tracing, context.call);
091        }
092    
093        @NotNull
094        public static <D extends CallableDescriptor> CallCandidateResolutionContext<D> createForCallBeingAnalyzed(
095                @NotNull MutableResolvedCall<D> candidateCall, @NotNull BasicCallResolutionContext context, @NotNull TracingStrategy tracing
096        ) {
097            return new CallCandidateResolutionContext<D>(
098                    candidateCall, tracing, context.trace, context.scope, context.call, context.expectedType,
099                    context.dataFlowInfo, context.contextDependency, context.checkArguments, context.resolutionResultsCache,
100                    context.dataFlowInfoForArguments, context.callChecker, context.statementFilter, ReceiverValue.NO_RECEIVER,
101                    context.isAnnotationContext, context.collectAllCandidates);
102        }
103    
104        @Override
105        protected CallCandidateResolutionContext<D> create(
106                @NotNull BindingTrace trace,
107                @NotNull JetScope scope,
108                @NotNull DataFlowInfo dataFlowInfo,
109                @NotNull JetType expectedType,
110                @NotNull ContextDependency contextDependency,
111                @NotNull ResolutionResultsCache resolutionResultsCache,
112                @NotNull StatementFilter statementFilter,
113                boolean collectAllCandidates
114        ) {
115            return new CallCandidateResolutionContext<D>(
116                    candidateCall, tracing, trace, scope, call, expectedType, dataFlowInfo, contextDependency, checkArguments,
117                    resolutionResultsCache, dataFlowInfoForArguments, callChecker, statementFilter,
118                    explicitExtensionReceiverForInvoke, isAnnotationContext, collectAllCandidates);
119        }
120    }