string plus is an intrinsic

This commit is contained in:
Dmitry Jemerov
2011-07-13 20:05:32 +02:00
parent 769b9cead5
commit bfd571b7ee
3 changed files with 30 additions and 18 deletions
@@ -35,7 +35,7 @@ import java.util.*;
public class ExpressionCodegen extends JetVisitor {
private static final String CLASS_OBJECT = "java/lang/Object";
private static final String CLASS_STRING = "java/lang/String";
private static final String CLASS_STRING_BUILDER = "java/lang/StringBuilder";
public static final String CLASS_STRING_BUILDER = "java/lang/StringBuilder";
private static final String CLASS_COMPARABLE = "java/lang/Comparable";
private static final String CLASS_ITERABLE = "java/lang/Iterable";
private static final String CLASS_ITERATOR = "java/util/Iterator";
@@ -1085,11 +1085,6 @@ public class ExpressionCodegen extends JetVisitor {
Arrays.asList(expression.getLeft(), expression.getRight())));
return;
}
DeclarationDescriptor cls = op.getContainingDeclaration();
if (isClass(cls, "String") && op.getName().equals("plus")) {
generateConcatenation(expression);
return;
}
}
throw new UnsupportedOperationException("Don't know how to generate binary op " + expression);
}
@@ -1289,15 +1284,7 @@ public class ExpressionCodegen extends JetVisitor {
}
}
private void generateConcatenation(JetBinaryExpression expression) {
generateStringBuilderConstructor();
invokeAppend(expression.getLeft());
invokeAppend(expression.getRight());
v.invokevirtual(CLASS_STRING_BUILDER, "toString", "()Ljava/lang/String;");
myStack.push(StackValue.onStack(Type.getObjectType(CLASS_STRING)));
}
private void generateStringBuilderConstructor() {
public void generateStringBuilderConstructor() {
Type type = Type.getObjectType(CLASS_STRING_BUILDER);
v.anew(type);
v.dup();
@@ -1305,7 +1292,7 @@ public class ExpressionCodegen extends JetVisitor {
v.invokespecial(CLASS_STRING_BUILDER, method.getName(), method.getDescriptor());
}
private void invokeAppend(final JetExpression expr) {
public void invokeAppend(final JetExpression expr) {
if (expr instanceof JetBinaryExpression) {
final JetBinaryExpression binaryExpression = (JetBinaryExpression) expr;
if (binaryExpression.getOperationToken() == JetTokens.PLUS) {
@@ -0,0 +1,24 @@
package org.jetbrains.jet.codegen.intrinsics;
import com.intellij.psi.PsiElement;
import org.jetbrains.jet.codegen.ExpressionCodegen;
import org.jetbrains.jet.codegen.StackValue;
import org.jetbrains.jet.lang.psi.JetExpression;
import org.objectweb.asm.Type;
import org.objectweb.asm.commons.InstructionAdapter;
import java.util.List;
/**
* @author yole
*/
public class Concat implements IntrinsicMethod {
@Override
public StackValue generate(ExpressionCodegen codegen, InstructionAdapter v, Type expectedType, PsiElement element, List<JetExpression> arguments) {
codegen.generateStringBuilderConstructor();
codegen.invokeAppend(arguments.get(0));
codegen.invokeAppend(arguments.get(1));
v.invokevirtual(ExpressionCodegen.CLASS_STRING_BUILDER, "toString", "()Ljava/lang/String;");
return StackValue.onStack(Type.getObjectType("java/lang/String"));
}
}
@@ -19,7 +19,6 @@ import java.util.Map;
public class IntrinsicMethods {
private static final IntrinsicMethod UNARY_MINUS = new UnaryMinus();
private static final IntrinsicMethod NUMBER_CAST = new NumberCast();
private static final IntrinsicMethod ARRAY_SIZE = new ArraySize();
private static final IntrinsicMethod INV = new Inv();
private static final IntrinsicMethod TYPEINFO = new TypeInfo();
private static final IntrinsicMethod VALUE_TYPEINFO = new ValueTypeInfo();
@@ -35,7 +34,7 @@ public class IntrinsicMethods {
for (String method : primitiveCastMethods) {
declareIntrinsicProperty("Number", method, NUMBER_CAST);
}
declareIntrinsicProperty("Array", "size", ARRAY_SIZE);
declareIntrinsicProperty("Array", "size", new ArraySize());
for (String primitiveNumberType : PRIMITIVE_NUMBER_TYPES) {
declareIntrinsicFunction(primitiveNumberType, "minus", 0, UNARY_MINUS);
@@ -57,6 +56,8 @@ public class IntrinsicMethods {
declareBinaryOp("and", Opcodes.IAND);
declareBinaryOp("or", Opcodes.IOR);
declareBinaryOp("xor", Opcodes.IXOR);
declareIntrinsicFunction("String", "plus", 1, new Concat());
}
private void declareBinaryOp(String methodName, int opcode) {