Skip to content

Commit 0bfa0b0

Browse files
fishythefishCommit Queue
authored andcommitted
[dart2js] Add Bitset and EnumSetDomain abstractions.
Change-Id: I85b401d7eb13e0d9ba0bfcb37aebdb28e65bc77f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/395062 Reviewed-by: Stephen Adams <[email protected]> Reviewed-by: Nate Biggs <[email protected]>
1 parent ab1f7fc commit 0bfa0b0

File tree

10 files changed

+151
-81
lines changed

10 files changed

+151
-81
lines changed

pkg/compiler/lib/src/common/codegen.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class _CodegenImpact extends WorldImpactBuilderImpl implements CodegenImpact {
124124
});
125125
bool usesInterceptor = source.readBool();
126126
final asyncMarkersValue = source.readInt();
127-
final asyncMarkers = EnumSet<AsyncMarker>(asyncMarkersValue);
127+
final asyncMarkers = EnumSet<AsyncMarker>.fromRawBits(asyncMarkersValue);
128128
final genericInstantiations = source
129129
.readListOrNull(() => GenericInstantiation.readFromDataSource(source))
130130
?.toSet();
@@ -169,7 +169,7 @@ class _CodegenImpact extends WorldImpactBuilderImpl implements CodegenImpact {
169169
sink.writeStringsOrNull(_constSymbols);
170170
sink.writeListOrNull(_specializedGetInterceptors, sink.writeClasses);
171171
sink.writeBool(_usesInterceptor);
172-
sink.writeInt(_asyncMarkers.mask);
172+
sink.writeInt(_asyncMarkers.mask.bits);
173173
sink.writeListOrNull(
174174
_genericInstantiations,
175175
(GenericInstantiation instantiation) =>

pkg/compiler/lib/src/inferrer/type_graph_nodes.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,13 @@ abstract class TypeInformation {
160160
EnumSet<_Flag> _flags = EnumSet.empty();
161161

162162
/// Number of times this [TypeInformation] has changed type.
163-
int get refineCount => _flags.mask >> NUM_TYPE_INFO_FLAGS;
163+
int get refineCount => _flags.mask.bits >> NUM_TYPE_INFO_FLAGS;
164164

165-
void incrementRefineCount() =>
166-
_flags = EnumSet(_flags.mask + (1 << NUM_TYPE_INFO_FLAGS));
165+
void incrementRefineCount() => _flags =
166+
EnumSet.fromRawBits(_flags.mask.bits + (1 << NUM_TYPE_INFO_FLAGS));
167167

168-
void clearRefineCount() =>
169-
_flags = EnumSet(_flags.mask & ((1 << NUM_TYPE_INFO_FLAGS) - 1));
168+
void clearRefineCount() => _flags =
169+
EnumSet.fromRawBits(_flags.mask.bits & ((1 << NUM_TYPE_INFO_FLAGS) - 1));
170170

171171
void addUser(TypeInformation user) {
172172
assert(!user.isConcrete);

pkg/compiler/lib/src/js_backend/annotations.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,9 @@ class AnnotationsDataImpl implements AnnotationsData {
372372
factory AnnotationsDataImpl.readFromDataSource(CompilerOptions options,
373373
DiagnosticReporter reporter, DataSourceReader source) {
374374
source.begin(tag);
375-
Map<MemberEntity, EnumSet<PragmaAnnotation>> pragmaAnnotations = source
376-
.readMemberMap((MemberEntity member) => EnumSet(source.readInt()));
375+
Map<MemberEntity, EnumSet<PragmaAnnotation>> pragmaAnnotations =
376+
source.readMemberMap(
377+
(MemberEntity member) => EnumSet.fromRawBits(source.readInt()));
377378
source.end(tag);
378379
return AnnotationsDataImpl(options, reporter, pragmaAnnotations);
379380
}
@@ -383,7 +384,7 @@ class AnnotationsDataImpl implements AnnotationsData {
383384
sink.begin(tag);
384385
sink.writeMemberMap(pragmaAnnotations,
385386
(MemberEntity member, EnumSet<PragmaAnnotation> set) {
386-
sink.writeInt(set.mask);
387+
sink.writeInt(set.mask.bits);
387388
});
388389
sink.end(tag);
389390
}

pkg/compiler/lib/src/universe/class_set.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,15 +219,15 @@ class ClassHierarchyNode {
219219
source.end(tag);
220220
return ClassHierarchyNode(parentNode, cls, hierarchyDepth)
221221
.._instantiatedSubclassCount = instantiatedSubclassCount
222-
.._mask = EnumSet(maskValue);
222+
.._mask = EnumSet.fromRawBits(maskValue);
223223
}
224224

225225
/// Serializes this [ClassHierarchyNode] to [sink].
226226
void writeToDataSink(DataSinkWriter sink) {
227227
sink.begin(tag);
228228
sink.writeClass(cls);
229229
sink.writeClassOrNull(parentNode?.cls);
230-
sink.writeInt(_mask.mask);
230+
sink.writeInt(_mask.mask.bits);
231231
sink.writeInt(hierarchyDepth);
232232
sink.writeInt(_instantiatedSubclassCount);
233233
sink.end(tag);

pkg/compiler/lib/src/universe/member_usage.dart

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -652,12 +652,14 @@ enum MemberUse {
652652

653653
/// Common [EnumSet]s used for [MemberUse].
654654
class MemberUses {
655-
static const EnumSet<MemberUse> NONE = EnumSet(0);
656-
static const EnumSet<MemberUse> NORMAL_ONLY = EnumSet(1);
657-
static const EnumSet<MemberUse> CLOSURIZE_INSTANCE_ONLY = EnumSet(2);
658-
static const EnumSet<MemberUse> CLOSURIZE_STATIC_ONLY = EnumSet(4);
659-
static const EnumSet<MemberUse> ALL_INSTANCE = EnumSet(3);
660-
static const EnumSet<MemberUse> ALL_STATIC = EnumSet(5);
655+
static const EnumSet<MemberUse> NONE = EnumSet.fromRawBits(0);
656+
static const EnumSet<MemberUse> NORMAL_ONLY = EnumSet.fromRawBits(1);
657+
static const EnumSet<MemberUse> CLOSURIZE_INSTANCE_ONLY =
658+
EnumSet.fromRawBits(2);
659+
static const EnumSet<MemberUse> CLOSURIZE_STATIC_ONLY =
660+
EnumSet.fromRawBits(4);
661+
static const EnumSet<MemberUse> ALL_INSTANCE = EnumSet.fromRawBits(3);
662+
static const EnumSet<MemberUse> ALL_STATIC = EnumSet.fromRawBits(5);
661663
}
662664

663665
typedef MemberUsedCallback = void Function(
@@ -705,10 +707,10 @@ enum ClassUse { INSTANTIATED, IMPLEMENTED }
705707

706708
/// Common [EnumSet]s used for [ClassUse].
707709
class ClassUses {
708-
static const EnumSet<ClassUse> NONE = EnumSet(0);
709-
static const EnumSet<ClassUse> INSTANTIATED_ONLY = EnumSet(1);
710-
static const EnumSet<ClassUse> IMPLEMENTED_ONLY = EnumSet(2);
711-
static const EnumSet<ClassUse> ALL = EnumSet(3);
710+
static const EnumSet<ClassUse> NONE = EnumSet.fromRawBits(0);
711+
static const EnumSet<ClassUse> INSTANTIATED_ONLY = EnumSet.fromRawBits(1);
712+
static const EnumSet<ClassUse> IMPLEMENTED_ONLY = EnumSet.fromRawBits(2);
713+
static const EnumSet<ClassUse> ALL = EnumSet.fromRawBits(3);
712714
}
713715

714716
typedef ClassUsedCallback = void Function(
@@ -859,15 +861,15 @@ enum Access {
859861
/// Access sets used for registration of member usage.
860862
class Accesses {
861863
/// Statically bound access of a member.
862-
static const EnumSet<Access> staticAccess = EnumSet(1);
864+
static const EnumSet<Access> staticAccess = EnumSet.fromRawBits(1);
863865

864866
/// Dynamically bound access of a member. This implies the statically bound
865867
/// access of the member.
866-
static const EnumSet<Access> dynamicAccess = EnumSet(3);
868+
static const EnumSet<Access> dynamicAccess = EnumSet.fromRawBits(3);
867869

868870
/// Direct access of a super class member. This implies the statically bound
869871
/// access of the member.
870-
static const EnumSet<Access> superAccess = EnumSet(5);
872+
static const EnumSet<Access> superAccess = EnumSet.fromRawBits(5);
871873
}
872874

873875
/// The accesses of a member collected during closed world computation.
@@ -882,18 +884,18 @@ class MemberAccess {
882884

883885
factory MemberAccess.readFromDataSource(DataSourceReader source) {
884886
source.begin(tag);
885-
EnumSet<Access> reads = EnumSet(source.readInt());
886-
EnumSet<Access> writes = EnumSet(source.readInt());
887-
EnumSet<Access> invokes = EnumSet(source.readInt());
887+
EnumSet<Access> reads = EnumSet.fromRawBits(source.readInt());
888+
EnumSet<Access> writes = EnumSet.fromRawBits(source.readInt());
889+
EnumSet<Access> invokes = EnumSet.fromRawBits(source.readInt());
888890
source.end(tag);
889891
return MemberAccess(reads, writes, invokes);
890892
}
891893

892894
void writeToDataSink(DataSinkWriter sink) {
893895
sink.begin(tag);
894-
sink.writeInt(reads.mask);
895-
sink.writeInt(writes.mask);
896-
sink.writeInt(invokes.mask);
896+
sink.writeInt(reads.mask.bits);
897+
sink.writeInt(writes.mask.bits);
898+
sink.writeInt(invokes.mask.bits);
897899
sink.end(tag);
898900
}
899901
}

pkg/compiler/lib/src/universe/side_effects.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ class SideEffects {
3434
EnumSet<SideEffectsFlag> _flags = EnumSet.empty();
3535

3636
static final EnumSet<SideEffectsFlag> allChanges =
37-
EnumSet((1 << SideEffectsFlag._changesCount) - 1);
37+
EnumSet.fromRawBits((1 << SideEffectsFlag._changesCount) - 1);
3838

39-
static final EnumSet<SideEffectsFlag> allDepends = EnumSet(
39+
static final EnumSet<SideEffectsFlag> allDepends = EnumSet.fromRawBits(
4040
((1 << SideEffectsFlag._dependsCount) - 1) <<
4141
SideEffectsFlag._changesCount);
4242

@@ -57,13 +57,13 @@ class SideEffects {
5757
source.begin(tag);
5858
int flags = source.readInt();
5959
source.end(tag);
60-
return SideEffects.fromFlags(EnumSet(flags));
60+
return SideEffects.fromFlags(EnumSet.fromRawBits(flags));
6161
}
6262

6363
/// Serializes this [SideEffects] to [sink].
6464
void writeToDataSink(DataSinkWriter sink) {
6565
sink.begin(tag);
66-
sink.writeInt(_flags.mask);
66+
sink.writeInt(_flags.mask.bits);
6767
sink.end(tag);
6868
}
6969

@@ -172,7 +172,7 @@ class SideEffects {
172172

173173
static EnumSet<SideEffectsFlag> computeDependsOnFlags(
174174
EnumSet<SideEffectsFlag> flags) =>
175-
EnumSet(flags.mask << SideEffectsFlag._changesCount);
175+
EnumSet.fromRawBits(flags.mask.bits << SideEffectsFlag._changesCount);
176176

177177
bool dependsOn(EnumSet<SideEffectsFlag> dependsFlags) =>
178178
_flags.intersects(dependsFlags);

pkg/compiler/lib/src/universe/use.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ class StaticUse {
226226
source.begin(tag);
227227
MemberEntity element = source.readMember();
228228
StaticUseKind kind = source.readEnum(StaticUseKind.values);
229-
final bitMask = EnumSet<_StaticUseFlag>(source.readInt());
229+
final bitMask = EnumSet<_StaticUseFlag>.fromRawBits(source.readInt());
230230
InterfaceType? type;
231231
CallStructure? callStructure;
232232
ImportEntity? deferredImport;
@@ -268,7 +268,7 @@ class StaticUse {
268268
if (constant != null) _StaticUseFlag.constant,
269269
if (typeArguments != null) _StaticUseFlag.typeArguments,
270270
]);
271-
sink.writeInt(bitMask.mask);
271+
sink.writeInt(bitMask.mask.bits);
272272
if (type != null) {
273273
sink.writeDartType(type!);
274274
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) 2024, 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+
import 'package:meta/meta.dart';
6+
7+
extension type const Bitset(int bits) {
8+
@useResult
9+
Bitset intersection(Bitset other) => Bitset(bits & other.bits);
10+
11+
@useResult
12+
Bitset union(Bitset other) => Bitset(bits | other.bits);
13+
14+
@useResult
15+
Bitset setMinus(Bitset other) => Bitset(bits & ~other.bits);
16+
17+
bool intersects(Bitset other) => bits & other.bits != 0;
18+
19+
bool get isEmpty => bits == 0;
20+
21+
bool get isNotEmpty => bits != 0;
22+
}

0 commit comments

Comments
 (0)