Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(155)

Side by Side Diff: lib/src/codegen/reify_coercions.dart

Issue 1840713003: fix to run against latest analyzer (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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'; 7 import 'package:analyzer/dart/element/element.dart';
8 import 'package:analyzer/dart/element/type.dart'; 8 import 'package:analyzer/dart/element/type.dart';
9 import 'package:analyzer/src/dart/ast/ast.dart' show FunctionBodyImpl;
9 import 'package:analyzer/src/dart/ast/utilities.dart' show NodeReplacer; 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;
10 import 'package:analyzer/src/task/strong/info.dart'; 13 import 'package:analyzer/src/task/strong/info.dart';
11 import 'package:logging/logging.dart' as logger; 14 import 'package:logging/logging.dart' as logger;
12 15
13 import 'ast_builder.dart'; 16 import 'ast_builder.dart';
14 17
15 final _log = new logger.Logger('dev_compiler.reify_coercions'); 18 final _log = new logger.Logger('dev_compiler.reify_coercions');
16 19
17 class NewTypeIdDesc { 20 class NewTypeIdDesc {
18 /// If null, then this is not a library level identifier (i.e. it's 21 /// If null, then this is not a library level identifier (i.e. it's
19 /// a type parameter, or a special type like void, dynamic, etc) 22 /// a type parameter, or a special type like void, dynamic, etc)
20 LibraryElement importedFrom; 23 LibraryElement importedFrom;
21 24
22 /// True => use/def in same library 25 /// True => use/def in same library
23 bool fromCurrent; 26 bool fromCurrent;
24 27
25 /// True => not a source variable 28 /// True => not a source variable
26 bool synthetic; 29 bool synthetic;
27 NewTypeIdDesc({this.fromCurrent, this.importedFrom, this.synthetic}); 30 NewTypeIdDesc({this.fromCurrent, this.importedFrom, this.synthetic});
28 } 31 }
29 32
30 // This class implements a pass which modifies (in place) the ast replacing 33 // This class implements a pass which modifies (in place) the ast replacing
31 // abstract coercion nodes with their dart implementations. 34 // abstract coercion nodes with their dart implementations.
32 class CoercionReifier extends analyzer.GeneralizingAstVisitor<Object> { 35 class CoercionReifier extends analyzer.GeneralizingAstVisitor<Object> {
36 final cloner = new _TreeCloner();
33 37
34 CoercionReifier(); 38 /// Makes coercions explicit in the resolved AST, and returns the new AST.
35 39 ///
36 /// This should be the entry point for this class. 40 /// This should be the entry point for this class.
41 /// Entering via the visit functions directly will incorrectly mutate the AST.
37 /// 42 ///
38 /// Entering via the visit functions directly may not do the right 43 /// Returns the new compilation units.
39 /// thing with respect to discharging the collected definitions. 44 List<CompilationUnit> reify(List<CompilationUnit> units) {
40 /// 45 // Copy the AST before modifying it.
41 /// Returns the set of new type identifiers added by the reifier 46 units = units.map(_clone).toList();
42 void reify(List<CompilationUnit> units) { 47 // Visit the AST and make coercions explicit.
43 units.forEach(visitCompilationUnit); 48 units.forEach(visitCompilationUnit);
49 return units;
44 } 50 }
45 51
46 @override 52 @override
47 Object visitExpression(Expression node) { 53 visitExpression(Expression node) {
48 var info = CoercionInfo.get(node); 54 var coercion = CoercionInfo.get(node);
49 if (info is DownCast) { 55 if (coercion is DownCast) {
50 return _visitDownCast(info, node); 56 return _visitDownCast(coercion, node);
51 } 57 }
52 return super.visitExpression(node); 58 return super.visitExpression(node);
53 } 59 }
54 60
55 ///////////////// Private ////////////////////////////////// 61 @override
62 visitForEachStatement(ForEachStatement node) {
63 // Visit other children.
64 node.iterable.accept(this);
65 node.body.accept(this);
56 66
57 Object _visitDownCast(DownCast node, Expression expr) { 67 // If needed, assert a cast inside the body before the variable is read.
58 var parent = expr.parent; 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) {
59 expr.visitChildren(this); 90 expr.visitChildren(this);
60 Expression newE = coerceExpression(expr, node); 91 _replaceNode(expr.parent, expr, coerceExpression(expr, node));
61 if (!identical(expr, newE)) { 92 }
62 var replaced = parent.accept(new NodeReplacer(expr, newE)); 93
94 void _replaceNode(AstNode parent, AstNode oldNode, AstNode newNode) {
95 if (!identical(oldNode, newNode)) {
96 var replaced = parent.accept(new NodeReplacer(oldNode, newNode));
63 // It looks like NodeReplacer will always return true. 97 // It looks like NodeReplacer will always return true.
64 // It does throw IllegalArgumentException though, if child is not found. 98 // It does throw IllegalArgumentException though, if child is not found.
65 assert(replaced); 99 assert(replaced);
66 } 100 }
67 return null;
68 } 101 }
69 102
70 /// Coerce [e] using [c], returning a new expression. 103 /// Coerce [e] using [c], returning a new expression.
71 Expression coerceExpression(Expression e, DownCast node) { 104 Expression coerceExpression(Expression e, DownCast node) {
72 if (e is NamedExpression) { 105 if (e is NamedExpression) {
73 Expression inner = coerceExpression(e.expression, node); 106 Expression inner = coerceExpression(e.expression, node);
74 return new NamedExpression(e.name, inner); 107 return new NamedExpression(e.name, inner);
75 } 108 }
76 return _castExpression(e, node.convertedType); 109 return _castExpression(e, node.convertedType);
77 } 110 }
78 111
79 ///////////////// Private //////////////////////////////////
80
81 Expression _castExpression(Expression e, DartType toType) { 112 Expression _castExpression(Expression e, DartType toType) {
82 // We use an empty name in the AST, because the JS code generator only cares 113 // We use an empty name in the AST, because the JS code generator only cares
83 // about the target type. It does not look at the AST name. 114 // about the target type. It does not look at the AST name.
84 var typeName = new TypeName(AstBuilder.identifierFromString(''), null); 115 var typeName = new TypeName(AstBuilder.identifierFromString(''), null);
85 typeName.type = toType; 116 typeName.type = toType;
86 var cast = AstBuilder.asExpression(e, typeName); 117 var cast = AstBuilder.asExpression(e, typeName);
87 cast.staticType = toType; 118 cast.staticType = toType;
88 return cast; 119 return cast;
89 } 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 }
90 } 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 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698