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