View Javadoc
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  
26  package jdk.nashorn.internal.ir;
27  
28  import static jdk.nashorn.internal.codegen.CompilerConstants.__DIR__;
29  import static jdk.nashorn.internal.codegen.CompilerConstants.__FILE__;
30  import static jdk.nashorn.internal.codegen.CompilerConstants.__LINE__;
31  
32  import jdk.nashorn.internal.codegen.types.Type;
33  import jdk.nashorn.internal.ir.annotations.Immutable;
34  import jdk.nashorn.internal.ir.visitor.NodeVisitor;
35  
36  /**
37   * IR representation for an identifier.
38   */
39  @Immutable
40  public final class IdentNode extends Expression implements PropertyKey, FunctionCall {
41      private static final int PROPERTY_NAME     = 1 << 0;
42      private static final int INITIALIZED_HERE  = 1 << 1;
43      private static final int FUNCTION          = 1 << 2;
44      private static final int FUTURESTRICT_NAME = 1 << 3;
45  
46      /** Identifier. */
47      private final String name;
48  
49      /** Type for a callsite, e.g. X in a get()X or a set(X)V */
50      private final Type callSiteType;
51  
52      private final int flags;
53  
54      /**
55       * Constructor
56       *
57       * @param token   token
58       * @param finish  finish position
59       * @param name    name of identifier
60       */
61      public IdentNode(final long token, final int finish, final String name) {
62          super(token, finish);
63          this.name = name.intern();
64          this.callSiteType = null;
65          this.flags = 0;
66      }
67  
68      private IdentNode(final IdentNode identNode, final String name, final Type callSiteType, final int flags) {
69          super(identNode);
70          this.name = name;
71          this.callSiteType = callSiteType;
72          this.flags = flags;
73      }
74  
75      /**
76       * Copy constructor - create a new IdentNode for the same location
77       *
78       * @param identNode  identNode
79       */
80      public IdentNode(final IdentNode identNode) {
81          super(identNode);
82          this.name         = identNode.getName();
83          this.callSiteType = null;
84          this.flags        = identNode.flags;
85      }
86  
87      @Override
88      public Type getType() {
89          return callSiteType == null ? super.getType() : callSiteType;
90      }
91  
92      @Override
93      public boolean isAtom() {
94          return true;
95      }
96  
97      private boolean hasCallSiteType() {
98          //this is an identity that's part of a getter or setter
99          return callSiteType != null;
100     }
101 
102     /**
103      * Assist in IR navigation.
104      *
105      * @param visitor IR navigating visitor.
106      */
107     @Override
108     public Node accept(final NodeVisitor<? extends LexicalContext> visitor) {
109         if (visitor.enterIdentNode(this)) {
110             return visitor.leaveIdentNode(this);
111         }
112 
113         return this;
114     }
115 
116     @Override
117     public void toString(final StringBuilder sb) {
118         if (hasCallSiteType()) {
119             sb.append('{');
120             final String desc = getType().getDescriptor();
121             sb.append(desc.charAt(desc.length() - 1) == ';' ? 'O' : getType().getDescriptor());
122             sb.append('}');
123         }
124 
125         sb.append(name);
126     }
127 
128     /**
129      * Get the name of the identifier
130      * @return  IdentNode name
131      */
132     public String getName() {
133         return name;
134     }
135 
136     @Override
137     public String getPropertyName() {
138         return getName();
139     }
140 
141     @Override
142     public boolean isLocal() {
143         return !getSymbol().isScope();
144     }
145 
146     /**
147      * Check if this IdentNode is a property name
148      * @return true if this is a property name
149      */
150     public boolean isPropertyName() {
151         return (flags & PROPERTY_NAME) != 0;
152     }
153 
154     /**
155      * Flag this IdentNode as a property name
156      * @return a node equivalent to this one except for the requested change.
157      */
158     public IdentNode setIsPropertyName() {
159         if (isPropertyName()) {
160             return this;
161         }
162         return new IdentNode(this, name, callSiteType, flags | PROPERTY_NAME);
163     }
164 
165     /**
166      * Check if this IdentNode is a future strict name
167      * @return true if this is a future strict name
168      */
169     public boolean isFutureStrictName() {
170         return (flags & FUTURESTRICT_NAME) != 0;
171     }
172 
173     /**
174      * Flag this IdentNode as a future strict name
175      * @return a node equivalent to this one except for the requested change.
176      */
177     public IdentNode setIsFutureStrictName() {
178         if (isFutureStrictName()) {
179             return this;
180         }
181         return new IdentNode(this, name, callSiteType, flags | FUTURESTRICT_NAME);
182     }
183 
184     /**
185      * Helper function for local def analysis.
186      * @return true if IdentNode is initialized on creation
187      */
188     public boolean isInitializedHere() {
189         return (flags & INITIALIZED_HERE) != 0;
190     }
191 
192     /**
193      * Flag IdentNode to be initialized on creation
194      * @return a node equivalent to this one except for the requested change.
195      */
196     public IdentNode setIsInitializedHere() {
197         if (isInitializedHere()) {
198             return this;
199         }
200         return new IdentNode(this, name, callSiteType, flags | INITIALIZED_HERE);
201     }
202 
203     /**
204      * Check if this IdentNode is a special identity, currently __DIR__, __FILE__
205      * or __LINE__
206      *
207      * @return true if this IdentNode is special
208      */
209     public boolean isSpecialIdentity() {
210         return name.equals(__DIR__.symbolName()) || name.equals(__FILE__.symbolName()) || name.equals(__LINE__.symbolName());
211     }
212 
213     @Override
214     public boolean isFunction() {
215         return (flags & FUNCTION) != 0;
216     }
217 
218     /**
219      * Mark this node as being the callee operand of a {@link CallNode}.
220      * @return an ident node identical to this one in all aspects except with its function flag set.
221      */
222     public IdentNode setIsFunction() {
223         if (isFunction()) {
224             return this;
225         }
226         return new IdentNode(this, name, callSiteType, flags | FUNCTION);
227     }
228 }