Drop BothSignatureWriter.needGenerics

It's OK to always make JVM generic signature since it's already constructed at
the end anyway
This commit is contained in:
Alexander Udalov
2013-12-23 22:32:28 +04:00
parent 135accea1d
commit 22883374ac
7 changed files with 31 additions and 63 deletions
@@ -292,7 +292,7 @@ public class ClosureCodegen extends ParentCodegenAwareImpl {
assert supertypes.size() == 1 : "Closure must have exactly one supertype: " + funDescriptor; assert supertypes.size() == 1 : "Closure must have exactly one supertype: " + funDescriptor;
JetType supertype = supertypes.iterator().next(); JetType supertype = supertypes.iterator().next();
BothSignatureWriter sw = new BothSignatureWriter(BothSignatureWriter.Mode.CLASS, true); BothSignatureWriter sw = new BothSignatureWriter(BothSignatureWriter.Mode.CLASS);
typeMapper.writeFormalTypeParameters(Collections.<TypeParameterDescriptor>emptyList(), sw); typeMapper.writeFormalTypeParameters(Collections.<TypeParameterDescriptor>emptyList(), sw);
sw.writeSuperclass(); sw.writeSuperclass();
typeMapper.mapSupertype(supertype, sw); typeMapper.mapSupertype(supertype, sw);
@@ -41,7 +41,10 @@ import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.TypeUtils; import org.jetbrains.jet.lang.types.TypeUtils;
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns; import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
import java.util.*; import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import static org.jetbrains.jet.lang.descriptors.Modality.ABSTRACT; import static org.jetbrains.jet.lang.descriptors.Modality.ABSTRACT;
import static org.jetbrains.jet.lang.resolve.java.AsmTypeConstants.OBJECT_TYPE; import static org.jetbrains.jet.lang.resolve.java.AsmTypeConstants.OBJECT_TYPE;
@@ -84,7 +87,7 @@ public class CodegenUtil {
} }
public static JvmMethodSignature erasedInvokeSignature(FunctionDescriptor fd) { public static JvmMethodSignature erasedInvokeSignature(FunctionDescriptor fd) {
BothSignatureWriter signatureWriter = new BothSignatureWriter(BothSignatureWriter.Mode.METHOD, false); BothSignatureWriter signatureWriter = new BothSignatureWriter(BothSignatureWriter.Mode.METHOD);
boolean isExtensionFunction = fd.getReceiverParameter() != null; boolean isExtensionFunction = fd.getReceiverParameter() != null;
int paramCount = fd.getValueParameters().size(); int paramCount = fd.getValueParameters().size();
@@ -77,7 +77,7 @@ public class FunctionCodegen extends ParentCodegenAwareImpl {
"in " + function.getContainingFile().getVirtualFile(); "in " + function.getContainingFile().getVirtualFile();
OwnerKind kind = owner.getContextKind(); OwnerKind kind = owner.getContextKind();
JvmMethodSignature method = typeMapper.mapSignature(functionDescriptor, true, kind); JvmMethodSignature method = typeMapper.mapSignature(functionDescriptor, kind);
if (kind != OwnerKind.TRAIT_IMPL || function.getBodyExpression() != null) { if (kind != OwnerKind.TRAIT_IMPL || function.getBodyExpression() != null) {
generateMethod(function, method, functionDescriptor, generateMethod(function, method, functionDescriptor,
@@ -334,13 +334,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
} }
private JvmClassSignature signature() { private JvmClassSignature signature() {
List<String> superInterfaces; BothSignatureWriter signatureVisitor = new BothSignatureWriter(BothSignatureWriter.Mode.CLASS);
LinkedHashSet<String> superInterfacesLinkedHashSet = new LinkedHashSet<String>();
// TODO: generics signature is not always needed
BothSignatureWriter signatureVisitor = new BothSignatureWriter(BothSignatureWriter.Mode.CLASS, true);
{ // type parameters { // type parameters
List<TypeParameterDescriptor> typeParameters = descriptor.getTypeConstructor().getParameters(); List<TypeParameterDescriptor> typeParameters = descriptor.getTypeConstructor().getParameters();
@@ -360,7 +354,10 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
} }
List<String> superInterfaces;
{ // superinterfaces { // superinterfaces
LinkedHashSet<String> superInterfacesLinkedHashSet = new LinkedHashSet<String>();
superInterfacesLinkedHashSet.add(JvmAbi.JET_OBJECT.getInternalName()); superInterfacesLinkedHashSet.add(JvmAbi.JET_OBJECT.getInternalName());
for (JetDelegationSpecifier specifier : myClass.getDelegationSpecifiers()) { for (JetDelegationSpecifier specifier : myClass.getDelegationSpecifiers()) {
@@ -24,7 +24,6 @@ import org.jetbrains.asm4.commons.Method;
import org.jetbrains.asm4.signature.SignatureVisitor; import org.jetbrains.asm4.signature.SignatureVisitor;
import org.jetbrains.asm4.signature.SignatureWriter; import org.jetbrains.asm4.signature.SignatureWriter;
import org.jetbrains.asm4.util.CheckSignatureAdapter; import org.jetbrains.asm4.util.CheckSignatureAdapter;
import org.jetbrains.jet.lang.resolve.java.AsmTypeConstants;
import org.jetbrains.jet.lang.resolve.name.Name; import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.types.Variance; import org.jetbrains.jet.lang.types.Variance;
@@ -55,13 +54,9 @@ public class BothSignatureWriter {
private JvmMethodParameterKind currentParameterKind; private JvmMethodParameterKind currentParameterKind;
private final boolean needGenerics;
private boolean generic = false; private boolean generic = false;
public BothSignatureWriter(Mode mode, boolean needGenerics) { public BothSignatureWriter(@NotNull Mode mode) {
this.needGenerics = needGenerics;
this.signatureVisitor = new CheckSignatureAdapter(mode.asmType, signatureWriter); this.signatureVisitor = new CheckSignatureAdapter(mode.asmType, signatureWriter);
} }
@@ -99,17 +94,6 @@ public class BothSignatureWriter {
} }
} }
public void writeNothing() {
signatureVisitor().visitBaseType('V');
writeAsmType0(Type.VOID_TYPE);
}
public void writeNullableNothing() {
signatureVisitor().visitClassType("java/lang/Object");
signatureVisitor().visitEnd();
writeAsmType0(AsmTypeConstants.OBJECT_TYPE);
}
private String makeArrayPrefix() { private String makeArrayPrefix() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (int i = 0; i < jvmCurrentTypeArrayLevel; ++i) { for (int i = 0; i < jvmCurrentTypeArrayLevel; ++i) {
@@ -268,11 +252,7 @@ public class BothSignatureWriter {
@NotNull @NotNull
public JvmMethodSignature makeJvmMethodSignature(String name) { public JvmMethodSignature makeJvmMethodSignature(String name) {
return new JvmMethodSignature( return new JvmMethodSignature(makeAsmMethod(name), makeJavaGenericSignature(), kotlinParameterTypes);
makeAsmMethod(name),
needGenerics ? makeJavaGenericSignature() : null,
kotlinParameterTypes
);
} }
} }
@@ -25,12 +25,9 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
public class JvmMethodSignature { public class JvmMethodSignature {
@NotNull @NotNull
private final Method asmMethod; private final Method asmMethod;
/** @Nullable
* Null when we don't care about type parameters
*/
private final String genericsSignature; private final String genericsSignature;
@NotNull @NotNull
private final List<JvmMethodParameterSignature> kotlinParameterTypes; private final List<JvmMethodParameterSignature> kotlinParameterTypes;
@@ -50,6 +47,7 @@ public class JvmMethodSignature {
return asmMethod; return asmMethod;
} }
@Nullable
public String getGenericsSignature() { public String getGenericsSignature() {
return genericsSignature; return genericsSignature;
} }
@@ -59,6 +57,7 @@ public class JvmMethodSignature {
return kotlinParameterTypes; return kotlinParameterTypes;
} }
@NotNull
public List<Type> getValueParameterTypes() { public List<Type> getValueParameterTypes() {
List<Type> r = new ArrayList<Type>(kotlinParameterTypes.size()); List<Type> r = new ArrayList<Type>(kotlinParameterTypes.size());
for (JvmMethodParameterSignature p : kotlinParameterTypes) { for (JvmMethodParameterSignature p : kotlinParameterTypes) {
@@ -412,7 +412,7 @@ public class JetTypeMapper extends BindingTraceAware {
functionDescriptor = unwrapFakeOverride(functionDescriptor.getOriginal()); functionDescriptor = unwrapFakeOverride(functionDescriptor.getOriginal());
JvmMethodSignature descriptor = mapSignature(functionDescriptor.getOriginal(), true, kind); JvmMethodSignature descriptor = mapSignature(functionDescriptor.getOriginal(), kind);
Type owner; Type owner;
Type ownerForDefaultImpl; Type ownerForDefaultImpl;
Type ownerForDefaultParam; Type ownerForDefaultParam;
@@ -433,7 +433,7 @@ public class JetTypeMapper extends BindingTraceAware {
owner = asmTypeForAnonymousClass(bindingContext, functionDescriptor); owner = asmTypeForAnonymousClass(bindingContext, functionDescriptor);
ownerForDefaultImpl = ownerForDefaultParam = thisClass = owner; ownerForDefaultImpl = ownerForDefaultParam = thisClass = owner;
invokeOpcode = INVOKEVIRTUAL; invokeOpcode = INVOKEVIRTUAL;
descriptor = mapSignature("invoke", functionDescriptor, true, kind); descriptor = mapSignature("invoke", functionDescriptor, kind);
calleeType = owner; calleeType = owner;
} }
else if (functionParent instanceof PackageFragmentDescriptor) { else if (functionParent instanceof PackageFragmentDescriptor) {
@@ -498,7 +498,7 @@ public class JetTypeMapper extends BindingTraceAware {
} }
if (isInterface && superCall) { if (isInterface && superCall) {
descriptor = mapSignature(functionDescriptor, false, OwnerKind.TRAIT_IMPL); descriptor = mapSignature(functionDescriptor, OwnerKind.TRAIT_IMPL);
owner = Type.getObjectType(owner.getInternalName() + JvmAbi.TRAIT_IMPL_SUFFIX); owner = Type.getObjectType(owner.getInternalName() + JvmAbi.TRAIT_IMPL_SUFFIX);
} }
thisClass = mapType(receiver.getDefaultType()); thisClass = mapType(receiver.getDefaultType());
@@ -564,32 +564,23 @@ public class JetTypeMapper extends BindingTraceAware {
} }
@NotNull @NotNull
public JvmMethodSignature mapSignature(@NotNull FunctionDescriptor f, boolean needGenericSignature, @NotNull OwnerKind kind) { public JvmMethodSignature mapSignature(@NotNull FunctionDescriptor f, @NotNull OwnerKind kind) {
return mapSignature(mapFunctionName(f), f, needGenericSignature, kind); return mapSignature(mapFunctionName(f), f, kind);
} }
@NotNull @NotNull
public JvmMethodSignature mapSignature(@NotNull Name functionName, @NotNull FunctionDescriptor f) { public JvmMethodSignature mapSignature(@NotNull Name functionName, @NotNull FunctionDescriptor f) {
return mapSignature(functionName.asString(), f, false, OwnerKind.IMPLEMENTATION); return mapSignature(functionName.asString(), f, OwnerKind.IMPLEMENTATION);
} }
@NotNull @NotNull
public JvmMethodSignature mapSignature(@NotNull FunctionDescriptor f) { public JvmMethodSignature mapSignature(@NotNull FunctionDescriptor f) {
return mapSignature(mapFunctionName(f), f, false, OwnerKind.IMPLEMENTATION); return mapSignature(mapFunctionName(f), f, OwnerKind.IMPLEMENTATION);
} }
@NotNull @NotNull
private JvmMethodSignature mapSignature( private JvmMethodSignature mapSignature(@NotNull String methodName, @NotNull FunctionDescriptor f, @NotNull OwnerKind kind) {
@NotNull String methodName, BothSignatureWriter signatureVisitor = new BothSignatureWriter(BothSignatureWriter.Mode.METHOD);
@NotNull FunctionDescriptor f,
boolean needGenericSignature,
@NotNull OwnerKind kind
) {
if (kind == OwnerKind.TRAIT_IMPL) {
needGenericSignature = false;
}
BothSignatureWriter signatureVisitor = new BothSignatureWriter(BothSignatureWriter.Mode.METHOD, needGenericSignature);
writeFormalTypeParameters(f.getTypeParameters(), signatureVisitor); writeFormalTypeParameters(f.getTypeParameters(), signatureVisitor);
@@ -623,7 +614,7 @@ public class JetTypeMapper extends BindingTraceAware {
@Nullable @Nullable
public String mapFieldSignature(@NotNull JetType backingFieldType) { public String mapFieldSignature(@NotNull JetType backingFieldType) {
BothSignatureWriter signatureVisitor = new BothSignatureWriter(BothSignatureWriter.Mode.TYPE, true); BothSignatureWriter signatureVisitor = new BothSignatureWriter(BothSignatureWriter.Mode.TYPE);
mapType(backingFieldType, signatureVisitor, JetTypeMapperMode.VALUE); mapType(backingFieldType, signatureVisitor, JetTypeMapperMode.VALUE);
return signatureVisitor.makeJavaGenericSignature(); return signatureVisitor.makeJavaGenericSignature();
} }
@@ -721,8 +712,7 @@ public class JetTypeMapper extends BindingTraceAware {
@NotNull @NotNull
public JvmMethodSignature mapGetterSignature(PropertyDescriptor descriptor, OwnerKind kind) { public JvmMethodSignature mapGetterSignature(PropertyDescriptor descriptor, OwnerKind kind) {
// TODO: do not genClassOrObject generics if not needed BothSignatureWriter signatureWriter = new BothSignatureWriter(BothSignatureWriter.Mode.METHOD);
BothSignatureWriter signatureWriter = new BothSignatureWriter(BothSignatureWriter.Mode.METHOD, true);
writeFormalTypeParameters(descriptor.getTypeParameters(), signatureWriter); writeFormalTypeParameters(descriptor.getTypeParameters(), signatureWriter);
@@ -743,8 +733,7 @@ public class JetTypeMapper extends BindingTraceAware {
public JvmMethodSignature mapSetterSignature(PropertyDescriptor descriptor, OwnerKind kind) { public JvmMethodSignature mapSetterSignature(PropertyDescriptor descriptor, OwnerKind kind) {
assert descriptor.isVar(); assert descriptor.isVar();
// TODO: generics signature is not always needed BothSignatureWriter signatureWriter = new BothSignatureWriter(BothSignatureWriter.Mode.METHOD);
BothSignatureWriter signatureWriter = new BothSignatureWriter(BothSignatureWriter.Mode.METHOD, true);
writeFormalTypeParameters(descriptor.getTypeParameters(), signatureWriter); writeFormalTypeParameters(descriptor.getTypeParameters(), signatureWriter);
@@ -767,9 +756,9 @@ public class JetTypeMapper extends BindingTraceAware {
@NotNull @NotNull
public JvmMethodSignature mapConstructorSignature(@NotNull ConstructorDescriptor descriptor) { public JvmMethodSignature mapConstructorSignature(@NotNull ConstructorDescriptor descriptor) {
BothSignatureWriter signatureWriter = new BothSignatureWriter(BothSignatureWriter.Mode.METHOD, true); BothSignatureWriter signatureWriter = new BothSignatureWriter(BothSignatureWriter.Mode.METHOD);
// constructor type parmeters are fake // constructor type parameters are fake
writeFormalTypeParameters(Collections.<TypeParameterDescriptor>emptyList(), signatureWriter); writeFormalTypeParameters(Collections.<TypeParameterDescriptor>emptyList(), signatureWriter);
signatureWriter.writeParametersStart(); signatureWriter.writeParametersStart();
@@ -850,7 +839,7 @@ public class JetTypeMapper extends BindingTraceAware {
@NotNull @NotNull
public JvmMethodSignature mapScriptSignature(@NotNull ScriptDescriptor script, @NotNull List<ScriptDescriptor> importedScripts) { public JvmMethodSignature mapScriptSignature(@NotNull ScriptDescriptor script, @NotNull List<ScriptDescriptor> importedScripts) {
BothSignatureWriter signatureWriter = new BothSignatureWriter(BothSignatureWriter.Mode.METHOD, false); BothSignatureWriter signatureWriter = new BothSignatureWriter(BothSignatureWriter.Mode.METHOD);
writeFormalTypeParameters(Collections.<TypeParameterDescriptor>emptyList(), signatureWriter); writeFormalTypeParameters(Collections.<TypeParameterDescriptor>emptyList(), signatureWriter);