replace JavaClassDescriptor with MutableClassDescriptorLite

This commit is contained in:
Stepan Koltsov
2012-01-14 04:27:22 +04:00
parent 10eb516743
commit 882a475ac8
8 changed files with 57 additions and 238 deletions
@@ -14,7 +14,6 @@ import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.calls.*;
import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstant;
import org.jetbrains.jet.lang.resolve.java.JavaClassDescriptor;
import org.jetbrains.jet.lang.resolve.java.JavaNamespaceDescriptor;
import org.jetbrains.jet.lang.resolve.java.JvmAbi;
import org.jetbrains.jet.lang.resolve.java.JvmStdlibNames;
@@ -1043,15 +1042,11 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
}
else {
owner = typeMapper.getOwner(functionDescriptor, OwnerKind.IMPLEMENTATION);
if(containingDeclaration instanceof JavaClassDescriptor) {
isInterface = CodegenUtil.isInterface(containingDeclaration);
if (containingDeclaration instanceof ClassDescriptor && ((ClassDescriptor) containingDeclaration).getKind() == ClassKind.TRAIT) {
isInterface = true;
}
else {
if(containingDeclaration instanceof ClassDescriptor && ((ClassDescriptor) containingDeclaration).getKind() == ClassKind.TRAIT)
isInterface = true;
else {
isInterface = false;
}
isInterface = false;
}
}
@@ -1097,23 +1092,30 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
}
}
}
if(!(containingDeclaration instanceof JavaNamespaceDescriptor) && !(containingDeclaration instanceof JavaClassDescriptor))
if(!(containingDeclaration instanceof JavaNamespaceDescriptor))
getter = typeMapper.mapGetterSignature(propertyDescriptor, OwnerKind.IMPLEMENTATION).getJvmMethodSignature().getAsmMethod();
else
getter = null;
if (propertyDescriptor.getGetter() == null) {
getter = null;
}
}
//noinspection ConstantConditions
if (isInsideClass && (propertyDescriptor.getSetter() == null || propertyDescriptor.getSetter().isDefault())) {
setter = null;
}
else {
if(!(containingDeclaration instanceof JavaNamespaceDescriptor) && !(containingDeclaration instanceof JavaClassDescriptor)) {
if(!(containingDeclaration instanceof JavaNamespaceDescriptor)) {
JvmPropertyAccessorSignature jvmMethodSignature = typeMapper.mapSetterSignature(propertyDescriptor, OwnerKind.IMPLEMENTATION);
setter = jvmMethodSignature != null ? jvmMethodSignature.getJvmMethodSignature().getAsmMethod() : null;
} else {
setter = null;
}
if (propertyDescriptor.getSetter() == null) {
setter = null;
}
}
}
@@ -6,7 +6,6 @@ import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
import org.jetbrains.jet.lang.descriptors.PropertyDescriptor;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.java.JavaClassDescriptor;
import org.jetbrains.jet.lang.resolve.java.JvmAbi;
import org.jetbrains.jet.lang.resolve.java.JvmStdlibNames;
import org.jetbrains.jet.lang.types.JetStandardClasses;
@@ -152,7 +151,7 @@ public class NamespaceCodegen {
}
DeclarationDescriptor declarationDescriptor = jetType.getConstructor().getDeclarationDescriptor();
if(!jetType.equals(root) && jetType.getArguments().size() == 0 && !(declarationDescriptor instanceof JavaClassDescriptor) && !JetStandardClasses.getAny().equals(declarationDescriptor)) {
if(!jetType.equals(root) && jetType.getArguments().size() == 0 && !JetStandardClasses.getAny().equals(declarationDescriptor)) {
// TODO: we need some better checks here
v.getstatic(typeMapper.mapType(jetType, OwnerKind.IMPLEMENTATION).getInternalName(), JvmAbi.TYPE_INFO_FIELD, "Ljet/TypeInfo;");
return;
@@ -744,7 +744,9 @@ public abstract class StackValue {
static class Property extends StackValue {
private final String name;
@Nullable
private final Method getter;
@Nullable
private final Method setter;
public final String owner;
private final boolean isStatic;
@@ -1,181 +0,0 @@
package org.jetbrains.jet.lang.resolve.java;
import com.google.common.collect.Sets;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.resolve.scopes.SubstitutingScope;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ClassReceiver;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
import org.jetbrains.jet.lang.types.*;
import java.util.*;
/**
* @author abreslav
*/
public class JavaClassDescriptor extends MutableDeclarationDescriptor implements ClassDescriptor {
private TypeConstructor typeConstructor;
private JavaClassMembersScope unsubstitutedMemberScope;
// private JetType classObjectType;
private final Set<ConstructorDescriptor> constructors = Sets.newLinkedHashSet();
private Modality modality;
private Visibility visibility;
private final ClassKind kind;
private ClassReceiver implicitReceiver;
public JavaClassDescriptor(DeclarationDescriptor containingDeclaration, @NotNull ClassKind kind) {
super(containingDeclaration);
this.kind = kind;
}
public void setTypeConstructor(TypeConstructor typeConstructor) {
this.typeConstructor = typeConstructor;
}
public void setModality(Modality modality) {
this.modality = modality;
}
public void setVisibility(Visibility visibility) {
this.visibility = visibility;
}
public void setUnsubstitutedMemberScope(JavaClassMembersScope memberScope) {
this.unsubstitutedMemberScope = memberScope;
}
// public void setClassObjectMemberScope(JavaClassMembersScope memberScope) {
// classObjectType = new JetTypeImpl(
// new TypeConstructorImpl(
// JavaDescriptorResolver.JAVA_CLASS_OBJECT,
// Collections.<AnnotationDescriptor>emptyList(),
// true,
// "Class object emulation for " + getName(),
// Collections.<TypeParameterDescriptor>emptyList(),
// Collections.<JetType>emptyList()
// ),
// memberScope
// );
// }
public void addConstructor(ConstructorDescriptor constructorDescriptor) {
this.constructors.add(constructorDescriptor);
}
private TypeSubstitutor createTypeSubstitutor(List<TypeProjection> typeArguments) {
List<TypeParameterDescriptor> parameters = getTypeConstructor().getParameters();
Map<TypeConstructor, TypeProjection> context = TypeUtils.buildSubstitutionContext(parameters, typeArguments);
return TypeSubstitutor.create(context);
}
@NotNull
@Override
public JetScope getMemberScope(List<TypeProjection> typeArguments) {
assert typeArguments.size() == typeConstructor.getParameters().size();
if (typeArguments.isEmpty()) return unsubstitutedMemberScope;
TypeSubstitutor substitutor = createTypeSubstitutor(typeArguments);
return new SubstitutingScope(unsubstitutedMemberScope, substitutor);
}
@NotNull
@Override
public Set<ConstructorDescriptor> getConstructors() {
return constructors;
}
@Override
public ConstructorDescriptor getUnsubstitutedPrimaryConstructor() {
return null;
}
@Override
public boolean hasConstructors() {
return !constructors.isEmpty();
}
@NotNull
@Override
public TypeConstructor getTypeConstructor() {
return typeConstructor;
}
@NotNull
@Override
public JetType getDefaultType() {
return TypeUtils.makeUnsubstitutedType(this, unsubstitutedMemberScope);
}
@NotNull
@Override
public ClassDescriptor substitute(TypeSubstitutor substitutor) {
throw new UnsupportedOperationException(); // TODO
}
@Override
public JetType getClassObjectType() {
return null;
}
@Override
public ClassDescriptor getClassObjectDescriptor() {
return null;
}
@Override
public boolean isClassObjectAValue() {
return false;
}
@NotNull
@Override
public ClassKind getKind() {
return kind;
}
@Override
@NotNull
public Modality getModality() {
return modality;
}
@NotNull
@Override
public Visibility getVisibility() {
return visibility;
}
@Override
public <R, D> R accept(DeclarationDescriptorVisitor<R, D> visitor, D data) {
return visitor.visitClassDescriptor(this, data);
}
@Override
public String toString() {
return "java class " + typeConstructor;
}
@NotNull
@Override
public ReceiverDescriptor getImplicitReceiver() {
if (implicitReceiver == null) {
implicitReceiver = new ClassReceiver(this);
}
return implicitReceiver;
}
@Override
public ClassDescriptor getInnerClassOrObject(String name) {
return null;
}
@NotNull
@Override
public Collection<ClassDescriptor> getInnerClassesAndObjects() {
return Collections.emptyList();
}
}
@@ -34,6 +34,7 @@ import org.jetbrains.jet.lang.descriptors.DeclarationDescriptorImpl;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptorVisitor;
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
import org.jetbrains.jet.lang.descriptors.Modality;
import org.jetbrains.jet.lang.descriptors.MutableClassDescriptorLite;
import org.jetbrains.jet.lang.descriptors.NamedFunctionDescriptorImpl;
import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
import org.jetbrains.jet.lang.descriptors.PropertyDescriptor;
@@ -50,7 +51,6 @@ import org.jetbrains.jet.lang.resolve.DescriptorUtils;
import org.jetbrains.jet.lang.resolve.java.kt.JetClassAnnotation;
import org.jetbrains.jet.lang.types.JetStandardClasses;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.TypeConstructorImpl;
import org.jetbrains.jet.lang.types.TypeSubstitutor;
import org.jetbrains.jet.lang.types.TypeUtils;
import org.jetbrains.jet.lang.types.Variance;
@@ -136,7 +136,7 @@ public class JavaDescriptorResolver {
}
private static class ResolverClassData extends ResolverScopeData {
private JavaClassDescriptor classDescriptor;
private MutableClassDescriptorLite classDescriptor;
@NotNull
public ClassDescriptor getClassDescriptor() {
@@ -231,8 +231,9 @@ public class JavaDescriptorResolver {
String name = psiClass.getName();
ResolverClassData classData = new ResolverClassData();
classData.classDescriptor = new JavaClassDescriptor(
resolveParentDescriptor(psiClass), psiClass.isInterface() ? ClassKind.TRAIT : ClassKind.CLASS
ClassKind kind = psiClass.isInterface() ? ClassKind.TRAIT : ClassKind.CLASS;
classData.classDescriptor = new MutableClassDescriptorLite(
resolveParentDescriptor(psiClass), kind
);
classData.classDescriptor.setName(name);
@@ -246,28 +247,24 @@ public class JavaDescriptorResolver {
}
List<JetType> supertypes = new ArrayList<JetType>();
List<TypeParameterDescriptor> typeParameters = resolveClassTypeParameters(psiClass, classData, new OuterClassTypeVariableResolver());
classData.classDescriptor.setTypeConstructor(new TypeConstructorImpl(
classData.classDescriptor,
Collections.<AnnotationDescriptor>emptyList(), // TODO
// TODO
psiClass.hasModifierProperty(PsiModifier.FINAL),
name,
typeParameters,
supertypes
));
classData.classDescriptor.setTypeParameterDescriptors(typeParameters);
classData.classDescriptor.setSupertypes(supertypes);
classData.classDescriptor.setVisibility(resolveVisibilityFromPsiModifiers(psiClass));
classData.classDescriptor.setModality(Modality.convertFromFlags(
psiClass.hasModifierProperty(PsiModifier.ABSTRACT) || psiClass.isInterface(),
!psiClass.hasModifierProperty(PsiModifier.FINAL))
);
classData.classDescriptor.setVisibility(resolveVisibilityFromPsiModifiers(psiClass));
classData.classDescriptor.createTypeConstructor();
classDescriptorCache.put(psiClass.getQualifiedName(), classData);
classData.classDescriptor.setUnsubstitutedMemberScope(new JavaClassMembersScope(classData.classDescriptor, psiClass, semanticServices, false));
classData.classDescriptor.setScopeForMemberLookup(new JavaClassMembersScope(classData.classDescriptor, psiClass, semanticServices, false));
// UGLY HACK (Andrey Breslav is not sure what did he mean)
initializeTypeParameters(psiClass);
// TODO: ugly hack: tests crash if initializeTypeParameters called with class containing proper supertypes
supertypes.addAll(getSupertypes(psiClass));
if (psiClass.isInterface()) {
//classData.classDescriptor.setSuperclassType(JetStandardClasses.getAnyType()); // TODO : Make it java.lang.Object
}
@@ -294,7 +291,7 @@ public class JavaDescriptorResolver {
false);
constructorDescriptor.initialize(typeParameters, Collections.<ValueParameterDescriptor>emptyList(), Modality.FINAL, classData.classDescriptor.getVisibility());
constructorDescriptor.setReturnType(classData.classDescriptor.getDefaultType());
classData.classDescriptor.addConstructor(constructorDescriptor);
classData.classDescriptor.addConstructor(constructorDescriptor, null);
semanticServices.getTrace().record(BindingContext.CONSTRUCTOR, psiClass, constructorDescriptor);
}
}
@@ -320,7 +317,7 @@ public class JavaDescriptorResolver {
constructorDescriptor.initialize(typeParameters, valueParameterDescriptors.descriptors, Modality.FINAL,
resolveVisibilityFromPsiModifiers(psiConstructor));
constructorDescriptor.setReturnType(classData.classDescriptor.getDefaultType());
classData.classDescriptor.addConstructor(constructorDescriptor);
classData.classDescriptor.addConstructor(constructorDescriptor, null);
semanticServices.getTrace().record(BindingContext.CONSTRUCTOR, psiConstructor, constructorDescriptor);
}
}
@@ -440,7 +437,7 @@ public class JavaDescriptorResolver {
* @see #resolveMethodTypeParametersFromJetSignature(String, FunctionDescriptor)
*/
private List<TypeParameterDescriptor> resolveClassTypeParametersFromJetSignature(String jetSignature, final PsiClass clazz,
final JavaClassDescriptor classDescriptor, final TypeVariableResolver outerClassTypeVariableResolver) {
final ClassDescriptor classDescriptor, final TypeVariableResolver outerClassTypeVariableResolver) {
final List<TypeParameterDescriptor> r = new ArrayList<TypeParameterDescriptor>();
class MyTypeVariableResolver implements TypeVariableResolver {
@@ -993,7 +990,7 @@ public class JavaDescriptorResolver {
ResolverScopeData scopeData;
if (owner instanceof JavaNamespaceDescriptor) {
scopeData = namespaceDescriptorCacheByFqn.get(((JavaNamespaceDescriptor) owner).getQualifiedName());
} else if (owner instanceof JavaClassDescriptor) {
} else if (owner instanceof ClassDescriptor) {
scopeData = classDescriptorCache.get(psiClass.getQualifiedName());
} else {
throw new IllegalStateException();
@@ -1319,7 +1316,7 @@ public class JavaDescriptorResolver {
}
/**
* @see #resolveClassTypeParametersFromJetSignature(String, com.intellij.psi.PsiClass, JavaClassDescriptor)
* @see #resolveClassTypeParametersFromJetSignature(String, com.intellij.psi.PsiClass, MutableClassDescriptorLite)
*/
private List<TypeParameterDescriptor> resolveMethodTypeParametersFromJetSignature(String jetSignature, final PsiMethod method,
final FunctionDescriptor functionDescriptor, final TypeVariableResolver classTypeVariableResolver)
@@ -1,9 +1,7 @@
package org.jetbrains.jet.lang.descriptors;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetParameter;
import org.jetbrains.jet.lang.resolve.AbstractScopeAdapter;
import org.jetbrains.jet.lang.resolve.BindingContext;
@@ -12,8 +10,6 @@ import org.jetbrains.jet.lang.resolve.TraceBasedRedeclarationHandler;
import org.jetbrains.jet.lang.resolve.scopes.*;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ClassReceiver;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
import org.jetbrains.jet.lang.types.*;
import org.jetbrains.jet.resolve.DescriptorRenderer;
import java.util.*;
@@ -31,7 +27,7 @@ public class MutableClassDescriptor extends MutableClassDescriptorLite {
private final WritableScope scopeForInitializers; //contains members + primary constructor value parameters + map for backing fields
public MutableClassDescriptor(@NotNull BindingTrace trace, @NotNull DeclarationDescriptor containingDeclaration, @NotNull JetScope outerScope, ClassKind kind) {
super(containingDeclaration, kind, new TraceBasedRedeclarationHandler(trace));
super(containingDeclaration, kind);
if (containingDeclaration instanceof ClassDescriptor
|| containingDeclaration instanceof NamespaceLike
@@ -43,6 +39,8 @@ public class MutableClassDescriptor extends MutableClassDescriptorLite {
}
TraceBasedRedeclarationHandler redeclarationHandler = new TraceBasedRedeclarationHandler(trace);
setScopeForMemberLookup(new WritableScopeImpl(JetScope.EMPTY, this, redeclarationHandler).setDebugName("MemberLookup").changeLockLevel(WritableScope.LockLevel.BOTH));
this.scopeForSupertypeResolution = new WritableScopeImpl(outerScope, this, redeclarationHandler).setDebugName("SupertypeResolution").changeLockLevel(WritableScope.LockLevel.BOTH);
this.scopeForMemberResolution = new WritableScopeImpl(scopeForSupertypeResolution, this, redeclarationHandler).setDebugName("MemberResolution").changeLockLevel(WritableScope.LockLevel.BOTH);
this.scopeForInitializers = new WritableScopeImpl(scopeForMemberResolution, this, redeclarationHandler).setDebugName("Initializers").changeLockLevel(WritableScope.LockLevel.BOTH);
@@ -55,15 +55,12 @@ public class MutableClassDescriptorLite extends MutableDeclarationDescriptor imp
private JetType defaultType;
private final ClassKind kind;
private final WritableScope scopeForMemberLookup;
private JetScope scopeForMemberLookup;
private ClassReceiver implicitReceiver;
public MutableClassDescriptorLite(DeclarationDescriptor containingDeclaration, ClassKind kind, RedeclarationHandler redeclarationHandler) {
public MutableClassDescriptorLite(DeclarationDescriptor containingDeclaration, ClassKind kind) {
super(containingDeclaration);
this.scopeForMemberLookup = new WritableScopeImpl(JetScope.EMPTY, this, redeclarationHandler).setDebugName("MemberLookup").changeLockLevel(WritableScope.LockLevel.BOTH);
this.kind = kind;
}
@@ -103,6 +100,10 @@ public class MutableClassDescriptorLite extends MutableDeclarationDescriptor imp
return typeConstructor;
}
public void setScopeForMemberLookup(JetScope scopeForMemberLookup) {
this.scopeForMemberLookup = scopeForMemberLookup;
}
public void createTypeConstructor() {
assert typeConstructor == null : typeConstructor;
this.typeConstructor = new TypeConstructorImpl(
@@ -117,9 +118,14 @@ public class MutableClassDescriptorLite extends MutableDeclarationDescriptor imp
}
}
private WritableScope getScopeForMemberLookupAsWritableScope() {
// hack
return (WritableScope) scopeForMemberLookup;
}
public void addSupertypesToScopeForMemberLookup() {
for (JetType supertype : supertypes) {
scopeForMemberLookup.importScope(supertype.getMemberScope());
getScopeForMemberLookupAsWritableScope().importScope(supertype.getMemberScope());
}
}
@@ -204,12 +210,9 @@ public class MutableClassDescriptorLite extends MutableDeclarationDescriptor imp
return supertypes;
}
public void setSupertypes(@NotNull Collection<JetType> supertypes) {
this.supertypes = supertypes;
}
public void setPrimaryConstructor(@NotNull ConstructorDescriptor constructorDescriptor, BindingTrace trace) {
@@ -249,12 +252,12 @@ public class MutableClassDescriptorLite extends MutableDeclarationDescriptor imp
@Override
public void addPropertyDescriptor(@NotNull PropertyDescriptor propertyDescriptor) {
scopeForMemberLookup.addPropertyDescriptor(propertyDescriptor);
getScopeForMemberLookupAsWritableScope().addPropertyDescriptor(propertyDescriptor);
}
@Override
public void addFunctionDescriptor(@NotNull NamedFunctionDescriptor functionDescriptor) {
scopeForMemberLookup.addFunctionDescriptor(functionDescriptor);
getScopeForMemberLookupAsWritableScope().addFunctionDescriptor(functionDescriptor);
}
@Override
@@ -269,7 +272,7 @@ public class MutableClassDescriptorLite extends MutableDeclarationDescriptor imp
@Override
public void addClassifierDescriptor(@NotNull MutableClassDescriptor classDescriptor) {
scopeForMemberLookup.addClassifierDescriptor(classDescriptor);
getScopeForMemberLookupAsWritableScope().addClassifierDescriptor(classDescriptor);
innerClassesAndObjects.put(classDescriptor.getName(), classDescriptor);
}
@@ -287,7 +290,7 @@ public class MutableClassDescriptorLite extends MutableDeclarationDescriptor imp
@Override
public void addObjectDescriptor(@NotNull MutableClassDescriptor objectDescriptor) {
scopeForMemberLookup.addObjectDescriptor(objectDescriptor);
getScopeForMemberLookupAsWritableScope().addObjectDescriptor(objectDescriptor);
innerClassesAndObjects.put(objectDescriptor.getName(), objectDescriptor);
}
@@ -309,7 +312,7 @@ public class MutableClassDescriptorLite extends MutableDeclarationDescriptor imp
public void lockScopes() {
scopeForMemberLookup.changeLockLevel(WritableScope.LockLevel.READING);
getScopeForMemberLookupAsWritableScope().changeLockLevel(WritableScope.LockLevel.READING);
if (classObjectDescriptor != null) {
classObjectDescriptor.lockScopes();
}
@@ -6,7 +6,6 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.descriptors.ModuleDescriptor;
import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
import org.jetbrains.jet.lang.resolve.java.JavaClassDescriptor;
import org.jetbrains.jet.lang.resolve.java.JavaDescriptorResolver;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.types.*;
@@ -65,7 +64,7 @@ public class JetPluginUtil {
JetScope libraryScope = standardLibrary.getLibraryScope();
DeclarationDescriptor declaration = type.getMemberScope().getContainingDeclaration();
if (declaration instanceof JavaClassDescriptor || ErrorUtils.isError(declaration)) {
if (ErrorUtils.isError(declaration)) {
return false;
}
while (!(declaration instanceof NamespaceDescriptor)) {