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.codegen;
018
019 import com.intellij.openapi.util.Pair;
020 import org.jetbrains.annotations.NotNull;
021 import org.jetbrains.annotations.Nullable;
022 import org.jetbrains.org.objectweb.asm.Type;
023 import org.jetbrains.org.objectweb.asm.commons.Method;
024 import org.jetbrains.jet.lang.descriptors.CallableMemberDescriptor;
025 import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
026 import org.jetbrains.jet.lang.descriptors.PropertyDescriptor;
027 import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor;
028 import org.jetbrains.jet.util.slicedmap.*;
029
030 import java.util.Collection;
031 import java.util.Map;
032
033 public final class JvmSerializationBindings {
034 public static final SerializationMappingSlice<FunctionDescriptor, Method> METHOD_FOR_FUNCTION =
035 SerializationMappingSlice.create();
036 public static final SerializationMappingSlice<PropertyDescriptor, Pair<Type, String>> FIELD_FOR_PROPERTY =
037 SerializationMappingSlice.create();
038 public static final SerializationMappingSlice<PropertyDescriptor, Method> SYNTHETIC_METHOD_FOR_PROPERTY =
039 SerializationMappingSlice.create();
040 public static final SerializationMappingSlice<CallableMemberDescriptor, String> IMPL_CLASS_NAME_FOR_CALLABLE =
041 SerializationMappingSlice.create();
042 public static final SerializationMappingSetSlice<PropertyDescriptor> STATIC_FIELD_IN_OUTER_CLASS =
043 SerializationMappingSetSlice.create();
044 public static final SerializationMappingSlice<ValueParameterDescriptor, Integer> INDEX_FOR_VALUE_PARAMETER =
045 SerializationMappingSlice.create();
046
047 private static final class SerializationMappingSlice<K, V> extends BasicWritableSlice<K, V> {
048 public SerializationMappingSlice() {
049 super(Slices.ONLY_REWRITE_TO_EQUAL, false);
050 }
051
052 @NotNull
053 public static <K, V> SerializationMappingSlice<K, V> create() {
054 return new SerializationMappingSlice<K, V>();
055 }
056 }
057
058 private static final class SerializationMappingSetSlice<K> extends Slices.SetSlice<K> {
059 public SerializationMappingSetSlice() {
060 super(Slices.ONLY_REWRITE_TO_EQUAL, false);
061 }
062
063 @NotNull
064 public static <K> SerializationMappingSetSlice<K> create() {
065 return new SerializationMappingSetSlice<K>();
066 }
067 }
068
069 static {
070 BasicWritableSlice.initSliceDebugNames(JvmSerializationBindings.class);
071 }
072
073 private final MutableSlicedMap map;
074
075 private JvmSerializationBindings(@NotNull MutableSlicedMap map) {
076 this.map = map;
077 }
078
079 public JvmSerializationBindings() {
080 this(SlicedMapImpl.create());
081 }
082
083 @NotNull
084 public static JvmSerializationBindings union(@NotNull Collection<JvmSerializationBindings> bindings) {
085 MutableSlicedMap result = SlicedMapImpl.create();
086 for (JvmSerializationBindings binding : bindings) {
087 for (Map.Entry<SlicedMapKey<?, ?>, ?> entry : binding.map) {
088 SlicedMapKey<?, ?> key = entry.getKey();
089 result.put((WritableSlice) key.getSlice(), key.getKey(), entry.getValue());
090 }
091 }
092 return new JvmSerializationBindings(result);
093 }
094
095 public <K, V> void put(@NotNull SerializationMappingSlice<K, V> slice, @NotNull K key, @NotNull V value) {
096 map.put(slice, key, value);
097 }
098
099 public <K> void put(@NotNull SerializationMappingSetSlice<K> slice, @NotNull K key) {
100 map.put(slice, key, true);
101 }
102
103 @Nullable
104 public <K, V> V get(@NotNull SerializationMappingSlice<K, V> slice, @NotNull K key) {
105 return map.get(slice, key);
106 }
107
108 public <K> boolean get(@NotNull SerializationMappingSetSlice<K> slice, @NotNull K key) {
109 return Boolean.TRUE.equals(map.get(slice, key));
110 }
111 }