Chromium Code Reviews| 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 #library("closureToClassMapper"); | 5 #library("closureToClassMapper"); |
| 6 | 6 |
| 7 #import("elements/elements.dart"); | 7 #import("elements/elements.dart"); |
| 8 #import("leg.dart"); | 8 #import("leg.dart"); |
| 9 #import("scanner/scannerlib.dart"); | 9 #import("scanner/scannerlib.dart"); |
| 10 #import("tree/tree.dart"); | 10 #import("tree/tree.dart"); |
| (...skipping 465 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 476 Element element = elements[definition]; | 476 Element element = elements[definition]; |
| 477 if (capturedVariableMapping.containsKey(element)) { | 477 if (capturedVariableMapping.containsKey(element)) { |
| 478 result.add(element); | 478 result.add(element); |
| 479 }; | 479 }; |
| 480 } | 480 } |
| 481 scopeData.boxedLoopVariables = result; | 481 scopeData.boxedLoopVariables = result; |
| 482 } | 482 } |
| 483 | 483 |
| 484 /** Returns a non-unique name for the given closure element. */ | 484 /** Returns a non-unique name for the given closure element. */ |
| 485 String closureName(Element element) { | 485 String closureName(Element element) { |
| 486 List<String> parts = <String>[]; | 486 String safeElementName(Element element) { |
| 487 SourceString name = element.name; | |
| 488 if (name == null) return ""; | |
| 489 String value = name.stringValue; | |
|
floitsch
2012/10/17 12:13:47
I would just ask if the element is an operator, an
ahe
2012/11/12 13:22:09
I'm using more telling names now.
| |
| 490 // A valid Dart identifier cannot start with a digit. | |
| 491 // Fortunately, operators are always enclosed in a class which | |
| 492 // means that the resulting closure name will not start with a | |
| 493 // digit. | |
| 494 if (value === '==') { | |
| 495 return '0'; | |
| 496 } else if (value === '~') { | |
| 497 return '1'; | |
| 498 } else if (value === '[]') { | |
| 499 return '2'; | |
| 500 } else if (value === '[]=') { | |
| 501 return '3'; | |
| 502 } else if (value === '*') { | |
| 503 return '4'; | |
| 504 } else if (value === '/') { | |
| 505 return '5'; | |
| 506 } else if (value === '%') { | |
| 507 return '6'; | |
| 508 } else if (value === '~/') { | |
| 509 return '7'; | |
| 510 } else if (value === '+') { | |
| 511 return '8'; | |
| 512 } else if (value === '<<') { | |
| 513 return '9'; | |
| 514 } else if (value === '>>>') { | |
| 515 return '10'; | |
| 516 } else if (value === '>>') { | |
| 517 return '11'; | |
| 518 } else if (value === '>=') { | |
| 519 return '12'; | |
| 520 } else if (value === '>') { | |
| 521 return '13'; | |
| 522 } else if (value === '<=') { | |
| 523 return '14'; | |
| 524 } else if (value === '<') { | |
| 525 return '15'; | |
| 526 } else if (value === '&') { | |
| 527 return '16'; | |
| 528 } else if (value === '^') { | |
| 529 return '17'; | |
| 530 } else if (value === '|') { | |
| 531 return '18'; | |
| 532 } else if (value === '-') { | |
| 533 return '19'; | |
| 534 } else if (value === 'unary -') { | |
| 535 return '20'; | |
| 536 } else { | |
| 537 return name.slowToString(); | |
| 538 } | |
| 539 } | |
| 540 | |
| 541 Link<String> parts = const Link<String>(); | |
| 487 SourceString ownName = element.name; | 542 SourceString ownName = element.name; |
| 488 if (ownName == null || ownName.stringValue == "") { | 543 if (ownName == null || ownName.stringValue == "") { |
| 489 parts.add("anon"); | 544 parts = parts.prepend("anon"); |
| 490 } else { | 545 } else { |
| 491 parts.add(ownName.slowToString()); | 546 parts = parts.prepend(ownName.slowToString()); |
| 492 } | 547 } |
| 493 for (Element enclosingElement = element.enclosingElement; | 548 for (Element enclosingElement = element.enclosingElement; |
| 494 enclosingElement != null && | 549 enclosingElement != null && |
| 495 (enclosingElement.kind === ElementKind.GENERATIVE_CONSTRUCTOR_BODY | 550 (enclosingElement.kind === ElementKind.GENERATIVE_CONSTRUCTOR_BODY |
| 496 || enclosingElement.kind === ElementKind.CLASS | 551 || enclosingElement.kind === ElementKind.CLASS |
| 497 || enclosingElement.kind === ElementKind.FUNCTION | 552 || enclosingElement.kind === ElementKind.FUNCTION |
| 498 || enclosingElement.kind === ElementKind.GETTER | 553 || enclosingElement.kind === ElementKind.GETTER |
| 499 || enclosingElement.kind === ElementKind.SETTER); | 554 || enclosingElement.kind === ElementKind.SETTER); |
| 500 enclosingElement = enclosingElement.enclosingElement) { | 555 enclosingElement = enclosingElement.enclosingElement) { |
| 501 SourceString surroundingName = enclosingElement.name; | 556 String surroundingName = safeElementName(enclosingElement); |
| 502 if (surroundingName != null) { | 557 if (surroundingName != "") parts = parts.prepend(surroundingName); |
|
floitsch
2012/10/17 12:13:47
NYC: no need to do this test.
ahe
2012/11/12 13:22:09
Done.
| |
| 503 String surroundingNameString = surroundingName.slowToString(); | |
| 504 if (surroundingNameString != "") parts.add(surroundingNameString); | |
| 505 } | |
| 506 } | 558 } |
| 507 // Invert the parts. | 559 StringBuffer sb = new StringBuffer(); |
| 508 for (int i = 0, j = parts.length - 1; i < j; i++, j--) { | 560 parts.printOn(sb, '_'); |
| 509 var tmp = parts[i]; | 561 return sb.toString(); |
| 510 parts[i] = parts[j]; | |
| 511 parts[j] = tmp; | |
| 512 } | |
| 513 return Strings.join(parts, "_"); | |
| 514 } | 562 } |
| 515 | 563 |
| 516 ClosureClassMap globalizeClosure(FunctionExpression node, Element element) { | 564 ClosureClassMap globalizeClosure(FunctionExpression node, Element element) { |
| 517 SourceString closureName = new SourceString(closureName(element)); | 565 SourceString closureName = new SourceString(closureName(element)); |
| 518 ClassElement globalizedElement = new ClosureClassElement( | 566 ClassElement globalizedElement = new ClosureClassElement( |
| 519 closureName, compiler, element, element.getCompilationUnit()); | 567 closureName, compiler, element, element.getCompilationUnit()); |
| 520 FunctionElement callElement = | 568 FunctionElement callElement = |
| 521 new FunctionElement.from(Compiler.CALL_OPERATOR_NAME, | 569 new FunctionElement.from(Compiler.CALL_OPERATOR_NAME, |
| 522 element, | 570 element, |
| 523 globalizedElement); | 571 globalizedElement); |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 625 } | 673 } |
| 626 | 674 |
| 627 visitTryStatement(TryStatement node) { | 675 visitTryStatement(TryStatement node) { |
| 628 // TODO(ngeoffray): implement finer grain state. | 676 // TODO(ngeoffray): implement finer grain state. |
| 629 bool oldInTryStatement = inTryStatement; | 677 bool oldInTryStatement = inTryStatement; |
| 630 inTryStatement = true; | 678 inTryStatement = true; |
| 631 node.visitChildren(this); | 679 node.visitChildren(this); |
| 632 inTryStatement = oldInTryStatement; | 680 inTryStatement = oldInTryStatement; |
| 633 } | 681 } |
| 634 } | 682 } |
| OLD | NEW |