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