From 80bf7316c718cd12e982a8c0b882d530838352cf Mon Sep 17 00:00:00 2001 From: Denis Zharkov Date: Mon, 26 Oct 2015 10:50:59 +0300 Subject: [PATCH] Add 'preserveSource' parameter to methods making substitution copies --- .../sam/SamAdapterFunctionDescriptor.java | 3 ++- .../synthetic/SamAdapterFunctionsScope.kt | 9 +++++--- .../JavaConstructorDescriptor.java | 7 ++++--- .../descriptors/JavaMethodDescriptor.java | 7 ++++--- .../functions/FunctionInvokeDescriptor.kt | 5 +++-- .../impl/ConstructorDescriptorImpl.java | 5 +++-- .../impl/FunctionDescriptorImpl.java | 21 +++++++++++++------ .../impl/ScriptCodeDescriptor.java | 3 ++- .../impl/SimpleFunctionDescriptorImpl.java | 15 +++++++------ .../ErrorSimpleFunctionDescriptorImpl.java | 3 ++- .../DeserializedConstructorDescriptor.kt | 4 +++- .../DeserializedSimpleFunctionDescriptor.java | 3 ++- 12 files changed, 55 insertions(+), 30 deletions(-) diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/sam/SamAdapterFunctionDescriptor.java b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/sam/SamAdapterFunctionDescriptor.java index d1f22e28a67..18f068b275e 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/sam/SamAdapterFunctionDescriptor.java +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/load/java/sam/SamAdapterFunctionDescriptor.java @@ -55,7 +55,8 @@ import org.jetbrains.kotlin.name.Name; @NotNull DeclarationDescriptor newOwner, @Nullable FunctionDescriptor original, @NotNull Kind kind, - @Nullable Name newName + @Nullable Name newName, + boolean preserveSource ) { return new SamAdapterFunctionDescriptor(newOwner, (SimpleFunctionDescriptor) original, kind, declaration); } diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/SamAdapterFunctionsScope.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/SamAdapterFunctionsScope.kt index 85d6d0194f0..b9f687a33f1 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/SamAdapterFunctionsScope.kt +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/SamAdapterFunctionsScope.kt @@ -19,6 +19,7 @@ package org.jetbrains.kotlin.synthetic import com.intellij.util.SmartList import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.annotations.Annotations +import org.jetbrains.kotlin.descriptors.impl.FunctionDescriptorImpl import org.jetbrains.kotlin.descriptors.impl.SimpleFunctionDescriptorImpl import org.jetbrains.kotlin.incremental.components.LookupLocation import org.jetbrains.kotlin.load.java.sam.SingleAbstractMethodUtils @@ -145,7 +146,8 @@ class SamAdapterFunctionsScope(storageManager: StorageManager) : BaseImportingSc newOwner: DeclarationDescriptor, original: FunctionDescriptor?, kind: CallableMemberDescriptor.Kind, - newName: Name? + newName: Name?, + preserveSource: Boolean ): MyFunctionDescriptor { return MyFunctionDescriptor( containingDeclaration, original as SimpleFunctionDescriptor?, annotations, newName ?: name, kind, source @@ -170,12 +172,13 @@ class SamAdapterFunctionsScope(storageManager: StorageManager) : BaseImportingSc newValueParameterDescriptors: MutableList, newExtensionReceiverParameterType: KotlinType?, newReturnType: KotlinType, - name: Name? + name: Name?, + preserveSource: Boolean ): FunctionDescriptor? { val descriptor = super.doSubstitute( originalSubstitutor, newOwner, newModality, newVisibility, newIsOperator, newIsInfix, newIsExternal, newIsInline, newIsTailrec, original, - copyOverrides, kind, newValueParameterDescriptors, newExtensionReceiverParameterType, newReturnType, name) + copyOverrides, kind, newValueParameterDescriptors, newExtensionReceiverParameterType, newReturnType, name, preserveSource) as MyFunctionDescriptor? ?: return null if (original == null) { diff --git a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/descriptors/JavaConstructorDescriptor.java b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/descriptors/JavaConstructorDescriptor.java index 19eba28aa77..27f69fc62dd 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/descriptors/JavaConstructorDescriptor.java +++ b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/descriptors/JavaConstructorDescriptor.java @@ -77,7 +77,8 @@ public class JavaConstructorDescriptor extends ConstructorDescriptorImpl impleme @NotNull DeclarationDescriptor newOwner, @Nullable FunctionDescriptor original, @NotNull Kind kind, - @Nullable Name newName + @Nullable Name newName, + boolean preserveSource ) { if (kind != Kind.DECLARATION && kind != Kind.SYNTHESIZED) { throw new IllegalStateException( @@ -91,7 +92,7 @@ public class JavaConstructorDescriptor extends ConstructorDescriptorImpl impleme assert newName == null : "Attempt to rename constructor: " + this; JavaConstructorDescriptor result = new JavaConstructorDescriptor( - (ClassDescriptor) newOwner, this, getAnnotations(), isPrimary, kind, SourceElement.NO_SOURCE + (ClassDescriptor) newOwner, this, getAnnotations(), isPrimary, kind, getSourceToUseForCopy(preserveSource, original) ); result.setHasStableParameterNames(hasStableParameterNames()); result.setHasSynthesizedParameterNames(hasSynthesizedParameterNames()); @@ -105,7 +106,7 @@ public class JavaConstructorDescriptor extends ConstructorDescriptorImpl impleme @NotNull List enhancedValueParametersTypes, @NotNull KotlinType enhancedReturnType ) { - JavaConstructorDescriptor enhanced = createSubstitutedCopy(getContainingDeclaration(), getOriginal(), getKind(), null); + JavaConstructorDescriptor enhanced = createSubstitutedCopy(getContainingDeclaration(), getOriginal(), getKind(), null, false); // We do not use doSubstitute here as in JavaMethodDescriptor.enhance because type parameters of constructor belongs to class enhanced.initialize( enhancedReceiverType, diff --git a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/descriptors/JavaMethodDescriptor.java b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/descriptors/JavaMethodDescriptor.java index 82c9ea28b40..07cfc5c3f17 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/descriptors/JavaMethodDescriptor.java +++ b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/descriptors/JavaMethodDescriptor.java @@ -114,7 +114,8 @@ public class JavaMethodDescriptor extends SimpleFunctionDescriptorImpl implement @NotNull DeclarationDescriptor newOwner, @Nullable FunctionDescriptor original, @NotNull Kind kind, - @Nullable Name newName + @Nullable Name newName, + boolean preserveSource ) { JavaMethodDescriptor result = new JavaMethodDescriptor( newOwner, @@ -122,7 +123,7 @@ public class JavaMethodDescriptor extends SimpleFunctionDescriptorImpl implement getAnnotations(), newName != null ? newName : getName(), kind, - original != null ? original.getSource() : SourceElement.NO_SOURCE + getSourceToUseForCopy(preserveSource, original) ); result.setParameterNamesStatus(hasStableParameterNames(), hasSynthesizedParameterNames()); return result; @@ -146,7 +147,7 @@ public class JavaMethodDescriptor extends SimpleFunctionDescriptorImpl implement isOperator(), isInfix(), isExternal(), isInline(), isTailrec(), getOriginal(), /* copyOverrides = */ true, getKind(), enhancedValueParameters, enhancedReceiverType, enhancedReturnType, - null); + null, false); assert enhancedMethod != null : "null after substitution while enhancing " + toString(); return enhancedMethod; diff --git a/core/descriptors/src/org/jetbrains/kotlin/builtins/functions/FunctionInvokeDescriptor.kt b/core/descriptors/src/org/jetbrains/kotlin/builtins/functions/FunctionInvokeDescriptor.kt index 177034ddd6b..b23d423bc54 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/builtins/functions/FunctionInvokeDescriptor.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/builtins/functions/FunctionInvokeDescriptor.kt @@ -18,6 +18,7 @@ package org.jetbrains.kotlin.builtins.functions import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.annotations.Annotations +import org.jetbrains.kotlin.descriptors.impl.FunctionDescriptorImpl import org.jetbrains.kotlin.descriptors.impl.SimpleFunctionDescriptorImpl import org.jetbrains.kotlin.descriptors.impl.ValueParameterDescriptorImpl import org.jetbrains.kotlin.name.Name @@ -42,8 +43,8 @@ public class FunctionInvokeDescriptor private constructor( newOwner: DeclarationDescriptor, original: FunctionDescriptor?, kind: CallableMemberDescriptor.Kind, - newName: Name? - ): FunctionInvokeDescriptor { + newName: Name?, preserveSource: Boolean + ): FunctionDescriptorImpl { return FunctionInvokeDescriptor(newOwner, original as FunctionInvokeDescriptor?, kind) } diff --git a/core/descriptors/src/org/jetbrains/kotlin/descriptors/impl/ConstructorDescriptorImpl.java b/core/descriptors/src/org/jetbrains/kotlin/descriptors/impl/ConstructorDescriptorImpl.java index e1425e8cb99..42ecf198b0e 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/descriptors/impl/ConstructorDescriptorImpl.java +++ b/core/descriptors/src/org/jetbrains/kotlin/descriptors/impl/ConstructorDescriptorImpl.java @@ -115,7 +115,8 @@ public class ConstructorDescriptorImpl extends FunctionDescriptorImpl implements @NotNull DeclarationDescriptor newOwner, @Nullable FunctionDescriptor original, @NotNull Kind kind, - @Nullable Name newName + @Nullable Name newName, + boolean preserveSource ) { if (kind != Kind.DECLARATION && kind != Kind.SYNTHESIZED) { throw new IllegalStateException("Attempt at creating a constructor that is not a declaration: \n" + @@ -131,7 +132,7 @@ public class ConstructorDescriptorImpl extends FunctionDescriptorImpl implements getAnnotations(), isPrimary, Kind.DECLARATION, - SourceElement.NO_SOURCE + getSourceToUseForCopy(preserveSource, original) ); } diff --git a/core/descriptors/src/org/jetbrains/kotlin/descriptors/impl/FunctionDescriptorImpl.java b/core/descriptors/src/org/jetbrains/kotlin/descriptors/impl/FunctionDescriptorImpl.java index b7e31037a95..995f4e89878 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/descriptors/impl/FunctionDescriptorImpl.java +++ b/core/descriptors/src/org/jetbrains/kotlin/descriptors/impl/FunctionDescriptorImpl.java @@ -269,9 +269,9 @@ public abstract class FunctionDescriptorImpl extends DeclarationDescriptorNonRoo @NotNull Kind kind ) { return doSubstitute(originalSubstitutor, - newOwner, newModality, newVisibility, isOperator, isInfix, isExternal, isInline, isTailrec, original, copyOverrides, kind, - getValueParameters(), getExtensionReceiverParameterType(), getReturnType(), - null); + newOwner, newModality, newVisibility, isOperator, isInfix, isExternal, isInline, isTailrec, original, copyOverrides, kind, + getValueParameters(), getExtensionReceiverParameterType(), getReturnType(), + null, false); } @Nullable @@ -298,9 +298,10 @@ public abstract class FunctionDescriptorImpl extends DeclarationDescriptorNonRoo @NotNull List newValueParameterDescriptors, @Nullable KotlinType newExtensionReceiverParameterType, @NotNull KotlinType newReturnType, - @Nullable Name name + @Nullable Name name, + boolean preserveSource ) { - FunctionDescriptorImpl substitutedDescriptor = createSubstitutedCopy(newOwner, original, kind, name); + FunctionDescriptorImpl substitutedDescriptor = createSubstitutedCopy(newOwner, original, kind, name, preserveSource); List originalTypeParameters = getTypeParameters(); List substitutedTypeParameters = new ArrayList(originalTypeParameters.size()); @@ -375,9 +376,17 @@ public abstract class FunctionDescriptorImpl extends DeclarationDescriptorNonRoo @NotNull DeclarationDescriptor newOwner, @Nullable FunctionDescriptor original, @NotNull Kind kind, - @Nullable Name newName + @Nullable Name newName, + boolean preserveSource ); + @NotNull + protected SourceElement getSourceToUseForCopy(boolean preserveSource, @Nullable FunctionDescriptor original) { + return preserveSource + ? (original != null ? original.getSource() : getOriginal().getSource()) + : SourceElement.NO_SOURCE; + } + @Override public R accept(DeclarationDescriptorVisitor visitor, D data) { return visitor.visitFunctionDescriptor(this, data); diff --git a/core/descriptors/src/org/jetbrains/kotlin/descriptors/impl/ScriptCodeDescriptor.java b/core/descriptors/src/org/jetbrains/kotlin/descriptors/impl/ScriptCodeDescriptor.java index 57828f0b43c..9fddf225085 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/descriptors/impl/ScriptCodeDescriptor.java +++ b/core/descriptors/src/org/jetbrains/kotlin/descriptors/impl/ScriptCodeDescriptor.java @@ -46,7 +46,8 @@ public class ScriptCodeDescriptor extends FunctionDescriptorImpl { @NotNull DeclarationDescriptor newOwner, @Nullable FunctionDescriptor original, @NotNull Kind kind, - @Nullable Name newName + @Nullable Name newName, + boolean preserveSource ) { throw new IllegalStateException("no need to copy script code descriptor"); } diff --git a/core/descriptors/src/org/jetbrains/kotlin/descriptors/impl/SimpleFunctionDescriptorImpl.java b/core/descriptors/src/org/jetbrains/kotlin/descriptors/impl/SimpleFunctionDescriptorImpl.java index dda85dfdcfe..669f6b8d492 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/descriptors/impl/SimpleFunctionDescriptorImpl.java +++ b/core/descriptors/src/org/jetbrains/kotlin/descriptors/impl/SimpleFunctionDescriptorImpl.java @@ -77,7 +77,8 @@ public class SimpleFunctionDescriptorImpl extends FunctionDescriptorImpl impleme @NotNull DeclarationDescriptor newOwner, @Nullable FunctionDescriptor original, @NotNull Kind kind, - @Nullable Name newName + @Nullable Name newName, + boolean preserveSource ) { return new SimpleFunctionDescriptorImpl( newOwner, @@ -86,7 +87,7 @@ public class SimpleFunctionDescriptorImpl extends FunctionDescriptorImpl impleme getAnnotations(), newName != null ? newName : getName(), kind, - SourceElement.NO_SOURCE + getSourceToUseForCopy(preserveSource, original) ); } @@ -109,20 +110,22 @@ public class SimpleFunctionDescriptorImpl extends FunctionDescriptorImpl impleme @NotNull @Override public SimpleFunctionDescriptor createRenamedCopy(@NotNull Name name) { + //noinspection ConstantConditions return (SimpleFunctionDescriptorImpl) doSubstitute( TypeSubstitutor.EMPTY, getContainingDeclaration(), getModality(), getVisibility(), isOperator(), isInfix(), isExternal(), isInline(), isTailrec(), - null, /* copyOverrides = */ true, getKind(), getValueParameters(), getExtensionReceiverParameterType(), getReturnType(), name - ); + null, /* copyOverrides = */ true, getKind(), getValueParameters(), getExtensionReceiverParameterType(), getReturnType(), name, + /* preserveSource = */ true); } @NotNull @Override public SimpleFunctionDescriptor createCopyWithNewValueParameters(@NotNull List valueParameters) { + //noinspection ConstantConditions return (SimpleFunctionDescriptorImpl) doSubstitute( TypeSubstitutor.EMPTY, getContainingDeclaration(), getModality(), getVisibility(), isOperator(), isInfix(), isExternal(), isInline(), isTailrec(), - null, /* copyOverrides = */ true, getKind(), valueParameters, getExtensionReceiverParameterType(), getReturnType(), null - ); + null, /* copyOverrides = */ true, getKind(), valueParameters, getExtensionReceiverParameterType(), getReturnType(), null, + /* preserveSource = */ true); } } diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/error/ErrorSimpleFunctionDescriptorImpl.java b/core/descriptors/src/org/jetbrains/kotlin/types/error/ErrorSimpleFunctionDescriptorImpl.java index 302bc2ab188..75bda2f6454 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/error/ErrorSimpleFunctionDescriptorImpl.java +++ b/core/descriptors/src/org/jetbrains/kotlin/types/error/ErrorSimpleFunctionDescriptorImpl.java @@ -43,7 +43,8 @@ public class ErrorSimpleFunctionDescriptorImpl extends SimpleFunctionDescriptorI @NotNull DeclarationDescriptor newOwner, @Nullable FunctionDescriptor original, @NotNull Kind kind, - @Nullable Name newName + @Nullable Name newName, + boolean preserveSource ) { return this; } diff --git a/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/descriptors/DeserializedConstructorDescriptor.kt b/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/descriptors/DeserializedConstructorDescriptor.kt index 16c0bc7e36f..cbd2a190988 100644 --- a/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/descriptors/DeserializedConstructorDescriptor.kt +++ b/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/descriptors/DeserializedConstructorDescriptor.kt @@ -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.descriptors.impl.FunctionDescriptorImpl import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.serialization.ProtoBuf import org.jetbrains.kotlin.serialization.deserialization.NameResolver @@ -40,7 +41,8 @@ public class DeserializedConstructorDescriptor( newOwner: DeclarationDescriptor, original: FunctionDescriptor?, kind: CallableMemberDescriptor.Kind, - newName: Name? + newName: Name?, + preserveSource: Boolean ): DeserializedConstructorDescriptor { return DeserializedConstructorDescriptor( newOwner as ClassDescriptor, original as ConstructorDescriptor?, diff --git a/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/descriptors/DeserializedSimpleFunctionDescriptor.java b/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/descriptors/DeserializedSimpleFunctionDescriptor.java index be11d06c2cb..28eb5728f68 100644 --- a/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/descriptors/DeserializedSimpleFunctionDescriptor.java +++ b/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/descriptors/DeserializedSimpleFunctionDescriptor.java @@ -60,7 +60,8 @@ public class DeserializedSimpleFunctionDescriptor extends SimpleFunctionDescript @NotNull DeclarationDescriptor newOwner, @Nullable FunctionDescriptor original, @NotNull Kind kind, - @Nullable Name newName + @Nullable Name newName, + boolean preserveSource ) { return new DeserializedSimpleFunctionDescriptor( newOwner,