Search in sources :

Example 31 with Expression

use of com.google.devtools.j2objc.ast.Expression in project j2objc by google.

the class OperatorRewriter method rewriteStringAppend.

private void rewriteStringAppend(Assignment node) {
    List<Expression> operands = getStringAppendOperands(node);
    Expression lhs = node.getLeftHandSide();
    TypeMirror lhsType = lhs.getTypeMirror();
    String funcName = "JreStrAppend" + translationUtil.getOperatorFunctionModifier(lhs);
    FunctionElement element = new FunctionElement(funcName, TypeUtil.ID_TYPE, null).addParameters(TypeUtil.ID_PTR_TYPE, TypeUtil.NATIVE_CHAR_PTR).setIsVarargs(true);
    FunctionInvocation invocation = new FunctionInvocation(element, lhsType);
    List<Expression> args = invocation.getArguments();
    args.add(new PrefixExpression(new PointerType(lhsType), PrefixExpression.Operator.ADDRESS_OF, TreeUtil.remove(lhs)));
    args.add(getStrcatTypesCString(operands));
    args.addAll(operands);
    node.replaceWith(invocation);
}
Also used : FunctionElement(com.google.devtools.j2objc.types.FunctionElement) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) Expression(com.google.devtools.j2objc.ast.Expression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) CommaExpression(com.google.devtools.j2objc.ast.CommaExpression) TypeMirror(javax.lang.model.type.TypeMirror) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) PointerType(com.google.devtools.j2objc.types.PointerType)

Example 32 with Expression

use of com.google.devtools.j2objc.ast.Expression in project j2objc by google.

the class OperatorRewriter method endVisit.

@Override
public void endVisit(InfixExpression node) {
    InfixExpression.Operator op = node.getOperator();
    TypeMirror nodeType = node.getTypeMirror();
    String funcName = getInfixFunction(op, nodeType);
    if (funcName != null) {
        Iterator<Expression> operandIter = node.getOperands().iterator();
        Expression leftOperand = operandIter.next();
        operandIter.remove();
        // translated here are all left-associative.
        while (operandIter.hasNext()) {
            Expression rightOperand = operandIter.next();
            operandIter.remove();
            FunctionElement element = new FunctionElement(funcName, nodeType, null).addParameters(leftOperand.getTypeMirror(), rightOperand.getTypeMirror());
            FunctionInvocation invocation = new FunctionInvocation(element, nodeType);
            List<Expression> args = invocation.getArguments();
            args.add(leftOperand);
            args.add(rightOperand);
            leftOperand = invocation;
        }
        node.replaceWith(leftOperand);
    } else if (op == InfixExpression.Operator.PLUS && typeUtil.isString(nodeType) && !isStringAppend(node.getParent())) {
        rewriteStringConcatenation(node);
    }
}
Also used : FunctionElement(com.google.devtools.j2objc.types.FunctionElement) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) TypeMirror(javax.lang.model.type.TypeMirror) Expression(com.google.devtools.j2objc.ast.Expression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) CommaExpression(com.google.devtools.j2objc.ast.CommaExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression)

Example 33 with Expression

use of com.google.devtools.j2objc.ast.Expression in project j2objc by google.

the class JavaToIOSMethodTranslator method visit.

@Override
public boolean visit(ClassInstanceCreation node) {
    // translate any embedded method invocations
    if (node.getExpression() != null) {
        node.getExpression().accept(this);
    }
    for (Expression e : node.getArguments()) {
        e.accept(this);
    }
    if (node.getAnonymousClassDeclaration() != null) {
        node.getAnonymousClassDeclaration().accept(this);
    }
    ExecutableElement method = node.getExecutableElement();
    String key = Mappings.getMethodKey(method, typeUtil);
    String selector = Mappings.STRING_CONSTRUCTOR_TO_METHOD_MAPPINGS.get(key);
    if (selector != null) {
        assert !node.hasRetainedResult();
        if (key.equals("java.lang.String.<init>(Ljava/lang/String;)V")) {
            // Special case: replace new String(constant) to constant (avoid clang warning).
            Expression arg = node.getArgument(0);
            if (arg instanceof StringLiteral) {
                node.replaceWith(arg.copy());
                return false;
            }
        }
        ExecutableElement newElement = GeneratedExecutableElement.newMappedMethod(selector, method);
        MethodInvocation newInvocation = new MethodInvocation(new ExecutablePair(newElement), new SimpleName(ElementUtil.getDeclaringClass(method)));
        TreeUtil.copyList(node.getArguments(), newInvocation.getArguments());
        node.replaceWith(newInvocation);
    }
    return true;
}
Also used : StringLiteral(com.google.devtools.j2objc.ast.StringLiteral) Expression(com.google.devtools.j2objc.ast.Expression) ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) MethodInvocation(com.google.devtools.j2objc.ast.MethodInvocation)

