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 com.google.common.collect.Lists;
021 import com.google.common.collect.Maps;
022 import org.jetbrains.annotations.NotNull;
023 import org.jetbrains.annotations.Nullable;
024 import org.jetbrains.annotations.TestOnly;
025 import org.jetbrains.kotlin.diagnostics.Diagnostic;
026 import org.jetbrains.kotlin.psi.KtExpression;
027 import org.jetbrains.kotlin.resolve.diagnostics.Diagnostics;
028 import org.jetbrains.kotlin.resolve.diagnostics.MutableDiagnosticsWithSuppression;
029 import org.jetbrains.kotlin.types.KotlinType;
030 import org.jetbrains.kotlin.types.expressions.KotlinTypeInfo;
031 import org.jetbrains.kotlin.types.expressions.typeInfoFactory.TypeInfoFactoryKt;
032 import org.jetbrains.kotlin.util.slicedMap.*;
033
034 import java.util.Collection;
035 import java.util.List;
036 import java.util.Map;
037
038 public class DelegatingBindingTrace implements BindingTrace {
039 @SuppressWarnings("ConstantConditions")
040 private final MutableSlicedMap map = BindingTraceContext.TRACK_REWRITES ? new TrackingSlicedMap(BindingTraceContext.TRACK_WITH_STACK_TRACES) : SlicedMapImpl.create();
041
042 private final BindingContext parentContext;
043 private final String name;
044 private final MutableDiagnosticsWithSuppression mutableDiagnostics;
045
046 private final BindingContext bindingContext = new BindingContext() {
047 @NotNull
048 @Override
049 public Diagnostics getDiagnostics() {
050 return mutableDiagnostics;
051 }
052
053 @Override
054 public <K, V> V get(ReadOnlySlice<K, V> slice, K key) {
055 return DelegatingBindingTrace.this.get(slice, key);
056 }
057
058
059 @Nullable
060 @Override
061 public KotlinType getType(@NotNull KtExpression expression) {
062 return DelegatingBindingTrace.this.getType(expression);
063 }
064
065 @NotNull
066 @Override
067 public <K, V> Collection<K> getKeys(WritableSlice<K, V> slice) {
068 return DelegatingBindingTrace.this.getKeys(slice);
069 }
070
071 @Override
072 public void addOwnDataTo(@NotNull BindingTrace trace, boolean commitDiagnostics) {
073 BindingContextUtils.addOwnDataTo(trace, null, commitDiagnostics, map, mutableDiagnostics);
074 }
075
076 @NotNull
077 @TestOnly
078 @Override
079 public <K, V> ImmutableMap<K, V> getSliceContents(@NotNull ReadOnlySlice<K, V> slice) {
080 Map<K, V> result = Maps.newHashMap();
081 result.putAll(parentContext.getSliceContents(slice));
082 result.putAll(map.getSliceContents(slice));
083 return ImmutableMap.copyOf(result);
084 }
085 };
086
087 public DelegatingBindingTrace(BindingContext parentContext, String debugName) {
088 this.parentContext = parentContext;
089 this.name = debugName;
090 this.mutableDiagnostics = new MutableDiagnosticsWithSuppression(bindingContext, parentContext.getDiagnostics());
091 }
092
093 public DelegatingBindingTrace(BindingContext parentContext, String debugName, @Nullable Object resolutionSubjectForMessage) {
094 this(parentContext, AnalyzingUtils.formDebugNameForBindingTrace(debugName, resolutionSubjectForMessage));
095 }
096
097 @Override
098 @NotNull
099 public BindingContext getBindingContext() {
100 return bindingContext;
101 }
102
103 @Override
104 public <K, V> void record(WritableSlice<K, V> slice, K key, V value) {
105 map.put(slice, key, value);
106 }
107
108 @Override
109 public <K> void record(WritableSlice<K, Boolean> slice, K key) {
110 record(slice, key, true);
111 }
112
113 @Override
114 public <K, V> V get(ReadOnlySlice<K, V> slice, K key) {
115 V value = map.get(slice, key);
116 if (slice instanceof Slices.SetSlice) {
117 assert value != null;
118 if (value.equals(true)) return value;
119 }
120 else if (value != null) {
121 return value;
122 }
123
124 return parentContext.get(slice, key);
125 }
126
127 @NotNull
128 @Override
129 public <K, V> Collection<K> getKeys(WritableSlice<K, V> slice) {
130 Collection<K> keys = map.getKeys(slice);
131 Collection<K> fromParent = parentContext.getKeys(slice);
132 if (keys.isEmpty()) return fromParent;
133 if (fromParent.isEmpty()) return keys;
134
135 List<K> result = Lists.newArrayList(keys);
136 result.addAll(fromParent);
137 return result;
138 }
139
140 @Nullable
141 @Override
142 public KotlinType getType(@NotNull KtExpression expression) {
143 KotlinTypeInfo typeInfo = get(BindingContext.EXPRESSION_TYPE_INFO, expression);
144 return typeInfo != null ? typeInfo.getType() : null;
145 }
146
147 @Override
148 public void recordType(@NotNull KtExpression expression, @Nullable KotlinType type) {
149 KotlinTypeInfo typeInfo = get(BindingContext.EXPRESSION_TYPE_INFO, expression);
150 if (typeInfo == null) {
151 typeInfo = TypeInfoFactoryKt.createTypeInfo(type);
152 }
153 else {
154 typeInfo = typeInfo.replaceType(type);
155 }
156 record(BindingContext.EXPRESSION_TYPE_INFO, expression, typeInfo);
157 }
158
159 public void addOwnDataTo(@NotNull BindingTrace trace) {
160 addOwnDataTo(trace, null, true);
161 }
162
163 public void moveAllMyDataTo(@NotNull BindingTrace trace) {
164 addOwnDataTo(trace, null, true);
165 clear();
166 }
167
168 public void addOwnDataTo(@NotNull BindingTrace trace, @Nullable TraceEntryFilter filter, boolean commitDiagnostics) {
169 BindingContextUtils.addOwnDataTo(trace, filter, commitDiagnostics, map, mutableDiagnostics);
170 }
171
172 public void clear() {
173 map.clear();
174 mutableDiagnostics.clear();
175 }
176
177 @Override
178 public void report(@NotNull Diagnostic diagnostic) {
179 mutableDiagnostics.report(diagnostic);
180 }
181
182 @Override
183 public String toString() {
184 return name;
185 }
186 }