| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library analyzer2dart.treeShaker; |
| 6 |
| 7 import 'package:analyzer/analyzer.dart'; |
| 8 import 'package:analyzer/src/generated/element.dart'; |
| 9 import 'package:analyzer/src/generated/engine.dart'; |
| 10 |
| 11 import 'closed_world.dart'; |
| 12 |
| 13 class TreeShaker { |
| 14 List<Element> _queue = <Element>[]; |
| 15 Set<Element> _alreadyEnqueued = new Set<Element>(); |
| 16 ClosedWorld _world = new ClosedWorld(); |
| 17 |
| 18 void add(Element e) { |
| 19 if (!_alreadyEnqueued.contains(e)) { |
| 20 _queue.add(e); |
| 21 _alreadyEnqueued.add(e); |
| 22 } |
| 23 } |
| 24 |
| 25 ClosedWorld shake(AnalysisContext context) { |
| 26 while (_queue.isNotEmpty) { |
| 27 Element e = _queue.removeAt(0); |
| 28 print('Tree shaker handling $e'); |
| 29 CompilationUnit compilationUnit = |
| 30 context.getResolvedCompilationUnit(e.source, e.library); |
| 31 AstNode identifier = |
| 32 new NodeLocator.con1(e.nameOffset).searchWithin(compilationUnit); |
| 33 FunctionDeclaration declaration = |
| 34 identifier.getAncestor((node) => node is FunctionDeclaration); |
| 35 _world.elements[e] = declaration; |
| 36 declaration.accept(new TreeShakingVisitor(this)); |
| 37 } |
| 38 print('Tree shaking done'); |
| 39 return _world; |
| 40 } |
| 41 } |
| 42 |
| 43 class TreeShakingVisitor extends RecursiveAstVisitor { |
| 44 final TreeShaker treeShaker; |
| 45 |
| 46 TreeShakingVisitor(this.treeShaker); |
| 47 |
| 48 @override |
| 49 void visitFunctionDeclaration(FunctionDeclaration node) { |
| 50 print('Visiting function ${node.name.name}'); |
| 51 super.visitFunctionDeclaration(node); |
| 52 } |
| 53 |
| 54 @override |
| 55 void visitMethodInvocation(MethodInvocation node) { |
| 56 print('Visiting invocation of ${node.methodName.name}'); |
| 57 Element staticElement = node.methodName.staticElement; |
| 58 if (staticElement != null) { |
| 59 // TODO(paulberry): deal with the case where staticElement is |
| 60 // not necessarily the exact target. (Dart2js calls this a |
| 61 // "dynamic invocation"). We need a notion of "selector". Maybe |
| 62 // we can use Dart2js selectors. |
| 63 treeShaker.add(staticElement); |
| 64 } else { |
| 65 // TODO(paulberry): deal with this case. |
| 66 } |
| 67 super.visitMethodInvocation(node); |
| 68 } |
| 69 |
| 70 } |
| 71 |
| OLD | NEW |