Example 34 with Expression

use of com.google.devtools.j2objc.ast.Expression in project j2objc by google.

the class Rewriter method rewriteStringConcat.

private void rewriteStringConcat(InfixExpression node) {
    // Collect all non-string operands that precede the first string operand.
    // If there are multiple such operands, move them into a sub-expression.
    List<Expression> nonStringOperands = new ArrayList<>();
    TypeMirror nonStringExprType = null;
    for (Expression operand : node.getOperands()) {
        TypeMirror operandType = operand.getTypeMirror();
        if (typeUtil.isString(operandType)) {
            break;
        }
        nonStringOperands.add(operand);
        nonStringExprType = getAdditionType(nonStringExprType, operandType);
    }
    if (nonStringOperands.size() < 2) {
        return;
    }
    InfixExpression nonStringExpr = new InfixExpression(nonStringExprType, InfixExpression.Operator.PLUS);
    for (Expression operand : nonStringOperands) {
        nonStringExpr.addOperand(TreeUtil.remove(operand));
    }
    node.addOperand(0, nonStringExpr);
}
Also used : Expression(com.google.devtools.j2objc.ast.Expression) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) TypeMirror(javax.lang.model.type.TypeMirror) ArrayList(java.util.ArrayList) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression)

Example 35 with Expression

use of com.google.devtools.j2objc.ast.Expression in project j2objc by google.

the class TranslationUtil method createAnnotationValue.

public Expression createAnnotationValue(TypeMirror type, AnnotationValue aValue) {
    Object value = aValue.getValue();
    if (value == null) {
        return new NullLiteral(typeUtil.getNull());
    } else if (value instanceof VariableElement) {
        return new SimpleName((VariableElement) value);
    } else if (TypeUtil.isArray(type)) {
        assert value instanceof List;
        ArrayType arrayType = (ArrayType) type;
        @SuppressWarnings("unchecked") List<? extends AnnotationValue> list = (List<? extends AnnotationValue>) value;
        List<Expression> generatedValues = new ArrayList<>();
        for (AnnotationValue elem : list) {
            generatedValues.add(createAnnotationValue(arrayType.getComponentType(), elem));
        }
        return createObjectArray(generatedValues, arrayType);
    } else if (TypeUtil.isAnnotation(type)) {
        assert value instanceof AnnotationMirror;
        return createAnnotation((AnnotationMirror) value);
    } else if (value instanceof TypeMirror) {
        return new TypeLiteral((TypeMirror) value, typeUtil);
    } else {
        // Boolean, Character, Number, String
        return TreeUtil.newLiteral(value, typeUtil);
    }
}
Also used : SimpleName(com.google.devtools.j2objc.ast.SimpleName) ArrayList(java.util.ArrayList) VariableElement(javax.lang.model.element.VariableElement) ArrayType(javax.lang.model.type.ArrayType) AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeLiteral(com.google.devtools.j2objc.ast.TypeLiteral) CastExpression(com.google.devtools.j2objc.ast.CastExpression) PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) Expression(com.google.devtools.j2objc.ast.Expression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) ConditionalExpression(com.google.devtools.j2objc.ast.ConditionalExpression) TypeMirror(javax.lang.model.type.TypeMirror) AnnotationValue(javax.lang.model.element.AnnotationValue) ArrayList(java.util.ArrayList) List(java.util.List) NullLiteral(com.google.devtools.j2objc.ast.NullLiteral)

Aggregations

Expression (com.google.devtools.j2objc.ast.Expression)106 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)80 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)68 ParenthesizedExpression (com.google.devtools.j2objc.ast.ParenthesizedExpression)60 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)57 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)50 CastExpression (com.google.devtools.j2objc.ast.CastExpression)46 VariableDeclarationExpression (com.google.devtools.j2objc.ast.VariableDeclarationExpression)45 ThisExpression (com.google.devtools.j2objc.ast.ThisExpression)42 CommaExpression (com.google.devtools.j2objc.ast.CommaExpression)32 TypeMirror (javax.lang.model.type.TypeMirror)32 InstanceofExpression (com.google.devtools.j2objc.ast.InstanceofExpression)30 VariableElement (javax.lang.model.element.VariableElement)30 LambdaExpression (com.google.devtools.j2objc.ast.LambdaExpression)26 SimpleName (com.google.devtools.j2objc.ast.SimpleName)25 FunctionInvocation (com.google.devtools.j2objc.ast.FunctionInvocation)19 NativeExpression (com.google.devtools.j2objc.ast.NativeExpression)19 FunctionalExpression (com.google.devtools.j2objc.ast.FunctionalExpression)15 FunctionElement (com.google.devtools.j2objc.types.FunctionElement)14 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)14