Example usage for org.eclipse.jdt.internal.compiler.ast BinaryExpression BinaryExpression

List of usage examples for org.eclipse.jdt.internal.compiler.ast BinaryExpression BinaryExpression

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.ast BinaryExpression BinaryExpression.

Prototype

public BinaryExpression(Expression left, Expression right, int operator) 

Source Link

Usage

From source file:lombok.eclipse.handlers.ast.EclipseASTMaker.java

License:Open Source License

@Override
public ASTNode visitBinary(final lombok.ast.Binary node, final Void p) {
    final String operator = node.getOperator();
    final int opCode;
    if (BINARY_OPERATORS.containsKey(operator)) {
        opCode = BINARY_OPERATORS.get(operator);
    } else {//  w  w  w.j a v  a 2  s  .  c  om
        throw new IllegalStateException(String.format("Unknown binary operator '%s'", operator));
    }
    final BinaryExpression binaryExpression;
    if ("||".equals(operator)) {
        binaryExpression = new OR_OR_Expression(build(node.getLeft(), Expression.class),
                build(node.getRight(), Expression.class), opCode);
    } else if ("&&".equals(operator)) {
        binaryExpression = new AND_AND_Expression(build(node.getLeft(), Expression.class),
                build(node.getRight(), Expression.class), opCode);
    } else if (Is.oneOf(operator, "==", "!=")) {
        binaryExpression = new EqualExpression(build(node.getLeft(), Expression.class),
                build(node.getRight(), Expression.class), opCode);
    } else {
        binaryExpression = new BinaryExpression(build(node.getLeft(), Expression.class),
                build(node.getRight(), Expression.class), opCode);
    }
    setGeneratedByAndCopyPos(binaryExpression, source, posHintOf(node));
    return binaryExpression;
}

From source file:lombok.eclipse.handlers.HandleEqualsAndHashCode.java

License:Open Source License

public Expression createResultCalculation(ASTNode source, Expression ex) {
    /* result = result * PRIME + (ex); */
    int pS = source.sourceStart, pE = source.sourceEnd;
    long p = (long) pS << 32 | pE;
    SingleNameReference resultRef = new SingleNameReference(RESULT, p);
    setGeneratedBy(resultRef, source);/*from w  w w.  j  a  v a  2s.c  o m*/
    SingleNameReference primeRef = new SingleNameReference(PRIME, p);
    setGeneratedBy(primeRef, source);
    BinaryExpression multiplyByPrime = new BinaryExpression(resultRef, primeRef, OperatorIds.MULTIPLY);
    multiplyByPrime.sourceStart = pS;
    multiplyByPrime.sourceEnd = pE;
    setGeneratedBy(multiplyByPrime, source);
    BinaryExpression addItem = new BinaryExpression(multiplyByPrime, ex, OperatorIds.PLUS);
    addItem.sourceStart = pS;
    addItem.sourceEnd = pE;
    setGeneratedBy(addItem, source);
    resultRef = new SingleNameReference(RESULT, p);
    setGeneratedBy(resultRef, source);
    Assignment assignment = new Assignment(resultRef, addItem, pE);
    assignment.sourceStart = pS;
    assignment.sourceEnd = assignment.statementEnd = pE;
    setGeneratedBy(assignment, source);
    return assignment;
}

From source file:lombok.eclipse.handlers.HandleEqualsAndHashCode.java

License:Open Source License

/** Give 2 clones! */
public Expression longToIntForHashCode(Expression ref1, Expression ref2, ASTNode source) {
    int pS = source.sourceStart, pE = source.sourceEnd;
    /* (int)(ref >>> 32 ^ ref) */
    IntLiteral int32 = makeIntLiteral("32".toCharArray(), source);
    BinaryExpression higherBits = new BinaryExpression(ref1, int32, OperatorIds.UNSIGNED_RIGHT_SHIFT);
    setGeneratedBy(higherBits, source);/*from ww w.  j  av  a  2 s . co m*/
    BinaryExpression xorParts = new BinaryExpression(ref2, higherBits, OperatorIds.XOR);
    setGeneratedBy(xorParts, source);
    TypeReference intRef = TypeReference.baseTypeReference(TypeIds.T_int, 0);
    intRef.sourceStart = pS;
    intRef.sourceEnd = pE;
    setGeneratedBy(intRef, source);
    CastExpression expr = makeCastExpression(xorParts, intRef, source);
    expr.sourceStart = pS;
    expr.sourceEnd = pE;
    return expr;
}

