Search in sources :

Example 1 with PointerType

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

the class EnhancedForRewriter method handleArrayIteration.

private void handleArrayIteration(EnhancedForStatement node) {
    Expression expression = node.getExpression();
    ArrayType expressionType = (ArrayType) expression.getTypeMirror();
    VariableElement loopVariable = node.getParameter().getVariableElement();
    TypeMirror componentType = expressionType.getComponentType();
    TypeElement iosArrayType = typeUtil.getIosArray(componentType);
    TypeMirror bufferType = new PointerType(componentType);
    VariableElement arrayVariable = GeneratedVariableElement.newLocalVar("a__", expressionType, null);
    VariableElement bufferVariable = GeneratedVariableElement.newLocalVar("b__", bufferType, null).setTypeQualifiers("const*");
    VariableElement endVariable = GeneratedVariableElement.newLocalVar("e__", bufferType, null).setTypeQualifiers("const*");
    VariableElement bufferField = GeneratedVariableElement.newField("buffer", bufferType, iosArrayType).addModifiers(Modifier.PUBLIC);
    VariableElement sizeField = GeneratedVariableElement.newField("size", typeUtil.getInt(), iosArrayType).addModifiers(Modifier.PUBLIC);
    VariableDeclarationStatement arrayDecl = new VariableDeclarationStatement(arrayVariable, TreeUtil.remove(expression));
    FieldAccess bufferAccess = new FieldAccess(bufferField, new SimpleName(arrayVariable));
    VariableDeclarationStatement bufferDecl = new VariableDeclarationStatement(bufferVariable, bufferAccess);
    InfixExpression endInit = new InfixExpression(bufferType, InfixExpression.Operator.PLUS, new SimpleName(bufferVariable), new FieldAccess(sizeField, new SimpleName(arrayVariable)));
    VariableDeclarationStatement endDecl = new VariableDeclarationStatement(endVariable, endInit);
    WhileStatement loop = new WhileStatement();
    loop.setExpression(new InfixExpression(typeUtil.getBoolean(), InfixExpression.Operator.LESS, new SimpleName(bufferVariable), new SimpleName(endVariable)));
    Block newLoopBody = makeBlock(TreeUtil.remove(node.getBody()));
    loop.setBody(newLoopBody);
    newLoopBody.addStatement(0, new VariableDeclarationStatement(loopVariable, new PrefixExpression(componentType, PrefixExpression.Operator.DEREFERENCE, new PostfixExpression(bufferVariable, PostfixExpression.Operator.INCREMENT))));
    Block block = new Block();
    List<Statement> stmts = block.getStatements();
    stmts.add(arrayDecl);
    stmts.add(bufferDecl);
    stmts.add(endDecl);
    stmts.add(loop);
    replaceLoop(node, block, loop);
}
Also used : TypeElement(javax.lang.model.element.TypeElement) EnhancedForStatement(com.google.devtools.j2objc.ast.EnhancedForStatement) LabeledStatement(com.google.devtools.j2objc.ast.LabeledStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) WhileStatement(com.google.devtools.j2objc.ast.WhileStatement) Statement(com.google.devtools.j2objc.ast.Statement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) PointerType(com.google.devtools.j2objc.types.PointerType) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) WhileStatement(com.google.devtools.j2objc.ast.WhileStatement) ArrayType(javax.lang.model.type.ArrayType) 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) TypeMirror(javax.lang.model.type.TypeMirror) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) Block(com.google.devtools.j2objc.ast.Block) PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) FieldAccess(com.google.devtools.j2objc.ast.FieldAccess)

Example 2 with PointerType

use of com.google.devtools.j2objc.types.PointerType 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 3 with PointerType

use of com.google.devtools.j2objc.types.PointerType 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 4 with PointerType

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

the class Autoboxer method rewriteBoxedAssignment.

private void rewriteBoxedAssignment(Assignment node) {
    Expression lhs = node.getLeftHandSide();
    Expression rhs = node.getRightHandSide();
    TypeMirror type = lhs.getTypeMirror();
    TypeMirror primitiveType = typeUtil.unboxedType(type);
    if (primitiveType == null) {
        return;
    }
    TypeMirror pointerType = new PointerType(type);
    String funcName = "JreBoxed" + getAssignFunctionName(node.getOperator()) + translationUtil.getOperatorFunctionModifier(lhs) + NameTable.capitalize(primitiveType.toString());
    FunctionElement element = new FunctionElement(funcName, type, TypeUtil.asTypeElement(type)).addParameters(pointerType, primitiveType);
    FunctionInvocation invocation = new FunctionInvocation(element, type);
    invocation.addArgument(new PrefixExpression(pointerType, PrefixExpression.Operator.ADDRESS_OF, TreeUtil.remove(lhs)));
    invocation.addArgument(TreeUtil.remove(rhs));
    unbox(rhs);
    node.replaceWith(invocation);
}
Also used : FunctionElement(com.google.devtools.j2objc.types.FunctionElement) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) 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) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) PointerType(com.google.devtools.j2objc.types.PointerType)

Example 5 with PointerType

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

the class ArrayRewriter method getMultiDimensionMethod.

private ExecutableElement getMultiDimensionMethod(TypeMirror componentType, TypeElement iosArrayType, boolean retainedResult) {
    boolean isPrimitive = componentType.getKind().isPrimitive();
    String selector = (retainedResult ? "newArray" : "array") + "WithDimensions:lengths:" + (isPrimitive ? "" : "type:");
    GeneratedExecutableElement element = GeneratedExecutableElement.newMethodWithSelector(selector, TypeUtil.IOS_OBJECT_ARRAY.asType(), iosArrayType).addModifiers(Modifier.PUBLIC, Modifier.STATIC);
    TypeMirror intType = typeUtil.getInt();
    element.addParameter(GeneratedVariableElement.newParameter("dimensions", intType, element));
    element.addParameter(GeneratedVariableElement.newParameter("dimensionLengths", new PointerType(intType), element));
    if (!isPrimitive) {
        element.addParameter(GeneratedVariableElement.newParameter("type", TypeUtil.IOS_CLASS.asType(), element));
    }
    return element;
}
Also used : GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) TypeMirror(javax.lang.model.type.TypeMirror) PointerType(com.google.devtools.j2objc.types.PointerType)

Aggregations

PointerType (com.google.devtools.j2objc.types.PointerType)14 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)13 TypeMirror (javax.lang.model.type.TypeMirror)13 Expression (com.google.devtools.j2objc.ast.Expression)9 FunctionInvocation (com.google.devtools.j2objc.ast.FunctionInvocation)9 FunctionElement (com.google.devtools.j2objc.types.FunctionElement)9 CommaExpression (com.google.devtools.j2objc.ast.CommaExpression)5 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)5 VariableElement (javax.lang.model.element.VariableElement)5 SimpleName (com.google.devtools.j2objc.ast.SimpleName)4 ThisExpression (com.google.devtools.j2objc.ast.ThisExpression)4 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)3 TypeElement (javax.lang.model.element.TypeElement)3 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)2 FieldAccess (com.google.devtools.j2objc.ast.FieldAccess)2 NativeExpression (com.google.devtools.j2objc.ast.NativeExpression)2 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)2 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)2 ArrayInitializer (com.google.devtools.j2objc.ast.ArrayInitializer)1 Block (com.google.devtools.j2objc.ast.Block)1