| 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 library dev_compiler.src.codegen.reify_coercions; | 5 library dev_compiler.src.codegen.reify_coercions; |
| 6 | 6 |
| 7 import 'package:analyzer/analyzer.dart' as analyzer; | 7 import 'package:analyzer/analyzer.dart' as analyzer; |
| 8 import 'package:analyzer/src/generated/ast.dart'; | 8 import 'package:analyzer/src/generated/ast.dart'; |
| 9 import 'package:analyzer/src/generated/element.dart'; | 9 import 'package:analyzer/src/generated/element.dart'; |
| 10 import 'package:logging/logging.dart' as logger; | 10 import 'package:logging/logging.dart' as logger; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 } | 38 } |
| 39 | 39 |
| 40 class _Inference extends DownwardsInference { | 40 class _Inference extends DownwardsInference { |
| 41 TypeManager _tm; | 41 TypeManager _tm; |
| 42 | 42 |
| 43 _Inference(TypeRules rules, this._tm) : super(rules); | 43 _Inference(TypeRules rules, this._tm) : super(rules); |
| 44 | 44 |
| 45 @override | 45 @override |
| 46 void annotateCastFromDynamic(Expression e, DartType t) { | 46 void annotateCastFromDynamic(Expression e, DartType t) { |
| 47 var cast = Coercion.cast(e.staticType, t); | 47 var cast = Coercion.cast(e.staticType, t); |
| 48 var node = new DynamicCast(rules, e, cast); | 48 var info = new DynamicCast(rules, e, cast); |
| 49 if (!NodeReplacer.replace(e, node)) { | 49 CoercionInfo.set(e, info); |
| 50 _log.severe("Failed to replace node for DownCast"); | |
| 51 } | |
| 52 } | 50 } |
| 53 | 51 |
| 54 @override | 52 @override |
| 55 void annotateListLiteral(ListLiteral e, List<DartType> targs) { | 53 void annotateListLiteral(ListLiteral e, List<DartType> targs) { |
| 56 var tNames = targs.map(_tm.typeNameFromDartType).toList(); | 54 var tNames = targs.map(_tm.typeNameFromDartType).toList(); |
| 57 e.typeArguments = AstBuilder.typeArgumentList(tNames); | 55 e.typeArguments = AstBuilder.typeArgumentList(tNames); |
| 58 var listT = rules.provider.listType.substitute4(targs); | 56 var listT = rules.provider.listType.substitute4(targs); |
| 59 e.staticType = listT; | 57 e.staticType = listT; |
| 60 } | 58 } |
| 61 | 59 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 83 | 81 |
| 84 @override | 82 @override |
| 85 void annotateFunctionExpression(FunctionExpression e, DartType returnType) { | 83 void annotateFunctionExpression(FunctionExpression e, DartType returnType) { |
| 86 // Implicitly changes e.staticType | 84 // Implicitly changes e.staticType |
| 87 (e.element as ExecutableElementImpl).returnType = returnType; | 85 (e.element as ExecutableElementImpl).returnType = returnType; |
| 88 } | 86 } |
| 89 } | 87 } |
| 90 | 88 |
| 91 // This class implements a pass which modifies (in place) the ast replacing | 89 // This class implements a pass which modifies (in place) the ast replacing |
| 92 // abstract coercion nodes with their dart implementations. | 90 // abstract coercion nodes with their dart implementations. |
| 93 class CoercionReifier extends analyzer.GeneralizingAstVisitor<Object> | 91 class CoercionReifier extends analyzer.GeneralizingAstVisitor<Object> { |
| 94 with ConversionVisitor<Object> { | |
| 95 final CoercionManager _cm; | 92 final CoercionManager _cm; |
| 96 final TypeManager _tm; | 93 final TypeManager _tm; |
| 97 final VariableManager _vm; | 94 final VariableManager _vm; |
| 98 final LibraryUnit _library; | 95 final LibraryUnit _library; |
| 99 final _Inference _inferrer; | 96 final _Inference _inferrer; |
| 100 | 97 |
| 101 CoercionReifier._( | 98 CoercionReifier._( |
| 102 this._cm, this._tm, this._vm, this._library, this._inferrer); | 99 this._cm, this._tm, this._vm, this._library, this._inferrer); |
| 103 | 100 |
| 104 factory CoercionReifier(LibraryUnit library, AbstractCompiler compiler) { | 101 factory CoercionReifier(LibraryUnit library, AbstractCompiler compiler) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 115 // Returns the set of new type identifiers added by the reifier | 112 // Returns the set of new type identifiers added by the reifier |
| 116 Map<Identifier, NewTypeIdDesc> reify() { | 113 Map<Identifier, NewTypeIdDesc> reify() { |
| 117 _library.partsThenLibrary.forEach(generateUnit); | 114 _library.partsThenLibrary.forEach(generateUnit); |
| 118 return _tm.addedTypes; | 115 return _tm.addedTypes; |
| 119 } | 116 } |
| 120 | 117 |
| 121 void generateUnit(CompilationUnit unit) { | 118 void generateUnit(CompilationUnit unit) { |
| 122 visitCompilationUnit(unit); | 119 visitCompilationUnit(unit); |
| 123 } | 120 } |
| 124 | 121 |
| 122 @override |
| 123 Object visitExpression(Expression node) { |
| 124 var info = CoercionInfo.get(node); |
| 125 if (info is InferredTypeBase) { |
| 126 return _visitInferredTypeBase(info); |
| 127 } else if (info is DownCast) { |
| 128 return _visitDownCast(info); |
| 129 } |
| 130 return super.visitExpression(node); |
| 131 } |
| 132 |
| 125 ///////////////// Private ////////////////////////////////// | 133 ///////////////// Private ////////////////////////////////// |
| 126 | 134 |
| 127 @override | 135 Object _visitInferredTypeBase(InferredTypeBase node) { |
| 128 Object visitInferredTypeBase(InferredTypeBase node) { | |
| 129 var expr = node.node; | 136 var expr = node.node; |
| 130 var b = _inferrer.inferExpression(expr, node.type, <String>[]); | 137 var success = _inferrer.inferExpression(expr, node.type, <String>[]); |
| 131 assert(b); | 138 assert(success); |
| 132 if (!NodeReplacer.replace(node, expr)) { | 139 expr.visitChildren(this); |
| 133 _log.severe("Failed to replace node for InferredType"); | |
| 134 } | |
| 135 expr.accept(this); | |
| 136 return null; | 140 return null; |
| 137 } | 141 } |
| 138 | 142 |
| 139 @override | 143 Object _visitDownCast(DownCast node) { |
| 140 Object visitDownCast(DownCast node) { | 144 var expr = node.node; |
| 141 Expression castNode = _cm.coerceExpression(node.node, node.cast); | 145 var parent = expr.parent; |
| 142 if (!NodeReplacer.replace(node, castNode)) { | 146 expr.visitChildren(this); |
| 143 _log.severe("Failed to replace node for DownCast"); | 147 Expression newE = _cm.coerceExpression(expr, node.cast); |
| 148 if (!identical(expr, newE)) { |
| 149 var replaced = parent.accept(new NodeReplacer(expr, newE)); |
| 150 // It looks like NodeReplacer will always return true. |
| 151 // It does throw IllegalArgumentException though, if child is not found. |
| 152 assert(replaced); |
| 144 } | 153 } |
| 145 castNode.accept(this); | |
| 146 return null; | 154 return null; |
| 147 } | 155 } |
| 148 | 156 |
| 149 Object visitCompilationUnit(CompilationUnit unit) { | 157 Object visitCompilationUnit(CompilationUnit unit) { |
| 150 _cm.enterCompilationUnit(unit); | 158 _cm.enterCompilationUnit(unit); |
| 151 Object ret = super.visitCompilationUnit(unit); | 159 Object ret = super.visitCompilationUnit(unit); |
| 152 _cm.exitCompilationUnit(unit); | 160 _cm.exitCompilationUnit(unit); |
| 153 return ret; | 161 return ret; |
| 154 } | 162 } |
| 155 } | 163 } |
| (...skipping 334 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 490 var t = _mkNewTypeName(dType, id, args); | 498 var t = _mkNewTypeName(dType, id, args); |
| 491 return t; | 499 return t; |
| 492 } | 500 } |
| 493 | 501 |
| 494 TypeName _mkNewTypeName(DartType type, Identifier id, List<TypeName> args) { | 502 TypeName _mkNewTypeName(DartType type, Identifier id, List<TypeName> args) { |
| 495 var t = AstBuilder.typeName(id, args); | 503 var t = AstBuilder.typeName(id, args); |
| 496 t.type = type; | 504 t.type = type; |
| 497 return t; | 505 return t; |
| 498 } | 506 } |
| 499 } | 507 } |
| OLD | NEW |