| 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 15 matching lines...) Expand all Loading... |
| 26 } | 26 } |
| 27 | 27 |
| 28 typedef T Function1<S, T>(S _); | 28 typedef T Function1<S, T>(S _); |
| 29 | 29 |
| 30 class _LocatedWrapper { | 30 class _LocatedWrapper { |
| 31 final String loc; | 31 final String loc; |
| 32 final Wrapper wrapper; | 32 final Wrapper wrapper; |
| 33 _LocatedWrapper(this.wrapper, this.loc); | 33 _LocatedWrapper(this.wrapper, this.loc); |
| 34 } | 34 } |
| 35 | 35 |
| 36 class _Inference extends DownwardsInference { |
| 37 TypeManager _tm; |
| 38 |
| 39 _Inference(TypeRules rules, this._tm) : super(rules); |
| 40 |
| 41 @override |
| 42 void annotateListLiteral(ListLiteral e, List<DartType> targs) { |
| 43 var tNames = targs.map(_tm.typeNameFromDartType).toList(); |
| 44 e.typeArguments = AstBuilder.typeArgumentList(tNames); |
| 45 var listT = rules.provider.listType.substitute4(targs); |
| 46 e.staticType = listT; |
| 47 } |
| 48 |
| 49 @override |
| 50 void annotateMapLiteral(MapLiteral e, List<DartType> targs) { |
| 51 var tNames = targs.map(_tm.typeNameFromDartType).toList(); |
| 52 e.typeArguments = AstBuilder.typeArgumentList(tNames); |
| 53 var mapT = rules.provider.mapType.substitute4(targs); |
| 54 e.staticType = mapT; |
| 55 } |
| 56 |
| 57 @override |
| 58 void annotateInstanceCreationExpression( |
| 59 InstanceCreationExpression e, List<DartType> targs) { |
| 60 var tNames = targs.map(_tm.typeNameFromDartType).toList(); |
| 61 var cName = e.constructorName; |
| 62 var id = cName.type.name; |
| 63 var typeName = AstBuilder.typeName(id, tNames); |
| 64 cName.type = typeName; |
| 65 var rawType = (e.staticType.element as ClassElement).type; |
| 66 e.staticType = rawType.substitute4(targs); |
| 67 } |
| 68 } |
| 69 |
| 36 // This class implements a pass which modifies (in place) the ast replacing | 70 // This class implements a pass which modifies (in place) the ast replacing |
| 37 // abstract coercion nodes with their dart implementations. | 71 // abstract coercion nodes with their dart implementations. |
| 38 class UnitCoercionReifier extends analyzer.GeneralizingAstVisitor<Object> | 72 class UnitCoercionReifier extends analyzer.GeneralizingAstVisitor<Object> |
| 39 with ConversionVisitor<Object> { | 73 with ConversionVisitor<Object> { |
| 40 CoercionManager _cm; | 74 CoercionManager _cm; |
| 41 final TypeManager _tm; | 75 final TypeManager _tm; |
| 42 final VariableManager _vm; | 76 final VariableManager _vm; |
| 43 SourceFile _file; | 77 SourceFile _file; |
| 44 bool _skipCoercions = false; | 78 bool _skipCoercions = false; |
| 45 final TypeRules _rules; | 79 final TypeRules _rules; |
| 80 _Inference _inferrer; |
| 46 | 81 |
| 47 UnitCoercionReifier(this._tm, this._vm, this._rules) { | 82 UnitCoercionReifier(this._tm, this._vm, this._rules) { |
| 48 _cm = new CoercionManager(_vm, _tm, _rules); | 83 _cm = new CoercionManager(_vm, _tm, _rules); |
| 84 _inferrer = new _Inference(_rules, _tm); |
| 49 } | 85 } |
| 50 | 86 |
| 51 // This should be the entry point for this class. Entering via the | 87 // This should be the entry point for this class. Entering via the |
| 52 // visit functions directly may not do the right thing with respect | 88 // visit functions directly may not do the right thing with respect |
| 53 // to discharging the collected definitions. | 89 // to discharging the collected definitions. |
| 54 void reify(CompilationUnit unit) { | 90 void reify(CompilationUnit unit) { |
| 55 _file = new SourceFile(unit.element.source.contents.data, | 91 _file = new SourceFile(unit.element.source.contents.data, |
| 56 url: unit.element.source.uri); | 92 url: unit.element.source.uri); |
| 57 visitCompilationUnit(unit); | 93 visitCompilationUnit(unit); |
| 58 } | 94 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 71 } | 107 } |
| 72 } | 108 } |
| 73 return null; | 109 return null; |
| 74 } | 110 } |
| 75 | 111 |
| 76 static String _conversionKind(Conversion node) { | 112 static String _conversionKind(Conversion node) { |
| 77 if (node is ClosureWrapLiteral) return "WrapLiteral"; | 113 if (node is ClosureWrapLiteral) return "WrapLiteral"; |
| 78 if (node is ClosureWrap) return "Wrap"; | 114 if (node is ClosureWrap) return "Wrap"; |
| 79 if (node is DynamicCast) return "DynamicCast"; | 115 if (node is DynamicCast) return "DynamicCast"; |
| 80 if (node is AssignmentCast) return "AssignmentCast"; | 116 if (node is AssignmentCast) return "AssignmentCast"; |
| 81 if (node is InferableLiteral) return "InferableLiteral"; | |
| 82 if (node is InferableClosure) return "InferableClosure"; | 117 if (node is InferableClosure) return "InferableClosure"; |
| 83 if (node is InferableAllocation) return "InferableAllocation"; | |
| 84 if (node is DownCastComposite) return "CompositeCast"; | 118 if (node is DownCastComposite) return "CompositeCast"; |
| 85 if (node is DownCastImplicit) return "ImplicitCast"; | 119 if (node is DownCastImplicit) return "ImplicitCast"; |
| 86 assert(false); | 120 assert(false); |
| 87 return ""; | 121 return ""; |
| 88 } | 122 } |
| 89 | 123 |
| 90 @override | 124 @override |
| 91 Object visitAsExpression(AsExpression e) { | 125 Object visitAsExpression(AsExpression e) { |
| 92 var cast = Coercion.cast(_rules.getStaticType(e.expression), e.type.type); | 126 var cast = Coercion.cast(_rules.getStaticType(e.expression), e.type.type); |
| 93 var loc = _locationInfo(e); | 127 var loc = _locationInfo(e); |
| 94 Expression castNode = | 128 Expression castNode = |
| 95 _cm.coerceExpression(e.expression, cast, "CastUser", loc); | 129 _cm.coerceExpression(e.expression, cast, "CastUser", loc); |
| 96 if (!NodeReplacer.replace(e, castNode)) { | 130 if (!NodeReplacer.replace(e, castNode)) { |
| 97 _log.severe("Failed to replace node for DownCast"); | 131 _log.severe("Failed to replace node for DownCast"); |
| 98 } | 132 } |
| 99 castNode.accept(this); | 133 castNode.accept(this); |
| 100 return null; | 134 return null; |
| 101 } | 135 } |
| 102 | 136 |
| 103 @override | 137 @override |
| 138 Object visitInferredTypeBase(InferredTypeBase node) { |
| 139 var expr = node.node; |
| 140 var b = _inferrer.inferExpression(expr, node.type); |
| 141 assert(b); |
| 142 if (!NodeReplacer.replace(node, expr)) { |
| 143 _log.severe("Failed to replace node for InferredType"); |
| 144 } |
| 145 expr.accept(this); |
| 146 return null; |
| 147 } |
| 148 |
| 149 @override |
| 104 Object visitDownCast(DownCast node) { | 150 Object visitDownCast(DownCast node) { |
| 105 if (_skipCoercions) { | 151 if (_skipCoercions) { |
| 106 _log.severe("Skipping runtime downcast in constant context"); | 152 _log.severe("Skipping runtime downcast in constant context"); |
| 107 return null; | 153 return null; |
| 108 } | 154 } |
| 109 String kind = _conversionKind(node); | 155 String kind = _conversionKind(node); |
| 110 var loc = _locationInfo(node); | 156 var loc = _locationInfo(node); |
| 111 Expression castNode = _cm.coerceExpression(node.node, node.cast, kind, loc); | 157 Expression castNode = _cm.coerceExpression(node.node, node.cast, kind, loc); |
| 112 if (!NodeReplacer.replace(node, castNode)) { | 158 if (!NodeReplacer.replace(node, castNode)) { |
| 113 _log.severe("Failed to replace node for DownCast"); | 159 _log.severe("Failed to replace node for DownCast"); |
| (...skipping 546 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 660 Expression _typeExpression(TypeName t) { | 706 Expression _typeExpression(TypeName t) { |
| 661 if (t.typeArguments != null && t.typeArguments.length > 0) { | 707 if (t.typeArguments != null && t.typeArguments.length > 0) { |
| 662 var w = AstBuilder.identifierFromString("_"); | 708 var w = AstBuilder.identifierFromString("_"); |
| 663 var fp = AstBuilder.simpleFormal(w, t); | 709 var fp = AstBuilder.simpleFormal(w, t); |
| 664 var f = AstBuilder.blockFunction(<FormalParameter>[fp], <Statement>[]); | 710 var f = AstBuilder.blockFunction(<FormalParameter>[fp], <Statement>[]); |
| 665 return new RuntimeOperation("type", <Expression>[f]); | 711 return new RuntimeOperation("type", <Expression>[f]); |
| 666 } | 712 } |
| 667 return t.name; | 713 return t.name; |
| 668 } | 714 } |
| 669 } | 715 } |
| OLD | NEW |