ObjectNode.java revision 1068:34ef988d5959
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.function.Function;
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 {
40    private static final long serialVersionUID = 1L;
41
42    /** Literal elements. */
43    private final List<PropertyNode> elements;
44
45    /**
46     * Constructor
47     *
48     * @param token    token
49     * @param finish   finish
50     * @param elements the elements used to initialize this ObjectNode
51     */
52    public ObjectNode(final long token, final int finish, final List<PropertyNode> elements) {
53        super(token, finish);
54        this.elements = elements;
55    }
56
57    private ObjectNode(final ObjectNode objectNode, final List<PropertyNode> elements) {
58        super(objectNode);
59        this.elements = elements;
60    }
61
62    @Override
63    public Node accept(final NodeVisitor<? extends LexicalContext> visitor) {
64        if (visitor.enterObjectNode(this)) {
65            return visitor.leaveObjectNode(setElements(Node.accept(visitor, elements)));
66        }
67
68        return this;
69    }
70
71    @Override
72    public Type getType(final Function<Symbol, Type> localVariableTypes) {
73        return Type.OBJECT;
74    }
75
76    @Override
77    public void toString(final StringBuilder sb, final boolean printType) {
78        sb.append('{');
79
80        if (!elements.isEmpty()) {
81            sb.append(' ');
82
83            boolean first = true;
84            for (final Node element : elements) {
85                if (!first) {
86                    sb.append(", ");
87                }
88                first = false;
89
90                element.toString(sb, printType);
91            }
92            sb.append(' ');
93        }
94
95        sb.append('}');
96    }
97
98    /**
99     * Get the elements of this literal node
100     * @return a list of elements
101     */
102    public List<PropertyNode> getElements() {
103        return Collections.unmodifiableList(elements);
104    }
105
106    private ObjectNode setElements(final List<PropertyNode> elements) {
107        if (this.elements == elements) {
108            return this;
109        }
110        return new ObjectNode(this, elements);
111    }
112}
113