| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 part of dart2js.js_emitter.program_builder; | |
| 6 | |
| 7 /// Maps [LibraryElement]s to their [Element]s. | |
| 8 /// | |
| 9 /// Fundamentally, this class nicely encapsulates a | |
| 10 /// `Map<LibraryElement, List<Element>>`. | |
| 11 /// | |
| 12 /// There exists exactly one instance per [OutputUnit]. | |
| 13 class LibrariesMap { | |
| 14 final Map<LibraryElement, List<Element>> _mapping = | |
| 15 <LibraryElement, List<Element>>{}; | |
| 16 | |
| 17 // It is very common to access the same library multiple times in a row, so | |
| 18 // we cache the last access. | |
| 19 LibraryElement _lastLibrary; | |
| 20 List<Element> _lastElements; | |
| 21 | |
| 22 /// A unique name representing this instance. | |
| 23 final String name; | |
| 24 final OutputUnit outputUnit; | |
| 25 | |
| 26 LibrariesMap.main(this.outputUnit) : name = ""; | |
| 27 | |
| 28 LibrariesMap.deferred(this.outputUnit, this.name) { | |
| 29 assert(name != ""); | |
| 30 } | |
| 31 | |
| 32 void add(LibraryElement library, Element element) { | |
| 33 if (_lastLibrary != library) { | |
| 34 _lastLibrary = library; | |
| 35 _lastElements = _mapping.putIfAbsent(library, () => <Element>[]); | |
| 36 } | |
| 37 _lastElements.add(element); | |
| 38 } | |
| 39 | |
| 40 int get length => _mapping.length; | |
| 41 | |
| 42 void forEach(void f(LibraryElement library, List<Element> elements)) { | |
| 43 _mapping.forEach(f); | |
| 44 } | |
| 45 } | |
| 46 | |
| 47 /// Keeps track of all elements and holders. | |
| 48 /// | |
| 49 /// This class assigns each registered element to its [LibrariesMap] (which are | |
| 50 /// in bijection with [OutputUnit]s). | |
| 51 /// | |
| 52 /// Registered holders are assigned a name. | |
| 53 class Registry { | |
| 54 final Compiler _compiler; | |
| 55 final Map<String, Holder> _holdersMap = <String, Holder>{}; | |
| 56 final Map<OutputUnit, LibrariesMap> _deferredLibrariesMap = | |
| 57 <OutputUnit, LibrariesMap>{}; | |
| 58 | |
| 59 /// Cache for the last seen output unit. | |
| 60 OutputUnit _lastOutputUnit; | |
| 61 LibrariesMap _lastLibrariesMap; | |
| 62 | |
| 63 DeferredLoadTask get _deferredLoadTask => _compiler.deferredLoadTask; | |
| 64 Iterable<Holder> get holders => _holdersMap.values; | |
| 65 Iterable<LibrariesMap> get deferredLibrariesMap => | |
| 66 _deferredLibrariesMap.values; | |
| 67 | |
| 68 // Add one for the main libraries map. | |
| 69 int get librariesMapCount => _deferredLibrariesMap.length + 1; | |
| 70 | |
| 71 LibrariesMap mainLibrariesMap; | |
| 72 | |
| 73 Registry(this._compiler); | |
| 74 | |
| 75 OutputUnit get _mainOutputUnit => _deferredLoadTask.mainOutputUnit; | |
| 76 | |
| 77 LibrariesMap _mapUnitToLibrariesMap(OutputUnit targetUnit) { | |
| 78 if (targetUnit == _lastOutputUnit) return _lastLibrariesMap; | |
| 79 | |
| 80 LibrariesMap result = (targetUnit == _mainOutputUnit) | |
| 81 ? mainLibrariesMap | |
| 82 : _deferredLibrariesMap[targetUnit]; | |
| 83 | |
| 84 assert(result != null); | |
| 85 _lastOutputUnit = targetUnit; | |
| 86 _lastLibrariesMap = result; | |
| 87 return result; | |
| 88 } | |
| 89 | |
| 90 void registerOutputUnit(OutputUnit outputUnit) { | |
| 91 if (outputUnit == _mainOutputUnit) { | |
| 92 assert(mainLibrariesMap == null); | |
| 93 mainLibrariesMap = | |
| 94 new LibrariesMap.main(_deferredLoadTask.mainOutputUnit); | |
| 95 } else { | |
| 96 assert(!_deferredLibrariesMap.containsKey(outputUnit)); | |
| 97 String name = outputUnit.name; | |
| 98 _deferredLibrariesMap[outputUnit] = | |
| 99 new LibrariesMap.deferred(outputUnit, name); | |
| 100 } | |
| 101 } | |
| 102 | |
| 103 /// Adds all elements to their respective libraries in the correct | |
| 104 /// libraries map. | |
| 105 void registerElements(OutputUnit outputUnit, Iterable<Element> elements) { | |
| 106 LibrariesMap targetLibrariesMap = _mapUnitToLibrariesMap(outputUnit); | |
| 107 for (Element element in Elements.sortedByPosition(elements)) { | |
| 108 targetLibrariesMap.add(element.library, element); | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 void registerConstant(OutputUnit outputUnit, ConstantValue constantValue) { | |
| 113 // Ignore for now. | |
| 114 } | |
| 115 | |
| 116 Holder registerHolder(String name) { | |
| 117 return _holdersMap.putIfAbsent( | |
| 118 name, | |
| 119 () => new Holder(name, _holdersMap.length)); | |
| 120 } | |
| 121 } | |
| OLD | NEW |