Example usage for org.eclipse.jdt.internal.compiler.ast ASTNode ParenthesizedSHIFT

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

Introduction

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

Prototype

int ParenthesizedSHIFT

To view the source code for org.eclipse.jdt.internal.compiler.ast ASTNode ParenthesizedSHIFT.

Click Source Link

Usage

From source file:com.android.tools.lint.psi.EcjPsiBuilder.java

License:Apache License

@NonNull
static TextRange toRange(@NonNull ASTNode node) {
    int sourceStart = node.sourceStart;
    int endOffset = node.sourceEnd + 1;

    // The source offsets include parentheses, but we don't currently create
    // PsiParenthesizedExpression's. We do however need to fix the source
    // offsets such that they point to the correct region excluding the parenthesis
    // range./*from  w w  w.j  a  v  a 2s.  com*/
    int parens = (node.bits & ASTNode.ParenthesizedMASK) >> ASTNode.ParenthesizedSHIFT;
    if (parens > 0) {
        sourceStart += parens;
        endOffset -= parens;
        if (endOffset < sourceStart) {
            // This shouldn't happen, but has for some source files discovered by
            // the unit tests: Give up on parenthesis adjustment if it leads to impossible
            // offsets
            sourceStart = node.sourceStart;
            endOffset = node.sourceEnd + 1;
        }
    }

    return new TextRange(sourceStart, endOffset);
}

From source file:com.android.tools.lint.psi.EcjPsiBuilder.java

License:Apache License

@NonNull
private EcjPsiJavaCodeReferenceElement toTypeReference(@NonNull EcjPsiSourceElement parent,
        @NonNull TypeReference reference) {
    char[][] tokens = reference.getTypeName();
    EcjPsiJavaCodeReferenceElement element = null;
    if (tokens.length == 1) {
        element = new EcjPsiJavaCodeReferenceElement(mManager, reference);
        parent.adoptChild(element);//from  w  w  w  . j  av  a 2s .c  om
        String referenceName = new String(tokens[tokens.length - 1]);
        int start = reference.sourceStart;
        start += (reference.bits & ASTNode.ParenthesizedMASK) >> ASTNode.ParenthesizedSHIFT;
        int end = start + referenceName.length();
        element.setNameElement(toIdentifier(element, referenceName, new TextRange(start, end)));
    } else {
        // ECJ doesn't have a hierarchy of AST nodes, but PSI does
        int startOffset = reference.sourceStart;
        int endOffset = startOffset + tokens[0].length;
        EcjPsiJavaCodeReferenceElement prev = new EcjPsiJavaCodeReferenceElement(mManager, reference);
        prev.setNameElement(toIdentifier(prev, tokens[0], toRange(startOffset, endOffset)));
        prev.setRange(startOffset, endOffset);

        for (int i = 1; i < tokens.length; i++) {
            element = new EcjPsiJavaCodeReferenceElement(mManager, reference);
            element.setQualifier(prev);
            char[] name = tokens[i];
            endOffset += name.length + 1; // +1: dot
            element.adoptChild(prev);
            element.setNameElement(toIdentifier(element, name, toRange(endOffset - name.length, endOffset)));
            element.setRange(startOffset, endOffset);
            prev = element;
        }

        assert element != null;
        if (reference instanceof ParameterizedSingleTypeReference) {
            ParameterizedSingleTypeReference typeReference = (ParameterizedSingleTypeReference) reference;
            element.setParameterList(toTypeParameterList(element, typeReference.typeArguments));
        }

        parent.adoptChild(element);
    }

    if (reference instanceof ParameterizedSingleTypeReference) {
        ParameterizedSingleTypeReference typeReference = (ParameterizedSingleTypeReference) reference;
        if (typeReference.typeArguments.length > 0) {
            EcjPsiReferenceParameterList parameterList = toTypeParameterList(element,
                    typeReference.typeArguments);
            element.setParameterList(parameterList);
            // Widen offset range: ECJ doesn't seem to include bounds of type parameters
            int endOffset = parameterList.getTextRange().getEndOffset();
            element.setRange(element.getTextRange().getStartOffset(), endOffset);
            //noinspection ConstantConditions
            while (parent != null) {
                if (parent.getTextRange().getEndOffset() < endOffset) {
                    parent.setRange(parent.getTextRange().getStartOffset(), endOffset);
                } else {
                    break;
                }
                parent = parent.getParent();
            }
        }
    }

    return element;
}

From source file:lombok.ast.ecj.EcjTreeConverter.java

License:Open Source License

private void set(ASTNode node, Node value) {
    if (result != null)
        throw new IllegalStateException("result is already set");

    if (value instanceof lombok.ast.Expression && hasFlag(FlagKey.AS_STATEMENT)) {
        lombok.ast.ExpressionStatement stat = new lombok.ast.ExpressionStatement();
        stat.astExpression((lombok.ast.Expression) value);
        int start = node.sourceStart;
        int end = node.sourceEnd;
        try {//  ww  w .  j  a v a  2  s.  c om
            end = (Integer) node.getClass().getField("statementEnd").get(node);
        } catch (Exception e) {
            // Not all these classes may have a statementEnd.
        }

        set(node, stat.setPosition(toPosition(start, end)));
        return;
    }

    if (value instanceof lombok.ast.Expression) {
        int parenCount = (node.bits & ASTNode.ParenthesizedMASK) >> ASTNode.ParenthesizedSHIFT;
        for (int i = 0; i < parenCount; i++) {
            ((lombok.ast.Expression) value).astParensPositions().add(value.getPosition());
        }
    }

    List<Node> result = Lists.newArrayList();
    if (value != null)
        result.add(value);
    this.result = result;
    if (value != null)
        value.setNativeNode(node);
}

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://from   w w  w .j ava2s  . 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   ww w  .  ja va  2 s.  co  m*/
 */
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.jdt.internal.compiler.parser.Parser.java

License:Open Source License

protected void consumePrimaryNoNewArray() {
    // PrimaryNoNewArray ::=  PushLPAREN Expression PushRPAREN
    final Expression parenthesizedExpression = this.expressionStack[this.expressionPtr];
    updateSourcePosition(parenthesizedExpression);
    int numberOfParenthesis = (parenthesizedExpression.bits
            & ASTNode.ParenthesizedMASK) >> ASTNode.ParenthesizedSHIFT;
    parenthesizedExpression.bits &= ~ASTNode.ParenthesizedMASK;
    parenthesizedExpression.bits |= (numberOfParenthesis + 1) << ASTNode.ParenthesizedSHIFT;
}

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

License:Open Source License

protected void consumePrimaryNoNewArrayWithName() {
    // PrimaryNoNewArray ::=  PushLPAREN Expression PushRPAREN
    pushOnExpressionStack(getUnspecifiedReferenceOptimized());
    final Expression parenthesizedExpression = this.expressionStack[this.expressionPtr];
    updateSourcePosition(parenthesizedExpression);
    int numberOfParenthesis = (parenthesizedExpression.bits
            & ASTNode.ParenthesizedMASK) >> ASTNode.ParenthesizedSHIFT;
    parenthesizedExpression.bits &= ~ASTNode.ParenthesizedMASK;
    parenthesizedExpression.bits |= (numberOfParenthesis + 1) << ASTNode.ParenthesizedSHIFT;
}