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 part of dart_backend; | 5 part of dart_backend; |
6 | 6 |
7 // TODO(ahe): This class is simply wrong. This backend should use | 7 // TODO(ahe): This class is simply wrong. This backend should use |
8 // elements when it can, not AST nodes. Perhaps a [Map<Element, | 8 // elements when it can, not AST nodes. Perhaps a [Map<Element, |
9 // TreeElements>] is what is needed. | 9 // TreeElements>] is what is needed. |
10 class ElementAst { | 10 class ElementAst { |
11 final Node ast; | 11 final Node ast; |
12 final TreeElements treeElements; | 12 final TreeElements treeElements; |
13 | 13 |
14 ElementAst(this.ast, this.treeElements); | 14 ElementAst(this.ast, this.treeElements); |
15 | 15 |
16 factory ElementAst.rewrite(compiler, ast, treeElements, stripAsserts) { | |
17 final rewriter = | |
18 new FunctionBodyRewriter(compiler, treeElements, stripAsserts); | |
19 return new ElementAst(rewriter.visit(ast), rewriter.cloneTreeElements); | |
20 } | |
21 | |
22 ElementAst.forClassLike(this.ast) | 16 ElementAst.forClassLike(this.ast) |
23 : this.treeElements = new TreeElementMapping(null); | 17 : this.treeElements = new TreeElementMapping(null); |
24 } | 18 } |
25 | 19 |
26 // TODO(ahe): This class should not subclass [TreeElementMapping], if | 20 // TODO(ahe): This class should not subclass [TreeElementMapping], if |
27 // anything, it should implement TreeElements. | 21 // anything, it should implement TreeElements. |
28 class AggregatedTreeElements extends TreeElementMapping { | 22 class AggregatedTreeElements extends TreeElementMapping { |
29 final List<TreeElements> treeElements; | 23 final List<TreeElements> treeElements; |
30 | 24 |
31 AggregatedTreeElements() : treeElements = <TreeElements>[], super(null); | 25 AggregatedTreeElements() : treeElements = <TreeElements>[], super(null); |
(...skipping 28 matching lines...) Expand all Loading... |
60 class VariableListAst extends ElementAst { | 54 class VariableListAst extends ElementAst { |
61 VariableListAst(ast) : super(ast, new AggregatedTreeElements()); | 55 VariableListAst(ast) : super(ast, new AggregatedTreeElements()); |
62 | 56 |
63 add(VariableElement element, TreeElements treeElements) { | 57 add(VariableElement element, TreeElements treeElements) { |
64 AggregatedTreeElements e = this.treeElements; | 58 AggregatedTreeElements e = this.treeElements; |
65 e[element.cachedNode] = element; | 59 e[element.cachedNode] = element; |
66 e.treeElements.add(treeElements); | 60 e.treeElements.add(treeElements); |
67 } | 61 } |
68 } | 62 } |
69 | 63 |
70 class FunctionBodyRewriter extends CloningVisitor { | |
71 final Compiler compiler; | |
72 final bool stripAsserts; | |
73 | |
74 FunctionBodyRewriter(this.compiler, originalTreeElements, this.stripAsserts) | |
75 : super(originalTreeElements); | |
76 | |
77 visitBlock(Block block) { | |
78 shouldOmit(Statement statement) { | |
79 if (statement is EmptyStatement) return true; | |
80 ExpressionStatement expressionStatement = | |
81 statement.asExpressionStatement(); | |
82 if (expressionStatement != null) { | |
83 Send send = expressionStatement.expression.asSend(); | |
84 if (send != null) { | |
85 Element element = originalTreeElements[send]; | |
86 if (stripAsserts && identical(element, compiler.assertMethod)) { | |
87 return true; | |
88 } | |
89 } | |
90 } | |
91 return false; | |
92 } | |
93 | |
94 rewriteStatement(Statement statement) { | |
95 Block block = statement.asBlock(); | |
96 if (block != null) { | |
97 Link statements = block.statements.nodes; | |
98 if (!statements.isEmpty && statements.tail.isEmpty) { | |
99 Statement single = statements.head; | |
100 bool isDeclaration = | |
101 single is VariableDefinitions || single is FunctionDeclaration; | |
102 if (!isDeclaration) return single; | |
103 } | |
104 } | |
105 return statement; | |
106 } | |
107 | |
108 NodeList statements = block.statements; | |
109 LinkBuilder<Statement> builder = new LinkBuilder<Statement>(); | |
110 for (Statement statement in statements.nodes) { | |
111 if (!shouldOmit(statement)) { | |
112 builder.addLast(visit(rewriteStatement(statement))); | |
113 } | |
114 } | |
115 return new Block(rewriteNodeList(statements, builder.toLink())); | |
116 } | |
117 } | |
118 | |
119 class DartBackend extends Backend { | 64 class DartBackend extends Backend { |
120 final List<CompilerTask> tasks; | 65 final List<CompilerTask> tasks; |
121 final bool forceStripTypes; | 66 final bool forceStripTypes; |
122 final bool stripAsserts; | 67 final bool stripAsserts; |
123 // TODO(antonm): make available from command-line options. | 68 // TODO(antonm): make available from command-line options. |
124 final bool outputAst = false; | 69 final bool outputAst = false; |
125 final Map<Node, String> renames; | 70 final Map<Node, String> renames; |
126 final Map<LibraryElement, String> imports; | 71 final Map<LibraryElement, String> imports; |
127 final Map<ClassNode, List<Node>> memberNodes; | 72 final Map<ClassNode, List<Node>> memberNodes; |
128 Map<Element, LibraryElement> reexportingLibraries; | 73 Map<Element, LibraryElement> reexportingLibraries; |
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
357 if (!shouldOutput(classElement)) return; | 302 if (!shouldOutput(classElement)) return; |
358 addClass(classElement); | 303 addClass(classElement); |
359 }; | 304 }; |
360 | 305 |
361 compiler.resolverWorld.instantiatedClasses.forEach( | 306 compiler.resolverWorld.instantiatedClasses.forEach( |
362 (ClassElement classElement) { | 307 (ClassElement classElement) { |
363 if (shouldOutput(classElement)) addClass(classElement); | 308 if (shouldOutput(classElement)) addClass(classElement); |
364 }); | 309 }); |
365 resolvedElements.forEach((element, treeElements) { | 310 resolvedElements.forEach((element, treeElements) { |
366 if (!shouldOutput(element) || treeElements == null) return; | 311 if (!shouldOutput(element) || treeElements == null) return; |
367 var elementAst = new ElementAst.rewrite( | 312 var elementAst = new ElementAst(parse(element), treeElements); |
368 compiler, parse(element), treeElements, stripAsserts); | |
369 if (element.isField()) { | 313 if (element.isField()) { |
370 final list = (element as VariableElement).variables; | 314 final list = (element as VariableElement).variables; |
371 elementAst = elementAsts.putIfAbsent( | 315 elementAst = elementAsts.putIfAbsent( |
372 list, () => new VariableListAst(parse(list))); | 316 list, () => new VariableListAst(parse(list))); |
373 (elementAst as VariableListAst).add(element, treeElements); | 317 (elementAst as VariableListAst).add(element, treeElements); |
374 element = list; | 318 element = list; |
375 } | 319 } |
376 | 320 |
377 if (element.isMember()) { | 321 if (element.isMember()) { |
378 ClassElement enclosingClass = element.getEnclosingClass(); | 322 ClassElement enclosingClass = element.getEnclosingClass(); |
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
644 } | 588 } |
645 | 589 |
646 compareElements(e0, e1) { | 590 compareElements(e0, e1) { |
647 int result = compareBy((e) => e.getLibrary().canonicalUri.toString())(e0, e1); | 591 int result = compareBy((e) => e.getLibrary().canonicalUri.toString())(e0, e1); |
648 if (result != 0) return result; | 592 if (result != 0) return result; |
649 return compareBy((e) => e.position().charOffset)(e0, e1); | 593 return compareBy((e) => e.position().charOffset)(e0, e1); |
650 } | 594 } |
651 | 595 |
652 List<Element> sortElements(Iterable<Element> elements) => | 596 List<Element> sortElements(Iterable<Element> elements) => |
653 sorted(elements, compareElements); | 597 sorted(elements, compareElements); |
OLD | NEW |