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