Java Code Examples for jdk.nashorn.internal.ir.Expression#getSymbol()

The following examples show how to use jdk.nashorn.internal.ir.Expression#getSymbol() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Attr.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Node leaveReturnNode(final ReturnNode returnNode) {
    final Expression expr = returnNode.getExpression();
    final Type returnType;

    if (expr != null) {
        //we can't do parameter specialization if we return something that hasn't been typed yet
        final Symbol symbol = expr.getSymbol();
        if (expr.getType().isUnknown() && symbol.isParam()) {
            symbol.setType(Type.OBJECT);
        }

        returnType = widestReturnType(returnTypes.pop(), symbol.getSymbolType());
    } else {
        returnType = Type.OBJECT; //undefined
    }
    LOG.info("Returntype is now ", returnType);
    returnTypes.push(returnType);

    end(returnNode);

    return returnNode;
}
 
Example 2
Source File: Attr.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Node leaveReturnNode(final ReturnNode returnNode) {
    final Expression expr = returnNode.getExpression();
    final Type returnType;

    if (expr != null) {
        //we can't do parameter specialization if we return something that hasn't been typed yet
        final Symbol symbol = expr.getSymbol();
        if (expr.getType().isUnknown() && symbol.isParam()) {
            symbol.setType(Type.OBJECT);
        }

        returnType = widestReturnType(returnTypes.pop(), symbol.getSymbolType());
    } else {
        returnType = Type.OBJECT; //undefined
    }
    LOG.info("Returntype is now ", returnType);
    returnTypes.push(returnType);

    end(returnNode);

    return returnNode;
}
 
Example 3
Source File: Attr.java    From nashorn with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Node leaveReturnNode(final ReturnNode returnNode) {
    final Expression expr = returnNode.getExpression();
    final Type returnType;

    if (expr != null) {
        //we can't do parameter specialization if we return something that hasn't been typed yet
        final Symbol symbol = expr.getSymbol();
        if (expr.getType().isUnknown() && symbol.isParam()) {
            symbol.setType(Type.OBJECT);
        }

        returnType = Type.widest(returnTypes.pop(), symbol.getSymbolType());
    } else {
        returnType = Type.OBJECT; //undefined
    }
    LOG.info("Returntype is now ", returnType);
    returnTypes.push(returnType);

    end(returnNode);

    return returnNode;
}
 
Example 4
Source File: Attr.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private static void ensureTypeNotUnknown(final Expression node) {

        final Symbol symbol = node.getSymbol();

        LOG.info("Ensure type not unknown for: ", symbol);

        /*
         * Note that not just unknowns, but params need to be blown
         * up to objects, because we can have something like
         *
         * function f(a) {
         *    var b = ~a; //b and a are inferred to be int
         *    return b;
         * }
         *
         * In this case, it would be correct to say that "if you have
         * an int at the callsite, just pass it".
         *
         * However
         *
         * function f(a) {
         *    var b = ~a;      //b and a are inferred to be int
         *    return b == 17;  //b is still inferred to be int.
         * }
         *
         * can be called with f("17") and if we assume that b is an
         * int and don't blow it up to an object in the comparison, we
         * are screwed. I hate JavaScript.
         *
         * This check has to be done for any operation that might take
         * objects as parameters, for example +, but not *, which is known
         * to coerce types into doubles
         */
        if (node.getType().isUnknown() || (symbol.isParam() && !symbol.isSpecializedParam())) {
            newType(symbol, Type.OBJECT);
            symbol.setCanBeUndefined();
         }
    }
 
Example 5
Source File: RangeAnalyzer.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Check for a loop counter. This is currently quite conservative, in that it only handles
 * x <= counter and x < counter.
 *
 * @param node loop node to check
 * @return
 */
