don't use PSI for calculating the type of first argument of trait function implementation

This commit is contained in:
Dmitry Jemerov
2012-07-13 16:12:33 +02:00
parent e182041eb9
commit 1ac60dae5e
11 changed files with 69 additions and 72 deletions
@@ -832,7 +832,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
reg += argType.getSize();
}
JetType jetType = TraitImplBodyCodegen.getSuperClass(declaration, bindingContext);
JetType jetType = TraitImplBodyCodegen.getSuperClass(declaration);
Type type = typeMapper.mapType(jetType, MapTypeMode.IMPL);
if (type.getInternalName().equals("java/lang/Object")) {
jetType = declaration.getDefaultType();
@@ -710,7 +710,7 @@ public class JetTypeMapper {
if (kind == OwnerKind.TRAIT_IMPL) {
ClassDescriptor containingDeclaration = (ClassDescriptor) f.getContainingDeclaration();
JetType jetType = TraitImplBodyCodegen.getSuperClass(containingDeclaration, bindingContext);
JetType jetType = TraitImplBodyCodegen.getSuperClass(containingDeclaration);
Type type = mapType(jetType, MapTypeMode.VALUE);
if(type.getInternalName().equals("java/lang/Object")) {
jetType = containingDeclaration.getDefaultType();
@@ -16,12 +16,10 @@
package org.jetbrains.jet.codegen;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.BindingContextUtils;
import org.jetbrains.jet.lang.descriptors.ClassKind;
import org.jetbrains.jet.lang.psi.JetClassOrObject;
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.lang.JetStandardClasses;
import org.objectweb.asm.Opcodes;
@@ -33,30 +31,11 @@ public class TraitImplBodyCodegen extends ClassBodyCodegen {
super(aClass, context, v, state);
}
//todo not needed when frontend will be able to calculate properly
static JetType getSuperClass(ClassDescriptor myClassDescr, BindingContext bindingContext) {
JetClassOrObject myClass = (JetClassOrObject) BindingContextUtils.classDescriptorToDeclaration(bindingContext, myClassDescr);
if (myClass == null)
return JetStandardClasses.getAnyType();
List<JetDelegationSpecifier> delegationSpecifiers = myClass.getDelegationSpecifiers();
for (JetDelegationSpecifier specifier : delegationSpecifiers) {
if (specifier instanceof JetDelegatorToSuperClass || specifier instanceof JetDelegatorToSuperCall) {
JetType superType = bindingContext.get(BindingContext.TYPE, specifier.getTypeReference());
ClassDescriptor superClassDescriptor = (ClassDescriptor) superType.getConstructor().getDeclarationDescriptor();
final PsiElement declaration = BindingContextUtils.classDescriptorToDeclaration(bindingContext, superClassDescriptor);
if (declaration != null) {
if (declaration instanceof PsiClass) {
if (!((PsiClass) declaration).isInterface()) {
return superClassDescriptor.getDefaultType();
}
}
else if (declaration instanceof JetClass) {
if (!((JetClass) declaration).isTrait()) {
return superClassDescriptor.getDefaultType();
}
}
}
static JetType getSuperClass(ClassDescriptor classDescriptor) {
final List<ClassDescriptor> superclassDescriptors = DescriptorUtils.getSuperclassDescriptors(classDescriptor);
for (ClassDescriptor descriptor : superclassDescriptors) {
if (descriptor.getKind() != ClassKind.TRAIT) {
return descriptor.getDefaultType();
}
}
return JetStandardClasses.getAnyType();
@@ -31,10 +31,7 @@ import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
import org.jetbrains.jet.lang.types.*;
import org.jetbrains.jet.lang.types.lang.JetStandardClasses;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import static org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor.NO_RECEIVER;
@@ -310,4 +307,30 @@ public class DescriptorUtils {
}
return false;
}
@NotNull
public static List<ClassDescriptor> getSuperclassDescriptors(@NotNull ClassDescriptor classDescriptor) {
Collection<? extends JetType> superclassTypes = classDescriptor.getTypeConstructor().getSupertypes();
List<ClassDescriptor> superClassDescriptors = new ArrayList<ClassDescriptor>();
for (JetType type : superclassTypes) {
ClassDescriptor result = getClassDescriptorForType(type);
if (isNotAny(result)) {
superClassDescriptors.add(result);
}
}
return superClassDescriptors;
}
@NotNull
public static ClassDescriptor getClassDescriptorForType(@NotNull JetType type) {
DeclarationDescriptor superClassDescriptor =
type.getConstructor().getDeclarationDescriptor();
assert superClassDescriptor instanceof ClassDescriptor
: "Superclass descriptor of a type should be of type ClassDescriptor";
return (ClassDescriptor)superClassDescriptor;
}
public static boolean isNotAny(@NotNull DeclarationDescriptor superClassDescriptor) {
return !superClassDescriptor.equals(JetStandardClasses.getAny());
}
}
@@ -0,0 +1,17 @@
open class Foo() {
public fun k(): String = "K"
}
trait T: Foo {
public fun xyzzy(): String = o() + k()
public fun o(): String
}
class TImpl(): Foo(), T {
public override fun o(): String = "O"
}
fun box(): String {
return TImpl().xyzzy()
}
@@ -60,4 +60,8 @@ public class TraitsTest extends CodegenTestCase {
public void testKt2399() {
blackBoxFile("regressions/kt2399.kt");
}
public void testTraitFuncCall() {
blackBoxFile("traits/traitFuncCall.kt");
}
}