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.KotlinType;
030
031 import java.util.Collections;
032
033 public class AccessorForPropertyDescriptor extends PropertyDescriptorImpl implements AccessorForCallableDescriptor<PropertyDescriptor> {
034 private final PropertyDescriptor calleeDescriptor;
035 private final ClassDescriptor superCallTarget;
036 @NotNull private final String nameSuffix;
037 private final boolean withSyntheticGetterAccessor;
038 private final boolean withSyntheticSetterAccessor;
039
040 public AccessorForPropertyDescriptor(
041 @NotNull PropertyDescriptor property,
042 @NotNull DeclarationDescriptor containingDeclaration,
043 @Nullable ClassDescriptor superCallTarget,
044 @NotNull String nameSuffix,
045 boolean getterAccessorRequired,
046 boolean setterAccessorRequired
047 ) {
048 this(property, property.getType(), DescriptorUtils.getReceiverParameterType(property.getExtensionReceiverParameter()),
049 property.getDispatchReceiverParameter(), containingDeclaration, superCallTarget, nameSuffix,
050 getterAccessorRequired, setterAccessorRequired);
051 }
052
053 protected AccessorForPropertyDescriptor(
054 @NotNull PropertyDescriptor original,
055 @NotNull KotlinType propertyType,
056 @Nullable KotlinType receiverType,
057 @Nullable ReceiverParameterDescriptor dispatchReceiverParameter,
058 @NotNull DeclarationDescriptor containingDeclaration,
059 @Nullable ClassDescriptor superCallTarget,
060 @NotNull String nameSuffix
061 ) {
062 this(original, propertyType, receiverType, dispatchReceiverParameter, containingDeclaration, superCallTarget, nameSuffix, true, true);
063 }
064
065 protected AccessorForPropertyDescriptor(
066 @NotNull PropertyDescriptor original,
067 @NotNull KotlinType propertyType,
068 @Nullable KotlinType receiverType,
069 @Nullable ReceiverParameterDescriptor dispatchReceiverParameter,
070 @NotNull DeclarationDescriptor containingDeclaration,
071 @Nullable ClassDescriptor superCallTarget,
072 @NotNull String nameSuffix,
073 boolean getterAccessorRequired,
074 boolean setterAccessorRequired
075 ) {
076 super(containingDeclaration, null, Annotations.Companion.getEMPTY(), Modality.FINAL, Visibilities.LOCAL,
077 original.isVar(), Name.identifier("access$" + nameSuffix),
078 Kind.DECLARATION, SourceElement.NO_SOURCE, /* lateInit = */ false, /* isConst = */ false);
079
080 this.calleeDescriptor = original;
081 this.superCallTarget = superCallTarget;
082 this.nameSuffix = nameSuffix;
083 setType(propertyType, Collections.<TypeParameterDescriptorImpl>emptyList(), dispatchReceiverParameter, receiverType);
084
085 this.withSyntheticGetterAccessor = getterAccessorRequired;
086 this.withSyntheticSetterAccessor = setterAccessorRequired;
087
088 PropertyGetterDescriptorImpl getterDescriptor =
089 getterAccessorRequired ? new Getter(this) : (PropertyGetterDescriptorImpl) original.getGetter();
090 PropertySetterDescriptor setterDescriptor =
091 setterAccessorRequired ? new Setter(this) : original.getSetter();
092 initialize(getterDescriptor, setterDescriptor);
093 }
094
095 public static class Getter extends PropertyGetterDescriptorImpl implements AccessorForCallableDescriptor<PropertyGetterDescriptor> {
096 public Getter(AccessorForPropertyDescriptor property) {
097 super(property, Annotations.Companion.getEMPTY(), Modality.FINAL, Visibilities.LOCAL,
098 false, false,
099 /* isExternal = */ false,
100 Kind.DECLARATION, null, SourceElement.NO_SOURCE);
101 initialize(property.getType());
102 }
103
104 @NotNull
105 @Override
106 public PropertyGetterDescriptor getCalleeDescriptor() {
107 //noinspection ConstantConditions
108 return ((AccessorForPropertyDescriptor) getCorrespondingProperty()).getCalleeDescriptor().getGetter();
109 }
110
111 @Override
112 @Nullable
113 public ClassDescriptor getSuperCallTarget() {
114 return ((AccessorForPropertyDescriptor) getCorrespondingProperty()).getSuperCallTarget();
115 }
116
117 }
118
119 public static class Setter extends PropertySetterDescriptorImpl implements AccessorForCallableDescriptor<PropertySetterDescriptor>{
120 public Setter(AccessorForPropertyDescriptor property) {
121 super(property, Annotations.Companion.getEMPTY(), Modality.FINAL, Visibilities.LOCAL,
122 false, false,
123 /* isExternal = */ false,
124 Kind.DECLARATION, null, SourceElement.NO_SOURCE);
125 initializeDefault();
126 }
127
128 @NotNull
129 @Override
130 public PropertySetterDescriptor getCalleeDescriptor() {
131 //noinspection ConstantConditions
132 return ((AccessorForPropertyDescriptor) getCorrespondingProperty()).getCalleeDescriptor().getSetter();
133 }
134
135 @Override
136 @Nullable
137 public ClassDescriptor getSuperCallTarget() {
138 return ((AccessorForPropertyDescriptor) getCorrespondingProperty()).getSuperCallTarget();
139 }
140 }
141
142 @NotNull
143 @Override
144 public PropertyDescriptor getCalleeDescriptor() {
145 return calleeDescriptor;
146 }
147
148 @Override
149 public ClassDescriptor getSuperCallTarget() {
150 return superCallTarget;
151 }
152
153 @NotNull
154 public String getAccessorSuffix() {
155 return nameSuffix;
156 }
157
158 public boolean isWithSyntheticGetterAccessor() {
159 return withSyntheticGetterAccessor;
160 }
161
162 public boolean isWithSyntheticSetterAccessor() {
163 return withSyntheticSetterAccessor;
164 }
165 }