| 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 'package:analyzer/analyzer.dart' as analyzer; | 5 import 'package:analyzer/analyzer.dart' as analyzer; |
| 6 import 'package:analyzer/src/generated/ast.dart'; | 6 import 'package:analyzer/dart/ast/ast.dart'; |
| 7 import 'package:analyzer/src/generated/element.dart'; | 7 import 'package:analyzer/dart/element/element.dart'; |
| 8 import 'package:analyzer/dart/element/type.dart'; |
| 9 import 'package:analyzer/src/dart/ast/utilities.dart' show NodeReplacer; |
| 8 import 'package:analyzer/src/generated/type_system.dart' | 10 import 'package:analyzer/src/generated/type_system.dart' |
| 9 show StrongTypeSystemImpl; | 11 show StrongTypeSystemImpl; |
| 10 import 'package:logging/logging.dart' as logger; | 12 import 'package:logging/logging.dart' as logger; |
| 11 | 13 |
| 12 import '../info.dart'; | 14 import '../info.dart'; |
| 13 | 15 import '../utils.dart' show getStaticType; |
| 14 import 'ast_builder.dart'; | 16 import 'ast_builder.dart'; |
| 15 | 17 |
| 16 final _log = new logger.Logger('dev_compiler.reify_coercions'); | 18 final _log = new logger.Logger('dev_compiler.reify_coercions'); |
| 17 | 19 |
| 18 class NewTypeIdDesc { | 20 class NewTypeIdDesc { |
| 19 /// If null, then this is not a library level identifier (i.e. it's | 21 /// If null, then this is not a library level identifier (i.e. it's |
| 20 /// a type parameter, or a special type like void, dynamic, etc) | 22 /// a type parameter, or a special type like void, dynamic, etc) |
| 21 LibraryElement importedFrom; | 23 LibraryElement importedFrom; |
| 22 | 24 |
| 23 /// True => use/def in same library | 25 /// True => use/def in same library |
| (...skipping 28 matching lines...) Expand all Loading... |
| 52 } else if (info is DownCast) { | 54 } else if (info is DownCast) { |
| 53 return _visitDownCast(info, node); | 55 return _visitDownCast(info, node); |
| 54 } | 56 } |
| 55 return super.visitExpression(node); | 57 return super.visitExpression(node); |
| 56 } | 58 } |
| 57 | 59 |
| 58 ///////////////// Private ////////////////////////////////// | 60 ///////////////// Private ////////////////////////////////// |
| 59 | 61 |
| 60 Object _visitInferredTypeBase(InferredTypeBase node, Expression expr) { | 62 Object _visitInferredTypeBase(InferredTypeBase node, Expression expr) { |
| 61 DartType t = node.type; | 63 DartType t = node.type; |
| 62 if (!_typeSystem.isSubtypeOf(_getStaticType(expr), t)) { | 64 if (!_typeSystem.isSubtypeOf(getStaticType(expr), t)) { |
| 63 if (_getStaticType(expr).isDynamic) { | 65 if (getStaticType(expr).isDynamic) { |
| 64 var cast = Coercion.cast(expr.staticType, t); | 66 var cast = Coercion.cast(expr.staticType, t); |
| 65 var info = new DynamicCast(_typeSystem, expr, cast); | 67 var info = new DynamicCast(_typeSystem, expr, cast); |
| 66 CoercionInfo.set(expr, info); | 68 CoercionInfo.set(expr, info); |
| 67 } | 69 } |
| 68 } | 70 } |
| 69 expr.visitChildren(this); | 71 expr.visitChildren(this); |
| 70 return null; | 72 return null; |
| 71 } | 73 } |
| 72 | 74 |
| 73 Object _visitDownCast(DownCast node, Expression expr) { | 75 Object _visitDownCast(DownCast node, Expression expr) { |
| 74 var parent = expr.parent; | 76 var parent = expr.parent; |
| 75 expr.visitChildren(this); | 77 expr.visitChildren(this); |
| 76 Expression newE = coerceExpression(expr, node.cast); | 78 Expression newE = coerceExpression(expr, node.cast); |
| 77 if (!identical(expr, newE)) { | 79 if (!identical(expr, newE)) { |
| 78 var replaced = parent.accept(new NodeReplacer(expr, newE)); | 80 var replaced = parent.accept(new NodeReplacer(expr, newE)); |
| 79 // It looks like NodeReplacer will always return true. | 81 // It looks like NodeReplacer will always return true. |
| 80 // It does throw IllegalArgumentException though, if child is not found. | 82 // It does throw IllegalArgumentException though, if child is not found. |
| 81 assert(replaced); | 83 assert(replaced); |
| 82 } | 84 } |
| 83 return null; | 85 return null; |
| 84 } | 86 } |
| 85 | 87 |
| 86 DartType _getStaticType(Expression expr) { | |
| 87 return expr.staticType ?? DynamicTypeImpl.instance; | |
| 88 } | |
| 89 | |
| 90 /// Coerce [e] using [c], returning a new expression. | 88 /// Coerce [e] using [c], returning a new expression. |
| 91 Expression coerceExpression(Expression e, Coercion c) { | 89 Expression coerceExpression(Expression e, Coercion c) { |
| 92 assert(c != null); | 90 assert(c != null); |
| 93 assert(c is! CoercionError); | 91 assert(c is! CoercionError); |
| 94 if (e is NamedExpression) { | 92 if (e is NamedExpression) { |
| 95 Expression inner = coerceExpression(e.expression, c); | 93 Expression inner = coerceExpression(e.expression, c); |
| 96 return new NamedExpression(e.name, inner); | 94 return new NamedExpression(e.name, inner); |
| 97 } | 95 } |
| 98 if (c is Cast) return _castExpression(e, c); | 96 if (c is Cast) return _castExpression(e, c); |
| 99 assert(c is Identity); | 97 assert(c is Identity); |
| 100 return e; | 98 return e; |
| 101 } | 99 } |
| 102 | 100 |
| 103 ///////////////// Private ////////////////////////////////// | 101 ///////////////// Private ////////////////////////////////// |
| 104 | 102 |
| 105 Expression _castExpression(Expression e, Cast c) { | 103 Expression _castExpression(Expression e, Cast c) { |
| 106 // We use an empty name in the AST, because the JS code generator only cares | 104 // We use an empty name in the AST, because the JS code generator only cares |
| 107 // about the target type. It does not look at the AST name. | 105 // about the target type. It does not look at the AST name. |
| 108 var typeName = new TypeName(AstBuilder.identifierFromString(''), null); | 106 var typeName = new TypeName(AstBuilder.identifierFromString(''), null); |
| 109 typeName.type = c.toType; | 107 typeName.type = c.toType; |
| 110 var cast = AstBuilder.asExpression(e, typeName); | 108 var cast = AstBuilder.asExpression(e, typeName); |
| 111 cast.staticType = c.toType; | 109 cast.staticType = c.toType; |
| 112 return cast; | 110 return cast; |
| 113 } | 111 } |
| 114 } | 112 } |
| OLD | NEW |