src/jdk/nashorn/internal/ir/IndexNode.java

Fri, 19 Apr 2013 16:11:16 +0200

author
lagergren
date
Fri, 19 Apr 2013 16:11:16 +0200
changeset 211
3a209cbd1d8f
parent 144
4be452026847
child 252
544e17632e96
permissions
-rw-r--r--

8010701: Immutable nodes - final iteration
Reviewed-by: sundar, hannesw, jlaskey

     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  */
    26 package jdk.nashorn.internal.ir;
    28 import jdk.nashorn.internal.codegen.types.Type;
    29 import jdk.nashorn.internal.ir.annotations.Immutable;
    30 import jdk.nashorn.internal.ir.visitor.NodeVisitor;
    31 import jdk.nashorn.internal.runtime.Source;
    33 /**
    34  * IR representation of an indexed access (brackets operator.)
    35  */
    36 @Immutable
    37 public final class IndexNode extends BaseNode {
    38     /** Property index. */
    39     private final Node index;
    41     /**
    42      * Constructors
    43      *
    44      * @param source  the source
    45      * @param token   token
    46      * @param finish  finish
    47      * @param base    base node for access
    48      * @param index   index for access
    49      */
    50     public IndexNode(final Source source, final long token, final int finish, final Node base, final Node index) {
    51         super(source, token, finish, base, false, false);
    52         this.index = index;
    53     }
    55     private IndexNode(final IndexNode indexNode, final Node base, final Node index, final boolean isFunction, final boolean hasCallSiteType) {
    56         super(indexNode, base, isFunction, hasCallSiteType);
    57         this.index = index;
    58     }
    60     @Override
    61     public Node accept(final NodeVisitor visitor) {
    62         if (visitor.enterIndexNode(this)) {
    63             final Node      newBase  = base.accept(visitor);
    64             final Node      newIndex = index.accept(visitor);
    65             final IndexNode newNode;
    66             if (newBase != base || newIndex != index) {
    67                 newNode = new IndexNode(this, newBase, newIndex, isFunction(), hasCallSiteType());
    68             } else {
    69                 newNode = this;
    70             }
    71             return visitor.leaveIndexNode(newNode);
    72         }
    74         return this;
    75     }
    77     @Override
    78     public void toString(final StringBuilder sb) {
    79         final boolean needsParen = tokenType().needsParens(base.tokenType(), true);
    81         if (hasCallSiteType()) {
    82             sb.append('{');
    83             final String desc = getType().getDescriptor();
    84             sb.append(desc.charAt(desc.length() - 1) == ';' ? "O" : getType().getDescriptor());
    85             sb.append('}');
    86         }
    88         if (needsParen) {
    89             sb.append('(');
    90         }
    92         base.toString(sb);
    94         if (needsParen) {
    95             sb.append(')');
    96         }
    98         sb.append('[');
    99         index.toString(sb);
   100         sb.append(']');
   101     }
   103     /**
   104      * Get the index expression for this IndexNode
   105      * @return the index
   106      */
   107     public Node getIndex() {
   108         return index;
   109     }
   111     @Override
   112     public BaseNode setIsFunction() {
   113         if (isFunction()) {
   114             return this;
   115         }
   116         return new IndexNode(this, base, index, true, hasCallSiteType());
   117     }
   119     @Override
   120     public IndexNode setType(final Type type) {
   121         logTypeChange(type);
   122         getSymbol().setTypeOverride(type); //always a temp so this is fine.
   123         return new IndexNode(this, base, index, isFunction(), true);
   124     }
   126 }

mercurial