| 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/dart/ast/ast.dart'; | 6 import 'package:analyzer/dart/ast/ast.dart'; |
| 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 import 'package:analyzer/src/dart/ast/utilities.dart' show NodeReplacer; | 9 import 'package:analyzer/src/dart/ast/utilities.dart' show NodeReplacer; |
| 10 import 'package:analyzer/src/generated/type_system.dart' | |
| 11 show StrongTypeSystemImpl; | |
| 12 import 'package:analyzer/src/task/strong/info.dart'; | 10 import 'package:analyzer/src/task/strong/info.dart'; |
| 13 import 'package:logging/logging.dart' as logger; | 11 import 'package:logging/logging.dart' as logger; |
| 14 | 12 |
| 15 import 'ast_builder.dart'; | 13 import 'ast_builder.dart'; |
| 16 import '../utils.dart' show getStaticType; | |
| 17 | 14 |
| 18 final _log = new logger.Logger('dev_compiler.reify_coercions'); | 15 final _log = new logger.Logger('dev_compiler.reify_coercions'); |
| 19 | 16 |
| 20 class NewTypeIdDesc { | 17 class NewTypeIdDesc { |
| 21 /// If null, then this is not a library level identifier (i.e. it's | 18 /// If null, then this is not a library level identifier (i.e. it's |
| 22 /// a type parameter, or a special type like void, dynamic, etc) | 19 /// a type parameter, or a special type like void, dynamic, etc) |
| 23 LibraryElement importedFrom; | 20 LibraryElement importedFrom; |
| 24 | 21 |
| 25 /// True => use/def in same library | 22 /// True => use/def in same library |
| 26 bool fromCurrent; | 23 bool fromCurrent; |
| 27 | 24 |
| 28 /// True => not a source variable | 25 /// True => not a source variable |
| 29 bool synthetic; | 26 bool synthetic; |
| 30 NewTypeIdDesc({this.fromCurrent, this.importedFrom, this.synthetic}); | 27 NewTypeIdDesc({this.fromCurrent, this.importedFrom, this.synthetic}); |
| 31 } | 28 } |
| 32 | 29 |
| 33 // This class implements a pass which modifies (in place) the ast replacing | 30 // This class implements a pass which modifies (in place) the ast replacing |
| 34 // abstract coercion nodes with their dart implementations. | 31 // abstract coercion nodes with their dart implementations. |
| 35 class CoercionReifier extends analyzer.GeneralizingAstVisitor<Object> { | 32 class CoercionReifier extends analyzer.GeneralizingAstVisitor<Object> { |
| 36 final StrongTypeSystemImpl _typeSystem; | |
| 37 | 33 |
| 38 CoercionReifier(this._typeSystem); | 34 CoercionReifier(); |
| 39 | 35 |
| 40 /// This should be the entry point for this class. | 36 /// This should be the entry point for this class. |
| 41 /// | 37 /// |
| 42 /// Entering via the visit functions directly may not do the right | 38 /// Entering via the visit functions directly may not do the right |
| 43 /// thing with respect to discharging the collected definitions. | 39 /// thing with respect to discharging the collected definitions. |
| 44 /// | 40 /// |
| 45 /// Returns the set of new type identifiers added by the reifier | 41 /// Returns the set of new type identifiers added by the reifier |
| 46 void reify(List<CompilationUnit> units) { | 42 void reify(List<CompilationUnit> units) { |
| 47 units.forEach(visitCompilationUnit); | 43 units.forEach(visitCompilationUnit); |
| 48 } | 44 } |
| 49 | 45 |
| 50 @override | 46 @override |
| 51 Object visitExpression(Expression node) { | 47 Object visitExpression(Expression node) { |
| 52 var info = CoercionInfo.get(node); | 48 var info = CoercionInfo.get(node); |
| 53 if (info is InferredTypeBase) { | 49 if (info is DownCast) { |
| 54 return _visitInferredTypeBase(info, node); | |
| 55 } else if (info is DownCast) { | |
| 56 return _visitDownCast(info, node); | 50 return _visitDownCast(info, node); |
| 57 } | 51 } |
| 58 return super.visitExpression(node); | 52 return super.visitExpression(node); |
| 59 } | 53 } |
| 60 | 54 |
| 61 ///////////////// Private ////////////////////////////////// | 55 ///////////////// Private ////////////////////////////////// |
| 62 | 56 |
| 63 Object _visitInferredTypeBase(InferredTypeBase node, Expression expr) { | |
| 64 DartType t = node.type; | |
| 65 if (!_typeSystem.isSubtypeOf(getStaticType(expr), t)) { | |
| 66 if (getStaticType(expr).isDynamic) { | |
| 67 var cast = Coercion.cast(expr.staticType, t); | |
| 68 var info = new DynamicCast(_typeSystem, expr, cast); | |
| 69 CoercionInfo.set(expr, info); | |
| 70 } | |
| 71 } | |
| 72 expr.visitChildren(this); | |
| 73 return null; | |
| 74 } | |
| 75 | |
| 76 Object _visitDownCast(DownCast node, Expression expr) { | 57 Object _visitDownCast(DownCast node, Expression expr) { |
| 77 var parent = expr.parent; | 58 var parent = expr.parent; |
| 78 expr.visitChildren(this); | 59 expr.visitChildren(this); |
| 79 Expression newE = coerceExpression(expr, node.cast); | 60 Expression newE = coerceExpression(expr, node); |
| 80 if (!identical(expr, newE)) { | 61 if (!identical(expr, newE)) { |
| 81 var replaced = parent.accept(new NodeReplacer(expr, newE)); | 62 var replaced = parent.accept(new NodeReplacer(expr, newE)); |
| 82 // It looks like NodeReplacer will always return true. | 63 // It looks like NodeReplacer will always return true. |
| 83 // It does throw IllegalArgumentException though, if child is not found. | 64 // It does throw IllegalArgumentException though, if child is not found. |
| 84 assert(replaced); | 65 assert(replaced); |
| 85 } | 66 } |
| 86 return null; | 67 return null; |
| 87 } | 68 } |
| 88 | 69 |
| 89 /// Coerce [e] using [c], returning a new expression. | 70 /// Coerce [e] using [c], returning a new expression. |
| 90 Expression coerceExpression(Expression e, Coercion c) { | 71 Expression coerceExpression(Expression e, DownCast node) { |
| 91 assert(c != null); | |
| 92 assert(c is! CoercionError); | |
| 93 if (e is NamedExpression) { | 72 if (e is NamedExpression) { |
| 94 Expression inner = coerceExpression(e.expression, c); | 73 Expression inner = coerceExpression(e.expression, node); |
| 95 return new NamedExpression(e.name, inner); | 74 return new NamedExpression(e.name, inner); |
| 96 } | 75 } |
| 97 if (c is Cast) return _castExpression(e, c); | 76 return _castExpression(e, node.convertedType); |
| 98 assert(c is Identity); | |
| 99 return e; | |
| 100 } | 77 } |
| 101 | 78 |
| 102 ///////////////// Private ////////////////////////////////// | 79 ///////////////// Private ////////////////////////////////// |
| 103 | 80 |
| 104 Expression _castExpression(Expression e, Cast c) { | 81 Expression _castExpression(Expression e, DartType toType) { |
| 105 // We use an empty name in the AST, because the JS code generator only cares | 82 // We use an empty name in the AST, because the JS code generator only cares |
| 106 // about the target type. It does not look at the AST name. | 83 // about the target type. It does not look at the AST name. |
| 107 var typeName = new TypeName(AstBuilder.identifierFromString(''), null); | 84 var typeName = new TypeName(AstBuilder.identifierFromString(''), null); |
| 108 typeName.type = c.toType; | 85 typeName.type = toType; |
| 109 var cast = AstBuilder.asExpression(e, typeName); | 86 var cast = AstBuilder.asExpression(e, typeName); |
| 110 cast.staticType = c.toType; | 87 cast.staticType = toType; |
| 111 return cast; | 88 return cast; |
| 112 } | 89 } |
| 113 } | 90 } |
| OLD | NEW |