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.CompileUnit;
29  import jdk.nashorn.internal.ir.annotations.Immutable;
30  import jdk.nashorn.internal.ir.visitor.NodeVisitor;
31  
32  /**
33   * Node indicating code is split across classes.
34   */
35  @Immutable
36  public class SplitNode extends LexicalContextStatement {
37      /** Split node method name. */
38      private final String name;
39  
40      /** Compilation unit. */
41      private final CompileUnit compileUnit;
42  
43      /** Body of split code. */
44      private final Node body;
45  
46      /**
47       * Constructor
48       *
49       * @param name        name of split node
50       * @param body        body of split code
51       * @param compileUnit compile unit to use for the body
52       */
53      public SplitNode(final String name, final Node body, final CompileUnit compileUnit) {
54          super(-1, body.getToken(), body.getFinish());
55          this.name        = name;
56          this.body        = body;
57          this.compileUnit = compileUnit;
58      }
59  
60      private SplitNode(final SplitNode splitNode, final Node body) {
61          super(splitNode);
62          this.name        = splitNode.name;
63          this.body        = body;
64          this.compileUnit = splitNode.compileUnit;
65      }
66  
67      /**
68       * Get the body for this split node - i.e. the actual code it encloses
69       * @return body for split node
70       */
71      public Node getBody() {
72          return body;
73      }
74  
75      private SplitNode setBody(final LexicalContext lc, final Node body) {
76          if (this.body == body) {
77              return this;
78          }
79          return Node.replaceInLexicalContext(lc, this, new SplitNode(this, body));
80      }
81  
82      @Override
83      public Node accept(final LexicalContext lc, final NodeVisitor<? extends LexicalContext> visitor) {
84          if (visitor.enterSplitNode(this)) {
85              return visitor.leaveSplitNode(setBody(lc, body.accept(visitor)));
86          }
87          return this;
88      }
89  
90      @Override
91      public void toString(final StringBuilder sb) {
92          sb.append("<split>(");
93          sb.append(compileUnit.getClass().getSimpleName());
94          sb.append(") ");
95          body.toString(sb);
96      }
97  
98      /**
99       * Get the name for this split node
100      * @return name
101      */
102     public String getName() {
103         return name;
104     }
105 
106     /**
107      * Get the compile unit for this split node
108      * @return compile unit
109      */
110     public CompileUnit getCompileUnit() {
111         return compileUnit;
112     }
113 
114 }