Introduce and implement SimpleFunctionDescriptor.createRenamedCopy

This commit is contained in:
Denis Zharkov
2015-10-23 11:22:11 +03:00
parent 69038063a6
commit ce34550c42
13 changed files with 76 additions and 28 deletions
@@ -23,6 +23,7 @@ import org.jetbrains.kotlin.descriptors.FunctionDescriptor;
import org.jetbrains.kotlin.descriptors.SimpleFunctionDescriptor;
import org.jetbrains.kotlin.load.java.descriptors.JavaMethodDescriptor;
import org.jetbrains.kotlin.load.java.descriptors.SamAdapterDescriptor;
import org.jetbrains.kotlin.name.Name;
/* package */ class SamAdapterFunctionDescriptor extends JavaMethodDescriptor implements SamAdapterDescriptor<JavaMethodDescriptor> {
private final JavaMethodDescriptor declaration;
@@ -53,7 +54,8 @@ import org.jetbrains.kotlin.load.java.descriptors.SamAdapterDescriptor;
protected JavaMethodDescriptor createSubstitutedCopy(
@NotNull DeclarationDescriptor newOwner,
@Nullable FunctionDescriptor original,
@NotNull Kind kind
@NotNull Kind kind,
@Nullable Name newName
) {
return new SamAdapterFunctionDescriptor(newOwner, (SimpleFunctionDescriptor) original, kind, declaration);
}
@@ -141,8 +141,15 @@ class SamAdapterFunctionsScope(storageManager: StorageManager) : BaseImportingSc
override fun hasStableParameterNames() = sourceFunction.hasStableParameterNames()
override fun hasSynthesizedParameterNames() = sourceFunction.hasSynthesizedParameterNames()
override fun createSubstitutedCopy(newOwner: DeclarationDescriptor, original: FunctionDescriptor?, kind: CallableMemberDescriptor.Kind): MyFunctionDescriptor {
return MyFunctionDescriptor(containingDeclaration, original as SimpleFunctionDescriptor?, annotations, name, kind, source).apply {
override fun createSubstitutedCopy(
newOwner: DeclarationDescriptor,
original: FunctionDescriptor?,
kind: CallableMemberDescriptor.Kind,
newName: Name?
): MyFunctionDescriptor {
return MyFunctionDescriptor(
containingDeclaration, original as SimpleFunctionDescriptor?, annotations, newName ?: name, kind, source
).apply {
sourceFunction = this@MyFunctionDescriptor.sourceFunction
}
}
@@ -162,13 +169,14 @@ class SamAdapterFunctionsScope(storageManager: StorageManager) : BaseImportingSc
kind: CallableMemberDescriptor.Kind,
newValueParameterDescriptors: MutableList<ValueParameterDescriptor>,
newExtensionReceiverParameterType: KotlinType?,
newReturnType: KotlinType
newReturnType: KotlinType,
name: Name?
): FunctionDescriptor? {
val descriptor = super<SimpleFunctionDescriptorImpl>.doSubstitute(
val descriptor = super.doSubstitute(
originalSubstitutor, newOwner, newModality, newVisibility,
newIsOperator, newIsInfix, newIsExternal, newIsInline, newIsTailrec, original,
copyOverrides, kind, newValueParameterDescriptors, newExtensionReceiverParameterType, newReturnType)
as MyFunctionDescriptor? ?: return null
copyOverrides, kind, newValueParameterDescriptors, newExtensionReceiverParameterType, newReturnType, name)
as MyFunctionDescriptor? ?: return null
if (original == null) {
throw UnsupportedOperationException("doSubstitute with no original should not be called for synthetic extension")
@@ -21,6 +21,7 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.descriptors.*;
import org.jetbrains.kotlin.descriptors.annotations.Annotations;
import org.jetbrains.kotlin.descriptors.impl.ConstructorDescriptorImpl;
import org.jetbrains.kotlin.name.Name;
import org.jetbrains.kotlin.types.KotlinType;
import java.util.List;
@@ -75,7 +76,8 @@ public class JavaConstructorDescriptor extends ConstructorDescriptorImpl impleme
protected JavaConstructorDescriptor createSubstitutedCopy(
@NotNull DeclarationDescriptor newOwner,
@Nullable FunctionDescriptor original,
@NotNull Kind kind
@NotNull Kind kind,
@Nullable Name newName
) {
if (kind != Kind.DECLARATION && kind != Kind.SYNTHESIZED) {
throw new IllegalStateException(
@@ -85,6 +87,9 @@ public class JavaConstructorDescriptor extends ConstructorDescriptorImpl impleme
"kind: " + kind
);
}
assert newName == null : "Attempt to rename constructor: " + this;
JavaConstructorDescriptor result = new JavaConstructorDescriptor(
(ClassDescriptor) newOwner, this, getAnnotations(), isPrimary, kind, SourceElement.NO_SOURCE
);
@@ -100,7 +105,7 @@ public class JavaConstructorDescriptor extends ConstructorDescriptorImpl impleme
@NotNull List<KotlinType> enhancedValueParametersTypes,
@NotNull KotlinType enhancedReturnType
) {
JavaConstructorDescriptor enhanced = createSubstitutedCopy(getContainingDeclaration(), getOriginal(), getKind());
JavaConstructorDescriptor enhanced = createSubstitutedCopy(getContainingDeclaration(), getOriginal(), getKind(), null);
// We do not use doSubstitute here as in JavaMethodDescriptor.enhance because type parameters of constructor belongs to class
enhanced.initialize(
enhancedReceiverType,
@@ -113,13 +113,14 @@ public class JavaMethodDescriptor extends SimpleFunctionDescriptorImpl implement
protected JavaMethodDescriptor createSubstitutedCopy(
@NotNull DeclarationDescriptor newOwner,
@Nullable FunctionDescriptor original,
@NotNull Kind kind
@NotNull Kind kind,
@Nullable Name newName
) {
JavaMethodDescriptor result = new JavaMethodDescriptor(
newOwner,
(SimpleFunctionDescriptor) original,
getAnnotations(),
getName(),
newName != null ? newName : getName(),
kind,
original != null ? original.getSource() : SourceElement.NO_SOURCE
);
@@ -144,8 +145,8 @@ public class JavaMethodDescriptor extends SimpleFunctionDescriptorImpl implement
TypeSubstitutor.EMPTY, getContainingDeclaration(), getModality(), getVisibility(),
isOperator(), isInfix(), isExternal(), isInline(), isTailrec(), getOriginal(),
/* copyOverrides = */ true, getKind(),
enhancedValueParameters, enhancedReceiverType, enhancedReturnType
);
enhancedValueParameters, enhancedReceiverType, enhancedReturnType,
null);
assert enhancedMethod != null : "null after substitution while enhancing " + toString();
return enhancedMethod;
@@ -41,7 +41,8 @@ public class FunctionInvokeDescriptor private constructor(
override fun createSubstitutedCopy(
newOwner: DeclarationDescriptor,
original: FunctionDescriptor?,
kind: CallableMemberDescriptor.Kind
kind: CallableMemberDescriptor.Kind,
newName: Name?
): FunctionInvokeDescriptor {
return FunctionInvokeDescriptor(newOwner, original as FunctionInvokeDescriptor?, kind)
}
@@ -17,6 +17,7 @@
package org.jetbrains.kotlin.descriptors;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.name.Name;
/**
* Simple functions are the ones with 'fun' keyword and function literals
@@ -26,6 +27,9 @@ public interface SimpleFunctionDescriptor extends FunctionDescriptor {
@Override
SimpleFunctionDescriptor copy(DeclarationDescriptor newOwner, Modality modality, Visibility visibility, Kind kind, boolean copyOverrides);
@NotNull
SimpleFunctionDescriptor createRenamedCopy(@NotNull Name name);
@NotNull
@Override
SimpleFunctionDescriptor getOriginal();
@@ -114,7 +114,8 @@ public class ConstructorDescriptorImpl extends FunctionDescriptorImpl implements
protected ConstructorDescriptorImpl createSubstitutedCopy(
@NotNull DeclarationDescriptor newOwner,
@Nullable FunctionDescriptor original,
@NotNull Kind kind
@NotNull Kind kind,
@Nullable Name newName
) {
if (kind != Kind.DECLARATION && kind != Kind.SYNTHESIZED) {
throw new IllegalStateException("Attempt at creating a constructor that is not a declaration: \n" +
@@ -123,6 +124,7 @@ public class ConstructorDescriptorImpl extends FunctionDescriptorImpl implements
"kind: " + kind);
}
assert original != null : "Attempt to create copy of constructor without preserving original: " + this;
assert newName == null : "Attempt to rename constructor: " + this;
return new ConstructorDescriptorImpl(
(ClassDescriptor) newOwner,
this,
@@ -270,8 +270,8 @@ public abstract class FunctionDescriptorImpl extends DeclarationDescriptorNonRoo
) {
return doSubstitute(originalSubstitutor,
newOwner, newModality, newVisibility, isOperator, isInfix, isExternal, isInline, isTailrec, original, copyOverrides, kind,
getValueParameters(), getExtensionReceiverParameterType(), getReturnType()
);
getValueParameters(), getExtensionReceiverParameterType(), getReturnType(),
null);
}
@Nullable
@@ -282,7 +282,8 @@ public abstract class FunctionDescriptorImpl extends DeclarationDescriptorNonRoo
@Nullable
protected FunctionDescriptor doSubstitute(@NotNull TypeSubstitutor originalSubstitutor,
protected FunctionDescriptor doSubstitute(
@NotNull TypeSubstitutor originalSubstitutor,
@NotNull DeclarationDescriptor newOwner,
@NotNull Modality newModality,
@NotNull Visibility newVisibility,
@@ -296,9 +297,10 @@ public abstract class FunctionDescriptorImpl extends DeclarationDescriptorNonRoo
@NotNull Kind kind,
@NotNull List<ValueParameterDescriptor> newValueParameterDescriptors,
@Nullable KotlinType newExtensionReceiverParameterType,
@NotNull KotlinType newReturnType
@NotNull KotlinType newReturnType,
@Nullable Name name
) {
FunctionDescriptorImpl substitutedDescriptor = createSubstitutedCopy(newOwner, original, kind);
FunctionDescriptorImpl substitutedDescriptor = createSubstitutedCopy(newOwner, original, kind, name);
List<TypeParameterDescriptor> originalTypeParameters = getTypeParameters();
List<TypeParameterDescriptor> substitutedTypeParameters = new ArrayList<TypeParameterDescriptor>(originalTypeParameters.size());
@@ -372,7 +374,8 @@ public abstract class FunctionDescriptorImpl extends DeclarationDescriptorNonRoo
protected abstract FunctionDescriptorImpl createSubstitutedCopy(
@NotNull DeclarationDescriptor newOwner,
@Nullable FunctionDescriptor original,
@NotNull Kind kind
@NotNull Kind kind,
@Nullable Name newName
);
@Override
@@ -45,7 +45,8 @@ public class ScriptCodeDescriptor extends FunctionDescriptorImpl {
protected FunctionDescriptorImpl createSubstitutedCopy(
@NotNull DeclarationDescriptor newOwner,
@Nullable FunctionDescriptor original,
@NotNull Kind kind
@NotNull Kind kind,
@Nullable Name newName
) {
throw new IllegalStateException("no need to copy script code descriptor");
}
@@ -76,14 +76,15 @@ public class SimpleFunctionDescriptorImpl extends FunctionDescriptorImpl impleme
protected FunctionDescriptorImpl createSubstitutedCopy(
@NotNull DeclarationDescriptor newOwner,
@Nullable FunctionDescriptor original,
@NotNull Kind kind
@NotNull Kind kind,
@Nullable Name newName
) {
return new SimpleFunctionDescriptorImpl(
newOwner,
(SimpleFunctionDescriptor) original,
// TODO : safeSubstitute
getAnnotations(),
getName(),
newName != null ? newName : getName(),
kind,
SourceElement.NO_SOURCE
);
@@ -104,4 +105,14 @@ public class SimpleFunctionDescriptorImpl extends FunctionDescriptorImpl impleme
null, copyOverrides, kind
);
}
@NotNull
@Override
public SimpleFunctionDescriptor createRenamedCopy(@NotNull Name name) {
return (SimpleFunctionDescriptorImpl) doSubstitute(
TypeSubstitutor.EMPTY, getContainingDeclaration(), getModality(), getVisibility(),
isOperator(), isInfix(), isExternal(), isInline(), isTailrec(),
null, /* copyOverrides = */ true, getKind(), getValueParameters(), getExtensionReceiverParameterType(), getReturnType(), name
);
}
}
@@ -40,7 +40,8 @@ public class ErrorSimpleFunctionDescriptorImpl extends SimpleFunctionDescriptorI
protected FunctionDescriptorImpl createSubstitutedCopy(
@NotNull DeclarationDescriptor newOwner,
@Nullable FunctionDescriptor original,
@NotNull Kind kind
@NotNull Kind kind,
@Nullable Name newName
) {
return this;
}
@@ -51,6 +52,12 @@ public class ErrorSimpleFunctionDescriptorImpl extends SimpleFunctionDescriptorI
return this;
}
@NotNull
@Override
public SimpleFunctionDescriptor createRenamedCopy(@NotNull Name name) {
return this;
}
@Override
public void addOverriddenDescriptor(@NotNull CallableMemberDescriptor overriddenFunction) {
// nop
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.serialization.deserialization.descriptors
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.descriptors.impl.ConstructorDescriptorImpl
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.serialization.ProtoBuf
import org.jetbrains.kotlin.serialization.deserialization.NameResolver
import org.jetbrains.kotlin.serialization.deserialization.TypeTable
@@ -38,7 +39,8 @@ public class DeserializedConstructorDescriptor(
override fun createSubstitutedCopy(
newOwner: DeclarationDescriptor,
original: FunctionDescriptor?,
kind: CallableMemberDescriptor.Kind
kind: CallableMemberDescriptor.Kind,
newName: Name?
): DeserializedConstructorDescriptor {
return DeserializedConstructorDescriptor(
newOwner as ClassDescriptor, original as ConstructorDescriptor?,
@@ -59,13 +59,14 @@ public class DeserializedSimpleFunctionDescriptor extends SimpleFunctionDescript
protected FunctionDescriptorImpl createSubstitutedCopy(
@NotNull DeclarationDescriptor newOwner,
@Nullable FunctionDescriptor original,
@NotNull Kind kind
@NotNull Kind kind,
@Nullable Name newName
) {
return new DeserializedSimpleFunctionDescriptor(
newOwner,
(DeserializedSimpleFunctionDescriptor) original,
getAnnotations(),
getName(),
newName != null ? newName : getName(),
kind,
proto,
nameResolver,