From source file:lombok.eclipse.handlers.HandleToString.java

License:Open Source License

public static MethodDeclaration createToString(EclipseNode type, Collection<EclipseNode> fields,
        boolean includeFieldNames, boolean callSuper, ASTNode source, FieldAccess fieldAccess) {
    String typeName = getTypeName(type);
    char[] suffix = ")".toCharArray();
    String infixS = ", ";
    char[] infix = infixS.toCharArray();
    int pS = source.sourceStart, pE = source.sourceEnd;
    long p = (long) pS << 32 | pE;
    final int PLUS = OperatorIds.PLUS;

    char[] prefix;

    if (callSuper) {
        prefix = (typeName + "(super=").toCharArray();
    } else if (fields.isEmpty()) {
        prefix = (typeName + "()").toCharArray();
    } else if (includeFieldNames) {
        prefix = (typeName + "(" + new String(((FieldDeclaration) fields.iterator().next().get()).name) + "=")
                .toCharArray();//from  w  w w.j  a  v a  2  s  .  c o m
    } else {
        prefix = (typeName + "(").toCharArray();
    }

    boolean first = true;
    Expression current = new StringLiteral(prefix, pS, pE, 0);
    setGeneratedBy(current, source);

    if (callSuper) {
        MessageSend callToSuper = new MessageSend();
        callToSuper.sourceStart = pS;
        callToSuper.sourceEnd = pE;
        setGeneratedBy(callToSuper, source);
        callToSuper.receiver = new SuperReference(pS, pE);
        setGeneratedBy(callToSuper, source);
        callToSuper.selector = "toString".toCharArray();
        current = new BinaryExpression(current, callToSuper, PLUS);
        setGeneratedBy(current, source);
        first = false;
    }

    for (EclipseNode field : fields) {
        TypeReference fieldType = getFieldType(field, fieldAccess);
        Expression fieldAccessor = createFieldAccessor(field, fieldAccess, source);

        // The distinction between primitive and object will be useful if we ever add a 'hideNulls' option.
        boolean fieldBaseTypeIsPrimitive = BUILT_IN_TYPES.contains(new String(fieldType.getLastToken()));
        boolean fieldIsPrimitive = fieldType.dimensions() == 0 && fieldBaseTypeIsPrimitive;
        boolean fieldIsPrimitiveArray = fieldType.dimensions() == 1 && fieldBaseTypeIsPrimitive;
        boolean fieldIsObjectArray = fieldType.dimensions() > 0 && !fieldIsPrimitiveArray;
        @SuppressWarnings("unused")
        boolean fieldIsObject = !fieldIsPrimitive && !fieldIsPrimitiveArray && !fieldIsObjectArray;

        Expression ex;
        if (fieldIsPrimitiveArray || fieldIsObjectArray) {
            MessageSend arrayToString = new MessageSend();
            arrayToString.sourceStart = pS;
            arrayToString.sourceEnd = pE;
            arrayToString.receiver = generateQualifiedNameRef(source, TypeConstants.JAVA, TypeConstants.UTIL,
                    "Arrays".toCharArray());
            arrayToString.arguments = new Expression[] { fieldAccessor };
            setGeneratedBy(arrayToString.arguments[0], source);
            arrayToString.selector = (fieldIsObjectArray ? "deepToString" : "toString").toCharArray();
            ex = arrayToString;
        } else {
            ex = fieldAccessor;
        }
        setGeneratedBy(ex, source);

        if (first) {
            current = new BinaryExpression(current, ex, PLUS);
            current.sourceStart = pS;
            current.sourceEnd = pE;
            setGeneratedBy(current, source);
            first = false;
            continue;
        }

        StringLiteral fieldNameLiteral;
        if (includeFieldNames) {
            char[] namePlusEqualsSign = (infixS + field.getName() + "=").toCharArray();
            fieldNameLiteral = new StringLiteral(namePlusEqualsSign, pS, pE, 0);
        } else {
            fieldNameLiteral = new StringLiteral(infix, pS, pE, 0);
        }
        setGeneratedBy(fieldNameLiteral, source);
        current = new BinaryExpression(current, fieldNameLiteral, PLUS);
        setGeneratedBy(current, source);
        current = new BinaryExpression(current, ex, PLUS);
        setGeneratedBy(current, source);
    }
    if (!first) {
        StringLiteral suffixLiteral = new StringLiteral(suffix, pS, pE, 0);
        setGeneratedBy(suffixLiteral, source);
        current = new BinaryExpression(current, suffixLiteral, PLUS);
        setGeneratedBy(current, source);
    }

    ReturnStatement returnStatement = new ReturnStatement(current, pS, pE);
    setGeneratedBy(returnStatement, source);

    MethodDeclaration method = new MethodDeclaration(
            ((CompilationUnitDeclaration) type.top().get()).compilationResult);
    setGeneratedBy(method, source);
    method.modifiers = toEclipseModifier(AccessLevel.PUBLIC);
    method.returnType = new QualifiedTypeReference(TypeConstants.JAVA_LANG_STRING, new long[] { p, p, p });
    setGeneratedBy(method.returnType, source);
    method.annotations = new Annotation[] { makeMarkerAnnotation(TypeConstants.JAVA_LANG_OVERRIDE, source) };
    method.arguments = null;
    method.selector = "toString".toCharArray();
    method.thrownExceptions = null;
    method.typeParameters = null;
    method.bits |= Eclipse.ECLIPSE_DO_NOT_TOUCH_FLAG;
    method.bodyStart = method.declarationSourceStart = method.sourceStart = source.sourceStart;
    method.bodyEnd = method.declarationSourceEnd = method.sourceEnd = source.sourceEnd;
    method.statements = new Statement[] { returnStatement };
    return method;
}

