Refactored ResolutionResultsCacheImpl

Used map instead of trace to store data
Rewrote it to Kotlin
This commit is contained in:
Svetlana Isakova
2014-04-28 16:31:36 +04:00
parent 5a84ed4497
commit 3df6eb8ffa
5 changed files with 76 additions and 117 deletions
@@ -60,7 +60,7 @@ public class BasicCallResolutionContext extends CallResolutionContext<BasicCallR
boolean isAnnotationContext
) {
return new BasicCallResolutionContext(trace, scope, call, expectedType, dataFlowInfo, contextDependency, checkArguments,
ResolutionResultsCacheImpl.create(), LabelResolver.create(), null,
new ResolutionResultsCacheImpl(), LabelResolver.create(), null,
callResolverExtension, isAnnotationContext, false);
}
@@ -1,114 +0,0 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.lang.resolve.calls.context;
import com.google.common.collect.Lists;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.CallableDescriptor;
import org.jetbrains.jet.lang.psi.CallKey;
import org.jetbrains.jet.lang.psi.JetExpression;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.DelegatingBindingTrace;
import org.jetbrains.jet.lang.resolve.calls.results.OverloadResolutionResultsImpl;
import org.jetbrains.jet.util.slicedmap.BasicWritableSlice;
import org.jetbrains.jet.util.slicedmap.Slices;
import org.jetbrains.jet.util.slicedmap.WritableSlice;
import static org.jetbrains.jet.lang.psi.Call.CallType;
import static org.jetbrains.jet.lang.psi.Call.CallType.*;
public class ResolutionResultsCacheImpl implements ResolutionResultsCache {
public static final WritableSlice<CallKey, OverloadResolutionResultsImpl<CallableDescriptor>> RESOLUTION_RESULTS = Slices.createSimpleSlice();
public static final WritableSlice<CallKey, DelegatingBindingTrace> TRACE_DELTAS_CACHE = Slices.createSimpleSlice();
public static final WritableSlice<CallKey, CallCandidateResolutionContext<?>> DEFERRED_COMPUTATION_FOR_CALL = Slices.createSimpleSlice();
static {
BasicWritableSlice.initSliceDebugNames(ResolutionResultsCacheImpl.class);
}
private final DelegatingBindingTrace trace = new DelegatingBindingTrace(
BindingContext.EMPTY, "Internal binding context in resolution results cache");
@Override
public <D extends CallableDescriptor> void recordResolutionResults(
@NotNull CallKey callKey,
@NotNull OverloadResolutionResultsImpl<D> results
) {
//noinspection unchecked
trace.record(RESOLUTION_RESULTS, callKey, (OverloadResolutionResultsImpl<CallableDescriptor>) results);
}
@Override
@Nullable
public <D extends CallableDescriptor> OverloadResolutionResultsImpl<D> getResolutionResults(
@NotNull CallKey callKey
) {
//noinspection unchecked
return (OverloadResolutionResultsImpl<D>) trace.get(RESOLUTION_RESULTS, callKey);
}
@Override
public void recordResolutionTrace(@NotNull CallKey callKey, @NotNull DelegatingBindingTrace delegatingTrace) {
trace.record(TRACE_DELTAS_CACHE, callKey, delegatingTrace);
}
@Override
@Nullable
public DelegatingBindingTrace getResolutionTrace(@NotNull CallKey callKey) {
return trace.get(TRACE_DELTAS_CACHE, callKey);
}
@Override
public <D extends CallableDescriptor> void recordDeferredComputationForCall(
@NotNull CallKey callKey,
@NotNull CallCandidateResolutionContext<D> deferredComputation
) {
trace.record(DEFERRED_COMPUTATION_FOR_CALL, callKey, deferredComputation);
}
@Override
@Nullable
public CallCandidateResolutionContext<?> getDeferredComputation(@Nullable JetExpression expression) {
return getValueTryingAllCallTypes(expression, DEFERRED_COMPUTATION_FOR_CALL);
}
@Nullable
private <T> T getValueTryingAllCallTypes(
@Nullable JetExpression expression,
@NotNull WritableSlice<CallKey, T> slice
) {
if (expression == null) return null;
for (CallType callType : Lists.newArrayList(DEFAULT, ARRAY_GET_METHOD, ARRAY_SET_METHOD, INVOKE)) {
CallKey callKey = CallKey.create(callType, expression);
T context = trace.get(slice, callKey);
if (context != null) {
return context;
}
}
return null;
}
@NotNull
public static ResolutionResultsCache create() {
return new ResolutionResultsCacheImpl();
}
/*package*/ void addData(@NotNull ResolutionResultsCacheImpl cache) {
cache.trace.addAllMyDataTo(this.trace);
}
}
@@ -0,0 +1,73 @@
/*
* Copyright 2010-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.lang.resolve.calls.context
import org.jetbrains.jet.lang.descriptors.CallableDescriptor
import org.jetbrains.jet.lang.psi.CallKey
import org.jetbrains.jet.lang.resolve.calls.results.OverloadResolutionResultsImpl
import org.jetbrains.jet.lang.resolve.DelegatingBindingTrace
import org.jetbrains.jet.lang.psi.JetExpression
import java.util.HashMap
import org.jetbrains.jet.lang.psi.Call.CallType.*
class ResolutionResultsCacheImpl : ResolutionResultsCache {
private class CachedData(
var results: OverloadResolutionResultsImpl<*>? = null,
var resolutionTrace: DelegatingBindingTrace? = null,
var deferredComputation: CallCandidateResolutionContext<*>? = null
)
private val data = HashMap<CallKey, CachedData>()
private fun getOrCreateCachedInfo(callKey: CallKey) = data.getOrPut(callKey, { CachedData() })
override fun <D : CallableDescriptor?> recordResolutionResults(callKey: CallKey, results: OverloadResolutionResultsImpl<D>) {
getOrCreateCachedInfo(callKey).results = results
}
override fun <D : CallableDescriptor?> getResolutionResults(callKey: CallKey): OverloadResolutionResultsImpl<D>? {
return data[callKey]?.results as OverloadResolutionResultsImpl<D>?
}
override fun recordResolutionTrace(callKey: CallKey, delegatingTrace: DelegatingBindingTrace) {
getOrCreateCachedInfo(callKey).resolutionTrace = delegatingTrace
}
override fun getResolutionTrace(callKey: CallKey): DelegatingBindingTrace? {
return data[callKey]?.resolutionTrace
}
override fun <D : CallableDescriptor?> recordDeferredComputationForCall(callKey: CallKey, deferredComputation: CallCandidateResolutionContext<D>) {
getOrCreateCachedInfo(callKey).deferredComputation = deferredComputation
}
override fun getDeferredComputation(expression: JetExpression?): CallCandidateResolutionContext<out CallableDescriptor?>? {
if (expression == null) return null
for (callType in listOf(DEFAULT, ARRAY_GET_METHOD, ARRAY_SET_METHOD, INVOKE)) {
val deferredComputation = data[CallKey.create(callType, expression)]?.deferredComputation
if (deferredComputation != null) {
return deferredComputation
}
}
return null
}
fun addData(cache: ResolutionResultsCacheImpl) {
data.putAll(cache.data)
}
}
@@ -49,7 +49,7 @@ public class SimpleResolutionContext extends ResolutionContext<SimpleResolutionC
@NotNull ContextDependency contextDependency,
@NotNull CallResolverExtension callResolverExtension
) {
this(trace, scope, expectedType, dataFlowInfo, contextDependency, ResolutionResultsCacheImpl.create(),
this(trace, scope, expectedType, dataFlowInfo, contextDependency, new ResolutionResultsCacheImpl(),
LabelResolver.create(), callResolverExtension, false, false);
}
@@ -40,7 +40,7 @@ public class ExpressionTypingContext extends ResolutionContext<ExpressionTypingC
@NotNull JetType expectedType
) {
return newContext(trace, scope, dataFlowInfo, expectedType,
ContextDependency.INDEPENDENT, ResolutionResultsCacheImpl.create(), LabelResolver.create(),
ContextDependency.INDEPENDENT, new ResolutionResultsCacheImpl(), LabelResolver.create(),
expressionTypingServices.createExtension(scope, false), false);
}