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; | 5 import 'dart:collection' show HashMap, HashSet; |
6 import 'dart:math' show min, max; | 6 import 'dart:math' show min, max; |
7 | 7 |
8 import 'package:analyzer/analyzer.dart' hide ConstantEvaluator; | 8 import 'package:analyzer/analyzer.dart' hide ConstantEvaluator; |
9 import 'package:analyzer/dart/ast/ast.dart'; | 9 import 'package:analyzer/dart/ast/ast.dart'; |
10 import 'package:analyzer/dart/ast/token.dart' show Token, TokenType; | 10 import 'package:analyzer/dart/ast/token.dart' show Token, TokenType; |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 | 120 |
121 BuildUnit _buildUnit; | 121 BuildUnit _buildUnit; |
122 | 122 |
123 String _buildRoot; | 123 String _buildRoot; |
124 | 124 |
125 bool _superAllowed = true; | 125 bool _superAllowed = true; |
126 | 126 |
127 List<JS.TemporaryId> _superHelperSymbols = <JS.TemporaryId>[]; | 127 List<JS.TemporaryId> _superHelperSymbols = <JS.TemporaryId>[]; |
128 List<JS.Method> _superHelpers = <JS.Method>[]; | 128 List<JS.Method> _superHelpers = <JS.Method>[]; |
129 | 129 |
| 130 List<TypeParameterType> _typeParamInConst = null; |
| 131 |
130 /// Whether we are currently generating code for the body of a `JS()` call. | 132 /// Whether we are currently generating code for the body of a `JS()` call. |
131 bool _isInForeignJS = false; | 133 bool _isInForeignJS = false; |
132 | 134 |
133 CodeGenerator(AnalysisContext c, this.options, this._extensionTypes) | 135 CodeGenerator(AnalysisContext c, this.options, this._extensionTypes) |
134 : context = c, | 136 : context = c, |
135 types = c.typeProvider, | 137 types = c.typeProvider, |
136 _asyncStreamIterator = | 138 _asyncStreamIterator = |
137 _getLibrary(c, 'dart:async').getType('StreamIterator').type, | 139 _getLibrary(c, 'dart:async').getType('StreamIterator').type, |
138 _jsArray = _getLibrary(c, 'dart:_interceptors').getType('JSArray'), | 140 _jsArray = _getLibrary(c, 'dart:_interceptors').getType('JSArray'), |
139 dartCoreLibrary = _getLibrary(c, 'dart:core'), | 141 dartCoreLibrary = _getLibrary(c, 'dart:core'), |
(...skipping 2160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2300 if (name == '' || name == null || lowerTypedef) { | 2302 if (name == '' || name == null || lowerTypedef) { |
2301 // TODO(jmesserly): should we change how typedefs work? They currently | 2303 // TODO(jmesserly): should we change how typedefs work? They currently |
2302 // go through use similar logic as generic classes. This makes them | 2304 // go through use similar logic as generic classes. This makes them |
2303 // different from universal function types. | 2305 // different from universal function types. |
2304 var ft = type as FunctionType; | 2306 var ft = type as FunctionType; |
2305 var parts = _emitFunctionTypeParts(ft, lowerTypedef: lowerTypedef); | 2307 var parts = _emitFunctionTypeParts(ft, lowerTypedef: lowerTypedef); |
2306 return js.call('dart.functionType(#)', [parts]); | 2308 return js.call('dart.functionType(#)', [parts]); |
2307 } | 2309 } |
2308 | 2310 |
2309 if (type is TypeParameterType) { | 2311 if (type is TypeParameterType) { |
| 2312 _typeParamInConst?.add(type); |
2310 return new JS.Identifier(name); | 2313 return new JS.Identifier(name); |
2311 } | 2314 } |
2312 | 2315 |
2313 if (type == subClass?.type) { | 2316 if (type == subClass?.type) { |
2314 return className; | 2317 return className; |
2315 } | 2318 } |
2316 | 2319 |
2317 if (type is ParameterizedType) { | 2320 if (type is ParameterizedType) { |
2318 var args = type.typeArguments; | 2321 var args = type.typeArguments; |
2319 Iterable jsArgs = null; | 2322 Iterable jsArgs = null; |
(...skipping 1065 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3385 variable ??= new JS.TemporaryId(name); | 3388 variable ??= new JS.TemporaryId(name); |
3386 | 3389 |
3387 id.staticElement = new TemporaryVariableElement.forNode(id, variable); | 3390 id.staticElement = new TemporaryVariableElement.forNode(id, variable); |
3388 id.staticType = type; | 3391 id.staticType = type; |
3389 DynamicInvoke.set(id, type.isDynamic); | 3392 DynamicInvoke.set(id, type.isDynamic); |
3390 addTemporaryVariable(id.staticElement, nullable: nullable); | 3393 addTemporaryVariable(id.staticElement, nullable: nullable); |
3391 return id; | 3394 return id; |
3392 } | 3395 } |
3393 | 3396 |
3394 JS.Expression _emitConst(JS.Expression expr()) { | 3397 JS.Expression _emitConst(JS.Expression expr()) { |
3395 // TODO(jmesserly): emit the constants at top level if possible. | 3398 var savedTypeParams = _typeParamInConst; |
3396 // This wasn't quite working, so disabled for now. | 3399 _typeParamInConst = []; |
3397 return js.call('dart.const(#)', expr()); | 3400 |
| 3401 var jsExpr = js.call('dart.const(#)', expr()); |
| 3402 |
| 3403 bool usesTypeParams = _typeParamInConst.isNotEmpty; |
| 3404 _typeParamInConst = savedTypeParams; |
| 3405 |
| 3406 // TODO(jmesserly): if it uses type params we can still hoist it up as far |
| 3407 // as it will go, e.g. at the level the generic class is defined where type |
| 3408 // params are available. |
| 3409 if (_currentFunction == null || usesTypeParams) return jsExpr; |
| 3410 |
| 3411 var temp = new JS.TemporaryId('const'); |
| 3412 _moduleItems.add(js.statement('let #;', [temp])); |
| 3413 return js.call('# || (# = #)', [temp, temp, jsExpr]); |
3398 } | 3414 } |
3399 | 3415 |
3400 /// Returns a new expression, which can be be used safely *once* on the | 3416 /// Returns a new expression, which can be be used safely *once* on the |
3401 /// left hand side, and *once* on the right side of an assignment. | 3417 /// left hand side, and *once* on the right side of an assignment. |
3402 /// For example: `expr1[expr2] += y` can be compiled as | 3418 /// For example: `expr1[expr2] += y` can be compiled as |
3403 /// `expr1[expr2] = expr1[expr2] + y`. | 3419 /// `expr1[expr2] = expr1[expr2] + y`. |
3404 /// | 3420 /// |
3405 /// The temporary scope will ensure `expr1` and `expr2` are only evaluated | 3421 /// The temporary scope will ensure `expr1` and `expr2` are only evaluated |
3406 /// once: `((x1, x2) => x1[x2] = x1[x2] + y)(expr1, expr2)`. | 3422 /// once: `((x1, x2) => x1[x2] = x1[x2] + y)(expr1, expr2)`. |
3407 /// | 3423 /// |
(...skipping 1063 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4471 } | 4487 } |
4472 | 4488 |
4473 bool isLibraryPrefix(Expression node) => | 4489 bool isLibraryPrefix(Expression node) => |
4474 node is SimpleIdentifier && node.staticElement is PrefixElement; | 4490 node is SimpleIdentifier && node.staticElement is PrefixElement; |
4475 | 4491 |
4476 LibraryElement _getLibrary(AnalysisContext c, String uri) => | 4492 LibraryElement _getLibrary(AnalysisContext c, String uri) => |
4477 c.computeLibraryElement(c.sourceFactory.forUri(uri)); | 4493 c.computeLibraryElement(c.sourceFactory.forUri(uri)); |
4478 | 4494 |
4479 bool _isDartRuntime(LibraryElement l) => | 4495 bool _isDartRuntime(LibraryElement l) => |
4480 l.isInSdk && l.source.uri.toString() == 'dart:_runtime'; | 4496 l.isInSdk && l.source.uri.toString() == 'dart:_runtime'; |
OLD | NEW |