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