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 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; | |
|
vsm
2015/03/27 21:20:59
Does e.staticType require updating?
Leaf
2015/03/30 23:26:00
Done.
| |
| 65 } | |
| 66 } | |
| 67 | |
| 36 // This class implements a pass which modifies (in place) the ast replacing | 68 // This class implements a pass which modifies (in place) the ast replacing |
| 37 // abstract coercion nodes with their dart implementations. | 69 // abstract coercion nodes with their dart implementations. |
| 38 class UnitCoercionReifier extends analyzer.GeneralizingAstVisitor<Object> | 70 class UnitCoercionReifier extends analyzer.GeneralizingAstVisitor<Object> |
| 39 with ConversionVisitor<Object> { | 71 with ConversionVisitor<Object> { |
| 40 CoercionManager _cm; | 72 CoercionManager _cm; |
| 41 final TypeManager _tm; | 73 final TypeManager _tm; |
| 42 final VariableManager _vm; | 74 final VariableManager _vm; |
| 43 SourceFile _file; | 75 SourceFile _file; |
| 44 bool _skipCoercions = false; | 76 bool _skipCoercions = false; |
| 45 final TypeRules _rules; | 77 final TypeRules _rules; |
| 78 _Inference _inferrer; | |
| 46 | 79 |
| 47 UnitCoercionReifier(this._tm, this._vm, this._rules) { | 80 UnitCoercionReifier(this._tm, this._vm, this._rules) { |
| 48 _cm = new CoercionManager(_vm, _tm, _rules); | 81 _cm = new CoercionManager(_vm, _tm, _rules); |
| 82 _inferrer = new _Inference(_rules, _tm); | |
| 49 } | 83 } |
| 50 | 84 |
| 51 // This should be the entry point for this class. Entering via the | 85 // This should be the entry point for this class. Entering via the |
| 52 // visit functions directly may not do the right thing with respect | 86 // visit functions directly may not do the right thing with respect |
| 53 // to discharging the collected definitions. | 87 // to discharging the collected definitions. |
| 54 void reify(CompilationUnit unit) { | 88 void reify(CompilationUnit unit) { |
| 55 _file = new SourceFile(unit.element.source.contents.data, | 89 _file = new SourceFile(unit.element.source.contents.data, |
| 56 url: unit.element.source.uri); | 90 url: unit.element.source.uri); |
| 57 visitCompilationUnit(unit); | 91 visitCompilationUnit(unit); |
| 58 } | 92 } |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 if (!b) { | |
| 142 print("Expression is $expr : ${node.type}"); | |
| 143 _inferrer.inferExpression(expr, node.type); | |
| 144 } | |
| 145 assert(b); | |
| 146 if (!NodeReplacer.replace(node, expr)) { | |
| 147 _log.severe("Failed to replace node for InferredType"); | |
| 148 } | |
| 149 expr.accept(this); | |
| 150 return null; | |
| 151 } | |
| 152 | |
| 153 @override | |
| 104 Object visitDownCast(DownCast node) { | 154 Object visitDownCast(DownCast node) { |
| 105 if (_skipCoercions) { | 155 if (_skipCoercions) { |
| 106 _log.severe("Skipping runtime downcast in constant context"); | 156 _log.severe("Skipping runtime downcast in constant context"); |
| 107 return null; | 157 return null; |
| 108 } | 158 } |
| 109 String kind = _conversionKind(node); | 159 String kind = _conversionKind(node); |
| 110 var loc = _locationInfo(node); | 160 var loc = _locationInfo(node); |
| 111 Expression castNode = _cm.coerceExpression(node.node, node.cast, kind, loc); | 161 Expression castNode = _cm.coerceExpression(node.node, node.cast, kind, loc); |
| 112 if (!NodeReplacer.replace(node, castNode)) { | 162 if (!NodeReplacer.replace(node, castNode)) { |
| 113 _log.severe("Failed to replace node for DownCast"); | 163 _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) { | 710 Expression _typeExpression(TypeName t) { |
| 661 if (t.typeArguments != null && t.typeArguments.length > 0) { | 711 if (t.typeArguments != null && t.typeArguments.length > 0) { |
| 662 var w = AstBuilder.identifierFromString("_"); | 712 var w = AstBuilder.identifierFromString("_"); |
| 663 var fp = AstBuilder.simpleFormal(w, t); | 713 var fp = AstBuilder.simpleFormal(w, t); |
| 664 var f = AstBuilder.blockFunction(<FormalParameter>[fp], <Statement>[]); | 714 var f = AstBuilder.blockFunction(<FormalParameter>[fp], <Statement>[]); |
| 665 return new RuntimeOperation("type", <Expression>[f]); | 715 return new RuntimeOperation("type", <Expression>[f]); |
| 666 } | 716 } |
| 667 return t.name; | 717 return t.name; |
| 668 } | 718 } |
| 669 } | 719 } |
| OLD | NEW |