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 1118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1129 void _emitClassSignature( | 1129 void _emitClassSignature( |
1130 List<MethodDeclaration> methods, | 1130 List<MethodDeclaration> methods, |
1131 ClassElement classElem, | 1131 ClassElement classElem, |
1132 List<ConstructorDeclaration> ctors, | 1132 List<ConstructorDeclaration> ctors, |
1133 List<ExecutableElement> extensions, | 1133 List<ExecutableElement> extensions, |
1134 JS.Expression className, | 1134 JS.Expression className, |
1135 List<JS.Statement> body) { | 1135 List<JS.Statement> body) { |
1136 if (classElem.interfaces.isNotEmpty) { | 1136 if (classElem.interfaces.isNotEmpty) { |
1137 body.add(js.statement('#[dart.implements] = () => #;', [ | 1137 body.add(js.statement('#[dart.implements] = () => #;', [ |
1138 className, | 1138 className, |
1139 new JS.ArrayInitializer(new List<JS.Expression>.from( | 1139 new JS.ArrayInitializer( |
1140 classElem.interfaces.map(_emitType))) | 1140 new List<JS.Expression>.from(classElem.interfaces.map(_emitType))) |
1141 ])); | 1141 ])); |
1142 } | 1142 } |
1143 | 1143 |
1144 var tStatics = <JS.Property>[]; | 1144 var tStatics = <JS.Property>[]; |
1145 var tMethods = <JS.Property>[]; | 1145 var tMethods = <JS.Property>[]; |
1146 var sNames = <JS.Expression>[]; | 1146 var sNames = <JS.Expression>[]; |
1147 for (MethodDeclaration node in methods) { | 1147 for (MethodDeclaration node in methods) { |
1148 if (!(node.isSetter || node.isGetter || node.isAbstract)) { | 1148 if (!(node.isSetter || node.isGetter || node.isAbstract)) { |
1149 var name = node.name.name; | 1149 var name = node.name.name; |
1150 var element = node.element; | 1150 var element = node.element; |
(...skipping 450 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1601 } else if (param.kind == ParameterKind.POSITIONAL) { | 1601 } else if (param.kind == ParameterKind.POSITIONAL) { |
1602 body.add(js.statement('if (# === void 0) # = #;', | 1602 body.add(js.statement('if (# === void 0) # = #;', |
1603 [jsParam, jsParam, _defaultParamValue(param)])); | 1603 [jsParam, jsParam, _defaultParamValue(param)])); |
1604 } | 1604 } |
1605 } | 1605 } |
1606 | 1606 |
1607 // TODO(jmesserly): various problems here, see: | 1607 // TODO(jmesserly): various problems here, see: |
1608 // https://github.com/dart-lang/dev_compiler/issues/161 | 1608 // https://github.com/dart-lang/dev_compiler/issues/161 |
1609 var paramType = param.element.type; | 1609 var paramType = param.element.type; |
1610 if (!constructor && _hasUnsoundTypeParameter(paramType)) { | 1610 if (!constructor && _hasUnsoundTypeParameter(paramType)) { |
1611 body.add(js | 1611 body.add( |
1612 .statement('dart.as(#, #);', [jsParam, _emitType(paramType)])); | 1612 js.statement('dart.as(#, #);', [jsParam, _emitType(paramType)])); |
1613 } | 1613 } |
1614 } | 1614 } |
1615 return body.isEmpty ? null : _statement(body); | 1615 return body.isEmpty ? null : _statement(body); |
1616 } | 1616 } |
1617 | 1617 |
1618 bool _isUnsoundTypeParameter(DartType t) => | 1618 bool _isUnsoundTypeParameter(DartType t) => |
1619 t is TypeParameterType && t.element.enclosingElement is ClassElement; | 1619 t is TypeParameterType && t.element.enclosingElement is ClassElement; |
1620 | 1620 |
1621 bool _hasUnsoundTypeParameter(DartType t) => | 1621 bool _hasUnsoundTypeParameter(DartType t) => |
1622 _isUnsoundTypeParameter(t) || | 1622 _isUnsoundTypeParameter(t) || |
(...skipping 638 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2261 } | 2261 } |
2262 | 2262 |
2263 if (type == subClass?.type) { | 2263 if (type == subClass?.type) { |
2264 return className; | 2264 return className; |
2265 } | 2265 } |
2266 | 2266 |
2267 if (type is ParameterizedType) { | 2267 if (type is ParameterizedType) { |
2268 var args = type.typeArguments; | 2268 var args = type.typeArguments; |
2269 Iterable jsArgs = null; | 2269 Iterable jsArgs = null; |
2270 if (args.any((a) => !a.isDynamic)) { | 2270 if (args.any((a) => !a.isDynamic)) { |
2271 jsArgs = args.map( | 2271 jsArgs = args |
2272 (x) => _emitType(x, subClass: subClass, className: className)); | 2272 .map((x) => _emitType(x, subClass: subClass, className: className)); |
2273 } else if (lowerGeneric) { | 2273 } else if (lowerGeneric) { |
2274 jsArgs = []; | 2274 jsArgs = []; |
2275 } | 2275 } |
2276 if (jsArgs != null) { | 2276 if (jsArgs != null) { |
2277 var genericName = _emitTopLevelName(element, suffix: '\$'); | 2277 var genericName = _emitTopLevelName(element, suffix: '\$'); |
2278 return js.call('#(#)', [genericName, jsArgs]); | 2278 return js.call('#(#)', [genericName, jsArgs]); |
2279 } | 2279 } |
2280 } | 2280 } |
2281 | 2281 |
2282 return _emitTopLevelName(element); | 2282 return _emitTopLevelName(element); |
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2542 } | 2542 } |
2543 | 2543 |
2544 /// If `g` is a generic function type, and `f` is an instantiation of it, | 2544 /// If `g` is a generic function type, and `f` is an instantiation of it, |
2545 /// then this will return the type arguments to apply, otherwise null. | 2545 /// then this will return the type arguments to apply, otherwise null. |
2546 List<JS.Expression> _emitFunctionTypeArguments(DartType g, DartType f, | 2546 List<JS.Expression> _emitFunctionTypeArguments(DartType g, DartType f, |
2547 [TypeArgumentList typeArgs]) { | 2547 [TypeArgumentList typeArgs]) { |
2548 if (g is FunctionType && | 2548 if (g is FunctionType && |
2549 g.typeFormals.isNotEmpty && | 2549 g.typeFormals.isNotEmpty && |
2550 f is FunctionType && | 2550 f is FunctionType && |
2551 f.typeFormals.isEmpty) { | 2551 f.typeFormals.isEmpty) { |
2552 return _recoverTypeArguments(g, f) | 2552 return _recoverTypeArguments(g, f).map(_emitType).toList(growable: false); |
2553 .map(_emitType) | |
2554 .toList(growable: false); | |
2555 } else if (typeArgs != null) { | 2553 } else if (typeArgs != null) { |
2556 // Dynamic calls may have type arguments, even though the function types | 2554 // Dynamic calls may have type arguments, even though the function types |
2557 // are not known. | 2555 // are not known. |
2558 // TODO(jmesserly): seems to be mostly broken in Analyzer at the moment: | 2556 // TODO(jmesserly): seems to be mostly broken in Analyzer at the moment: |
2559 // https://github.com/dart-lang/sdk/issues/26368 | 2557 // https://github.com/dart-lang/sdk/issues/26368 |
2560 return typeArgs.arguments.map(visitTypeName).toList(growable: false); | 2558 return typeArgs.arguments.map(visitTypeName).toList(growable: false); |
2561 } | 2559 } |
2562 return null; | 2560 return null; |
2563 } | 2561 } |
2564 | 2562 |
(...skipping 1370 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3935 | 3933 |
3936 JS.Statement _catchClauseGuard(CatchClause clause, JS.Statement otherwise) { | 3934 JS.Statement _catchClauseGuard(CatchClause clause, JS.Statement otherwise) { |
3937 var then = visitCatchClause(clause); | 3935 var then = visitCatchClause(clause); |
3938 | 3936 |
3939 // Discard following clauses, if any, as they are unreachable. | 3937 // Discard following clauses, if any, as they are unreachable. |
3940 if (clause.exceptionType == null) return then; | 3938 if (clause.exceptionType == null) return then; |
3941 | 3939 |
3942 // TODO(jmesserly): this is inconsistent with [visitIsExpression], which | 3940 // TODO(jmesserly): this is inconsistent with [visitIsExpression], which |
3943 // has special case for typeof. | 3941 // has special case for typeof. |
3944 return new JS.If( | 3942 return new JS.If( |
3945 js.call('dart.is(#, #)', [ | 3943 js.call('dart.is(#, #)', |
3946 _visit(_catchParameter), | 3944 [_visit(_catchParameter), _emitType(clause.exceptionType.type),]), |
3947 _emitType(clause.exceptionType.type), | |
3948 ]), | |
3949 then, | 3945 then, |
3950 otherwise); | 3946 otherwise); |
3951 } | 3947 } |
3952 | 3948 |
3953 JS.Statement _statement(List<JS.Statement> statements) { | 3949 JS.Statement _statement(List<JS.Statement> statements) { |
3954 // TODO(jmesserly): empty block singleton? | 3950 // TODO(jmesserly): empty block singleton? |
3955 if (statements.length == 0) return new JS.Block([]); | 3951 if (statements.length == 0) return new JS.Block([]); |
3956 if (statements.length == 1) return statements[0]; | 3952 if (statements.length == 1) return statements[0]; |
3957 return new JS.Block(statements); | 3953 return new JS.Block(statements); |
3958 } | 3954 } |
(...skipping 448 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4407 } | 4403 } |
4408 | 4404 |
4409 bool isLibraryPrefix(Expression node) => | 4405 bool isLibraryPrefix(Expression node) => |
4410 node is SimpleIdentifier && node.staticElement is PrefixElement; | 4406 node is SimpleIdentifier && node.staticElement is PrefixElement; |
4411 | 4407 |
4412 LibraryElement _getLibrary(AnalysisContext c, String uri) => | 4408 LibraryElement _getLibrary(AnalysisContext c, String uri) => |
4413 c.computeLibraryElement(c.sourceFactory.forUri(uri)); | 4409 c.computeLibraryElement(c.sourceFactory.forUri(uri)); |
4414 | 4410 |
4415 bool _isDartRuntime(LibraryElement l) => | 4411 bool _isDartRuntime(LibraryElement l) => |
4416 l.isInSdk && l.source.uri.toString() == 'dart:_runtime'; | 4412 l.isInSdk && l.source.uri.toString() == 'dart:_runtime'; |
OLD | NEW |