From 0c8ee872f987827365a7c007d3e3c2e16e2da667 Mon Sep 17 00:00:00 2001 From: Michael Bogdanov Date: Fri, 3 Apr 2015 13:55:52 +0300 Subject: [PATCH] Increment convertion --- .../kotlin/codegen/ExpressionCodegen.java | 8 ++--- .../jetbrains/kotlin/codegen/StackValue.java | 3 ++ .../kotlin/codegen/intrinsics/Increment.kt | 31 +++++++++++++------ 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java index e14bbdb051e..1019ddbf28e 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java @@ -3394,15 +3394,15 @@ public class ExpressionCodegen extends JetVisitor implem Collections.singletonList(expression.getBaseExpression()), receiver); } - DeclarationDescriptor cls = op.getContainingDeclaration(); - - if (isPrimitiveNumberClassDescriptor(cls) || !(originalOperation.getName().asString().equals("inc") || originalOperation.getName().asString().equals("dec"))) { + String operationName = originalOperation == null ? "" : originalOperation.getName().asString(); + if (!(operationName.equals("inc") || operationName.equals("dec"))) { return invokeFunction(resolvedCall, receiver); } + int increment = operationName.equals("inc") ? 1 : -1; Type type = expressionType(expression.getBaseExpression()); StackValue value = gen(expression.getBaseExpression()); - return StackValue.preIncrement(type, value, -1, callable, resolvedCall, this); + return StackValue.preIncrement(type, value, increment, callable, resolvedCall, this); } @Override diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/StackValue.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/StackValue.java index 6cb40510fa2..a763698791c 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/StackValue.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/StackValue.java @@ -420,6 +420,9 @@ public abstract class StackValue { ResolvedCall resolvedCall, @NotNull ExpressionCodegen codegen ) { + if (stackValue instanceof StackValue.Local && Type.INT_TYPE == stackValue.type) { + return preIncrementForLocalVar(((StackValue.Local) stackValue).index, delta); + } return new PrefixIncrement(type, stackValue, delta, method, resolvedCall, codegen); } diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/intrinsics/Increment.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/intrinsics/Increment.kt index 87c28ddc7e6..1b7b961ded3 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/intrinsics/Increment.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/intrinsics/Increment.kt @@ -25,6 +25,9 @@ import org.jetbrains.kotlin.codegen.AsmUtil.genIncrement import org.jetbrains.kotlin.codegen.AsmUtil.isPrimitive import org.jetbrains.kotlin.codegen.CallableMethod import org.jetbrains.kotlin.codegen.ExtendedCallable +import org.jetbrains.kotlin.descriptors.FunctionDescriptor +import org.jetbrains.kotlin.psi.JetPrefixExpression +import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall public class Increment(private val myDelta: Int) : LazyIntrinsicMethod() { @@ -32,14 +35,15 @@ public class Increment(private val myDelta: Int) : LazyIntrinsicMethod() { assert(isPrimitive(returnType)) { "Return type of Increment intrinsic should be of primitive type : " + returnType } if (arguments.size() > 0) { - val operand = arguments.get(0) - val stackValue = codegen.genQualified(receiver, operand) - if (stackValue is StackValue.Local && Type.INT_TYPE == stackValue.type) { - return StackValue.preIncrementForLocalVar(stackValue.index, myDelta) - } - else { - return StackValue.preIncrement(returnType, stackValue, myDelta, this, null, codegen) - } + throw UnsupportedOperationException("fail"); +// val operand = arguments.get(0) +// val stackValue = codegen.genQualified(receiver, operand) +// if (stackValue is StackValue.Local && Type.INT_TYPE == stackValue.type) { +// return StackValue.preIncrementForLocalVar(stackValue.index, myDelta) +// } +// else { +// return StackValue.preIncrement(returnType, stackValue, myDelta, this, null, codegen) +// } } else { return StackValue.operation(returnType) { @@ -50,6 +54,15 @@ public class Increment(private val myDelta: Int) : LazyIntrinsicMethod() { } override fun supportCallable(): Boolean { - return false + return true + } + + override fun toCallable(fd: FunctionDescriptor, isSuper: Boolean, resolvedCall: ResolvedCall<*>, codegen: ExpressionCodegen): ExtendedCallable { + val method = codegen.getState().getTypeMapper().mapToCallableMethod(fd, false, codegen.getContext()) + return MappedCallable(method) { + val jetExpression = resolvedCall.getCall().getCalleeExpression() + assert(jetExpression !is JetPrefixExpression) { "There should be postfix increment ${jetExpression!!.getText()}" } + genIncrement(getReturnType(), myDelta, it) + } } }