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

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

Issue 10916053: Get rid of duplication of class node unparsing. (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(); 11 unparser.sb = new StringBuffer();
Roman 2012/08/31 13:52:15 This hack does not look good. That 'sb' field look
Anton Muhin 2012/08/31 14:01:09 Let me disagree. Rather you original decision to
Roman 2012/08/31 15:39:00 1) Well it was not my decision :) What makes you t
Anton Muhin 2012/08/31 15:47:48 If I meet an object with the following property: u
Roman 2012/08/31 16:49:55 I see visit() as an implementation detail.
Anton Muhin 2012/08/31 18:13:23 Sure, I agree with you. I just didn't want to tou
12 final processedVariableLists = new Set<VariableListElement>(); 12 final processedVariableLists = new Set<VariableListElement>();
13 13
14 void outputElement(Element element) { 14 void outputElement(Element element) {
15 if (element is SynthesizedConstructorElement) return; 15 if (element is SynthesizedConstructorElement) return;
16 if (element.isField()) { 16 if (element.isField()) {
17 assert(element is VariableElement); 17 assert(element is VariableElement);
18 // Different VariableElement's may refer to the same VariableListElement. 18 // Different VariableElement's may refer to the same VariableListElement.
19 // Output this list only once. 19 // Output this list only once.
20 // TODO: only emit used variables. 20 // TODO: only emit used variables.
21 VariableElement variableElement = element; 21 VariableElement variableElement = element;
22 final variableList = variableElement.variables; 22 final variableList = variableElement.variables;
23 if (!processedVariableLists.contains(variableList)) { 23 if (!processedVariableLists.contains(variableList)) {
24 processedVariableLists.add(variableList); 24 processedVariableLists.add(variableList);
25 sb.add(unparser.unparse(variableList.parseNode(compiler))); 25 unparser.visit(variableList.parseNode(compiler));
26 } 26 }
27 } else { 27 } else {
28 sb.add(unparser.unparse(element.parseNode(compiler))); 28 unparser.visit(element.parseNode(compiler));
29 } 29 }
30 } 30 }
31 31
32 void outputClass(ClassElement classElement, Collection<Element> members) { 32 void outputClass(ClassElement classElement, Collection<Element> members) {
33 ClassNode classNode = classElement.parseNode(compiler); 33 unparser.emitClassWithBody(classElement.parseNode(compiler), () {
34 // classElement.beginToken is 'class', 'interface', or 'abstract'. 34 members.forEach((element) {
35 sb.add(classNode.beginToken.slowToString()); 35 // TODO(smok): Filter out default constructors here.
36 if (classNode.beginToken.slowToString() == 'abstract') { 36 outputElement(element);
37 sb.add(' '); 37 });
38 sb.add(classNode.beginToken.next.slowToString()); // 'class'
39 }
40 sb.add(' ');
41 sb.add(unparser.unparse(classNode.name));
42 if (classNode.typeParameters !== null) {
43 sb.add(unparser.unparse(classNode.typeParameters));
44 }
45 if (classNode.extendsKeyword !== null) {
46 sb.add(' ');
47 classNode.extendsKeyword.value.printOn(sb);
48 sb.add(' ');
49 sb.add(unparser.unparse(classNode.superclass));
50 }
51 if (!classNode.interfaces.isEmpty()) {
52 sb.add(' ');
53 sb.add(unparser.unparse(classNode.interfaces));
54 }
55 if (classNode.defaultClause !== null) {
56 sb.add(' default ');
57 sb.add(unparser.unparse(classNode.defaultClause));
58 }
59 sb.add('{');
60 members.forEach((element) {
61 // TODO(smok): Filter out default constructors here.
62 outputElement(element);
63 }); 38 });
64 sb.add('}');
65 } 39 }
66 40
67 final libraries = compiler.libraries; 41 final libraries = compiler.libraries;
68 for (final uri in libraries.getKeys()) { 42 for (final uri in libraries.getKeys()) {
69 // Same library element may be a value for different uris as of now 43 // Same library element may be a value for different uris as of now
70 // e.g., core libraryElement is a value for both keys 'dart:core' 44 // e.g., core libraryElement is a value for both keys 'dart:core'
71 // and full file name. Only care about uris with dart scheme. 45 // and full file name. Only care about uris with dart scheme.
72 if (!uri.startsWith('dart:')) continue; 46 if (!uri.startsWith('dart:')) continue;
73 final lib = libraries[uri]; 47 final lib = libraries[uri];
74 if (imports.containsKey(lib)) { 48 if (imports.containsKey(lib)) {
75 sb.add('#import("$uri", prefix: "${imports[lib]}");'); 49 unparser.sb.add('#import("$uri", prefix: "${imports[lib]}");');
76 } 50 }
77 } 51 }
78 52
79 for (final element in topLevelElements) { 53 for (final element in topLevelElements) {
80 if (element is ClassElement) { 54 if (element is ClassElement) {
81 outputClass(element, classMembers[element]); 55 outputClass(element, classMembers[element]);
82 } else { 56 } else {
83 outputElement(element); 57 outputElement(element);
84 } 58 }
85 } 59 }
86 60
87 return sb.toString(); 61 return unparser.sb.toString();
88 } 62 }
OLDNEW
« no previous file with comments | « no previous file | lib/compiler/implementation/tree/unparser.dart » ('j') | lib/compiler/implementation/tree/unparser.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698