| OLD | NEW |
| 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 '../constants/values.dart'; | 7 import '../constants/values.dart'; |
| 8 import '../util/util.dart'; | 8 import '../util/util.dart'; |
| 9 import 'cps_ir_nodes.dart'; | 9 import 'cps_ir_nodes.dart'; |
| 10 | 10 |
| 11 /// A [Decorator] is a function used by [SExpressionStringifier] to augment the | 11 /// A [Decorator] is a function used by [SExpressionStringifier] to augment the |
| 12 /// output produced for a node. It can be provided to the constructor. | 12 /// output produced for a node. It can be provided to the constructor. |
| 13 typedef String Decorator(Node node, String s); | 13 typedef String Decorator(Node node, String s); |
| 14 | 14 |
| 15 /// Generate a Lisp-like S-expression representation of an IR node as a string. | 15 /// Generate a Lisp-like S-expression representation of an IR node as a string. |
| 16 class SExpressionStringifier extends Visitor<String> with Indentation { | 16 class SExpressionStringifier extends Indentation implements Visitor<String> { |
| 17 final _Namer namer = new _Namer(); | 17 final _Namer namer = new _Namer(); |
| 18 | 18 |
| 19 String newValueName(Primitive node) => namer.nameValue(node); | 19 String newValueName(Primitive node) => namer.nameValue(node); |
| 20 String newContinuationName(Continuation node) => namer.nameContinuation(node); | 20 String newContinuationName(Continuation node) => namer.nameContinuation(node); |
| 21 Decorator decorator; | 21 Decorator decorator; |
| 22 | 22 |
| 23 SExpressionStringifier([this.decorator]) { | 23 SExpressionStringifier([this.decorator]) { |
| 24 if (this.decorator == null) { | 24 if (this.decorator == null) { |
| 25 this.decorator = (Node node, String s) => s; | 25 this.decorator = (Node node, String s) => s; |
| 26 } | 26 } |
| 27 } | 27 } |
| 28 | 28 |
| 29 String access(Reference<Definition> r) { | 29 String access(Reference<Definition> r) { |
| 30 return decorator(r.definition, namer.getName(r.definition)); | 30 return decorator(r.definition, namer.getName(r.definition)); |
| 31 } | 31 } |
| 32 | 32 |
| 33 String visitParameter(Parameter node) { | 33 String visitParameter(Parameter node) { |
| 34 return namer.nameParameter(node); | 34 return namer.nameParameter(node); |
| 35 } | 35 } |
| 36 | 36 |
| 37 String visitMutableVariable(MutableVariable node) { | 37 String visitMutableVariable(MutableVariable node) { |
| 38 return namer.nameMutableVariable(node); | 38 return namer.nameMutableVariable(node); |
| 39 } | 39 } |
| 40 | 40 |
| 41 /// Main entry point for creating a [String] from a [Node]. All recursive | 41 /// Main entry point for creating a [String] from a [Node]. All recursive |
| 42 /// calls must go through this method. | 42 /// calls must go through this method. |
| 43 String visit(Node node) { | 43 String visit(Node node) { |
| 44 String s = super.visit(node); | 44 String s = node.accept(this); |
| 45 return decorator(node, s); | 45 return decorator(node, s); |
| 46 } | 46 } |
| 47 | 47 |
| 48 String visitFunctionDefinition(FunctionDefinition node) { | 48 String visitFunctionDefinition(FunctionDefinition node) { |
| 49 String name = node.element.name; | 49 String name = node.element.name; |
| 50 namer.setReturnContinuation(node.body.returnContinuation); | |
| 51 String parameters = node.parameters.map(visit).join(' '); | 50 String parameters = node.parameters.map(visit).join(' '); |
| 52 String body = indentBlock(() => visit(node.body.body)); | 51 String body = visit(node.body); |
| 53 return '$indentation(FunctionDefinition $name ($parameters) return\n' | 52 return '$indentation(FunctionDefinition $name ($parameters) return\n' |
| 54 '$body)'; | 53 '$body)'; |
| 55 } | 54 } |
| 56 | 55 |
| 57 String visitFieldDefinition(FieldDefinition node) { | 56 String visitFieldDefinition(FieldDefinition node) { |
| 58 String name = node.element.name; | 57 String name = node.element.name; |
| 59 if (node.hasInitializer) { | 58 if (node.hasInitializer) { |
| 60 namer.setReturnContinuation(node.body.returnContinuation); | 59 String body = visit(node.body); |
| 61 String body = indentBlock(() => visit(node.body.body)); | |
| 62 return '$indentation(FieldDefinition $name () return\n' | 60 return '$indentation(FieldDefinition $name () return\n' |
| 63 '$body)'; | 61 '$body)'; |
| 64 } else { | 62 } else { |
| 65 return '$indentation(FieldDefinition $name)'; | 63 return '$indentation(FieldDefinition $name)'; |
| 66 } | 64 } |
| 67 } | 65 } |
| 68 | 66 |
| 67 String visitConstructorDefinition(ConstructorDefinition node) { |
| 68 String name = node.element.name; |
| 69 if (name != '') name = '$name '; |
| 70 String parameters = node.parameters.map(visit).join(' '); |
| 71 if (node.body != null) { |
| 72 String initializers = indentBlock(() { |
| 73 return indentBlock(() { |
| 74 if (node.initializers.isEmpty) { |
| 75 return '$indentation'; |
| 76 } else { |
| 77 return node.initializers.map(visit).join('\n'); |
| 78 } |
| 79 }); |
| 80 }); |
| 81 String body = visit(node.body); |
| 82 return '$indentation(ConstructorDefinition $name($parameters) return' |
| 83 ' (\n$initializers)\n$body)'; |
| 84 } else { |
| 85 return '$indentation(ConstructorDefinition $name($parameters) return)'; |
| 86 } |
| 87 } |
| 88 |
| 89 String visitFieldInitializer(FieldInitializer node) { |
| 90 String name = node.element.name; |
| 91 String body = visit(node.body); |
| 92 return '$indentation(FieldInitializer $name\$body)'; |
| 93 } |
| 94 |
| 95 String visitSuperInitializer(SuperInitializer node) { |
| 96 String target = node.target.name; |
| 97 String selector = node.selector.name; |
| 98 String arguments = |
| 99 indentBlock(() => |
| 100 indentBlock(() => node.arguments.map(visit).join('\n'))); |
| 101 return '$indentation(SuperInitializer $target $selector (\n$arguments)'; |
| 102 } |
| 103 |
| 104 String visitRunnableBody(RunnableBody node) { |
| 105 namer.setReturnContinuation(node.returnContinuation); |
| 106 return indentBlock(() => visit(node.body)); |
| 107 } |
| 108 |
| 69 String visitLetPrim(LetPrim node) { | 109 String visitLetPrim(LetPrim node) { |
| 70 String name = newValueName(node.primitive); | 110 String name = newValueName(node.primitive); |
| 71 String value = visit(node.primitive); | 111 String value = visit(node.primitive); |
| 72 String body = indentBlock(() => visit(node.body)); | 112 String body = indentBlock(() => visit(node.body)); |
| 73 return '$indentation(LetPrim ($name $value)\n$body)'; | 113 return '$indentation(LetPrim ($name $value)\n$body)'; |
| 74 } | 114 } |
| 75 | 115 |
| 76 String visitLetCont(LetCont node) { | 116 String visitLetCont(LetCont node) { |
| 77 String conts; | 117 String conts; |
| 78 bool first = true; | 118 bool first = true; |
| (...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 391 void setReturnContinuation(Continuation node) { | 431 void setReturnContinuation(Continuation node) { |
| 392 assert(!_names.containsKey(node) || _names[node] == 'return'); | 432 assert(!_names.containsKey(node) || _names[node] == 'return'); |
| 393 _names[node] = 'return'; | 433 _names[node] = 'return'; |
| 394 } | 434 } |
| 395 | 435 |
| 396 String getName(Node node) { | 436 String getName(Node node) { |
| 397 assert(_names.containsKey(node)); | 437 assert(_names.containsKey(node)); |
| 398 return _names[node]; | 438 return _names[node]; |
| 399 } | 439 } |
| 400 } | 440 } |
| OLD | NEW |