Chromium Code Reviews| 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/src/generated/utilities_dart.dart'; | 9 import 'package:analyzer/src/generated/utilities_dart.dart'; |
| 10 import 'package:analyzer/src/summary/format.dart'; | 10 import 'package:analyzer/src/summary/format.dart'; |
| 11 import 'package:analyzer/src/summary/idl.dart'; | 11 import 'package:analyzer/src/summary/idl.dart'; |
| 12 | 12 |
| 13 /** | 13 /** |
| 14 * Object that gathers information about the whole package index and then uses | 14 * Object that gathers information about the whole package index and then uses |
| 15 * it to assemble a new [PackageIndexBuilder]. Call [index] on each compilation | 15 * it to assemble a new [PackageIndexBuilder]. Call [index] on each compilation |
| 16 * unit to be indexed, then call [assemble] to retrieve the complete index for | 16 * unit to be indexed, then call [assemble] to retrieve the complete index for |
| 17 * the package. | 17 * the package. |
| 18 */ | 18 */ |
| 19 class PackageIndexAssembler { | 19 class PackageIndexAssembler { |
| 20 /** | 20 /** |
| 21 * Map associating referenced elements with their [_ElementInfo]s. | 21 * Map associating referenced elements with their [_ElementInfo]s. |
| 22 */ | 22 */ |
| 23 final Map<Element, _ElementInfo> _elementMap = <Element, _ElementInfo>{}; | 23 final Map<Element, _ElementInfo> _elementMap = <Element, _ElementInfo>{}; |
| 24 | 24 |
| 25 /** | 25 /** |
| 26 * Map associating [CompilationUnitElement]s with their identifiers, which | 26 * Map associating [CompilationUnitElement]s with their identifiers, which |
| 27 * are indices into [_elementLibraryUris] and [_elementUnitUris]. | 27 * are indices into [_unitLibraryUris] and [_unitUnitUris]. |
| 28 */ | 28 */ |
| 29 final Map<CompilationUnitElement, int> _elementUnitMap = | 29 final Map<CompilationUnitElement, int> _unitMap = |
| 30 <CompilationUnitElement, int>{}; | 30 <CompilationUnitElement, int>{}; |
| 31 | 31 |
| 32 /** | 32 /** |
| 33 * Each item of this list corresponds to the library URI of a unique | 33 * Each item of this list corresponds to the library URI of a unique |
| 34 * [CompilationUnitElement]. It is an index into [_strings]. | 34 * [CompilationUnitElement]. It is an index into [_strings]. |
| 35 */ | 35 */ |
| 36 final List<int> _elementLibraryUris = <int>[]; | 36 final List<int> _unitLibraryUris = <int>[]; |
| 37 | 37 |
| 38 /** | 38 /** |
| 39 * Each item of this list corresponds to the unit URI of a unique | 39 * Each item of this list corresponds to the unit URI of a unique |
| 40 * [CompilationUnitElement]. It is an index into [_strings]. | 40 * [CompilationUnitElement]. It is an index into [_strings]. |
| 41 */ | 41 */ |
| 42 final List<int> _elementUnitUris = <int>[]; | 42 final List<int> _unitUnitUris = <int>[]; |
| 43 | 43 |
| 44 /** | 44 /** |
| 45 * Map associating strings with their identifiers, which are indices | 45 * Map associating strings with their identifiers, which are indices |
| 46 * into [_strings]. | 46 * into [_strings]. |
| 47 */ | 47 */ |
| 48 final Map<String, int> _stringMap = <String, int>{}; | 48 final Map<String, int> _stringMap = <String, int>{}; |
| 49 | 49 |
| 50 /** | 50 /** |
| 51 * List of unique strings used in this index. | 51 * List of unique strings used in this index. |
| 52 */ | 52 */ |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 63 */ | 63 */ |
| 64 PackageIndexBuilder assemble() { | 64 PackageIndexBuilder assemble() { |
| 65 List<_ElementInfo> elementInfoList = _elementMap.values.toList(); | 65 List<_ElementInfo> elementInfoList = _elementMap.values.toList(); |
| 66 elementInfoList.sort((a, b) { | 66 elementInfoList.sort((a, b) { |
| 67 return a.offset - b.offset; | 67 return a.offset - b.offset; |
| 68 }); | 68 }); |
| 69 for (int i = 0; i < elementInfoList.length; i++) { | 69 for (int i = 0; i < elementInfoList.length; i++) { |
| 70 elementInfoList[i].id = i; | 70 elementInfoList[i].id = i; |
| 71 } | 71 } |
| 72 return new PackageIndexBuilder( | 72 return new PackageIndexBuilder( |
| 73 elementLibraryUris: _elementLibraryUris, | 73 unitLibraryUris: _unitLibraryUris, |
| 74 elementUnitUris: _elementUnitUris, | 74 unitUnitUris: _unitUnitUris, |
| 75 elementUnits: elementInfoList.map((e) => e.unitId).toList(), | 75 elementUnits: elementInfoList.map((e) => e.unitId).toList(), |
| 76 elementOffsets: elementInfoList.map((e) => e.offset).toList(), | 76 elementOffsets: elementInfoList.map((e) => e.offset).toList(), |
| 77 elementKinds: elementInfoList.map((e) => e.kind).toList(), | 77 elementKinds: elementInfoList.map((e) => e.kind).toList(), |
| 78 strings: _strings, | 78 strings: _strings, |
| 79 units: _units.map((unit) => unit.assemble()).toList()); | 79 units: _units.map((unit) => unit.assemble()).toList()); |
| 80 } | 80 } |
| 81 | 81 |
| 82 /** | 82 /** |
| 83 * Index the given fully resolved [unit]. | 83 * Index the given fully resolved [unit]. |
| 84 */ | 84 */ |
| 85 void index(CompilationUnit unit) { | 85 void index(CompilationUnit unit) { |
| 86 CompilationUnitElement unitElement = unit.element; | 86 CompilationUnitElement unitElement = unit.element; |
| 87 _UnitIndexAssembler assembler = new _UnitIndexAssembler(this, unitElement); | 87 _UnitIndexAssembler assembler = new _UnitIndexAssembler(this, unitElement); |
| 88 _units.add(assembler); | 88 _units.add(assembler); |
| 89 unit.accept(new _IndexContributor(assembler)); | 89 unit.accept(new _IndexContributor(assembler)); |
| 90 } | 90 } |
| 91 | 91 |
| 92 /** | 92 /** |
| 93 * Return the unique [_ElementInfo] corresponding the [element]. The field | 93 * Return the unique [_ElementInfo] corresponding the [element]. The field |
| 94 * [_ElementInfo.id] is filled by [assemble] during final sorting. | 94 * [_ElementInfo.id] is filled by [assemble] during final sorting. |
| 95 */ | 95 */ |
| 96 _ElementInfo _getElementInfo(Element element) { | 96 _ElementInfo _getElementInfo(Element element) { |
| 97 return _elementMap.putIfAbsent(element, () { | 97 return _elementMap.putIfAbsent(element, () { |
| 98 CompilationUnitElement unitElement = getUnitElement(element); | 98 CompilationUnitElement unitElement = getUnitElement(element); |
| 99 int unitId = _getUnitElementId(unitElement); | 99 int unitId = _getUnitId(unitElement); |
| 100 int offset = element.nameOffset; | 100 int offset = element.nameOffset; |
| 101 if (element is LibraryElement || element is CompilationUnitElement) { | 101 if (element is LibraryElement || element is CompilationUnitElement) { |
| 102 offset = 0; | 102 offset = 0; |
| 103 } | 103 } |
| 104 IndexSyntheticElementKind kind = getIndexElementKind(element); | 104 IndexSyntheticElementKind kind = getIndexElementKind(element); |
| 105 return new _ElementInfo(unitId, offset, kind); | 105 return new _ElementInfo(unitId, offset, kind); |
| 106 }); | 106 }); |
| 107 } | 107 } |
| 108 | 108 |
| 109 /** | 109 /** |
| 110 * Add information about [str] to [_strings] if necessary, and return the | 110 * Add information about [str] to [_strings] if necessary, and return the |
| 111 * location in this array representing [str]. | 111 * location in this array representing [str]. |
| 112 */ | 112 */ |
| 113 int _getStringId(String str) { | 113 int _getStringId(String str) { |
| 114 return _stringMap.putIfAbsent(str, () { | 114 return _stringMap.putIfAbsent(str, () { |
| 115 int id = _strings.length; | 115 int id = _strings.length; |
| 116 _strings.add(str); | 116 _strings.add(str); |
| 117 return id; | 117 return id; |
| 118 }); | 118 }); |
| 119 } | 119 } |
| 120 | 120 |
| 121 /** | 121 /** |
| 122 * Add information about [unitElement] to [_elementUnitUris] and | 122 * Add information about [unitElement] to [_unitUnitUris] and |
| 123 * [_elementLibraryUris] if necessary, and return the location in those | 123 * [_unitLibraryUris] if necessary, and return the location in those |
| 124 * arrays representing [unitElement]. | 124 * arrays representing [unitElement]. |
| 125 */ | 125 */ |
| 126 int _getUnitElementId(CompilationUnitElement unitElement) { | 126 int _getUnitId(CompilationUnitElement unitElement) { |
| 127 return _elementUnitMap.putIfAbsent(unitElement, () { | 127 return _unitMap.putIfAbsent(unitElement, () { |
| 128 assert(_elementLibraryUris.length == _elementUnitUris.length); | 128 assert(_unitLibraryUris.length == _unitUnitUris.length); |
| 129 int id = _elementUnitUris.length; | 129 int id = _unitUnitUris.length; |
| 130 _elementLibraryUris.add(_getUriId(unitElement.library.source.uri)); | 130 _unitLibraryUris.add(_getUriId(unitElement.library.source.uri)); |
| 131 _elementUnitUris.add(_getUriId(unitElement.source.uri)); | 131 _unitUnitUris.add(_getUriId(unitElement.source.uri)); |
| 132 return id; | 132 return id; |
| 133 }); | 133 }); |
| 134 } | 134 } |
| 135 | 135 |
| 136 /** | 136 /** |
| 137 * Return the identifier corresponding to [uri]. | 137 * Return the identifier corresponding to [uri]. |
| 138 */ | 138 */ |
| 139 int _getUriId(Uri uri) { | 139 int _getUriId(Uri uri) { |
| 140 String str = uri.toString(); | 140 String str = uri.toString(); |
| 141 return _getStringId(str); | 141 return _getStringId(str); |
| (...skipping 444 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 586 * by [addRelation] and [defineName]. | 586 * by [addRelation] and [defineName]. |
| 587 */ | 587 */ |
| 588 UnitIndexBuilder assemble() { | 588 UnitIndexBuilder assemble() { |
| 589 relations.sort((a, b) { | 589 relations.sort((a, b) { |
| 590 return a.elementInfo.id - b.elementInfo.id; | 590 return a.elementInfo.id - b.elementInfo.id; |
| 591 }); | 591 }); |
| 592 definedNames.sort((a, b) { | 592 definedNames.sort((a, b) { |
| 593 return a.nameId - b.nameId; | 593 return a.nameId - b.nameId; |
| 594 }); | 594 }); |
| 595 return new UnitIndexBuilder( | 595 return new UnitIndexBuilder( |
| 596 unit: pkg._getUnitId(unitElement), | |
|
Paul Berry
2016/02/29 18:39:01
I don't think this is a bug but I'm nervous about
scheglov
2016/02/29 18:48:27
Thank you.
I changed it to pass unitId into the Un
| |
| 596 definedNames: definedNames.map((n) => n.nameId).toList(), | 597 definedNames: definedNames.map((n) => n.nameId).toList(), |
| 597 definedNameKinds: definedNames.map((n) => n.kind).toList(), | 598 definedNameKinds: definedNames.map((n) => n.kind).toList(), |
| 598 definedNameOffsets: definedNames.map((n) => n.offset).toList(), | 599 definedNameOffsets: definedNames.map((n) => n.offset).toList(), |
| 599 elements: relations.map((r) => r.elementInfo.id).toList(), | 600 usedElements: relations.map((r) => r.elementInfo.id).toList(), |
| 600 kinds: relations.map((r) => r.kind).toList(), | 601 usedElementKinds: relations.map((r) => r.kind).toList(), |
| 601 locationOffsets: relations.map((r) => r.offset).toList(), | 602 usedElementOffsets: relations.map((r) => r.offset).toList(), |
| 602 locationLengths: relations.map((r) => r.length).toList(), | 603 usedElementLengths: relations.map((r) => r.length).toList()); |
| 603 libraryUri: pkg._getUriId(unitElement.library.source.uri), | |
| 604 unitUri: pkg._getUriId(unitElement.source.uri)); | |
| 605 } | 604 } |
| 606 | 605 |
| 607 void defineName(String name, IndexNameKind kind, int offset) { | 606 void defineName(String name, IndexNameKind kind, int offset) { |
| 608 int nameId = pkg._getStringId(name); | 607 int nameId = pkg._getStringId(name); |
| 609 definedNames.add(new _DefinedNameInfo(nameId, kind, offset)); | 608 definedNames.add(new _DefinedNameInfo(nameId, kind, offset)); |
| 610 } | 609 } |
| 611 } | 610 } |
| OLD | NEW |