OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library engine.sdk; |
| 6 |
| 7 import 'dart:collection'; |
| 8 |
| 9 import 'ast.dart'; |
| 10 import 'engine.dart' show AnalysisContext; |
| 11 import 'source.dart' show ContentCache, Source, UriKind; |
| 12 |
| 13 /** |
| 14 * A Dart SDK installed in a specified location. |
| 15 */ |
| 16 abstract class DartSdk { |
| 17 /** |
| 18 * The short name of the dart SDK 'async' library. |
| 19 */ |
| 20 static final String DART_ASYNC = "dart:async"; |
| 21 |
| 22 /** |
| 23 * The short name of the dart SDK 'core' library. |
| 24 */ |
| 25 static final String DART_CORE = "dart:core"; |
| 26 |
| 27 /** |
| 28 * The short name of the dart SDK 'html' library. |
| 29 */ |
| 30 static final String DART_HTML = "dart:html"; |
| 31 |
| 32 /** |
| 33 * The version number that is returned when the real version number could not |
| 34 * be determined. |
| 35 */ |
| 36 static final String DEFAULT_VERSION = "0"; |
| 37 |
| 38 /** |
| 39 * Return the analysis context used for all of the sources in this [DartSdk]. |
| 40 */ |
| 41 AnalysisContext get context; |
| 42 |
| 43 /** |
| 44 * Return a list containing all of the libraries defined in this SDK. |
| 45 */ |
| 46 List<SdkLibrary> get sdkLibraries; |
| 47 |
| 48 /** |
| 49 * Return the revision number of this SDK, or `"0"` if the revision number |
| 50 * cannot be discovered. |
| 51 */ |
| 52 String get sdkVersion; |
| 53 |
| 54 /** |
| 55 * Return a list containing the library URI's for the libraries defined in |
| 56 * this SDK. |
| 57 */ |
| 58 List<String> get uris; |
| 59 |
| 60 /** |
| 61 * Return a source representing the given 'file:' [uri] if the file is in this |
| 62 * SDK, or `null` if the file is not in this SDK. |
| 63 */ |
| 64 Source fromFileUri(Uri uri); |
| 65 |
| 66 /** |
| 67 * Return the library representing the library with the given 'dart:' [uri], |
| 68 * or `null` if the given URI does not denote a library in this SDK. |
| 69 */ |
| 70 SdkLibrary getSdkLibrary(String uri); |
| 71 |
| 72 /** |
| 73 * Return the source representing the library with the given 'dart:' [uri], or |
| 74 * `null` if the given URI does not denote a library in this SDK. |
| 75 */ |
| 76 Source mapDartUri(String uri); |
| 77 } |
| 78 |
| 79 /** |
| 80 * A map from Dart library URI's to the [SdkLibraryImpl] representing that |
| 81 * library. |
| 82 */ |
| 83 class LibraryMap { |
| 84 /** |
| 85 * A table mapping Dart library URI's to the library. |
| 86 */ |
| 87 HashMap<String, SdkLibraryImpl> _libraryMap = |
| 88 new HashMap<String, SdkLibraryImpl>(); |
| 89 |
| 90 /** |
| 91 * Return a list containing all of the sdk libraries in this mapping. |
| 92 */ |
| 93 List<SdkLibrary> get sdkLibraries => new List.from(_libraryMap.values); |
| 94 |
| 95 /** |
| 96 * Return a list containing the library URI's for which a mapping is available
. |
| 97 */ |
| 98 List<String> get uris => new List.from(_libraryMap.keys.toSet()); |
| 99 |
| 100 /** |
| 101 * Return the library with the given 'dart:' [uri], or `null` if the URI does |
| 102 * not map to a library. |
| 103 */ |
| 104 SdkLibrary getLibrary(String uri) => _libraryMap[uri]; |
| 105 |
| 106 /** |
| 107 * Set the library with the given 'dart:' [uri] to the given [library]. |
| 108 */ |
| 109 void setLibrary(String dartUri, SdkLibraryImpl library) { |
| 110 _libraryMap[dartUri] = library; |
| 111 } |
| 112 |
| 113 /** |
| 114 * Return the number of library URI's for which a mapping is available. |
| 115 */ |
| 116 int size() => _libraryMap.length; |
| 117 } |
| 118 |
| 119 class SdkLibrariesReader_LibraryBuilder extends RecursiveAstVisitor<Object> { |
| 120 /** |
| 121 * The prefix added to the name of a library to form the URI used in code to |
| 122 * reference the library. |
| 123 */ |
| 124 static String _LIBRARY_PREFIX = "dart:"; |
| 125 |
| 126 /** |
| 127 * The name of the optional parameter used to indicate whether the library is |
| 128 * an implementation library. |
| 129 */ |
| 130 static String _IMPLEMENTATION = "implementation"; |
| 131 |
| 132 /** |
| 133 * The name of the optional parameter used to specify the path used when |
| 134 * compiling for dart2js. |
| 135 */ |
| 136 static String _DART2JS_PATH = "dart2jsPath"; |
| 137 |
| 138 /** |
| 139 * The name of the optional parameter used to indicate whether the library is |
| 140 * documented. |
| 141 */ |
| 142 static String _DOCUMENTED = "documented"; |
| 143 |
| 144 /** |
| 145 * The name of the optional parameter used to specify the category of the |
| 146 * library. |
| 147 */ |
| 148 static String _CATEGORY = "category"; |
| 149 |
| 150 /** |
| 151 * The name of the optional parameter used to specify the platforms on which |
| 152 * the library can be used. |
| 153 */ |
| 154 static String _PLATFORMS = "platforms"; |
| 155 |
| 156 /** |
| 157 * The value of the [PLATFORMS] parameter used to specify that the library can |
| 158 * be used on the VM. |
| 159 */ |
| 160 static String _VM_PLATFORM = "VM_PLATFORM"; |
| 161 |
| 162 /** |
| 163 * A flag indicating whether the dart2js path should be used when it is |
| 164 * available. |
| 165 */ |
| 166 final bool _useDart2jsPaths; |
| 167 |
| 168 /** |
| 169 * The library map that is populated by visiting the AST structure parsed from |
| 170 * the contents of the libraries file. |
| 171 */ |
| 172 LibraryMap _librariesMap = new LibraryMap(); |
| 173 |
| 174 /** |
| 175 * Initialize a newly created library builder to use the dart2js path if |
| 176 * [_useDart2jsPaths] is `true`. |
| 177 */ |
| 178 SdkLibrariesReader_LibraryBuilder(this._useDart2jsPaths); |
| 179 |
| 180 /** |
| 181 * Return the library map that was populated by visiting the AST structure |
| 182 * parsed from the contents of the libraries file. |
| 183 */ |
| 184 LibraryMap get librariesMap => _librariesMap; |
| 185 |
| 186 @override |
| 187 Object visitMapLiteralEntry(MapLiteralEntry node) { |
| 188 String libraryName = null; |
| 189 Expression key = node.key; |
| 190 if (key is SimpleStringLiteral) { |
| 191 libraryName = "$_LIBRARY_PREFIX${key.value}"; |
| 192 } |
| 193 Expression value = node.value; |
| 194 if (value is InstanceCreationExpression) { |
| 195 SdkLibraryImpl library = new SdkLibraryImpl(libraryName); |
| 196 List<Expression> arguments = value.argumentList.arguments; |
| 197 for (Expression argument in arguments) { |
| 198 if (argument is SimpleStringLiteral) { |
| 199 library.path = argument.value; |
| 200 } else if (argument is NamedExpression) { |
| 201 String name = argument.name.label.name; |
| 202 Expression expression = argument.expression; |
| 203 if (name == _CATEGORY) { |
| 204 library.category = (expression as SimpleStringLiteral).value; |
| 205 } else if (name == _IMPLEMENTATION) { |
| 206 library.implementation = (expression as BooleanLiteral).value; |
| 207 } else if (name == _DOCUMENTED) { |
| 208 library.documented = (expression as BooleanLiteral).value; |
| 209 } else if (name == _PLATFORMS) { |
| 210 if (expression is SimpleIdentifier) { |
| 211 String identifier = expression.name; |
| 212 if (identifier == _VM_PLATFORM) { |
| 213 library.setVmLibrary(); |
| 214 } else { |
| 215 library.setDart2JsLibrary(); |
| 216 } |
| 217 } |
| 218 } else if (_useDart2jsPaths && name == _DART2JS_PATH) { |
| 219 if (expression is SimpleStringLiteral) { |
| 220 library.path = expression.value; |
| 221 } |
| 222 } |
| 223 } |
| 224 } |
| 225 _librariesMap.setLibrary(libraryName, library); |
| 226 } |
| 227 return null; |
| 228 } |
| 229 } |
| 230 |
| 231 /** |
| 232 * Represents a single library in the SDK |
| 233 */ |
| 234 abstract class SdkLibrary { |
| 235 /** |
| 236 * Return the name of the category containing the library. |
| 237 */ |
| 238 String get category; |
| 239 |
| 240 /** |
| 241 * Return `true` if this library can be compiled to JavaScript by dart2js. |
| 242 */ |
| 243 bool get isDart2JsLibrary; |
| 244 |
| 245 /** |
| 246 * Return `true` if the library is documented. |
| 247 */ |
| 248 bool get isDocumented; |
| 249 |
| 250 /** |
| 251 * Return `true` if the library is an implementation library. |
| 252 */ |
| 253 bool get isImplementation; |
| 254 |
| 255 /** |
| 256 * Return `true` if library is internal can be used only by other SDK librarie
s. |
| 257 */ |
| 258 bool get isInternal; |
| 259 |
| 260 /** |
| 261 * Return `true` if this library can be used for both client and server. |
| 262 */ |
| 263 bool get isShared; |
| 264 |
| 265 /** |
| 266 * Return `true` if this library can be run on the VM. |
| 267 */ |
| 268 bool get isVmLibrary; |
| 269 |
| 270 /** |
| 271 * Return the path to the file defining the library. The path is relative to |
| 272 * the `lib` directory within the SDK. |
| 273 */ |
| 274 String get path; |
| 275 |
| 276 /** |
| 277 * Return the short name of the library. This is the URI of the library, |
| 278 * including `dart:`. |
| 279 */ |
| 280 String get shortName; |
| 281 } |
| 282 |
| 283 /** |
| 284 * The information known about a single library within the SDK. |
| 285 */ |
| 286 class SdkLibraryImpl implements SdkLibrary { |
| 287 /** |
| 288 * The bit mask used to access the bit representing the flag indicating |
| 289 * whether a library is intended to work on the dart2js platform. |
| 290 */ |
| 291 static int DART2JS_PLATFORM = 1; |
| 292 |
| 293 /** |
| 294 * The bit mask used to access the bit representing the flag indicating |
| 295 * whether a library is intended to work on the VM platform. |
| 296 */ |
| 297 static int VM_PLATFORM = 2; |
| 298 |
| 299 /** |
| 300 * The short name of the library. This is the name used after 'dart:' in a |
| 301 * URI. |
| 302 */ |
| 303 final String shortName; |
| 304 |
| 305 /** |
| 306 * The path to the file defining the library. The path is relative to the |
| 307 * 'lib' directory within the SDK. |
| 308 */ |
| 309 String path = null; |
| 310 |
| 311 /** |
| 312 * The name of the category containing the library. Unless otherwise specified |
| 313 * in the libraries file all libraries are assumed to be shared between server |
| 314 * and client. |
| 315 */ |
| 316 String category = "Shared"; |
| 317 |
| 318 /** |
| 319 * A flag indicating whether the library is documented. |
| 320 */ |
| 321 bool _documented = true; |
| 322 |
| 323 /** |
| 324 * A flag indicating whether the library is an implementation library. |
| 325 */ |
| 326 bool _implementation = false; |
| 327 |
| 328 /** |
| 329 * An encoding of which platforms this library is intended to work on. |
| 330 */ |
| 331 int _platforms = 0; |
| 332 |
| 333 /** |
| 334 * Initialize a newly created library to represent the library with the given |
| 335 * [name]. |
| 336 */ |
| 337 SdkLibraryImpl(this.shortName); |
| 338 |
| 339 /** |
| 340 * Set whether the library is documented. |
| 341 */ |
| 342 void set documented(bool documented) { |
| 343 this._documented = documented; |
| 344 } |
| 345 |
| 346 /** |
| 347 * Set whether the library is an implementation library. |
| 348 */ |
| 349 void set implementation(bool implementation) { |
| 350 this._implementation = implementation; |
| 351 } |
| 352 |
| 353 @override |
| 354 bool get isDart2JsLibrary => (_platforms & DART2JS_PLATFORM) != 0; |
| 355 |
| 356 @override |
| 357 bool get isDocumented => _documented; |
| 358 |
| 359 @override |
| 360 bool get isImplementation => _implementation; |
| 361 |
| 362 @override |
| 363 bool get isInternal => "Internal" == category; |
| 364 |
| 365 @override |
| 366 bool get isShared => category == "Shared"; |
| 367 |
| 368 @override |
| 369 bool get isVmLibrary => (_platforms & VM_PLATFORM) != 0; |
| 370 |
| 371 /** |
| 372 * Record that this library can be compiled to JavaScript by dart2js. |
| 373 */ |
| 374 void setDart2JsLibrary() { |
| 375 _platforms |= DART2JS_PLATFORM; |
| 376 } |
| 377 |
| 378 /** |
| 379 * Record that this library can be run on the VM. |
| 380 */ |
| 381 void setVmLibrary() { |
| 382 _platforms |= VM_PLATFORM; |
| 383 } |
| 384 } |
OLD | NEW |