Chromium Code Reviews| 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 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.isTopLevel()); | |
|
Anton Muhin
2012/07/10 12:07:29
you may want to add an assert that it's a class.
Roman
2012/07/10 12:18:02
Done.
| |
| 92 addMemberToClass(element, enclosingClass); | |
| 93 return; | |
| 94 } | |
| 58 if (!element.isTopLevel()) { | 95 if (!element.isTopLevel()) { |
| 59 bailout('Cannot process non top-level $element'); | 96 bailout('Cannot process non top-level $element'); |
| 60 } | 97 } |
| 61 | 98 |
| 62 if (element.isField()) { | 99 if (element.isField()) { |
| 63 // Add modifiers first. | 100 // Add modifiers first. |
| 64 sb.add(element.modifiers.toString()); | 101 sb.add(element.modifiers.toString()); |
| 65 sb.add(' '); | 102 sb.add(' '); |
| 66 // Figure out type. | 103 // Figure out type. |
| 67 if (element is VariableElement) { | 104 if (element is VariableElement) { |
| 68 VariableListElement variables = element.variables; | 105 VariableListElement variables = element.variables; |
| 69 if (variables.type !== null) { | 106 if (variables.type !== null) { |
| 70 sb.add(variables.type); | 107 sb.add(variables.type); |
| 71 sb.add(' '); | 108 sb.add(' '); |
| 72 } | 109 } |
| 73 } | 110 } |
| 74 // TODO(smok): Maybe not rely on node unparsing, | 111 // TODO(smok): Maybe not rely on node unparsing, |
| 75 // but unparse initializer manually. | 112 // but unparse initializer manually. |
| 76 sb.add(element.parseNode(compiler).unparse()); | 113 sb.add(element.parseNode(compiler).unparse()); |
| 77 sb.add(';'); | 114 sb.add(';'); |
| 78 } else { | 115 } else { |
| 79 sb.add(element.parseNode(compiler).unparse()); | 116 sb.add(element.parseNode(compiler).unparse()); |
| 80 } | 117 } |
| 81 }); | 118 }); |
| 119 | |
| 120 // Now output resolved classes with inner elements we met before. | |
| 121 resolvedClassMembers.forEach((classElement, resolvedElements) { | |
| 122 outputClass(classElement, resolvedElements, sb); | |
| 123 }); | |
| 82 compiler.assembledCode = sb.toString(); | 124 compiler.assembledCode = sb.toString(); |
| 83 } catch (BailoutException e) { | 125 } catch (BailoutException e) { |
| 84 compiler.assembledCode = ''' | 126 compiler.assembledCode = ''' |
| 85 main() { | 127 main() { |
| 86 final bailout_reason = "${e.reason}"; | 128 final bailout_reason = "${e.reason}"; |
| 87 } | 129 } |
| 88 '''; | 130 '''; |
| 89 } | 131 } |
| 90 } | 132 } |
| 91 | 133 |
| 92 log(String message) => compiler.log('[DartBackend] $message'); | 134 log(String message) => compiler.log('[DartBackend] $message'); |
| 93 } | 135 } |
| OLD | NEW |