From source file:lombok.eclipse.handlers.singulars.EclipseJavaUtilSingularizer.java

License:Open Source License

protected List<Statement> createJavaUtilSimpleCreationAndFillStatements(SingularData data,
        EclipseNode builderType, boolean mapMode, boolean defineVar, boolean addInitialCapacityArg,
        boolean nullGuard, String targetType) {
    char[] varName = mapMode ? (new String(data.getPluralName()) + "$key").toCharArray() : data.getPluralName();

    Statement createStat;//from   www . j  a va2  s . co m
    {
        // pluralName = new java.util.TargetType(initialCap);
        Expression[] constructorArgs = null;
        if (addInitialCapacityArg) {
            // this.varName.size() < MAX_POWER_OF_2 ? 1 + this.varName.size() + (this.varName.size() - 3) / 3 : Integer.MAX_VALUE;
            // lessThanCutOff = this.varName.size() < MAX_POWER_OF_2
            Expression lessThanCutoff = new BinaryExpression(getSize(builderType, varName, nullGuard),
                    makeIntLiteral("0x40000000".toCharArray(), null), OperatorIds.LESS);
            FieldReference integerMaxValue = new FieldReference("MAX_VALUE".toCharArray(), 0L);
            integerMaxValue.receiver = new QualifiedNameReference(TypeConstants.JAVA_LANG_INTEGER, NULL_POSS, 0,
                    0);
            Expression sizeFormulaLeft = new BinaryExpression(makeIntLiteral(new char[] { '1' }, null),
                    getSize(builderType, varName, nullGuard), OperatorIds.PLUS);
            Expression sizeFormulaRightLeft = new BinaryExpression(getSize(builderType, varName, nullGuard),
                    makeIntLiteral(new char[] { '3' }, null), OperatorIds.MINUS);
            Expression sizeFormulaRight = new BinaryExpression(sizeFormulaRightLeft,
                    makeIntLiteral(new char[] { '3' }, null), OperatorIds.DIVIDE);
            Expression sizeFormula = new BinaryExpression(sizeFormulaLeft, sizeFormulaRight, OperatorIds.PLUS);
            Expression cond = new ConditionalExpression(lessThanCutoff, sizeFormula, integerMaxValue);
            constructorArgs = new Expression[] { cond };
        }

        TypeReference targetTypeRef = new QualifiedTypeReference(
                new char[][] { TypeConstants.JAVA, TypeConstants.UTIL, targetType.toCharArray() }, NULL_POSS);
        targetTypeRef = addTypeArgs(mapMode ? 2 : 1, false, builderType, targetTypeRef, data.getTypeArgs());
        AllocationExpression constructorCall = new AllocationExpression();
        constructorCall.type = targetTypeRef;
        constructorCall.arguments = constructorArgs;

        if (defineVar) {
            TypeReference localShadowerType = new QualifiedTypeReference(fromQualifiedName(data.getTargetFqn()),
                    NULL_POSS);
            localShadowerType = addTypeArgs(mapMode ? 2 : 1, false, builderType, localShadowerType,
                    data.getTypeArgs());
            LocalDeclaration localShadowerDecl = new LocalDeclaration(data.getPluralName(), 0, 0);
            localShadowerDecl.type = localShadowerType;
            localShadowerDecl.initialization = constructorCall;
            createStat = localShadowerDecl;
        } else {
            createStat = new Assignment(new SingleNameReference(data.getPluralName(), 0L), constructorCall, 0);
        }
    }

    Statement fillStat;
    {
        if (mapMode) {
            // for (int $i = 0; $i < this.pluralname$key.size(); i++) pluralname.put(this.pluralname$key.get($i), this.pluralname$value.get($i));
            char[] iVar = new char[] { '$', 'i' };
            MessageSend pluralnameDotPut = new MessageSend();
            pluralnameDotPut.selector = new char[] { 'p', 'u', 't' };
            pluralnameDotPut.receiver = new SingleNameReference(data.getPluralName(), 0L);
            FieldReference thisDotKey = new FieldReference(varName, 0L);
            thisDotKey.receiver = new ThisReference(0, 0);
            FieldReference thisDotValue = new FieldReference(
                    (new String(data.getPluralName()) + "$value").toCharArray(), 0L);
            thisDotValue.receiver = new ThisReference(0, 0);
            MessageSend keyArg = new MessageSend();
            keyArg.receiver = thisDotKey;
            keyArg.arguments = new Expression[] { new SingleNameReference(iVar, 0L) };
            keyArg.selector = new char[] { 'g', 'e', 't' };
            MessageSend valueArg = new MessageSend();
            valueArg.receiver = thisDotValue;
            valueArg.arguments = new Expression[] { new SingleNameReference(iVar, 0L) };
            valueArg.selector = new char[] { 'g', 'e', 't' };
            pluralnameDotPut.arguments = new Expression[] { keyArg, valueArg };

            LocalDeclaration forInit = new LocalDeclaration(iVar, 0, 0);
            forInit.type = TypeReference.baseTypeReference(TypeIds.T_int, 0);
            forInit.initialization = makeIntLiteral(new char[] { '0' }, null);
            Expression checkExpr = new BinaryExpression(new SingleNameReference(iVar, 0L),
                    getSize(builderType, varName, nullGuard), OperatorIds.LESS);
            Expression incrementExpr = new PostfixExpression(new SingleNameReference(iVar, 0L), IntLiteral.One,
                    OperatorIds.PLUS, 0);
            fillStat = new ForStatement(new Statement[] { forInit }, checkExpr,
                    new Statement[] { incrementExpr }, pluralnameDotPut, true, 0, 0);
        } else {
            // pluralname.addAll(this.pluralname);
            MessageSend pluralnameDotAddAll = new MessageSend();
            pluralnameDotAddAll.selector = new char[] { 'a', 'd', 'd', 'A', 'l', 'l' };
            pluralnameDotAddAll.receiver = new SingleNameReference(data.getPluralName(), 0L);
            FieldReference thisDotPluralname = new FieldReference(varName, 0L);
            thisDotPluralname.receiver = new ThisReference(0, 0);
            pluralnameDotAddAll.arguments = new Expression[] { thisDotPluralname };
            fillStat = pluralnameDotAddAll;
        }

        if (nullGuard) {
            FieldReference thisDotField = new FieldReference(varName, 0L);
            thisDotField.receiver = new ThisReference(0, 0);
            Expression cond = new EqualExpression(thisDotField, new NullLiteral(0, 0), OperatorIds.NOT_EQUAL);
            fillStat = new IfStatement(cond, fillStat, 0, 0);
        }
    }

    Statement unmodifiableStat;
    {
        // pluralname = Collections.unmodifiableInterfaceType(pluralname);
        Expression arg = new SingleNameReference(data.getPluralName(), 0L);
        MessageSend invoke = new MessageSend();
        invoke.arguments = new Expression[] { arg };
        invoke.selector = ("unmodifiable" + data.getTargetSimpleType()).toCharArray();
        invoke.receiver = new QualifiedNameReference(JAVA_UTIL_COLLECTIONS, NULL_POSS, 0, 0);
        unmodifiableStat = new Assignment(new SingleNameReference(data.getPluralName(), 0L), invoke, 0);
    }

    return Arrays.asList(createStat, fillStat, unmodifiableStat);
}

