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 library dart2js.js.enqueue; | 5 library dart2js.js.enqueue; |
6 | 6 |
7 import 'dart:collection' show Queue; | 7 import 'dart:collection' show Queue; |
8 | 8 |
| 9 import '../cache_strategy.dart' show CacheStrategy; |
9 import '../common/backend_api.dart' show Backend; | 10 import '../common/backend_api.dart' show Backend; |
10 import '../common/codegen.dart' show CodegenWorkItem; | 11 import '../common/codegen.dart' show CodegenWorkItem; |
11 import '../common/names.dart' show Identifiers; | 12 import '../common/names.dart' show Identifiers; |
12 import '../common/tasks.dart' show CompilerTask; | 13 import '../common/tasks.dart' show CompilerTask; |
13 import '../common/work.dart' show WorkItem; | 14 import '../common/work.dart' show WorkItem; |
14 import '../common.dart'; | 15 import '../common.dart'; |
15 import '../compiler.dart' show Compiler; | 16 import '../compiler.dart' show Compiler; |
16 import '../dart_types.dart' show DartType, InterfaceType; | 17 import '../dart_types.dart' show DartType, InterfaceType; |
| 18 import '../dump_info.dart'; |
17 import '../elements/elements.dart' | 19 import '../elements/elements.dart' |
18 show | 20 show |
19 ClassElement, | 21 ClassElement, |
20 Element, | 22 Element, |
21 Entity, | 23 Entity, |
22 FunctionElement, | 24 FunctionElement, |
23 MemberElement, | 25 MemberElement, |
24 MethodElement, | 26 MethodElement, |
25 TypedElement; | 27 TypedElement; |
26 import '../elements/entities.dart'; | 28 import '../elements/entities.dart'; |
27 import '../enqueue.dart'; | 29 import '../enqueue.dart'; |
28 import '../js/js.dart' as js; | 30 import '../js/js.dart' as js; |
29 import '../native/native.dart' as native; | 31 import '../native/native.dart' as native; |
30 import '../options.dart'; | 32 import '../options.dart'; |
31 import '../types/types.dart' show TypeMaskStrategy; | 33 import '../types/types.dart' show TypeMaskStrategy; |
32 import '../universe/selector.dart' show Selector; | 34 import '../universe/selector.dart' show Selector; |
33 import '../universe/world_builder.dart'; | 35 import '../universe/world_builder.dart'; |
34 import '../universe/use.dart' | 36 import '../universe/use.dart' |
35 show DynamicUse, StaticUse, StaticUseKind, TypeUse, TypeUseKind; | 37 show DynamicUse, StaticUse, StaticUseKind, TypeUse, TypeUseKind; |
36 import '../universe/world_impact.dart' | 38 import '../universe/world_impact.dart' |
37 show ImpactUseCase, WorldImpact, WorldImpactVisitor; | 39 show ImpactUseCase, WorldImpact, WorldImpactVisitor; |
38 import '../util/util.dart' show Setlet; | 40 import '../util/util.dart' show Setlet; |
39 import '../world.dart'; | 41 import '../world.dart'; |
40 | 42 |
41 /// [Enqueuer] which is specific to code generation. | 43 /// [Enqueuer] which is specific to code generation. |
42 class CodegenEnqueuer extends EnqueuerImpl { | 44 class CodegenEnqueuer extends EnqueuerImpl { |
43 final String name; | 45 final String name; |
44 @deprecated | |
45 final Compiler _compiler; // TODO(ahe): Remove this dependency. | |
46 final EnqueuerStrategy strategy; | 46 final EnqueuerStrategy strategy; |
47 final Map<String, Set<Element>> _instanceMembersByName = | 47 final Map<String, Set<Element>> _instanceMembersByName = |
48 new Map<String, Set<Element>>(); | 48 new Map<String, Set<Element>>(); |
49 final Map<String, Set<Element>> _instanceFunctionsByName = | 49 final Map<String, Set<Element>> _instanceFunctionsByName = |
50 new Map<String, Set<Element>>(); | 50 new Map<String, Set<Element>>(); |
51 final Set<ClassElement> _processedClasses = new Set<ClassElement>(); | 51 final Set<ClassElement> _processedClasses = new Set<ClassElement>(); |
52 Set<ClassElement> _recentClasses = new Setlet<ClassElement>(); | 52 Set<ClassElement> _recentClasses = new Setlet<ClassElement>(); |
53 final CodegenWorldBuilderImpl _universe = | 53 final CodegenWorldBuilderImpl _universe = |
54 new CodegenWorldBuilderImpl(const TypeMaskStrategy()); | 54 new CodegenWorldBuilderImpl(const TypeMaskStrategy()); |
55 | 55 |
56 bool queueIsClosed = false; | 56 bool queueIsClosed = false; |
57 final CompilerTask task; | 57 final CompilerTask task; |
58 final native.NativeEnqueuer nativeEnqueuer; | 58 final native.NativeEnqueuer nativeEnqueuer; |
| 59 final Backend _backend; |
| 60 final CompilerOptions _options; |
59 | 61 |
60 WorldImpactVisitor _impactVisitor; | 62 WorldImpactVisitor _impactVisitor; |
61 | 63 |
62 final Queue<WorkItem> queue = new Queue<WorkItem>(); | 64 final Queue<WorkItem> _queue = new Queue<WorkItem>(); |
63 final Map<Element, js.Expression> generatedCode = <Element, js.Expression>{}; | 65 final Map<Element, js.Expression> generatedCode = <Element, js.Expression>{}; |
64 | 66 |
65 final Set<Element> newlyEnqueuedElements; | 67 final Set<Element> newlyEnqueuedElements; |
66 | 68 |
67 final Set<DynamicUse> newlySeenSelectors; | 69 final Set<DynamicUse> newlySeenSelectors; |
68 | 70 |
69 bool _enabledNoSuchMethod = false; | 71 bool _enabledNoSuchMethod = false; |
70 | 72 |
71 static const ImpactUseCase IMPACT_USE = | 73 static const ImpactUseCase IMPACT_USE = |
72 const ImpactUseCase('CodegenEnqueuer'); | 74 const ImpactUseCase('CodegenEnqueuer'); |
73 | 75 |
74 CodegenEnqueuer(this.task, Compiler compiler, this.strategy) | 76 CodegenEnqueuer(this.task, CacheStrategy cacheStrategy, Backend backend, |
75 : newlyEnqueuedElements = compiler.cacheStrategy.newSet(), | 77 this._options, this.strategy) |
76 newlySeenSelectors = compiler.cacheStrategy.newSet(), | 78 : newlyEnqueuedElements = cacheStrategy.newSet(), |
77 nativeEnqueuer = compiler.backend.nativeCodegenEnqueuer(), | 79 newlySeenSelectors = cacheStrategy.newSet(), |
78 this.name = 'codegen enqueuer', | 80 nativeEnqueuer = backend.nativeCodegenEnqueuer(), |
79 this._compiler = compiler { | 81 this._backend = backend, |
| 82 this.name = 'codegen enqueuer' { |
80 _impactVisitor = new EnqueuerImplImpactVisitor(this); | 83 _impactVisitor = new EnqueuerImplImpactVisitor(this); |
81 } | 84 } |
82 | 85 |
83 CodegenWorldBuilder get universe => _universe; | 86 CodegenWorldBuilder get universe => _universe; |
84 | 87 |
85 Backend get _backend => _compiler.backend; | 88 // TODO(johnniwinther): Remove these hacks: |
| 89 ClosedWorld get _world => _backend.compiler.closedWorld; |
| 90 DumpInfoTask get _dumpInfoTask => _backend.compiler.dumpInfoTask; |
86 | 91 |
87 CompilerOptions get _options => _compiler.options; | 92 bool get queueIsEmpty => _queue.isEmpty; |
88 | |
89 ClosedWorld get _world => _compiler.closedWorld; | |
90 | |
91 bool get queueIsEmpty => queue.isEmpty; | |
92 | 93 |
93 /// Returns [:true:] if this enqueuer is the resolution enqueuer. | 94 /// Returns [:true:] if this enqueuer is the resolution enqueuer. |
94 bool get isResolutionQueue => false; | 95 bool get isResolutionQueue => false; |
95 | 96 |
96 /** | 97 /** |
97 * Documentation wanted -- johnniwinther | 98 * Documentation wanted -- johnniwinther |
98 * | 99 * |
99 * Invariant: [element] must be a declaration element. | 100 * Invariant: [element] must be a declaration element. |
100 */ | 101 */ |
101 void _addToWorkList(Element element) { | 102 void _addToWorkList(Element element) { |
(...skipping 11 matching lines...) Expand all Loading... |
113 } | 114 } |
114 | 115 |
115 if (_options.hasIncrementalSupport && !isProcessed(element)) { | 116 if (_options.hasIncrementalSupport && !isProcessed(element)) { |
116 newlyEnqueuedElements.add(element); | 117 newlyEnqueuedElements.add(element); |
117 } | 118 } |
118 | 119 |
119 if (queueIsClosed) { | 120 if (queueIsClosed) { |
120 throw new SpannableAssertionFailure( | 121 throw new SpannableAssertionFailure( |
121 element, "Codegen work list is closed. Trying to add $element"); | 122 element, "Codegen work list is closed. Trying to add $element"); |
122 } | 123 } |
123 queue.add(new CodegenWorkItem(_backend, element)); | 124 _queue.add(new CodegenWorkItem(_backend, element)); |
124 // TODO(sigmund): add other missing dependencies (internals, selectors | 125 // TODO(sigmund): add other missing dependencies (internals, selectors |
125 // enqueued after allocations). | 126 // enqueued after allocations). |
126 _compiler.dumpInfoTask | 127 _dumpInfoTask.registerDependency(element); |
127 .registerDependency(_compiler.currentElement, element); | |
128 } | 128 } |
129 | 129 |
130 void applyImpact(WorldImpact worldImpact, {Element impactSource}) { | 130 void applyImpact(WorldImpact worldImpact, {Element impactSource}) { |
131 if (worldImpact.isEmpty) return; | 131 if (worldImpact.isEmpty) return; |
132 impactStrategy.visitImpact( | 132 impactStrategy.visitImpact( |
133 impactSource, worldImpact, _impactVisitor, impactUse); | 133 impactSource, worldImpact, _impactVisitor, impactUse); |
134 } | 134 } |
135 | 135 |
136 void _registerInstantiatedType(InterfaceType type, | 136 void _registerInstantiatedType(InterfaceType type, |
137 {bool mirrorUsage: false, bool nativeUsage: false}) { | 137 {bool mirrorUsage: false, bool nativeUsage: false}) { |
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
401 if (_options.enableTypeAssertions) { | 401 if (_options.enableTypeAssertions) { |
402 _registerIsCheck(type); | 402 _registerIsCheck(type); |
403 } | 403 } |
404 break; | 404 break; |
405 case TypeUseKind.TYPE_LITERAL: | 405 case TypeUseKind.TYPE_LITERAL: |
406 break; | 406 break; |
407 } | 407 } |
408 } | 408 } |
409 | 409 |
410 void _registerIsCheck(DartType type) { | 410 void _registerIsCheck(DartType type) { |
411 type = _universe.registerIsCheck(type, _compiler.resolution); | 411 type = _universe.registerIsCheck(type); |
412 // Even in checked mode, type annotations for return type and argument | 412 // Even in checked mode, type annotations for return type and argument |
413 // types do not imply type checks, so there should never be a check | 413 // types do not imply type checks, so there should never be a check |
414 // against the type variable of a typedef. | 414 // against the type variable of a typedef. |
415 assert(!type.isTypeVariable || !type.element.enclosingElement.isTypedef); | 415 assert(!type.isTypeVariable || !type.element.enclosingElement.isTypedef); |
416 } | 416 } |
417 | 417 |
418 void _registerCallMethodWithFreeTypeVariables(Element element) { | 418 void _registerCallMethodWithFreeTypeVariables(Element element) { |
419 applyImpact(_backend.registerCallMethodWithFreeTypeVariables(element, | 419 applyImpact(_backend.registerCallMethodWithFreeTypeVariables(element, |
420 forResolution: false)); | 420 forResolution: false)); |
421 } | 421 } |
422 | 422 |
423 void _registerClosurizedMember(TypedElement element) { | 423 void _registerClosurizedMember(TypedElement element) { |
424 assert(element.isInstanceMember); | 424 assert(element.isInstanceMember); |
425 if (element.type.containsTypeVariables) { | 425 if (element.type.containsTypeVariables) { |
426 applyImpact(_backend.registerClosureWithFreeTypeVariables(element, | 426 applyImpact(_backend.registerClosureWithFreeTypeVariables(element, |
427 forResolution: false)); | 427 forResolution: false)); |
428 } | 428 } |
429 applyImpact(_backend.registerBoundClosure()); | 429 applyImpact(_backend.registerBoundClosure()); |
430 } | 430 } |
431 | 431 |
432 void forEach(void f(WorkItem work)) { | 432 void forEach(void f(WorkItem work)) { |
433 do { | 433 do { |
434 while (queue.isNotEmpty) { | 434 while (_queue.isNotEmpty) { |
435 // TODO(johnniwinther): Find an optimal process order. | 435 // TODO(johnniwinther): Find an optimal process order. |
436 WorkItem work = queue.removeLast(); | 436 WorkItem work = _queue.removeLast(); |
437 if (!isProcessed(work.element)) { | 437 if (!isProcessed(work.element)) { |
438 strategy.processWorkItem(f, work); | 438 strategy.processWorkItem(f, work); |
439 // TODO(johnniwinther): Register the processed element here. This | 439 // TODO(johnniwinther): Register the processed element here. This |
440 // is currently a side-effect of calling `work.run`. | 440 // is currently a side-effect of calling `work.run`. |
441 } | 441 } |
442 } | 442 } |
443 List recents = _recentClasses.toList(growable: false); | 443 List recents = _recentClasses.toList(growable: false); |
444 _recentClasses.clear(); | 444 _recentClasses.clear(); |
445 if (!_onQueueEmpty(recents)) _recentClasses.addAll(recents); | 445 if (!_onQueueEmpty(recents)) _recentClasses.addAll(recents); |
446 } while (queue.isNotEmpty || _recentClasses.isNotEmpty); | 446 } while (_queue.isNotEmpty || _recentClasses.isNotEmpty); |
447 } | 447 } |
448 | 448 |
449 /// [_onQueueEmpty] is called whenever the queue is drained. [recentClasses] | 449 /// [_onQueueEmpty] is called whenever the queue is drained. [recentClasses] |
450 /// contains the set of all classes seen for the first time since | 450 /// contains the set of all classes seen for the first time since |
451 /// [_onQueueEmpty] was called last. A return value of [true] indicates that | 451 /// [_onQueueEmpty] was called last. A return value of [true] indicates that |
452 /// the [recentClasses] have been processed and may be cleared. If [false] is | 452 /// the [recentClasses] have been processed and may be cleared. If [false] is |
453 /// returned, [_onQueueEmpty] will be called once the queue is empty again (or | 453 /// returned, [_onQueueEmpty] will be called once the queue is empty again (or |
454 /// still empty) and [recentClasses] will be a superset of the current value. | 454 /// still empty) and [recentClasses] will be a superset of the current value. |
455 bool _onQueueEmpty(Iterable<ClassElement> recentClasses) { | 455 bool _onQueueEmpty(Iterable<ClassElement> recentClasses) { |
456 return _backend.onQueueEmpty(this, recentClasses); | 456 return _backend.onQueueEmpty(this, recentClasses); |
(...skipping 12 matching lines...) Expand all Loading... |
469 member.isAbstract || generatedCode.containsKey(member); | 469 member.isAbstract || generatedCode.containsKey(member); |
470 | 470 |
471 void _registerNoSuchMethod(Element element) { | 471 void _registerNoSuchMethod(Element element) { |
472 if (!_enabledNoSuchMethod && _backend.enabledNoSuchMethod) { | 472 if (!_enabledNoSuchMethod && _backend.enabledNoSuchMethod) { |
473 applyImpact(_backend.enableNoSuchMethod()); | 473 applyImpact(_backend.enableNoSuchMethod()); |
474 _enabledNoSuchMethod = true; | 474 _enabledNoSuchMethod = true; |
475 } | 475 } |
476 } | 476 } |
477 | 477 |
478 void forgetEntity(Element element, Compiler compiler) { | 478 void forgetEntity(Element element, Compiler compiler) { |
479 _universe.forgetElement(element, _compiler); | 479 _universe.forgetElement(element, compiler); |
480 _processedClasses.remove(element); | 480 _processedClasses.remove(element); |
481 _instanceMembersByName[element.name]?.remove(element); | 481 _instanceMembersByName[element.name]?.remove(element); |
482 _instanceFunctionsByName[element.name]?.remove(element); | 482 _instanceFunctionsByName[element.name]?.remove(element); |
483 generatedCode.remove(element); | 483 generatedCode.remove(element); |
484 if (element is MemberElement) { | 484 if (element is MemberElement) { |
485 for (Element closure in element.nestedClosures) { | 485 for (Element closure in element.nestedClosures) { |
486 generatedCode.remove(closure); | 486 generatedCode.remove(closure); |
487 removeFromSet(_instanceMembersByName, closure); | 487 removeFromSet(_instanceMembersByName, closure); |
488 removeFromSet(_instanceFunctionsByName, closure); | 488 removeFromSet(_instanceFunctionsByName, closure); |
489 } | 489 } |
490 } | 490 } |
491 } | 491 } |
492 | 492 |
493 @override | 493 @override |
494 Iterable<Entity> get processedEntities => generatedCode.keys; | 494 Iterable<Entity> get processedEntities => generatedCode.keys; |
495 | 495 |
496 @override | 496 @override |
497 Iterable<ClassEntity> get processedClasses => _processedClasses; | 497 Iterable<ClassEntity> get processedClasses => _processedClasses; |
498 } | 498 } |
499 | 499 |
500 void removeFromSet(Map<String, Set<Element>> map, Element element) { | 500 void removeFromSet(Map<String, Set<Element>> map, Element element) { |
501 Set<Element> set = map[element.name]; | 501 Set<Element> set = map[element.name]; |
502 if (set == null) return; | 502 if (set == null) return; |
503 set.remove(element); | 503 set.remove(element); |
504 } | 504 } |
OLD | NEW |