Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(72)

Side by Side Diff: pkg/analyzer/lib/src/summary/index_unit.dart

Issue 1743963002: Index defined top-level and class member names. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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';
(...skipping 13 matching lines...) Expand all
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 [_elementLibraryUris] and [_elementUnitUris].
28 */ 28 */
29 final Map<CompilationUnitElement, int> _elementUnitMap = 29 final Map<CompilationUnitElement, int> _elementUnitMap =
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 [_uris]. 34 * [CompilationUnitElement]. It is an index into [_strings].
35 */ 35 */
36 final List<int> _elementLibraryUris = <int>[]; 36 final List<int> _elementLibraryUris = <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 [_uris]. 40 * [CompilationUnitElement]. It is an index into [_strings].
41 */ 41 */
42 final List<int> _elementUnitUris = <int>[]; 42 final List<int> _elementUnitUris = <int>[];
43 43
44 /** 44 /**
45 * Map associating URIs with their identifiers, which are indices 45 * Map associating strings with their identifiers, which are indices
46 * into [_uris]. 46 * into [_strings].
47 */ 47 */
48 final Map<String, int> _uriMap = <String, int>{}; 48 final Map<String, int> _stringMap = <String, int>{};
49 49
50 /** 50 /**
51 * List of unique URIs used in this index. 51 * List of unique strings used in this index.
52 */ 52 */
53 final List<String> _uris = <String>[]; 53 final List<String> _strings = <String>[];
54 54
55 /** 55 /**
56 * List of information about each unit indexed in this index. 56 * List of information about each unit indexed in this index.
57 */ 57 */
58 final List<_UnitIndexAssembler> _units = <_UnitIndexAssembler>[]; 58 final List<_UnitIndexAssembler> _units = <_UnitIndexAssembler>[];
59 59
60 /** 60 /**
61 * Assemble a new [PackageIndexBuilder] using the information gathered by 61 * Assemble a new [PackageIndexBuilder] using the information gathered by
62 * [index]. 62 * [index].
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 elementLibraryUris: _elementLibraryUris,
74 elementUnitUris: _elementUnitUris, 74 elementUnitUris: _elementUnitUris,
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 uris: _uris, 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);
(...skipping 11 matching lines...) Expand all
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
111 * location in this array representing [str].
112 */
113 int _getStringId(String str) {
114 return _stringMap.putIfAbsent(str, () {
115 int id = _strings.length;
116 _strings.add(str);
117 return id;
118 });
119 }
120
121 /**
110 * Add information about [unitElement] to [_elementUnitUris] and 122 * Add information about [unitElement] to [_elementUnitUris] and
111 * [_elementLibraryUris] if necessary, and return the location in those 123 * [_elementLibraryUris] if necessary, and return the location in those
112 * arrays representing [unitElement]. 124 * arrays representing [unitElement].
113 */ 125 */
114 int _getUnitElementId(CompilationUnitElement unitElement) { 126 int _getUnitElementId(CompilationUnitElement unitElement) {
115 return _elementUnitMap.putIfAbsent(unitElement, () { 127 return _elementUnitMap.putIfAbsent(unitElement, () {
116 assert(_elementLibraryUris.length == _elementUnitUris.length); 128 assert(_elementLibraryUris.length == _elementUnitUris.length);
117 int id = _elementUnitUris.length; 129 int id = _elementUnitUris.length;
118 _elementLibraryUris.add(_getUriId(unitElement.library.source.uri)); 130 _elementLibraryUris.add(_getUriId(unitElement.library.source.uri));
119 _elementUnitUris.add(_getUriId(unitElement.source.uri)); 131 _elementUnitUris.add(_getUriId(unitElement.source.uri));
120 return id; 132 return id;
121 }); 133 });
122 } 134 }
123 135
124 /** 136 /**
125 * Add information about [uri] to [_uris] if necessary, and return the 137 * Return the identifier corresponding to [uri].
126 * location in this array representing [uri].
127 */ 138 */
128 int _getUriId(Uri uri) { 139 int _getUriId(Uri uri) {
129 String str = uri.toString(); 140 String str = uri.toString();
130 return _uriMap.putIfAbsent(str, () { 141 return _getStringId(str);
131 int id = _uris.length;
132 _uris.add(str);
133 return id;
134 });
135 } 142 }
136 143
137 /** 144 /**
138 * Return the kind of the given [element]. 145 * Return the kind of the given [element].
139 */ 146 */
140 static IndexSyntheticElementKind getIndexElementKind(Element element) { 147 static IndexSyntheticElementKind getIndexElementKind(Element element) {
141 if (element.isSynthetic) { 148 if (element.isSynthetic) {
142 if (element is ConstructorElement) { 149 if (element is ConstructorElement) {
143 return IndexSyntheticElementKind.constructor; 150 return IndexSyntheticElementKind.constructor;
144 } 151 }
(...skipping 17 matching lines...) Expand all
162 } 169 }
163 if (e is LibraryElement) { 170 if (e is LibraryElement) {
164 return e.definingCompilationUnit; 171 return e.definingCompilationUnit;
165 } 172 }
166 } 173 }
167 throw new StateError(element.toString()); 174 throw new StateError(element.toString());
168 } 175 }
169 } 176 }
170 177
171 /** 178 /**
179 * Information about a single defined name. Any [_DefinedNameInfo] is always
180 * part of a [_UnitIndexAssembler], so [offset] should be understood within the
181 * context of the compilation unit pointed to by the [_UnitIndexAssembler].
182 */
183 class _DefinedNameInfo {
184 final int nameId;
Paul Berry 2016/02/29 20:26:02 Please document what this id refers to (I assume i
scheglov 2016/02/29 20:47:44 Fixed in https://codereview.chromium.org/175343300
185 final IndexNameKind kind;
186 final int offset;
187
188 _DefinedNameInfo(this.nameId, this.kind, this.offset);
189 }
190
191 /**
172 * Information about an element referenced in index. 192 * Information about an element referenced in index.
173 */ 193 */
174 class _ElementInfo { 194 class _ElementInfo {
175 /** 195 /**
176 * The identifier of the [CompilationUnitElement] containing this element. 196 * The identifier of the [CompilationUnitElement] containing this element.
177 */ 197 */
178 final int unitId; 198 final int unitId;
179 199
180 /** 200 /**
181 * The name offset of the element. 201 * The name offset of the element.
(...skipping 16 matching lines...) Expand all
198 218
199 /** 219 /**
200 * Visits a resolved AST and adds relationships into [_UnitIndexAssembler]. 220 * Visits a resolved AST and adds relationships into [_UnitIndexAssembler].
201 */ 221 */
202 class _IndexContributor extends GeneralizingAstVisitor { 222 class _IndexContributor extends GeneralizingAstVisitor {
203 final _UnitIndexAssembler assembler; 223 final _UnitIndexAssembler assembler;
204 224
205 _IndexContributor(this.assembler); 225 _IndexContributor(this.assembler);
206 226
207 /** 227 /**
228 * Record definition of the given [element].
229 */
230 void recordDefinedElement(Element element) {
231 if (element != null) {
232 String name = element.displayName;
233 int offset = element.nameOffset;
234 Element enclosing = element.enclosingElement;
235 if (enclosing is CompilationUnitElement) {
236 assembler.defineName(name, IndexNameKind.topLevel, offset);
237 } else if (enclosing is ClassElement) {
238 assembler.defineName(name, IndexNameKind.classMember, offset);
239 }
240 }
241 }
242
243 /**
208 * Record reference to the given operator [Element] and name. 244 * Record reference to the given operator [Element] and name.
209 */ 245 */
210 void recordOperatorReference(Token operator, Element element) { 246 void recordOperatorReference(Token operator, Element element) {
211 recordRelationToken(element, IndexRelationKind.IS_INVOKED_BY, operator); 247 recordRelationToken(element, IndexRelationKind.IS_INVOKED_BY, operator);
212 // TODO(scheglov) do we need this? 248 // TODO(scheglov) do we need this?
213 // // prepare location 249 // // prepare location
214 // LocationImpl location = _createLocationForToken(operator, element != null) ; 250 // LocationImpl location = _createLocationForToken(operator, element != null) ;
215 // // record name reference 251 // // record name reference
216 // { 252 // {
217 // String name = operator.lexeme; 253 // String name = operator.lexeme;
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
282 * Record a relation between a super [typeName] and its [Element]. 318 * Record a relation between a super [typeName] and its [Element].
283 */ 319 */
284 void recordSuperType(TypeName typeName, IndexRelationKind kind) { 320 void recordSuperType(TypeName typeName, IndexRelationKind kind) {
285 Identifier name = typeName?.name; 321 Identifier name = typeName?.name;
286 if (name != null) { 322 if (name != null) {
287 recordRelation(name.staticElement, kind, name); 323 recordRelation(name.staticElement, kind, name);
288 typeName.typeArguments?.accept(this); 324 typeName.typeArguments?.accept(this);
289 } 325 }
290 } 326 }
291 327
292 /**
293 * Record the top-level [element] definition.
294 */
295 void recordTopLevelElementDefinition(Element element) {
296 // TODO(scheglov) do we need this?
297 // if (element?.enclosingElement is CompilationUnitElement) {
298 // IndexableElement indexable = new IndexableElement(element);
299 // int offset = element.nameOffset;
300 // int length = element.nameLength;
301 // LocationImpl location = new LocationImpl(indexable, offset, length);
302 // recordRelationshipElement(
303 // _libraryElement, IndexConstants.DEFINES, location);
304 // _store.recordTopLevelDeclaration(element);
305 // }
306 }
307
308 void recordUriReference(Element element, UriBasedDirective directive) { 328 void recordUriReference(Element element, UriBasedDirective directive) {
309 recordRelation(element, IndexRelationKind.IS_REFERENCED_BY, directive.uri); 329 recordRelation(element, IndexRelationKind.IS_REFERENCED_BY, directive.uri);
310 } 330 }
311 331
312 @override 332 @override
313 visitAssignmentExpression(AssignmentExpression node) { 333 visitAssignmentExpression(AssignmentExpression node) {
314 recordOperatorReference(node.operator, node.bestElement); 334 recordOperatorReference(node.operator, node.bestElement);
315 super.visitAssignmentExpression(node); 335 super.visitAssignmentExpression(node);
316 } 336 }
317 337
318 @override 338 @override
319 visitBinaryExpression(BinaryExpression node) { 339 visitBinaryExpression(BinaryExpression node) {
320 recordOperatorReference(node.operator, node.bestElement); 340 recordOperatorReference(node.operator, node.bestElement);
321 super.visitBinaryExpression(node); 341 super.visitBinaryExpression(node);
322 } 342 }
323 343
324 @override 344 @override
325 visitClassDeclaration(ClassDeclaration node) { 345 visitClassDeclaration(ClassDeclaration node) {
326 ClassElement element = node.element;
327 recordTopLevelElementDefinition(element);
328 if (node.extendsClause == null) { 346 if (node.extendsClause == null) {
329 ClassElement objectElement = element.supertype?.element; 347 ClassElement objectElement = node.element.supertype?.element;
330 recordRelationOffset( 348 recordRelationOffset(
331 objectElement, IndexRelationKind.IS_EXTENDED_BY, node.name.offset, 0); 349 objectElement, IndexRelationKind.IS_EXTENDED_BY, node.name.offset, 0);
332 } 350 }
333 super.visitClassDeclaration(node); 351 super.visitClassDeclaration(node);
334 } 352 }
335 353
336 @override 354 @override
337 visitClassTypeAlias(ClassTypeAlias node) {
338 ClassElement element = node.element;
339 recordTopLevelElementDefinition(element);
340 super.visitClassTypeAlias(node);
341 }
342
343 @override
344 visitConstructorFieldInitializer(ConstructorFieldInitializer node) { 355 visitConstructorFieldInitializer(ConstructorFieldInitializer node) {
345 SimpleIdentifier fieldName = node.fieldName; 356 SimpleIdentifier fieldName = node.fieldName;
346 if (fieldName != null) { 357 if (fieldName != null) {
347 Element element = fieldName.staticElement; 358 Element element = fieldName.staticElement;
348 recordRelation(element, IndexRelationKind.IS_REFERENCED_BY, fieldName); 359 recordRelation(element, IndexRelationKind.IS_REFERENCED_BY, fieldName);
349 } 360 }
350 node.expression?.accept(this); 361 node.expression?.accept(this);
351 } 362 }
352 363
353 @override 364 @override
(...skipping 15 matching lines...) Expand all
369 element, IndexRelationKind.IS_REFERENCED_BY, offset, length); 380 element, IndexRelationKind.IS_REFERENCED_BY, offset, length);
370 } else { 381 } else {
371 int offset = node.type.end; 382 int offset = node.type.end;
372 recordRelationOffset( 383 recordRelationOffset(
373 element, IndexRelationKind.IS_REFERENCED_BY, offset, 0); 384 element, IndexRelationKind.IS_REFERENCED_BY, offset, 0);
374 } 385 }
375 super.visitConstructorName(node); 386 super.visitConstructorName(node);
376 } 387 }
377 388
378 @override 389 @override
379 visitEnumDeclaration(EnumDeclaration node) {
380 ClassElement element = node.element;
381 recordTopLevelElementDefinition(element);
382 super.visitEnumDeclaration(node);
383 }
384
385 @override
386 visitExportDirective(ExportDirective node) { 390 visitExportDirective(ExportDirective node) {
387 ExportElement element = node.element; 391 ExportElement element = node.element;
388 recordUriReference(element?.exportedLibrary, node); 392 recordUriReference(element?.exportedLibrary, node);
389 super.visitExportDirective(node); 393 super.visitExportDirective(node);
390 } 394 }
391 395
392 @override 396 @override
393 visitExtendsClause(ExtendsClause node) { 397 visitExtendsClause(ExtendsClause node) {
394 recordSuperType(node.superclass, IndexRelationKind.IS_EXTENDED_BY); 398 recordSuperType(node.superclass, IndexRelationKind.IS_EXTENDED_BY);
395 } 399 }
396 400
397 @override 401 @override
398 visitFunctionDeclaration(FunctionDeclaration node) {
399 Element element = node.element;
400 recordTopLevelElementDefinition(element);
401 super.visitFunctionDeclaration(node);
402 }
403
404 @override
405 visitFunctionTypeAlias(FunctionTypeAlias node) {
406 Element element = node.element;
407 recordTopLevelElementDefinition(element);
408 super.visitFunctionTypeAlias(node);
409 }
410
411 @override
412 visitImplementsClause(ImplementsClause node) { 402 visitImplementsClause(ImplementsClause node) {
413 for (TypeName typeName in node.interfaces) { 403 for (TypeName typeName in node.interfaces) {
414 recordSuperType(typeName, IndexRelationKind.IS_IMPLEMENTED_BY); 404 recordSuperType(typeName, IndexRelationKind.IS_IMPLEMENTED_BY);
415 } 405 }
416 } 406 }
417 407
418 @override 408 @override
419 visitImportDirective(ImportDirective node) { 409 visitImportDirective(ImportDirective node) {
420 ImportElement element = node.element; 410 ImportElement element = node.element;
421 recordUriReference(element?.importedLibrary, node); 411 recordUriReference(element?.importedLibrary, node);
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
487 } else { 477 } else {
488 int offset = node.thisKeyword.end; 478 int offset = node.thisKeyword.end;
489 recordRelationOffset( 479 recordRelationOffset(
490 element, IndexRelationKind.IS_REFERENCED_BY, offset, 0); 480 element, IndexRelationKind.IS_REFERENCED_BY, offset, 0);
491 } 481 }
492 super.visitRedirectingConstructorInvocation(node); 482 super.visitRedirectingConstructorInvocation(node);
493 } 483 }
494 484
495 @override 485 @override
496 visitSimpleIdentifier(SimpleIdentifier node) { 486 visitSimpleIdentifier(SimpleIdentifier node) {
487 Element element = node.bestElement;
497 // TODO(scheglov) do we need this? 488 // TODO(scheglov) do we need this?
498 // IndexableName indexableName = new IndexableName(node.name); 489 // IndexableName indexableName = new IndexableName(node.name);
499 // LocationImpl location = _createLocationForNode(node); 490 // LocationImpl location = _createLocationForNode(node);
500 // if (location == null) { 491 // if (location == null) {
501 // return; 492 // return;
502 // } 493 // }
503 // name in declaration 494 // name in declaration
504 if (node.inDeclarationContext()) { 495 if (node.inDeclarationContext()) {
505 // TODO(scheglov) do we need this? 496 recordDefinedElement(element);
506 // recordRelationshipIndexable(
507 // indexableName, IndexConstants.NAME_IS_DEFINED_BY, location);
508 return; 497 return;
509 } 498 }
510 Element element = node.bestElement;
511 // this.field parameter 499 // this.field parameter
512 if (element is FieldFormalParameterElement) { 500 if (element is FieldFormalParameterElement) {
513 recordRelation(element.field, IndexRelationKind.IS_REFERENCED_BY, node); 501 recordRelation(element.field, IndexRelationKind.IS_REFERENCED_BY, node);
514 return; 502 return;
515 } 503 }
516 // record specific relations 504 // record specific relations
517 IndexRelationKind kind = node.isQualified 505 IndexRelationKind kind = node.isQualified
518 ? IndexRelationKind.IS_REFERENCED_QUALIFIED_BY 506 ? IndexRelationKind.IS_REFERENCED_QUALIFIED_BY
519 : IndexRelationKind.IS_REFERENCED_BY; 507 : IndexRelationKind.IS_REFERENCED_BY;
520 recordRelation(element, kind, node); 508 recordRelation(element, kind, node);
(...skipping 19 matching lines...) Expand all
540 visitTypeName(TypeName node) { 528 visitTypeName(TypeName node) {
541 AstNode parent = node.parent; 529 AstNode parent = node.parent;
542 if (parent is ClassTypeAlias && parent.superclass == node) { 530 if (parent is ClassTypeAlias && parent.superclass == node) {
543 recordSuperType(node, IndexRelationKind.IS_EXTENDED_BY); 531 recordSuperType(node, IndexRelationKind.IS_EXTENDED_BY);
544 } else { 532 } else {
545 super.visitTypeName(node); 533 super.visitTypeName(node);
546 } 534 }
547 } 535 }
548 536
549 @override 537 @override
550 visitVariableDeclaration(VariableDeclaration node) {
551 VariableElement element = node.element;
552 recordTopLevelElementDefinition(element);
553 // TODO(scheglov) do we need this?
554 // // record declaration
555 // {
556 // SimpleIdentifier name = node.name;
557 // LocationImpl location = _createLocationForNode(name);
558 // location = _getLocationWithExpressionType(location, node.initializer);
559 // recordRelationshipElement(
560 // element, IndexConstants.NAME_IS_DEFINED_BY, location);
561 // }
562 super.visitVariableDeclaration(node);
563 }
564
565 @override
566 visitWithClause(WithClause node) { 538 visitWithClause(WithClause node) {
567 for (TypeName typeName in node.mixinTypes) { 539 for (TypeName typeName in node.mixinTypes) {
568 recordSuperType(typeName, IndexRelationKind.IS_MIXED_IN_BY); 540 recordSuperType(typeName, IndexRelationKind.IS_MIXED_IN_BY);
569 } 541 }
570 } 542 }
571 } 543 }
572 544
573 /** 545 /**
574 * Information about a single relation. Any [_RelationInfo] is always part 546 * Information about a single relation. Any [_RelationInfo] is always part
575 * of a [_UnitIndexAssembler], so [offset] and [length] should be understood 547 * of a [_UnitIndexAssembler], so [offset] and [length] should be understood
576 * within the context of the compilation unit pointed to by the 548 * within the context of the compilation unit pointed to by the
577 * [_UnitIndexAssembler]. 549 * [_UnitIndexAssembler].
578 */ 550 */
579 class _RelationInfo { 551 class _RelationInfo {
580 final _ElementInfo elementInfo; 552 final _ElementInfo elementInfo;
581 final IndexRelationKind kind; 553 final IndexRelationKind kind;
582 final int offset; 554 final int offset;
583 final int length; 555 final int length;
584 556
585 _RelationInfo(this.elementInfo, this.kind, this.offset, this.length); 557 _RelationInfo(this.elementInfo, this.kind, this.offset, this.length);
586 } 558 }
587 559
588 /** 560 /**
589 * Assembler of a single [CompilationUnit] index. The intended usage sequence: 561 * Assembler of a single [CompilationUnit] index. The intended usage sequence:
590 * 562 *
563 * - Call [defineName] for name defined in the compilation unit.
Paul Berry 2016/02/29 20:26:02 s/for name/for each name/
591 * - Call [addRelation] for each relation found in the compilation unit. 564 * - Call [addRelation] for each relation found in the compilation unit.
592 * - Assign ids to all the [_ElementInfo] objects reachable from [relations]. 565 * - Assign ids to all the [_ElementInfo] objects reachable from [relations].
593 * - Call [assemble] to produce the final unit index. 566 * - Call [assemble] to produce the final unit index.
594 */ 567 */
595 class _UnitIndexAssembler { 568 class _UnitIndexAssembler {
596 final PackageIndexAssembler pkg; 569 final PackageIndexAssembler pkg;
597 final CompilationUnitElement unitElement; 570 final CompilationUnitElement unitElement;
571 final List<_DefinedNameInfo> definedNames = <_DefinedNameInfo>[];
598 final List<_RelationInfo> relations = <_RelationInfo>[]; 572 final List<_RelationInfo> relations = <_RelationInfo>[];
599 573
600 _UnitIndexAssembler(this.pkg, this.unitElement); 574 _UnitIndexAssembler(this.pkg, this.unitElement);
601 575
602 void addRelation( 576 void addRelation(
603 Element element, IndexRelationKind kind, int offset, int length) { 577 Element element, IndexRelationKind kind, int offset, int length) {
604 try { 578 try {
605 _ElementInfo elementInfo = pkg._getElementInfo(element); 579 _ElementInfo elementInfo = pkg._getElementInfo(element);
606 relations.add(new _RelationInfo(elementInfo, kind, offset, length)); 580 relations.add(new _RelationInfo(elementInfo, kind, offset, length));
607 } on StateError {} 581 } on StateError {}
608 } 582 }
609 583
610 /** 584 /**
611 * Assemble a new [UnitIndexBuilder] using the information gathered 585 * Assemble a new [UnitIndexBuilder] using the information gathered
612 * by [addRelation] 586 * by [addRelation] and [defineName].
613 */ 587 */
614 UnitIndexBuilder assemble() { 588 UnitIndexBuilder assemble() {
615 relations.sort((a, b) { 589 relations.sort((a, b) {
616 return a.elementInfo.id - b.elementInfo.id; 590 return a.elementInfo.id - b.elementInfo.id;
617 }); 591 });
592 definedNames.sort((a, b) {
593 return a.nameId - b.nameId;
594 });
618 return new UnitIndexBuilder( 595 return new UnitIndexBuilder(
596 definedNames: definedNames.map((n) => n.nameId).toList(),
597 definedNameKinds: definedNames.map((n) => n.kind).toList(),
598 definedNameOffsets: definedNames.map((n) => n.offset).toList(),
619 elements: relations.map((r) => r.elementInfo.id).toList(), 599 elements: relations.map((r) => r.elementInfo.id).toList(),
620 kinds: relations.map((r) => r.kind).toList(), 600 kinds: relations.map((r) => r.kind).toList(),
621 locationOffsets: relations.map((r) => r.offset).toList(), 601 locationOffsets: relations.map((r) => r.offset).toList(),
622 locationLengths: relations.map((r) => r.length).toList(), 602 locationLengths: relations.map((r) => r.length).toList(),
623 libraryUri: pkg._getUriId(unitElement.library.source.uri), 603 libraryUri: pkg._getUriId(unitElement.library.source.uri),
624 unitUri: pkg._getUriId(unitElement.source.uri)); 604 unitUri: pkg._getUriId(unitElement.source.uri));
625 } 605 }
606
607 void defineName(String name, IndexNameKind kind, int offset) {
608 int nameId = pkg._getStringId(name);
609 definedNames.add(new _DefinedNameInfo(nameId, kind, offset));
610 }
626 } 611 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698