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

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 requiresVar;
28 DeclarationTypePlaceholder(this.typeNode, this.requiresVar);
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 makeOmitDeclarationTypePlaceholder(TypeAnnotation type) {
230 if (type === null) return;
231 declarationTypePlaceholders.add(
232 new DeclarationTypePlaceholder(type, false));
233 }
234
235 void makeVarDeclarationTypePlaceholder(VariableDefinitions node) {
236 // TODO(smok): Maybe instead of calling this method and
237 // makeDeclaratioTypePlaceholder have type declaration placeholder
238 // collector logic in visitVariableDefinitions when resolver becomes better
239 // and/or catch syntax changes.
240 if (node.type === null) return;
241 Element definitionElement = treeElements[node.definitions.nodes.head];
242 bool requiresVar = !node.modifiers.isFinalOrConst();
243 declarationTypePlaceholders.add(
244 new DeclarationTypePlaceholder(node.type, requiresVar));
245 }
246
220 void makeNullPlaceholder(Node node) { 247 void makeNullPlaceholder(Node node) {
221 assert(node is Identifier || node is Send); 248 assert(node is Identifier || node is Send);
222 nullNodes.add(node); 249 nullNodes.add(node);
223 } 250 }
224 251
225 void makeElementPlaceholder(Node node, Element element) { 252 void makeElementPlaceholder(Node node, Element element) {
226 assert(element !== null); 253 assert(element !== null);
227 if (element === entryFunction) return; 254 if (element === entryFunction) return;
228 if (element.getLibrary() === coreLibrary) return; 255 if (element.getLibrary() === coreLibrary) return;
229 if (isDartCoreLib(compiler, element.getLibrary()) 256 if (isDartCoreLib(compiler, element.getLibrary())
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
384 if (element !== null) { 411 if (element !== null) {
385 // Rename only local functions. 412 // Rename only local functions.
386 if (element !== currentElement) { 413 if (element !== currentElement) {
387 if (node.name !== null) { 414 if (node.name !== null) {
388 assert(node.name is Identifier); 415 assert(node.name is Identifier);
389 tryMakeLocalPlaceholder(element, node.name); 416 tryMakeLocalPlaceholder(element, node.name);
390 } 417 }
391 } 418 }
392 } 419 }
393 node.visitChildren(this); 420 node.visitChildren(this);
421 makeOmitDeclarationTypePlaceholder(node.returnType);
422 collectFunctionParameters(node.parameters);
423 }
424
425 void collectFunctionParameters(NodeList parameters) {
426 if (parameters === null) return;
427 for (Node parameter in parameters.nodes) {
428 if (parameter is NodeList) {
429 // Optional parameter list.
430 collectFunctionParameters(parameter);
431 } else {
432 assert(parameter is VariableDefinitions);
433 makeOmitDeclarationTypePlaceholder(
434 parameter.asVariableDefinitions().type);
435 }
436 }
394 } 437 }
395 438
396 visitClassNode(ClassNode node) { 439 visitClassNode(ClassNode node) {
397 ClassElement classElement = currentElement; 440 ClassElement classElement = currentElement;
398 makeElementPlaceholder(node.name, classElement); 441 makeElementPlaceholder(node.name, classElement);
399 node.visitChildren(this); 442 node.visitChildren(this);
400 if (node.defaultClause !== null) { 443 if (node.defaultClause !== null) {
401 // Can't just visit class node's default clause because of the bug in the 444 // 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. 445 // resolver, it just crashes when it meets type variable.
403 Type defaultType = classElement.defaultClass; 446 Type defaultType = classElement.defaultClass;
(...skipping 28 matching lines...) Expand all
432 visitTypeVariable(TypeVariable node) { 475 visitTypeVariable(TypeVariable node) {
433 assert(currentElement is TypedefElement || currentElement is ClassElement); 476 assert(currentElement is TypedefElement || currentElement is ClassElement);
434 tryResolveAndCollectTypeVariable(currentElement, node.name); 477 tryResolveAndCollectTypeVariable(currentElement, node.name);
435 node.visitChildren(this); 478 node.visitChildren(this);
436 } 479 }
437 480
438 visitTypedef(Typedef node) { 481 visitTypedef(Typedef node) {
439 assert(currentElement is TypedefElement); 482 assert(currentElement is TypedefElement);
440 makeElementPlaceholder(node.name, currentElement); 483 makeElementPlaceholder(node.name, currentElement);
441 node.visitChildren(this); 484 node.visitChildren(this);
485 makeOmitDeclarationTypePlaceholder(node.returnType);
486 collectFunctionParameters(node.formals);
487 }
488
489 visitBlock(Block node) {
490 for (Node statement in node.statements.nodes) {
491 if (statement is VariableDefinitions) {
492 makeVarDeclarationTypePlaceholder(statement);
493 }
494 }
495 node.visitChildren(this);
442 } 496 }
443 } 497 }
OLDNEW
« no previous file with comments | « lib/compiler/implementation/dart_backend/backend.dart ('k') | lib/compiler/implementation/dart_backend/renamer.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698