OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import 'dart:collection' show HashMap, HashSet, LinkedHashMap; | 5 import 'dart:collection' show HashMap, HashSet, LinkedHashMap; |
6 | 6 |
7 import 'package:analyzer/dart/element/element.dart'; | 7 import 'package:analyzer/dart/element/element.dart'; |
8 import 'package:analyzer/dart/element/type.dart'; | 8 import 'package:analyzer/dart/element/type.dart'; |
9 | 9 |
10 import '../js_ast/js_ast.dart' as JS; | 10 import '../js_ast/js_ast.dart' as JS; |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 JS.Identifier chooseTypeName(DartType type) { | 115 JS.Identifier chooseTypeName(DartType type) { |
116 return new JS.TemporaryId(_typeString(type)); | 116 return new JS.TemporaryId(_typeString(type)); |
117 } | 117 } |
118 } | 118 } |
119 | 119 |
120 /// _GeneratorTable tracks types which have been | 120 /// _GeneratorTable tracks types which have been |
121 /// named and hoisted. | 121 /// named and hoisted. |
122 class _GeneratorTable extends _CacheTable { | 122 class _GeneratorTable extends _CacheTable { |
123 final _defs = new HashMap<DartType, JS.Expression>(); | 123 final _defs = new HashMap<DartType, JS.Expression>(); |
124 | 124 |
| 125 final JS.Identifier _runtimeModule; |
| 126 |
| 127 _GeneratorTable(this._runtimeModule); |
| 128 |
125 JS.Statement _dischargeType(DartType t) { | 129 JS.Statement _dischargeType(DartType t) { |
126 var name = _names.remove(t); | 130 var name = _names.remove(t); |
127 if (name != null) { | 131 if (name != null) { |
128 JS.Expression init = _defs.remove(t); | 132 JS.Expression init = _defs.remove(t); |
129 assert(init != null); | 133 assert(init != null); |
130 return js.statement( | 134 return js.statement( |
131 'let # = () => ((# = dart.constFn(#))());', [name, name, init]); | 135 'let # = () => ((# = #.constFn(#))());', [name, name, _runtimeModule,
init]); |
132 } | 136 } |
133 return null; | 137 return null; |
134 } | 138 } |
135 | 139 |
136 /// If [type] does not already have a generator name chosen for it, | 140 /// If [type] does not already have a generator name chosen for it, |
137 /// assign it one, using [typeRep] as the initializer for it. | 141 /// assign it one, using [typeRep] as the initializer for it. |
138 /// Emit an expression which calls the generator name. | 142 /// Emit an expression which calls the generator name. |
139 JS.Expression nameType(DartType type, JS.Expression typeRep) { | 143 JS.Expression nameType(DartType type, JS.Expression typeRep) { |
140 var temp = _names[type]; | 144 var temp = _names[type]; |
141 if (temp == null) { | 145 if (temp == null) { |
142 _names[type] = temp = chooseTypeName(type); | 146 _names[type] = temp = chooseTypeName(type); |
143 _defs[type] = typeRep; | 147 _defs[type] = typeRep; |
144 } | 148 } |
145 return js.call('#()', [temp]); | 149 return js.call('#()', [temp]); |
146 } | 150 } |
147 } | 151 } |
148 | 152 |
149 class TypeTable { | 153 class TypeTable { |
150 /// Cache variable names for types emitted in place. | 154 /// Cache variable names for types emitted in place. |
151 final _cacheNames = new _CacheTable(); | 155 final _cacheNames = new _CacheTable(); |
152 | 156 |
153 /// Cache variable names for definite function types emitted in place. | 157 /// Cache variable names for definite function types emitted in place. |
154 final _definiteCacheNames = new _CacheTable(); | 158 final _definiteCacheNames = new _CacheTable(); |
155 | 159 |
156 /// Generator variable names for hoisted types. | 160 /// Generator variable names for hoisted types. |
157 final _generators = new _GeneratorTable(); | 161 final _GeneratorTable _generators; |
158 | 162 |
159 /// Generator variable names for hoisted definite function types. | 163 /// Generator variable names for hoisted definite function types. |
160 final _definiteGenerators = new _GeneratorTable(); | 164 final _GeneratorTable _definiteGenerators; |
161 | 165 |
162 /// Mapping from type parameters to the types which must have their | 166 /// Mapping from type parameters to the types which must have their |
163 /// cache/generator variables discharged at the binding site for the | 167 /// cache/generator variables discharged at the binding site for the |
164 /// type variable since the type definition depends on the type | 168 /// type variable since the type definition depends on the type |
165 /// parameter. | 169 /// parameter. |
166 final _scopeDependencies = | 170 final _scopeDependencies = |
167 new HashMap<TypeParameterElement, List<DartType>>(); | 171 new HashMap<TypeParameterElement, List<DartType>>(); |
168 | 172 |
| 173 TypeTable(JS.Identifier runtime) |
| 174 : _generators = new _GeneratorTable(runtime), |
| 175 _definiteGenerators = new _GeneratorTable(runtime); |
| 176 |
169 /// Emit a list of statements declaring the cache variables and generator | 177 /// Emit a list of statements declaring the cache variables and generator |
170 /// definitions tracked by the table. If [formals] is present, only | 178 /// definitions tracked by the table. If [formals] is present, only |
171 /// emit the definitions which depend on the formals. | 179 /// emit the definitions which depend on the formals. |
172 List<JS.Statement> discharge([List<TypeParameterElement> formals]) { | 180 List<JS.Statement> discharge([List<TypeParameterElement> formals]) { |
173 var filter = formals?.expand((p) => _scopeDependencies[p] ?? <DartType>[]); | 181 var filter = formals?.expand((p) => _scopeDependencies[p] ?? <DartType>[]); |
174 var stmts = [ | 182 var stmts = [ |
175 _cacheNames, | 183 _cacheNames, |
176 _definiteCacheNames, | 184 _definiteCacheNames, |
177 _generators, | 185 _generators, |
178 _definiteGenerators | 186 _definiteGenerators |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 assert(hoistType != null); | 237 assert(hoistType != null); |
230 var table = hoistType | 238 var table = hoistType |
231 ? (definite ? _definiteGenerators : _generators) | 239 ? (definite ? _definiteGenerators : _generators) |
232 : (definite ? _definiteCacheNames : _cacheNames); | 240 : (definite ? _definiteCacheNames : _cacheNames); |
233 if (!table.isNamed(type)) { | 241 if (!table.isNamed(type)) { |
234 if (recordScopeDependencies(type)) return typeRep; | 242 if (recordScopeDependencies(type)) return typeRep; |
235 } | 243 } |
236 return table.nameType(type, typeRep); | 244 return table.nameType(type, typeRep); |
237 } | 245 } |
238 } | 246 } |
OLD | NEW |