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

Side by Side Diff: pkg/analyzer/lib/src/generated/sdk.dart

Issue 2107793002: Generalize DartSdkManager to support embedder SDKs (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Created 4 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
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 analyzer.src.generated.sdk; 5 library analyzer.src.generated.sdk;
6 6
7 import 'dart:collection'; 7 import 'dart:collection';
8 8
9 import 'package:analyzer/dart/ast/ast.dart'; 9 import 'package:analyzer/dart/ast/ast.dart';
10 import 'package:analyzer/dart/ast/visitor.dart'; 10 import 'package:analyzer/dart/ast/visitor.dart';
11 import 'package:analyzer/src/generated/engine.dart' 11 import 'package:analyzer/src/generated/engine.dart'
12 show AnalysisContext, AnalysisOptions; 12 show AnalysisContext, AnalysisOptions;
13 import 'package:analyzer/src/generated/source.dart' show Source; 13 import 'package:analyzer/src/generated/source.dart' show Source;
14 import 'package:analyzer/src/generated/utilities_general.dart';
14 15
15 /** 16 /**
16 * A function used to create a new DartSdk with the given [options]. If the 17 * A function used to create a new DartSdk with the given [options]. If the
17 * passed [options] are `null`, then default options are used. 18 * passed [options] are `null`, then default options are used.
18 */ 19 */
19 typedef DartSdk SdkCreator(AnalysisOptions options); 20 typedef DartSdk SdkCreator(AnalysisOptions options);
20 21
21 /** 22 /**
22 * A Dart SDK installed in a specified location. 23 * A Dart SDK installed in a specified location.
23 */ 24 */
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 Source mapDartUri(String uri); 90 Source mapDartUri(String uri);
90 } 91 }
91 92
92 /** 93 /**
93 * Manages the DartSdk's that have been created. Clients need to create multiple 94 * Manages the DartSdk's that have been created. Clients need to create multiple
94 * SDKs when the analysis options associated with those SDK's contexts will 95 * SDKs when the analysis options associated with those SDK's contexts will
95 * produce different analysis results. 96 * produce different analysis results.
96 */ 97 */
97 class DartSdkManager { 98 class DartSdkManager {
98 /** 99 /**
100 * The absolute path to the directory containing the default SDK.
101 */
102 final String defaultSdkDirectory;
103
104 /**
105 * A flag indicating whether it is acceptable to use summaries when they are
106 * available.
107 */
108 final bool canUseSummaries;
109
110 /**
99 * The function used to create new SDK's. 111 * The function used to create new SDK's.
100 */ 112 */
101 final SdkCreator sdkCreator; 113 final SdkCreator sdkCreator;
102 114
103 /** 115 /**
104 * A table mapping (an encoding of) analysis options to the SDK that has been 116 * A table mapping (an encoding of) analysis options and SDK locations to the
105 * configured with those options. 117 * DartSdk from that location that has been configured with those options.
106 */ 118 */
107 Map<int, DartSdk> sdkMap = new HashMap<int, DartSdk>(); 119 Map<SdkDescription, DartSdk> sdkMap = new HashMap<SdkDescription, DartSdk>();
108 120
109 /** 121 /**
110 * Initialize a newly created manager. 122 * Initialize a newly created manager.
111 */ 123 */
112 DartSdkManager(this.sdkCreator); 124 DartSdkManager(
125 this.defaultSdkDirectory, this.canUseSummaries, this.sdkCreator);
113 126
114 /** 127 /**
115 * Return any SDK that has been created, or `null` if no SDKs have been 128 * Return any SDK that has been created, or `null` if no SDKs have been
116 * created. 129 * created.
117 */ 130 */
118 DartSdk get anySdk { 131 DartSdk get anySdk {
119 if (sdkMap.isEmpty) { 132 if (sdkMap.isEmpty) {
120 return null; 133 return null;
121 } 134 }
122 return sdkMap.values.first; 135 return sdkMap.values.first;
123 } 136 }
124 137
125 /** 138 /**
126 * Return the Dart SDK that is appropriate for the given analysis [options]. 139 * Return the Dart SDK that is appropriate for the given analysis [options].
127 * If such an SDK has not yet been created, then the [sdkCreator] will be 140 * If such an SDK has not yet been created, then the [sdkCreator] will be
128 * invoked to create it. 141 * invoked to create it.
129 */ 142 */
143 DartSdk getSdk(SdkDescription description, DartSdk ifAbsent()) {
144 return sdkMap.putIfAbsent(description, ifAbsent);
145 }
146
147 /**
148 * Return the Dart SDK that is appropriate for the given analysis [options].
149 * If such an SDK has not yet been created, then the [sdkCreator] will be
150 * invoked to create it.
151 */
130 DartSdk getSdkForOptions(AnalysisOptions options) { 152 DartSdk getSdkForOptions(AnalysisOptions options) {
131 int encoding = options.encodeCrossContextOptions(); 153 // TODO(brianwilkerson) Remove this method and the field sdkCreator.
132 return sdkMap.putIfAbsent(encoding, () => sdkCreator(options)); 154 SdkDescription description =
155 new SdkDescription(<String>[defaultSdkDirectory], options);
156 return getSdk(description, () => sdkCreator(options));
133 } 157 }
134 } 158 }
135 159
136 /** 160 /**
137 * A map from Dart library URI's to the [SdkLibraryImpl] representing that 161 * A map from Dart library URI's to the [SdkLibraryImpl] representing that
138 * library. 162 * library.
139 */ 163 */
140 class LibraryMap { 164 class LibraryMap {
141 /** 165 /**
142 * A table mapping Dart library URI's to the library. 166 * A table mapping Dart library URI's to the library.
(...skipping 23 matching lines...) Expand all
166 void setLibrary(String dartUri, SdkLibraryImpl library) { 190 void setLibrary(String dartUri, SdkLibraryImpl library) {
167 _libraryMap[dartUri] = library; 191 _libraryMap[dartUri] = library;
168 } 192 }
169 193
170 /** 194 /**
171 * Return the number of library URI's for which a mapping is available. 195 * Return the number of library URI's for which a mapping is available.
172 */ 196 */
173 int size() => _libraryMap.length; 197 int size() => _libraryMap.length;
174 } 198 }
175 199
200 /**
201 * A description of a [DartSdk].
202 */
203 class SdkDescription {
204 /**
205 * The paths to the files or directories that define the SDK.
206 */
207 final List<String> paths;
208
209 /**
210 * The analysis options that will be used by the SDK's context.
211 */
212 final AnalysisOptions options;
213
214 /**
215 * Initialize a newly created SDK description to describe an SDK based on the
216 * files or directories at the given [paths] that is analyzed using the given
217 * [options].
218 */
219 SdkDescription(this.paths, this.options);
220
221 @override
222 int get hashCode {
223 int hashCode = options.encodeCrossContextOptions();
224 for (String path in paths) {
225 hashCode = JenkinsSmiHash.combine(hashCode, path.hashCode);
226 }
227 return JenkinsSmiHash.finish(hashCode);
228 }
229
230 @override
231 bool operator ==(Object other) {
232 if (other is SdkDescription) {
233 if (options.encodeCrossContextOptions() !=
234 other.options.encodeCrossContextOptions()) {
235 return false;
236 }
237 int length = paths.length;
238 if (other.paths.length != length) {
239 return false;
240 }
241 for (int i = 0; i < length; i++) {
242 if (other.paths[i] != paths[i]) {
243 return false;
244 }
245 }
246 return true;
247 }
248 return false;
249 }
250 }
251
176 class SdkLibrariesReader_LibraryBuilder extends RecursiveAstVisitor<Object> { 252 class SdkLibrariesReader_LibraryBuilder extends RecursiveAstVisitor<Object> {
177 /** 253 /**
178 * The prefix added to the name of a library to form the URI used in code to 254 * The prefix added to the name of a library to form the URI used in code to
179 * reference the library. 255 * reference the library.
180 */ 256 */
181 static String _LIBRARY_PREFIX = "dart:"; 257 static String _LIBRARY_PREFIX = "dart:";
182 258
183 /** 259 /**
184 * The name of the optional parameter used to indicate whether the library is 260 * The name of the optional parameter used to indicate whether the library is
185 * an implementation library. 261 * an implementation library.
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after
443 _platforms |= DART2JS_PLATFORM; 519 _platforms |= DART2JS_PLATFORM;
444 } 520 }
445 521
446 /** 522 /**
447 * Record that this library can be run on the VM. 523 * Record that this library can be run on the VM.
448 */ 524 */
449 void setVmLibrary() { 525 void setVmLibrary() {
450 _platforms |= VM_PLATFORM; 526 _platforms |= VM_PLATFORM;
451 } 527 }
452 } 528 }
OLDNEW
« no previous file with comments | « pkg/analysis_server/test/socket_server_test.dart ('k') | pkg/analyzer/test/generated/sdk_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698