| 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 874 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 885 | 885 |
| 886 // This name is used as part of the name of a TypeConstant | 886 // This name is used as part of the name of a TypeConstant |
| 887 String uniqueNameForTypeConstantElement(Element element) { | 887 String uniqueNameForTypeConstantElement(Element element) { |
| 888 // TODO(sra): If we replace the period with an identifier character, | 888 // TODO(sra): If we replace the period with an identifier character, |
| 889 // TypeConstants will have better names in unminified code. | 889 // TypeConstants will have better names in unminified code. |
| 890 return "${globalObjectFor(element)}.${getNameX(element)}"; | 890 return "${globalObjectFor(element)}.${getNameX(element)}"; |
| 891 } | 891 } |
| 892 | 892 |
| 893 String globalObjectForConstant(ConstantValue constant) => 'C'; | 893 String globalObjectForConstant(ConstantValue constant) => 'C'; |
| 894 | 894 |
| 895 String operatorIsPrefix() => r'$is'; | 895 String get operatorIsPrefix => r'$is'; |
| 896 | 896 |
| 897 String operatorAsPrefix() => r'$as'; | 897 String get operatorAsPrefix => r'$as'; |
| 898 | 898 |
| 899 String operatorSignature() => r'$signature'; | 899 String get operatorSignature => r'$signature'; |
| 900 | 900 |
| 901 String typedefTag() => r'typedef'; | 901 String get typedefTag => r'typedef'; |
| 902 | 902 |
| 903 String functionTypeTag() => r'func'; | 903 String get functionTypeTag => r'func'; |
| 904 | 904 |
| 905 String functionTypeVoidReturnTag() => r'void'; | 905 String get functionTypeVoidReturnTag => r'void'; |
| 906 | 906 |
| 907 String functionTypeReturnTypeTag() => r'ret'; | 907 String get functionTypeReturnTypeTag => r'ret'; |
| 908 | 908 |
| 909 String functionTypeRequiredParametersTag() => r'args'; | 909 String get functionTypeRequiredParametersTag => r'args'; |
| 910 | 910 |
| 911 String functionTypeOptionalParametersTag() => r'opt'; | 911 String get functionTypeOptionalParametersTag => r'opt'; |
| 912 | 912 |
| 913 String functionTypeNamedParametersTag() => r'named'; | 913 String get functionTypeNamedParametersTag => r'named'; |
| 914 | 914 |
| 915 Map<FunctionType,String> functionTypeNameMap = | 915 Map<FunctionType,String> functionTypeNameMap = |
| 916 new Map<FunctionType,String>(); | 916 new Map<FunctionType,String>(); |
| 917 final FunctionTypeNamer functionTypeNamer; | 917 final FunctionTypeNamer functionTypeNamer; |
| 918 | 918 |
| 919 String getFunctionTypeName(FunctionType functionType) { | 919 String getFunctionTypeName(FunctionType functionType) { |
| 920 return functionTypeNameMap.putIfAbsent(functionType, () { | 920 return functionTypeNameMap.putIfAbsent(functionType, () { |
| 921 String proposedName = functionTypeNamer.computeName(functionType); | 921 String proposedName = functionTypeNamer.computeName(functionType); |
| 922 String freshName = getFreshName(proposedName, usedInstanceNames, | 922 String freshName = getFreshName(proposedName, usedInstanceNames, |
| 923 suggestedInstanceNames, ensureSafe: true); | 923 suggestedInstanceNames, ensureSafe: true); |
| 924 return freshName; | 924 return freshName; |
| 925 }); | 925 }); |
| 926 } | 926 } |
| 927 | 927 |
| 928 String operatorIsType(DartType type) { | 928 String operatorIsType(DartType type) { |
| 929 if (type.isFunctionType) { | 929 if (type.isFunctionType) { |
| 930 // TODO(erikcorry): Reduce from $isx to ix when we are minifying. | 930 // TODO(erikcorry): Reduce from $isx to ix when we are minifying. |
| 931 return '${operatorIsPrefix()}_${getFunctionTypeName(type)}'; | 931 return '${operatorIsPrefix}_${getFunctionTypeName(type)}'; |
| 932 } | 932 } |
| 933 return operatorIs(type.element); | 933 return operatorIs(type.element); |
| 934 } | 934 } |
| 935 | 935 |
| 936 String operatorIs(Element element) { | 936 String operatorIs(Element element) { |
| 937 // TODO(erikcorry): Reduce from $isx to ix when we are minifying. | 937 // TODO(erikcorry): Reduce from $isx to ix when we are minifying. |
| 938 return '${operatorIsPrefix()}${getRuntimeTypeName(element)}'; | 938 return '${operatorIsPrefix}${getRuntimeTypeName(element)}'; |
| 939 } | 939 } |
| 940 | 940 |
| 941 /* | 941 /* |
| 942 * Returns a name that does not clash with reserved JS keywords, | 942 * Returns a name that does not clash with reserved JS keywords, |
| 943 * and also ensures it won't clash with other identifiers. | 943 * and also ensures it won't clash with other identifiers. |
| 944 */ | 944 */ |
| 945 String _safeName(String name, Set<String> reserved) { | 945 String _safeName(String name, Set<String> reserved) { |
| 946 if (reserved.contains(name) || name.startsWith(r'$')) { | 946 if (reserved.contains(name) || name.startsWith(r'$')) { |
| 947 name = '\$$name'; | 947 name = '\$$name'; |
| 948 } | 948 } |
| 949 assert(!reserved.contains(name)); | 949 assert(!reserved.contains(name)); |
| 950 return name; | 950 return name; |
| 951 } | 951 } |
| 952 | 952 |
| 953 String substitutionName(Element element) { | 953 String substitutionName(Element element) { |
| 954 // TODO(ahe): Creating a string here is unfortunate. It is slow (due to | 954 // TODO(ahe): Creating a string here is unfortunate. It is slow (due to |
| 955 // string concatenation in the implementation), and may prevent | 955 // string concatenation in the implementation), and may prevent |
| 956 // segmentation of '$'. | 956 // segmentation of '$'. |
| 957 return '${operatorAsPrefix()}${getNameForRti(element)}'; | 957 return '${operatorAsPrefix}${getNameForRti(element)}'; |
| 958 } | 958 } |
| 959 | 959 |
| 960 String safeName(String name) => _safeName(name, jsReserved); | 960 String safeName(String name) => _safeName(name, jsReserved); |
| 961 String safeVariableName(String name) => _safeName(name, jsVariableReserved); | 961 String safeVariableName(String name) => _safeName(name, jsVariableReserved); |
| 962 | 962 |
| 963 String operatorNameToIdentifier(String name) { | 963 String operatorNameToIdentifier(String name) { |
| 964 if (name == null) return null; | 964 if (name == null) return null; |
| 965 if (name == '==') { | 965 if (name == '==') { |
| 966 return r'$eq'; | 966 return r'$eq'; |
| 967 } else if (name == '~') { | 967 } else if (name == '~') { |
| (...skipping 429 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1397 if (!first) { | 1397 if (!first) { |
| 1398 sb.write('_'); | 1398 sb.write('_'); |
| 1399 } | 1399 } |
| 1400 sb.write('_'); | 1400 sb.write('_'); |
| 1401 visit(parameter); | 1401 visit(parameter); |
| 1402 first = true; | 1402 first = true; |
| 1403 } | 1403 } |
| 1404 } | 1404 } |
| 1405 } | 1405 } |
| 1406 } | 1406 } |
| OLD | NEW |