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

Side by Side Diff: lib/compiler/implementation/dart_backend/emitter.dart

Issue 10919033: Rework Unparser/unparse StringBuffer management. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 3 months 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) 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 String emitCode( 5 String emitCode(
6 Compiler compiler, 6 Compiler compiler,
7 Unparser unparser, 7 Unparser unparser,
8 Map<LibraryElement, String> imports, 8 Map<LibraryElement, String> imports,
9 Collection<Element> topLevelElements, 9 Collection<Element> topLevelElements,
10 Map<ClassElement, Collection<Element>> classMembers) { 10 Map<ClassElement, Collection<Element>> classMembers) {
11 final sb = new StringBuffer();
12 final processedVariableLists = new Set<VariableListElement>(); 11 final processedVariableLists = new Set<VariableListElement>();
13 12
14 void outputElement(Element element) { 13 void outputElement(Element element) {
15 if (element is SynthesizedConstructorElement) return; 14 if (element is SynthesizedConstructorElement) return;
16 if (element.isField()) { 15 if (element.isField()) {
17 assert(element is VariableElement); 16 assert(element is VariableElement);
18 // Different VariableElement's may refer to the same VariableListElement. 17 // Different VariableElement's may refer to the same VariableListElement.
19 // Output this list only once. 18 // Output this list only once.
20 // TODO: only emit used variables. 19 // TODO: only emit used variables.
21 VariableElement variableElement = element; 20 VariableElement variableElement = element;
22 final variableList = variableElement.variables; 21 final variableList = variableElement.variables;
23 if (!processedVariableLists.contains(variableList)) { 22 if (!processedVariableLists.contains(variableList)) {
24 processedVariableLists.add(variableList); 23 processedVariableLists.add(variableList);
25 sb.add(unparser.unparse(variableList.parseNode(compiler))); 24 unparser.visit(variableList.parseNode(compiler));
Roman 2012/09/03 06:39:24 Can you please make a function 'unparse' in Unpars
Anton Muhin 2012/09/03 09:28:10 Done.
26 } 25 }
27 } else { 26 } else {
28 sb.add(unparser.unparse(element.parseNode(compiler))); 27 unparser.visit(element.parseNode(compiler));
29 } 28 }
30 } 29 }
31 30
32 void outputClass(ClassElement classElement, Collection<Element> members) { 31 void outputClass(ClassElement classElement, Collection<Element> members) {
33 ClassNode classNode = classElement.parseNode(compiler); 32 ClassNode classNode = classElement.parseNode(compiler);
34 // classElement.beginToken is 'class', 'interface', or 'abstract'. 33 // classElement.beginToken is 'class', 'interface', or 'abstract'.
35 sb.add(classNode.beginToken.slowToString()); 34 unparser.addToken(classNode.beginToken);
36 if (classNode.beginToken.slowToString() == 'abstract') { 35 if (classNode.beginToken.stringValue == 'abstract') {
37 sb.add(' '); 36 unparser.addToken(classNode.beginToken.next);
38 sb.add(classNode.beginToken.next.slowToString()); // 'class'
39 } 37 }
40 sb.add(' '); 38 unparser.visit(classNode.name);
41 sb.add(unparser.unparse(classNode.name));
42 if (classNode.typeParameters !== null) { 39 if (classNode.typeParameters !== null) {
43 sb.add(unparser.unparse(classNode.typeParameters)); 40 unparser.visit(classNode.typeParameters);
44 } 41 }
45 if (classNode.extendsKeyword !== null) { 42 if (classNode.extendsKeyword !== null) {
46 sb.add(' '); 43 unparser.addString(' ');
47 classNode.extendsKeyword.value.printOn(sb); 44 unparser.addToken(classNode.extendsKeyword);
48 sb.add(' '); 45 unparser.visit(classNode.superclass);
49 sb.add(unparser.unparse(classNode.superclass));
50 } 46 }
51 if (!classNode.interfaces.isEmpty()) { 47 if (!classNode.interfaces.isEmpty()) {
52 sb.add(' '); 48 unparser.addString(' ');
53 sb.add(unparser.unparse(classNode.interfaces)); 49 unparser.visit(classNode.interfaces);
54 } 50 }
55 if (classNode.defaultClause !== null) { 51 if (classNode.defaultClause !== null) {
56 sb.add(' default '); 52 unparser.addString(' default ');
57 sb.add(unparser.unparse(classNode.defaultClause)); 53 unparser.visit(classNode.defaultClause);
58 } 54 }
59 sb.add('{'); 55 unparser.addString('{');
60 members.forEach((element) { 56 members.forEach((element) {
61 // TODO(smok): Filter out default constructors here. 57 // TODO(smok): Filter out default constructors here.
62 outputElement(element); 58 outputElement(element);
63 }); 59 });
64 sb.add('}'); 60 unparser.addString('}');
65 } 61 }
66 62
67 imports.forEach((libraryElement, prefix) { 63 imports.forEach((libraryElement, prefix) {
68 sb.add('#import("${libraryElement.uri}",prefix:"$prefix");'); 64 unparser.addString('#import("${libraryElement.uri}",prefix:"$prefix");');
69 }); 65 });
70 66
71 for (final element in topLevelElements) { 67 for (final element in topLevelElements) {
72 if (element is ClassElement) { 68 if (element is ClassElement) {
73 outputClass(element, classMembers[element]); 69 outputClass(element, classMembers[element]);
74 } else { 70 } else {
75 outputElement(element); 71 outputElement(element);
76 } 72 }
77 } 73 }
78
79 return sb.toString();
80 } 74 }
OLDNEW
« no previous file with comments | « lib/compiler/implementation/dart_backend/backend.dart ('k') | lib/compiler/implementation/tree/nodes.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698