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.psi.JetSuperExpression;
029    import org.jetbrains.kotlin.resolve.DescriptorUtils;
030    import org.jetbrains.kotlin.types.JetType;
031    
032    import java.util.Collections;
033    
034    public class AccessorForPropertyDescriptor extends PropertyDescriptorImpl implements AccessorForCallableDescriptor<PropertyDescriptor> {
035        private final PropertyDescriptor calleeDescriptor;
036        private final int accessorIndex;
037        private final JetSuperExpression superCallExpression;
038    
039        public AccessorForPropertyDescriptor(
040                @NotNull PropertyDescriptor property,
041                @NotNull DeclarationDescriptor containingDeclaration,
042                int index,
043                @Nullable JetSuperExpression superCallExpression
044        ) {
045            this(property, property.getType(), DescriptorUtils.getReceiverParameterType(property.getExtensionReceiverParameter()),
046                 property.getDispatchReceiverParameter(), containingDeclaration, index, superCallExpression);
047        }
048    
049        protected AccessorForPropertyDescriptor(
050                @NotNull PropertyDescriptor original,
051                @NotNull JetType propertyType,
052                @Nullable JetType receiverType,
053                @Nullable ReceiverParameterDescriptor dispatchReceiverParameter,
054                @NotNull DeclarationDescriptor containingDeclaration,
055                int index,
056                @Nullable JetSuperExpression superCallExpression
057        ) {
058            super(containingDeclaration, null, Annotations.EMPTY, Modality.FINAL, Visibilities.LOCAL,
059                  original.isVar(), Name.identifier("access$" + getIndexedAccessorSuffix(original, index)),
060                  Kind.DECLARATION, SourceElement.NO_SOURCE, /* lateInit = */ false, /* isConst = */ false);
061    
062            this.calleeDescriptor = original;
063            this.accessorIndex = index;
064            this.superCallExpression = superCallExpression;
065            setType(propertyType, Collections.<TypeParameterDescriptorImpl>emptyList(), dispatchReceiverParameter, receiverType);
066            initialize(new Getter(this), new Setter(this));
067        }
068    
069        public static class Getter extends PropertyGetterDescriptorImpl implements AccessorForCallableDescriptor<PropertyGetterDescriptor> {
070            public Getter(AccessorForPropertyDescriptor property) {
071                super(property, Annotations.EMPTY, Modality.FINAL, Visibilities.LOCAL,
072                      false, false, Kind.DECLARATION, null, SourceElement.NO_SOURCE);
073                initialize(property.getType());
074            }
075    
076            @NotNull
077            @Override
078            public PropertyGetterDescriptor getCalleeDescriptor() {
079                //noinspection ConstantConditions
080                return ((AccessorForPropertyDescriptor) getCorrespondingProperty()).getCalleeDescriptor().getGetter();
081            }
082    
083            @Nullable
084            @Override
085            public JetSuperExpression getSuperCallExpression() {
086                return ((AccessorForPropertyDescriptor) getCorrespondingProperty()).getSuperCallExpression();
087            }
088        }
089    
090        public static class Setter extends PropertySetterDescriptorImpl implements AccessorForCallableDescriptor<PropertySetterDescriptor>{
091            public Setter(AccessorForPropertyDescriptor property) {
092                super(property, Annotations.EMPTY, Modality.FINAL, Visibilities.LOCAL,
093                      false, false, Kind.DECLARATION, null, SourceElement.NO_SOURCE);
094                initializeDefault();
095            }
096    
097            @NotNull
098            @Override
099            public PropertySetterDescriptor getCalleeDescriptor() {
100                //noinspection ConstantConditions
101                return ((AccessorForPropertyDescriptor) getCorrespondingProperty()).getCalleeDescriptor().getSetter();
102            }
103    
104            @Nullable
105            @Override
106            public JetSuperExpression getSuperCallExpression() {
107                return ((AccessorForPropertyDescriptor) getCorrespondingProperty()).getSuperCallExpression();
108            }
109        }
110    
111        @NotNull
112        @Override
113        public PropertyDescriptor getCalleeDescriptor() {
114            return calleeDescriptor;
115        }
116    
117        @Override
118        public JetSuperExpression getSuperCallExpression() {
119            return superCallExpression;
120        }
121    
122        @NotNull
123        public String getIndexedAccessorSuffix() {
124            return getIndexedAccessorSuffix(calleeDescriptor, accessorIndex);
125        }
126    
127        @NotNull
128        private static String getIndexedAccessorSuffix(@NotNull PropertyDescriptor original, int index) {
129            return original.getName() + "$" + index;
130        }
131    }