From source file:org.eclipse.jdt.internal.compiler.parser.Parser.java

License:Open Source License

protected void consumeBinaryExpression(int op) {
    // MultiplicativeExpression ::= MultiplicativeExpression '*' UnaryExpression
    // MultiplicativeExpression ::= MultiplicativeExpression '/' UnaryExpression
    // MultiplicativeExpression ::= MultiplicativeExpression '%' UnaryExpression
    // AdditiveExpression ::= AdditiveExpression '+' MultiplicativeExpression
    // AdditiveExpression ::= AdditiveExpression '-' MultiplicativeExpression
    // ShiftExpression ::= ShiftExpression '<<'  AdditiveExpression
    // ShiftExpression ::= ShiftExpression '>>'  AdditiveExpression
    // ShiftExpression ::= ShiftExpression '>>>' AdditiveExpression
    // RelationalExpression ::= RelationalExpression '<'  ShiftExpression
    // RelationalExpression ::= RelationalExpression '>'  ShiftExpression
    // RelationalExpression ::= RelationalExpression '<=' ShiftExpression
    // RelationalExpression ::= RelationalExpression '>=' ShiftExpression
    // AndExpression ::= AndExpression '&' EqualityExpression
    // ExclusiveOrExpression ::= ExclusiveOrExpression '^' AndExpression
    // InclusiveOrExpression ::= InclusiveOrExpression '|' ExclusiveOrExpression
    // ConditionalAndExpression ::= ConditionalAndExpression '&&' InclusiveOrExpression
    // ConditionalOrExpression ::= ConditionalOrExpression '||' ConditionalAndExpression

    //optimize the push/pop

    this.expressionPtr--;
    this.expressionLengthPtr--;
    Expression expr1 = this.expressionStack[this.expressionPtr];
    Expression expr2 = this.expressionStack[this.expressionPtr + 1];
    switch (op) {
    case OR_OR:// www.jav a  2 s  . c o m
        this.expressionStack[this.expressionPtr] = new OR_OR_Expression(expr1, expr2, op);
        break;
    case AND_AND:
        this.expressionStack[this.expressionPtr] = new AND_AND_Expression(expr1, expr2, op);
        break;
    case PLUS:
        // look for "string1" + "string2"
        if (this.optimizeStringLiterals) {
            if (expr1 instanceof StringLiteral) {
                if (((expr1.bits & ASTNode.ParenthesizedMASK) >> ASTNode.ParenthesizedSHIFT) == 0) {
                    if (expr2 instanceof CharLiteral) { // string+char
                        this.expressionStack[this.expressionPtr] = ((StringLiteral) expr1)
                                .extendWith((CharLiteral) expr2);
                    } else if (expr2 instanceof StringLiteral) { //string+string
                        this.expressionStack[this.expressionPtr] = ((StringLiteral) expr1)
                                .extendWith((StringLiteral) expr2);
                    } else {
                        this.expressionStack[this.expressionPtr] = new BinaryExpression(expr1, expr2, PLUS);
                    }
                } else {
                    this.expressionStack[this.expressionPtr] = new BinaryExpression(expr1, expr2, PLUS);
                }
            } else if (expr1 instanceof CombinedBinaryExpression) {
                CombinedBinaryExpression cursor;
                // left branch is comprised of PLUS BEs
                // cursor is shifted upwards, while needed BEs are added
                // on demand; past the arityMax-th
                // consecutive BE, a CBE is inserted that holds a
                // full-fledged references table
                if ((cursor = (CombinedBinaryExpression) expr1).arity < cursor.arityMax) {
                    cursor.left = new BinaryExpression(cursor);
                    cursor.arity++;
                } else {
                    cursor.left = new CombinedBinaryExpression(cursor);
                    cursor.arity = 0;
                    cursor.tuneArityMax();
                }
                cursor.right = expr2;
                cursor.sourceEnd = expr2.sourceEnd;
                this.expressionStack[this.expressionPtr] = cursor;
                // BE_INSTRUMENTATION: neutralized in the released code
                //               cursor.depthTracker = ((BinaryExpression)cursor.left).
                //                  depthTracker + 1;
            } else if (expr1 instanceof BinaryExpression &&
            // single out the a + b case, which is a BE
            // instead of a CBE (slightly more than a half of
            // strings concatenation are one-deep binary
            // expressions)
                    ((expr1.bits & ASTNode.OperatorMASK) >> ASTNode.OperatorSHIFT) == OperatorIds.PLUS) {
                this.expressionStack[this.expressionPtr] = new CombinedBinaryExpression(expr1, expr2, PLUS, 1);
            } else {
                this.expressionStack[this.expressionPtr] = new BinaryExpression(expr1, expr2, PLUS);
            }
        } else if (expr1 instanceof StringLiteral) {
            if (expr2 instanceof StringLiteral
                    && ((expr1.bits & ASTNode.ParenthesizedMASK) >> ASTNode.ParenthesizedSHIFT) == 0) {
                // string + string
                this.expressionStack[this.expressionPtr] = ((StringLiteral) expr1)
                        .extendsWith((StringLiteral) expr2);
            } else {
                // single out the a + b case
                this.expressionStack[this.expressionPtr] = new BinaryExpression(expr1, expr2, PLUS);
            }
        } else if (expr1 instanceof CombinedBinaryExpression) {
            CombinedBinaryExpression cursor;
            // shift cursor; create BE/CBE as needed
            if ((cursor = (CombinedBinaryExpression) expr1).arity < cursor.arityMax) {
                cursor.left = new BinaryExpression(cursor);
                // clear the bits on cursor
                cursor.bits &= ~ASTNode.ParenthesizedMASK;
                cursor.arity++;
            } else {
                cursor.left = new CombinedBinaryExpression(cursor);
                // clear the bits on cursor
                cursor.bits &= ~ASTNode.ParenthesizedMASK;
                cursor.arity = 0;
                cursor.tuneArityMax();
            }
            cursor.right = expr2;
            cursor.sourceEnd = expr2.sourceEnd;
            // BE_INSTRUMENTATION: neutralized in the released code
            //               cursor.depthTracker = ((BinaryExpression)cursor.left).
            //                  depthTracker + 1;
            this.expressionStack[this.expressionPtr] = cursor;
        } else if (expr1 instanceof BinaryExpression
                && ((expr1.bits & ASTNode.OperatorMASK) >> ASTNode.OperatorSHIFT) == OperatorIds.PLUS) {
            // single out the a + b case
            this.expressionStack[this.expressionPtr] = new CombinedBinaryExpression(expr1, expr2, PLUS, 1);
        } else {
            this.expressionStack[this.expressionPtr] = new BinaryExpression(expr1, expr2, PLUS);
        }
        break;
    case LESS:
    case MULTIPLY:
        this.intPtr--; // star end position or starting position of angle bracket
        this.expressionStack[this.expressionPtr] = new BinaryExpression(expr1, expr2, op);
        break;
    default:
        this.expressionStack[this.expressionPtr] = new BinaryExpression(expr1, expr2, op);
    }
}

