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

Side by Side Diff: pkg/analyzer/lib/src/dart/analysis/driver.dart

Issue 2691433008: AnalysisDriver.getUnitElement() should return a structure with a key. (Closed)
Patch Set: Created 3 years, 10 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 'dart:async'; 5 import 'dart:async';
6 import 'dart:collection'; 6 import 'dart:collection';
7 import 'dart:typed_data'; 7 import 'dart:typed_data';
8 8
9 import 'package:analyzer/context/declared_variables.dart'; 9 import 'package:analyzer/context/declared_variables.dart';
10 import 'package:analyzer/dart/ast/ast.dart'; 10 import 'package:analyzer/dart/ast/ast.dart';
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 final _topLevelNameDeclarationsTasks = <_TopLevelNameDeclarationsTask>[]; 172 final _topLevelNameDeclarationsTasks = <_TopLevelNameDeclarationsTask>[];
173 173
174 /** 174 /**
175 * The mapping from the files for which the index was requested using 175 * The mapping from the files for which the index was requested using
176 * [getIndex] to the [Completer]s to report the result. 176 * [getIndex] to the [Completer]s to report the result.
177 */ 177 */
178 final _indexRequestedFiles = 178 final _indexRequestedFiles =
179 <String, List<Completer<AnalysisDriverUnitIndex>>>{}; 179 <String, List<Completer<AnalysisDriverUnitIndex>>>{};
180 180
181 /** 181 /**
182 * The mapping from the files for which the index was requested using 182 * The mapping from the files for which the unit element was requested using
183 * [getIndex] to the [Completer]s to report the result. 183 * [getUnitElement] to the [Completer]s to report the result.
184 */ 184 */
185 final _unitElementRequestedFiles = 185 final _unitElementRequestedFiles =
186 <String, List<Completer<CompilationUnitElement>>>{}; 186 <String, List<Completer<UnitElementResult>>>{};
187 187
188 /** 188 /**
189 * The mapping from the files for which analysis was requested using 189 * The mapping from the files for which analysis was requested using
190 * [getResult], and which were found to be parts without known libraries, 190 * [getResult], and which were found to be parts without known libraries,
191 * to the [Completer]s to report the result. 191 * to the [Completer]s to report the result.
192 */ 192 */
193 final _requestedParts = <String, List<Completer<AnalysisResult>>>{}; 193 final _requestedParts = <String, List<Completer<AnalysisResult>>>{};
194 194
195 /** 195 /**
196 * The set of part files that are currently scheduled for analysis. 196 * The set of part files that are currently scheduled for analysis.
(...skipping 393 matching lines...) Expand 10 before | Expand all | Expand 10 after
590 */ 590 */
591 Future<List<TopLevelDeclarationInSource>> getTopLevelNameDeclarations( 591 Future<List<TopLevelDeclarationInSource>> getTopLevelNameDeclarations(
592 String name) { 592 String name) {
593 var task = new _TopLevelNameDeclarationsTask(this, name); 593 var task = new _TopLevelNameDeclarationsTask(this, name);
594 _topLevelNameDeclarationsTasks.add(task); 594 _topLevelNameDeclarationsTasks.add(task);
595 _scheduler.notify(this); 595 _scheduler.notify(this);
596 return task.completer.future; 596 return task.completer.future;
597 } 597 }
598 598
599 /** 599 /**
600 * Return a [Future] that completes with the [CompilationUnitElement] for the 600 * Return a [Future] that completes with the [UnitElementResult] for the
601 * file with the given [path], or with `null` if the file cannot be analyzed. 601 * file with the given [path], or with `null` if the file cannot be analyzed.
602 */ 602 */
603 Future<CompilationUnitElement> getUnitElement(String path) { 603 Future<UnitElementResult> getUnitElement(String path) {
604 if (!_fileTracker.fsState.hasUri(path)) { 604 if (!_fileTracker.fsState.hasUri(path)) {
605 return new Future.value(); 605 return new Future.value();
606 } 606 }
607 var completer = new Completer<CompilationUnitElement>(); 607 var completer = new Completer<UnitElementResult>();
608 _unitElementRequestedFiles 608 _unitElementRequestedFiles
609 .putIfAbsent(path, () => <Completer<CompilationUnitElement>>[]) 609 .putIfAbsent(path, () => <Completer<UnitElementResult>>[])
610 .add(completer); 610 .add(completer);
611 _scheduler.notify(this); 611 _scheduler.notify(this);
612 return completer.future; 612 return completer.future;
613 } 613 }
614 614
615 /** 615 /**
616 * Return a [Future] that completes with a [ParseResult] for the file 616 * Return a [Future] that completes with a [ParseResult] for the file
617 * with the given [path]. 617 * with the given [path].
618 * 618 *
619 * The [path] must be absolute and normalized. 619 * The [path] must be absolute and normalized.
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
741 } 741 }
742 }); 742 });
743 } 743 }
744 744
745 AnalysisDriverUnitIndex _computeIndex(String path) { 745 AnalysisDriverUnitIndex _computeIndex(String path) {
746 AnalysisResult analysisResult = _computeAnalysisResult(path, 746 AnalysisResult analysisResult = _computeAnalysisResult(path,
747 withUnit: false, asIsIfPartWithoutLibrary: true); 747 withUnit: false, asIsIfPartWithoutLibrary: true);
748 return analysisResult._index; 748 return analysisResult._index;
749 } 749 }
750 750
751 CompilationUnitElement _computeUnitElement(String path) { 751 UnitElementResult _computeUnitElement(String path) {
752 FileState file = _fileTracker.fsState.getFileForPath(path); 752 FileState file = _fileTracker.fsState.getFileForPath(path);
753 FileState libraryFile = file.library ?? file; 753 FileState library = file.library ?? file;
754 754
755 // Create the AnalysisContext to resynthesize elements in. 755 // Create the AnalysisContext to resynthesize elements in.
756 LibraryContext libraryContext = _createLibraryContext(libraryFile); 756 LibraryContext libraryContext = _createLibraryContext(library);
757 757
758 // Resynthesize the CompilationUnitElement in the context. 758 // Resynthesize the CompilationUnitElement in the context.
759 try { 759 try {
760 return libraryContext.computeUnitElement(libraryFile.source, file.source); 760 CompilationUnitElement element =
761 libraryContext.computeUnitElement(library.source, file.source);
762 String key = _getResolvedUnitKey(library, file);
763 return new UnitElementResult(path, file.contentHash, key, element);
761 } finally { 764 } finally {
762 libraryContext.dispose(); 765 libraryContext.dispose();
763 } 766 }
764 } 767 }
765 768
766 /** 769 /**
767 * Creates a new [FileTracker] object and stores it in [_fileTracker]. 770 * Creates a new [FileTracker] object and stores it in [_fileTracker].
768 * 771 *
769 * This is used both on initial construction and whenever the configuration 772 * This is used both on initial construction and whenever the configuration
770 * changes. 773 * changes.
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
918 AnalysisDriverUnitIndex index = _computeIndex(path); 921 AnalysisDriverUnitIndex index = _computeIndex(path);
919 _indexRequestedFiles.remove(path).forEach((completer) { 922 _indexRequestedFiles.remove(path).forEach((completer) {
920 completer.complete(index); 923 completer.complete(index);
921 }); 924 });
922 return; 925 return;
923 } 926 }
924 927
925 // Process a unit request. 928 // Process a unit request.
926 if (_unitElementRequestedFiles.isNotEmpty) { 929 if (_unitElementRequestedFiles.isNotEmpty) {
927 String path = _unitElementRequestedFiles.keys.first; 930 String path = _unitElementRequestedFiles.keys.first;
928 CompilationUnitElement unitElement = _computeUnitElement(path); 931 UnitElementResult result = _computeUnitElement(path);
929 _unitElementRequestedFiles.remove(path).forEach((completer) { 932 _unitElementRequestedFiles.remove(path).forEach((completer) {
930 completer.complete(unitElement); 933 completer.complete(result);
931 }); 934 });
932 return; 935 return;
933 } 936 }
934 937
935 // Compute files defining a name. 938 // Compute files defining a name.
936 if (_definingClassMemberNameTasks.isNotEmpty) { 939 if (_definingClassMemberNameTasks.isNotEmpty) {
937 _FilesDefiningClassMemberNameTask task = 940 _FilesDefiningClassMemberNameTask task =
938 _definingClassMemberNameTasks.first; 941 _definingClassMemberNameTasks.first;
939 bool isDone = await task.perform(); 942 bool isDone = await task.perform();
940 if (isDone) { 943 if (isDone) {
(...skipping 636 matching lines...) Expand 10 before | Expand all | Expand 10 after
1577 */ 1580 */
1578 void exit() { 1581 void exit() {
1579 _timer.stop(); 1582 _timer.stop();
1580 _logger._level--; 1583 _logger._level--;
1581 int ms = _timer.elapsedMilliseconds; 1584 int ms = _timer.elapsedMilliseconds;
1582 _logger.writeln('--- $_msg in $ms ms.'); 1585 _logger.writeln('--- $_msg in $ms ms.');
1583 } 1586 }
1584 } 1587 }
1585 1588
1586 /** 1589 /**
1590 * The result with the [CompilationUnitElement] of a single file.
1591 *
1592 * These results are self-consistent, i.e. all elements and types accessible
1593 * through [element], including defined in other files, correspond to each
1594 * other. But none of the results is guaranteed to be consistent with the state
1595 * of the files.
1596 *
1597 * Every result is independent, and is not guaranteed to be consistent with
1598 * any previously returned result, even inside of the same library.
1599 */
1600 class UnitElementResult {
1601 /**
1602 * The path of the file, absolute and normalized.
1603 */
1604 final String path;
1605
1606 /**
1607 * The MD5 hash of the file content.
1608 */
1609 final String contentHash;
1610
1611 /**
1612 * The key of the [element] based on the transitive closure of files imported
1613 * and exported by the requested file.
1614 */
1615 final String key;
1616
1617 /**
1618 * The element of the file.
1619 */
1620 final CompilationUnitElement element;
1621
1622 UnitElementResult(this.path, this.contentHash, this.key, this.element);
1623 }
1624
1625 /**
1587 * Information about an exception and its context. 1626 * Information about an exception and its context.
1588 */ 1627 */
1589 class _ExceptionState { 1628 class _ExceptionState {
1590 final exception; 1629 final exception;
1591 final StackTrace stackTrace; 1630 final StackTrace stackTrace;
1592 1631
1593 /** 1632 /**
1594 * The key under which the context of the exception was stored, or `null` 1633 * The key under which the context of the exception was stored, or `null`
1595 * if unknown, the maximum number of context to store was reached, etc. 1634 * if unknown, the maximum number of context to store was reached, etc.
1596 */ 1635 */
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
1768 libraryDeclarations.add(new TopLevelDeclarationInSource( 1807 libraryDeclarations.add(new TopLevelDeclarationInSource(
1769 file.source, declaration, isExported)); 1808 file.source, declaration, isExported));
1770 } 1809 }
1771 } 1810 }
1772 } 1811 }
1773 1812
1774 // We're not done yet. 1813 // We're not done yet.
1775 return false; 1814 return false;
1776 } 1815 }
1777 } 1816 }
OLDNEW
« no previous file with comments | « pkg/analysis_server/test/src/utilities/change_builder_dart_test.dart ('k') | pkg/analyzer/lib/src/dart/analysis/search.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698