Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(498)

Side by Side Diff: lib/compiler/implementation/dart_backend/backend.dart

Issue 10963055: [dart2dart] Make removal of asserts and forcing type strips go under (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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
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 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
299 new ElementAst.forClassLike(parse(element))); 302 new ElementAst.forClassLike(parse(element)));
300 }; 303 };
301 newClassElementCallback = (ClassElement classElement) { 304 newClassElementCallback = (ClassElement classElement) {
302 if (!shouldOutput(classElement)) return; 305 if (!shouldOutput(classElement)) return;
303 addClass(classElement); 306 addClass(classElement);
304 }; 307 };
305 308
306 resolvedElements.forEach((element, treeElements) { 309 resolvedElements.forEach((element, treeElements) {
307 if (!shouldOutput(element)) return; 310 if (!shouldOutput(element)) return;
308 311
309 var elementAst = 312 var elementAst = new ElementAst.rewrite(
310 new ElementAst.rewrite(compiler, parse(element), treeElements); 313 compiler, parse(element), treeElements, stripAsserts);
311 if (element.isField()) { 314 if (element.isField()) {
312 final list = (element as VariableElement).variables; 315 final list = (element as VariableElement).variables;
313 elementAst = elementAsts.putIfAbsent( 316 elementAst = elementAsts.putIfAbsent(
314 list, () => new VariableListAst(parse(list))); 317 list, () => new VariableListAst(parse(list)));
315 (elementAst as VariableListAst).add(element, treeElements); 318 (elementAst as VariableListAst).add(element, treeElements);
316 element = list; 319 element = list;
317 } 320 }
318 321
319 if (element.isMember()) { 322 if (element.isMember()) {
320 ClassElement enclosingClass = element.getEnclosingClass(); 323 ClassElement enclosingClass = element.getEnclosingClass();
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
376 makePlaceholders(element) { 379 makePlaceholders(element) {
377 collector.collect(element); 380 collector.collect(element);
378 if (element is ClassElement) { 381 if (element is ClassElement) {
379 classMembers[element].forEach(makePlaceholders); 382 classMembers[element].forEach(makePlaceholders);
380 } 383 }
381 } 384 }
382 topLevelElements.forEach(makePlaceholders); 385 topLevelElements.forEach(makePlaceholders);
383 // Create renames. 386 // Create renames.
384 Map<Node, String> renames = new Map<Node, String>(); 387 Map<Node, String> renames = new Map<Node, String>();
385 Map<LibraryElement, String> imports = new Map<LibraryElement, String>(); 388 Map<LibraryElement, String> imports = new Map<LibraryElement, String>();
386 bool shouldCutDeclarationTypes = forceCutDeclarationTypes 389 bool shouldCutDeclarationTypes = forceStripTypes
387 || (compiler.enableMinification 390 || (compiler.enableMinification
388 && isSafeToRemoveTypeDeclarations(classMembers)); 391 && isSafeToRemoveTypeDeclarations(classMembers));
389 renamePlaceholders( 392 renamePlaceholders(
390 compiler, collector, renames, imports, 393 compiler, collector, renames, imports,
391 fixedMemberNames, shouldCutDeclarationTypes); 394 fixedMemberNames, shouldCutDeclarationTypes);
392 395
393 // Sort elements. 396 // Sort elements.
394 final sortedTopLevels = sortElements(topLevelElements); 397 final sortedTopLevels = sortElements(topLevelElements);
395 final sortedClassMembers = new Map<ClassElement, List<Element>>(); 398 final sortedClassMembers = new Map<ClassElement, List<Element>>();
396 classMembers.forEach((classElement, members) { 399 classMembers.forEach((classElement, members) {
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
515 } 518 }
516 519
517 compareElements(e0, e1) { 520 compareElements(e0, e1) {
518 int result = compareBy((e) => e.getLibrary().uri.toString())(e0, e1); 521 int result = compareBy((e) => e.getLibrary().uri.toString())(e0, e1);
519 if (result != 0) return result; 522 if (result != 0) return result;
520 return compareBy((e) => e.position().charOffset)(e0, e1); 523 return compareBy((e) => e.position().charOffset)(e0, e1);
521 } 524 }
522 525
523 List<Element> sortElements(Collection<Element> elements) => 526 List<Element> sortElements(Collection<Element> elements) =>
524 sorted(elements, compareElements); 527 sorted(elements, compareElements);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698