View Javadoc
1   /*
2    * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
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.codegen.Label;
29  import jdk.nashorn.internal.ir.annotations.Immutable;
30  import jdk.nashorn.internal.ir.visitor.NodeVisitor;
31  
32  /**
33   * IR representation of CASE clause.
34   * Case nodes are not BreakableNodes, but the SwitchNode is
35   */
36  @Immutable
37  public final class CaseNode extends Node {
38      /** Test expression. */
39      private final Expression test;
40  
41      /** Statements. */
42      private final Block body;
43  
44      /** Case entry label. */
45      private final Label entry;
46  
47      /**
48       * Constructors
49       *
50       * @param token    token
51       * @param finish   finish
52       * @param test     case test node, can be any node in JavaScript
53       * @param body     case body
54       */
55      public CaseNode(final long token, final int finish, final Expression test, final Block body) {
56          super(token, finish);
57  
58          this.test  = test;
59          this.body  = body;
60          this.entry = new Label("entry");
61      }
62  
63      CaseNode(final CaseNode caseNode, final Expression test, final Block body) {
64          super(caseNode);
65  
66          this.test  = test;
67          this.body  = body;
68          this.entry = new Label(caseNode.entry);
69      }
70  
71      @Override
72      public boolean isTerminal() {
73          return body.isTerminal();
74      }
75  
76      /**
77       * Assist in IR navigation.
78       * @param visitor IR navigating visitor.
79       */
80      @Override
81      public Node accept(final NodeVisitor<? extends LexicalContext> visitor) {
82          if (visitor.enterCaseNode(this)) {
83              final Expression newTest = test == null ? null : (Expression)test.accept(visitor);
84              final Block newBody = body == null ? null : (Block)body.accept(visitor);
85  
86              return visitor.leaveCaseNode(setTest(newTest).setBody(newBody));
87          }
88  
89          return this;
90      }
91  
92      @Override
93      public void toString(final StringBuilder sb) {
94          if (test != null) {
95              sb.append("case ");
96              test.toString(sb);
97              sb.append(':');
98          } else {
99              sb.append("default:");
100         }
101     }
102 
103     /**
104      * Get the body for this case node
105      * @return the body
106      */
107     public Block getBody() {
108         return body;
109     }
110 
111     /**
112      * Get the entry label for this case node
113      * @return the entry label
114      */
115     public Label getEntry() {
116         return entry;
117     }
118 
119     /**
120      * Get the test expression for this case node
121      * @return the test
122      */
123     public Expression getTest() {
124         return test;
125     }
126 
127     /**
128      * Reset the test expression for this case node
129      * @param test new test expression
130      * @return new or same CaseNode
131      */
132     public CaseNode setTest(final Expression test) {
133         if (this.test == test) {
134             return this;
135         }
136         return new CaseNode(this, test, body);
137     }
138 
139     private CaseNode setBody(final Block body) {
140         if (this.body == body) {
141             return this;
142         }
143         return new CaseNode(this, test, body);
144     }
145 }