| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 library kernel.canonical_name; | 4 library kernel.canonical_name; |
| 5 | 5 |
| 6 import 'ast.dart'; | 6 import 'ast.dart'; |
| 7 | 7 |
| 8 /// A string sequence that identifies a library, class, or member. | 8 /// A string sequence that identifies a library, class, or member. |
| 9 /// | 9 /// |
| 10 /// Canonical names are organized in a prefix tree. Each node knows its | 10 /// Canonical names are organized in a prefix tree. Each node knows its |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 /// "@factories" | 50 /// "@factories" |
| 51 /// Qualified name | 51 /// Qualified name |
| 52 /// | 52 /// |
| 53 /// Qualified name: | 53 /// Qualified name: |
| 54 /// if private: URI of library | 54 /// if private: URI of library |
| 55 /// Name text | 55 /// Name text |
| 56 /// | 56 /// |
| 57 /// The "qualified name" allows a member to have a name that is private to | 57 /// The "qualified name" allows a member to have a name that is private to |
| 58 /// a library other than the one containing that member. | 58 /// a library other than the one containing that member. |
| 59 class CanonicalName { | 59 class CanonicalName { |
| 60 final CanonicalName parent; | 60 CanonicalName _parent; |
| 61 final String name; | 61 final String name; |
| 62 | 62 |
| 63 Map<String, CanonicalName> _children; | 63 Map<String, CanonicalName> _children; |
| 64 | 64 |
| 65 /// The library, class, or member bound to this name. | 65 /// The library, class, or member bound to this name. |
| 66 Reference reference; | 66 Reference reference; |
| 67 | 67 |
| 68 /// Temporary index used during serialization. | 68 /// Temporary index used during serialization. |
| 69 int index = -1; | 69 int index = -1; |
| 70 | 70 |
| 71 CanonicalName._(this.parent, this.name) { | 71 CanonicalName._(this._parent, this.name) { |
| 72 assert(name != null); | 72 assert(name != null); |
| 73 } | 73 } |
| 74 | 74 |
| 75 CanonicalName.root() | 75 CanonicalName.root() : name = ''; |
| 76 : parent = null, | |
| 77 name = ''; | |
| 78 | 76 |
| 79 bool get isRoot => parent == null; | 77 bool get isRoot => parent == null; |
| 80 | 78 |
| 79 CanonicalName get parent => _parent; |
| 80 |
| 81 Iterable<CanonicalName> get children => | 81 Iterable<CanonicalName> get children => |
| 82 _children?.values ?? const <CanonicalName>[]; | 82 _children?.values ?? const <CanonicalName>[]; |
| 83 | 83 |
| 84 CanonicalName getChild(String name) { | 84 CanonicalName getChild(String name) { |
| 85 var map = _children ??= <String, CanonicalName>{}; | 85 var map = _children ??= <String, CanonicalName>{}; |
| 86 return map[name] ??= new CanonicalName._(this, name); | 86 return map[name] ??= new CanonicalName._(this, name); |
| 87 } | 87 } |
| 88 | 88 |
| 89 CanonicalName getChildFromUri(Uri uri) { | 89 CanonicalName getChildFromUri(Uri uri) { |
| 90 // Note that the Uri class caches its string representation, and all library | 90 // Note that the Uri class caches its string representation, and all library |
| (...skipping 14 matching lines...) Expand all Loading... |
| 105 } | 105 } |
| 106 | 106 |
| 107 CanonicalName getChildFromTypedef(Typedef typedef_) { | 107 CanonicalName getChildFromTypedef(Typedef typedef_) { |
| 108 return getChild('@typedefs').getChild(typedef_.name); | 108 return getChild('@typedefs').getChild(typedef_.name); |
| 109 } | 109 } |
| 110 | 110 |
| 111 void removeChild(String name) { | 111 void removeChild(String name) { |
| 112 _children?.remove(name); | 112 _children?.remove(name); |
| 113 } | 113 } |
| 114 | 114 |
| 115 /// Transplant the given [child] into this name. |
| 116 void transplantChild(CanonicalName child) { |
| 117 var map = _children ??= <String, CanonicalName>{}; |
| 118 map[child.name] = child; |
| 119 child._parent = this; |
| 120 } |
| 121 |
| 115 void bindTo(Reference target) { | 122 void bindTo(Reference target) { |
| 116 if (reference == target) return; | 123 if (reference == target) return; |
| 117 if (reference != null) { | 124 if (reference != null) { |
| 118 throw '$this is already bound'; | 125 throw '$this is already bound'; |
| 119 } | 126 } |
| 120 if (target.canonicalName != null) { | 127 if (target.canonicalName != null) { |
| 121 throw 'Cannot bind $this to ${target.node}, target is already bound to ' | 128 throw 'Cannot bind $this to ${target.node}, target is already bound to ' |
| 122 '${target.canonicalName}'; | 129 '${target.canonicalName}'; |
| 123 } | 130 } |
| 124 target.canonicalName = this; | 131 target.canonicalName = this; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 154 } | 161 } |
| 155 if (member is Field) { | 162 if (member is Field) { |
| 156 return '@fields'; | 163 return '@fields'; |
| 157 } | 164 } |
| 158 if (member is Constructor) { | 165 if (member is Constructor) { |
| 159 return '@constructors'; | 166 return '@constructors'; |
| 160 } | 167 } |
| 161 throw 'Unexpected member: $member'; | 168 throw 'Unexpected member: $member'; |
| 162 } | 169 } |
| 163 } | 170 } |
| OLD | NEW |