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

Side by Side Diff: pkg/analyzer/lib/src/index/index.dart

Issue 365193004: Move Index and IndexStore implementations into Engine. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 5 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 | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 // This code was auto-generated, is not intended to be edited, and is subject to
6 // significant change. Please see the README file for more information.
7
8 library engine.src.index;
9
10 import 'dart:collection' show Queue;
11
12 import 'package:analyzer/index/index.dart';
13 import 'package:analyzer/index/index_store.dart';
14 import 'package:analyzer/src/generated/ast.dart';
15 import 'package:analyzer/src/generated/element.dart';
16 import 'package:analyzer/src/generated/engine.dart';
17 import 'package:analyzer/src/generated/html.dart' as ht;
18 import 'package:analyzer/src/generated/java_core.dart';
19 import 'package:analyzer/src/generated/java_engine.dart';
20 import 'package:analyzer/src/generated/resolver.dart';
21 import 'package:analyzer/src/generated/scanner.dart';
22 import 'package:analyzer/src/generated/source.dart';
23
24
25 /**
26 * Adds data to [store] based on the resolved Dart [unit].
27 */
28 void indexDartUnit(IndexStore store, AnalysisContext context,
29 CompilationUnit unit) {
30 CompilationUnitElement unitElement = unit.element;
31 bool mayIndex = store.aboutToIndexDart(context, unitElement);
32 if (!mayIndex) {
33 return;
34 }
35 unit.accept(new _IndexContributor(store));
36 unit.accept(new _AngularDartIndexContributor(store));
37 store.doneIndex();
38 }
39
40
41 /**
42 * Adds data to [store] based on the resolved HTML [unit].
43 */
44 void indexHtmlUnit(IndexStore store, AnalysisContext context, ht.HtmlUnit unit)
45 {
46 HtmlElement unitElement = unit.element;
47 bool mayIndex = store.aboutToIndexHtml(context, unitElement);
48 if (!mayIndex) {
49 return;
50 }
51 unit.accept(new _AngularHtmlIndexContributor(store));
52 store.doneIndex();
53 }
54
55
56 /**
57 * Visits resolved [CompilationUnit] and adds Angular specific relationships
58 * into [IndexStore].
59 */
60 class _AngularDartIndexContributor extends GeneralizingAstVisitor<Object> {
61 final IndexStore _store;
62
63 _AngularDartIndexContributor(this._store);
64
65 @override
66 Object visitClassDeclaration(ClassDeclaration node) {
67 ClassElement classElement = node.element;
68 if (classElement != null) {
69 List<ToolkitObjectElement> toolkitObjects = classElement.toolkitObjects;
70 for (ToolkitObjectElement object in toolkitObjects) {
71 if (object is AngularComponentElement) {
72 _indexComponent(object);
73 }
74 if (object is AngularDecoratorElement) {
75 AngularDecoratorElement directive = object;
76 _indexDirective(directive);
77 }
78 }
79 }
80 // stop visiting
81 return null;
82 }
83
84 @override
85 Object visitCompilationUnitMember(CompilationUnitMember node) => null;
86
87 void _indexComponent(AngularComponentElement component) {
88 _indexProperties(component.properties);
89 }
90
91 void _indexDirective(AngularDecoratorElement directive) {
92 _indexProperties(directive.properties);
93 }
94
95 /**
96 * Index [FieldElement] references from [AngularPropertyElement]s.
97 */
98 void _indexProperties(List<AngularPropertyElement> properties) {
99 for (AngularPropertyElement property in properties) {
100 FieldElement field = property.field;
101 if (field != null) {
102 int offset = property.fieldNameOffset;
103 if (offset == -1) {
104 continue;
105 }
106 int length = field.name.length;
107 Location location = new Location(property, offset, length);
108 // getter reference
109 if (property.propertyKind.callsGetter()) {
110 PropertyAccessorElement getter = field.getter;
111 if (getter != null) {
112 _store.recordRelationship(getter,
113 IndexConstants.IS_REFERENCED_BY_QUALIFIED, location);
114 }
115 }
116 // setter reference
117 if (property.propertyKind.callsSetter()) {
118 PropertyAccessorElement setter = field.setter;
119 if (setter != null) {
120 _store.recordRelationship(setter,
121 IndexConstants.IS_REFERENCED_BY_QUALIFIED, location);
122 }
123 }
124 }
125 }
126 }
127 }
128
129
130 /**
131 * Visits resolved [HtmlUnit] and adds relationships into [IndexStore].
132 */
133 class _AngularHtmlIndexContributor extends _ExpressionVisitor {
134 /**
135 * The [IndexStore] to record relations into.
136 */
137 final IndexStore _store;
138
139 /**
140 * The index contributor used to index Dart [Expression]s.
141 */
142 _IndexContributor _indexContributor;
143
144 HtmlElement _htmlUnitElement;
145
146 /**
147 * Initialize a newly created Angular HTML index contributor.
148 *
149 * [store] - the [IndexStore] to record relations into.
150 */
151 _AngularHtmlIndexContributor(this._store) {
152 _indexContributor = new _AngularHtmlIndexContributor_forEmbeddedDart(_store,
153 this);
154 }
155
156 @override
157 void visitExpression(Expression expression) {
158 // Formatter
159 if (expression is SimpleIdentifier) {
160 Element element = expression.bestElement;
161 if (element is AngularElement) {
162 _store.recordRelationship(element, IndexConstants.ANGULAR_REFERENCE,
163 _createLocationForIdentifier(expression));
164 return;
165 }
166 }
167 // index as a normal Dart expression
168 expression.accept(_indexContributor);
169 }
170
171 @override
172 Object visitHtmlUnit(ht.HtmlUnit node) {
173 _htmlUnitElement = node.element;
174 CompilationUnitElement dartUnitElement =
175 _htmlUnitElement.angularCompilationUnit;
176 _indexContributor.enterScope(dartUnitElement);
177 return super.visitHtmlUnit(node);
178 }
179
180 @override
181 Object visitXmlAttributeNode(ht.XmlAttributeNode node) {
182 Element element = node.element;
183 if (element != null) {
184 ht.Token nameToken = node.nameToken;
185 Location location = _createLocationForToken(nameToken);
186 _store.recordRelationship(element, IndexConstants.ANGULAR_REFERENCE,
187 location);
188 }
189 return super.visitXmlAttributeNode(node);
190 }
191
192 @override
193 Object visitXmlTagNode(ht.XmlTagNode node) {
194 Element element = node.element;
195 if (element != null) {
196 // tag
197 {
198 ht.Token tagToken = node.tagToken;
199 Location location = _createLocationForToken(tagToken);
200 _store.recordRelationship(element, IndexConstants.ANGULAR_REFERENCE,
201 location);
202 }
203 // maybe add closing tag range
204 ht.Token closingTag = node.closingTag;
205 if (closingTag != null) {
206 Location location = _createLocationForToken(closingTag);
207 _store.recordRelationship(element,
208 IndexConstants.ANGULAR_CLOSING_TAG_REFERENCE, location);
209 }
210 }
211 return super.visitXmlTagNode(node);
212 }
213
214 Location _createLocationForIdentifier(SimpleIdentifier identifier) =>
215 new Location(_htmlUnitElement, identifier.offset, identifier.length);
216
217 Location _createLocationForToken(ht.Token token) => new Location(
218 _htmlUnitElement, token.offset, token.length);
219 }
220
221
222 class _AngularHtmlIndexContributor_forEmbeddedDart extends _IndexContributor {
223 final _AngularHtmlIndexContributor angularContributor;
224
225 _AngularHtmlIndexContributor_forEmbeddedDart(IndexStore store,
226 this.angularContributor) : super(store);
227
228 @override
229 Element peekElement() => angularContributor._htmlUnitElement;
230
231 @override
232 void recordRelationship(Element element, Relationship relationship,
233 Location location) {
234 AngularElement angularElement = AngularHtmlUnitResolver.getAngularElement(
235 element);
236 if (angularElement != null) {
237 element = angularElement;
238 relationship = IndexConstants.ANGULAR_REFERENCE;
239 }
240 super.recordRelationship(element, relationship, location);
241 }
242 }
243
244
245 /**
246 * Recursively visits an [HtmlUnit] and every embedded [Expression].
247 */
248 abstract class _ExpressionVisitor extends ht.RecursiveXmlVisitor<Object> {
249 /**
250 * Visits the given [Expression]s embedded into tag or attribute.
251 *
252 * [expression] - the [Expression] to visit, not `null`
253 */
254 void visitExpression(Expression expression);
255
256 @override
257 Object visitXmlAttributeNode(ht.XmlAttributeNode node) {
258 _visitExpressions(node.expressions);
259 return super.visitXmlAttributeNode(node);
260 }
261
262 @override
263 Object visitXmlTagNode(ht.XmlTagNode node) {
264 _visitExpressions(node.expressions);
265 return super.visitXmlTagNode(node);
266 }
267
268 /**
269 * Visits [Expression]s of the given [XmlExpression]s.
270 */
271 void _visitExpressions(List<ht.XmlExpression> expressions) {
272 for (ht.XmlExpression xmlExpression in expressions) {
273 if (xmlExpression is AngularXmlExpression) {
274 AngularXmlExpression angularXmlExpression = xmlExpression;
275 List<Expression> dartExpressions =
276 angularXmlExpression.expression.expressions;
277 for (Expression dartExpression in dartExpressions) {
278 visitExpression(dartExpression);
279 }
280 }
281 if (xmlExpression is ht.RawXmlExpression) {
282 ht.RawXmlExpression rawXmlExpression = xmlExpression;
283 visitExpression(rawXmlExpression.expression);
284 }
285 }
286 }
287 }
288
289
290 /**
291 * Information about [ImportElement] and place where it is referenced using
292 * [PrefixElement].
293 */
294 class _ImportElementInfo {
295 ImportElement _element;
296
297 int _periodEnd = 0;
298 }
299
300
301 /**
302 * Visits a resolved AST and adds relationships into [IndexStore].
303 */
304 class _IndexContributor extends GeneralizingAstVisitor<Object> {
305 final IndexStore _store;
306
307 LibraryElement _libraryElement;
308
309 Map<ImportElement, Set<Element>> _importElementsMap = {};
310
311 /**
312 * A stack whose top element (the element with the largest index) is an elemen t representing the
313 * inner-most enclosing scope.
314 */
315 Queue<Element> _elementStack = new Queue();
316
317 _IndexContributor(this._store);
318
319 /**
320 * Enter a new scope represented by the given [Element].
321 */
322 void enterScope(Element element) {
323 _elementStack.addFirst(element);
324 }
325
326 /**
327 * @return the inner-most enclosing [Element], may be `null`.
328 */
329 Element peekElement() {
330 for (Element element in _elementStack) {
331 if (element != null) {
332 return element;
333 }
334 }
335 return null;
336 }
337
338 /**
339 * Record the given relationship between the given [Element] and [Location].
340 */
341 void recordRelationship(Element element, Relationship relationship,
342 Location location) {
343 if (element != null && location != null) {
344 _store.recordRelationship(element, relationship, location);
345 }
346 }
347
348 @override
349 Object visitAssignmentExpression(AssignmentExpression node) {
350 _recordOperatorReference(node.operator, node.bestElement);
351 return super.visitAssignmentExpression(node);
352 }
353
354 @override
355 Object visitBinaryExpression(BinaryExpression node) {
356 _recordOperatorReference(node.operator, node.bestElement);
357 return super.visitBinaryExpression(node);
358 }
359
360 @override
361 Object visitClassDeclaration(ClassDeclaration node) {
362 ClassElement element = node.element;
363 enterScope(element);
364 try {
365 _recordElementDefinition(element, IndexConstants.DEFINES_CLASS);
366 {
367 ExtendsClause extendsClause = node.extendsClause;
368 if (extendsClause != null) {
369 TypeName superclassNode = extendsClause.superclass;
370 _recordSuperType(superclassNode, IndexConstants.IS_EXTENDED_BY);
371 } else {
372 InterfaceType superType = element.supertype;
373 if (superType != null) {
374 ClassElement objectElement = superType.element;
375 recordRelationship(objectElement, IndexConstants.IS_EXTENDED_BY,
376 _createLocationFromOffset(node.name.offset, 0));
377 }
378 }
379 }
380 {
381 WithClause withClause = node.withClause;
382 if (withClause != null) {
383 for (TypeName mixinNode in withClause.mixinTypes) {
384 _recordSuperType(mixinNode, IndexConstants.IS_MIXED_IN_BY);
385 }
386 }
387 }
388 {
389 ImplementsClause implementsClause = node.implementsClause;
390 if (implementsClause != null) {
391 for (TypeName interfaceNode in implementsClause.interfaces) {
392 _recordSuperType(interfaceNode, IndexConstants.IS_IMPLEMENTED_BY);
393 }
394 }
395 }
396 return super.visitClassDeclaration(node);
397 } finally {
398 _exitScope();
399 }
400 }
401
402 @override
403 Object visitClassTypeAlias(ClassTypeAlias node) {
404 ClassElement element = node.element;
405 enterScope(element);
406 try {
407 _recordElementDefinition(element, IndexConstants.DEFINES_CLASS_ALIAS);
408 {
409 TypeName superclassNode = node.superclass;
410 if (superclassNode != null) {
411 _recordSuperType(superclassNode, IndexConstants.IS_EXTENDED_BY);
412 }
413 }
414 {
415 WithClause withClause = node.withClause;
416 if (withClause != null) {
417 for (TypeName mixinNode in withClause.mixinTypes) {
418 _recordSuperType(mixinNode, IndexConstants.IS_MIXED_IN_BY);
419 }
420 }
421 }
422 {
423 ImplementsClause implementsClause = node.implementsClause;
424 if (implementsClause != null) {
425 for (TypeName interfaceNode in implementsClause.interfaces) {
426 _recordSuperType(interfaceNode, IndexConstants.IS_IMPLEMENTED_BY);
427 }
428 }
429 }
430 return super.visitClassTypeAlias(node);
431 } finally {
432 _exitScope();
433 }
434 }
435
436 @override
437 Object visitCompilationUnit(CompilationUnit node) {
438 CompilationUnitElement unitElement = node.element;
439 if (unitElement != null) {
440 _elementStack.add(unitElement);
441 _libraryElement = unitElement.enclosingElement;
442 if (_libraryElement != null) {
443 return super.visitCompilationUnit(node);
444 }
445 }
446 return null;
447 }
448
449 @override
450 Object visitConstructorDeclaration(ConstructorDeclaration node) {
451 ConstructorElement element = node.element;
452 // define
453 {
454 Location location;
455 if (node.name != null) {
456 int start = node.period.offset;
457 int end = node.name.end;
458 location = _createLocationFromOffset(start, end - start);
459 } else {
460 int start = node.returnType.end;
461 location = _createLocationFromOffset(start, 0);
462 }
463 recordRelationship(element, IndexConstants.IS_DEFINED_BY, location);
464 }
465 // visit children
466 enterScope(element);
467 try {
468 return super.visitConstructorDeclaration(node);
469 } finally {
470 _exitScope();
471 }
472 }
473
474 @override
475 Object visitConstructorName(ConstructorName node) {
476 ConstructorElement element = node.staticElement;
477 // in 'class B = A;' actually A constructors are invoked
478 if (element != null && element.isSynthetic && element.redirectedConstructor
479 != null) {
480 element = element.redirectedConstructor;
481 }
482 // prepare location
483 Location location;
484 if (node.name != null) {
485 int start = node.period.offset;
486 int end = node.name.end;
487 location = _createLocationFromOffset(start, end - start);
488 } else {
489 int start = node.type.end;
490 location = _createLocationFromOffset(start, 0);
491 }
492 // record relationship
493 recordRelationship(element, IndexConstants.IS_REFERENCED_BY, location);
494 return super.visitConstructorName(node);
495 }
496
497 @override
498 Object visitExportDirective(ExportDirective node) {
499 ExportElement element = node.element;
500 if (element != null) {
501 LibraryElement expLibrary = element.exportedLibrary;
502 _recordLibraryReference(node, expLibrary);
503 }
504 return super.visitExportDirective(node);
505 }
506
507 @override
508 Object visitFormalParameter(FormalParameter node) {
509 ParameterElement element = node.element;
510 enterScope(element);
511 try {
512 return super.visitFormalParameter(node);
513 } finally {
514 _exitScope();
515 }
516 }
517
518 @override
519 Object visitFunctionDeclaration(FunctionDeclaration node) {
520 Element element = node.element;
521 _recordElementDefinition(element, IndexConstants.DEFINES_FUNCTION);
522 enterScope(element);
523 try {
524 return super.visitFunctionDeclaration(node);
525 } finally {
526 _exitScope();
527 }
528 }
529
530 @override
531 Object visitFunctionTypeAlias(FunctionTypeAlias node) {
532 Element element = node.element;
533 _recordElementDefinition(element, IndexConstants.DEFINES_FUNCTION_TYPE);
534 return super.visitFunctionTypeAlias(node);
535 }
536
537 @override
538 Object visitImportDirective(ImportDirective node) {
539 ImportElement element = node.element;
540 if (element != null) {
541 LibraryElement impLibrary = element.importedLibrary;
542 _recordLibraryReference(node, impLibrary);
543 }
544 return super.visitImportDirective(node);
545 }
546
547 @override
548 Object visitIndexExpression(IndexExpression node) {
549 MethodElement element = node.bestElement;
550 if (element is MethodElement) {
551 Token operator = node.leftBracket;
552 Location location = _createLocationFromToken(operator);
553 recordRelationship(element, IndexConstants.IS_INVOKED_BY_QUALIFIED,
554 location);
555 }
556 return super.visitIndexExpression(node);
557 }
558
559 @override
560 Object visitMethodDeclaration(MethodDeclaration node) {
561 ExecutableElement element = node.element;
562 enterScope(element);
563 try {
564 return super.visitMethodDeclaration(node);
565 } finally {
566 _exitScope();
567 }
568 }
569
570 @override
571 Object visitMethodInvocation(MethodInvocation node) {
572 SimpleIdentifier name = node.methodName;
573 Element element = name.bestElement;
574 if (element is MethodElement || element is PropertyAccessorElement) {
575 Location location = _createLocationFromNode(name);
576 Relationship relationship;
577 if (node.target != null) {
578 relationship = IndexConstants.IS_INVOKED_BY_QUALIFIED;
579 } else {
580 relationship = IndexConstants.IS_INVOKED_BY_UNQUALIFIED;
581 }
582 recordRelationship(element, relationship, location);
583 }
584 if (element is FunctionElement || element is VariableElement) {
585 Location location = _createLocationFromNode(name);
586 recordRelationship(element, IndexConstants.IS_INVOKED_BY, location);
587 }
588 // name invocation
589 {
590 Element nameElement = new NameElement(name.name);
591 Location location = _createLocationFromNode(name);
592 Relationship kind = element != null ?
593 IndexConstants.NAME_IS_INVOKED_BY_RESOLVED :
594 IndexConstants.NAME_IS_INVOKED_BY_UNRESOLVED;
595 _store.recordRelationship(nameElement, kind, location);
596 }
597 _recordImportElementReferenceWithoutPrefix(name);
598 return super.visitMethodInvocation(node);
599 }
600
601 @override
602 Object visitPartDirective(PartDirective node) {
603 Element element = node.element;
604 Location location = _createLocationFromNode(node.uri);
605 recordRelationship(element, IndexConstants.IS_REFERENCED_BY, location);
606 return super.visitPartDirective(node);
607 }
608
609 @override
610 Object visitPartOfDirective(PartOfDirective node) {
611 Location location = _createLocationFromNode(node.libraryName);
612 recordRelationship(node.element, IndexConstants.IS_REFERENCED_BY, location);
613 return null;
614 }
615
616 @override
617 Object visitPostfixExpression(PostfixExpression node) {
618 _recordOperatorReference(node.operator, node.bestElement);
619 return super.visitPostfixExpression(node);
620 }
621
622 @override
623 Object visitPrefixExpression(PrefixExpression node) {
624 _recordOperatorReference(node.operator, node.bestElement);
625 return super.visitPrefixExpression(node);
626 }
627
628 @override
629 Object visitSimpleIdentifier(SimpleIdentifier node) {
630 Element nameElement = new NameElement(node.name);
631 Location location = _createLocationFromNode(node);
632 // name in declaration
633 if (node.inDeclarationContext()) {
634 recordRelationship(nameElement, IndexConstants.IS_DEFINED_BY, location);
635 return null;
636 }
637 // prepare information
638 Element element = node.bestElement;
639 // qualified name reference
640 _recordQualifiedMemberReference(node, element, nameElement, location);
641 // stop if already handled
642 if (_isAlreadyHandledName(node)) {
643 return null;
644 }
645 // record name read/write
646 {
647 bool inGetterContext = node.inGetterContext();
648 bool inSetterContext = node.inSetterContext();
649 if (inGetterContext && inSetterContext) {
650 Relationship kind = element != null ?
651 IndexConstants.NAME_IS_READ_WRITTEN_BY_RESOLVED :
652 IndexConstants.NAME_IS_READ_WRITTEN_BY_UNRESOLVED;
653 _store.recordRelationship(nameElement, kind, location);
654 } else if (inGetterContext) {
655 Relationship kind = element != null ?
656 IndexConstants.NAME_IS_READ_BY_RESOLVED :
657 IndexConstants.NAME_IS_READ_BY_UNRESOLVED;
658 _store.recordRelationship(nameElement, kind, location);
659 } else if (inSetterContext) {
660 Relationship kind = element != null ?
661 IndexConstants.NAME_IS_WRITTEN_BY_RESOLVED :
662 IndexConstants.NAME_IS_WRITTEN_BY_UNRESOLVED;
663 _store.recordRelationship(nameElement, kind, location);
664 }
665 }
666 // record specific relations
667 if (element is ClassElement || element is FunctionElement || element is
668 FunctionTypeAliasElement || element is LabelElement || element is
669 TypeParameterElement) {
670 recordRelationship(element, IndexConstants.IS_REFERENCED_BY, location);
671 } else if (element is FieldElement) {
672 location = _getLocationWithInitializerType(node, location);
673 recordRelationship(element, IndexConstants.IS_REFERENCED_BY, location);
674 } else if (element is FieldFormalParameterElement) {
675 FieldFormalParameterElement fieldParameter = element;
676 FieldElement field = fieldParameter.field;
677 recordRelationship(field, IndexConstants.IS_REFERENCED_BY_QUALIFIED,
678 location);
679 } else if (element is PrefixElement) {
680 _recordImportElementReferenceWithPrefix(node);
681 } else if (element is PropertyAccessorElement || element is MethodElement) {
682 location = _getLocationWithTypeAssignedToField(node, element, location);
683 if (node.isQualified) {
684 recordRelationship(element, IndexConstants.IS_REFERENCED_BY_QUALIFIED,
685 location);
686 } else {
687 recordRelationship(element, IndexConstants.IS_REFERENCED_BY_UNQUALIFIED,
688 location);
689 }
690 } else if (element is ParameterElement || element is LocalVariableElement) {
691 bool inGetterContext = node.inGetterContext();
692 bool inSetterContext = node.inSetterContext();
693 if (inGetterContext && inSetterContext) {
694 recordRelationship(element, IndexConstants.IS_READ_WRITTEN_BY,
695 location);
696 } else if (inGetterContext) {
697 recordRelationship(element, IndexConstants.IS_READ_BY, location);
698 } else if (inSetterContext) {
699 recordRelationship(element, IndexConstants.IS_WRITTEN_BY, location);
700 } else {
701 recordRelationship(element, IndexConstants.IS_REFERENCED_BY, location);
702 }
703 }
704 _recordImportElementReferenceWithoutPrefix(node);
705 return super.visitSimpleIdentifier(node);
706 }
707
708 @override
709 Object visitSuperConstructorInvocation(SuperConstructorInvocation node) {
710 ConstructorElement element = node.staticElement;
711 Location location;
712 if (node.constructorName != null) {
713 int start = node.period.offset;
714 int end = node.constructorName.end;
715 location = _createLocationFromOffset(start, end - start);
716 } else {
717 int start = node.keyword.end;
718 location = _createLocationFromOffset(start, 0);
719 }
720 recordRelationship(element, IndexConstants.IS_REFERENCED_BY, location);
721 return super.visitSuperConstructorInvocation(node);
722 }
723
724 @override
725 Object visitTopLevelVariableDeclaration(TopLevelVariableDeclaration node) {
726 VariableDeclarationList variables = node.variables;
727 for (VariableDeclaration variableDeclaration in variables.variables) {
728 Element element = variableDeclaration.element;
729 _recordElementDefinition(element, IndexConstants.DEFINES_VARIABLE);
730 }
731 return super.visitTopLevelVariableDeclaration(node);
732 }
733
734 @override
735 Object visitTypeParameter(TypeParameter node) {
736 TypeParameterElement element = node.element;
737 enterScope(element);
738 try {
739 return super.visitTypeParameter(node);
740 } finally {
741 _exitScope();
742 }
743 }
744
745 @override
746 Object visitVariableDeclaration(VariableDeclaration node) {
747 VariableElement element = node.element;
748 // record declaration
749 {
750 SimpleIdentifier name = node.name;
751 Location location = _createLocationFromNode(name);
752 location = _getLocationWithExpressionType(location, node.initializer);
753 recordRelationship(element, IndexConstants.IS_DEFINED_BY, location);
754 }
755 // visit
756 enterScope(element);
757 try {
758 return super.visitVariableDeclaration(node);
759 } finally {
760 _exitScope();
761 }
762 }
763
764 @override
765 Object visitVariableDeclarationList(VariableDeclarationList node) {
766 NodeList<VariableDeclaration> variables = node.variables;
767 if (variables != null) {
768 // use first VariableDeclaration as Element for Location(s) in type
769 {
770 TypeName type = node.type;
771 if (type != null) {
772 for (VariableDeclaration variableDeclaration in variables) {
773 enterScope(variableDeclaration.element);
774 try {
775 type.accept(this);
776 } finally {
777 _exitScope();
778 }
779 // only one iteration
780 break;
781 }
782 }
783 }
784 // visit variables
785 variables.accept(this);
786 }
787 return null;
788 }
789
790 /**
791 * @return the [Location] representing location of the [AstNode].
792 */
793 Location _createLocationFromNode(AstNode node) => _createLocationFromOffset(
794 node.offset, node.length);
795
796 /**
797 * [offset] - the offset of the location within [Source].
798 * [length] - the length of the location.
799 *
800 * Returns the [Location] representing the given offset and length within the
801 * inner-most [Element].
802 */
803 Location _createLocationFromOffset(int offset, int length) {
804 Element element = peekElement();
805 return new Location(element, offset, length);
806 }
807
808 /**
809 * @return the [Location] representing location of the [Token].
810 */
811 Location _createLocationFromToken(Token token) => _createLocationFromOffset(
812 token.offset, token.length);
813
814 /**
815 * Exit the current scope.
816 */
817 void _exitScope() {
818 _elementStack.removeFirst();
819 }
820
821 /**
822 * @return `true` if given node already indexed as more interesting reference, so it should
823 * not be indexed again.
824 */
825 bool _isAlreadyHandledName(SimpleIdentifier node) {
826 AstNode parent = node.parent;
827 if (parent is MethodInvocation) {
828 return identical(parent.methodName, node);
829 }
830 return false;
831 }
832
833 /**
834 * Records the [Element] definition in the library and universe.
835 */
836 void _recordElementDefinition(Element element, Relationship relationship) {
837 Location location = createLocation(element);
838 recordRelationship(_libraryElement, relationship, location);
839 recordRelationship(IndexConstants.UNIVERSE, relationship, location);
840 }
841
842 /**
843 * Records [ImportElement] that declares given prefix and imports library with element used
844 * with given prefix node.
845 */
846 void _recordImportElementReferenceWithPrefix(SimpleIdentifier prefixNode) {
847 _ImportElementInfo info = getImportElementInfo(prefixNode);
848 if (info != null) {
849 int offset = prefixNode.offset;
850 int length = info._periodEnd - offset;
851 Location location = _createLocationFromOffset(offset, length);
852 recordRelationship(info._element, IndexConstants.IS_REFERENCED_BY,
853 location);
854 }
855 }
856
857 /**
858 * Records [ImportElement] reference if given [SimpleIdentifier] references so me
859 * top-level element and not qualified with import prefix.
860 */
861 void _recordImportElementReferenceWithoutPrefix(SimpleIdentifier node) {
862 if (_isIdentifierInImportCombinator(node)) {
863 return;
864 }
865 if (_isIdentifierInPrefixedIdentifier(node)) {
866 return;
867 }
868 Element element = node.staticElement;
869 ImportElement importElement = _internalGetImportElement(_libraryElement,
870 null, element, _importElementsMap);
871 if (importElement != null) {
872 Location location = _createLocationFromOffset(node.offset, 0);
873 recordRelationship(importElement, IndexConstants.IS_REFERENCED_BY,
874 location);
875 }
876 }
877
878 /**
879 * Records reference to defining [CompilationUnitElement] of the given
880 * [LibraryElement].
881 */
882 void _recordLibraryReference(UriBasedDirective node, LibraryElement library) {
883 if (library != null) {
884 Location location = _createLocationFromNode(node.uri);
885 recordRelationship(library.definingCompilationUnit,
886 IndexConstants.IS_REFERENCED_BY, location);
887 }
888 }
889
890 /**
891 * Record reference to the given operator [Element] and name.
892 */
893 void _recordOperatorReference(Token operator, Element element) {
894 // prepare location
895 Location location = _createLocationFromToken(operator);
896 // record name reference
897 {
898 String name = operator.lexeme;
899 if (name == "++") {
900 name = "+";
901 }
902 if (name == "--") {
903 name = "-";
904 }
905 if (StringUtilities.endsWithChar(name, 0x3D) && name != "==") {
906 name = name.substring(0, name.length - 1);
907 }
908 Element nameElement = new NameElement(name);
909 Relationship relationship = element != null ?
910 IndexConstants.IS_REFERENCED_BY_QUALIFIED_RESOLVED :
911 IndexConstants.IS_REFERENCED_BY_QUALIFIED_UNRESOLVED;
912 recordRelationship(nameElement, relationship, location);
913 }
914 // record element reference
915 if (element != null) {
916 recordRelationship(element, IndexConstants.IS_INVOKED_BY_QUALIFIED,
917 location);
918 }
919 }
920
921 /**
922 * Records reference if the given [SimpleIdentifier] looks like a qualified pr operty access
923 * or method invocation.
924 */
925 void _recordQualifiedMemberReference(SimpleIdentifier node, Element element,
926 Element nameElement, Location location) {
927 if (node.isQualified) {
928 Relationship relationship = element != null ?
929 IndexConstants.IS_REFERENCED_BY_QUALIFIED_RESOLVED :
930 IndexConstants.IS_REFERENCED_BY_QUALIFIED_UNRESOLVED;
931 recordRelationship(nameElement, relationship, location);
932 }
933 }
934
935 /**
936 * Records extends/implements relationships between given [ClassElement] and [ Type] of
937 * "superNode".
938 */
939 void _recordSuperType(TypeName superNode, Relationship relationship) {
940 if (superNode != null) {
941 Identifier superName = superNode.name;
942 if (superName != null) {
943 Element superElement = superName.staticElement;
944 recordRelationship(superElement, relationship, _createLocationFromNode(
945 superNode));
946 }
947 }
948 }
949
950 /**
951 * @return the [Location] representing location of the [Element].
952 */
953 static Location createLocation(Element element) {
954 if (element != null) {
955 int offset = element.nameOffset;
956 int length = element.displayName.length;
957 return new Location(element, offset, length);
958 }
959 return null;
960 }
961
962 /**
963 * @return the [ImportElement] that is referenced by this node with [PrefixEle ment],
964 * may be `null`.
965 */
966 static ImportElement getImportElement(SimpleIdentifier prefixNode) {
967 _ImportElementInfo info = getImportElementInfo(prefixNode);
968 return info != null ? info._element : null;
969 }
970
971 /**
972 * @return the [ImportElementInfo] with [ImportElement] that is referenced by this
973 * node with [PrefixElement], may be `null`.
974 */
975 static _ImportElementInfo getImportElementInfo(SimpleIdentifier prefixNode) {
976 _ImportElementInfo info = new _ImportElementInfo();
977 // prepare environment
978 AstNode parent = prefixNode.parent;
979 CompilationUnit unit = prefixNode.getAncestor((node) => node is
980 CompilationUnit);
981 LibraryElement libraryElement = unit.element.library;
982 // prepare used element
983 Element usedElement = null;
984 if (parent is PrefixedIdentifier) {
985 PrefixedIdentifier prefixed = parent;
986 if (identical(prefixed.prefix, prefixNode)) {
987 usedElement = prefixed.staticElement;
988 info._periodEnd = prefixed.period.end;
989 }
990 }
991 if (parent is MethodInvocation) {
992 MethodInvocation invocation = parent;
993 if (identical(invocation.target, prefixNode)) {
994 usedElement = invocation.methodName.staticElement;
995 info._periodEnd = invocation.period.end;
996 }
997 }
998 // we need used Element
999 if (usedElement == null) {
1000 return null;
1001 }
1002 // find ImportElement
1003 String prefix = prefixNode.name;
1004 Map<ImportElement, Set<Element>> importElementsMap = {};
1005 info._element = _internalGetImportElement(libraryElement, prefix,
1006 usedElement, importElementsMap);
1007 if (info._element == null) {
1008 return null;
1009 }
1010 return info;
1011 }
1012
1013 /**
1014 * If the given expression has resolved type, returns the new location with th is type.
1015 *
1016 * [location] - the base location
1017 * [expression] - the expression assigned at the given location
1018 */
1019 static Location _getLocationWithExpressionType(Location location,
1020 Expression expression) {
1021 if (expression != null) {
1022 return new LocationWithData<DartType>(location, expression.bestType);
1023 }
1024 return location;
1025 }
1026
1027 /**
1028 * If the given node is the part of the [ConstructorFieldInitializer], returns location with
1029 * type of the initializer expression.
1030 */
1031 static Location _getLocationWithInitializerType(SimpleIdentifier node,
1032 Location location) {
1033 if (node.parent is ConstructorFieldInitializer) {
1034 ConstructorFieldInitializer initializer = node.parent as
1035 ConstructorFieldInitializer;
1036 if (identical(initializer.fieldName, node)) {
1037 location = _getLocationWithExpressionType(location,
1038 initializer.expression);
1039 }
1040 }
1041 return location;
1042 }
1043
1044 /**
1045 * If the given identifier has a synthetic [PropertyAccessorElement], i.e.
1046 * accessor for normal field, and it is LHS of assignment, then include [Type]
1047 * of the assigned value into the [Location].
1048 *
1049 * [identifier] - the identifier to record location.
1050 * [element] - the [Element] of the identifier.
1051 * [location] - the raw location
1052 *
1053 * Returns the [Location] with the type of the assigned value
1054 */
1055 static Location
1056 _getLocationWithTypeAssignedToField(SimpleIdentifier identifier,
1057 Element element, Location location) {
1058 // we need accessor
1059 if (element is! PropertyAccessorElement) {
1060 return location;
1061 }
1062 PropertyAccessorElement accessor = element as PropertyAccessorElement;
1063 // should be setter
1064 if (!accessor.isSetter) {
1065 return location;
1066 }
1067 // accessor should be synthetic, i.e. field normal
1068 if (!accessor.isSynthetic) {
1069 return location;
1070 }
1071 // should be LHS of assignment
1072 AstNode parent;
1073 {
1074 AstNode node = identifier;
1075 parent = node.parent;
1076 // new T().field = x;
1077 if (parent is PropertyAccess) {
1078 PropertyAccess propertyAccess = parent as PropertyAccess;
1079 if (identical(propertyAccess.propertyName, node)) {
1080 node = propertyAccess;
1081 parent = propertyAccess.parent;
1082 }
1083 }
1084 // obj.field = x;
1085 if (parent is PrefixedIdentifier) {
1086 PrefixedIdentifier prefixedIdentifier = parent as PrefixedIdentifier;
1087 if (identical(prefixedIdentifier.identifier, node)) {
1088 node = prefixedIdentifier;
1089 parent = prefixedIdentifier.parent;
1090 }
1091 }
1092 }
1093 // OK, remember the type
1094 if (parent is AssignmentExpression) {
1095 AssignmentExpression assignment = parent as AssignmentExpression;
1096 Expression rhs = assignment.rightHandSide;
1097 location = _getLocationWithExpressionType(location, rhs);
1098 }
1099 // done
1100 return location;
1101 }
1102
1103 /**
1104 * @return the [ImportElement] that declares given [PrefixElement] and imports library
1105 * with given "usedElement".
1106 */
1107 static ImportElement _internalGetImportElement(LibraryElement libraryElement,
1108 String prefix, Element usedElement, Map<ImportElement,
1109 Set<Element>> importElementsMap) {
1110 // validate Element
1111 if (usedElement == null) {
1112 return null;
1113 }
1114 if (usedElement.enclosingElement is! CompilationUnitElement) {
1115 return null;
1116 }
1117 LibraryElement usedLibrary = usedElement.library;
1118 // find ImportElement that imports used library with used prefix
1119 List<ImportElement> candidates = null;
1120 for (ImportElement importElement in libraryElement.imports) {
1121 // required library
1122 if (importElement.importedLibrary != usedLibrary) {
1123 continue;
1124 }
1125 // required prefix
1126 PrefixElement prefixElement = importElement.prefix;
1127 if (prefix == null) {
1128 if (prefixElement != null) {
1129 continue;
1130 }
1131 } else {
1132 if (prefixElement == null) {
1133 continue;
1134 }
1135 if (prefix != prefixElement.name) {
1136 continue;
1137 }
1138 }
1139 // no combinators => only possible candidate
1140 if (importElement.combinators.length == 0) {
1141 return importElement;
1142 }
1143 // OK, we have candidate
1144 if (candidates == null) {
1145 candidates = [];
1146 }
1147 candidates.add(importElement);
1148 }
1149 // no candidates, probably element is defined in this library
1150 if (candidates == null) {
1151 return null;
1152 }
1153 // one candidate
1154 if (candidates.length == 1) {
1155 return candidates[0];
1156 }
1157 // ensure that each ImportElement has set of elements
1158 for (ImportElement importElement in candidates) {
1159 if (importElementsMap.containsKey(importElement)) {
1160 continue;
1161 }
1162 Namespace namespace = new NamespaceBuilder(
1163 ).createImportNamespaceForDirective(importElement);
1164 Set<Element> elements = new Set();
1165 importElementsMap[importElement] = elements;
1166 }
1167 // use import namespace to choose correct one
1168 for (MapEntry<ImportElement, Set<Element>> entry in getMapEntrySet(
1169 importElementsMap)) {
1170 if (entry.getValue().contains(usedElement)) {
1171 return entry.getKey();
1172 }
1173 }
1174 // not found
1175 return null;
1176 }
1177
1178 /**
1179 * @return `true` if given "node" is part of an import [Combinator].
1180 */
1181 static bool _isIdentifierInImportCombinator(SimpleIdentifier node) {
1182 AstNode parent = node.parent;
1183 return parent is Combinator;
1184 }
1185
1186 /**
1187 * @return `true` if given "node" is part of [PrefixedIdentifier] "prefix.node ".
1188 */
1189 static bool _isIdentifierInPrefixedIdentifier(SimpleIdentifier node) {
1190 AstNode parent = node.parent;
1191 return parent is PrefixedIdentifier && identical(parent.identifier, node);
1192 }
1193 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698