DelegatingBindingTrace: J2K

This commit is contained in:
Dmitry Jemerov
2016-09-23 13:55:08 +02:00
parent 9e6f03cd94
commit bcde67dc91
@@ -14,179 +14,131 @@
* limitations under the License. * limitations under the License.
*/ */
package org.jetbrains.kotlin.resolve; package org.jetbrains.kotlin.resolve
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap
import com.google.common.collect.Lists; import org.jetbrains.annotations.TestOnly
import com.google.common.collect.Maps; import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.annotations.Nullable; import org.jetbrains.kotlin.resolve.diagnostics.Diagnostics
import org.jetbrains.annotations.TestOnly; import org.jetbrains.kotlin.resolve.diagnostics.MutableDiagnosticsWithSuppression
import org.jetbrains.kotlin.diagnostics.Diagnostic; import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.psi.KtExpression; import org.jetbrains.kotlin.types.expressions.typeInfoFactory.createTypeInfo
import org.jetbrains.kotlin.resolve.diagnostics.Diagnostics; import org.jetbrains.kotlin.util.slicedMap.*
import org.jetbrains.kotlin.resolve.diagnostics.MutableDiagnosticsWithSuppression;
import org.jetbrains.kotlin.types.KotlinType;
import org.jetbrains.kotlin.types.expressions.KotlinTypeInfo;
import org.jetbrains.kotlin.types.expressions.typeInfoFactory.TypeInfoFactoryKt;
import org.jetbrains.kotlin.util.slicedMap.*;
import java.util.Collection; open class DelegatingBindingTrace(private val parentContext: BindingContext,
import java.util.List; withParentDiagnostics: Boolean,
import java.util.Map; private val name: String) : BindingTrace {
private val map = if (BindingTraceContext.TRACK_REWRITES) TrackingSlicedMap(BindingTraceContext.TRACK_WITH_STACK_TRACES) else SlicedMapImpl.create()
private val mutableDiagnostics: MutableDiagnosticsWithSuppression
public class DelegatingBindingTrace implements BindingTrace { private inner class MyBindingContext : BindingContext {
@SuppressWarnings("ConstantConditions") override fun getDiagnostics(): Diagnostics = mutableDiagnostics
private final MutableSlicedMap map = BindingTraceContext.TRACK_REWRITES ? new TrackingSlicedMap(BindingTraceContext.TRACK_WITH_STACK_TRACES) : SlicedMapImpl.create();
private final BindingContext parentContext; override fun <K, V> get(slice: ReadOnlySlice<K, V>, key: K): V? {
private final String name; return this@DelegatingBindingTrace.get(slice, key)
private final MutableDiagnosticsWithSuppression mutableDiagnostics;
private final BindingContext bindingContext = new BindingContext() {
@NotNull
@Override
public Diagnostics getDiagnostics() {
return mutableDiagnostics;
} }
@Override override fun getType(expression: KtExpression): KotlinType? {
public <K, V> V get(ReadOnlySlice<K, V> slice, K key) { return this@DelegatingBindingTrace.getType(expression)
return DelegatingBindingTrace.this.get(slice, key);
} }
override fun <K, V> getKeys(slice: WritableSlice<K, V>): Collection<K> {
@Nullable return this@DelegatingBindingTrace.getKeys(slice)
@Override
public KotlinType getType(@NotNull KtExpression expression) {
return DelegatingBindingTrace.this.getType(expression);
} }
@NotNull override fun addOwnDataTo(trace: BindingTrace, commitDiagnostics: Boolean) {
@Override BindingContextUtils.addOwnDataTo(trace, null, commitDiagnostics, map, mutableDiagnostics)
public <K, V> Collection<K> getKeys(WritableSlice<K, V> slice) {
return DelegatingBindingTrace.this.getKeys(slice);
} }
@Override
public void addOwnDataTo(@NotNull BindingTrace trace, boolean commitDiagnostics) {
BindingContextUtils.addOwnDataTo(trace, null, commitDiagnostics, map, mutableDiagnostics);
}
@NotNull
@TestOnly @TestOnly
@Override override fun <K, V> getSliceContents(slice: ReadOnlySlice<K, V>): ImmutableMap<K, V> {
public <K, V> ImmutableMap<K, V> getSliceContents(@NotNull ReadOnlySlice<K, V> slice) { return ImmutableMap.copyOf(parentContext.getSliceContents(slice) + map.getSliceContents(slice))
Map<K, V> result = Maps.newHashMap();
result.putAll(parentContext.getSliceContents(slice));
result.putAll(map.getSliceContents(slice));
return ImmutableMap.copyOf(result);
} }
};
public DelegatingBindingTrace(BindingContext parentContext, String debugName) {
this(parentContext, true, debugName);
} }
public DelegatingBindingTrace(BindingContext parentContext, boolean withParentDiagnostics, String debugName) { private val bindingContext = MyBindingContext()
this.parentContext = parentContext;
this.name = debugName; constructor(parentContext: BindingContext, debugName: String) : this(parentContext, true, debugName) {
this.mutableDiagnostics = withParentDiagnostics ?
new MutableDiagnosticsWithSuppression(bindingContext, parentContext.getDiagnostics()) :
new MutableDiagnosticsWithSuppression(bindingContext);
} }
public DelegatingBindingTrace(BindingContext parentContext, String debugName, @Nullable Object resolutionSubjectForMessage) { init {
this(parentContext, AnalyzingUtils.formDebugNameForBindingTrace(debugName, resolutionSubjectForMessage)); this.mutableDiagnostics = if (withParentDiagnostics)
MutableDiagnosticsWithSuppression(bindingContext, parentContext.diagnostics)
else
MutableDiagnosticsWithSuppression(bindingContext)
} }
@Override constructor(parentContext: BindingContext,
@NotNull debugName: String,
public BindingContext getBindingContext() { resolutionSubjectForMessage: Any?) : this(parentContext, AnalyzingUtils.formDebugNameForBindingTrace(debugName, resolutionSubjectForMessage)) {
return bindingContext;
} }
@Override override fun getBindingContext(): BindingContext = bindingContext
public <K, V> void record(WritableSlice<K, V> slice, K key, V value) {
map.put(slice, key, value); override fun <K, V> record(slice: WritableSlice<K, V>, key: K, value: V) {
map.put(slice, key, value)
} }
@Override override fun <K> record(slice: WritableSlice<K, Boolean>, key: K) {
public <K> void record(WritableSlice<K, Boolean> slice, K key) { record(slice, key, true)
record(slice, key, true);
} }
@Override override fun <K, V> get(slice: ReadOnlySlice<K, V>, key: K): V? {
public <K, V> V get(ReadOnlySlice<K, V> slice, K key) { val value = map.get(slice, key)
V value = map.get(slice, key); if (slice is SetSlice<*>) {
if (slice instanceof SetSlice) { assert(value != null)
assert value != null; if (value != SetSlice.DEFAULT) return value
if (!value.equals(SetSlice.DEFAULT)) return value;
} }
else if (value != null) { else if (value != null) {
return value; return value
} }
return parentContext.get(slice, key); return parentContext.get(slice, key)
} }
@NotNull override fun <K, V> getKeys(slice: WritableSlice<K, V>): Collection<K> {
@Override val keys = map.getKeys(slice)
public <K, V> Collection<K> getKeys(WritableSlice<K, V> slice) { val fromParent = parentContext.getKeys(slice)
Collection<K> keys = map.getKeys(slice); if (keys.isEmpty()) return fromParent
Collection<K> fromParent = parentContext.getKeys(slice); if (fromParent.isEmpty()) return keys
if (keys.isEmpty()) return fromParent;
if (fromParent.isEmpty()) return keys;
List<K> result = Lists.newArrayList(keys); return keys + fromParent
result.addAll(fromParent);
return result;
} }
@Nullable override fun getType(expression: KtExpression): KotlinType? {
@Override val typeInfo = get(BindingContext.EXPRESSION_TYPE_INFO, expression)
public KotlinType getType(@NotNull KtExpression expression) { return typeInfo?.type
KotlinTypeInfo typeInfo = get(BindingContext.EXPRESSION_TYPE_INFO, expression);
return typeInfo != null ? typeInfo.getType() : null;
} }
@Override override fun recordType(expression: KtExpression, type: KotlinType?) {
public void recordType(@NotNull KtExpression expression, @Nullable KotlinType type) { var typeInfo = get(BindingContext.EXPRESSION_TYPE_INFO, expression)
KotlinTypeInfo typeInfo = get(BindingContext.EXPRESSION_TYPE_INFO, expression);
if (typeInfo == null) { if (typeInfo == null) {
typeInfo = TypeInfoFactoryKt.createTypeInfo(type); typeInfo = createTypeInfo(type)
} }
else { else {
typeInfo = typeInfo.replaceType(type); typeInfo = typeInfo.replaceType(type)
} }
record(BindingContext.EXPRESSION_TYPE_INFO, expression, typeInfo); record(BindingContext.EXPRESSION_TYPE_INFO, expression, typeInfo)
} }
public void addOwnDataTo(@NotNull BindingTrace trace) { fun moveAllMyDataTo(trace: BindingTrace) {
addOwnDataTo(trace, null, true); addOwnDataTo(trace, null, true)
clear()
} }
public void moveAllMyDataTo(@NotNull BindingTrace trace) { @JvmOverloads fun addOwnDataTo(trace: BindingTrace, filter: TraceEntryFilter? = null, commitDiagnostics: Boolean = true) {
addOwnDataTo(trace, null, true); BindingContextUtils.addOwnDataTo(trace, filter, commitDiagnostics, map, mutableDiagnostics)
clear();
} }
public void addOwnDataTo(@NotNull BindingTrace trace, @Nullable TraceEntryFilter filter, boolean commitDiagnostics) { fun clear() {
BindingContextUtils.addOwnDataTo(trace, filter, commitDiagnostics, map, mutableDiagnostics); map.clear()
mutableDiagnostics.clear()
} }
public void clear() { override fun report(diagnostic: Diagnostic) {
map.clear(); mutableDiagnostics.report(diagnostic)
mutableDiagnostics.clear();
} }
@Override override fun toString(): String = name
public void report(@NotNull Diagnostic diagnostic) {
mutableDiagnostics.report(diagnostic);
}
@Override
public String toString() {
return name;
}
} }