001    /*
002     * Copyright 2010-2013 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.jet.lang.resolve.calls.context;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.annotations.Nullable;
021    import org.jetbrains.jet.lang.resolve.BindingTrace;
022    import org.jetbrains.jet.lang.resolve.calls.autocasts.DataFlowInfo;
023    import org.jetbrains.jet.lang.resolve.scopes.JetScope;
024    import org.jetbrains.jet.lang.types.JetType;
025    import org.jetbrains.jet.lang.types.TypeUtils;
026    import org.jetbrains.jet.lang.types.expressions.LabelResolver;
027    
028    public abstract class ResolutionContext<Context extends ResolutionContext<Context>> {
029        public final BindingTrace trace;
030        public final JetScope scope;
031        public final JetType expectedType;
032        public final DataFlowInfo dataFlowInfo;
033        public final ExpressionPosition expressionPosition;
034        public final ContextDependency contextDependency;
035        public final ResolutionResultsCache resolutionResultsCache;
036        public final LabelResolver labelResolver;
037    
038        protected ResolutionContext(
039                @NotNull BindingTrace trace,
040                @NotNull JetScope scope,
041                @NotNull JetType expectedType,
042                @NotNull DataFlowInfo dataFlowInfo,
043                @NotNull ExpressionPosition expressionPosition,
044                @NotNull ContextDependency contextDependency,
045                @NotNull ResolutionResultsCache resolutionResultsCache,
046                @NotNull LabelResolver labelResolver
047        ) {
048            this.trace = trace;
049            this.scope = scope;
050            this.expectedType = expectedType;
051            this.dataFlowInfo = dataFlowInfo;
052            this.expressionPosition = expressionPosition;
053            this.contextDependency = contextDependency;
054            this.resolutionResultsCache = resolutionResultsCache;
055            this.labelResolver = labelResolver;
056        }
057    
058        protected abstract Context create(
059                @NotNull BindingTrace trace,
060                @NotNull JetScope scope,
061                @NotNull DataFlowInfo dataFlowInfo,
062                @NotNull JetType expectedType,
063                @NotNull ExpressionPosition expressionPosition,
064                @NotNull ContextDependency contextDependency,
065                @NotNull ResolutionResultsCache resolutionResultsCache,
066                @NotNull LabelResolver labelResolver
067        );
068        
069        protected abstract Context self();
070        
071        public Context replaceBindingTrace(@NotNull BindingTrace trace) {
072            if (this.trace == trace) return self();
073            return create(trace, scope, dataFlowInfo, expectedType, expressionPosition, contextDependency, resolutionResultsCache,
074                          labelResolver);
075        }
076    
077        @NotNull
078        public Context replaceExpressionPosition(@NotNull ExpressionPosition expressionPosition) {
079            if (expressionPosition == this.expressionPosition) return self();
080            return create(trace, scope, dataFlowInfo, expectedType, expressionPosition, contextDependency, resolutionResultsCache,
081                          labelResolver);
082        }
083    
084        @NotNull
085        public Context replaceDataFlowInfo(@NotNull DataFlowInfo newDataFlowInfo) {
086            if (newDataFlowInfo == dataFlowInfo) return self();
087            return create(trace, scope, newDataFlowInfo, expectedType, expressionPosition, contextDependency, resolutionResultsCache,
088                          labelResolver);
089        }
090    
091        @NotNull
092        public Context replaceExpectedType(@Nullable JetType newExpectedType) {
093            if (newExpectedType == null) return replaceExpectedType(TypeUtils.NO_EXPECTED_TYPE);
094            if (expectedType == newExpectedType) return self();
095            return create(trace, scope, dataFlowInfo, newExpectedType, expressionPosition, contextDependency, resolutionResultsCache,
096                          labelResolver);
097        }
098    
099        @NotNull
100        public Context replaceScope(@NotNull JetScope newScope) {
101            if (newScope == scope) return self();
102            return create(trace, newScope, dataFlowInfo, expectedType, expressionPosition, contextDependency, resolutionResultsCache,
103                          labelResolver);
104        }
105    
106        @NotNull
107        public Context replaceContextDependency(@NotNull ContextDependency newContextDependency) {
108            if (newContextDependency == contextDependency) return self();
109            return create(trace, scope, dataFlowInfo, expectedType, expressionPosition, newContextDependency, resolutionResultsCache,
110                          labelResolver);
111        }
112    
113        @NotNull
114        public Context replaceResolutionResultsCache(@NotNull ResolutionResultsCache newResolutionResultsCache) {
115            if (newResolutionResultsCache == resolutionResultsCache) return self();
116            return create(trace, scope, dataFlowInfo, expectedType, expressionPosition, contextDependency, newResolutionResultsCache,
117                          labelResolver);
118        }
119    
120        public Context replaceTraceAndCache(@NotNull TemporaryTraceAndCache traceAndCache) {
121            return replaceBindingTrace(traceAndCache.trace).replaceResolutionResultsCache(traceAndCache.cache);
122        }
123    }