OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 part of js_backend; | 5 part of js_backend; |
6 | 6 |
7 /** | 7 /** |
8 * Assigns JavaScript identifiers to Dart variables, class-names and members. | 8 * Assigns JavaScript identifiers to Dart variables, class-names and members. |
9 */ | 9 */ |
10 class Namer implements ClosureNamer { | 10 class Namer implements ClosureNamer { |
(...skipping 691 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
702 } | 702 } |
703 | 703 |
704 String isolateLazyInitializerAccess(Element element) { | 704 String isolateLazyInitializerAccess(Element element) { |
705 return "$CURRENT_ISOLATE.${getLazyInitializerName(element)}"; | 705 return "$CURRENT_ISOLATE.${getLazyInitializerName(element)}"; |
706 } | 706 } |
707 | 707 |
708 String operatorIsPrefix() => r'$is'; | 708 String operatorIsPrefix() => r'$is'; |
709 | 709 |
710 String operatorAsPrefix() => r'$as'; | 710 String operatorAsPrefix() => r'$as'; |
711 | 711 |
| 712 String operatorSignature() => r'$signature'; |
| 713 |
| 714 Map<FunctionType,String> functionTypeNameMap = new Map<FunctionType,String>(); |
| 715 FunctionTypeNamer functionTypeNamer = new FunctionTypeNamer(); |
| 716 |
| 717 String getFunctionTypeName(FunctionType functionType) { |
| 718 return functionTypeNameMap.putIfAbsent(functionType, () { |
| 719 String proposedName = functionTypeNamer.computeName(functionType); |
| 720 String freshName = getFreshName(proposedName, usedInstanceNames, |
| 721 suggestedInstanceNames, ensureSafe: true); |
| 722 return freshName; |
| 723 }); |
| 724 } |
| 725 |
| 726 String operatorIsFunctionType(FunctionType type) { |
| 727 // TODO(erikcorry): Reduce from $isx to ix when we are minifying. |
| 728 return '${operatorIsPrefix()}${getFunctionTypeName(type)}'; |
| 729 } |
| 730 |
712 String operatorIs(Element element) { | 731 String operatorIs(Element element) { |
713 // TODO(erikcorry): Reduce from $isx to ix when we are minifying. | 732 // TODO(erikcorry): Reduce from $isx to ix when we are minifying. |
714 return '${operatorIsPrefix()}${getName(element)}'; | 733 return '${operatorIsPrefix()}${getName(element)}'; |
715 } | 734 } |
716 | 735 |
717 /* | 736 /* |
718 * Returns a name that does not clash with reserved JS keywords, | 737 * Returns a name that does not clash with reserved JS keywords, |
719 * and also ensures it won't clash with other identifiers. | 738 * and also ensures it won't clash with other identifiers. |
720 */ | 739 */ |
721 String _safeName(String name, Set<String> reserved) { | 740 String _safeName(String name, Set<String> reserved) { |
722 if (reserved.contains(name) || name.startsWith(r'$')) { | 741 if (reserved.contains(name) || name.startsWith(r'$')) { |
723 name = '\$$name'; | 742 name = '\$$name'; |
724 } | 743 } |
725 assert(!reserved.contains(name)); | 744 assert(!reserved.contains(name)); |
726 return name; | 745 return name; |
727 } | 746 } |
728 | 747 |
729 String substitutionName(Element element) { | 748 String substitutionName(Element element) { |
730 return '${operatorAsPrefix()}${getName(element)}'; | 749 return '${operatorAsPrefix()}${getName(element)}'; |
731 } | 750 } |
732 | 751 |
| 752 String signatureName(FunctionType type) { |
| 753 ClassElement classElement = Types.getClassContext(type); |
| 754 String signature = '${operatorSignature()}_${getFunctionTypeName(type)}'; |
| 755 if (classElement != null) { |
| 756 return '${isolateAccess(classElement)}.$signature'; |
| 757 } else { |
| 758 return '${CURRENT_ISOLATE}.$signature'; |
| 759 } |
| 760 } |
| 761 |
733 String safeName(String name) => _safeName(name, jsReserved); | 762 String safeName(String name) => _safeName(name, jsReserved); |
734 String safeVariableName(String name) => _safeName(name, jsVariableReserved); | 763 String safeVariableName(String name) => _safeName(name, jsVariableReserved); |
735 | 764 |
736 SourceString operatorNameToIdentifier(SourceString name) { | 765 SourceString operatorNameToIdentifier(SourceString name) { |
737 if (name == null) return null; | 766 if (name == null) return null; |
738 String value = name.stringValue; | 767 String value = name.stringValue; |
739 if (value == null) { | 768 if (value == null) { |
740 return name; | 769 return name; |
741 } else if (value == '==') { | 770 } else if (value == '==') { |
742 return const SourceString(r'$eq'); | 771 return const SourceString(r'$eq'); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
776 return const SourceString(r'$or'); | 805 return const SourceString(r'$or'); |
777 } else if (value == '-') { | 806 } else if (value == '-') { |
778 return const SourceString(r'$sub'); | 807 return const SourceString(r'$sub'); |
779 } else if (value == 'unary-') { | 808 } else if (value == 'unary-') { |
780 return const SourceString(r'$negate'); | 809 return const SourceString(r'$negate'); |
781 } else { | 810 } else { |
782 return name; | 811 return name; |
783 } | 812 } |
784 } | 813 } |
785 } | 814 } |
| 815 |
| 816 class FunctionTypeNamer extends DartTypeVisitor { |
| 817 StringBuffer sb; |
| 818 |
| 819 String computeName(DartType type) { |
| 820 sb = new StringBuffer(); |
| 821 visit(type); |
| 822 return sb.toString(); |
| 823 } |
| 824 |
| 825 visit(DartType type) { |
| 826 type.accept(this, null); |
| 827 } |
| 828 |
| 829 visitType(DartType type, _) { |
| 830 sb.write(type.name.slowToString()); |
| 831 } |
| 832 |
| 833 visitFunctionType(FunctionType type, _) { |
| 834 visit(type.returnType); |
| 835 sb.write('_'); |
| 836 for (Link<DartType> link = type.parameterTypes; |
| 837 !link.isEmpty; |
| 838 link = link.tail) { |
| 839 sb.write('_'); |
| 840 visit(link.head); |
| 841 } |
| 842 for (Link<DartType> link = type.optionalParameterTypes; |
| 843 !link.isEmpty; |
| 844 link = link.tail) { |
| 845 sb.write('_'); |
| 846 visit(link.head); |
| 847 } |
| 848 if (!type.namedParameterTypes.isEmpty) { |
| 849 for (Link<DartType> link = type.namedParameterTypes; |
| 850 !link.isEmpty; |
| 851 link = link.tail) { |
| 852 sb.write('_'); |
| 853 visit(link.head); |
| 854 } |
| 855 } |
| 856 } |
| 857 |
| 858 } |
OLD | NEW |