Make PropertyAccessorDescriptor#hasBody a front-end utility
This commit is contained in:
committed by
Alexander Udalov
parent
ae14d185eb
commit
4553afbd0c
+3
-7
@@ -33,7 +33,7 @@ import java.util.Collections;
|
||||
public class AccessorForPropertyDescriptor extends PropertyDescriptorImpl implements AccessorForCallableDescriptor<PropertyDescriptor> {
|
||||
private final PropertyDescriptor calleeDescriptor;
|
||||
private final ClassDescriptor superCallTarget;
|
||||
@NotNull private final String nameSuffix;
|
||||
private final String nameSuffix;
|
||||
private final boolean withSyntheticGetterAccessor;
|
||||
private final boolean withSyntheticSetterAccessor;
|
||||
|
||||
@@ -95,9 +95,7 @@ public class AccessorForPropertyDescriptor extends PropertyDescriptorImpl implem
|
||||
public static class Getter extends PropertyGetterDescriptorImpl implements AccessorForCallableDescriptor<PropertyGetterDescriptor> {
|
||||
public Getter(AccessorForPropertyDescriptor property) {
|
||||
super(property, Annotations.Companion.getEMPTY(), Modality.FINAL, Visibilities.LOCAL,
|
||||
false, false,
|
||||
/* isExternal = */ false,
|
||||
Kind.DECLARATION, null, SourceElement.NO_SOURCE);
|
||||
/* isDefault = */ false, /* isExternal = */ false, Kind.DECLARATION, null, SourceElement.NO_SOURCE);
|
||||
initialize(property.getType());
|
||||
}
|
||||
|
||||
@@ -119,9 +117,7 @@ public class AccessorForPropertyDescriptor extends PropertyDescriptorImpl implem
|
||||
public static class Setter extends PropertySetterDescriptorImpl implements AccessorForCallableDescriptor<PropertySetterDescriptor>{
|
||||
public Setter(AccessorForPropertyDescriptor property) {
|
||||
super(property, Annotations.Companion.getEMPTY(), Modality.FINAL, Visibilities.LOCAL,
|
||||
false, false,
|
||||
/* isExternal = */ false,
|
||||
Kind.DECLARATION, null, SourceElement.NO_SOURCE);
|
||||
/* isDefault = */ false, /* isExternal = */ false, Kind.DECLARATION, null, SourceElement.NO_SOURCE);
|
||||
initializeDefault();
|
||||
}
|
||||
|
||||
|
||||
@@ -161,7 +161,7 @@ public class JvmCodegenUtil {
|
||||
if (accessor == null) return true;
|
||||
|
||||
// If the accessor is non-default (i.e. it has some code) we should call that accessor and not use direct access
|
||||
if (accessor.hasBody()) return false;
|
||||
if (DescriptorPsiUtilsKt.hasBody(accessor)) return false;
|
||||
|
||||
// If the accessor is private or final, it can't be overridden in the subclass and thus we can use direct access
|
||||
return Visibilities.isPrivate(property.getVisibility()) || accessor.getModality() == FINAL;
|
||||
|
||||
+1
-3
@@ -315,7 +315,6 @@ class JavaSyntheticPropertiesScope(storageManager: StorageManager, private val l
|
||||
Modality.FINAL,
|
||||
visibility,
|
||||
false,
|
||||
false,
|
||||
getMethod.isExternal,
|
||||
CallableMemberDescriptor.Kind.SYNTHESIZED,
|
||||
null,
|
||||
@@ -328,7 +327,6 @@ class JavaSyntheticPropertiesScope(storageManager: StorageManager, private val l
|
||||
Modality.FINAL,
|
||||
syntheticExtensionVisibility(setMethod),
|
||||
false,
|
||||
false,
|
||||
setMethod.isExternal,
|
||||
CallableMemberDescriptor.Kind.SYNTHESIZED,
|
||||
null,
|
||||
@@ -351,7 +349,7 @@ class JavaSyntheticPropertiesScope(storageManager: StorageManager, private val l
|
||||
}
|
||||
|
||||
override fun substitute(originalSubstitutor: TypeSubstitutor): PropertyDescriptor? {
|
||||
val descriptor = super<PropertyDescriptorImpl>.substitute(originalSubstitutor) as MyPropertyDescriptor
|
||||
val descriptor = super.substitute(originalSubstitutor) as MyPropertyDescriptor
|
||||
if (descriptor == this) return descriptor
|
||||
|
||||
val classTypeParameters = (getMethod.containingDeclaration as ClassDescriptor).typeConstructor.parameters
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* Copyright 2010-2016 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.descriptors
|
||||
|
||||
import org.jetbrains.kotlin.psi.KtPropertyAccessor
|
||||
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
|
||||
|
||||
fun PropertyAccessorDescriptor.hasBody(): Boolean {
|
||||
val ktAccessor = DescriptorToSourceUtils.getSourceFromDescriptor(this) as? KtPropertyAccessor
|
||||
return ktAccessor != null && ktAccessor.hasBody()
|
||||
}
|
||||
@@ -183,8 +183,8 @@ public interface BindingContext {
|
||||
|
||||
if (getter == null) return true;
|
||||
if (propertyDescriptor.isVar() && setter == null) return true;
|
||||
if (setter != null && !setter.hasBody() && setter.getModality() != Modality.ABSTRACT) return true;
|
||||
if (!getter.hasBody() && getter.getModality() != Modality.ABSTRACT) return true;
|
||||
if (setter != null && !DescriptorPsiUtilsKt.hasBody(setter) && setter.getModality() != Modality.ABSTRACT) return true;
|
||||
if (!DescriptorPsiUtilsKt.hasBody(getter) && getter.getModality() != Modality.ABSTRACT) return true;
|
||||
|
||||
return backingFieldRequired;
|
||||
}
|
||||
|
||||
@@ -816,12 +816,13 @@ public class DescriptorResolver {
|
||||
annotationResolver.resolveAnnotationsWithoutArguments(scopeWithTypeParameters, setter.getModifierList(), trace)));
|
||||
KtParameter parameter = setter.getParameter();
|
||||
|
||||
setterDescriptor = new PropertySetterDescriptorImpl(propertyDescriptor, annotations,
|
||||
resolveModalityFromModifiers(setter, propertyDescriptor.getModality()),
|
||||
resolveVisibilityFromModifiers(setter, propertyDescriptor.getVisibility()),
|
||||
setter.hasBody(), false, setter.hasModifier(EXTERNAL_KEYWORD),
|
||||
CallableMemberDescriptor.Kind.DECLARATION, null, KotlinSourceElementKt
|
||||
.toSourceElement(setter));
|
||||
setterDescriptor = new PropertySetterDescriptorImpl(
|
||||
propertyDescriptor, annotations,
|
||||
resolveModalityFromModifiers(setter, propertyDescriptor.getModality()),
|
||||
resolveVisibilityFromModifiers(setter, propertyDescriptor.getVisibility()),
|
||||
/* isDefault = */ false, setter.hasModifier(EXTERNAL_KEYWORD),
|
||||
CallableMemberDescriptor.Kind.DECLARATION, null, KotlinSourceElementKt.toSourceElement(setter)
|
||||
);
|
||||
KtTypeReference returnTypeReference = setter.getReturnTypeReference();
|
||||
if (returnTypeReference != null) {
|
||||
KotlinType returnType = typeResolver.resolveType(scopeWithTypeParameters, returnTypeReference, trace, true);
|
||||
@@ -905,12 +906,13 @@ public class DescriptorResolver {
|
||||
}
|
||||
}
|
||||
|
||||
getterDescriptor = new PropertyGetterDescriptorImpl(propertyDescriptor, getterAnnotations,
|
||||
resolveModalityFromModifiers(getter, propertyDescriptor.getModality()),
|
||||
resolveVisibilityFromModifiers(getter, propertyDescriptor.getVisibility()),
|
||||
getter.hasBody(), false, getter.hasModifier(EXTERNAL_KEYWORD),
|
||||
CallableMemberDescriptor.Kind.DECLARATION, null, KotlinSourceElementKt
|
||||
.toSourceElement(getter));
|
||||
getterDescriptor = new PropertyGetterDescriptorImpl(
|
||||
propertyDescriptor, getterAnnotations,
|
||||
resolveModalityFromModifiers(getter, propertyDescriptor.getModality()),
|
||||
resolveVisibilityFromModifiers(getter, propertyDescriptor.getVisibility()),
|
||||
/* isDefault = */ false, getter.hasModifier(EXTERNAL_KEYWORD),
|
||||
CallableMemberDescriptor.Kind.DECLARATION, null, KotlinSourceElementKt.toSourceElement(getter)
|
||||
);
|
||||
if (returnType.isError() && !getter.hasBlockBody() && getter.hasBody()) {
|
||||
returnType = inferReturnTypeFromExpressionBody(storageManager, expressionTypingServices, trace, scopeWithTypeParameters,
|
||||
DataFlowInfoFactory.EMPTY, getter, getterDescriptor);
|
||||
|
||||
+4
-2
@@ -112,7 +112,8 @@ public class JavaPropertyDescriptor extends PropertyDescriptorImpl implements Ja
|
||||
if (getter != null) {
|
||||
newGetter = new PropertyGetterDescriptorImpl(
|
||||
enhanced, getter.getAnnotations(), getter.getModality(), getter.getVisibility(),
|
||||
getter.hasBody(), getter.isDefault(), getter.isExternal(), getKind(), getter, getter.getSource());
|
||||
getter.isDefault(), getter.isExternal(), getKind(), getter, getter.getSource()
|
||||
);
|
||||
newGetter.setInitialSignatureDescriptor(getter.getInitialSignatureDescriptor());
|
||||
newGetter.initialize(enhancedReturnType);
|
||||
}
|
||||
@@ -122,7 +123,8 @@ public class JavaPropertyDescriptor extends PropertyDescriptorImpl implements Ja
|
||||
if (setter != null) {
|
||||
newSetter = new PropertySetterDescriptorImpl(
|
||||
enhanced, setter.getAnnotations(), setter.getModality(), setter.getVisibility(),
|
||||
setter.hasBody(), setter.isDefault(), setter.isExternal(), getKind(), setter, setter.getSource());
|
||||
setter.isDefault(), setter.isExternal(), getKind(), setter, setter.getSource()
|
||||
);
|
||||
newSetter.setInitialSignatureDescriptor(newSetter.getInitialSignatureDescriptor());
|
||||
newSetter.initialize(setter.getValueParameters().get(0));
|
||||
}
|
||||
|
||||
@@ -21,8 +21,6 @@ import org.jetbrains.annotations.NotNull;
|
||||
import java.util.Collection;
|
||||
|
||||
public interface PropertyAccessorDescriptor extends FunctionDescriptor {
|
||||
boolean hasBody();
|
||||
|
||||
boolean isDefault();
|
||||
|
||||
@NotNull
|
||||
@@ -45,6 +43,4 @@ public interface PropertyAccessorDescriptor extends FunctionDescriptor {
|
||||
Kind kind,
|
||||
boolean copyOverrides
|
||||
);
|
||||
|
||||
boolean isExternal();
|
||||
}
|
||||
|
||||
-9
@@ -29,8 +29,6 @@ import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class PropertyAccessorDescriptorImpl extends DeclarationDescriptorNonRootImpl implements PropertyAccessorDescriptor {
|
||||
|
||||
private final boolean hasBody;
|
||||
private final boolean isDefault;
|
||||
private final boolean isExternal;
|
||||
private final Modality modality;
|
||||
@@ -46,7 +44,6 @@ public abstract class PropertyAccessorDescriptorImpl extends DeclarationDescript
|
||||
@NotNull PropertyDescriptor correspondingProperty,
|
||||
@NotNull Annotations annotations,
|
||||
@NotNull Name name,
|
||||
boolean hasBody,
|
||||
boolean isDefault,
|
||||
boolean isExternal,
|
||||
Kind kind,
|
||||
@@ -56,17 +53,11 @@ public abstract class PropertyAccessorDescriptorImpl extends DeclarationDescript
|
||||
this.modality = modality;
|
||||
this.visibility = visibility;
|
||||
this.correspondingProperty = correspondingProperty;
|
||||
this.hasBody = hasBody;
|
||||
this.isDefault = isDefault;
|
||||
this.isExternal = isExternal;
|
||||
this.kind = kind;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasBody() {
|
||||
return hasBody;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDefault() {
|
||||
return isDefault;
|
||||
|
||||
+2
-2
@@ -260,7 +260,7 @@ public class PropertyDescriptorImpl extends VariableDescriptorWithInitializerImp
|
||||
|
||||
PropertyGetterDescriptorImpl newGetter = getter == null ? null : new PropertyGetterDescriptorImpl(
|
||||
substitutedDescriptor, getter.getAnnotations(), newModality, getter.getVisibility(),
|
||||
getter.hasBody(), getter.isDefault(), getter.isExternal(), kind, original == null ? null : original.getGetter(),
|
||||
getter.isDefault(), getter.isExternal(), kind, original == null ? null : original.getGetter(),
|
||||
SourceElement.NO_SOURCE
|
||||
);
|
||||
if (newGetter != null) {
|
||||
@@ -270,7 +270,7 @@ public class PropertyDescriptorImpl extends VariableDescriptorWithInitializerImp
|
||||
}
|
||||
PropertySetterDescriptorImpl newSetter = setter == null ? null : new PropertySetterDescriptorImpl(
|
||||
substitutedDescriptor, setter.getAnnotations(), newModality, setter.getVisibility(),
|
||||
setter.hasBody(), setter.isDefault(), setter.isExternal(), kind, original == null ? null : original.getSetter(),
|
||||
setter.isDefault(), setter.isExternal(), kind, original == null ? null : original.getSetter(),
|
||||
SourceElement.NO_SOURCE
|
||||
);
|
||||
if (newSetter != null) {
|
||||
|
||||
+2
-4
@@ -38,16 +38,14 @@ public class PropertyGetterDescriptorImpl extends PropertyAccessorDescriptorImpl
|
||||
@NotNull Annotations annotations,
|
||||
@NotNull Modality modality,
|
||||
@NotNull Visibility visibility,
|
||||
boolean hasBody,
|
||||
boolean isDefault,
|
||||
boolean isExternal,
|
||||
@NotNull Kind kind,
|
||||
@Nullable PropertyGetterDescriptor original,
|
||||
@NotNull SourceElement source
|
||||
)
|
||||
{
|
||||
) {
|
||||
super(modality, visibility, correspondingProperty, annotations, Name.special("<get-" + correspondingProperty.getName() + ">"),
|
||||
hasBody, isDefault, isExternal, kind, source);
|
||||
isDefault, isExternal, kind, source);
|
||||
this.original = original != null ? original : this;
|
||||
}
|
||||
|
||||
|
||||
+2
-3
@@ -30,8 +30,8 @@ import java.util.List;
|
||||
import static org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilsKt.getBuiltIns;
|
||||
|
||||
public class PropertySetterDescriptorImpl extends PropertyAccessorDescriptorImpl implements PropertySetterDescriptor {
|
||||
|
||||
private ValueParameterDescriptor parameter;
|
||||
|
||||
@NotNull
|
||||
private final PropertySetterDescriptor original;
|
||||
|
||||
@@ -40,7 +40,6 @@ public class PropertySetterDescriptorImpl extends PropertyAccessorDescriptorImpl
|
||||
@NotNull Annotations annotations,
|
||||
@NotNull Modality modality,
|
||||
@NotNull Visibility visibility,
|
||||
boolean hasBody,
|
||||
boolean isDefault,
|
||||
boolean isExternal,
|
||||
@NotNull Kind kind,
|
||||
@@ -48,7 +47,7 @@ public class PropertySetterDescriptorImpl extends PropertyAccessorDescriptorImpl
|
||||
@NotNull SourceElement source
|
||||
) {
|
||||
super(modality, visibility, correspondingProperty, annotations, Name.special("<set-" + correspondingProperty.getName() + ">"),
|
||||
hasBody, isDefault, isExternal, kind, source);
|
||||
isDefault, isExternal, kind, source);
|
||||
this.original = original != null ? original : this;
|
||||
}
|
||||
|
||||
|
||||
@@ -71,10 +71,10 @@ public class DescriptorFactory {
|
||||
@NotNull Visibility visibility,
|
||||
@NotNull SourceElement sourceElement
|
||||
) {
|
||||
PropertySetterDescriptorImpl setterDescriptor =
|
||||
new PropertySetterDescriptorImpl(propertyDescriptor, annotations, propertyDescriptor.getModality(),
|
||||
visibility, !isDefault, isDefault, isExternal,
|
||||
CallableMemberDescriptor.Kind.DECLARATION, null, sourceElement);
|
||||
PropertySetterDescriptorImpl setterDescriptor = new PropertySetterDescriptorImpl(
|
||||
propertyDescriptor, annotations, propertyDescriptor.getModality(), visibility, isDefault, isExternal,
|
||||
CallableMemberDescriptor.Kind.DECLARATION, null, sourceElement
|
||||
);
|
||||
setterDescriptor.initializeDefault();
|
||||
return setterDescriptor;
|
||||
}
|
||||
@@ -103,10 +103,12 @@ public class DescriptorFactory {
|
||||
@NotNull Annotations annotations,
|
||||
boolean isDefault,
|
||||
boolean isExternal,
|
||||
@NotNull SourceElement sourceElement) {
|
||||
return new PropertyGetterDescriptorImpl(propertyDescriptor, annotations, propertyDescriptor.getModality(),
|
||||
propertyDescriptor.getVisibility(), !isDefault, isDefault, isExternal,
|
||||
CallableMemberDescriptor.Kind.DECLARATION, null, sourceElement);
|
||||
@NotNull SourceElement sourceElement
|
||||
) {
|
||||
return new PropertyGetterDescriptorImpl(
|
||||
propertyDescriptor, annotations, propertyDescriptor.getModality(), propertyDescriptor.getVisibility(),
|
||||
isDefault, isExternal, CallableMemberDescriptor.Kind.DECLARATION, null, sourceElement
|
||||
);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
||||
+4
-6
@@ -40,7 +40,7 @@ class MemberDeserializer(private val c: DeserializationContext) {
|
||||
Deserialization.modality(Flags.MODALITY.get(flags)),
|
||||
Deserialization.visibility(Flags.VISIBILITY.get(flags)),
|
||||
Flags.IS_VAR.get(flags),
|
||||
c.nameResolver.getName(proto.getName()),
|
||||
c.nameResolver.getName(proto.name),
|
||||
Deserialization.memberKind(Flags.MEMBER_KIND.get(flags)),
|
||||
Flags.IS_LATEINIT.get(flags),
|
||||
Flags.IS_CONST.get(flags),
|
||||
@@ -75,16 +75,15 @@ class MemberDeserializer(private val c: DeserializationContext) {
|
||||
getAnnotations(proto, getterFlags, AnnotatedCallableKind.PROPERTY_GETTER),
|
||||
Deserialization.modality(Flags.MODALITY.get(getterFlags)),
|
||||
Deserialization.visibility(Flags.VISIBILITY.get(getterFlags)),
|
||||
/* hasBody = */ isNotDefault,
|
||||
/* isDefault = */ !isNotDefault,
|
||||
/* isExternal = */ isExternal,
|
||||
property.getKind(), null, SourceElement.NO_SOURCE
|
||||
property.kind, null, SourceElement.NO_SOURCE
|
||||
)
|
||||
}
|
||||
else {
|
||||
DescriptorFactory.createDefaultGetter(property, Annotations.EMPTY)
|
||||
}
|
||||
getter.initialize(property.getReturnType())
|
||||
getter.initialize(property.returnType)
|
||||
getter
|
||||
}
|
||||
else {
|
||||
@@ -101,10 +100,9 @@ class MemberDeserializer(private val c: DeserializationContext) {
|
||||
getAnnotations(proto, setterFlags, AnnotatedCallableKind.PROPERTY_SETTER),
|
||||
Deserialization.modality(Flags.MODALITY.get(setterFlags)),
|
||||
Deserialization.visibility(Flags.VISIBILITY.get(setterFlags)),
|
||||
/* hasBody = */ isNotDefault,
|
||||
/* isDefault = */ !isNotDefault,
|
||||
/* isExternal = */ isExternal,
|
||||
property.getKind(), null, SourceElement.NO_SOURCE
|
||||
property.kind, null, SourceElement.NO_SOURCE
|
||||
)
|
||||
val setterLocal = local.childContext(setter, listOf())
|
||||
val valueParameters = setterLocal.memberDeserializer.valueParameters(
|
||||
|
||||
+2
-2
@@ -122,10 +122,10 @@ private fun genProperty(
|
||||
Visibilities.PUBLIC,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
CallableMemberDescriptor.Kind.SYNTHESIZED,
|
||||
null,
|
||||
SourceElement.NO_SOURCE)
|
||||
SourceElement.NO_SOURCE
|
||||
)
|
||||
|
||||
getter.initialize(null)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user