| 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'; | |
| 8 import 'package:analyzer/dart/element/type.dart'; | 7 import 'package:analyzer/dart/element/type.dart'; |
| 9 import 'package:analyzer/src/dart/ast/ast.dart' show FunctionBodyImpl; | 8 import 'package:analyzer/src/dart/ast/ast.dart' show FunctionBodyImpl; |
| 10 import 'package:analyzer/src/dart/ast/utilities.dart' show NodeReplacer; | 9 import 'package:analyzer/src/dart/ast/utilities.dart' show NodeReplacer; |
| 11 import 'package:analyzer/src/dart/element/type.dart' show DynamicTypeImpl; | 10 import 'package:analyzer/src/dart/element/type.dart' show DynamicTypeImpl; |
| 12 import 'package:analyzer/src/generated/parser.dart' show ResolutionCopier; | 11 import 'package:analyzer/src/generated/parser.dart' show ResolutionCopier; |
| 13 import 'package:analyzer/src/task/strong/info.dart'; | 12 import 'package:analyzer/src/task/strong/info.dart'; |
| 14 import 'package:logging/logging.dart' as logger; | 13 import 'package:logging/logging.dart' as logger; |
| 15 | 14 |
| 16 import 'ast_builder.dart'; | 15 import 'ast_builder.dart'; |
| 17 | 16 |
| 18 final _log = new logger.Logger('dev_compiler.reify_coercions'); | 17 final _log = new logger.Logger('dev_compiler.reify_coercions'); |
| 19 | 18 |
| 20 class NewTypeIdDesc { | |
| 21 /// 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) | |
| 23 LibraryElement importedFrom; | |
| 24 | |
| 25 /// True => use/def in same library | |
| 26 bool fromCurrent; | |
| 27 | |
| 28 /// True => not a source variable | |
| 29 bool synthetic; | |
| 30 NewTypeIdDesc({this.fromCurrent, this.importedFrom, this.synthetic}); | |
| 31 } | |
| 32 | |
| 33 // This class implements a pass which modifies (in place) the ast replacing | 19 // This class implements a pass which modifies (in place) the ast replacing |
| 34 // abstract coercion nodes with their dart implementations. | 20 // abstract coercion nodes with their dart implementations. |
| 35 class CoercionReifier extends analyzer.GeneralizingAstVisitor<Object> { | 21 class CoercionReifier extends analyzer.GeneralizingAstVisitor<Object> { |
| 36 final cloner = new _TreeCloner(); | 22 final cloner = new _TreeCloner(); |
| 37 | 23 |
| 38 /// Makes coercions explicit in the resolved AST, and returns the new AST. | 24 CoercionReifier._(); |
| 39 /// | 25 |
| 40 /// This should be the entry point for this class. | 26 /// Transforms the given compilation units, and returns a new AST with |
| 41 /// Entering via the visit functions directly will incorrectly mutate the AST. | 27 /// explicit coercion nodes in appropriate places. |
| 42 /// | 28 static List<CompilationUnit> reify(List<CompilationUnit> units) { |
| 43 /// Returns the new compilation units. | 29 var cr = new CoercionReifier._(); |
| 44 List<CompilationUnit> reify(List<CompilationUnit> units) { | 30 // Clone compilation units, so we don't modify the originals. |
| 45 // Copy the AST before modifying it. | 31 units = units.map(cr._clone).toList(growable: false); |
| 46 units = units.map(_clone).toList(); | 32 units.forEach(cr.visitCompilationUnit); |
| 47 // Visit the AST and make coercions explicit. | |
| 48 units.forEach(visitCompilationUnit); | |
| 49 return units; | 33 return units; |
| 50 } | 34 } |
| 51 | 35 |
| 52 @override | 36 @override |
| 53 visitExpression(Expression node) { | 37 visitExpression(Expression node) { |
| 54 var coercion = CoercionInfo.get(node); | 38 var coercion = CoercionInfo.get(node); |
| 55 if (coercion is DownCast) { | 39 if (coercion is DownCast) { |
| 56 return _visitDownCast(coercion, node); | 40 return _visitDownCast(coercion, node); |
| 57 } | 41 } |
| 58 return super.visitExpression(node); | 42 return super.visitExpression(node); |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 | 145 |
| 162 @override | 146 @override |
| 163 ExpressionFunctionBody visitExpressionFunctionBody( | 147 ExpressionFunctionBody visitExpressionFunctionBody( |
| 164 ExpressionFunctionBody node) { | 148 ExpressionFunctionBody node) { |
| 165 var clone = super.visitExpressionFunctionBody(node); | 149 var clone = super.visitExpressionFunctionBody(node); |
| 166 (clone as FunctionBodyImpl).localVariableInfo = | 150 (clone as FunctionBodyImpl).localVariableInfo = |
| 167 (node as FunctionBodyImpl).localVariableInfo; | 151 (node as FunctionBodyImpl).localVariableInfo; |
| 168 return clone; | 152 return clone; |
| 169 } | 153 } |
| 170 } | 154 } |
| OLD | NEW |