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;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.annotations.Nullable;
021    import org.jetbrains.kotlin.descriptors.*;
022    import org.jetbrains.kotlin.descriptors.annotations.Annotations;
023    import org.jetbrains.kotlin.descriptors.impl.PropertyDescriptorImpl;
024    import org.jetbrains.kotlin.descriptors.impl.PropertyGetterDescriptorImpl;
025    import org.jetbrains.kotlin.descriptors.impl.PropertySetterDescriptorImpl;
026    import org.jetbrains.kotlin.descriptors.impl.TypeParameterDescriptorImpl;
027    import org.jetbrains.kotlin.name.Name;
028    import org.jetbrains.kotlin.resolve.DescriptorUtils;
029    import org.jetbrains.kotlin.types.JetType;
030    
031    import java.util.Collections;
032    
033    public class AccessorForPropertyDescriptor extends PropertyDescriptorImpl implements AccessorForCallableDescriptor<PropertyDescriptor> {
034        private final PropertyDescriptor calleeDescriptor;
035        private final int accessorIndex;
036    
037        public AccessorForPropertyDescriptor(@NotNull PropertyDescriptor pd, @NotNull DeclarationDescriptor containingDeclaration, int index) {
038            this(pd, pd.getType(), DescriptorUtils.getReceiverParameterType(pd.getExtensionReceiverParameter()), pd.getDispatchReceiverParameter(), containingDeclaration, index);
039        }
040    
041        protected AccessorForPropertyDescriptor(
042                @NotNull PropertyDescriptor original,
043                @NotNull JetType propertyType,
044                @Nullable JetType receiverType,
045                @Nullable ReceiverParameterDescriptor dispatchReceiverParameter,
046                @NotNull DeclarationDescriptor containingDeclaration,
047                int index
048        ) {
049            super(containingDeclaration, null, Annotations.EMPTY, Modality.FINAL, Visibilities.LOCAL,
050                  original.isVar(), Name.identifier("access$" + getIndexedAccessorSuffix(original, index)),
051                  Kind.DECLARATION, SourceElement.NO_SOURCE);
052    
053            this.calleeDescriptor = original;
054            this.accessorIndex = index;
055            setType(propertyType, Collections.<TypeParameterDescriptorImpl>emptyList(), dispatchReceiverParameter, receiverType);
056            initialize(new Getter(this), new Setter(this));
057        }
058    
059        public static class Getter extends PropertyGetterDescriptorImpl implements AccessorForCallableDescriptor<PropertyGetterDescriptor> {
060            public Getter(AccessorForPropertyDescriptor property) {
061                super(property, Annotations.EMPTY, Modality.FINAL, Visibilities.LOCAL,
062                      false, false, Kind.DECLARATION, null, SourceElement.NO_SOURCE);
063                initialize(property.getType());
064            }
065    
066            @NotNull
067            @Override
068            public PropertyGetterDescriptor getCalleeDescriptor() {
069                //noinspection ConstantConditions
070                return ((AccessorForPropertyDescriptor) getCorrespondingProperty()).getCalleeDescriptor().getGetter();
071            }
072        }
073    
074        public static class Setter extends PropertySetterDescriptorImpl implements AccessorForCallableDescriptor<PropertySetterDescriptor>{
075            public Setter(AccessorForPropertyDescriptor property) {
076                super(property, Annotations.EMPTY, Modality.FINAL, Visibilities.LOCAL,
077                      false, false, Kind.DECLARATION, null, SourceElement.NO_SOURCE);
078                initializeDefault();
079            }
080    
081            @NotNull
082            @Override
083            public PropertySetterDescriptor getCalleeDescriptor() {
084                //noinspection ConstantConditions
085                return ((AccessorForPropertyDescriptor) getCorrespondingProperty()).getCalleeDescriptor().getSetter();
086            }
087        }
088    
089        @NotNull
090        @Override
091        public PropertyDescriptor getCalleeDescriptor() {
092            return calleeDescriptor;
093        }
094    
095        @NotNull
096        public String getIndexedAccessorSuffix() {
097            return getIndexedAccessorSuffix(calleeDescriptor, accessorIndex);
098        }
099    
100        @NotNull
101        private static String getIndexedAccessorSuffix(@NotNull PropertyDescriptor original, int index) {
102            return original.getName() + "$" + index;
103        }
104    }