private static Symbol findLoopCounter(final LoopNode node) {
    final Expression test = node.getTest();

    if (test != null && test.isComparison()) {
        final BinaryNode binaryNode = (BinaryNode)test;
        final Expression lhs = binaryNode.lhs();
        final Expression rhs = binaryNode.rhs();

        //detect ident cmp int_literal
        if (lhs instanceof IdentNode && rhs instanceof LiteralNode && ((LiteralNode<?>)rhs).getType().isInteger()) {
            final Symbol    symbol = lhs.getSymbol();
            final int       margin = ((LiteralNode<?>)rhs).getInt32();
            final TokenType op     = test.tokenType();

            switch (op) {
            case LT:
            case LE:
                symbol.setRange(RANGE.join(symbol.getRange(), Range.createRange(op == TokenType.LT ? margin - 1 : margin)));
                return symbol;
            case GT:
            case GE:
                //setRange(lhs, Range.createRange(op == TokenType.GT ? margin + 1 : margin));
                //return symbol;
            default:
                break;
            }
        }
    }

    return null;
}
 
Example 6
Source File: Attr.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private static void ensureTypeNotUnknown(final Expression node) {

        final Symbol symbol = node.getSymbol();

        LOG.info("Ensure type not unknown for: ", symbol);

        /*
         * Note that not just unknowns, but params need to be blown
         * up to objects, because we can have something like
         *
         * function f(a) {
         *    var b = ~a; //b and a are inferred to be int
         *    return b;
         * }
         *
         * In this case, it would be correct to say that "if you have
         * an int at the callsite, just pass it".
         *
         * However
         *
         * function f(a) {
         *    var b = ~a;      //b and a are inferred to be int
         *    return b == 17;  //b is still inferred to be int.
         * }
         *
         * can be called with f("17") and if we assume that b is an
         * int and don't blow it up to an object in the comparison, we
         * are screwed. I hate JavaScript.
         *
         * This check has to be done for any operation that might take
         * objects as parameters, for example +, but not *, which is known
         * to coerce types into doubles
         */
        if (node.getType().isUnknown() || (symbol.isParam() && !symbol.isSpecializedParam())) {
            newType(symbol, Type.OBJECT);
            symbol.setCanBeUndefined();
         }
    }
 
Example 7
Source File: RangeAnalyzer.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Check for a loop counter. This is currently quite conservative, in that it only handles
 * x <= counter and x < counter.
 *
 * @param node loop node to check
 * @return
 */
private static Symbol findLoopCounter(final LoopNode node) {
    final Expression test = node.getTest();

    if (test != null && test.isComparison()) {
        final BinaryNode binaryNode = (BinaryNode)test;
        final Expression lhs = binaryNode.lhs();
        final Expression rhs = binaryNode.rhs();

        //detect ident cmp int_literal
        if (lhs instanceof IdentNode && rhs instanceof LiteralNode && ((LiteralNode<?>)rhs).getType().isInteger()) {
            final Symbol    symbol = lhs.getSymbol();
            final int       margin = ((LiteralNode<?>)rhs).getInt32();
            final TokenType op     = test.tokenType();

            switch (op) {
            case LT:
            case LE:
                symbol.setRange(RANGE.join(symbol.getRange(), Range.createRange(op == TokenType.LT ? margin - 1 : margin)));
                return symbol;
            case GT:
            case GE:
                //setRange(lhs, Range.createRange(op == TokenType.GT ? margin + 1 : margin));
                //return symbol;
            default:
                break;
            }
        }
    }

    return null;
}
 
Example 8
Source File: Attr.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
private static void ensureTypeNotUnknown(final Expression node) {

        final Symbol symbol = node.getSymbol();

        LOG.info("Ensure type not unknown for: ", symbol);

        /*
         * Note that not just unknowns, but params need to be blown
         * up to objects, because we can have something like
         *
         * function f(a) {
         *    var b = ~a; //b and a are inferred to be int
         *    return b;
         * }
         *
         * In this case, it would be correct to say that "if you have
         * an int at the callsite, just pass it".
         *
         * However
         *
         * function f(a) {
         *    var b = ~a;      //b and a are inferred to be int
         *    return b == 17;  //b is still inferred to be int.
         * }
         *
         * can be called with f("17") and if we assume that b is an
         * int and don't blow it up to an object in the comparison, we
         * are screwed. I hate JavaScript.
         *
         * This check has to be done for any operation that might take
         * objects as parameters, for example +, but not *, which is known
         * to coerce types into doubles
         */
        if (node.getType().isUnknown() || (symbol.isParam() && !symbol.isSpecializedParam())) {
            newType(symbol, Type.OBJECT);
            symbol.setCanBeUndefined();
         }
    }
 
