OLD | NEW |
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 deferred_load; | 5 library deferred_load; |
6 | 6 |
7 import 'common/backend_api.dart' show Backend; | 7 import 'common/backend_api.dart' show Backend; |
8 import 'common/tasks.dart' show CompilerTask; | 8 import 'common/tasks.dart' show CompilerTask; |
9 import 'common.dart'; | 9 import 'common.dart'; |
10 import 'compiler.dart' show Compiler; | 10 import 'compiler.dart' show Compiler; |
(...skipping 18 matching lines...) Expand all Loading... |
29 ImportElement, | 29 ImportElement, |
30 LibraryElement, | 30 LibraryElement, |
31 MetadataAnnotation, | 31 MetadataAnnotation, |
32 PrefixElement, | 32 PrefixElement, |
33 ResolvedAstKind, | 33 ResolvedAstKind, |
34 TypedefElement; | 34 TypedefElement; |
35 import 'js_backend/js_backend.dart' show JavaScriptBackend; | 35 import 'js_backend/js_backend.dart' show JavaScriptBackend; |
36 import 'resolution/resolution.dart' show AnalyzableElementX; | 36 import 'resolution/resolution.dart' show AnalyzableElementX; |
37 import 'resolution/tree_elements.dart' show TreeElements; | 37 import 'resolution/tree_elements.dart' show TreeElements; |
38 import 'tree/tree.dart' as ast; | 38 import 'tree/tree.dart' as ast; |
39 import 'tree/tree.dart' show Import, Node; | |
40 import 'universe/use.dart' show StaticUse, TypeUse, TypeUseKind; | 39 import 'universe/use.dart' show StaticUse, TypeUse, TypeUseKind; |
41 import 'universe/world_impact.dart' | 40 import 'universe/world_impact.dart' |
42 show ImpactUseCase, WorldImpact, WorldImpactVisitorImpl; | 41 show ImpactUseCase, WorldImpact, WorldImpactVisitorImpl; |
43 import 'util/setlet.dart' show Setlet; | 42 import 'util/setlet.dart' show Setlet; |
44 import 'util/uri_extras.dart' as uri_extras; | 43 import 'util/uri_extras.dart' as uri_extras; |
45 import 'util/util.dart' show Link, makeUnique; | 44 import 'util/util.dart' show Link, makeUnique; |
46 | 45 |
47 /// A "hunk" of the program that will be loaded whenever one of its [imports] | 46 /// A "hunk" of the program that will be loaded whenever one of its [imports] |
48 /// are loaded. | 47 /// are loaded. |
49 /// | 48 /// |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 /// For each deferred import, find elements and constants to be loaded when that | 85 /// For each deferred import, find elements and constants to be loaded when that |
87 /// import is loaded. Elements that are used by several deferred imports are in | 86 /// import is loaded. Elements that are used by several deferred imports are in |
88 /// shared OutputUnits. | 87 /// shared OutputUnits. |
89 class DeferredLoadTask extends CompilerTask { | 88 class DeferredLoadTask extends CompilerTask { |
90 /// The name of this task. | 89 /// The name of this task. |
91 String get name => 'Deferred Loading'; | 90 String get name => 'Deferred Loading'; |
92 | 91 |
93 /// DeferredLibrary from dart:async | 92 /// DeferredLibrary from dart:async |
94 ClassElement get deferredLibraryClass => compiler.deferredLibraryClass; | 93 ClassElement get deferredLibraryClass => compiler.deferredLibraryClass; |
95 | 94 |
96 /// A synthetic [Import] representing the loading of the main | 95 /// A synthetic import representing the loading of the main program. |
97 /// program. | |
98 final _DeferredImport _fakeMainImport = const _DeferredImport(); | 96 final _DeferredImport _fakeMainImport = const _DeferredImport(); |
99 | 97 |
100 /// The OutputUnit that will be loaded when the program starts. | 98 /// The OutputUnit that will be loaded when the program starts. |
101 final OutputUnit mainOutputUnit = new OutputUnit(isMainOutput: true); | 99 final OutputUnit mainOutputUnit = new OutputUnit(isMainOutput: true); |
102 | 100 |
103 /// A set containing (eventually) all output units that will result from the | 101 /// A set containing (eventually) all output units that will result from the |
104 /// program. | 102 /// program. |
105 final Set<OutputUnit> allOutputUnits = new Set<OutputUnit>(); | 103 final Set<OutputUnit> allOutputUnits = new Set<OutputUnit>(); |
106 | 104 |
107 /// Will be `true` if the program contains deferred libraries. | 105 /// Will be `true` if the program contains deferred libraries. |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
228 // If the element is not imported explicitly, it is implicitly imported | 226 // If the element is not imported explicitly, it is implicitly imported |
229 // not deferred. | 227 // not deferred. |
230 if (imports.isEmpty) return false; | 228 if (imports.isEmpty) return false; |
231 // An element could potentially be loaded by several imports. If all of them | 229 // An element could potentially be loaded by several imports. If all of them |
232 // is explicitly deferred, we say the element is explicitly deferred. | 230 // is explicitly deferred, we say the element is explicitly deferred. |
233 // TODO(sigurdm): We might want to give a warning if the imports do not | 231 // TODO(sigurdm): We might want to give a warning if the imports do not |
234 // agree. | 232 // agree. |
235 return imports.every((ImportElement import) => import.isDeferred); | 233 return imports.every((ImportElement import) => import.isDeferred); |
236 } | 234 } |
237 | 235 |
238 /// Returns a [Link] of every [Import] that imports [element] into [library]. | 236 /// Returns every [ImportElement] that imports [element] into [library]. |
239 Iterable<ImportElement> _getImports(Element element, LibraryElement library) { | 237 Iterable<ImportElement> _getImports(Element element, LibraryElement library) { |
240 if (element.isClassMember) { | 238 if (element.isClassMember) { |
241 element = element.enclosingClass; | 239 element = element.enclosingClass; |
242 } | 240 } |
243 if (element.isAccessor) { | 241 if (element.isAccessor) { |
244 element = (element as AccessorElement).abstractField; | 242 element = (element as AccessorElement).abstractField; |
245 } | 243 } |
246 return library.getImportsFor(element); | 244 return library.getImportsFor(element); |
247 } | 245 } |
248 | 246 |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 TreeElements treeElements = analyzableElement.resolvedAst.elements; | 338 TreeElements treeElements = analyzableElement.resolvedAst.elements; |
341 assert(treeElements != null); | 339 assert(treeElements != null); |
342 | 340 |
343 // TODO(johnniwinther): Add only expressions that are actually needed. | 341 // TODO(johnniwinther): Add only expressions that are actually needed. |
344 // Currently we have some noise here: Some potential expressions are | 342 // Currently we have some noise here: Some potential expressions are |
345 // seen that should never be added (for instance field initializers | 343 // seen that should never be added (for instance field initializers |
346 // in constant constructors, like `this.field = parameter`). And some | 344 // in constant constructors, like `this.field = parameter`). And some |
347 // implicit constant expression are seen that we should be able to add | 345 // implicit constant expression are seen that we should be able to add |
348 // (like primitive constant literals like `true`, `"foo"` and `0`). | 346 // (like primitive constant literals like `true`, `"foo"` and `0`). |
349 // See dartbug.com/26406 for context. | 347 // See dartbug.com/26406 for context. |
350 treeElements | 348 treeElements.forEachConstantNode( |
351 .forEachConstantNode((Node node, ConstantExpression expression) { | 349 (ast.Node node, ConstantExpression expression) { |
352 if (compiler.serialization.isDeserialized(analyzableElement)) { | 350 if (compiler.serialization.isDeserialized(analyzableElement)) { |
353 if (!expression.isImplicit && !expression.isPotential) { | 351 if (!expression.isImplicit && !expression.isPotential) { |
354 // Enforce evaluation of [expression]. | 352 // Enforce evaluation of [expression]. |
355 backend.constants.getConstantValue(expression); | 353 backend.constants.getConstantValue(expression); |
356 } | 354 } |
357 } | 355 } |
358 | 356 |
359 // Explicitly depend on the backend constants. | 357 // Explicitly depend on the backend constants. |
360 if (backend.constants.hasConstantValue(expression)) { | 358 if (backend.constants.hasConstantValue(expression)) { |
361 ConstantValue value = | 359 ConstantValue value = |
(...skipping 662 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1024 | 1022 |
1025 bool operator ==(other) { | 1023 bool operator ==(other) { |
1026 if (other is! _DeclaredDeferredImport) return false; | 1024 if (other is! _DeclaredDeferredImport) return false; |
1027 return declaration == other.declaration; | 1025 return declaration == other.declaration; |
1028 } | 1026 } |
1029 | 1027 |
1030 int get hashCode => declaration.hashCode * 17; | 1028 int get hashCode => declaration.hashCode * 17; |
1031 | 1029 |
1032 String toString() => '$declaration'; | 1030 String toString() => '$declaration'; |
1033 } | 1031 } |
OLD | NEW |