| 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 | 4 |
| 5 import 'package:analyzer/dart/ast/ast.dart'; | 5 import 'package:analyzer/dart/ast/ast.dart'; |
| 6 import 'package:analyzer/dart/ast/token.dart'; | 6 import 'package:analyzer/dart/ast/token.dart'; |
| 7 import 'package:analyzer/dart/ast/visitor.dart'; | 7 import 'package:analyzer/dart/ast/visitor.dart'; |
| 8 import 'package:analyzer/dart/element/element.dart'; | 8 import 'package:analyzer/dart/element/element.dart'; |
| 9 import 'package:analyzer/dart/element/type.dart'; | 9 import 'package:analyzer/dart/element/type.dart'; |
| 10 import 'package:analyzer/src/dart/element/member.dart'; | 10 import 'package:analyzer/src/dart/element/member.dart'; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 | 26 |
| 27 /** | 27 /** |
| 28 * Map associating [CompilationUnitElement]s with their identifiers, which | 28 * Map associating [CompilationUnitElement]s with their identifiers, which |
| 29 * are indices into [_unitLibraryUris] and [_unitUnitUris]. | 29 * are indices into [_unitLibraryUris] and [_unitUnitUris]. |
| 30 */ | 30 */ |
| 31 final Map<CompilationUnitElement, int> _unitMap = | 31 final Map<CompilationUnitElement, int> _unitMap = |
| 32 <CompilationUnitElement, int>{}; | 32 <CompilationUnitElement, int>{}; |
| 33 | 33 |
| 34 /** | 34 /** |
| 35 * Each item of this list corresponds to the library URI of a unique | 35 * Each item of this list corresponds to the library URI of a unique |
| 36 * [CompilationUnitElement]. It is an index into [_strings]. | 36 * [CompilationUnitElement]. |
| 37 */ | 37 */ |
| 38 final List<int> _unitLibraryUris = <int>[]; | 38 final List<_StringInfo> _unitLibraryUris = <_StringInfo>[]; |
| 39 | 39 |
| 40 /** | 40 /** |
| 41 * Each item of this list corresponds to the unit URI of a unique | 41 * Each item of this list corresponds to the unit URI of a unique |
| 42 * [CompilationUnitElement]. It is an index into [_strings]. | 42 * [CompilationUnitElement]. |
| 43 */ | 43 */ |
| 44 final List<int> _unitUnitUris = <int>[]; | 44 final List<_StringInfo> _unitUnitUris = <_StringInfo>[]; |
| 45 | 45 |
| 46 /** | 46 /** |
| 47 * Map associating strings with their identifiers, which are indices | 47 * Map associating strings with their [_StringInfo]s. |
| 48 * into [_strings]. | |
| 49 */ | 48 */ |
| 50 final Map<String, int> _stringMap = <String, int>{}; | 49 final Map<String, _StringInfo> _stringMap = <String, _StringInfo>{}; |
| 51 | |
| 52 /** | |
| 53 * List of unique strings used in this index. | |
| 54 */ | |
| 55 final List<String> _strings = <String>[]; | |
| 56 | 50 |
| 57 /** | 51 /** |
| 58 * List of information about each unit indexed in this index. | 52 * List of information about each unit indexed in this index. |
| 59 */ | 53 */ |
| 60 final List<_UnitIndexAssembler> _units = <_UnitIndexAssembler>[]; | 54 final List<_UnitIndexAssembler> _units = <_UnitIndexAssembler>[]; |
| 61 | 55 |
| 62 /** | 56 /** |
| 63 * Assemble a new [PackageIndexBuilder] using the information gathered by | 57 * Assemble a new [PackageIndexBuilder] using the information gathered by |
| 64 * [index]. | 58 * [index]. |
| 65 */ | 59 */ |
| 66 PackageIndexBuilder assemble() { | 60 PackageIndexBuilder assemble() { |
| 61 // sort strings end set IDs |
| 62 List<_StringInfo> stringInfoList = _stringMap.values.toList(); |
| 63 stringInfoList.sort((a, b) { |
| 64 return a.value.compareTo(b.value); |
| 65 }); |
| 66 for (int i = 0; i < stringInfoList.length; i++) { |
| 67 stringInfoList[i].id = i; |
| 68 } |
| 69 // sort elements and set IDs |
| 67 List<_ElementInfo> elementInfoList = _elementMap.values.toList(); | 70 List<_ElementInfo> elementInfoList = _elementMap.values.toList(); |
| 68 elementInfoList.sort((a, b) { | 71 elementInfoList.sort((a, b) { |
| 69 return a.offset - b.offset; | 72 return a.offset - b.offset; |
| 70 }); | 73 }); |
| 71 for (int i = 0; i < elementInfoList.length; i++) { | 74 for (int i = 0; i < elementInfoList.length; i++) { |
| 72 elementInfoList[i].id = i; | 75 elementInfoList[i].id = i; |
| 73 } | 76 } |
| 74 return new PackageIndexBuilder( | 77 return new PackageIndexBuilder( |
| 75 unitLibraryUris: _unitLibraryUris, | 78 unitLibraryUris: _unitLibraryUris.map((s) => s.id).toList(), |
| 76 unitUnitUris: _unitUnitUris, | 79 unitUnitUris: _unitUnitUris.map((s) => s.id).toList(), |
| 77 elementUnits: elementInfoList.map((e) => e.unitId).toList(), | 80 elementUnits: elementInfoList.map((e) => e.unitId).toList(), |
| 78 elementOffsets: elementInfoList.map((e) => e.offset).toList(), | 81 elementOffsets: elementInfoList.map((e) => e.offset).toList(), |
| 79 elementKinds: elementInfoList.map((e) => e.kind).toList(), | 82 elementKinds: elementInfoList.map((e) => e.kind).toList(), |
| 80 strings: _strings, | 83 strings: stringInfoList.map((s) => s.value).toList(), |
| 81 units: _units.map((unit) => unit.assemble()).toList()); | 84 units: _units.map((unit) => unit.assemble()).toList()); |
| 82 } | 85 } |
| 83 | 86 |
| 84 /** | 87 /** |
| 85 * Index the given fully resolved [unit]. | 88 * Index the given fully resolved [unit]. |
| 86 */ | 89 */ |
| 87 void index(CompilationUnit unit) { | 90 void index(CompilationUnit unit) { |
| 88 int unitId = _getUnitId(unit.element); | 91 int unitId = _getUnitId(unit.element); |
| 89 _UnitIndexAssembler assembler = new _UnitIndexAssembler(this, unitId); | 92 _UnitIndexAssembler assembler = new _UnitIndexAssembler(this, unitId); |
| 90 _units.add(assembler); | 93 _units.add(assembler); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 105 int offset = element.nameOffset; | 108 int offset = element.nameOffset; |
| 106 if (element is LibraryElement || element is CompilationUnitElement) { | 109 if (element is LibraryElement || element is CompilationUnitElement) { |
| 107 offset = 0; | 110 offset = 0; |
| 108 } | 111 } |
| 109 IndexSyntheticElementKind kind = getIndexElementKind(element); | 112 IndexSyntheticElementKind kind = getIndexElementKind(element); |
| 110 return new _ElementInfo(unitId, offset, kind); | 113 return new _ElementInfo(unitId, offset, kind); |
| 111 }); | 114 }); |
| 112 } | 115 } |
| 113 | 116 |
| 114 /** | 117 /** |
| 115 * Add information about [str] to [_strings] if necessary, and return the | 118 * Return the unique [_StringInfo] corresponding the [str]. The field |
| 116 * location in this array representing [str]. | 119 * [_StringInfo.id] is filled by [assemble] during final sorting. |
| 117 */ | 120 */ |
| 118 int _getStringId(String str) { | 121 _StringInfo _getStringInfo(String str) { |
| 119 return _stringMap.putIfAbsent(str, () { | 122 return _stringMap.putIfAbsent(str, () { |
| 120 int id = _strings.length; | 123 return new _StringInfo(str); |
| 121 _strings.add(str); | |
| 122 return id; | |
| 123 }); | 124 }); |
| 124 } | 125 } |
| 125 | 126 |
| 126 /** | 127 /** |
| 127 * Add information about [unitElement] to [_unitUnitUris] and | 128 * Add information about [unitElement] to [_unitUnitUris] and |
| 128 * [_unitLibraryUris] if necessary, and return the location in those | 129 * [_unitLibraryUris] if necessary, and return the location in those |
| 129 * arrays representing [unitElement]. | 130 * arrays representing [unitElement]. |
| 130 */ | 131 */ |
| 131 int _getUnitId(CompilationUnitElement unitElement) { | 132 int _getUnitId(CompilationUnitElement unitElement) { |
| 132 return _unitMap.putIfAbsent(unitElement, () { | 133 return _unitMap.putIfAbsent(unitElement, () { |
| 133 assert(_unitLibraryUris.length == _unitUnitUris.length); | 134 assert(_unitLibraryUris.length == _unitUnitUris.length); |
| 134 int id = _unitUnitUris.length; | 135 int id = _unitUnitUris.length; |
| 135 _unitLibraryUris.add(_getUriId(unitElement.library.source.uri)); | 136 _unitLibraryUris.add(_getUriInfo(unitElement.library.source.uri)); |
| 136 _unitUnitUris.add(_getUriId(unitElement.source.uri)); | 137 _unitUnitUris.add(_getUriInfo(unitElement.source.uri)); |
| 137 return id; | 138 return id; |
| 138 }); | 139 }); |
| 139 } | 140 } |
| 140 | 141 |
| 141 /** | 142 /** |
| 142 * Return the identifier corresponding to [uri]. | 143 * Return the unique [_StringInfo] corresponding [uri]. The field |
| 144 * [_StringInfo.id] is filled by [assemble] during final sorting. |
| 143 */ | 145 */ |
| 144 int _getUriId(Uri uri) { | 146 _StringInfo _getUriInfo(Uri uri) { |
| 145 String str = uri.toString(); | 147 String str = uri.toString(); |
| 146 return _getStringId(str); | 148 return _getStringInfo(str); |
| 147 } | 149 } |
| 148 | 150 |
| 149 /** | 151 /** |
| 150 * Return the kind of the given [element]. | 152 * Return the kind of the given [element]. |
| 151 */ | 153 */ |
| 152 static IndexSyntheticElementKind getIndexElementKind(Element element) { | 154 static IndexSyntheticElementKind getIndexElementKind(Element element) { |
| 153 if (element.isSynthetic) { | 155 if (element.isSynthetic) { |
| 154 if (element is ConstructorElement) { | 156 if (element is ConstructorElement) { |
| 155 return IndexSyntheticElementKind.constructor; | 157 return IndexSyntheticElementKind.constructor; |
| 156 } | 158 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 180 } | 182 } |
| 181 } | 183 } |
| 182 | 184 |
| 183 /** | 185 /** |
| 184 * Information about a single defined name. Any [_DefinedNameInfo] is always | 186 * Information about a single defined name. Any [_DefinedNameInfo] is always |
| 185 * part of a [_UnitIndexAssembler], so [offset] should be understood within the | 187 * part of a [_UnitIndexAssembler], so [offset] should be understood within the |
| 186 * context of the compilation unit pointed to by the [_UnitIndexAssembler]. | 188 * context of the compilation unit pointed to by the [_UnitIndexAssembler]. |
| 187 */ | 189 */ |
| 188 class _DefinedNameInfo { | 190 class _DefinedNameInfo { |
| 189 /** | 191 /** |
| 190 * The identifier of the name returned [PackageIndexAssembler._getStringId]. | 192 * The information about the name returned from |
| 193 * [PackageIndexAssembler._getStringInfo]. |
| 191 */ | 194 */ |
| 192 final int nameId; | 195 final _StringInfo nameInfo; |
| 193 | 196 |
| 194 /** | 197 /** |
| 195 * The coarse-grained kind of the defined name. | 198 * The coarse-grained kind of the defined name. |
| 196 */ | 199 */ |
| 197 final IndexNameKind kind; | 200 final IndexNameKind kind; |
| 198 | 201 |
| 199 /** | 202 /** |
| 200 * The name offset of the defined element. | 203 * The name offset of the defined element. |
| 201 */ | 204 */ |
| 202 final int offset; | 205 final int offset; |
| 203 | 206 |
| 204 _DefinedNameInfo(this.nameId, this.kind, this.offset); | 207 _DefinedNameInfo(this.nameInfo, this.kind, this.offset); |
| 205 } | 208 } |
| 206 | 209 |
| 207 /** | 210 /** |
| 208 * Information about an element referenced in index. | 211 * Information about an element referenced in index. |
| 209 */ | 212 */ |
| 210 class _ElementInfo { | 213 class _ElementInfo { |
| 211 /** | 214 /** |
| 212 * The identifier of the [CompilationUnitElement] containing this element. | 215 * The identifier of the [CompilationUnitElement] containing this element. |
| 213 */ | 216 */ |
| 214 final int unitId; | 217 final int unitId; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 226 /** | 229 /** |
| 227 * The unique id of the element. It is set after indexing of the whole | 230 * The unique id of the element. It is set after indexing of the whole |
| 228 * package is done and we are assembling the full package index. | 231 * package is done and we are assembling the full package index. |
| 229 */ | 232 */ |
| 230 int id; | 233 int id; |
| 231 | 234 |
| 232 _ElementInfo(this.unitId, this.offset, this.kind); | 235 _ElementInfo(this.unitId, this.offset, this.kind); |
| 233 } | 236 } |
| 234 | 237 |
| 235 /** | 238 /** |
| 239 * Information about a string referenced in the index. |
| 240 */ |
| 241 class _StringInfo { |
| 242 /** |
| 243 * The value of the string. |
| 244 */ |
| 245 final String value; |
| 246 |
| 247 /** |
| 248 * The unique id of the string. It is set after indexing of the whole |
| 249 * package is done and we are assembling the full package index. |
| 250 */ |
| 251 int id; |
| 252 |
| 253 _StringInfo(this.value); |
| 254 } |
| 255 |
| 256 /** |
| 236 * Information about a single relation. Any [_ElementRelationInfo] is always | 257 * Information about a single relation. Any [_ElementRelationInfo] is always |
| 237 * part of a [_UnitIndexAssembler], so [offset] and [length] should be | 258 * part of a [_UnitIndexAssembler], so [offset] and [length] should be |
| 238 * understood within the context of the compilation unit pointed to by the | 259 * understood within the context of the compilation unit pointed to by the |
| 239 * [_UnitIndexAssembler]. | 260 * [_UnitIndexAssembler]. |
| 240 */ | 261 */ |
| 241 class _ElementRelationInfo { | 262 class _ElementRelationInfo { |
| 242 final _ElementInfo elementInfo; | 263 final _ElementInfo elementInfo; |
| 243 final IndexRelationKind kind; | 264 final IndexRelationKind kind; |
| 244 final int offset; | 265 final int offset; |
| 245 final int length; | 266 final int length; |
| (...skipping 396 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 642 } | 663 } |
| 643 } | 664 } |
| 644 | 665 |
| 645 /** | 666 /** |
| 646 * Information about a single name relation. Any [_NameRelationInfo] is always | 667 * Information about a single name relation. Any [_NameRelationInfo] is always |
| 647 * part of a [_UnitIndexAssembler], so [offset] should be understood within the | 668 * part of a [_UnitIndexAssembler], so [offset] should be understood within the |
| 648 * context of the compilation unit pointed to by the [_UnitIndexAssembler]. | 669 * context of the compilation unit pointed to by the [_UnitIndexAssembler]. |
| 649 */ | 670 */ |
| 650 class _NameRelationInfo { | 671 class _NameRelationInfo { |
| 651 /** | 672 /** |
| 652 * The identifier of the name returned [PackageIndexAssembler._getStringId]. | 673 * The information about the name returned from |
| 674 * [PackageIndexAssembler._getStringInfo]. |
| 653 */ | 675 */ |
| 654 final int nameId; | 676 final _StringInfo nameInfo; |
| 655 final IndexRelationKind kind; | 677 final IndexRelationKind kind; |
| 656 final int offset; | 678 final int offset; |
| 657 | 679 |
| 658 _NameRelationInfo(this.nameId, this.kind, this.offset); | 680 _NameRelationInfo(this.nameInfo, this.kind, this.offset); |
| 659 } | 681 } |
| 660 | 682 |
| 661 /** | 683 /** |
| 662 * Assembler of a single [CompilationUnit] index. The intended usage sequence: | 684 * Assembler of a single [CompilationUnit] index. The intended usage sequence: |
| 663 * | 685 * |
| 664 * - Call [defineName] for each name defined in the compilation unit. | 686 * - Call [defineName] for each name defined in the compilation unit. |
| 665 * - Call [addElementRelation] for each element relation found in the | 687 * - Call [addElementRelation] for each element relation found in the |
| 666 * compilation unit. | 688 * compilation unit. |
| 667 * - Call [addNameRelation] for each name relation found in the | 689 * - Call [addNameRelation] for each name relation found in the |
| 668 * compilation unit. | 690 * compilation unit. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 682 void addElementRelation(Element element, IndexRelationKind kind, int offset, | 704 void addElementRelation(Element element, IndexRelationKind kind, int offset, |
| 683 int length, bool isQualified) { | 705 int length, bool isQualified) { |
| 684 try { | 706 try { |
| 685 _ElementInfo elementInfo = pkg._getElementInfo(element); | 707 _ElementInfo elementInfo = pkg._getElementInfo(element); |
| 686 elementRelations.add(new _ElementRelationInfo( | 708 elementRelations.add(new _ElementRelationInfo( |
| 687 elementInfo, kind, offset, length, isQualified)); | 709 elementInfo, kind, offset, length, isQualified)); |
| 688 } on StateError {} | 710 } on StateError {} |
| 689 } | 711 } |
| 690 | 712 |
| 691 void addNameRelation(String name, IndexRelationKind kind, int offset) { | 713 void addNameRelation(String name, IndexRelationKind kind, int offset) { |
| 692 int nameId = pkg._getStringId(name); | 714 _StringInfo nameId = pkg._getStringInfo(name); |
| 693 nameRelations.add(new _NameRelationInfo(nameId, kind, offset)); | 715 nameRelations.add(new _NameRelationInfo(nameId, kind, offset)); |
| 694 } | 716 } |
| 695 | 717 |
| 696 /** | 718 /** |
| 697 * Assemble a new [UnitIndexBuilder] using the information gathered | 719 * Assemble a new [UnitIndexBuilder] using the information gathered |
| 698 * by [addElementRelation] and [defineName]. | 720 * by [addElementRelation] and [defineName]. |
| 699 */ | 721 */ |
| 700 UnitIndexBuilder assemble() { | 722 UnitIndexBuilder assemble() { |
| 701 definedNames.sort((a, b) { | 723 definedNames.sort((a, b) { |
| 702 return a.nameId - b.nameId; | 724 return a.nameInfo.id - b.nameInfo.id; |
| 703 }); | 725 }); |
| 704 elementRelations.sort((a, b) { | 726 elementRelations.sort((a, b) { |
| 705 return a.elementInfo.id - b.elementInfo.id; | 727 return a.elementInfo.id - b.elementInfo.id; |
| 706 }); | 728 }); |
| 707 nameRelations.sort((a, b) { | 729 nameRelations.sort((a, b) { |
| 708 return a.nameId - b.nameId; | 730 return a.nameInfo.id - b.nameInfo.id; |
| 709 }); | 731 }); |
| 710 return new UnitIndexBuilder( | 732 return new UnitIndexBuilder( |
| 711 unit: unitId, | 733 unit: unitId, |
| 712 definedNames: definedNames.map((n) => n.nameId).toList(), | 734 definedNames: definedNames.map((n) => n.nameInfo.id).toList(), |
| 713 definedNameKinds: definedNames.map((n) => n.kind).toList(), | 735 definedNameKinds: definedNames.map((n) => n.kind).toList(), |
| 714 definedNameOffsets: definedNames.map((n) => n.offset).toList(), | 736 definedNameOffsets: definedNames.map((n) => n.offset).toList(), |
| 715 usedElements: elementRelations.map((r) => r.elementInfo.id).toList(), | 737 usedElements: elementRelations.map((r) => r.elementInfo.id).toList(), |
| 716 usedElementKinds: elementRelations.map((r) => r.kind).toList(), | 738 usedElementKinds: elementRelations.map((r) => r.kind).toList(), |
| 717 usedElementOffsets: elementRelations.map((r) => r.offset).toList(), | 739 usedElementOffsets: elementRelations.map((r) => r.offset).toList(), |
| 718 usedElementLengths: elementRelations.map((r) => r.length).toList(), | 740 usedElementLengths: elementRelations.map((r) => r.length).toList(), |
| 719 usedElementIsQualifiedFlags: | 741 usedElementIsQualifiedFlags: |
| 720 elementRelations.map((r) => r.isQualified).toList(), | 742 elementRelations.map((r) => r.isQualified).toList(), |
| 721 usedNames: nameRelations.map((r) => r.nameId).toList(), | 743 usedNames: nameRelations.map((r) => r.nameInfo.id).toList(), |
| 722 usedNameKinds: nameRelations.map((r) => r.kind).toList(), | 744 usedNameKinds: nameRelations.map((r) => r.kind).toList(), |
| 723 usedNameOffsets: nameRelations.map((r) => r.offset).toList()); | 745 usedNameOffsets: nameRelations.map((r) => r.offset).toList()); |
| 724 } | 746 } |
| 725 | 747 |
| 726 void defineName(String name, IndexNameKind kind, int offset) { | 748 void defineName(String name, IndexNameKind kind, int offset) { |
| 727 int nameId = pkg._getStringId(name); | 749 _StringInfo nameInfo = pkg._getStringInfo(name); |
| 728 definedNames.add(new _DefinedNameInfo(nameId, kind, offset)); | 750 definedNames.add(new _DefinedNameInfo(nameInfo, kind, offset)); |
| 729 } | 751 } |
| 730 } | 752 } |
| OLD | NEW |