Debian Codegraph @

rhino/src/org/mozilla/javascript/ast/Name.java

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 package org.mozilla.javascript.ast;
8
9 import org.mozilla.javascript.Token;
10
11 /**
12 * AST node for a simple name. A simple name is an identifier that is
13 * not a keyword. Node type is {@link Token#NAME}.<p>
14 *
15 * This node type is also used to represent certain non-identifier names that
16 * are part of the language syntax. It's used for the "get" and "set"
17 * pseudo-keywords for object-initializer getter/setter properties, and it's
18 * also used for the "*" wildcard in E4X XML namespace and name expressions.
19 */
20 public class Name extends AstNode {
21
22 private String identifier;
23 private Scope scope;
24
25 {
26 type = Token.NAME;
27 }
28
29 public Name() {
30 }
31
32 public Name(int pos) {
33 super(pos);
34 }
35
36 public Name(int pos, int len) {
37 super(pos, len);
38 }
39
40 /**
41 * Constructs a new {@link Name}
42 * @param pos node start position
43 * @param len node length
44 * @param name the identifier associated with this {@code Name} node
45 */
46 public Name(int pos, int len, String name) {
47 super(pos, len);
48 setIdentifier(name);
49 }
50
51 public Name(int pos, String name) {
52 super(pos);
53 setIdentifier(name);
54 setLength(name.length());
55 }
56
57 /**
58 * Returns the node's identifier
59 */
60 public String getIdentifier() {
61 return identifier;
62 }
63
64 /**
65 * Sets the node's identifier
66 * @throws IllegalArgumentException if identifier is null
67 */
68 public void setIdentifier(String identifier) {
69 assertNotNull(identifier);
70 this.identifier = identifier;
71 setLength(identifier.length());
72 }
73
74 /**
75 * Set the {@link Scope} associated with this node. This method does not
76 * set the scope's ast-node field to this node. The field exists only
77 * for temporary storage by the code generator. Not every name has an
78 * associated scope - typically only function and variable names (but not
79 * property names) are registered in a scope.
80 *
81 * @param s the scope. Can be null. Doesn't set any fields in the
82 * scope.
83 */
84 @Override
85 public void setScope(Scope s) {
86 scope = s;
87 }
88
89 /**
90 * Return the {@link Scope} associated with this node. This is
91 * <em>only</em> used for (and set by) the code generator, so it will always
92 * be null in frontend AST-processing code. Use {@link #getDefiningScope}
93 * to find the lexical {@code Scope} in which this {@code Name} is defined,
94 * if any.
95 */
96 @Override
97 public Scope getScope() {
98 return scope;
99 }
100
101 /**
102 * Returns the {@link Scope} in which this {@code Name} is defined.
103 * @return the scope in which this name is defined, or {@code null}
104 * if it's not defined in the current lexical scope chain
105 */
106 public Scope getDefiningScope() {
107 Scope enclosing = getEnclosingScope();
108 String name = getIdentifier();
109 return enclosing == null ? null : enclosing.getDefiningScope(name);
110 }
111
112 /**
113 * Return true if this node is known to be defined as a symbol in a
114 * lexical scope other than the top-level (global) scope.
115 *
116 * @return {@code true} if this name appears as local variable, a let-bound
117 * variable not in the global scope, a function parameter, a loop
118 * variable, the property named in a {@link PropertyGet}, or in any other
119 * context where the node is known not to resolve to the global scope.
120 * Returns {@code false} if the node is defined in the top-level scope
121 * (i.e., its defining scope is an {@link AstRoot} object), or if its
122 * name is not defined as a symbol in the symbol table, in which case it
123 * may be an external or built-in name (or just an error of some sort.)
124 */
125 public boolean isLocalName() {
126 Scope scope = getDefiningScope();
127 return scope != null && scope.getParentScope() != null;
128 }
129
130 /**
131 * Return the length of this node's identifier, to let you pretend
132 * it's a {@link String}. Don't confuse this method with the
133 * {@link AstNode#getLength} method, which returns the range of
134 * characters that this node overlaps in the source input.
135 */
136 public int length() {
137 return identifier == null ? 0 : identifier.length();
138 }
139
140 @Override
141 public String toSource(int depth) {
142 return makeIndent(depth) + (identifier == null ? "<null>" : identifier);
143 }
144
145 /**
146 * Visits this node. There are no children to visit.
147 */
148 @Override
149 public void visit(NodeVisitor v) {
150 v.visit(this);
151 }
152 }