OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 part of js_backend; | |
6 | |
7 abstract class _NamerName extends jsAst.Name { | |
8 int get _kind; | |
9 | |
10 toString() => throw new UnsupportedError("Cannot convert a name to a string"); | |
11 } | |
12 | |
13 enum _NamerNameKinds { | |
14 StringBacked, | |
15 Getter, | |
16 Setter, | |
17 Async, | |
18 Compound, | |
19 Token | |
20 } | |
21 | |
22 class StringBackedName extends _NamerName { | |
23 final String name; | |
24 int get _kind => _NamerNameKinds.StringBacked.index; | |
25 | |
26 StringBackedName(this.name); | |
27 | |
28 operator==(other) { | |
29 if (identical(this, other)) return true; | |
30 return (other is StringBackedName) && other.name == name; | |
31 } | |
32 | |
33 int get hashCode => name.hashCode; | |
34 | |
35 int compareTo(_NamerName other) { | |
36 if (other._kind != _kind) return other._kind - _kind; | |
37 return name.compareTo(other.name); | |
38 } | |
39 } | |
40 | |
41 abstract class _PrefixedName extends _NamerName implements jsAst.AstContainer { | |
42 final jsAst.Name prefix; | |
43 final jsAst.Name base; | |
44 int get _kind; | |
45 | |
46 Iterable<jsAst.Node> get containedNodes => [prefix, base]; | |
47 | |
48 _PrefixedName(this.prefix, this.base); | |
49 | |
50 String get name => prefix.name + base.name; | |
51 | |
52 bool operator==(other) { | |
53 if (identical(this, other)) return true; | |
54 if (other is! _PrefixedName) return false; | |
55 return other.base == base && other.prefix == prefix; | |
56 } | |
57 | |
58 int get hashCode => base.hashCode * 13 + prefix.hashCode; | |
59 | |
60 int compareTo(_NamerName other) { | |
61 if (other._kind != _kind) return other._kind - _kind; | |
62 _PrefixedName otherSameKind = other; | |
63 int result = prefix.compareTo(otherSameKind.prefix); | |
64 if (result == 0) { | |
65 result = prefix.compareTo(otherSameKind.prefix); | |
66 if (result == 0) { | |
67 result = base.compareTo(otherSameKind.base); | |
68 } | |
69 } | |
70 return result; | |
71 } | |
72 } | |
73 | |
74 class GetterName extends _PrefixedName { | |
75 int get _kind => _NamerNameKinds.Getter.index; | |
76 | |
77 GetterName(jsAst.Name prefix, jsAst.Name base) : super(prefix, base); | |
78 } | |
79 | |
80 class SetterName extends _PrefixedName { | |
81 int get _kind => _NamerNameKinds.Setter.index; | |
82 | |
83 SetterName(jsAst.Name prefix, jsAst.Name base) : super(prefix, base); | |
84 } | |
85 | |
86 class _AsyncName extends _PrefixedName { | |
87 int get _kind => _NamerNameKinds.Async.index; | |
88 | |
89 _AsyncName(jsAst.Name prefix, jsAst.Name base) : super(prefix, base); | |
90 | |
91 @override | |
92 bool get allowRename => true; | |
93 } | |
94 | |
95 class CompoundName extends _NamerName implements jsAst.AstContainer { | |
96 final List<_NamerName> _parts; | |
97 int get _kind => _NamerNameKinds.Compound.index; | |
98 String _cachedName; | |
99 int _cachedHashCode = -1; | |
100 | |
101 Iterable<jsAst.Node> get containedNodes => _parts; | |
102 | |
103 CompoundName(this._parts); | |
104 | |
105 String get name { | |
106 if (_cachedName == null) { | |
107 _cachedName = _parts.map((jsAst.Name name) => name.name).join(); | |
108 } | |
109 return _cachedName; | |
110 } | |
111 | |
112 bool operator==(other) { | |
113 if (identical(this, other)) return true; | |
114 if (other is! CompoundName) return false; | |
115 if (other._parts.length != _parts.length) return false; | |
116 for (int i = 0; i < _parts.length; ++i) { | |
117 if (other._parts[i] != _parts[i]) return false; | |
118 } | |
119 return true; | |
120 } | |
121 | |
122 int get hashCode { | |
123 if (_cachedHashCode < 0) { | |
124 _cachedHashCode = 0; | |
125 for (jsAst.Name name in _parts) { | |
126 _cachedHashCode = (_cachedHashCode * 17 + name.hashCode) & 0x7fffffff; | |
127 } | |
128 } | |
129 return _cachedHashCode; | |
130 } | |
131 | |
132 int compareTo(_NamerName other) { | |
133 if (other._kind != _kind) return other._kind - _kind; | |
134 CompoundName otherSameKind = other; | |
135 if (otherSameKind._parts.length != _parts.length) { | |
136 return otherSameKind._parts.length - _parts.length; | |
137 } | |
138 int result = 0; | |
139 for (int pos = 0; result == 0 && pos < _parts.length; pos++) { | |
140 result = _parts[pos].compareTo(otherSameKind._parts[pos]); | |
141 } | |
142 return result; | |
143 } | |
144 } | |
145 | |
146 class TokenName extends _NamerName implements jsAst.ReferenceCountedAstNode { | |
147 int get _kind => _NamerNameKinds.Token.index; | |
148 String _name; | |
149 String _proposed; | |
150 TokenScope _scope; | |
sra1
2015/07/01 04:11:09
Can _scope, _proposed be final?
herhut
2015/07/02 08:54:51
Yes they can! Thanks.
| |
151 int _rc = 0; | |
152 | |
153 TokenName(this._scope, this._proposed); | |
154 | |
155 bool get isFinalized => _name != null; | |
156 | |
157 String get name { | |
158 assert(isFinalized); | |
159 return _name; | |
160 } | |
161 | |
162 @override | |
163 int compareTo(_NamerName other) { | |
164 if (other._kind != _kind) return other._kind - _kind; | |
165 TokenName otherToken = other; | |
166 return _proposed.compareTo(otherToken._proposed); | |
167 } | |
168 | |
169 markSeen(jsAst.TokenCounter counter) => _rc++; | |
170 | |
171 finalize() { | |
172 assert(!isFinalized); | |
173 _name = _scope.getNextName(); | |
174 } | |
175 } | |
OLD | NEW |