| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 /** | 5 /** |
| 6 * Dart backend helper for converting program IR back to source code. | 6 * Dart backend helper for converting program IR back to source code. |
| 7 */ | 7 */ |
| 8 class Emitter { | 8 class Emitter { |
| 9 | 9 |
| 10 final Compiler compiler; | 10 final Compiler compiler; |
| 11 final StringBuffer sb; | 11 final StringBuffer sb; |
| 12 final Renamer renamer; |
| 12 | 13 |
| 13 Emitter(this.compiler) : sb = new StringBuffer(); | 14 Emitter(Compiler compiler) : |
| 15 this.compiler = compiler, |
| 16 sb = new StringBuffer(), |
| 17 renamer = new ConflictingRenamer(compiler); |
| 14 | 18 |
| 15 /** | 19 /** |
| 16 * Outputs given class element with selected inner elements. | 20 * Outputs given class element with selected inner elements. |
| 17 */ | 21 */ |
| 18 void outputClass(ClassElement classElement, Set<Element> innerElements) { | 22 void outputClass(ClassElement classElement, Set<Element> innerElements) { |
| 23 Unparser unparser = new Unparser(renamer); |
| 24 renamer.setContext(classElement.getCompilationUnit()); |
| 19 ClassNode classNode = classElement.parseNode(compiler); | 25 ClassNode classNode = classElement.parseNode(compiler); |
| 20 // classElement.beginToken is 'class', 'interface', or 'abstract'. | 26 // classElement.beginToken is 'class', 'interface', or 'abstract'. |
| 21 sb.add(classElement.beginToken.slowToString()); | 27 sb.add(classElement.beginToken.slowToString()); |
| 22 if (classElement.beginToken.slowToString() == 'abstract') { | 28 if (classElement.beginToken.slowToString() == 'abstract') { |
| 23 sb.add(' '); | 29 sb.add(' '); |
| 24 sb.add(classElement.beginToken.next.slowToString()); // 'class' | 30 sb.add(classElement.beginToken.next.slowToString()); // 'class' |
| 25 } | 31 } |
| 26 sb.add(' '); | 32 sb.add(' '); |
| 27 sb.add(classNode.name.unparse()); | 33 sb.add(renamer.renameType(classElement.type)); |
| 28 if (classNode.typeParameters !== null) { | 34 if (classNode.typeParameters !== null) { |
| 29 sb.add(classNode.typeParameters.unparse()); | 35 sb.add(unparser.unparse(classNode.typeParameters)); |
| 30 } | 36 } |
| 31 if (classNode.extendsKeyword !== null) { | 37 if (classNode.extendsKeyword !== null) { |
| 32 sb.add(' '); | 38 sb.add(' '); |
| 33 classNode.extendsKeyword.value.printOn(sb); | 39 classNode.extendsKeyword.value.printOn(sb); |
| 34 sb.add(' '); | 40 sb.add(' '); |
| 35 sb.add(classNode.superclass.unparse()); | 41 sb.add(renamer.renameType(classElement.supertype)); |
| 36 } | 42 } |
| 37 if (!classNode.interfaces.isEmpty()) { | 43 if (!classNode.interfaces.isEmpty()) { |
| 38 sb.add(classElement.isInterface() ? ' extends ' : ' implements '); | 44 sb.add(classElement.isInterface() ? ' extends ' : ' implements '); |
| 39 classNode.interfaces.nodes.printOn(sb, classNode.interfaces.delimiter); | 45 sb.add(unparser.unparse(classNode.interfaces)); |
| 40 } | 46 } |
| 41 if (classNode.defaultClause !== null) { | 47 if (classNode.defaultClause !== null) { |
| 42 sb.add(' default '); | 48 sb.add(' default '); |
| 43 sb.add(classNode.defaultClause.unparse()); | 49 sb.add(unparser.unparse(classNode.defaultClause)); |
| 44 } | 50 } |
| 45 sb.add('{'); | 51 sb.add('{'); |
| 46 innerElements.forEach((element) { | 52 innerElements.forEach((element) { |
| 47 // TODO(smok): Filter out default constructors here. | 53 // TODO(smok): Filter out default constructors here. |
| 48 outputElement(element); | 54 outputElement(element); |
| 49 }); | 55 }); |
| 50 sb.add('}'); | 56 sb.add('}'); |
| 51 } | 57 } |
| 52 | 58 |
| 53 void outputElement(Element element) { | 59 void outputElement(Element element) { |
| 60 Unparser unparser = new Unparser(renamer); |
| 61 renamer.setContext(element); |
| 54 // TODO(smok): Figure out why AbstractFieldElement appears here, | 62 // TODO(smok): Figure out why AbstractFieldElement appears here, |
| 55 // we have used getters/setters resolved instead of it. | 63 // we have used getters/setters resolved instead of it. |
| 56 if (element is SynthesizedConstructorElement | 64 if (element is SynthesizedConstructorElement |
| 57 || element is AbstractFieldElement) return; | 65 || element is AbstractFieldElement) return; |
| 58 if (element.isField()) { | 66 if (element.isField()) { |
| 59 assert(element is VariableElement); | 67 assert(element is VariableElement); |
| 60 sb.add(element.variables.parseNode(compiler).unparse()); | 68 sb.add(unparser.unparse(element.variables.parseNode(compiler))); |
| 61 } else { | 69 } else { |
| 62 sb.add(element.parseNode(compiler).unparse()); | 70 sb.add(unparser.unparse(element.parseNode(compiler))); |
| 63 } | 71 } |
| 64 } | 72 } |
| 65 | 73 |
| 66 String toString() => sb.toString(); | 74 String toString() => sb.toString(); |
| 67 } | 75 } |
| OLD | NEW |