| 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 422 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 433 } | 433 } |
| 434 } | 434 } |
| 435 } | 435 } |
| 436 | 436 |
| 437 /** | 437 /** |
| 438 * Returns the internal name used for an invocation mirror of this selector. | 438 * Returns the internal name used for an invocation mirror of this selector. |
| 439 */ | 439 */ |
| 440 String invocationMirrorInternalName(Selector selector) | 440 String invocationMirrorInternalName(Selector selector) |
| 441 => invocationName(selector); | 441 => invocationName(selector); |
| 442 | 442 |
| 443 String instanceFieldName(Element element) { | 443 /** |
| 444 * Returns name of accessor (root to getter and setter) for a static or |
| 445 * instance field. |
| 446 */ |
| 447 String fieldAccessorName(Element element) { |
| 448 return element.isInstanceMember() |
| 449 ? instanceFieldAccessorName(element) |
| 450 : getNameOfField(element); |
| 451 } |
| 452 |
| 453 /** |
| 454 * Returns name of the JavaScript property used to store a static or instance |
| 455 * field. |
| 456 */ |
| 457 String fieldPropertyName(Element element) { |
| 458 return element.isInstanceMember() |
| 459 ? instanceFieldPropertyName(element) |
| 460 : getNameOfField(element); |
| 461 } |
| 462 |
| 463 /** |
| 464 * Returns name of accessor (root to getter and setter) for an instance field. |
| 465 */ |
| 466 String instanceFieldAccessorName(Element element) { |
| 444 String proposedName = privateName(element.getLibrary(), element.name); | 467 String proposedName = privateName(element.getLibrary(), element.name); |
| 445 return getMappedInstanceName(proposedName); | 468 return getMappedInstanceName(proposedName); |
| 446 } | 469 } |
| 447 | 470 |
| 448 // Construct a new name for the element based on the library and class it is | 471 /** |
| 449 // in. The name here is not important, we just need to make sure it is | 472 * Returns name of the JavaScript property used to store an instance field. |
| 450 // unique. If we are minifying, we actually construct the name from the | 473 */ |
| 451 // minified versions of the class and instance names, but the result is | 474 String instanceFieldPropertyName(Element element) { |
| 452 // minified once again, so that is not visible in the end result. | 475 if (element.hasFixedBackendName()) { |
| 453 String shadowedFieldName(Element fieldElement) { | 476 return element.fixedBackendName(); |
| 454 // Check for following situation: Native field ${fieldElement.name} has | 477 } |
| 455 // fixed JSName ${fieldElement.nativeName()}, but a subclass shadows this | 478 // If a class is used anywhere as a mixin, we must make the name unique so |
| 456 // name. We normally handle that by renaming the superclass field, but we | 479 // that it does not accidentally shadow. Also, the mixin name must be |
| 457 // can't do that because native fields have fixed JavaScript names. | 480 // constant over all mixins. |
| 458 // In practice this can't happen because we can't inherit from native | 481 if (compiler.world.isUsedAsMixin(element.getEnclosingClass()) || |
| 459 // classes. | 482 shadowingAnotherField(element)) { |
| 460 assert (!fieldElement.hasFixedBackendName()); | 483 // Construct a new name for the element based on the library and class it |
| 484 // is in. The name here is not important, we just need to make sure it is |
| 485 // unique. If we are minifying, we actually construct the name from the |
| 486 // minified version of the class name, but the result is minified once |
| 487 // again, so that is not visible in the end result. |
| 488 String libraryName = getNameOfLibrary(element.getLibrary()); |
| 489 String className = getNameOfClass(element.getEnclosingClass()); |
| 490 String instanceName = privateName(element.getLibrary(), element.name); |
| 491 return getMappedInstanceName('$libraryName\$$className\$$instanceName'); |
| 492 } |
| 461 | 493 |
| 462 String libraryName = getNameOfLibrary(fieldElement.getLibrary()); | 494 String proposedName = privateName(element.getLibrary(), element.name); |
| 463 String className = getNameOfClass(fieldElement.getEnclosingClass()); | 495 return getMappedInstanceName(proposedName); |
| 464 String instanceName = instanceFieldName(fieldElement); | 496 } |
| 465 return getMappedInstanceName('$libraryName\$$className\$$instanceName'); | 497 |
| 498 |
| 499 bool shadowingAnotherField(Element element) { |
| 500 return element.getEnclosingClass().hasFieldShadowedBy(element); |
| 466 } | 501 } |
| 467 | 502 |
| 468 String setterName(Element element) { | 503 String setterName(Element element) { |
| 469 // We dynamically create setters from the field-name. The setter name must | 504 // We dynamically create setters from the field-name. The setter name must |
| 470 // therefore be derived from the instance field-name. | 505 // therefore be derived from the instance field-name. |
| 471 LibraryElement library = element.getLibrary(); | 506 LibraryElement library = element.getLibrary(); |
| 472 String name = getMappedInstanceName(privateName(library, element.name)); | 507 String name = getMappedInstanceName(privateName(library, element.name)); |
| 473 return '$setterPrefix$name'; | 508 return '$setterPrefix$name'; |
| 474 } | 509 } |
| 475 | 510 |
| (...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 711 String getNameX(Element element) { | 746 String getNameX(Element element) { |
| 712 if (element.isInstanceMember()) { | 747 if (element.isInstanceMember()) { |
| 713 if (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR_BODY | 748 if (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR_BODY |
| 714 || element.kind == ElementKind.FUNCTION) { | 749 || element.kind == ElementKind.FUNCTION) { |
| 715 return instanceMethodName(element); | 750 return instanceMethodName(element); |
| 716 } else if (element.kind == ElementKind.GETTER) { | 751 } else if (element.kind == ElementKind.GETTER) { |
| 717 return getterName(element); | 752 return getterName(element); |
| 718 } else if (element.kind == ElementKind.SETTER) { | 753 } else if (element.kind == ElementKind.SETTER) { |
| 719 return setterName(element); | 754 return setterName(element); |
| 720 } else if (element.kind == ElementKind.FIELD) { | 755 } else if (element.kind == ElementKind.FIELD) { |
| 721 return instanceFieldName(element); | 756 compiler.internalError( |
| 757 'use instanceFieldPropertyName or instanceFieldAccessorName', |
| 758 node: element.parseNode(compiler)); |
| 722 } else { | 759 } else { |
| 723 compiler.internalError('getName for bad kind: ${element.kind}', | 760 compiler.internalError('getName for bad kind: ${element.kind}', |
| 724 node: element.parseNode(compiler)); | 761 node: element.parseNode(compiler)); |
| 725 } | 762 } |
| 726 } else { | 763 } else { |
| 727 // Use declaration element to ensure invariant on [globals]. | 764 // Use declaration element to ensure invariant on [globals]. |
| 728 element = element.declaration; | 765 element = element.declaration; |
| 729 // Dealing with a top-level or static element. | 766 // Dealing with a top-level or static element. |
| 730 String cached = globals[element]; | 767 String cached = globals[element]; |
| 731 if (cached != null) return cached; | 768 if (cached != null) return cached; |
| (...skipping 606 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1338 if (!first) { | 1375 if (!first) { |
| 1339 sb.write('_'); | 1376 sb.write('_'); |
| 1340 } | 1377 } |
| 1341 sb.write('_'); | 1378 sb.write('_'); |
| 1342 visit(link.head); | 1379 visit(link.head); |
| 1343 first = true; | 1380 first = true; |
| 1344 } | 1381 } |
| 1345 } | 1382 } |
| 1346 } | 1383 } |
| 1347 } | 1384 } |
| OLD | NEW |