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

changeset 211
3a209cbd1d8f
parent 144
4be452026847
child 252
544e17632e96
     1.1 --- a/src/jdk/nashorn/internal/ir/IndexNode.java	Fri Apr 19 18:23:00 2013 +0530
     1.2 +++ b/src/jdk/nashorn/internal/ir/IndexNode.java	Fri Apr 19 16:11:16 2013 +0200
     1.3 @@ -25,22 +25,18 @@
     1.4  
     1.5  package jdk.nashorn.internal.ir;
     1.6  
     1.7 -import static jdk.nashorn.internal.codegen.ObjectClassGenerator.DEBUG_FIELDS;
     1.8 -
     1.9 -import jdk.nashorn.internal.codegen.ObjectClassGenerator;
    1.10  import jdk.nashorn.internal.codegen.types.Type;
    1.11 +import jdk.nashorn.internal.ir.annotations.Immutable;
    1.12  import jdk.nashorn.internal.ir.visitor.NodeVisitor;
    1.13  import jdk.nashorn.internal.runtime.Source;
    1.14  
    1.15  /**
    1.16   * IR representation of an indexed access (brackets operator.)
    1.17 - *
    1.18   */
    1.19 -public class IndexNode extends BaseNode implements TypeOverride<IndexNode> {
    1.20 -    /** Property ident. */
    1.21 -    private Node index;
    1.22 -
    1.23 -    private boolean hasCallSiteType;
    1.24 +@Immutable
    1.25 +public final class IndexNode extends BaseNode {
    1.26 +    /** Property index. */
    1.27 +    private final Node index;
    1.28  
    1.29      /**
    1.30       * Constructors
    1.31 @@ -52,50 +48,27 @@
    1.32       * @param index   index for access
    1.33       */
    1.34      public IndexNode(final Source source, final long token, final int finish, final Node base, final Node index) {
    1.35 -        super(source, token, finish, base);
    1.36 -
    1.37 +        super(source, token, finish, base, false, false);
    1.38          this.index = index;
    1.39      }
    1.40  
    1.41 -    /**
    1.42 -     * Copy constructor
    1.43 -     *
    1.44 -     * @param indexNode source node
    1.45 -     */
    1.46 -    public IndexNode(final IndexNode indexNode) {
    1.47 -        this(indexNode, new CopyState());
    1.48 -    }
    1.49 -
    1.50 -    private IndexNode(final IndexNode indexNode, final CopyState cs) {
    1.51 -        super(indexNode, cs);
    1.52 -
    1.53 -        index = cs.existingOrCopy(indexNode.index);
    1.54 -    }
    1.55 -
    1.56 -    @Override
    1.57 -    protected Node copy(final CopyState cs) {
    1.58 -        return new IndexNode(this, cs);
    1.59 -    }
    1.60 -
    1.61 -    @Override
    1.62 -    public boolean equals(final Object other) {
    1.63 -        if (!super.equals(other)) {
    1.64 -            return false;
    1.65 -        }
    1.66 -        return index.equals(((IndexNode)other).getIndex());
    1.67 -    }
    1.68 -
    1.69 -    @Override
    1.70 -    public int hashCode() {
    1.71 -        return super.hashCode() ^ getIndex().hashCode();
    1.72 +    private IndexNode(final IndexNode indexNode, final Node base, final Node index, final boolean isFunction, final boolean hasCallSiteType) {
    1.73 +        super(indexNode, base, isFunction, hasCallSiteType);
    1.74 +        this.index = index;
    1.75      }
    1.76  
    1.77      @Override
    1.78      public Node accept(final NodeVisitor visitor) {
    1.79 -        if (visitor.enterIndexNode(this) != null) {
    1.80 -            base = base.accept(visitor);
    1.81 -            index = index.accept(visitor);
    1.82 -            return visitor.leaveIndexNode(this);
    1.83 +        if (visitor.enterIndexNode(this)) {
    1.84 +            final Node      newBase  = base.accept(visitor);
    1.85 +            final Node      newIndex = index.accept(visitor);
    1.86 +            final IndexNode newNode;
    1.87 +            if (newBase != base || newIndex != index) {
    1.88 +                newNode = new IndexNode(this, newBase, newIndex, isFunction(), hasCallSiteType());
    1.89 +            } else {
    1.90 +                newNode = this;
    1.91 +            }
    1.92 +            return visitor.leaveIndexNode(newNode);
    1.93          }
    1.94  
    1.95          return this;
    1.96 @@ -105,7 +78,7 @@
    1.97      public void toString(final StringBuilder sb) {
    1.98          final boolean needsParen = tokenType().needsParens(base.tokenType(), true);
    1.99  
   1.100 -        if (hasCallSiteType) {
   1.101 +        if (hasCallSiteType()) {
   1.102              sb.append('{');
   1.103              final String desc = getType().getDescriptor();
   1.104              sb.append(desc.charAt(desc.length() - 1) == ';' ? "O" : getType().getDescriptor());
   1.105 @@ -135,27 +108,19 @@
   1.106          return index;
   1.107      }
   1.108  
   1.109 -    /**
   1.110 -     * Reset the index expression for this IndexNode
   1.111 -     * @param index a new index expression
   1.112 -     */
   1.113 -    public void setIndex(final Node index) {
   1.114 -        this.index = index;
   1.115 +    @Override
   1.116 +    public BaseNode setIsFunction() {
   1.117 +        if (isFunction()) {
   1.118 +            return this;
   1.119 +        }
   1.120 +        return new IndexNode(this, base, index, true, hasCallSiteType());
   1.121      }
   1.122  
   1.123      @Override
   1.124      public IndexNode setType(final Type type) {
   1.125 -        if (DEBUG_FIELDS && !Type.areEquivalent(getSymbol().getSymbolType(), type)) {
   1.126 -            ObjectClassGenerator.LOG.info(getClass().getName() + " " + this + " => " + type + " instead of " + getType());
   1.127 -        }
   1.128 -        hasCallSiteType = true;
   1.129 -        getSymbol().setTypeOverride(type);
   1.130 -        return this;
   1.131 -    }
   1.132 -
   1.133 -    @Override
   1.134 -    public boolean canHaveCallSiteType() {
   1.135 -        return true; //carried by the symbol and always the same nodetype==symboltype
   1.136 +        logTypeChange(type);
   1.137 +        getSymbol().setTypeOverride(type); //always a temp so this is fine.
   1.138 +        return new IndexNode(this, base, index, isFunction(), true);
   1.139      }
   1.140  
   1.141  }

mercurial