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.context;
018
019 import org.jetbrains.annotations.NotNull;
020 import org.jetbrains.annotations.Nullable;
021 import org.jetbrains.kotlin.codegen.AccessorForPropertyDescriptor;
022 import org.jetbrains.kotlin.codegen.OwnerKind;
023 import org.jetbrains.kotlin.codegen.binding.MutableClosure;
024 import org.jetbrains.kotlin.codegen.state.JetTypeMapper;
025 import org.jetbrains.kotlin.descriptors.ClassDescriptor;
026 import org.jetbrains.kotlin.descriptors.DeclarationDescriptor;
027 import org.jetbrains.kotlin.descriptors.PropertyDescriptor;
028 import org.jetbrains.kotlin.resolve.jvm.annotations.AnnotationUtilKt;
029
030 import java.util.HashMap;
031 import java.util.Map;
032
033 public abstract class FieldOwnerContext<T extends DeclarationDescriptor> extends CodegenContext<T> {
034 //default property name -> map<property descriptor -> bytecode name>
035 private final Map<String, Map<PropertyDescriptor, String>> fieldNames = new HashMap<String, Map<PropertyDescriptor, String>>();
036
037 public FieldOwnerContext(
038 @NotNull T contextDescriptor,
039 @NotNull OwnerKind contextKind,
040 @Nullable CodegenContext parentContext,
041 @Nullable MutableClosure closure,
042 @Nullable ClassDescriptor thisDescriptor,
043 @Nullable LocalLookup expressionCodegen
044 ) {
045 super(contextDescriptor, contextKind, parentContext, closure, thisDescriptor, expressionCodegen);
046 }
047
048 @NotNull
049 public String getFieldName(@NotNull PropertyDescriptor possiblySubstitutedDescriptor, boolean isDelegated) {
050 if (possiblySubstitutedDescriptor instanceof AccessorForPropertyDescriptor) {
051 possiblySubstitutedDescriptor = ((AccessorForPropertyDescriptor) possiblySubstitutedDescriptor).getCalleeDescriptor();
052 }
053
054 PropertyDescriptor descriptor = possiblySubstitutedDescriptor.getOriginal();
055 assert descriptor.getKind().isReal() : "Only declared properties can have backing fields: " + descriptor;
056
057 String defaultPropertyName = JetTypeMapper.mapDefaultFieldName(descriptor, isDelegated);
058
059 Map<PropertyDescriptor, String> descriptor2Name = fieldNames.get(defaultPropertyName);
060 if (descriptor2Name == null) {
061 descriptor2Name = new HashMap<PropertyDescriptor, String>();
062 fieldNames.put(defaultPropertyName, descriptor2Name);
063 }
064
065 String actualName = descriptor2Name.get(descriptor);
066 if (actualName != null) return actualName;
067
068 String newName = descriptor2Name.isEmpty() || AnnotationUtilKt.hasJvmFieldAnnotation(descriptor)
069 ? defaultPropertyName
070 : defaultPropertyName + "$" + descriptor2Name.size();
071 descriptor2Name.put(descriptor, newName);
072 return newName;
073 }
074 }