Merge branch 'master' of ssh://git.labs.intellij.net/jet

This commit is contained in:
svtk
2011-09-27 16:25:26 +04:00
29 changed files with 356 additions and 78 deletions
@@ -85,6 +85,9 @@ public class JavaClassMembersScope implements JetScope {
} }
for (PsiField field : psiClass.getAllFields()) { for (PsiField field : psiClass.getAllFields()) {
if (field.hasModifierProperty(PsiModifier.STATIC) != staticMembers) {
continue;
}
VariableDescriptor variableDescriptor = semanticServices.getDescriptorResolver().resolveFieldToVariableDescriptor(containingDeclaration, field); VariableDescriptor variableDescriptor = semanticServices.getDescriptorResolver().resolveFieldToVariableDescriptor(containingDeclaration, field);
allDescriptors.add(variableDescriptor); allDescriptors.add(variableDescriptor);
} }
@@ -11,4 +11,7 @@ public interface CallableMemberDescriptor extends CallableDescriptor, MemberDesc
@NotNull @NotNull
@Override @Override
Set<? extends CallableMemberDescriptor> getOverriddenDescriptors(); Set<? extends CallableMemberDescriptor> getOverriddenDescriptors();
@NotNull
CallableMemberDescriptor copy(DeclarationDescriptor newOwner, boolean makeNonAbstract);
} }
@@ -28,7 +28,7 @@ public class ConstructorDescriptorImpl extends FunctionDescriptorImpl implements
@Override @Override
@Deprecated @Deprecated
public FunctionDescriptorImpl initialize(@Nullable JetType receiverType, @NotNull List<TypeParameterDescriptor> typeParameters, @NotNull List<ValueParameterDescriptor> unsubstitutedValueParameters, @Nullable JetType unsubstitutedReturnType, Modality modality, Visibility visibility) { public FunctionDescriptorImpl initialize(@Nullable JetType receiverType, @NotNull List<TypeParameterDescriptor> typeParameters, @NotNull List<ValueParameterDescriptor> unsubstitutedValueParameters, @Nullable JetType unsubstitutedReturnType, Modality modality, @NotNull Visibility visibility) {
assert receiverType == null; assert receiverType == null;
return super.initialize(null, typeParameters, unsubstitutedValueParameters, unsubstitutedReturnType, modality, visibility); return super.initialize(null, typeParameters, unsubstitutedValueParameters, unsubstitutedReturnType, modality, visibility);
} }
@@ -78,4 +78,10 @@ public class ConstructorDescriptorImpl extends FunctionDescriptorImpl implements
Collections.<AnnotationDescriptor>emptyList(), // TODO Collections.<AnnotationDescriptor>emptyList(), // TODO
isPrimary); isPrimary);
} }
@NotNull
@Override
public ConstructorDescriptor copy(DeclarationDescriptor newOwner, boolean makeNonAbstract) {
throw new UnsupportedOperationException("Constructors should not be copied for overriding");
}
} }
@@ -23,4 +23,8 @@ public interface FunctionDescriptor extends CallableMemberDescriptor {
@Override @Override
@NotNull @NotNull
Set<? extends FunctionDescriptor> getOverriddenDescriptors(); Set<? extends FunctionDescriptor> getOverriddenDescriptors();
@NotNull
@Override
FunctionDescriptor copy(DeclarationDescriptor newOwner, boolean makeNonAbstract);
} }
@@ -5,6 +5,7 @@ import com.google.common.collect.Sets;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
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.scopes.receivers.ExtensionReceiver; import org.jetbrains.jet.lang.resolve.scopes.receivers.ExtensionReceiver;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor; import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
import org.jetbrains.jet.lang.types.DescriptorSubstitutor; import org.jetbrains.jet.lang.types.DescriptorSubstitutor;
@@ -175,24 +176,18 @@ public class FunctionDescriptorImpl extends DeclarationDescriptorImpl implements
return visitor.visitFunctionDescriptor(this, data); return visitor.visitFunctionDescriptor(this, data);
} }
// @Override @NotNull
// public boolean equals(Object obj) { @Override
// if (obj == null) return false; public FunctionDescriptor copy(DeclarationDescriptor newOwner, boolean makeNonAbstract) {
// if (obj.getClass() != FunctionDescriptorImpl.class) return false; FunctionDescriptorImpl copy = new FunctionDescriptorImpl(newOwner, Lists.newArrayList(getAnnotations()), getName());
// FunctionDescriptorImpl other = (FunctionDescriptorImpl) obj; copy.initialize(
// if (!eq(this.getName(), other.getName())) return false; getReceiver().exists() ? getReceiver().getType() : null,
// if (!eq(this.getContainingDeclaration(), other.getContainingDeclaration())) return false; DescriptorUtils.copyTypeParameters(copy, typeParameters),
// DescriptorUtils.copyValueParameters(copy, unsubstitutedValueParameters),
// } unsubstitutedReturnType,
// DescriptorUtils.convertModality(modality, makeNonAbstract),
// private static boolean eq(Object a, Object b) { visibility
// if (a == null) return b == null; );
// if (b == null) return false; return copy;
// return a.equals(b); }
// }
//
// @Override
// public int hashCode() {
// return super.hashCode(); // TODO
// }
} }
@@ -8,14 +8,14 @@ public enum Modality {
OPEN(true), OPEN(true),
ABSTRACT(true); ABSTRACT(true);
private final boolean open; private final boolean overridable;
private Modality(boolean open) { private Modality(boolean overridable) {
this.open = open; this.overridable = overridable;
} }
public boolean isOpen() { public boolean isOverridable() {
return open; return overridable;
} }
public static Modality convertFromFlags(boolean _abstract, boolean open) { public static Modality convertFromFlags(boolean _abstract, boolean open) {
@@ -172,7 +172,7 @@ public class MutableClassDescriptor extends MutableDeclarationDescriptor impleme
this.typeConstructor = new TypeConstructorImpl( this.typeConstructor = new TypeConstructorImpl(
this, this,
Collections.<AnnotationDescriptor>emptyList(), // TODO : pass annotations from the class? Collections.<AnnotationDescriptor>emptyList(), // TODO : pass annotations from the class?
!modality.isOpen(), !modality.isOverridable(),
getName(), getName(),
typeParameters, typeParameters,
supertypes); supertypes);
@@ -76,4 +76,10 @@ public abstract class PropertyAccessorDescriptor extends DeclarationDescriptorIm
public PropertyDescriptor getCorrespondingProperty() { public PropertyDescriptor getCorrespondingProperty() {
return correspondingProperty; return correspondingProperty;
} }
@NotNull
@Override
public PropertyAccessorDescriptor copy(DeclarationDescriptor newOwner, boolean makeNonAbstract) {
throw new UnsupportedOperationException("Accessors must be copied by the corresponding property");
}
} }
@@ -5,6 +5,7 @@ import com.google.common.collect.Sets;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
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.scopes.receivers.ExtensionReceiver; import org.jetbrains.jet.lang.resolve.scopes.receivers.ExtensionReceiver;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor; import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
import org.jetbrains.jet.lang.types.JetType; import org.jetbrains.jet.lang.types.JetType;
@@ -168,5 +169,27 @@ public class PropertyDescriptor extends VariableDescriptorImpl implements Callab
@Override @Override
public Set<? extends PropertyDescriptor> getOverriddenDescriptors() { public Set<? extends PropertyDescriptor> getOverriddenDescriptors() {
return overriddenProperties; return overriddenProperties;
}
@NotNull
@Override
public PropertyDescriptor copy(DeclarationDescriptor newOwner, boolean makeNonAbstract) {
PropertyDescriptor propertyDescriptor = new PropertyDescriptor(
newOwner,
Lists.newArrayList(getAnnotations()),
DescriptorUtils.convertModality(modality, makeNonAbstract), visibility, isVar,
receiver.exists() ? receiver.getType() : null, getName(), getInType(), getOutType());
PropertyGetterDescriptor newGetter = getter == null ? null : new PropertyGetterDescriptor(
propertyDescriptor, Lists.newArrayList(getter.getAnnotations()),
DescriptorUtils.convertModality(getter.getModality(), makeNonAbstract), getter.getVisibility(),
getter.getReturnType(),
getter.hasBody(), getter.isDefault());
PropertySetterDescriptor newSetter = setter == null ? null : new PropertySetterDescriptor(
DescriptorUtils.convertModality(setter.getModality(), makeNonAbstract), setter.getVisibility(),
propertyDescriptor,
Lists.newArrayList(setter.getAnnotations()),
setter.hasBody(), setter.isDefault());;
propertyDescriptor.initialize(DescriptorUtils.copyTypeParameters(propertyDescriptor, getTypeParameters()), newGetter, newSetter);
return propertyDescriptor;
} }
} }
@@ -18,7 +18,7 @@ public class PropertyGetterDescriptor extends PropertyAccessorDescriptor {
private final Set<PropertyGetterDescriptor> overriddenGetters = Sets.newHashSet(); private final Set<PropertyGetterDescriptor> overriddenGetters = Sets.newHashSet();
private JetType returnType; private JetType returnType;
public PropertyGetterDescriptor(@NotNull Modality modality, @NotNull Visibility visibility, @NotNull PropertyDescriptor correspondingProperty, @NotNull List<AnnotationDescriptor> annotations, @Nullable JetType returnType, boolean hasBody, boolean isDefault) { public PropertyGetterDescriptor(@NotNull PropertyDescriptor correspondingProperty, @NotNull List<AnnotationDescriptor> annotations, @NotNull Modality modality, @NotNull Visibility visibility, @Nullable JetType returnType, boolean hasBody, boolean isDefault) {
super(modality, visibility, correspondingProperty, annotations, "get-" + correspondingProperty.getName(), hasBody, isDefault); super(modality, visibility, correspondingProperty, annotations, "get-" + correspondingProperty.getName(), hasBody, isDefault);
this.returnType = returnType == null ? correspondingProperty.getOutType() : returnType; this.returnType = returnType == null ? correspondingProperty.getOutType() : returnType;
} }
@@ -64,5 +64,4 @@ public class PropertySetterDescriptor extends PropertyAccessorDescriptor {
public <R, D> R accept(DeclarationDescriptorVisitor<R, D> visitor, D data) { public <R, D> R accept(DeclarationDescriptorVisitor<R, D> visitor, D data) {
return visitor.visitPropertySetterDescriptor(this, data); return visitor.visitPropertySetterDescriptor(this, data);
} }
} }
@@ -1,5 +1,6 @@
package org.jetbrains.jet.lang.descriptors; package org.jetbrains.jet.lang.descriptors;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
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;
@@ -157,4 +158,9 @@ public class TypeParameterDescriptor extends DeclarationDescriptorImpl implement
public int getIndex() { public int getIndex() {
return index; return index;
} }
@NotNull
public TypeParameterDescriptor copy(@NotNull DeclarationDescriptor newOwner) {
return new TypeParameterDescriptor(newOwner, Lists.newArrayList(getAnnotations()), variance, getName(), index);
}
} }
@@ -23,4 +23,7 @@ public interface ValueParameterDescriptor extends VariableDescriptor {
@Override @Override
ValueParameterDescriptor getOriginal(); ValueParameterDescriptor getOriginal();
@NotNull
ValueParameterDescriptor copy(DeclarationDescriptor newOwner);
} }
@@ -1,5 +1,6 @@
package org.jetbrains.jet.lang.descriptors; package org.jetbrains.jet.lang.descriptors;
import com.google.common.collect.Lists;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor; import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
@@ -101,4 +102,10 @@ public class ValueParameterDescriptorImpl extends VariableDescriptorImpl impleme
public boolean isVar() { public boolean isVar() {
return isVar; return isVar;
} }
@NotNull
@Override
public ValueParameterDescriptor copy(@NotNull DeclarationDescriptor newOwner) {
return new ValueParameterDescriptorImpl(newOwner, index, Lists.newArrayList(getAnnotations()), getName(), getInType(), getOutType(), hasDefaultValue, isVararg);
}
} }
@@ -31,4 +31,10 @@ public class VariableAsFunctionDescriptor extends FunctionDescriptorImpl {
public VariableDescriptor getVariableDescriptor() { public VariableDescriptor getVariableDescriptor() {
return variableDescriptor; return variableDescriptor;
} }
@NotNull
@Override
public VariableAsFunctionDescriptor copy(DeclarationDescriptor newOwner, boolean makeNonAbstract) {
throw new UnsupportedOperationException("Should not be copied for overriding");
}
} }
@@ -140,6 +140,7 @@ public interface Errors {
SimpleDiagnosticFactory MANY_CLASS_OBJECTS = SimpleDiagnosticFactory.create(ERROR, "Only one class object is allowed per class"); SimpleDiagnosticFactory MANY_CLASS_OBJECTS = SimpleDiagnosticFactory.create(ERROR, "Only one class object is allowed per class");
SimpleDiagnosticFactory CLASS_OBJECT_NOT_ALLOWED = SimpleDiagnosticFactory.create(ERROR, "A class object is not allowed here"); SimpleDiagnosticFactory CLASS_OBJECT_NOT_ALLOWED = SimpleDiagnosticFactory.create(ERROR, "A class object is not allowed here");
SimpleDiagnosticFactory DELEGATION_IN_TRAIT = SimpleDiagnosticFactory.create(ERROR, "Traits cannot use delegation"); SimpleDiagnosticFactory DELEGATION_IN_TRAIT = SimpleDiagnosticFactory.create(ERROR, "Traits cannot use delegation");
SimpleDiagnosticFactory DELEGATION_NOT_TO_TRAIT = SimpleDiagnosticFactory.create(ERROR, "Only traits can be delegated to");
SimpleDiagnosticFactory NO_CONSTRUCTOR = SimpleDiagnosticFactory.create(ERROR, "This class does not have a constructor"); SimpleDiagnosticFactory NO_CONSTRUCTOR = SimpleDiagnosticFactory.create(ERROR, "This class does not have a constructor");
SimpleDiagnosticFactory NOT_A_CLASS = SimpleDiagnosticFactory.create(ERROR, "Not a class"); SimpleDiagnosticFactory NOT_A_CLASS = SimpleDiagnosticFactory.create(ERROR, "Not a class");
SimpleDiagnosticFactory ILLEGAL_ESCAPE_SEQUENCE = SimpleDiagnosticFactory.create(ERROR, "Illegal escape sequence"); SimpleDiagnosticFactory ILLEGAL_ESCAPE_SEQUENCE = SimpleDiagnosticFactory.create(ERROR, "Illegal escape sequence");
@@ -314,6 +315,7 @@ public interface Errors {
} }
}; };
ParameterizedDiagnosticFactory2<PropertyDescriptor, PropertyDescriptor> VAR_OVERRIDDEN_BY_VAL = new ParameterizedDiagnosticFactory2<PropertyDescriptor, PropertyDescriptor>(ERROR, "Var-property {0} cannot be overridden by val-property {1}", DescriptorRenderer.TEXT);
ParameterizedDiagnosticFactory2<JetClassOrObject, CallableMemberDescriptor> ABSTRACT_MEMBER_NOT_IMPLEMENTED = new ParameterizedDiagnosticFactory2<JetClassOrObject, CallableMemberDescriptor>(ERROR, "Class ''{0}'' must be declared abstract or implement abstract member {1}") { ParameterizedDiagnosticFactory2<JetClassOrObject, CallableMemberDescriptor> ABSTRACT_MEMBER_NOT_IMPLEMENTED = new ParameterizedDiagnosticFactory2<JetClassOrObject, CallableMemberDescriptor>(ERROR, "Class ''{0}'' must be declared abstract or implement abstract member {1}") {
@Override @Override
@@ -3,6 +3,7 @@ package org.jetbrains.jet.lang.psi;
import com.intellij.lang.ASTNode; import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement; import com.intellij.psi.PsiElement;
import com.intellij.psi.tree.IElementType; import com.intellij.psi.tree.IElementType;
import com.intellij.psi.tree.TokenSet;
import com.intellij.psi.util.PsiTreeUtil; import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@@ -11,6 +12,11 @@ import org.jetbrains.jet.lexer.JetTokens;
import java.util.List; import java.util.List;
import static org.jetbrains.jet.JetNodeTypes.PROPERTY_ACCESSOR;
import static org.jetbrains.jet.lexer.JetTokens.EQ;
import static org.jetbrains.jet.lexer.JetTokens.VAL_KEYWORD;
import static org.jetbrains.jet.lexer.JetTokens.VAR_KEYWORD;
/** /**
* @author max * @author max
*/ */
@@ -69,7 +75,7 @@ public class JetProperty extends JetTypeParameterListOwner implements JetModifie
@NotNull @NotNull
public List<JetPropertyAccessor> getAccessors() { public List<JetPropertyAccessor> getAccessors() {
return findChildrenByType(JetNodeTypes.PROPERTY_ACCESSOR); return findChildrenByType(PROPERTY_ACCESSOR);
} }
@Nullable @Nullable
@@ -92,7 +98,12 @@ public class JetProperty extends JetTypeParameterListOwner implements JetModifie
@Nullable @Nullable
public JetExpression getInitializer() { public JetExpression getInitializer() {
PsiElement eq = findChildByType(JetTokens.EQ); PsiElement eq = findChildByType(EQ);
return PsiTreeUtil.getNextSiblingOfType(eq, JetExpression.class); return PsiTreeUtil.getNextSiblingOfType(eq, JetExpression.class);
} }
@NotNull
public ASTNode getValOrVarNode() {
return getNode().findChildByType(TokenSet.create(VAL_KEYWORD, VAR_KEYWORD));
}
} }
@@ -40,6 +40,7 @@ public interface BindingContext {
WritableSlice<JetExpression, DeclarationDescriptor> VARIABLE_ASSIGNMENT = Slices.createSimpleSlice("VARIABLE_ASSIGNMENT"); WritableSlice<JetExpression, DeclarationDescriptor> VARIABLE_ASSIGNMENT = Slices.createSimpleSlice("VARIABLE_ASSIGNMENT");
WritableSlice<JetExpression, Boolean> PROCESSED = Slices.createSimpleSetSlice("PROCESSED"); WritableSlice<JetExpression, Boolean> PROCESSED = Slices.createSimpleSetSlice("PROCESSED");
WritableSlice<JetElement, Boolean> STATEMENT = Slices.createRemovableSetSlice("STATEMENT"); WritableSlice<JetElement, Boolean> STATEMENT = Slices.createRemovableSetSlice("STATEMENT");
WritableSlice<CallableMemberDescriptor, Boolean> DELEGATED = Slices.createRemovableSetSlice("DELEGATED");
WritableSlice<PropertyDescriptor, Boolean> BACKING_FIELD_REQUIRED = new Slices.SetSlice<PropertyDescriptor>("BACKING_FIELD_REQUIRED", RewritePolicy.DO_NOTHING) { WritableSlice<PropertyDescriptor, Boolean> BACKING_FIELD_REQUIRED = new Slices.SetSlice<PropertyDescriptor>("BACKING_FIELD_REQUIRED", RewritePolicy.DO_NOTHING) {
@Override @Override
@@ -136,6 +136,15 @@ public class BodyResolver {
} }
JetType supertype = context.getTrace().getBindingContext().get(BindingContext.TYPE, specifier.getTypeReference()); JetType supertype = context.getTrace().getBindingContext().get(BindingContext.TYPE, specifier.getTypeReference());
recordSupertype(specifier.getTypeReference(), supertype); recordSupertype(specifier.getTypeReference(), supertype);
if (supertype != null) {
DeclarationDescriptor declarationDescriptor = supertype.getConstructor().getDeclarationDescriptor();
if (declarationDescriptor instanceof ClassDescriptor) {
ClassDescriptor classDescriptor = (ClassDescriptor) declarationDescriptor;
if (classDescriptor.getKind() != ClassKind.TRAIT) {
context.getTrace().report(DELEGATION_NOT_TO_TRAIT.on(specifier.getTypeReference()));
}
}
}
JetExpression delegateExpression = specifier.getDelegateExpression(); JetExpression delegateExpression = specifier.getDelegateExpression();
if (delegateExpression != null) { if (delegateExpression != null) {
JetScope scope = scopeForConstructor == null JetScope scope = scopeForConstructor == null
@@ -708,16 +708,16 @@ public class ClassDescriptorResolver {
} }
getterDescriptor = new PropertyGetterDescriptor( getterDescriptor = new PropertyGetterDescriptor(
resolveModalityFromModifiers(trace, getter.getModifierList(), propertyDescriptor.getModality()), propertyDescriptor, annotations, resolveModalityFromModifiers(trace, getter.getModifierList(), propertyDescriptor.getModality()),
resolveVisibilityFromModifiers(trace, getter.getModifierList(), propertyDescriptor.getVisibility()), resolveVisibilityFromModifiers(trace, getter.getModifierList(), propertyDescriptor.getVisibility()),
propertyDescriptor, annotations, returnType, getter.getBodyExpression() != null, false); returnType, getter.getBodyExpression() != null, false);
trace.record(BindingContext.PROPERTY_ACCESSOR, getter, getterDescriptor); trace.record(BindingContext.PROPERTY_ACCESSOR, getter, getterDescriptor);
} }
else { else {
getterDescriptor = new PropertyGetterDescriptor( getterDescriptor = new PropertyGetterDescriptor(
propertyDescriptor.getModality(), propertyDescriptor, Collections.<AnnotationDescriptor>emptyList(), propertyDescriptor.getModality(),
propertyDescriptor.getVisibility(), propertyDescriptor.getVisibility(),
propertyDescriptor, Collections.<AnnotationDescriptor>emptyList(), propertyDescriptor.getOutType(), false, true); propertyDescriptor.getOutType(), false, true);
} }
return getterDescriptor; return getterDescriptor;
} }
@@ -0,0 +1,66 @@
package org.jetbrains.jet.lang.resolve;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
import org.jetbrains.jet.lang.descriptors.MutableClassDescriptor;
import org.jetbrains.jet.lang.descriptors.PropertyDescriptor;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.types.JetType;
import java.util.Map;
import static org.jetbrains.jet.lang.resolve.BindingContext.DELEGATED;
/**
* @author abreslav
*/
public class DelegationResolver {
private final TopDownAnalysisContext context;
public DelegationResolver(TopDownAnalysisContext context) {
this.context = context;
}
public void process() {
addDelegatedMembers();
}
private void addDelegatedMembers() {
for (Map.Entry<JetClass, MutableClassDescriptor> entry : context.getClasses().entrySet()) {
addDelegatedMembers(entry.getKey(), entry.getValue());
}
for (Map.Entry<JetObjectDeclaration, MutableClassDescriptor> entry : context.getObjects().entrySet()) {
addDelegatedMembers(entry.getKey(), entry.getValue());
}
}
private void addDelegatedMembers(JetClassOrObject jetClass, MutableClassDescriptor classDescriptor) {
for (JetDelegationSpecifier delegationSpecifier : jetClass.getDelegationSpecifiers()) {
if (delegationSpecifier instanceof JetDelegatorByExpressionSpecifier) {
JetDelegatorByExpressionSpecifier specifier = (JetDelegatorByExpressionSpecifier) delegationSpecifier;
JetType type = context.getTrace().get(BindingContext.TYPE, specifier.getTypeReference());
if (type != null) {
for (DeclarationDescriptor declarationDescriptor : type.getMemberScope().getAllDescriptors()) {
if (declarationDescriptor instanceof PropertyDescriptor) {
PropertyDescriptor propertyDescriptor = (PropertyDescriptor) declarationDescriptor;
if (propertyDescriptor.getModality().isOverridable()) {
PropertyDescriptor copy = propertyDescriptor.copy(classDescriptor, true);
classDescriptor.addPropertyDescriptor(copy);
context.getTrace().record(DELEGATED, copy);
}
}
else if (declarationDescriptor instanceof FunctionDescriptor) {
FunctionDescriptor functionDescriptor = (FunctionDescriptor) declarationDescriptor;
if (functionDescriptor.getModality().isOverridable()) {
FunctionDescriptor copy = functionDescriptor.copy(classDescriptor, true);
classDescriptor.addFunctionDescriptor(copy);
context.getTrace().record(DELEGATED, copy);
}
}
}
}
}
}
}
}
@@ -1,5 +1,6 @@
package org.jetbrains.jet.lang.resolve; package org.jetbrains.jet.lang.resolve;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.*; import org.jetbrains.jet.lang.descriptors.*;
@@ -89,4 +90,25 @@ public class DescriptorUtils {
} }
}); });
} }
public static List<ValueParameterDescriptor> copyValueParameters(DeclarationDescriptor newOwner, List<ValueParameterDescriptor> parameters) {
List<ValueParameterDescriptor> result = Lists.newArrayList();
for (ValueParameterDescriptor parameter : parameters) {
result.add(parameter.copy(newOwner));
}
return result;
}
public static List<TypeParameterDescriptor> copyTypeParameters(DeclarationDescriptor newOwner, List<TypeParameterDescriptor> parameters) {
List<TypeParameterDescriptor> result = Lists.newArrayList();
for (TypeParameterDescriptor parameter : parameters) {
result.add(parameter.copy(newOwner));
}
return result;
}
public static Modality convertModality(Modality modality, boolean makeNonAbstract) {
if (makeNonAbstract && modality == Modality.ABSTRACT) return Modality.OPEN;
return modality;
}
} }
@@ -1,7 +1,6 @@
package org.jetbrains.jet.lang.resolve; package org.jetbrains.jet.lang.resolve;
import com.google.common.collect.Multimap; import com.google.common.collect.*;
import com.google.common.collect.Sets;
import com.intellij.lang.ASTNode; import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement; import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@@ -18,6 +17,7 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import static org.jetbrains.jet.lang.diagnostics.Errors.*; import static org.jetbrains.jet.lang.diagnostics.Errors.*;
import static org.jetbrains.jet.lang.resolve.BindingContext.DELEGATED;
/** /**
* @author abreslav * @author abreslav
@@ -105,6 +105,7 @@ public class OverrideResolver {
// Check if everything that must be overridden, actually is // Check if everything that must be overridden, actually is
// Everything from supertypes // Everything from supertypes
Map<CallableMemberDescriptor, CallableMemberDescriptor> implementedWithDelegationBy = Maps.newHashMap();
Set<CallableMemberDescriptor> inheritedFunctions = Sets.newLinkedHashSet(); Set<CallableMemberDescriptor> inheritedFunctions = Sets.newLinkedHashSet();
for (JetType supertype : classDescriptor.getSupertypes()) { for (JetType supertype : classDescriptor.getSupertypes()) {
for (DeclarationDescriptor descriptor : supertype.getMemberScope().getAllDescriptors()) { for (DeclarationDescriptor descriptor : supertype.getMemberScope().getAllDescriptors()) {
@@ -123,7 +124,7 @@ public class OverrideResolver {
for (CallableMemberDescriptor one : filteredMembers) { for (CallableMemberDescriptor one : filteredMembers) {
if (factoredMembers.values().contains(one)) continue; if (factoredMembers.values().contains(one)) continue;
for (CallableMemberDescriptor another : filteredMembers) { for (CallableMemberDescriptor another : filteredMembers) {
if (one == another) continue; // if (one == another) continue;
factoredMembers.put(one, one); factoredMembers.put(one, one);
if (OverridingUtil.isOverridableBy(one, another).isSuccess() if (OverridingUtil.isOverridableBy(one, another).isSuccess()
|| OverridingUtil.isOverridableBy(another, one).isSuccess()) { || OverridingUtil.isOverridableBy(another, one).isSuccess()) {
@@ -154,7 +155,7 @@ public class OverrideResolver {
} }
// Members actually present (declared) in the class // Members actually present (declared) in the class
Set<CallableDescriptor> actuallyOverridden = Sets.newHashSet(); Set<CallableMemberDescriptor> actuallyOverridden = Sets.newHashSet(implementedWithDelegationBy.keySet());
for (CallableMemberDescriptor member : classDescriptor.getCallableMembers()) { for (CallableMemberDescriptor member : classDescriptor.getCallableMembers()) {
actuallyOverridden.addAll(member.getOverriddenDescriptors()); actuallyOverridden.addAll(member.getOverriddenDescriptors());
} }
@@ -171,63 +172,68 @@ public class OverrideResolver {
nameIdentifier = ((JetObjectDeclaration) klass).getNameIdentifier(); nameIdentifier = ((JetObjectDeclaration) klass).getNameIdentifier();
} }
// for (CallableMemberDescriptor memberDescriptor : manyImpl) { for (CallableMemberDescriptor memberDescriptor : manyImpl) {
// context.getTrace().report(MANY_IMPL_MEMBER_NOT_IMPLEMENTED.on(nameIdentifier, klass, memberDescriptor)); context.getTrace().report(MANY_IMPL_MEMBER_NOT_IMPLEMENTED.on(nameIdentifier, klass, memberDescriptor));
// break; break;
// } }
if (classDescriptor.getModality() == Modality.ABSTRACT) { if (classDescriptor.getModality() == Modality.ABSTRACT) {
return; return;
} }
// for (CallableMemberDescriptor memberDescriptor : abstractNoImpl) { for (CallableMemberDescriptor memberDescriptor : abstractNoImpl) {
// context.getTrace().report(ABSTRACT_MEMBER_NOT_IMPLEMENTED.on(nameIdentifier, klass, memberDescriptor)); context.getTrace().report(ABSTRACT_MEMBER_NOT_IMPLEMENTED.on(nameIdentifier, klass, memberDescriptor));
// break; break;
// } }
Set<FunctionDescriptor> allOverriddenFunctions = Sets.newHashSet(); // Set<FunctionDescriptor> allOverriddenFunctions = Sets.newHashSet();
Collection<DeclarationDescriptor> allDescriptors = classDescriptor.getDefaultType().getMemberScope().getAllDescriptors(); // Collection<DeclarationDescriptor> allDescriptors = classDescriptor.getDefaultType().getMemberScope().getAllDescriptors();
for (DeclarationDescriptor descriptor : allDescriptors) { // for (DeclarationDescriptor descriptor : allDescriptors) {
if (descriptor instanceof FunctionDescriptor) { // if (descriptor instanceof FunctionDescriptor) {
FunctionDescriptor functionDescriptor = (FunctionDescriptor) descriptor; // FunctionDescriptor functionDescriptor = (FunctionDescriptor) descriptor;
if (functionDescriptor.getModality() != Modality.ABSTRACT) { // if (functionDescriptor.getModality() != Modality.ABSTRACT) {
for (FunctionDescriptor overriddenDescriptor : functionDescriptor.getOverriddenDescriptors()) { // for (FunctionDescriptor overriddenDescriptor : functionDescriptor.getOverriddenDescriptors()) {
allOverriddenFunctions.add(overriddenDescriptor.getOriginal()); // allOverriddenFunctions.add(overriddenDescriptor.getOriginal());
} // }
} // }
} // }
} // }
boolean foundError = false; // boolean foundError = false;
for (DeclarationDescriptor descriptor : allDescriptors) { // for (DeclarationDescriptor descriptor : allDescriptors) {
if (descriptor instanceof FunctionDescriptor) { // if (descriptor instanceof FunctionDescriptor) {
FunctionDescriptor functionDescriptor = (FunctionDescriptor) descriptor; // FunctionDescriptor functionDescriptor = (FunctionDescriptor) descriptor;
if (functionDescriptor.getModality() == Modality.ABSTRACT && !allOverriddenFunctions.contains(functionDescriptor.getOriginal()) && !foundError && nameIdentifier != null) { // if (functionDescriptor.getModality() == Modality.ABSTRACT && !allOverriddenFunctions.contains(functionDescriptor.getOriginal()) && !foundError && nameIdentifier != null) {
DeclarationDescriptor declarationDescriptor = functionDescriptor.getContainingDeclaration(); // DeclarationDescriptor declarationDescriptor = functionDescriptor.getContainingDeclaration();
if (declarationDescriptor != classDescriptor) { // if (declarationDescriptor != classDescriptor) {
// context.getTrace().getErrorHandler().genericError(nameIdentifier.getNode(), "Class '" + klass.getName() + "' must be declared abstract or implement abstract method '" + //// context.getTrace().getErrorHandler().genericError(nameIdentifier.getNode(), "Class '" + klass.getName() + "' must be declared abstract or implement abstract method '" +
// functionDescriptor.getName() + "' declared in " + declarationDescriptor.getName()); //// functionDescriptor.getName() + "' declared in " + declarationDescriptor.getName());
context.getTrace().report(ABSTRACT_MEMBER_NOT_IMPLEMENTED.on(nameIdentifier, klass, functionDescriptor)); // context.getTrace().report(ABSTRACT_MEMBER_NOT_IMPLEMENTED.on(nameIdentifier, klass, functionDescriptor));
foundError = true; // foundError = true;
} // }
} // }
} // }
} // }
} }
private void checkOverride(CallableMemberDescriptor declared) { private void checkOverride(CallableMemberDescriptor declared) {
JetNamedDeclaration member = (JetNamedDeclaration) context.getTrace().get(BindingContext.DESCRIPTOR_TO_DECLARATION, declared); JetNamedDeclaration member = (JetNamedDeclaration) context.getTrace().get(BindingContext.DESCRIPTOR_TO_DECLARATION, declared);
assert member != null; if (member == null) {
assert context.getTrace().get(DELEGATED, declared);
return;
}
JetModifierList modifierList = member.getModifierList(); JetModifierList modifierList = member.getModifierList();
ASTNode overrideNode = modifierList != null ? modifierList.getModifierNode(JetTokens.OVERRIDE_KEYWORD) : null; ASTNode overrideNode = modifierList != null ? modifierList.getModifierNode(JetTokens.OVERRIDE_KEYWORD) : null;
boolean hasOverrideModifier = overrideNode != null; boolean hasOverrideModifier = overrideNode != null;
boolean finalOverriddenError = false; boolean finalOverriddenError = false;
boolean typeMismatchError = false; boolean typeMismatchError = false;
boolean kindMismatchError = false;
for (CallableMemberDescriptor overridden : declared.getOverriddenDescriptors()) { for (CallableMemberDescriptor overridden : declared.getOverriddenDescriptors()) {
if (overridden != null) { if (overridden != null) {
if (hasOverrideModifier) { if (hasOverrideModifier) {
if (!overridden.getModality().isOpen() && !finalOverriddenError) { if (!overridden.getModality().isOverridable() && !finalOverriddenError) {
// context.getTrace().getErrorHandler().genericError(overrideNode, "Method " + overridden.getName() + " in " + overridden.getContainingDeclaration().getName() + " is final and cannot be overridden"); // context.getTrace().getErrorHandler().genericError(overrideNode, "Method " + overridden.getName() + " in " + overridden.getContainingDeclaration().getName() + " is final and cannot be overridden");
context.getTrace().report(OVERRIDING_FINAL_MEMBER.on(overrideNode, overridden, overridden.getContainingDeclaration())); context.getTrace().report(OVERRIDING_FINAL_MEMBER.on(overrideNode, overridden, overridden.getContainingDeclaration()));
finalOverriddenError = true; finalOverriddenError = true;
@@ -237,6 +243,11 @@ public class OverrideResolver {
context.getTrace().report(RETURN_TYPE_MISMATCH_ON_OVERRIDE.on(member, declared, overridden)); context.getTrace().report(RETURN_TYPE_MISMATCH_ON_OVERRIDE.on(member, declared, overridden));
typeMismatchError = true; typeMismatchError = true;
} }
if (checkPropertyKind(overridden, true) && checkPropertyKind(declared, false) && !kindMismatchError) {
context.getTrace().report(VAR_OVERRIDDEN_BY_VAL.on(member, ((JetProperty) member).getValOrVarNode(), (PropertyDescriptor) declared, (PropertyDescriptor) overridden));
kindMismatchError = true;
}
} }
} }
} }
@@ -253,4 +264,12 @@ public class OverrideResolver {
context.getTrace().report(VIRTUAL_MEMBER_HIDDEN.on(member, nameIdentifier, declared, overridden, overridden.getContainingDeclaration())); context.getTrace().report(VIRTUAL_MEMBER_HIDDEN.on(member, nameIdentifier, declared, overridden, overridden.getContainingDeclaration()));
} }
} }
private boolean checkPropertyKind(CallableMemberDescriptor descriptor, boolean isVar) {
if (descriptor instanceof PropertyDescriptor) {
PropertyDescriptor propertyDescriptor = (PropertyDescriptor) descriptor;
return propertyDescriptor.isVar() == isVar;
}
return false;
}
} }
@@ -85,6 +85,12 @@ public class OverridingUtil {
@NotNull @NotNull
public static OverrideCompatibilityInfo isOverridableBy(@NotNull CallableDescriptor superDescriptor, @NotNull CallableDescriptor subDescriptor) { public static OverrideCompatibilityInfo isOverridableBy(@NotNull CallableDescriptor superDescriptor, @NotNull CallableDescriptor subDescriptor) {
if (superDescriptor instanceof FunctionDescriptor) {
if (subDescriptor instanceof PropertyDescriptor) return OverrideCompatibilityInfo.memberKindMismatch();
}
if (superDescriptor instanceof PropertyDescriptor) {
if (subDescriptor instanceof FunctionDescriptor) return OverrideCompatibilityInfo.memberKindMismatch();
}
if (!superDescriptor.getName().equals(subDescriptor.getName())) { if (!superDescriptor.getName().equals(subDescriptor.getName())) {
return OverrideCompatibilityInfo.nameMismatch(); return OverrideCompatibilityInfo.nameMismatch();
} }
@@ -192,6 +198,11 @@ public class OverridingUtil {
return new OverrideCompatibilityInfo(false, "valueParameterTypeMismatch"); // TODO return new OverrideCompatibilityInfo(false, "valueParameterTypeMismatch"); // TODO
} }
@NotNull
public static OverrideCompatibilityInfo memberKindMismatch() {
return new OverrideCompatibilityInfo(false, "memberKindMismatch"); // TODO
}
@NotNull @NotNull
public static OverrideCompatibilityInfo returnTypeMismatch(JetType substitutedSuperReturnType, JetType unsubstitutedSubReturnType) { public static OverrideCompatibilityInfo returnTypeMismatch(JetType substitutedSuperReturnType, JetType unsubstitutedSubReturnType) {
return new OverrideCompatibilityInfo(true, "returnTypeMismatch: " + unsubstitutedSubReturnType + " >< " + substitutedSuperReturnType); // TODO return new OverrideCompatibilityInfo(true, "returnTypeMismatch: " + unsubstitutedSubReturnType + " >< " + substitutedSuperReturnType); // TODO
@@ -65,6 +65,7 @@ public class TopDownAnalyzer {
TopDownAnalysisContext context = new TopDownAnalysisContext(semanticServices, trace); TopDownAnalysisContext context = new TopDownAnalysisContext(semanticServices, trace);
new TypeHierarchyResolver(context).process(outerScope, owner, declarations); new TypeHierarchyResolver(context).process(outerScope, owner, declarations);
new DeclarationResolver(context).process(); new DeclarationResolver(context).process();
new DelegationResolver(context).process();
new OverrideResolver(context).process(); new OverrideResolver(context).process();
new BodyResolver(context).resolveBehaviorDeclarationBodies(); new BodyResolver(context).resolveBehaviorDeclarationBodies();
} }
@@ -78,6 +79,7 @@ public class TopDownAnalyzer {
context.getNamespaceDescriptors().put(namespace, standardLibraryNamespace); context.getNamespaceDescriptors().put(namespace, standardLibraryNamespace);
new TypeHierarchyResolver(context).process(outerScope, standardLibraryNamespace, namespace.getDeclarations()); new TypeHierarchyResolver(context).process(outerScope, standardLibraryNamespace, namespace.getDeclarations());
new DeclarationResolver(context).process(); new DeclarationResolver(context).process();
new DelegationResolver(context).process();
OverrideResolver overrideResolver = new OverrideResolver(context) { OverrideResolver overrideResolver = new OverrideResolver(context) {
@Override @Override
protected void checkOverridesInAClass(MutableClassDescriptor classDescriptor, JetClassOrObject klass) { protected void checkOverridesInAClass(MutableClassDescriptor classDescriptor, JetClassOrObject klass) {
@@ -0,0 +1,8 @@
class Foo1() : java.util.ArrayList<Int>()
open class Bar() {
fun v() : Int = 1
val v : Int = 1
}
class Barr() : Bar() {}
@@ -0,0 +1,38 @@
//namespace override
trait T {
fun foo()
val v : Int
}
open class <!ABSTRACT_MEMBER_NOT_IMPLEMENTED!>Br<!>(t : T) : T {
}
class <!ABSTRACT_MEMBER_NOT_IMPLEMENTED!>Br3<!>(t : T) : Br(t) {
}
open class Br1(t : T) : T by t {
}
class Br2(t : T) : Br1(t) {
}
trait G<T> {
fun foo(t : T) : T
}
class <!ABSTRACT_MEMBER_NOT_IMPLEMENTED!>GC<!>() : G<Int> {
}
open class GC1(g : G<Int>) : G<Int> by g {
}
open class GC2(g : G<Int>) : GC1(g) {
}
@@ -0,0 +1,13 @@
open class Foo() {
}
class Barrr() : <!DELEGATION_NOT_TO_TRAIT!>Foo<!> by Foo() {}
trait T {}
class Br(t : T) : T by t {}
open enum class E() {}
class Test2(e : E) : <!DELEGATION_NOT_TO_TRAIT!>E<!> by e {}
@@ -0,0 +1,15 @@
open class Var() {
open var v : Int = 1
}
trait VarT {
var v : Int
}
class Val() : Var(), VarT {
override <!VAR_OVERRIDDEN_BY_VAL!>val<!> v : Int = 1
}
class Var2() : Var() {
override var v : Int = 1
}