| 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 | |
| 7 class ElementAst { | 5 class ElementAst { |
| 8 final Node ast; | 6 final Node ast; |
| 9 final TreeElements treeElements; | 7 final TreeElements treeElements; |
| 10 | 8 |
| 11 ElementAst(this.ast, this.treeElements); | 9 ElementAst(this.ast, this.treeElements); |
| 12 | 10 |
| 13 factory ElementAst.rewrite(compiler, ast, treeElements) { | 11 factory ElementAst.rewrite(compiler, ast, treeElements, stripAsserts) { |
| 14 final rewriter = new FunctionBodyRewriter(compiler, treeElements); | 12 final rewriter = |
| 13 new FunctionBodyRewriter(compiler, treeElements, stripAsserts); |
| 15 return new ElementAst(rewriter.visit(ast), rewriter.cloneTreeElements); | 14 return new ElementAst(rewriter.visit(ast), rewriter.cloneTreeElements); |
| 16 } | 15 } |
| 17 | 16 |
| 18 ElementAst.forClassLike(this.ast) | 17 ElementAst.forClassLike(this.ast) |
| 19 : this.treeElements = new TreeElementMapping(); | 18 : this.treeElements = new TreeElementMapping(); |
| 20 } | 19 } |
| 21 | 20 |
| 22 class AggregatedTreeElements extends TreeElementMapping { | 21 class AggregatedTreeElements extends TreeElementMapping { |
| 23 final List<TreeElements> treeElements; | 22 final List<TreeElements> treeElements; |
| 24 | 23 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 | 55 |
| 57 add(VariableElement element, TreeElements treeElements) { | 56 add(VariableElement element, TreeElements treeElements) { |
| 58 AggregatedTreeElements e = this.treeElements; | 57 AggregatedTreeElements e = this.treeElements; |
| 59 e[element.cachedNode] = element; | 58 e[element.cachedNode] = element; |
| 60 e.treeElements.add(treeElements); | 59 e.treeElements.add(treeElements); |
| 61 } | 60 } |
| 62 } | 61 } |
| 63 | 62 |
| 64 class FunctionBodyRewriter extends CloningVisitor { | 63 class FunctionBodyRewriter extends CloningVisitor { |
| 65 final Compiler compiler; | 64 final Compiler compiler; |
| 65 final bool stripAsserts; |
| 66 | 66 |
| 67 FunctionBodyRewriter(this.compiler, originalTreeElements) | 67 FunctionBodyRewriter(this.compiler, originalTreeElements, this.stripAsserts) |
| 68 : super(originalTreeElements); | 68 : super(originalTreeElements); |
| 69 | 69 |
| 70 visitFunctionExpression(FunctionExpression node) { | 70 visitFunctionExpression(FunctionExpression node) { |
| 71 shouldOmit(Statement statement) { | 71 shouldOmit(Statement statement) { |
| 72 if (statement is EmptyStatement) return true; | 72 if (statement is EmptyStatement) return true; |
| 73 if (statement is ExpressionStatement) { | 73 if (statement is ExpressionStatement) { |
| 74 Send send = statement.expression.asSend(); | 74 Send send = statement.expression.asSend(); |
| 75 if (send !== null) { | 75 if (send !== null) { |
| 76 Element element = originalTreeElements[send]; | 76 Element element = originalTreeElements[send]; |
| 77 if (REMOVE_ASSERTS && element === compiler.assertMethod) { | 77 if (stripAsserts && element === compiler.assertMethod) { |
| 78 return true; | 78 return true; |
| 79 } | 79 } |
| 80 } | 80 } |
| 81 } | 81 } |
| 82 return false; | 82 return false; |
| 83 } | 83 } |
| 84 | 84 |
| 85 rewritTo(Statement statement) { | 85 rewritTo(Statement statement) { |
| 86 if (statement is Block) { | 86 if (statement is Block) { |
| 87 Link statements = statement.statements.nodes; | 87 Link statements = statement.statements.nodes; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 107 } | 107 } |
| 108 return new Block(rewriteNodeList(statements, builder.toLink())); | 108 return new Block(rewriteNodeList(statements, builder.toLink())); |
| 109 } | 109 } |
| 110 | 110 |
| 111 return rewriteFunctionExpression(node, rewriteBody(node.body)); | 111 return rewriteFunctionExpression(node, rewriteBody(node.body)); |
| 112 } | 112 } |
| 113 } | 113 } |
| 114 | 114 |
| 115 class DartBackend extends Backend { | 115 class DartBackend extends Backend { |
| 116 final List<CompilerTask> tasks; | 116 final List<CompilerTask> tasks; |
| 117 final bool forceCutDeclarationTypes; | 117 final bool forceStripTypes; |
| 118 final bool stripAsserts; |
| 118 // TODO(antonm): make available from command-line options. | 119 // TODO(antonm): make available from command-line options. |
| 119 final bool outputAst = false; | 120 final bool outputAst = false; |
| 120 | 121 |
| 121 Map<Element, TreeElements> get resolvedElements => | 122 Map<Element, TreeElements> get resolvedElements => |
| 122 compiler.enqueuer.resolution.resolvedElements; | 123 compiler.enqueuer.resolution.resolvedElements; |
| 123 | 124 |
| 124 /** | 125 /** |
| 125 * Tells whether it is safe to remove type declarations from variables, | 126 * Tells whether it is safe to remove type declarations from variables, |
| 126 * functions parameters. It becomes not safe if: | 127 * functions parameters. It becomes not safe if: |
| 127 * 1) TypeError is used somewhere in the code, | 128 * 1) TypeError is used somewhere in the code, |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 } | 181 } |
| 181 // Check all supertypes. | 182 // Check all supertypes. |
| 182 if (element.allSupertypes !== null) { | 183 if (element.allSupertypes !== null) { |
| 183 workQueue.addAll(element.allSupertypes.toList()); | 184 workQueue.addAll(element.allSupertypes.toList()); |
| 184 } | 185 } |
| 185 } | 186 } |
| 186 } | 187 } |
| 187 return true; | 188 return true; |
| 188 } | 189 } |
| 189 | 190 |
| 190 DartBackend(Compiler compiler, this.forceCutDeclarationTypes) | 191 DartBackend(Compiler compiler, List<String> strips) |
| 191 : tasks = <CompilerTask>[], | 192 : tasks = <CompilerTask>[], |
| 193 forceStripTypes = strips.indexOf('types') != -1, |
| 194 stripAsserts = strips.indexOf('asserts') != -1, |
| 192 super(compiler); | 195 super(compiler); |
| 193 | 196 |
| 194 void enqueueHelpers(Enqueuer world) { | 197 void enqueueHelpers(Enqueuer world) { |
| 195 // Right now resolver doesn't always resolve interfaces needed | 198 // Right now resolver doesn't always resolve interfaces needed |
| 196 // for literals, so force them. TODO(antonm): fix in the resolver. | 199 // for literals, so force them. TODO(antonm): fix in the resolver. |
| 197 final LITERAL_TYPE_NAMES = const [ | 200 final LITERAL_TYPE_NAMES = const [ |
| 198 'Map', 'List', 'num', 'int', 'double', 'bool' | 201 'Map', 'List', 'num', 'int', 'double', 'bool' |
| 199 ]; | 202 ]; |
| 200 final coreLibrary = compiler.coreLibrary; | 203 final coreLibrary = compiler.coreLibrary; |
| 201 for (final name in LITERAL_TYPE_NAMES) { | 204 for (final name in LITERAL_TYPE_NAMES) { |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 new ElementAst.forClassLike(parse(element))); | 295 new ElementAst.forClassLike(parse(element))); |
| 293 }; | 296 }; |
| 294 newClassElementCallback = (ClassElement classElement) { | 297 newClassElementCallback = (ClassElement classElement) { |
| 295 if (!shouldOutput(classElement)) return; | 298 if (!shouldOutput(classElement)) return; |
| 296 addClass(classElement); | 299 addClass(classElement); |
| 297 }; | 300 }; |
| 298 | 301 |
| 299 resolvedElements.forEach((element, treeElements) { | 302 resolvedElements.forEach((element, treeElements) { |
| 300 if (!shouldOutput(element)) return; | 303 if (!shouldOutput(element)) return; |
| 301 | 304 |
| 302 var elementAst = | 305 var elementAst = new ElementAst.rewrite( |
| 303 new ElementAst.rewrite(compiler, parse(element), treeElements); | 306 compiler, parse(element), treeElements, stripAsserts); |
| 304 if (element.isField()) { | 307 if (element.isField()) { |
| 305 final list = (element as VariableElement).variables; | 308 final list = (element as VariableElement).variables; |
| 306 elementAst = elementAsts.putIfAbsent( | 309 elementAst = elementAsts.putIfAbsent( |
| 307 list, () => new VariableListAst(parse(list))); | 310 list, () => new VariableListAst(parse(list))); |
| 308 (elementAst as VariableListAst).add(element, treeElements); | 311 (elementAst as VariableListAst).add(element, treeElements); |
| 309 element = list; | 312 element = list; |
| 310 } | 313 } |
| 311 | 314 |
| 312 if (element.isMember()) { | 315 if (element.isMember()) { |
| 313 ClassElement enclosingClass = element.getEnclosingClass(); | 316 ClassElement enclosingClass = element.getEnclosingClass(); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 331 makePlaceholders(element) { | 334 makePlaceholders(element) { |
| 332 collector.collect(element); | 335 collector.collect(element); |
| 333 if (element is ClassElement) { | 336 if (element is ClassElement) { |
| 334 classMembers[element].forEach(makePlaceholders); | 337 classMembers[element].forEach(makePlaceholders); |
| 335 } | 338 } |
| 336 } | 339 } |
| 337 topLevelElements.forEach(makePlaceholders); | 340 topLevelElements.forEach(makePlaceholders); |
| 338 // Create renames. | 341 // Create renames. |
| 339 Map<Node, String> renames = new Map<Node, String>(); | 342 Map<Node, String> renames = new Map<Node, String>(); |
| 340 Map<LibraryElement, String> imports = new Map<LibraryElement, String>(); | 343 Map<LibraryElement, String> imports = new Map<LibraryElement, String>(); |
| 341 bool shouldCutDeclarationTypes = forceCutDeclarationTypes | 344 bool shouldCutDeclarationTypes = forceStripTypes |
| 342 || (compiler.enableMinification | 345 || (compiler.enableMinification |
| 343 && isSafeToRemoveTypeDeclarations(classMembers)); | 346 && isSafeToRemoveTypeDeclarations(classMembers)); |
| 344 renamePlaceholders( | 347 renamePlaceholders( |
| 345 compiler, collector, renames, imports, | 348 compiler, collector, renames, imports, |
| 346 fixedMemberNames, shouldCutDeclarationTypes); | 349 fixedMemberNames, shouldCutDeclarationTypes); |
| 347 | 350 |
| 348 // Sort elements. | 351 // Sort elements. |
| 349 final sortedTopLevels = sortElements(topLevelElements); | 352 final sortedTopLevels = sortElements(topLevelElements); |
| 350 final sortedClassMembers = new Map<ClassElement, List<Element>>(); | 353 final sortedClassMembers = new Map<ClassElement, List<Element>>(); |
| 351 classMembers.forEach((classElement, members) { | 354 classMembers.forEach((classElement, members) { |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 451 } | 454 } |
| 452 | 455 |
| 453 compareElements(e0, e1) { | 456 compareElements(e0, e1) { |
| 454 int result = compareBy((e) => e.getLibrary().uri.toString())(e0, e1); | 457 int result = compareBy((e) => e.getLibrary().uri.toString())(e0, e1); |
| 455 if (result != 0) return result; | 458 if (result != 0) return result; |
| 456 return compareBy((e) => e.position().charOffset)(e0, e1); | 459 return compareBy((e) => e.position().charOffset)(e0, e1); |
| 457 } | 460 } |
| 458 | 461 |
| 459 List<Element> sortElements(Collection<Element> elements) => | 462 List<Element> sortElements(Collection<Element> elements) => |
| 460 sorted(elements, compareElements); | 463 sorted(elements, compareElements); |
| OLD | NEW |