author | attila |
Wed, 05 Jun 2013 10:44:32 +0200 | |
changeset 17981 | 9b8e085aa1fe |
parent 17769 | 14ea7feaf658 |
child 18867 | bc91e3fcc5ba |
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 java.util.Collections; |
|
29 |
import java.util.List; |
|
17233 | 30 |
import jdk.nashorn.internal.ir.annotations.Immutable; |
16147 | 31 |
import jdk.nashorn.internal.ir.visitor.NodeVisitor; |
32 |
||
33 |
/** |
|
34 |
* IR representation of an object literal. |
|
35 |
*/ |
|
17233 | 36 |
@Immutable |
37 |
public final class ObjectNode extends Node { |
|
16147 | 38 |
|
39 |
/** Literal elements. */ |
|
17981
9b8e085aa1fe
8015955: ObjectNode.elements should be stronger typed
attila
parents:
17769
diff
changeset
|
40 |
private final List<PropertyNode> elements; |
16147 | 41 |
|
42 |
/** |
|
43 |
* Constructor |
|
44 |
* |
|
45 |
* @param token token |
|
46 |
* @param finish finish |
|
47 |
* @param elements the elements used to initialize this ObjectNode |
|
48 |
*/ |
|
17981
9b8e085aa1fe
8015955: ObjectNode.elements should be stronger typed
attila
parents:
17769
diff
changeset
|
49 |
public ObjectNode(final long token, final int finish, final List<PropertyNode> elements) { |
17523
cb4a7c901e0d
8013913: Removed Source field from all nodes except FunctionNode in order to save footprint
lagergren
parents:
17233
diff
changeset
|
50 |
super(token, finish); |
16147 | 51 |
this.elements = elements; |
52 |
} |
|
53 |
||
17981
9b8e085aa1fe
8015955: ObjectNode.elements should be stronger typed
attila
parents:
17769
diff
changeset
|
54 |
private ObjectNode(final ObjectNode objectNode, final List<PropertyNode> elements) { |
16147 | 55 |
super(objectNode); |
17233 | 56 |
this.elements = elements; |
16147 | 57 |
} |
58 |
||
59 |
@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:
17523
diff
changeset
|
60 |
public Node accept(final NodeVisitor<? extends LexicalContext> visitor) { |
17233 | 61 |
if (visitor.enterObjectNode(this)) { |
17981
9b8e085aa1fe
8015955: ObjectNode.elements should be stronger typed
attila
parents:
17769
diff
changeset
|
62 |
return visitor.leaveObjectNode(setElements(Node.accept(visitor, PropertyNode.class, elements))); |
16147 | 63 |
} |
64 |
||
65 |
return this; |
|
66 |
} |
|
67 |
||
68 |
@Override |
|
69 |
public void toString(final StringBuilder sb) { |
|
70 |
sb.append('{'); |
|
71 |
||
72 |
if (!elements.isEmpty()) { |
|
73 |
sb.append(' '); |
|
74 |
||
75 |
boolean first = true; |
|
76 |
for (final Node element : elements) { |
|
77 |
if (!first) { |
|
78 |
sb.append(", "); |
|
79 |
} |
|
80 |
first = false; |
|
81 |
||
82 |
element.toString(sb); |
|
83 |
} |
|
84 |
sb.append(' '); |
|
85 |
} |
|
86 |
||
87 |
sb.append('}'); |
|
88 |
} |
|
89 |
||
90 |
/** |
|
91 |
* Get the elements of this literal node |
|
92 |
* @return a list of elements |
|
93 |
*/ |
|
17981
9b8e085aa1fe
8015955: ObjectNode.elements should be stronger typed
attila
parents:
17769
diff
changeset
|
94 |
public List<PropertyNode> getElements() { |
16147 | 95 |
return Collections.unmodifiableList(elements); |
96 |
} |
|
17233 | 97 |
|
17981
9b8e085aa1fe
8015955: ObjectNode.elements should be stronger typed
attila
parents:
17769
diff
changeset
|
98 |
private ObjectNode setElements(final List<PropertyNode> elements) { |
17233 | 99 |
if (this.elements == elements) { |
100 |
return this; |
|
101 |
} |
|
102 |
return new ObjectNode(this, elements); |
|
103 |
} |
|
16147 | 104 |
} |