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

Side by Side Diff: pkg/compiler/lib/src/cps_ir/cps_ir_nodes_sexpr.dart

Issue 810783002: Clean up FunctionDefinition in the IR S-expression representation. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, 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 dart2js.ir_nodes_sexpr; 5 library dart2js.ir_nodes_sexpr;
6 6
7 import '../util/util.dart'; 7 import '../util/util.dart';
8 import 'cps_ir_nodes.dart'; 8 import 'cps_ir_nodes.dart';
9 9
10 /// A [Decorator] is a function used by [SExpressionStringifier] to augment the 10 /// A [Decorator] is a function used by [SExpressionStringifier] to augment the
11 /// output produced for a node. It can be provided to the constructor. 11 /// output produced for a node. It can be provided to the constructor.
12 typedef String Decorator(Node node, String s); 12 typedef String Decorator(Node node, String s);
13 13
14 /// Generate a Lisp-like S-expression representation of an IR node as a string. 14 /// Generate a Lisp-like S-expression representation of an IR node as a string.
15 class SExpressionStringifier extends Visitor<String> with Indentation { 15 class SExpressionStringifier extends Visitor<String> with Indentation {
16 final _Namer namer = new _Namer(); 16 final _Namer namer = new _Namer();
17 17
18 String newValueName(Node node) => namer.defineValueName(node); 18 String newValueName(Primitive node) => namer.nameValue(node);
19 String newContinuationName(Node node) => namer.defineContinuationName(node); 19 String newContinuationName(Continuation node) => namer.nameContinuation(node);
20 Decorator decorator; 20 Decorator decorator;
21 21
22 SExpressionStringifier([this.decorator]) { 22 SExpressionStringifier([this.decorator]) {
23 if (this.decorator == null) { 23 if (this.decorator == null) {
24 this.decorator = (Node node, String s) => s; 24 this.decorator = (Node node, String s) => s;
25 } 25 }
26 } 26 }
27 27
28 String access(Reference<Definition> r) { 28 String access(Reference<Definition> r) {
29 return decorator(r.definition, namer.getName(r.definition)); 29 return decorator(r.definition, namer.getName(r.definition));
30 } 30 }
31 31
32 String visitParameter(Parameter node) { 32 String visitParameter(Parameter node) {
33 return namer.useElementName(node); 33 return namer.nameParameter(node);
34 } 34 }
35 35
36 String visitClosureVariable(ClosureVariable node) { 36 String visitClosureVariable(ClosureVariable node) {
37 return namer.getName(node); 37 return namer.getName(node);
38 } 38 }
39 39
40 /// Main entry point for creating a [String] from a [Node]. All recursive 40 /// Main entry point for creating a [String] from a [Node]. All recursive
41 /// calls must go through this method. 41 /// calls must go through this method.
42 String visit(Node node) { 42 String visit(Node node) {
43 String s = super.visit(node); 43 String s = super.visit(node);
44 return (decorator == null) ? s : decorator(node, s); 44 return decorator(node, s);
45 } 45 }
46 46
47 String visitFunctionDefinition(FunctionDefinition node) { 47 String visitFunctionDefinition(FunctionDefinition node) {
48 String name = node.element.name; 48 String name = node.element.name;
49 namer.useReturnName(node.returnContinuation); 49 namer.setReturnContinuation(node.returnContinuation);
50 String closureVariables = node.closureVariables.isEmpty 50 String closureVariables =
51 ? '' 51 node.closureVariables.map(namer.nameClosureVariable).join(' ');
52 : '{${node.closureVariables.map(namer.defineClosureName).join(' ')}} ';
53 String parameters = node.parameters.map(visit).join(' '); 52 String parameters = node.parameters.map(visit).join(' ');
54 String body = indentBlock(() => visit(node.body)); 53 String body = indentBlock(() => visit(node.body));
55 return '$indentation(FunctionDefinition $name $closureVariables' 54 return '$indentation(FunctionDefinition $name ($parameters) return'
56 '($parameters return)\n$body)'; 55 ' ($closureVariables)\n$body)';
57 } 56 }
58 57
59 String visitFieldDefinition(FieldDefinition node) { 58 String visitFieldDefinition(FieldDefinition node) {
60 String name = node.element.name; 59 String name = node.element.name;
61 if (node.hasInitializer) { 60 if (node.hasInitializer) {
62 namer.useReturnName(node.returnContinuation); 61 namer.setReturnContinuation(node.returnContinuation);
63 String body = indentBlock(() => visit(node.body)); 62 String body = indentBlock(() => visit(node.body));
64 return '$indentation(FieldDefinition $name (return)\n' 63 return '$indentation(FieldDefinition $name (return)\n'
65 '$body)'; 64 '$body)';
66 } else { 65 } else {
67 return '$indentation(FieldDefinition $name)'; 66 return '$indentation(FieldDefinition $name)';
68 } 67 }
69 } 68 }
70 69
71 String visitLetPrim(LetPrim node) { 70 String visitLetPrim(LetPrim node) {
72 String name = newValueName(node.primitive); 71 String name = newValueName(node.primitive);
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
229 String right = access(node.right); 228 String right = access(node.right);
230 return '(Identical $left $right)'; 229 return '(Identical $left $right)';
231 } 230 }
232 } 231 }
233 232
234 class _Namer { 233 class _Namer {
235 final Map<Node, String> _names = <Node, String>{}; 234 final Map<Node, String> _names = <Node, String>{};
236 int _valueCounter = 0; 235 int _valueCounter = 0;
237 int _continuationCounter = 0; 236 int _continuationCounter = 0;
238 237
239 String useElementName(Parameter parameter) { 238 String nameParameter(Parameter parameter) {
240 assert(!_names.containsKey(parameter)); 239 assert(!_names.containsKey(parameter));
241 return _names[parameter] = parameter.hint.name; 240 return _names[parameter] = parameter.hint.name;
242 } 241 }
243 242
244 String defineClosureName(ClosureVariable variable) { 243 String nameClosureVariable(ClosureVariable variable) {
245 assert(!_names.containsKey(variable)); 244 assert(!_names.containsKey(variable));
246 return _names[variable] = variable.hint.name; 245 return _names[variable] = variable.hint.name;
247 } 246 }
248 247
249 String defineContinuationName(Node node) { 248 String nameContinuation(Continuation node) {
250 assert(!_names.containsKey(node)); 249 assert(!_names.containsKey(node));
251 return _names[node] = 'k${_continuationCounter++}'; 250 return _names[node] = 'k${_continuationCounter++}';
252 } 251 }
253 252
254 String defineValueName(Node node) { 253 String nameValue(Primitive node) {
255 assert(!_names.containsKey(node)); 254 assert(!_names.containsKey(node));
256 return _names[node] = 'v${_valueCounter++}'; 255 return _names[node] = 'v${_valueCounter++}';
257 } 256 }
258 257
259 String useReturnName(Continuation node) { 258 void setReturnContinuation(Continuation node) {
260 assert(!_names.containsKey(node) || _names[node] == 'return'); 259 assert(!_names.containsKey(node) || _names[node] == 'return');
261 return _names[node] = 'return'; 260 _names[node] = 'return';
262 } 261 }
263 262
264 String getName(Node node) { 263 String getName(Node node) {
265 assert(_names.containsKey(node)); 264 assert(_names.containsKey(node));
266 return _names[node]; 265 return _names[node];
267 } 266 }
268 } 267 }
OLDNEW
« no previous file with comments | « pkg/analyzer2dart/test/sexpr_data.dart ('k') | tests/compiler/dart2js/backend_dart/opt_constprop_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698