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);
}
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
@@ -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);
}
@@ -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)
}
}
}