Tue, 07 May 2013 14:36:57 +0200
8013913: Removed Source field from all nodes except FunctionNode in order to save footprint
Reviewed-by: jlaskey, attila
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 */
26 package jdk.nashorn.internal.ir;
28 import jdk.nashorn.internal.codegen.types.Type;
29 import jdk.nashorn.internal.ir.annotations.Immutable;
30 import jdk.nashorn.internal.ir.visitor.NodeVisitor;
32 /**
33 * IR representation of an indexed access (brackets operator.)
34 */
35 @Immutable
36 public final class IndexNode extends BaseNode {
37 /** Property index. */
38 private final Node index;
40 /**
41 * Constructors
42 *
43 * @param token token
44 * @param finish finish
45 * @param base base node for access
46 * @param index index for access
47 */
48 public IndexNode(final long token, final int finish, final Node base, final Node index) {
49 super(token, finish, base, false, false);
50 this.index = index;
51 }
53 private IndexNode(final IndexNode indexNode, final Node base, final Node index, final boolean isFunction, final boolean hasCallSiteType) {
54 super(indexNode, base, isFunction, hasCallSiteType);
55 this.index = index;
56 }
58 @Override
59 public Node accept(final NodeVisitor visitor) {
60 if (visitor.enterIndexNode(this)) {
61 final Node newBase = base.accept(visitor);
62 final Node newIndex = index.accept(visitor);
63 final IndexNode newNode;
64 if (newBase != base || newIndex != index) {
65 newNode = new IndexNode(this, newBase, newIndex, isFunction(), hasCallSiteType());
66 } else {
67 newNode = this;
68 }
69 return visitor.leaveIndexNode(newNode);
70 }
72 return this;
73 }
75 @Override
76 public void toString(final StringBuilder sb) {
77 final boolean needsParen = tokenType().needsParens(base.tokenType(), true);
79 if (hasCallSiteType()) {
80 sb.append('{');
81 final String desc = getType().getDescriptor();
82 sb.append(desc.charAt(desc.length() - 1) == ';' ? "O" : getType().getDescriptor());
83 sb.append('}');
84 }
86 if (needsParen) {
87 sb.append('(');
88 }
90 base.toString(sb);
92 if (needsParen) {
93 sb.append(')');
94 }
96 sb.append('[');
97 index.toString(sb);
98 sb.append(']');
99 }
101 /**
102 * Get the index expression for this IndexNode
103 * @return the index
104 */
105 public Node getIndex() {
106 return index;
107 }
109 @Override
110 public BaseNode setIsFunction() {
111 if (isFunction()) {
112 return this;
113 }
114 return new IndexNode(this, base, index, true, hasCallSiteType());
115 }
117 @Override
118 public IndexNode setType(final Type type) {
119 logTypeChange(type);
120 getSymbol().setTypeOverride(type); //always a temp so this is fine.
121 return new IndexNode(this, base, index, isFunction(), true);
122 }
124 }