Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(134)

Side by Side Diff: pkg/dev_compiler/lib/src/compiler/type_utilities.dart

Issue 2954523002: fix #27259, implement covariance checking for strong mode and DDC (Closed)
Patch Set: format Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 var temp = _names[type]; 144 var temp = _names[type];
145 if (temp == null) { 145 if (temp == null) {
146 _names[type] = temp = chooseTypeName(type); 146 _names[type] = temp = chooseTypeName(type);
147 _defs[type] = typeRep; 147 _defs[type] = typeRep;
148 } 148 }
149 return js.call('#()', [temp]); 149 return js.call('#()', [temp]);
150 } 150 }
151 } 151 }
152 152
153 class TypeTable { 153 class TypeTable {
154 /// Cache variable names for types emitted in place.
155 final _cacheNames = new _CacheTable();
156
157 /// Cache variable names for definite function types emitted in place.
158 final _definiteCacheNames = new _CacheTable();
159
160 /// Generator variable names for hoisted types. 154 /// Generator variable names for hoisted types.
161 final _GeneratorTable _generators; 155 final _GeneratorTable _generators;
162 156
163 /// Generator variable names for hoisted definite function types. 157 /// Generator variable names for hoisted definite function types.
164 final _GeneratorTable _definiteGenerators; 158 final _GeneratorTable _definiteGenerators;
165 159
166 /// Mapping from type parameters to the types which must have their 160 /// Mapping from type parameters to the types which must have their
167 /// cache/generator variables discharged at the binding site for the 161 /// cache/generator variables discharged at the binding site for the
168 /// type variable since the type definition depends on the type 162 /// type variable since the type definition depends on the type
169 /// parameter. 163 /// parameter.
170 final _scopeDependencies = 164 final _scopeDependencies =
171 new HashMap<TypeParameterElement, List<DartType>>(); 165 new HashMap<TypeParameterElement, List<DartType>>();
172 166
173 TypeTable(JS.Identifier runtime) 167 TypeTable(JS.Identifier runtime)
174 : _generators = new _GeneratorTable(runtime), 168 : _generators = new _GeneratorTable(runtime),
175 _definiteGenerators = new _GeneratorTable(runtime); 169 _definiteGenerators = new _GeneratorTable(runtime);
176 170
177 /// Emit a list of statements declaring the cache variables and generator 171 /// Emit a list of statements declaring the cache variables and generator
178 /// definitions tracked by the table. If [formals] is present, only 172 /// definitions tracked by the table. If [formals] is present, only
179 /// emit the definitions which depend on the formals. 173 /// emit the definitions which depend on the formals.
180 List<JS.Statement> discharge([List<TypeParameterElement> formals]) { 174 List<JS.Statement> discharge([List<TypeParameterElement> formals]) {
181 var filter = formals?.expand((p) => _scopeDependencies[p] ?? <DartType>[]); 175 var filter = formals?.expand((p) => _scopeDependencies[p] ?? <DartType>[]);
182 var stmts = [ 176 var stmts = [_generators, _definiteGenerators]
183 _cacheNames, 177 .expand((c) => c.discharge(filter))
184 _definiteCacheNames, 178 .toList();
185 _generators,
186 _definiteGenerators
187 ].expand((c) => c.discharge(filter)).toList();
188 formals?.forEach(_scopeDependencies.remove); 179 formals?.forEach(_scopeDependencies.remove);
189 return stmts; 180 return stmts;
190 } 181 }
191 182
192 /// Record the dependencies of the type on its free variables 183 /// Record the dependencies of the type on its free variables
193 bool recordScopeDependencies(DartType type) { 184 bool recordScopeDependencies(DartType type) {
194 var fvs = freeTypeParameters(type); 185 var fvs = freeTypeParameters(type);
195 // TODO(leafp): This is a hack to avoid trying to hoist out of 186 // TODO(leafp): This is a hack to avoid trying to hoist out of
196 // generic functions and generic function types. This often degrades 187 // generic functions and generic function types. This often degrades
197 // readability to little or no benefit. It would be good to do this 188 // readability to little or no benefit. It would be good to do this
(...skipping 28 matching lines...) Expand all
226 /// initializer expression will be emitted in place. The generated code 217 /// initializer expression will be emitted in place. The generated code
227 /// for dart.is(x, type) in this case ends up as: 218 /// for dart.is(x, type) in this case ends up as:
228 /// let generator = () => (generator = dart.constFn(type))() 219 /// let generator = () => (generator = dart.constFn(type))()
229 /// .... 220 /// ....
230 /// dart.is(x, generator()) 221 /// dart.is(x, generator())
231 /// 222 ///
232 /// The boolean parameter [definite] distinguishes between definite function 223 /// The boolean parameter [definite] distinguishes between definite function
233 /// types and other types (since the same DartType may have different 224 /// types and other types (since the same DartType may have different
234 /// representations as definite and indefinite function types). 225 /// representations as definite and indefinite function types).
235 JS.Expression nameType(DartType type, JS.Expression typeRep, 226 JS.Expression nameType(DartType type, JS.Expression typeRep,
236 {bool hoistType, bool definite: false}) { 227 {bool definite: false}) {
237 assert(hoistType != null); 228 var table = definite ? _definiteGenerators : _generators;
238 var table = hoistType
239 ? (definite ? _definiteGenerators : _generators)
240 : (definite ? _definiteCacheNames : _cacheNames);
241 if (!table.isNamed(type)) { 229 if (!table.isNamed(type)) {
242 if (recordScopeDependencies(type)) return typeRep; 230 if (recordScopeDependencies(type)) return typeRep;
243 } 231 }
244 return table.nameType(type, typeRep); 232 return table.nameType(type, typeRep);
245 } 233 }
246 } 234 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698