author | hannesw |
Fri, 25 Oct 2013 10:20:49 +0200 | |
changeset 21457 | 381acbd07fe5 |
parent 20928 | 3ff39d5c8c08 |
child 24719 | f726e9d67629 |
permissions | -rw-r--r-- |
16147 | 1 |
/* |
16151 | 2 |
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
16147 | 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 static jdk.nashorn.internal.codegen.CompilerConstants.__DIR__; |
|
29 |
import static jdk.nashorn.internal.codegen.CompilerConstants.__FILE__; |
|
30 |
import static jdk.nashorn.internal.codegen.CompilerConstants.__LINE__; |
|
18867
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
31 |
|
16147 | 32 |
import jdk.nashorn.internal.codegen.types.Type; |
17233 | 33 |
import jdk.nashorn.internal.ir.annotations.Immutable; |
16147 | 34 |
import jdk.nashorn.internal.ir.visitor.NodeVisitor; |
35 |
||
36 |
/** |
|
37 |
* IR representation for an identifier. |
|
38 |
*/ |
|
17233 | 39 |
@Immutable |
20928
3ff39d5c8c08
8026137: Fix Issues with Binary Evaluation Order
lagergren
parents:
20571
diff
changeset
|
40 |
public final class IdentNode extends Expression implements PropertyKey, FunctionCall { |
20571
f52b0abf5d88
8026039: future strict names are allowed as function name and argument name of a strict function
sundar
parents:
19889
diff
changeset
|
41 |
private static final int PROPERTY_NAME = 1 << 0; |
f52b0abf5d88
8026039: future strict names are allowed as function name and argument name of a strict function
sundar
parents:
19889
diff
changeset
|
42 |
private static final int INITIALIZED_HERE = 1 << 1; |
f52b0abf5d88
8026039: future strict names are allowed as function name and argument name of a strict function
sundar
parents:
19889
diff
changeset
|
43 |
private static final int FUNCTION = 1 << 2; |
f52b0abf5d88
8026039: future strict names are allowed as function name and argument name of a strict function
sundar
parents:
19889
diff
changeset
|
44 |
private static final int FUTURESTRICT_NAME = 1 << 3; |
16530
201d682e75f4
8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
16240
diff
changeset
|
45 |
|
16147 | 46 |
/** Identifier. */ |
47 |
private final String name; |
|
48 |
||
49 |
/** Type for a callsite, e.g. X in a get()X or a set(X)V */ |
|
17233 | 50 |
private final Type callSiteType; |
16147 | 51 |
|
17233 | 52 |
private final int flags; |
16147 | 53 |
|
54 |
/** |
|
55 |
* Constructor |
|
56 |
* |
|
57 |
* @param token token |
|
58 |
* @param finish finish position |
|
59 |
* @param name name of identifier |
|
60 |
*/ |
|
17523
cb4a7c901e0d
8013913: Removed Source field from all nodes except FunctionNode in order to save footprint
lagergren
parents:
17233
diff
changeset
|
61 |
public IdentNode(final long token, final int finish, final String name) { |
cb4a7c901e0d
8013913: Removed Source field from all nodes except FunctionNode in order to save footprint
lagergren
parents:
17233
diff
changeset
|
62 |
super(token, finish); |
17525 | 63 |
this.name = name.intern(); |
17233 | 64 |
this.callSiteType = null; |
65 |
this.flags = 0; |
|
66 |
} |
|
67 |
||
68 |
private IdentNode(final IdentNode identNode, final String name, final Type callSiteType, final int flags) { |
|
69 |
super(identNode); |
|
70 |
this.name = name; |
|
71 |
this.callSiteType = callSiteType; |
|
72 |
this.flags = flags; |
|
16147 | 73 |
} |
74 |
||
75 |
/** |
|
76 |
* Copy constructor - create a new IdentNode for the same location |
|
77 |
* |
|
78 |
* @param identNode identNode |
|
79 |
*/ |
|
80 |
public IdentNode(final IdentNode identNode) { |
|
81 |
super(identNode); |
|
17233 | 82 |
this.name = identNode.getName(); |
83 |
this.callSiteType = null; |
|
84 |
this.flags = identNode.flags; |
|
16147 | 85 |
} |
86 |
||
87 |
@Override |
|
88 |
public Type getType() { |
|
89 |
return callSiteType == null ? super.getType() : callSiteType; |
|
90 |
} |
|
91 |
||
92 |
@Override |
|
93 |
public boolean isAtom() { |
|
94 |
return true; |
|
95 |
} |
|
96 |
||
97 |
private boolean hasCallSiteType() { |
|
98 |
//this is an identity that's part of a getter or setter |
|
99 |
return callSiteType != null; |
|
100 |
} |
|
101 |
||
102 |
/** |
|
103 |
* Assist in IR navigation. |
|
104 |
* |
|
105 |
* @param visitor IR navigating visitor. |
|
106 |
*/ |
|
107 |
@Override |
|
17769
14ea7feaf658
8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
17525
diff
changeset
|
108 |
public Node accept(final NodeVisitor<? extends LexicalContext> visitor) { |
17233 | 109 |
if (visitor.enterIdentNode(this)) { |
16530
201d682e75f4
8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
16240
diff
changeset
|
110 |
return visitor.leaveIdentNode(this); |
16147 | 111 |
} |
112 |
||
113 |
return this; |
|
114 |
} |
|
115 |
||
116 |
@Override |
|
117 |
public void toString(final StringBuilder sb) { |
|
118 |
if (hasCallSiteType()) { |
|
119 |
sb.append('{'); |
|
120 |
final String desc = getType().getDescriptor(); |
|
17233 | 121 |
sb.append(desc.charAt(desc.length() - 1) == ';' ? 'O' : getType().getDescriptor()); |
16147 | 122 |
sb.append('}'); |
123 |
} |
|
124 |
||
125 |
sb.append(name); |
|
126 |
} |
|
127 |
||
128 |
/** |
|
129 |
* Get the name of the identifier |
|
130 |
* @return IdentNode name |
|
131 |
*/ |
|
132 |
public String getName() { |
|
133 |
return name; |
|
134 |
} |
|
135 |
||
136 |
@Override |
|
137 |
public String getPropertyName() { |
|
138 |
return getName(); |
|
139 |
} |
|
140 |
||
21457
381acbd07fe5
8027042: Evaluation order for binary operators can be improved
hannesw
parents:
20928
diff
changeset
|
141 |
@Override |
381acbd07fe5
8027042: Evaluation order for binary operators can be improved
hannesw
parents:
20928
diff
changeset
|
142 |
public boolean isLocal() { |
381acbd07fe5
8027042: Evaluation order for binary operators can be improved
hannesw
parents:
20928
diff
changeset
|
143 |
return !getSymbol().isScope(); |
381acbd07fe5
8027042: Evaluation order for binary operators can be improved
hannesw
parents:
20928
diff
changeset
|
144 |
} |
381acbd07fe5
8027042: Evaluation order for binary operators can be improved
hannesw
parents:
20928
diff
changeset
|
145 |
|
16147 | 146 |
/** |
147 |
* Check if this IdentNode is a property name |
|
148 |
* @return true if this is a property name |
|
149 |
*/ |
|
150 |
public boolean isPropertyName() { |
|
16530
201d682e75f4
8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
16240
diff
changeset
|
151 |
return (flags & PROPERTY_NAME) != 0; |
16147 | 152 |
} |
153 |
||
154 |
/** |
|
155 |
* Flag this IdentNode as a property name |
|
16530
201d682e75f4
8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
16240
diff
changeset
|
156 |
* @return a node equivalent to this one except for the requested change. |
16147 | 157 |
*/ |
16530
201d682e75f4
8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
16240
diff
changeset
|
158 |
public IdentNode setIsPropertyName() { |
17233 | 159 |
if (isPropertyName()) { |
160 |
return this; |
|
161 |
} |
|
162 |
return new IdentNode(this, name, callSiteType, flags | PROPERTY_NAME); |
|
16147 | 163 |
} |
164 |
||
165 |
/** |
|
20571
f52b0abf5d88
8026039: future strict names are allowed as function name and argument name of a strict function
sundar
parents:
19889
diff
changeset
|
166 |
* Check if this IdentNode is a future strict name |
f52b0abf5d88
8026039: future strict names are allowed as function name and argument name of a strict function
sundar
parents:
19889
diff
changeset
|
167 |
* @return true if this is a future strict name |
f52b0abf5d88
8026039: future strict names are allowed as function name and argument name of a strict function
sundar
parents:
19889
diff
changeset
|
168 |
*/ |
f52b0abf5d88
8026039: future strict names are allowed as function name and argument name of a strict function
sundar
parents:
19889
diff
changeset
|
169 |
public boolean isFutureStrictName() { |
f52b0abf5d88
8026039: future strict names are allowed as function name and argument name of a strict function
sundar
parents:
19889
diff
changeset
|
170 |
return (flags & FUTURESTRICT_NAME) != 0; |
f52b0abf5d88
8026039: future strict names are allowed as function name and argument name of a strict function
sundar
parents:
19889
diff
changeset
|
171 |
} |
f52b0abf5d88
8026039: future strict names are allowed as function name and argument name of a strict function
sundar
parents:
19889
diff
changeset
|
172 |
|
f52b0abf5d88
8026039: future strict names are allowed as function name and argument name of a strict function
sundar
parents:
19889
diff
changeset
|
173 |
/** |
f52b0abf5d88
8026039: future strict names are allowed as function name and argument name of a strict function
sundar
parents:
19889
diff
changeset
|
174 |
* Flag this IdentNode as a future strict name |
f52b0abf5d88
8026039: future strict names are allowed as function name and argument name of a strict function
sundar
parents:
19889
diff
changeset
|
175 |
* @return a node equivalent to this one except for the requested change. |
f52b0abf5d88
8026039: future strict names are allowed as function name and argument name of a strict function
sundar
parents:
19889
diff
changeset
|
176 |
*/ |
f52b0abf5d88
8026039: future strict names are allowed as function name and argument name of a strict function
sundar
parents:
19889
diff
changeset
|
177 |
public IdentNode setIsFutureStrictName() { |
f52b0abf5d88
8026039: future strict names are allowed as function name and argument name of a strict function
sundar
parents:
19889
diff
changeset
|
178 |
if (isFutureStrictName()) { |
f52b0abf5d88
8026039: future strict names are allowed as function name and argument name of a strict function
sundar
parents:
19889
diff
changeset
|
179 |
return this; |
f52b0abf5d88
8026039: future strict names are allowed as function name and argument name of a strict function
sundar
parents:
19889
diff
changeset
|
180 |
} |
f52b0abf5d88
8026039: future strict names are allowed as function name and argument name of a strict function
sundar
parents:
19889
diff
changeset
|
181 |
return new IdentNode(this, name, callSiteType, flags | FUTURESTRICT_NAME); |
f52b0abf5d88
8026039: future strict names are allowed as function name and argument name of a strict function
sundar
parents:
19889
diff
changeset
|
182 |
} |
f52b0abf5d88
8026039: future strict names are allowed as function name and argument name of a strict function
sundar
parents:
19889
diff
changeset
|
183 |
|
f52b0abf5d88
8026039: future strict names are allowed as function name and argument name of a strict function
sundar
parents:
19889
diff
changeset
|
184 |
/** |
16147 | 185 |
* Helper function for local def analysis. |
186 |
* @return true if IdentNode is initialized on creation |
|
187 |
*/ |
|
188 |
public boolean isInitializedHere() { |
|
16530
201d682e75f4
8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
16240
diff
changeset
|
189 |
return (flags & INITIALIZED_HERE) != 0; |
16147 | 190 |
} |
191 |
||
192 |
/** |
|
193 |
* Flag IdentNode to be initialized on creation |
|
16530
201d682e75f4
8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
16240
diff
changeset
|
194 |
* @return a node equivalent to this one except for the requested change. |
16147 | 195 |
*/ |
16530
201d682e75f4
8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
16240
diff
changeset
|
196 |
public IdentNode setIsInitializedHere() { |
17233 | 197 |
if (isInitializedHere()) { |
198 |
return this; |
|
199 |
} |
|
200 |
return new IdentNode(this, name, callSiteType, flags | INITIALIZED_HERE); |
|
16147 | 201 |
} |
202 |
||
203 |
/** |
|
204 |
* Check if this IdentNode is a special identity, currently __DIR__, __FILE__ |
|
205 |
* or __LINE__ |
|
206 |
* |
|
207 |
* @return true if this IdentNode is special |
|
208 |
*/ |
|
209 |
public boolean isSpecialIdentity() { |
|
17233 | 210 |
return name.equals(__DIR__.symbolName()) || name.equals(__FILE__.symbolName()) || name.equals(__LINE__.symbolName()); |
16147 | 211 |
} |
212 |
||
213 |
@Override |
|
214 |
public boolean isFunction() { |
|
16530
201d682e75f4
8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
16240
diff
changeset
|
215 |
return (flags & FUNCTION) != 0; |
201d682e75f4
8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
16240
diff
changeset
|
216 |
} |
201d682e75f4
8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
16240
diff
changeset
|
217 |
|
201d682e75f4
8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
16240
diff
changeset
|
218 |
/** |
201d682e75f4
8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
16240
diff
changeset
|
219 |
* Mark this node as being the callee operand of a {@link CallNode}. |
201d682e75f4
8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
16240
diff
changeset
|
220 |
* @return an ident node identical to this one in all aspects except with its function flag set. |
201d682e75f4
8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
16240
diff
changeset
|
221 |
*/ |
201d682e75f4
8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
16240
diff
changeset
|
222 |
public IdentNode setIsFunction() { |
17233 | 223 |
if (isFunction()) { |
224 |
return this; |
|
225 |
} |
|
226 |
return new IdentNode(this, name, callSiteType, flags | FUNCTION); |
|
16147 | 227 |
} |
228 |
} |