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

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

Issue 10690110: dart2dart support for instantiatin simple top-level class: (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 5 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
« no previous file with comments | « no previous file | tests/compiler/dart2js/unparser_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 class BailoutException { 5 class BailoutException {
6 final String reason; 6 final String reason;
7 7
8 const BailoutException(this.reason); 8 const BailoutException(this.reason);
9 } 9 }
10 10
11 class DartBackend extends Backend { 11 class DartBackend extends Backend {
12 final List<CompilerTask> tasks; 12 final List<CompilerTask> tasks;
13 final UnparseValidator unparseValidator; 13 final UnparseValidator unparseValidator;
14 14
15 Map<Element, TreeElements> get resolvedElements() => 15 Map<Element, TreeElements> get resolvedElements() =>
16 compiler.enqueuer.resolution.resolvedElements; 16 compiler.enqueuer.resolution.resolvedElements;
17 Map<ClassElement, Set<Element>> resolvedClassMembers;
17 18
18 DartBackend(Compiler compiler, [bool validateUnparse = false]) 19 DartBackend(Compiler compiler, [bool validateUnparse = false])
19 : tasks = <CompilerTask>[], 20 : tasks = <CompilerTask>[],
20 unparseValidator = new UnparseValidator(compiler, validateUnparse), 21 unparseValidator = new UnparseValidator(compiler, validateUnparse),
22 resolvedClassMembers = new Map<ClassElement, Set<Element>>(),
21 super(compiler) { 23 super(compiler) {
22 tasks.add(unparseValidator); 24 tasks.add(unparseValidator);
23 } 25 }
24 26
25 void enqueueHelpers(Enqueuer world) { 27 void enqueueHelpers(Enqueuer world) {
26 // TODO(antonm): Implement this method, if needed. 28 // TODO(antonm): Implement this method, if needed.
27 } 29 }
28 30
29 CodeBlock codegen(WorkItem work) { return new CodeBlock(null, null); } 31 CodeBlock codegen(WorkItem work) { return new CodeBlock(null, null); }
30 32
31 void processNativeClasses(Enqueuer world, 33 void processNativeClasses(Enqueuer world,
32 Collection<LibraryElement> libraries) { 34 Collection<LibraryElement> libraries) {
33 } 35 }
34 36
37 /**
38 * Adds given class element with its member element to resolved classes
39 * collections.
40 */
41 void addMemberToClass(Element element, ClassElement classElement) {
42 // ${element} should have ${classElement} as enclosing.
43 assert(element.enclosingElement == classElement);
44 List<Element> resolvedElementsInClass =
45 resolvedClassMembers.putIfAbsent(classElement, () => <Element>[]);
46 resolvedElementsInClass.add(element);
47 }
48
49 /**
50 * Outputs given class element with given inner elements to a string buffer.
51 */
52 void outputClass(ClassElement classElement, List<Element> innerElements,
53 StringBuffer sb) {
54 // TODO(smok): Very soon properly print out correct class declaration with
55 // extends, implements, etc.
56 sb.add('class ');
57 sb.add(classElement.name.slowToString());
58 sb.add('{');
59 innerElements.forEach((element) {
60 // TODO(smok): Filter out default constructors here.
61 sb.add(element.parseNode(compiler).unparse());
62 });
63 sb.add('}');
64 }
65
35 void assembleProgram() { 66 void assembleProgram() {
36 resolvedElements.forEach((element, treeElements) { 67 resolvedElements.forEach((element, treeElements) {
37 unparseValidator.check(element); 68 unparseValidator.check(element);
38 }); 69 });
39 70
40 // TODO(antonm): Eventually bailouts will be proper errors. 71 // TODO(antonm): Eventually bailouts will be proper errors.
41 void bailout(String reason) { 72 void bailout(String reason) {
42 throw new BailoutException(reason); 73 throw new BailoutException(reason);
43 } 74 }
44 75
45 /** 76 /**
46 * Tells whether we should output given element. Corelib classes like 77 * Tells whether we should output given element. Corelib classes like
47 * Object should not be in the resulting code. 78 * Object should not be in the resulting code.
48 */ 79 */
49 bool shouldOutput(Element element) { 80 bool shouldOutput(Element element) {
50 return element.kind !== ElementKind.VOID 81 return element.kind !== ElementKind.VOID
51 && element.getLibrary() !== compiler.coreLibrary; 82 && element.getLibrary() !== compiler.coreLibrary;
52 } 83 }
53 84
54 try { 85 try {
55 StringBuffer sb = new StringBuffer(); 86 StringBuffer sb = new StringBuffer();
56 resolvedElements.forEach((element, treeElements) { 87 resolvedElements.forEach((element, treeElements) {
57 if (!shouldOutput(element)) return; 88 if (!shouldOutput(element)) return;
89 if (element.isMember()) {
90 var enclosingClass = element.enclosingElement;
91 assert(enclosingClass.isClass());
92 assert(enclosingClass.isTopLevel());
93 addMemberToClass(element, enclosingClass);
94 return;
95 }
58 if (!element.isTopLevel()) { 96 if (!element.isTopLevel()) {
59 bailout('Cannot process non top-level $element'); 97 bailout('Cannot process non top-level $element');
60 } 98 }
61 99
62 if (element.isField()) { 100 if (element.isField()) {
63 // Add modifiers first. 101 // Add modifiers first.
64 sb.add(element.modifiers.toString()); 102 sb.add(element.modifiers.toString());
65 sb.add(' '); 103 sb.add(' ');
66 // Figure out type. 104 // Figure out type.
67 if (element is VariableElement) { 105 if (element is VariableElement) {
68 VariableListElement variables = element.variables; 106 VariableListElement variables = element.variables;
69 if (variables.type !== null) { 107 if (variables.type !== null) {
70 sb.add(variables.type); 108 sb.add(variables.type);
71 sb.add(' '); 109 sb.add(' ');
72 } 110 }
73 } 111 }
74 // TODO(smok): Maybe not rely on node unparsing, 112 // TODO(smok): Maybe not rely on node unparsing,
75 // but unparse initializer manually. 113 // but unparse initializer manually.
76 sb.add(element.parseNode(compiler).unparse()); 114 sb.add(element.parseNode(compiler).unparse());
77 sb.add(';'); 115 sb.add(';');
78 } else { 116 } else {
79 sb.add(element.parseNode(compiler).unparse()); 117 sb.add(element.parseNode(compiler).unparse());
80 } 118 }
81 }); 119 });
120
121 // Now output resolved classes with inner elements we met before.
122 resolvedClassMembers.forEach((classElement, resolvedElements) {
123 outputClass(classElement, resolvedElements, sb);
124 });
82 compiler.assembledCode = sb.toString(); 125 compiler.assembledCode = sb.toString();
83 } catch (BailoutException e) { 126 } catch (BailoutException e) {
84 compiler.assembledCode = ''' 127 compiler.assembledCode = '''
85 main() { 128 main() {
86 final bailout_reason = "${e.reason}"; 129 final bailout_reason = "${e.reason}";
87 } 130 }
88 '''; 131 ''';
89 } 132 }
90 } 133 }
91 134
92 log(String message) => compiler.log('[DartBackend] $message'); 135 log(String message) => compiler.log('[DartBackend] $message');
93 } 136 }
OLDNEW
« no previous file with comments | « no previous file | tests/compiler/dart2js/unparser_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698