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