Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 library analyzer2dart.treeShaker; | 5 library analyzer2dart.treeShaker; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 | 8 |
| 9 import 'package:analyzer/analyzer.dart'; | 9 import 'package:analyzer/analyzer.dart'; |
| 10 import 'package:analyzer/src/generated/element.dart'; | 10 import 'package:analyzer/src/generated/element.dart'; |
| 11 import 'package:compiler/implementation/universe/universe.dart'; | 11 import 'package:compiler/implementation/universe/universe.dart'; |
| 12 | 12 |
| 13 import 'closed_world.dart'; | 13 import 'closed_world.dart'; |
| 14 | 14 |
| 15 class TreeShaker { | 15 class TreeShaker { |
| 16 List<Element> _queue = <Element>[]; | 16 List<Element> _queue = <Element>[]; |
| 17 Set<Element> _alreadyEnqueued = new HashSet<Element>(); | 17 Set<Element> _alreadyEnqueued = new HashSet<Element>(); |
| 18 ClosedWorld _world = new ClosedWorld(); | 18 ClosedWorld _world; |
| 19 Set<Selector> _selectors = new HashSet<Selector>(); | 19 Set<Selector> _selectors = new HashSet<Selector>(); |
| 20 | 20 |
| 21 TreeShaker(FunctionElement mainFunction) | |
| 22 : _world = new ClosedWorld(mainFunction) { | |
| 23 addElement(mainFunction); | |
| 24 } | |
| 25 | |
| 26 | |
|
sigurdm
2014/09/08 14:07:15
Double newline
Johnni Winther
2014/09/09 14:21:26
Done.
| |
| 21 void addElement(Element element) { | 27 void addElement(Element element) { |
| 22 if (_alreadyEnqueued.add(element)) { | 28 if (_alreadyEnqueued.add(element)) { |
| 23 _queue.add(element); | 29 _queue.add(element); |
| 24 } | 30 } |
| 25 } | 31 } |
| 26 | 32 |
| 27 void addSelector(Selector selector) { | 33 void addSelector(Selector selector) { |
| 28 if (_selectors.add(selector)) { | 34 if (_selectors.add(selector)) { |
| 29 // New selector, so match it against all class methods. | 35 // New selector, so match it against all class methods. |
| 30 _world.instantiatedClasses.forEach((ClassElement element, AstNode node) { | 36 _world.instantiatedClasses.forEach((ClassElement element, AstNode node) { |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 89 _world.fields[element] = declaration; | 95 _world.fields[element] = declaration; |
| 90 } else { | 96 } else { |
| 91 throw new Exception('Unexpected element type while tree shaking'); | 97 throw new Exception('Unexpected element type while tree shaking'); |
| 92 } | 98 } |
| 93 } | 99 } |
| 94 print('Tree shaking done'); | 100 print('Tree shaking done'); |
| 95 return _world; | 101 return _world; |
| 96 } | 102 } |
| 97 } | 103 } |
| 98 | 104 |
| 105 Selector createSelectorFromMethodInvocation(MethodInvocation node) { | |
| 106 int arity = 0; | |
| 107 List<String> namedArguments = <String>[]; | |
| 108 for (var x in node.argumentList.arguments) { | |
| 109 if (x is NamedExpression) { | |
| 110 namedArguments.add(x.name.label.name); | |
| 111 } else { | |
| 112 arity++; | |
| 113 } | |
| 114 } | |
| 115 return new Selector.call(node.methodName.name, null, arity, namedArguments); | |
| 116 } | |
| 117 | |
| 99 class TreeShakingVisitor extends RecursiveAstVisitor { | 118 class TreeShakingVisitor extends RecursiveAstVisitor { |
| 100 final TreeShaker treeShaker; | 119 final TreeShaker treeShaker; |
| 101 | 120 |
| 102 TreeShakingVisitor(this.treeShaker); | 121 TreeShakingVisitor(this.treeShaker); |
| 103 | 122 |
| 104 /** | 123 /** |
| 105 * Handle a true method call (a MethodInvocation that represents a call to | 124 * Handle a true method call (a MethodInvocation that represents a call to |
| 106 * a non-static method). | 125 * a non-static method). |
| 107 */ | 126 */ |
| 108 void handleMethodCall(MethodInvocation node) { | 127 void handleMethodCall(MethodInvocation node) { |
| 109 int arity = 0; | 128 treeShaker.addSelector(createSelectorFromMethodInvocation(node)); |
| 110 List<String> namedArguments = <String>[]; | |
| 111 for (var x in node.argumentList.arguments) { | |
| 112 if (x is NamedExpression) { | |
| 113 namedArguments.add(x.name.label.name); | |
| 114 } else { | |
| 115 arity++; | |
| 116 } | |
| 117 } | |
| 118 treeShaker.addSelector( | |
| 119 new Selector.call(node.methodName.name, null, arity, namedArguments)); | |
| 120 } | 129 } |
| 121 | 130 |
| 122 @override | 131 @override |
| 123 void visitFunctionDeclaration(FunctionDeclaration node) { | 132 void visitFunctionDeclaration(FunctionDeclaration node) { |
| 124 super.visitFunctionDeclaration(node); | 133 super.visitFunctionDeclaration(node); |
| 125 } | 134 } |
| 126 | 135 |
| 127 @override | 136 @override |
| 128 void visitInstanceCreationExpression(InstanceCreationExpression node) { | 137 void visitInstanceCreationExpression(InstanceCreationExpression node) { |
| 129 ConstructorElement staticElement = node.staticElement; | 138 ConstructorElement staticElement = node.staticElement; |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 242 // main() { | 251 // main() { |
| 243 // new A().g; | 252 // new A().g; |
| 244 // } | 253 // } |
| 245 // TODO(paulberry): do setters go through this path as well? | 254 // TODO(paulberry): do setters go through this path as well? |
| 246 // TODO(paulberry): handle cases where the property access is represented | 255 // TODO(paulberry): handle cases where the property access is represented |
| 247 // as a PrefixedIdentifier. | 256 // as a PrefixedIdentifier. |
| 248 super.visitPropertyAccess(node); | 257 super.visitPropertyAccess(node); |
| 249 treeShaker.addSelector(new Selector.getter(node.propertyName.name, null)); | 258 treeShaker.addSelector(new Selector.getter(node.propertyName.name, null)); |
| 250 } | 259 } |
| 251 } | 260 } |
| OLD | NEW |