| 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:analyzer/src/generated/engine.dart' show AnalysisContext; |
| 10 import 'package:logging/logging.dart' as logger; | 11 import 'package:logging/logging.dart' as logger; |
| 11 import 'package:source_span/source_span.dart' show SourceFile; | |
| 12 | 12 |
| 13 import 'package:dev_compiler/devc.dart' show AbstractCompiler; |
| 13 import 'package:dev_compiler/src/checker/rules.dart'; | 14 import 'package:dev_compiler/src/checker/rules.dart'; |
| 14 import 'package:dev_compiler/src/info.dart'; | 15 import 'package:dev_compiler/src/info.dart'; |
| 15 import 'package:dev_compiler/src/options.dart' show CompilerOptions; | 16 import 'package:dev_compiler/src/options.dart' show CompilerOptions; |
| 16 import 'package:dev_compiler/src/utils.dart' as utils; | 17 import 'package:dev_compiler/src/utils.dart' as utils; |
| 17 | 18 |
| 18 import 'ast_builder.dart'; | 19 import 'ast_builder.dart'; |
| 19 | 20 |
| 20 final _log = new logger.Logger('dev_compiler.reify_coercions'); | 21 final _log = new logger.Logger('dev_compiler.reify_coercions'); |
| 21 | 22 |
| 22 // TODO(leafp) Factor this out or use an existing library | 23 // TODO(leafp) Factor this out or use an existing library |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 void annotateFunctionExpression(FunctionExpression e, DartType returnType) { | 102 void annotateFunctionExpression(FunctionExpression e, DartType returnType) { |
| 102 // Implicitly changes e.staticType | 103 // Implicitly changes e.staticType |
| 103 (e.element as ExecutableElementImpl).returnType = returnType; | 104 (e.element as ExecutableElementImpl).returnType = returnType; |
| 104 } | 105 } |
| 105 } | 106 } |
| 106 | 107 |
| 107 // This class implements a pass which modifies (in place) the ast replacing | 108 // This class implements a pass which modifies (in place) the ast replacing |
| 108 // abstract coercion nodes with their dart implementations. | 109 // abstract coercion nodes with their dart implementations. |
| 109 class CoercionReifier extends analyzer.GeneralizingAstVisitor<Object> | 110 class CoercionReifier extends analyzer.GeneralizingAstVisitor<Object> |
| 110 with ConversionVisitor<Object> { | 111 with ConversionVisitor<Object> { |
| 112 final AnalysisContext _context; |
| 111 final CoercionManager _cm; | 113 final CoercionManager _cm; |
| 112 final TypeManager _tm; | 114 final TypeManager _tm; |
| 113 final VariableManager _vm; | 115 final VariableManager _vm; |
| 114 final LibraryUnit _library; | 116 final LibraryUnit _library; |
| 115 SourceFile _file; | |
| 116 bool _skipCoercions = false; | 117 bool _skipCoercions = false; |
| 117 final TypeRules _rules; | 118 final TypeRules _rules; |
| 118 final _Inference _inferrer; | 119 final _Inference _inferrer; |
| 119 final InstrumentedRuntime _runtime; | 120 final InstrumentedRuntime _runtime; |
| 120 final CompilerOptions _options; | 121 final CompilerOptions _options; |
| 121 | 122 |
| 123 CompilationUnit _unit; |
| 124 |
| 122 CoercionReifier._(this._cm, this._tm, this._vm, this._library, this._rules, | 125 CoercionReifier._(this._cm, this._tm, this._vm, this._library, this._rules, |
| 123 this._inferrer, this._runtime, this._options); | 126 this._inferrer, this._runtime, this._context, this._options); |
| 124 | 127 |
| 125 factory CoercionReifier( | 128 factory CoercionReifier(LibraryUnit library, AbstractCompiler compiler, |
| 126 LibraryUnit library, TypeRules rules, CompilerOptions options, | |
| 127 [InstrumentedRuntime runtime]) { | 129 [InstrumentedRuntime runtime]) { |
| 128 var vm = new VariableManager(); | 130 var vm = new VariableManager(); |
| 129 var tm = | 131 var tm = |
| 130 new TypeManager(library.library.element.enclosingElement, vm, runtime); | 132 new TypeManager(library.library.element.enclosingElement, vm, runtime); |
| 133 var rules = compiler.rules; |
| 131 var cm = new CoercionManager(vm, tm, rules, runtime); | 134 var cm = new CoercionManager(vm, tm, rules, runtime); |
| 132 var inferrer = new _Inference(rules, tm); | 135 var inferrer = new _Inference(rules, tm); |
| 136 var context = compiler.context; |
| 137 var options = compiler.options; |
| 133 return new CoercionReifier._( | 138 return new CoercionReifier._( |
| 134 cm, tm, vm, library, rules, inferrer, runtime, options); | 139 cm, tm, vm, library, rules, inferrer, runtime, context, options); |
| 135 } | 140 } |
| 136 | 141 |
| 137 // This should be the entry point for this class. Entering via the | 142 // This should be the entry point for this class. Entering via the |
| 138 // visit functions directly may not do the right thing with respect | 143 // visit functions directly may not do the right thing with respect |
| 139 // to discharging the collected definitions. | 144 // to discharging the collected definitions. |
| 140 // Returns the set of new type identifiers added by the reifier | 145 // Returns the set of new type identifiers added by the reifier |
| 141 Map<Identifier, NewTypeIdDesc> reify() { | 146 Map<Identifier, NewTypeIdDesc> reify() { |
| 142 _library.partsThenLibrary.forEach(generateUnit); | 147 _library.partsThenLibrary.forEach(generateUnit); |
| 143 return _tm.addedTypes; | 148 return _tm.addedTypes; |
| 144 } | 149 } |
| 145 | 150 |
| 146 void generateUnit(CompilationUnit unit) { | 151 void generateUnit(CompilationUnit unit) { |
| 147 _file = new SourceFile(unit.element.source.contents.data, | 152 _unit = unit; |
| 148 url: unit.element.source.uri); | |
| 149 visitCompilationUnit(unit); | 153 visitCompilationUnit(unit); |
| 150 _file = null; | 154 _unit = null; |
| 151 } | 155 } |
| 152 | 156 |
| 153 ///////////////// Private ////////////////////////////////// | 157 ///////////////// Private ////////////////////////////////// |
| 154 | 158 |
| 155 String _locationInfo(Expression e) { | 159 String _locationInfo(Expression e) { |
| 156 if (_file != null) { | 160 if (_unit != null) { |
| 157 final begin = e is AnnotatedNode | 161 final begin = e is AnnotatedNode |
| 158 ? (e as AnnotatedNode).firstTokenAfterCommentAndMetadata.offset | 162 ? (e as AnnotatedNode).firstTokenAfterCommentAndMetadata.offset |
| 159 : e.offset; | 163 : e.offset; |
| 160 if (begin != 0 && e.end > begin) { | 164 if (begin != 0 && e.end > begin) { |
| 161 var span = _file.span(begin, e.end); | 165 var span = utils.createSpan(_context, _unit, begin, e.end); |
| 162 var s = span.message("Cast"); | 166 var s = span.message("Cast"); |
| 163 return s.substring(0, s.indexOf("Cast")); | 167 return s.substring(0, s.indexOf("Cast")); |
| 164 } | 168 } |
| 165 } | 169 } |
| 166 return null; | 170 return null; |
| 167 } | 171 } |
| 168 | 172 |
| 169 static String _conversionKind(Conversion node) { | 173 static String _conversionKind(Conversion node) { |
| 170 if (node is ClosureWrapLiteral) return "WrapLiteral"; | 174 if (node is ClosureWrapLiteral) return "WrapLiteral"; |
| 171 if (node is ClosureWrap) return "Wrap"; | 175 if (node is ClosureWrap) return "Wrap"; |
| (...skipping 685 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 857 assert(_runtime != null); | 861 assert(_runtime != null); |
| 858 if (t.typeArguments != null && t.typeArguments.length > 0) { | 862 if (t.typeArguments != null && t.typeArguments.length > 0) { |
| 859 var w = AstBuilder.identifierFromString("_"); | 863 var w = AstBuilder.identifierFromString("_"); |
| 860 var fp = AstBuilder.simpleFormal(w, t); | 864 var fp = AstBuilder.simpleFormal(w, t); |
| 861 var f = AstBuilder.blockFunction(<FormalParameter>[fp], <Statement>[]); | 865 var f = AstBuilder.blockFunction(<FormalParameter>[fp], <Statement>[]); |
| 862 return _runtime.type(f); | 866 return _runtime.type(f); |
| 863 } | 867 } |
| 864 return t.name; | 868 return t.name; |
| 865 } | 869 } |
| 866 } | 870 } |
| OLD | NEW |