properly rewrite *Descriptor owner in FunctionDescriptorImpl.copy()

This commit is contained in:
Stepan Koltsov
2012-02-06 20:51:25 +04:00
parent 7364633be3
commit f4ffbd3360
6 changed files with 34 additions and 29 deletions
@@ -23,8 +23,8 @@ public class ConstructorDescriptorImpl extends FunctionDescriptorImpl implements
this.isPrimary = isPrimary; this.isPrimary = isPrimary;
} }
public ConstructorDescriptorImpl(@NotNull ConstructorDescriptor original, @NotNull List<AnnotationDescriptor> annotations, boolean isPrimary) { public ConstructorDescriptorImpl(@NotNull ClassDescriptor containingDeclaration, @NotNull ConstructorDescriptor original, @NotNull List<AnnotationDescriptor> annotations, boolean isPrimary) {
super(original.getContainingDeclaration(), original, annotations, "<init>"); super(containingDeclaration, original, annotations, "<init>");
this.isPrimary = isPrimary; this.isPrimary = isPrimary;
} }
@@ -85,8 +85,9 @@ public class ConstructorDescriptorImpl extends FunctionDescriptorImpl implements
} }
@Override @Override
protected FunctionDescriptorImpl createSubstitutedCopy() { protected FunctionDescriptorImpl createSubstitutedCopy(DeclarationDescriptor newOwner, boolean preserveOriginal) {
return new ConstructorDescriptorImpl( return new ConstructorDescriptorImpl(
(ClassDescriptor) newOwner,
this, this,
Collections.<AnnotationDescriptor>emptyList(), // TODO Collections.<AnnotationDescriptor>emptyList(), // TODO
isPrimary); isPrimary);
@@ -135,7 +135,12 @@ public abstract class FunctionDescriptorImpl extends DeclarationDescriptorImpl i
if (originalSubstitutor.isEmpty()) { if (originalSubstitutor.isEmpty()) {
return this; return this;
} }
FunctionDescriptorImpl substitutedDescriptor = createSubstitutedCopy(); return doSubstitute(originalSubstitutor, getContainingDeclaration(), modality, true);
}
protected FunctionDescriptor doSubstitute(TypeSubstitutor originalSubstitutor,
DeclarationDescriptor newOwner, Modality newModality, boolean preserveOriginal) {
FunctionDescriptorImpl substitutedDescriptor = createSubstitutedCopy(newOwner, preserveOriginal);
List<TypeParameterDescriptor> substitutedTypeParameters = Lists.newArrayList(); List<TypeParameterDescriptor> substitutedTypeParameters = Lists.newArrayList();
TypeSubstitutor substitutor = DescriptorSubstitutor.substituteTypeParameters(getTypeParameters(), originalSubstitutor, substitutedDescriptor, substitutedTypeParameters); TypeSubstitutor substitutor = DescriptorSubstitutor.substituteTypeParameters(getTypeParameters(), originalSubstitutor, substitutedDescriptor, substitutedTypeParameters);
@@ -147,7 +152,7 @@ public abstract class FunctionDescriptorImpl extends DeclarationDescriptorImpl i
return null; return null;
} }
} }
ReceiverDescriptor substitutedExpectedThis = NO_RECEIVER; ReceiverDescriptor substitutedExpectedThis = NO_RECEIVER;
if (expectedThisObject.exists()) { if (expectedThisObject.exists()) {
JetType substitutedType = substitutor.substitute(expectedThisObject.getType(), Variance.INVARIANT); JetType substitutedType = substitutor.substitute(expectedThisObject.getType(), Variance.INVARIANT);
@@ -173,7 +178,7 @@ public abstract class FunctionDescriptorImpl extends DeclarationDescriptorImpl i
substitutedTypeParameters, substitutedTypeParameters,
substitutedValueParameters, substitutedValueParameters,
substitutedReturnType, substitutedReturnType,
modality, newModality,
visibility visibility
); );
for (FunctionDescriptor overriddenFunction : overriddenFunctions) { for (FunctionDescriptor overriddenFunction : overriddenFunctions) {
@@ -182,7 +187,7 @@ public abstract class FunctionDescriptorImpl extends DeclarationDescriptorImpl i
return substitutedDescriptor; return substitutedDescriptor;
} }
protected abstract FunctionDescriptorImpl createSubstitutedCopy(); protected abstract FunctionDescriptorImpl createSubstitutedCopy(DeclarationDescriptor newOwner, boolean preserveOriginal);
@Override @Override
public <R, D> R accept(DeclarationDescriptorVisitor<R, D> visitor, D data) { public <R, D> R accept(DeclarationDescriptorVisitor<R, D> visitor, D data) {
@@ -4,6 +4,7 @@ import com.google.common.collect.Lists;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor; import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.resolve.DescriptorUtils; import org.jetbrains.jet.lang.resolve.DescriptorUtils;
import org.jetbrains.jet.lang.types.TypeSubstitutor;
import java.util.List; import java.util.List;
@@ -27,28 +28,26 @@ public class NamedFunctionDescriptorImpl extends FunctionDescriptorImpl implemen
} }
@Override @Override
protected FunctionDescriptorImpl createSubstitutedCopy() { protected FunctionDescriptorImpl createSubstitutedCopy(DeclarationDescriptor newOwner, boolean preserveOriginal) {
return new NamedFunctionDescriptorImpl( if (preserveOriginal) {
getContainingDeclaration(), return new NamedFunctionDescriptorImpl(
this, newOwner,
// TODO : safeSubstitute getOriginal(),
getAnnotations(), // TODO : safeSubstitute
getName()); getAnnotations(),
getName());
} else {
return new NamedFunctionDescriptorImpl(
newOwner,
// TODO : safeSubstitute
getAnnotations(),
getName());
}
} }
@NotNull @NotNull
@Override @Override
public NamedFunctionDescriptor copy(DeclarationDescriptor newOwner, boolean makeNonAbstract) { public NamedFunctionDescriptor copy(DeclarationDescriptor newOwner, boolean makeNonAbstract) {
NamedFunctionDescriptorImpl copy = new NamedFunctionDescriptorImpl(newOwner, Lists.newArrayList(getAnnotations()), getName()); return (NamedFunctionDescriptor) doSubstitute(TypeSubstitutor.EMPTY, newOwner, DescriptorUtils.convertModality(modality, makeNonAbstract), false);
copy.initialize(
getReceiverParameter().exists() ? getReceiverParameter().getType() : null,
expectedThisObject,
DescriptorUtils.copyTypeParameters(copy, typeParameters),
DescriptorUtils.copyValueParameters(copy, unsubstitutedValueParameters),
unsubstitutedReturnType,
DescriptorUtils.convertModality(modality, makeNonAbstract),
visibility
);
return copy;
} }
} }
@@ -36,7 +36,7 @@ public class VariableAsFunctionDescriptor extends FunctionDescriptorImpl {
} }
@Override @Override
protected FunctionDescriptorImpl createSubstitutedCopy() { protected FunctionDescriptorImpl createSubstitutedCopy(DeclarationDescriptor newOwner, boolean preserveOriginal) {
throw new IllegalStateException(); throw new IllegalStateException();
} }
} }
@@ -20,7 +20,7 @@ public class ExpressionAsFunctionDescriptor extends FunctionDescriptorImpl {
} }
@Override @Override
protected FunctionDescriptorImpl createSubstitutedCopy() { protected FunctionDescriptorImpl createSubstitutedCopy(DeclarationDescriptor newOwner, boolean preserveOriginal) {
throw new IllegalStateException(); throw new IllegalStateException();
} }
@@ -25,7 +25,7 @@ public class NamedFunctionDescriptorErrorImpl extends NamedFunctionDescriptorImp
} }
@Override @Override
protected FunctionDescriptorImpl createSubstitutedCopy() { protected FunctionDescriptorImpl createSubstitutedCopy(DeclarationDescriptor newOwner, boolean preserveOriginal) {
return this; return this;
} }