001    /*
002     * Copyright 2010-2016 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.jvm.functions.Function1;
020    import org.jetbrains.annotations.NotNull;
021    import org.jetbrains.kotlin.psi.KtExpression;
022    import org.jetbrains.kotlin.resolve.BindingTrace;
023    import org.jetbrains.kotlin.resolve.StatementFilter;
024    import org.jetbrains.kotlin.resolve.calls.context.*;
025    import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo;
026    import org.jetbrains.kotlin.resolve.scopes.LexicalScope;
027    import org.jetbrains.kotlin.types.KotlinType;
028    
029    public class ExpressionTypingContext extends ResolutionContext<ExpressionTypingContext> {
030    
031        @NotNull
032        public static ExpressionTypingContext newContext(
033                @NotNull BindingTrace trace,
034                @NotNull LexicalScope scope,
035                @NotNull DataFlowInfo dataFlowInfo,
036                @NotNull KotlinType expectedType
037        ) {
038            return newContext(trace, scope, dataFlowInfo, expectedType, ContextDependency.INDEPENDENT, StatementFilter.NONE);
039        }
040    
041        @NotNull
042        public static ExpressionTypingContext newContext(
043                @NotNull BindingTrace trace,
044                @NotNull LexicalScope scope,
045                @NotNull DataFlowInfo dataFlowInfo,
046                @NotNull KotlinType expectedType,
047                @NotNull ContextDependency contextDependency,
048                @NotNull StatementFilter statementFilter
049        ) {
050            return newContext(trace, scope, dataFlowInfo, expectedType, contextDependency,
051                              new ResolutionResultsCacheImpl(), statementFilter, false);
052        }
053    
054        @NotNull
055        public static ExpressionTypingContext newContext(@NotNull ResolutionContext context) {
056            return new ExpressionTypingContext(
057                    context.trace, context.scope, context.dataFlowInfo, context.expectedType,
058                    context.contextDependency, context.resolutionResultsCache,
059                    context.statementFilter,
060                    context.isAnnotationContext, context.isDebuggerContext, context.collectAllCandidates,
061                    context.callPosition, context.expressionContextProvider);
062        }
063    
064        @NotNull
065        public static ExpressionTypingContext newContext(@NotNull ResolutionContext context, boolean isDebuggerContext) {
066            return new ExpressionTypingContext(
067                    context.trace, context.scope, context.dataFlowInfo, context.expectedType,
068                    context.contextDependency, context.resolutionResultsCache,
069                    context.statementFilter,
070                    context.isAnnotationContext, isDebuggerContext, context.collectAllCandidates,
071                    context.callPosition, context.expressionContextProvider);
072        }
073    
074        @NotNull
075        public static ExpressionTypingContext newContext(
076                @NotNull BindingTrace trace,
077                @NotNull LexicalScope scope,
078                @NotNull DataFlowInfo dataFlowInfo,
079                @NotNull KotlinType expectedType,
080                @NotNull ContextDependency contextDependency,
081                @NotNull ResolutionResultsCache resolutionResultsCache,
082                @NotNull StatementFilter statementFilter,
083                boolean isAnnotationContext
084        ) {
085            return new ExpressionTypingContext(
086                    trace, scope, dataFlowInfo, expectedType, contextDependency, resolutionResultsCache,
087                    statementFilter, isAnnotationContext, false, false, CallPosition.Unknown.INSTANCE, DEFAULT_EXPRESSION_CONTEXT_PROVIDER);
088        }
089    
090        private ExpressionTypingContext(
091                @NotNull BindingTrace trace,
092                @NotNull LexicalScope scope,
093                @NotNull DataFlowInfo dataFlowInfo,
094                @NotNull KotlinType expectedType,
095                @NotNull ContextDependency contextDependency,
096                @NotNull ResolutionResultsCache resolutionResultsCache,
097                @NotNull StatementFilter statementFilter,
098                boolean isAnnotationContext,
099                boolean isDebuggerContext,
100                boolean collectAllCandidates,
101                @NotNull CallPosition callPosition,
102                @NotNull Function1<KtExpression, KtExpression> expressionContextProvider
103        ) {
104            super(trace, scope, expectedType, dataFlowInfo, contextDependency, resolutionResultsCache,
105                  statementFilter, isAnnotationContext, isDebuggerContext, collectAllCandidates, callPosition, expressionContextProvider);
106        }
107    
108        @Override
109        protected ExpressionTypingContext create(
110                @NotNull BindingTrace trace,
111                @NotNull LexicalScope scope,
112                @NotNull DataFlowInfo dataFlowInfo,
113                @NotNull KotlinType expectedType,
114                @NotNull ContextDependency contextDependency,
115                @NotNull ResolutionResultsCache resolutionResultsCache,
116                @NotNull StatementFilter statementFilter,
117                boolean collectAllCandidates,
118                @NotNull CallPosition callPosition,
119                @NotNull Function1<KtExpression, KtExpression> expressionContextProvider
120        ) {
121            return new ExpressionTypingContext(trace, scope, dataFlowInfo,
122                                               expectedType, contextDependency, resolutionResultsCache,
123                                               statementFilter, isAnnotationContext, isDebuggerContext,
124                                               collectAllCandidates, callPosition, expressionContextProvider);
125        }
126    }