Example 9
Source File: RangeAnalyzer.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Check for a loop counter. This is currently quite conservative, in that it only handles
 * x <= counter and x < counter.
 *
 * @param node loop node to check
 * @return
 */
private static Symbol findLoopCounter(final LoopNode node) {
    final Expression test = node.getTest();

    if (test != null && test.isComparison()) {
        final BinaryNode binaryNode = (BinaryNode)test;
        final Expression lhs = binaryNode.lhs();
        final Expression rhs = binaryNode.rhs();

        //detect ident cmp int_literal
        if (lhs instanceof IdentNode && rhs instanceof LiteralNode && ((LiteralNode<?>)rhs).getType().isInteger()) {
            final Symbol    symbol = lhs.getSymbol();
            final int       margin = ((LiteralNode<?>)rhs).getInt32();
            final TokenType op     = test.tokenType();

            switch (op) {
            case LT:
            case LE:
                symbol.setRange(RANGE.join(symbol.getRange(), Range.createRange(op == TokenType.LT ? margin - 1 : margin)));
                return symbol;
            case GT:
            case GE:
                //setRange(lhs, Range.createRange(op == TokenType.GT ? margin + 1 : margin));
                //return symbol;
            default:
                break;
            }
        }
    }

    return null;
}
 
Example 10
Source File: FinalizeTypes.java    From nashorn with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Whenever an expression like an addition or an assignment changes type, it
 * may be that case that {@link Attr} created a symbol for an intermediate
 * result of the expression, say for an addition. This also has to be updated
 * if the expression type changes.
 *
 * Assignments use their lhs as node symbol, and in this case we can't modify
 * it. Then {@link CodeGenerator.Store} needs to do an explicit conversion.
 * This is happens very rarely.
 *
 * @param node
 * @param to
 */
private Expression propagateType(final Expression node, final Type to) {
    Symbol symbol = node.getSymbol();
    if (symbol.isTemp() && symbol.getSymbolType() != to) {
        symbol = symbol.setTypeOverrideShared(to, temporarySymbols);
        LOG.info("Type override for temporary in '", node, "' => ", to);
    }
    return node.setSymbol(lc, symbol);
}
 
Example 11
Source File: Lower.java    From openjdk-8-source with GNU General Public License v2.0 2 votes vote down vote up
/**
 * An internal expression has a symbol that is tagged internal. Check if
 * this is such a node
 *
 * @param expression expression to check for internal symbol
 * @return true if internal, false otherwise
 */
private static boolean isInternalExpression(final Expression expression) {
    final Symbol symbol = expression.getSymbol();
    return symbol != null && symbol.isInternal();
}
 
Example 12
Source File: Lower.java    From openjdk-8 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * An internal expression has a symbol that is tagged internal. Check if
 * this is such a node
 *
 * @param expression expression to check for internal symbol
 * @return true if internal, false otherwise
 */
private static boolean isInternalExpression(final Expression expression) {
    final Symbol symbol = expression.getSymbol();
    return symbol != null && symbol.isInternal();
}
 
Example 13
Source File: Lower.java    From nashorn with GNU General Public License v2.0 2 votes vote down vote up
/**
 * An internal expression has a symbol that is tagged internal. Check if
 * this is such a node
 *
 * @param expression expression to check for internal symbol
 * @return true if internal, false otherwise
 */
private static boolean isInternalExpression(final Expression expression) {
    final Symbol symbol = expression.getSymbol();
    return symbol != null && symbol.isInternal();
}