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

Side by Side Diff: pkg/front_end/lib/src/dependency_grapher_impl.dart

Issue 2617483007: Extract code from dependency_grapher.dart for easier re-use elsewhere in the front end. (Closed)
Patch Set: Created 3 years, 11 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
« no previous file with comments | « pkg/front_end/lib/dependency_grapher.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2017, 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 6
7 import 'package:analyzer/dart/ast/ast.dart'; 7 import 'package:analyzer/dart/ast/ast.dart';
8 import 'package:analyzer/error/listener.dart'; 8 import 'package:analyzer/error/listener.dart';
9 import 'package:analyzer/src/dart/scanner/reader.dart'; 9 import 'package:analyzer/src/dart/scanner/reader.dart';
10 import 'package:analyzer/src/generated/parser.dart'; 10 import 'package:analyzer/src/generated/parser.dart';
11 import 'package:front_end/dependency_grapher.dart';
11 import 'package:front_end/file_system.dart'; 12 import 'package:front_end/file_system.dart';
12 import 'package:front_end/src/async_dependency_walker.dart'; 13 import 'package:front_end/src/async_dependency_walker.dart';
13 import 'package:front_end/src/base/processed_options.dart'; 14 import 'package:front_end/src/base/processed_options.dart';
14 import 'package:front_end/src/base/uri_resolver.dart'; 15 import 'package:front_end/src/base/uri_resolver.dart';
15 import 'package:front_end/src/scanner/scanner.dart'; 16 import 'package:front_end/src/scanner/scanner.dart';
16 17
17 import 'compiler_options.dart';
18
19 /// Generates a representation of the dependency graph of a program. 18 /// Generates a representation of the dependency graph of a program.
20 /// 19 ///
21 /// Given the Uri of one or more files, this function follows `import`, 20 /// Given the Uri of one or more files, this function follows `import`,
22 /// `export`, and `part` declarations to discover a graph of all files involved 21 /// `export`, and `part` declarations to discover a graph of all files involved
23 /// in the program. 22 /// in the program.
23 ///
24 /// This is intended for internal use by the front end. Clients should use
25 /// package:front_end/dependency_grapher.dart.
24 Future<Graph> graphForProgram( 26 Future<Graph> graphForProgram(
25 List<Uri> sources, CompilerOptions options) async { 27 List<Uri> sources, ProcessedOptions options) async {
26 var processedOptions = new ProcessedOptions(options); 28 var uriResolver = await options.getUriResolver();
27 var uriResolver = await processedOptions.getUriResolver(); 29 var walker = new _Walker(options.fileSystem, uriResolver, options.compileSdk);
28 var walker = new _Walker(processedOptions.fileSystem, uriResolver, processedOp tions.compileSdk);
29 var startingPoint = new _StartingPoint(walker, sources); 30 var startingPoint = new _StartingPoint(walker, sources);
30 await walker.walk(startingPoint); 31 await walker.walk(startingPoint);
31 return walker.graph; 32 return walker.graph;
32 } 33 }
33 34
34 /// A representation of the dependency graph of a program.
35 ///
36 /// Not intended to be extended, implemented, or mixed in by clients.
37 class Graph {
38 /// A list of all library cycles in the program, in topologically sorted order
39 /// (each cycle only depends on libraries in the cycles that precede it).
40 final topologicallySortedCycles = <LibraryCycleNode>[];
41
42 Graph._();
43 }
44
45 /// A representation of a single library cycle in the dependency graph of a
46 /// program.
47 ///
48 /// Not intended to be extended, implemented, or mixed in by clients.
49 class LibraryCycleNode {
50 /// A map of all the libraries in the cycle, keyed by the URI of their
51 /// defining compilation unit.
52 final libraries = <Uri, LibraryNode>{};
53
54 LibraryCycleNode._();
55 }
56
57 /// A representation of a single library in the dependency graph of a program.
58 ///
59 /// Not intended to be extended, implemented, or mixed in by clients.
60 class LibraryNode {
61 /// The URI of this library's defining compilation unit.
62 final Uri uri;
63
64 /// A list of the URIs of all of this library's "part" files.
65 final parts = <Uri>[];
66
67 /// A list of all the other libraries this library directly depends on.
68 final dependencies = <LibraryNode>[];
69
70 LibraryNode._(this.uri);
71 }
72
73 class _Scanner extends Scanner { 35 class _Scanner extends Scanner {
74 _Scanner(String contents) : super(new CharSequenceReader(contents)) { 36 _Scanner(String contents) : super(new CharSequenceReader(contents)) {
75 preserveComments = false; 37 preserveComments = false;
76 } 38 }
77 39
78 @override 40 @override
79 void reportError(errorCode, int offset, List<Object> arguments) { 41 void reportError(errorCode, int offset, List<Object> arguments) {
80 // TODO(paulberry): report errors. 42 // TODO(paulberry): report errors.
81 } 43 }
82 } 44 }
83 45
84 class _StartingPoint extends _WalkerNode { 46 class _StartingPoint extends _WalkerNode {
85 final List<Uri> sources; 47 final List<Uri> sources;
86 48
87 _StartingPoint(_Walker walker, this.sources) : super(walker, null); 49 _StartingPoint(_Walker walker, this.sources) : super(walker, null);
88 50
89 @override 51 @override
90 Future<List<_WalkerNode>> computeDependencies() async => 52 Future<List<_WalkerNode>> computeDependencies() async =>
91 sources.map(walker.nodeForUri).toList(); 53 sources.map(walker.nodeForUri).toList();
92 } 54 }
93 55
94 class _Walker extends AsyncDependencyWalker<_WalkerNode> { 56 class _Walker extends AsyncDependencyWalker<_WalkerNode> {
95 final FileSystem fileSystem; 57 final FileSystem fileSystem;
96 final UriResolver uriResolver; 58 final UriResolver uriResolver;
97 final _nodesByUri = <Uri, _WalkerNode>{}; 59 final _nodesByUri = <Uri, _WalkerNode>{};
98 final graph = new Graph._(); 60 final graph = new Graph();
99 final bool compileSdk; 61 final bool compileSdk;
100 62
101 _Walker(this.fileSystem, this.uriResolver, this.compileSdk); 63 _Walker(this.fileSystem, this.uriResolver, this.compileSdk);
102 64
103 @override 65 @override
104 Future<Null> evaluate(_WalkerNode v) { 66 Future<Null> evaluate(_WalkerNode v) {
105 if (v is _StartingPoint) return new Future.value(); 67 if (v is _StartingPoint) return new Future.value();
106 return evaluateScc([v]); 68 return evaluateScc([v]);
107 } 69 }
108 70
109 @override 71 @override
110 Future<Null> evaluateScc(List<_WalkerNode> scc) { 72 Future<Null> evaluateScc(List<_WalkerNode> scc) {
111 var cycle = new LibraryCycleNode._(); 73 var cycle = new LibraryCycleNode();
112 for (var walkerNode in scc) { 74 for (var walkerNode in scc) {
113 cycle.libraries[walkerNode.uri] = walkerNode.library; 75 cycle.libraries[walkerNode.uri] = walkerNode.library;
114 } 76 }
115 graph.topologicallySortedCycles.add(cycle); 77 graph.topologicallySortedCycles.add(cycle);
116 return new Future.value(); 78 return new Future.value();
117 } 79 }
118 80
119 _WalkerNode nodeForUri(Uri referencedUri) { 81 _WalkerNode nodeForUri(Uri referencedUri) {
120 var dependencyNode = _nodesByUri.putIfAbsent( 82 var dependencyNode = _nodesByUri.putIfAbsent(
121 referencedUri, () => new _WalkerNode(this, referencedUri)); 83 referencedUri, () => new _WalkerNode(this, referencedUri));
122 return dependencyNode; 84 return dependencyNode;
123 } 85 }
124 } 86 }
125 87
126 class _WalkerNode extends Node<_WalkerNode> { 88 class _WalkerNode extends Node<_WalkerNode> {
127 static final dartCoreUri = Uri.parse('dart:core'); 89 static final dartCoreUri = Uri.parse('dart:core');
128 final _Walker walker; 90 final _Walker walker;
129 final Uri uri; 91 final Uri uri;
130 final LibraryNode library; 92 final LibraryNode library;
131 93
132 _WalkerNode(this.walker, Uri uri) 94 _WalkerNode(this.walker, Uri uri)
133 : uri = uri, 95 : uri = uri,
134 library = new LibraryNode._(uri); 96 library = new LibraryNode(uri);
135 97
136 @override 98 @override
137 Future<List<_WalkerNode>> computeDependencies() async { 99 Future<List<_WalkerNode>> computeDependencies() async {
138 var dependencies = <_WalkerNode>[]; 100 var dependencies = <_WalkerNode>[];
139 // TODO(paulberry): add error recovery if the file can't be read. 101 // TODO(paulberry): add error recovery if the file can't be read.
140 var path = walker.uriResolver.resolve(uri); 102 var path = walker.uriResolver.resolve(uri);
141 if (path == null) { 103 if (path == null) {
142 // TODO(paulberry): If an error reporter was provided, report the error 104 // TODO(paulberry): If an error reporter was provided, report the error
143 // in the proper way and continue. 105 // in the proper way and continue.
144 throw new StateError('Invalid URI: $uri'); 106 throw new StateError('Invalid URI: $uri');
(...skipping 27 matching lines...) Expand all
172 handleDependency(referencedUri); 134 handleDependency(referencedUri);
173 } 135 }
174 } 136 }
175 } 137 }
176 if (!coreUriFound) { 138 if (!coreUriFound) {
177 handleDependency(dartCoreUri); 139 handleDependency(dartCoreUri);
178 } 140 }
179 return dependencies; 141 return dependencies;
180 } 142 }
181 } 143 }
OLDNEW
« no previous file with comments | « pkg/front_end/lib/dependency_grapher.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698