| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 import 'package:analyzer/analyzer.dart' as analyzer; | |
| 6 import 'package:analyzer/dart/ast/ast.dart'; | |
| 7 import 'package:analyzer/dart/element/element.dart'; | |
| 8 import 'package:analyzer/dart/element/type.dart'; | |
| 9 import 'package:analyzer/src/dart/ast/ast.dart' show FunctionBodyImpl; | |
| 10 import 'package:analyzer/src/dart/ast/utilities.dart' show NodeReplacer; | |
| 11 import 'package:analyzer/src/dart/element/type.dart' show DynamicTypeImpl; | |
| 12 import 'package:analyzer/src/generated/parser.dart' show ResolutionCopier; | |
| 13 import 'package:analyzer/src/task/strong/info.dart'; | |
| 14 import 'package:logging/logging.dart' as logger; | |
| 15 | |
| 16 import 'ast_builder.dart'; | |
| 17 | |
| 18 final _log = new logger.Logger('dev_compiler.reify_coercions'); | |
| 19 | |
| 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 | |
| 34 // abstract coercion nodes with their dart implementations. | |
| 35 class CoercionReifier extends analyzer.GeneralizingAstVisitor<Object> { | |
| 36 final cloner = new _TreeCloner(); | |
| 37 | |
| 38 /// Makes coercions explicit in the resolved AST, and returns the new AST. | |
| 39 /// | |
| 40 /// This should be the entry point for this class. | |
| 41 /// Entering via the visit functions directly will incorrectly mutate the AST. | |
| 42 /// | |
| 43 /// Returns the new compilation units. | |
| 44 List<CompilationUnit> reify(List<CompilationUnit> units) { | |
| 45 // Copy the AST before modifying it. | |
| 46 units = units.map(_clone).toList(); | |
| 47 // Visit the AST and make coercions explicit. | |
| 48 units.forEach(visitCompilationUnit); | |
| 49 return units; | |
| 50 } | |
| 51 | |
| 52 @override | |
| 53 visitExpression(Expression node) { | |
| 54 var coercion = CoercionInfo.get(node); | |
| 55 if (coercion is DownCast) { | |
| 56 return _visitDownCast(coercion, node); | |
| 57 } | |
| 58 return super.visitExpression(node); | |
| 59 } | |
| 60 | |
| 61 @override | |
| 62 visitForEachStatement(ForEachStatement node) { | |
| 63 // Visit other children. | |
| 64 node.iterable.accept(this); | |
| 65 node.body.accept(this); | |
| 66 | |
| 67 // If needed, assert a cast inside the body before the variable is read. | |
| 68 var variable = node.identifier ?? node.loopVariable.identifier; | |
| 69 var coercion = CoercionInfo.get(variable); | |
| 70 if (coercion is DownCast) { | |
| 71 // Build the cast. We will place this cast in the body, so need to clone | |
| 72 // the variable's AST node and clear out its static type (otherwise we | |
| 73 // will optimize away the cast). | |
| 74 var cast = _castExpression( | |
| 75 _clone(variable)..staticType = DynamicTypeImpl.instance, | |
| 76 coercion.convertedType); | |
| 77 | |
| 78 var body = node.body; | |
| 79 var blockBody = <Statement>[RawAstBuilder.expressionStatement(cast)]; | |
| 80 if (body is Block) { | |
| 81 blockBody.addAll(body.statements); | |
| 82 } else { | |
| 83 blockBody.add(body); | |
| 84 } | |
| 85 _replaceNode(node, body, RawAstBuilder.block(blockBody)); | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 void _visitDownCast(DownCast node, Expression expr) { | |
| 90 expr.visitChildren(this); | |
| 91 _replaceNode(expr.parent, expr, coerceExpression(expr, node)); | |
| 92 } | |
| 93 | |
| 94 void _replaceNode(AstNode parent, AstNode oldNode, AstNode newNode) { | |
| 95 if (!identical(oldNode, newNode)) { | |
| 96 var replaced = parent.accept(new NodeReplacer(oldNode, newNode)); | |
| 97 // It looks like NodeReplacer will always return true. | |
| 98 // It does throw IllegalArgumentException though, if child is not found. | |
| 99 assert(replaced); | |
| 100 } | |
| 101 } | |
| 102 | |
| 103 /// Coerce [e] using [c], returning a new expression. | |
| 104 Expression coerceExpression(Expression e, DownCast node) { | |
| 105 if (e is NamedExpression) { | |
| 106 Expression inner = coerceExpression(e.expression, node); | |
| 107 return new NamedExpression(e.name, inner); | |
| 108 } | |
| 109 return _castExpression(e, node.convertedType); | |
| 110 } | |
| 111 | |
| 112 Expression _castExpression(Expression e, DartType toType) { | |
| 113 // We use an empty name in the AST, because the JS code generator only cares | |
| 114 // about the target type. It does not look at the AST name. | |
| 115 var typeName = new TypeName(AstBuilder.identifierFromString(''), null); | |
| 116 typeName.type = toType; | |
| 117 var cast = AstBuilder.asExpression(e, typeName); | |
| 118 cast.staticType = toType; | |
| 119 return cast; | |
| 120 } | |
| 121 | |
| 122 /*=T*/ _clone/*<T extends AstNode>*/(/*=T*/ node) { | |
| 123 var copy = node.accept(cloner); | |
| 124 ResolutionCopier.copyResolutionData(node, copy); | |
| 125 return copy; | |
| 126 } | |
| 127 } | |
| 128 | |
| 129 class _TreeCloner extends analyzer.AstCloner { | |
| 130 void _cloneProperties(AstNode clone, AstNode node) { | |
| 131 if (clone != null) { | |
| 132 CoercionInfo.set(clone, CoercionInfo.get(node)); | |
| 133 DynamicInvoke.set(clone, DynamicInvoke.get(node)); | |
| 134 } | |
| 135 } | |
| 136 | |
| 137 @override | |
| 138 AstNode cloneNode(AstNode node) { | |
| 139 var clone = super.cloneNode(node); | |
| 140 _cloneProperties(clone, node); | |
| 141 return clone; | |
| 142 } | |
| 143 | |
| 144 @override | |
| 145 List cloneNodeList(List list) { | |
| 146 var clone = super.cloneNodeList(list); | |
| 147 for (int i = 0, len = list.length; i < len; i++) { | |
| 148 _cloneProperties(clone[i], list[i]); | |
| 149 } | |
| 150 return clone; | |
| 151 } | |
| 152 | |
| 153 // TODO(jmesserly): ResolutionCopier is not copying this yet. | |
| 154 @override | |
| 155 BlockFunctionBody visitBlockFunctionBody(BlockFunctionBody node) { | |
| 156 var clone = super.visitBlockFunctionBody(node); | |
| 157 (clone as FunctionBodyImpl).localVariableInfo = | |
| 158 (node as FunctionBodyImpl).localVariableInfo; | |
| 159 return clone; | |
| 160 } | |
| 161 | |
| 162 @override | |
| 163 ExpressionFunctionBody visitExpressionFunctionBody( | |
| 164 ExpressionFunctionBody node) { | |
| 165 var clone = super.visitExpressionFunctionBody(node); | |
| 166 (clone as FunctionBodyImpl).localVariableInfo = | |
| 167 (node as FunctionBodyImpl).localVariableInfo; | |
| 168 return clone; | |
| 169 } | |
| 170 } | |
| OLD | NEW |