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

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

Issue 2408863002: Parse the 'patches' argument in SDK libraries declarations. (Closed)
Patch Set: Rework to support 'PLATFORM1 | PLATFORM2' and [list, of, paths]. Created 4 years, 2 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/analysis_server/test/mock_sdk.dart ('k') | pkg/analyzer/test/src/context/mock_sdk.dart » ('j') | 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) 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/token.dart';
10 import 'package:analyzer/dart/ast/visitor.dart'; 11 import 'package:analyzer/dart/ast/visitor.dart';
11 import 'package:analyzer/src/generated/engine.dart' 12 import 'package:analyzer/src/generated/engine.dart'
12 show AnalysisContext, AnalysisOptions, AnalysisOptionsImpl; 13 show AnalysisContext, AnalysisOptions, AnalysisOptionsImpl;
13 import 'package:analyzer/src/generated/source.dart' show Source; 14 import 'package:analyzer/src/generated/source.dart' show Source;
14 import 'package:analyzer/src/generated/utilities_general.dart'; 15 import 'package:analyzer/src/generated/utilities_general.dart';
15 import 'package:analyzer/src/summary/idl.dart' show PackageBundle; 16 import 'package:analyzer/src/summary/idl.dart' show PackageBundle;
16 17
17 /** 18 /**
18 * A function used to create a new DartSdk with the given [options]. If the 19 * A function used to create a new DartSdk with the given [options]. If the
19 * passed [options] are `null`, then default options are used. 20 * passed [options] are `null`, then default options are used.
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 String toString() { 268 String toString() {
268 StringBuffer buffer = new StringBuffer(); 269 StringBuffer buffer = new StringBuffer();
269 bool needsSeparator = false; 270 bool needsSeparator = false;
270 void add(String optionName) { 271 void add(String optionName) {
271 if (needsSeparator) { 272 if (needsSeparator) {
272 buffer.write(', '); 273 buffer.write(', ');
273 } 274 }
274 buffer.write(optionName); 275 buffer.write(optionName);
275 needsSeparator = true; 276 needsSeparator = true;
276 } 277 }
278
277 for (String path in paths) { 279 for (String path in paths) {
278 add(path); 280 add(path);
279 } 281 }
280 if (needsSeparator) { 282 if (needsSeparator) {
281 buffer.write(' '); 283 buffer.write(' ');
282 } 284 }
283 buffer.write('('); 285 buffer.write('(');
284 buffer.write(AnalysisOptionsImpl 286 buffer.write(AnalysisOptionsImpl
285 .decodeCrossContextOptions(options.encodeCrossContextOptions())); 287 .decodeCrossContextOptions(options.encodeCrossContextOptions()));
286 buffer.write(')'); 288 buffer.write(')');
(...skipping 14 matching lines...) Expand all
301 */ 303 */
302 static String _IMPLEMENTATION = "implementation"; 304 static String _IMPLEMENTATION = "implementation";
303 305
304 /** 306 /**
305 * The name of the optional parameter used to specify the path used when 307 * The name of the optional parameter used to specify the path used when
306 * compiling for dart2js. 308 * compiling for dart2js.
307 */ 309 */
308 static String _DART2JS_PATH = "dart2jsPath"; 310 static String _DART2JS_PATH = "dart2jsPath";
309 311
310 /** 312 /**
313 * The name of the `dart2js` platform.
314 */
315 static String _DART2JS_PLATFORM = 'DART2JS_PLATFORM';
316
317 /**
311 * The name of the optional parameter used to indicate whether the library is 318 * The name of the optional parameter used to indicate whether the library is
312 * documented. 319 * documented.
313 */ 320 */
314 static String _DOCUMENTED = "documented"; 321 static String _DOCUMENTED = "documented";
315 322
316 /** 323 /**
317 * The name of the optional parameter used to specify the category of the 324 * The name of the optional parameter used to specify the category of the
318 * library. 325 * library.
319 */ 326 */
320 static String _CATEGORIES = "categories"; 327 static String _CATEGORIES = "categories";
321 328
322 /** 329 /**
330 * The name of the optional parameter used to specify the patches for
331 * the library.
332 */
333 static String _PATCHES = "patches";
334
335 /**
323 * The name of the optional parameter used to specify the platforms on which 336 * The name of the optional parameter used to specify the platforms on which
324 * the library can be used. 337 * the library can be used.
325 */ 338 */
326 static String _PLATFORMS = "platforms"; 339 static String _PLATFORMS = "platforms";
327 340
328 /** 341 /**
329 * The value of the [PLATFORMS] parameter used to specify that the library can 342 * The value of the [PLATFORMS] parameter used to specify that the library can
330 * be used on the VM. 343 * be used on the VM.
331 */ 344 */
332 static String _VM_PLATFORM = "VM_PLATFORM"; 345 static String _VM_PLATFORM = "VM_PLATFORM";
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
390 } else if (argument is NamedExpression) { 403 } else if (argument is NamedExpression) {
391 String name = argument.name.label.name; 404 String name = argument.name.label.name;
392 Expression expression = argument.expression; 405 Expression expression = argument.expression;
393 if (name == _CATEGORIES) { 406 if (name == _CATEGORIES) {
394 library.category = 407 library.category =
395 convertCategories((expression as StringLiteral).stringValue); 408 convertCategories((expression as StringLiteral).stringValue);
396 } else if (name == _IMPLEMENTATION) { 409 } else if (name == _IMPLEMENTATION) {
397 library._implementation = (expression as BooleanLiteral).value; 410 library._implementation = (expression as BooleanLiteral).value;
398 } else if (name == _DOCUMENTED) { 411 } else if (name == _DOCUMENTED) {
399 library.documented = (expression as BooleanLiteral).value; 412 library.documented = (expression as BooleanLiteral).value;
413 } else if (name == _PATCHES) {
414 if (expression is MapLiteral) {
415 expression.entries.forEach((MapLiteralEntry entry) {
416 int platforms = _convertPlatforms(entry.key);
417 Expression pathsListLiteral = entry.value;
418 if (pathsListLiteral is ListLiteral) {
419 List<String> paths = <String>[];
420 pathsListLiteral.elements.forEach((Expression pathExpr) {
421 if (pathExpr is SimpleStringLiteral) {
422 paths.add(pathExpr.value);
423 } else {
424 throw new ArgumentError(
425 'The "patch" argument items must be simple strings.');
426 }
427 });
428 library.setPatchPaths(platforms, paths);
429 } else {
430 throw new ArgumentError(
431 'The "patch" argument values must be list literals.');
432 }
433 });
434 }
400 } else if (name == _PLATFORMS) { 435 } else if (name == _PLATFORMS) {
401 if (expression is SimpleIdentifier) { 436 if (expression is SimpleIdentifier) {
402 String identifier = expression.name; 437 String identifier = expression.name;
403 if (identifier == _VM_PLATFORM) { 438 if (identifier == _VM_PLATFORM) {
404 library.setVmLibrary(); 439 library.setVmLibrary();
405 } else { 440 } else {
406 library.setDart2JsLibrary(); 441 library.setDart2JsLibrary();
407 } 442 }
408 } 443 }
409 } else if (_useDart2jsPaths && name == _DART2JS_PATH) { 444 } else if (_useDart2jsPaths && name == _DART2JS_PATH) {
410 if (expression is SimpleStringLiteral) { 445 if (expression is SimpleStringLiteral) {
411 library.path = expression.value; 446 library.path = expression.value;
412 } 447 }
413 } 448 }
414 } 449 }
415 } 450 }
416 _librariesMap.setLibrary(libraryName, library); 451 _librariesMap.setLibrary(libraryName, library);
417 } 452 }
418 return null; 453 return null;
419 } 454 }
455
456 /**
457 * Return the platform constant value for the given [expr].
458 * Throw [ArgumentError] if not a valid platform name given.
459 */
460 static int _convertPlatform(Expression expr) {
461 if (expr is SimpleIdentifier) {
462 String name = expr.name;
463 if (name == _DART2JS_PLATFORM) {
464 return SdkLibraryImpl.DART2JS_PLATFORM;
465 }
466 if (name == _VM_PLATFORM) {
467 return SdkLibraryImpl.VM_PLATFORM;
468 }
469 throw new ArgumentError('Invalid platform name: $name');
470 }
471 throw new ArgumentError('Invalid platform type: ${expr.runtimeType}');
472 }
473
474 /**
475 * Return the platforms combination value for the [expr], which should be
476 * either `name1 | name2` or `name`. Throw [ArgumentError] if any of the
477 * names is not a valid platform name.
478 */
479 static int _convertPlatforms(Expression expr) {
480 if (expr is BinaryExpression) {
481 TokenType operator = expr.operator?.type;
482 if (operator == TokenType.BAR) {
483 return _convertPlatforms(expr.leftOperand) |
484 _convertPlatforms(expr.rightOperand);
485 } else {
486 throw new ArgumentError('Invalid platforms combination: $operator');
487 }
488 } else {
489 return _convertPlatform(expr);
490 }
491 }
420 } 492 }
421 493
422 /** 494 /**
423 * Represents a single library in the SDK 495 * Represents a single library in the SDK
424 */ 496 */
425 abstract class SdkLibrary { 497 abstract class SdkLibrary {
426 /** 498 /**
427 * Return the name of the category containing the library. 499 * Return the name of the category containing the library.
428 */ 500 */
429 String get category; 501 String get category;
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
462 * Return the path to the file defining the library. The path is relative to 534 * Return the path to the file defining the library. The path is relative to
463 * the `lib` directory within the SDK. 535 * the `lib` directory within the SDK.
464 */ 536 */
465 String get path; 537 String get path;
466 538
467 /** 539 /**
468 * Return the short name of the library. This is the URI of the library, 540 * Return the short name of the library. This is the URI of the library,
469 * including `dart:`. 541 * including `dart:`.
470 */ 542 */
471 String get shortName; 543 String get shortName;
544
545 /**
546 * Return the list of paths to the patch files that should be applied
547 * to this library for the given [platform], not `null`.
548 */
549 List<String> getPatches(int platform);
Bill Hesse 2016/10/11 21:35:59 This is a breaking change to the interface. You n
472 } 550 }
473 551
474 /** 552 /**
475 * The information known about a single library within the SDK. 553 * The information known about a single library within the SDK.
476 */ 554 */
477 class SdkLibraryImpl implements SdkLibrary { 555 class SdkLibraryImpl implements SdkLibrary {
478 /** 556 /**
479 * The bit mask used to access the bit representing the flag indicating 557 * The bit mask used to access the bit representing the flag indicating
480 * whether a library is intended to work on the dart2js platform. 558 * whether a library is intended to work on the dart2js platform.
481 */ 559 */
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
514 * A flag indicating whether the library is an implementation library. 592 * A flag indicating whether the library is an implementation library.
515 */ 593 */
516 bool _implementation = false; 594 bool _implementation = false;
517 595
518 /** 596 /**
519 * An encoding of which platforms this library is intended to work on. 597 * An encoding of which platforms this library is intended to work on.
520 */ 598 */
521 int _platforms = 0; 599 int _platforms = 0;
522 600
523 /** 601 /**
602 * The mapping from the platform combination to the list of paths (relative
603 * to the `sdk/lib` folder) of patches that should be applied to this library
604 * on every platform in the combination.
605 */
606 final Map<int, List<String>> _platformsToPatchPaths =
607 new HashMap<int, List<String>>();
608
609 /**
524 * Initialize a newly created library to represent the library with the given 610 * Initialize a newly created library to represent the library with the given
525 * [name]. 611 * [name].
526 */ 612 */
527 SdkLibraryImpl(this.shortName); 613 SdkLibraryImpl(this.shortName);
528 614
529 /** 615 /**
530 * Set whether the library is documented. 616 * Set whether the library is documented.
531 */ 617 */
532 void set documented(bool documented) { 618 void set documented(bool documented) {
533 this._documented = documented; 619 this._documented = documented;
(...skipping 10 matching lines...) Expand all
544 630
545 @override 631 @override
546 bool get isInternal => category == "Internal"; 632 bool get isInternal => category == "Internal";
547 633
548 @override 634 @override
549 bool get isShared => category == "Shared"; 635 bool get isShared => category == "Shared";
550 636
551 @override 637 @override
552 bool get isVmLibrary => (_platforms & VM_PLATFORM) != 0; 638 bool get isVmLibrary => (_platforms & VM_PLATFORM) != 0;
553 639
640 @override
641 List<String> getPatches(int platform) {
642 List<String> paths = <String>[];
643 _platformsToPatchPaths.forEach((int platforms, List<String> value) {
644 if ((platforms & platform) != 0) {
645 paths.addAll(value);
646 }
647 });
648 return paths;
649 }
650
554 /** 651 /**
555 * Record that this library can be compiled to JavaScript by dart2js. 652 * Record that this library can be compiled to JavaScript by dart2js.
556 */ 653 */
557 void setDart2JsLibrary() { 654 void setDart2JsLibrary() {
558 _platforms |= DART2JS_PLATFORM; 655 _platforms |= DART2JS_PLATFORM;
559 } 656 }
560 657
561 /** 658 /**
659 * Add a new patch with the given [path] that should be applied for the
660 * given [platforms].
661 */
662 void setPatchPaths(int platforms, List<String> paths) {
663 _platformsToPatchPaths[platforms] = paths;
664 }
665
666 /**
562 * Record that this library can be run on the VM. 667 * Record that this library can be run on the VM.
563 */ 668 */
564 void setVmLibrary() { 669 void setVmLibrary() {
565 _platforms |= VM_PLATFORM; 670 _platforms |= VM_PLATFORM;
566 } 671 }
567 } 672 }
OLDNEW
« no previous file with comments | « pkg/analysis_server/test/mock_sdk.dart ('k') | pkg/analyzer/test/src/context/mock_sdk.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698