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 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 50 * Outputs given class element with given inner elements to a string buffer. | 50 * Outputs given class element with given inner elements to a string buffer. |
| 51 */ | 51 */ |
| 52 void outputClass(ClassElement classElement, Set<Element> innerElements, | 52 void outputClass(ClassElement classElement, Set<Element> innerElements, |
| 53 StringBuffer sb) { | 53 StringBuffer sb) { |
| 54 // TODO(smok): Very soon properly print out correct class declaration with | 54 // TODO(smok): Very soon properly print out correct class declaration with |
| 55 // extends, implements, etc. | 55 // extends, implements, etc. |
| 56 sb.add('class '); | 56 sb.add('class '); |
| 57 sb.add(classElement.name.slowToString()); | 57 sb.add(classElement.name.slowToString()); |
| 58 sb.add('{'); | 58 sb.add('{'); |
| 59 innerElements.forEach((element) { | 59 innerElements.forEach((element) { |
| 60 if (element is SynthesizedConstructorElement) return; | |
| 61 // TODO(smok): Filter out default constructors here. | 60 // TODO(smok): Filter out default constructors here. |
| 62 sb.add(element.parseNode(compiler).unparse()); | 61 outputElement(element, sb); |
| 63 }); | 62 }); |
| 64 sb.add('}'); | 63 sb.add('}'); |
| 65 } | 64 } |
| 66 | 65 |
| 66 void outputElement(Element element, StringBuffer sb) { | |
|
Anton Muhin
2012/07/11 15:40:28
maybe it's time to introduce emitter class instead
| |
| 67 // TODO(smok): Figure out why AbstractFieldElement appears here, | |
| 68 // we have used getters/setters resolved instead of it. | |
| 69 if (element is SynthesizedConstructorElement | |
| 70 || element is AbstractFieldElement) return; | |
| 71 if (element.isField()) { | |
| 72 // Add modifiers first. | |
| 73 sb.add(element.modifiers.toString()); | |
| 74 sb.add(' '); | |
| 75 // Figure out type. | |
| 76 if (element is VariableElement) { | |
| 77 VariableListElement variables = element.variables; | |
| 78 if (variables.type !== null) { | |
| 79 sb.add(variables.type); | |
| 80 sb.add(' '); | |
| 81 } | |
| 82 } | |
| 83 // TODO(smok): Maybe not rely on node unparsing, | |
| 84 // but unparse initializer manually. | |
| 85 sb.add(element.parseNode(compiler).unparse()); | |
| 86 sb.add(';'); | |
| 87 } else { | |
| 88 if (element.isSetter()) { | |
| 89 sb.add('set '); | |
| 90 } else if (element.isGetter()) { | |
| 91 sb.add('get '); | |
| 92 } | |
| 93 sb.add(element.parseNode(compiler).unparse()); | |
| 94 } | |
| 95 } | |
| 96 | |
| 67 void assembleProgram() { | 97 void assembleProgram() { |
| 68 resolvedElements.forEach((element, treeElements) { | 98 resolvedElements.forEach((element, treeElements) { |
| 69 unparseValidator.check(element); | 99 unparseValidator.check(element); |
| 70 }); | 100 }); |
| 71 | 101 |
| 72 // TODO(antonm): Eventually bailouts will be proper errors. | 102 // TODO(antonm): Eventually bailouts will be proper errors. |
| 73 void bailout(String reason) { | 103 void bailout(String reason) { |
| 74 throw new BailoutException(reason); | 104 throw new BailoutException(reason); |
| 75 } | 105 } |
| 76 | 106 |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 91 var enclosingClass = element.enclosingElement; | 121 var enclosingClass = element.enclosingElement; |
| 92 assert(enclosingClass.isClass()); | 122 assert(enclosingClass.isClass()); |
| 93 assert(enclosingClass.isTopLevel()); | 123 assert(enclosingClass.isTopLevel()); |
| 94 addMemberToClass(element, enclosingClass); | 124 addMemberToClass(element, enclosingClass); |
| 95 return; | 125 return; |
| 96 } | 126 } |
| 97 if (!element.isTopLevel()) { | 127 if (!element.isTopLevel()) { |
| 98 bailout('Cannot process non top-level $element'); | 128 bailout('Cannot process non top-level $element'); |
| 99 } | 129 } |
| 100 | 130 |
| 101 if (element.isField()) { | 131 outputElement(element, sb); |
| 102 // Add modifiers first. | |
| 103 sb.add(element.modifiers.toString()); | |
| 104 sb.add(' '); | |
| 105 // Figure out type. | |
| 106 if (element is VariableElement) { | |
| 107 VariableListElement variables = element.variables; | |
| 108 if (variables.type !== null) { | |
| 109 sb.add(variables.type); | |
| 110 sb.add(' '); | |
| 111 } | |
| 112 } | |
| 113 // TODO(smok): Maybe not rely on node unparsing, | |
| 114 // but unparse initializer manually. | |
| 115 sb.add(element.parseNode(compiler).unparse()); | |
| 116 sb.add(';'); | |
| 117 } else { | |
| 118 sb.add(element.parseNode(compiler).unparse()); | |
| 119 } | |
| 120 }); | 132 }); |
| 121 | 133 |
| 122 // Now output resolved classes with inner elements we met before. | 134 // Now output resolved classes with inner elements we met before. |
| 123 resolvedClassMembers.forEach((classElement, resolvedElements) { | 135 resolvedClassMembers.forEach((classElement, resolvedElements) { |
| 124 outputClass(classElement, resolvedElements, sb); | 136 outputClass(classElement, resolvedElements, sb); |
| 125 }); | 137 }); |
| 126 compiler.assembledCode = sb.toString(); | 138 compiler.assembledCode = sb.toString(); |
| 127 } catch (BailoutException e) { | 139 } catch (BailoutException e) { |
| 128 compiler.assembledCode = ''' | 140 compiler.assembledCode = ''' |
| 129 main() { | 141 main() { |
| 130 final bailout_reason = "${e.reason}"; | 142 final bailout_reason = "${e.reason}"; |
| 131 } | 143 } |
| 132 '''; | 144 '''; |
| 133 } | 145 } |
| 134 } | 146 } |
| 135 | 147 |
| 136 log(String message) => compiler.log('[DartBackend] $message'); | 148 log(String message) => compiler.log('[DartBackend] $message'); |
| 137 } | 149 } |
| OLD | NEW |