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