Merge two JetTypeMapper#mapToCallableMethod methods

This commit is contained in:
Alexander Udalov
2015-09-14 21:04:14 +03:00
parent 7c0780455a
commit 296212eab1
4 changed files with 20 additions and 32 deletions
@@ -1472,7 +1472,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
ConstructorDescriptor constructorToCall = SamCodegenUtil.resolveSamAdapter(superConstructor); ConstructorDescriptor constructorToCall = SamCodegenUtil.resolveSamAdapter(superConstructor);
List<ValueParameterDescriptor> superValueParameters = superConstructor.getValueParameters(); List<ValueParameterDescriptor> superValueParameters = superConstructor.getValueParameters();
int params = superValueParameters.size(); int params = superValueParameters.size();
List<Type> superMappedTypes = typeMapper.mapToCallableMethod(constructorToCall).getValueParameterTypes(); List<Type> superMappedTypes = typeMapper.mapToCallableMethod(constructorToCall, false).getValueParameterTypes();
assert superMappedTypes.size() >= params : String assert superMappedTypes.size() >= params : String
.format("Incorrect number of mapped parameters vs arguments: %d < %d for %s", .format("Incorrect number of mapped parameters vs arguments: %d < %d for %s",
superMappedTypes.size(), params, classDescriptor); superMappedTypes.size(), params, classDescriptor);
@@ -3414,7 +3414,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
pushClosureOnStack(constructor.getContainingDeclaration(), dispatchReceiver == null, defaultCallGenerator); pushClosureOnStack(constructor.getContainingDeclaration(), dispatchReceiver == null, defaultCallGenerator);
constructor = SamCodegenUtil.resolveSamAdapter(constructor); constructor = SamCodegenUtil.resolveSamAdapter(constructor);
CallableMethod method = typeMapper.mapToCallableMethod(constructor); CallableMethod method = typeMapper.mapToCallableMethod(constructor, false);
invokeMethodWithArguments(method, resolvedCall, StackValue.none()); invokeMethodWithArguments(method, resolvedCall, StackValue.none());
return Unit.INSTANCE$; return Unit.INSTANCE$;
@@ -664,13 +664,7 @@ public class FunctionCodegen {
generator.putValueIfNeeded(parameterDescriptor, type, StackValue.local(parameterIndex, type)); generator.putValueIfNeeded(parameterDescriptor, type, StackValue.local(parameterIndex, type));
} }
CallableMethod method; CallableMethod method = state.getTypeMapper().mapToCallableMethod(functionDescriptor, false);
if (functionDescriptor instanceof ConstructorDescriptor) {
method = state.getTypeMapper().mapToCallableMethod((ConstructorDescriptor) functionDescriptor);
}
else {
method = state.getTypeMapper().mapToCallableMethod(functionDescriptor, false);
}
generator.genCallWithoutAssertions(method, codegen); generator.genCallWithoutAssertions(method, codegen);
@@ -937,17 +937,16 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
@Nullable FunctionDescriptor accessorDescriptor, @Nullable FunctionDescriptor accessorDescriptor,
@NotNull InstructionAdapter iv @NotNull InstructionAdapter iv
) { ) {
boolean isConstructor = functionDescriptor instanceof ConstructorDescriptor; CallableMethod callableMethod = typeMapper.mapToCallableMethod(
boolean accessorIsConstructor = accessorDescriptor instanceof AccessorForConstructorDescriptor; functionDescriptor,
accessorDescriptor instanceof AccessorForCallableDescriptor &&
boolean superCall = accessorDescriptor instanceof AccessorForCallableDescriptor && ((AccessorForCallableDescriptor) accessorDescriptor).getSuperCallExpression() != null
((AccessorForCallableDescriptor) accessorDescriptor).getSuperCallExpression() != null; );
CallableMethod callableMethod = isConstructor ?
typeMapper.mapToCallableMethod((ConstructorDescriptor) functionDescriptor) :
typeMapper.mapToCallableMethod(functionDescriptor, superCall);
int reg = 1; int reg = 1;
if (isConstructor && !accessorIsConstructor) {
boolean accessorIsConstructor = accessorDescriptor instanceof AccessorForConstructorDescriptor;
if (!accessorIsConstructor && functionDescriptor instanceof ConstructorDescriptor) {
iv.anew(callableMethod.getOwner()); iv.anew(callableMethod.getOwner());
iv.dup(); iv.dup();
reg = 0; reg = 0;
@@ -1496,8 +1495,8 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
iv.load(0, OBJECT_TYPE); iv.load(0, OBJECT_TYPE);
ConstructorDescriptor delegateConstructor = SamCodegenUtil.resolveSamAdapter(codegen.getConstructorDescriptor(delegationConstructorCall)); ConstructorDescriptor delegateConstructor = SamCodegenUtil.resolveSamAdapter(codegen.getConstructorDescriptor(delegationConstructorCall));
CallableMethod delegateConstructorCallable = typeMapper.mapToCallableMethod(delegateConstructor); CallableMethod delegateConstructorCallable = typeMapper.mapToCallableMethod(delegateConstructor, false);
CallableMethod callable = typeMapper.mapToCallableMethod(constructorDescriptor); CallableMethod callable = typeMapper.mapToCallableMethod(constructorDescriptor, false);
List<JvmMethodParameterSignature> delegatingParameters = delegateConstructorCallable.getValueParameters(); List<JvmMethodParameterSignature> delegatingParameters = delegateConstructorCallable.getValueParameters();
List<JvmMethodParameterSignature> parameters = callable.getValueParameters(); List<JvmMethodParameterSignature> parameters = callable.getValueParameters();
@@ -1711,7 +1710,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
if (delegationSpecifiers.size() == 1 && !enumEntryNeedSubclass(bindingContext, enumEntry)) { if (delegationSpecifiers.size() == 1 && !enumEntryNeedSubclass(bindingContext, enumEntry)) {
ResolvedCall<?> resolvedCall = CallUtilPackage.getResolvedCallWithAssert(delegationSpecifiers.get(0), bindingContext); ResolvedCall<?> resolvedCall = CallUtilPackage.getResolvedCallWithAssert(delegationSpecifiers.get(0), bindingContext);
CallableMethod method = typeMapper.mapToCallableMethod((ConstructorDescriptor) resolvedCall.getResultingDescriptor()); CallableMethod method = typeMapper.mapToCallableMethod((ConstructorDescriptor) resolvedCall.getResultingDescriptor(), false);
codegen.invokeMethodWithArguments(method, resolvedCall, StackValue.none()); codegen.invokeMethodWithArguments(method, resolvedCall, StackValue.none());
} }
@@ -593,6 +593,12 @@ public class JetTypeMapper {
@NotNull @NotNull
public CallableMethod mapToCallableMethod(@NotNull FunctionDescriptor descriptor, boolean superCall) { public CallableMethod mapToCallableMethod(@NotNull FunctionDescriptor descriptor, boolean superCall) {
if (descriptor instanceof ConstructorDescriptor) {
JvmMethodSignature method = mapSignature(descriptor);
Type owner = mapClass(((ConstructorDescriptor) descriptor).getContainingDeclaration());
return new CallableMethod(owner, owner, owner, method, INVOKESPECIAL, null, null, null);
}
DeclarationDescriptor functionParent = descriptor.getOriginal().getContainingDeclaration(); DeclarationDescriptor functionParent = descriptor.getOriginal().getContainingDeclaration();
FunctionDescriptor functionDescriptor = unwrapFakeOverride(descriptor.getOriginal()); FunctionDescriptor functionDescriptor = unwrapFakeOverride(descriptor.getOriginal());
@@ -1113,17 +1119,6 @@ public class JetTypeMapper {
return sw.makeJvmMethodSignature("<init>"); return sw.makeJvmMethodSignature("<init>");
} }
@NotNull
public CallableMethod mapToCallableMethod(@NotNull ConstructorDescriptor descriptor) {
JvmMethodSignature method = mapSignature(descriptor);
ClassDescriptor container = descriptor.getContainingDeclaration();
Type owner = mapClass(container);
if (owner.getSort() != Type.OBJECT) {
throw new IllegalStateException("type must have been mapped to object: " + container.getDefaultType() + ", actual: " + owner);
}
return new CallableMethod(owner, owner, owner, method, INVOKESPECIAL, null, null, null);
}
public Type getSharedVarType(DeclarationDescriptor descriptor) { public Type getSharedVarType(DeclarationDescriptor descriptor) {
if (descriptor instanceof SimpleFunctionDescriptor && descriptor.getContainingDeclaration() instanceof FunctionDescriptor) { if (descriptor instanceof SimpleFunctionDescriptor && descriptor.getContainingDeclaration() instanceof FunctionDescriptor) {
return asmTypeForAnonymousClass(bindingContext, (FunctionDescriptor) descriptor); return asmTypeForAnonymousClass(bindingContext, (FunctionDescriptor) descriptor);