ObjectNode.java revision 1435:6e5080fdfaad
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
26package jdk.nashorn.internal.ir;
27
28import java.util.Collections;
29import java.util.List;
30import java.util.RandomAccess;
31import jdk.nashorn.internal.codegen.types.Type;
32import jdk.nashorn.internal.ir.annotations.Immutable;
33import jdk.nashorn.internal.ir.visitor.NodeVisitor;
34
35/**
36 * IR representation of an object literal.
37 */
38@Immutable
39public final class ObjectNode extends Expression implements LexicalContextNode, Splittable {
40    private static final long serialVersionUID = 1L;
41
42    /** Literal elements. */
43    private final List<PropertyNode> elements;
44
45    /** Ranges for splitting large literals over multiple compile units in codegen. */
46    private final List<Splittable.SplitRange> splitRanges;
47
48    /**
49     * Constructor
50     *
51     * @param token    token
52     * @param finish   finish
53     * @param elements the elements used to initialize this ObjectNode
54     */
55    public ObjectNode(final long token, final int finish, final List<PropertyNode> elements) {
56        super(token, finish);
57        this.elements = elements;
58        this.splitRanges = null;
59        assert elements instanceof RandomAccess : "Splitting requires random access lists";
60    }
61
62    private ObjectNode(final ObjectNode objectNode, final List<PropertyNode> elements,
63                       final List<Splittable.SplitRange> splitRanges ) {
64        super(objectNode);
65        this.elements = elements;
66        this.splitRanges = splitRanges;
67    }
68
69    @Override
70    public Node accept(final NodeVisitor<? extends LexicalContext> visitor) {
71        return Acceptor.accept(this, visitor);
72    }
73
74    @Override
75    public Node accept(final LexicalContext lc, final NodeVisitor<? extends LexicalContext> visitor) {
76        if (visitor.enterObjectNode(this)) {
77            return visitor.leaveObjectNode(setElements(lc, Node.accept(visitor, elements)));
78        }
79        return this;
80    }
81
82    @Override
83    public Type getType() {
84        return Type.OBJECT;
85    }
86
87    @Override
88    public void toString(final StringBuilder sb, final boolean printType) {
89        sb.append('{');
90
91        if (!elements.isEmpty()) {
92            sb.append(' ');
93
94            boolean first = true;
95            for (final Node element : elements) {
96                if (!first) {
97                    sb.append(", ");
98                }
99                first = false;
100
101                element.toString(sb, printType);
102            }
103            sb.append(' ');
104        }
105
106        sb.append('}');
107    }
108
109    /**
110     * Get the elements of this literal node
111     * @return a list of elements
112     */
113    public List<PropertyNode> getElements() {
114        return Collections.unmodifiableList(elements);
115    }
116
117    private ObjectNode setElements(final LexicalContext lc, final List<PropertyNode> elements) {
118        if (this.elements == elements) {
119            return this;
120        }
121        return Node.replaceInLexicalContext(lc, this, new ObjectNode(this, elements, this.splitRanges));
122    }
123
124    /**
125     * Set the split ranges for this ObjectNode
126     * @see Splittable.SplitRange
127     * @param lc the lexical context
128     * @param splitRanges list of split ranges
129     * @return new or changed object node
130     */
131    public ObjectNode setSplitRanges(final LexicalContext lc, final List<Splittable.SplitRange> splitRanges) {
132        if (this.splitRanges == splitRanges) {
133            return this;
134        }
135        return Node.replaceInLexicalContext(lc, this, new ObjectNode(this, elements, splitRanges));
136    }
137
138    /**
139     * Get the split ranges for this ObjectNode, or null if the object is not split.
140     * @see Splittable.SplitRange
141     * @return list of split ranges
142     */
143    @Override
144    public List<Splittable.SplitRange> getSplitRanges() {
145        return splitRanges == null ? null : Collections.unmodifiableList(splitRanges);
146    }
147
148}
149