forked from flutter/engine
/
sdk_io.dart
298 lines (271 loc) · 9.58 KB
/
sdk_io.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
@deprecated
library analyzer.src.generated.sdk_io;
import 'dart:collection';
import 'package:analyzer/dart/analysis/features.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/exception/exception.dart';
import 'package:analyzer/src/context/context.dart';
import 'package:analyzer/src/dart/scanner/reader.dart';
import 'package:analyzer/src/dart/scanner/scanner.dart';
import 'package:analyzer/src/generated/engine.dart';
import 'package:analyzer/src/generated/error.dart';
import 'package:analyzer/src/generated/java_io.dart';
import 'package:analyzer/src/generated/parser.dart';
import 'package:analyzer/src/generated/sdk.dart';
import 'package:analyzer/src/generated/source_io.dart';
import 'package:analyzer/src/summary/idl.dart' show PackageBundle;
/**
* An abstract implementation of a Dart SDK in which the available libraries are
* stored in a library map. Subclasses are responsible for populating the
* library map.
*/
@deprecated
abstract class AbstractDartSdk implements DartSdk {
/**
* A mapping from Dart library URI's to the library represented by that URI.
*/
LibraryMap libraryMap = new LibraryMap();
/**
* The [AnalysisOptions] to use to create the [context].
*/
AnalysisOptions _analysisOptions;
/**
* The flag that specifies whether an SDK summary should be used. This is a
* temporary flag until summaries are enabled by default.
*/
bool _useSummary = false;
/**
* The [AnalysisContext] which is used for all of the sources in this SDK.
*/
InternalAnalysisContext _analysisContext;
/**
* The mapping from Dart URI's to the corresponding sources.
*/
Map<String, Source> _uriToSourceMap = new HashMap<String, Source>();
/**
* Set the [options] for this SDK analysis context. Throw [StateError] if the
* context has been already created.
*/
void set analysisOptions(AnalysisOptions options) {
if (_analysisContext != null) {
throw new StateError(
'Analysis options cannot be changed after context creation.');
}
_analysisOptions = options;
}
@override
AnalysisContext get context {
if (_analysisContext == null) {
_analysisContext = new SdkAnalysisContext(_analysisOptions);
SourceFactory factory = new SourceFactory([new DartUriResolver(this)]);
_analysisContext.sourceFactory = factory;
}
return _analysisContext;
}
@override
List<SdkLibrary> get sdkLibraries => libraryMap.sdkLibraries;
@override
List<String> get uris => libraryMap.uris;
/**
* Return `true` if the SDK summary will be used when available.
*/
bool get useSummary => _useSummary;
/**
* Specify whether SDK summary should be used.
*/
void set useSummary(bool use) {
if (_analysisContext != null) {
throw new StateError(
'The "useSummary" flag cannot be changed after context creation.');
}
_useSummary = use;
}
/**
* Add the extensions from one or more sdk extension files to this sdk. The
* [extensions] should be a table mapping the names of extensions to the paths
* where those extensions can be found.
*/
void addExtensions(Map<String, String> extensions) {
extensions.forEach((String uri, String path) {
String shortName = uri.substring(uri.indexOf(':') + 1);
SdkLibraryImpl library = new SdkLibraryImpl(shortName);
library.path = path;
libraryMap.setLibrary(uri, library);
});
}
@override
Source fromFileUri(Uri uri) {
JavaFile file = new JavaFile.fromUri(uri);
String path = _getPath(file);
if (path == null) {
return null;
}
try {
return new FileBasedSource(file, Uri.parse(path));
} on FormatException catch (exception, stackTrace) {
AnalysisEngine.instance.logger.logInformation(
"Failed to create URI: $path",
new CaughtException(exception, stackTrace));
}
return null;
}
String getRelativePathFromFile(JavaFile file);
@override
SdkLibrary getSdkLibrary(String dartUri) => libraryMap.getLibrary(dartUri);
/**
* Return the [PackageBundle] for this SDK, if it exists, or `null` otherwise.
* This method should not be used outside of `analyzer` and `analyzer_cli`
* packages.
*/
@deprecated
PackageBundle getSummarySdkBundle(bool _);
FileBasedSource internalMapDartUri(String dartUri) {
// TODO(brianwilkerson) Figure out how to unify the implementations in the
// two subclasses.
String libraryName;
String relativePath;
int index = dartUri.indexOf('/');
if (index >= 0) {
libraryName = dartUri.substring(0, index);
relativePath = dartUri.substring(index + 1);
} else {
libraryName = dartUri;
relativePath = "";
}
SdkLibrary library = getSdkLibrary(libraryName);
if (library == null) {
return null;
}
String srcPath;
if (relativePath.isEmpty) {
srcPath = library.path;
} else {
String libraryPath = library.path;
int index = libraryPath.lastIndexOf(JavaFile.separator);
if (index == -1) {
index = libraryPath.lastIndexOf('/');
if (index == -1) {
return null;
}
}
String prefix = libraryPath.substring(0, index + 1);
srcPath = '$prefix$relativePath';
}
String filePath = srcPath.replaceAll('/', JavaFile.separator);
try {
JavaFile file = new JavaFile(filePath);
return new FileBasedSource(file, Uri.parse(dartUri));
} on FormatException {
return null;
}
}
@override
Source mapDartUri(String dartUri) {
Source source = _uriToSourceMap[dartUri];
if (source == null) {
source = internalMapDartUri(dartUri);
_uriToSourceMap[dartUri] = source;
}
return source;
}
String _getPath(JavaFile file) {
List<SdkLibrary> libraries = libraryMap.sdkLibraries;
int length = libraries.length;
List<String> paths = new List(length);
String filePath = getRelativePathFromFile(file);
if (filePath == null) {
return null;
}
for (int i = 0; i < length; i++) {
SdkLibrary library = libraries[i];
String libraryPath = library.path.replaceAll('/', JavaFile.separator);
if (filePath == libraryPath) {
return library.shortName;
}
paths[i] = libraryPath;
}
for (int i = 0; i < length; i++) {
SdkLibrary library = libraries[i];
String libraryPath = paths[i];
int index = libraryPath.lastIndexOf(JavaFile.separator);
if (index >= 0) {
String prefix = libraryPath.substring(0, index + 1);
if (filePath.startsWith(prefix)) {
String relPath = filePath
.substring(prefix.length)
.replaceAll(JavaFile.separator, '/');
return '${library.shortName}/$relPath';
}
}
}
return null;
}
}
/**
* An object used to read and parse the libraries file
* (dart-sdk/lib/_internal/sdk_library_metadata/lib/libraries.dart) for information
* about the libraries in an SDK. The library information is represented as a
* Dart file containing a single top-level variable whose value is a const map.
* The keys of the map are the names of libraries defined in the SDK and the
* values in the map are info objects defining the library. For example, a
* subset of a typical SDK might have a libraries file that looks like the
* following:
*
* final Map<String, LibraryInfo> LIBRARIES = const <LibraryInfo> {
* // Used by VM applications
* "builtin" : const LibraryInfo(
* "builtin/builtin_runtime.dart",
* category: "Server",
* platforms: VM_PLATFORM),
*
* "compiler" : const LibraryInfo(
* "compiler/compiler.dart",
* category: "Tools",
* platforms: 0),
* };
*/
@deprecated
class SdkLibrariesReader {
/**
* A flag indicating whether the dart2js path should be used when it is
* available.
*/
final bool _useDart2jsPaths;
/**
* Initialize a newly created library reader to use the dart2js path if
* [_useDart2jsPaths] is `true`.
*/
SdkLibrariesReader(this._useDart2jsPaths);
/**
* Return the library map read from the given [file], given that the content
* of the file is already known to be [libraryFileContents].
*/
LibraryMap readFromFile(JavaFile file, String libraryFileContents) =>
readFromSource(new FileBasedSource(file), libraryFileContents);
/**
* Return the library map read from the given [source], given that the content
* of the file is already known to be [libraryFileContents].
*/
LibraryMap readFromSource(Source source, String libraryFileContents) {
BooleanErrorListener errorListener = new BooleanErrorListener();
// TODO(paulberry): initialize the feature set appropriately based on the
// version of the SDK we are reading, and enable flags.
var featureSet = FeatureSet.fromEnableFlags([]);
Scanner scanner = new Scanner(
source, new CharSequenceReader(libraryFileContents), errorListener)
..configureFeatures(featureSet);
Parser parser = new Parser(source, errorListener, featureSet: featureSet);
CompilationUnit unit = parser.parseCompilationUnit(scanner.tokenize());
SdkLibrariesReader_LibraryBuilder libraryBuilder =
new SdkLibrariesReader_LibraryBuilder(_useDart2jsPaths);
// If any syntactic errors were found then don't try to visit the AST
// structure.
if (!errorListener.errorReported) {
unit.accept(libraryBuilder);
}
return libraryBuilder.librariesMap;
}
}