From source file:org.eclipse.jdt.internal.compiler.parser.Parser.java

License:Open Source License

/**
 * @param op binary operator//from www.  j av a 2  s .com
 */
protected void consumeBinaryExpressionWithName(int op) {
    pushOnExpressionStack(getUnspecifiedReferenceOptimized());
    this.expressionPtr--;
    this.expressionLengthPtr--;
    /*
    if (op == OR_OR) {
       this.expressionStack[this.expressionPtr] =
          new OR_OR_Expression(
    this.expressionStack[this.expressionPtr + 1],
    this.expressionStack[this.expressionPtr],
    op);
    } else {
       if (op == AND_AND) {
          this.expressionStack[this.expressionPtr] =
    new AND_AND_Expression(
       this.expressionStack[this.expressionPtr + 1],
       this.expressionStack[this.expressionPtr],
       op);
       } else {
          // look for "string1" + "string2"
          if ((op == PLUS) && this.optimizeStringLiterals) {
    Expression expr1, expr2;
    expr1 = this.expressionStack[this.expressionPtr + 1];
    expr2 = this.expressionStack[this.expressionPtr];
    if (expr1 instanceof StringLiteral) {
       if (expr2 instanceof CharLiteral) { // string+char
          this.expressionStack[this.expressionPtr] =
             ((StringLiteral) expr1).extendWith((CharLiteral) expr2);
       } else if (expr2 instanceof StringLiteral) { //string+string
          this.expressionStack[this.expressionPtr] =
             ((StringLiteral) expr1).extendWith((StringLiteral) expr2);
       } else {
          this.expressionStack[this.expressionPtr] = new BinaryExpression(expr1, expr2, PLUS);
       }
    } else {
       this.expressionStack[this.expressionPtr] = new BinaryExpression(expr1, expr2, PLUS);
    }
          } else {
    this.expressionStack[this.expressionPtr] =
       new BinaryExpression(
          this.expressionStack[this.expressionPtr + 1],
          this.expressionStack[this.expressionPtr],
          op);
          }
       }
    }
    */
    Expression expr1 = this.expressionStack[this.expressionPtr + 1];
    Expression expr2 = this.expressionStack[this.expressionPtr];
    // Note: we do not attempt to promote BinaryExpression-s to
    //       IndexedBinaryExpression-s here since expr1 always holds a name
    switch (op) {
    case OR_OR:
        this.expressionStack[this.expressionPtr] = new OR_OR_Expression(expr1, expr2, op);
        break;
    case AND_AND:
        this.expressionStack[this.expressionPtr] = new AND_AND_Expression(expr1, expr2, op);
        break;
    case PLUS:
        // look for "string1" + "string2"
        if (this.optimizeStringLiterals) {
            if (expr1 instanceof StringLiteral
                    && ((expr1.bits & ASTNode.ParenthesizedMASK) >> ASTNode.ParenthesizedSHIFT) == 0) {
                if (expr2 instanceof CharLiteral) { // string+char
                    this.expressionStack[this.expressionPtr] = ((StringLiteral) expr1)
                            .extendWith((CharLiteral) expr2);
                } else if (expr2 instanceof StringLiteral) { //string+string
                    this.expressionStack[this.expressionPtr] = ((StringLiteral) expr1)
                            .extendWith((StringLiteral) expr2);
                } else {
                    this.expressionStack[this.expressionPtr] = new BinaryExpression(expr1, expr2, PLUS);
                }
            } else {
                this.expressionStack[this.expressionPtr] = new BinaryExpression(expr1, expr2, PLUS);
            }
        } else if (expr1 instanceof StringLiteral) {
            if (expr2 instanceof StringLiteral
                    && ((expr1.bits & ASTNode.ParenthesizedMASK) >> ASTNode.ParenthesizedSHIFT) == 0) {
                // string + string
                this.expressionStack[this.expressionPtr] = ((StringLiteral) expr1)
                        .extendsWith((StringLiteral) expr2);
            } else {
                this.expressionStack[this.expressionPtr] = new BinaryExpression(expr1, expr2, op);
            }
        } else {
            this.expressionStack[this.expressionPtr] = new BinaryExpression(expr1, expr2, op);
        }
        break;
    case LESS:
    case MULTIPLY:
        this.intPtr--; // star end position or starting position of angle bracket
        this.expressionStack[this.expressionPtr] = new BinaryExpression(expr1, expr2, op);
        break;
    default:
        this.expressionStack[this.expressionPtr] = new BinaryExpression(expr1, expr2, op);
    }
}

