OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 /** | 5 /** |
6 * This library contains the infrastructure to parse and integrate patch files. | 6 * This library contains the infrastructure to parse and integrate patch files. |
7 * | 7 * |
8 * Three types of elements can be patched: [LibraryElement], [ClassElement], | 8 * Three types of elements can be patched: [LibraryElement], [ClassElement], |
9 * [FunctionElement]. Patches are introduced in patch libraries which are loaded | 9 * [FunctionElement]. Patches are introduced in patch libraries which are loaded |
10 * together with the corresponding origin library. Which libraries that are | 10 * together with the corresponding origin library. Which libraries that are |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 * TODO(johnniwinther): Simplify this invariant to use only declarations in | 107 * TODO(johnniwinther): Simplify this invariant to use only declarations in |
108 * [tree.TreeElements]. | 108 * [tree.TreeElements]. |
109 * - Builders shift between declaration and implementation depending on usages. | 109 * - Builders shift between declaration and implementation depending on usages. |
110 * - Compile-time constants use constructor implementation exclusively. | 110 * - Compile-time constants use constructor implementation exclusively. |
111 * - Work on function parameters is performed on the declaration of the function | 111 * - Work on function parameters is performed on the declaration of the function |
112 * element. | 112 * element. |
113 */ | 113 */ |
114 | 114 |
115 library patchparser; | 115 library patchparser; |
116 | 116 |
| 117 import 'dart:async'; |
| 118 |
117 import "tree/tree.dart" as tree; | 119 import "tree/tree.dart" as tree; |
118 import "dart2jslib.dart" as leg; // CompilerTask, Compiler. | 120 import "dart2jslib.dart" as leg; // CompilerTask, Compiler. |
119 import "../compiler.dart" as api; | 121 import "../compiler.dart" as api; |
120 import "scanner/scannerlib.dart"; // Scanner, Parsers, Listeners | 122 import "scanner/scannerlib.dart"; // Scanner, Parsers, Listeners |
121 import "elements/elements.dart"; | 123 import "elements/elements.dart"; |
122 import "elements/modelx.dart" show LibraryElementX, MetadataAnnotationX; | 124 import "elements/modelx.dart" show LibraryElementX, MetadataAnnotationX; |
123 import 'util/util.dart'; | 125 import 'util/util.dart'; |
124 | 126 |
125 class PatchParserTask extends leg.CompilerTask { | 127 class PatchParserTask extends leg.CompilerTask { |
126 PatchParserTask(leg.Compiler compiler): super(compiler); | 128 PatchParserTask(leg.Compiler compiler): super(compiler); |
127 final String name = "Patching Parser"; | 129 final String name = "Patching Parser"; |
128 | 130 |
129 /** | 131 /** |
130 * Scans a library patch file, applies the method patches and | 132 * Scans a library patch file, applies the method patches and |
131 * injections to the library, and returns a list of class | 133 * injections to the library, and returns a list of class |
132 * patches. | 134 * patches. |
133 */ | 135 */ |
134 void patchLibrary(leg.LibraryDependencyHandler handler, | 136 Future patchLibrary(leg.LibraryDependencyHandler handler, |
135 Uri patchUri, LibraryElement originLibrary) { | 137 Uri patchUri, LibraryElement originLibrary) { |
136 | 138 return compiler.readScript(patchUri, null).then((leg.Script script) { |
137 leg.Script script = compiler.readScript(patchUri, null); | 139 var patchLibrary = new LibraryElementX(script, null, originLibrary); |
138 var patchLibrary = new LibraryElementX(script, null, originLibrary); | 140 return compiler.withCurrentElement(patchLibrary, () { |
139 compiler.withCurrentElement(patchLibrary, () { | 141 handler.registerNewLibrary(patchLibrary); |
140 handler.registerNewLibrary(patchLibrary); | 142 var imports = new LinkBuilder<tree.LibraryTag>(); |
141 LinkBuilder<tree.LibraryTag> imports = new LinkBuilder<tree.LibraryTag>(); | 143 compiler.withCurrentElement(patchLibrary.entryCompilationUnit, () { |
142 compiler.withCurrentElement(patchLibrary.entryCompilationUnit, () { | 144 // This patches the elements of the patch library into [library]. |
143 // This patches the elements of the patch library into [library]. | 145 // Injected elements are added directly under the compilation unit. |
144 // Injected elements are added directly under the compilation unit. | 146 // Patch elements are stored on the patched functions or classes. |
145 // Patch elements are stored on the patched functions or classes. | 147 scanLibraryElements(patchLibrary.entryCompilationUnit, imports); |
146 scanLibraryElements(patchLibrary.entryCompilationUnit, imports); | 148 }); |
| 149 // After scanning declarations, we handle the import tags in the patch. |
| 150 // TODO(lrn): These imports end up in the original library and are in |
| 151 // scope for the original methods too. This should be fixed. |
| 152 compiler.importHelperLibrary(originLibrary); |
| 153 // TODO(rnystrom): Remove .toList() here if #11523 is fixed. |
| 154 return Future.forEach(imports.toLink().toList(), (tag) { |
| 155 return compiler.withCurrentElement(patchLibrary, () { |
| 156 return compiler.libraryLoader.registerLibraryFromTag( |
| 157 handler, patchLibrary, tag); |
| 158 }); |
| 159 }); |
147 }); | 160 }); |
148 // After scanning declarations, we handle the import tags in the patch. | |
149 // TODO(lrn): These imports end up in the original library and are in | |
150 // scope for the original methods too. This should be fixed. | |
151 compiler.importHelperLibrary(originLibrary); | |
152 for (tree.LibraryTag tag in imports.toLink()) { | |
153 compiler.libraryLoader.registerLibraryFromTag( | |
154 handler, patchLibrary, tag); | |
155 } | |
156 }); | 161 }); |
157 } | 162 } |
158 | 163 |
159 void scanLibraryElements( | 164 void scanLibraryElements( |
160 CompilationUnitElement compilationUnit, | 165 CompilationUnitElement compilationUnit, |
161 LinkBuilder<tree.LibraryTag> imports) { | 166 LinkBuilder<tree.LibraryTag> imports) { |
162 measure(() { | 167 measure(() { |
163 // TODO(lrn): Possibly recursively handle 'part' directives in patch. | 168 // TODO(lrn): Possibly recursively handle 'part' directives in patch. |
164 leg.Script script = compilationUnit.script; | 169 leg.Script script = compilationUnit.script; |
165 Token tokens = new StringScanner(script.text).tokenize(); | 170 Token tokens = new StringScanner(script.text).tokenize(); |
(...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
549 | 554 |
550 // TODO(johnniwinther): Add unittest when patch is (real) metadata. | 555 // TODO(johnniwinther): Add unittest when patch is (real) metadata. |
551 bool isPatchElement(Element element) { | 556 bool isPatchElement(Element element) { |
552 // TODO(lrn): More checks needed if we introduce metadata for real. | 557 // TODO(lrn): More checks needed if we introduce metadata for real. |
553 // In that case, it must have the identifier "native" as metadata. | 558 // In that case, it must have the identifier "native" as metadata. |
554 for (Link link = element.metadata; !link.isEmpty; link = link.tail) { | 559 for (Link link = element.metadata; !link.isEmpty; link = link.tail) { |
555 if (link.head is PatchMetadataAnnotation) return true; | 560 if (link.head is PatchMetadataAnnotation) return true; |
556 } | 561 } |
557 return false; | 562 return false; |
558 } | 563 } |
OLD | NEW |