Add synthetic argument to default constructors

It's need to add synthetic argument (of type that user can't use)
to constructors with default arguments to avoid clashing with
real user's constructor having the same set of parameters
and additional int's arguments.
This commit is contained in:
Denis Zharkov
2015-02-27 19:07:24 +03:00
parent 67f97fea42
commit 4022982760
8 changed files with 80 additions and 5 deletions
@@ -138,6 +138,7 @@ public class CallableMethod implements Callable {
Method method = getAsmMethod();
String desc = JetTypeMapper.getDefaultDescriptor(method, receiverParameterType != null);
if ("<init>".equals(method.getName())) {
v.aconst(null);
v.visitMethodInsn(INVOKESPECIAL, defaultImplOwner.getInternalName(), "<init>", desc, false);
}
else {
@@ -585,6 +585,10 @@ public class FunctionCodegen {
for (int m : masks) {
v.iconst(m);
}
// constructors with default arguments has last synthetic argument of specific type
v.aconst(null);
String desc = JetTypeMapper.getDefaultDescriptor(method.getAsmMethod(), false);
v.invokespecial(methodOwner.getInternalName(), "<init>", desc, false);
v.areturn(Type.VOID_TYPE);
@@ -77,6 +77,7 @@ import static org.jetbrains.kotlin.resolve.DescriptorUtils.*;
import static org.jetbrains.org.objectweb.asm.Opcodes.*;
public class JetTypeMapper {
private static final String DEFAULT_CONSTRUCTOR_MARKER_INTERNAL_CLASS_NAME = "kotlin/jvm/internal/DefaultConstructorMarker";
private final BindingContext bindingContext;
private final ClassBuilderMode classBuilderMode;
@@ -713,8 +714,15 @@ public class JetTypeMapper {
argumentsCount--;
}
int maskArgumentsCount = (argumentsCount + Integer.SIZE - 1) / Integer.SIZE;
String maskArguments = StringUtil.repeat(Type.INT_TYPE.getDescriptor(), maskArgumentsCount);
return descriptor.replace(")", maskArguments + ")");
String additionalArgs = StringUtil.repeat(Type.INT_TYPE.getDescriptor(), maskArgumentsCount);
if (isConstructor(method)) {
additionalArgs += Type.getObjectType(DEFAULT_CONSTRUCTOR_MARKER_INTERNAL_CLASS_NAME).getDescriptor();
}
return descriptor.replace(")", additionalArgs + ")");
}
private static boolean isConstructor(@NotNull Method method) {
return "<init>".equals(method.getName());
}
@NotNull
@@ -722,7 +730,7 @@ public class JetTypeMapper {
Method jvmSignature = mapSignature(functionDescriptor, kind).getAsmMethod();
Type ownerType = mapOwner(functionDescriptor, isCallInsideSameModuleAsDeclared(functionDescriptor, context, getOutDirectory()));
String descriptor = getDefaultDescriptor(jvmSignature, functionDescriptor.getExtensionReceiverParameter() != null);
boolean isConstructor = "<init>".equals(jvmSignature.getName());
boolean isConstructor = isConstructor(jvmSignature);
if (!isStaticMethod(kind, functionDescriptor) && !isConstructor) {
descriptor = descriptor.replace("(", "(" + ownerType.getDescriptor());
}