From source file:org.eclipse.objectteams.otdt.internal.core.compiler.lifting.ArrayTranslations.java

License:Open Source License

/**
 *    for(int i0=0;i0 < role.length;i0++)
 *   {/*from   ww w. j  av a  2 s.  com*/
 *      result = new Base[role.length][][];
 *      for(...){
 *         ...
 *            for(int i2=0;i2 < role[i0][i1].length;i2++){
 *                 result[i0][i1][i2] = translate(role[i0][i1][i2]);
 *             }
 *          ...
 *       }
 *   }
 * @param currentDimension counter for recursion only. invoke initially with 0.
 * @param arrayDimensions maximum of dimensions of the array
 * @return the new created ForStatement with all subcycles
 */
private ForStatement generateForStatement(int currentDimension, int arrayDimensions, AstGenerator gen) {

    Statement[] init = new Statement[1];
    char[] name = generateArrayIndexName(currentDimension);

    LocalDeclaration initializer = new LocalDeclaration(name, 0, 0);
    initializer.initialization = IntLiteral.buildIntLiteral("0".toCharArray(), 0, 0); //$NON-NLS-1$
    initializer.type = new SingleTypeReference(TypeConstants.INT, 0);
    init[0] = initializer;

    SingleNameReference condLeft = new SingleNameReference(name, 0);

    FieldReference condRight = new FieldReference(IOTConstants.LENGTH, 0);
    SingleNameReference roleNameReference = gen.singleNameReference(ROLE_ARRAY_ARG);
    decapsulationInput(roleNameReference);

    Expression lastArrayReference = roleNameReference;

    for (int idx = 0; idx < currentDimension; idx++) {
        SingleNameReference pos = new SingleNameReference(generateArrayIndexName(idx), 0);
        ArrayReference nextArray = new ArrayReference(lastArrayReference, pos);
        lastArrayReference = nextArray;
    }
    condRight.receiver = lastArrayReference;

    Expression cond = new BinaryExpression(condLeft, condRight, OperatorIds.LESS);
    Statement[] inc = new Statement[1];

    inc[0] = new PostfixExpression(new SingleNameReference(name, 0), IntLiteral.One, OperatorIds.PLUS, 0);

    Block action = new Block(0);

    // result = new Base[role.length][][];
    Assignment arrayInstantiation = generateArrayInstantiation(currentDimension + 1, arrayDimensions, gen);

    // if(r1 == null)continue;
    IfStatement ifStatement = generateIfStatement(currentDimension + 1, arrayDimensions);

    if (currentDimension < arrayDimensions - 1) {
        ForStatement innerForStatement = generateForStatement(currentDimension + 1, arrayDimensions, gen);
        action.statements = new Statement[3];
        action.statements[0] = ifStatement;
        action.statements[1] = arrayInstantiation;
        action.statements[2] = innerForStatement;
    } else {
        action.statements = new Statement[2];
        action.statements[0] = ifStatement;
        action.statements[1] = arrayInstantiation;
    }

    // for(;i0 < role.length;i0++)
    ForStatement outerForStatement = new ForStatement(init, cond, inc, action, true, 0, 0);

    return outerForStatement;
}

