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

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 inner element to resolved classes
Anton Muhin 2012/07/10 11:31:40 inner should probably be something like member, it
Roman 2012/07/10 11:53:40 Done.
39 * collections.
40 */
41 void addResolvedElementForClass(Element element, ClassElement classElement) {
42 // ${element} should have ${classElement} as enclosing.
43 assert(element.enclosingElement == classElement);
44 List<Element> resolvedElementsInClass = resolvedClassMembers.putIfAbsent(
Anton Muhin 2012/07/10 11:31:40 nit: even as rCM.putIfAbsent(classElement, () =>
Roman 2012/07/10 11:53:40 Done.
45 classElement, () => new List<Element>());
46 resolvedElementsInClass.add(element);
47 }
48
49 /**
50 * Resolves enclosing class for given element and adds to a collection of
51 * resolved class members.
52 */
53 void resolveMember(Element element) {
Anton Muhin 2012/07/10 11:31:40 again, it's not resolving. If anything, it's proc
Roman 2012/07/10 11:53:40 Done.
54 // We should not resolve classes for top-level elements.
55 assert(!element.isTopLevel());
56 assert(element.isMember());
Anton Muhin 2012/07/10 11:31:40 I don't think you may have element.isMember() && e
Roman 2012/07/10 11:53:40 removed isTopLevel() assert
57 var enclosingClass = element.enclosingElement;
58 assert(enclosingClass.isTopLevel());
59 addResolvedElementForClass(element, enclosingClass);
60 }
61
62 /**
63 * Outputs given class element with given inner elements to a string buffer.
64 */
65 void outputClass(ClassElement classElement, List<Element> innerElements,
66 StringBuffer sb) {
67 // TODO(smok): Very soon properly print out correct class declaration with
68 // extends, implements, etc.
69 sb.add('class ');
70 sb.add(classElement.name.slowToString());
71 sb.add('{');
72 innerElements.forEach((element) {
73 // TODO(smok): Filter out default constructors here.
74 sb.add(element.parseNode(compiler).unparse());
75 });
76 sb.add('}');
77 }
78
35 void assembleProgram() { 79 void assembleProgram() {
36 resolvedElements.forEach((element, treeElements) { 80 resolvedElements.forEach((element, treeElements) {
37 unparseValidator.check(element); 81 unparseValidator.check(element);
38 }); 82 });
39 83
40 // TODO(antonm): Eventually bailouts will be proper errors. 84 // TODO(antonm): Eventually bailouts will be proper errors.
41 void bailout(String reason) { 85 void bailout(String reason) {
42 throw new BailoutException(reason); 86 throw new BailoutException(reason);
43 } 87 }
44 88
45 /** 89 /**
46 * Tells whether we should output given element. Corelib classes like 90 * Tells whether we should output given element. Corelib classes like
47 * Object should not be in the resulting code. 91 * Object should not be in the resulting code.
48 */ 92 */
49 bool shouldOutput(Element element) { 93 bool shouldOutput(Element element) {
50 return element.kind !== ElementKind.VOID 94 return element.kind !== ElementKind.VOID
51 && element.getLibrary() !== compiler.coreLibrary; 95 && element.getLibrary() !== compiler.coreLibrary;
52 } 96 }
53 97
54 try { 98 try {
55 StringBuffer sb = new StringBuffer(); 99 StringBuffer sb = new StringBuffer();
56 resolvedElements.forEach((element, treeElements) { 100 resolvedElements.forEach((element, treeElements) {
57 if (!shouldOutput(element)) return; 101 if (!shouldOutput(element)) return;
102 if (element.isMember()) {
103 resolveMember(element);
104 return;
105 }
58 if (!element.isTopLevel()) { 106 if (!element.isTopLevel()) {
59 bailout('Cannot process non top-level $element'); 107 bailout('Cannot process non top-level $element');
60 } 108 }
61 109
62 if (element.isField()) { 110 if (element.isField()) {
63 // Add modifiers first. 111 // Add modifiers first.
64 sb.add(element.modifiers.toString()); 112 sb.add(element.modifiers.toString());
65 sb.add(' '); 113 sb.add(' ');
66 // Figure out type. 114 // Figure out type.
67 if (element is VariableElement) { 115 if (element is VariableElement) {
68 VariableListElement variables = element.variables; 116 VariableListElement variables = element.variables;
69 if (variables.type !== null) { 117 if (variables.type !== null) {
70 sb.add(variables.type); 118 sb.add(variables.type);
71 sb.add(' '); 119 sb.add(' ');
72 } 120 }
73 } 121 }
74 // TODO(smok): Maybe not rely on node unparsing, 122 // TODO(smok): Maybe not rely on node unparsing,
75 // but unparse initializer manually. 123 // but unparse initializer manually.
76 sb.add(element.parseNode(compiler).unparse()); 124 sb.add(element.parseNode(compiler).unparse());
77 sb.add(';'); 125 sb.add(';');
78 } else { 126 } else {
79 sb.add(element.parseNode(compiler).unparse()); 127 sb.add(element.parseNode(compiler).unparse());
80 } 128 }
81 }); 129 });
130
131 // Now output resolved classes with inner elements we met before.
132 resolvedClassMembers.forEach((classElement, resolvedElements) {
133 outputClass(classElement, resolvedElements, sb);
134 });
82 compiler.assembledCode = sb.toString(); 135 compiler.assembledCode = sb.toString();
83 } catch (BailoutException e) { 136 } catch (BailoutException e) {
84 compiler.assembledCode = ''' 137 compiler.assembledCode = '''
85 main() { 138 main() {
86 final bailout_reason = "${e.reason}"; 139 final bailout_reason = "${e.reason}";
87 } 140 }
88 '''; 141 ''';
89 } 142 }
90 } 143 }
91 144
92 log(String message) => compiler.log('[DartBackend] $message'); 145 log(String message) => compiler.log('[DartBackend] $message');
93 } 146 }
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