From b881827b74ce7218701ad387fdb9c8b4c8e6856e Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Wed, 29 Apr 2015 17:39:43 +0300 Subject: [PATCH] Use descriptors instead of PSI in ImplementationBodyCodegen --- .../codegen/ImplementationBodyCodegen.java | 96 ++++++------------- 1 file changed, 27 insertions(+), 69 deletions(-) diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ImplementationBodyCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ImplementationBodyCodegen.java index e9dc8a2d2a2..9843e033fa4 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ImplementationBodyCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ImplementationBodyCodegen.java @@ -16,7 +16,6 @@ package org.jetbrains.kotlin.codegen; -import com.google.common.collect.Lists; import com.intellij.openapi.progress.ProcessCanceledException; import com.intellij.psi.PsiElement; import com.intellij.util.ArrayUtil; @@ -43,7 +42,6 @@ import org.jetbrains.kotlin.load.java.descriptors.JavaCallableMemberDescriptor; import org.jetbrains.kotlin.name.FqName; import org.jetbrains.kotlin.name.Name; import org.jetbrains.kotlin.psi.*; -import org.jetbrains.kotlin.psi.psiUtil.PsiUtilPackage; import org.jetbrains.kotlin.resolve.BindingContext; import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils; import org.jetbrains.kotlin.resolve.DescriptorUtils; @@ -134,7 +132,6 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen { isAbstract = true; isInterface = true; isAnnotation = true; - signature.getInterfaces().add("java/lang/annotation/Annotation"); } else if (jetClass.isEnum()) { isAbstract = hasAbstractMembers(descriptor); @@ -152,7 +149,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen { isStatic = !jetClass.isInner(); } else { - isStatic = myClass instanceof JetObjectDeclaration && ((JetObjectDeclaration) myClass).isCompanion() ; + isStatic = isCompanionObject(descriptor); isFinal = true; } @@ -200,14 +197,16 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen { } access |= ACC_ENUM; } - List interfaces = signature.getInterfaces(); - v.defineClass(myClass, V1_6, - access, - signature.getName(), - signature.getJavaGenericSignature(), - signature.getSuperclassName(), - ArrayUtil.toStringArray(interfaces) + + v.defineClass( + myClass, V1_6, + access, + signature.getName(), + signature.getJavaGenericSignature(), + signature.getSuperclassName(), + ArrayUtil.toStringArray(signature.getInterfaces()) ); + v.visitSource(myClass.getContainingFile().getName(), null); InlineCodegenUtil.initDefaultSourceMappingIfNeeded(context, this, state); @@ -287,22 +286,15 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen { } sw.writeSuperclassEnd(); - List interfaceSupertypes = Lists.newArrayList(); - - for (JetDelegationSpecifier specifier : myClass.getDelegationSpecifiers()) { - JetType superType = bindingContext.get(BindingContext.TYPE, specifier.getTypeReference()); - assert superType != null : "No supertype for class: " + myClass.getText(); - if (isInterface(superType.getConstructor().getDeclarationDescriptor())) { - interfaceSupertypes.add(superType); - } - } - LinkedHashSet superInterfaces = new LinkedHashSet(); - for (JetType supertype : interfaceSupertypes) { - sw.writeInterface(); - Type jvmName = typeMapper.mapSupertype(supertype, sw); - sw.writeInterfaceEnd(); - superInterfaces.add(jvmName.getInternalName()); + + for (JetType supertype : descriptor.getTypeConstructor().getSupertypes()) { + if (isInterface(supertype.getConstructor().getDeclarationDescriptor())) { + sw.writeInterface(); + Type jvmName = typeMapper.mapSupertype(supertype, sw); + sw.writeInterfaceEnd(); + superInterfaces.add(jvmName.getInternalName()); + } } return new JvmClassSignature(classAsmType.getInternalName(), superClassAsmType.getInternalName(), @@ -313,37 +305,16 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen { superClassAsmType = OBJECT_TYPE; superClassType = null; - List delegationSpecifiers = myClass.getDelegationSpecifiers(); - - if (myClass instanceof JetClass && ((JetClass) myClass).isInterface()) { + if (descriptor.getKind() == ClassKind.INTERFACE) { return; } - for (JetDelegationSpecifier specifier : delegationSpecifiers) { - if (specifier instanceof JetDelegatorToSuperClass || specifier instanceof JetDelegatorToSuperCall) { - JetType superType = bindingContext.get(BindingContext.TYPE, specifier.getTypeReference()); - assert superType != null : - String.format("No type recorded for \n---\n%s\n---\n", PsiUtilPackage.getElementTextWithContext(specifier)); - - ClassifierDescriptor classifierDescriptor = superType.getConstructor().getDeclarationDescriptor(); - if (!(classifierDescriptor instanceof ClassDescriptor)) continue; - - ClassDescriptor superClassDescriptor = (ClassDescriptor) classifierDescriptor; - if (!isInterface(superClassDescriptor)) { - superClassType = superType; - superClassAsmType = typeMapper.mapClass(superClassDescriptor); - } - } - } - - if (superClassType == null) { - if (descriptor.getKind() == ClassKind.ENUM_CLASS) { - superClassType = getBuiltIns(descriptor).getEnumType(descriptor.getDefaultType()); - superClassAsmType = typeMapper.mapType(superClassType); - } - if (descriptor.getKind() == ClassKind.ENUM_ENTRY) { - superClassType = descriptor.getTypeConstructor().getSupertypes().iterator().next(); - superClassAsmType = typeMapper.mapType(superClassType); + for (JetType supertype : descriptor.getTypeConstructor().getSupertypes()) { + ClassifierDescriptor superClass = supertype.getConstructor().getDeclarationDescriptor(); + if (superClass != null && !isInterface(superClass)) { + superClassAsmType = typeMapper.mapClass(superClass); + superClassType = supertype; + return; } } } @@ -1057,7 +1028,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen { } private void generatePrimaryConstructor(final DelegationFieldsInfo delegationFieldsInfo) { - if (ignoreIfTraitOrAnnotation()) return; + if (isTrait(descriptor) || isAnnotationClass(descriptor)) return; ConstructorDescriptor constructorDescriptor = descriptor.getUnsubstitutedPrimaryConstructor(); if (constructorDescriptor == null) return; @@ -1391,21 +1362,8 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen { } } - private boolean ignoreIfTraitOrAnnotation() { - if (myClass instanceof JetClass) { - JetClass aClass = (JetClass) myClass; - if (aClass.isInterface()) { - return true; - } - if (aClass.isAnnotation()) { - return true; - } - } - return false; - } - private void generateTraitMethods() { - if (JetPsiUtil.isTrait(myClass)) return; + if (isTrait(descriptor)) return; for (Map.Entry entry : CodegenUtil.getTraitMethods(descriptor).entrySet()) { FunctionDescriptor traitFun = entry.getKey();