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.codegen.serialization;
018
019 import com.intellij.openapi.util.Pair;
020 import org.jetbrains.annotations.NotNull;
021 import org.jetbrains.annotations.Nullable;
022 import org.jetbrains.kotlin.descriptors.FunctionDescriptor;
023 import org.jetbrains.kotlin.descriptors.PropertyDescriptor;
024 import org.jetbrains.kotlin.util.slicedMap.*;
025 import org.jetbrains.org.objectweb.asm.Type;
026 import org.jetbrains.org.objectweb.asm.commons.Method;
027
028 public final class JvmSerializationBindings {
029 public static final SerializationMappingSlice<FunctionDescriptor, Method> METHOD_FOR_FUNCTION =
030 SerializationMappingSlice.create();
031 public static final SerializationMappingSlice<PropertyDescriptor, Pair<Type, String>> FIELD_FOR_PROPERTY =
032 SerializationMappingSlice.create();
033 public static final SerializationMappingSlice<PropertyDescriptor, Method> SYNTHETIC_METHOD_FOR_PROPERTY =
034 SerializationMappingSlice.create();
035
036 private static final class SerializationMappingSlice<K, V> extends BasicWritableSlice<K, V> {
037 public SerializationMappingSlice() {
038 super(Slices.ONLY_REWRITE_TO_EQUAL, false);
039 }
040
041 @NotNull
042 public static <K, V> SerializationMappingSlice<K, V> create() {
043 return new SerializationMappingSlice<K, V>();
044 }
045 }
046
047 private static final class SerializationMappingSetSlice<K> extends SetSlice<K> {
048 public SerializationMappingSetSlice() {
049 super(Slices.ONLY_REWRITE_TO_EQUAL, false);
050 }
051
052 @NotNull
053 public static <K> SerializationMappingSetSlice<K> create() {
054 return new SerializationMappingSetSlice<K>();
055 }
056 }
057
058 static {
059 BasicWritableSlice.initSliceDebugNames(JvmSerializationBindings.class);
060 }
061
062 private final MutableSlicedMap map = SlicedMapImpl.create();
063
064 public <K, V> void put(@NotNull SerializationMappingSlice<K, V> slice, @NotNull K key, @NotNull V value) {
065 map.put(slice, key, value);
066 }
067
068 public <K> void put(@NotNull SerializationMappingSetSlice<K> slice, @NotNull K key) {
069 map.put(slice, key, true);
070 }
071
072 @Nullable
073 public <K, V> V get(@NotNull SerializationMappingSlice<K, V> slice, @NotNull K key) {
074 return map.get(slice, key);
075 }
076
077 public <K> boolean get(@NotNull SerializationMappingSetSlice<K> slice, @NotNull K key) {
078 return Boolean.TRUE.equals(map.get(slice, key));
079 }
080 }