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 const bool REMOVE_ASSERTS = false; | |
| 6 | |
| 5 class ElementAst { | 7 class ElementAst { |
| 6 final Node ast; | 8 final Node ast; |
| 7 final TreeElements treeElements; | 9 final TreeElements treeElements; |
| 8 | 10 |
| 9 ElementAst(this.ast, this.treeElements); | 11 ElementAst(this.ast, this.treeElements); |
| 10 | 12 |
| 11 factory ElementAst.rewrite(ast, treeElements) { | 13 factory ElementAst.rewrite(compiler, ast, treeElements) { |
| 12 final rewriter = new FunctionBodyRewriter(treeElements); | 14 final rewriter = new FunctionBodyRewriter(compiler, treeElements); |
| 13 return new ElementAst(rewriter.visit(ast), rewriter.cloneTreeElements); | 15 return new ElementAst(rewriter.visit(ast), rewriter.cloneTreeElements); |
| 14 } | 16 } |
| 15 | 17 |
| 16 ElementAst.forClassLike(this.ast) | 18 ElementAst.forClassLike(this.ast) |
| 17 : this.treeElements = new TreeElementMapping(); | 19 : this.treeElements = new TreeElementMapping(); |
| 18 } | 20 } |
| 19 | 21 |
| 20 class AggregatedTreeElements extends TreeElementMapping { | 22 class AggregatedTreeElements extends TreeElementMapping { |
| 21 final List<TreeElements> treeElements; | 23 final List<TreeElements> treeElements; |
| 22 | 24 |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 53 VariableListAst(ast) : super(ast, new AggregatedTreeElements()); | 55 VariableListAst(ast) : super(ast, new AggregatedTreeElements()); |
| 54 | 56 |
| 55 add(VariableElement element, TreeElements treeElements) { | 57 add(VariableElement element, TreeElements treeElements) { |
| 56 AggregatedTreeElements e = this.treeElements; | 58 AggregatedTreeElements e = this.treeElements; |
| 57 e[element.cachedNode] = element; | 59 e[element.cachedNode] = element; |
| 58 e.treeElements.add(treeElements); | 60 e.treeElements.add(treeElements); |
| 59 } | 61 } |
| 60 } | 62 } |
| 61 | 63 |
| 62 class FunctionBodyRewriter extends CloningVisitor { | 64 class FunctionBodyRewriter extends CloningVisitor { |
| 63 FunctionBodyRewriter(originalTreeElements) : super(originalTreeElements); | 65 final Compiler compiler; |
| 66 | |
| 67 FunctionBodyRewriter(this.compiler, originalTreeElements) | |
| 68 : super(originalTreeElements); | |
| 64 | 69 |
| 65 visitFunctionExpression(FunctionExpression node) { | 70 visitFunctionExpression(FunctionExpression node) { |
| 66 shouldOmit(Statement statement) => statement is EmptyStatement; | 71 shouldOmit(Statement statement) { |
| 72 if (statement is EmptyStatement) return true; | |
| 73 if (statement is ExpressionStatement) { | |
| 74 Send send = statement.expression.asSend(); | |
| 75 if (send !== null) { | |
| 76 Element element = originalTreeElements[send]; | |
| 77 if (REMOVE_ASSERTS && element === compiler.assertMethod) { | |
| 78 return true; | |
| 79 } | |
| 80 } | |
| 81 } | |
| 82 return false; | |
| 83 } | |
| 67 | 84 |
| 68 rewriteBody(Statement body) { | 85 rewriteBody(Statement body) { |
| 69 if (body is !Block) return visit(body); | 86 if (body is !Block) return visit(body); |
| 70 Block block = body; | 87 Block block = body; |
| 71 NodeList statements = block.statements; | 88 NodeList statements = block.statements; |
| 72 LinkBuilder<Statement> builder = new LinkBuilder<Statement>(); | 89 LinkBuilder<Statement> builder = new LinkBuilder<Statement>(); |
| 73 for (Statement statement in statements.nodes) { | 90 for (Statement statement in statements.nodes) { |
| 74 if (!shouldOmit(statement)) builder.addLast(visit(statement)); | 91 if (!shouldOmit(statement)) builder.addLast(visit(statement)); |
| 75 } | 92 } |
| 76 return new Block(rewriteNodeList(statements, builder.toLink())); | 93 return new Block(rewriteNodeList(statements, builder.toLink())); |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 191 new ElementAst.forClassLike(parse(element))); | 208 new ElementAst.forClassLike(parse(element))); |
| 192 }; | 209 }; |
| 193 newClassElementCallback = (ClassElement classElement) { | 210 newClassElementCallback = (ClassElement classElement) { |
| 194 if (!shouldOutput(classElement)) return; | 211 if (!shouldOutput(classElement)) return; |
| 195 addClass(classElement); | 212 addClass(classElement); |
| 196 }; | 213 }; |
| 197 | 214 |
| 198 resolvedElements.forEach((element, treeElements) { | 215 resolvedElements.forEach((element, treeElements) { |
| 199 if (!shouldOutput(element)) return; | 216 if (!shouldOutput(element)) return; |
| 200 | 217 |
| 201 var elementAst = new ElementAst.rewrite(parse(element), treeElements); | 218 var elementAst = new ElementAst.rewrite(compiler, parse(element), treeElem ents); |
|
Roman
2012/09/18 09:57:43
80 chars
| |
| 202 if (element.isField()) { | 219 if (element.isField()) { |
| 203 final list = (element as VariableElement).variables; | 220 final list = (element as VariableElement).variables; |
| 204 elementAst = elementAsts.putIfAbsent( | 221 elementAst = elementAsts.putIfAbsent( |
| 205 list, () => new VariableListAst(parse(list))); | 222 list, () => new VariableListAst(parse(list))); |
| 206 (elementAst as VariableListAst).add(element, treeElements); | 223 (elementAst as VariableListAst).add(element, treeElements); |
| 207 element = list; | 224 element = list; |
| 208 } | 225 } |
| 209 | 226 |
| 210 if (element.isMember()) { | 227 if (element.isMember()) { |
| 211 ClassElement enclosingClass = element.getEnclosingClass(); | 228 ClassElement enclosingClass = element.getEnclosingClass(); |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 347 } | 364 } |
| 348 | 365 |
| 349 compareElements(e0, e1) { | 366 compareElements(e0, e1) { |
| 350 int result = compareBy((e) => e.getLibrary().uri.toString())(e0, e1); | 367 int result = compareBy((e) => e.getLibrary().uri.toString())(e0, e1); |
| 351 if (result != 0) return result; | 368 if (result != 0) return result; |
| 352 return compareBy((e) => e.position().charOffset)(e0, e1); | 369 return compareBy((e) => e.position().charOffset)(e0, e1); |
| 353 } | 370 } |
| 354 | 371 |
| 355 List<Element> sortElements(Collection<Element> elements) => | 372 List<Element> sortElements(Collection<Element> elements) => |
| 356 sorted(elements, compareElements); | 373 sorted(elements, compareElements); |
| OLD | NEW |