| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 elements; | 5 part of elements; |
| 6 | 6 |
| 7 /// A [Name] represents the abstraction of a Dart identifier which takes privacy | 7 /// A [Name] represents the abstraction of a Dart identifier which takes privacy |
| 8 /// and setter into account. | 8 /// and setter into account. |
| 9 // TODO(johnniwinther): Try to share logic with [Selector]. | 9 // TODO(johnniwinther): Try to share logic with [Selector]. |
| 10 abstract class Name { | 10 abstract class Name { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 /// is returned. | 30 /// is returned. |
| 31 Name get getter; | 31 Name get getter; |
| 32 | 32 |
| 33 /// Returns the seeter name corresponding to this name. If this name is a | 33 /// Returns the seeter name corresponding to this name. If this name is a |
| 34 /// getter name 'v' then the name 'v=' is returned, otherwsie the name itself | 34 /// getter name 'v' then the name 'v=' is returned, otherwsie the name itself |
| 35 /// is returned. | 35 /// is returned. |
| 36 Name get setter; | 36 Name get setter; |
| 37 | 37 |
| 38 /// Returns `true` if an entity of this name is accessible from library | 38 /// Returns `true` if an entity of this name is accessible from library |
| 39 /// [element]. | 39 /// [element]. |
| 40 bool isAccessibleFrom(LibraryElement element); | 40 bool isAccessibleFrom(LibraryEntity element); |
| 41 | 41 |
| 42 /// Returns `true` if this name is private. | 42 /// Returns `true` if this name is private. |
| 43 bool get isPrivate; | 43 bool get isPrivate; |
| 44 | 44 |
| 45 /// Returns `true` if this name is the same as [other] not taking the library | 45 /// Returns `true` if this name is the same as [other] not taking the library |
| 46 /// privacy into account. | 46 /// privacy into account. |
| 47 bool isSimilarTo(Name other); | 47 bool isSimilarTo(Name other); |
| 48 int get similarHashCode; | 48 int get similarHashCode; |
| 49 | 49 |
| 50 LibraryElement get library; | 50 LibraryEntity get library; |
| 51 | 51 |
| 52 /// Returns `true` when [s] is private if used as an identifier. | 52 /// Returns `true` when [s] is private if used as an identifier. |
| 53 static bool isPrivateName(String s) => !s.isEmpty && s.codeUnitAt(0) == $_; | 53 static bool isPrivateName(String s) => !s.isEmpty && s.codeUnitAt(0) == $_; |
| 54 | 54 |
| 55 /// Returns `true` when [s] is public if used as an identifier. | 55 /// Returns `true` when [s] is public if used as an identifier. |
| 56 static bool isPublicName(String s) => !isPrivateName(s); | 56 static bool isPublicName(String s) => !isPrivateName(s); |
| 57 } | 57 } |
| 58 | 58 |
| 59 class PublicName implements Name { | 59 class PublicName implements Name { |
| 60 final String text; | 60 final String text; |
| 61 final bool isSetter; | 61 final bool isSetter; |
| 62 | 62 |
| 63 const PublicName(this.text, {this.isSetter: false}); | 63 const PublicName(this.text, {this.isSetter: false}); |
| 64 | 64 |
| 65 Name get getter => isSetter ? new PublicName(text) : this; | 65 Name get getter => isSetter ? new PublicName(text) : this; |
| 66 | 66 |
| 67 Name get setter => isSetter ? this : new PublicName(text, isSetter: true); | 67 Name get setter => isSetter ? this : new PublicName(text, isSetter: true); |
| 68 | 68 |
| 69 bool isAccessibleFrom(LibraryElement element) => true; | 69 bool isAccessibleFrom(LibraryEntity element) => true; |
| 70 | 70 |
| 71 bool get isPrivate => false; | 71 bool get isPrivate => false; |
| 72 | 72 |
| 73 int get hashCode => similarHashCode; | 73 int get hashCode => similarHashCode; |
| 74 | 74 |
| 75 bool operator ==(other) { | 75 bool operator ==(other) { |
| 76 if (other is! PublicName) return false; | 76 if (other is! PublicName) return false; |
| 77 return isSimilarTo(other); | 77 return isSimilarTo(other); |
| 78 } | 78 } |
| 79 | 79 |
| 80 bool isSimilarTo(Name other) => | 80 bool isSimilarTo(Name other) => |
| 81 text == other.text && isSetter == other.isSetter; | 81 text == other.text && isSetter == other.isSetter; |
| 82 int get similarHashCode => text.hashCode + 11 * isSetter.hashCode; | 82 int get similarHashCode => text.hashCode + 11 * isSetter.hashCode; |
| 83 | 83 |
| 84 LibraryElement get library => null; | 84 LibraryEntity get library => null; |
| 85 | 85 |
| 86 String toString() => isSetter ? '$text=' : text; | 86 String toString() => isSetter ? '$text=' : text; |
| 87 } | 87 } |
| 88 | 88 |
| 89 class PrivateName extends PublicName { | 89 class PrivateName extends PublicName { |
| 90 final LibraryElement library; | 90 final LibraryEntity library; |
| 91 | 91 |
| 92 PrivateName(String text, this.library, {bool isSetter: false}) | 92 PrivateName(String text, this.library, {bool isSetter: false}) |
| 93 : super(text, isSetter: isSetter); | 93 : super(text, isSetter: isSetter); |
| 94 | 94 |
| 95 Name get getter => isSetter ? new PrivateName(text, library) : this; | 95 Name get getter => isSetter ? new PrivateName(text, library) : this; |
| 96 | 96 |
| 97 Name get setter { | 97 Name get setter { |
| 98 return isSetter ? this : new PrivateName(text, library, isSetter: true); | 98 return isSetter ? this : new PrivateName(text, library, isSetter: true); |
| 99 } | 99 } |
| 100 | 100 |
| 101 bool isAccessibleFrom(LibraryElement element) => library == element; | 101 bool isAccessibleFrom(LibraryElement element) => library == element; |
| 102 | 102 |
| 103 bool get isPrivate => true; | 103 bool get isPrivate => true; |
| 104 | 104 |
| 105 int get hashCode => super.hashCode + 13 * library.hashCode; | 105 int get hashCode => super.hashCode + 13 * library.hashCode; |
| 106 | 106 |
| 107 bool operator ==(other) { | 107 bool operator ==(other) { |
| 108 if (other is! PrivateName) return false; | 108 if (other is! PrivateName) return false; |
| 109 return super == (other) && library == other.library; | 109 return super == (other) && library == other.library; |
| 110 } | 110 } |
| 111 | 111 |
| 112 String toString() => '${library.libraryName}#${super.toString()}'; | 112 String toString() => '${library.libraryName}#${super.toString()}'; |
| 113 } | 113 } |
| OLD | NEW |