Minor refactoring in JetTypeMapper.mapReturnType

This commit is contained in:
Alexander Udalov
2013-12-27 14:05:00 +04:00
parent d8c5b0236d
commit fe96e64bd4
4 changed files with 16 additions and 25 deletions
@@ -532,7 +532,7 @@ public class AsmUtil {
JetType type = descriptor.getReturnType(); JetType type = descriptor.getReturnType();
if (type == null || isNullableType(type)) return; if (type == null || isNullableType(type)) return;
Type asmType = state.getTypeMapper().mapReturnType(type); Type asmType = state.getTypeMapper().mapReturnType(descriptor);
if (asmType.getSort() == Type.OBJECT || asmType.getSort() == Type.ARRAY) { if (asmType.getSort() == Type.OBJECT || asmType.getSort() == Type.ARRAY) {
v.dup(); v.dup();
v.visitLdcInsn(descriptor.getContainingDeclaration().getName().asString()); v.visitLdcInsn(descriptor.getContainingDeclaration().getName().asString());
@@ -2006,8 +2006,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
if (callable instanceof CallableMethod) { if (callable instanceof CallableMethod) {
CallableMethod callableMethod = (CallableMethod) callable; CallableMethod callableMethod = (CallableMethod) callable;
invokeMethodWithArguments(callableMethod, resolvedCall, receiver); invokeMethodWithArguments(callableMethod, resolvedCall, receiver);
//noinspection ConstantConditions Type returnType = typeMapper.mapReturnType(resolvedCall.getResultingDescriptor());
Type returnType = typeMapper.mapReturnType(resolvedCall.getResultingDescriptor().getReturnType());
StackValue.coerce(callableMethod.getSignature().getAsmMethod().getReturnType(), returnType, v); StackValue.coerce(callableMethod.getSignature().getAsmMethod().getReturnType(), returnType, v);
return StackValue.onStack(returnType); return StackValue.onStack(returnType);
} }
@@ -788,13 +788,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
} }
private void generateComponentFunction(@NotNull FunctionDescriptor function, @NotNull final ValueParameterDescriptor parameter) { private void generateComponentFunction(@NotNull FunctionDescriptor function, @NotNull final ValueParameterDescriptor parameter) {
JetType returnType = function.getReturnType(); functionCodegen.generateMethod(myClass, typeMapper.mapSignature(function), function, new FunctionGenerationStrategy() {
assert returnType != null : "Return type of component function should not be null: " + function;
final Type componentType = typeMapper.mapReturnType(returnType);
JvmMethodSignature signature = typeMapper.mapSignature(function);
functionCodegen.generateMethod(myClass, signature, function, new FunctionGenerationStrategy() {
@Override @Override
public void generateBody( public void generateBody(
@NotNull MethodVisitor mv, @NotNull MethodVisitor mv,
@@ -802,6 +796,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
@NotNull MethodContext context, @NotNull MethodContext context,
@Nullable MemberCodegen parentCodegen @Nullable MemberCodegen parentCodegen
) { ) {
Type componentType = signature.getAsmMethod().getReturnType();
InstructionAdapter iv = new InstructionAdapter(mv); InstructionAdapter iv = new InstructionAdapter(mv);
if (!componentType.equals(Type.VOID_TYPE)) { if (!componentType.equals(Type.VOID_TYPE)) {
iv.load(0, classAsmType); iv.load(0, classAsmType);
@@ -143,19 +143,23 @@ public class JetTypeMapper extends BindingTraceAware {
} }
@NotNull @NotNull
public Type mapReturnType(@NotNull JetType jetType) { public Type mapReturnType(@NotNull CallableDescriptor descriptor) {
return mapReturnType(jetType, null); return mapReturnType(descriptor, null);
} }
@NotNull @NotNull
private Type mapReturnType(@NotNull JetType jetType, @Nullable BothSignatureWriter signatureVisitor) { private Type mapReturnType(@NotNull CallableDescriptor descriptor, @Nullable BothSignatureWriter sw) {
if (jetType.equals(KotlinBuiltIns.getInstance().getUnitType())) { JetType returnType = descriptor.getReturnType();
if (signatureVisitor != null) { assert returnType != null : "Function has no return type: " + descriptor;
signatureVisitor.writeAsmType(Type.VOID_TYPE); if (returnType.equals(KotlinBuiltIns.getInstance().getUnitType()) && !(descriptor instanceof PropertyGetterDescriptor)) {
if (sw != null) {
sw.writeAsmType(Type.VOID_TYPE);
} }
return Type.VOID_TYPE; return Type.VOID_TYPE;
} }
return mapType(jetType, signatureVisitor, JetTypeMapperMode.VALUE, Variance.OUT_VARIANCE, false); else {
return mapType(returnType, sw, JetTypeMapperMode.VALUE, Variance.OUT_VARIANCE, false);
}
} }
@NotNull @NotNull
@@ -602,14 +606,7 @@ public class JetTypeMapper extends BindingTraceAware {
} }
sw.writeReturnType(); sw.writeReturnType();
JetType returnType = f.getReturnType(); mapReturnType(f, sw);
assert returnType != null : "Function " + f + " has no return type";
if (f instanceof PropertyGetterDescriptor) {
mapType(returnType, sw, JetTypeMapperMode.VALUE, Variance.OUT_VARIANCE, false);
}
else {
mapReturnType(returnType, sw);
}
sw.writeReturnTypeEnd(); sw.writeReturnTypeEnd();
} }