Increment convertion

This commit is contained in:
Michael Bogdanov
2015-04-03 13:55:52 +03:00
parent 8ec0cd18b7
commit 0c8ee872f9
3 changed files with 29 additions and 13 deletions
@@ -3394,15 +3394,15 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
Collections.singletonList(expression.getBaseExpression()), receiver); Collections.singletonList(expression.getBaseExpression()), receiver);
} }
DeclarationDescriptor cls = op.getContainingDeclaration(); String operationName = originalOperation == null ? "" : originalOperation.getName().asString();
if (!(operationName.equals("inc") || operationName.equals("dec"))) {
if (isPrimitiveNumberClassDescriptor(cls) || !(originalOperation.getName().asString().equals("inc") || originalOperation.getName().asString().equals("dec"))) {
return invokeFunction(resolvedCall, receiver); return invokeFunction(resolvedCall, receiver);
} }
int increment = operationName.equals("inc") ? 1 : -1;
Type type = expressionType(expression.getBaseExpression()); Type type = expressionType(expression.getBaseExpression());
StackValue value = gen(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 @Override
@@ -420,6 +420,9 @@ public abstract class StackValue {
ResolvedCall resolvedCall, ResolvedCall resolvedCall,
@NotNull ExpressionCodegen codegen @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); return new PrefixIncrement(type, stackValue, delta, method, resolvedCall, codegen);
} }
@@ -25,6 +25,9 @@ import org.jetbrains.kotlin.codegen.AsmUtil.genIncrement
import org.jetbrains.kotlin.codegen.AsmUtil.isPrimitive import org.jetbrains.kotlin.codegen.AsmUtil.isPrimitive
import org.jetbrains.kotlin.codegen.CallableMethod import org.jetbrains.kotlin.codegen.CallableMethod
import org.jetbrains.kotlin.codegen.ExtendedCallable 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() { 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 } assert(isPrimitive(returnType)) { "Return type of Increment intrinsic should be of primitive type : " + returnType }
if (arguments.size() > 0) { if (arguments.size() > 0) {
val operand = arguments.get(0) throw UnsupportedOperationException("fail");
val stackValue = codegen.genQualified(receiver, operand) // val operand = arguments.get(0)
if (stackValue is StackValue.Local && Type.INT_TYPE == stackValue.type) { // val stackValue = codegen.genQualified(receiver, operand)
return StackValue.preIncrementForLocalVar(stackValue.index, myDelta) // 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.preIncrement(returnType, stackValue, myDelta, this, null, codegen)
// }
} }
else { else {
return StackValue.operation(returnType) { return StackValue.operation(returnType) {
@@ -50,6 +54,15 @@ public class Increment(private val myDelta: Int) : LazyIntrinsicMethod() {
} }
override fun supportCallable(): Boolean { 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)
}
} }
} }