From source file:org.eclipse.objectteams.otdt.internal.core.compiler.statemachine.transformer.ReflectionGenerator.java

License:Open Source License

private static Statement createThrowNoSuchRole(AstGenerator gen) {
    /*/*from  w  w  w.ja  v  a  2 s.  c  o  m*/
     * throw new IllegalArgumentException("No such role type in this team: "+class_arg.getName());
     */
    return gen.throwStatement(gen.allocation(gen.qualifiedTypeReference(ILLEGAL_ARGUMENT_EXCEPTION),
            new Expression[] { new BinaryExpression(
                    gen.stringLiteral("No such bound role type in this team: ".toCharArray()), //$NON-NLS-1$
                    gen.messageSend(gen.singleNameReference(CLASS_ARG), GET_NAME, new Expression[0]),
                    OperatorIds.PLUS) }));
}

From source file:org.eclipse.objectteams.otdt.internal.core.compiler.util.AstGenerator.java

License:Open Source License

/** print a message plus the value of an expression. */
public MessageSend println(String msg, Expression expr) {
    return messageSend(new QualifiedNameReference(new char[][] { "System".toCharArray(), "out".toCharArray() }, //$NON-NLS-1$ //$NON-NLS-2$
            new long[] { this.pos, this.pos }, this.sourceStart, this.sourceEnd), "println".toCharArray(), //$NON-NLS-1$
            new Expression[] {
                    new BinaryExpression(stringLiteral(msg.toCharArray()), expr, OperatorIds.PLUS) });
}