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

Side by Side Diff: pkg/code_transformers/lib/src/resolver_impl.dart

Issue 181383015: Refactoring resolvers to make them able to be run in parallel. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 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 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 library code_transformer.src.resolver_impl; 5 library code_transformer.src.resolver_impl;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 import 'package:analyzer/src/generated/ast.dart'; 8 import 'package:analyzer/src/generated/ast.dart';
9 import 'package:analyzer/src/generated/element.dart'; 9 import 'package:analyzer/src/generated/element.dart';
10 import 'package:analyzer/src/generated/engine.dart'; 10 import 'package:analyzer/src/generated/engine.dart';
(...skipping 28 matching lines...) Expand all
39 39
40 final AnalysisContext _context = 40 final AnalysisContext _context =
41 AnalysisEngine.instance.createAnalysisContext(); 41 AnalysisEngine.instance.createAnalysisContext();
42 42
43 /// Transform for which this is currently updating, or null when not updating. 43 /// Transform for which this is currently updating, or null when not updating.
44 Transform _currentTransform; 44 Transform _currentTransform;
45 45
46 /// The currently resolved library, or null if unresolved. 46 /// The currently resolved library, or null if unresolved.
47 LibraryElement _entryLibrary; 47 LibraryElement _entryLibrary;
48 48
49 /// Future indicating when this resolver is done in the current phase.
50 Future _lastPhaseComplete = new Future.value();
51
52 /// Completer for wrapping up the current phase.
53 Completer _currentPhaseComplete;
54
49 /// Handler for all Dart SDK (dart:) sources. 55 /// Handler for all Dart SDK (dart:) sources.
50 DirectoryBasedDartSdk _dartSdk; 56 DirectoryBasedDartSdk _dartSdk;
51 57
52 /// Creates a resolver that will resolve the Dart code starting at 58 /// Creates a resolver that will resolve the Dart code starting at
53 /// [entryPoint]. 59 /// [entryPoint].
54 /// 60 ///
55 /// [sdkDir] is the root directory of the Dart SDK, for resolving dart: 61 /// [sdkDir] is the root directory of the Dart SDK, for resolving dart:
56 /// imports. 62 /// imports.
57 ResolverImpl(this.entryPoint, String sdkDir, {AnalysisOptions options}) { 63 ResolverImpl(this.entryPoint, String sdkDir, {AnalysisOptions options}) {
58 if (options == null) { 64 if (options == null) {
59 options = new AnalysisOptionsImpl() 65 options = new AnalysisOptionsImpl()
60 ..cacheSize = 256 // # of sources to cache ASTs for. 66 ..cacheSize = 256 // # of sources to cache ASTs for.
61 ..preserveComments = false 67 ..preserveComments = false
62 ..analyzeFunctionBodies = true; 68 ..analyzeFunctionBodies = true;
63 } 69 }
64 _context.analysisOptions = options; 70 _context.analysisOptions = options;
65 71
66 _dartSdk = new _DirectoryBasedDartSdkProxy(new JavaFile(sdkDir)); 72 _dartSdk = new _DirectoryBasedDartSdkProxy(new JavaFile(sdkDir));
67 _dartSdk.context.analysisOptions = options; 73 _dartSdk.context.analysisOptions = options;
68 74
69 _context.sourceFactory = new SourceFactory.con2([ 75 _context.sourceFactory = new SourceFactory.con2([
70 new DartUriResolverProxy(_dartSdk), 76 new DartUriResolverProxy(_dartSdk),
71 new _AssetUriResolver(this)]); 77 new _AssetUriResolver(this)]);
72 } 78 }
73 79
74 LibraryElement get entryLibrary => _entryLibrary; 80 LibraryElement get entryLibrary => _entryLibrary;
75 81
82 Future<Resolver> resolve(Transform transform) {
83 // Can only have one resolve in progress at a time, so chain the current
84 // resolution to be after the last one.
85 var phaseComplete = new Completer();
86 var future = _lastPhaseComplete.then((_) {
87 _currentPhaseComplete = phaseComplete;
76 88
77 /// Update the status of all the sources referenced by the entryPoint and 89 return _performResolve(transform);
78 /// update the resolved library. 90 }).then((_) {
79 /// 91 return this;
80 /// This will be invoked automatically by [ResolverTransformer]. Only one 92 });
Siggi Cherem (dart-lang) 2014/02/27 18:20:41 style nit: use (_) => this, maybe fold into the pr
blois 2014/02/27 23:47:17 Done.
81 /// transformer may update this at a time. 93 // Advance the lastPhaseComplete to be done when this phase is all done.
82 Future updateSources(Transform transform) { 94 _lastPhaseComplete = phaseComplete.future;
95 return future;
96 }
97
98 void release() {
99 if (_currentPhaseComplete == null) {
100 throw new StateError('Releasing without current lock.');
101 }
102 _currentPhaseComplete.complete(null);
103 _currentPhaseComplete = null;
104
105 // Clear out the entry lib since it should not be referenced after release.
106 _entryLibrary = null;
107 }
108
109 Future _performResolve(Transform transform) {
83 if (_currentTransform != null) { 110 if (_currentTransform != null) {
84 throw new StateError('Cannot be accessed by concurrent transforms'); 111 throw new StateError('Cannot be accessed by concurrent transforms');
85 } 112 }
86 _currentTransform = transform; 113 _currentTransform = transform;
87 // Clear this out and update once all asset changes have been processed.
88 _entryLibrary = null;
89 114
90 // Basic approach is to start at the first file, update it's contents 115 // Basic approach is to start at the first file, update it's contents
91 // and see if it changed, then walk all files accessed by it. 116 // and see if it changed, then walk all files accessed by it.
92 var visited = new Set<AssetId>(); 117 var visited = new Set<AssetId>();
93 var visiting = new FutureGroup(); 118 var visiting = new FutureGroup();
94 119
95 void processAsset(AssetId assetId) { 120 void processAsset(AssetId assetId) {
96 visited.add(assetId); 121 visited.add(assetId);
97 122
98 visiting.add(transform.readInputAsString(assetId).then((contents) { 123 visiting.add(transform.readInputAsString(assetId).then((contents) {
(...skipping 439 matching lines...) Expand 10 before | Expand all | Expand 10 after
538 563
539 /** 564 /**
540 * A Future that complets with a List of the values from all the added 565 * A Future that complets with a List of the values from all the added
541 * tasks, when they have all completed. 566 * tasks, when they have all completed.
542 * 567 *
543 * If any task fails, this Future will receive the error. Only the first 568 * If any task fails, this Future will receive the error. Only the first
544 * error will be sent to the Future. 569 * error will be sent to the Future.
545 */ 570 */
546 Future<List<E>> get future => _completer.future; 571 Future<List<E>> get future => _completer.future;
547 } 572 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698