Fri, 17 Oct 2014 14:24:26 +0200
8059843: Make AST serializable
Reviewed-by: hannesw, lagergren
attila@963 | 1 | /* |
attila@963 | 2 | * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
attila@963 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
attila@963 | 4 | * |
attila@963 | 5 | * This code is free software; you can redistribute it and/or modify it |
attila@963 | 6 | * under the terms of the GNU General Public License version 2 only, as |
attila@963 | 7 | * published by the Free Software Foundation. Oracle designates this |
attila@963 | 8 | * particular file as subject to the "Classpath" exception as provided |
attila@963 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
attila@963 | 10 | * |
attila@963 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
attila@963 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
attila@963 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
attila@963 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
attila@963 | 15 | * accompanied this code). |
attila@963 | 16 | * |
attila@963 | 17 | * You should have received a copy of the GNU General Public License version |
attila@963 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
attila@963 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
attila@963 | 20 | * |
attila@963 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
attila@963 | 22 | * or visit www.oracle.com if you need additional information or have any |
attila@963 | 23 | * questions. |
attila@963 | 24 | */ |
attila@963 | 25 | |
attila@963 | 26 | package jdk.nashorn.internal.ir; |
attila@963 | 27 | |
attila@963 | 28 | import java.util.function.Function; |
attila@963 | 29 | import jdk.nashorn.internal.codegen.types.Type; |
attila@963 | 30 | import jdk.nashorn.internal.ir.visitor.NodeVisitor; |
attila@963 | 31 | |
attila@963 | 32 | /** |
attila@963 | 33 | * A wrapper for an expression that is in a position to be a join predecessor. |
attila@963 | 34 | */ |
attila@963 | 35 | public class JoinPredecessorExpression extends Expression implements JoinPredecessor { |
attila@1063 | 36 | private static final long serialVersionUID = 1L; |
attila@963 | 37 | |
attila@963 | 38 | private final Expression expression; |
attila@963 | 39 | private final LocalVariableConversion conversion; |
attila@963 | 40 | |
attila@963 | 41 | /** |
attila@963 | 42 | * A no-arg constructor does not wrap any expression on its own, but can be used as a place to contain a local |
attila@963 | 43 | * variable conversion in a place where an expression can otherwise stand. |
attila@963 | 44 | */ |
attila@963 | 45 | public JoinPredecessorExpression() { |
attila@963 | 46 | this(null); |
attila@963 | 47 | } |
attila@963 | 48 | |
attila@963 | 49 | /** |
attila@963 | 50 | * A constructor for wrapping an expression and making it a join predecessor. Typically used on true and false |
attila@963 | 51 | * subexpressions of the ternary node as well as on the operands of short-circuiting logical expressions {@code &&} |
attila@963 | 52 | * and {@code ||}. |
attila@963 | 53 | * @param expression the expression to wrap |
attila@963 | 54 | */ |
attila@963 | 55 | public JoinPredecessorExpression(final Expression expression) { |
attila@963 | 56 | this(expression, null); |
attila@963 | 57 | } |
attila@963 | 58 | |
attila@963 | 59 | private JoinPredecessorExpression(final Expression expression, final LocalVariableConversion conversion) { |
attila@963 | 60 | super(expression == null ? 0L : expression.getToken(), expression == null ? 0 : expression.getStart(), expression == null ? 0 : expression.getFinish()); |
attila@963 | 61 | this.expression = expression; |
attila@963 | 62 | this.conversion = conversion; |
attila@963 | 63 | } |
attila@963 | 64 | |
attila@963 | 65 | @Override |
attila@963 | 66 | public JoinPredecessor setLocalVariableConversion(final LexicalContext lc, final LocalVariableConversion conversion) { |
attila@963 | 67 | if(conversion == this.conversion) { |
attila@963 | 68 | return this; |
attila@963 | 69 | } |
attila@963 | 70 | return new JoinPredecessorExpression(expression, conversion); |
attila@963 | 71 | } |
attila@963 | 72 | |
attila@963 | 73 | @Override |
attila@963 | 74 | public Type getType(final Function<Symbol, Type> localVariableTypes) { |
attila@963 | 75 | return expression.getType(localVariableTypes); |
attila@963 | 76 | } |
attila@963 | 77 | |
attila@963 | 78 | @Override |
attila@963 | 79 | public boolean isAlwaysFalse() { |
attila@963 | 80 | return expression != null && expression.isAlwaysFalse(); |
attila@963 | 81 | } |
attila@963 | 82 | |
attila@963 | 83 | @Override |
attila@963 | 84 | public boolean isAlwaysTrue() { |
attila@963 | 85 | return expression != null && expression.isAlwaysTrue(); |
attila@963 | 86 | } |
attila@963 | 87 | |
attila@963 | 88 | /** |
attila@963 | 89 | * Returns the underlying expression. |
attila@963 | 90 | * @return the underlying expression. |
attila@963 | 91 | */ |
attila@963 | 92 | public Expression getExpression() { |
attila@963 | 93 | return expression; |
attila@963 | 94 | } |
attila@963 | 95 | |
attila@963 | 96 | /** |
attila@963 | 97 | * Sets the underlying expression. |
attila@963 | 98 | * @param expression the new underlying expression |
attila@963 | 99 | * @return this or modified join predecessor expression object. |
attila@963 | 100 | */ |
attila@963 | 101 | public JoinPredecessorExpression setExpression(final Expression expression) { |
attila@963 | 102 | if(expression == this.expression) { |
attila@963 | 103 | return this; |
attila@963 | 104 | } |
attila@963 | 105 | return new JoinPredecessorExpression(expression, conversion); |
attila@963 | 106 | } |
attila@963 | 107 | |
attila@963 | 108 | @Override |
attila@963 | 109 | public LocalVariableConversion getLocalVariableConversion() { |
attila@963 | 110 | return conversion; |
attila@963 | 111 | } |
attila@963 | 112 | |
attila@963 | 113 | @Override |
attila@963 | 114 | public Node accept(final NodeVisitor<? extends LexicalContext> visitor) { |
attila@963 | 115 | if(visitor.enterJoinPredecessorExpression(this)) { |
attila@963 | 116 | final Expression expr = getExpression(); |
attila@963 | 117 | return visitor.leaveJoinPredecessorExpression(expr == null ? this : setExpression((Expression)expr.accept(visitor))); |
attila@963 | 118 | } |
attila@963 | 119 | return this; |
attila@963 | 120 | } |
attila@963 | 121 | |
attila@963 | 122 | @Override |
attila@963 | 123 | public void toString(final StringBuilder sb, final boolean printType) { |
attila@963 | 124 | if(expression != null) { |
attila@963 | 125 | expression.toString(sb, printType); |
attila@963 | 126 | } |
attila@963 | 127 | if(conversion != null) { |
attila@963 | 128 | conversion.toString(sb); |
attila@963 | 129 | } |
attila@963 | 130 | } |
attila@963 | 131 | |
attila@963 | 132 | } |