001 /* 002 * Copyright 2010-2015 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.kotlin.resolve; 018 019 import com.google.common.collect.ImmutableMap; 020 import org.jetbrains.annotations.NotNull; 021 import org.jetbrains.annotations.TestOnly; 022 import org.jetbrains.kotlin.diagnostics.Diagnostic; 023 import org.jetbrains.kotlin.resolve.diagnostics.Diagnostics; 024 import org.jetbrains.kotlin.resolve.diagnostics.MutableDiagnosticsWithSuppression; 025 import org.jetbrains.kotlin.util.slicedMap.*; 026 027 import java.util.Collection; 028 029 public class BindingTraceContext implements BindingTrace { 030 // These flags are used for debugging of "Rewrite at slice..." exceptions 031 /* package */ final static boolean TRACK_REWRITES = false; 032 /* package */ final static boolean TRACK_WITH_STACK_TRACES = true; 033 034 private final MutableSlicedMap map; 035 private final MutableDiagnosticsWithSuppression mutableDiagnostics; 036 037 private final BindingContext bindingContext = new BindingContext() { 038 039 @NotNull 040 @Override 041 public Diagnostics getDiagnostics() { 042 return mutableDiagnostics; 043 } 044 045 @Override 046 public <K, V> V get(ReadOnlySlice<K, V> slice, K key) { 047 return BindingTraceContext.this.get(slice, key); 048 } 049 050 @NotNull 051 @Override 052 public <K, V> Collection<K> getKeys(WritableSlice<K, V> slice) { 053 return BindingTraceContext.this.getKeys(slice); 054 } 055 056 @NotNull 057 @TestOnly 058 @Override 059 public <K, V> ImmutableMap<K, V> getSliceContents(@NotNull ReadOnlySlice<K, V> slice) { 060 return map.getSliceContents(slice); 061 } 062 }; 063 064 public BindingTraceContext() { 065 //noinspection ConstantConditions 066 this(TRACK_REWRITES ? new TrackingSlicedMap(TRACK_WITH_STACK_TRACES) : SlicedMapImpl.create()); 067 } 068 069 070 private BindingTraceContext(@NotNull MutableSlicedMap map) { 071 this.map = map; 072 this.mutableDiagnostics = new MutableDiagnosticsWithSuppression(bindingContext, Diagnostics.EMPTY); 073 } 074 075 @TestOnly 076 public static BindingTraceContext createTraceableBindingTrace() { 077 return new BindingTraceContext(new TrackingSlicedMap(TRACK_WITH_STACK_TRACES)); 078 } 079 080 @Override 081 public void report(@NotNull Diagnostic diagnostic) { 082 mutableDiagnostics.report(diagnostic); 083 } 084 085 public void clearDiagnostics() { 086 mutableDiagnostics.clear(); 087 } 088 089 @NotNull 090 @Override 091 public BindingContext getBindingContext() { 092 return bindingContext; 093 } 094 095 @Override 096 public <K, V> void record(WritableSlice<K, V> slice, K key, V value) { 097 map.put(slice, key, value); 098 } 099 100 @Override 101 public <K> void record(WritableSlice<K, Boolean> slice, K key) { 102 record(slice, key, true); 103 } 104 105 @Override 106 public <K, V> V get(ReadOnlySlice<K, V> slice, K key) { 107 return map.get(slice, key); 108 } 109 110 @NotNull 111 @Override 112 public <K, V> Collection<K> getKeys(WritableSlice<K, V> slice) { 113 return map.getKeys(slice); 114 } 115 }