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

Side by Side Diff: pkg/compiler/lib/src/js_emitter/registry.dart

Issue 838643002: dart2js: Register constants in the model builder. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Cache main-unit access. Created 5 years, 11 months 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 | Annotate | Revision Log
« no previous file with comments | « pkg/compiler/lib/src/js_emitter/program_builder.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 part of dart2js.js_emitter.program_builder; 5 part of dart2js.js_emitter.program_builder;
6 6
7 /// A Fragment maps [LibraryElement]s to their [Element]s. 7 /// A Fragment maps [LibraryElement]s to their [Element]s.
8 /// 8 ///
9 /// Fundamentally, this class nicely encapsulates a 9 /// Fundamentally, this class nicely encapsulates a
10 /// `Map<LibraryElement, List<Element>>`. 10 /// `Map<LibraryElement, List<Element>>`.
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
47 /// This class assigns each registered element to its [Fragment] (which are in 47 /// This class assigns each registered element to its [Fragment] (which are in
48 /// bijection with [OutputUnit]s). 48 /// bijection with [OutputUnit]s).
49 /// 49 ///
50 /// Registered holders are assigned a name. 50 /// Registered holders are assigned a name.
51 class Registry { 51 class Registry {
52 final Compiler _compiler; 52 final Compiler _compiler;
53 final Map<String, Holder> _holdersMap = <String, Holder>{}; 53 final Map<String, Holder> _holdersMap = <String, Holder>{};
54 final Map<OutputUnit, Fragment> _deferredFragmentsMap = 54 final Map<OutputUnit, Fragment> _deferredFragmentsMap =
55 <OutputUnit, Fragment>{}; 55 <OutputUnit, Fragment>{};
56 56
57 /// Cache for the last seen output unit.
58 OutputUnit _lastOutputUnit;
59 Fragment _lastFragment;
60
57 DeferredLoadTask get _deferredLoadTask => _compiler.deferredLoadTask; 61 DeferredLoadTask get _deferredLoadTask => _compiler.deferredLoadTask;
58 Iterable<Holder> get holders => _holdersMap.values; 62 Iterable<Holder> get holders => _holdersMap.values;
59 Iterable<Fragment> get deferredFragments => _deferredFragmentsMap.values; 63 Iterable<Fragment> get deferredFragments => _deferredFragmentsMap.values;
60 // Add one for the main fragment. 64 // Add one for the main fragment.
61 int get fragmentCount => _deferredFragmentsMap.length + 1; 65 int get fragmentCount => _deferredFragmentsMap.length + 1;
62 66
63 Fragment mainFragment; 67 Fragment mainFragment;
64 68
65 Registry(this._compiler); 69 Registry(this._compiler);
66 70
67 bool get _isProgramSplit => _deferredLoadTask.isProgramSplit; 71 bool get _isProgramSplit => _deferredLoadTask.isProgramSplit;
68 OutputUnit get _mainOutputUnit => _deferredLoadTask.mainOutputUnit; 72 OutputUnit get _mainOutputUnit => _deferredLoadTask.mainOutputUnit;
69 73
70 Fragment _mapUnitToFragment(OutputUnit targetUnit) { 74 Fragment _mapUnitToFragment(OutputUnit targetUnit) {
75 if (targetUnit == _lastOutputUnit) return _lastFragment;
76
71 if (mainFragment == null) { 77 if (mainFragment == null) {
72 mainFragment = new Fragment.main(_deferredLoadTask.mainOutputUnit); 78 mainFragment = new Fragment.main(_deferredLoadTask.mainOutputUnit);
73 } 79 }
74 if (!_isProgramSplit) { 80
75 assert(targetUnit == _deferredLoadTask.mainOutputUnit); 81 Fragment result;
76 return mainFragment; 82 if (targetUnit == _mainOutputUnit) {
83 result = mainFragment;
84 } else {
85 String name = targetUnit.name;
86 result = _deferredFragmentsMap.putIfAbsent(
87 targetUnit, () => new Fragment.deferred(targetUnit, name));
77 } 88 }
78 if (targetUnit == _mainOutputUnit) return mainFragment; 89 _lastOutputUnit = targetUnit;
79 String name = targetUnit.name; 90 _lastFragment = result;
80 return _deferredFragmentsMap.putIfAbsent( 91 return result;
81 targetUnit, () => new Fragment.deferred(targetUnit, name));
82 } 92 }
83 93
84 /// Adds all elements to their respective libraries in the correct fragment. 94 /// Adds all elements to their respective libraries in the correct fragment.
85 void registerElements(OutputUnit outputUnit, List<Element> elements) { 95 void registerElements(OutputUnit outputUnit, List<Element> elements) {
86 Fragment targetFragment = _mapUnitToFragment(outputUnit); 96 Fragment targetFragment = _mapUnitToFragment(outputUnit);
87 for (Element element in Elements.sortedByPosition(elements)) { 97 for (Element element in Elements.sortedByPosition(elements)) {
88 targetFragment.add(element.library, element); 98 targetFragment.add(element.library, element);
89 } 99 }
90 } 100 }
91 101
102 void registerConstant(OutputUnit outputUnit, ConstantValue constantValue) {
103 // We just need to make sure that the target fragment is registered.
104 // Otherwise a fragment that contains only constants is not built.
105 _mapUnitToFragment(outputUnit);
106 }
107
92 Holder registerHolder(String name) { 108 Holder registerHolder(String name) {
93 return _holdersMap.putIfAbsent( 109 return _holdersMap.putIfAbsent(
94 name, 110 name,
95 () => new Holder(name, _holdersMap.length)); 111 () => new Holder(name, _holdersMap.length));
96 } 112 }
97 } 113 }
OLDNEW
« no previous file with comments | « pkg/compiler/lib/src/js_emitter/program_builder.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698