Search in sources :

Example 6 with FunctionElement

use of com.google.devtools.j2objc.types.FunctionElement in project j2objc by google.

the class Functionizer method endVisit.

@Override
public void endVisit(SuperConstructorInvocation node) {
    ExecutableElement element = node.getExecutableElement();
    AbstractTypeDeclaration typeDecl = TreeUtil.getEnclosingType(node);
    TypeElement type = typeDecl.getTypeElement();
    FunctionElement funcElement = newFunctionElement(element);
    FunctionInvocation invocation = new FunctionInvocation(funcElement, typeUtil.getVoid());
    List<Expression> args = invocation.getArguments();
    args.add(new ThisExpression(ElementUtil.getDeclaringClass(element).asType()));
    if (typeDecl instanceof TypeDeclaration) {
        TypeDeclaration typeDeclaration = (TypeDeclaration) typeDecl;
        if (captureInfo.needsOuterParam(ElementUtil.getSuperclass(type))) {
            Expression outerArg = TreeUtil.remove(node.getExpression());
            args.add(outerArg != null ? outerArg : typeDeclaration.getSuperOuter().copy());
        }
        TreeUtil.moveList(typeDeclaration.getSuperCaptureArgs(), args);
    }
    TreeUtil.moveList(node.getArguments(), args);
    if (ElementUtil.isEnum(type)) {
        for (VariableElement param : captureInfo.getImplicitEnumParams()) {
            args.add(new SimpleName(param));
        }
    }
    node.replaceWith(new ExpressionStatement(invocation));
    assert funcElement.getParameterTypes().size() == args.size();
}
Also used : FunctionElement(com.google.devtools.j2objc.types.FunctionElement) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) Expression(com.google.devtools.j2objc.ast.Expression) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) TypeElement(javax.lang.model.element.TypeElement) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) VariableElement(javax.lang.model.element.VariableElement) AnnotationTypeDeclaration(com.google.devtools.j2objc.ast.AnnotationTypeDeclaration) AbstractTypeDeclaration(com.google.devtools.j2objc.ast.AbstractTypeDeclaration) TypeDeclaration(com.google.devtools.j2objc.ast.TypeDeclaration) AbstractTypeDeclaration(com.google.devtools.j2objc.ast.AbstractTypeDeclaration)

Example 7 with FunctionElement

use of com.google.devtools.j2objc.types.FunctionElement in project j2objc by google.

the class NilCheckResolver method addNilCheck.

private void addNilCheck(Expression node) {
    if (!needsNilCheck(node)) {
        return;
    }
    VariableElement var = TreeUtil.getVariableElement(node);
    if (var != null) {
        addSafeVar(var);
    }
    TypeMirror idType = TypeUtil.ID_TYPE;
    FunctionElement element = new FunctionElement("nil_chk", idType, null).addParameters(idType);
    FunctionInvocation nilChkInvocation = new FunctionInvocation(element, node.getTypeMirror());
    node.replaceWith(nilChkInvocation);
    nilChkInvocation.addArgument(node);
}
Also used : FunctionElement(com.google.devtools.j2objc.types.FunctionElement) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) TypeMirror(javax.lang.model.type.TypeMirror) VariableElement(javax.lang.model.element.VariableElement)

Example 8 with FunctionElement

use of com.google.devtools.j2objc.types.FunctionElement in project j2objc by google.

the class OperatorRewriter method rewriteRegularAssignment.

private void rewriteRegularAssignment(Assignment node) {
    VariableElement var = TreeUtil.getVariableElement(node.getLeftHandSide());
    if (var == null) {
        return;
    }
    handleRetainedLocal(var, node.getRightHandSide());
    boolean isRetainedWith = ElementUtil.isRetainedWithField(var);
    String funcName = getAssignmentFunctionName(node, var, isRetainedWith);
    if (funcName == null) {
        return;
    }
    TypeMirror type = node.getTypeMirror();
    TypeMirror idType = TypeUtil.ID_TYPE;
    TypeMirror declaredType = type.getKind().isPrimitive() ? type : idType;
    Expression lhs = node.getLeftHandSide();
    FunctionElement element = new FunctionElement(funcName, declaredType, null);
    FunctionInvocation invocation = new FunctionInvocation(element, type);
    List<Expression> args = invocation.getArguments();
    if (isRetainedWith) {
        element.addParameters(idType);
        args.add(getRetainedWithTarget(node, var));
    }
    element.addParameters(TypeUtil.ID_PTR_TYPE, idType);
    args.add(new PrefixExpression(new PointerType(lhs.getTypeMirror()), PrefixExpression.Operator.ADDRESS_OF, TreeUtil.remove(lhs)));
    args.add(TreeUtil.remove(node.getRightHandSide()));
    node.replaceWith(invocation);
}
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) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) PointerType(com.google.devtools.j2objc.types.PointerType) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement)

Example 9 with FunctionElement

use of com.google.devtools.j2objc.types.FunctionElement in project j2objc by google.

the class OperatorRewriter method rewriteStringConcatenation.

private void rewriteStringConcatenation(InfixExpression node) {
    List<Expression> childOperands = node.getOperands();
    List<Expression> operands = Lists.newArrayListWithCapacity(childOperands.size());
    TreeUtil.moveList(childOperands, operands);
    operands = coalesceStringLiterals(operands);
    if (operands.size() == 1 && typeUtil.isString(operands.get(0).getTypeMirror())) {
        node.replaceWith(operands.get(0));
        return;
    }
    TypeMirror stringType = typeUtil.getJavaString().asType();
    FunctionElement element = new FunctionElement("JreStrcat", stringType, null).addParameters(TypeUtil.NATIVE_CHAR_PTR).setIsVarargs(true);
    FunctionInvocation invocation = new FunctionInvocation(element, stringType);
    List<Expression> args = invocation.getArguments();
    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)

Example 10 with FunctionElement

use of com.google.devtools.j2objc.types.FunctionElement 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)

Aggregations

FunctionElement (com.google.devtools.j2objc.types.FunctionElement)27 FunctionInvocation (com.google.devtools.j2objc.ast.FunctionInvocation)25 TypeMirror (javax.lang.model.type.TypeMirror)20 Expression (com.google.devtools.j2objc.ast.Expression)14 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)12 TypeElement (javax.lang.model.element.TypeElement)11 ThisExpression (com.google.devtools.j2objc.ast.ThisExpression)10 PointerType (com.google.devtools.j2objc.types.PointerType)9 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)8 ExecutableElement (javax.lang.model.element.ExecutableElement)8 VariableElement (javax.lang.model.element.VariableElement)8 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)7 SimpleName (com.google.devtools.j2objc.ast.SimpleName)7 CommaExpression (com.google.devtools.j2objc.ast.CommaExpression)6 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)6 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)5 NativeExpression (com.google.devtools.j2objc.ast.NativeExpression)3 Assignment (com.google.devtools.j2objc.ast.Assignment)2 CastExpression (com.google.devtools.j2objc.ast.CastExpression)2 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)2