< prev index next >

src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/ir/CatchNode.java

Print this page




  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.nashorn.internal.ir;
  27 
  28 import jdk.nashorn.internal.ir.annotations.Immutable;
  29 import jdk.nashorn.internal.ir.visitor.NodeVisitor;
  30 
  31 /**
  32  * IR representation of a catch clause.
  33  */
  34 @Immutable
  35 public final class CatchNode extends Statement {
  36     private static final long serialVersionUID = 1L;
  37 
  38     /** Exception identifier. */
  39     private final IdentNode exception;
  40 
  41     /** Exception condition. */
  42     private final Expression exceptionCondition;
  43 
  44     /** Catch body. */
  45     private final Block body;
  46 
  47     private final boolean isSyntheticRethrow;
  48 
  49     /**
  50      * Constructors
  51      *
  52      * @param lineNumber         lineNumber
  53      * @param token              token
  54      * @param finish             finish
  55      * @param exception          variable name of exception
  56      * @param exceptionCondition exception condition
  57      * @param body               catch body
  58      * @param isSyntheticRethrow true if this node is a synthetically generated rethrow node.
  59      */
  60     public CatchNode(final int lineNumber, final long token, final int finish, final IdentNode exception,
  61             final Expression exceptionCondition, final Block body, final boolean isSyntheticRethrow) {
  62         super(lineNumber, token, finish);
  63         this.exception          = exception == null ? null : exception.setIsInitializedHere();





  64         this.exceptionCondition = exceptionCondition;
  65         this.body               = body;
  66         this.isSyntheticRethrow = isSyntheticRethrow;
  67     }
  68 
  69     private CatchNode(final CatchNode catchNode, final IdentNode exception, final Expression exceptionCondition,
  70             final Block body, final boolean isSyntheticRethrow) {
  71         super(catchNode);
  72         this.exception          = exception;
  73         this.exceptionCondition = exceptionCondition;
  74         this.body               = body;
  75         this.isSyntheticRethrow = isSyntheticRethrow;
  76     }
  77 
  78     /**
  79      * Assist in IR navigation.
  80      * @param visitor IR navigating visitor.
  81      */
  82     @Override
  83     public Node accept(final NodeVisitor<? extends LexicalContext> visitor) {
  84         if (visitor.enterCatchNode(this)) {
  85             return visitor.leaveCatchNode(
  86                 setException((IdentNode)exception.accept(visitor)).
  87                 setExceptionCondition(exceptionCondition == null ? null : (Expression)exceptionCondition.accept(visitor)).
  88                 setBody((Block)body.accept(visitor)));
  89         }
  90 
  91         return this;
  92     }
  93 
  94     @Override
  95     public boolean isTerminal() {
  96         return body.isTerminal();
  97     }
  98 
  99     @Override
 100     public void toString(final StringBuilder sb, final boolean printTypes) {
 101         sb.append(" catch (");
 102         exception.toString(sb, printTypes);
 103 
 104         if (exceptionCondition != null) {
 105             sb.append(" if ");
 106             exceptionCondition.toString(sb, printTypes);
 107         }
 108         sb.append(')');
 109     }
 110 
 111     /**
 112      * Get the identifier representing the exception thrown
 113      * @return the exception identifier
 114      */
 115     public IdentNode getException() {
 116         return exception;
 117     }
 118 
 119     /**
 120      * Get the exception condition for this catch block
 121      * @return the exception condition
 122      */
 123     public Expression getExceptionCondition() {
 124         return exceptionCondition;
 125     }
 126 
 127     /**
 128      * Reset the exception condition for this catch block
 129      * @param exceptionCondition the new exception condition
 130      * @return new or same CatchNode
 131      */
 132     public CatchNode setExceptionCondition(final Expression exceptionCondition) {
 133         if (this.exceptionCondition == exceptionCondition) {
 134             return this;
 135         }
 136         return new CatchNode(this, exception, exceptionCondition, body, isSyntheticRethrow);
 137     }
 138 
 139     /**
 140      * Get the body for this catch block
 141      * @return the catch block body
 142      */
 143     public Block getBody() {
 144         return body;
 145     }
 146 
 147     /**
 148      * Resets the exception of a catch block
 149      * @param exception new exception
 150      * @return new catch node if changed, same otherwise
 151      */
 152     public CatchNode setException(final IdentNode exception) {
 153         if (this.exception == exception) {
 154             return this;
 155         }
 156         return new CatchNode(this, exception, exceptionCondition, body, isSyntheticRethrow);
 157     }
 158 
 159     private CatchNode setBody(final Block body) {
 160         if (this.body == body) {
 161             return this;
 162         }
 163         return new CatchNode(this, exception, exceptionCondition, body, isSyntheticRethrow);
 164     }
 165 
 166     /**
 167      * Is this catch block a non-JavaScript constructor, for example created as
 168      * part of the rethrow mechanism of a finally block in Lower? Then we just
 169      * pass the exception on and need not unwrap whatever is in the ECMAException
 170      * object catch symbol
 171      * @return true if a finally synthetic rethrow
 172      */


  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.nashorn.internal.ir;
  27 
  28 import jdk.nashorn.internal.ir.annotations.Immutable;
  29 import jdk.nashorn.internal.ir.visitor.NodeVisitor;
  30 
  31 /**
  32  * IR representation of a catch clause.
  33  */
  34 @Immutable
  35 public final class CatchNode extends Statement {
  36     private static final long serialVersionUID = 1L;
  37 
  38     /** Exception identifier or pattern. */
  39     private final Expression exception;
  40 
  41     /** Exception condition. */
  42     private final Expression exceptionCondition;
  43 
  44     /** Catch body. */
  45     private final Block body;
  46 
  47     private final boolean isSyntheticRethrow;
  48 
  49     /**
  50      * Constructors
  51      *
  52      * @param lineNumber         lineNumber
  53      * @param token              token
  54      * @param finish             finish
  55      * @param exception          variable name or pattern of exception
  56      * @param exceptionCondition exception condition
  57      * @param body               catch body
  58      * @param isSyntheticRethrow true if this node is a synthetically generated rethrow node.
  59      */
  60     public CatchNode(final int lineNumber, final long token, final int finish, final Expression exception,
  61             final Expression exceptionCondition, final Block body, final boolean isSyntheticRethrow) {
  62         super(lineNumber, token, finish);
  63         if (exception instanceof IdentNode) {
  64             this.exception = ((IdentNode) exception).setIsInitializedHere();
  65         } else {
  66             this.exception = exception;
  67         }
  68 
  69         this.exceptionCondition = exceptionCondition;
  70         this.body = body;
  71         this.isSyntheticRethrow = isSyntheticRethrow;
  72     }
  73 
  74     private CatchNode(final CatchNode catchNode, final Expression exception, final Expression exceptionCondition,
  75             final Block body, final boolean isSyntheticRethrow) {
  76         super(catchNode);
  77         this.exception          = exception;
  78         this.exceptionCondition = exceptionCondition;
  79         this.body               = body;
  80         this.isSyntheticRethrow = isSyntheticRethrow;
  81     }
  82 
  83     /**
  84      * Assist in IR navigation.
  85      * @param visitor IR navigating visitor.
  86      */
  87     @Override
  88     public Node accept(final NodeVisitor<? extends LexicalContext> visitor) {
  89         if (visitor.enterCatchNode(this)) {
  90             return visitor.leaveCatchNode(
  91                     setException((Expression) exception.accept(visitor)).
  92                             setExceptionCondition(exceptionCondition == null ? null : (Expression) exceptionCondition.accept(visitor)).
  93                             setBody((Block) body.accept(visitor)));
  94         }

  95         return this;
  96     }
  97 
  98     @Override
  99     public boolean isTerminal() {
 100         return body.isTerminal();
 101     }
 102 
 103     @Override
 104     public void toString(final StringBuilder sb, final boolean printTypes) {
 105         sb.append(" catch (");
 106         exception.toString(sb, printTypes);
 107 
 108         if (exceptionCondition != null) {
 109             sb.append(" if ");
 110             exceptionCondition.toString(sb, printTypes);
 111         }
 112         sb.append(')');
 113     }
 114 
 115     /**
 116      * Get the identifier representing the exception thrown
 117      * @return the exception identifier
 118      */
 119     public Expression getException() {
 120         return exception;
 121     }
 122 
 123     /**
 124      * Get the exception condition for this catch block
 125      * @return the exception condition
 126      */
 127     public Expression getExceptionCondition() {
 128         return exceptionCondition;
 129     }
 130 
 131     /**
 132      * Reset the exception condition for this catch block
 133      * @param exceptionCondition the new exception condition
 134      * @return new or same CatchNode
 135      */
 136     public CatchNode setExceptionCondition(final Expression exceptionCondition) {
 137         if (this.exceptionCondition == exceptionCondition) {
 138             return this;
 139         }
 140         return new CatchNode(this, exception, exceptionCondition, body, isSyntheticRethrow);
 141     }
 142 
 143     /**
 144      * Get the body for this catch block
 145      * @return the catch block body
 146      */
 147     public Block getBody() {
 148         return body;
 149     }
 150 
 151     /**
 152      * Resets the exception of a catch block
 153      * @param exception new exception
 154      * @return new catch node if changed, same otherwise
 155      */
 156     public CatchNode setException(final Expression exception) {
 157         if (this.exception == exception) {
 158             return this;
 159         }
 160         return new CatchNode(this, exception, exceptionCondition, body, isSyntheticRethrow);
 161     }
 162 
 163     private CatchNode setBody(final Block body) {
 164         if (this.body == body) {
 165             return this;
 166         }
 167         return new CatchNode(this, exception, exceptionCondition, body, isSyntheticRethrow);
 168     }
 169 
 170     /**
 171      * Is this catch block a non-JavaScript constructor, for example created as
 172      * part of the rethrow mechanism of a finally block in Lower? Then we just
 173      * pass the exception on and need not unwrap whatever is in the ECMAException
 174      * object catch symbol
 175      * @return true if a finally synthetic rethrow
 176      */
< prev index next >