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

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

Issue 2843283004: Use more entities in program_builder, collector and registry (Closed)
Patch Set: Updated cf. comments. Created 3 years, 7 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
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 /// Maps [LibraryElement]s to their [Element]s. 7 class LibraryContents {
8 final List<ClassEntity> classes = <ClassEntity>[];
9 final List<MemberEntity> members = <MemberEntity>[];
10 }
11
12 /// Maps [LibraryEntity]s to their [ClassEntity]s and [MemberEntity]s.
8 /// 13 ///
9 /// Fundamentally, this class nicely encapsulates a 14 /// Fundamentally, this class nicely encapsulates a
10 /// `Map<LibraryElement, List<Element>>`. 15 /// `Map<LibraryElement, Pair<List<ClassElement>, List<MemberElement>>>`.
11 /// 16 ///
12 /// There exists exactly one instance per [OutputUnit]. 17 /// There exists exactly one instance per [OutputUnit].
13 class LibrariesMap { 18 class LibrariesMap {
14 final Map<LibraryElement, List<Element>> _mapping = 19 final Map<LibraryEntity, LibraryContents> _mapping =
15 <LibraryElement, List<Element>>{}; 20 <LibraryEntity, LibraryContents>{};
16 21
17 // It is very common to access the same library multiple times in a row, so 22 // It is very common to access the same library multiple times in a row, so
18 // we cache the last access. 23 // we cache the last access.
19 LibraryElement _lastLibrary; 24 LibraryEntity _lastLibrary;
20 List<Element> _lastElements; 25 LibraryContents _lastMapping;
21 26
22 /// A unique name representing this instance. 27 /// A unique name representing this instance.
23 final String name; 28 final String name;
24 final OutputUnit outputUnit; 29 final OutputUnit outputUnit;
25 30
26 LibrariesMap.main(this.outputUnit) : name = ""; 31 LibrariesMap.main(this.outputUnit) : name = "";
27 32
28 LibrariesMap.deferred(this.outputUnit, this.name) { 33 LibrariesMap.deferred(this.outputUnit, this.name) {
29 assert(name != ""); 34 assert(name != "");
30 } 35 }
31 36
32 void add(LibraryElement library, Element element) { 37 LibraryContents _getMapping(LibraryEntity library) {
33 if (_lastLibrary != library) { 38 if (_lastLibrary != library) {
34 _lastLibrary = library; 39 _lastLibrary = library;
35 _lastElements = _mapping.putIfAbsent(library, () => <Element>[]); 40 _lastMapping = _mapping.putIfAbsent(library, () => new LibraryContents());
36 } 41 }
37 _lastElements.add(element); 42 return _lastMapping;
43 }
44
45 void addClass(LibraryEntity library, ClassEntity element) {
46 _getMapping(library).classes.add(element);
47 }
48
49 void addMember(LibraryEntity library, MemberEntity element) {
50 _getMapping(library).members.add(element);
38 } 51 }
39 52
40 int get length => _mapping.length; 53 int get length => _mapping.length;
41 54
42 void forEach(void f(LibraryElement library, List<Element> elements)) { 55 void forEach(
43 _mapping.forEach(f); 56 void f(LibraryEntity library, List<ClassEntity> classes,
57 List<MemberEntity> members)) {
58 _mapping.forEach((LibraryEntity library, LibraryContents mapping) {
59 f(library, mapping.classes, mapping.members);
60 });
44 } 61 }
45 } 62 }
46 63
47 /// Keeps track of all elements and holders. 64 /// Keeps track of all elements and holders.
48 /// 65 ///
49 /// This class assigns each registered element to its [LibrariesMap] (which are 66 /// This class assigns each registered element to its [LibrariesMap] (which are
50 /// in bijection with [OutputUnit]s). 67 /// in bijection with [OutputUnit]s).
51 /// 68 ///
52 /// Registered holders are assigned a name. 69 /// Registered holders are assigned a name.
53 class Registry { 70 class Registry {
54 final DeferredLoadTask _deferredLoadTask; 71 final DeferredLoadTask _deferredLoadTask;
72 final Sorter _sorter;
55 final Map<String, Holder> _holdersMap = <String, Holder>{}; 73 final Map<String, Holder> _holdersMap = <String, Holder>{};
56 final Map<OutputUnit, LibrariesMap> _deferredLibrariesMap = 74 final Map<OutputUnit, LibrariesMap> _deferredLibrariesMap =
57 <OutputUnit, LibrariesMap>{}; 75 <OutputUnit, LibrariesMap>{};
58 76
59 /// Cache for the last seen output unit. 77 /// Cache for the last seen output unit.
60 OutputUnit _lastOutputUnit; 78 OutputUnit _lastOutputUnit;
61 LibrariesMap _lastLibrariesMap; 79 LibrariesMap _lastLibrariesMap;
62 80
63 Iterable<Holder> get holders => _holdersMap.values; 81 Iterable<Holder> get holders => _holdersMap.values;
64 Iterable<LibrariesMap> get deferredLibrariesMap => 82 Iterable<LibrariesMap> get deferredLibrariesMap =>
65 _deferredLibrariesMap.values; 83 _deferredLibrariesMap.values;
66 84
67 // Add one for the main libraries map. 85 // Add one for the main libraries map.
68 int get librariesMapCount => _deferredLibrariesMap.length + 1; 86 int get librariesMapCount => _deferredLibrariesMap.length + 1;
69 87
70 LibrariesMap mainLibrariesMap; 88 LibrariesMap mainLibrariesMap;
71 89
72 Registry(this._deferredLoadTask); 90 Registry(this._deferredLoadTask, this._sorter);
73 91
74 OutputUnit get _mainOutputUnit => _deferredLoadTask.mainOutputUnit; 92 OutputUnit get _mainOutputUnit => _deferredLoadTask.mainOutputUnit;
75 93
76 LibrariesMap _mapUnitToLibrariesMap(OutputUnit targetUnit) { 94 LibrariesMap _mapUnitToLibrariesMap(OutputUnit targetUnit) {
77 if (targetUnit == _lastOutputUnit) return _lastLibrariesMap; 95 if (targetUnit == _lastOutputUnit) return _lastLibrariesMap;
78 96
79 LibrariesMap result = (targetUnit == _mainOutputUnit) 97 LibrariesMap result = (targetUnit == _mainOutputUnit)
80 ? mainLibrariesMap 98 ? mainLibrariesMap
81 : _deferredLibrariesMap[targetUnit]; 99 : _deferredLibrariesMap[targetUnit];
82 100
(...skipping 11 matching lines...) Expand all
94 } else { 112 } else {
95 assert(!_deferredLibrariesMap.containsKey(outputUnit)); 113 assert(!_deferredLibrariesMap.containsKey(outputUnit));
96 String name = outputUnit.name; 114 String name = outputUnit.name;
97 _deferredLibrariesMap[outputUnit] = 115 _deferredLibrariesMap[outputUnit] =
98 new LibrariesMap.deferred(outputUnit, name); 116 new LibrariesMap.deferred(outputUnit, name);
99 } 117 }
100 } 118 }
101 119
102 /// Adds all elements to their respective libraries in the correct 120 /// Adds all elements to their respective libraries in the correct
103 /// libraries map. 121 /// libraries map.
104 void registerElements(OutputUnit outputUnit, Iterable<Element> elements) { 122 void registerClasses(OutputUnit outputUnit, Iterable<ClassEntity> elements) {
105 LibrariesMap targetLibrariesMap = _mapUnitToLibrariesMap(outputUnit); 123 LibrariesMap targetLibrariesMap = _mapUnitToLibrariesMap(outputUnit);
106 for (Element element in Elements.sortedByPosition(elements)) { 124 for (ClassEntity element in _sorter.sortClasses(elements)) {
107 targetLibrariesMap.add(element.library, element); 125 targetLibrariesMap.addClass(element.library, element);
108 } 126 }
109 } 127 }
110 128
129 /// Adds all elements to their respective libraries in the correct
130 /// libraries map.
131 void registerMembers(OutputUnit outputUnit, Iterable<MemberEntity> elements) {
132 LibrariesMap targetLibrariesMap = _mapUnitToLibrariesMap(outputUnit);
133 for (MemberEntity element in _sorter.sortMembers(elements)) {
134 targetLibrariesMap.addMember(element.library, element);
135 }
136 }
137
111 void registerConstant(OutputUnit outputUnit, ConstantValue constantValue) { 138 void registerConstant(OutputUnit outputUnit, ConstantValue constantValue) {
112 // Ignore for now. 139 // Ignore for now.
113 } 140 }
114 141
115 Holder registerHolder(String name, 142 Holder registerHolder(String name,
116 {bool isStaticStateHolder: false, bool isConstantsHolder: false}) { 143 {bool isStaticStateHolder: false, bool isConstantsHolder: false}) {
117 assert(_holdersMap[name] == null || 144 assert(_holdersMap[name] == null ||
118 (_holdersMap[name].isStaticStateHolder == isStaticStateHolder && 145 (_holdersMap[name].isStaticStateHolder == isStaticStateHolder &&
119 _holdersMap[name].isConstantsHolder == isConstantsHolder)); 146 _holdersMap[name].isConstantsHolder == isConstantsHolder));
120 147
121 return _holdersMap.putIfAbsent(name, () { 148 return _holdersMap.putIfAbsent(name, () {
122 return new Holder(name, _holdersMap.length, 149 return new Holder(name, _holdersMap.length,
123 isStaticStateHolder: isStaticStateHolder, 150 isStaticStateHolder: isStaticStateHolder,
124 isConstantsHolder: isConstantsHolder); 151 isConstantsHolder: isConstantsHolder);
125 }); 152 });
126 } 153 }
127 } 154 }
OLDNEW
« no previous file with comments | « pkg/compiler/lib/src/js_emitter/program_builder/program_builder.dart ('k') | pkg/compiler/lib/src/js_emitter/sorter.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698