OLD | NEW |
1 #!/usr/bin/env dart | 1 #!/usr/bin/env dart |
2 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 2 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
3 // for details. All rights reserved. Use of this source code is governed by a | 3 // for details. All rights reserved. Use of this source code is governed by a |
4 // BSD-style license that can be found in the LICENSE file. | 4 // BSD-style license that can be found in the LICENSE file. |
5 | 5 |
6 /// Command line tool to merge the SDK libraries and our patch files. | 6 /// Command line tool to merge the SDK libraries and our patch files. |
7 /// This is currently designed as an offline tool, but we could automate it. | 7 /// This is currently designed as an offline tool, but we could automate it. |
8 | 8 |
9 import 'dart:io'; | 9 import 'dart:io'; |
10 import 'dart:isolate' show RawReceivePort; | 10 import 'dart:isolate' show RawReceivePort; |
(...skipping 579 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
590 } | 590 } |
591 | 591 |
592 void _maybePatch(AstNode node) { | 592 void _maybePatch(AstNode node) { |
593 if (node is FieldDeclaration) return; | 593 if (node is FieldDeclaration) return; |
594 | 594 |
595 var externalKeyword = (node as dynamic).externalKeyword; | 595 var externalKeyword = (node as dynamic).externalKeyword; |
596 | 596 |
597 var name = _qualifiedName(node); | 597 var name = _qualifiedName(node); |
598 var patchNode = patch.patches[name]; | 598 var patchNode = patch.patches[name]; |
599 if (patchNode == null) { | 599 if (patchNode == null) { |
600 if (externalKeyword != null) { | 600 if (externalKeyword != null && _shouldHaveImplementation(name)) { |
601 print('warning: patch not found for $name: $node'); | 601 print('warning: patch not found for $name: $node'); |
| 602 exitCode = 1; |
602 } | 603 } |
603 return; | 604 return; |
604 } | 605 } |
605 patch.applied.add(name); | 606 patch.applied.add(name); |
606 | 607 |
607 Annotation patchMeta = patchNode.metadata.lastWhere(_isPatchAnnotation); | 608 Annotation patchMeta = patchNode.metadata.lastWhere(_isPatchAnnotation); |
608 int start = patchMeta.endToken.next.offset; | 609 int start = patchMeta.endToken.next.offset; |
609 var code = patch.contents.substring(start, patchNode.end); | 610 var code = patch.contents.substring(start, patchNode.end); |
610 | 611 |
611 // For some node like static fields, the node's offset doesn't include | 612 // For some node like static fields, the node's offset doesn't include |
612 // the external keyword. Also starting from the keyword lets us preserve | 613 // the external keyword. Also starting from the keyword lets us preserve |
613 // documentation comments. | 614 // documentation comments. |
614 edits.replace(externalKeyword?.offset ?? node.offset, node.end, code); | 615 edits.replace(externalKeyword?.offset ?? node.offset, node.end, code); |
615 } | 616 } |
616 } | 617 } |
617 | 618 |
| 619 /// Whether a member should have an implementation after patching the SDK. |
| 620 /// |
| 621 /// True for most members except for the *.fromEnvironment constructors under |
| 622 /// the dart2js target. |
| 623 bool _shouldHaveImplementation(String qualifiedName) { |
| 624 if (!forDart2js) return true; |
| 625 // Note: dart2js implements int.fromEnvironment, bool.fromEnvironment |
| 626 // and String.fromEnvironment directly and expects the SDK code to |
| 627 // have an external declaration. |
| 628 var isFromEnvironment = const [ |
| 629 'bool.fromEnvironment', |
| 630 'int.fromEnvironment', |
| 631 'String.fromEnvironment' |
| 632 ].contains(qualifiedName); |
| 633 return !isFromEnvironment; |
| 634 } |
| 635 |
618 class PatchFinder extends GeneralizingAstVisitor { | 636 class PatchFinder extends GeneralizingAstVisitor { |
619 final String contents; | 637 final String contents; |
620 final CompilationUnit unit; | 638 final CompilationUnit unit; |
621 | 639 |
622 final Map patches = <String, Declaration>{}; | 640 final Map patches = <String, Declaration>{}; |
623 final Map mergeMembers = <String, List<ClassMember>>{}; | 641 final Map mergeMembers = <String, List<ClassMember>>{}; |
624 final List mergeDeclarations = <CompilationUnitMember>[]; | 642 final List mergeDeclarations = <CompilationUnitMember>[]; |
625 final Set<String> applied = new Set<String>(); | 643 final Set<String> applied = new Set<String>(); |
626 | 644 |
627 PatchFinder.parseAndVisit(String name, String contents) | 645 PatchFinder.parseAndVisit(String name, String contents) |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
781 if (diff != 0) return diff; | 799 if (diff != 0) return diff; |
782 return end - other.end; | 800 return end - other.end; |
783 } | 801 } |
784 } | 802 } |
785 | 803 |
786 List<SdkLibrary> _getSdkLibraries(String contents) { | 804 List<SdkLibrary> _getSdkLibraries(String contents) { |
787 var libraryBuilder = new SdkLibrariesReader_LibraryBuilder(forDart2js); | 805 var libraryBuilder = new SdkLibrariesReader_LibraryBuilder(forDart2js); |
788 parseCompilationUnit(contents).accept(libraryBuilder); | 806 parseCompilationUnit(contents).accept(libraryBuilder); |
789 return libraryBuilder.librariesMap.sdkLibraries; | 807 return libraryBuilder.librariesMap.sdkLibraries; |
790 } | 808 } |
OLD | NEW |