22// for details. All rights reserved. Use of this source code is governed by a
33// BSD-style license that can be found in the LICENSE file.
44
5- // @dart = 2.10
6-
75import 'output_unit.dart' ;
86
97import 'program_split_constraints/builder.dart' as psc show SetTransition;
@@ -61,12 +59,12 @@ class ImportSetLattice {
6159 final Map <ImportEntity , _DeferredImport > _importIndex = {};
6260
6361 /// The canonical instance representing the empty import set.
64- final ImportSet _emptySet = ImportSet . empty ();
62+ final ImportSet emptySet = _EmptyImportSet ();
6563
6664 /// The [ImportSet] representing the main output unit.
67- ImportSet _mainSet;
65+ late ImportSet _mainSet;
6866 ImportSet get mainSet {
69- assert (_mainSet != null && _mainSet .unit != null );
67+ assert (_mainSet.unit != null );
7068 return _mainSet;
7169 }
7270
@@ -81,14 +79,14 @@ class ImportSetLattice {
8179 /// [ImportEntity] .
8280 ImportSet _singleton (ImportEntity import) {
8381 // Ensure we have import in the index.
84- return _emptySet ._add (_wrap (import));
82+ return emptySet ._add (_wrap (import));
8583 }
8684
8785 /// A helper method to convert a [Set<ImportEntity>] to an [ImportSet] .
8886 ImportSet setOfImportsToImportSet (Set <ImportEntity > setOfImports) {
8987 List <_DeferredImport > imports = setOfImports.map (_wrap).toList ();
9088 imports.sort ((a, b) => a.index - b.index);
91- var result = _emptySet ;
89+ var result = emptySet ;
9290 for (var import in imports) {
9391 result = result._add (import);
9492 }
@@ -124,8 +122,8 @@ class ImportSetLattice {
124122
125123 /// Get the import set that includes the union of [a] and [b] .
126124 ImportSet union (ImportSet a, ImportSet b) {
127- if (a == null || a.isEmpty ) return b;
128- if (b == null || b.isEmpty ) return a;
125+ if (a is _EmptyImportSet ) return b;
126+ if (b is _EmptyImportSet ) return a;
129127
130128 // Create the union by merging the imports in canonical order. The sets are
131129 // basically lists linked by the `_previous` field in reverse order. We do a
@@ -137,11 +135,11 @@ class ImportSetLattice {
137135 List <_DeferredImport > imports = [];
138136
139137 while (true ) {
140- if (a.isEmpty ) {
138+ if (a is ! _NonEmptyImportSet ) {
141139 result = b;
142140 break ;
143141 }
144- if (b.isEmpty || identical (a, b)) {
142+ if (b is ! _NonEmptyImportSet || identical (a, b)) {
145143 result = a;
146144 break ;
147145 }
@@ -181,7 +179,7 @@ class ImportSetLattice {
181179
182180 // Try and apply any [ImportSetTransition]s that have not yet been
183181 // applied to this [ImportSet].
184- var candidateTransitions = allCandidateTransitions[originalImportSet];
182+ var candidateTransitions = allCandidateTransitions[originalImportSet]! ;
185183 for (var transition in candidateTransitions) {
186184 if (originalImportSet.containsAll (transition.source)) {
187185 importSet = union (importSet, transition.transitions);
@@ -221,51 +219,33 @@ class ImportSetLattice {
221219}
222220
223221/// A canonical set of deferred imports.
224- class ImportSet {
225- /// Last element added to set.
226- ///
227- /// This set comprises [_import] appended onto [_previous] . *Note*: [_import]
228- /// is the last element in the set in the canonical order imposed by
229- /// [ImportSetLattice] .
230- final _DeferredImport _import; // `null` for empty ImportSet
231- /// The set containing all previous elements.
232- final ImportSet _previous;
233- final int length;
222+ abstract class ImportSet {
223+ /// Links to other import sets in the lattice by adding one import.
224+ final Map <_DeferredImport , _NonEmptyImportSet > _transitions = Maplet ();
225+
226+ /// The output unit corresponding to this set of imports, if any.
227+ OutputUnit ? unit;
234228
235- bool get isEmpty => _import == null ;
236- bool get isNotEmpty => _import != null ;
229+ int get length;
237230
238231 /// Returns an iterable over the imports in this set in canonical order.
239232 Iterable <_DeferredImport > collectImports () {
240233 List <_DeferredImport > result = [];
241234 ImportSet current = this ;
242- while (current.isNotEmpty ) {
235+ while (current is _NonEmptyImportSet ) {
243236 result.add (current._import);
244237 current = current._previous;
245238 }
246239 assert (result.length == this .length);
247240 return result.reversed;
248241 }
249242
250- /// Links to other import sets in the lattice by adding one import.
251- final Map <_DeferredImport , ImportSet > _transitions = Maplet ();
252-
253- ImportSet .empty ()
254- : _import = null ,
255- _previous = null ,
256- length = 0 ;
257-
258- ImportSet (this ._import, this ._previous, this .length);
259-
260- /// The output unit corresponding to this set of imports, if any.
261- OutputUnit unit;
262-
263243 /// Returns true if this [ImportSet] contains all of [other] .
264244 bool containsAll (ImportSet other) {
265245 var current = this ;
266246 while (true ) {
267- if (other.isEmpty ) return true ;
268- if (current.isEmpty ) return false ;
247+ if (other is ! _NonEmptyImportSet ) return true ;
248+ if (current is ! _NonEmptyImportSet ) return false ;
269249
270250 if (current._import.index > other._import.index) {
271251 current = current._previous;
@@ -284,8 +264,10 @@ class ImportSet {
284264 /// this current set. This should only be called from [ImportSetLattice] ,
285265 /// since it is where we preserve this invariant.
286266 ImportSet _add (_DeferredImport import) {
287- assert (_import == null || import.index > _import.index);
288- return _transitions[import] ?? = ImportSet (import, this , length + 1 );
267+ var self = this ;
268+ assert (self is ! _NonEmptyImportSet || import.index > self._import.index);
269+ return _transitions[import] ?? =
270+ _NonEmptyImportSet (import, this , length + 1 );
289271 }
290272
291273 @override
@@ -304,3 +286,25 @@ class ImportSet {
304286 Set <ImportEntity > toSet () =>
305287 collectImports ().map ((i) => i.declaration).toSet ();
306288}
289+
290+ class _NonEmptyImportSet extends ImportSet {
291+ /// Last element added to set.
292+ ///
293+ /// This set comprises [_import] appended onto [_previous] . *Note*: [_import]
294+ /// is the last element in the set in the canonical order imposed by
295+ /// [ImportSetLattice] .
296+ final _DeferredImport _import; // `null` for empty ImportSet
297+
298+ /// The set containing all previous elements.
299+ final ImportSet _previous;
300+
301+ @override
302+ final int length;
303+
304+ _NonEmptyImportSet (this ._import, this ._previous, this .length);
305+ }
306+
307+ class _EmptyImportSet extends ImportSet {
308+ @override
309+ int get length => 0 ;
310+ }
0 commit comments