| 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 * Names are generated through three stages: | 10 * Names are generated through three stages: |
| (...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 380 /// [_disambiguateMember], [_disambiguateInternalMember], | 380 /// [_disambiguateMember], [_disambiguateInternalMember], |
| 381 /// [_disambiguateOperator], and [reservePublicMemberName]. | 381 /// [_disambiguateOperator], and [reservePublicMemberName]. |
| 382 final Set<String> usedInstanceNames = new Set<String>(); | 382 final Set<String> usedInstanceNames = new Set<String>(); |
| 383 final Map<String, jsAst.Name> userInstanceMembers = | 383 final Map<String, jsAst.Name> userInstanceMembers = |
| 384 new HashMap<String, jsAst.Name>(); | 384 new HashMap<String, jsAst.Name>(); |
| 385 final Map<Element, jsAst.Name> internalInstanceMembers = | 385 final Map<Element, jsAst.Name> internalInstanceMembers = |
| 386 new HashMap<Element, jsAst.Name>(); | 386 new HashMap<Element, jsAst.Name>(); |
| 387 final Map<String, jsAst.Name> userInstanceOperators = | 387 final Map<String, jsAst.Name> userInstanceOperators = |
| 388 new HashMap<String, jsAst.Name>(); | 388 new HashMap<String, jsAst.Name>(); |
| 389 | 389 |
| 390 /// Used to disambiguate names for constants in [constantName]. |
| 391 final Set<String> usedConstantNames = new Set<String>(); |
| 392 |
| 390 Set<String> getUsedNames(NamingScope scope) { | 393 Set<String> getUsedNames(NamingScope scope) { |
| 391 if (scope == NamingScope.global) { | 394 if (scope == NamingScope.global) { |
| 392 return usedGlobalNames; | 395 return usedGlobalNames; |
| 396 } else if (scope == NamingScope.instance){ |
| 397 return usedInstanceNames; |
| 393 } else { | 398 } else { |
| 394 assert(scope == NamingScope.instance); | 399 assert(scope == NamingScope.constant); |
| 395 return usedInstanceNames; | 400 return usedConstantNames; |
| 396 } | 401 } |
| 397 } | 402 } |
| 398 | 403 |
| 399 final Map<String, int> popularNameCounters = <String, int>{}; | 404 final Map<String, int> popularNameCounters = <String, int>{}; |
| 400 | 405 |
| 401 final Map<LibraryElement, String> libraryLongNames = | 406 final Map<LibraryElement, String> libraryLongNames = |
| 402 new HashMap<LibraryElement, String>(); | 407 new HashMap<LibraryElement, String>(); |
| 403 | 408 |
| 404 final Map<ConstantValue, jsAst.Name> constantNames = | 409 final Map<ConstantValue, jsAst.Name> constantNames = |
| 405 new HashMap<ConstantValue, jsAst.Name>(); | 410 new HashMap<ConstantValue, jsAst.Name>(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 418 /// names be given to the first item with the given proposed name. | 423 /// names be given to the first item with the given proposed name. |
| 419 /// | 424 /// |
| 420 /// This is currently used in [MinifyNamer] to assign very short minified | 425 /// This is currently used in [MinifyNamer] to assign very short minified |
| 421 /// names to things that tend to be used very often. | 426 /// names to things that tend to be used very often. |
| 422 final Map<String, String> suggestedGlobalNames = <String, String>{}; | 427 final Map<String, String> suggestedGlobalNames = <String, String>{}; |
| 423 final Map<String, String> suggestedInstanceNames = <String, String>{}; | 428 final Map<String, String> suggestedInstanceNames = <String, String>{}; |
| 424 | 429 |
| 425 Map<String, String> getSuggestedNames(NamingScope scope) { | 430 Map<String, String> getSuggestedNames(NamingScope scope) { |
| 426 if (scope == NamingScope.global) { | 431 if (scope == NamingScope.global) { |
| 427 return suggestedGlobalNames; | 432 return suggestedGlobalNames; |
| 433 } else if (scope == NamingScope.instance) { |
| 434 return suggestedInstanceNames; |
| 428 } else { | 435 } else { |
| 429 assert(scope == NamingScope.instance); | 436 assert(scope == NamingScope.constant); |
| 430 return suggestedInstanceNames; | 437 return const {}; |
| 431 } | 438 } |
| 432 } | 439 } |
| 433 | 440 |
| 434 | 441 |
| 435 /// Used to store unique keys for library names. Keys are not used as names, | 442 /// Used to store unique keys for library names. Keys are not used as names, |
| 436 /// nor are they visible in the output. The only serve as an internal | 443 /// nor are they visible in the output. The only serve as an internal |
| 437 /// key into maps. | 444 /// key into maps. |
| 438 final Map<LibraryElement, String> _libraryKeys = | 445 final Map<LibraryElement, String> _libraryKeys = |
| 439 new HashMap<LibraryElement, String>(); | 446 new HashMap<LibraryElement, String>(); |
| 440 | 447 |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 517 /// | 524 /// |
| 518 /// Unique within the global-member namespace. | 525 /// Unique within the global-member namespace. |
| 519 jsAst.Name constantName(ConstantValue constant) { | 526 jsAst.Name constantName(ConstantValue constant) { |
| 520 // In the current implementation it doesn't make sense to give names to | 527 // In the current implementation it doesn't make sense to give names to |
| 521 // function constants since the function-implementation itself serves as | 528 // function constants since the function-implementation itself serves as |
| 522 // constant and can be accessed directly. | 529 // constant and can be accessed directly. |
| 523 assert(!constant.isFunction); | 530 assert(!constant.isFunction); |
| 524 jsAst.Name result = constantNames[constant]; | 531 jsAst.Name result = constantNames[constant]; |
| 525 if (result == null) { | 532 if (result == null) { |
| 526 String longName = constantLongName(constant); | 533 String longName = constantLongName(constant); |
| 527 result = getFreshName(NamingScope.global, longName); | 534 result = getFreshName(NamingScope.constant, longName); |
| 528 constantNames[constant] = result; | 535 constantNames[constant] = result; |
| 529 } | 536 } |
| 530 return result; | 537 return result; |
| 531 } | 538 } |
| 532 | 539 |
| 533 /// Proposed name for [constant]. | 540 /// Proposed name for [constant]. |
| 534 String constantLongName(ConstantValue constant) { | 541 String constantLongName(ConstantValue constant) { |
| 535 String longName = constantLongNames[constant]; | 542 String longName = constantLongNames[constant]; |
| 536 if (longName == null) { | 543 if (longName == null) { |
| 537 longName = new ConstantNamingVisitor(compiler, constantHasher) | 544 longName = new ConstantNamingVisitor(compiler, constantHasher) |
| (...skipping 1431 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1969 sb.write('_'); | 1976 sb.write('_'); |
| 1970 visit(parameter); | 1977 visit(parameter); |
| 1971 first = true; | 1978 first = true; |
| 1972 } | 1979 } |
| 1973 } | 1980 } |
| 1974 } | 1981 } |
| 1975 } | 1982 } |
| 1976 | 1983 |
| 1977 enum NamingScope { | 1984 enum NamingScope { |
| 1978 global, | 1985 global, |
| 1979 instance | 1986 instance, |
| 1987 constant |
| 1980 } | 1988 } |
| OLD | NEW |