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