/*
 * Copyright (c) 2010, 2019, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * The Universal Permissive License (UPL), Version 1.0
 *
 * Subject to the condition set forth below, permission is hereby granted to any
 * person obtaining a copy of this software, associated documentation and/or
 * data (collectively the "Software"), free of charge and under any and all
 * copyright rights in the Software, and any and all patent rights owned or
 * freely licensable by each licensor hereunder covering either (i) the
 * unmodified Software as contributed to or provided by such licensor, or (ii)
 * the Larger Works (as defined below), to deal in both
 *
 * (a) the Software, and
 *
 * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
 * one is included with the Software each a "Larger Work" to which the Software
 * is contributed by such licensors),
 *
 * without restriction, including without limitation the rights to copy, create
 * derivative works of, display, perform, and distribute the Software and make,
 * use, sell, offer for sale, import, export, have made, and have sold the
 * Software and the Larger Work(s), and to sublicense the foregoing rights on
 * either these or other terms.
 *
 * This license is subject to the following condition:
 *
 * The above copyright notice and either this complete permission notice or at a
 * minimum a reference to the UPL must be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.oracle.js.parser.ir;

import com.oracle.js.parser.TokenType;
import com.oracle.js.parser.ir.visitor.NodeVisitor;
import com.oracle.js.parser.ir.visitor.TranslatorNodeVisitor;

BinaryNode nodes represent two operand operations.
/** * BinaryNode nodes represent two operand operations. */
public final class BinaryNode extends Expression implements Assignment<Expression> {
Left hand side argument.
/** Left hand side argument. */
private final Expression lhs;
Right hand side argument.
/** Right hand side argument. */
private final Expression rhs;
Constructor
Params:
  • token – token
  • lhs – left hand side
  • rhs – right hand side
/** * Constructor * * @param token token * @param lhs left hand side * @param rhs right hand side */
public BinaryNode(final long token, final Expression lhs, final Expression rhs) { super(token, Math.min(lhs.getStart(), rhs.getStart()), Math.max(rhs.getFinish(), lhs.getFinish())); assert !isLogical() || lhs instanceof JoinPredecessorExpression; this.lhs = lhs; this.rhs = rhs; } private BinaryNode(final BinaryNode binaryNode, final Expression lhs, final Expression rhs) { super(binaryNode); this.lhs = lhs; this.rhs = rhs; }
Returns true if the node is a comparison operation (either equality, inequality, or relational).
Returns:true if the node is a comparison operation.
/** * Returns true if the node is a comparison operation (either equality, inequality, or * relational). * * @return true if the node is a comparison operation. */
public boolean isComparison() { switch (tokenType()) { case EQ: case EQ_STRICT: case NE: case NE_STRICT: case LE: case LT: case GE: case GT: return true; default: return false; } }
Returns true if the node is a relational operation (less than (or equals), greater than (or equals)).
Returns:true if the node is a relational operation.
/** * Returns true if the node is a relational operation (less than (or equals), greater than (or * equals)). * * @return true if the node is a relational operation. */
public boolean isRelational() { switch (tokenType()) { case LT: case GT: case LE: case GE: return true; default: return false; } }
Returns true if the node is a logical operation.
Returns:true if the node is a logical operation.
/** * Returns true if the node is a logical operation. * * @return true if the node is a logical operation. */
public boolean isLogical() { return isLogical(tokenType()); }
Returns true if the token type represents a logical operation.
Params:
  • tokenType – the token type
Returns:true if the token type represents a logical operation.
/** * Returns true if the token type represents a logical operation. * * @param tokenType the token type * @return true if the token type represents a logical operation. */
public static boolean isLogical(final TokenType tokenType) { switch (tokenType) { case AND: case OR: case NULLISHCOALESC: return true; default: return false; } }
Check if this node is an assignment
Returns:true if this node assigns a value
/** * Check if this node is an assignment * * @return true if this node assigns a value */
@Override public boolean isAssignment() { return tokenType().isAssignment(); } @Override public boolean isSelfModifying() { return isAssignment() && !isTokenType(TokenType.ASSIGN); } @Override public Expression getAssignmentDest() { return isAssignment() ? getLhs() : null; } @Override public Expression getAssignmentSource() { return getRhs(); }
Assist in IR navigation.
Params:
  • visitor – IR navigating visitor.
/** * Assist in IR navigation. * * @param visitor IR navigating visitor. */
@Override public Node accept(final NodeVisitor<? extends LexicalContext> visitor) { if (visitor.enterBinaryNode(this)) { return visitor.leaveBinaryNode(setLHS((Expression) lhs.accept(visitor)).setRHS((Expression) rhs.accept(visitor))); } return this; } @Override public <R> R accept(TranslatorNodeVisitor<? extends LexicalContext, R> visitor) { return visitor.enterBinaryNode(this); } @Override public boolean isAlwaysFalse() { switch (tokenType()) { case COMMALEFT: return lhs.isAlwaysFalse(); case COMMARIGHT: return rhs.isAlwaysFalse(); default: return false; } } @Override public boolean isAlwaysTrue() { switch (tokenType()) { case COMMALEFT: return lhs.isAlwaysTrue(); case COMMARIGHT: return rhs.isAlwaysTrue(); default: return false; } } @Override public void toString(final StringBuilder sb, final boolean printType) { final TokenType tokenType = tokenType(); final boolean lhsParen = tokenType.needsParens(getLhs().tokenType(), true); final boolean rhsParen = tokenType.needsParens(getRhs().tokenType(), false); if (lhsParen) { sb.append('('); } getLhs().toString(sb, printType); if (lhsParen) { sb.append(')'); } sb.append(' '); switch (tokenType) { case COMMALEFT: sb.append(",<"); break; case COMMARIGHT: sb.append(",>"); break; case INCPREFIX: case DECPREFIX: sb.append("++"); break; case ASSIGN_INIT: sb.append(":="); break; default: sb.append(tokenType.getName()); break; } sb.append(' '); if (rhsParen) { sb.append('('); } getRhs().toString(sb, printType); if (rhsParen) { sb.append(')'); } }
Get the left hand side expression for this node
Returns:the left hand side expression
/** * Get the left hand side expression for this node * * @return the left hand side expression */
public Expression getLhs() { return lhs; }
Get the right hand side expression for this node
Returns:the left hand side expression
/** * Get the right hand side expression for this node * * @return the left hand side expression */
public Expression getRhs() { return rhs; }
Set the left hand side expression for this node
Params:
  • lhs – new left hand side expression
Returns:a node equivalent to this one except for the requested change.
/** * Set the left hand side expression for this node * * @param lhs new left hand side expression * @return a node equivalent to this one except for the requested change. */
public BinaryNode setLHS(final Expression lhs) { if (this.lhs == lhs) { return this; } return new BinaryNode(this, lhs, rhs); }
Set the right hand side expression for this node
Params:
  • rhs – new left hand side expression
Returns:a node equivalent to this one except for the requested change.
/** * Set the right hand side expression for this node * * @param rhs new left hand side expression * @return a node equivalent to this one except for the requested change. */
public BinaryNode setRHS(final Expression rhs) { if (this.rhs == rhs) { return this; } return new BinaryNode(this, lhs, rhs); } }