Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit e53cd30

Browse files
fishythefishcommit-bot@chromium.org
authored andcommitted
Add SetTypeMask.
Change-Id: I9f202506c364cc1387efa8cea4cca6038f350470 Reviewed-on: https://dart-review.googlesource.com/c/92146 Reviewed-by: Sigmund Cherem <[email protected]>
1 parent 15cd12b commit e53cd30

File tree

4 files changed

+128
-1
lines changed

4 files changed

+128
-1
lines changed

pkg/compiler/lib/src/inferrer/typemasks/masks.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ part 'dictionary_type_mask.dart';
2929
part 'flat_type_mask.dart';
3030
part 'forwarding_type_mask.dart';
3131
part 'map_type_mask.dart';
32+
part 'set_type_mask.dart';
3233
part 'type_mask.dart';
3334
part 'union_type_mask.dart';
3435
part 'value_type_mask.dart';
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// Copyright (c) 2019, 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 masks;
6+
7+
/// A [SetTypeMask] is a [TypeMask] for a specific allocation site of a set
8+
/// (currently only the internal Set class) that will get specialized once the
9+
/// [TypeGraphInferrer] phase finds an element type for it.
10+
class SetTypeMask extends AllocationTypeMask {
11+
/// Tag used for identifying serialized [SetTypeMask] objects in a debugging
12+
/// data stream.
13+
static const String tag = 'set-type-mask';
14+
15+
final TypeMask forwardTo;
16+
17+
// The [Node] where this type mask was created.
18+
final ir.TreeNode allocationNode;
19+
20+
// The [Entity] where this type mask was created.
21+
final MemberEntity allocationElement;
22+
23+
// The element type of this set.
24+
final TypeMask elementType;
25+
26+
SetTypeMask(this.forwardTo, this.allocationNode, this.allocationElement,
27+
this.elementType);
28+
29+
/// Deserializes a [SetTypeMask] object from [source].
30+
factory SetTypeMask.readFromDataSource(
31+
DataSource source, JClosedWorld closedWorld) {
32+
source.begin(tag);
33+
TypeMask forwardTo = new TypeMask.readFromDataSource(source, closedWorld);
34+
ir.TreeNode allocationNode = source.readTreeNodeOrNull();
35+
MemberEntity allocationElement = source.readMemberOrNull();
36+
TypeMask elementType = new TypeMask.readFromDataSource(source, closedWorld);
37+
source.end(tag);
38+
return new SetTypeMask(
39+
forwardTo, allocationNode, allocationElement, elementType);
40+
}
41+
42+
/// Serializes this [SetTypeMask] to [sink].
43+
@override
44+
void writeToDataSink(DataSink sink) {
45+
sink.writeEnum(TypeMaskKind.set);
46+
sink.begin(tag);
47+
forwardTo.writeToDataSink(sink);
48+
sink.writeTreeNodeOrNull(allocationNode);
49+
sink.writeMemberOrNull(allocationElement);
50+
elementType.writeToDataSink(sink);
51+
sink.end(tag);
52+
}
53+
54+
@override
55+
TypeMask nullable() => isNullable
56+
? this
57+
: new SetTypeMask(
58+
forwardTo.nullable(), allocationNode, allocationElement, elementType);
59+
60+
@override
61+
TypeMask nonNullable() => isNullable
62+
? new SetTypeMask(forwardTo.nonNullable(), allocationNode,
63+
allocationElement, elementType)
64+
: this;
65+
66+
@override
67+
bool get isSet => true;
68+
69+
@override
70+
bool get isExact => true;
71+
72+
@override
73+
bool equalsDisregardNull(other) {
74+
if (other is! SetTypeMask) return false;
75+
return super.equalsDisregardNull(other) &&
76+
allocationNode == other.allocationNode &&
77+
elementType == other.elementType;
78+
}
79+
80+
@override
81+
TypeMask intersection(TypeMask other, JClosedWorld closedWorld) {
82+
TypeMask forwardIntersection = forwardTo.intersection(other, closedWorld);
83+
if (forwardIntersection.isEmptyOrNull) return forwardIntersection;
84+
return forwardIntersection.isNullable ? nullable() : nonNullable();
85+
}
86+
87+
@override
88+
TypeMask union(dynamic other, JClosedWorld closedWorld) {
89+
if (this == other) {
90+
return this;
91+
} else if (equalsDisregardNull(other)) {
92+
return other.isNullable ? other : this;
93+
} else if (other.isEmptyOrNull) {
94+
return other.isNullable ? this.nullable() : this;
95+
} else if (other.isSet &&
96+
elementType != null &&
97+
other.elementType != null) {
98+
TypeMask newElementType =
99+
elementType.union(other.elementType, closedWorld);
100+
TypeMask newForwardTo = forwardTo.union(other.forwardTo, closedWorld);
101+
return new SetTypeMask(newForwardTo, null, null, newElementType);
102+
} else {
103+
return forwardTo.union(other, closedWorld);
104+
}
105+
}
106+
107+
@override
108+
bool operator ==(other) => super == other;
109+
110+
@override
111+
int get hashCode =>
112+
computeHashCode(allocationNode, isNullable, elementType, forwardTo);
113+
114+
@override
115+
String toString() => 'Set($forwardTo, element: $elementType)';
116+
}

pkg/compiler/lib/src/inferrer/typemasks/type_mask.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ enum TypeMaskKind {
9898
flat,
9999
union,
100100
container,
101+
set,
101102
map,
102103
dictionary,
103104
value,
@@ -230,6 +231,8 @@ abstract class TypeMask implements AbstractValue {
230231
return new UnionTypeMask.readFromDataSource(source, closedWorld);
231232
case TypeMaskKind.container:
232233
return new ContainerTypeMask.readFromDataSource(source, closedWorld);
234+
case TypeMaskKind.set:
235+
return new SetTypeMask.readFromDataSource(source, closedWorld);
233236
case TypeMaskKind.map:
234237
return new MapTypeMask.readFromDataSource(source, closedWorld);
235238
case TypeMaskKind.dictionary:

tests/compiler/dart2js/analyses/dart2js_allowed.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@
9090
"Dynamic access of 'allocationNode'.": 1,
9191
"Dynamic access of 'allocationElement'.": 1
9292
},
93+
"pkg/compiler/lib/src/inferrer/typemasks/set_type_mask.dart": {
94+
"Dynamic access of 'isNullable'.": 2,
95+
"Dynamic access of 'isEmptyOrNull'.": 1,
96+
"Dynamic access of 'isSet'.": 1,
97+
"Dynamic access of 'elementType'.": 2,
98+
"Dynamic access of 'forwardTo'.": 1
99+
},
93100
"pkg/compiler/lib/src/inferrer/typemasks/type_mask.dart": {
94101
"Dynamic access of 'isForwarding'.": 1,
95102
"Dynamic access of 'forwardTo'.": 1
@@ -278,4 +285,4 @@
278285
"Dynamic access of 'superclass'.": 1,
279286
"Dynamic access of 'needsTearOff'.": 1
280287
}
281-
}
288+
}

0 commit comments

Comments
 (0)