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

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

Issue 2560323002: Simplify how patch files are specified to analyzer. (Closed)
Patch Set: Created 4 years 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/token.dart';
11 import 'package:analyzer/dart/ast/visitor.dart'; 10 import 'package:analyzer/dart/ast/visitor.dart';
12 import 'package:analyzer/src/generated/engine.dart' 11 import 'package:analyzer/src/generated/engine.dart'
13 show AnalysisContext, AnalysisOptions, AnalysisOptionsImpl; 12 show AnalysisContext, AnalysisOptions, AnalysisOptionsImpl;
14 import 'package:analyzer/src/generated/source.dart' show Source; 13 import 'package:analyzer/src/generated/source.dart' show Source;
15 import 'package:analyzer/src/generated/utilities_general.dart'; 14 import 'package:analyzer/src/generated/utilities_general.dart';
16 import 'package:analyzer/src/summary/idl.dart' show PackageBundle; 15 import 'package:analyzer/src/summary/idl.dart' show PackageBundle;
17 16
18 /** 17 /**
19 * A Dart SDK installed in a specified location. 18 * A Dart SDK installed in a specified location.
20 */ 19 */
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 */ 283 */
285 static String _IMPLEMENTATION = "implementation"; 284 static String _IMPLEMENTATION = "implementation";
286 285
287 /** 286 /**
288 * The name of the optional parameter used to specify the path used when 287 * The name of the optional parameter used to specify the path used when
289 * compiling for dart2js. 288 * compiling for dart2js.
290 */ 289 */
291 static String _DART2JS_PATH = "dart2jsPath"; 290 static String _DART2JS_PATH = "dart2jsPath";
292 291
293 /** 292 /**
294 * The name of the `dart2js` platform.
295 */
296 static String _DART2JS_PLATFORM = 'DART2JS_PLATFORM';
297
298 /**
299 * The name of the optional parameter used to indicate whether the library is 293 * The name of the optional parameter used to indicate whether the library is
300 * documented. 294 * documented.
301 */ 295 */
302 static String _DOCUMENTED = "documented"; 296 static String _DOCUMENTED = "documented";
303 297
304 /** 298 /**
305 * The name of the optional parameter used to specify the category of the 299 * The name of the optional parameter used to specify the category of the
306 * library. 300 * library.
307 */ 301 */
308 static String _CATEGORIES = "categories"; 302 static String _CATEGORIES = "categories";
309 303
310 /** 304 /**
311 * The name of the optional parameter used to specify the patches for
312 * the library.
313 */
314 static String _PATCHES = "patches";
315
316 /**
317 * The name of the optional parameter used to specify the platforms on which 305 * The name of the optional parameter used to specify the platforms on which
318 * the library can be used. 306 * the library can be used.
319 */ 307 */
320 static String _PLATFORMS = "platforms"; 308 static String _PLATFORMS = "platforms";
321 309
322 /** 310 /**
323 * The value of the [PLATFORMS] parameter used to specify that the library can 311 * The value of the [PLATFORMS] parameter used to specify that the library can
324 * be used on the VM. 312 * be used on the VM.
325 */ 313 */
326 static String _VM_PLATFORM = "VM_PLATFORM"; 314 static String _VM_PLATFORM = "VM_PLATFORM";
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
384 } else if (argument is NamedExpression) { 372 } else if (argument is NamedExpression) {
385 String name = argument.name.label.name; 373 String name = argument.name.label.name;
386 Expression expression = argument.expression; 374 Expression expression = argument.expression;
387 if (name == _CATEGORIES) { 375 if (name == _CATEGORIES) {
388 library.category = 376 library.category =
389 convertCategories((expression as StringLiteral).stringValue); 377 convertCategories((expression as StringLiteral).stringValue);
390 } else if (name == _IMPLEMENTATION) { 378 } else if (name == _IMPLEMENTATION) {
391 library._implementation = (expression as BooleanLiteral).value; 379 library._implementation = (expression as BooleanLiteral).value;
392 } else if (name == _DOCUMENTED) { 380 } else if (name == _DOCUMENTED) {
393 library.documented = (expression as BooleanLiteral).value; 381 library.documented = (expression as BooleanLiteral).value;
394 } else if (name == _PATCHES) {
395 if (expression is MapLiteral) {
396 expression.entries.forEach((MapLiteralEntry entry) {
397 int platforms = _convertPlatforms(entry.key);
398 Expression pathsListLiteral = entry.value;
399 if (pathsListLiteral is ListLiteral) {
400 List<String> paths = <String>[];
401 pathsListLiteral.elements.forEach((Expression pathExpr) {
402 if (pathExpr is SimpleStringLiteral) {
403 String path = pathExpr.value;
404 _validatePatchPath(path);
405 paths.add(path);
406 } else {
407 throw new ArgumentError(
408 'The "patch" argument items must be simple strings.');
409 }
410 });
411 library.setPatchPaths(platforms, paths);
412 } else {
413 throw new ArgumentError(
414 'The "patch" argument values must be list literals.');
415 }
416 });
417 }
418 } else if (name == _PLATFORMS) { 382 } else if (name == _PLATFORMS) {
419 if (expression is SimpleIdentifier) { 383 if (expression is SimpleIdentifier) {
420 String identifier = expression.name; 384 String identifier = expression.name;
421 if (identifier == _VM_PLATFORM) { 385 if (identifier == _VM_PLATFORM) {
422 library.setVmLibrary(); 386 library.setVmLibrary();
423 } else { 387 } else {
424 library.setDart2JsLibrary(); 388 library.setDart2JsLibrary();
425 } 389 }
426 } 390 }
427 } else if (_useDart2jsPaths && name == _DART2JS_PATH) { 391 } else if (_useDart2jsPaths && name == _DART2JS_PATH) {
428 if (expression is SimpleStringLiteral) { 392 if (expression is SimpleStringLiteral) {
429 library.path = expression.value; 393 library.path = expression.value;
430 } 394 }
431 } 395 }
432 } 396 }
433 } 397 }
434 _librariesMap.setLibrary(libraryName, library); 398 _librariesMap.setLibrary(libraryName, library);
435 } 399 }
436 return null; 400 return null;
437 } 401 }
438
439 /**
440 * Validate the given [path] to a patch file. Throw [ArgumentError] if not a
441 * valid path: is absolute, or contains `..`.
442 */
443 void _validatePatchPath(String path) {
444 if (path.contains(r'\')) {
445 throw new ArgumentError('The path to a patch file must be posix: $path');
446 }
447 if (path.contains('..')) {
448 throw new ArgumentError(
449 'The path to a patch file cannot contain "..": $path');
450 }
451 if (path.startsWith('/')) {
452 throw new ArgumentError(
453 'The path to a patch file cannot be absolute: $path');
454 }
455 }
456
457 /**
458 * Return the platform constant value for the given [expr].
459 * Throw [ArgumentError] if not a valid platform name given.
460 */
461 static int _convertPlatform(Expression expr) {
462 if (expr is SimpleIdentifier) {
463 String name = expr.name;
464 if (name == _DART2JS_PLATFORM) {
465 return SdkLibraryImpl.DART2JS_PLATFORM;
466 }
467 if (name == _VM_PLATFORM) {
468 return SdkLibraryImpl.VM_PLATFORM;
469 }
470 throw new ArgumentError('Invalid platform name: $name');
471 }
472 throw new ArgumentError('Invalid platform type: ${expr.runtimeType}');
473 }
474
475 /**
476 * Return the platforms combination value for the [expr], which should be
477 * either `name1 | name2` or `name`. Throw [ArgumentError] if any of the
478 * names is not a valid platform name.
479 */
480 static int _convertPlatforms(Expression expr) {
481 if (expr is BinaryExpression) {
482 TokenType operator = expr.operator?.type;
483 if (operator == TokenType.BAR) {
484 return _convertPlatforms(expr.leftOperand) |
485 _convertPlatforms(expr.rightOperand);
486 } else {
487 throw new ArgumentError('Invalid platforms combination: $operator');
488 }
489 } else {
490 return _convertPlatform(expr);
491 }
492 }
493 } 402 }
494 403
495 /** 404 /**
496 * Represents a single library in the SDK 405 * Represents a single library in the SDK
497 */ 406 */
498 abstract class SdkLibrary { 407 abstract class SdkLibrary {
499 /** 408 /**
500 * Return the name of the category containing the library. 409 * Return the name of the category containing the library.
501 */ 410 */
502 String get category; 411 String get category;
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
535 * Return the path to the file defining the library. The path is relative to 444 * Return the path to the file defining the library. The path is relative to
536 * the `lib` directory within the SDK. 445 * the `lib` directory within the SDK.
537 */ 446 */
538 String get path; 447 String get path;
539 448
540 /** 449 /**
541 * Return the short name of the library. This is the URI of the library, 450 * Return the short name of the library. This is the URI of the library,
542 * including `dart:`. 451 * including `dart:`.
543 */ 452 */
544 String get shortName; 453 String get shortName;
545
546 /**
547 * Return the list of paths to the patch files that should be applied
548 * to this library for the given [platform], not `null`.
549 */
550 List<String> getPatches(int platform);
551 } 454 }
552 455
553 /** 456 /**
554 * The information known about a single library within the SDK. 457 * The information known about a single library within the SDK.
555 */ 458 */
556 class SdkLibraryImpl implements SdkLibrary { 459 class SdkLibraryImpl implements SdkLibrary {
557 /** 460 /**
558 * The bit mask used to access the bit representing the flag indicating 461 * The bit mask used to access the bit representing the flag indicating
559 * whether a library is intended to work on the dart2js platform. 462 * whether a library is intended to work on the dart2js platform.
560 */ 463 */
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
593 * A flag indicating whether the library is an implementation library. 496 * A flag indicating whether the library is an implementation library.
594 */ 497 */
595 bool _implementation = false; 498 bool _implementation = false;
596 499
597 /** 500 /**
598 * An encoding of which platforms this library is intended to work on. 501 * An encoding of which platforms this library is intended to work on.
599 */ 502 */
600 int _platforms = 0; 503 int _platforms = 0;
601 504
602 /** 505 /**
603 * The mapping from the platform combination to the list of paths (relative
604 * to the `sdk/lib` folder) of patches that should be applied to this library
605 * on every platform in the combination.
606 */
607 final Map<int, List<String>> _platformsToPatchPaths =
608 new HashMap<int, List<String>>();
609
610 /**
611 * Initialize a newly created library to represent the library with the given 506 * Initialize a newly created library to represent the library with the given
612 * [name]. 507 * [name].
613 */ 508 */
614 SdkLibraryImpl(this.shortName); 509 SdkLibraryImpl(this.shortName);
615 510
616 /** 511 /**
617 * Set whether the library is documented. 512 * Set whether the library is documented.
618 */ 513 */
619 void set documented(bool documented) { 514 void set documented(bool documented) {
620 this._documented = documented; 515 this._documented = documented;
(...skipping 10 matching lines...) Expand all
631 526
632 @override 527 @override
633 bool get isInternal => category == "Internal"; 528 bool get isInternal => category == "Internal";
634 529
635 @override 530 @override
636 bool get isShared => category == "Shared"; 531 bool get isShared => category == "Shared";
637 532
638 @override 533 @override
639 bool get isVmLibrary => (_platforms & VM_PLATFORM) != 0; 534 bool get isVmLibrary => (_platforms & VM_PLATFORM) != 0;
640 535
641 @override
642 List<String> getPatches(int platform) {
643 List<String> paths = <String>[];
644 _platformsToPatchPaths.forEach((int platforms, List<String> value) {
645 if ((platforms & platform) != 0) {
646 paths.addAll(value);
647 }
648 });
649 return paths;
650 }
651
652 /** 536 /**
653 * Record that this library can be compiled to JavaScript by dart2js. 537 * Record that this library can be compiled to JavaScript by dart2js.
654 */ 538 */
655 void setDart2JsLibrary() { 539 void setDart2JsLibrary() {
656 _platforms |= DART2JS_PLATFORM; 540 _platforms |= DART2JS_PLATFORM;
657 } 541 }
658 542
659 /** 543 /**
660 * Add a new patch with the given [path] that should be applied for the
661 * given [platforms].
662 */
663 void setPatchPaths(int platforms, List<String> paths) {
664 _platformsToPatchPaths[platforms] = paths;
665 }
666
667 /**
668 * Record that this library can be run on the VM. 544 * Record that this library can be run on the VM.
669 */ 545 */
670 void setVmLibrary() { 546 void setVmLibrary() {
671 _platforms |= VM_PLATFORM; 547 _platforms |= VM_PLATFORM;
672 } 548 }
673 } 549 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698