Search in sources :

Example 1 with StringLiteral

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

the class EnumRewriter method addNonArcInitialization.

private void addNonArcInitialization(EnumDeclaration node) {
    TypeElement type = node.getTypeElement();
    int baseTypeCount = 0;
    List<Statement> sizeStatements = new ArrayList<>();
    List<Statement> initStatements = new ArrayList<>();
    TypeMirror voidType = typeUtil.getVoid();
    VariableElement localEnum = GeneratedVariableElement.newLocalVar("e", TypeUtil.ID_TYPE, null);
    int i = 0;
    for (EnumConstantDeclaration constant : node.getEnumConstants()) {
        VariableElement varElement = constant.getVariableElement();
        String varName = ElementUtil.getName(varElement);
        ExecutableElement methodElement = constant.getExecutableElement();
        TypeElement valueType = ElementUtil.getDeclaringClass(methodElement);
        boolean isAnonymous = valueType != type;
        String classExpr = isAnonymous ? "[" + nameTable.getFullName(valueType) + " class]" : "self";
        String sizeName = "objSize" + (isAnonymous ? "_" + varName : "");
        if (isAnonymous) {
            sizeStatements.add(new NativeStatement(UnicodeUtils.format("size_t %s = class_getInstanceSize(%s);", sizeName, classExpr)));
            sizeStatements.add(new NativeStatement(UnicodeUtils.format("allocSize += %s;", sizeName)));
        } else {
            baseTypeCount++;
        }
        initStatements.add(new ExpressionStatement(new CommaExpression(new Assignment(new SimpleName(varElement), new Assignment(new SimpleName(localEnum), new NativeExpression(UnicodeUtils.format("objc_constructInstance(%s, (void *)ptr)", classExpr), type.asType()))), new NativeExpression("ptr += " + sizeName, voidType))));
        String initName = nameTable.getFullFunctionName(methodElement);
        FunctionElement initElement = new FunctionElement(initName, voidType, valueType).addParameters(valueType.asType()).addParameters(ElementUtil.asTypes(methodElement.getParameters()));
        FunctionInvocation initFunc = new FunctionInvocation(initElement, voidType);
        initFunc.addArgument(new SimpleName(localEnum));
        TreeUtil.copyList(constant.getArguments(), initFunc.getArguments());
        initFunc.addArgument(new StringLiteral(varName, typeUtil));
        initFunc.addArgument(new NumberLiteral(i++, typeUtil));
        initStatements.add(new ExpressionStatement(initFunc));
    }
    List<Statement> stmts = node.getClassInitStatements().subList(0, 0);
    if (baseTypeCount == 0) {
        stmts.add(new NativeStatement("size_t allocSize = 0;"));
    } else {
        stmts.add(new NativeStatement("size_t objSize = class_getInstanceSize(self);"));
        stmts.add(new NativeStatement(UnicodeUtils.format("size_t allocSize = %s * objSize;", baseTypeCount)));
    }
    stmts.addAll(sizeStatements);
    stmts.add(new NativeStatement("uintptr_t ptr = (uintptr_t)calloc(allocSize, 1);"));
    stmts.add(new VariableDeclarationStatement(localEnum, null));
    stmts.addAll(initStatements);
}
Also used : FunctionElement(com.google.devtools.j2objc.types.FunctionElement) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) GeneratedTypeElement(com.google.devtools.j2objc.types.GeneratedTypeElement) TypeElement(javax.lang.model.element.TypeElement) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) ForStatement(com.google.devtools.j2objc.ast.ForStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) Statement(com.google.devtools.j2objc.ast.Statement) NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) ExecutableElement(javax.lang.model.element.ExecutableElement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) ArrayList(java.util.ArrayList) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) Assignment(com.google.devtools.j2objc.ast.Assignment) EnumConstantDeclaration(com.google.devtools.j2objc.ast.EnumConstantDeclaration) NativeExpression(com.google.devtools.j2objc.ast.NativeExpression) NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) CommaExpression(com.google.devtools.j2objc.ast.CommaExpression) StringLiteral(com.google.devtools.j2objc.ast.StringLiteral) TypeMirror(javax.lang.model.type.TypeMirror) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) NumberLiteral(com.google.devtools.j2objc.ast.NumberLiteral)

Example 2 with StringLiteral

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

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

the class TreeConverter method convertStringLiteral.

