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