| 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 ConflictingRenamer renamer; | 12 final ConflictingRenamer renamer; |
| 13 final Set<VariableListElement> processedVariableLists; | 13 final Set<VariableListElement> processedVariableLists; |
| 14 | 14 |
| 15 Emitter(Compiler compiler) : | 15 Emitter(this.compiler, this.renamer) : |
| 16 this.compiler = compiler, | |
| 17 sb = new StringBuffer(), | 16 sb = new StringBuffer(), |
| 18 renamer = new ConflictingRenamer(compiler), | |
| 19 processedVariableLists = new Set<VariableListElement>(); | 17 processedVariableLists = new Set<VariableListElement>(); |
| 20 | 18 |
| 21 /** | 19 /** |
| 22 * Outputs given class element with selected inner elements. | 20 * Outputs given class element with selected inner elements. |
| 23 */ | 21 */ |
| 24 void outputClass(ClassElement classElement, Set<Element> innerElements) { | 22 void outputClass(ClassElement classElement, Set<Element> innerElements) { |
| 25 Unparser unparser = new Unparser(renamer); | 23 Unparser unparser = new Unparser(renamer); |
| 26 ClassNode classNode = classElement.parseNode(compiler); | 24 ClassNode classNode = classElement.parseNode(compiler); |
| 27 // classElement.beginToken is 'class', 'interface', or 'abstract'. | 25 // classElement.beginToken is 'class', 'interface', or 'abstract'. |
| 28 sb.add(classElement.beginToken.slowToString()); | 26 sb.add(classElement.beginToken.slowToString()); |
| 29 if (classElement.beginToken.slowToString() == 'abstract') { | 27 if (classElement.beginToken.slowToString() == 'abstract') { |
| 30 sb.add(' '); | 28 sb.add(' '); |
| 31 sb.add(classElement.beginToken.next.slowToString()); // 'class' | 29 sb.add(classElement.beginToken.next.slowToString()); // 'class' |
| 32 } | 30 } |
| 33 sb.add(' '); | 31 sb.add(' '); |
| 34 sb.add(renamer.renameElement(classElement)); | 32 sb.add(renamer.renameElement(classElement)); |
| 35 if (classNode.typeParameters !== null) { | 33 if (classNode.typeParameters !== null) { |
| 36 sb.add(unparser.unparse(classNode.typeParameters)); | 34 sb.add(unparser.unparse(classNode.typeParameters)); |
| 37 } | 35 } |
| 38 renamer.setContext(classElement); | |
| 39 if (classNode.extendsKeyword !== null) { | 36 if (classNode.extendsKeyword !== null) { |
| 40 sb.add(' '); | 37 sb.add(' '); |
| 41 classNode.extendsKeyword.value.printOn(sb); | 38 classNode.extendsKeyword.value.printOn(sb); |
| 42 sb.add(' '); | 39 sb.add(' '); |
| 43 sb.add(unparser.unparse(classNode.superclass)); | 40 sb.add(unparser.unparse(classNode.superclass)); |
| 44 } | 41 } |
| 45 if (!classNode.interfaces.isEmpty()) { | 42 if (!classNode.interfaces.isEmpty()) { |
| 46 sb.add(' '); | 43 sb.add(' '); |
| 47 sb.add(unparser.unparse(classNode.interfaces)); | 44 sb.add(unparser.unparse(classNode.interfaces)); |
| 48 } | 45 } |
| 49 if (classNode.defaultClause !== null) { | 46 if (classNode.defaultClause !== null) { |
| 50 sb.add(' default '); | 47 sb.add(' default '); |
| 51 sb.add(unparser.unparse(classNode.defaultClause)); | 48 sb.add(unparser.unparse(classNode.defaultClause)); |
| 52 } | 49 } |
| 53 sb.add('{'); | 50 sb.add('{'); |
| 54 innerElements.forEach((element) { | 51 innerElements.forEach((element) { |
| 55 // TODO(smok): Filter out default constructors here. | 52 // TODO(smok): Filter out default constructors here. |
| 56 outputElement(element); | 53 outputElement(element); |
| 57 }); | 54 }); |
| 58 sb.add('}'); | 55 sb.add('}'); |
| 59 } | 56 } |
| 60 | 57 |
| 61 void outputElement(Element element) { | 58 void outputElement(Element element) { |
| 62 Unparser unparser = new Unparser(renamer); | 59 Unparser unparser = new Unparser(renamer); |
| 63 renamer.setContext(element); | |
| 64 // TODO(smok): Figure out why AbstractFieldElement appears here, | 60 // TODO(smok): Figure out why AbstractFieldElement appears here, |
| 65 // we have used getters/setters resolved instead of it. | 61 // we have used getters/setters resolved instead of it. |
| 66 if (element is SynthesizedConstructorElement | 62 if (element is SynthesizedConstructorElement |
| 67 || element is AbstractFieldElement) return; | 63 || element is AbstractFieldElement) return; |
| 68 if (element.isField()) { | 64 if (element.isField()) { |
| 69 assert(element is VariableElement); | 65 assert(element is VariableElement); |
| 70 // Different VariableElement's may refer to the same VariableListElement. | 66 // Different VariableElement's may refer to the same VariableListElement. |
| 71 // Output this list only once. | 67 // Output this list only once. |
| 72 // TODO: only emit used variables. | 68 // TODO: only emit used variables. |
| 73 final variableList = element.variables; | 69 final variableList = element.variables; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 90 if (!uri.startsWith('dart:')) continue; | 86 if (!uri.startsWith('dart:')) continue; |
| 91 final lib = libraries[uri]; | 87 final lib = libraries[uri]; |
| 92 if (renamer.imports.containsKey(lib)) { | 88 if (renamer.imports.containsKey(lib)) { |
| 93 result.add('#import("$uri", prefix: "${renamer.imports[lib]}");'); | 89 result.add('#import("$uri", prefix: "${renamer.imports[lib]}");'); |
| 94 } | 90 } |
| 95 } | 91 } |
| 96 result.add(sb); | 92 result.add(sb); |
| 97 return result.toString(); | 93 return result.toString(); |
| 98 } | 94 } |
| 99 } | 95 } |
| OLD | NEW |