private static TreeNode convertStringLiteral(org.eclipse.jdt.core.dom.StringLiteral node) {
    StringLiteral newNode = new StringLiteral();
    convertExpression(node, newNode);
    return newNode.setLiteralValue(node.getLiteralValue()).setTypeMirror(BindingConverter.getType(node.resolveTypeBinding()));
}
Also used : StringLiteral(com.google.devtools.j2objc.ast.StringLiteral)

Example 4 with StringLiteral

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

the class StatementGenerator method visit.

@Override
public boolean visit(InfixExpression node) {
    InfixExpression.Operator op = node.getOperator();
    List<Expression> operands = node.getOperands();
    assert operands.size() >= 2;
    if ((op.equals(InfixExpression.Operator.EQUALS) || op.equals(InfixExpression.Operator.NOT_EQUALS))) {
        Expression lhs = operands.get(0);
        Expression rhs = operands.get(1);
        if (lhs instanceof StringLiteral || rhs instanceof StringLiteral) {
            if (!(lhs instanceof StringLiteral)) {
                // In case the lhs can't call isEqual.
                lhs = operands.get(1);
                rhs = operands.get(0);
            }
            buffer.append(op.equals(InfixExpression.Operator.NOT_EQUALS) ? "![" : "[");
            lhs.accept(this);
            buffer.append(" isEqual:");
            rhs.accept(this);
            buffer.append("]");
            return false;
        }
    }
    String opStr = ' ' + op.toString() + ' ';
    boolean isFirst = true;
    for (Expression operand : operands) {
        if (!isFirst) {
            buffer.append(opStr);
        }
        isFirst = false;
        operand.accept(this);
    }
    return false;
}
Also used : StringLiteral(com.google.devtools.j2objc.ast.StringLiteral) CStringLiteral(com.google.devtools.j2objc.ast.CStringLiteral) LambdaExpression(com.google.devtools.j2objc.ast.LambdaExpression) PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) InstanceofExpression(com.google.devtools.j2objc.ast.InstanceofExpression) Expression(com.google.devtools.j2objc.ast.Expression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) NativeExpression(com.google.devtools.j2objc.ast.NativeExpression) CommaExpression(com.google.devtools.j2objc.ast.CommaExpression) ConditionalExpression(com.google.devtools.j2objc.ast.ConditionalExpression) CastExpression(com.google.devtools.j2objc.ast.CastExpression) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression)

Example 5 with StringLiteral

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

the class AnnotationRewriter method createDescriptionMethod.

private MethodDeclaration createDescriptionMethod(TypeElement type) {
    ExecutableElement descriptionElement = GeneratedExecutableElement.newMethodWithSelector("description", typeUtil.getJavaString().asType(), type);
    MethodDeclaration descriptionMethod = new MethodDeclaration(descriptionElement);
    descriptionMethod.setHasDeclaration(false);
    Block descriptionBody = new Block();
    descriptionMethod.setBody(descriptionBody);
    descriptionBody.addStatement(new ReturnStatement(new StringLiteral("@" + elementUtil.getBinaryName(type) + "()", typeUtil)));
    return descriptionMethod;
}
Also used : StringLiteral(com.google.devtools.j2objc.ast.StringLiteral) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ReturnStatement(com.google.devtools.j2objc.ast.ReturnStatement) Block(com.google.devtools.j2objc.ast.Block)

Aggregations

StringLiteral (com.google.devtools.j2objc.ast.StringLiteral)7 ExecutableElement (javax.lang.model.element.ExecutableElement)4 SimpleName (com.google.devtools.j2objc.ast.SimpleName)3 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)3 Assignment (com.google.devtools.j2objc.ast.Assignment)2 Block (com.google.devtools.j2objc.ast.Block)2 CommaExpression (com.google.devtools.j2objc.ast.CommaExpression)2 EnumConstantDeclaration (com.google.devtools.j2objc.ast.EnumConstantDeclaration)2 Expression (com.google.devtools.j2objc.ast.Expression)2 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)2 ForStatement (com.google.devtools.j2objc.ast.ForStatement)2 MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)2 NativeExpression (com.google.devtools.j2objc.ast.NativeExpression)2 NativeStatement (com.google.devtools.j2objc.ast.NativeStatement)2 NumberLiteral (com.google.devtools.j2objc.ast.NumberLiteral)2 ReturnStatement (com.google.devtools.j2objc.ast.ReturnStatement)2 Statement (com.google.devtools.j2objc.ast.Statement)2 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)2 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)2 VariableElement (javax.lang.model.element.VariableElement)2