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

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

Issue 10891004: [dart2dart] Optionally cut types in variable declarations: (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 3 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 class LocalPlaceholder implements Hashable { 5 class LocalPlaceholder implements Hashable {
6 final String identifier; 6 final String identifier;
7 final Set<Node> nodes; 7 final Set<Node> nodes;
8 LocalPlaceholder(this.identifier) : nodes = new Set<Node>(); 8 LocalPlaceholder(this.identifier) : nodes = new Set<Node>();
9 int hashCode() => identifier.hashCode(); 9 int hashCode() => identifier.hashCode();
10 String toString() => 10 String toString() =>
11 'local_placeholder[id($identifier), nodes($nodes)]'; 11 'local_placeholder[id($identifier), nodes($nodes)]';
12 } 12 }
13 13
14 class FunctionScope { 14 class FunctionScope {
15 final Set<String> parameterIdentifiers; 15 final Set<String> parameterIdentifiers;
16 final Set<LocalPlaceholder> localPlaceholders; 16 final Set<LocalPlaceholder> localPlaceholders;
17 FunctionScope() 17 FunctionScope()
18 : parameterIdentifiers = new Set<String>(), 18 : parameterIdentifiers = new Set<String>(),
19 localPlaceholders = new Set<LocalPlaceholder>(); 19 localPlaceholders = new Set<LocalPlaceholder>();
20 void registerParameter(Identifier node) { 20 void registerParameter(Identifier node) {
21 parameterIdentifiers.add(node.source.slowToString()); 21 parameterIdentifiers.add(node.source.slowToString());
22 } 22 }
23 } 23 }
24 24
25 class DeclarationTypePlaceholder {
26 final TypeAnnotation typeNode;
27 final bool canOmitType;
Anton Muhin 2012/08/28 09:13:37 canOmitType is somewhat misnomer as in the case of
Roman 2012/08/28 12:49:30 Done.
28 DeclarationTypePlaceholder(this.typeNode, this.canOmitType);
29 }
30
25 class SendVisitor extends ResolvedVisitor { 31 class SendVisitor extends ResolvedVisitor {
26 final PlaceholderCollector collector; 32 final PlaceholderCollector collector;
27 33
28 SendVisitor(this.collector, TreeElements elements) : super(elements); 34 SendVisitor(this.collector, TreeElements elements) : super(elements);
29 35
30 visitDynamicSend(Send node) {} 36 visitDynamicSend(Send node) {}
31 visitSuperSend(Send node) {} 37 visitSuperSend(Send node) {}
32 visitOperatorSend(Send node) {} 38 visitOperatorSend(Send node) {}
33 visitForeignSend(Send node) {} 39 visitForeignSend(Send node) {}
34 40
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 } 84 }
79 } 85 }
80 86
81 class PlaceholderCollector extends AbstractVisitor { 87 class PlaceholderCollector extends AbstractVisitor {
82 final Compiler compiler; 88 final Compiler compiler;
83 final Set<Node> nullNodes; // Nodes that should not be in output. 89 final Set<Node> nullNodes; // Nodes that should not be in output.
84 final Set<Identifier> unresolvedNodes; 90 final Set<Identifier> unresolvedNodes;
85 final Map<Element, Set<Node>> elementNodes; 91 final Map<Element, Set<Node>> elementNodes;
86 final Map<FunctionElement, FunctionScope> functionScopes; 92 final Map<FunctionElement, FunctionScope> functionScopes;
87 final Map<LibraryElement, Set<Identifier>> privateNodes; 93 final Map<LibraryElement, Set<Identifier>> privateNodes;
94 final List<DeclarationTypePlaceholder> declarationTypePlaceholders;
88 Map<String, LocalPlaceholder> currentLocalPlaceholders; 95 Map<String, LocalPlaceholder> currentLocalPlaceholders;
89 Element currentElement; 96 Element currentElement;
90 TreeElements treeElements; 97 TreeElements treeElements;
91 98
92 LibraryElement get coreLibrary => compiler.coreLibrary; 99 LibraryElement get coreLibrary => compiler.coreLibrary;
93 FunctionElement get entryFunction => compiler.mainApp.find(Compiler.MAIN); 100 FunctionElement get entryFunction => compiler.mainApp.find(Compiler.MAIN);
94 101
95 PlaceholderCollector(this.compiler) : 102 PlaceholderCollector(this.compiler) :
96 nullNodes = new Set<Node>(), 103 nullNodes = new Set<Node>(),
97 unresolvedNodes = new Set<Identifier>(), 104 unresolvedNodes = new Set<Identifier>(),
98 elementNodes = new Map<Element, Set<Node>>(), 105 elementNodes = new Map<Element, Set<Node>>(),
99 functionScopes = new Map<FunctionElement, FunctionScope>(), 106 functionScopes = new Map<FunctionElement, FunctionScope>(),
100 privateNodes = new Map<LibraryElement, Set<Identifier>>(); 107 privateNodes = new Map<LibraryElement, Set<Identifier>>(),
108 declarationTypePlaceholders = new List<DeclarationTypePlaceholder>();
101 109
102 void tryMakeConstructorNamePlaceholder( 110 void tryMakeConstructorNamePlaceholder(
103 FunctionExpression constructor, ClassElement element) { 111 FunctionExpression constructor, ClassElement element) {
104 Node nameNode = constructor.name; 112 Node nameNode = constructor.name;
105 if (nameNode is Send) nameNode = nameNode.receiver; 113 if (nameNode is Send) nameNode = nameNode.receiver;
106 if (nameNode.asIdentifier().token.slowToString() 114 if (nameNode.asIdentifier().token.slowToString()
107 == element.name.slowToString()) { 115 == element.name.slowToString()) {
108 makeElementPlaceholder(nameNode, element); 116 makeElementPlaceholder(nameNode, element);
109 } 117 }
110 } 118 }
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 if (Elements.isStaticOrTopLevel(element)) { 165 if (Elements.isStaticOrTopLevel(element)) {
158 Node fieldNode = element.parseNode(compiler); 166 Node fieldNode = element.parseNode(compiler);
159 if (fieldNode is Identifier) { 167 if (fieldNode is Identifier) {
160 makeElementPlaceholder(fieldNode, element); 168 makeElementPlaceholder(fieldNode, element);
161 } else if (fieldNode is SendSet) { 169 } else if (fieldNode is SendSet) {
162 makeElementPlaceholder(fieldNode.selector, element); 170 makeElementPlaceholder(fieldNode.selector, element);
163 } else { 171 } else {
164 unreachable(); 172 unreachable();
165 } 173 }
166 } 174 }
175 makeVarDeclarationTypePlaceholder(node);
167 } 176 }
168 177
169 void collect(Element element, TreeElements elements) { 178 void collect(Element element, TreeElements elements) {
170 treeElements = elements; 179 treeElements = elements;
171 Node elementNode; 180 Node elementNode;
172 if (element is FunctionElement) { 181 if (element is FunctionElement) {
173 currentElement = element; 182 currentElement = element;
174 elementNode = currentElement.parseNode(compiler); 183 elementNode = currentElement.parseNode(compiler);
175 collectFunctionDeclarationPlaceholders(element, elementNode); 184 collectFunctionDeclarationPlaceholders(element, elementNode);
176 } else if (element.isField()) { 185 } else if (element.isField()) {
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 .registerParameter(node); 219 .registerParameter(node);
211 } else if (Elements.isLocal(element)) { 220 } else if (Elements.isLocal(element)) {
212 makeLocalPlaceholder(node); 221 makeLocalPlaceholder(node);
213 } 222 }
214 } 223 }
215 224
216 void makeTypePlaceholder(Node node, Type type) { 225 void makeTypePlaceholder(Node node, Type type) {
217 makeElementPlaceholder(node, type.element); 226 makeElementPlaceholder(node, type.element);
218 } 227 }
219 228
229 void makeDeclarationTypePlaceholder(TypeAnnotation type, bool canOmitType) {
Anton Muhin 2012/08/28 09:13:37 it looks like the only place where canOmitType is
Roman 2012/08/28 12:49:30 I can, but I don't see any benefits. I will save o
Anton Muhin 2012/08/28 13:06:32 That's not the matter of saving argument, but the
Roman 2012/08/28 13:27:51 What about renaming this method to makeOmitDeclara
Anton Muhin 2012/08/28 13:37:23 I like the idea with renaming On 2012/08/28 13:2
Roman 2012/08/28 14:48:55 Done.
230 if (type === null) return;
231 declarationTypePlaceholders.add(
232 new DeclarationTypePlaceholder(type, canOmitType));
233 }
234
235 void makeVarDeclarationTypePlaceholder(VariableDefinitions node) {
236 Element definitionElement = treeElements[node.definitions.nodes.head];
237 bool canOmitType = node.modifiers.isFinalOrConst();
238 makeDeclarationTypePlaceholder(node.type, canOmitType);
239 }
240
220 void makeNullPlaceholder(Node node) { 241 void makeNullPlaceholder(Node node) {
221 assert(node is Identifier || node is Send); 242 assert(node is Identifier || node is Send);
222 nullNodes.add(node); 243 nullNodes.add(node);
223 } 244 }
224 245
225 void makeElementPlaceholder(Node node, Element element) { 246 void makeElementPlaceholder(Node node, Element element) {
226 assert(element !== null); 247 assert(element !== null);
227 if (element === entryFunction) return; 248 if (element === entryFunction) return;
228 if (element.getLibrary() === coreLibrary) return; 249 if (element.getLibrary() === coreLibrary) return;
229 if (isDartCoreLib(compiler, element.getLibrary()) 250 if (isDartCoreLib(compiler, element.getLibrary())
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
384 if (element !== null) { 405 if (element !== null) {
385 // Rename only local functions. 406 // Rename only local functions.
386 if (element !== currentElement) { 407 if (element !== currentElement) {
387 if (node.name !== null) { 408 if (node.name !== null) {
388 assert(node.name is Identifier); 409 assert(node.name is Identifier);
389 tryMakeLocalPlaceholder(element, node.name); 410 tryMakeLocalPlaceholder(element, node.name);
390 } 411 }
391 } 412 }
392 } 413 }
393 node.visitChildren(this); 414 node.visitChildren(this);
415 makeDeclarationTypePlaceholder(node.returnType, true);
416 collectFunctionParameters(node.parameters);
417 }
418
419 void collectFunctionParameters(NodeList parameters) {
420 if (parameters === null) return;
421 for (Link<Node> link = parameters.nodes; !link.isEmpty(); link= link.tail) {
Anton Muhin 2012/08/28 09:13:37 for (Node parameter in parameters) ?
Anton Muhin 2012/08/28 09:13:37 nit: space before =
Roman 2012/08/28 12:49:30 Can't do this, the structure is: first several Nod
Roman 2012/08/28 12:49:30 Done.
Anton Muhin 2012/08/28 13:06:32 Yes, this looks more readable to me. On 2012/08/2
Roman 2012/08/28 13:27:51 Sorry, which one looks more readable?
Anton Muhin 2012/08/28 13:37:23 Sorry, with for loop and inner dispatch, one in yo
Roman 2012/08/28 14:48:55 Done.
422 Node parameter = link.head;
423 if (parameter is NodeList) {
424 link = parameter.nodes;
425 parameter = link.head;
426 }
427 assert(parameter is VariableDefinitions);
428 makeDeclarationTypePlaceholder(
429 (parameter as VariableDefinitions).type, true);
Anton Muhin 2012/08/28 09:13:37 parameter.asVariableDefinitions() ?
Roman 2012/08/28 12:49:30 Done.
430 }
394 } 431 }
395 432
396 visitClassNode(ClassNode node) { 433 visitClassNode(ClassNode node) {
397 ClassElement classElement = currentElement; 434 ClassElement classElement = currentElement;
398 makeElementPlaceholder(node.name, classElement); 435 makeElementPlaceholder(node.name, classElement);
399 node.visitChildren(this); 436 node.visitChildren(this);
400 if (node.defaultClause !== null) { 437 if (node.defaultClause !== null) {
401 // Can't just visit class node's default clause because of the bug in the 438 // Can't just visit class node's default clause because of the bug in the
402 // resolver, it just crashes when it meets type variable. 439 // resolver, it just crashes when it meets type variable.
403 Type defaultType = classElement.defaultClass; 440 Type defaultType = classElement.defaultClass;
(...skipping 28 matching lines...) Expand all
432 visitTypeVariable(TypeVariable node) { 469 visitTypeVariable(TypeVariable node) {
433 assert(currentElement is TypedefElement || currentElement is ClassElement); 470 assert(currentElement is TypedefElement || currentElement is ClassElement);
434 tryResolveAndCollectTypeVariable(currentElement, node.name); 471 tryResolveAndCollectTypeVariable(currentElement, node.name);
435 node.visitChildren(this); 472 node.visitChildren(this);
436 } 473 }
437 474
438 visitTypedef(Typedef node) { 475 visitTypedef(Typedef node) {
439 assert(currentElement is TypedefElement); 476 assert(currentElement is TypedefElement);
440 makeElementPlaceholder(node.name, currentElement); 477 makeElementPlaceholder(node.name, currentElement);
441 node.visitChildren(this); 478 node.visitChildren(this);
479 makeDeclarationTypePlaceholder(node.returnType, true);
480 collectFunctionParameters(node.formals);
481 }
482
483 visitBlock(Block node) {
484 node.statements.nodes.forEach((Node statement) {
Anton Muhin 2012/08/28 09:13:37 plain for loop?
Roman 2012/08/28 12:49:30 Done.
485 if (statement is VariableDefinitions) {
486 makeVarDeclarationTypePlaceholder(statement);
Anton Muhin 2012/08/28 09:13:37 why this cannot be a part of visitVariableDefiniti
Roman 2012/08/28 12:49:30 As discussed, there's a catch statement that I wan
487 }
488 });
489 node.visitChildren(this);
442 } 490 }
443 } 491 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698