UNPKG

4.77 kBJavaScriptView Raw
1/*
2 Copyright (C) 2015 Yusuke Suzuki <utatane.tea@gmail.com>
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
6
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12
13 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
17 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23*/
24
25const READ = 0x1;
26const WRITE = 0x2;
27const RW = READ | WRITE;
28
29/**
30 * A Reference represents a single occurrence of an identifier in code.
31 * @constructor Reference
32 */
33class Reference {
34 constructor(ident, scope, flag, writeExpr, maybeImplicitGlobal, partial, init) {
35
36 /**
37 * Identifier syntax node.
38 * @member {espreeIdentifier} Reference#identifier
39 */
40 this.identifier = ident;
41
42 /**
43 * Reference to the enclosing Scope.
44 * @member {Scope} Reference#from
45 */
46 this.from = scope;
47
48 /**
49 * Whether the reference comes from a dynamic scope (such as 'eval',
50 * 'with', etc.), and may be trapped by dynamic scopes.
51 * @member {boolean} Reference#tainted
52 */
53 this.tainted = false;
54
55 /**
56 * The variable this reference is resolved with.
57 * @member {Variable} Reference#resolved
58 */
59 this.resolved = null;
60
61 /**
62 * The read-write mode of the reference. (Value is one of {@link
63 * Reference.READ}, {@link Reference.RW}, {@link Reference.WRITE}).
64 * @member {number} Reference#flag
65 * @private
66 */
67 this.flag = flag;
68 if (this.isWrite()) {
69
70 /**
71 * If reference is writeable, this is the tree being written to it.
72 * @member {espreeNode} Reference#writeExpr
73 */
74 this.writeExpr = writeExpr;
75
76 /**
77 * Whether the Reference might refer to a partial value of writeExpr.
78 * @member {boolean} Reference#partial
79 */
80 this.partial = partial;
81
82 /**
83 * Whether the Reference is to write of initialization.
84 * @member {boolean} Reference#init
85 */
86 this.init = init;
87 }
88 this.__maybeImplicitGlobal = maybeImplicitGlobal;
89 }
90
91 /**
92 * Whether the reference is static.
93 * @function Reference#isStatic
94 * @returns {boolean} static
95 */
96 isStatic() {
97 return !this.tainted && this.resolved && this.resolved.scope.isStatic();
98 }
99
100 /**
101 * Whether the reference is writeable.
102 * @function Reference#isWrite
103 * @returns {boolean} write
104 */
105 isWrite() {
106 return !!(this.flag & Reference.WRITE);
107 }
108
109 /**
110 * Whether the reference is readable.
111 * @function Reference#isRead
112 * @returns {boolean} read
113 */
114 isRead() {
115 return !!(this.flag & Reference.READ);
116 }
117
118 /**
119 * Whether the reference is read-only.
120 * @function Reference#isReadOnly
121 * @returns {boolean} read only
122 */
123 isReadOnly() {
124 return this.flag === Reference.READ;
125 }
126
127 /**
128 * Whether the reference is write-only.
129 * @function Reference#isWriteOnly
130 * @returns {boolean} write only
131 */
132 isWriteOnly() {
133 return this.flag === Reference.WRITE;
134 }
135
136 /**
137 * Whether the reference is read-write.
138 * @function Reference#isReadWrite
139 * @returns {boolean} read write
140 */
141 isReadWrite() {
142 return this.flag === Reference.RW;
143 }
144}
145
146/**
147 * @constant Reference.READ
148 * @private
149 */
150Reference.READ = READ;
151
152/**
153 * @constant Reference.WRITE
154 * @private
155 */
156Reference.WRITE = WRITE;
157
158/**
159 * @constant Reference.RW
160 * @private
161 */
162Reference.RW = RW;
163
164export default Reference;
165
166/* vim: set sw=4 ts=4 et tw=80 : */