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.descriptors.CallableDescriptor;
022    import org.jetbrains.jet.lang.psi.CallKey;
023    import org.jetbrains.jet.lang.psi.JetExpression;
024    import org.jetbrains.jet.lang.resolve.DelegatingBindingTrace;
025    import org.jetbrains.jet.lang.resolve.calls.results.OverloadResolutionResultsImpl;
026    
027    public class TemporaryResolutionResultsCache implements ResolutionResultsCache {
028        private final ResolutionResultsCache parentCache;
029        private final ResolutionResultsCacheImpl innerCache;
030    
031        public TemporaryResolutionResultsCache(@NotNull ResolutionResultsCache parentCache) {
032            assert parentCache instanceof ResolutionResultsCacheImpl || parentCache instanceof TemporaryResolutionResultsCache :
033                "Unsupported parent cache: " + parentCache;
034            this.parentCache = parentCache;
035            this.innerCache = new ResolutionResultsCacheImpl();
036        }
037    
038        @Override
039        public <D extends CallableDescriptor> void recordResolutionResults(
040                @NotNull CallKey callKey,
041                @NotNull OverloadResolutionResultsImpl<D> results
042        ) {
043            innerCache.recordResolutionResults(callKey, results);
044        }
045    
046        @Nullable
047        @Override
048        public <D extends CallableDescriptor> OverloadResolutionResultsImpl<D> getResolutionResults(
049                @NotNull CallKey callKey
050        ) {
051            OverloadResolutionResultsImpl<D> results = innerCache.getResolutionResults(callKey);
052            if (results != null) {
053                return results;
054            }
055            return parentCache.getResolutionResults(callKey);
056        }
057    
058        @Override
059        public void recordResolutionTrace(
060                @NotNull CallKey callKey, @NotNull DelegatingBindingTrace delegatingTrace
061        ) {
062            innerCache.recordResolutionTrace(callKey, delegatingTrace);
063        }
064    
065        @Nullable
066        @Override
067        public DelegatingBindingTrace getResolutionTrace(@NotNull CallKey callKey) {
068            DelegatingBindingTrace trace = innerCache.getResolutionTrace(callKey);
069            if (trace != null) {
070                return trace;
071            }
072            return parentCache.getResolutionTrace(callKey);
073        }
074    
075        @Override
076        public <D extends CallableDescriptor> void recordDeferredComputationForCall(
077                @NotNull CallKey callKey,
078                @NotNull CallCandidateResolutionContext<D> deferredComputation
079        ) {
080            innerCache.recordDeferredComputationForCall(callKey, deferredComputation);
081        }
082    
083        @Nullable
084        @Override
085        public CallCandidateResolutionContext<?> getDeferredComputation(@Nullable JetExpression expression) {
086            CallCandidateResolutionContext<?> computation = innerCache.getDeferredComputation(expression);
087            if (computation != null) {
088                return computation;
089            }
090            return parentCache.getDeferredComputation(expression);
091        }
092    
093        public void commit() {
094            if (parentCache instanceof ResolutionResultsCacheImpl) {
095                ((ResolutionResultsCacheImpl) parentCache).addData(innerCache);
096                return;
097            }
098            assert parentCache instanceof TemporaryResolutionResultsCache;
099            ((TemporaryResolutionResultsCache) parentCache).innerCache.addData(innerCache);
100        }
101    }