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        @NotNull
073        private Context self() {
074            //noinspection unchecked
075            return (Context) this;
076        }
077    
078        @NotNull
079        public Context replaceBindingTrace(@NotNull BindingTrace trace) {
080            if (this.trace == trace) return self();
081            return create(trace, scope, dataFlowInfo, expectedType, contextDependency, resolutionResultsCache, 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, contextDependency, resolutionResultsCache, labelResolver);
088        }
089    
090        @NotNull
091        public Context replaceExpectedType(@Nullable JetType newExpectedType) {
092            if (newExpectedType == null) return replaceExpectedType(TypeUtils.NO_EXPECTED_TYPE);
093            if (expectedType == newExpectedType) return self();
094            return create(trace, scope, dataFlowInfo, newExpectedType, contextDependency, resolutionResultsCache, labelResolver);
095        }
096    
097        @NotNull
098        public Context replaceScope(@NotNull JetScope newScope) {
099            if (newScope == scope) return self();
100            return create(trace, newScope, dataFlowInfo, expectedType, contextDependency, resolutionResultsCache, labelResolver);
101        }
102    
103        @NotNull
104        public Context replaceContextDependency(@NotNull ContextDependency newContextDependency) {
105            if (newContextDependency == contextDependency) return self();
106            return create(trace, scope, dataFlowInfo, expectedType, newContextDependency, resolutionResultsCache, labelResolver);
107        }
108    
109        @NotNull
110        public Context replaceResolutionResultsCache(@NotNull ResolutionResultsCache newResolutionResultsCache) {
111            if (newResolutionResultsCache == resolutionResultsCache) return self();
112            return create(trace, scope, dataFlowInfo, expectedType, contextDependency, newResolutionResultsCache, labelResolver);
113        }
114    
115        @NotNull
116        public Context replaceTraceAndCache(@NotNull TemporaryTraceAndCache traceAndCache) {
117            return replaceBindingTrace(traceAndCache.trace).replaceResolutionResultsCache(traceAndCache.cache);
118        }
119    }