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

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

Issue 11267046: [dart2dart] fix after https://codereview.chromium.org/11227007 (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 1 month 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 { 5 class LocalPlaceholder {
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 get hashCode => identifier.hashCode; 9 int get 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 ConstructorPlaceholder {
26 final Node node;
27 final DartType type;
28 ConstructorPlaceholder(this.node, this.type);
29 }
30
25 class DeclarationTypePlaceholder { 31 class DeclarationTypePlaceholder {
26 final TypeAnnotation typeNode; 32 final TypeAnnotation typeNode;
27 final bool requiresVar; 33 final bool requiresVar;
28 DeclarationTypePlaceholder(this.typeNode, this.requiresVar); 34 DeclarationTypePlaceholder(this.typeNode, this.requiresVar);
29 } 35 }
30 36
31 class SendVisitor extends ResolvedVisitor { 37 class SendVisitor extends ResolvedVisitor {
32 final PlaceholderCollector collector; 38 final PlaceholderCollector collector;
33 39
34 get compiler => collector.compiler; 40 get compiler => collector.compiler;
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 collector.internalError(reason, node); 143 collector.internalError(reason, node);
138 } 144 }
139 } 145 }
140 146
141 class PlaceholderCollector extends Visitor { 147 class PlaceholderCollector extends Visitor {
142 final Compiler compiler; 148 final Compiler compiler;
143 final Set<String> fixedMemberNames; // member names which cannot be renamed. 149 final Set<String> fixedMemberNames; // member names which cannot be renamed.
144 final Map<Element, ElementAst> elementAsts; 150 final Map<Element, ElementAst> elementAsts;
145 final Set<Node> nullNodes; // Nodes that should not be in output. 151 final Set<Node> nullNodes; // Nodes that should not be in output.
146 final Set<Identifier> unresolvedNodes; 152 final Set<Identifier> unresolvedNodes;
147 final Map<Element, Set<Identifier>> elementNodes; 153 final Map<Element, Set<Node>> elementNodes;
148 final Map<FunctionElement, FunctionScope> functionScopes; 154 final Map<FunctionElement, FunctionScope> functionScopes;
149 final Map<LibraryElement, Set<Identifier>> privateNodes; 155 final Map<LibraryElement, Set<Identifier>> privateNodes;
150 final List<DeclarationTypePlaceholder> declarationTypePlaceholders; 156 final List<DeclarationTypePlaceholder> declarationTypePlaceholders;
151 final Map<String, Set<Identifier>> memberPlaceholders; 157 final Map<String, Set<Identifier>> memberPlaceholders;
158 final Map<Element, List<ConstructorPlaceholder>> constructorPlaceholders;
152 Map<String, LocalPlaceholder> currentLocalPlaceholders; 159 Map<String, LocalPlaceholder> currentLocalPlaceholders;
153 Element currentElement; 160 Element currentElement;
154 FunctionElement topmostEnclosingFunction; 161 FunctionElement topmostEnclosingFunction;
155 TreeElements treeElements; 162 TreeElements treeElements;
156 163
157 LibraryElement get coreLibrary => compiler.coreLibrary; 164 LibraryElement get coreLibrary => compiler.coreLibrary;
158 FunctionElement get entryFunction => compiler.mainApp.find(Compiler.MAIN); 165 FunctionElement get entryFunction => compiler.mainApp.find(Compiler.MAIN);
159 166
160 get currentFunctionScope => functionScopes.putIfAbsent( 167 get currentFunctionScope => functionScopes.putIfAbsent(
161 topmostEnclosingFunction, () => new FunctionScope()); 168 topmostEnclosingFunction, () => new FunctionScope());
162 169
163 PlaceholderCollector(this.compiler, this.fixedMemberNames, this.elementAsts) : 170 PlaceholderCollector(this.compiler, this.fixedMemberNames, this.elementAsts) :
164 nullNodes = new Set<Node>(), 171 nullNodes = new Set<Node>(),
165 unresolvedNodes = new Set<Identifier>(), 172 unresolvedNodes = new Set<Identifier>(),
166 elementNodes = new Map<Element, Set<Identifier>>(), 173 elementNodes = new Map<Element, Set<Node>>(),
167 functionScopes = new Map<FunctionElement, FunctionScope>(), 174 functionScopes = new Map<FunctionElement, FunctionScope>(),
168 privateNodes = new Map<LibraryElement, Set<Identifier>>(), 175 privateNodes = new Map<LibraryElement, Set<Identifier>>(),
169 declarationTypePlaceholders = new List<DeclarationTypePlaceholder>(), 176 declarationTypePlaceholders = new List<DeclarationTypePlaceholder>(),
170 memberPlaceholders = new Map<String, Set<Identifier>>(); 177 memberPlaceholders = new Map<String, Set<Identifier>>(),
178 constructorPlaceholders = new Map<Element, List<ConstructorPlaceholder>>() ;
171 179
172 void tryMakeConstructorNamePlaceholder( 180 void tryMakeConstructorNamePlaceholder(
173 FunctionExpression constructor, ClassElement element) { 181 FunctionExpression constructor, ClassElement element) {
174 Node nameNode = constructor.name; 182 Node nameNode = constructor.name;
175 if (nameNode is Send) nameNode = nameNode.receiver; 183 if (nameNode is Send) nameNode = nameNode.receiver;
176 if (nameNode.asIdentifier().token.slowToString() 184 if (nameNode.asIdentifier().token.slowToString()
177 == element.name.slowToString()) { 185 == element.name.slowToString()) {
178 makeElementPlaceholder(nameNode, element); 186 makeElementPlaceholder(nameNode, element);
179 } 187 }
180 } 188 }
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
322 bool requiresVar = !node.modifiers.isFinalOrConst(); 330 bool requiresVar = !node.modifiers.isFinalOrConst();
323 declarationTypePlaceholders.add( 331 declarationTypePlaceholders.add(
324 new DeclarationTypePlaceholder(node.type, requiresVar)); 332 new DeclarationTypePlaceholder(node.type, requiresVar));
325 } 333 }
326 334
327 void makeNullPlaceholder(Node node) { 335 void makeNullPlaceholder(Node node) {
328 assert(node is Identifier || node is Send); 336 assert(node is Identifier || node is Send);
329 nullNodes.add(node); 337 nullNodes.add(node);
330 } 338 }
331 339
332 void makeElementPlaceholder(Identifier node, Element element) { 340 void makeElementPlaceholder(Node node, Element element) {
333 assert(element != null); 341 assert(element != null);
334 if (identical(element, entryFunction)) return; 342 if (identical(element, entryFunction)) return;
335 if (identical(element.getLibrary(), coreLibrary)) return; 343 if (identical(element.getLibrary(), coreLibrary)) return;
336 if (element.getLibrary().isPlatformLibrary && !element.isTopLevel()) { 344 if (element.getLibrary().isPlatformLibrary && !element.isTopLevel()) {
337 return; 345 return;
338 } 346 }
339 if (element == compiler.types.dynamicType.element) { 347 if (element == compiler.types.dynamicType.element) {
340 internalError( 348 internalError(
341 'Should never make element placeholder for dynamic type element', 349 'Should never make element placeholder for dynamic type element',
342 node); 350 node);
343 } 351 }
344 elementNodes.putIfAbsent(element, () => new Set<Identifier>()).add(node); 352 elementNodes.putIfAbsent(element, () => new Set<Node>()).add(node);
345 } 353 }
346 354
347 void makePrivateIdentifier(Identifier node) { 355 void makePrivateIdentifier(Identifier node) {
348 assert(node != null); 356 assert(node != null);
349 privateNodes.putIfAbsent( 357 privateNodes.putIfAbsent(
350 currentElement.getLibrary(), () => new Set<Identifier>()).add(node); 358 currentElement.getLibrary(), () => new Set<Identifier>()).add(node);
351 } 359 }
352 360
353 void makeUnresolvedPlaceholder(Node node) { 361 void makeUnresolvedPlaceholder(Node node) {
354 unresolvedNodes.add(node); 362 unresolvedNodes.add(node);
355 } 363 }
356 364
357 void makeLocalPlaceholder(Identifier identifier) { 365 void makeLocalPlaceholder(Identifier identifier) {
358 LocalPlaceholder getLocalPlaceholder() { 366 LocalPlaceholder getLocalPlaceholder() {
359 String name = identifier.source.slowToString(); 367 String name = identifier.source.slowToString();
360 return currentLocalPlaceholders.putIfAbsent(name, () { 368 return currentLocalPlaceholders.putIfAbsent(name, () {
361 LocalPlaceholder localPlaceholder = new LocalPlaceholder(name); 369 LocalPlaceholder localPlaceholder = new LocalPlaceholder(name);
362 currentFunctionScope.localPlaceholders.add(localPlaceholder); 370 currentFunctionScope.localPlaceholders.add(localPlaceholder);
363 return localPlaceholder; 371 return localPlaceholder;
364 }); 372 });
365 } 373 }
366 374
367 getLocalPlaceholder().nodes.add(identifier); 375 getLocalPlaceholder().nodes.add(identifier);
368 } 376 }
369 377
378 void makeConstructorPlaceholder(Node node, Element element, DartType type) {
379 constructorPlaceholders
380 .putIfAbsent(element, () => <ConstructorPlaceholder>[])
381 .add(new ConstructorPlaceholder(node, type));
382 }
383
370 void internalError(String reason, {Node node}) { 384 void internalError(String reason, {Node node}) {
371 compiler.cancel(reason, node: node); 385 compiler.cancel(reason, node: node);
372 } 386 }
373 387
374 void unreachable() { internalError('Unreachable case'); } 388 void unreachable() { internalError('Unreachable case'); }
375 389
376 visit(Node node) => (node == null) ? null : node.accept(this); 390 visit(Node node) => (node == null) ? null : node.accept(this);
377 391
378 visitNode(Node node) { node.visitChildren(this); } // We must go deeper. 392 visitNode(Node node) { node.visitChildren(this); } // We must go deeper.
379 393
394 visitNewExpression(NewExpression node) {
395 Send send = node.send;
396 InterfaceType type = treeElements.getType(node);
397 assert(type !== null);
398 Element constructor = treeElements[send];
399 assert(constructor !== null);
400 assert(send.receiver == null);
401 if (constructor is !ErroneousElement) {
402 makeConstructorPlaceholder(node.send.selector, constructor, type);
403 }
404 visit(node.send.argumentsNode);
405 }
406
380 visitSend(Send send) { 407 visitSend(Send send) {
381 new SendVisitor(this, treeElements).visitSend(send); 408 new SendVisitor(this, treeElements).visitSend(send);
382 send.visitChildren(this); 409 send.visitChildren(this);
383 } 410 }
384 411
385 visitSendSet(SendSet send) { 412 visitSendSet(SendSet send) {
386 Element element = treeElements[send]; 413 Element element = treeElements[send];
387 if (Elements.isErroneousElement(element)) { 414 if (Elements.isErroneousElement(element)) {
388 // Complicated case: constructs like receiver.selector++ can resolve 415 // Complicated case: constructs like receiver.selector++ can resolve
389 // to ErroneousElement. Fortunately, receiver.selector still 416 // to ErroneousElement. Fortunately, receiver.selector still
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after
636 663
637 visitBlock(Block node) { 664 visitBlock(Block node) {
638 for (Node statement in node.statements.nodes) { 665 for (Node statement in node.statements.nodes) {
639 if (statement is VariableDefinitions) { 666 if (statement is VariableDefinitions) {
640 makeVarDeclarationTypePlaceholder(statement); 667 makeVarDeclarationTypePlaceholder(statement);
641 } 668 }
642 } 669 }
643 node.visitChildren(this); 670 node.visitChildren(this);
644 } 671 }
645 } 672 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698